1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * Copyright (c) 2012
3cabdff1aSopenharmony_ci *      MIPS Technologies, Inc., California.
4cabdff1aSopenharmony_ci *
5cabdff1aSopenharmony_ci * Redistribution and use in source and binary forms, with or without
6cabdff1aSopenharmony_ci * modification, are permitted provided that the following conditions
7cabdff1aSopenharmony_ci * are met:
8cabdff1aSopenharmony_ci * 1. Redistributions of source code must retain the above copyright
9cabdff1aSopenharmony_ci *    notice, this list of conditions and the following disclaimer.
10cabdff1aSopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
11cabdff1aSopenharmony_ci *    notice, this list of conditions and the following disclaimer in the
12cabdff1aSopenharmony_ci *    documentation and/or other materials provided with the distribution.
13cabdff1aSopenharmony_ci * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
14cabdff1aSopenharmony_ci *    contributors may be used to endorse or promote products derived from
15cabdff1aSopenharmony_ci *    this software without specific prior written permission.
16cabdff1aSopenharmony_ci *
17cabdff1aSopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
18cabdff1aSopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19cabdff1aSopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20cabdff1aSopenharmony_ci * ARE DISCLAIMED.  IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
21cabdff1aSopenharmony_ci * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22cabdff1aSopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23cabdff1aSopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24cabdff1aSopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25cabdff1aSopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26cabdff1aSopenharmony_ci * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27cabdff1aSopenharmony_ci * SUCH DAMAGE.
28cabdff1aSopenharmony_ci *
29cabdff1aSopenharmony_ci * Author:  Nedeljko Babic (nedeljko.babic imgtec com)
30cabdff1aSopenharmony_ci *
31cabdff1aSopenharmony_ci * This file is part of FFmpeg.
32cabdff1aSopenharmony_ci *
33cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
34cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
35cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
36cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
37cabdff1aSopenharmony_ci *
38cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
39cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
40cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
41cabdff1aSopenharmony_ci * Lesser General Public License for more details.
42cabdff1aSopenharmony_ci *
43cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
44cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
45cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
46cabdff1aSopenharmony_ci */
47cabdff1aSopenharmony_ci
48cabdff1aSopenharmony_ci#include "common.h"
49cabdff1aSopenharmony_ci#include "fixed_dsp.h"
50cabdff1aSopenharmony_ci
51cabdff1aSopenharmony_cistatic void vector_fmul_add_c(int *dst, const int *src0, const int *src1, const int *src2, int len){
52cabdff1aSopenharmony_ci    int i;
53cabdff1aSopenharmony_ci    int64_t accu;
54cabdff1aSopenharmony_ci
55cabdff1aSopenharmony_ci    for (i=0; i<len; i++) {
56cabdff1aSopenharmony_ci        accu = (int64_t)src0[i] * src1[i];
57cabdff1aSopenharmony_ci        dst[i] = src2[i] + (int)((accu + 0x40000000) >> 31);
58cabdff1aSopenharmony_ci    }
59cabdff1aSopenharmony_ci}
60cabdff1aSopenharmony_ci
61cabdff1aSopenharmony_cistatic void vector_fmul_reverse_c(int *dst, const int *src0, const int *src1, int len)
62cabdff1aSopenharmony_ci{
63cabdff1aSopenharmony_ci    int i;
64cabdff1aSopenharmony_ci    int64_t accu;
65cabdff1aSopenharmony_ci
66cabdff1aSopenharmony_ci    src1 += len-1;
67cabdff1aSopenharmony_ci    for (i=0; i<len; i++) {
68cabdff1aSopenharmony_ci        accu = (int64_t)src0[i] * src1[-i];
69cabdff1aSopenharmony_ci        dst[i] = (int)((accu+0x40000000) >> 31);
70cabdff1aSopenharmony_ci    }
71cabdff1aSopenharmony_ci}
72cabdff1aSopenharmony_ci
73cabdff1aSopenharmony_cistatic void vector_fmul_window_scaled_c(int16_t *dst, const int32_t *src0,
74cabdff1aSopenharmony_ci                                       const int32_t *src1, const int32_t *win,
75cabdff1aSopenharmony_ci                                       int len, uint8_t bits)
76cabdff1aSopenharmony_ci{
77cabdff1aSopenharmony_ci    int32_t s0, s1, wi, wj, i,j, round;
78cabdff1aSopenharmony_ci
79cabdff1aSopenharmony_ci    dst += len;
80cabdff1aSopenharmony_ci    win += len;
81cabdff1aSopenharmony_ci    src0+= len;
82cabdff1aSopenharmony_ci    round = bits? 1 << (bits-1) : 0;
83cabdff1aSopenharmony_ci
84cabdff1aSopenharmony_ci    for (i=-len, j=len-1; i<0; i++, j--) {
85cabdff1aSopenharmony_ci        s0 = src0[i];
86cabdff1aSopenharmony_ci        s1 = src1[j];
87cabdff1aSopenharmony_ci        wi = win[i];
88cabdff1aSopenharmony_ci        wj = win[j];
89cabdff1aSopenharmony_ci        dst[i] = av_clip_int16(((((int64_t)s0*wj - (int64_t)s1*wi + 0x40000000) >> 31) + round) >> bits);
90cabdff1aSopenharmony_ci        dst[j] = av_clip_int16(((((int64_t)s0*wi + (int64_t)s1*wj + 0x40000000) >> 31) + round) >> bits);
91cabdff1aSopenharmony_ci    }
92cabdff1aSopenharmony_ci}
93cabdff1aSopenharmony_ci
94cabdff1aSopenharmony_cistatic void vector_fmul_window_c(int32_t *dst, const int32_t *src0,
95cabdff1aSopenharmony_ci                                       const int32_t *src1, const int32_t *win,
96cabdff1aSopenharmony_ci                                       int len)
97cabdff1aSopenharmony_ci{
98cabdff1aSopenharmony_ci    int32_t s0, s1, wi, wj, i, j;
99cabdff1aSopenharmony_ci
100cabdff1aSopenharmony_ci    dst += len;
101cabdff1aSopenharmony_ci    win += len;
102cabdff1aSopenharmony_ci    src0+= len;
103cabdff1aSopenharmony_ci
104cabdff1aSopenharmony_ci    for (i=-len, j=len-1; i<0; i++, j--) {
105cabdff1aSopenharmony_ci        s0 = src0[i];
106cabdff1aSopenharmony_ci        s1 = src1[j];
107cabdff1aSopenharmony_ci        wi = win[i];
108cabdff1aSopenharmony_ci        wj = win[j];
109cabdff1aSopenharmony_ci        dst[i] = ((int64_t)s0*wj - (int64_t)s1*wi + 0x40000000) >> 31;
110cabdff1aSopenharmony_ci        dst[j] = ((int64_t)s0*wi + (int64_t)s1*wj + 0x40000000) >> 31;
111cabdff1aSopenharmony_ci    }
112cabdff1aSopenharmony_ci}
113cabdff1aSopenharmony_ci
114cabdff1aSopenharmony_cistatic void vector_fmul_c(int *dst, const int *src0, const int *src1, int len)
115cabdff1aSopenharmony_ci{
116cabdff1aSopenharmony_ci    int i;
117cabdff1aSopenharmony_ci    int64_t accu;
118cabdff1aSopenharmony_ci
119cabdff1aSopenharmony_ci    for (i = 0; i < len; i++){
120cabdff1aSopenharmony_ci        accu = (int64_t)src0[i] * src1[i];
121cabdff1aSopenharmony_ci        dst[i] = (int)((accu+0x40000000) >> 31);
122cabdff1aSopenharmony_ci    }
123cabdff1aSopenharmony_ci}
124cabdff1aSopenharmony_ci
125cabdff1aSopenharmony_cistatic int scalarproduct_fixed_c(const int *v1, const int *v2, int len)
126cabdff1aSopenharmony_ci{
127cabdff1aSopenharmony_ci    /** p is initialized with 0x40000000 so that the proper rounding will occur
128cabdff1aSopenharmony_ci      * at the end */
129cabdff1aSopenharmony_ci    int64_t p = 0x40000000;
130cabdff1aSopenharmony_ci    int i;
131cabdff1aSopenharmony_ci
132cabdff1aSopenharmony_ci    for (i = 0; i < len; i++)
133cabdff1aSopenharmony_ci        p += (int64_t)v1[i] * v2[i];
134cabdff1aSopenharmony_ci
135cabdff1aSopenharmony_ci    return (int)(p >> 31);
136cabdff1aSopenharmony_ci}
137cabdff1aSopenharmony_ci
138cabdff1aSopenharmony_cistatic void butterflies_fixed_c(int *v1s, int *v2, int len)
139cabdff1aSopenharmony_ci{
140cabdff1aSopenharmony_ci    int i;
141cabdff1aSopenharmony_ci    unsigned int *v1 = v1s;
142cabdff1aSopenharmony_ci
143cabdff1aSopenharmony_ci    for (i = 0; i < len; i++){
144cabdff1aSopenharmony_ci        int t = v1[i] - v2[i];
145cabdff1aSopenharmony_ci        v1[i] += v2[i];
146cabdff1aSopenharmony_ci        v2[i] = t;
147cabdff1aSopenharmony_ci    }
148cabdff1aSopenharmony_ci}
149cabdff1aSopenharmony_ci
150cabdff1aSopenharmony_ciAVFixedDSPContext * avpriv_alloc_fixed_dsp(int bit_exact)
151cabdff1aSopenharmony_ci{
152cabdff1aSopenharmony_ci    AVFixedDSPContext * fdsp = av_malloc(sizeof(AVFixedDSPContext));
153cabdff1aSopenharmony_ci
154cabdff1aSopenharmony_ci    if (!fdsp)
155cabdff1aSopenharmony_ci        return NULL;
156cabdff1aSopenharmony_ci
157cabdff1aSopenharmony_ci    fdsp->vector_fmul_window_scaled = vector_fmul_window_scaled_c;
158cabdff1aSopenharmony_ci    fdsp->vector_fmul_window = vector_fmul_window_c;
159cabdff1aSopenharmony_ci    fdsp->vector_fmul = vector_fmul_c;
160cabdff1aSopenharmony_ci    fdsp->vector_fmul_add = vector_fmul_add_c;
161cabdff1aSopenharmony_ci    fdsp->vector_fmul_reverse = vector_fmul_reverse_c;
162cabdff1aSopenharmony_ci    fdsp->butterflies_fixed = butterflies_fixed_c;
163cabdff1aSopenharmony_ci    fdsp->scalarproduct_fixed = scalarproduct_fixed_c;
164cabdff1aSopenharmony_ci
165cabdff1aSopenharmony_ci#if ARCH_X86
166cabdff1aSopenharmony_ci    ff_fixed_dsp_init_x86(fdsp);
167cabdff1aSopenharmony_ci#endif
168cabdff1aSopenharmony_ci
169cabdff1aSopenharmony_ci    return fdsp;
170cabdff1aSopenharmony_ci}
171