Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/celt/celt.c
Line
Count
Source
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(FIXED_POINT) && defined(__mips)
58
#include "mips/celt_mipsr1.h"
59
#endif
60
61
62
int resampling_factor(opus_int32 rate)
63
51.7k
{
64
51.7k
   int ret;
65
51.7k
   switch (rate)
66
51.7k
   {
67
#ifdef ENABLE_QEXT
68
   case 96000:
69
#endif
70
51.7k
   case 48000:
71
51.7k
      ret = 1;
72
51.7k
      break;
73
0
   case 24000:
74
0
      ret = 2;
75
0
      break;
76
0
   case 16000:
77
0
      ret = 3;
78
0
      break;
79
0
   case 12000:
80
0
      ret = 4;
81
0
      break;
82
0
   case 8000:
83
0
      ret = 6;
84
0
      break;
85
0
   default:
86
0
#ifndef CUSTOM_MODES
87
0
      celt_assert(0);
88
0
#endif
89
0
      ret = 0;
90
0
      break;
91
51.7k
   }
92
51.7k
   return ret;
93
51.7k
}
94
95
96
#if !defined(OVERRIDE_COMB_FILTER_CONST) || defined(NON_STATIC_COMB_FILTER_CONST_C)
97
/* This version should be faster on ARM */
98
#ifdef OPUS_ARM_ASM
99
#ifndef NON_STATIC_COMB_FILTER_CONST_C
100
static
101
#endif
102
void comb_filter_const_c(opus_val32 *y, opus_val32 *x, int T, int N,
103
      celt_coef g10, celt_coef g11, celt_coef g12)
104
{
105
   opus_val32 x0, x1, x2, x3, x4;
106
   int i;
107
   x4 = SHL32(x[-T-2], 1);
108
   x3 = SHL32(x[-T-1], 1);
109
   x2 = SHL32(x[-T], 1);
110
   x1 = SHL32(x[-T+1], 1);
111
   for (i=0;i<N-4;i+=5)
112
   {
113
      opus_val32 t;
114
      x0=SHL32(x[i-T+2],1);
115
      t = MAC_COEF_32_ARM(x[i], g10, x2);
116
      t = MAC_COEF_32_ARM(t, g11, ADD32(x1,x3));
117
      t = MAC_COEF_32_ARM(t, g12, ADD32(x0,x4));
118
      t = SATURATE(t, SIG_SAT);
119
      y[i] = t;
120
      x4=SHL32(x[i-T+3],1);
121
      t = MAC_COEF_32_ARM(x[i+1], g10, x1);
122
      t = MAC_COEF_32_ARM(t, g11, ADD32(x0,x2));
123
      t = MAC_COEF_32_ARM(t, g12, ADD32(x4,x3));
124
      t = SATURATE(t, SIG_SAT);
125
      y[i+1] = t;
126
      x3=SHL32(x[i-T+4],1);
127
      t = MAC_COEF_32_ARM(x[i+2], g10, x0);
128
      t = MAC_COEF_32_ARM(t, g11, ADD32(x4,x1));
129
      t = MAC_COEF_32_ARM(t, g12, ADD32(x3,x2));
130
      t = SATURATE(t, SIG_SAT);
131
      y[i+2] = t;
132
      x2=SHL32(x[i-T+5],1);
133
      t = MAC_COEF_32_ARM(x[i+3], g10, x4);
134
      t = MAC_COEF_32_ARM(t, g11, ADD32(x3,x0));
135
      t = MAC_COEF_32_ARM(t, g12, ADD32(x2,x1));
136
      t = SATURATE(t, SIG_SAT);
137
      y[i+3] = t;
138
      x1=SHL32(x[i-T+6],1);
139
      t = MAC_COEF_32_ARM(x[i+4], g10, x3);
140
      t = MAC_COEF_32_ARM(t, g11, ADD32(x2,x4));
141
      t = MAC_COEF_32_ARM(t, g12, ADD32(x1,x0));
142
      t = SATURATE(t, SIG_SAT);
143
      y[i+4] = t;
144
   }
145
#ifdef CUSTOM_MODES
146
   for (;i<N;i++)
147
   {
148
      opus_val32 t;
149
      x0=SHL32(x[i-T+2],1);
150
      t = MAC_COEF_32_ARM(x[i], g10, x2);
151
      t = MAC_COEF_32_ARM(t, g11, ADD32(x1,x3));
152
      t = MAC_COEF_32_ARM(t, g12, ADD32(x0,x4));
153
      t = SATURATE(t, SIG_SAT);
154
      y[i] = t;
155
      x4=x3;
156
      x3=x2;
157
      x2=x1;
158
      x1=x0;
159
   }
160
#endif
161
}
162
#else
163
#ifndef NON_STATIC_COMB_FILTER_CONST_C
164
static
165
#endif
166
void comb_filter_const_c(opus_val32 *y, opus_val32 *x, int T, int N,
167
      celt_coef g10, celt_coef g11, celt_coef g12)
168
{
169
   opus_val32 x0, x1, x2, x3, x4;
170
   int i;
171
   x4 = x[-T-2];
172
   x3 = x[-T-1];
173
   x2 = x[-T];
174
   x1 = x[-T+1];
175
   for (i=0;i<N;i++)
176
   {
177
      x0=x[i-T+2];
178
      y[i] = x[i]
179
               + MULT_COEF_32(g10,x2)
180
               + MULT_COEF_32(g11,ADD32(x1,x3))
181
               + MULT_COEF_32(g12,ADD32(x0,x4));
182
#ifdef FIXED_POINT
183
      /* A bit of bias seems to help here. */
184
      y[i] = SUB32(y[i], 1);
185
#endif
186
      y[i] = SATURATE(y[i], SIG_SAT);
187
      x4=x3;
188
      x3=x2;
189
      x2=x1;
190
      x1=x0;
191
   }
192
193
}
194
#endif
195
#endif
196
197
#ifdef ENABLE_QEXT
198
static void comb_filter_qext(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
199
      opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
200
      const celt_coef *window, int overlap, int arch)
201
{
202
   VARDECL(opus_val32, mem_buf);
203
   VARDECL(opus_val32, buf);
204
   celt_coef new_window[120];
205
   int s;
206
   int i;
207
   int N2;
208
   int overlap2;
209
   SAVE_STACK;
210
   /* Using ALLOC() instead of a regular stack allocation to minimize real stack use when using the pseudostack.
211
      This is useful on some embedded systems. */
212
   ALLOC(mem_buf, COMBFILTER_MAXPERIOD+960, opus_val32);
213
   ALLOC(buf, COMBFILTER_MAXPERIOD+960, opus_val32);
214
   N2 = N/2;
215
   overlap2=overlap/2;
216
   /* At 96 kHz, we double the period and the spacing between taps, which is equivalent
217
      to creating a mirror image of the filter around 24 kHz. It also means we can process
218
      the even and odd samples completely independently. */
219
   for (s=0;s<2;s++) {
220
      opus_val32 *yptr;
221
      for (i=0;i<overlap2;i++) new_window[i] = window[2*i+s];
222
      for (i=0;i<COMBFILTER_MAXPERIOD+N2;i++) mem_buf[i] = x[2*i+s-2*COMBFILTER_MAXPERIOD];
223
      if (x==y) {
224
         yptr = mem_buf+COMBFILTER_MAXPERIOD;
225
      } else {
226
         for (i=0;i<N2;i++) buf[i] = y[2*i+s];
227
         yptr = buf;
228
      }
229
      comb_filter(yptr, mem_buf+COMBFILTER_MAXPERIOD, T0, T1, N2, g0, g1, tapset0, tapset1, new_window, overlap2, arch, 1);
230
      for (i=0;i<N2;i++) y[2*i+s] = yptr[i];
231
   }
232
   RESTORE_STACK;
233
   return;
234
}
235
#endif
236
237
#ifndef OVERRIDE_comb_filter
238
void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
239
      opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
240
      const celt_coef *window, int overlap, int arch ARG_QEXT(int qext_scale))
241
582k
{
242
582k
   int i;
243
   /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
244
582k
   celt_coef g00, g01, g02, g10, g11, g12;
245
582k
   opus_val32 x0, x1, x2, x3, x4;
246
582k
   static const opus_val16 gains[3][3] = {
247
582k
         {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
248
582k
         {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
249
582k
         {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
250
#ifdef ENABLE_QEXT
251
   /* The 96 kHz modes double the history buffers and halve the period, which
252
      is what the mirrored filter expects. */
253
   if (qext_scale == 2) {
254
      comb_filter_qext(y, x, T0, T1, N, g0, g1, tapset0, tapset1, window, overlap, arch);
255
      return;
256
   }
257
#endif
258
582k
   if (g0==0 && g1==0)
259
523k
   {
260
      /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
261
523k
      if (x!=y)
262
22.7k
         OPUS_MOVE(y, x, N);
263
523k
      return;
264
523k
   }
265
   /* When the gain is zero, T0 and/or T1 is set to zero. We need
266
      to have then be at least 2 to avoid processing garbage data. */
267
58.8k
   T0 = IMAX(T0, COMBFILTER_MINPERIOD);
268
58.8k
   T1 = IMAX(T1, COMBFILTER_MINPERIOD);
269
58.8k
   g00 = MULT_COEF_TAPS(g0, gains[tapset0][0]);
270
58.8k
   g01 = MULT_COEF_TAPS(g0, gains[tapset0][1]);
271
58.8k
   g02 = MULT_COEF_TAPS(g0, gains[tapset0][2]);
272
58.8k
   g10 = MULT_COEF_TAPS(g1, gains[tapset1][0]);
273
58.8k
   g11 = MULT_COEF_TAPS(g1, gains[tapset1][1]);
274
58.8k
   g12 = MULT_COEF_TAPS(g1, gains[tapset1][2]);
275
58.8k
   x1 = x[-T1+1];
276
58.8k
   x2 = x[-T1  ];
277
58.8k
   x3 = x[-T1-1];
278
58.8k
   x4 = x[-T1-2];
279
   /* If the filter didn't change, we don't need the overlap */
280
58.8k
   if (g0==g1 && T0==T1 && tapset0==tapset1)
281
31.0k
      overlap=0;
282
3.27M
   for (i=0;i<overlap;i++)
283
3.21M
   {
284
3.21M
      celt_coef f;
285
3.21M
      x0=x[i-T1+2];
286
3.21M
      f = MULT_COEF(window[i],window[i]);
287
3.21M
      y[i] = x[i]
288
3.21M
               + MULT_COEF_32(MULT_COEF((COEF_ONE-f),g00),x[i-T0])
289
3.21M
               + MULT_COEF_32(MULT_COEF((COEF_ONE-f),g01),ADD32(x[i-T0+1],x[i-T0-1]))
290
3.21M
               + MULT_COEF_32(MULT_COEF((COEF_ONE-f),g02),ADD32(x[i-T0+2],x[i-T0-2]))
291
3.21M
               + MULT_COEF_32(MULT_COEF(f,g10),x2)
292
3.21M
               + MULT_COEF_32(MULT_COEF(f,g11),ADD32(x1,x3))
293
3.21M
               + MULT_COEF_32(MULT_COEF(f,g12),ADD32(x0,x4));
294
#ifdef FIXED_POINT
295
      /* A bit of bias seems to help here. */
296
      y[i] = SUB32(y[i], 3);
297
#endif
298
3.21M
      y[i] = SATURATE(y[i], SIG_SAT);
299
3.21M
      x4=x3;
300
3.21M
      x3=x2;
301
3.21M
      x2=x1;
302
3.21M
      x1=x0;
303
304
3.21M
   }
305
58.8k
   if (g1==0)
306
9.30k
   {
307
      /* OPT: Happens to work without the OPUS_MOVE(), but only because the current encoder already copies x to y */
308
9.30k
      if (x!=y)
309
645
         OPUS_MOVE(y+overlap, x+overlap, N-overlap);
310
9.30k
      return;
311
9.30k
   }
312
313
   /* Compute the part with the constant filter. */
314
49.5k
   comb_filter_const(y+i, x+i, T1, N-i, g10, g11, g12, arch);
315
49.5k
}
316
#endif /* OVERRIDE_comb_filter */
317
318
/* TF change table. Positive values mean better frequency resolution (longer
319
   effective window), whereas negative values mean better time resolution
320
   (shorter effective window). The second index is computed as:
321
   4*isTransient + 2*tf_select + per_band_flag */
322
const signed char tf_select_table[4][8] = {
323
    /*isTransient=0     isTransient=1 */
324
      {0, -1, 0, -1,    0,-1, 0,-1}, /* 2.5 ms */
325
      {0, -1, 0, -2,    1, 0, 1,-1}, /* 5 ms */
326
      {0, -2, 0, -3,    2, 0, 1,-1}, /* 10 ms */
327
      {0, -2, 0, -3,    3, 0, 1,-1}, /* 20 ms */
328
};
329
330
331
void init_caps(const CELTMode *m,int *cap,int LM,int C)
332
82.4k
{
333
82.4k
   int i;
334
1.81M
   for (i=0;i<m->nbEBands;i++)
335
1.73M
   {
336
1.73M
      int N;
337
1.73M
      N=(m->eBands[i+1]-m->eBands[i])<<LM;
338
1.73M
      cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
339
1.73M
   }
340
82.4k
}
341
342
343
344
const char *opus_strerror(int error)
345
37.1k
{
346
37.1k
   static const char * const error_strings[8] = {
347
37.1k
      "success",
348
37.1k
      "invalid argument",
349
37.1k
      "buffer too small",
350
37.1k
      "internal error",
351
37.1k
      "corrupted stream",
352
37.1k
      "request not implemented",
353
37.1k
      "invalid state",
354
37.1k
      "memory allocation failed"
355
37.1k
   };
356
37.1k
   if (error > 0 || error < -7)
357
0
      return "unknown error";
358
37.1k
   else
359
37.1k
      return error_strings[-error];
360
37.1k
}
361
362
const char *opus_get_version_string(void)
363
0
{
364
0
    return "libopus " PACKAGE_VERSION
365
    /* Applications may rely on the presence of this substring in the version
366
       string to determine if they have a fixed-point or floating-point build
367
       at runtime. */
368
#ifdef FIXED_POINT
369
          "-fixed"
370
#endif
371
#ifdef FUZZING
372
          "-fuzzing"
373
#endif
374
0
          ;
375
0
}