Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/celt/bands.c
Line
Count
Source
1
/* Copyright (c) 2007-2008 CSIRO
2
   Copyright (c) 2007-2009 Xiph.Org Foundation
3
   Copyright (c) 2008-2009 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
#include <math.h>
35
#include "bands.h"
36
#include "modes.h"
37
#include "vq.h"
38
#include "cwrs.h"
39
#include "stack_alloc.h"
40
#include "os_support.h"
41
#include "mathops.h"
42
#include "rate.h"
43
#include "quant_bands.h"
44
#include "pitch.h"
45
46
int hysteresis_decision(opus_val16 val, const opus_val16 *thresholds, const opus_val16 *hysteresis, int N, int prev)
47
0
{
48
0
   int i;
49
0
   for (i=0;i<N;i++)
50
0
   {
51
0
      if (val < thresholds[i])
52
0
         break;
53
0
   }
54
0
   if (i>prev && val < thresholds[prev]+hysteresis[prev])
55
0
      i=prev;
56
0
   if (i<prev && val > thresholds[prev-1]-hysteresis[prev-1])
57
0
      i=prev;
58
0
   return i;
59
0
}
60
61
opus_uint32 celt_lcg_rand(opus_uint32 seed)
62
49.8M
{
63
49.8M
   return 1664525 * seed + 1013904223;
64
49.8M
}
65
66
/* This is a cos() approximation designed to be bit-exact on any platform. Bit exactness
67
   with this approximation is important because it has an impact on the bit allocation */
68
opus_int16 bitexact_cos(opus_int16 x)
69
339k
{
70
339k
   opus_int32 tmp;
71
339k
   opus_int16 x2;
72
339k
   tmp = (4096+((opus_int32)(x)*(x)))>>13;
73
339k
   celt_sig_assert(tmp<=32767);
74
339k
   x2 = tmp;
75
339k
   x2 = (32767-x2) + FRAC_MUL16(x2, (-7651 + FRAC_MUL16(x2, (8277 + FRAC_MUL16(-626, x2)))));
76
339k
   celt_sig_assert(x2<=32766);
77
339k
   return 1+x2;
78
339k
}
79
80
int bitexact_log2tan(int isin,int icos)
81
169k
{
82
169k
   int lc;
83
169k
   int ls;
84
169k
   lc=EC_ILOG(icos);
85
169k
   ls=EC_ILOG(isin);
86
169k
   icos<<=15-lc;
87
169k
   isin<<=15-ls;
88
169k
   return (ls-lc)*(1<<11)
89
169k
         +FRAC_MUL16(isin, FRAC_MUL16(isin, -2597) + 7932)
90
169k
         -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932);
91
169k
}
92
93
#ifdef FIXED_POINT
94
/* Compute the amplitude (sqrt energy) in each of the bands */
95
void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int LM, int arch)
96
{
97
   int i, c, N;
98
   const opus_int16 *eBands = m->eBands;
99
   (void)arch;
100
   N = m->shortMdctSize<<LM;
101
   c=0; do {
102
      for (i=0;i<end;i++)
103
      {
104
         int j;
105
         opus_val32 maxval=0;
106
         opus_val32 sum = 0;
107
108
         maxval = celt_maxabs32(&X[c*N+(eBands[i]<<LM)], (eBands[i+1]-eBands[i])<<LM);
109
         if (maxval > 0)
110
         {
111
            int shift = IMAX(0, 30 - celt_ilog2(maxval+(maxval>>14)+1) - ((((m->logN[i]+7)>>BITRES)+LM+1)>>1));
112
            j=eBands[i]<<LM; do {
113
               opus_val32 x = SHL32(X[j+c*N],shift);
114
               sum = ADD32(sum, MULT32_32_Q31(x, x));
115
            } while (++j<eBands[i+1]<<LM);
116
            bandE[i+c*m->nbEBands] = MAX32(maxval, PSHR32(celt_sqrt32(SHR32(sum,1)), shift));
117
         } else {
118
            bandE[i+c*m->nbEBands] = EPSILON;
119
         }
120
      }
121
   } while (++c<C);
122
}
123
124
/* Normalise each band such that the energy is one. */
125
void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, celt_norm * OPUS_RESTRICT X, const celt_ener *bandE, int end, int C, int M)
126
{
127
   int i, c, N;
128
   const opus_int16 *eBands = m->eBands;
129
   N = M*m->shortMdctSize;
130
   c=0; do {
131
      i=0; do {
132
         int j,shift;
133
         opus_val32 E;
134
         opus_val32 g;
135
         E = bandE[i+c*m->nbEBands];
136
         /* For very low energies, we need this to make sure not to prevent energy rounding from
137
            blowing up the normalized signal. */
138
         if (E < 10) E += EPSILON;
139
         shift = 30-celt_zlog2(E);
140
         E = SHL32(E, shift);
141
         g = celt_rcp_norm32(E);
142
         j=M*eBands[i]; do {
143
            X[j+c*N] = PSHR32(MULT32_32_Q31(g, SHL32(freq[j+c*N], shift)), 30-NORM_SHIFT);
144
         } while (++j<M*eBands[i+1]);
145
      } while (++i<end);
146
   } while (++c<C);
147
}
148
149
#else /* FIXED_POINT */
150
/* Compute the amplitude (sqrt energy) in each of the bands */
151
void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int LM, int arch)
152
0
{
153
0
   int i, c, N;
154
0
   const opus_int16 *eBands = m->eBands;
155
0
   N = m->shortMdctSize<<LM;
156
0
   c=0; do {
157
0
      for (i=0;i<end;i++)
158
0
      {
159
0
         opus_val32 sum;
160
0
         sum = 1e-27f + celt_inner_prod(&X[c*N+(eBands[i]<<LM)], &X[c*N+(eBands[i]<<LM)], (eBands[i+1]-eBands[i])<<LM, arch);
161
0
         bandE[i+c*m->nbEBands] = celt_sqrt(sum);
162
         /*printf ("%f ", bandE[i+c*m->nbEBands]);*/
163
0
      }
164
0
   } while (++c<C);
165
   /*printf ("\n");*/
166
0
}
167
168
/* Normalise each band such that the energy is one. */
169
void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, celt_norm * OPUS_RESTRICT X, const celt_ener *bandE, int end, int C, int M)
170
0
{
171
0
   int i, c, N;
172
0
   const opus_int16 *eBands = m->eBands;
173
0
   N = M*m->shortMdctSize;
174
0
   c=0; do {
175
0
      for (i=0;i<end;i++)
176
0
      {
177
0
         int j;
178
0
         opus_val16 g = 1.f/(1e-27f+bandE[i+c*m->nbEBands]);
179
0
         for (j=M*eBands[i];j<M*eBands[i+1];j++)
180
0
            X[j+c*N] = freq[j+c*N]*g;
181
0
      }
182
0
   } while (++c<C);
183
0
}
184
185
#endif /* FIXED_POINT */
186
187
/* De-normalise the energy to produce the synthesis from the unit-energy bands */
188
void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X,
189
      celt_sig * OPUS_RESTRICT freq, const celt_glog *bandLogE, int start,
190
      int end, int M, int downsample, int silence)
191
249k
{
192
249k
   int i, N;
193
249k
   int bound;
194
249k
   celt_sig * OPUS_RESTRICT f;
195
249k
   const celt_norm * OPUS_RESTRICT x;
196
249k
   const opus_int16 *eBands = m->eBands;
197
249k
   N = M*m->shortMdctSize;
198
249k
   bound = M*eBands[end];
199
249k
   if (downsample!=1)
200
0
      bound = IMIN(bound, N/downsample);
201
249k
   if (silence)
202
35.7k
   {
203
35.7k
      bound = 0;
204
35.7k
      start = end = 0;
205
35.7k
   }
206
249k
   f = freq;
207
249k
   x = X+M*eBands[start];
208
249k
   if (start != 0)
209
49.3k
   {
210
11.1M
      for (i=0;i<M*eBands[start];i++)
211
11.1M
         *f++ = 0;
212
199k
   } else {
213
199k
      f += M*eBands[start];
214
199k
   }
215
3.27M
   for (i=start;i<end;i++)
216
3.02M
   {
217
3.02M
      int j, band_end;
218
3.02M
      opus_val32 g;
219
3.02M
      celt_glog lg;
220
#ifdef FIXED_POINT
221
      int shift;
222
#endif
223
3.02M
      j=M*eBands[i];
224
3.02M
      band_end = M*eBands[i+1];
225
3.02M
      lg = ADD32(bandLogE[i], SHL32((opus_val32)eMeans[i],DB_SHIFT-4));
226
3.02M
#ifndef FIXED_POINT
227
3.02M
      g = celt_exp2_db(MIN32(32.f, lg));
228
#else
229
      /* Handle the integer part of the log energy */
230
      shift = 17-(lg>>DB_SHIFT);
231
      if (shift>=31)
232
      {
233
         shift=0;
234
         g=0;
235
      } else {
236
         /* Handle the fractional part. */
237
         g = SHL32(celt_exp2_db_frac((lg&((1<<DB_SHIFT)-1))), 2);
238
      }
239
      /* Handle extreme gains with negative shift. */
240
      if (shift<0)
241
      {
242
         /* To avoid overflow, we're
243
            capping the gain here, which is equivalent to a cap of 18 on lg.
244
            This shouldn't trigger unless the bitstream is already corrupted. */
245
         g = 2147483647;
246
         shift = 0;
247
      }
248
#endif
249
50.9M
      do {
250
50.9M
         *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift);
251
50.9M
         x++;
252
50.9M
      } while (++j<band_end);
253
3.02M
   }
254
249k
   celt_assert(start <= end);
255
249k
   OPUS_CLEAR(&freq[bound], N-bound);
256
249k
}
257
258
/* This prevents energy collapse for transients with multiple short MDCTs */
259
void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_masks, int LM, int C, int size,
260
      int start, int end, const celt_glog *logE, const celt_glog *prev1logE,
261
      const celt_glog *prev2logE, const int *pulses, opus_uint32 seed, int encode, int arch)
262
1.77k
{
263
1.77k
   int c, i, j, k;
264
28.5k
   for (i=start;i<end;i++)
265
26.7k
   {
266
26.7k
      int N0;
267
26.7k
      opus_val16 thresh, sqrt_1;
268
26.7k
      int depth;
269
#ifdef FIXED_POINT
270
      int shift;
271
      opus_val32 thresh32;
272
#endif
273
274
26.7k
      N0 = m->eBands[i+1]-m->eBands[i];
275
      /* depth in 1/8 bits */
276
26.7k
      celt_sig_assert(pulses[i]>=0);
277
26.7k
      depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i]))>>LM;
278
279
#ifdef FIXED_POINT
280
      thresh32 = SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1);
281
      thresh = MULT16_32_Q15(QCONST16(0.5f, 15), MIN32(32767,thresh32));
282
      {
283
         opus_val32 t;
284
         t = N0<<LM;
285
         shift = celt_ilog2(t)>>1;
286
         t = SHL32(t, (7-shift)<<1);
287
         sqrt_1 = celt_rsqrt_norm(t);
288
      }
289
#else
290
26.7k
      thresh = .5f*celt_exp2(-.125f*depth);
291
26.7k
      sqrt_1 = celt_rsqrt(N0<<LM);
292
26.7k
#endif
293
294
26.7k
      c=0; do
295
30.7k
      {
296
30.7k
         celt_norm *X;
297
30.7k
         celt_glog prev1;
298
30.7k
         celt_glog prev2;
299
30.7k
         opus_val32 Ediff;
300
30.7k
         celt_norm r;
301
30.7k
         int renormalize=0;
302
30.7k
         prev1 = prev1logE[c*m->nbEBands+i];
303
30.7k
         prev2 = prev2logE[c*m->nbEBands+i];
304
30.7k
         if (!encode && C==1)
305
22.8k
         {
306
22.8k
            prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]);
307
22.8k
            prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]);
308
22.8k
         }
309
30.7k
         Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2);
310
30.7k
         Ediff = MAX32(0, Ediff);
311
312
#ifdef FIXED_POINT
313
         if (Ediff < GCONST(16.f))
314
         {
315
            opus_val32 r32 = SHR32(celt_exp2_db(-Ediff),1);
316
            r = 2*MIN16(16383,r32);
317
         } else {
318
            r = 0;
319
         }
320
         if (LM==3)
321
            r = MULT16_16_Q14(23170, MIN32(23169, r));
322
         r = SHR16(MIN16(thresh, r),1);
323
         r = VSHR32(MULT16_16_Q15(sqrt_1, r),shift+14-NORM_SHIFT);
324
#else
325
         /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because
326
            short blocks don't have the same energy as long */
327
30.7k
         r = 2.f*celt_exp2_db(-Ediff);
328
30.7k
         if (LM==3)
329
5.52k
            r *= 1.41421356f;
330
30.7k
         r = MIN16(thresh, r);
331
30.7k
         r = r*sqrt_1;
332
30.7k
#endif
333
30.7k
         X = X_+c*size+(m->eBands[i]<<LM);
334
175k
         for (k=0;k<1<<LM;k++)
335
145k
         {
336
            /* Detect collapse */
337
145k
            if (!(collapse_masks[i*C+c]&1<<k))
338
30.8k
            {
339
               /* Fill with noise */
340
110k
               for (j=0;j<N0;j++)
341
79.6k
               {
342
79.6k
                  seed = celt_lcg_rand(seed);
343
79.6k
                  X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
344
79.6k
               }
345
30.8k
               renormalize = 1;
346
30.8k
            }
347
145k
         }
348
         /* We just added some energy, so we need to renormalise */
349
30.7k
         if (renormalize)
350
10.7k
            renormalise_vector(X, N0<<LM, Q31ONE, arch);
351
30.7k
      } while (++c<C);
352
26.7k
   }
353
1.77k
}
354
355
/* Compute the weights to use for optimizing normalized distortion across
356
   channels. We use the amplitude to weight square distortion, which means
357
   that we use the square root of the value we would have been using if we
358
   wanted to minimize the MSE in the non-normalized domain. This roughly
359
   corresponds to some quick-and-dirty perceptual experiments I ran to
360
   measure inter-aural masking (there doesn't seem to be any published data
361
   on the topic). */
362
static void compute_channel_weights(celt_ener Ex, celt_ener Ey, opus_val16 w[2])
363
0
{
364
0
   celt_ener minE;
365
#ifdef FIXED_POINT
366
   int shift;
367
#endif
368
0
   minE = MIN32(Ex, Ey);
369
   /* Adjustment to make the weights a bit more conservative. */
370
0
   Ex = ADD32(Ex, minE/3);
371
0
   Ey = ADD32(Ey, minE/3);
372
#ifdef FIXED_POINT
373
   shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
374
#endif
375
0
   w[0] = VSHR32(Ex, shift);
376
0
   w[1] = VSHR32(Ey, shift);
377
0
}
378
379
static void intensity_stereo(const CELTMode *m, celt_norm * OPUS_RESTRICT X, const celt_norm * OPUS_RESTRICT Y, const celt_ener *bandE, int bandID, int N)
380
0
{
381
0
   int i = bandID;
382
0
   int j;
383
0
   opus_val16 a1, a2;
384
0
   opus_val16 left, right;
385
0
   opus_val16 norm;
386
#ifdef FIXED_POINT
387
   int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
388
#endif
389
0
   left = VSHR32(bandE[i],shift);
390
0
   right = VSHR32(bandE[i+m->nbEBands],shift);
391
0
   norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
392
#ifdef FIXED_POINT
393
   left = MIN32(left, norm-1);
394
   right = MIN32(right, norm-1);
395
#endif
396
0
   a1 = DIV32_16(SHL32(EXTEND32(left),15),norm);
397
0
   a2 = DIV32_16(SHL32(EXTEND32(right),15),norm);
398
0
   for (j=0;j<N;j++)
399
0
   {
400
0
      X[j] = ADD32(MULT16_32_Q15(a1, X[j]), MULT16_32_Q15(a2, Y[j]));
401
      /* Side is not encoded, no need to calculate */
402
0
   }
403
0
}
404
405
static void stereo_split(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, int N)
406
0
{
407
0
   int j;
408
0
   for (j=0;j<N;j++)
409
0
   {
410
0
      opus_val32 r, l;
411
0
      l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]);
412
0
      r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]);
413
0
      X[j] = ADD32(l, r);
414
0
      Y[j] = SUB32(r, l);
415
0
   }
416
0
}
417
418
static void stereo_merge(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, opus_val32 mid, int N, int arch)
419
196k
{
420
196k
   int j;
421
196k
   opus_val32 xp=0, side=0;
422
196k
   opus_val32 El, Er;
423
#ifdef FIXED_POINT
424
   int kl, kr;
425
#endif
426
196k
   opus_val32 t, lgain, rgain;
427
428
   /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
429
196k
   xp = celt_inner_prod_norm_shift(Y, X, N, arch);
430
196k
   side = celt_inner_prod_norm_shift(Y, Y, N, arch);
431
   /* Compensating for the mid normalization */
432
196k
   xp = MULT32_32_Q31(mid, xp);
433
   /* mid and side are in Q15, not Q14 like X and Y */
434
196k
   El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp;
435
196k
   Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp;
436
196k
   if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
437
740
   {
438
740
      OPUS_COPY(Y, X, N);
439
740
      return;
440
740
   }
441
442
#ifdef FIXED_POINT
443
   kl = celt_ilog2(El)>>1;
444
   kr = celt_ilog2(Er)>>1;
445
#endif
446
195k
   t = VSHR32(El, (kl<<1)-29);
447
195k
   lgain = celt_rsqrt_norm32(t);
448
195k
   t = VSHR32(Er, (kr<<1)-29);
449
195k
   rgain = celt_rsqrt_norm32(t);
450
451
#ifdef FIXED_POINT
452
   if (kl < 7)
453
      kl = 7;
454
   if (kr < 7)
455
      kr = 7;
456
#endif
457
458
6.00M
   for (j=0;j<N;j++)
459
5.80M
   {
460
5.80M
      celt_norm r, l;
461
      /* Apply mid scaling (side is already scaled) */
462
5.80M
      l = MULT32_32_Q31(mid, X[j]);
463
5.80M
      r = Y[j];
464
5.80M
      X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15);
465
5.80M
      Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15);
466
5.80M
   }
467
195k
}
468
469
/* Decide whether we should spread the pulses in the current frame */
470
int spreading_decision(const CELTMode *m, const celt_norm *X, int *average,
471
      int last_decision, int *hf_average, int *tapset_decision, int update_hf,
472
      int end, int C, int M, const int *spread_weight)
473
0
{
474
0
   int i, c, N0;
475
0
   int sum = 0, nbBands=0;
476
0
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
477
0
   int decision;
478
0
   int hf_sum=0;
479
480
0
   celt_assert(end>0);
481
482
0
   N0 = M*m->shortMdctSize;
483
484
0
   if (M*(eBands[end]-eBands[end-1]) <= 8)
485
0
      return SPREAD_NONE;
486
0
   c=0; do {
487
0
      for (i=0;i<end;i++)
488
0
      {
489
0
         int j, N, tmp=0;
490
0
         int tcount[3] = {0,0,0};
491
0
         const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
492
0
         N = M*(eBands[i+1]-eBands[i]);
493
0
         if (N<=8)
494
0
            continue;
495
         /* Compute rough CDF of |x[j]| */
496
0
         for (j=0;j<N;j++)
497
0
         {
498
0
            opus_val32 x2N; /* Q13 */
499
500
0
            x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N);
501
0
            if (x2N < QCONST16(0.25f,13))
502
0
               tcount[0]++;
503
0
            if (x2N < QCONST16(0.0625f,13))
504
0
               tcount[1]++;
505
0
            if (x2N < QCONST16(0.015625f,13))
506
0
               tcount[2]++;
507
0
         }
508
509
         /* Only include four last bands (8 kHz and up) */
510
0
         if (i>m->nbEBands-4)
511
0
            hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
512
0
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
513
0
         sum += tmp*spread_weight[i];
514
0
         nbBands+=spread_weight[i];
515
0
      }
516
0
   } while (++c<C);
517
518
0
   if (update_hf)
519
0
   {
520
0
      if (hf_sum)
521
0
         hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
522
0
      *hf_average = (*hf_average+hf_sum)>>1;
523
0
      hf_sum = *hf_average;
524
0
      if (*tapset_decision==2)
525
0
         hf_sum += 4;
526
0
      else if (*tapset_decision==0)
527
0
         hf_sum -= 4;
528
0
      if (hf_sum > 22)
529
0
         *tapset_decision=2;
530
0
      else if (hf_sum > 18)
531
0
         *tapset_decision=1;
532
0
      else
533
0
         *tapset_decision=0;
534
0
   }
535
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
536
0
   celt_assert(nbBands>0); /* end has to be non-zero */
537
0
   celt_assert(sum>=0);
538
0
   sum = celt_udiv((opus_int32)sum<<8, nbBands);
539
   /* Recursive averaging */
540
0
   sum = (sum+*average)>>1;
541
0
   *average = sum;
542
   /* Hysteresis */
543
0
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
544
0
   if (sum < 80)
545
0
   {
546
0
      decision = SPREAD_AGGRESSIVE;
547
0
   } else if (sum < 256)
548
0
   {
549
0
      decision = SPREAD_NORMAL;
550
0
   } else if (sum < 384)
551
0
   {
552
0
      decision = SPREAD_LIGHT;
553
0
   } else {
554
0
      decision = SPREAD_NONE;
555
0
   }
556
#ifdef FUZZING
557
   decision = rand()&0x3;
558
   *tapset_decision=rand()%3;
559
#endif
560
0
   return decision;
561
0
}
562
563
/* Indexing table for converting from natural Hadamard to ordery Hadamard
564
   This is essentially a bit-reversed Gray, on top of which we've added
565
   an inversion of the order because we want the DC at the end rather than
566
   the beginning. The lines are for N=2, 4, 8, 16 */
567
static const int ordery_table[] = {
568
       1,  0,
569
       3,  0,  2,  1,
570
       7,  0,  4,  3,  6,  1,  5,  2,
571
      15,  0,  8,  7, 12,  3, 11,  4, 14,  1,  9,  6, 13,  2, 10,  5,
572
};
573
574
static void deinterleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
575
137k
{
576
137k
   int i,j;
577
137k
   VARDECL(celt_norm, tmp);
578
137k
   int N;
579
137k
   SAVE_STACK;
580
137k
   N = N0*stride;
581
137k
   ALLOC(tmp, N, celt_norm);
582
137k
   celt_assert(stride>0);
583
137k
   if (hadamard)
584
82.8k
   {
585
82.8k
      const int *ordery = ordery_table+stride-2;
586
489k
      for (i=0;i<stride;i++)
587
406k
      {
588
1.63M
         for (j=0;j<N0;j++)
589
1.22M
            tmp[ordery[i]*N0+j] = X[j*stride+i];
590
406k
      }
591
82.8k
   } else {
592
333k
      for (i=0;i<stride;i++)
593
930k
         for (j=0;j<N0;j++)
594
651k
            tmp[i*N0+j] = X[j*stride+i];
595
54.6k
   }
596
137k
   OPUS_COPY(X, tmp, N);
597
137k
   RESTORE_STACK;
598
137k
}
599
600
static void interleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
601
169k
{
602
169k
   int i,j;
603
169k
   VARDECL(celt_norm, tmp);
604
169k
   int N;
605
169k
   SAVE_STACK;
606
169k
   N = N0*stride;
607
169k
   ALLOC(tmp, N, celt_norm);
608
169k
   if (hadamard)
609
107k
   {
610
107k
      const int *ordery = ordery_table+stride-2;
611
641k
      for (i=0;i<stride;i++)
612
2.35M
         for (j=0;j<N0;j++)
613
1.82M
            tmp[j*stride+i] = X[ordery[i]*N0+j];
614
107k
   } else {
615
384k
      for (i=0;i<stride;i++)
616
1.10M
         for (j=0;j<N0;j++)
617
782k
            tmp[j*stride+i] = X[i*N0+j];
618
61.8k
   }
619
169k
   OPUS_COPY(X, tmp, N);
620
169k
   RESTORE_STACK;
621
169k
}
622
623
void haar1(celt_norm *X, int N0, int stride)
624
630k
{
625
630k
   int i, j;
626
630k
   N0 >>= 1;
627
1.78M
   for (i=0;i<stride;i++)
628
6.46M
      for (j=0;j<N0;j++)
629
5.31M
      {
630
5.31M
         opus_val32 tmp1, tmp2;
631
5.31M
         tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]);
632
5.31M
         tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]);
633
5.31M
         X[stride*2*j+i] = ADD32(tmp1, tmp2);
634
5.31M
         X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2);
635
5.31M
      }
636
630k
}
637
638
static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo)
639
408k
{
640
408k
   static const opus_int16 exp2_table8[8] =
641
408k
      {16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048};
642
408k
   int qn, qb;
643
408k
   int N2 = 2*N-1;
644
408k
   if (stereo && N==2)
645
56.4k
      N2--;
646
   /* The upper limit ensures that in a stereo split with itheta==16384, we'll
647
       always have enough bits left over to code at least one pulse in the
648
       side; otherwise it would collapse, since it doesn't get folded. */
649
408k
   qb = celt_sudiv(b+N2*offset, N2);
650
408k
   qb = IMIN(b-pulse_cap-(4<<BITRES), qb);
651
652
408k
   qb = IMIN(8<<BITRES, qb);
653
654
408k
   if (qb<(1<<BITRES>>1)) {
655
187k
      qn = 1;
656
220k
   } else {
657
220k
      qn = exp2_table8[qb&0x7]>>(14-(qb>>BITRES));
658
220k
      qn = (qn+1)>>1<<1;
659
220k
   }
660
408k
   celt_assert(qn <= 256);
661
408k
   return qn;
662
408k
}
663
664
struct band_ctx {
665
   int encode;
666
   int resynth;
667
   const CELTMode *m;
668
   int i;
669
   int intensity;
670
   int spread;
671
   int tf_change;
672
   ec_ctx *ec;
673
   opus_int32 remaining_bits;
674
   const celt_ener *bandE;
675
   opus_uint32 seed;
676
   int arch;
677
   int theta_round;
678
   int disable_inv;
679
   int avoid_split_noise;
680
#ifdef ENABLE_QEXT
681
   ec_ctx *ext_ec;
682
   int extra_bits;
683
   opus_int32 ext_total_bits;
684
   int extra_bands;
685
#endif
686
};
687
688
struct split_ctx {
689
   int inv;
690
   int imid;
691
   int iside;
692
   int delta;
693
   int itheta;
694
#ifdef ENABLE_QEXT
695
   int itheta_q30;
696
#endif
697
   int qalloc;
698
};
699
700
static void compute_theta(struct band_ctx *ctx, struct split_ctx *sctx,
701
      celt_norm *X, celt_norm *Y, int N, int *b, int B, int B0,
702
      int LM,
703
      int stereo, int *fill ARG_QEXT(int *ext_b))
704
408k
{
705
408k
   int qn;
706
408k
   int itheta=0;
707
408k
   int itheta_q30=0;
708
408k
   int delta;
709
408k
   int imid, iside;
710
408k
   int qalloc;
711
408k
   int pulse_cap;
712
408k
   int offset;
713
408k
   opus_int32 tell;
714
408k
   int inv=0;
715
408k
   int encode;
716
408k
   const CELTMode *m;
717
408k
   int i;
718
408k
   int intensity;
719
408k
   ec_ctx *ec;
720
408k
   const celt_ener *bandE;
721
722
408k
   encode = ctx->encode;
723
408k
   m = ctx->m;
724
408k
   i = ctx->i;
725
408k
   intensity = ctx->intensity;
726
408k
   ec = ctx->ec;
727
408k
   bandE = ctx->bandE;
728
729
   /* Decide on the resolution to give to the split parameter theta */
730
408k
   pulse_cap = m->logN[i]+LM*(1<<BITRES);
731
408k
   offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
732
408k
   qn = compute_qn(N, *b, offset, pulse_cap, stereo);
733
408k
   if (stereo && i>=intensity)
734
221k
      qn = 1;
735
408k
   if (encode)
736
0
   {
737
      /* theta is the atan() of the ratio between the (normalized)
738
         side and mid. With just that parameter, we can re-scale both
739
         mid and side because we know that 1) they have unit norm and
740
         2) they are orthogonal. */
741
0
      itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch);
742
0
      itheta = itheta_q30>>16;
743
0
   }
744
408k
   tell = ec_tell_frac(ec);
745
408k
   if (qn!=1)
746
180k
   {
747
180k
      if (encode)
748
0
      {
749
0
         if (!stereo || ctx->theta_round == 0)
750
0
         {
751
0
            itheta = (itheta*(opus_int32)qn+8192)>>14;
752
0
            if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
753
0
            {
754
               /* Check if the selected value of theta will cause the bit allocation
755
                  to inject noise on one side. If so, make sure the energy of that side
756
                  is zero. */
757
0
               int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
758
0
               imid = bitexact_cos((opus_int16)unquantized);
759
0
               iside = bitexact_cos((opus_int16)(16384-unquantized));
760
0
               delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
761
0
               if (delta > *b)
762
0
                  itheta = qn;
763
0
               else if (delta < -*b)
764
0
                  itheta = 0;
765
0
            }
766
0
         } else {
767
0
            int down;
768
            /* Bias quantization towards itheta=0 and itheta=16384. */
769
0
            int bias = itheta > 8192 ? 32767/qn : -32767/qn;
770
0
            down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
771
0
            if (ctx->theta_round < 0)
772
0
               itheta = down;
773
0
            else
774
0
               itheta = down+1;
775
0
         }
776
0
      }
777
      /* Entropy coding of the angle. We use a uniform pdf for the
778
         time split, a step for stereo, and a triangular one for the rest. */
779
180k
      if (stereo && N>2)
780
15.2k
      {
781
15.2k
         int p0 = 3;
782
15.2k
         int x = itheta;
783
15.2k
         int x0 = qn/2;
784
15.2k
         int ft = p0*(x0+1) + x0;
785
         /* Use a probability of p0 up to itheta=8192 and then use 1 after */
786
15.2k
         if (encode)
787
0
         {
788
0
            ec_encode(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft);
789
15.2k
         } else {
790
15.2k
            int fs;
791
15.2k
            fs=ec_decode(ec,ft);
792
15.2k
            if (fs<(x0+1)*p0)
793
11.7k
               x=fs/p0;
794
3.47k
            else
795
3.47k
               x=x0+1+(fs-(x0+1)*p0);
796
15.2k
            ec_dec_update(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft);
797
15.2k
            itheta = x;
798
15.2k
         }
799
165k
      } else if (B0>1 || stereo) {
800
         /* Uniform pdf */
801
57.2k
         if (encode)
802
0
            ec_enc_uint(ec, itheta, qn+1);
803
57.2k
         else
804
57.2k
            itheta = ec_dec_uint(ec, qn+1);
805
108k
      } else {
806
108k
         int fs=1, ft;
807
108k
         ft = ((qn>>1)+1)*((qn>>1)+1);
808
108k
         if (encode)
809
0
         {
810
0
            int fl;
811
812
0
            fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
813
0
            fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
814
0
             ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
815
816
0
            ec_encode(ec, fl, fl+fs, ft);
817
108k
         } else {
818
            /* Triangular pdf */
819
108k
            int fl=0;
820
108k
            int fm;
821
108k
            fm = ec_decode(ec, ft);
822
823
108k
            if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
824
53.3k
            {
825
53.3k
               itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
826
53.3k
               fs = itheta + 1;
827
53.3k
               fl = itheta*(itheta + 1)>>1;
828
53.3k
            }
829
54.7k
            else
830
54.7k
            {
831
54.7k
               itheta = (2*(qn + 1)
832
54.7k
                - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
833
54.7k
               fs = qn + 1 - itheta;
834
54.7k
               fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
835
54.7k
            }
836
837
108k
            ec_dec_update(ec, fl, fl+fs, ft);
838
108k
         }
839
108k
      }
840
180k
      celt_assert(itheta>=0);
841
180k
      itheta = celt_udiv((opus_int32)itheta*16384, qn);
842
#ifdef ENABLE_QEXT
843
      *ext_b = IMIN(*ext_b, ctx->ext_total_bits - (opus_int32)ec_tell_frac(ctx->ext_ec));
844
      if (*ext_b >= 2*N<<BITRES && ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec)-1 > 2<<BITRES) {
845
         int extra_bits;
846
         int ext_tell = ec_tell_frac(ctx->ext_ec);
847
         extra_bits = IMIN(14, IMAX(2, celt_sudiv(*ext_b, (2*N-1)<<BITRES)));
848
         if (encode) {
849
            itheta_q30 = itheta_q30 - (itheta<<16);
850
            itheta_q30 = (itheta_q30*(opus_int64)qn*((1<<extra_bits)-1)+(1<<29))>>30;
851
            itheta_q30 += (1<<(extra_bits-1))-1;
852
            itheta_q30 = IMAX(0, IMIN((1<<extra_bits)-2, itheta_q30));
853
            ec_enc_uint(ctx->ext_ec, itheta_q30, (1<<extra_bits)-1);
854
         } else {
855
            itheta_q30 = ec_dec_uint(ctx->ext_ec, (1<<extra_bits)-1);
856
         }
857
         itheta_q30 -= (1<<(extra_bits-1))-1;
858
         itheta_q30 = (itheta<<16) + itheta_q30*(opus_int64)(1<<30)/(qn*((1<<extra_bits)-1));
859
         /* Hard bounds on itheta (can only trigger on corrupted bitstreams). */
860
         itheta_q30 = IMAX(0, IMIN(itheta_q30, 1073741824));
861
         *ext_b -= ec_tell_frac(ctx->ext_ec) - ext_tell;
862
      } else {
863
         itheta_q30 = (opus_int32)itheta<<16;
864
      }
865
#endif
866
180k
      if (encode && stereo)
867
0
      {
868
0
         if (itheta==0)
869
0
            intensity_stereo(m, X, Y, bandE, i, N);
870
0
         else
871
0
            stereo_split(X, Y, N);
872
0
      }
873
      /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate.
874
               Let's do that at higher complexity */
875
227k
   } else if (stereo) {
876
227k
      if (encode)
877
0
      {
878
0
         inv = itheta > 8192 && !ctx->disable_inv;
879
0
         if (inv)
880
0
         {
881
0
            int j;
882
0
            for (j=0;j<N;j++)
883
0
               Y[j] = -Y[j];
884
0
         }
885
0
         intensity_stereo(m, X, Y, bandE, i, N);
886
0
      }
887
227k
      if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
888
54.1k
      {
889
54.1k
         if (encode)
890
0
            ec_enc_bit_logp(ec, inv, 2);
891
54.1k
         else
892
54.1k
            inv = ec_dec_bit_logp(ec, 2);
893
54.1k
      } else
894
173k
         inv = 0;
895
      /* inv flag override to avoid problems with downmixing. */
896
227k
      if (ctx->disable_inv)
897
0
         inv = 0;
898
227k
      itheta = 0;
899
227k
      itheta_q30 = 0;
900
227k
   }
901
408k
   qalloc = ec_tell_frac(ec) - tell;
902
408k
   *b -= qalloc;
903
904
408k
   if (itheta == 0)
905
233k
   {
906
233k
      imid = 32767;
907
233k
      iside = 0;
908
233k
      *fill &= (1<<B)-1;
909
233k
      delta = -16384;
910
233k
   } else if (itheta == 16384)
911
4.61k
   {
912
4.61k
      imid = 0;
913
4.61k
      iside = 32767;
914
4.61k
      *fill &= ((1<<B)-1)<<B;
915
4.61k
      delta = 16384;
916
169k
   } else {
917
169k
      imid = bitexact_cos((opus_int16)itheta);
918
169k
      iside = bitexact_cos((opus_int16)(16384-itheta));
919
      /* This is the mid vs side allocation that minimizes squared error
920
         in that band. */
921
169k
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
922
169k
   }
923
924
408k
   sctx->inv = inv;
925
408k
   sctx->imid = imid;
926
408k
   sctx->iside = iside;
927
408k
   sctx->delta = delta;
928
408k
   sctx->itheta = itheta;
929
#ifdef ENABLE_QEXT
930
   sctx->itheta_q30 = itheta_q30;
931
#endif
932
408k
   sctx->qalloc = qalloc;
933
408k
}
934
static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y,
935
      celt_norm *lowband_out)
936
122k
{
937
122k
   int c;
938
122k
   int stereo;
939
122k
   celt_norm *x = X;
940
122k
   int encode;
941
122k
   ec_ctx *ec;
942
943
122k
   encode = ctx->encode;
944
122k
   ec = ctx->ec;
945
946
122k
   stereo = Y != NULL;
947
174k
   c=0; do {
948
174k
      int sign=0;
949
174k
      if (ctx->remaining_bits>=1<<BITRES)
950
58.5k
      {
951
58.5k
         if (encode)
952
0
         {
953
0
            sign = x[0]<0;
954
0
            ec_enc_bits(ec, sign, 1);
955
58.5k
         } else {
956
58.5k
            sign = ec_dec_bits(ec, 1);
957
58.5k
         }
958
58.5k
         ctx->remaining_bits -= 1<<BITRES;
959
58.5k
      }
960
174k
      if (ctx->resynth)
961
174k
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
962
174k
      x = Y;
963
174k
   } while (++c<1+stereo);
964
122k
   if (lowband_out)
965
122k
      lowband_out[0] = SHR32(X[0],4);
966
122k
   return 1;
967
122k
}
968
969
/* This function is responsible for encoding and decoding a mono partition.
970
   It can split the band in two and transmit the energy difference with
971
   the two half-bands. It can be called recursively so bands can end up being
972
   split in 8 parts. */
973
static unsigned quant_partition(struct band_ctx *ctx, celt_norm *X,
974
      int N, int b, int B, celt_norm *lowband,
975
      int LM,
976
      opus_val32 gain, int fill
977
      ARG_QEXT(int ext_b))
978
1.40M
{
979
1.40M
   const unsigned char *cache;
980
1.40M
   int q;
981
1.40M
   int curr_bits;
982
1.40M
   int imid=0, iside=0;
983
1.40M
   int B0=B;
984
1.40M
   opus_val32 mid=0, side=0;
985
1.40M
   unsigned cm=0;
986
1.40M
   celt_norm *Y=NULL;
987
1.40M
   int encode;
988
1.40M
   const CELTMode *m;
989
1.40M
   int i;
990
1.40M
   int spread;
991
1.40M
   ec_ctx *ec;
992
993
1.40M
   encode = ctx->encode;
994
1.40M
   m = ctx->m;
995
1.40M
   i = ctx->i;
996
1.40M
   spread = ctx->spread;
997
1.40M
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
1.40M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
1.40M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
155k
   {
1003
155k
      int mbits, sbits, delta;
1004
155k
      int itheta;
1005
155k
      int qalloc;
1006
155k
      struct split_ctx sctx;
1007
155k
      celt_norm *next_lowband2=NULL;
1008
155k
      opus_int32 rebalance;
1009
1010
155k
      N >>= 1;
1011
155k
      Y = X+N;
1012
155k
      LM -= 1;
1013
155k
      if (B==1)
1014
108k
         fill = (fill&1)|(fill<<1);
1015
155k
      B = (B+1)>>1;
1016
1017
155k
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
155k
      imid = sctx.imid;
1019
155k
      iside = sctx.iside;
1020
155k
      delta = sctx.delta;
1021
155k
      itheta = sctx.itheta;
1022
155k
      qalloc = sctx.qalloc;
1023
#ifdef FIXED_POINT
1024
# ifdef ENABLE_QEXT
1025
      (void)imid;
1026
      (void)iside;
1027
      mid = celt_cos_norm32(sctx.itheta_q30);
1028
      side = celt_cos_norm32((1<<30)-sctx.itheta_q30);
1029
# else
1030
      mid = SHL32(EXTEND32(imid), 16);
1031
      side = SHL32(EXTEND32(iside), 16);
1032
# endif
1033
#else
1034
# ifdef ENABLE_QEXT
1035
      (void)imid;
1036
      (void)iside;
1037
      mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1038
      side = celt_cos_norm2(1.f-sctx.itheta_q30*(1.f/(1<<30)));
1039
# else
1040
155k
      mid = (1.f/32768)*imid;
1041
155k
      side = (1.f/32768)*iside;
1042
155k
# endif
1043
155k
#endif
1044
1045
      /* Give more bits to low-energy MDCTs than they would otherwise deserve */
1046
155k
      if (B0>1 && (itheta&0x3fff))
1047
44.1k
      {
1048
44.1k
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
19.8k
            delta -= delta>>(4-LM);
1051
24.3k
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
24.3k
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
44.1k
      }
1055
155k
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
155k
      sbits = b-mbits;
1057
155k
      ctx->remaining_bits -= qalloc;
1058
1059
155k
      if (lowband)
1060
116k
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
155k
      rebalance = ctx->remaining_bits;
1063
155k
      if (mbits >= sbits)
1064
79.4k
      {
1065
79.4k
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
79.4k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
79.4k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
79.4k
         if (rebalance > 3<<BITRES && itheta!=0)
1069
43.2k
            sbits += rebalance - (3<<BITRES);
1070
79.4k
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
79.4k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
79.4k
      } else {
1073
76.2k
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
76.2k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
76.2k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
76.2k
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
44.5k
            mbits += rebalance - (3<<BITRES);
1078
76.2k
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
76.2k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
76.2k
      }
1081
1.24M
   } else {
1082
#ifdef ENABLE_QEXT
1083
      int extra_bits;
1084
      int ext_remaining_bits;
1085
      extra_bits = ext_b/(N-1)>>BITRES;
1086
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1087
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1088
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1089
         extra_bits = IMAX(extra_bits-1, 0);
1090
      }
1091
      extra_bits = IMIN(14, extra_bits);
1092
#endif
1093
      /* This is the basic no-split case */
1094
1.24M
      q = bits2pulses(m, i, LM, b);
1095
1.24M
      curr_bits = pulses2bits(m, i, LM, q);
1096
1.24M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
1.26M
      while (ctx->remaining_bits < 0 && q > 0)
1100
14.7k
      {
1101
14.7k
         ctx->remaining_bits += curr_bits;
1102
14.7k
         q--;
1103
14.7k
         curr_bits = pulses2bits(m, i, LM, q);
1104
14.7k
         ctx->remaining_bits -= curr_bits;
1105
14.7k
      }
1106
1107
1.24M
      if (q!=0)
1108
486k
      {
1109
486k
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
486k
         if (encode)
1113
0
         {
1114
0
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
0
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
0
                           ctx->arch);
1117
486k
         } else {
1118
486k
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
486k
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
486k
         }
1121
#ifdef ENABLE_QEXT
1122
      } else if (ext_b > 2*N<<BITRES)
1123
      {
1124
         extra_bits = ext_b/(N-1)>>BITRES;
1125
         ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec);
1126
         if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1127
            extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1128
            extra_bits = IMAX(extra_bits-1, 0);
1129
         }
1130
         extra_bits = IMIN(14, extra_bits);
1131
         if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth);
1132
         else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain);
1133
#endif
1134
761k
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
761k
         int j;
1137
761k
         if (ctx->resynth)
1138
761k
         {
1139
761k
            unsigned cm_mask;
1140
            /* B can be as large as 16, so this shift might overflow an int on a
1141
               16-bit platform; use a long to get defined behavior.*/
1142
761k
            cm_mask = (unsigned)(1UL<<B)-1;
1143
761k
            fill &= cm_mask;
1144
761k
            if (!fill)
1145
190k
            {
1146
190k
               OPUS_CLEAR(X, N);
1147
571k
            } else {
1148
571k
               if (lowband == NULL)
1149
37.6k
               {
1150
                  /* Noise */
1151
1.09M
                  for (j=0;j<N;j++)
1152
1.05M
                  {
1153
1.05M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
1.05M
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
1.05M
                  }
1156
37.6k
                  cm = cm_mask;
1157
533k
               } else {
1158
                  /* Folded spectrum */
1159
10.7M
                  for (j=0;j<N;j++)
1160
10.1M
                  {
1161
10.1M
                     opus_val16 tmp;
1162
10.1M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
10.1M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
10.1M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
10.1M
                     X[j] = lowband[j]+tmp;
1167
10.1M
                  }
1168
533k
                  cm = fill;
1169
533k
               }
1170
571k
               renormalise_vector(X, N, gain, ctx->arch);
1171
571k
            }
1172
761k
         }
1173
761k
      }
1174
1.24M
   }
1175
1176
1.40M
   return cm;
1177
1.40M
}
1178
1179
#ifdef ENABLE_QEXT
1180
static unsigned cubic_quant_partition(struct band_ctx *ctx, celt_norm *X, int N, int b, int B, ec_ctx *ec, int LM, opus_val32 gain, int resynth, int encode)
1181
{
1182
   celt_assert(LM>=0);
1183
   ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1184
   b = IMIN(b, ctx->remaining_bits);
1185
   /* As long as we have at least two bits of depth, split all the way to LM=0 (not -1 like PVQ). */
1186
   if (LM==0 || b<=2*N<<BITRES) {
1187
      int res, ret;
1188
      b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits);
1189
      /* Resolution left after taking into account coding the cube face. */
1190
      res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES;
1191
      res = IMIN(14, IMAX(0, res));
1192
      if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth);
1193
      else ret = cubic_unquant(X, N, res, B, ec, gain);
1194
      ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1195
      return ret;
1196
   } else {
1197
      celt_norm *Y;
1198
      opus_int32 itheta_q30;
1199
      opus_val32 g1, g2;
1200
      opus_int32 theta_res;
1201
      opus_int32 qtheta;
1202
      int delta;
1203
      int b1, b2;
1204
      int cm;
1205
      int N0;
1206
      N0 = N;
1207
      N >>= 1;
1208
      Y = X+N;
1209
      LM -= 1;
1210
      B = (B+1)>>1;
1211
      theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1);
1212
      if (encode) {
1213
         itheta_q30 = stereo_itheta(X, Y, 0, N, ctx->arch);
1214
         qtheta = (itheta_q30+(1<<(29-theta_res)))>>(30-theta_res);
1215
         ec_enc_uint(ec, qtheta, (1<<theta_res)+1);
1216
      } else {
1217
         qtheta = ec_dec_uint(ec, (1<<theta_res)+1);
1218
      }
1219
      itheta_q30 = qtheta<<(30-theta_res);
1220
      b -= theta_res<<BITRES;
1221
      delta = (N0-1) * 23 * ((itheta_q30>>16)-8192) >> (17-BITRES);
1222
1223
#ifdef FIXED_POINT
1224
      g1 = celt_cos_norm32(itheta_q30);
1225
      g2 = celt_cos_norm32((1<<30)-itheta_q30);
1226
#else
1227
      g1 = celt_cos_norm2(itheta_q30*(1.f/(1<<30)));
1228
      g2 = celt_cos_norm2(1.f-itheta_q30*(1.f/(1<<30)));
1229
#endif
1230
      if (itheta_q30 == 0) {
1231
         b1=b;
1232
         b2=0;
1233
      } else if (itheta_q30==1073741824) {
1234
         b1=0;
1235
         b2=b;
1236
      } else {
1237
         b1 = IMIN(b, IMAX(0, (b-delta)/2));
1238
         b2 = b-b1;
1239
      }
1240
      cm  = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode);
1241
      cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode);
1242
      return cm;
1243
   }
1244
}
1245
#endif
1246
1247
/* This function is responsible for encoding and decoding a band for the mono case. */
1248
static unsigned quant_band(struct band_ctx *ctx, celt_norm *X,
1249
      int N, int b, int B, celt_norm *lowband,
1250
      int LM, celt_norm *lowband_out,
1251
      opus_val32 gain, celt_norm *lowband_scratch, int fill
1252
      ARG_QEXT(int ext_b))
1253
1.16M
{
1254
1.16M
   int N0=N;
1255
1.16M
   int N_B=N;
1256
1.16M
   int N_B0;
1257
1.16M
   int B0=B;
1258
1.16M
   int time_divide=0;
1259
1.16M
   int recombine=0;
1260
1.16M
   int longBlocks;
1261
1.16M
   unsigned cm=0;
1262
1.16M
   int k;
1263
1.16M
   int encode;
1264
1.16M
   int tf_change;
1265
1266
1.16M
   encode = ctx->encode;
1267
1.16M
   tf_change = ctx->tf_change;
1268
1269
1.16M
   longBlocks = B0==1;
1270
1271
1.16M
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
1.16M
   if (N==1)
1275
70.2k
   {
1276
70.2k
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
70.2k
   }
1278
1279
1.09M
   if (tf_change>0)
1280
68.4k
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
1.09M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
162k
   {
1285
162k
      OPUS_COPY(lowband_scratch, lowband, N);
1286
162k
      lowband = lowband_scratch;
1287
162k
   }
1288
1289
1.20M
   for (k=0;k<recombine;k++)
1290
110k
   {
1291
110k
      static const unsigned char bit_interleave_table[16]={
1292
110k
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
110k
      };
1294
110k
      if (encode)
1295
0
         haar1(X, N>>k, 1<<k);
1296
110k
      if (lowband)
1297
89.8k
         haar1(lowband, N>>k, 1<<k);
1298
110k
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
110k
   }
1300
1.09M
   B>>=recombine;
1301
1.09M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
1.33M
   while ((N_B&1) == 0 && tf_change<0)
1305
242k
   {
1306
242k
      if (encode)
1307
0
         haar1(X, N_B, B);
1308
242k
      if (lowband)
1309
187k
         haar1(lowband, N_B, B);
1310
242k
      fill |= fill<<B;
1311
242k
      B <<= 1;
1312
242k
      N_B >>= 1;
1313
242k
      time_divide++;
1314
242k
      tf_change++;
1315
242k
   }
1316
1.09M
   B0=B;
1317
1.09M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
1.09M
   if (B0>1)
1321
169k
   {
1322
169k
      if (encode)
1323
0
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
169k
      if (lowband)
1325
137k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
169k
   }
1327
1328
#ifdef ENABLE_QEXT
1329
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1330
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1331
   } else
1332
#endif
1333
1.09M
   {
1334
1.09M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
1.09M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
1.09M
   if (ctx->resynth)
1339
1.09M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
1.09M
      if (B0>1)
1342
169k
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
1.09M
      N_B = N_B0;
1346
1.09M
      B = B0;
1347
1.33M
      for (k=0;k<time_divide;k++)
1348
242k
      {
1349
242k
         B >>= 1;
1350
242k
         N_B <<= 1;
1351
242k
         cm |= cm>>B;
1352
242k
         haar1(X, N_B, B);
1353
242k
      }
1354
1355
1.20M
      for (k=0;k<recombine;k++)
1356
110k
      {
1357
110k
         static const unsigned char bit_deinterleave_table[16]={
1358
110k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
110k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
110k
         };
1361
110k
         cm = bit_deinterleave_table[cm];
1362
110k
         haar1(X, N0>>k, 1<<k);
1363
110k
      }
1364
1.09M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
1.09M
      if (lowband_out)
1368
813k
      {
1369
813k
         int j;
1370
813k
         opus_val16 n;
1371
813k
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
10.4M
         for (j=0;j<N0;j++)
1373
9.66M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
813k
      }
1375
1.09M
      cm &= (1<<B)-1;
1376
1.09M
   }
1377
1.09M
   return cm;
1378
1.16M
}
1379
1380
#ifdef FIXED_POINT
1381
#define MIN_STEREO_ENERGY 2
1382
#else
1383
0
#define MIN_STEREO_ENERGY 1e-10f
1384
#endif
1385
1386
/* This function is responsible for encoding and decoding a band for the stereo case. */
1387
static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm *Y,
1388
      int N, int b, int B, celt_norm *lowband,
1389
      int LM, celt_norm *lowband_out,
1390
      celt_norm *lowband_scratch, int fill
1391
      ARG_QEXT(int ext_b) ARG_QEXT(const int *cap))
1392
304k
{
1393
304k
   int imid=0, iside=0;
1394
304k
   int inv = 0;
1395
304k
   opus_val32 mid=0, side=0;
1396
304k
   unsigned cm=0;
1397
304k
   int mbits, sbits, delta;
1398
304k
   int itheta;
1399
304k
   int qalloc;
1400
304k
   struct split_ctx sctx;
1401
304k
   int orig_fill;
1402
304k
   int encode;
1403
304k
   ec_ctx *ec;
1404
1405
304k
   encode = ctx->encode;
1406
304k
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
304k
   if (N==1)
1410
51.9k
   {
1411
51.9k
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
51.9k
   }
1413
1414
252k
   orig_fill = fill;
1415
1416
252k
   if (encode) {
1417
0
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
0
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
0
         else OPUS_COPY(X, Y, N);
1420
0
      }
1421
0
   }
1422
252k
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
252k
   inv = sctx.inv;
1424
252k
   imid = sctx.imid;
1425
252k
   iside = sctx.iside;
1426
252k
   delta = sctx.delta;
1427
252k
   itheta = sctx.itheta;
1428
252k
   qalloc = sctx.qalloc;
1429
#ifdef FIXED_POINT
1430
# ifdef ENABLE_QEXT
1431
   (void)imid;
1432
   (void)iside;
1433
   mid = celt_cos_norm32(sctx.itheta_q30);
1434
   side = celt_cos_norm32((1<<30)-sctx.itheta_q30);
1435
# else
1436
   mid = SHL32(EXTEND32(imid), 16);
1437
   side = SHL32(EXTEND32(iside), 16);
1438
# endif
1439
#else
1440
# ifdef ENABLE_QEXT
1441
   (void)imid;
1442
   (void)iside;
1443
   mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1444
   side = celt_cos_norm2(1.f-sctx.itheta_q30*(1.f/(1<<30)));
1445
# else
1446
252k
   mid = (1.f/32768)*imid;
1447
252k
   side = (1.f/32768)*iside;
1448
252k
# endif
1449
252k
#endif
1450
1451
   /* This is a special case for N=2 that only works for stereo and takes
1452
      advantage of the fact that mid and side are orthogonal to encode
1453
      the side with just one bit. */
1454
252k
   if (N==2)
1455
56.4k
   {
1456
56.4k
      int c;
1457
56.4k
      int sign=0;
1458
56.4k
      celt_norm *x2, *y2;
1459
56.4k
      mbits = b;
1460
56.4k
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
56.4k
      if (itheta != 0 && itheta != 16384)
1463
8.06k
         sbits = 1<<BITRES;
1464
56.4k
      mbits -= sbits;
1465
56.4k
      c = itheta > 8192;
1466
56.4k
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
56.4k
      x2 = c ? Y : X;
1469
56.4k
      y2 = c ? X : Y;
1470
56.4k
      if (sbits)
1471
8.06k
      {
1472
8.06k
         if (encode)
1473
0
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
0
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
0
            ec_enc_bits(ec, sign, 1);
1478
8.06k
         } else {
1479
8.06k
            sign = ec_dec_bits(ec, 1);
1480
8.06k
         }
1481
8.06k
      }
1482
56.4k
      sign = 1-2*sign;
1483
      /* We use orig_fill here because we want to fold the side, but if
1484
         itheta==16384, we'll have cleared the low bits of fill. */
1485
56.4k
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
56.4k
            lowband_scratch, orig_fill ARG_QEXT(ext_b));
1487
      /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse),
1488
         and there's no need to worry about mixing with the other channel. */
1489
56.4k
      y2[0] = -sign*x2[1];
1490
56.4k
      y2[1] = sign*x2[0];
1491
56.4k
      if (ctx->resynth)
1492
56.4k
      {
1493
56.4k
         celt_norm tmp;
1494
56.4k
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
56.4k
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
56.4k
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
56.4k
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
56.4k
         tmp = X[0];
1499
56.4k
         X[0] = SUB32(tmp,Y[0]);
1500
56.4k
         Y[0] = ADD32(tmp,Y[0]);
1501
56.4k
         tmp = X[1];
1502
56.4k
         X[1] = SUB32(tmp,Y[1]);
1503
56.4k
         Y[1] = ADD32(tmp,Y[1]);
1504
56.4k
      }
1505
196k
   } else {
1506
      /* "Normal" split code */
1507
196k
      opus_int32 rebalance;
1508
1509
196k
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
196k
      sbits = b-mbits;
1511
196k
      ctx->remaining_bits -= qalloc;
1512
1513
196k
      rebalance = ctx->remaining_bits;
1514
196k
      if (mbits >= sbits)
1515
190k
      {
1516
#ifdef ENABLE_QEXT
1517
         int qext_extra = 0;
1518
         /* Reallocate any mid bits that cannot be used to extra mid bits. */
1519
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, mbits - cap[ctx->i]/2));
1520
#endif
1521
         /* In stereo mode, we do not apply a scaling to the mid because we need the normalized
1522
            mid for folding later. */
1523
190k
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
190k
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
190k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
190k
         if (rebalance > 3<<BITRES && itheta!=0)
1527
888
            sbits += rebalance - (3<<BITRES);
1528
#ifdef ENABLE_QEXT
1529
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the mid. */
1530
         if (ctx->extra_bands) sbits = IMIN(sbits, ctx->remaining_bits);
1531
#endif
1532
         /* For a stereo split, the high bits of fill are always zero, so no
1533
            folding will be done to the side. */
1534
190k
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
190k
      } else {
1536
#ifdef ENABLE_QEXT
1537
         int qext_extra = 0;
1538
         /* Reallocate any side bits that cannot be used to extra side bits. */
1539
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, sbits - cap[ctx->i]/2));
1540
#endif
1541
         /* For a stereo split, the high bits of fill are always zero, so no
1542
            folding will be done to the side. */
1543
5.84k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
5.84k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
5.84k
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
422
            mbits += rebalance - (3<<BITRES);
1547
#ifdef ENABLE_QEXT
1548
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the side. */
1549
         if (ctx->extra_bands) mbits = IMIN(mbits, ctx->remaining_bits);
1550
#endif
1551
         /* In stereo mode, we do not apply a scaling to the mid because we need the normalized
1552
            mid for folding later. */
1553
5.84k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
5.84k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
5.84k
      }
1556
196k
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
252k
   if (ctx->resynth)
1561
252k
   {
1562
252k
      if (N!=2)
1563
196k
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
252k
      if (inv)
1565
12.3k
      {
1566
12.3k
         int j;
1567
124k
         for (j=0;j<N;j++)
1568
112k
            Y[j] = -Y[j];
1569
12.3k
      }
1570
252k
   }
1571
252k
   return cm;
1572
304k
}
1573
1574
#ifndef DISABLE_UPDATE_DRAFT
1575
static void special_hybrid_folding(const CELTMode *m, celt_norm *norm, celt_norm *norm2, int start, int M, int dual_stereo)
1576
82.4k
{
1577
82.4k
   int n1, n2;
1578
82.4k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1579
82.4k
   n1 = M*(eBands[start+1]-eBands[start]);
1580
82.4k
   n2 = M*(eBands[start+2]-eBands[start+1]);
1581
   /* Duplicate enough of the first band folding data to be able to fold the second band.
1582
      Copies no data for CELT-only mode. */
1583
82.4k
   OPUS_COPY(&norm[n1], &norm[2*n1 - n2], n2-n1);
1584
82.4k
   if (dual_stereo)
1585
3.74k
      OPUS_COPY(&norm2[n1], &norm2[2*n1 - n2], n2-n1);
1586
82.4k
}
1587
#endif
1588
1589
void quant_all_bands(int encode, const CELTMode *m, int start, int end,
1590
      celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks,
1591
      const celt_ener *bandE, int *pulses, int shortBlocks, int spread,
1592
      int dual_stereo, int intensity, int *tf_res, opus_int32 total_bits,
1593
      opus_int32 balance, ec_ctx *ec, int LM, int codedBands,
1594
      opus_uint32 *seed, int complexity, int arch, int disable_inv
1595
      ARG_QEXT(ec_ctx *ext_ec) ARG_QEXT(int *extra_pulses)
1596
      ARG_QEXT(opus_int32 ext_total_bits) ARG_QEXT(const int *cap))
1597
82.4k
{
1598
82.4k
   int i;
1599
82.4k
   opus_int32 remaining_bits;
1600
82.4k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
82.4k
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
82.4k
   VARDECL(celt_norm, _norm);
1603
82.4k
   VARDECL(celt_norm, _lowband_scratch);
1604
82.4k
   VARDECL(celt_norm, X_save);
1605
82.4k
   VARDECL(celt_norm, Y_save);
1606
82.4k
   VARDECL(celt_norm, X_save2);
1607
82.4k
   VARDECL(celt_norm, Y_save2);
1608
82.4k
   VARDECL(celt_norm, norm_save2);
1609
82.4k
   VARDECL(unsigned char, bytes_save);
1610
82.4k
   int resynth_alloc;
1611
82.4k
   celt_norm *lowband_scratch;
1612
82.4k
   int B;
1613
82.4k
   int M;
1614
82.4k
   int lowband_offset;
1615
82.4k
   int update_lowband = 1;
1616
82.4k
   int C = Y_ != NULL ? 2 : 1;
1617
82.4k
   int norm_offset;
1618
82.4k
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1619
#ifdef RESYNTH
1620
   int resynth = 1;
1621
#else
1622
82.4k
   int resynth = !encode || theta_rdo;
1623
82.4k
#endif
1624
82.4k
   struct band_ctx ctx;
1625
#ifdef ENABLE_QEXT
1626
   int ext_b;
1627
   opus_int32 ext_balance=0;
1628
   opus_int32 ext_tell=0;
1629
   VARDECL(unsigned char, ext_bytes_save);
1630
#endif
1631
82.4k
   SAVE_STACK;
1632
1633
82.4k
   M = 1<<LM;
1634
82.4k
   B = shortBlocks ? M : 1;
1635
82.4k
   norm_offset = M*eBands[start];
1636
   /* No need to allocate norm for the last band because we don't need an
1637
      output in that band. */
1638
82.4k
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1639
82.4k
   norm = _norm;
1640
82.4k
   norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset;
1641
1642
   /* For decoding, we can use the last band as scratch space because we don't need that
1643
      scratch space for the last band and we don't care about the data there until we're
1644
      decoding the last band. */
1645
82.4k
   if (encode && resynth)
1646
0
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1647
82.4k
   else
1648
82.4k
      resynth_alloc = ALLOC_NONE;
1649
82.4k
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1650
82.4k
   if (encode && resynth)
1651
0
      lowband_scratch = _lowband_scratch;
1652
82.4k
   else
1653
82.4k
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1654
82.4k
   ALLOC(X_save, resynth_alloc, celt_norm);
1655
82.4k
   ALLOC(Y_save, resynth_alloc, celt_norm);
1656
82.4k
   ALLOC(X_save2, resynth_alloc, celt_norm);
1657
82.4k
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1658
82.4k
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1659
1660
82.4k
   lowband_offset = 0;
1661
82.4k
   ctx.bandE = bandE;
1662
82.4k
   ctx.ec = ec;
1663
82.4k
   ctx.encode = encode;
1664
82.4k
   ctx.intensity = intensity;
1665
82.4k
   ctx.m = m;
1666
82.4k
   ctx.seed = *seed;
1667
82.4k
   ctx.spread = spread;
1668
82.4k
   ctx.arch = arch;
1669
82.4k
   ctx.disable_inv = disable_inv;
1670
82.4k
   ctx.resynth = resynth;
1671
82.4k
   ctx.theta_round = 0;
1672
#ifdef ENABLE_QEXT
1673
   ctx.ext_ec = ext_ec;
1674
   ctx.ext_total_bits = ext_total_bits;
1675
   ctx.extra_bands = (cap == NULL);
1676
   if (ctx.extra_bands || ext_total_bits!=0) theta_rdo = 0;
1677
   ALLOC(ext_bytes_save, theta_rdo ? QEXT_PACKET_SIZE_CAP : ALLOC_NONE, unsigned char);
1678
#endif
1679
82.4k
   ALLOC(bytes_save, theta_rdo ? 1275 : ALLOC_NONE, unsigned char);
1680
1681
   /* Avoid injecting noise in the first band on transients. */
1682
82.4k
   ctx.avoid_split_noise = B > 1;
1683
1.07M
   for (i=start;i<end;i++)
1684
987k
   {
1685
987k
      opus_int32 tell;
1686
987k
      int b;
1687
987k
      int N;
1688
987k
      opus_int32 curr_balance;
1689
987k
      int effective_lowband=-1;
1690
987k
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1691
987k
      int tf_change=0;
1692
987k
      unsigned x_cm;
1693
987k
      unsigned y_cm;
1694
987k
      int last;
1695
1696
987k
      ctx.i = i;
1697
987k
      last = (i==end-1);
1698
1699
987k
      X = X_+M*eBands[i];
1700
987k
      if (Y_!=NULL)
1701
335k
         Y = Y_+M*eBands[i];
1702
652k
      else
1703
652k
         Y = NULL;
1704
987k
      N = M*eBands[i+1]-M*eBands[i];
1705
987k
      celt_assert(N > 0);
1706
987k
      tell = ec_tell_frac(ec);
1707
1708
      /* Compute how many bits we want to allocate to this band */
1709
987k
      if (i != start)
1710
905k
         balance -= tell;
1711
987k
      remaining_bits = total_bits-tell-1;
1712
987k
      ctx.remaining_bits = remaining_bits;
1713
#ifdef ENABLE_QEXT
1714
      if (i != start) {
1715
         ext_balance += extra_pulses[i-1] + ext_tell;
1716
      }
1717
      ext_tell = ec_tell_frac(ext_ec);
1718
      ctx.extra_bits = extra_pulses[i];
1719
      if (i != start)
1720
         ext_balance -= ext_tell;
1721
      if (i <= codedBands-1)
1722
      {
1723
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1724
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1725
      } else {
1726
         ext_b = 0;
1727
      }
1728
#endif
1729
987k
      if (i <= codedBands-1)
1730
402k
      {
1731
402k
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1732
402k
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1733
585k
      } else {
1734
585k
         b = 0;
1735
585k
      }
1736
1737
987k
#ifndef DISABLE_UPDATE_DRAFT
1738
987k
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1739
313k
            lowband_offset = i;
1740
987k
      if (i == start+1)
1741
82.4k
         special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1742
#else
1743
      if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0))
1744
            lowband_offset = i;
1745
#endif
1746
1747
987k
      tf_change = tf_res[i];
1748
987k
      ctx.tf_change = tf_change;
1749
987k
      if (i>=m->effEBands)
1750
0
      {
1751
0
         celt_assert(eBands[i+1]-eBands[i] <= eBands[m->nbEBands-1]-eBands[start]);
1752
0
         X=norm;
1753
0
         if (Y_!=NULL)
1754
0
            Y = norm2;
1755
0
         lowband_scratch = NULL;
1756
0
      }
1757
987k
      if (last && !theta_rdo)
1758
82.4k
         lowband_scratch = NULL;
1759
1760
      /* Get a conservative estimate of the collapse_mask's for the bands we're
1761
         going to be folding from. */
1762
987k
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1763
883k
      {
1764
883k
         int fold_start;
1765
883k
         int fold_end;
1766
883k
         int fold_i;
1767
         /* This ensures we never repeat spectral content within one band */
1768
883k
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1769
883k
         fold_start = lowband_offset;
1770
1.12M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1771
883k
         fold_end = lowband_offset-1;
1772
883k
#ifndef DISABLE_UPDATE_DRAFT
1773
1.67M
         while(++fold_end < i && M*eBands[fold_end] < effective_lowband+norm_offset+N);
1774
#else
1775
         while(M*eBands[++fold_end] < effective_lowband+norm_offset+N);
1776
#endif
1777
883k
         x_cm = y_cm = 0;
1778
1.92M
         fold_i = fold_start; do {
1779
1.92M
           x_cm |= collapse_masks[fold_i*C+0];
1780
1.92M
           y_cm |= collapse_masks[fold_i*C+C-1];
1781
1.92M
         } while (++fold_i<fold_end);
1782
883k
      }
1783
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1784
         always) be non-zero. */
1785
104k
      else
1786
104k
         x_cm = y_cm = (1<<B)-1;
1787
1788
987k
      if (dual_stereo && i==intensity)
1789
3.36k
      {
1790
3.36k
         int j;
1791
1792
         /* Switch off dual stereo to do intensity. */
1793
3.36k
         dual_stereo = 0;
1794
3.36k
         if (resynth)
1795
110k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1796
107k
               norm[j] = HALF32(norm[j]+norm2[j]);
1797
3.36k
      }
1798
987k
      if (dual_stereo)
1799
30.4k
      {
1800
30.4k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1801
30.4k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1802
30.4k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1803
30.4k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1804
30.4k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1805
30.4k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1806
957k
      } else {
1807
957k
         if (Y!=NULL)
1808
304k
         {
1809
304k
            if (theta_rdo && i < intensity)
1810
0
            {
1811
0
               ec_ctx ec_save, ec_save2;
1812
0
               struct band_ctx ctx_save, ctx_save2;
1813
0
               opus_val32 dist0, dist1;
1814
0
               unsigned cm, cm2;
1815
0
               int nstart_bytes, nend_bytes, save_bytes;
1816
0
               unsigned char *bytes_buf;
1817
#ifdef ENABLE_QEXT
1818
               ec_ctx ext_ec_save, ext_ec_save2;
1819
               unsigned char *ext_bytes_buf;
1820
               int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes;
1821
#endif
1822
0
               opus_val16 w[2];
1823
0
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1824
               /* Make a copy. */
1825
0
               cm = x_cm|y_cm;
1826
0
               ec_save = *ec;
1827
#ifdef ENABLE_QEXT
1828
               ext_ec_save = *ext_ec;
1829
#endif
1830
0
               ctx_save = ctx;
1831
0
               OPUS_COPY(X_save, X, N);
1832
0
               OPUS_COPY(Y_save, Y, N);
1833
               /* Encode and round down. */
1834
0
               ctx.theta_round = -1;
1835
0
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1836
0
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1837
0
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1838
0
               dist0 = MULT16_32_Q15(w[0], celt_inner_prod_norm_shift(X_save, X, N, arch)) + MULT16_32_Q15(w[1], celt_inner_prod_norm_shift(Y_save, Y, N, arch));
1839
1840
               /* Save first result. */
1841
0
               cm2 = x_cm;
1842
0
               ec_save2 = *ec;
1843
#ifdef ENABLE_QEXT
1844
               ext_ec_save2 = *ext_ec;
1845
#endif
1846
0
               ctx_save2 = ctx;
1847
0
               OPUS_COPY(X_save2, X, N);
1848
0
               OPUS_COPY(Y_save2, Y, N);
1849
0
               if (!last)
1850
0
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1851
0
               nstart_bytes = ec_save.offs;
1852
0
               nend_bytes = ec_save.storage;
1853
0
               bytes_buf = ec_save.buf+nstart_bytes;
1854
0
               save_bytes = nend_bytes-nstart_bytes;
1855
0
               OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1856
#ifdef ENABLE_QEXT
1857
               ext_nstart_bytes = ext_ec_save.offs;
1858
               ext_nend_bytes = ext_ec_save.storage;
1859
               ext_bytes_buf = ext_ec_save.buf!=NULL ? ext_ec_save.buf+ext_nstart_bytes : NULL;
1860
               ext_save_bytes = ext_nend_bytes-ext_nstart_bytes;
1861
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1862
#endif
1863
               /* Restore */
1864
0
               *ec = ec_save;
1865
#ifdef ENABLE_QEXT
1866
               *ext_ec = ext_ec_save;
1867
#endif
1868
0
               ctx = ctx_save;
1869
0
               OPUS_COPY(X, X_save, N);
1870
0
               OPUS_COPY(Y, Y_save, N);
1871
0
#ifndef DISABLE_UPDATE_DRAFT
1872
0
               if (i == start+1)
1873
0
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1874
0
#endif
1875
               /* Encode and round up. */
1876
0
               ctx.theta_round = 1;
1877
0
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1878
0
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1879
0
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1880
0
               dist1 = MULT16_32_Q15(w[0], celt_inner_prod_norm_shift(X_save, X, N, arch)) + MULT16_32_Q15(w[1], celt_inner_prod_norm_shift(Y_save, Y, N, arch));
1881
0
               if (dist0 >= dist1) {
1882
0
                  x_cm = cm2;
1883
0
                  *ec = ec_save2;
1884
#ifdef ENABLE_QEXT
1885
                  *ext_ec = ext_ec_save2;
1886
#endif
1887
0
                  ctx = ctx_save2;
1888
0
                  OPUS_COPY(X, X_save2, N);
1889
0
                  OPUS_COPY(Y, Y_save2, N);
1890
0
                  if (!last)
1891
0
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1892
0
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1893
#ifdef ENABLE_QEXT
1894
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1895
#endif
1896
0
               }
1897
304k
            } else {
1898
304k
               ctx.theta_round = 0;
1899
304k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1900
304k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1901
304k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1902
304k
            }
1903
652k
         } else {
1904
652k
            x_cm = quant_band(&ctx, X, N, b, B,
1905
652k
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1906
652k
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1907
652k
         }
1908
957k
         y_cm = x_cm;
1909
957k
      }
1910
987k
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1911
987k
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1912
987k
      balance += pulses[i] + tell;
1913
1914
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1915
987k
      update_lowband = b>(N<<BITRES);
1916
      /* We only need to avoid noise on a split for the first band. After that, we
1917
         have folding. */
1918
987k
      ctx.avoid_split_noise = 0;
1919
987k
   }
1920
82.4k
   *seed = ctx.seed;
1921
1922
82.4k
   RESTORE_STACK;
1923
82.4k
}