• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2007-2008 CSIRO
2    Copyright (c) 2007-2010 Xiph.Org Foundation
3    Copyright (c) 2008 Gregory Maxwell
4    Written by Jean-Marc Valin and Gregory Maxwell */
5 /*
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9 
10    - Redistributions of source code must retain the above copyright
11    notice, this list of conditions and the following disclaimer.
12 
13    - Redistributions in binary form must reproduce the above copyright
14    notice, this list of conditions and the following disclaimer in the
15    documentation and/or other materials provided with the distribution.
16 
17    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #define CELT_C
35 
36 #include "os_support.h"
37 #include "mdct.h"
38 #include <math.h>
39 #include "celt.h"
40 #include "pitch.h"
41 #include "bands.h"
42 #include "modes.h"
43 #include "entcode.h"
44 #include "quant_bands.h"
45 #include "rate.h"
46 #include "stack_alloc.h"
47 #include "mathops.h"
48 #include "float_cast.h"
49 #include <stdarg.h>
50 #include "celt_lpc.h"
51 #include "vq.h"
52 
53 #ifndef PACKAGE_VERSION
54 #define PACKAGE_VERSION "unknown"
55 #endif
56 
57 #if defined(MIPSr1_ASM)
58 #include "mips/celt_mipsr1.h"
59 #endif
60 
61 
resampling_factor(opus_int32 rate)62 int resampling_factor(opus_int32 rate)
63 {
64    int ret;
65    switch (rate)
66    {
67    case 48000:
68       ret = 1;
69       break;
70    case 24000:
71       ret = 2;
72       break;
73    case 16000:
74       ret = 3;
75       break;
76    case 12000:
77       ret = 4;
78       break;
79    case 8000:
80       ret = 6;
81       break;
82    default:
83 #ifndef CUSTOM_MODES
84       celt_assert(0);
85 #endif
86       ret = 0;
87       break;
88    }
89    return ret;
90 }
91 
92 #if !defined(OVERRIDE_COMB_FILTER_CONST) || defined(NON_STATIC_COMB_FILTER_CONST_C)
93 /* This version should be faster on ARM */
94 #ifdef OPUS_ARM_ASM
95 #ifndef NON_STATIC_COMB_FILTER_CONST_C
96 static
97 #endif
comb_filter_const_c(opus_val32 * y,opus_val32 * x,int T,int N,opus_val16 g10,opus_val16 g11,opus_val16 g12)98 void comb_filter_const_c(opus_val32 *y, opus_val32 *x, int T, int N,
99       opus_val16 g10, opus_val16 g11, opus_val16 g12)
100 {
101    opus_val32 x0, x1, x2, x3, x4;
102    int i;
103    x4 = SHL32(x[-T-2], 1);
104    x3 = SHL32(x[-T-1], 1);
105    x2 = SHL32(x[-T], 1);
106    x1 = SHL32(x[-T+1], 1);
107    for (i=0;i<N-4;i+=5)
108    {
109       opus_val32 t;
110       x0=SHL32(x[i-T+2],1);
111       t = MAC16_32_Q16(x[i], g10, x2);
112       t = MAC16_32_Q16(t, g11, ADD32(x1,x3));
113       t = MAC16_32_Q16(t, g12, ADD32(x0,x4));
114       y[i] = t;
115       x4=SHL32(x[i-T+3],1);
116       t = MAC16_32_Q16(x[i+1], g10, x1);
117       t = MAC16_32_Q16(t, g11, ADD32(x0,x2));
118       t = MAC16_32_Q16(t, g12, ADD32(x4,x3));
119       y[i+1] = t;
120       x3=SHL32(x[i-T+4],1);
121       t = MAC16_32_Q16(x[i+2], g10, x0);
122       t = MAC16_32_Q16(t, g11, ADD32(x4,x1));
123       t = MAC16_32_Q16(t, g12, ADD32(x3,x2));
124       y[i+2] = t;
125       x2=SHL32(x[i-T+5],1);
126       t = MAC16_32_Q16(x[i+3], g10, x4);
127       t = MAC16_32_Q16(t, g11, ADD32(x3,x0));
128       t = MAC16_32_Q16(t, g12, ADD32(x2,x1));
129       y[i+3] = t;
130       x1=SHL32(x[i-T+6],1);
131       t = MAC16_32_Q16(x[i+4], g10, x3);
132       t = MAC16_32_Q16(t, g11, ADD32(x2,x4));
133       t = MAC16_32_Q16(t, g12, ADD32(x1,x0));
134       y[i+4] = t;
135    }
136 #ifdef CUSTOM_MODES
137    for (;i<N;i++)
138    {
139       opus_val32 t;
140       x0=SHL32(x[i-T+2],1);
141       t = MAC16_32_Q16(x[i], g10, x2);
142       t = MAC16_32_Q16(t, g11, ADD32(x1,x3));
143       t = MAC16_32_Q16(t, g12, ADD32(x0,x4));
144       y[i] = t;
145       x4=x3;
146       x3=x2;
147       x2=x1;
148       x1=x0;
149    }
150 #endif
151 }
152 #else
153 #ifndef NON_STATIC_COMB_FILTER_CONST_C
154 static
155 #endif
comb_filter_const_c(opus_val32 * y,opus_val32 * x,int T,int N,opus_val16 g10,opus_val16 g11,opus_val16 g12)156 void comb_filter_const_c(opus_val32 *y, opus_val32 *x, int T, int N,
157       opus_val16 g10, opus_val16 g11, opus_val16 g12)
158 {
159    opus_val32 x0, x1, x2, x3, x4;
160    int i;
161    x4 = x[-T-2];
162    x3 = x[-T-1];
163    x2 = x[-T];
164    x1 = x[-T+1];
165    for (i=0;i<N;i++)
166    {
167       x0=x[i-T+2];
168       y[i] = x[i]
169                + MULT16_32_Q15(g10,x2)
170                + MULT16_32_Q15(g11,ADD32(x1,x3))
171                + MULT16_32_Q15(g12,ADD32(x0,x4));
172       x4=x3;
173       x3=x2;
174       x2=x1;
175       x1=x0;
176    }
177 
178 }
179 #endif
180 #endif
181 
182 #ifndef OVERRIDE_comb_filter
comb_filter(opus_val32 * y,opus_val32 * x,int T0,int T1,int N,opus_val16 g0,opus_val16 g1,int tapset0,int tapset1,const opus_val16 * window,int overlap,int arch)183 void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
184       opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
185       const opus_val16 *window, int overlap, int arch)
186 {
187    int i;
188    /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
189    opus_val16 g00, g01, g02, g10, g11, g12;
190    opus_val32 x0, x1, x2, x3, x4;
191    static const opus_val16 gains[3][3] = {
192          {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
193          {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
194          {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
195 
196    if (g0==0 && g1==0)
197    {
198       /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
199       if (x!=y)
200          OPUS_MOVE(y, x, N);
201       return;
202    }
203    g00 = MULT16_16_P15(g0, gains[tapset0][0]);
204    g01 = MULT16_16_P15(g0, gains[tapset0][1]);
205    g02 = MULT16_16_P15(g0, gains[tapset0][2]);
206    g10 = MULT16_16_P15(g1, gains[tapset1][0]);
207    g11 = MULT16_16_P15(g1, gains[tapset1][1]);
208    g12 = MULT16_16_P15(g1, gains[tapset1][2]);
209    x1 = x[-T1+1];
210    x2 = x[-T1  ];
211    x3 = x[-T1-1];
212    x4 = x[-T1-2];
213    /* If the filter didn't change, we don't need the overlap */
214    if (g0==g1 && T0==T1 && tapset0==tapset1)
215       overlap=0;
216    for (i=0;i<overlap;i++)
217    {
218       opus_val16 f;
219       x0=x[i-T1+2];
220       f = MULT16_16_Q15(window[i],window[i]);
221       y[i] = x[i]
222                + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
223                + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),ADD32(x[i-T0+1],x[i-T0-1]))
224                + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),ADD32(x[i-T0+2],x[i-T0-2]))
225                + MULT16_32_Q15(MULT16_16_Q15(f,g10),x2)
226                + MULT16_32_Q15(MULT16_16_Q15(f,g11),ADD32(x1,x3))
227                + MULT16_32_Q15(MULT16_16_Q15(f,g12),ADD32(x0,x4));
228       x4=x3;
229       x3=x2;
230       x2=x1;
231       x1=x0;
232 
233    }
234    if (g1==0)
235    {
236       /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
237       if (x!=y)
238          OPUS_MOVE(y+overlap, x+overlap, N-overlap);
239       return;
240    }
241 
242    /* Compute the part with the constant filter. */
243    comb_filter_const(y+i, x+i, T1, N-i, g10, g11, g12, arch);
244 }
245 #endif /* OVERRIDE_comb_filter */
246 
247 const signed char tf_select_table[4][8] = {
248       {0, -1, 0, -1,    0,-1, 0,-1},
249       {0, -1, 0, -2,    1, 0, 1,-1},
250       {0, -2, 0, -3,    2, 0, 1,-1},
251       {0, -2, 0, -3,    3, 0, 1,-1},
252 };
253 
254 
init_caps(const CELTMode * m,int * cap,int LM,int C)255 void init_caps(const CELTMode *m,int *cap,int LM,int C)
256 {
257    int i;
258    for (i=0;i<m->nbEBands;i++)
259    {
260       int N;
261       N=(m->eBands[i+1]-m->eBands[i])<<LM;
262       cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
263    }
264 }
265 
266 
267 
opus_strerror(int error)268 const char *opus_strerror(int error)
269 {
270    static const char * const error_strings[8] = {
271       "success",
272       "invalid argument",
273       "buffer too small",
274       "internal error",
275       "corrupted stream",
276       "request not implemented",
277       "invalid state",
278       "memory allocation failed"
279    };
280    if (error > 0 || error < -7)
281       return "unknown error";
282    else
283       return error_strings[-error];
284 }
285 
opus_get_version_string(void)286 const char *opus_get_version_string(void)
287 {
288     return "libopus " PACKAGE_VERSION
289     /* Applications may rely on the presence of this substring in the version
290        string to determine if they have a fixed-point or floating-point build
291        at runtime. */
292 #ifdef FIXED_POINT
293           "-fixed"
294 #endif
295 #ifdef FUZZING
296           "-fuzzing"
297 #endif
298           ;
299 }
300