Coverage Report

Created: 2025-08-28 07:12

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