Coverage Report

Created: 2026-02-14 07:28

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
25.3M
{
48
25.3M
   int i;
49
326M
   for (i=0;i<N;i++)
50
325M
   {
51
325M
      if (val < thresholds[i])
52
25.0M
         break;
53
325M
   }
54
25.3M
   if (i>prev && val < thresholds[prev]+hysteresis[prev])
55
69.1k
      i=prev;
56
25.3M
   if (i<prev && val > thresholds[prev-1]-hysteresis[prev-1])
57
2.46k
      i=prev;
58
25.3M
   return i;
59
25.3M
}
60
61
opus_uint32 celt_lcg_rand(opus_uint32 seed)
62
1.23G
{
63
1.23G
   return 1664525 * seed + 1013904223;
64
1.23G
}
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
71.4M
{
70
71.4M
   opus_int32 tmp;
71
71.4M
   opus_int16 x2;
72
71.4M
   tmp = (4096+((opus_int32)(x)*(x)))>>13;
73
71.4M
   celt_sig_assert(tmp<=32767);
74
71.4M
   x2 = tmp;
75
71.4M
   x2 = (32767-x2) + FRAC_MUL16(x2, (-7651 + FRAC_MUL16(x2, (8277 + FRAC_MUL16(-626, x2)))));
76
71.4M
   celt_sig_assert(x2<=32766);
77
71.4M
   return 1+x2;
78
71.4M
}
79
80
int bitexact_log2tan(int isin,int icos)
81
35.7M
{
82
35.7M
   int lc;
83
35.7M
   int ls;
84
35.7M
   lc=EC_ILOG(icos);
85
35.7M
   ls=EC_ILOG(isin);
86
35.7M
   icos<<=15-lc;
87
35.7M
   isin<<=15-ls;
88
35.7M
   return (ls-lc)*(1<<11)
89
35.7M
         +FRAC_MUL16(isin, FRAC_MUL16(isin, -2597) + 7932)
90
35.7M
         -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932);
91
35.7M
}
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
49.3M
{
97
49.3M
   int i, c, N;
98
49.3M
   const opus_int16 *eBands = m->eBands;
99
49.3M
   (void)arch;
100
49.3M
   N = m->shortMdctSize<<LM;
101
57.1M
   c=0; do {
102
901M
      for (i=0;i<end;i++)
103
844M
      {
104
844M
         int j;
105
844M
         opus_val32 maxval=0;
106
844M
         opus_val32 sum = 0;
107
108
844M
         maxval = celt_maxabs32(&X[c*N+(eBands[i]<<LM)], (eBands[i+1]-eBands[i])<<LM);
109
844M
         if (maxval > 0)
110
100M
         {
111
100M
            int shift = IMAX(0, 30 - celt_ilog2(maxval+(maxval>>14)+1) - ((((m->logN[i]+7)>>BITRES)+LM+1)>>1));
112
543M
            j=eBands[i]<<LM; do {
113
543M
               opus_val32 x = SHL32(X[j+c*N],shift);
114
543M
               sum = ADD32(sum, MULT32_32_Q31(x, x));
115
543M
            } while (++j<eBands[i+1]<<LM);
116
100M
            bandE[i+c*m->nbEBands] = MAX32(maxval, PSHR32(celt_sqrt32(SHR32(sum,1)), shift));
117
743M
         } else {
118
743M
            bandE[i+c*m->nbEBands] = EPSILON;
119
743M
         }
120
844M
      }
121
57.1M
   } while (++c<C);
122
49.3M
}
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
49.1M
{
127
49.1M
   int i, c, N;
128
49.1M
   const opus_int16 *eBands = m->eBands;
129
49.1M
   N = M*m->shortMdctSize;
130
56.8M
   c=0; do {
131
839M
      i=0; do {
132
839M
         int j,shift;
133
839M
         opus_val32 E;
134
839M
         opus_val32 g;
135
839M
         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
839M
         if (E < 10) E += EPSILON;
139
839M
         shift = 30-celt_zlog2(E);
140
839M
         E = SHL32(E, shift);
141
839M
         g = celt_rcp_norm32(E);
142
6.30G
         j=M*eBands[i]; do {
143
6.30G
            X[j+c*N] = PSHR32(MULT32_32_Q31(g, SHL32(freq[j+c*N], shift)), 30-NORM_SHIFT);
144
6.30G
         } while (++j<M*eBands[i+1]);
145
839M
      } while (++i<end);
146
56.8M
   } while (++c<C);
147
49.1M
}
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
77.1M
{
153
77.1M
   int i, c, N;
154
77.1M
   const opus_int16 *eBands = m->eBands;
155
77.1M
   N = m->shortMdctSize<<LM;
156
94.8M
   c=0; do {
157
1.51G
      for (i=0;i<end;i++)
158
1.42G
      {
159
1.42G
         opus_val32 sum;
160
1.42G
         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
1.42G
         bandE[i+c*m->nbEBands] = celt_sqrt(sum);
162
         /*printf ("%f ", bandE[i+c*m->nbEBands]);*/
163
1.42G
      }
164
94.8M
   } while (++c<C);
165
   /*printf ("\n");*/
166
77.1M
}
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
76.9M
{
171
76.9M
   int i, c, N;
172
76.9M
   const opus_int16 *eBands = m->eBands;
173
76.9M
   N = M*m->shortMdctSize;
174
94.4M
   c=0; do {
175
1.51G
      for (i=0;i<end;i++)
176
1.41G
      {
177
1.41G
         int j;
178
1.41G
         opus_val16 g = 1.f/(1e-27f+bandE[i+c*m->nbEBands]);
179
13.1G
         for (j=M*eBands[i];j<M*eBands[i+1];j++)
180
11.7G
            X[j+c*N] = freq[j+c*N]*g;
181
1.41G
      }
182
94.4M
   } while (++c<C);
183
76.9M
}
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
477k
{
192
477k
   int i, N;
193
477k
   int bound;
194
477k
   celt_sig * OPUS_RESTRICT f;
195
477k
   const celt_norm * OPUS_RESTRICT x;
196
477k
   const opus_int16 *eBands = m->eBands;
197
477k
   N = M*m->shortMdctSize;
198
477k
   bound = M*eBands[end];
199
477k
   if (downsample!=1)
200
308k
      bound = IMIN(bound, N/downsample);
201
477k
   if (silence)
202
61.5k
   {
203
61.5k
      bound = 0;
204
61.5k
      start = end = 0;
205
61.5k
   }
206
477k
   f = freq;
207
477k
   x = X+M*eBands[start];
208
477k
   if (start != 0)
209
66.0k
   {
210
12.0M
      for (i=0;i<M*eBands[start];i++)
211
12.0M
         *f++ = 0;
212
411k
   } else {
213
411k
      f += M*eBands[start];
214
411k
   }
215
6.79M
   for (i=start;i<end;i++)
216
6.31M
   {
217
6.31M
      int j, band_end;
218
6.31M
      opus_val32 g;
219
6.31M
      celt_glog lg;
220
#ifdef FIXED_POINT
221
      int shift;
222
#endif
223
6.31M
      j=M*eBands[i];
224
6.31M
      band_end = M*eBands[i+1];
225
6.31M
      lg = ADD32(bandLogE[i], SHL32((opus_val32)eMeans[i],DB_SHIFT-4));
226
#ifndef FIXED_POINT
227
2.76M
      g = celt_exp2_db(MIN32(32.f, lg));
228
#else
229
      /* Handle the integer part of the log energy */
230
3.54M
      shift = 17-(lg>>DB_SHIFT);
231
3.54M
      if (shift>=31)
232
62.3k
      {
233
62.3k
         shift=0;
234
62.3k
         g=0;
235
3.48M
      } else {
236
         /* Handle the fractional part. */
237
3.48M
         g = SHL32(celt_exp2_db_frac((lg&((1<<DB_SHIFT)-1))), 2);
238
3.48M
      }
239
      /* Handle extreme gains with negative shift. */
240
3.54M
      if (shift<0)
241
42.6k
      {
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.6k
         g = 2147483647;
246
42.6k
         shift = 0;
247
42.6k
      }
248
#endif
249
62.0M
      do {
250
62.0M
         *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift);
251
62.0M
         x++;
252
62.0M
      } while (++j<band_end);
253
6.31M
   }
254
477k
   celt_assert(start <= end);
255
477k
   OPUS_CLEAR(&freq[bound], N-bound);
256
477k
}
denormalise_bands
Line
Count
Source
191
255k
{
192
255k
   int i, N;
193
255k
   int bound;
194
255k
   celt_sig * OPUS_RESTRICT f;
195
255k
   const celt_norm * OPUS_RESTRICT x;
196
255k
   const opus_int16 *eBands = m->eBands;
197
255k
   N = M*m->shortMdctSize;
198
255k
   bound = M*eBands[end];
199
255k
   if (downsample!=1)
200
159k
      bound = IMIN(bound, N/downsample);
201
255k
   if (silence)
202
28.9k
   {
203
28.9k
      bound = 0;
204
28.9k
      start = end = 0;
205
28.9k
   }
206
255k
   f = freq;
207
255k
   x = X+M*eBands[start];
208
255k
   if (start != 0)
209
31.3k
   {
210
5.64M
      for (i=0;i<M*eBands[start];i++)
211
5.61M
         *f++ = 0;
212
224k
   } else {
213
224k
      f += M*eBands[start];
214
224k
   }
215
3.80M
   for (i=start;i<end;i++)
216
3.54M
   {
217
3.54M
      int j, band_end;
218
3.54M
      opus_val32 g;
219
3.54M
      celt_glog lg;
220
3.54M
#ifdef FIXED_POINT
221
3.54M
      int shift;
222
3.54M
#endif
223
3.54M
      j=M*eBands[i];
224
3.54M
      band_end = M*eBands[i+1];
225
3.54M
      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.54M
      shift = 17-(lg>>DB_SHIFT);
231
3.54M
      if (shift>=31)
232
62.3k
      {
233
62.3k
         shift=0;
234
62.3k
         g=0;
235
3.48M
      } else {
236
         /* Handle the fractional part. */
237
3.48M
         g = SHL32(celt_exp2_db_frac((lg&((1<<DB_SHIFT)-1))), 2);
238
3.48M
      }
239
      /* Handle extreme gains with negative shift. */
240
3.54M
      if (shift<0)
241
42.6k
      {
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.6k
         g = 2147483647;
246
42.6k
         shift = 0;
247
42.6k
      }
248
3.54M
#endif
249
33.0M
      do {
250
33.0M
         *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift);
251
33.0M
         x++;
252
33.0M
      } while (++j<band_end);
253
3.54M
   }
254
255k
   celt_assert(start <= end);
255
255k
   OPUS_CLEAR(&freq[bound], N-bound);
256
255k
}
denormalise_bands
Line
Count
Source
191
222k
{
192
222k
   int i, N;
193
222k
   int bound;
194
222k
   celt_sig * OPUS_RESTRICT f;
195
222k
   const celt_norm * OPUS_RESTRICT x;
196
222k
   const opus_int16 *eBands = m->eBands;
197
222k
   N = M*m->shortMdctSize;
198
222k
   bound = M*eBands[end];
199
222k
   if (downsample!=1)
200
149k
      bound = IMIN(bound, N/downsample);
201
222k
   if (silence)
202
32.6k
   {
203
32.6k
      bound = 0;
204
32.6k
      start = end = 0;
205
32.6k
   }
206
222k
   f = freq;
207
222k
   x = X+M*eBands[start];
208
222k
   if (start != 0)
209
34.6k
   {
210
6.45M
      for (i=0;i<M*eBands[start];i++)
211
6.42M
         *f++ = 0;
212
187k
   } else {
213
187k
      f += M*eBands[start];
214
187k
   }
215
2.99M
   for (i=start;i<end;i++)
216
2.76M
   {
217
2.76M
      int j, band_end;
218
2.76M
      opus_val32 g;
219
2.76M
      celt_glog lg;
220
#ifdef FIXED_POINT
221
      int shift;
222
#endif
223
2.76M
      j=M*eBands[i];
224
2.76M
      band_end = M*eBands[i+1];
225
2.76M
      lg = ADD32(bandLogE[i], SHL32((opus_val32)eMeans[i],DB_SHIFT-4));
226
2.76M
#ifndef FIXED_POINT
227
2.76M
      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
29.0M
      do {
250
29.0M
         *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift);
251
29.0M
         x++;
252
29.0M
      } while (++j<band_end);
253
2.76M
   }
254
222k
   celt_assert(start <= end);
255
222k
   OPUS_CLEAR(&freq[bound], N-bound);
256
222k
}
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.59k
{
263
6.59k
   int c, i, j, k;
264
112k
   for (i=start;i<end;i++)
265
105k
   {
266
105k
      int N0;
267
105k
      opus_val16 thresh, sqrt_1;
268
105k
      int depth;
269
#ifdef FIXED_POINT
270
      int shift;
271
      opus_val32 thresh32;
272
#endif
273
274
105k
      N0 = m->eBands[i+1]-m->eBands[i];
275
      /* depth in 1/8 bits */
276
105k
      celt_sig_assert(pulses[i]>=0);
277
105k
      depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i]))>>LM;
278
279
#ifdef FIXED_POINT
280
55.4k
      thresh32 = SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1);
281
55.4k
      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
55.4k
         t = SHL32(t, (7-shift)<<1);
287
         sqrt_1 = celt_rsqrt_norm(t);
288
      }
289
#else
290
      thresh = .5f*celt_exp2(-.125f*depth);
291
50.2k
      sqrt_1 = celt_rsqrt(N0<<LM);
292
#endif
293
294
105k
      c=0; do
295
185k
      {
296
185k
         celt_norm *X;
297
185k
         celt_glog prev1;
298
185k
         celt_glog prev2;
299
185k
         opus_val32 Ediff;
300
185k
         celt_norm r;
301
185k
         int renormalize=0;
302
185k
         prev1 = prev1logE[c*m->nbEBands+i];
303
185k
         prev2 = prev2logE[c*m->nbEBands+i];
304
185k
         if (!encode && C==1)
305
26.4k
         {
306
26.4k
            prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]);
307
26.4k
            prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]);
308
26.4k
         }
309
185k
         Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2);
310
185k
         Ediff = MAX32(0, Ediff);
311
312
#ifdef FIXED_POINT
313
95.2k
         if (Ediff < GCONST(16.f))
314
15.0k
         {
315
15.0k
            opus_val32 r32 = SHR32(celt_exp2_db(-Ediff),1);
316
15.0k
            r = 2*MIN16(16383,r32);
317
80.1k
         } else {
318
80.1k
            r = 0;
319
80.1k
         }
320
95.2k
         if (LM==3)
321
15.5k
            r = MULT16_16_Q14(23170, MIN32(23169, r));
322
95.2k
         r = SHR16(MIN16(thresh, r),1);
323
95.2k
         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
89.8k
         r = 2.f*celt_exp2_db(-Ediff);
328
89.8k
         if (LM==3)
329
14.9k
            r *= 1.41421356f;
330
89.8k
         r = MIN16(thresh, r);
331
         r = r*sqrt_1;
332
#endif
333
185k
         X = X_+c*size+(m->eBands[i]<<LM);
334
1.04M
         for (k=0;k<1<<LM;k++)
335
862k
         {
336
            /* Detect collapse */
337
862k
            if (!(collapse_masks[i*C+c]&1<<k))
338
87.3k
            {
339
               /* Fill with noise */
340
333k
               for (j=0;j<N0;j++)
341
246k
               {
342
246k
                  seed = celt_lcg_rand(seed);
343
246k
                  X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
344
246k
               }
345
87.3k
               renormalize = 1;
346
87.3k
            }
347
862k
         }
348
         /* We just added some energy, so we need to renormalise */
349
185k
         if (renormalize)
350
33.5k
            renormalise_vector(X, N0<<LM, Q31ONE, arch);
351
185k
      } while (++c<C);
352
105k
   }
353
6.59k
}
anti_collapse
Line
Count
Source
262
3.49k
{
263
3.49k
   int c, i, j, k;
264
58.9k
   for (i=start;i<end;i++)
265
55.4k
   {
266
55.4k
      int N0;
267
55.4k
      opus_val16 thresh, sqrt_1;
268
55.4k
      int depth;
269
55.4k
#ifdef FIXED_POINT
270
55.4k
      int shift;
271
55.4k
      opus_val32 thresh32;
272
55.4k
#endif
273
274
55.4k
      N0 = m->eBands[i+1]-m->eBands[i];
275
      /* depth in 1/8 bits */
276
55.4k
      celt_sig_assert(pulses[i]>=0);
277
55.4k
      depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i]))>>LM;
278
279
55.4k
#ifdef FIXED_POINT
280
55.4k
      thresh32 = SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1);
281
55.4k
      thresh = MULT16_32_Q15(QCONST16(0.5f, 15), MIN32(32767,thresh32));
282
55.4k
      {
283
55.4k
         opus_val32 t;
284
55.4k
         t = N0<<LM;
285
55.4k
         shift = celt_ilog2(t)>>1;
286
55.4k
         t = SHL32(t, (7-shift)<<1);
287
55.4k
         sqrt_1 = celt_rsqrt_norm(t);
288
55.4k
      }
289
#else
290
      thresh = .5f*celt_exp2(-.125f*depth);
291
      sqrt_1 = celt_rsqrt(N0<<LM);
292
#endif
293
294
55.4k
      c=0; do
295
95.2k
      {
296
95.2k
         celt_norm *X;
297
95.2k
         celt_glog prev1;
298
95.2k
         celt_glog prev2;
299
95.2k
         opus_val32 Ediff;
300
95.2k
         celt_norm r;
301
95.2k
         int renormalize=0;
302
95.2k
         prev1 = prev1logE[c*m->nbEBands+i];
303
95.2k
         prev2 = prev2logE[c*m->nbEBands+i];
304
95.2k
         if (!encode && C==1)
305
15.7k
         {
306
15.7k
            prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]);
307
15.7k
            prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]);
308
15.7k
         }
309
95.2k
         Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2);
310
95.2k
         Ediff = MAX32(0, Ediff);
311
312
95.2k
#ifdef FIXED_POINT
313
95.2k
         if (Ediff < GCONST(16.f))
314
15.0k
         {
315
15.0k
            opus_val32 r32 = SHR32(celt_exp2_db(-Ediff),1);
316
15.0k
            r = 2*MIN16(16383,r32);
317
80.1k
         } else {
318
80.1k
            r = 0;
319
80.1k
         }
320
95.2k
         if (LM==3)
321
15.5k
            r = MULT16_16_Q14(23170, MIN32(23169, r));
322
95.2k
         r = SHR16(MIN16(thresh, r),1);
323
95.2k
         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
95.2k
         X = X_+c*size+(m->eBands[i]<<LM);
334
538k
         for (k=0;k<1<<LM;k++)
335
443k
         {
336
            /* Detect collapse */
337
443k
            if (!(collapse_masks[i*C+c]&1<<k))
338
49.2k
            {
339
               /* Fill with noise */
340
186k
               for (j=0;j<N0;j++)
341
136k
               {
342
136k
                  seed = celt_lcg_rand(seed);
343
136k
                  X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
344
136k
               }
345
49.2k
               renormalize = 1;
346
49.2k
            }
347
443k
         }
348
         /* We just added some energy, so we need to renormalise */
349
95.2k
         if (renormalize)
350
17.9k
            renormalise_vector(X, N0<<LM, Q31ONE, arch);
351
95.2k
      } while (++c<C);
352
55.4k
   }
353
3.49k
}
anti_collapse
Line
Count
Source
262
3.09k
{
263
3.09k
   int c, i, j, k;
264
53.3k
   for (i=start;i<end;i++)
265
50.2k
   {
266
50.2k
      int N0;
267
50.2k
      opus_val16 thresh, sqrt_1;
268
50.2k
      int depth;
269
#ifdef FIXED_POINT
270
      int shift;
271
      opus_val32 thresh32;
272
#endif
273
274
50.2k
      N0 = m->eBands[i+1]-m->eBands[i];
275
      /* depth in 1/8 bits */
276
50.2k
      celt_sig_assert(pulses[i]>=0);
277
50.2k
      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
50.2k
      thresh = .5f*celt_exp2(-.125f*depth);
291
50.2k
      sqrt_1 = celt_rsqrt(N0<<LM);
292
50.2k
#endif
293
294
50.2k
      c=0; do
295
89.8k
      {
296
89.8k
         celt_norm *X;
297
89.8k
         celt_glog prev1;
298
89.8k
         celt_glog prev2;
299
89.8k
         opus_val32 Ediff;
300
89.8k
         celt_norm r;
301
89.8k
         int renormalize=0;
302
89.8k
         prev1 = prev1logE[c*m->nbEBands+i];
303
89.8k
         prev2 = prev2logE[c*m->nbEBands+i];
304
89.8k
         if (!encode && C==1)
305
10.7k
         {
306
10.7k
            prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]);
307
10.7k
            prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]);
308
10.7k
         }
309
89.8k
         Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2);
310
89.8k
         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
89.8k
         r = 2.f*celt_exp2_db(-Ediff);
328
89.8k
         if (LM==3)
329
14.9k
            r *= 1.41421356f;
330
89.8k
         r = MIN16(thresh, r);
331
89.8k
         r = r*sqrt_1;
332
89.8k
#endif
333
89.8k
         X = X_+c*size+(m->eBands[i]<<LM);
334
508k
         for (k=0;k<1<<LM;k++)
335
419k
         {
336
            /* Detect collapse */
337
419k
            if (!(collapse_masks[i*C+c]&1<<k))
338
38.0k
            {
339
               /* Fill with noise */
340
147k
               for (j=0;j<N0;j++)
341
109k
               {
342
109k
                  seed = celt_lcg_rand(seed);
343
109k
                  X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
344
109k
               }
345
38.0k
               renormalize = 1;
346
38.0k
            }
347
419k
         }
348
         /* We just added some energy, so we need to renormalise */
349
89.8k
         if (renormalize)
350
15.6k
            renormalise_vector(X, N0<<LM, Q31ONE, arch);
351
89.8k
      } while (++c<C);
352
50.2k
   }
353
3.09k
}
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
7.17M
{
364
7.17M
   celt_ener minE;
365
#ifdef FIXED_POINT
366
   int shift;
367
#endif
368
7.17M
   minE = MIN32(Ex, Ey);
369
   /* Adjustment to make the weights a bit more conservative. */
370
7.17M
   Ex = ADD32(Ex, minE/3);
371
7.17M
   Ey = ADD32(Ey, minE/3);
372
#ifdef FIXED_POINT
373
5.77M
   shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
374
#endif
375
7.17M
   w[0] = VSHR32(Ex, shift);
376
7.17M
   w[1] = VSHR32(Ey, shift);
377
7.17M
}
bands.c:compute_channel_weights
Line
Count
Source
363
5.77M
{
364
5.77M
   celt_ener minE;
365
5.77M
#ifdef FIXED_POINT
366
5.77M
   int shift;
367
5.77M
#endif
368
5.77M
   minE = MIN32(Ex, Ey);
369
   /* Adjustment to make the weights a bit more conservative. */
370
5.77M
   Ex = ADD32(Ex, minE/3);
371
5.77M
   Ey = ADD32(Ey, minE/3);
372
5.77M
#ifdef FIXED_POINT
373
5.77M
   shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
374
5.77M
#endif
375
5.77M
   w[0] = VSHR32(Ex, shift);
376
5.77M
   w[1] = VSHR32(Ey, shift);
377
5.77M
}
bands.c:compute_channel_weights
Line
Count
Source
363
1.40M
{
364
1.40M
   celt_ener minE;
365
#ifdef FIXED_POINT
366
   int shift;
367
#endif
368
1.40M
   minE = MIN32(Ex, Ey);
369
   /* Adjustment to make the weights a bit more conservative. */
370
1.40M
   Ex = ADD32(Ex, minE/3);
371
1.40M
   Ey = ADD32(Ey, minE/3);
372
#ifdef FIXED_POINT
373
   shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
374
#endif
375
1.40M
   w[0] = VSHR32(Ex, shift);
376
1.40M
   w[1] = VSHR32(Ey, shift);
377
1.40M
}
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
314M
{
381
314M
   int i = bandID;
382
314M
   int j;
383
314M
   opus_val16 a1, a2;
384
314M
   opus_val16 left, right;
385
314M
   opus_val16 norm;
386
#ifdef FIXED_POINT
387
87.5M
   int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
388
#endif
389
314M
   left = VSHR32(bandE[i],shift);
390
314M
   right = VSHR32(bandE[i+m->nbEBands],shift);
391
314M
   norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
392
#ifdef FIXED_POINT
393
87.5M
   left = MIN32(left, norm-1);
394
87.5M
   right = MIN32(right, norm-1);
395
#endif
396
314M
   a1 = DIV32_16(SHL32(EXTEND32(left),15),norm);
397
314M
   a2 = DIV32_16(SHL32(EXTEND32(right),15),norm);
398
3.74G
   for (j=0;j<N;j++)
399
3.43G
   {
400
3.43G
      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
3.43G
   }
403
314M
}
bands.c:intensity_stereo
Line
Count
Source
380
87.5M
{
381
87.5M
   int i = bandID;
382
87.5M
   int j;
383
87.5M
   opus_val16 a1, a2;
384
87.5M
   opus_val16 left, right;
385
87.5M
   opus_val16 norm;
386
87.5M
#ifdef FIXED_POINT
387
87.5M
   int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
388
87.5M
#endif
389
87.5M
   left = VSHR32(bandE[i],shift);
390
87.5M
   right = VSHR32(bandE[i+m->nbEBands],shift);
391
87.5M
   norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
392
87.5M
#ifdef FIXED_POINT
393
87.5M
   left = MIN32(left, norm-1);
394
87.5M
   right = MIN32(right, norm-1);
395
87.5M
#endif
396
87.5M
   a1 = DIV32_16(SHL32(EXTEND32(left),15),norm);
397
87.5M
   a2 = DIV32_16(SHL32(EXTEND32(right),15),norm);
398
1.00G
   for (j=0;j<N;j++)
399
922M
   {
400
922M
      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
922M
   }
403
87.5M
}
bands.c:intensity_stereo
Line
Count
Source
380
227M
{
381
227M
   int i = bandID;
382
227M
   int j;
383
227M
   opus_val16 a1, a2;
384
227M
   opus_val16 left, right;
385
227M
   opus_val16 norm;
386
#ifdef FIXED_POINT
387
   int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
388
#endif
389
227M
   left = VSHR32(bandE[i],shift);
390
227M
   right = VSHR32(bandE[i+m->nbEBands],shift);
391
227M
   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
227M
   a1 = DIV32_16(SHL32(EXTEND32(left),15),norm);
397
227M
   a2 = DIV32_16(SHL32(EXTEND32(right),15),norm);
398
2.73G
   for (j=0;j<N;j++)
399
2.50G
   {
400
2.50G
      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
2.50G
   }
403
227M
}
404
405
static void stereo_split(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, int N)
406
16.5M
{
407
16.5M
   int j;
408
172M
   for (j=0;j<N;j++)
409
155M
   {
410
155M
      opus_val32 r, l;
411
155M
      l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]);
412
155M
      r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]);
413
155M
      X[j] = ADD32(l, r);
414
155M
      Y[j] = SUB32(r, l);
415
155M
   }
416
16.5M
}
bands.c:stereo_split
Line
Count
Source
406
8.29M
{
407
8.29M
   int j;
408
86.1M
   for (j=0;j<N;j++)
409
77.9M
   {
410
77.9M
      opus_val32 r, l;
411
77.9M
      l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]);
412
77.9M
      r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]);
413
77.9M
      X[j] = ADD32(l, r);
414
77.9M
      Y[j] = SUB32(r, l);
415
77.9M
   }
416
8.29M
}
bands.c:stereo_split
Line
Count
Source
406
8.29M
{
407
8.29M
   int j;
408
86.1M
   for (j=0;j<N;j++)
409
77.9M
   {
410
77.9M
      opus_val32 r, l;
411
77.9M
      l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]);
412
77.9M
      r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]);
413
77.9M
      X[j] = ADD32(l, r);
414
77.9M
      Y[j] = SUB32(r, l);
415
77.9M
   }
416
8.29M
}
417
418
static void stereo_merge(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, opus_val32 mid, int N, int arch)
419
95.6M
{
420
95.6M
   int j;
421
95.6M
   opus_val32 xp=0, side=0;
422
95.6M
   opus_val32 El, Er;
423
#ifdef FIXED_POINT
424
   int kl, kr;
425
#endif
426
95.6M
   opus_val32 t, lgain, rgain;
427
428
   /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
429
95.6M
   xp = celt_inner_prod_norm_shift(Y, X, N, arch);
430
95.6M
   side = celt_inner_prod_norm_shift(Y, Y, N, arch);
431
   /* Compensating for the mid normalization */
432
95.6M
   xp = MULT32_32_Q31(mid, xp);
433
   /* mid and side are in Q15, not Q14 like X and Y */
434
95.6M
   El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp;
435
95.6M
   Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp;
436
95.6M
   if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
437
211k
   {
438
211k
      OPUS_COPY(Y, X, N);
439
211k
      return;
440
211k
   }
441
442
#ifdef FIXED_POINT
443
23.1M
   kl = celt_ilog2(El)>>1;
444
23.1M
   kr = celt_ilog2(Er)>>1;
445
23.1M
#endif
446
95.4M
   t = VSHR32(El, (kl<<1)-29);
447
72.3M
   lgain = celt_rsqrt_norm32(t);
448
95.4M
   t = VSHR32(Er, (kr<<1)-29);
449
72.3M
   rgain = celt_rsqrt_norm32(t);
450
451
#ifdef FIXED_POINT
452
23.1M
   if (kl < 7)
453
0
      kl = 7;
454
23.1M
   if (kr < 7)
455
0
      kr = 7;
456
#endif
457
458
1.43G
   for (j=0;j<N;j++)
459
1.33G
   {
460
1.33G
      celt_norm r, l;
461
      /* Apply mid scaling (side is already scaled) */
462
1.33G
      l = MULT32_32_Q31(mid, X[j]);
463
1.33G
      r = Y[j];
464
1.33G
      X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15);
465
1.33G
      Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15);
466
1.33G
   }
467
72.3M
}
bands.c:stereo_merge
Line
Count
Source
419
23.3M
{
420
23.3M
   int j;
421
23.3M
   opus_val32 xp=0, side=0;
422
23.3M
   opus_val32 El, Er;
423
23.3M
#ifdef FIXED_POINT
424
23.3M
   int kl, kr;
425
23.3M
#endif
426
23.3M
   opus_val32 t, lgain, rgain;
427
428
   /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
429
23.3M
   xp = celt_inner_prod_norm_shift(Y, X, N, arch);
430
23.3M
   side = celt_inner_prod_norm_shift(Y, Y, N, arch);
431
   /* Compensating for the mid normalization */
432
23.3M
   xp = MULT32_32_Q31(mid, xp);
433
   /* mid and side are in Q15, not Q14 like X and Y */
434
23.3M
   El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp;
435
23.3M
   Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp;
436
23.3M
   if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
437
209k
   {
438
209k
      OPUS_COPY(Y, X, N);
439
209k
      return;
440
209k
   }
441
442
23.1M
#ifdef FIXED_POINT
443
23.1M
   kl = celt_ilog2(El)>>1;
444
23.1M
   kr = celt_ilog2(Er)>>1;
445
23.1M
#endif
446
23.1M
   t = VSHR32(El, (kl<<1)-29);
447
23.1M
   lgain = celt_rsqrt_norm32(t);
448
23.1M
   t = VSHR32(Er, (kr<<1)-29);
449
23.1M
   rgain = celt_rsqrt_norm32(t);
450
451
23.1M
#ifdef FIXED_POINT
452
23.1M
   if (kl < 7)
453
0
      kl = 7;
454
23.1M
   if (kr < 7)
455
0
      kr = 7;
456
23.1M
#endif
457
458
303M
   for (j=0;j<N;j++)
459
280M
   {
460
280M
      celt_norm r, l;
461
      /* Apply mid scaling (side is already scaled) */
462
280M
      l = MULT32_32_Q31(mid, X[j]);
463
280M
      r = Y[j];
464
280M
      X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15);
465
280M
      Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15);
466
280M
   }
467
23.1M
}
bands.c:stereo_merge
Line
Count
Source
419
72.3M
{
420
72.3M
   int j;
421
72.3M
   opus_val32 xp=0, side=0;
422
72.3M
   opus_val32 El, Er;
423
#ifdef FIXED_POINT
424
   int kl, kr;
425
#endif
426
72.3M
   opus_val32 t, lgain, rgain;
427
428
   /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
429
72.3M
   xp = celt_inner_prod_norm_shift(Y, X, N, arch);
430
72.3M
   side = celt_inner_prod_norm_shift(Y, Y, N, arch);
431
   /* Compensating for the mid normalization */
432
72.3M
   xp = MULT32_32_Q31(mid, xp);
433
   /* mid and side are in Q15, not Q14 like X and Y */
434
72.3M
   El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp;
435
72.3M
   Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp;
436
72.3M
   if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
437
2.04k
   {
438
2.04k
      OPUS_COPY(Y, X, N);
439
2.04k
      return;
440
2.04k
   }
441
442
#ifdef FIXED_POINT
443
   kl = celt_ilog2(El)>>1;
444
   kr = celt_ilog2(Er)>>1;
445
#endif
446
72.3M
   t = VSHR32(El, (kl<<1)-29);
447
72.3M
   lgain = celt_rsqrt_norm32(t);
448
72.3M
   t = VSHR32(Er, (kr<<1)-29);
449
72.3M
   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
1.12G
   for (j=0;j<N;j++)
459
1.05G
   {
460
1.05G
      celt_norm r, l;
461
      /* Apply mid scaling (side is already scaled) */
462
1.05G
      l = MULT32_32_Q31(mid, X[j]);
463
1.05G
      r = Y[j];
464
1.05G
      X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15);
465
1.05G
      Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15);
466
1.05G
   }
467
72.3M
}
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
19.2M
{
474
19.2M
   int i, c, N0;
475
19.2M
   int sum = 0, nbBands=0;
476
19.2M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
477
19.2M
   int decision;
478
19.2M
   int hf_sum=0;
479
480
19.2M
   celt_assert(end>0);
481
482
19.2M
   N0 = M*m->shortMdctSize;
483
484
19.2M
   if (M*(eBands[end]-eBands[end-1]) <= 8)
485
7.01M
      return SPREAD_NONE;
486
14.6M
   c=0; do {
487
251M
      for (i=0;i<end;i++)
488
237M
      {
489
237M
         int j, N, tmp=0;
490
237M
         int tcount[3] = {0,0,0};
491
237M
         const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
492
237M
         N = M*(eBands[i+1]-eBands[i]);
493
237M
         if (N<=8)
494
147M
            continue;
495
         /* Compute rough CDF of |x[j]| */
496
2.57G
         for (j=0;j<N;j++)
497
2.48G
         {
498
2.48G
            opus_val32 x2N; /* Q13 */
499
500
2.48G
            x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N);
501
2.48G
            if (x2N < QCONST16(0.25f,13))
502
2.26G
               tcount[0]++;
503
2.48G
            if (x2N < QCONST16(0.0625f,13))
504
2.19G
               tcount[1]++;
505
2.48G
            if (x2N < QCONST16(0.015625f,13))
506
2.14G
               tcount[2]++;
507
2.48G
         }
508
509
         /* Only include four last bands (8 kHz and up) */
510
89.6M
         if (i>m->nbEBands-4)
511
5.05M
            hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
512
89.6M
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
513
89.6M
         sum += tmp*spread_weight[i];
514
89.6M
         nbBands+=spread_weight[i];
515
89.6M
      }
516
14.6M
   } while (++c<C);
517
518
12.2M
   if (update_hf)
519
328k
   {
520
328k
      if (hf_sum)
521
91.4k
         hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
522
328k
      *hf_average = (*hf_average+hf_sum)>>1;
523
328k
      hf_sum = *hf_average;
524
328k
      if (*tapset_decision==2)
525
45.9k
         hf_sum += 4;
526
282k
      else if (*tapset_decision==0)
527
277k
         hf_sum -= 4;
528
328k
      if (hf_sum > 22)
529
46.2k
         *tapset_decision=2;
530
282k
      else if (hf_sum > 18)
531
5.32k
         *tapset_decision=1;
532
277k
      else
533
277k
         *tapset_decision=0;
534
328k
   }
535
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
536
12.2M
   celt_assert(nbBands>0); /* end has to be non-zero */
537
12.2M
   celt_assert(sum>=0);
538
12.2M
   sum = celt_udiv((opus_int32)sum<<8, nbBands);
539
   /* Recursive averaging */
540
12.2M
   sum = (sum+*average)>>1;
541
12.2M
   *average = sum;
542
   /* Hysteresis */
543
12.2M
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
544
12.2M
   if (sum < 80)
545
1.18M
   {
546
1.18M
      decision = SPREAD_AGGRESSIVE;
547
11.0M
   } else if (sum < 256)
548
705k
   {
549
705k
      decision = SPREAD_NORMAL;
550
10.3M
   } else if (sum < 384)
551
137k
   {
552
137k
      decision = SPREAD_LIGHT;
553
10.1M
   } else {
554
10.1M
      decision = SPREAD_NONE;
555
10.1M
   }
556
#ifdef FUZZING
557
   decision = rand()&0x3;
558
   *tapset_decision=rand()%3;
559
#endif
560
12.2M
   return decision;
561
12.2M
}
spreading_decision
Line
Count
Source
473
9.61M
{
474
9.61M
   int i, c, N0;
475
9.61M
   int sum = 0, nbBands=0;
476
9.61M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
477
9.61M
   int decision;
478
9.61M
   int hf_sum=0;
479
480
9.61M
   celt_assert(end>0);
481
482
9.61M
   N0 = M*m->shortMdctSize;
483
484
9.61M
   if (M*(eBands[end]-eBands[end-1]) <= 8)
485
3.50M
      return SPREAD_NONE;
486
7.33M
   c=0; do {
487
125M
      for (i=0;i<end;i++)
488
118M
      {
489
118M
         int j, N, tmp=0;
490
118M
         int tcount[3] = {0,0,0};
491
118M
         const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
492
118M
         N = M*(eBands[i+1]-eBands[i]);
493
118M
         if (N<=8)
494
73.8M
            continue;
495
         /* Compute rough CDF of |x[j]| */
496
1.28G
         for (j=0;j<N;j++)
497
1.24G
         {
498
1.24G
            opus_val32 x2N; /* Q13 */
499
500
1.24G
            x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N);
501
1.24G
            if (x2N < QCONST16(0.25f,13))
502
1.13G
               tcount[0]++;
503
1.24G
            if (x2N < QCONST16(0.0625f,13))
504
1.09G
               tcount[1]++;
505
1.24G
            if (x2N < QCONST16(0.015625f,13))
506
1.07G
               tcount[2]++;
507
1.24G
         }
508
509
         /* Only include four last bands (8 kHz and up) */
510
44.8M
         if (i>m->nbEBands-4)
511
2.52M
            hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
512
44.8M
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
513
44.8M
         sum += tmp*spread_weight[i];
514
44.8M
         nbBands+=spread_weight[i];
515
44.8M
      }
516
7.33M
   } while (++c<C);
517
518
6.10M
   if (update_hf)
519
164k
   {
520
164k
      if (hf_sum)
521
45.7k
         hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
522
164k
      *hf_average = (*hf_average+hf_sum)>>1;
523
164k
      hf_sum = *hf_average;
524
164k
      if (*tapset_decision==2)
525
22.9k
         hf_sum += 4;
526
141k
      else if (*tapset_decision==0)
527
138k
         hf_sum -= 4;
528
164k
      if (hf_sum > 22)
529
23.1k
         *tapset_decision=2;
530
141k
      else if (hf_sum > 18)
531
2.66k
         *tapset_decision=1;
532
138k
      else
533
138k
         *tapset_decision=0;
534
164k
   }
535
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
536
6.10M
   celt_assert(nbBands>0); /* end has to be non-zero */
537
6.10M
   celt_assert(sum>=0);
538
6.10M
   sum = celt_udiv((opus_int32)sum<<8, nbBands);
539
   /* Recursive averaging */
540
6.10M
   sum = (sum+*average)>>1;
541
6.10M
   *average = sum;
542
   /* Hysteresis */
543
6.10M
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
544
6.10M
   if (sum < 80)
545
590k
   {
546
590k
      decision = SPREAD_AGGRESSIVE;
547
5.51M
   } else if (sum < 256)
548
352k
   {
549
352k
      decision = SPREAD_NORMAL;
550
5.16M
   } else if (sum < 384)
551
68.9k
   {
552
68.9k
      decision = SPREAD_LIGHT;
553
5.09M
   } else {
554
5.09M
      decision = SPREAD_NONE;
555
5.09M
   }
556
#ifdef FUZZING
557
   decision = rand()&0x3;
558
   *tapset_decision=rand()%3;
559
#endif
560
6.10M
   return decision;
561
6.10M
}
spreading_decision
Line
Count
Source
473
9.61M
{
474
9.61M
   int i, c, N0;
475
9.61M
   int sum = 0, nbBands=0;
476
9.61M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
477
9.61M
   int decision;
478
9.61M
   int hf_sum=0;
479
480
9.61M
   celt_assert(end>0);
481
482
9.61M
   N0 = M*m->shortMdctSize;
483
484
9.61M
   if (M*(eBands[end]-eBands[end-1]) <= 8)
485
3.50M
      return SPREAD_NONE;
486
7.33M
   c=0; do {
487
125M
      for (i=0;i<end;i++)
488
118M
      {
489
118M
         int j, N, tmp=0;
490
118M
         int tcount[3] = {0,0,0};
491
118M
         const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
492
118M
         N = M*(eBands[i+1]-eBands[i]);
493
118M
         if (N<=8)
494
73.8M
            continue;
495
         /* Compute rough CDF of |x[j]| */
496
1.28G
         for (j=0;j<N;j++)
497
1.24G
         {
498
1.24G
            opus_val32 x2N; /* Q13 */
499
500
1.24G
            x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N);
501
1.24G
            if (x2N < QCONST16(0.25f,13))
502
1.13G
               tcount[0]++;
503
1.24G
            if (x2N < QCONST16(0.0625f,13))
504
1.09G
               tcount[1]++;
505
1.24G
            if (x2N < QCONST16(0.015625f,13))
506
1.07G
               tcount[2]++;
507
1.24G
         }
508
509
         /* Only include four last bands (8 kHz and up) */
510
44.8M
         if (i>m->nbEBands-4)
511
2.52M
            hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
512
44.8M
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
513
44.8M
         sum += tmp*spread_weight[i];
514
44.8M
         nbBands+=spread_weight[i];
515
44.8M
      }
516
7.33M
   } while (++c<C);
517
518
6.10M
   if (update_hf)
519
164k
   {
520
164k
      if (hf_sum)
521
45.7k
         hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
522
164k
      *hf_average = (*hf_average+hf_sum)>>1;
523
164k
      hf_sum = *hf_average;
524
164k
      if (*tapset_decision==2)
525
22.9k
         hf_sum += 4;
526
141k
      else if (*tapset_decision==0)
527
138k
         hf_sum -= 4;
528
164k
      if (hf_sum > 22)
529
23.1k
         *tapset_decision=2;
530
141k
      else if (hf_sum > 18)
531
2.66k
         *tapset_decision=1;
532
138k
      else
533
138k
         *tapset_decision=0;
534
164k
   }
535
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
536
6.10M
   celt_assert(nbBands>0); /* end has to be non-zero */
537
6.10M
   celt_assert(sum>=0);
538
6.10M
   sum = celt_udiv((opus_int32)sum<<8, nbBands);
539
   /* Recursive averaging */
540
6.10M
   sum = (sum+*average)>>1;
541
6.10M
   *average = sum;
542
   /* Hysteresis */
543
6.10M
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
544
6.10M
   if (sum < 80)
545
590k
   {
546
590k
      decision = SPREAD_AGGRESSIVE;
547
5.51M
   } else if (sum < 256)
548
352k
   {
549
352k
      decision = SPREAD_NORMAL;
550
5.16M
   } else if (sum < 384)
551
68.9k
   {
552
68.9k
      decision = SPREAD_LIGHT;
553
5.09M
   } else {
554
5.09M
      decision = SPREAD_NONE;
555
5.09M
   }
556
#ifdef FUZZING
557
   decision = rand()&0x3;
558
   *tapset_decision=rand()%3;
559
#endif
560
6.10M
   return decision;
561
6.10M
}
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
20.0M
{
576
20.0M
   int i,j;
577
20.0M
   VARDECL(celt_norm, tmp);
578
20.0M
   int N;
579
20.0M
   SAVE_STACK;
580
20.0M
   N = N0*stride;
581
20.0M
   ALLOC(tmp, N, celt_norm);
582
20.0M
   celt_assert(stride>0);
583
20.0M
   if (hadamard)
584
4.27M
   {
585
4.27M
      const int *ordery = ordery_table+stride-2;
586
21.0M
      for (i=0;i<stride;i++)
587
16.7M
      {
588
88.7M
         for (j=0;j<N0;j++)
589
71.9M
            tmp[ordery[i]*N0+j] = X[j*stride+i];
590
16.7M
      }
591
15.7M
   } else {
592
126M
      for (i=0;i<stride;i++)
593
370M
         for (j=0;j<N0;j++)
594
260M
            tmp[i*N0+j] = X[j*stride+i];
595
15.7M
   }
596
20.0M
   OPUS_COPY(X, tmp, N);
597
20.0M
   RESTORE_STACK;
598
20.0M
}
599
600
static void interleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
601
3.52M
{
602
3.52M
   int i,j;
603
3.52M
   VARDECL(celt_norm, tmp);
604
3.52M
   int N;
605
3.52M
   SAVE_STACK;
606
3.52M
   N = N0*stride;
607
3.52M
   ALLOC(tmp, N, celt_norm);
608
3.52M
   if (hadamard)
609
1.35M
   {
610
1.35M
      const int *ordery = ordery_table+stride-2;
611
6.49M
      for (i=0;i<stride;i++)
612
25.7M
         for (j=0;j<N0;j++)
613
20.5M
            tmp[j*stride+i] = X[ordery[i]*N0+j];
614
2.17M
   } else {
615
19.2M
      for (i=0;i<stride;i++)
616
47.8M
         for (j=0;j<N0;j++)
617
30.8M
            tmp[j*stride+i] = X[i*N0+j];
618
2.17M
   }
619
3.52M
   OPUS_COPY(X, tmp, N);
620
3.52M
   RESTORE_STACK;
621
3.52M
}
622
623
void haar1(celt_norm *X, int N0, int stride)
624
2.44G
{
625
2.44G
   int i, j;
626
2.44G
   N0 >>= 1;
627
8.76G
   for (i=0;i<stride;i++)
628
26.5G
      for (j=0;j<N0;j++)
629
20.2G
      {
630
20.2G
         opus_val32 tmp1, tmp2;
631
20.2G
         tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]);
632
20.2G
         tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]);
633
20.2G
         X[stride*2*j+i] = ADD32(tmp1, tmp2);
634
20.2G
         X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2);
635
20.2G
      }
636
2.44G
}
haar1
Line
Count
Source
624
1.22G
{
625
1.22G
   int i, j;
626
1.22G
   N0 >>= 1;
627
4.38G
   for (i=0;i<stride;i++)
628
13.2G
      for (j=0;j<N0;j++)
629
10.1G
      {
630
10.1G
         opus_val32 tmp1, tmp2;
631
10.1G
         tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]);
632
10.1G
         tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]);
633
10.1G
         X[stride*2*j+i] = ADD32(tmp1, tmp2);
634
10.1G
         X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2);
635
10.1G
      }
636
1.22G
}
haar1
Line
Count
Source
624
1.22G
{
625
1.22G
   int i, j;
626
1.22G
   N0 >>= 1;
627
4.38G
   for (i=0;i<stride;i++)
628
13.2G
      for (j=0;j<N0;j++)
629
10.1G
      {
630
10.1G
         opus_val32 tmp1, tmp2;
631
10.1G
         tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]);
632
10.1G
         tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]);
633
10.1G
         X[stride*2*j+i] = ADD32(tmp1, tmp2);
634
10.1G
         X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2);
635
10.1G
      }
636
1.22G
}
637
638
static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo)
639
420M
{
640
420M
   static const opus_int16 exp2_table8[8] =
641
420M
      {16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048};
642
420M
   int qn, qb;
643
420M
   int N2 = 2*N-1;
644
420M
   if (stereo && N==2)
645
80.5M
      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
420M
   qb = celt_sudiv(b+N2*offset, N2);
650
420M
   qb = IMIN(b-pulse_cap-(4<<BITRES), qb);
651
652
420M
   qb = IMIN(8<<BITRES, qb);
653
654
420M
   if (qb<(1<<BITRES>>1)) {
655
287M
      qn = 1;
656
287M
   } else {
657
133M
      qn = exp2_table8[qb&0x7]>>(14-(qb>>BITRES));
658
133M
      qn = (qn+1)>>1<<1;
659
133M
   }
660
420M
   celt_assert(qn <= 256);
661
420M
   return qn;
662
420M
}
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
420M
{
705
420M
   int qn;
706
420M
   int itheta=0;
707
420M
   int itheta_q30=0;
708
420M
   int delta;
709
420M
   int imid, iside;
710
420M
   int qalloc;
711
420M
   int pulse_cap;
712
420M
   int offset;
713
420M
   opus_int32 tell;
714
420M
   int inv=0;
715
420M
   int encode;
716
420M
   const CELTMode *m;
717
420M
   int i;
718
420M
   int intensity;
719
420M
   ec_ctx *ec;
720
420M
   const celt_ener *bandE;
721
722
420M
   encode = ctx->encode;
723
420M
   m = ctx->m;
724
420M
   i = ctx->i;
725
420M
   intensity = ctx->intensity;
726
420M
   ec = ctx->ec;
727
420M
   bandE = ctx->bandE;
728
729
   /* Decide on the resolution to give to the split parameter theta */
730
420M
   pulse_cap = m->logN[i]+LM*(1<<BITRES);
731
420M
   offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
732
420M
   qn = compute_qn(N, *b, offset, pulse_cap, stereo);
733
420M
   if (stereo && i>=intensity)
734
289M
      qn = 1;
735
420M
   if (encode)
736
418M
   {
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
418M
      itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch);
742
418M
      itheta = itheta_q30>>16;
743
418M
   }
744
420M
   tell = ec_tell_frac(ec);
745
420M
   if (qn!=1)
746
126M
   {
747
126M
      if (encode)
748
126M
      {
749
126M
         if (!stereo || ctx->theta_round == 0)
750
114M
         {
751
114M
            itheta = (itheta*(opus_int32)qn+8192)>>14;
752
114M
            if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
753
440k
            {
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
440k
               int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
758
440k
               imid = bitexact_cos((opus_int16)unquantized);
759
440k
               iside = bitexact_cos((opus_int16)(16384-unquantized));
760
440k
               delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
761
440k
               if (delta > *b)
762
806
                  itheta = qn;
763
439k
               else if (delta < -*b)
764
824
                  itheta = 0;
765
440k
            }
766
114M
         } else {
767
11.6M
            int down;
768
            /* Bias quantization towards itheta=0 and itheta=16384. */
769
11.6M
            int bias = itheta > 8192 ? 32767/qn : -32767/qn;
770
11.6M
            down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
771
11.6M
            if (ctx->theta_round < 0)
772
5.84M
               itheta = down;
773
5.84M
            else
774
5.84M
               itheta = down+1;
775
11.6M
         }
776
126M
      }
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
126M
      if (stereo && N>2)
780
26.5M
      {
781
26.5M
         int p0 = 3;
782
26.5M
         int x = itheta;
783
26.5M
         int x0 = qn/2;
784
26.5M
         int ft = p0*(x0+1) + x0;
785
         /* Use a probability of p0 up to itheta=8192 and then use 1 after */
786
26.5M
         if (encode)
787
26.4M
         {
788
26.4M
            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
26.4M
         } else {
790
67.8k
            int fs;
791
67.8k
            fs=ec_decode(ec,ft);
792
67.8k
            if (fs<(x0+1)*p0)
793
47.0k
               x=fs/p0;
794
20.8k
            else
795
20.8k
               x=x0+1+(fs-(x0+1)*p0);
796
67.8k
            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
67.8k
            itheta = x;
798
67.8k
         }
799
100M
      } else if (B0>1 || stereo) {
800
         /* Uniform pdf */
801
16.1M
         if (encode)
802
15.8M
            ec_enc_uint(ec, itheta, qn+1);
803
342k
         else
804
342k
            itheta = ec_dec_uint(ec, qn+1);
805
84.2M
      } else {
806
84.2M
         int fs=1, ft;
807
84.2M
         ft = ((qn>>1)+1)*((qn>>1)+1);
808
84.2M
         if (encode)
809
83.7M
         {
810
83.7M
            int fl;
811
812
83.7M
            fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
813
83.7M
            fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
814
83.7M
             ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
815
816
83.7M
            ec_encode(ec, fl, fl+fs, ft);
817
83.7M
         } else {
818
            /* Triangular pdf */
819
438k
            int fl=0;
820
438k
            int fm;
821
438k
            fm = ec_decode(ec, ft);
822
823
438k
            if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
824
222k
            {
825
222k
               itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
826
222k
               fs = itheta + 1;
827
222k
               fl = itheta*(itheta + 1)>>1;
828
222k
            }
829
216k
            else
830
216k
            {
831
216k
               itheta = (2*(qn + 1)
832
216k
                - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
833
216k
               fs = qn + 1 - itheta;
834
216k
               fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
835
216k
            }
836
837
438k
            ec_dec_update(ec, fl, fl+fs, ft);
838
438k
         }
839
84.2M
      }
840
126M
      celt_assert(itheta>=0);
841
126M
      itheta = celt_udiv((opus_int32)itheta*16384, qn);
842
#ifdef ENABLE_QEXT
843
25.3M
      *ext_b = IMIN(*ext_b, ctx->ext_total_bits - (opus_int32)ec_tell_frac(ctx->ext_ec));
844
25.3M
      if (*ext_b >= 2*N<<BITRES && ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec)-1 > 2<<BITRES) {
845
170k
         int extra_bits;
846
170k
         int ext_tell = ec_tell_frac(ctx->ext_ec);
847
170k
         extra_bits = IMIN(12, IMAX(2, celt_sudiv(*ext_b, (2*N-1)<<BITRES)));
848
170k
         if (encode) {
849
138k
            itheta_q30 = itheta_q30 - (itheta<<16);
850
138k
            itheta_q30 = (itheta_q30*(opus_int64)qn*((1<<extra_bits)-1)+(1<<29))>>30;
851
138k
            itheta_q30 += (1<<(extra_bits-1))-1;
852
138k
            itheta_q30 = IMAX(0, IMIN((1<<extra_bits)-2, itheta_q30));
853
138k
            ec_enc_uint(ctx->ext_ec, itheta_q30, (1<<extra_bits)-1);
854
138k
         } else {
855
31.6k
            itheta_q30 = ec_dec_uint(ctx->ext_ec, (1<<extra_bits)-1);
856
31.6k
         }
857
170k
         itheta_q30 -= (1<<(extra_bits-1))-1;
858
170k
         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
170k
         itheta_q30 = IMAX(0, IMIN(itheta_q30, 1073741824));
861
170k
         *ext_b -= ec_tell_frac(ctx->ext_ec) - ext_tell;
862
25.1M
      } else {
863
25.1M
         itheta_q30 = (opus_int32)itheta<<16;
864
25.1M
      }
865
#endif
866
126M
      if (encode && stereo)
867
30.2M
      {
868
30.2M
         if (itheta==0)
869
21.9M
            intensity_stereo(m, X, Y, bandE, i, N);
870
8.29M
         else
871
8.29M
            stereo_split(X, Y, N);
872
30.2M
      }
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
294M
   } else if (stereo) {
876
294M
      if (encode)
877
292M
      {
878
292M
         inv = itheta > 8192 && !ctx->disable_inv;
879
292M
         if (inv)
880
367k
         {
881
367k
            int j;
882
8.97M
            for (j=0;j<N;j++)
883
8.60M
               Y[j] = -Y[j];
884
367k
         }
885
292M
         intensity_stereo(m, X, Y, bandE, i, N);
886
292M
      }
887
294M
      if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
888
13.4M
      {
889
13.4M
         if (encode)
890
13.3M
            ec_enc_bit_logp(ec, inv, 2);
891
156k
         else
892
156k
            inv = ec_dec_bit_logp(ec, 2);
893
13.4M
      } else
894
280M
         inv = 0;
895
      /* inv flag override to avoid problems with downmixing. */
896
294M
      if (ctx->disable_inv)
897
131M
         inv = 0;
898
294M
      itheta = 0;
899
294M
      itheta_q30 = 0;
900
294M
   }
901
420M
   qalloc = ec_tell_frac(ec) - tell;
902
420M
   *b -= qalloc;
903
904
420M
   if (itheta == 0)
905
384M
   {
906
384M
      imid = 32767;
907
384M
      iside = 0;
908
384M
      *fill &= (1<<B)-1;
909
384M
      delta = -16384;
910
384M
   } else if (itheta == 16384)
911
1.62M
   {
912
1.62M
      imid = 0;
913
1.62M
      iside = 32767;
914
1.62M
      *fill &= ((1<<B)-1)<<B;
915
1.62M
      delta = 16384;
916
35.2M
   } else {
917
35.2M
      imid = bitexact_cos((opus_int16)itheta);
918
35.2M
      iside = bitexact_cos((opus_int16)(16384-itheta));
919
      /* This is the mid vs side allocation that minimizes squared error
920
         in that band. */
921
35.2M
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
922
35.2M
   }
923
924
420M
   sctx->inv = inv;
925
420M
   sctx->imid = imid;
926
420M
   sctx->iside = iside;
927
420M
   sctx->delta = delta;
928
420M
   sctx->itheta = itheta;
929
#ifdef ENABLE_QEXT
930
   sctx->itheta_q30 = itheta_q30;
931
#endif
932
420M
   sctx->qalloc = qalloc;
933
420M
}
bands.c:compute_theta
Line
Count
Source
704
266M
{
705
266M
   int qn;
706
266M
   int itheta=0;
707
266M
   int itheta_q30=0;
708
266M
   int delta;
709
266M
   int imid, iside;
710
266M
   int qalloc;
711
266M
   int pulse_cap;
712
266M
   int offset;
713
266M
   opus_int32 tell;
714
266M
   int inv=0;
715
266M
   int encode;
716
266M
   const CELTMode *m;
717
266M
   int i;
718
266M
   int intensity;
719
266M
   ec_ctx *ec;
720
266M
   const celt_ener *bandE;
721
722
266M
   encode = ctx->encode;
723
266M
   m = ctx->m;
724
266M
   i = ctx->i;
725
266M
   intensity = ctx->intensity;
726
266M
   ec = ctx->ec;
727
266M
   bandE = ctx->bandE;
728
729
   /* Decide on the resolution to give to the split parameter theta */
730
266M
   pulse_cap = m->logN[i]+LM*(1<<BITRES);
731
266M
   offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
732
266M
   qn = compute_qn(N, *b, offset, pulse_cap, stereo);
733
266M
   if (stereo && i>=intensity)
734
160M
      qn = 1;
735
266M
   if (encode)
736
265M
   {
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
265M
      itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch);
742
265M
      itheta = itheta_q30>>16;
743
265M
   }
744
266M
   tell = ec_tell_frac(ec);
745
266M
   if (qn!=1)
746
101M
   {
747
101M
      if (encode)
748
101M
      {
749
101M
         if (!stereo || ctx->theta_round == 0)
750
90.1M
         {
751
90.1M
            itheta = (itheta*(opus_int32)qn+8192)>>14;
752
90.1M
            if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
753
345k
            {
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
345k
               int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
758
345k
               imid = bitexact_cos((opus_int16)unquantized);
759
345k
               iside = bitexact_cos((opus_int16)(16384-unquantized));
760
345k
               delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
761
345k
               if (delta > *b)
762
378
                  itheta = qn;
763
345k
               else if (delta < -*b)
764
355
                  itheta = 0;
765
345k
            }
766
90.1M
         } else {
767
11.0M
            int down;
768
            /* Bias quantization towards itheta=0 and itheta=16384. */
769
11.0M
            int bias = itheta > 8192 ? 32767/qn : -32767/qn;
770
11.0M
            down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
771
11.0M
            if (ctx->theta_round < 0)
772
5.53M
               itheta = down;
773
5.53M
            else
774
5.53M
               itheta = down+1;
775
11.0M
         }
776
101M
      }
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
101M
      if (stereo && N>2)
780
22.1M
      {
781
22.1M
         int p0 = 3;
782
22.1M
         int x = itheta;
783
22.1M
         int x0 = qn/2;
784
22.1M
         int ft = p0*(x0+1) + x0;
785
         /* Use a probability of p0 up to itheta=8192 and then use 1 after */
786
22.1M
         if (encode)
787
22.1M
         {
788
22.1M
            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
22.1M
         } else {
790
26.4k
            int fs;
791
26.4k
            fs=ec_decode(ec,ft);
792
26.4k
            if (fs<(x0+1)*p0)
793
18.4k
               x=fs/p0;
794
8.00k
            else
795
8.00k
               x=x0+1+(fs-(x0+1)*p0);
796
26.4k
            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
26.4k
            itheta = x;
798
26.4k
         }
799
79.3M
      } else if (B0>1 || stereo) {
800
         /* Uniform pdf */
801
11.6M
         if (encode)
802
11.5M
            ec_enc_uint(ec, itheta, qn+1);
803
163k
         else
804
163k
            itheta = ec_dec_uint(ec, qn+1);
805
67.6M
      } else {
806
67.6M
         int fs=1, ft;
807
67.6M
         ft = ((qn>>1)+1)*((qn>>1)+1);
808
67.6M
         if (encode)
809
67.4M
         {
810
67.4M
            int fl;
811
812
67.4M
            fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
813
67.4M
            fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
814
67.4M
             ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
815
816
67.4M
            ec_encode(ec, fl, fl+fs, ft);
817
67.4M
         } else {
818
            /* Triangular pdf */
819
167k
            int fl=0;
820
167k
            int fm;
821
167k
            fm = ec_decode(ec, ft);
822
823
167k
            if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
824
84.3k
            {
825
84.3k
               itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
826
84.3k
               fs = itheta + 1;
827
84.3k
               fl = itheta*(itheta + 1)>>1;
828
84.3k
            }
829
83.5k
            else
830
83.5k
            {
831
83.5k
               itheta = (2*(qn + 1)
832
83.5k
                - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
833
83.5k
               fs = qn + 1 - itheta;
834
83.5k
               fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
835
83.5k
            }
836
837
167k
            ec_dec_update(ec, fl, fl+fs, ft);
838
167k
         }
839
67.6M
      }
840
101M
      celt_assert(itheta>=0);
841
101M
      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
101M
      if (encode && stereo)
867
25.5M
      {
868
25.5M
         if (itheta==0)
869
18.1M
            intensity_stereo(m, X, Y, bandE, i, N);
870
7.38M
         else
871
7.38M
            stereo_split(X, Y, N);
872
25.5M
      }
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
165M
   } else if (stereo) {
876
165M
      if (encode)
877
164M
      {
878
164M
         inv = itheta > 8192 && !ctx->disable_inv;
879
164M
         if (inv)
880
286k
         {
881
286k
            int j;
882
7.24M
            for (j=0;j<N;j++)
883
6.95M
               Y[j] = -Y[j];
884
286k
         }
885
164M
         intensity_stereo(m, X, Y, bandE, i, N);
886
164M
      }
887
165M
      if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
888
11.0M
      {
889
11.0M
         if (encode)
890
11.0M
            ec_enc_bit_logp(ec, inv, 2);
891
66.6k
         else
892
66.6k
            inv = ec_dec_bit_logp(ec, 2);
893
11.0M
      } else
894
153M
         inv = 0;
895
      /* inv flag override to avoid problems with downmixing. */
896
165M
      if (ctx->disable_inv)
897
71.8M
         inv = 0;
898
165M
      itheta = 0;
899
165M
      itheta_q30 = 0;
900
165M
   }
901
266M
   qalloc = ec_tell_frac(ec) - tell;
902
266M
   *b -= qalloc;
903
904
266M
   if (itheta == 0)
905
239M
   {
906
239M
      imid = 32767;
907
239M
      iside = 0;
908
239M
      *fill &= (1<<B)-1;
909
239M
      delta = -16384;
910
239M
   } else if (itheta == 16384)
911
1.07M
   {
912
1.07M
      imid = 0;
913
1.07M
      iside = 32767;
914
1.07M
      *fill &= ((1<<B)-1)<<B;
915
1.07M
      delta = 16384;
916
26.0M
   } else {
917
26.0M
      imid = bitexact_cos((opus_int16)itheta);
918
26.0M
      iside = bitexact_cos((opus_int16)(16384-itheta));
919
      /* This is the mid vs side allocation that minimizes squared error
920
         in that band. */
921
26.0M
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
922
26.0M
   }
923
924
266M
   sctx->inv = inv;
925
266M
   sctx->imid = imid;
926
266M
   sctx->iside = iside;
927
266M
   sctx->delta = delta;
928
266M
   sctx->itheta = itheta;
929
#ifdef ENABLE_QEXT
930
   sctx->itheta_q30 = itheta_q30;
931
#endif
932
266M
   sctx->qalloc = qalloc;
933
266M
}
bands.c:compute_theta
Line
Count
Source
704
154M
{
705
154M
   int qn;
706
154M
   int itheta=0;
707
154M
   int itheta_q30=0;
708
154M
   int delta;
709
154M
   int imid, iside;
710
154M
   int qalloc;
711
154M
   int pulse_cap;
712
154M
   int offset;
713
154M
   opus_int32 tell;
714
154M
   int inv=0;
715
154M
   int encode;
716
154M
   const CELTMode *m;
717
154M
   int i;
718
154M
   int intensity;
719
154M
   ec_ctx *ec;
720
154M
   const celt_ener *bandE;
721
722
154M
   encode = ctx->encode;
723
154M
   m = ctx->m;
724
154M
   i = ctx->i;
725
154M
   intensity = ctx->intensity;
726
154M
   ec = ctx->ec;
727
154M
   bandE = ctx->bandE;
728
729
   /* Decide on the resolution to give to the split parameter theta */
730
154M
   pulse_cap = m->logN[i]+LM*(1<<BITRES);
731
154M
   offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
732
154M
   qn = compute_qn(N, *b, offset, pulse_cap, stereo);
733
154M
   if (stereo && i>=intensity)
734
128M
      qn = 1;
735
154M
   if (encode)
736
153M
   {
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
153M
      itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch);
742
153M
      itheta = itheta_q30>>16;
743
153M
   }
744
154M
   tell = ec_tell_frac(ec);
745
154M
   if (qn!=1)
746
25.3M
   {
747
25.3M
      if (encode)
748
24.8M
      {
749
24.8M
         if (!stereo || ctx->theta_round == 0)
750
24.2M
         {
751
24.2M
            itheta = (itheta*(opus_int32)qn+8192)>>14;
752
24.2M
            if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
753
94.3k
            {
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
94.3k
               int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
758
94.3k
               imid = bitexact_cos((opus_int16)unquantized);
759
94.3k
               iside = bitexact_cos((opus_int16)(16384-unquantized));
760
94.3k
               delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
761
94.3k
               if (delta > *b)
762
428
                  itheta = qn;
763
93.8k
               else if (delta < -*b)
764
469
                  itheta = 0;
765
94.3k
            }
766
24.2M
         } else {
767
626k
            int down;
768
            /* Bias quantization towards itheta=0 and itheta=16384. */
769
626k
            int bias = itheta > 8192 ? 32767/qn : -32767/qn;
770
626k
            down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
771
626k
            if (ctx->theta_round < 0)
772
313k
               itheta = down;
773
313k
            else
774
313k
               itheta = down+1;
775
626k
         }
776
24.8M
      }
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
25.3M
      if (stereo && N>2)
780
4.32M
      {
781
4.32M
         int p0 = 3;
782
4.32M
         int x = itheta;
783
4.32M
         int x0 = qn/2;
784
4.32M
         int ft = p0*(x0+1) + x0;
785
         /* Use a probability of p0 up to itheta=8192 and then use 1 after */
786
4.32M
         if (encode)
787
4.28M
         {
788
4.28M
            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
4.28M
         } else {
790
41.3k
            int fs;
791
41.3k
            fs=ec_decode(ec,ft);
792
41.3k
            if (fs<(x0+1)*p0)
793
28.5k
               x=fs/p0;
794
12.7k
            else
795
12.7k
               x=x0+1+(fs-(x0+1)*p0);
796
41.3k
            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
41.3k
            itheta = x;
798
41.3k
         }
799
21.0M
      } else if (B0>1 || stereo) {
800
         /* Uniform pdf */
801
4.49M
         if (encode)
802
4.31M
            ec_enc_uint(ec, itheta, qn+1);
803
178k
         else
804
178k
            itheta = ec_dec_uint(ec, qn+1);
805
16.5M
      } else {
806
16.5M
         int fs=1, ft;
807
16.5M
         ft = ((qn>>1)+1)*((qn>>1)+1);
808
16.5M
         if (encode)
809
16.2M
         {
810
16.2M
            int fl;
811
812
16.2M
            fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
813
16.2M
            fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
814
16.2M
             ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
815
816
16.2M
            ec_encode(ec, fl, fl+fs, ft);
817
16.2M
         } else {
818
            /* Triangular pdf */
819
270k
            int fl=0;
820
270k
            int fm;
821
270k
            fm = ec_decode(ec, ft);
822
823
270k
            if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
824
138k
            {
825
138k
               itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
826
138k
               fs = itheta + 1;
827
138k
               fl = itheta*(itheta + 1)>>1;
828
138k
            }
829
132k
            else
830
132k
            {
831
132k
               itheta = (2*(qn + 1)
832
132k
                - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
833
132k
               fs = qn + 1 - itheta;
834
132k
               fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
835
132k
            }
836
837
270k
            ec_dec_update(ec, fl, fl+fs, ft);
838
270k
         }
839
16.5M
      }
840
25.3M
      celt_assert(itheta>=0);
841
25.3M
      itheta = celt_udiv((opus_int32)itheta*16384, qn);
842
25.3M
#ifdef ENABLE_QEXT
843
25.3M
      *ext_b = IMIN(*ext_b, ctx->ext_total_bits - (opus_int32)ec_tell_frac(ctx->ext_ec));
844
25.3M
      if (*ext_b >= 2*N<<BITRES && ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec)-1 > 2<<BITRES) {
845
170k
         int extra_bits;
846
170k
         int ext_tell = ec_tell_frac(ctx->ext_ec);
847
170k
         extra_bits = IMIN(12, IMAX(2, celt_sudiv(*ext_b, (2*N-1)<<BITRES)));
848
170k
         if (encode) {
849
138k
            itheta_q30 = itheta_q30 - (itheta<<16);
850
138k
            itheta_q30 = (itheta_q30*(opus_int64)qn*((1<<extra_bits)-1)+(1<<29))>>30;
851
138k
            itheta_q30 += (1<<(extra_bits-1))-1;
852
138k
            itheta_q30 = IMAX(0, IMIN((1<<extra_bits)-2, itheta_q30));
853
138k
            ec_enc_uint(ctx->ext_ec, itheta_q30, (1<<extra_bits)-1);
854
138k
         } else {
855
31.6k
            itheta_q30 = ec_dec_uint(ctx->ext_ec, (1<<extra_bits)-1);
856
31.6k
         }
857
170k
         itheta_q30 -= (1<<(extra_bits-1))-1;
858
170k
         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
170k
         itheta_q30 = IMAX(0, IMIN(itheta_q30, 1073741824));
861
170k
         *ext_b -= ec_tell_frac(ctx->ext_ec) - ext_tell;
862
25.1M
      } else {
863
25.1M
         itheta_q30 = (opus_int32)itheta<<16;
864
25.1M
      }
865
25.3M
#endif
866
25.3M
      if (encode && stereo)
867
4.70M
      {
868
4.70M
         if (itheta==0)
869
3.79M
            intensity_stereo(m, X, Y, bandE, i, N);
870
912k
         else
871
912k
            stereo_split(X, Y, N);
872
4.70M
      }
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
128M
   } else if (stereo) {
876
128M
      if (encode)
877
128M
      {
878
128M
         inv = itheta > 8192 && !ctx->disable_inv;
879
128M
         if (inv)
880
80.5k
         {
881
80.5k
            int j;
882
1.72M
            for (j=0;j<N;j++)
883
1.64M
               Y[j] = -Y[j];
884
80.5k
         }
885
128M
         intensity_stereo(m, X, Y, bandE, i, N);
886
128M
      }
887
128M
      if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
888
2.41M
      {
889
2.41M
         if (encode)
890
2.32M
            ec_enc_bit_logp(ec, inv, 2);
891
89.6k
         else
892
89.6k
            inv = ec_dec_bit_logp(ec, 2);
893
2.41M
      } else
894
126M
         inv = 0;
895
      /* inv flag override to avoid problems with downmixing. */
896
128M
      if (ctx->disable_inv)
897
59.2M
         inv = 0;
898
128M
      itheta = 0;
899
128M
      itheta_q30 = 0;
900
128M
   }
901
154M
   qalloc = ec_tell_frac(ec) - tell;
902
154M
   *b -= qalloc;
903
904
154M
   if (itheta == 0)
905
144M
   {
906
144M
      imid = 32767;
907
144M
      iside = 0;
908
144M
      *fill &= (1<<B)-1;
909
144M
      delta = -16384;
910
144M
   } else if (itheta == 16384)
911
547k
   {
912
547k
      imid = 0;
913
547k
      iside = 32767;
914
547k
      *fill &= ((1<<B)-1)<<B;
915
547k
      delta = 16384;
916
9.22M
   } else {
917
9.22M
      imid = bitexact_cos((opus_int16)itheta);
918
9.22M
      iside = bitexact_cos((opus_int16)(16384-itheta));
919
      /* This is the mid vs side allocation that minimizes squared error
920
         in that band. */
921
9.22M
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
922
9.22M
   }
923
924
154M
   sctx->inv = inv;
925
154M
   sctx->imid = imid;
926
154M
   sctx->iside = iside;
927
154M
   sctx->delta = delta;
928
154M
   sctx->itheta = itheta;
929
154M
#ifdef ENABLE_QEXT
930
154M
   sctx->itheta_q30 = itheta_q30;
931
154M
#endif
932
154M
   sctx->qalloc = qalloc;
933
154M
}
934
static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y,
935
      celt_norm *lowband_out)
936
917M
{
937
917M
   int c;
938
917M
   int stereo;
939
917M
   celt_norm *x = X;
940
917M
   int encode;
941
917M
   ec_ctx *ec;
942
943
917M
   encode = ctx->encode;
944
917M
   ec = ctx->ec;
945
946
917M
   stereo = Y != NULL;
947
1.02G
   c=0; do {
948
1.02G
      int sign=0;
949
1.02G
      if (ctx->remaining_bits>=1<<BITRES)
950
155M
      {
951
155M
         if (encode)
952
155M
         {
953
155M
            sign = x[0]<0;
954
155M
            ec_enc_bits(ec, sign, 1);
955
155M
         } else {
956
552k
            sign = ec_dec_bits(ec, 1);
957
552k
         }
958
155M
         ctx->remaining_bits -= 1<<BITRES;
959
155M
      }
960
1.02G
      if (ctx->resynth)
961
27.9M
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
962
1.02G
      x = Y;
963
1.02G
   } while (++c<1+stereo);
964
917M
   if (lowband_out)
965
917M
      lowband_out[0] = SHR32(X[0],4);
966
917M
   return 1;
967
917M
}
bands.c:quant_band_n1
Line
Count
Source
936
458M
{
937
458M
   int c;
938
458M
   int stereo;
939
458M
   celt_norm *x = X;
940
458M
   int encode;
941
458M
   ec_ctx *ec;
942
943
458M
   encode = ctx->encode;
944
458M
   ec = ctx->ec;
945
946
458M
   stereo = Y != NULL;
947
510M
   c=0; do {
948
510M
      int sign=0;
949
510M
      if (ctx->remaining_bits>=1<<BITRES)
950
77.8M
      {
951
77.8M
         if (encode)
952
77.5M
         {
953
77.5M
            sign = x[0]<0;
954
77.5M
            ec_enc_bits(ec, sign, 1);
955
77.5M
         } else {
956
276k
            sign = ec_dec_bits(ec, 1);
957
276k
         }
958
77.8M
         ctx->remaining_bits -= 1<<BITRES;
959
77.8M
      }
960
510M
      if (ctx->resynth)
961
13.9M
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
962
510M
      x = Y;
963
510M
   } while (++c<1+stereo);
964
458M
   if (lowband_out)
965
458M
      lowband_out[0] = SHR32(X[0],4);
966
458M
   return 1;
967
458M
}
bands.c:quant_band_n1
Line
Count
Source
936
458M
{
937
458M
   int c;
938
458M
   int stereo;
939
458M
   celt_norm *x = X;
940
458M
   int encode;
941
458M
   ec_ctx *ec;
942
943
458M
   encode = ctx->encode;
944
458M
   ec = ctx->ec;
945
946
458M
   stereo = Y != NULL;
947
510M
   c=0; do {
948
510M
      int sign=0;
949
510M
      if (ctx->remaining_bits>=1<<BITRES)
950
77.8M
      {
951
77.8M
         if (encode)
952
77.5M
         {
953
77.5M
            sign = x[0]<0;
954
77.5M
            ec_enc_bits(ec, sign, 1);
955
77.5M
         } else {
956
276k
            sign = ec_dec_bits(ec, 1);
957
276k
         }
958
77.8M
         ctx->remaining_bits -= 1<<BITRES;
959
77.8M
      }
960
510M
      if (ctx->resynth)
961
13.9M
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
962
510M
      x = Y;
963
510M
   } while (++c<1+stereo);
964
458M
   if (lowband_out)
965
458M
      lowband_out[0] = SHR32(X[0],4);
966
458M
   return 1;
967
458M
}
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
3.72G
{
979
3.72G
   const unsigned char *cache;
980
3.72G
   int q;
981
3.72G
   int curr_bits;
982
3.72G
   int imid=0, iside=0;
983
3.72G
   int B0=B;
984
3.72G
   opus_val32 mid=0, side=0;
985
3.72G
   unsigned cm=0;
986
3.72G
   celt_norm *Y=NULL;
987
3.72G
   int encode;
988
3.72G
   const CELTMode *m;
989
3.72G
   int i;
990
3.72G
   int spread;
991
3.72G
   ec_ctx *ec;
992
993
3.72G
   encode = ctx->encode;
994
3.72G
   m = ctx->m;
995
3.72G
   i = ctx->i;
996
3.72G
   spread = ctx->spread;
997
3.72G
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
3.72G
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
3.72G
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
193M
   {
1003
193M
      int mbits, sbits, delta;
1004
193M
      int itheta;
1005
193M
      int qalloc;
1006
193M
      struct split_ctx sctx;
1007
193M
      celt_norm *next_lowband2=NULL;
1008
193M
      opus_int32 rebalance;
1009
1010
193M
      N >>= 1;
1011
193M
      Y = X+N;
1012
193M
      LM -= 1;
1013
193M
      if (B==1)
1014
168M
         fill = (fill&1)|(fill<<1);
1015
193M
      B = (B+1)>>1;
1016
1017
193M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
193M
      imid = sctx.imid;
1019
193M
      iside = sctx.iside;
1020
193M
      delta = sctx.delta;
1021
193M
      itheta = sctx.itheta;
1022
193M
      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
75.9M
      mid = SHL32(EXTEND32(imid), 16);
1031
75.9M
      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
193M
      if (B0>1 && (itheta&0x3fff))
1047
17.5M
      {
1048
17.5M
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
7.06M
            delta -= delta>>(4-LM);
1051
10.4M
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
10.4M
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
17.5M
      }
1055
193M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
193M
      sbits = b-mbits;
1057
193M
      ctx->remaining_bits -= qalloc;
1058
1059
193M
      if (lowband)
1060
12.7M
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
193M
      rebalance = ctx->remaining_bits;
1063
193M
      if (mbits >= sbits)
1064
166M
      {
1065
166M
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
166M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
166M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
166M
         if (rebalance > 3<<BITRES && itheta!=0)
1069
7.93M
            sbits += rebalance - (3<<BITRES);
1070
166M
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
166M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
166M
      } else {
1073
26.1M
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
26.1M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
26.1M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
26.1M
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
5.63M
            mbits += rebalance - (3<<BITRES);
1078
26.1M
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
26.1M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
26.1M
      }
1081
3.53G
   } else {
1082
#ifdef ENABLE_QEXT
1083
      int extra_bits;
1084
      int ext_remaining_bits;
1085
1.53G
      extra_bits = ext_b/(N-1)>>BITRES;
1086
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1087
1.53G
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1088
1.53G
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1089
1.53G
         extra_bits = IMAX(extra_bits-1, 0);
1090
1.53G
      }
1091
1.53G
      extra_bits = IMIN(12, extra_bits);
1092
#endif
1093
      /* This is the basic no-split case */
1094
3.53G
      q = bits2pulses(m, i, LM, b);
1095
3.53G
      curr_bits = pulses2bits(m, i, LM, q);
1096
3.53G
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
3.55G
      while (ctx->remaining_bits < 0 && q > 0)
1100
20.0M
      {
1101
20.0M
         ctx->remaining_bits += curr_bits;
1102
20.0M
         q--;
1103
20.0M
         curr_bits = pulses2bits(m, i, LM, q);
1104
20.0M
         ctx->remaining_bits -= curr_bits;
1105
20.0M
      }
1106
1107
3.53G
      if (q!=0)
1108
557M
      {
1109
151M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
557M
         if (encode)
1113
554M
         {
1114
554M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
554M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
554M
                           ctx->arch);
1117
554M
         } else {
1118
3.50M
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
3.50M
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
3.50M
         }
1121
#ifdef ENABLE_QEXT
1122
1.38G
      } else if (ext_b > 2*N<<BITRES)
1123
60.4k
      {
1124
60.4k
         extra_bits = ext_b/(N-1)>>BITRES;
1125
60.4k
         ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec);
1126
60.4k
         if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1127
2.66k
            extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1128
2.66k
            extra_bits = IMAX(extra_bits-1, 0);
1129
2.66k
         }
1130
60.4k
         extra_bits = IMIN(14, extra_bits);
1131
60.4k
         if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth);
1132
17.6k
         else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain);
1133
#endif
1134
2.97G
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
2.97G
         int j;
1137
2.97G
         if (ctx->resynth)
1138
388M
         {
1139
388M
            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
388M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
388M
            fill &= cm_mask;
1144
388M
            if (!fill)
1145
191M
            {
1146
191M
               OPUS_CLEAR(X, N);
1147
197M
            } else {
1148
197M
               if (lowband == NULL)
1149
12.1M
               {
1150
                  /* Noise */
1151
80.3M
                  for (j=0;j<N;j++)
1152
68.1M
                  {
1153
68.1M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
68.1M
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
68.1M
                  }
1156
12.1M
                  cm = cm_mask;
1157
185M
               } else {
1158
                  /* Folded spectrum */
1159
2.57G
                  for (j=0;j<N;j++)
1160
2.39G
                  {
1161
2.39G
                     opus_val16 tmp;
1162
2.39G
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
2.39G
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
2.39G
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
2.39G
                     X[j] = lowband[j]+tmp;
1167
2.39G
                  }
1168
185M
                  cm = fill;
1169
185M
               }
1170
197M
               renormalise_vector(X, N, gain, ctx->arch);
1171
197M
            }
1172
388M
         }
1173
2.97G
      }
1174
3.53G
   }
1175
1176
3.72G
   return cm;
1177
3.72G
}
bands.c:quant_partition
Line
Count
Source
978
1.07G
{
979
1.07G
   const unsigned char *cache;
980
1.07G
   int q;
981
1.07G
   int curr_bits;
982
1.07G
   int imid=0, iside=0;
983
1.07G
   int B0=B;
984
1.07G
   opus_val32 mid=0, side=0;
985
1.07G
   unsigned cm=0;
986
1.07G
   celt_norm *Y=NULL;
987
1.07G
   int encode;
988
1.07G
   const CELTMode *m;
989
1.07G
   int i;
990
1.07G
   int spread;
991
1.07G
   ec_ctx *ec;
992
993
1.07G
   encode = ctx->encode;
994
1.07G
   m = ctx->m;
995
1.07G
   i = ctx->i;
996
1.07G
   spread = ctx->spread;
997
1.07G
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
1.07G
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
1.07G
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
75.9M
   {
1003
75.9M
      int mbits, sbits, delta;
1004
75.9M
      int itheta;
1005
75.9M
      int qalloc;
1006
75.9M
      struct split_ctx sctx;
1007
75.9M
      celt_norm *next_lowband2=NULL;
1008
75.9M
      opus_int32 rebalance;
1009
1010
75.9M
      N >>= 1;
1011
75.9M
      Y = X+N;
1012
75.9M
      LM -= 1;
1013
75.9M
      if (B==1)
1014
67.6M
         fill = (fill&1)|(fill<<1);
1015
75.9M
      B = (B+1)>>1;
1016
1017
75.9M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
75.9M
      imid = sctx.imid;
1019
75.9M
      iside = sctx.iside;
1020
75.9M
      delta = sctx.delta;
1021
75.9M
      itheta = sctx.itheta;
1022
75.9M
      qalloc = sctx.qalloc;
1023
75.9M
#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
75.9M
      mid = SHL32(EXTEND32(imid), 16);
1031
75.9M
      side = SHL32(EXTEND32(iside), 16);
1032
75.9M
# 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
75.9M
      if (B0>1 && (itheta&0x3fff))
1047
5.60M
      {
1048
5.60M
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
2.12M
            delta -= delta>>(4-LM);
1051
3.48M
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
3.48M
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
5.60M
      }
1055
75.9M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
75.9M
      sbits = b-mbits;
1057
75.9M
      ctx->remaining_bits -= qalloc;
1058
1059
75.9M
      if (lowband)
1060
5.71M
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
75.9M
      rebalance = ctx->remaining_bits;
1063
75.9M
      if (mbits >= sbits)
1064
67.1M
      {
1065
67.1M
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
67.1M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
67.1M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
67.1M
         if (rebalance > 3<<BITRES && itheta!=0)
1069
3.06M
            sbits += rebalance - (3<<BITRES);
1070
67.1M
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
67.1M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
67.1M
      } else {
1073
8.77M
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
8.77M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
8.77M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
8.77M
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
1.94M
            mbits += rebalance - (3<<BITRES);
1078
8.77M
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
8.77M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
8.77M
      }
1081
996M
   } 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
996M
      q = bits2pulses(m, i, LM, b);
1095
996M
      curr_bits = pulses2bits(m, i, LM, q);
1096
996M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
1.00G
      while (ctx->remaining_bits < 0 && q > 0)
1100
7.44M
      {
1101
7.44M
         ctx->remaining_bits += curr_bits;
1102
7.44M
         q--;
1103
7.44M
         curr_bits = pulses2bits(m, i, LM, q);
1104
7.44M
         ctx->remaining_bits -= curr_bits;
1105
7.44M
      }
1106
1107
996M
      if (q!=0)
1108
203M
      {
1109
203M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
203M
         if (encode)
1113
202M
         {
1114
202M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
202M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
202M
                           ctx->arch);
1117
202M
         } else {
1118
729k
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
729k
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
729k
         }
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
793M
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
793M
         int j;
1137
793M
         if (ctx->resynth)
1138
122M
         {
1139
122M
            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
122M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
122M
            fill &= cm_mask;
1144
122M
            if (!fill)
1145
63.3M
            {
1146
63.3M
               OPUS_CLEAR(X, N);
1147
63.3M
            } else {
1148
59.4M
               if (lowband == NULL)
1149
3.51M
               {
1150
                  /* Noise */
1151
23.1M
                  for (j=0;j<N;j++)
1152
19.6M
                  {
1153
19.6M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
19.6M
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
19.6M
                  }
1156
3.51M
                  cm = cm_mask;
1157
55.9M
               } else {
1158
                  /* Folded spectrum */
1159
805M
                  for (j=0;j<N;j++)
1160
749M
                  {
1161
749M
                     opus_val16 tmp;
1162
749M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
749M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
749M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
749M
                     X[j] = lowband[j]+tmp;
1167
749M
                  }
1168
55.9M
                  cm = fill;
1169
55.9M
               }
1170
59.4M
               renormalise_vector(X, N, gain, ctx->arch);
1171
59.4M
            }
1172
122M
         }
1173
793M
      }
1174
996M
   }
1175
1176
1.07G
   return cm;
1177
1.07G
}
bands.c:quant_partition
Line
Count
Source
978
789M
{
979
789M
   const unsigned char *cache;
980
789M
   int q;
981
789M
   int curr_bits;
982
789M
   int imid=0, iside=0;
983
789M
   int B0=B;
984
789M
   opus_val32 mid=0, side=0;
985
789M
   unsigned cm=0;
986
789M
   celt_norm *Y=NULL;
987
789M
   int encode;
988
789M
   const CELTMode *m;
989
789M
   int i;
990
789M
   int spread;
991
789M
   ec_ctx *ec;
992
993
789M
   encode = ctx->encode;
994
789M
   m = ctx->m;
995
789M
   i = ctx->i;
996
789M
   spread = ctx->spread;
997
789M
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
789M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
789M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
20.5M
   {
1003
20.5M
      int mbits, sbits, delta;
1004
20.5M
      int itheta;
1005
20.5M
      int qalloc;
1006
20.5M
      struct split_ctx sctx;
1007
20.5M
      celt_norm *next_lowband2=NULL;
1008
20.5M
      opus_int32 rebalance;
1009
1010
20.5M
      N >>= 1;
1011
20.5M
      Y = X+N;
1012
20.5M
      LM -= 1;
1013
20.5M
      if (B==1)
1014
16.5M
         fill = (fill&1)|(fill<<1);
1015
20.5M
      B = (B+1)>>1;
1016
1017
20.5M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
20.5M
      imid = sctx.imid;
1019
20.5M
      iside = sctx.iside;
1020
20.5M
      delta = sctx.delta;
1021
20.5M
      itheta = sctx.itheta;
1022
20.5M
      qalloc = sctx.qalloc;
1023
20.5M
#ifdef FIXED_POINT
1024
20.5M
# ifdef ENABLE_QEXT
1025
20.5M
      (void)imid;
1026
20.5M
      (void)iside;
1027
20.5M
      mid = celt_cos_norm32(sctx.itheta_q30);
1028
20.5M
      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
20.5M
      if (B0>1 && (itheta&0x3fff))
1047
3.15M
      {
1048
3.15M
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
1.40M
            delta -= delta>>(4-LM);
1051
1.74M
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
1.74M
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
3.15M
      }
1055
20.5M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
20.5M
      sbits = b-mbits;
1057
20.5M
      ctx->remaining_bits -= qalloc;
1058
1059
20.5M
      if (lowband)
1060
632k
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
20.5M
      rebalance = ctx->remaining_bits;
1063
20.5M
      if (mbits >= sbits)
1064
16.3M
      {
1065
16.3M
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
16.3M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
16.3M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
16.3M
         if (rebalance > 3<<BITRES && itheta!=0)
1069
899k
            sbits += rebalance - (3<<BITRES);
1070
16.3M
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
16.3M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
16.3M
      } else {
1073
4.28M
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
4.28M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
4.28M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
4.28M
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
867k
            mbits += rebalance - (3<<BITRES);
1078
4.28M
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
4.28M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
4.28M
      }
1081
768M
   } else {
1082
768M
#ifdef ENABLE_QEXT
1083
768M
      int extra_bits;
1084
768M
      int ext_remaining_bits;
1085
768M
      extra_bits = ext_b/(N-1)>>BITRES;
1086
768M
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1087
768M
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1088
766M
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1089
766M
         extra_bits = IMAX(extra_bits-1, 0);
1090
766M
      }
1091
768M
      extra_bits = IMIN(12, extra_bits);
1092
768M
#endif
1093
      /* This is the basic no-split case */
1094
768M
      q = bits2pulses(m, i, LM, b);
1095
768M
      curr_bits = pulses2bits(m, i, LM, q);
1096
768M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
771M
      while (ctx->remaining_bits < 0 && q > 0)
1100
2.56M
      {
1101
2.56M
         ctx->remaining_bits += curr_bits;
1102
2.56M
         q--;
1103
2.56M
         curr_bits = pulses2bits(m, i, LM, q);
1104
2.56M
         ctx->remaining_bits -= curr_bits;
1105
2.56M
      }
1106
1107
768M
      if (q!=0)
1108
75.5M
      {
1109
75.5M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
75.5M
         if (encode)
1113
74.5M
         {
1114
74.5M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
74.5M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
74.5M
                           ctx->arch);
1117
74.5M
         } else {
1118
1.02M
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
1.02M
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
1.02M
         }
1121
75.5M
#ifdef ENABLE_QEXT
1122
693M
      } else if (ext_b > 2*N<<BITRES)
1123
30.2k
      {
1124
30.2k
         extra_bits = ext_b/(N-1)>>BITRES;
1125
30.2k
         ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec);
1126
30.2k
         if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1127
1.33k
            extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1128
1.33k
            extra_bits = IMAX(extra_bits-1, 0);
1129
1.33k
         }
1130
30.2k
         extra_bits = IMIN(14, extra_bits);
1131
30.2k
         if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth);
1132
8.81k
         else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain);
1133
30.2k
#endif
1134
693M
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
693M
         int j;
1137
693M
         if (ctx->resynth)
1138
71.5M
         {
1139
71.5M
            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
71.5M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
71.5M
            fill &= cm_mask;
1144
71.5M
            if (!fill)
1145
32.3M
            {
1146
32.3M
               OPUS_CLEAR(X, N);
1147
39.1M
            } else {
1148
39.1M
               if (lowband == NULL)
1149
2.57M
               {
1150
                  /* Noise */
1151
17.0M
                  for (j=0;j<N;j++)
1152
14.4M
                  {
1153
14.4M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
14.4M
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
14.4M
                  }
1156
2.57M
                  cm = cm_mask;
1157
36.5M
               } else {
1158
                  /* Folded spectrum */
1159
482M
                  for (j=0;j<N;j++)
1160
446M
                  {
1161
446M
                     opus_val16 tmp;
1162
446M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
446M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
446M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
446M
                     X[j] = lowband[j]+tmp;
1167
446M
                  }
1168
36.5M
                  cm = fill;
1169
36.5M
               }
1170
39.1M
               renormalise_vector(X, N, gain, ctx->arch);
1171
39.1M
            }
1172
71.5M
         }
1173
693M
      }
1174
768M
   }
1175
1176
789M
   return cm;
1177
789M
}
bands.c:quant_partition
Line
Count
Source
978
789M
{
979
789M
   const unsigned char *cache;
980
789M
   int q;
981
789M
   int curr_bits;
982
789M
   int imid=0, iside=0;
983
789M
   int B0=B;
984
789M
   opus_val32 mid=0, side=0;
985
789M
   unsigned cm=0;
986
789M
   celt_norm *Y=NULL;
987
789M
   int encode;
988
789M
   const CELTMode *m;
989
789M
   int i;
990
789M
   int spread;
991
789M
   ec_ctx *ec;
992
993
789M
   encode = ctx->encode;
994
789M
   m = ctx->m;
995
789M
   i = ctx->i;
996
789M
   spread = ctx->spread;
997
789M
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
789M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
789M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
20.5M
   {
1003
20.5M
      int mbits, sbits, delta;
1004
20.5M
      int itheta;
1005
20.5M
      int qalloc;
1006
20.5M
      struct split_ctx sctx;
1007
20.5M
      celt_norm *next_lowband2=NULL;
1008
20.5M
      opus_int32 rebalance;
1009
1010
20.5M
      N >>= 1;
1011
20.5M
      Y = X+N;
1012
20.5M
      LM -= 1;
1013
20.5M
      if (B==1)
1014
16.5M
         fill = (fill&1)|(fill<<1);
1015
20.5M
      B = (B+1)>>1;
1016
1017
20.5M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
20.5M
      imid = sctx.imid;
1019
20.5M
      iside = sctx.iside;
1020
20.5M
      delta = sctx.delta;
1021
20.5M
      itheta = sctx.itheta;
1022
20.5M
      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
20.5M
# ifdef ENABLE_QEXT
1035
20.5M
      (void)imid;
1036
20.5M
      (void)iside;
1037
20.5M
      mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1038
20.5M
      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
20.5M
#endif
1044
1045
      /* Give more bits to low-energy MDCTs than they would otherwise deserve */
1046
20.5M
      if (B0>1 && (itheta&0x3fff))
1047
3.15M
      {
1048
3.15M
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
1.40M
            delta -= delta>>(4-LM);
1051
1.74M
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
1.74M
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
3.15M
      }
1055
20.5M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
20.5M
      sbits = b-mbits;
1057
20.5M
      ctx->remaining_bits -= qalloc;
1058
1059
20.5M
      if (lowband)
1060
632k
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
20.5M
      rebalance = ctx->remaining_bits;
1063
20.5M
      if (mbits >= sbits)
1064
16.3M
      {
1065
16.3M
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
16.3M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
16.3M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
16.3M
         if (rebalance > 3<<BITRES && itheta!=0)
1069
899k
            sbits += rebalance - (3<<BITRES);
1070
16.3M
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
16.3M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
16.3M
      } else {
1073
4.28M
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
4.28M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
4.28M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
4.28M
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
867k
            mbits += rebalance - (3<<BITRES);
1078
4.28M
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
4.28M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
4.28M
      }
1081
768M
   } else {
1082
768M
#ifdef ENABLE_QEXT
1083
768M
      int extra_bits;
1084
768M
      int ext_remaining_bits;
1085
768M
      extra_bits = ext_b/(N-1)>>BITRES;
1086
768M
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1087
768M
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1088
766M
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1089
766M
         extra_bits = IMAX(extra_bits-1, 0);
1090
766M
      }
1091
768M
      extra_bits = IMIN(12, extra_bits);
1092
768M
#endif
1093
      /* This is the basic no-split case */
1094
768M
      q = bits2pulses(m, i, LM, b);
1095
768M
      curr_bits = pulses2bits(m, i, LM, q);
1096
768M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
771M
      while (ctx->remaining_bits < 0 && q > 0)
1100
2.56M
      {
1101
2.56M
         ctx->remaining_bits += curr_bits;
1102
2.56M
         q--;
1103
2.56M
         curr_bits = pulses2bits(m, i, LM, q);
1104
2.56M
         ctx->remaining_bits -= curr_bits;
1105
2.56M
      }
1106
1107
768M
      if (q!=0)
1108
75.5M
      {
1109
75.5M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
75.5M
         if (encode)
1113
74.5M
         {
1114
74.5M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
74.5M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
74.5M
                           ctx->arch);
1117
74.5M
         } else {
1118
1.02M
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
1.02M
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
1.02M
         }
1121
75.5M
#ifdef ENABLE_QEXT
1122
693M
      } else if (ext_b > 2*N<<BITRES)
1123
30.2k
      {
1124
30.2k
         extra_bits = ext_b/(N-1)>>BITRES;
1125
30.2k
         ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec);
1126
30.2k
         if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1127
1.33k
            extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1128
1.33k
            extra_bits = IMAX(extra_bits-1, 0);
1129
1.33k
         }
1130
30.2k
         extra_bits = IMIN(14, extra_bits);
1131
30.2k
         if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth);
1132
8.81k
         else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain);
1133
30.2k
#endif
1134
693M
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
693M
         int j;
1137
693M
         if (ctx->resynth)
1138
71.5M
         {
1139
71.5M
            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
71.5M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
71.5M
            fill &= cm_mask;
1144
71.5M
            if (!fill)
1145
32.3M
            {
1146
32.3M
               OPUS_CLEAR(X, N);
1147
39.1M
            } else {
1148
39.1M
               if (lowband == NULL)
1149
2.57M
               {
1150
                  /* Noise */
1151
17.0M
                  for (j=0;j<N;j++)
1152
14.4M
                  {
1153
14.4M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
14.4M
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
14.4M
                  }
1156
2.57M
                  cm = cm_mask;
1157
36.5M
               } else {
1158
                  /* Folded spectrum */
1159
482M
                  for (j=0;j<N;j++)
1160
446M
                  {
1161
446M
                     opus_val16 tmp;
1162
446M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
446M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
446M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
446M
                     X[j] = lowband[j]+tmp;
1167
446M
                  }
1168
36.5M
                  cm = fill;
1169
36.5M
               }
1170
39.1M
               renormalise_vector(X, N, gain, ctx->arch);
1171
39.1M
            }
1172
71.5M
         }
1173
693M
      }
1174
768M
   }
1175
1176
789M
   return cm;
1177
789M
}
bands.c:quant_partition
Line
Count
Source
978
1.07G
{
979
1.07G
   const unsigned char *cache;
980
1.07G
   int q;
981
1.07G
   int curr_bits;
982
1.07G
   int imid=0, iside=0;
983
1.07G
   int B0=B;
984
1.07G
   opus_val32 mid=0, side=0;
985
1.07G
   unsigned cm=0;
986
1.07G
   celt_norm *Y=NULL;
987
1.07G
   int encode;
988
1.07G
   const CELTMode *m;
989
1.07G
   int i;
990
1.07G
   int spread;
991
1.07G
   ec_ctx *ec;
992
993
1.07G
   encode = ctx->encode;
994
1.07G
   m = ctx->m;
995
1.07G
   i = ctx->i;
996
1.07G
   spread = ctx->spread;
997
1.07G
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
1.07G
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
1.07G
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
75.9M
   {
1003
75.9M
      int mbits, sbits, delta;
1004
75.9M
      int itheta;
1005
75.9M
      int qalloc;
1006
75.9M
      struct split_ctx sctx;
1007
75.9M
      celt_norm *next_lowband2=NULL;
1008
75.9M
      opus_int32 rebalance;
1009
1010
75.9M
      N >>= 1;
1011
75.9M
      Y = X+N;
1012
75.9M
      LM -= 1;
1013
75.9M
      if (B==1)
1014
67.6M
         fill = (fill&1)|(fill<<1);
1015
75.9M
      B = (B+1)>>1;
1016
1017
75.9M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
75.9M
      imid = sctx.imid;
1019
75.9M
      iside = sctx.iside;
1020
75.9M
      delta = sctx.delta;
1021
75.9M
      itheta = sctx.itheta;
1022
75.9M
      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
75.9M
      mid = (1.f/32768)*imid;
1041
75.9M
      side = (1.f/32768)*iside;
1042
75.9M
# endif
1043
75.9M
#endif
1044
1045
      /* Give more bits to low-energy MDCTs than they would otherwise deserve */
1046
75.9M
      if (B0>1 && (itheta&0x3fff))
1047
5.60M
      {
1048
5.60M
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
2.12M
            delta -= delta>>(4-LM);
1051
3.48M
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
3.48M
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
5.60M
      }
1055
75.9M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
75.9M
      sbits = b-mbits;
1057
75.9M
      ctx->remaining_bits -= qalloc;
1058
1059
75.9M
      if (lowband)
1060
5.71M
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
75.9M
      rebalance = ctx->remaining_bits;
1063
75.9M
      if (mbits >= sbits)
1064
67.1M
      {
1065
67.1M
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
67.1M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
67.1M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
67.1M
         if (rebalance > 3<<BITRES && itheta!=0)
1069
3.06M
            sbits += rebalance - (3<<BITRES);
1070
67.1M
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
67.1M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
67.1M
      } else {
1073
8.77M
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
8.77M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
8.77M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
8.77M
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
1.94M
            mbits += rebalance - (3<<BITRES);
1078
8.77M
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
8.77M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
8.77M
      }
1081
996M
   } 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
996M
      q = bits2pulses(m, i, LM, b);
1095
996M
      curr_bits = pulses2bits(m, i, LM, q);
1096
996M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
1.00G
      while (ctx->remaining_bits < 0 && q > 0)
1100
7.44M
      {
1101
7.44M
         ctx->remaining_bits += curr_bits;
1102
7.44M
         q--;
1103
7.44M
         curr_bits = pulses2bits(m, i, LM, q);
1104
7.44M
         ctx->remaining_bits -= curr_bits;
1105
7.44M
      }
1106
1107
996M
      if (q!=0)
1108
203M
      {
1109
203M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
203M
         if (encode)
1113
202M
         {
1114
202M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
202M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
202M
                           ctx->arch);
1117
202M
         } else {
1118
729k
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
729k
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
729k
         }
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
793M
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
793M
         int j;
1137
793M
         if (ctx->resynth)
1138
122M
         {
1139
122M
            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
122M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
122M
            fill &= cm_mask;
1144
122M
            if (!fill)
1145
63.3M
            {
1146
63.3M
               OPUS_CLEAR(X, N);
1147
63.3M
            } else {
1148
59.4M
               if (lowband == NULL)
1149
3.51M
               {
1150
                  /* Noise */
1151
23.1M
                  for (j=0;j<N;j++)
1152
19.6M
                  {
1153
19.6M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
19.6M
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
19.6M
                  }
1156
3.51M
                  cm = cm_mask;
1157
55.9M
               } else {
1158
                  /* Folded spectrum */
1159
805M
                  for (j=0;j<N;j++)
1160
749M
                  {
1161
749M
                     opus_val16 tmp;
1162
749M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
749M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
749M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
749M
                     X[j] = lowband[j]+tmp;
1167
749M
                  }
1168
55.9M
                  cm = fill;
1169
55.9M
               }
1170
59.4M
               renormalise_vector(X, N, gain, ctx->arch);
1171
59.4M
            }
1172
122M
         }
1173
793M
      }
1174
996M
   }
1175
1176
1.07G
   return cm;
1177
1.07G
}
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
173k
{
1182
173k
   celt_assert(LM>=0);
1183
173k
   ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1184
173k
   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
173k
   if (LM==0 || b<=2*N<<BITRES) {
1187
101k
      int res, ret;
1188
101k
      b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits);
1189
      /* Resolution left after taking into account coding the cube face. */
1190
101k
      res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES;
1191
101k
      res = IMIN(14, IMAX(0, res));
1192
101k
      if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth);
1193
100k
      else ret = cubic_unquant(X, N, res, B, ec, gain);
1194
101k
      ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1195
101k
      return ret;
1196
101k
   } else {
1197
72.2k
      celt_norm *Y;
1198
72.2k
      opus_int32 itheta_q30;
1199
72.2k
      opus_val32 g1, g2;
1200
72.2k
      opus_int32 theta_res;
1201
72.2k
      opus_int32 qtheta;
1202
72.2k
      int delta;
1203
72.2k
      int b1, b2;
1204
72.2k
      int cm;
1205
72.2k
      int N0;
1206
72.2k
      N0 = N;
1207
72.2k
      N >>= 1;
1208
72.2k
      Y = X+N;
1209
72.2k
      LM -= 1;
1210
72.2k
      B = (B+1)>>1;
1211
72.2k
      theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1);
1212
72.2k
      if (encode) {
1213
300
         itheta_q30 = stereo_itheta(X, Y, 0, N, ctx->arch);
1214
300
         qtheta = (itheta_q30+(1<<(29-theta_res)))>>(30-theta_res);
1215
300
         ec_enc_uint(ec, qtheta, (1<<theta_res)+1);
1216
71.9k
      } else {
1217
71.9k
         qtheta = ec_dec_uint(ec, (1<<theta_res)+1);
1218
71.9k
      }
1219
72.2k
      itheta_q30 = qtheta<<(30-theta_res);
1220
72.2k
      b -= theta_res<<BITRES;
1221
72.2k
      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
72.2k
      if (itheta_q30 == 0) {
1231
2.46k
         b1=b;
1232
2.46k
         b2=0;
1233
69.7k
      } else if (itheta_q30==1073741824) {
1234
2.46k
         b1=0;
1235
2.46k
         b2=b;
1236
67.3k
      } else {
1237
67.3k
         b1 = IMIN(b, IMAX(0, (b-delta)/2));
1238
67.3k
         b2 = b-b1;
1239
67.3k
      }
1240
72.2k
      cm  = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode);
1241
72.2k
      cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode);
1242
72.2k
      return cm;
1243
72.2k
   }
1244
173k
}
bands.c:cubic_quant_partition
Line
Count
Source
1181
86.6k
{
1182
86.6k
   celt_assert(LM>=0);
1183
86.6k
   ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1184
86.6k
   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
86.6k
   if (LM==0 || b<=2*N<<BITRES) {
1187
50.5k
      int res, ret;
1188
50.5k
      b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits);
1189
      /* Resolution left after taking into account coding the cube face. */
1190
50.5k
      res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES;
1191
50.5k
      res = IMIN(14, IMAX(0, res));
1192
50.5k
      if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth);
1193
50.3k
      else ret = cubic_unquant(X, N, res, B, ec, gain);
1194
50.5k
      ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1195
50.5k
      return ret;
1196
50.5k
   } else {
1197
36.1k
      celt_norm *Y;
1198
36.1k
      opus_int32 itheta_q30;
1199
36.1k
      opus_val32 g1, g2;
1200
36.1k
      opus_int32 theta_res;
1201
36.1k
      opus_int32 qtheta;
1202
36.1k
      int delta;
1203
36.1k
      int b1, b2;
1204
36.1k
      int cm;
1205
36.1k
      int N0;
1206
36.1k
      N0 = N;
1207
36.1k
      N >>= 1;
1208
36.1k
      Y = X+N;
1209
36.1k
      LM -= 1;
1210
36.1k
      B = (B+1)>>1;
1211
36.1k
      theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1);
1212
36.1k
      if (encode) {
1213
150
         itheta_q30 = stereo_itheta(X, Y, 0, N, ctx->arch);
1214
150
         qtheta = (itheta_q30+(1<<(29-theta_res)))>>(30-theta_res);
1215
150
         ec_enc_uint(ec, qtheta, (1<<theta_res)+1);
1216
35.9k
      } else {
1217
35.9k
         qtheta = ec_dec_uint(ec, (1<<theta_res)+1);
1218
35.9k
      }
1219
36.1k
      itheta_q30 = qtheta<<(30-theta_res);
1220
36.1k
      b -= theta_res<<BITRES;
1221
36.1k
      delta = (N0-1) * 23 * ((itheta_q30>>16)-8192) >> (17-BITRES);
1222
1223
36.1k
#ifdef FIXED_POINT
1224
36.1k
      g1 = celt_cos_norm32(itheta_q30);
1225
36.1k
      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
36.1k
      if (itheta_q30 == 0) {
1231
1.23k
         b1=b;
1232
1.23k
         b2=0;
1233
34.8k
      } else if (itheta_q30==1073741824) {
1234
1.23k
         b1=0;
1235
1.23k
         b2=b;
1236
33.6k
      } else {
1237
33.6k
         b1 = IMIN(b, IMAX(0, (b-delta)/2));
1238
33.6k
         b2 = b-b1;
1239
33.6k
      }
1240
36.1k
      cm  = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode);
1241
36.1k
      cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode);
1242
36.1k
      return cm;
1243
36.1k
   }
1244
86.6k
}
bands.c:cubic_quant_partition
Line
Count
Source
1181
86.6k
{
1182
86.6k
   celt_assert(LM>=0);
1183
86.6k
   ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1184
86.6k
   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
86.6k
   if (LM==0 || b<=2*N<<BITRES) {
1187
50.5k
      int res, ret;
1188
50.5k
      b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits);
1189
      /* Resolution left after taking into account coding the cube face. */
1190
50.5k
      res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES;
1191
50.5k
      res = IMIN(14, IMAX(0, res));
1192
50.5k
      if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth);
1193
50.3k
      else ret = cubic_unquant(X, N, res, B, ec, gain);
1194
50.5k
      ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1195
50.5k
      return ret;
1196
50.5k
   } else {
1197
36.1k
      celt_norm *Y;
1198
36.1k
      opus_int32 itheta_q30;
1199
36.1k
      opus_val32 g1, g2;
1200
36.1k
      opus_int32 theta_res;
1201
36.1k
      opus_int32 qtheta;
1202
36.1k
      int delta;
1203
36.1k
      int b1, b2;
1204
36.1k
      int cm;
1205
36.1k
      int N0;
1206
36.1k
      N0 = N;
1207
36.1k
      N >>= 1;
1208
36.1k
      Y = X+N;
1209
36.1k
      LM -= 1;
1210
36.1k
      B = (B+1)>>1;
1211
36.1k
      theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1);
1212
36.1k
      if (encode) {
1213
150
         itheta_q30 = stereo_itheta(X, Y, 0, N, ctx->arch);
1214
150
         qtheta = (itheta_q30+(1<<(29-theta_res)))>>(30-theta_res);
1215
150
         ec_enc_uint(ec, qtheta, (1<<theta_res)+1);
1216
35.9k
      } else {
1217
35.9k
         qtheta = ec_dec_uint(ec, (1<<theta_res)+1);
1218
35.9k
      }
1219
36.1k
      itheta_q30 = qtheta<<(30-theta_res);
1220
36.1k
      b -= theta_res<<BITRES;
1221
36.1k
      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
36.1k
      g1 = celt_cos_norm2(itheta_q30*(1.f/(1<<30)));
1228
36.1k
      g2 = celt_cos_norm2(1.f-itheta_q30*(1.f/(1<<30)));
1229
36.1k
#endif
1230
36.1k
      if (itheta_q30 == 0) {
1231
1.23k
         b1=b;
1232
1.23k
         b2=0;
1233
34.8k
      } else if (itheta_q30==1073741824) {
1234
1.23k
         b1=0;
1235
1.23k
         b2=b;
1236
33.6k
      } else {
1237
33.6k
         b1 = IMIN(b, IMAX(0, (b-delta)/2));
1238
33.6k
         b2 = b-b1;
1239
33.6k
      }
1240
36.1k
      cm  = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode);
1241
36.1k
      cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode);
1242
36.1k
      return cm;
1243
36.1k
   }
1244
86.6k
}
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
4.15G
{
1254
4.15G
   int N0=N;
1255
4.15G
   int N_B=N;
1256
4.15G
   int N_B0;
1257
4.15G
   int B0=B;
1258
4.15G
   int time_divide=0;
1259
4.15G
   int recombine=0;
1260
4.15G
   int longBlocks;
1261
4.15G
   unsigned cm=0;
1262
4.15G
   int k;
1263
4.15G
   int encode;
1264
4.15G
   int tf_change;
1265
1266
4.15G
   encode = ctx->encode;
1267
4.15G
   tf_change = ctx->tf_change;
1268
1269
4.15G
   longBlocks = B0==1;
1270
1271
4.15G
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
4.15G
   if (N==1)
1275
813M
   {
1276
813M
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
813M
   }
1278
1279
3.33G
   if (tf_change>0)
1280
22.1M
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
3.33G
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
4.35M
   {
1285
4.35M
      OPUS_COPY(lowband_scratch, lowband, N);
1286
4.35M
      lowband = lowband_scratch;
1287
4.35M
   }
1288
1289
3.37G
   for (k=0;k<recombine;k++)
1290
37.6M
   {
1291
37.6M
      static const unsigned char bit_interleave_table[16]={
1292
37.6M
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
37.6M
      };
1294
37.6M
      if (encode)
1295
35.9M
         haar1(X, N>>k, 1<<k);
1296
37.6M
      if (lowband)
1297
1.75M
         haar1(lowband, N>>k, 1<<k);
1298
37.6M
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
37.6M
   }
1300
3.33G
   B>>=recombine;
1301
3.33G
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
3.35G
   while ((N_B&1) == 0 && tf_change<0)
1305
21.0M
   {
1306
21.0M
      if (encode)
1307
19.8M
         haar1(X, N_B, B);
1308
21.0M
      if (lowband)
1309
3.23M
         haar1(lowband, N_B, B);
1310
21.0M
      fill |= fill<<B;
1311
21.0M
      B <<= 1;
1312
21.0M
      N_B >>= 1;
1313
21.0M
      time_divide++;
1314
21.0M
      tf_change++;
1315
21.0M
   }
1316
3.33G
   B0=B;
1317
3.33G
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
3.33G
   if (B0>1)
1321
37.4M
   {
1322
37.4M
      if (encode)
1323
36.3M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
37.4M
      if (lowband)
1325
3.70M
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
37.4M
   }
1327
1328
#ifdef ENABLE_QEXT
1329
1.49G
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1330
28.7k
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1331
28.7k
   } else
1332
1.49G
#endif
1333
1.49G
   {
1334
1.49G
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
1.49G
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
3.33G
   if (ctx->resynth)
1339
421M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
421M
      if (B0>1)
1342
7.04M
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
421M
      N_B = N_B0;
1346
421M
      B = B0;
1347
427M
      for (k=0;k<time_divide;k++)
1348
6.11M
      {
1349
6.11M
         B >>= 1;
1350
6.11M
         N_B <<= 1;
1351
6.11M
         cm |= cm>>B;
1352
6.11M
         haar1(X, N_B, B);
1353
6.11M
      }
1354
1355
424M
      for (k=0;k<recombine;k++)
1356
3.03M
      {
1357
3.03M
         static const unsigned char bit_deinterleave_table[16]={
1358
3.03M
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
3.03M
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
3.03M
         };
1361
3.03M
         cm = bit_deinterleave_table[cm];
1362
3.03M
         haar1(X, N0>>k, 1<<k);
1363
3.03M
      }
1364
421M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
421M
      if (lowband_out)
1368
214M
      {
1369
214M
         int j;
1370
214M
         opus_val16 n;
1371
214M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
2.50G
         for (j=0;j<N0;j++)
1373
2.29G
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
214M
      }
1375
421M
      cm &= (1<<B)-1;
1376
421M
   }
1377
3.33G
   return cm;
1378
4.15G
}
bands.c:quant_band
Line
Count
Source
1253
1.18G
{
1254
1.18G
   int N0=N;
1255
1.18G
   int N_B=N;
1256
1.18G
   int N_B0;
1257
1.18G
   int B0=B;
1258
1.18G
   int time_divide=0;
1259
1.18G
   int recombine=0;
1260
1.18G
   int longBlocks;
1261
1.18G
   unsigned cm=0;
1262
1.18G
   int k;
1263
1.18G
   int encode;
1264
1.18G
   int tf_change;
1265
1266
1.18G
   encode = ctx->encode;
1267
1.18G
   tf_change = ctx->tf_change;
1268
1269
1.18G
   longBlocks = B0==1;
1270
1271
1.18G
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
1.18G
   if (N==1)
1275
263M
   {
1276
263M
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
263M
   }
1278
1279
920M
   if (tf_change>0)
1280
9.68M
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
920M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
1.46M
   {
1285
1.46M
      OPUS_COPY(lowband_scratch, lowband, N);
1286
1.46M
      lowband = lowband_scratch;
1287
1.46M
   }
1288
1289
936M
   for (k=0;k<recombine;k++)
1290
16.0M
   {
1291
16.0M
      static const unsigned char bit_interleave_table[16]={
1292
16.0M
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
16.0M
      };
1294
16.0M
      if (encode)
1295
15.4M
         haar1(X, N>>k, 1<<k);
1296
16.0M
      if (lowband)
1297
610k
         haar1(lowband, N>>k, 1<<k);
1298
16.0M
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
16.0M
   }
1300
920M
   B>>=recombine;
1301
920M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
926M
   while ((N_B&1) == 0 && tf_change<0)
1305
6.03M
   {
1306
6.03M
      if (encode)
1307
5.77M
         haar1(X, N_B, B);
1308
6.03M
      if (lowband)
1309
1.13M
         haar1(lowband, N_B, B);
1310
6.03M
      fill |= fill<<B;
1311
6.03M
      B <<= 1;
1312
6.03M
      N_B >>= 1;
1313
6.03M
      time_divide++;
1314
6.03M
      tf_change++;
1315
6.03M
   }
1316
920M
   B0=B;
1317
920M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
920M
   if (B0>1)
1321
12.9M
   {
1322
12.9M
      if (encode)
1323
12.7M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
12.9M
      if (lowband)
1325
1.23M
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
12.9M
   }
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
920M
   {
1334
920M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
920M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
920M
   if (ctx->resynth)
1339
137M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
137M
      if (B0>1)
1342
2.43M
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
137M
      N_B = N_B0;
1346
137M
      B = B0;
1347
139M
      for (k=0;k<time_divide;k++)
1348
2.22M
      {
1349
2.22M
         B >>= 1;
1350
2.22M
         N_B <<= 1;
1351
2.22M
         cm |= cm>>B;
1352
2.22M
         haar1(X, N_B, B);
1353
2.22M
      }
1354
1355
138M
      for (k=0;k<recombine;k++)
1356
1.08M
      {
1357
1.08M
         static const unsigned char bit_deinterleave_table[16]={
1358
1.08M
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
1.08M
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
1.08M
         };
1361
1.08M
         cm = bit_deinterleave_table[cm];
1362
1.08M
         haar1(X, N0>>k, 1<<k);
1363
1.08M
      }
1364
137M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
137M
      if (lowband_out)
1368
69.6M
      {
1369
69.6M
         int j;
1370
69.6M
         opus_val16 n;
1371
69.6M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
828M
         for (j=0;j<N0;j++)
1373
759M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
69.6M
      }
1375
137M
      cm &= (1<<B)-1;
1376
137M
   }
1377
920M
   return cm;
1378
1.18G
}
bands.c:quant_band
Line
Count
Source
1253
891M
{
1254
891M
   int N0=N;
1255
891M
   int N_B=N;
1256
891M
   int N_B0;
1257
891M
   int B0=B;
1258
891M
   int time_divide=0;
1259
891M
   int recombine=0;
1260
891M
   int longBlocks;
1261
891M
   unsigned cm=0;
1262
891M
   int k;
1263
891M
   int encode;
1264
891M
   int tf_change;
1265
1266
891M
   encode = ctx->encode;
1267
891M
   tf_change = ctx->tf_change;
1268
1269
891M
   longBlocks = B0==1;
1270
1271
891M
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
891M
   if (N==1)
1275
143M
   {
1276
143M
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
143M
   }
1278
1279
748M
   if (tf_change>0)
1280
1.39M
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
748M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
714k
   {
1285
714k
      OPUS_COPY(lowband_scratch, lowband, N);
1286
714k
      lowband = lowband_scratch;
1287
714k
   }
1288
1289
751M
   for (k=0;k<recombine;k++)
1290
2.77M
   {
1291
2.77M
      static const unsigned char bit_interleave_table[16]={
1292
2.77M
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
2.77M
      };
1294
2.77M
      if (encode)
1295
2.46M
         haar1(X, N>>k, 1<<k);
1296
2.77M
      if (lowband)
1297
266k
         haar1(lowband, N>>k, 1<<k);
1298
2.77M
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
2.77M
   }
1300
748M
   B>>=recombine;
1301
748M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
752M
   while ((N_B&1) == 0 && tf_change<0)
1305
4.48M
   {
1306
4.48M
      if (encode)
1307
4.14M
         haar1(X, N_B, B);
1308
4.48M
      if (lowband)
1309
488k
         haar1(lowband, N_B, B);
1310
4.48M
      fill |= fill<<B;
1311
4.48M
      B <<= 1;
1312
4.48M
      N_B >>= 1;
1313
4.48M
      time_divide++;
1314
4.48M
      tf_change++;
1315
4.48M
   }
1316
748M
   B0=B;
1317
748M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
748M
   if (B0>1)
1321
5.77M
   {
1322
5.77M
      if (encode)
1323
5.44M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
5.77M
      if (lowband)
1325
615k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
5.77M
   }
1327
1328
748M
#ifdef ENABLE_QEXT
1329
748M
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1330
14.3k
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1331
14.3k
   } else
1332
748M
#endif
1333
748M
   {
1334
748M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
748M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
748M
   if (ctx->resynth)
1339
73.2M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
73.2M
      if (B0>1)
1342
1.08M
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
73.2M
      N_B = N_B0;
1346
73.2M
      B = B0;
1347
74.0M
      for (k=0;k<time_divide;k++)
1348
832k
      {
1349
832k
         B >>= 1;
1350
832k
         N_B <<= 1;
1351
832k
         cm |= cm>>B;
1352
832k
         haar1(X, N_B, B);
1353
832k
      }
1354
1355
73.6M
      for (k=0;k<recombine;k++)
1356
428k
      {
1357
428k
         static const unsigned char bit_deinterleave_table[16]={
1358
428k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
428k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
428k
         };
1361
428k
         cm = bit_deinterleave_table[cm];
1362
428k
         haar1(X, N0>>k, 1<<k);
1363
428k
      }
1364
73.2M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
73.2M
      if (lowband_out)
1368
37.7M
      {
1369
37.7M
         int j;
1370
37.7M
         opus_val16 n;
1371
37.7M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
423M
         for (j=0;j<N0;j++)
1373
385M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
37.7M
      }
1375
73.2M
      cm &= (1<<B)-1;
1376
73.2M
   }
1377
748M
   return cm;
1378
891M
}
bands.c:quant_band
Line
Count
Source
1253
891M
{
1254
891M
   int N0=N;
1255
891M
   int N_B=N;
1256
891M
   int N_B0;
1257
891M
   int B0=B;
1258
891M
   int time_divide=0;
1259
891M
   int recombine=0;
1260
891M
   int longBlocks;
1261
891M
   unsigned cm=0;
1262
891M
   int k;
1263
891M
   int encode;
1264
891M
   int tf_change;
1265
1266
891M
   encode = ctx->encode;
1267
891M
   tf_change = ctx->tf_change;
1268
1269
891M
   longBlocks = B0==1;
1270
1271
891M
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
891M
   if (N==1)
1275
143M
   {
1276
143M
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
143M
   }
1278
1279
748M
   if (tf_change>0)
1280
1.39M
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
748M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
714k
   {
1285
714k
      OPUS_COPY(lowband_scratch, lowband, N);
1286
714k
      lowband = lowband_scratch;
1287
714k
   }
1288
1289
751M
   for (k=0;k<recombine;k++)
1290
2.77M
   {
1291
2.77M
      static const unsigned char bit_interleave_table[16]={
1292
2.77M
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
2.77M
      };
1294
2.77M
      if (encode)
1295
2.46M
         haar1(X, N>>k, 1<<k);
1296
2.77M
      if (lowband)
1297
266k
         haar1(lowband, N>>k, 1<<k);
1298
2.77M
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
2.77M
   }
1300
748M
   B>>=recombine;
1301
748M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
752M
   while ((N_B&1) == 0 && tf_change<0)
1305
4.48M
   {
1306
4.48M
      if (encode)
1307
4.14M
         haar1(X, N_B, B);
1308
4.48M
      if (lowband)
1309
488k
         haar1(lowband, N_B, B);
1310
4.48M
      fill |= fill<<B;
1311
4.48M
      B <<= 1;
1312
4.48M
      N_B >>= 1;
1313
4.48M
      time_divide++;
1314
4.48M
      tf_change++;
1315
4.48M
   }
1316
748M
   B0=B;
1317
748M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
748M
   if (B0>1)
1321
5.77M
   {
1322
5.77M
      if (encode)
1323
5.44M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
5.77M
      if (lowband)
1325
615k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
5.77M
   }
1327
1328
748M
#ifdef ENABLE_QEXT
1329
748M
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1330
14.3k
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1331
14.3k
   } else
1332
748M
#endif
1333
748M
   {
1334
748M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
748M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
748M
   if (ctx->resynth)
1339
73.2M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
73.2M
      if (B0>1)
1342
1.08M
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
73.2M
      N_B = N_B0;
1346
73.2M
      B = B0;
1347
74.0M
      for (k=0;k<time_divide;k++)
1348
832k
      {
1349
832k
         B >>= 1;
1350
832k
         N_B <<= 1;
1351
832k
         cm |= cm>>B;
1352
832k
         haar1(X, N_B, B);
1353
832k
      }
1354
1355
73.6M
      for (k=0;k<recombine;k++)
1356
428k
      {
1357
428k
         static const unsigned char bit_deinterleave_table[16]={
1358
428k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
428k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
428k
         };
1361
428k
         cm = bit_deinterleave_table[cm];
1362
428k
         haar1(X, N0>>k, 1<<k);
1363
428k
      }
1364
73.2M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
73.2M
      if (lowband_out)
1368
37.7M
      {
1369
37.7M
         int j;
1370
37.7M
         opus_val16 n;
1371
37.7M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
423M
         for (j=0;j<N0;j++)
1373
385M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
37.7M
      }
1375
73.2M
      cm &= (1<<B)-1;
1376
73.2M
   }
1377
748M
   return cm;
1378
891M
}
bands.c:quant_band
Line
Count
Source
1253
1.18G
{
1254
1.18G
   int N0=N;
1255
1.18G
   int N_B=N;
1256
1.18G
   int N_B0;
1257
1.18G
   int B0=B;
1258
1.18G
   int time_divide=0;
1259
1.18G
   int recombine=0;
1260
1.18G
   int longBlocks;
1261
1.18G
   unsigned cm=0;
1262
1.18G
   int k;
1263
1.18G
   int encode;
1264
1.18G
   int tf_change;
1265
1266
1.18G
   encode = ctx->encode;
1267
1.18G
   tf_change = ctx->tf_change;
1268
1269
1.18G
   longBlocks = B0==1;
1270
1271
1.18G
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
1.18G
   if (N==1)
1275
263M
   {
1276
263M
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
263M
   }
1278
1279
920M
   if (tf_change>0)
1280
9.68M
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
920M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
1.46M
   {
1285
1.46M
      OPUS_COPY(lowband_scratch, lowband, N);
1286
1.46M
      lowband = lowband_scratch;
1287
1.46M
   }
1288
1289
936M
   for (k=0;k<recombine;k++)
1290
16.0M
   {
1291
16.0M
      static const unsigned char bit_interleave_table[16]={
1292
16.0M
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
16.0M
      };
1294
16.0M
      if (encode)
1295
15.4M
         haar1(X, N>>k, 1<<k);
1296
16.0M
      if (lowband)
1297
610k
         haar1(lowband, N>>k, 1<<k);
1298
16.0M
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
16.0M
   }
1300
920M
   B>>=recombine;
1301
920M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
926M
   while ((N_B&1) == 0 && tf_change<0)
1305
6.03M
   {
1306
6.03M
      if (encode)
1307
5.77M
         haar1(X, N_B, B);
1308
6.03M
      if (lowband)
1309
1.13M
         haar1(lowband, N_B, B);
1310
6.03M
      fill |= fill<<B;
1311
6.03M
      B <<= 1;
1312
6.03M
      N_B >>= 1;
1313
6.03M
      time_divide++;
1314
6.03M
      tf_change++;
1315
6.03M
   }
1316
920M
   B0=B;
1317
920M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
920M
   if (B0>1)
1321
12.9M
   {
1322
12.9M
      if (encode)
1323
12.7M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
12.9M
      if (lowband)
1325
1.23M
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
12.9M
   }
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
920M
   {
1334
920M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
920M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
920M
   if (ctx->resynth)
1339
137M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
137M
      if (B0>1)
1342
2.43M
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
137M
      N_B = N_B0;
1346
137M
      B = B0;
1347
139M
      for (k=0;k<time_divide;k++)
1348
2.22M
      {
1349
2.22M
         B >>= 1;
1350
2.22M
         N_B <<= 1;
1351
2.22M
         cm |= cm>>B;
1352
2.22M
         haar1(X, N_B, B);
1353
2.22M
      }
1354
1355
138M
      for (k=0;k<recombine;k++)
1356
1.08M
      {
1357
1.08M
         static const unsigned char bit_deinterleave_table[16]={
1358
1.08M
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
1.08M
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
1.08M
         };
1361
1.08M
         cm = bit_deinterleave_table[cm];
1362
1.08M
         haar1(X, N0>>k, 1<<k);
1363
1.08M
      }
1364
137M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
137M
      if (lowband_out)
1368
69.6M
      {
1369
69.6M
         int j;
1370
69.6M
         opus_val16 n;
1371
69.6M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
828M
         for (j=0;j<N0;j++)
1373
759M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
69.6M
      }
1375
137M
      cm &= (1<<B)-1;
1376
137M
   }
1377
920M
   return cm;
1378
1.18G
}
1379
1380
#ifdef FIXED_POINT
1381
660M
#define MIN_STEREO_ENERGY 2
1382
#else
1383
660M
#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
752M
{
1393
752M
   int imid=0, iside=0;
1394
752M
   int inv = 0;
1395
752M
   opus_val32 mid=0, side=0;
1396
752M
   unsigned cm=0;
1397
752M
   int mbits, sbits, delta;
1398
752M
   int itheta;
1399
752M
   int qalloc;
1400
752M
   struct split_ctx sctx;
1401
752M
   int orig_fill;
1402
752M
   int encode;
1403
752M
   ec_ctx *ec;
1404
1405
752M
   encode = ctx->encode;
1406
752M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
752M
   if (N==1)
1410
104M
   {
1411
104M
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
104M
   }
1413
1414
648M
   orig_fill = fill;
1415
1416
648M
   if (encode) {
1417
646M
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
618M
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
617M
         else OPUS_COPY(X, Y, N);
1420
618M
      }
1421
646M
   }
1422
648M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
648M
   inv = sctx.inv;
1424
648M
   imid = sctx.imid;
1425
648M
   iside = sctx.iside;
1426
648M
   delta = sctx.delta;
1427
648M
   itheta = sctx.itheta;
1428
648M
   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
190M
   mid = SHL32(EXTEND32(imid), 16);
1437
190M
   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
648M
   if (N==2)
1455
161M
   {
1456
161M
      int c;
1457
161M
      int sign=0;
1458
161M
      celt_norm *x2, *y2;
1459
161M
      mbits = b;
1460
161M
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
161M
      if (itheta != 0 && itheta != 16384)
1463
928k
         sbits = 1<<BITRES;
1464
161M
      mbits -= sbits;
1465
161M
      c = itheta > 8192;
1466
161M
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
161M
      x2 = c ? Y : X;
1469
161M
      y2 = c ? X : Y;
1470
161M
      if (sbits)
1471
928k
      {
1472
928k
         if (encode)
1473
880k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
880k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
880k
            ec_enc_bits(ec, sign, 1);
1478
880k
         } else {
1479
47.7k
            sign = ec_dec_bits(ec, 1);
1480
47.7k
         }
1481
928k
      }
1482
161M
      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
161M
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
161M
            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
161M
      y2[0] = -sign*x2[1];
1490
161M
      y2[1] = sign*x2[0];
1491
161M
      if (ctx->resynth)
1492
33.4M
      {
1493
33.4M
         celt_norm tmp;
1494
33.4M
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
33.4M
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
33.4M
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
33.4M
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
33.4M
         tmp = X[0];
1499
33.4M
         X[0] = SUB32(tmp,Y[0]);
1500
33.4M
         Y[0] = ADD32(tmp,Y[0]);
1501
33.4M
         tmp = X[1];
1502
33.4M
         X[1] = SUB32(tmp,Y[1]);
1503
33.4M
         Y[1] = ADD32(tmp,Y[1]);
1504
33.4M
      }
1505
487M
   } else {
1506
      /* "Normal" split code */
1507
487M
      opus_int32 rebalance;
1508
1509
487M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
487M
      sbits = b-mbits;
1511
487M
      ctx->remaining_bits -= qalloc;
1512
1513
487M
      rebalance = ctx->remaining_bits;
1514
487M
      if (mbits >= sbits)
1515
484M
      {
1516
#ifdef ENABLE_QEXT
1517
         int qext_extra = 0;
1518
         /* Reallocate any mid bits that cannot be used to extra mid bits. */
1519
202M
         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
484M
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
484M
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
484M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
484M
         if (rebalance > 3<<BITRES && itheta!=0)
1527
1.11M
            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
202M
         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
484M
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
484M
      } else {
1536
#ifdef ENABLE_QEXT
1537
         int qext_extra = 0;
1538
         /* Reallocate any side bits that cannot be used to extra side bits. */
1539
591k
         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
2.64M
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
2.64M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
2.64M
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
59.0k
            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
591k
         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
2.64M
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
2.64M
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
2.64M
      }
1556
487M
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
648M
   if (ctx->resynth)
1561
224M
   {
1562
224M
      if (N!=2)
1563
191M
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
224M
      if (inv)
1565
179k
      {
1566
179k
         int j;
1567
3.66M
         for (j=0;j<N;j++)
1568
3.48M
            Y[j] = -Y[j];
1569
179k
      }
1570
224M
   }
1571
648M
   return cm;
1572
752M
}
bands.c:quant_band_stereo
Line
Count
Source
1392
235M
{
1393
235M
   int imid=0, iside=0;
1394
235M
   int inv = 0;
1395
235M
   opus_val32 mid=0, side=0;
1396
235M
   unsigned cm=0;
1397
235M
   int mbits, sbits, delta;
1398
235M
   int itheta;
1399
235M
   int qalloc;
1400
235M
   struct split_ctx sctx;
1401
235M
   int orig_fill;
1402
235M
   int encode;
1403
235M
   ec_ctx *ec;
1404
1405
235M
   encode = ctx->encode;
1406
235M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
235M
   if (N==1)
1410
44.9M
   {
1411
44.9M
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
44.9M
   }
1413
1414
190M
   orig_fill = fill;
1415
1416
190M
   if (encode) {
1417
189M
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
179M
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
178M
         else OPUS_COPY(X, Y, N);
1420
179M
      }
1421
189M
   }
1422
190M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
190M
   inv = sctx.inv;
1424
190M
   imid = sctx.imid;
1425
190M
   iside = sctx.iside;
1426
190M
   delta = sctx.delta;
1427
190M
   itheta = sctx.itheta;
1428
190M
   qalloc = sctx.qalloc;
1429
190M
#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
190M
   mid = SHL32(EXTEND32(imid), 16);
1437
190M
   side = SHL32(EXTEND32(iside), 16);
1438
190M
# 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
190M
   if (N==2)
1455
48.3M
   {
1456
48.3M
      int c;
1457
48.3M
      int sign=0;
1458
48.3M
      celt_norm *x2, *y2;
1459
48.3M
      mbits = b;
1460
48.3M
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
48.3M
      if (itheta != 0 && itheta != 16384)
1463
353k
         sbits = 1<<BITRES;
1464
48.3M
      mbits -= sbits;
1465
48.3M
      c = itheta > 8192;
1466
48.3M
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
48.3M
      x2 = c ? Y : X;
1469
48.3M
      y2 = c ? X : Y;
1470
48.3M
      if (sbits)
1471
353k
      {
1472
353k
         if (encode)
1473
342k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
342k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
342k
            ec_enc_bits(ec, sign, 1);
1478
342k
         } else {
1479
10.7k
            sign = ec_dec_bits(ec, 1);
1480
10.7k
         }
1481
353k
      }
1482
48.3M
      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
48.3M
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
48.3M
            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
48.3M
      y2[0] = -sign*x2[1];
1490
48.3M
      y2[1] = sign*x2[0];
1491
48.3M
      if (ctx->resynth)
1492
10.3M
      {
1493
10.3M
         celt_norm tmp;
1494
10.3M
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
10.3M
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
10.3M
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
10.3M
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
10.3M
         tmp = X[0];
1499
10.3M
         X[0] = SUB32(tmp,Y[0]);
1500
10.3M
         Y[0] = ADD32(tmp,Y[0]);
1501
10.3M
         tmp = X[1];
1502
10.3M
         X[1] = SUB32(tmp,Y[1]);
1503
10.3M
         Y[1] = ADD32(tmp,Y[1]);
1504
10.3M
      }
1505
142M
   } else {
1506
      /* "Normal" split code */
1507
142M
      opus_int32 rebalance;
1508
1509
142M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
142M
      sbits = b-mbits;
1511
142M
      ctx->remaining_bits -= qalloc;
1512
1513
142M
      rebalance = ctx->remaining_bits;
1514
142M
      if (mbits >= sbits)
1515
141M
      {
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
141M
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
141M
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
141M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
141M
         if (rebalance > 3<<BITRES && itheta!=0)
1527
531k
            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
141M
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
141M
      } 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
1.02M
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
1.02M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
1.02M
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
18.2k
            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
1.02M
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
1.02M
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
1.02M
      }
1556
142M
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
190M
   if (ctx->resynth)
1561
73.3M
   {
1562
73.3M
      if (N!=2)
1563
62.9M
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
73.3M
      if (inv)
1565
56.4k
      {
1566
56.4k
         int j;
1567
1.34M
         for (j=0;j<N;j++)
1568
1.28M
            Y[j] = -Y[j];
1569
56.4k
      }
1570
73.3M
   }
1571
190M
   return cm;
1572
235M
}
bands.c:quant_band_stereo
Line
Count
Source
1392
140M
{
1393
140M
   int imid=0, iside=0;
1394
140M
   int inv = 0;
1395
140M
   opus_val32 mid=0, side=0;
1396
140M
   unsigned cm=0;
1397
140M
   int mbits, sbits, delta;
1398
140M
   int itheta;
1399
140M
   int qalloc;
1400
140M
   struct split_ctx sctx;
1401
140M
   int orig_fill;
1402
140M
   int encode;
1403
140M
   ec_ctx *ec;
1404
1405
140M
   encode = ctx->encode;
1406
140M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
140M
   if (N==1)
1410
7.18M
   {
1411
7.18M
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
7.18M
   }
1413
1414
133M
   orig_fill = fill;
1415
1416
133M
   if (encode) {
1417
133M
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
130M
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
129M
         else OPUS_COPY(X, Y, N);
1420
130M
      }
1421
133M
   }
1422
133M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
133M
   inv = sctx.inv;
1424
133M
   imid = sctx.imid;
1425
133M
   iside = sctx.iside;
1426
133M
   delta = sctx.delta;
1427
133M
   itheta = sctx.itheta;
1428
133M
   qalloc = sctx.qalloc;
1429
133M
#ifdef FIXED_POINT
1430
133M
# ifdef ENABLE_QEXT
1431
133M
   (void)imid;
1432
133M
   (void)iside;
1433
133M
   mid = celt_cos_norm32(sctx.itheta_q30);
1434
133M
   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
133M
   if (N==2)
1455
32.1M
   {
1456
32.1M
      int c;
1457
32.1M
      int sign=0;
1458
32.1M
      celt_norm *x2, *y2;
1459
32.1M
      mbits = b;
1460
32.1M
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
32.1M
      if (itheta != 0 && itheta != 16384)
1463
110k
         sbits = 1<<BITRES;
1464
32.1M
      mbits -= sbits;
1465
32.1M
      c = itheta > 8192;
1466
32.1M
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
32.1M
      x2 = c ? Y : X;
1469
32.1M
      y2 = c ? X : Y;
1470
32.1M
      if (sbits)
1471
110k
      {
1472
110k
         if (encode)
1473
97.3k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
97.3k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
97.3k
            ec_enc_bits(ec, sign, 1);
1478
97.3k
         } else {
1479
13.1k
            sign = ec_dec_bits(ec, 1);
1480
13.1k
         }
1481
110k
      }
1482
32.1M
      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
32.1M
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
32.1M
            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
32.1M
      y2[0] = -sign*x2[1];
1490
32.1M
      y2[1] = sign*x2[0];
1491
32.1M
      if (ctx->resynth)
1492
6.33M
      {
1493
6.33M
         celt_norm tmp;
1494
6.33M
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
6.33M
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
6.33M
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
6.33M
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
6.33M
         tmp = X[0];
1499
6.33M
         X[0] = SUB32(tmp,Y[0]);
1500
6.33M
         Y[0] = ADD32(tmp,Y[0]);
1501
6.33M
         tmp = X[1];
1502
6.33M
         X[1] = SUB32(tmp,Y[1]);
1503
6.33M
         Y[1] = ADD32(tmp,Y[1]);
1504
6.33M
      }
1505
101M
   } else {
1506
      /* "Normal" split code */
1507
101M
      opus_int32 rebalance;
1508
1509
101M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
101M
      sbits = b-mbits;
1511
101M
      ctx->remaining_bits -= qalloc;
1512
1513
101M
      rebalance = ctx->remaining_bits;
1514
101M
      if (mbits >= sbits)
1515
101M
      {
1516
101M
#ifdef ENABLE_QEXT
1517
101M
         int qext_extra = 0;
1518
         /* Reallocate any mid bits that cannot be used to extra mid bits. */
1519
101M
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, mbits - cap[ctx->i]/2));
1520
101M
#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
101M
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
101M
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
101M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
101M
         if (rebalance > 3<<BITRES && itheta!=0)
1527
24.5k
            sbits += rebalance - (3<<BITRES);
1528
101M
#ifdef ENABLE_QEXT
1529
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the mid. */
1530
101M
         if (ctx->extra_bands) sbits = IMIN(sbits, ctx->remaining_bits);
1531
101M
#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
101M
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
101M
      } else {
1536
295k
#ifdef ENABLE_QEXT
1537
295k
         int qext_extra = 0;
1538
         /* Reallocate any side bits that cannot be used to extra side bits. */
1539
295k
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, sbits - cap[ctx->i]/2));
1540
295k
#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
295k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
295k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
295k
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
11.2k
            mbits += rebalance - (3<<BITRES);
1547
295k
#ifdef ENABLE_QEXT
1548
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the side. */
1549
295k
         if (ctx->extra_bands) mbits = IMIN(mbits, ctx->remaining_bits);
1550
295k
#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
295k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
295k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
295k
      }
1556
101M
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
133M
   if (ctx->resynth)
1561
39.0M
   {
1562
39.0M
      if (N!=2)
1563
32.7M
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
39.0M
      if (inv)
1565
33.2k
      {
1566
33.2k
         int j;
1567
489k
         for (j=0;j<N;j++)
1568
455k
            Y[j] = -Y[j];
1569
33.2k
      }
1570
39.0M
   }
1571
133M
   return cm;
1572
140M
}
bands.c:quant_band_stereo
Line
Count
Source
1392
140M
{
1393
140M
   int imid=0, iside=0;
1394
140M
   int inv = 0;
1395
140M
   opus_val32 mid=0, side=0;
1396
140M
   unsigned cm=0;
1397
140M
   int mbits, sbits, delta;
1398
140M
   int itheta;
1399
140M
   int qalloc;
1400
140M
   struct split_ctx sctx;
1401
140M
   int orig_fill;
1402
140M
   int encode;
1403
140M
   ec_ctx *ec;
1404
1405
140M
   encode = ctx->encode;
1406
140M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
140M
   if (N==1)
1410
7.18M
   {
1411
7.18M
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
7.18M
   }
1413
1414
133M
   orig_fill = fill;
1415
1416
133M
   if (encode) {
1417
133M
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
130M
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
129M
         else OPUS_COPY(X, Y, N);
1420
130M
      }
1421
133M
   }
1422
133M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
133M
   inv = sctx.inv;
1424
133M
   imid = sctx.imid;
1425
133M
   iside = sctx.iside;
1426
133M
   delta = sctx.delta;
1427
133M
   itheta = sctx.itheta;
1428
133M
   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
133M
# ifdef ENABLE_QEXT
1441
133M
   (void)imid;
1442
133M
   (void)iside;
1443
133M
   mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1444
133M
   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
133M
#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
133M
   if (N==2)
1455
32.1M
   {
1456
32.1M
      int c;
1457
32.1M
      int sign=0;
1458
32.1M
      celt_norm *x2, *y2;
1459
32.1M
      mbits = b;
1460
32.1M
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
32.1M
      if (itheta != 0 && itheta != 16384)
1463
110k
         sbits = 1<<BITRES;
1464
32.1M
      mbits -= sbits;
1465
32.1M
      c = itheta > 8192;
1466
32.1M
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
32.1M
      x2 = c ? Y : X;
1469
32.1M
      y2 = c ? X : Y;
1470
32.1M
      if (sbits)
1471
110k
      {
1472
110k
         if (encode)
1473
97.3k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
97.3k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
97.3k
            ec_enc_bits(ec, sign, 1);
1478
97.3k
         } else {
1479
13.1k
            sign = ec_dec_bits(ec, 1);
1480
13.1k
         }
1481
110k
      }
1482
32.1M
      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
32.1M
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
32.1M
            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
32.1M
      y2[0] = -sign*x2[1];
1490
32.1M
      y2[1] = sign*x2[0];
1491
32.1M
      if (ctx->resynth)
1492
6.33M
      {
1493
6.33M
         celt_norm tmp;
1494
6.33M
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
6.33M
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
6.33M
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
6.33M
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
6.33M
         tmp = X[0];
1499
6.33M
         X[0] = SUB32(tmp,Y[0]);
1500
6.33M
         Y[0] = ADD32(tmp,Y[0]);
1501
6.33M
         tmp = X[1];
1502
6.33M
         X[1] = SUB32(tmp,Y[1]);
1503
6.33M
         Y[1] = ADD32(tmp,Y[1]);
1504
6.33M
      }
1505
101M
   } else {
1506
      /* "Normal" split code */
1507
101M
      opus_int32 rebalance;
1508
1509
101M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
101M
      sbits = b-mbits;
1511
101M
      ctx->remaining_bits -= qalloc;
1512
1513
101M
      rebalance = ctx->remaining_bits;
1514
101M
      if (mbits >= sbits)
1515
101M
      {
1516
101M
#ifdef ENABLE_QEXT
1517
101M
         int qext_extra = 0;
1518
         /* Reallocate any mid bits that cannot be used to extra mid bits. */
1519
101M
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, mbits - cap[ctx->i]/2));
1520
101M
#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
101M
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
101M
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
101M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
101M
         if (rebalance > 3<<BITRES && itheta!=0)
1527
24.5k
            sbits += rebalance - (3<<BITRES);
1528
101M
#ifdef ENABLE_QEXT
1529
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the mid. */
1530
101M
         if (ctx->extra_bands) sbits = IMIN(sbits, ctx->remaining_bits);
1531
101M
#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
101M
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
101M
      } else {
1536
295k
#ifdef ENABLE_QEXT
1537
295k
         int qext_extra = 0;
1538
         /* Reallocate any side bits that cannot be used to extra side bits. */
1539
295k
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, sbits - cap[ctx->i]/2));
1540
295k
#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
295k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
295k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
295k
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
11.2k
            mbits += rebalance - (3<<BITRES);
1547
295k
#ifdef ENABLE_QEXT
1548
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the side. */
1549
295k
         if (ctx->extra_bands) mbits = IMIN(mbits, ctx->remaining_bits);
1550
295k
#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
295k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
295k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
295k
      }
1556
101M
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
133M
   if (ctx->resynth)
1561
39.0M
   {
1562
39.0M
      if (N!=2)
1563
32.7M
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
39.0M
      if (inv)
1565
33.2k
      {
1566
33.2k
         int j;
1567
489k
         for (j=0;j<N;j++)
1568
455k
            Y[j] = -Y[j];
1569
33.2k
      }
1570
39.0M
   }
1571
133M
   return cm;
1572
140M
}
bands.c:quant_band_stereo
Line
Count
Source
1392
235M
{
1393
235M
   int imid=0, iside=0;
1394
235M
   int inv = 0;
1395
235M
   opus_val32 mid=0, side=0;
1396
235M
   unsigned cm=0;
1397
235M
   int mbits, sbits, delta;
1398
235M
   int itheta;
1399
235M
   int qalloc;
1400
235M
   struct split_ctx sctx;
1401
235M
   int orig_fill;
1402
235M
   int encode;
1403
235M
   ec_ctx *ec;
1404
1405
235M
   encode = ctx->encode;
1406
235M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
235M
   if (N==1)
1410
44.9M
   {
1411
44.9M
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
44.9M
   }
1413
1414
190M
   orig_fill = fill;
1415
1416
190M
   if (encode) {
1417
189M
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
179M
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
178M
         else OPUS_COPY(X, Y, N);
1420
179M
      }
1421
189M
   }
1422
190M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
190M
   inv = sctx.inv;
1424
190M
   imid = sctx.imid;
1425
190M
   iside = sctx.iside;
1426
190M
   delta = sctx.delta;
1427
190M
   itheta = sctx.itheta;
1428
190M
   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
190M
   mid = (1.f/32768)*imid;
1447
190M
   side = (1.f/32768)*iside;
1448
190M
# endif
1449
190M
#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
190M
   if (N==2)
1455
48.3M
   {
1456
48.3M
      int c;
1457
48.3M
      int sign=0;
1458
48.3M
      celt_norm *x2, *y2;
1459
48.3M
      mbits = b;
1460
48.3M
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
48.3M
      if (itheta != 0 && itheta != 16384)
1463
353k
         sbits = 1<<BITRES;
1464
48.3M
      mbits -= sbits;
1465
48.3M
      c = itheta > 8192;
1466
48.3M
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
48.3M
      x2 = c ? Y : X;
1469
48.3M
      y2 = c ? X : Y;
1470
48.3M
      if (sbits)
1471
353k
      {
1472
353k
         if (encode)
1473
342k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
342k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
342k
            ec_enc_bits(ec, sign, 1);
1478
342k
         } else {
1479
10.7k
            sign = ec_dec_bits(ec, 1);
1480
10.7k
         }
1481
353k
      }
1482
48.3M
      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
48.3M
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
48.3M
            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
48.3M
      y2[0] = -sign*x2[1];
1490
48.3M
      y2[1] = sign*x2[0];
1491
48.3M
      if (ctx->resynth)
1492
10.3M
      {
1493
10.3M
         celt_norm tmp;
1494
10.3M
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
10.3M
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
10.3M
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
10.3M
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
10.3M
         tmp = X[0];
1499
10.3M
         X[0] = SUB32(tmp,Y[0]);
1500
10.3M
         Y[0] = ADD32(tmp,Y[0]);
1501
10.3M
         tmp = X[1];
1502
10.3M
         X[1] = SUB32(tmp,Y[1]);
1503
10.3M
         Y[1] = ADD32(tmp,Y[1]);
1504
10.3M
      }
1505
142M
   } else {
1506
      /* "Normal" split code */
1507
142M
      opus_int32 rebalance;
1508
1509
142M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
142M
      sbits = b-mbits;
1511
142M
      ctx->remaining_bits -= qalloc;
1512
1513
142M
      rebalance = ctx->remaining_bits;
1514
142M
      if (mbits >= sbits)
1515
141M
      {
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
141M
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
141M
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
141M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
141M
         if (rebalance > 3<<BITRES && itheta!=0)
1527
531k
            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
141M
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
141M
      } 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
1.02M
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
1.02M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
1.02M
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
18.2k
            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
1.02M
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
1.02M
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
1.02M
      }
1556
142M
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
190M
   if (ctx->resynth)
1561
73.3M
   {
1562
73.3M
      if (N!=2)
1563
62.9M
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
73.3M
      if (inv)
1565
56.4k
      {
1566
56.4k
         int j;
1567
1.34M
         for (j=0;j<N;j++)
1568
1.28M
            Y[j] = -Y[j];
1569
56.4k
      }
1570
73.3M
   }
1571
190M
   return cm;
1572
235M
}
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
127M
{
1577
127M
   int n1, n2;
1578
127M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1579
127M
   n1 = M*(eBands[start+1]-eBands[start]);
1580
127M
   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
127M
   OPUS_COPY(&norm[n1], &norm[2*n1 - n2], n2-n1);
1584
127M
   if (dual_stereo)
1585
474k
      OPUS_COPY(&norm2[n1], &norm2[2*n1 - n2], n2-n1);
1586
127M
}
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
252M
{
1598
252M
   int i;
1599
252M
   opus_int32 remaining_bits;
1600
252M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
252M
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
252M
   VARDECL(celt_norm, _norm);
1603
252M
   VARDECL(celt_norm, _lowband_scratch);
1604
252M
   VARDECL(celt_norm, X_save);
1605
252M
   VARDECL(celt_norm, Y_save);
1606
252M
   VARDECL(celt_norm, X_save2);
1607
252M
   VARDECL(celt_norm, Y_save2);
1608
252M
   VARDECL(celt_norm, norm_save2);
1609
252M
   VARDECL(unsigned char, bytes_save);
1610
252M
   int resynth_alloc;
1611
252M
   celt_norm *lowband_scratch;
1612
252M
   int B;
1613
252M
   int M;
1614
252M
   int lowband_offset;
1615
252M
   int update_lowband = 1;
1616
252M
   int C = Y_ != NULL ? 2 : 1;
1617
252M
   int norm_offset;
1618
252M
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1619
#ifdef RESYNTH
1620
   int resynth = 1;
1621
#else
1622
252M
   int resynth = !encode || theta_rdo;
1623
252M
#endif
1624
252M
   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
252M
   SAVE_STACK;
1632
1633
252M
   M = 1<<LM;
1634
252M
   B = shortBlocks ? M : 1;
1635
252M
   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
252M
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1639
252M
   norm = _norm;
1640
252M
   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
252M
   if (encode && resynth)
1646
14.6M
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1647
238M
   else
1648
238M
      resynth_alloc = ALLOC_NONE;
1649
252M
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1650
252M
   if (encode && resynth)
1651
14.6M
      lowband_scratch = _lowband_scratch;
1652
238M
   else
1653
238M
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1654
252M
   ALLOC(X_save, resynth_alloc, celt_norm);
1655
252M
   ALLOC(Y_save, resynth_alloc, celt_norm);
1656
252M
   ALLOC(X_save2, resynth_alloc, celt_norm);
1657
252M
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1658
252M
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1659
1660
252M
   lowband_offset = 0;
1661
252M
   ctx.bandE = bandE;
1662
252M
   ctx.ec = ec;
1663
252M
   ctx.encode = encode;
1664
252M
   ctx.intensity = intensity;
1665
252M
   ctx.m = m;
1666
252M
   ctx.seed = *seed;
1667
252M
   ctx.spread = spread;
1668
252M
   ctx.arch = arch;
1669
252M
   ctx.disable_inv = disable_inv;
1670
252M
   ctx.resynth = resynth;
1671
252M
   ctx.theta_round = 0;
1672
#ifdef ENABLE_QEXT
1673
   ctx.ext_ec = ext_ec;
1674
   ctx.ext_total_bits = ext_total_bits;
1675
108M
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1676
108M
   if (ctx.extra_bands) theta_rdo = 0;
1677
108M
   ALLOC(ext_bytes_save, theta_rdo ? QEXT_PACKET_SIZE_CAP : ALLOC_NONE, unsigned char);
1678
#endif
1679
252M
   ALLOC(bytes_save, theta_rdo ? 1275 : ALLOC_NONE, unsigned char);
1680
1681
   /* Avoid injecting noise in the first band on transients. */
1682
252M
   ctx.avoid_split_noise = B > 1;
1683
3.99G
   for (i=start;i<end;i++)
1684
3.74G
   {
1685
3.74G
      opus_int32 tell;
1686
3.74G
      int b;
1687
3.74G
      int N;
1688
3.74G
      opus_int32 curr_balance;
1689
3.74G
      int effective_lowband=-1;
1690
3.74G
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1691
3.74G
      int tf_change=0;
1692
3.74G
      unsigned x_cm;
1693
3.74G
      unsigned y_cm;
1694
3.74G
      int last;
1695
1696
3.74G
      ctx.i = i;
1697
3.74G
      last = (i==end-1);
1698
1699
3.74G
      X = X_+M*eBands[i];
1700
3.74G
      if (Y_!=NULL)
1701
749M
         Y = Y_+M*eBands[i];
1702
2.99G
      else
1703
2.99G
         Y = NULL;
1704
3.74G
      N = M*eBands[i+1]-M*eBands[i];
1705
3.74G
      celt_assert(N > 0);
1706
3.74G
      tell = ec_tell_frac(ec);
1707
1708
      /* Compute how many bits we want to allocate to this band */
1709
3.74G
      if (i != start)
1710
3.48G
         balance -= tell;
1711
3.74G
      remaining_bits = total_bits-tell-1;
1712
3.74G
      ctx.remaining_bits = remaining_bits;
1713
#ifdef ENABLE_QEXT
1714
1.59G
      if (i != start) {
1715
1.48G
         ext_balance += extra_pulses[i-1] + ext_tell;
1716
1.48G
      }
1717
      ext_tell = ec_tell_frac(ext_ec);
1718
      ctx.extra_bits = extra_pulses[i];
1719
1.59G
      if (i != start)
1720
1.48G
         ext_balance -= ext_tell;
1721
1.59G
      if (i <= codedBands-1)
1722
246M
      {
1723
246M
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1724
246M
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1725
1.34G
      } else {
1726
1.34G
         ext_b = 0;
1727
1.34G
      }
1728
#endif
1729
3.74G
      if (i <= codedBands-1)
1730
821M
      {
1731
821M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1732
821M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1733
2.92G
      } else {
1734
2.92G
         b = 0;
1735
2.92G
      }
1736
1737
3.74G
#ifndef DISABLE_UPDATE_DRAFT
1738
3.74G
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1739
30.5M
            lowband_offset = i;
1740
3.74G
      if (i == start+1)
1741
252M
         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.74G
      tf_change = tf_res[i];
1748
3.74G
      ctx.tf_change = tf_change;
1749
3.74G
      if (i>=m->effEBands)
1750
44.7k
      {
1751
44.7k
         X=norm;
1752
44.7k
         if (Y_!=NULL)
1753
33.9k
            Y = norm;
1754
44.7k
         lowband_scratch = NULL;
1755
44.7k
      }
1756
3.74G
      if (last && !theta_rdo)
1757
238M
         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.74G
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1762
214M
      {
1763
214M
         int fold_start;
1764
214M
         int fold_end;
1765
214M
         int fold_i;
1766
         /* This ensures we never repeat spectral content within one band */
1767
214M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1768
214M
         fold_start = lowband_offset;
1769
222M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1770
214M
         fold_end = lowband_offset-1;
1771
214M
#ifndef DISABLE_UPDATE_DRAFT
1772
459M
         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
214M
         x_cm = y_cm = 0;
1777
468M
         fold_i = fold_start; do {
1778
468M
           x_cm |= collapse_masks[fold_i*C+0];
1779
468M
           y_cm |= collapse_masks[fold_i*C+C-1];
1780
468M
         } while (++fold_i<fold_end);
1781
214M
      }
1782
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1783
         always) be non-zero. */
1784
3.52G
      else
1785
3.52G
         x_cm = y_cm = (1<<B)-1;
1786
1787
3.74G
      if (dual_stereo && i==intensity)
1788
715k
      {
1789
715k
         int j;
1790
1791
         /* Switch off dual stereo to do intensity. */
1792
715k
         dual_stereo = 0;
1793
715k
         if (resynth)
1794
1.18M
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1795
1.15M
               norm[j] = HALF32(norm[j]+norm2[j]);
1796
715k
      }
1797
3.74G
      if (dual_stereo)
1798
10.7M
      {
1799
10.7M
         x_cm = quant_band(&ctx, X, N, b/2, B,
1800
10.7M
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1801
10.7M
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1802
10.7M
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1803
10.7M
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1804
10.7M
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1805
3.73G
      } else {
1806
3.73G
         if (Y!=NULL)
1807
738M
         {
1808
738M
            if (theta_rdo && i < intensity)
1809
14.3M
            {
1810
14.3M
               ec_ctx ec_save, ec_save2;
1811
14.3M
               struct band_ctx ctx_save, ctx_save2;
1812
14.3M
               opus_val32 dist0, dist1;
1813
14.3M
               unsigned cm, cm2;
1814
14.3M
               int nstart_bytes, nend_bytes, save_bytes;
1815
14.3M
               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
14.3M
               opus_val16 w[2];
1822
14.3M
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1823
               /* Make a copy. */
1824
14.3M
               cm = x_cm|y_cm;
1825
14.3M
               ec_save = *ec;
1826
#ifdef ENABLE_QEXT
1827
               ext_ec_save = *ext_ec;
1828
#endif
1829
14.3M
               ctx_save = ctx;
1830
14.3M
               OPUS_COPY(X_save, X, N);
1831
14.3M
               OPUS_COPY(Y_save, Y, N);
1832
               /* Encode and round down. */
1833
14.3M
               ctx.theta_round = -1;
1834
14.3M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1835
14.3M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1836
14.3M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1837
14.3M
               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
14.3M
               cm2 = x_cm;
1841
14.3M
               ec_save2 = *ec;
1842
#ifdef ENABLE_QEXT
1843
               ext_ec_save2 = *ext_ec;
1844
#endif
1845
14.3M
               ctx_save2 = ctx;
1846
14.3M
               OPUS_COPY(X_save2, X, N);
1847
14.3M
               OPUS_COPY(Y_save2, Y, N);
1848
14.3M
               if (!last)
1849
14.2M
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1850
14.3M
               nstart_bytes = ec_save.offs;
1851
14.3M
               nend_bytes = ec_save.storage;
1852
14.3M
               bytes_buf = ec_save.buf+nstart_bytes;
1853
14.3M
               save_bytes = nend_bytes-nstart_bytes;
1854
14.3M
               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
788k
               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
788k
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1861
#endif
1862
               /* Restore */
1863
14.3M
               *ec = ec_save;
1864
#ifdef ENABLE_QEXT
1865
               *ext_ec = ext_ec_save;
1866
#endif
1867
14.3M
               ctx = ctx_save;
1868
14.3M
               OPUS_COPY(X, X_save, N);
1869
14.3M
               OPUS_COPY(Y, Y_save, N);
1870
14.3M
#ifndef DISABLE_UPDATE_DRAFT
1871
14.3M
               if (i == start+1)
1872
1.41M
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1873
14.3M
#endif
1874
               /* Encode and round up. */
1875
14.3M
               ctx.theta_round = 1;
1876
14.3M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1877
14.3M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1878
14.3M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1879
14.3M
               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
14.3M
               if (dist0 >= dist1) {
1881
13.3M
                  x_cm = cm2;
1882
13.3M
                  *ec = ec_save2;
1883
#ifdef ENABLE_QEXT
1884
                  *ext_ec = ext_ec_save2;
1885
#endif
1886
13.3M
                  ctx = ctx_save2;
1887
13.3M
                  OPUS_COPY(X, X_save2, N);
1888
13.3M
                  OPUS_COPY(Y, Y_save2, N);
1889
13.3M
                  if (!last)
1890
13.2M
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1891
13.3M
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1892
#ifdef ENABLE_QEXT
1893
509k
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1894
#endif
1895
13.3M
               }
1896
724M
            } else {
1897
724M
               ctx.theta_round = 0;
1898
724M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1899
724M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1900
724M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1901
724M
            }
1902
2.99G
         } else {
1903
2.99G
            x_cm = quant_band(&ctx, X, N, b, B,
1904
2.99G
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1905
2.99G
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1906
2.99G
         }
1907
3.73G
         y_cm = x_cm;
1908
3.73G
      }
1909
3.74G
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1910
3.74G
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1911
3.74G
      balance += pulses[i] + tell;
1912
1913
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1914
3.74G
      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.74G
      ctx.avoid_split_noise = 0;
1918
3.74G
   }
1919
252M
   *seed = ctx.seed;
1920
1921
252M
   RESTORE_STACK;
1922
252M
}
quant_all_bands
Line
Count
Source
1597
72.0M
{
1598
72.0M
   int i;
1599
72.0M
   opus_int32 remaining_bits;
1600
72.0M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
72.0M
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
72.0M
   VARDECL(celt_norm, _norm);
1603
72.0M
   VARDECL(celt_norm, _lowband_scratch);
1604
72.0M
   VARDECL(celt_norm, X_save);
1605
72.0M
   VARDECL(celt_norm, Y_save);
1606
72.0M
   VARDECL(celt_norm, X_save2);
1607
72.0M
   VARDECL(celt_norm, Y_save2);
1608
72.0M
   VARDECL(celt_norm, norm_save2);
1609
72.0M
   VARDECL(unsigned char, bytes_save);
1610
72.0M
   int resynth_alloc;
1611
72.0M
   celt_norm *lowband_scratch;
1612
72.0M
   int B;
1613
72.0M
   int M;
1614
72.0M
   int lowband_offset;
1615
72.0M
   int update_lowband = 1;
1616
72.0M
   int C = Y_ != NULL ? 2 : 1;
1617
72.0M
   int norm_offset;
1618
72.0M
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1619
#ifdef RESYNTH
1620
   int resynth = 1;
1621
#else
1622
72.0M
   int resynth = !encode || theta_rdo;
1623
72.0M
#endif
1624
72.0M
   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
72.0M
   SAVE_STACK;
1632
1633
72.0M
   M = 1<<LM;
1634
72.0M
   B = shortBlocks ? M : 1;
1635
72.0M
   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
72.0M
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1639
72.0M
   norm = _norm;
1640
72.0M
   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
72.0M
   if (encode && resynth)
1646
4.68M
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1647
67.3M
   else
1648
67.3M
      resynth_alloc = ALLOC_NONE;
1649
72.0M
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1650
72.0M
   if (encode && resynth)
1651
4.68M
      lowband_scratch = _lowband_scratch;
1652
67.3M
   else
1653
67.3M
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1654
72.0M
   ALLOC(X_save, resynth_alloc, celt_norm);
1655
72.0M
   ALLOC(Y_save, resynth_alloc, celt_norm);
1656
72.0M
   ALLOC(X_save2, resynth_alloc, celt_norm);
1657
72.0M
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1658
72.0M
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1659
1660
72.0M
   lowband_offset = 0;
1661
72.0M
   ctx.bandE = bandE;
1662
72.0M
   ctx.ec = ec;
1663
72.0M
   ctx.encode = encode;
1664
72.0M
   ctx.intensity = intensity;
1665
72.0M
   ctx.m = m;
1666
72.0M
   ctx.seed = *seed;
1667
72.0M
   ctx.spread = spread;
1668
72.0M
   ctx.arch = arch;
1669
72.0M
   ctx.disable_inv = disable_inv;
1670
72.0M
   ctx.resynth = resynth;
1671
72.0M
   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
72.0M
   ALLOC(bytes_save, theta_rdo ? 1275 : ALLOC_NONE, unsigned char);
1680
1681
   /* Avoid injecting noise in the first band on transients. */
1682
72.0M
   ctx.avoid_split_noise = B > 1;
1683
1.14G
   for (i=start;i<end;i++)
1684
1.07G
   {
1685
1.07G
      opus_int32 tell;
1686
1.07G
      int b;
1687
1.07G
      int N;
1688
1.07G
      opus_int32 curr_balance;
1689
1.07G
      int effective_lowband=-1;
1690
1.07G
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1691
1.07G
      int tf_change=0;
1692
1.07G
      unsigned x_cm;
1693
1.07G
      unsigned y_cm;
1694
1.07G
      int last;
1695
1696
1.07G
      ctx.i = i;
1697
1.07G
      last = (i==end-1);
1698
1699
1.07G
      X = X_+M*eBands[i];
1700
1.07G
      if (Y_!=NULL)
1701
233M
         Y = Y_+M*eBands[i];
1702
840M
      else
1703
840M
         Y = NULL;
1704
1.07G
      N = M*eBands[i+1]-M*eBands[i];
1705
1.07G
      celt_assert(N > 0);
1706
1.07G
      tell = ec_tell_frac(ec);
1707
1708
      /* Compute how many bits we want to allocate to this band */
1709
1.07G
      if (i != start)
1710
1.00G
         balance -= tell;
1711
1.07G
      remaining_bits = total_bits-tell-1;
1712
1.07G
      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
1.07G
      if (i <= codedBands-1)
1730
287M
      {
1731
287M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1732
287M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1733
786M
      } else {
1734
786M
         b = 0;
1735
786M
      }
1736
1737
1.07G
#ifndef DISABLE_UPDATE_DRAFT
1738
1.07G
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1739
11.6M
            lowband_offset = i;
1740
1.07G
      if (i == start+1)
1741
72.0M
         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
1.07G
      tf_change = tf_res[i];
1748
1.07G
      ctx.tf_change = tf_change;
1749
1.07G
      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
1.07G
      if (last && !theta_rdo)
1757
67.3M
         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
1.07G
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1762
68.5M
      {
1763
68.5M
         int fold_start;
1764
68.5M
         int fold_end;
1765
68.5M
         int fold_i;
1766
         /* This ensures we never repeat spectral content within one band */
1767
68.5M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1768
68.5M
         fold_start = lowband_offset;
1769
72.3M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1770
68.5M
         fold_end = lowband_offset-1;
1771
68.5M
#ifndef DISABLE_UPDATE_DRAFT
1772
147M
         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
68.5M
         x_cm = y_cm = 0;
1777
151M
         fold_i = fold_start; do {
1778
151M
           x_cm |= collapse_masks[fold_i*C+0];
1779
151M
           y_cm |= collapse_masks[fold_i*C+C-1];
1780
151M
         } while (++fold_i<fold_end);
1781
68.5M
      }
1782
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1783
         always) be non-zero. */
1784
1.00G
      else
1785
1.00G
         x_cm = y_cm = (1<<B)-1;
1786
1787
1.07G
      if (dual_stereo && i==intensity)
1788
308k
      {
1789
308k
         int j;
1790
1791
         /* Switch off dual stereo to do intensity. */
1792
308k
         dual_stereo = 0;
1793
308k
         if (resynth)
1794
222k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1795
216k
               norm[j] = HALF32(norm[j]+norm2[j]);
1796
308k
      }
1797
1.07G
      if (dual_stereo)
1798
4.80M
      {
1799
4.80M
         x_cm = quant_band(&ctx, X, N, b/2, B,
1800
4.80M
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1801
4.80M
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1802
4.80M
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1803
4.80M
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1804
4.80M
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1805
1.06G
      } else {
1806
1.06G
         if (Y!=NULL)
1807
228M
         {
1808
228M
            if (theta_rdo && i < intensity)
1809
6.77M
            {
1810
6.77M
               ec_ctx ec_save, ec_save2;
1811
6.77M
               struct band_ctx ctx_save, ctx_save2;
1812
6.77M
               opus_val32 dist0, dist1;
1813
6.77M
               unsigned cm, cm2;
1814
6.77M
               int nstart_bytes, nend_bytes, save_bytes;
1815
6.77M
               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
6.77M
               opus_val16 w[2];
1822
6.77M
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1823
               /* Make a copy. */
1824
6.77M
               cm = x_cm|y_cm;
1825
6.77M
               ec_save = *ec;
1826
#ifdef ENABLE_QEXT
1827
               ext_ec_save = *ext_ec;
1828
#endif
1829
6.77M
               ctx_save = ctx;
1830
6.77M
               OPUS_COPY(X_save, X, N);
1831
6.77M
               OPUS_COPY(Y_save, Y, N);
1832
               /* Encode and round down. */
1833
6.77M
               ctx.theta_round = -1;
1834
6.77M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1835
6.77M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1836
6.77M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1837
6.77M
               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
6.77M
               cm2 = x_cm;
1841
6.77M
               ec_save2 = *ec;
1842
#ifdef ENABLE_QEXT
1843
               ext_ec_save2 = *ext_ec;
1844
#endif
1845
6.77M
               ctx_save2 = ctx;
1846
6.77M
               OPUS_COPY(X_save2, X, N);
1847
6.77M
               OPUS_COPY(Y_save2, Y, N);
1848
6.77M
               if (!last)
1849
6.73M
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1850
6.77M
               nstart_bytes = ec_save.offs;
1851
6.77M
               nend_bytes = ec_save.storage;
1852
6.77M
               bytes_buf = ec_save.buf+nstart_bytes;
1853
6.77M
               save_bytes = nend_bytes-nstart_bytes;
1854
6.77M
               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
6.77M
               *ec = ec_save;
1864
#ifdef ENABLE_QEXT
1865
               *ext_ec = ext_ec_save;
1866
#endif
1867
6.77M
               ctx = ctx_save;
1868
6.77M
               OPUS_COPY(X, X_save, N);
1869
6.77M
               OPUS_COPY(Y, Y_save, N);
1870
6.77M
#ifndef DISABLE_UPDATE_DRAFT
1871
6.77M
               if (i == start+1)
1872
671k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1873
6.77M
#endif
1874
               /* Encode and round up. */
1875
6.77M
               ctx.theta_round = 1;
1876
6.77M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1877
6.77M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1878
6.77M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1879
6.77M
               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
6.77M
               if (dist0 >= dist1) {
1881
6.41M
                  x_cm = cm2;
1882
6.41M
                  *ec = ec_save2;
1883
#ifdef ENABLE_QEXT
1884
                  *ext_ec = ext_ec_save2;
1885
#endif
1886
6.41M
                  ctx = ctx_save2;
1887
6.41M
                  OPUS_COPY(X, X_save2, N);
1888
6.41M
                  OPUS_COPY(Y, Y_save2, N);
1889
6.41M
                  if (!last)
1890
6.37M
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1891
6.41M
                  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
6.41M
               }
1896
221M
            } else {
1897
221M
               ctx.theta_round = 0;
1898
221M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1899
221M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1900
221M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1901
221M
            }
1902
840M
         } else {
1903
840M
            x_cm = quant_band(&ctx, X, N, b, B,
1904
840M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1905
840M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1906
840M
         }
1907
1.06G
         y_cm = x_cm;
1908
1.06G
      }
1909
1.07G
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1910
1.07G
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1911
1.07G
      balance += pulses[i] + tell;
1912
1913
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1914
1.07G
      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
1.07G
      ctx.avoid_split_noise = 0;
1918
1.07G
   }
1919
72.0M
   *seed = ctx.seed;
1920
1921
72.0M
   RESTORE_STACK;
1922
72.0M
}
quant_all_bands
Line
Count
Source
1597
54.3M
{
1598
54.3M
   int i;
1599
54.3M
   opus_int32 remaining_bits;
1600
54.3M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
54.3M
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
54.3M
   VARDECL(celt_norm, _norm);
1603
54.3M
   VARDECL(celt_norm, _lowband_scratch);
1604
54.3M
   VARDECL(celt_norm, X_save);
1605
54.3M
   VARDECL(celt_norm, Y_save);
1606
54.3M
   VARDECL(celt_norm, X_save2);
1607
54.3M
   VARDECL(celt_norm, Y_save2);
1608
54.3M
   VARDECL(celt_norm, norm_save2);
1609
54.3M
   VARDECL(unsigned char, bytes_save);
1610
54.3M
   int resynth_alloc;
1611
54.3M
   celt_norm *lowband_scratch;
1612
54.3M
   int B;
1613
54.3M
   int M;
1614
54.3M
   int lowband_offset;
1615
54.3M
   int update_lowband = 1;
1616
54.3M
   int C = Y_ != NULL ? 2 : 1;
1617
54.3M
   int norm_offset;
1618
54.3M
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1619
#ifdef RESYNTH
1620
   int resynth = 1;
1621
#else
1622
54.3M
   int resynth = !encode || theta_rdo;
1623
54.3M
#endif
1624
54.3M
   struct band_ctx ctx;
1625
54.3M
#ifdef ENABLE_QEXT
1626
54.3M
   int ext_b;
1627
54.3M
   opus_int32 ext_balance=0;
1628
54.3M
   opus_int32 ext_tell=0;
1629
54.3M
   VARDECL(unsigned char, ext_bytes_save);
1630
54.3M
#endif
1631
54.3M
   SAVE_STACK;
1632
1633
54.3M
   M = 1<<LM;
1634
54.3M
   B = shortBlocks ? M : 1;
1635
54.3M
   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
54.3M
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1639
54.3M
   norm = _norm;
1640
54.3M
   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
54.3M
   if (encode && resynth)
1646
2.62M
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1647
51.7M
   else
1648
51.7M
      resynth_alloc = ALLOC_NONE;
1649
54.3M
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1650
54.3M
   if (encode && resynth)
1651
2.62M
      lowband_scratch = _lowband_scratch;
1652
51.7M
   else
1653
51.7M
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1654
54.3M
   ALLOC(X_save, resynth_alloc, celt_norm);
1655
54.3M
   ALLOC(Y_save, resynth_alloc, celt_norm);
1656
54.3M
   ALLOC(X_save2, resynth_alloc, celt_norm);
1657
54.3M
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1658
54.3M
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1659
1660
54.3M
   lowband_offset = 0;
1661
54.3M
   ctx.bandE = bandE;
1662
54.3M
   ctx.ec = ec;
1663
54.3M
   ctx.encode = encode;
1664
54.3M
   ctx.intensity = intensity;
1665
54.3M
   ctx.m = m;
1666
54.3M
   ctx.seed = *seed;
1667
54.3M
   ctx.spread = spread;
1668
54.3M
   ctx.arch = arch;
1669
54.3M
   ctx.disable_inv = disable_inv;
1670
54.3M
   ctx.resynth = resynth;
1671
54.3M
   ctx.theta_round = 0;
1672
54.3M
#ifdef ENABLE_QEXT
1673
54.3M
   ctx.ext_ec = ext_ec;
1674
54.3M
   ctx.ext_total_bits = ext_total_bits;
1675
54.3M
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1676
54.3M
   if (ctx.extra_bands) theta_rdo = 0;
1677
54.3M
   ALLOC(ext_bytes_save, theta_rdo ? QEXT_PACKET_SIZE_CAP : ALLOC_NONE, unsigned char);
1678
54.3M
#endif
1679
54.3M
   ALLOC(bytes_save, theta_rdo ? 1275 : ALLOC_NONE, unsigned char);
1680
1681
   /* Avoid injecting noise in the first band on transients. */
1682
54.3M
   ctx.avoid_split_noise = B > 1;
1683
850M
   for (i=start;i<end;i++)
1684
796M
   {
1685
796M
      opus_int32 tell;
1686
796M
      int b;
1687
796M
      int N;
1688
796M
      opus_int32 curr_balance;
1689
796M
      int effective_lowband=-1;
1690
796M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1691
796M
      int tf_change=0;
1692
796M
      unsigned x_cm;
1693
796M
      unsigned y_cm;
1694
796M
      int last;
1695
1696
796M
      ctx.i = i;
1697
796M
      last = (i==end-1);
1698
1699
796M
      X = X_+M*eBands[i];
1700
796M
      if (Y_!=NULL)
1701
141M
         Y = Y_+M*eBands[i];
1702
655M
      else
1703
655M
         Y = NULL;
1704
796M
      N = M*eBands[i+1]-M*eBands[i];
1705
796M
      celt_assert(N > 0);
1706
796M
      tell = ec_tell_frac(ec);
1707
1708
      /* Compute how many bits we want to allocate to this band */
1709
796M
      if (i != start)
1710
742M
         balance -= tell;
1711
796M
      remaining_bits = total_bits-tell-1;
1712
796M
      ctx.remaining_bits = remaining_bits;
1713
796M
#ifdef ENABLE_QEXT
1714
796M
      if (i != start) {
1715
742M
         ext_balance += extra_pulses[i-1] + ext_tell;
1716
742M
      }
1717
796M
      ext_tell = ec_tell_frac(ext_ec);
1718
796M
      ctx.extra_bits = extra_pulses[i];
1719
796M
      if (i != start)
1720
742M
         ext_balance -= ext_tell;
1721
796M
      if (i <= codedBands-1)
1722
123M
      {
1723
123M
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1724
123M
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1725
673M
      } else {
1726
673M
         ext_b = 0;
1727
673M
      }
1728
796M
#endif
1729
796M
      if (i <= codedBands-1)
1730
123M
      {
1731
123M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1732
123M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1733
673M
      } else {
1734
673M
         b = 0;
1735
673M
      }
1736
1737
796M
#ifndef DISABLE_UPDATE_DRAFT
1738
796M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1739
3.67M
            lowband_offset = i;
1740
796M
      if (i == start+1)
1741
54.3M
         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
796M
      tf_change = tf_res[i];
1748
796M
      ctx.tf_change = tf_change;
1749
796M
      if (i>=m->effEBands)
1750
22.3k
      {
1751
22.3k
         X=norm;
1752
22.3k
         if (Y_!=NULL)
1753
16.9k
            Y = norm;
1754
22.3k
         lowband_scratch = NULL;
1755
22.3k
      }
1756
796M
      if (last && !theta_rdo)
1757
51.7M
         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
796M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1762
38.5M
      {
1763
38.5M
         int fold_start;
1764
38.5M
         int fold_end;
1765
38.5M
         int fold_i;
1766
         /* This ensures we never repeat spectral content within one band */
1767
38.5M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1768
38.5M
         fold_start = lowband_offset;
1769
38.9M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1770
38.5M
         fold_end = lowband_offset-1;
1771
38.5M
#ifndef DISABLE_UPDATE_DRAFT
1772
82.5M
         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
38.5M
         x_cm = y_cm = 0;
1777
82.9M
         fold_i = fold_start; do {
1778
82.9M
           x_cm |= collapse_masks[fold_i*C+0];
1779
82.9M
           y_cm |= collapse_masks[fold_i*C+C-1];
1780
82.9M
         } while (++fold_i<fold_end);
1781
38.5M
      }
1782
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1783
         always) be non-zero. */
1784
757M
      else
1785
757M
         x_cm = y_cm = (1<<B)-1;
1786
1787
796M
      if (dual_stereo && i==intensity)
1788
49.1k
      {
1789
49.1k
         int j;
1790
1791
         /* Switch off dual stereo to do intensity. */
1792
49.1k
         dual_stereo = 0;
1793
49.1k
         if (resynth)
1794
369k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1795
360k
               norm[j] = HALF32(norm[j]+norm2[j]);
1796
49.1k
      }
1797
796M
      if (dual_stereo)
1798
568k
      {
1799
568k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1800
568k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1801
568k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1802
568k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1803
568k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1804
568k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1805
795M
      } else {
1806
795M
         if (Y!=NULL)
1807
140M
         {
1808
140M
            if (theta_rdo && i < intensity)
1809
394k
            {
1810
394k
               ec_ctx ec_save, ec_save2;
1811
394k
               struct band_ctx ctx_save, ctx_save2;
1812
394k
               opus_val32 dist0, dist1;
1813
394k
               unsigned cm, cm2;
1814
394k
               int nstart_bytes, nend_bytes, save_bytes;
1815
394k
               unsigned char *bytes_buf;
1816
394k
#ifdef ENABLE_QEXT
1817
394k
               ec_ctx ext_ec_save, ext_ec_save2;
1818
394k
               unsigned char *ext_bytes_buf;
1819
394k
               int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes;
1820
394k
#endif
1821
394k
               opus_val16 w[2];
1822
394k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1823
               /* Make a copy. */
1824
394k
               cm = x_cm|y_cm;
1825
394k
               ec_save = *ec;
1826
394k
#ifdef ENABLE_QEXT
1827
394k
               ext_ec_save = *ext_ec;
1828
394k
#endif
1829
394k
               ctx_save = ctx;
1830
394k
               OPUS_COPY(X_save, X, N);
1831
394k
               OPUS_COPY(Y_save, Y, N);
1832
               /* Encode and round down. */
1833
394k
               ctx.theta_round = -1;
1834
394k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1835
394k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1836
394k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1837
394k
               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
394k
               cm2 = x_cm;
1841
394k
               ec_save2 = *ec;
1842
394k
#ifdef ENABLE_QEXT
1843
394k
               ext_ec_save2 = *ext_ec;
1844
394k
#endif
1845
394k
               ctx_save2 = ctx;
1846
394k
               OPUS_COPY(X_save2, X, N);
1847
394k
               OPUS_COPY(Y_save2, Y, N);
1848
394k
               if (!last)
1849
387k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1850
394k
               nstart_bytes = ec_save.offs;
1851
394k
               nend_bytes = ec_save.storage;
1852
394k
               bytes_buf = ec_save.buf+nstart_bytes;
1853
394k
               save_bytes = nend_bytes-nstart_bytes;
1854
394k
               OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1855
394k
#ifdef ENABLE_QEXT
1856
394k
               ext_nstart_bytes = ext_ec_save.offs;
1857
394k
               ext_nend_bytes = ext_ec_save.storage;
1858
394k
               ext_bytes_buf = ext_ec_save.buf!=NULL ? ext_ec_save.buf+ext_nstart_bytes : NULL;
1859
394k
               ext_save_bytes = ext_nend_bytes-ext_nstart_bytes;
1860
394k
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1861
394k
#endif
1862
               /* Restore */
1863
394k
               *ec = ec_save;
1864
394k
#ifdef ENABLE_QEXT
1865
394k
               *ext_ec = ext_ec_save;
1866
394k
#endif
1867
394k
               ctx = ctx_save;
1868
394k
               OPUS_COPY(X, X_save, N);
1869
394k
               OPUS_COPY(Y, Y_save, N);
1870
394k
#ifndef DISABLE_UPDATE_DRAFT
1871
394k
               if (i == start+1)
1872
33.9k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1873
394k
#endif
1874
               /* Encode and round up. */
1875
394k
               ctx.theta_round = 1;
1876
394k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1877
394k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1878
394k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1879
394k
               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
394k
               if (dist0 >= dist1) {
1881
254k
                  x_cm = cm2;
1882
254k
                  *ec = ec_save2;
1883
254k
#ifdef ENABLE_QEXT
1884
254k
                  *ext_ec = ext_ec_save2;
1885
254k
#endif
1886
254k
                  ctx = ctx_save2;
1887
254k
                  OPUS_COPY(X, X_save2, N);
1888
254k
                  OPUS_COPY(Y, Y_save2, N);
1889
254k
                  if (!last)
1890
251k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1891
254k
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1892
254k
#ifdef ENABLE_QEXT
1893
254k
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1894
254k
#endif
1895
254k
               }
1896
140M
            } else {
1897
140M
               ctx.theta_round = 0;
1898
140M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1899
140M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1900
140M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1901
140M
            }
1902
655M
         } else {
1903
655M
            x_cm = quant_band(&ctx, X, N, b, B,
1904
655M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1905
655M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1906
655M
         }
1907
795M
         y_cm = x_cm;
1908
795M
      }
1909
796M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1910
796M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1911
796M
      balance += pulses[i] + tell;
1912
1913
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1914
796M
      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
796M
      ctx.avoid_split_noise = 0;
1918
796M
   }
1919
54.3M
   *seed = ctx.seed;
1920
1921
54.3M
   RESTORE_STACK;
1922
54.3M
}
quant_all_bands
Line
Count
Source
1597
54.3M
{
1598
54.3M
   int i;
1599
54.3M
   opus_int32 remaining_bits;
1600
54.3M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
54.3M
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
54.3M
   VARDECL(celt_norm, _norm);
1603
54.3M
   VARDECL(celt_norm, _lowband_scratch);
1604
54.3M
   VARDECL(celt_norm, X_save);
1605
54.3M
   VARDECL(celt_norm, Y_save);
1606
54.3M
   VARDECL(celt_norm, X_save2);
1607
54.3M
   VARDECL(celt_norm, Y_save2);
1608
54.3M
   VARDECL(celt_norm, norm_save2);
1609
54.3M
   VARDECL(unsigned char, bytes_save);
1610
54.3M
   int resynth_alloc;
1611
54.3M
   celt_norm *lowband_scratch;
1612
54.3M
   int B;
1613
54.3M
   int M;
1614
54.3M
   int lowband_offset;
1615
54.3M
   int update_lowband = 1;
1616
54.3M
   int C = Y_ != NULL ? 2 : 1;
1617
54.3M
   int norm_offset;
1618
54.3M
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1619
#ifdef RESYNTH
1620
   int resynth = 1;
1621
#else
1622
54.3M
   int resynth = !encode || theta_rdo;
1623
54.3M
#endif
1624
54.3M
   struct band_ctx ctx;
1625
54.3M
#ifdef ENABLE_QEXT
1626
54.3M
   int ext_b;
1627
54.3M
   opus_int32 ext_balance=0;
1628
54.3M
   opus_int32 ext_tell=0;
1629
54.3M
   VARDECL(unsigned char, ext_bytes_save);
1630
54.3M
#endif
1631
54.3M
   SAVE_STACK;
1632
1633
54.3M
   M = 1<<LM;
1634
54.3M
   B = shortBlocks ? M : 1;
1635
54.3M
   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
54.3M
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1639
54.3M
   norm = _norm;
1640
54.3M
   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
54.3M
   if (encode && resynth)
1646
2.62M
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1647
51.7M
   else
1648
51.7M
      resynth_alloc = ALLOC_NONE;
1649
54.3M
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1650
54.3M
   if (encode && resynth)
1651
2.62M
      lowband_scratch = _lowband_scratch;
1652
51.7M
   else
1653
51.7M
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1654
54.3M
   ALLOC(X_save, resynth_alloc, celt_norm);
1655
54.3M
   ALLOC(Y_save, resynth_alloc, celt_norm);
1656
54.3M
   ALLOC(X_save2, resynth_alloc, celt_norm);
1657
54.3M
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1658
54.3M
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1659
1660
54.3M
   lowband_offset = 0;
1661
54.3M
   ctx.bandE = bandE;
1662
54.3M
   ctx.ec = ec;
1663
54.3M
   ctx.encode = encode;
1664
54.3M
   ctx.intensity = intensity;
1665
54.3M
   ctx.m = m;
1666
54.3M
   ctx.seed = *seed;
1667
54.3M
   ctx.spread = spread;
1668
54.3M
   ctx.arch = arch;
1669
54.3M
   ctx.disable_inv = disable_inv;
1670
54.3M
   ctx.resynth = resynth;
1671
54.3M
   ctx.theta_round = 0;
1672
54.3M
#ifdef ENABLE_QEXT
1673
54.3M
   ctx.ext_ec = ext_ec;
1674
54.3M
   ctx.ext_total_bits = ext_total_bits;
1675
54.3M
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1676
54.3M
   if (ctx.extra_bands) theta_rdo = 0;
1677
54.3M
   ALLOC(ext_bytes_save, theta_rdo ? QEXT_PACKET_SIZE_CAP : ALLOC_NONE, unsigned char);
1678
54.3M
#endif
1679
54.3M
   ALLOC(bytes_save, theta_rdo ? 1275 : ALLOC_NONE, unsigned char);
1680
1681
   /* Avoid injecting noise in the first band on transients. */
1682
54.3M
   ctx.avoid_split_noise = B > 1;
1683
850M
   for (i=start;i<end;i++)
1684
796M
   {
1685
796M
      opus_int32 tell;
1686
796M
      int b;
1687
796M
      int N;
1688
796M
      opus_int32 curr_balance;
1689
796M
      int effective_lowband=-1;
1690
796M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1691
796M
      int tf_change=0;
1692
796M
      unsigned x_cm;
1693
796M
      unsigned y_cm;
1694
796M
      int last;
1695
1696
796M
      ctx.i = i;
1697
796M
      last = (i==end-1);
1698
1699
796M
      X = X_+M*eBands[i];
1700
796M
      if (Y_!=NULL)
1701
141M
         Y = Y_+M*eBands[i];
1702
655M
      else
1703
655M
         Y = NULL;
1704
796M
      N = M*eBands[i+1]-M*eBands[i];
1705
796M
      celt_assert(N > 0);
1706
796M
      tell = ec_tell_frac(ec);
1707
1708
      /* Compute how many bits we want to allocate to this band */
1709
796M
      if (i != start)
1710
742M
         balance -= tell;
1711
796M
      remaining_bits = total_bits-tell-1;
1712
796M
      ctx.remaining_bits = remaining_bits;
1713
796M
#ifdef ENABLE_QEXT
1714
796M
      if (i != start) {
1715
742M
         ext_balance += extra_pulses[i-1] + ext_tell;
1716
742M
      }
1717
796M
      ext_tell = ec_tell_frac(ext_ec);
1718
796M
      ctx.extra_bits = extra_pulses[i];
1719
796M
      if (i != start)
1720
742M
         ext_balance -= ext_tell;
1721
796M
      if (i <= codedBands-1)
1722
123M
      {
1723
123M
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1724
123M
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1725
673M
      } else {
1726
673M
         ext_b = 0;
1727
673M
      }
1728
796M
#endif
1729
796M
      if (i <= codedBands-1)
1730
123M
      {
1731
123M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1732
123M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1733
673M
      } else {
1734
673M
         b = 0;
1735
673M
      }
1736
1737
796M
#ifndef DISABLE_UPDATE_DRAFT
1738
796M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1739
3.67M
            lowband_offset = i;
1740
796M
      if (i == start+1)
1741
54.3M
         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
796M
      tf_change = tf_res[i];
1748
796M
      ctx.tf_change = tf_change;
1749
796M
      if (i>=m->effEBands)
1750
22.3k
      {
1751
22.3k
         X=norm;
1752
22.3k
         if (Y_!=NULL)
1753
16.9k
            Y = norm;
1754
22.3k
         lowband_scratch = NULL;
1755
22.3k
      }
1756
796M
      if (last && !theta_rdo)
1757
51.7M
         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
796M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1762
38.5M
      {
1763
38.5M
         int fold_start;
1764
38.5M
         int fold_end;
1765
38.5M
         int fold_i;
1766
         /* This ensures we never repeat spectral content within one band */
1767
38.5M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1768
38.5M
         fold_start = lowband_offset;
1769
38.9M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1770
38.5M
         fold_end = lowband_offset-1;
1771
38.5M
#ifndef DISABLE_UPDATE_DRAFT
1772
82.5M
         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
38.5M
         x_cm = y_cm = 0;
1777
82.9M
         fold_i = fold_start; do {
1778
82.9M
           x_cm |= collapse_masks[fold_i*C+0];
1779
82.9M
           y_cm |= collapse_masks[fold_i*C+C-1];
1780
82.9M
         } while (++fold_i<fold_end);
1781
38.5M
      }
1782
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1783
         always) be non-zero. */
1784
757M
      else
1785
757M
         x_cm = y_cm = (1<<B)-1;
1786
1787
796M
      if (dual_stereo && i==intensity)
1788
49.1k
      {
1789
49.1k
         int j;
1790
1791
         /* Switch off dual stereo to do intensity. */
1792
49.1k
         dual_stereo = 0;
1793
49.1k
         if (resynth)
1794
369k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1795
360k
               norm[j] = HALF32(norm[j]+norm2[j]);
1796
49.1k
      }
1797
796M
      if (dual_stereo)
1798
568k
      {
1799
568k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1800
568k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1801
568k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1802
568k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1803
568k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1804
568k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1805
795M
      } else {
1806
795M
         if (Y!=NULL)
1807
140M
         {
1808
140M
            if (theta_rdo && i < intensity)
1809
394k
            {
1810
394k
               ec_ctx ec_save, ec_save2;
1811
394k
               struct band_ctx ctx_save, ctx_save2;
1812
394k
               opus_val32 dist0, dist1;
1813
394k
               unsigned cm, cm2;
1814
394k
               int nstart_bytes, nend_bytes, save_bytes;
1815
394k
               unsigned char *bytes_buf;
1816
394k
#ifdef ENABLE_QEXT
1817
394k
               ec_ctx ext_ec_save, ext_ec_save2;
1818
394k
               unsigned char *ext_bytes_buf;
1819
394k
               int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes;
1820
394k
#endif
1821
394k
               opus_val16 w[2];
1822
394k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1823
               /* Make a copy. */
1824
394k
               cm = x_cm|y_cm;
1825
394k
               ec_save = *ec;
1826
394k
#ifdef ENABLE_QEXT
1827
394k
               ext_ec_save = *ext_ec;
1828
394k
#endif
1829
394k
               ctx_save = ctx;
1830
394k
               OPUS_COPY(X_save, X, N);
1831
394k
               OPUS_COPY(Y_save, Y, N);
1832
               /* Encode and round down. */
1833
394k
               ctx.theta_round = -1;
1834
394k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1835
394k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1836
394k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1837
394k
               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
394k
               cm2 = x_cm;
1841
394k
               ec_save2 = *ec;
1842
394k
#ifdef ENABLE_QEXT
1843
394k
               ext_ec_save2 = *ext_ec;
1844
394k
#endif
1845
394k
               ctx_save2 = ctx;
1846
394k
               OPUS_COPY(X_save2, X, N);
1847
394k
               OPUS_COPY(Y_save2, Y, N);
1848
394k
               if (!last)
1849
387k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1850
394k
               nstart_bytes = ec_save.offs;
1851
394k
               nend_bytes = ec_save.storage;
1852
394k
               bytes_buf = ec_save.buf+nstart_bytes;
1853
394k
               save_bytes = nend_bytes-nstart_bytes;
1854
394k
               OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1855
394k
#ifdef ENABLE_QEXT
1856
394k
               ext_nstart_bytes = ext_ec_save.offs;
1857
394k
               ext_nend_bytes = ext_ec_save.storage;
1858
394k
               ext_bytes_buf = ext_ec_save.buf!=NULL ? ext_ec_save.buf+ext_nstart_bytes : NULL;
1859
394k
               ext_save_bytes = ext_nend_bytes-ext_nstart_bytes;
1860
394k
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1861
394k
#endif
1862
               /* Restore */
1863
394k
               *ec = ec_save;
1864
394k
#ifdef ENABLE_QEXT
1865
394k
               *ext_ec = ext_ec_save;
1866
394k
#endif
1867
394k
               ctx = ctx_save;
1868
394k
               OPUS_COPY(X, X_save, N);
1869
394k
               OPUS_COPY(Y, Y_save, N);
1870
394k
#ifndef DISABLE_UPDATE_DRAFT
1871
394k
               if (i == start+1)
1872
33.9k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1873
394k
#endif
1874
               /* Encode and round up. */
1875
394k
               ctx.theta_round = 1;
1876
394k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1877
394k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1878
394k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1879
394k
               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
394k
               if (dist0 >= dist1) {
1881
254k
                  x_cm = cm2;
1882
254k
                  *ec = ec_save2;
1883
254k
#ifdef ENABLE_QEXT
1884
254k
                  *ext_ec = ext_ec_save2;
1885
254k
#endif
1886
254k
                  ctx = ctx_save2;
1887
254k
                  OPUS_COPY(X, X_save2, N);
1888
254k
                  OPUS_COPY(Y, Y_save2, N);
1889
254k
                  if (!last)
1890
251k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1891
254k
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1892
254k
#ifdef ENABLE_QEXT
1893
254k
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1894
254k
#endif
1895
254k
               }
1896
140M
            } else {
1897
140M
               ctx.theta_round = 0;
1898
140M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1899
140M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1900
140M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1901
140M
            }
1902
655M
         } else {
1903
655M
            x_cm = quant_band(&ctx, X, N, b, B,
1904
655M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1905
655M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1906
655M
         }
1907
795M
         y_cm = x_cm;
1908
795M
      }
1909
796M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1910
796M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1911
796M
      balance += pulses[i] + tell;
1912
1913
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1914
796M
      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
796M
      ctx.avoid_split_noise = 0;
1918
796M
   }
1919
54.3M
   *seed = ctx.seed;
1920
1921
54.3M
   RESTORE_STACK;
1922
54.3M
}
quant_all_bands
Line
Count
Source
1597
72.0M
{
1598
72.0M
   int i;
1599
72.0M
   opus_int32 remaining_bits;
1600
72.0M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
72.0M
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
72.0M
   VARDECL(celt_norm, _norm);
1603
72.0M
   VARDECL(celt_norm, _lowband_scratch);
1604
72.0M
   VARDECL(celt_norm, X_save);
1605
72.0M
   VARDECL(celt_norm, Y_save);
1606
72.0M
   VARDECL(celt_norm, X_save2);
1607
72.0M
   VARDECL(celt_norm, Y_save2);
1608
72.0M
   VARDECL(celt_norm, norm_save2);
1609
72.0M
   VARDECL(unsigned char, bytes_save);
1610
72.0M
   int resynth_alloc;
1611
72.0M
   celt_norm *lowband_scratch;
1612
72.0M
   int B;
1613
72.0M
   int M;
1614
72.0M
   int lowband_offset;
1615
72.0M
   int update_lowband = 1;
1616
72.0M
   int C = Y_ != NULL ? 2 : 1;
1617
72.0M
   int norm_offset;
1618
72.0M
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1619
#ifdef RESYNTH
1620
   int resynth = 1;
1621
#else
1622
72.0M
   int resynth = !encode || theta_rdo;
1623
72.0M
#endif
1624
72.0M
   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
72.0M
   SAVE_STACK;
1632
1633
72.0M
   M = 1<<LM;
1634
72.0M
   B = shortBlocks ? M : 1;
1635
72.0M
   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
72.0M
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1639
72.0M
   norm = _norm;
1640
72.0M
   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
72.0M
   if (encode && resynth)
1646
4.68M
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1647
67.3M
   else
1648
67.3M
      resynth_alloc = ALLOC_NONE;
1649
72.0M
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1650
72.0M
   if (encode && resynth)
1651
4.68M
      lowband_scratch = _lowband_scratch;
1652
67.3M
   else
1653
67.3M
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1654
72.0M
   ALLOC(X_save, resynth_alloc, celt_norm);
1655
72.0M
   ALLOC(Y_save, resynth_alloc, celt_norm);
1656
72.0M
   ALLOC(X_save2, resynth_alloc, celt_norm);
1657
72.0M
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1658
72.0M
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1659
1660
72.0M
   lowband_offset = 0;
1661
72.0M
   ctx.bandE = bandE;
1662
72.0M
   ctx.ec = ec;
1663
72.0M
   ctx.encode = encode;
1664
72.0M
   ctx.intensity = intensity;
1665
72.0M
   ctx.m = m;
1666
72.0M
   ctx.seed = *seed;
1667
72.0M
   ctx.spread = spread;
1668
72.0M
   ctx.arch = arch;
1669
72.0M
   ctx.disable_inv = disable_inv;
1670
72.0M
   ctx.resynth = resynth;
1671
72.0M
   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
72.0M
   ALLOC(bytes_save, theta_rdo ? 1275 : ALLOC_NONE, unsigned char);
1680
1681
   /* Avoid injecting noise in the first band on transients. */
1682
72.0M
   ctx.avoid_split_noise = B > 1;
1683
1.14G
   for (i=start;i<end;i++)
1684
1.07G
   {
1685
1.07G
      opus_int32 tell;
1686
1.07G
      int b;
1687
1.07G
      int N;
1688
1.07G
      opus_int32 curr_balance;
1689
1.07G
      int effective_lowband=-1;
1690
1.07G
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1691
1.07G
      int tf_change=0;
1692
1.07G
      unsigned x_cm;
1693
1.07G
      unsigned y_cm;
1694
1.07G
      int last;
1695
1696
1.07G
      ctx.i = i;
1697
1.07G
      last = (i==end-1);
1698
1699
1.07G
      X = X_+M*eBands[i];
1700
1.07G
      if (Y_!=NULL)
1701
233M
         Y = Y_+M*eBands[i];
1702
840M
      else
1703
840M
         Y = NULL;
1704
1.07G
      N = M*eBands[i+1]-M*eBands[i];
1705
1.07G
      celt_assert(N > 0);
1706
1.07G
      tell = ec_tell_frac(ec);
1707
1708
      /* Compute how many bits we want to allocate to this band */
1709
1.07G
      if (i != start)
1710
1.00G
         balance -= tell;
1711
1.07G
      remaining_bits = total_bits-tell-1;
1712
1.07G
      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
1.07G
      if (i <= codedBands-1)
1730
287M
      {
1731
287M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1732
287M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1733
786M
      } else {
1734
786M
         b = 0;
1735
786M
      }
1736
1737
1.07G
#ifndef DISABLE_UPDATE_DRAFT
1738
1.07G
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1739
11.6M
            lowband_offset = i;
1740
1.07G
      if (i == start+1)
1741
72.0M
         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
1.07G
      tf_change = tf_res[i];
1748
1.07G
      ctx.tf_change = tf_change;
1749
1.07G
      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
1.07G
      if (last && !theta_rdo)
1757
67.3M
         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
1.07G
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1762
68.5M
      {
1763
68.5M
         int fold_start;
1764
68.5M
         int fold_end;
1765
68.5M
         int fold_i;
1766
         /* This ensures we never repeat spectral content within one band */
1767
68.5M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1768
68.5M
         fold_start = lowband_offset;
1769
72.3M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1770
68.5M
         fold_end = lowband_offset-1;
1771
68.5M
#ifndef DISABLE_UPDATE_DRAFT
1772
147M
         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
68.5M
         x_cm = y_cm = 0;
1777
151M
         fold_i = fold_start; do {
1778
151M
           x_cm |= collapse_masks[fold_i*C+0];
1779
151M
           y_cm |= collapse_masks[fold_i*C+C-1];
1780
151M
         } while (++fold_i<fold_end);
1781
68.5M
      }
1782
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1783
         always) be non-zero. */
1784
1.00G
      else
1785
1.00G
         x_cm = y_cm = (1<<B)-1;
1786
1787
1.07G
      if (dual_stereo && i==intensity)
1788
308k
      {
1789
308k
         int j;
1790
1791
         /* Switch off dual stereo to do intensity. */
1792
308k
         dual_stereo = 0;
1793
308k
         if (resynth)
1794
222k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1795
216k
               norm[j] = HALF32(norm[j]+norm2[j]);
1796
308k
      }
1797
1.07G
      if (dual_stereo)
1798
4.80M
      {
1799
4.80M
         x_cm = quant_band(&ctx, X, N, b/2, B,
1800
4.80M
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1801
4.80M
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1802
4.80M
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1803
4.80M
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1804
4.80M
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1805
1.06G
      } else {
1806
1.06G
         if (Y!=NULL)
1807
228M
         {
1808
228M
            if (theta_rdo && i < intensity)
1809
6.77M
            {
1810
6.77M
               ec_ctx ec_save, ec_save2;
1811
6.77M
               struct band_ctx ctx_save, ctx_save2;
1812
6.77M
               opus_val32 dist0, dist1;
1813
6.77M
               unsigned cm, cm2;
1814
6.77M
               int nstart_bytes, nend_bytes, save_bytes;
1815
6.77M
               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
6.77M
               opus_val16 w[2];
1822
6.77M
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1823
               /* Make a copy. */
1824
6.77M
               cm = x_cm|y_cm;
1825
6.77M
               ec_save = *ec;
1826
#ifdef ENABLE_QEXT
1827
               ext_ec_save = *ext_ec;
1828
#endif
1829
6.77M
               ctx_save = ctx;
1830
6.77M
               OPUS_COPY(X_save, X, N);
1831
6.77M
               OPUS_COPY(Y_save, Y, N);
1832
               /* Encode and round down. */
1833
6.77M
               ctx.theta_round = -1;
1834
6.77M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1835
6.77M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1836
6.77M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1837
6.77M
               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
6.77M
               cm2 = x_cm;
1841
6.77M
               ec_save2 = *ec;
1842
#ifdef ENABLE_QEXT
1843
               ext_ec_save2 = *ext_ec;
1844
#endif
1845
6.77M
               ctx_save2 = ctx;
1846
6.77M
               OPUS_COPY(X_save2, X, N);
1847
6.77M
               OPUS_COPY(Y_save2, Y, N);
1848
6.77M
               if (!last)
1849
6.73M
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1850
6.77M
               nstart_bytes = ec_save.offs;
1851
6.77M
               nend_bytes = ec_save.storage;
1852
6.77M
               bytes_buf = ec_save.buf+nstart_bytes;
1853
6.77M
               save_bytes = nend_bytes-nstart_bytes;
1854
6.77M
               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
6.77M
               *ec = ec_save;
1864
#ifdef ENABLE_QEXT
1865
               *ext_ec = ext_ec_save;
1866
#endif
1867
6.77M
               ctx = ctx_save;
1868
6.77M
               OPUS_COPY(X, X_save, N);
1869
6.77M
               OPUS_COPY(Y, Y_save, N);
1870
6.77M
#ifndef DISABLE_UPDATE_DRAFT
1871
6.77M
               if (i == start+1)
1872
671k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1873
6.77M
#endif
1874
               /* Encode and round up. */
1875
6.77M
               ctx.theta_round = 1;
1876
6.77M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1877
6.77M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1878
6.77M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1879
6.77M
               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
6.77M
               if (dist0 >= dist1) {
1881
6.41M
                  x_cm = cm2;
1882
6.41M
                  *ec = ec_save2;
1883
#ifdef ENABLE_QEXT
1884
                  *ext_ec = ext_ec_save2;
1885
#endif
1886
6.41M
                  ctx = ctx_save2;
1887
6.41M
                  OPUS_COPY(X, X_save2, N);
1888
6.41M
                  OPUS_COPY(Y, Y_save2, N);
1889
6.41M
                  if (!last)
1890
6.37M
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1891
6.41M
                  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
6.41M
               }
1896
221M
            } else {
1897
221M
               ctx.theta_round = 0;
1898
221M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1899
221M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1900
221M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1901
221M
            }
1902
840M
         } else {
1903
840M
            x_cm = quant_band(&ctx, X, N, b, B,
1904
840M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1905
840M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1906
840M
         }
1907
1.06G
         y_cm = x_cm;
1908
1.06G
      }
1909
1.07G
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1910
1.07G
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1911
1.07G
      balance += pulses[i] + tell;
1912
1913
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1914
1.07G
      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
1.07G
      ctx.avoid_split_noise = 0;
1918
1.07G
   }
1919
72.0M
   *seed = ctx.seed;
1920
1921
72.0M
   RESTORE_STACK;
1922
72.0M
}