Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/media/libopus/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
0
         /*printf ("%f ", bandE[i+c*m->nbEBands]);*/
171
0
      }
172
0
   } while (++c<C);
173
0
   /*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
      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
0
      /* 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
0
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
0
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
0
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
0
            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
0
            /* Detect collapse */
346
0
            if (!(collapse_masks[i*C+c]&1<<k))
347
0
            {
348
0
               /* 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
0
         /* 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
#if FIXED_POINT
375
   int shift;
376
#endif
377
0
   minE = MIN32(Ex, Ey);
378
0
   /* Adjustment to make the weights a bit more conservative. */
379
0
   Ex = ADD32(Ex, minE/3);
380
0
   Ey = ADD32(Ey, minE/3);
381
#if 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
0
      /* 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
   opus_val32 t, lgain, rgain;
436
0
437
0
   /* 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
0
   /* Compensating for the mid normalization */
440
0
   xp = MULT16_32_Q15(mid, xp);
441
0
   /* 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
0
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
0
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
0
      /* 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
0
489
0
   celt_assert(end>0);
490
0
491
0
   N0 = M*m->shortMdctSize;
492
0
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
0
         /* Compute rough CDF of |x[j]| */
505
0
         for (j=0;j<N;j++)
506
0
         {
507
0
            opus_val32 x2N; /* Q13 */
508
0
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
0
518
0
         /* 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
0
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
0
   /*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
0
   /* Recursive averaging */
549
0
   sum = (sum+*average)>>1;
550
0
   *average = sum;
551
0
   /* 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
0
#ifdef FUZZING
566
0
   decision = rand()&0x3;
567
0
   *tapset_decision=rand()%3;
568
0
#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
0
   /* The upper limit ensures that in a stereo split with itheta==16384, we'll
656
0
       always have enough bits left over to code at least one pulse in the
657
0
       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
0
661
0
   qb = IMIN(8<<BITRES, qb);
662
0
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
0
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
0
728
0
   /* 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
0
      /* theta is the atan() of the ratio between the (normalized)
737
0
         side and mid. With just that parameter, we can re-scale both
738
0
         mid and side because we know that 1) they have unit norm and
739
0
         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
0
               /* Check if the selected value of theta will cause the bit allocation
753
0
                  to inject noise on one side. If so, make sure the energy of that side
754
0
                  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
0
            /* 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
0
      /* Entropy coding of the angle. We use a uniform pdf for the
776
0
         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
0
         /* 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
0
         /* 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
0
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
0
814
0
            ec_encode(ec, fl, fl+fs, ft);
815
0
         } else {
816
0
            /* Triangular pdf */
817
0
            int fl=0;
818
0
            int fm;
819
0
            fm = ec_decode(ec, ft);
820
0
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
0
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
0
      /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate.
848
0
               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
0
      /* 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
0
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
0
      /* This is the mid vs side allocation that minimizes squared error
893
0
         in that band. */
894
0
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
895
0
   }
896
0
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, int b,
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
0
913
0
   encode = ctx->encode;
914
0
   ec = ctx->ec;
915
0
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
         b-=1<<BITRES;
930
0
      }
931
0
      if (ctx->resynth)
932
0
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
933
0
      x = Y;
934
0
   } while (++c<1+stereo);
935
0
   if (lowband_out)
936
0
      lowband_out[0] = SHR16(X[0],4);
937
0
   return 1;
938
0
}
939
940
/* This function is responsible for encoding and decoding a mono partition.
941
   It can split the band in two and transmit the energy difference with
942
   the two half-bands. It can be called recursively so bands can end up being
943
   split in 8 parts. */
944
static unsigned quant_partition(struct band_ctx *ctx, celt_norm *X,
945
      int N, int b, int B, celt_norm *lowband,
946
      int LM,
947
      opus_val16 gain, int fill)
948
0
{
949
0
   const unsigned char *cache;
950
0
   int q;
951
0
   int curr_bits;
952
0
   int imid=0, iside=0;
953
0
   int B0=B;
954
0
   opus_val16 mid=0, side=0;
955
0
   unsigned cm=0;
956
0
   celt_norm *Y=NULL;
957
0
   int encode;
958
0
   const CELTMode *m;
959
0
   int i;
960
0
   int spread;
961
0
   ec_ctx *ec;
962
0
963
0
   encode = ctx->encode;
964
0
   m = ctx->m;
965
0
   i = ctx->i;
966
0
   spread = ctx->spread;
967
0
   ec = ctx->ec;
968
0
969
0
   /* If we need 1.5 more bit than we can produce, split the band in two. */
970
0
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
971
0
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
972
0
   {
973
0
      int mbits, sbits, delta;
974
0
      int itheta;
975
0
      int qalloc;
976
0
      struct split_ctx sctx;
977
0
      celt_norm *next_lowband2=NULL;
978
0
      opus_int32 rebalance;
979
0
980
0
      N >>= 1;
981
0
      Y = X+N;
982
0
      LM -= 1;
983
0
      if (B==1)
984
0
         fill = (fill&1)|(fill<<1);
985
0
      B = (B+1)>>1;
986
0
987
0
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill);
988
0
      imid = sctx.imid;
989
0
      iside = sctx.iside;
990
0
      delta = sctx.delta;
991
0
      itheta = sctx.itheta;
992
0
      qalloc = sctx.qalloc;
993
#ifdef FIXED_POINT
994
      mid = imid;
995
      side = iside;
996
#else
997
      mid = (1.f/32768)*imid;
998
0
      side = (1.f/32768)*iside;
999
0
#endif
1000
0
1001
0
      /* Give more bits to low-energy MDCTs than they would otherwise deserve */
1002
0
      if (B0>1 && (itheta&0x3fff))
1003
0
      {
1004
0
         if (itheta > 8192)
1005
0
            /* Rough approximation for pre-echo masking */
1006
0
            delta -= delta>>(4-LM);
1007
0
         else
1008
0
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1009
0
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1010
0
      }
1011
0
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1012
0
      sbits = b-mbits;
1013
0
      ctx->remaining_bits -= qalloc;
1014
0
1015
0
      if (lowband)
1016
0
         next_lowband2 = lowband+N; /* >32-bit split case */
1017
0
1018
0
      rebalance = ctx->remaining_bits;
1019
0
      if (mbits >= sbits)
1020
0
      {
1021
0
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1022
0
               MULT16_16_P15(gain,mid), fill);
1023
0
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1024
0
         if (rebalance > 3<<BITRES && itheta!=0)
1025
0
            sbits += rebalance - (3<<BITRES);
1026
0
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1027
0
               MULT16_16_P15(gain,side), fill>>B)<<(B0>>1);
1028
0
      } else {
1029
0
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1030
0
               MULT16_16_P15(gain,side), fill>>B)<<(B0>>1);
1031
0
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1032
0
         if (rebalance > 3<<BITRES && itheta!=16384)
1033
0
            mbits += rebalance - (3<<BITRES);
1034
0
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1035
0
               MULT16_16_P15(gain,mid), fill);
1036
0
      }
1037
0
   } else {
1038
0
      /* This is the basic no-split case */
1039
0
      q = bits2pulses(m, i, LM, b);
1040
0
      curr_bits = pulses2bits(m, i, LM, q);
1041
0
      ctx->remaining_bits -= curr_bits;
1042
0
1043
0
      /* Ensures we can never bust the budget */
1044
0
      while (ctx->remaining_bits < 0 && q > 0)
1045
0
      {
1046
0
         ctx->remaining_bits += curr_bits;
1047
0
         q--;
1048
0
         curr_bits = pulses2bits(m, i, LM, q);
1049
0
         ctx->remaining_bits -= curr_bits;
1050
0
      }
1051
0
1052
0
      if (q!=0)
1053
0
      {
1054
0
         int K = get_pulses(q);
1055
0
1056
0
         /* Finally do the actual quantization */
1057
0
         if (encode)
1058
0
         {
1059
0
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth, ctx->arch);
1060
0
         } else {
1061
0
            cm = alg_unquant(X, N, K, spread, B, ec, gain);
1062
0
         }
1063
0
      } else {
1064
0
         /* If there's no pulse, fill the band anyway */
1065
0
         int j;
1066
0
         if (ctx->resynth)
1067
0
         {
1068
0
            unsigned cm_mask;
1069
0
            /* B can be as large as 16, so this shift might overflow an int on a
1070
0
               16-bit platform; use a long to get defined behavior.*/
1071
0
            cm_mask = (unsigned)(1UL<<B)-1;
1072
0
            fill &= cm_mask;
1073
0
            if (!fill)
1074
0
            {
1075
0
               OPUS_CLEAR(X, N);
1076
0
            } else {
1077
0
               if (lowband == NULL)
1078
0
               {
1079
0
                  /* Noise */
1080
0
                  for (j=0;j<N;j++)
1081
0
                  {
1082
0
                     ctx->seed = celt_lcg_rand(ctx->seed);
1083
0
                     X[j] = (celt_norm)((opus_int32)ctx->seed>>20);
1084
0
                  }
1085
0
                  cm = cm_mask;
1086
0
               } else {
1087
0
                  /* Folded spectrum */
1088
0
                  for (j=0;j<N;j++)
1089
0
                  {
1090
0
                     opus_val16 tmp;
1091
0
                     ctx->seed = celt_lcg_rand(ctx->seed);
1092
0
                     /* About 48 dB below the "normal" folding level */
1093
0
                     tmp = QCONST16(1.0f/256, 10);
1094
0
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1095
0
                     X[j] = lowband[j]+tmp;
1096
0
                  }
1097
0
                  cm = fill;
1098
0
               }
1099
0
               renormalise_vector(X, N, gain, ctx->arch);
1100
0
            }
1101
0
         }
1102
0
      }
1103
0
   }
1104
0
1105
0
   return cm;
1106
0
}
1107
1108
1109
/* This function is responsible for encoding and decoding a band for the mono case. */
1110
static unsigned quant_band(struct band_ctx *ctx, celt_norm *X,
1111
      int N, int b, int B, celt_norm *lowband,
1112
      int LM, celt_norm *lowband_out,
1113
      opus_val16 gain, celt_norm *lowband_scratch, int fill)
1114
0
{
1115
0
   int N0=N;
1116
0
   int N_B=N;
1117
0
   int N_B0;
1118
0
   int B0=B;
1119
0
   int time_divide=0;
1120
0
   int recombine=0;
1121
0
   int longBlocks;
1122
0
   unsigned cm=0;
1123
0
   int k;
1124
0
   int encode;
1125
0
   int tf_change;
1126
0
1127
0
   encode = ctx->encode;
1128
0
   tf_change = ctx->tf_change;
1129
0
1130
0
   longBlocks = B0==1;
1131
0
1132
0
   N_B = celt_udiv(N_B, B);
1133
0
1134
0
   /* Special case for one sample */
1135
0
   if (N==1)
1136
0
   {
1137
0
      return quant_band_n1(ctx, X, NULL, b, lowband_out);
1138
0
   }
1139
0
1140
0
   if (tf_change>0)
1141
0
      recombine = tf_change;
1142
0
   /* Band recombining to increase frequency resolution */
1143
0
1144
0
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1145
0
   {
1146
0
      OPUS_COPY(lowband_scratch, lowband, N);
1147
0
      lowband = lowband_scratch;
1148
0
   }
1149
0
1150
0
   for (k=0;k<recombine;k++)
1151
0
   {
1152
0
      static const unsigned char bit_interleave_table[16]={
1153
0
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1154
0
      };
1155
0
      if (encode)
1156
0
         haar1(X, N>>k, 1<<k);
1157
0
      if (lowband)
1158
0
         haar1(lowband, N>>k, 1<<k);
1159
0
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1160
0
   }
1161
0
   B>>=recombine;
1162
0
   N_B<<=recombine;
1163
0
1164
0
   /* Increasing the time resolution */
1165
0
   while ((N_B&1) == 0 && tf_change<0)
1166
0
   {
1167
0
      if (encode)
1168
0
         haar1(X, N_B, B);
1169
0
      if (lowband)
1170
0
         haar1(lowband, N_B, B);
1171
0
      fill |= fill<<B;
1172
0
      B <<= 1;
1173
0
      N_B >>= 1;
1174
0
      time_divide++;
1175
0
      tf_change++;
1176
0
   }
1177
0
   B0=B;
1178
0
   N_B0 = N_B;
1179
0
1180
0
   /* Reorganize the samples in time order instead of frequency order */
1181
0
   if (B0>1)
1182
0
   {
1183
0
      if (encode)
1184
0
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1185
0
      if (lowband)
1186
0
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1187
0
   }
1188
0
1189
0
   cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill);
1190
0
1191
0
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1192
0
   if (ctx->resynth)
1193
0
   {
1194
0
      /* Undo the sample reorganization going from time order to frequency order */
1195
0
      if (B0>1)
1196
0
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1197
0
1198
0
      /* Undo time-freq changes that we did earlier */
1199
0
      N_B = N_B0;
1200
0
      B = B0;
1201
0
      for (k=0;k<time_divide;k++)
1202
0
      {
1203
0
         B >>= 1;
1204
0
         N_B <<= 1;
1205
0
         cm |= cm>>B;
1206
0
         haar1(X, N_B, B);
1207
0
      }
1208
0
1209
0
      for (k=0;k<recombine;k++)
1210
0
      {
1211
0
         static const unsigned char bit_deinterleave_table[16]={
1212
0
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1213
0
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1214
0
         };
1215
0
         cm = bit_deinterleave_table[cm];
1216
0
         haar1(X, N0>>k, 1<<k);
1217
0
      }
1218
0
      B<<=recombine;
1219
0
1220
0
      /* Scale output for later folding */
1221
0
      if (lowband_out)
1222
0
      {
1223
0
         int j;
1224
0
         opus_val16 n;
1225
0
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1226
0
         for (j=0;j<N0;j++)
1227
0
            lowband_out[j] = MULT16_16_Q15(n,X[j]);
1228
0
      }
1229
0
      cm &= (1<<B)-1;
1230
0
   }
1231
0
   return cm;
1232
0
}
1233
1234
1235
/* This function is responsible for encoding and decoding a band for the stereo case. */
1236
static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm *Y,
1237
      int N, int b, int B, celt_norm *lowband,
1238
      int LM, celt_norm *lowband_out,
1239
      celt_norm *lowband_scratch, int fill)
1240
0
{
1241
0
   int imid=0, iside=0;
1242
0
   int inv = 0;
1243
0
   opus_val16 mid=0, side=0;
1244
0
   unsigned cm=0;
1245
0
   int mbits, sbits, delta;
1246
0
   int itheta;
1247
0
   int qalloc;
1248
0
   struct split_ctx sctx;
1249
0
   int orig_fill;
1250
0
   int encode;
1251
0
   ec_ctx *ec;
1252
0
1253
0
   encode = ctx->encode;
1254
0
   ec = ctx->ec;
1255
0
1256
0
   /* Special case for one sample */
1257
0
   if (N==1)
1258
0
   {
1259
0
      return quant_band_n1(ctx, X, Y, b, lowband_out);
1260
0
   }
1261
0
1262
0
   orig_fill = fill;
1263
0
1264
0
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill);
1265
0
   inv = sctx.inv;
1266
0
   imid = sctx.imid;
1267
0
   iside = sctx.iside;
1268
0
   delta = sctx.delta;
1269
0
   itheta = sctx.itheta;
1270
0
   qalloc = sctx.qalloc;
1271
#ifdef FIXED_POINT
1272
   mid = imid;
1273
   side = iside;
1274
#else
1275
   mid = (1.f/32768)*imid;
1276
0
   side = (1.f/32768)*iside;
1277
0
#endif
1278
0
1279
0
   /* This is a special case for N=2 that only works for stereo and takes
1280
0
      advantage of the fact that mid and side are orthogonal to encode
1281
0
      the side with just one bit. */
1282
0
   if (N==2)
1283
0
   {
1284
0
      int c;
1285
0
      int sign=0;
1286
0
      celt_norm *x2, *y2;
1287
0
      mbits = b;
1288
0
      sbits = 0;
1289
0
      /* Only need one bit for the side. */
1290
0
      if (itheta != 0 && itheta != 16384)
1291
0
         sbits = 1<<BITRES;
1292
0
      mbits -= sbits;
1293
0
      c = itheta > 8192;
1294
0
      ctx->remaining_bits -= qalloc+sbits;
1295
0
1296
0
      x2 = c ? Y : X;
1297
0
      y2 = c ? X : Y;
1298
0
      if (sbits)
1299
0
      {
1300
0
         if (encode)
1301
0
         {
1302
0
            /* Here we only need to encode a sign for the side. */
1303
0
            sign = x2[0]*y2[1] - x2[1]*y2[0] < 0;
1304
0
            ec_enc_bits(ec, sign, 1);
1305
0
         } else {
1306
0
            sign = ec_dec_bits(ec, 1);
1307
0
         }
1308
0
      }
1309
0
      sign = 1-2*sign;
1310
0
      /* We use orig_fill here because we want to fold the side, but if
1311
0
         itheta==16384, we'll have cleared the low bits of fill. */
1312
0
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q15ONE,
1313
0
            lowband_scratch, orig_fill);
1314
0
      /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse),
1315
0
         and there's no need to worry about mixing with the other channel. */
1316
0
      y2[0] = -sign*x2[1];
1317
0
      y2[1] = sign*x2[0];
1318
0
      if (ctx->resynth)
1319
0
      {
1320
0
         celt_norm tmp;
1321
0
         X[0] = MULT16_16_Q15(mid, X[0]);
1322
0
         X[1] = MULT16_16_Q15(mid, X[1]);
1323
0
         Y[0] = MULT16_16_Q15(side, Y[0]);
1324
0
         Y[1] = MULT16_16_Q15(side, Y[1]);
1325
0
         tmp = X[0];
1326
0
         X[0] = SUB16(tmp,Y[0]);
1327
0
         Y[0] = ADD16(tmp,Y[0]);
1328
0
         tmp = X[1];
1329
0
         X[1] = SUB16(tmp,Y[1]);
1330
0
         Y[1] = ADD16(tmp,Y[1]);
1331
0
      }
1332
0
   } else {
1333
0
      /* "Normal" split code */
1334
0
      opus_int32 rebalance;
1335
0
1336
0
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1337
0
      sbits = b-mbits;
1338
0
      ctx->remaining_bits -= qalloc;
1339
0
1340
0
      rebalance = ctx->remaining_bits;
1341
0
      if (mbits >= sbits)
1342
0
      {
1343
0
         /* In stereo mode, we do not apply a scaling to the mid because we need the normalized
1344
0
            mid for folding later. */
1345
0
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q15ONE,
1346
0
               lowband_scratch, fill);
1347
0
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1348
0
         if (rebalance > 3<<BITRES && itheta!=0)
1349
0
            sbits += rebalance - (3<<BITRES);
1350
0
1351
0
         /* For a stereo split, the high bits of fill are always zero, so no
1352
0
            folding will be done to the side. */
1353
0
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B);
1354
0
      } else {
1355
0
         /* For a stereo split, the high bits of fill are always zero, so no
1356
0
            folding will be done to the side. */
1357
0
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B);
1358
0
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1359
0
         if (rebalance > 3<<BITRES && itheta!=16384)
1360
0
            mbits += rebalance - (3<<BITRES);
1361
0
         /* In stereo mode, we do not apply a scaling to the mid because we need the normalized
1362
0
            mid for folding later. */
1363
0
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q15ONE,
1364
0
               lowband_scratch, fill);
1365
0
      }
1366
0
   }
1367
0
1368
0
1369
0
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1370
0
   if (ctx->resynth)
1371
0
   {
1372
0
      if (N!=2)
1373
0
         stereo_merge(X, Y, mid, N, ctx->arch);
1374
0
      if (inv)
1375
0
      {
1376
0
         int j;
1377
0
         for (j=0;j<N;j++)
1378
0
            Y[j] = -Y[j];
1379
0
      }
1380
0
   }
1381
0
   return cm;
1382
0
}
1383
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
0
   /* Duplicate enough of the first band folding data to be able to fold the second band.
1391
0
      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
1397
void quant_all_bands(int encode, const CELTMode *m, int start, int end,
1398
      celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks,
1399
      const celt_ener *bandE, int *pulses, int shortBlocks, int spread,
1400
      int dual_stereo, int intensity, int *tf_res, opus_int32 total_bits,
1401
      opus_int32 balance, ec_ctx *ec, int LM, int codedBands,
1402
      opus_uint32 *seed, int complexity, int arch, int disable_inv)
1403
0
{
1404
0
   int i;
1405
0
   opus_int32 remaining_bits;
1406
0
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1407
0
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1408
0
   VARDECL(celt_norm, _norm);
1409
0
   VARDECL(celt_norm, _lowband_scratch);
1410
0
   VARDECL(celt_norm, X_save);
1411
0
   VARDECL(celt_norm, Y_save);
1412
0
   VARDECL(celt_norm, X_save2);
1413
0
   VARDECL(celt_norm, Y_save2);
1414
0
   VARDECL(celt_norm, norm_save2);
1415
0
   int resynth_alloc;
1416
0
   celt_norm *lowband_scratch;
1417
0
   int B;
1418
0
   int M;
1419
0
   int lowband_offset;
1420
0
   int update_lowband = 1;
1421
0
   int C = Y_ != NULL ? 2 : 1;
1422
0
   int norm_offset;
1423
0
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1424
#ifdef RESYNTH
1425
   int resynth = 1;
1426
#else
1427
0
   int resynth = !encode || theta_rdo;
1428
0
#endif
1429
0
   struct band_ctx ctx;
1430
0
   SAVE_STACK;
1431
0
1432
0
   M = 1<<LM;
1433
0
   B = shortBlocks ? M : 1;
1434
0
   norm_offset = M*eBands[start];
1435
0
   /* No need to allocate norm for the last band because we don't need an
1436
0
      output in that band. */
1437
0
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1438
0
   norm = _norm;
1439
0
   norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset;
1440
0
1441
0
   /* For decoding, we can use the last band as scratch space because we don't need that
1442
0
      scratch space for the last band and we don't care about the data there until we're
1443
0
      decoding the last band. */
1444
0
   if (encode && resynth)
1445
0
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1446
0
   else
1447
0
      resynth_alloc = ALLOC_NONE;
1448
0
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1449
0
   if (encode && resynth)
1450
0
      lowband_scratch = _lowband_scratch;
1451
0
   else
1452
0
      lowband_scratch = X_+M*eBands[m->nbEBands-1];
1453
0
   ALLOC(X_save, resynth_alloc, celt_norm);
1454
0
   ALLOC(Y_save, resynth_alloc, celt_norm);
1455
0
   ALLOC(X_save2, resynth_alloc, celt_norm);
1456
0
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1457
0
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1458
0
1459
0
   lowband_offset = 0;
1460
0
   ctx.bandE = bandE;
1461
0
   ctx.ec = ec;
1462
0
   ctx.encode = encode;
1463
0
   ctx.intensity = intensity;
1464
0
   ctx.m = m;
1465
0
   ctx.seed = *seed;
1466
0
   ctx.spread = spread;
1467
0
   ctx.arch = arch;
1468
0
   ctx.disable_inv = disable_inv;
1469
0
   ctx.resynth = resynth;
1470
0
   ctx.theta_round = 0;
1471
0
   /* Avoid injecting noise in the first band on transients. */
1472
0
   ctx.avoid_split_noise = B > 1;
1473
0
   for (i=start;i<end;i++)
1474
0
   {
1475
0
      opus_int32 tell;
1476
0
      int b;
1477
0
      int N;
1478
0
      opus_int32 curr_balance;
1479
0
      int effective_lowband=-1;
1480
0
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1481
0
      int tf_change=0;
1482
0
      unsigned x_cm;
1483
0
      unsigned y_cm;
1484
0
      int last;
1485
0
1486
0
      ctx.i = i;
1487
0
      last = (i==end-1);
1488
0
1489
0
      X = X_+M*eBands[i];
1490
0
      if (Y_!=NULL)
1491
0
         Y = Y_+M*eBands[i];
1492
0
      else
1493
0
         Y = NULL;
1494
0
      N = M*eBands[i+1]-M*eBands[i];
1495
0
      celt_assert(N > 0);
1496
0
      tell = ec_tell_frac(ec);
1497
0
1498
0
      /* Compute how many bits we want to allocate to this band */
1499
0
      if (i != start)
1500
0
         balance -= tell;
1501
0
      remaining_bits = total_bits-tell-1;
1502
0
      ctx.remaining_bits = remaining_bits;
1503
0
      if (i <= codedBands-1)
1504
0
      {
1505
0
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1506
0
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1507
0
      } else {
1508
0
         b = 0;
1509
0
      }
1510
0
1511
0
#ifndef DISABLE_UPDATE_DRAFT
1512
0
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1513
0
            lowband_offset = i;
1514
0
      if (i == start+1)
1515
0
         special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1516
#else
1517
      if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0))
1518
            lowband_offset = i;
1519
#endif
1520
1521
0
      tf_change = tf_res[i];
1522
0
      ctx.tf_change = tf_change;
1523
0
      if (i>=m->effEBands)
1524
0
      {
1525
0
         X=norm;
1526
0
         if (Y_!=NULL)
1527
0
            Y = norm;
1528
0
         lowband_scratch = NULL;
1529
0
      }
1530
0
      if (last && !theta_rdo)
1531
0
         lowband_scratch = NULL;
1532
0
1533
0
      /* Get a conservative estimate of the collapse_mask's for the bands we're
1534
0
         going to be folding from. */
1535
0
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1536
0
      {
1537
0
         int fold_start;
1538
0
         int fold_end;
1539
0
         int fold_i;
1540
0
         /* This ensures we never repeat spectral content within one band */
1541
0
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1542
0
         fold_start = lowband_offset;
1543
0
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1544
0
         fold_end = lowband_offset-1;
1545
0
#ifndef DISABLE_UPDATE_DRAFT
1546
0
         while(++fold_end < i && M*eBands[fold_end] < effective_lowband+norm_offset+N);
1547
#else
1548
         while(M*eBands[++fold_end] < effective_lowband+norm_offset+N);
1549
#endif
1550
         x_cm = y_cm = 0;
1551
0
         fold_i = fold_start; do {
1552
0
           x_cm |= collapse_masks[fold_i*C+0];
1553
0
           y_cm |= collapse_masks[fold_i*C+C-1];
1554
0
         } while (++fold_i<fold_end);
1555
0
      }
1556
0
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1557
0
         always) be non-zero. */
1558
0
      else
1559
0
         x_cm = y_cm = (1<<B)-1;
1560
0
1561
0
      if (dual_stereo && i==intensity)
1562
0
      {
1563
0
         int j;
1564
0
1565
0
         /* Switch off dual stereo to do intensity. */
1566
0
         dual_stereo = 0;
1567
0
         if (resynth)
1568
0
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1569
0
               norm[j] = HALF32(norm[j]+norm2[j]);
1570
0
      }
1571
0
      if (dual_stereo)
1572
0
      {
1573
0
         x_cm = quant_band(&ctx, X, N, b/2, B,
1574
0
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1575
0
               last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, x_cm);
1576
0
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1577
0
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1578
0
               last?NULL:norm2+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, y_cm);
1579
0
      } else {
1580
0
         if (Y!=NULL)
1581
0
         {
1582
0
            if (theta_rdo && i < intensity)
1583
0
            {
1584
0
               ec_ctx ec_save, ec_save2;
1585
0
               struct band_ctx ctx_save, ctx_save2;
1586
0
               opus_val32 dist0, dist1;
1587
0
               unsigned cm, cm2;
1588
0
               int nstart_bytes, nend_bytes, save_bytes;
1589
0
               unsigned char *bytes_buf;
1590
0
               unsigned char bytes_save[1275];
1591
0
               opus_val16 w[2];
1592
0
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1593
0
               /* Make a copy. */
1594
0
               cm = x_cm|y_cm;
1595
0
               ec_save = *ec;
1596
0
               ctx_save = ctx;
1597
0
               OPUS_COPY(X_save, X, N);
1598
0
               OPUS_COPY(Y_save, Y, N);
1599
0
               /* Encode and round down. */
1600
0
               ctx.theta_round = -1;
1601
0
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1602
0
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1603
0
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm);
1604
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));
1605
0
1606
0
               /* Save first result. */
1607
0
               cm2 = x_cm;
1608
0
               ec_save2 = *ec;
1609
0
               ctx_save2 = ctx;
1610
0
               OPUS_COPY(X_save2, X, N);
1611
0
               OPUS_COPY(Y_save2, Y, N);
1612
0
               if (!last)
1613
0
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1614
0
               nstart_bytes = ec_save.offs;
1615
0
               nend_bytes = ec_save.storage;
1616
0
               bytes_buf = ec_save.buf+nstart_bytes;
1617
0
               save_bytes = nend_bytes-nstart_bytes;
1618
0
               OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1619
0
1620
0
               /* Restore */
1621
0
               *ec = ec_save;
1622
0
               ctx = ctx_save;
1623
0
               OPUS_COPY(X, X_save, N);
1624
0
               OPUS_COPY(Y, Y_save, N);
1625
0
#ifndef DISABLE_UPDATE_DRAFT
1626
0
               if (i == start+1)
1627
0
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1628
0
#endif
1629
0
               /* Encode and round up. */
1630
0
               ctx.theta_round = 1;
1631
0
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1632
0
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1633
0
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm);
1634
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));
1635
0
               if (dist0 >= dist1) {
1636
0
                  x_cm = cm2;
1637
0
                  *ec = ec_save2;
1638
0
                  ctx = ctx_save2;
1639
0
                  OPUS_COPY(X, X_save2, N);
1640
0
                  OPUS_COPY(Y, Y_save2, N);
1641
0
                  if (!last)
1642
0
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1643
0
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1644
0
               }
1645
0
            } else {
1646
0
               ctx.theta_round = 0;
1647
0
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1648
0
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1649
0
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm);
1650
0
            }
1651
0
         } else {
1652
0
            x_cm = quant_band(&ctx, X, N, b, B,
1653
0
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1654
0
                  last?NULL:norm+M*eBands[i]-norm_offset, Q15ONE, lowband_scratch, x_cm|y_cm);
1655
0
         }
1656
0
         y_cm = x_cm;
1657
0
      }
1658
0
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1659
0
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1660
0
      balance += pulses[i] + tell;
1661
0
1662
0
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1663
0
      update_lowband = b>(N<<BITRES);
1664
0
      /* We only need to avoid noise on a split for the first band. After that, we
1665
0
         have folding. */
1666
0
      ctx.avoid_split_noise = 0;
1667
0
   }
1668
0
   *seed = ctx.seed;
1669
0
1670
0
   RESTORE_STACK;
1671
0
}
1672