Coverage Report

Created: 2024-09-06 07:53

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