Coverage Report

Created: 2026-09-28 07:55

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