Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/celt/mdct.c
Line
Count
Source
1
/* Copyright (c) 2007-2008 CSIRO
2
   Copyright (c) 2007-2008 Xiph.Org Foundation
3
   Written by Jean-Marc Valin */
4
/*
5
   Redistribution and use in source and binary forms, with or without
6
   modification, are permitted provided that the following conditions
7
   are met:
8
9
   - Redistributions of source code must retain the above copyright
10
   notice, this list of conditions and the following disclaimer.
11
12
   - Redistributions in binary form must reproduce the above copyright
13
   notice, this list of conditions and the following disclaimer in the
14
   documentation and/or other materials provided with the distribution.
15
16
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20
   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
*/
28
29
/* This is a simple MDCT implementation that uses a N/4 complex FFT
30
   to do most of the work. It should be relatively straightforward to
31
   plug in pretty much and FFT here.
32
33
   This replaces the Vorbis FFT (and uses the exact same API), which
34
   was a bit too messy and that was ending up duplicating code
35
   (might as well use the same FFT everywhere).
36
37
   The algorithm is similar to (and inspired from) Fabrice Bellard's
38
   MDCT implementation in FFMPEG, but has differences in signs, ordering
39
   and scaling in many places.
40
*/
41
42
#ifndef SKIP_CONFIG_H
43
#ifdef HAVE_CONFIG_H
44
#include "config.h"
45
#endif
46
#endif
47
48
#include "mdct.h"
49
#include "kiss_fft.h"
50
#include "_kiss_fft_guts.h"
51
#include <math.h>
52
#include "os_support.h"
53
#include "mathops.h"
54
#include "stack_alloc.h"
55
56
#if defined(FIXED_POINT) && defined(__mips) && __mips == 32
57
#include "mips/mdct_mipsr1.h"
58
#endif
59
60
#ifndef M_PI
61
#define M_PI 3.141592653
62
#endif
63
64
#ifdef CUSTOM_MODES
65
66
int clt_mdct_init(mdct_lookup *l,int N, int maxshift, int arch)
67
{
68
   int i;
69
   kiss_twiddle_scalar *trig;
70
   int shift;
71
   int N2=N>>1;
72
   l->n = N;
73
   l->maxshift = maxshift;
74
   for (i=0;i<=maxshift;i++)
75
   {
76
      if (i==0)
77
         l->kfft[i] = opus_fft_alloc(N>>2>>i, 0, 0, arch);
78
      else
79
         l->kfft[i] = opus_fft_alloc_twiddles(N>>2>>i, 0, 0, l->kfft[0], arch);
80
#ifndef ENABLE_TI_DSPLIB55
81
      if (l->kfft[i]==NULL)
82
         return 0;
83
#endif
84
   }
85
   l->trig = trig = (kiss_twiddle_scalar*)opus_alloc((N-(N2>>maxshift))*sizeof(kiss_twiddle_scalar));
86
   if (l->trig==NULL)
87
     return 0;
88
   for (shift=0;shift<=maxshift;shift++)
89
   {
90
      int N4 = N2 >> 1;
91
      /* Interleave -sine (imag) and cosine (real) as complex pairs */
92
#if defined(FIXED_POINT)
93
#ifndef ENABLE_QEXT
94
      for (i=0;i<N4;i++) {
95
         trig[2*i] = TRIG_UPSCALE*celt_cos_norm(DIV32(ADD32(SHL32(EXTEND32(i+N4),17),N2+16384),N));
96
         trig[2*i+1] = TRIG_UPSCALE*celt_cos_norm(DIV32(ADD32(SHL32(EXTEND32(i),17),N2+16384),N));
97
      }
98
#else
99
      for (i=0;i<N4;i++) {
100
         trig[2*i] = (kiss_twiddle_scalar)MAX32(-2147483647,MIN32(2147483647,floor(.5-2147483648*sin(2*M_PI*(i+.125)/N))));
101
         trig[2*i+1] = (kiss_twiddle_scalar)MAX32(-2147483647,MIN32(2147483647,floor(.5+2147483648*cos(2*M_PI*(i+.125)/N))));
102
      }
103
#endif
104
#else
105
      for (i=0;i<N4;i++) {
106
         trig[2*i] = (kiss_twiddle_scalar)(-sin(2*PI*(i+.125)/N));
107
         trig[2*i+1] = (kiss_twiddle_scalar)cos(2*PI*(i+.125)/N);
108
      }
109
#endif
110
      trig += N2;
111
      N2 >>= 1;
112
      N >>= 1;
113
   }
114
   return 1;
115
}
116
117
void clt_mdct_clear(mdct_lookup *l, int arch)
118
{
119
   int i;
120
   for (i=0;i<=l->maxshift;i++)
121
      opus_fft_free(l->kfft[i], arch);
122
   opus_free((kiss_twiddle_scalar*)l->trig);
123
}
124
125
#endif /* CUSTOM_MODES */
126
127
/* Forward MDCT trashes the input array */
128
#ifndef OVERRIDE_clt_mdct_forward
129
void clt_mdct_forward_c(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar * OPUS_RESTRICT out,
130
      const celt_coef *window, int overlap, int shift, int stride, int arch)
131
0
{
132
0
   int i;
133
0
   int N, N2, N4;
134
0
   VARDECL(kiss_fft_scalar, f);
135
0
   VARDECL(kiss_fft_cpx, f2);
136
0
   const kiss_fft_state *st = l->kfft[shift];
137
0
   const kiss_twiddle_scalar *trig;
138
0
   celt_coef scale;
139
#ifdef FIXED_POINT
140
   /* Allows us to scale with MULT16_32_Q16(), which is faster than
141
      MULT16_32_Q15() on ARM. */
142
   int scale_shift = st->scale_shift-1;
143
   int headroom = 0;
144
#endif
145
0
   SAVE_STACK;
146
0
   (void)arch;
147
0
   scale = st->scale;
148
149
0
   N = l->n;
150
0
   trig = l->trig;
151
0
   for (i=0;i<shift;i++)
152
0
   {
153
0
      N >>= 1;
154
0
      trig += N;
155
0
   }
156
0
   N2 = N>>1;
157
0
   N4 = N>>2;
158
159
0
   ALLOC(f, N2, kiss_fft_scalar);
160
0
   ALLOC(f2, N4, kiss_fft_cpx);
161
162
   /* Consider the input to be composed of four blocks: [a, b, c, d] */
163
   /* Window, shuffle, fold */
164
0
   {
165
      /* Temp pointers to make it really clear to the compiler what we're doing */
166
0
      const kiss_fft_scalar * OPUS_RESTRICT xp1 = in+(overlap>>1);
167
0
      const kiss_fft_scalar * OPUS_RESTRICT xp2 = in+N2-1+(overlap>>1);
168
0
      kiss_fft_scalar * OPUS_RESTRICT yp = f;
169
0
      const celt_coef * OPUS_RESTRICT wp1 = window+(overlap>>1);
170
0
      const celt_coef * OPUS_RESTRICT wp2 = window+(overlap>>1)-1;
171
0
      for(i=0;i<((overlap+3)>>2);i++)
172
0
      {
173
         /* Real part arranged as -d-cR, Imag part arranged as -b+aR*/
174
0
         *yp++ = S_MUL(xp1[N2], *wp2) + S_MUL(*xp2, *wp1);
175
0
         *yp++ = S_MUL(*xp1, *wp1)    - S_MUL(xp2[-N2], *wp2);
176
0
         xp1+=2;
177
0
         xp2-=2;
178
0
         wp1+=2;
179
0
         wp2-=2;
180
0
      }
181
0
      wp1 = window;
182
0
      wp2 = window+overlap-1;
183
0
      for(;i<N4-((overlap+3)>>2);i++)
184
0
      {
185
         /* Real part arranged as a-bR, Imag part arranged as -c-dR */
186
0
         *yp++ = *xp2;
187
0
         *yp++ = *xp1;
188
0
         xp1+=2;
189
0
         xp2-=2;
190
0
      }
191
0
      for(;i<N4;i++)
192
0
      {
193
         /* Real part arranged as a-bR, Imag part arranged as -c-dR */
194
0
         *yp++ =  -S_MUL(xp1[-N2], *wp1) + S_MUL(*xp2, *wp2);
195
0
         *yp++ = S_MUL(*xp1, *wp2)     + S_MUL(xp2[N2], *wp1);
196
0
         xp1+=2;
197
0
         xp2-=2;
198
0
         wp1+=2;
199
0
         wp2-=2;
200
0
      }
201
0
   }
202
   /* Pre-rotation */
203
0
   {
204
0
      kiss_fft_scalar * OPUS_RESTRICT yp = f;
205
0
      const kiss_twiddle_scalar *t = &trig[0];
206
#ifdef FIXED_POINT
207
      opus_val32 maxval=1;
208
#endif
209
0
      for(i=0;i<N4;i++)
210
0
      {
211
0
         kiss_fft_cpx yc;
212
0
         kiss_twiddle_scalar t0, t1;
213
0
         kiss_fft_scalar re, im, yr, yi;
214
0
         t0 = t[2*i];     /* -sin */
215
0
         t1 = t[2*i+1];   /* cos */
216
0
         re = *yp++;
217
0
         im = *yp++;
218
0
         yr = S_MUL(re,t1)  -  S_MUL(im,t0);
219
0
         yi = S_MUL(im,t1)  +  S_MUL(re,t0);
220
         /* For QEXT, it's best to scale before the FFT, but otherwise it's best to scale after.
221
            For floating-point it doesn't matter. */
222
0
#ifndef ENABLE_QEXT
223
0
         yc.r = S_MUL2(yr, scale);
224
0
         yc.i = S_MUL2(yi, scale);
225
#else
226
         yc.r = yr;
227
         yc.i = yi;
228
#endif
229
#ifdef FIXED_POINT
230
         maxval = MAX32(maxval, MAX32(ABS32(yc.r), ABS32(yc.i)));
231
#endif
232
#if defined(ENABLE_PFA)
233
         f2[i] = yc;
234
#else
235
0
         f2[st->bitrev[i]] = yc;
236
0
#endif
237
0
      }
238
#ifdef FIXED_POINT
239
      headroom = IMAX(0, IMIN(scale_shift, 28-celt_ilog2(maxval)));
240
#endif
241
0
   }
242
243
   /* N/4 complex FFT, does not downscale anymore */
244
#if defined(ENABLE_PFA)
245
   opus_fft_pfa_c(st, f2, f2 ARG_FIXED(scale_shift-headroom));
246
#else
247
0
   opus_fft_impl(st, f2 ARG_FIXED(scale_shift-headroom));
248
0
#endif
249
250
   /* Post-rotate */
251
0
   {
252
      /* Temp pointers to make it really clear to the compiler what we're doing */
253
0
      const kiss_fft_cpx * OPUS_RESTRICT fp = f2;
254
0
      kiss_fft_scalar * OPUS_RESTRICT yp1 = out;
255
0
      kiss_fft_scalar * OPUS_RESTRICT yp2 = out+stride*(N2-1);
256
0
      const kiss_twiddle_scalar *t = &trig[0];
257
      /* Temp pointers to make it really clear to the compiler what we're doing */
258
0
      for(i=0;i<N4;i++)
259
0
      {
260
0
         kiss_fft_scalar yr, yi;
261
0
         kiss_fft_scalar t0, t1;
262
#ifdef ENABLE_QEXT
263
         t0 = S_MUL2(t[2*i], scale);
264
         t1 = S_MUL2(t[2*i+1], scale);
265
#else
266
0
         t0 = t[2*i];
267
0
         t1 = t[2*i+1];
268
0
#endif
269
0
         yr = PSHR32(S_MUL(fp->i,t0) - S_MUL(fp->r,t1), headroom);
270
0
         yi = PSHR32(S_MUL(fp->r,t0) + S_MUL(fp->i,t1), headroom);
271
0
         *yp1 = yr;
272
0
         *yp2 = yi;
273
0
         fp++;
274
0
         yp1 += 2*stride;
275
0
         yp2 -= 2*stride;
276
0
      }
277
0
   }
278
0
   RESTORE_STACK;
279
0
}
280
#endif /* OVERRIDE_clt_mdct_forward */
281
282
#ifndef OVERRIDE_clt_mdct_backward
283
void clt_mdct_backward_c(const mdct_lookup *l, kiss_fft_scalar *in, kiss_fft_scalar * OPUS_RESTRICT out,
284
      const celt_coef * OPUS_RESTRICT window, int overlap, int shift, int stride, int arch)
285
325k
{
286
325k
   int i;
287
325k
   int N, N2, N4;
288
325k
   const kiss_twiddle_scalar *trig;
289
#ifdef FIXED_POINT
290
   int pre_shift, post_shift, fft_shift;
291
#endif
292
325k
   (void) arch;
293
294
325k
   N = l->n;
295
325k
   trig = l->trig;
296
788k
   for (i=0;i<shift;i++)
297
462k
   {
298
462k
      N >>= 1;
299
462k
      trig += N;
300
462k
   }
301
325k
   N2 = N>>1;
302
325k
   N4 = N>>2;
303
304
#ifdef FIXED_POINT
305
   {
306
      opus_val32 sumval=N2;
307
      opus_val32 maxval=0;
308
      for (i=0;i<N2;i++) {
309
         maxval = MAX32(maxval, ABS32(in[i*stride]));
310
         sumval = ADD32_ovflw(sumval, ABS32(SHR32(in[i*stride],11)));
311
      }
312
      pre_shift = IMAX(0, 29-celt_zlog2(1+maxval));
313
      /* Worst-case where all the energy goes to a single sample. */
314
      post_shift = IMAX(0, 19-celt_ilog2(ABS32(sumval)));
315
      post_shift = IMIN(post_shift, pre_shift);
316
      fft_shift = pre_shift - post_shift;
317
   }
318
#endif
319
   /* Pre-rotate */
320
325k
   {
321
      /* Temp pointers to make it really clear to the compiler what we're doing */
322
325k
      const kiss_fft_scalar * OPUS_RESTRICT xp1 = in;
323
325k
      const kiss_fft_scalar * OPUS_RESTRICT xp2 = in+stride*(N2-1);
324
325k
      kiss_fft_scalar * OPUS_RESTRICT yp = out+(overlap>>1);
325
325k
      const kiss_twiddle_scalar * OPUS_RESTRICT t = &trig[0];
326
325k
      const opus_int16 * OPUS_RESTRICT bitrev = l->kfft[shift]->bitrev;
327
72.2M
      for(i=0;i<N4;i++)
328
71.8M
      {
329
#if defined(ENABLE_PFA)
330
         kiss_fft_scalar yr, yi;
331
         opus_val32 x1, x2;
332
         x1 = SHL32_ovflw(*xp1, pre_shift);
333
         x2 = SHL32_ovflw(*xp2, pre_shift);
334
         yr = ADD32_ovflw(S_MUL(x2, t[2*i+1]), S_MUL(x1, t[2*i]));
335
         yi = SUB32_ovflw(S_MUL(x1, t[2*i+1]), S_MUL(x2, t[2*i]));
336
         (void)bitrev;
337
         yp[2*i+1] = yr;
338
         yp[2*i] = yi;
339
#else
340
71.8M
         {
341
71.8M
            int rev = *bitrev++;
342
71.8M
            kiss_fft_scalar yr, yi;
343
71.8M
            opus_val32 x1, x2;
344
71.8M
            x1 = SHL32_ovflw(*xp1, pre_shift);
345
71.8M
            x2 = SHL32_ovflw(*xp2, pre_shift);
346
71.8M
            yr = ADD32_ovflw(S_MUL(x2, t[2*i+1]), S_MUL(x1, t[2*i]));
347
71.8M
            yi = SUB32_ovflw(S_MUL(x1, t[2*i+1]), S_MUL(x2, t[2*i]));
348
71.8M
            yp[2*rev+1] = yr;
349
71.8M
            yp[2*rev] = yi;
350
71.8M
         }
351
71.8M
#endif
352
         /* Storing the pre-rotation directly in the bitrev order. */
353
71.8M
         xp1+=2*stride;
354
71.8M
         xp2-=2*stride;
355
71.8M
      }
356
325k
   }
357
358
#if defined(ENABLE_PFA)
359
   opus_fft_pfa_c(l->kfft[shift], (kiss_fft_cpx*)(out+(overlap>>1)), (kiss_fft_cpx*)(out+(overlap>>1)) ARG_FIXED(fft_shift));
360
#else
361
325k
   opus_fft_impl(l->kfft[shift], (kiss_fft_cpx*)(out+(overlap>>1)) ARG_FIXED(fft_shift));
362
325k
#endif
363
364
   /* Post-rotate and de-shuffle from both ends of the buffer at once to make
365
      it in-place. */
366
325k
   {
367
325k
      kiss_fft_scalar * yp0 = out+(overlap>>1);
368
325k
      kiss_fft_scalar * yp1 = out+(overlap>>1)+N2-2;
369
325k
      const kiss_twiddle_scalar *t = &trig[0];
370
      /* Loop to (N4+1)>>1 to handle odd N4. When N4 is odd, the
371
         middle pair will be computed twice. */
372
36.2M
      for(i=0;i<(N4+1)>>1;i++)
373
35.9M
      {
374
35.9M
         kiss_fft_scalar re, im, yr, yi;
375
35.9M
         kiss_twiddle_scalar t0, t1;
376
         /* We swap real and imag because we're using an FFT instead of an IFFT. */
377
35.9M
         re = yp0[1];
378
35.9M
         im = yp0[0];
379
35.9M
         t0 = t[2*i];     /* -sin */
380
35.9M
         t1 = t[2*i+1];   /* cos */
381
         /* We'd scale up by 2 here, but instead it's done when mixing the windows */
382
35.9M
         yr = PSHR32_ovflw(ADD32_ovflw(S_MUL(re,t1), S_MUL(im,t0)), post_shift);
383
35.9M
         yi = PSHR32_ovflw(SUB32_ovflw(S_MUL(re,t0), S_MUL(im,t1)), post_shift);
384
         /* We swap real and imag because we're using an FFT instead of an IFFT. */
385
35.9M
         re = yp1[1];
386
35.9M
         im = yp1[0];
387
35.9M
         yp0[0] = yr;
388
35.9M
         yp1[1] = yi;
389
390
35.9M
         t0 = t[2*(N4-i-1)];     /* -sin */
391
35.9M
         t1 = t[2*(N4-i-1)+1];   /* cos */
392
         /* We'd scale up by 2 here, but instead it's done when mixing the windows */
393
35.9M
         yr = PSHR32_ovflw(ADD32_ovflw(S_MUL(re,t1), S_MUL(im,t0)), post_shift);
394
35.9M
         yi = PSHR32_ovflw(SUB32_ovflw(S_MUL(re,t0), S_MUL(im,t1)), post_shift);
395
35.9M
         yp1[0] = yr;
396
35.9M
         yp0[1] = yi;
397
35.9M
         yp0 += 2;
398
35.9M
         yp1 -= 2;
399
35.9M
      }
400
325k
   }
401
402
   /* Mirror on both sides for TDAC */
403
325k
   {
404
325k
      kiss_fft_scalar * OPUS_RESTRICT xp1 = out+overlap-1;
405
325k
      kiss_fft_scalar * OPUS_RESTRICT yp1 = out;
406
325k
      const celt_coef * OPUS_RESTRICT wp1 = window;
407
325k
      const celt_coef * OPUS_RESTRICT wp2 = window+overlap-1;
408
409
19.8M
      for(i = 0; i < overlap/2; i++)
410
19.5M
      {
411
19.5M
         kiss_fft_scalar x1, x2;
412
19.5M
         x1 = *xp1;
413
19.5M
         x2 = *yp1;
414
19.5M
         *yp1++ = SUB32_ovflw(S_MUL(x2, *wp2), S_MUL(x1, *wp1));
415
19.5M
         *xp1-- = ADD32_ovflw(S_MUL(x2, *wp1), S_MUL(x1, *wp2));
416
19.5M
         wp1++;
417
19.5M
         wp2--;
418
19.5M
      }
419
325k
   }
420
325k
}
421
#endif /* OVERRIDE_clt_mdct_backward */