Coverage Report

Created: 2026-05-30 07:48

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
9.53M
{
48
9.53M
   int i;
49
107M
   for (i=0;i<N;i++)
50
107M
   {
51
107M
      if (val < thresholds[i])
52
9.52M
         break;
53
107M
   }
54
9.53M
   if (i>prev && val < thresholds[prev]+hysteresis[prev])
55
48.2k
      i=prev;
56
9.53M
   if (i<prev && val > thresholds[prev-1]-hysteresis[prev-1])
57
684
      i=prev;
58
9.53M
   return i;
59
9.53M
}
60
61
opus_uint32 celt_lcg_rand(opus_uint32 seed)
62
428M
{
63
428M
   return 1664525 * seed + 1013904223;
64
428M
}
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
32.6M
{
70
32.6M
   opus_int32 tmp;
71
32.6M
   opus_int16 x2;
72
32.6M
   tmp = (4096+((opus_int32)(x)*(x)))>>13;
73
32.6M
   celt_sig_assert(tmp<=32767);
74
32.6M
   x2 = tmp;
75
32.6M
   x2 = (32767-x2) + FRAC_MUL16(x2, (-7651 + FRAC_MUL16(x2, (8277 + FRAC_MUL16(-626, x2)))));
76
32.6M
   celt_sig_assert(x2<=32766);
77
32.6M
   return 1+x2;
78
32.6M
}
79
80
int bitexact_log2tan(int isin,int icos)
81
16.3M
{
82
16.3M
   int lc;
83
16.3M
   int ls;
84
16.3M
   lc=EC_ILOG(icos);
85
16.3M
   ls=EC_ILOG(isin);
86
16.3M
   icos<<=15-lc;
87
16.3M
   isin<<=15-ls;
88
16.3M
   return (ls-lc)*(1<<11)
89
16.3M
         +FRAC_MUL16(isin, FRAC_MUL16(isin, -2597) + 7932)
90
16.3M
         -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932);
91
16.3M
}
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
66.8M
{
97
66.8M
   int i, c, N;
98
66.8M
   const opus_int16 *eBands = m->eBands;
99
66.8M
   (void)arch;
100
66.8M
   N = m->shortMdctSize<<LM;
101
76.3M
   c=0; do {
102
1.17G
      for (i=0;i<end;i++)
103
1.09G
      {
104
1.09G
         int j;
105
1.09G
         opus_val32 maxval=0;
106
1.09G
         opus_val32 sum = 0;
107
108
1.09G
         maxval = celt_maxabs32(&X[c*N+(eBands[i]<<LM)], (eBands[i+1]-eBands[i])<<LM);
109
1.09G
         if (maxval > 0)
110
86.9M
         {
111
86.9M
            int shift = IMAX(0, 30 - celt_ilog2(maxval+(maxval>>14)+1) - ((((m->logN[i]+7)>>BITRES)+LM+1)>>1));
112
473M
            j=eBands[i]<<LM; do {
113
473M
               opus_val32 x = SHL32(X[j+c*N],shift);
114
473M
               sum = ADD32(sum, MULT32_32_Q31(x, x));
115
473M
            } while (++j<eBands[i+1]<<LM);
116
86.9M
            bandE[i+c*m->nbEBands] = MAX32(maxval, PSHR32(celt_sqrt32(SHR32(sum,1)), shift));
117
1.00G
         } else {
118
1.00G
            bandE[i+c*m->nbEBands] = EPSILON;
119
1.00G
         }
120
1.09G
      }
121
76.3M
   } while (++c<C);
122
66.8M
}
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
66.6M
{
127
66.6M
   int i, c, N;
128
66.6M
   const opus_int16 *eBands = m->eBands;
129
66.6M
   N = M*m->shortMdctSize;
130
76.1M
   c=0; do {
131
1.09G
      i=0; do {
132
1.09G
         int j,shift;
133
1.09G
         opus_val32 E;
134
1.09G
         opus_val32 g;
135
1.09G
         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
1.09G
         if (E < 10) E += EPSILON;
139
1.09G
         shift = 30-celt_zlog2(E);
140
1.09G
         E = SHL32(E, shift);
141
1.09G
         g = celt_rcp_norm32(E);
142
7.52G
         j=M*eBands[i]; do {
143
7.52G
            X[j+c*N] = PSHR32(MULT32_32_Q31(g, SHL32(freq[j+c*N], shift)), 30-NORM_SHIFT);
144
7.52G
         } while (++j<M*eBands[i+1]);
145
1.09G
      } while (++i<end);
146
76.1M
   } while (++c<C);
147
66.6M
}
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
214k
{
153
214k
   int i, c, N;
154
214k
   const opus_int16 *eBands = m->eBands;
155
214k
   N = m->shortMdctSize<<LM;
156
278k
   c=0; do {
157
4.48M
      for (i=0;i<end;i++)
158
4.20M
      {
159
4.20M
         opus_val32 sum;
160
4.20M
         sum = 1e-27f + celt_inner_prod(&X[c*N+(eBands[i]<<LM)], &X[c*N+(eBands[i]<<LM)], (eBands[i+1]-eBands[i])<<LM, arch);
161
4.20M
         bandE[i+c*m->nbEBands] = celt_sqrt(sum);
162
         /*printf ("%f ", bandE[i+c*m->nbEBands]);*/
163
4.20M
      }
164
278k
   } while (++c<C);
165
   /*printf ("\n");*/
166
214k
}
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
171k
{
171
171k
   int i, c, N;
172
171k
   const opus_int16 *eBands = m->eBands;
173
171k
   N = M*m->shortMdctSize;
174
222k
   c=0; do {
175
3.51M
      for (i=0;i<end;i++)
176
3.29M
      {
177
3.29M
         int j;
178
3.29M
         opus_val16 g = 1.f/(1e-27f+bandE[i+c*m->nbEBands]);
179
36.2M
         for (j=M*eBands[i];j<M*eBands[i+1];j++)
180
32.9M
            X[j+c*N] = freq[j+c*N]*g;
181
3.29M
      }
182
222k
   } while (++c<C);
183
171k
}
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
496k
{
192
496k
   int i, N;
193
496k
   int bound;
194
496k
   celt_sig * OPUS_RESTRICT f;
195
496k
   const celt_norm * OPUS_RESTRICT x;
196
496k
   const opus_int16 *eBands = m->eBands;
197
496k
   N = M*m->shortMdctSize;
198
496k
   bound = M*eBands[end];
199
496k
   if (downsample!=1)
200
319k
      bound = IMIN(bound, N/downsample);
201
496k
   if (silence)
202
65.7k
   {
203
65.7k
      bound = 0;
204
65.7k
      start = end = 0;
205
65.7k
   }
206
496k
   f = freq;
207
496k
   x = X+M*eBands[start];
208
496k
   if (start != 0)
209
71.8k
   {
210
12.9M
      for (i=0;i<M*eBands[start];i++)
211
12.9M
         *f++ = 0;
212
424k
   } else {
213
424k
      f += M*eBands[start];
214
424k
   }
215
6.96M
   for (i=start;i<end;i++)
216
6.47M
   {
217
6.47M
      int j, band_end;
218
6.47M
      opus_val32 g;
219
6.47M
      celt_glog lg;
220
#ifdef FIXED_POINT
221
      int shift;
222
#endif
223
6.47M
      j=M*eBands[i];
224
6.47M
      band_end = M*eBands[i+1];
225
6.47M
      lg = ADD32(bandLogE[i], SHL32((opus_val32)eMeans[i],DB_SHIFT-4));
226
#ifndef FIXED_POINT
227
2.92M
      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.6k
      {
233
62.6k
         shift=0;
234
62.6k
         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
43.8k
      {
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
43.8k
         g = 2147483647;
246
43.8k
         shift = 0;
247
43.8k
      }
248
#endif
249
64.9M
      do {
250
64.9M
         *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift);
251
64.9M
         x++;
252
64.9M
      } while (++j<band_end);
253
6.47M
   }
254
496k
   celt_assert(start <= end);
255
496k
   OPUS_CLEAR(&freq[bound], N-bound);
256
496k
}
denormalise_bands
Line
Count
Source
191
259k
{
192
259k
   int i, N;
193
259k
   int bound;
194
259k
   celt_sig * OPUS_RESTRICT f;
195
259k
   const celt_norm * OPUS_RESTRICT x;
196
259k
   const opus_int16 *eBands = m->eBands;
197
259k
   N = M*m->shortMdctSize;
198
259k
   bound = M*eBands[end];
199
259k
   if (downsample!=1)
200
160k
      bound = IMIN(bound, N/downsample);
201
259k
   if (silence)
202
30.7k
   {
203
30.7k
      bound = 0;
204
30.7k
      start = end = 0;
205
30.7k
   }
206
259k
   f = freq;
207
259k
   x = X+M*eBands[start];
208
259k
   if (start != 0)
209
33.2k
   {
210
5.96M
      for (i=0;i<M*eBands[start];i++)
211
5.93M
         *f++ = 0;
212
226k
   } else {
213
226k
      f += M*eBands[start];
214
226k
   }
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.6k
      {
233
62.6k
         shift=0;
234
62.6k
         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
43.8k
      {
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
43.8k
         g = 2147483647;
246
43.8k
         shift = 0;
247
43.8k
      }
248
3.54M
#endif
249
33.2M
      do {
250
33.2M
         *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift);
251
33.2M
         x++;
252
33.2M
      } while (++j<band_end);
253
3.54M
   }
254
259k
   celt_assert(start <= end);
255
259k
   OPUS_CLEAR(&freq[bound], N-bound);
256
259k
}
denormalise_bands
Line
Count
Source
191
236k
{
192
236k
   int i, N;
193
236k
   int bound;
194
236k
   celt_sig * OPUS_RESTRICT f;
195
236k
   const celt_norm * OPUS_RESTRICT x;
196
236k
   const opus_int16 *eBands = m->eBands;
197
236k
   N = M*m->shortMdctSize;
198
236k
   bound = M*eBands[end];
199
236k
   if (downsample!=1)
200
159k
      bound = IMIN(bound, N/downsample);
201
236k
   if (silence)
202
34.9k
   {
203
34.9k
      bound = 0;
204
34.9k
      start = end = 0;
205
34.9k
   }
206
236k
   f = freq;
207
236k
   x = X+M*eBands[start];
208
236k
   if (start != 0)
209
38.5k
   {
210
7.01M
      for (i=0;i<M*eBands[start];i++)
211
6.97M
         *f++ = 0;
212
197k
   } else {
213
197k
      f += M*eBands[start];
214
197k
   }
215
3.15M
   for (i=start;i<end;i++)
216
2.92M
   {
217
2.92M
      int j, band_end;
218
2.92M
      opus_val32 g;
219
2.92M
      celt_glog lg;
220
#ifdef FIXED_POINT
221
      int shift;
222
#endif
223
2.92M
      j=M*eBands[i];
224
2.92M
      band_end = M*eBands[i+1];
225
2.92M
      lg = ADD32(bandLogE[i], SHL32((opus_val32)eMeans[i],DB_SHIFT-4));
226
2.92M
#ifndef FIXED_POINT
227
2.92M
      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
31.7M
      do {
250
31.7M
         *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift);
251
31.7M
         x++;
252
31.7M
      } while (++j<band_end);
253
2.92M
   }
254
236k
   celt_assert(start <= end);
255
236k
   OPUS_CLEAR(&freq[bound], N-bound);
256
236k
}
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.77k
{
263
6.77k
   int c, i, j, k;
264
115k
   for (i=start;i<end;i++)
265
108k
   {
266
108k
      int N0;
267
108k
      opus_val16 thresh, sqrt_1;
268
108k
      int depth;
269
#ifdef FIXED_POINT
270
      int shift;
271
      opus_val32 thresh32;
272
#endif
273
274
108k
      N0 = m->eBands[i+1]-m->eBands[i];
275
      /* depth in 1/8 bits */
276
108k
      celt_sig_assert(pulses[i]>=0);
277
108k
      depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i]))>>LM;
278
279
#ifdef FIXED_POINT
280
58.0k
      thresh32 = SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1);
281
58.0k
      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
58.0k
         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.8k
      sqrt_1 = celt_rsqrt(N0<<LM);
292
#endif
293
294
108k
      c=0; do
295
188k
      {
296
188k
         celt_norm *X;
297
188k
         celt_glog prev1;
298
188k
         celt_glog prev2;
299
188k
         opus_val32 Ediff;
300
188k
         celt_norm r;
301
188k
         int renormalize=0;
302
188k
         prev1 = prev1logE[c*m->nbEBands+i];
303
188k
         prev2 = prev2logE[c*m->nbEBands+i];
304
188k
         if (!encode && C==1)
305
28.9k
         {
306
28.9k
            prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]);
307
28.9k
            prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]);
308
28.9k
         }
309
188k
         Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2);
310
188k
         Ediff = MAX32(0, Ediff);
311
312
#ifdef FIXED_POINT
313
99.4k
         if (Ediff < GCONST(16.f))
314
15.1k
         {
315
15.1k
            opus_val32 r32 = SHR32(celt_exp2_db(-Ediff),1);
316
15.1k
            r = 2*MIN16(16383,r32);
317
84.3k
         } else {
318
84.3k
            r = 0;
319
84.3k
         }
320
99.4k
         if (LM==3)
321
14.9k
            r = MULT16_16_Q14(23170, MIN32(23169, r));
322
99.4k
         r = SHR16(MIN16(thresh, r),1);
323
99.4k
         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.3k
         r = 2.f*celt_exp2_db(-Ediff);
328
89.3k
         if (LM==3)
329
14.2k
            r *= 1.41421356f;
330
89.3k
         r = MIN16(thresh, r);
331
         r = r*sqrt_1;
332
#endif
333
188k
         X = X_+c*size+(m->eBands[i]<<LM);
334
1.06M
         for (k=0;k<1<<LM;k++)
335
872k
         {
336
            /* Detect collapse */
337
872k
            if (!(collapse_masks[i*C+c]&1<<k))
338
86.7k
            {
339
               /* Fill with noise */
340
327k
               for (j=0;j<N0;j++)
341
240k
               {
342
240k
                  seed = celt_lcg_rand(seed);
343
240k
                  X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
344
240k
               }
345
86.7k
               renormalize = 1;
346
86.7k
            }
347
872k
         }
348
         /* We just added some energy, so we need to renormalise */
349
188k
         if (renormalize)
350
33.7k
            renormalise_vector(X, N0<<LM, Q31ONE, arch);
351
188k
      } while (++c<C);
352
108k
   }
353
6.77k
}
anti_collapse
Line
Count
Source
262
3.64k
{
263
3.64k
   int c, i, j, k;
264
61.6k
   for (i=start;i<end;i++)
265
58.0k
   {
266
58.0k
      int N0;
267
58.0k
      opus_val16 thresh, sqrt_1;
268
58.0k
      int depth;
269
58.0k
#ifdef FIXED_POINT
270
58.0k
      int shift;
271
58.0k
      opus_val32 thresh32;
272
58.0k
#endif
273
274
58.0k
      N0 = m->eBands[i+1]-m->eBands[i];
275
      /* depth in 1/8 bits */
276
58.0k
      celt_sig_assert(pulses[i]>=0);
277
58.0k
      depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i]))>>LM;
278
279
58.0k
#ifdef FIXED_POINT
280
58.0k
      thresh32 = SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1);
281
58.0k
      thresh = MULT16_32_Q15(QCONST16(0.5f, 15), MIN32(32767,thresh32));
282
58.0k
      {
283
58.0k
         opus_val32 t;
284
58.0k
         t = N0<<LM;
285
58.0k
         shift = celt_ilog2(t)>>1;
286
58.0k
         t = SHL32(t, (7-shift)<<1);
287
58.0k
         sqrt_1 = celt_rsqrt_norm(t);
288
58.0k
      }
289
#else
290
      thresh = .5f*celt_exp2(-.125f*depth);
291
      sqrt_1 = celt_rsqrt(N0<<LM);
292
#endif
293
294
58.0k
      c=0; do
295
99.4k
      {
296
99.4k
         celt_norm *X;
297
99.4k
         celt_glog prev1;
298
99.4k
         celt_glog prev2;
299
99.4k
         opus_val32 Ediff;
300
99.4k
         celt_norm r;
301
99.4k
         int renormalize=0;
302
99.4k
         prev1 = prev1logE[c*m->nbEBands+i];
303
99.4k
         prev2 = prev2logE[c*m->nbEBands+i];
304
99.4k
         if (!encode && C==1)
305
16.6k
         {
306
16.6k
            prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]);
307
16.6k
            prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]);
308
16.6k
         }
309
99.4k
         Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2);
310
99.4k
         Ediff = MAX32(0, Ediff);
311
312
99.4k
#ifdef FIXED_POINT
313
99.4k
         if (Ediff < GCONST(16.f))
314
15.1k
         {
315
15.1k
            opus_val32 r32 = SHR32(celt_exp2_db(-Ediff),1);
316
15.1k
            r = 2*MIN16(16383,r32);
317
84.3k
         } else {
318
84.3k
            r = 0;
319
84.3k
         }
320
99.4k
         if (LM==3)
321
14.9k
            r = MULT16_16_Q14(23170, MIN32(23169, r));
322
99.4k
         r = SHR16(MIN16(thresh, r),1);
323
99.4k
         r = VSHR32(MULT16_16_Q15(sqrt_1, r),shift+14-NORM_SHIFT);
324
#else
325
         /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because
326
            short blocks don't have the same energy as long */
327
         r = 2.f*celt_exp2_db(-Ediff);
328
         if (LM==3)
329
            r *= 1.41421356f;
330
         r = MIN16(thresh, r);
331
         r = r*sqrt_1;
332
#endif
333
99.4k
         X = X_+c*size+(m->eBands[i]<<LM);
334
557k
         for (k=0;k<1<<LM;k++)
335
457k
         {
336
            /* Detect collapse */
337
457k
            if (!(collapse_masks[i*C+c]&1<<k))
338
48.1k
            {
339
               /* Fill with noise */
340
179k
               for (j=0;j<N0;j++)
341
131k
               {
342
131k
                  seed = celt_lcg_rand(seed);
343
131k
                  X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
344
131k
               }
345
48.1k
               renormalize = 1;
346
48.1k
            }
347
457k
         }
348
         /* We just added some energy, so we need to renormalise */
349
99.4k
         if (renormalize)
350
17.7k
            renormalise_vector(X, N0<<LM, Q31ONE, arch);
351
99.4k
      } while (++c<C);
352
58.0k
   }
353
3.64k
}
anti_collapse
Line
Count
Source
262
3.13k
{
263
3.13k
   int c, i, j, k;
264
53.9k
   for (i=start;i<end;i++)
265
50.8k
   {
266
50.8k
      int N0;
267
50.8k
      opus_val16 thresh, sqrt_1;
268
50.8k
      int depth;
269
#ifdef FIXED_POINT
270
      int shift;
271
      opus_val32 thresh32;
272
#endif
273
274
50.8k
      N0 = m->eBands[i+1]-m->eBands[i];
275
      /* depth in 1/8 bits */
276
50.8k
      celt_sig_assert(pulses[i]>=0);
277
50.8k
      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.8k
      thresh = .5f*celt_exp2(-.125f*depth);
291
50.8k
      sqrt_1 = celt_rsqrt(N0<<LM);
292
50.8k
#endif
293
294
50.8k
      c=0; do
295
89.3k
      {
296
89.3k
         celt_norm *X;
297
89.3k
         celt_glog prev1;
298
89.3k
         celt_glog prev2;
299
89.3k
         opus_val32 Ediff;
300
89.3k
         celt_norm r;
301
89.3k
         int renormalize=0;
302
89.3k
         prev1 = prev1logE[c*m->nbEBands+i];
303
89.3k
         prev2 = prev2logE[c*m->nbEBands+i];
304
89.3k
         if (!encode && C==1)
305
12.3k
         {
306
12.3k
            prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]);
307
12.3k
            prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]);
308
12.3k
         }
309
89.3k
         Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2);
310
89.3k
         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.3k
         r = 2.f*celt_exp2_db(-Ediff);
328
89.3k
         if (LM==3)
329
14.2k
            r *= 1.41421356f;
330
89.3k
         r = MIN16(thresh, r);
331
89.3k
         r = r*sqrt_1;
332
89.3k
#endif
333
89.3k
         X = X_+c*size+(m->eBands[i]<<LM);
334
503k
         for (k=0;k<1<<LM;k++)
335
414k
         {
336
            /* Detect collapse */
337
414k
            if (!(collapse_masks[i*C+c]&1<<k))
338
38.5k
            {
339
               /* Fill with noise */
340
148k
               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.5k
               renormalize = 1;
346
38.5k
            }
347
414k
         }
348
         /* We just added some energy, so we need to renormalise */
349
89.3k
         if (renormalize)
350
16.0k
            renormalise_vector(X, N0<<LM, Q31ONE, arch);
351
89.3k
      } while (++c<C);
352
50.8k
   }
353
3.13k
}
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
1.77M
{
364
1.77M
   celt_ener minE;
365
#ifdef FIXED_POINT
366
   int shift;
367
#endif
368
1.77M
   minE = MIN32(Ex, Ey);
369
   /* Adjustment to make the weights a bit more conservative. */
370
1.77M
   Ex = ADD32(Ex, minE/3);
371
1.77M
   Ey = ADD32(Ey, minE/3);
372
#ifdef FIXED_POINT
373
1.59M
   shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
374
#endif
375
1.77M
   w[0] = VSHR32(Ex, shift);
376
1.77M
   w[1] = VSHR32(Ey, shift);
377
1.77M
}
bands.c:compute_channel_weights
Line
Count
Source
363
1.59M
{
364
1.59M
   celt_ener minE;
365
1.59M
#ifdef FIXED_POINT
366
1.59M
   int shift;
367
1.59M
#endif
368
1.59M
   minE = MIN32(Ex, Ey);
369
   /* Adjustment to make the weights a bit more conservative. */
370
1.59M
   Ex = ADD32(Ex, minE/3);
371
1.59M
   Ey = ADD32(Ey, minE/3);
372
1.59M
#ifdef FIXED_POINT
373
1.59M
   shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
374
1.59M
#endif
375
1.59M
   w[0] = VSHR32(Ex, shift);
376
1.59M
   w[1] = VSHR32(Ey, shift);
377
1.59M
}
bands.c:compute_channel_weights
Line
Count
Source
363
179k
{
364
179k
   celt_ener minE;
365
#ifdef FIXED_POINT
366
   int shift;
367
#endif
368
179k
   minE = MIN32(Ex, Ey);
369
   /* Adjustment to make the weights a bit more conservative. */
370
179k
   Ex = ADD32(Ex, minE/3);
371
179k
   Ey = ADD32(Ey, minE/3);
372
#ifdef FIXED_POINT
373
   shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
374
#endif
375
179k
   w[0] = VSHR32(Ex, shift);
376
179k
   w[1] = VSHR32(Ey, shift);
377
179k
}
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
115M
{
381
115M
   int i = bandID;
382
115M
   int j;
383
115M
   opus_val16 a1, a2;
384
115M
   opus_val16 left, right;
385
115M
   opus_val16 norm;
386
#ifdef FIXED_POINT
387
115M
   int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
388
#endif
389
115M
   left = VSHR32(bandE[i],shift);
390
115M
   right = VSHR32(bandE[i+m->nbEBands],shift);
391
115M
   norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
392
#ifdef FIXED_POINT
393
115M
   left = MIN32(left, norm-1);
394
115M
   right = MIN32(right, norm-1);
395
#endif
396
115M
   a1 = DIV32_16(SHL32(EXTEND32(left),15),norm);
397
115M
   a2 = DIV32_16(SHL32(EXTEND32(right),15),norm);
398
1.36G
   for (j=0;j<N;j++)
399
1.24G
   {
400
1.24G
      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
1.24G
   }
403
115M
}
bands.c:intensity_stereo
Line
Count
Source
380
115M
{
381
115M
   int i = bandID;
382
115M
   int j;
383
115M
   opus_val16 a1, a2;
384
115M
   opus_val16 left, right;
385
115M
   opus_val16 norm;
386
115M
#ifdef FIXED_POINT
387
115M
   int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
388
115M
#endif
389
115M
   left = VSHR32(bandE[i],shift);
390
115M
   right = VSHR32(bandE[i+m->nbEBands],shift);
391
115M
   norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
392
115M
#ifdef FIXED_POINT
393
115M
   left = MIN32(left, norm-1);
394
115M
   right = MIN32(right, norm-1);
395
115M
#endif
396
115M
   a1 = DIV32_16(SHL32(EXTEND32(left),15),norm);
397
115M
   a2 = DIV32_16(SHL32(EXTEND32(right),15),norm);
398
1.35G
   for (j=0;j<N;j++)
399
1.24G
   {
400
1.24G
      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
1.24G
   }
403
115M
}
bands.c:intensity_stereo
Line
Count
Source
380
314k
{
381
314k
   int i = bandID;
382
314k
   int j;
383
314k
   opus_val16 a1, a2;
384
314k
   opus_val16 left, right;
385
314k
   opus_val16 norm;
386
#ifdef FIXED_POINT
387
   int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
388
#endif
389
314k
   left = VSHR32(bandE[i],shift);
390
314k
   right = VSHR32(bandE[i+m->nbEBands],shift);
391
314k
   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
314k
   a1 = DIV32_16(SHL32(EXTEND32(left),15),norm);
397
314k
   a2 = DIV32_16(SHL32(EXTEND32(right),15),norm);
398
4.96M
   for (j=0;j<N;j++)
399
4.64M
   {
400
4.64M
      X[j] = ADD32(MULT16_32_Q15(a1, X[j]), MULT16_32_Q15(a2, Y[j]));
401
      /* Side is not encoded, no need to calculate */
402
4.64M
   }
403
314k
}
404
405
static void stereo_split(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, int N)
406
4.33M
{
407
4.33M
   int j;
408
42.4M
   for (j=0;j<N;j++)
409
38.0M
   {
410
38.0M
      opus_val32 r, l;
411
38.0M
      l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]);
412
38.0M
      r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]);
413
38.0M
      X[j] = ADD32(l, r);
414
38.0M
      Y[j] = SUB32(r, l);
415
38.0M
   }
416
4.33M
}
bands.c:stereo_split
Line
Count
Source
406
2.16M
{
407
2.16M
   int j;
408
21.2M
   for (j=0;j<N;j++)
409
19.0M
   {
410
19.0M
      opus_val32 r, l;
411
19.0M
      l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]);
412
19.0M
      r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]);
413
19.0M
      X[j] = ADD32(l, r);
414
19.0M
      Y[j] = SUB32(r, l);
415
19.0M
   }
416
2.16M
}
bands.c:stereo_split
Line
Count
Source
406
2.16M
{
407
2.16M
   int j;
408
21.2M
   for (j=0;j<N;j++)
409
19.0M
   {
410
19.0M
      opus_val32 r, l;
411
19.0M
      l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]);
412
19.0M
      r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]);
413
19.0M
      X[j] = ADD32(l, r);
414
19.0M
      Y[j] = SUB32(r, l);
415
19.0M
   }
416
2.16M
}
417
418
static void stereo_merge(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, opus_val32 mid, int N, int arch)
419
29.3M
{
420
29.3M
   int j;
421
29.3M
   opus_val32 xp=0, side=0;
422
29.3M
   opus_val32 El, Er;
423
#ifdef FIXED_POINT
424
   int kl, kr;
425
#endif
426
29.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
29.3M
   xp = celt_inner_prod_norm_shift(Y, X, N, arch);
430
29.3M
   side = celt_inner_prod_norm_shift(Y, Y, N, arch);
431
   /* Compensating for the mid normalization */
432
29.3M
   xp = MULT32_32_Q31(mid, xp);
433
   /* mid and side are in Q15, not Q14 like X and Y */
434
29.3M
   El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp;
435
29.3M
   Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp;
436
29.3M
   if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
437
156k
   {
438
156k
      OPUS_COPY(Y, X, N);
439
156k
      return;
440
156k
   }
441
442
#ifdef FIXED_POINT
443
28.4M
   kl = celt_ilog2(El)>>1;
444
28.4M
   kr = celt_ilog2(Er)>>1;
445
28.4M
#endif
446
29.2M
   t = VSHR32(El, (kl<<1)-29);
447
734k
   lgain = celt_rsqrt_norm32(t);
448
29.2M
   t = VSHR32(Er, (kr<<1)-29);
449
734k
   rgain = celt_rsqrt_norm32(t);
450
451
#ifdef FIXED_POINT
452
28.4M
   if (kl < 7)
453
0
      kl = 7;
454
28.4M
   if (kr < 7)
455
0
      kr = 7;
456
#endif
457
458
457M
   for (j=0;j<N;j++)
459
428M
   {
460
428M
      celt_norm r, l;
461
      /* Apply mid scaling (side is already scaled) */
462
428M
      l = MULT32_32_Q31(mid, X[j]);
463
428M
      r = Y[j];
464
428M
      X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15);
465
428M
      Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15);
466
428M
   }
467
734k
}
bands.c:stereo_merge
Line
Count
Source
419
28.6M
{
420
28.6M
   int j;
421
28.6M
   opus_val32 xp=0, side=0;
422
28.6M
   opus_val32 El, Er;
423
28.6M
#ifdef FIXED_POINT
424
28.6M
   int kl, kr;
425
28.6M
#endif
426
28.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
28.6M
   xp = celt_inner_prod_norm_shift(Y, X, N, arch);
430
28.6M
   side = celt_inner_prod_norm_shift(Y, Y, N, arch);
431
   /* Compensating for the mid normalization */
432
28.6M
   xp = MULT32_32_Q31(mid, xp);
433
   /* mid and side are in Q15, not Q14 like X and Y */
434
28.6M
   El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp;
435
28.6M
   Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp;
436
28.6M
   if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
437
154k
   {
438
154k
      OPUS_COPY(Y, X, N);
439
154k
      return;
440
154k
   }
441
442
28.4M
#ifdef FIXED_POINT
443
28.4M
   kl = celt_ilog2(El)>>1;
444
28.4M
   kr = celt_ilog2(Er)>>1;
445
28.4M
#endif
446
28.4M
   t = VSHR32(El, (kl<<1)-29);
447
28.4M
   lgain = celt_rsqrt_norm32(t);
448
28.4M
   t = VSHR32(Er, (kr<<1)-29);
449
28.4M
   rgain = celt_rsqrt_norm32(t);
450
451
28.4M
#ifdef FIXED_POINT
452
28.4M
   if (kl < 7)
453
0
      kl = 7;
454
28.4M
   if (kr < 7)
455
0
      kr = 7;
456
28.4M
#endif
457
458
444M
   for (j=0;j<N;j++)
459
416M
   {
460
416M
      celt_norm r, l;
461
      /* Apply mid scaling (side is already scaled) */
462
416M
      l = MULT32_32_Q31(mid, X[j]);
463
416M
      r = Y[j];
464
416M
      X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15);
465
416M
      Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15);
466
416M
   }
467
28.4M
}
bands.c:stereo_merge
Line
Count
Source
419
735k
{
420
735k
   int j;
421
735k
   opus_val32 xp=0, side=0;
422
735k
   opus_val32 El, Er;
423
#ifdef FIXED_POINT
424
   int kl, kr;
425
#endif
426
735k
   opus_val32 t, lgain, rgain;
427
428
   /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
429
735k
   xp = celt_inner_prod_norm_shift(Y, X, N, arch);
430
735k
   side = celt_inner_prod_norm_shift(Y, Y, N, arch);
431
   /* Compensating for the mid normalization */
432
735k
   xp = MULT32_32_Q31(mid, xp);
433
   /* mid and side are in Q15, not Q14 like X and Y */
434
735k
   El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp;
435
735k
   Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp;
436
735k
   if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
437
1.29k
   {
438
1.29k
      OPUS_COPY(Y, X, N);
439
1.29k
      return;
440
1.29k
   }
441
442
#ifdef FIXED_POINT
443
   kl = celt_ilog2(El)>>1;
444
   kr = celt_ilog2(Er)>>1;
445
#endif
446
734k
   t = VSHR32(El, (kl<<1)-29);
447
734k
   lgain = celt_rsqrt_norm32(t);
448
734k
   t = VSHR32(Er, (kr<<1)-29);
449
734k
   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
13.0M
   for (j=0;j<N;j++)
459
12.3M
   {
460
12.3M
      celt_norm r, l;
461
      /* Apply mid scaling (side is already scaled) */
462
12.3M
      l = MULT32_32_Q31(mid, X[j]);
463
12.3M
      r = Y[j];
464
12.3M
      X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15);
465
12.3M
      Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15);
466
12.3M
   }
467
734k
}
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
8.25M
{
474
8.25M
   int i, c, N0;
475
8.25M
   int sum = 0, nbBands=0;
476
8.25M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
477
8.25M
   int decision;
478
8.25M
   int hf_sum=0;
479
480
8.25M
   celt_assert(end>0);
481
482
8.25M
   N0 = M*m->shortMdctSize;
483
484
8.25M
   if (M*(eBands[end]-eBands[end-1]) <= 8)
485
2.30M
      return SPREAD_NONE;
486
6.66M
   c=0; do {
487
111M
      for (i=0;i<end;i++)
488
104M
      {
489
104M
         int j, N, tmp=0;
490
104M
         int tcount[3] = {0,0,0};
491
104M
         const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
492
104M
         N = M*(eBands[i+1]-eBands[i]);
493
104M
         if (N<=8)
494
63.4M
            continue;
495
         /* Compute rough CDF of |x[j]| */
496
1.16G
         for (j=0;j<N;j++)
497
1.12G
         {
498
1.12G
            opus_val32 x2N; /* Q13 */
499
500
1.12G
            x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N);
501
1.12G
            if (x2N < QCONST16(0.25f,13))
502
1.07G
               tcount[0]++;
503
1.12G
            if (x2N < QCONST16(0.0625f,13))
504
1.05G
               tcount[1]++;
505
1.12G
            if (x2N < QCONST16(0.015625f,13))
506
1.04G
               tcount[2]++;
507
1.12G
         }
508
509
         /* Only include four last bands (8 kHz and up) */
510
41.2M
         if (i>m->nbEBands-4)
511
1.37M
            hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
512
41.2M
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
513
41.2M
         sum += tmp*spread_weight[i];
514
41.2M
         nbBands+=spread_weight[i];
515
41.2M
      }
516
6.66M
   } while (++c<C);
517
518
5.95M
   if (update_hf)
519
39.4k
   {
520
39.4k
      if (hf_sum)
521
5.94k
         hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
522
39.4k
      *hf_average = (*hf_average+hf_sum)>>1;
523
39.4k
      hf_sum = *hf_average;
524
39.4k
      if (*tapset_decision==2)
525
312
         hf_sum += 4;
526
39.1k
      else if (*tapset_decision==0)
527
38.7k
         hf_sum -= 4;
528
39.4k
      if (hf_sum > 22)
529
590
         *tapset_decision=2;
530
38.8k
      else if (hf_sum > 18)
531
608
         *tapset_decision=1;
532
38.2k
      else
533
38.2k
         *tapset_decision=0;
534
39.4k
   }
535
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
536
5.95M
   celt_assert(nbBands>0); /* end has to be non-zero */
537
5.95M
   celt_assert(sum>=0);
538
5.95M
   sum = celt_udiv((opus_int32)sum<<8, nbBands);
539
   /* Recursive averaging */
540
5.95M
   sum = (sum+*average)>>1;
541
5.95M
   *average = sum;
542
   /* Hysteresis */
543
5.95M
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
544
5.95M
   if (sum < 80)
545
290k
   {
546
290k
      decision = SPREAD_AGGRESSIVE;
547
5.66M
   } else if (sum < 256)
548
176k
   {
549
176k
      decision = SPREAD_NORMAL;
550
5.48M
   } else if (sum < 384)
551
29.5k
   {
552
29.5k
      decision = SPREAD_LIGHT;
553
5.46M
   } else {
554
5.46M
      decision = SPREAD_NONE;
555
5.46M
   }
556
#ifdef FUZZING
557
   decision = rand()&0x3;
558
   *tapset_decision=rand()%3;
559
#endif
560
5.95M
   return decision;
561
5.95M
}
spreading_decision
Line
Count
Source
473
4.12M
{
474
4.12M
   int i, c, N0;
475
4.12M
   int sum = 0, nbBands=0;
476
4.12M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
477
4.12M
   int decision;
478
4.12M
   int hf_sum=0;
479
480
4.12M
   celt_assert(end>0);
481
482
4.12M
   N0 = M*m->shortMdctSize;
483
484
4.12M
   if (M*(eBands[end]-eBands[end-1]) <= 8)
485
1.15M
      return SPREAD_NONE;
486
3.33M
   c=0; do {
487
55.6M
      for (i=0;i<end;i++)
488
52.3M
      {
489
52.3M
         int j, N, tmp=0;
490
52.3M
         int tcount[3] = {0,0,0};
491
52.3M
         const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
492
52.3M
         N = M*(eBands[i+1]-eBands[i]);
493
52.3M
         if (N<=8)
494
31.7M
            continue;
495
         /* Compute rough CDF of |x[j]| */
496
584M
         for (j=0;j<N;j++)
497
563M
         {
498
563M
            opus_val32 x2N; /* Q13 */
499
500
563M
            x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N);
501
563M
            if (x2N < QCONST16(0.25f,13))
502
537M
               tcount[0]++;
503
563M
            if (x2N < QCONST16(0.0625f,13))
504
529M
               tcount[1]++;
505
563M
            if (x2N < QCONST16(0.015625f,13))
506
524M
               tcount[2]++;
507
563M
         }
508
509
         /* Only include four last bands (8 kHz and up) */
510
20.6M
         if (i>m->nbEBands-4)
511
687k
            hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
512
20.6M
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
513
20.6M
         sum += tmp*spread_weight[i];
514
20.6M
         nbBands+=spread_weight[i];
515
20.6M
      }
516
3.33M
   } while (++c<C);
517
518
2.97M
   if (update_hf)
519
19.7k
   {
520
19.7k
      if (hf_sum)
521
2.97k
         hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
522
19.7k
      *hf_average = (*hf_average+hf_sum)>>1;
523
19.7k
      hf_sum = *hf_average;
524
19.7k
      if (*tapset_decision==2)
525
156
         hf_sum += 4;
526
19.5k
      else if (*tapset_decision==0)
527
19.3k
         hf_sum -= 4;
528
19.7k
      if (hf_sum > 22)
529
295
         *tapset_decision=2;
530
19.4k
      else if (hf_sum > 18)
531
304
         *tapset_decision=1;
532
19.1k
      else
533
19.1k
         *tapset_decision=0;
534
19.7k
   }
535
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
536
2.97M
   celt_assert(nbBands>0); /* end has to be non-zero */
537
2.97M
   celt_assert(sum>=0);
538
2.97M
   sum = celt_udiv((opus_int32)sum<<8, nbBands);
539
   /* Recursive averaging */
540
2.97M
   sum = (sum+*average)>>1;
541
2.97M
   *average = sum;
542
   /* Hysteresis */
543
2.97M
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
544
2.97M
   if (sum < 80)
545
145k
   {
546
145k
      decision = SPREAD_AGGRESSIVE;
547
2.83M
   } else if (sum < 256)
548
88.4k
   {
549
88.4k
      decision = SPREAD_NORMAL;
550
2.74M
   } else if (sum < 384)
551
14.7k
   {
552
14.7k
      decision = SPREAD_LIGHT;
553
2.73M
   } else {
554
2.73M
      decision = SPREAD_NONE;
555
2.73M
   }
556
#ifdef FUZZING
557
   decision = rand()&0x3;
558
   *tapset_decision=rand()%3;
559
#endif
560
2.97M
   return decision;
561
2.97M
}
spreading_decision
Line
Count
Source
473
4.12M
{
474
4.12M
   int i, c, N0;
475
4.12M
   int sum = 0, nbBands=0;
476
4.12M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
477
4.12M
   int decision;
478
4.12M
   int hf_sum=0;
479
480
4.12M
   celt_assert(end>0);
481
482
4.12M
   N0 = M*m->shortMdctSize;
483
484
4.12M
   if (M*(eBands[end]-eBands[end-1]) <= 8)
485
1.15M
      return SPREAD_NONE;
486
3.33M
   c=0; do {
487
55.6M
      for (i=0;i<end;i++)
488
52.3M
      {
489
52.3M
         int j, N, tmp=0;
490
52.3M
         int tcount[3] = {0,0,0};
491
52.3M
         const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
492
52.3M
         N = M*(eBands[i+1]-eBands[i]);
493
52.3M
         if (N<=8)
494
31.7M
            continue;
495
         /* Compute rough CDF of |x[j]| */
496
584M
         for (j=0;j<N;j++)
497
563M
         {
498
563M
            opus_val32 x2N; /* Q13 */
499
500
563M
            x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N);
501
563M
            if (x2N < QCONST16(0.25f,13))
502
537M
               tcount[0]++;
503
563M
            if (x2N < QCONST16(0.0625f,13))
504
529M
               tcount[1]++;
505
563M
            if (x2N < QCONST16(0.015625f,13))
506
524M
               tcount[2]++;
507
563M
         }
508
509
         /* Only include four last bands (8 kHz and up) */
510
20.6M
         if (i>m->nbEBands-4)
511
687k
            hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
512
20.6M
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
513
20.6M
         sum += tmp*spread_weight[i];
514
20.6M
         nbBands+=spread_weight[i];
515
20.6M
      }
516
3.33M
   } while (++c<C);
517
518
2.97M
   if (update_hf)
519
19.7k
   {
520
19.7k
      if (hf_sum)
521
2.97k
         hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
522
19.7k
      *hf_average = (*hf_average+hf_sum)>>1;
523
19.7k
      hf_sum = *hf_average;
524
19.7k
      if (*tapset_decision==2)
525
156
         hf_sum += 4;
526
19.5k
      else if (*tapset_decision==0)
527
19.3k
         hf_sum -= 4;
528
19.7k
      if (hf_sum > 22)
529
295
         *tapset_decision=2;
530
19.4k
      else if (hf_sum > 18)
531
304
         *tapset_decision=1;
532
19.1k
      else
533
19.1k
         *tapset_decision=0;
534
19.7k
   }
535
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
536
2.97M
   celt_assert(nbBands>0); /* end has to be non-zero */
537
2.97M
   celt_assert(sum>=0);
538
2.97M
   sum = celt_udiv((opus_int32)sum<<8, nbBands);
539
   /* Recursive averaging */
540
2.97M
   sum = (sum+*average)>>1;
541
2.97M
   *average = sum;
542
   /* Hysteresis */
543
2.97M
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
544
2.97M
   if (sum < 80)
545
145k
   {
546
145k
      decision = SPREAD_AGGRESSIVE;
547
2.83M
   } else if (sum < 256)
548
88.4k
   {
549
88.4k
      decision = SPREAD_NORMAL;
550
2.74M
   } else if (sum < 384)
551
14.7k
   {
552
14.7k
      decision = SPREAD_LIGHT;
553
2.73M
   } else {
554
2.73M
      decision = SPREAD_NONE;
555
2.73M
   }
556
#ifdef FUZZING
557
   decision = rand()&0x3;
558
   *tapset_decision=rand()%3;
559
#endif
560
2.97M
   return decision;
561
2.97M
}
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
13.0M
{
576
13.0M
   int i,j;
577
13.0M
   VARDECL(celt_norm, tmp);
578
13.0M
   int N;
579
13.0M
   SAVE_STACK;
580
13.0M
   N = N0*stride;
581
13.0M
   ALLOC(tmp, N, celt_norm);
582
13.0M
   celt_assert(stride>0);
583
13.0M
   if (hadamard)
584
1.11M
   {
585
1.11M
      const int *ordery = ordery_table+stride-2;
586
5.31M
      for (i=0;i<stride;i++)
587
4.19M
      {
588
20.8M
         for (j=0;j<N0;j++)
589
16.6M
            tmp[ordery[i]*N0+j] = X[j*stride+i];
590
4.19M
      }
591
11.9M
   } else {
592
68.8M
      for (i=0;i<stride;i++)
593
172M
         for (j=0;j<N0;j++)
594
115M
            tmp[i*N0+j] = X[j*stride+i];
595
11.9M
   }
596
13.0M
   OPUS_COPY(X, tmp, N);
597
13.0M
   RESTORE_STACK;
598
13.0M
}
599
600
static void interleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
601
1.83M
{
602
1.83M
   int i,j;
603
1.83M
   VARDECL(celt_norm, tmp);
604
1.83M
   int N;
605
1.83M
   SAVE_STACK;
606
1.83M
   N = N0*stride;
607
1.83M
   ALLOC(tmp, N, celt_norm);
608
1.83M
   if (hadamard)
609
553k
   {
610
553k
      const int *ordery = ordery_table+stride-2;
611
2.47M
      for (i=0;i<stride;i++)
612
9.38M
         for (j=0;j<N0;j++)
613
7.46M
            tmp[j*stride+i] = X[ordery[i]*N0+j];
614
1.28M
   } else {
615
10.7M
      for (i=0;i<stride;i++)
616
25.6M
         for (j=0;j<N0;j++)
617
16.1M
            tmp[j*stride+i] = X[i*N0+j];
618
1.28M
   }
619
1.83M
   OPUS_COPY(X, tmp, N);
620
1.83M
   RESTORE_STACK;
621
1.83M
}
622
623
void haar1(celt_norm *X, int N0, int stride)
624
1.03G
{
625
1.03G
   int i, j;
626
1.03G
   N0 >>= 1;
627
3.76G
   for (i=0;i<stride;i++)
628
11.0G
      for (j=0;j<N0;j++)
629
8.31G
      {
630
8.31G
         opus_val32 tmp1, tmp2;
631
8.31G
         tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]);
632
8.31G
         tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]);
633
8.31G
         X[stride*2*j+i] = ADD32(tmp1, tmp2);
634
8.31G
         X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2);
635
8.31G
      }
636
1.03G
}
haar1
Line
Count
Source
624
518M
{
625
518M
   int i, j;
626
518M
   N0 >>= 1;
627
1.88G
   for (i=0;i<stride;i++)
628
5.52G
      for (j=0;j<N0;j++)
629
4.15G
      {
630
4.15G
         opus_val32 tmp1, tmp2;
631
4.15G
         tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]);
632
4.15G
         tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]);
633
4.15G
         X[stride*2*j+i] = ADD32(tmp1, tmp2);
634
4.15G
         X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2);
635
4.15G
      }
636
518M
}
haar1
Line
Count
Source
624
518M
{
625
518M
   int i, j;
626
518M
   N0 >>= 1;
627
1.88G
   for (i=0;i<stride;i++)
628
5.52G
      for (j=0;j<N0;j++)
629
4.15G
      {
630
4.15G
         opus_val32 tmp1, tmp2;
631
4.15G
         tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]);
632
4.15G
         tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]);
633
4.15G
         X[stride*2*j+i] = ADD32(tmp1, tmp2);
634
4.15G
         X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2);
635
4.15G
      }
636
518M
}
637
638
static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo)
639
195M
{
640
195M
   static const opus_int16 exp2_table8[8] =
641
195M
      {16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048};
642
195M
   int qn, qb;
643
195M
   int N2 = 2*N-1;
644
195M
   if (stereo && N==2)
645
25.7M
      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
195M
   qb = celt_sudiv(b+N2*offset, N2);
650
195M
   qb = IMIN(b-pulse_cap-(4<<BITRES), qb);
651
652
195M
   qb = IMIN(8<<BITRES, qb);
653
654
195M
   if (qb<(1<<BITRES>>1)) {
655
96.5M
      qn = 1;
656
98.9M
   } else {
657
98.9M
      qn = exp2_table8[qb&0x7]>>(14-(qb>>BITRES));
658
98.9M
      qn = (qn+1)>>1<<1;
659
98.9M
   }
660
195M
   celt_assert(qn <= 256);
661
195M
   return qn;
662
195M
}
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
195M
{
705
195M
   int qn;
706
195M
   int itheta=0;
707
195M
   int itheta_q30=0;
708
195M
   int delta;
709
195M
   int imid, iside;
710
195M
   int qalloc;
711
195M
   int pulse_cap;
712
195M
   int offset;
713
195M
   opus_int32 tell;
714
195M
   int inv=0;
715
195M
   int encode;
716
195M
   const CELTMode *m;
717
195M
   int i;
718
195M
   int intensity;
719
195M
   ec_ctx *ec;
720
195M
   const celt_ener *bandE;
721
722
195M
   encode = ctx->encode;
723
195M
   m = ctx->m;
724
195M
   i = ctx->i;
725
195M
   intensity = ctx->intensity;
726
195M
   ec = ctx->ec;
727
195M
   bandE = ctx->bandE;
728
729
   /* Decide on the resolution to give to the split parameter theta */
730
195M
   pulse_cap = m->logN[i]+LM*(1<<BITRES);
731
195M
   offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
732
195M
   qn = compute_qn(N, *b, offset, pulse_cap, stereo);
733
195M
   if (stereo && i>=intensity)
734
98.3M
      qn = 1;
735
195M
   if (encode)
736
193M
   {
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
193M
      itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch);
742
193M
      itheta = itheta_q30>>16;
743
193M
   }
744
195M
   tell = ec_tell_frac(ec);
745
195M
   if (qn!=1)
746
94.8M
   {
747
94.8M
      if (encode)
748
94.0M
      {
749
94.0M
         if (!stereo || ctx->theta_round == 0)
750
91.7M
         {
751
91.7M
            itheta = (itheta*(opus_int32)qn+8192)>>14;
752
91.7M
            if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
753
77.8k
            {
754
               /* Check if the selected value of theta will cause the bit allocation
755
                  to inject noise on one side. If so, make sure the energy of that side
756
                  is zero. */
757
77.8k
               int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
758
77.8k
               imid = bitexact_cos((opus_int16)unquantized);
759
77.8k
               iside = bitexact_cos((opus_int16)(16384-unquantized));
760
77.8k
               delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
761
77.8k
               if (delta > *b)
762
633
                  itheta = qn;
763
77.2k
               else if (delta < -*b)
764
515
                  itheta = 0;
765
77.8k
            }
766
91.7M
         } else {
767
2.33M
            int down;
768
            /* Bias quantization towards itheta=0 and itheta=16384. */
769
2.33M
            int bias = itheta > 8192 ? 32767/qn : -32767/qn;
770
2.33M
            down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
771
2.33M
            if (ctx->theta_round < 0)
772
1.16M
               itheta = down;
773
1.16M
            else
774
1.16M
               itheta = down+1;
775
2.33M
         }
776
94.0M
      }
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
94.8M
      if (stereo && N>2)
780
16.9M
      {
781
16.9M
         int p0 = 3;
782
16.9M
         int x = itheta;
783
16.9M
         int x0 = qn/2;
784
16.9M
         int ft = p0*(x0+1) + x0;
785
         /* Use a probability of p0 up to itheta=8192 and then use 1 after */
786
16.9M
         if (encode)
787
16.8M
         {
788
16.8M
            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
16.8M
         } else {
790
70.6k
            int fs;
791
70.6k
            fs=ec_decode(ec,ft);
792
70.6k
            if (fs<(x0+1)*p0)
793
48.6k
               x=fs/p0;
794
21.9k
            else
795
21.9k
               x=x0+1+(fs-(x0+1)*p0);
796
70.6k
            ec_dec_update(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft);
797
70.6k
            itheta = x;
798
70.6k
         }
799
77.9M
      } else if (B0>1 || stereo) {
800
         /* Uniform pdf */
801
6.53M
         if (encode)
802
6.19M
            ec_enc_uint(ec, itheta, qn+1);
803
333k
         else
804
333k
            itheta = ec_dec_uint(ec, qn+1);
805
71.4M
      } else {
806
71.4M
         int fs=1, ft;
807
71.4M
         ft = ((qn>>1)+1)*((qn>>1)+1);
808
71.4M
         if (encode)
809
71.0M
         {
810
71.0M
            int fl;
811
812
71.0M
            fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
813
71.0M
            fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
814
71.0M
             ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
815
816
71.0M
            ec_encode(ec, fl, fl+fs, ft);
817
71.0M
         } else {
818
            /* Triangular pdf */
819
412k
            int fl=0;
820
412k
            int fm;
821
412k
            fm = ec_decode(ec, ft);
822
823
412k
            if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
824
209k
            {
825
209k
               itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
826
209k
               fs = itheta + 1;
827
209k
               fl = itheta*(itheta + 1)>>1;
828
209k
            }
829
203k
            else
830
203k
            {
831
203k
               itheta = (2*(qn + 1)
832
203k
                - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
833
203k
               fs = qn + 1 - itheta;
834
203k
               fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
835
203k
            }
836
837
412k
            ec_dec_update(ec, fl, fl+fs, ft);
838
412k
         }
839
71.4M
      }
840
94.8M
      celt_assert(itheta>=0);
841
94.8M
      itheta = celt_udiv((opus_int32)itheta*16384, qn);
842
#ifdef ENABLE_QEXT
843
93.3M
      *ext_b = IMIN(*ext_b, ctx->ext_total_bits - (opus_int32)ec_tell_frac(ctx->ext_ec));
844
93.3M
      if (*ext_b >= 2*N<<BITRES && ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec)-1 > 2<<BITRES) {
845
1.47M
         int extra_bits;
846
1.47M
         int ext_tell = ec_tell_frac(ctx->ext_ec);
847
1.47M
         extra_bits = IMIN(12, IMAX(2, celt_sudiv(*ext_b, (2*N-1)<<BITRES)));
848
1.47M
         if (encode) {
849
1.45M
            itheta_q30 = itheta_q30 - (itheta<<16);
850
1.45M
            itheta_q30 = (itheta_q30*(opus_int64)qn*((1<<extra_bits)-1)+(1<<29))>>30;
851
1.45M
            itheta_q30 += (1<<(extra_bits-1))-1;
852
1.45M
            itheta_q30 = IMAX(0, IMIN((1<<extra_bits)-2, itheta_q30));
853
1.45M
            ec_enc_uint(ctx->ext_ec, itheta_q30, (1<<extra_bits)-1);
854
1.45M
         } else {
855
27.9k
            itheta_q30 = ec_dec_uint(ctx->ext_ec, (1<<extra_bits)-1);
856
27.9k
         }
857
1.47M
         itheta_q30 -= (1<<(extra_bits-1))-1;
858
1.47M
         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
1.47M
         itheta_q30 = IMAX(0, IMIN(itheta_q30, 1073741824));
861
1.47M
         *ext_b -= ec_tell_frac(ctx->ext_ec) - ext_tell;
862
91.8M
      } else {
863
91.8M
         itheta_q30 = (opus_int32)itheta<<16;
864
91.8M
      }
865
#endif
866
94.8M
      if (encode && stereo)
867
18.4M
      {
868
18.4M
         if (itheta==0)
869
16.2M
            intensity_stereo(m, X, Y, bandE, i, N);
870
2.16M
         else
871
2.16M
            stereo_split(X, Y, N);
872
18.4M
      }
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
100M
   } else if (stereo) {
876
100M
      if (encode)
877
99.3M
      {
878
99.3M
         inv = itheta > 8192 && !ctx->disable_inv;
879
99.3M
         if (inv)
880
153k
         {
881
153k
            int j;
882
3.03M
            for (j=0;j<N;j++)
883
2.88M
               Y[j] = -Y[j];
884
153k
         }
885
99.3M
         intensity_stereo(m, X, Y, bandE, i, N);
886
99.3M
      }
887
100M
      if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
888
7.17M
      {
889
7.17M
         if (encode)
890
7.01M
            ec_enc_bit_logp(ec, inv, 2);
891
158k
         else
892
158k
            inv = ec_dec_bit_logp(ec, 2);
893
7.17M
      } else
894
93.4M
         inv = 0;
895
      /* inv flag override to avoid problems with downmixing. */
896
100M
      if (ctx->disable_inv)
897
41.2M
         inv = 0;
898
100M
      itheta = 0;
899
100M
      itheta_q30 = 0;
900
100M
   }
901
195M
   qalloc = ec_tell_frac(ec) - tell;
902
195M
   *b -= qalloc;
903
904
195M
   if (itheta == 0)
905
178M
   {
906
178M
      imid = 32767;
907
178M
      iside = 0;
908
178M
      *fill &= (1<<B)-1;
909
178M
      delta = -16384;
910
178M
   } else if (itheta == 16384)
911
675k
   {
912
675k
      imid = 0;
913
675k
      iside = 32767;
914
675k
      *fill &= ((1<<B)-1)<<B;
915
675k
      delta = 16384;
916
16.2M
   } else {
917
16.2M
      imid = bitexact_cos((opus_int16)itheta);
918
16.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
16.2M
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
922
16.2M
   }
923
924
195M
   sctx->inv = inv;
925
195M
   sctx->imid = imid;
926
195M
   sctx->iside = iside;
927
195M
   sctx->delta = delta;
928
195M
   sctx->itheta = itheta;
929
#ifdef ENABLE_QEXT
930
   sctx->itheta_q30 = itheta_q30;
931
#endif
932
195M
   sctx->qalloc = qalloc;
933
195M
}
bands.c:compute_theta
Line
Count
Source
704
2.42M
{
705
2.42M
   int qn;
706
2.42M
   int itheta=0;
707
2.42M
   int itheta_q30=0;
708
2.42M
   int delta;
709
2.42M
   int imid, iside;
710
2.42M
   int qalloc;
711
2.42M
   int pulse_cap;
712
2.42M
   int offset;
713
2.42M
   opus_int32 tell;
714
2.42M
   int inv=0;
715
2.42M
   int encode;
716
2.42M
   const CELTMode *m;
717
2.42M
   int i;
718
2.42M
   int intensity;
719
2.42M
   ec_ctx *ec;
720
2.42M
   const celt_ener *bandE;
721
722
2.42M
   encode = ctx->encode;
723
2.42M
   m = ctx->m;
724
2.42M
   i = ctx->i;
725
2.42M
   intensity = ctx->intensity;
726
2.42M
   ec = ctx->ec;
727
2.42M
   bandE = ctx->bandE;
728
729
   /* Decide on the resolution to give to the split parameter theta */
730
2.42M
   pulse_cap = m->logN[i]+LM*(1<<BITRES);
731
2.42M
   offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
732
2.42M
   qn = compute_qn(N, *b, offset, pulse_cap, stereo);
733
2.42M
   if (stereo && i>=intensity)
734
841k
      qn = 1;
735
2.42M
   if (encode)
736
1.44M
   {
737
      /* theta is the atan() of the ratio between the (normalized)
738
         side and mid. With just that parameter, we can re-scale both
739
         mid and side because we know that 1) they have unit norm and
740
         2) they are orthogonal. */
741
1.44M
      itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch);
742
1.44M
      itheta = itheta_q30>>16;
743
1.44M
   }
744
2.42M
   tell = ec_tell_frac(ec);
745
2.42M
   if (qn!=1)
746
1.54M
   {
747
1.54M
      if (encode)
748
1.18M
      {
749
1.18M
         if (!stereo || ctx->theta_round == 0)
750
858k
         {
751
858k
            itheta = (itheta*(opus_int32)qn+8192)>>14;
752
858k
            if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
753
24.4k
            {
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
24.4k
               int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
758
24.4k
               imid = bitexact_cos((opus_int16)unquantized);
759
24.4k
               iside = bitexact_cos((opus_int16)(16384-unquantized));
760
24.4k
               delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
761
24.4k
               if (delta > *b)
762
315
                  itheta = qn;
763
24.1k
               else if (delta < -*b)
764
225
                  itheta = 0;
765
24.4k
            }
766
858k
         } else {
767
325k
            int down;
768
            /* Bias quantization towards itheta=0 and itheta=16384. */
769
325k
            int bias = itheta > 8192 ? 32767/qn : -32767/qn;
770
325k
            down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
771
325k
            if (ctx->theta_round < 0)
772
162k
               itheta = down;
773
162k
            else
774
162k
               itheta = down+1;
775
325k
         }
776
1.18M
      }
777
      /* Entropy coding of the angle. We use a uniform pdf for the
778
         time split, a step for stereo, and a triangular one for the rest. */
779
1.54M
      if (stereo && N>2)
780
341k
      {
781
341k
         int p0 = 3;
782
341k
         int x = itheta;
783
341k
         int x0 = qn/2;
784
341k
         int ft = p0*(x0+1) + x0;
785
         /* Use a probability of p0 up to itheta=8192 and then use 1 after */
786
341k
         if (encode)
787
314k
         {
788
314k
            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
314k
         } else {
790
27.1k
            int fs;
791
27.1k
            fs=ec_decode(ec,ft);
792
27.1k
            if (fs<(x0+1)*p0)
793
18.9k
               x=fs/p0;
794
8.19k
            else
795
8.19k
               x=x0+1+(fs-(x0+1)*p0);
796
27.1k
            ec_dec_update(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft);
797
27.1k
            itheta = x;
798
27.1k
         }
799
1.19M
      } else if (B0>1 || stereo) {
800
         /* Uniform pdf */
801
874k
         if (encode)
802
715k
            ec_enc_uint(ec, itheta, qn+1);
803
159k
         else
804
159k
            itheta = ec_dec_uint(ec, qn+1);
805
874k
      } else {
806
324k
         int fs=1, ft;
807
324k
         ft = ((qn>>1)+1)*((qn>>1)+1);
808
324k
         if (encode)
809
154k
         {
810
154k
            int fl;
811
812
154k
            fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
813
154k
            fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
814
154k
             ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
815
816
154k
            ec_encode(ec, fl, fl+fs, ft);
817
169k
         } else {
818
            /* Triangular pdf */
819
169k
            int fl=0;
820
169k
            int fm;
821
169k
            fm = ec_decode(ec, ft);
822
823
169k
            if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
824
85.5k
            {
825
85.5k
               itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
826
85.5k
               fs = itheta + 1;
827
85.5k
               fl = itheta*(itheta + 1)>>1;
828
85.5k
            }
829
84.2k
            else
830
84.2k
            {
831
84.2k
               itheta = (2*(qn + 1)
832
84.2k
                - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
833
84.2k
               fs = qn + 1 - itheta;
834
84.2k
               fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
835
84.2k
            }
836
837
169k
            ec_dec_update(ec, fl, fl+fs, ft);
838
169k
         }
839
324k
      }
840
1.54M
      celt_assert(itheta>=0);
841
1.54M
      itheta = celt_udiv((opus_int32)itheta*16384, qn);
842
#ifdef ENABLE_QEXT
843
      *ext_b = IMIN(*ext_b, ctx->ext_total_bits - (opus_int32)ec_tell_frac(ctx->ext_ec));
844
      if (*ext_b >= 2*N<<BITRES && ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec)-1 > 2<<BITRES) {
845
         int extra_bits;
846
         int ext_tell = ec_tell_frac(ctx->ext_ec);
847
         extra_bits = IMIN(12, IMAX(2, celt_sudiv(*ext_b, (2*N-1)<<BITRES)));
848
         if (encode) {
849
            itheta_q30 = itheta_q30 - (itheta<<16);
850
            itheta_q30 = (itheta_q30*(opus_int64)qn*((1<<extra_bits)-1)+(1<<29))>>30;
851
            itheta_q30 += (1<<(extra_bits-1))-1;
852
            itheta_q30 = IMAX(0, IMIN((1<<extra_bits)-2, itheta_q30));
853
            ec_enc_uint(ctx->ext_ec, itheta_q30, (1<<extra_bits)-1);
854
         } else {
855
            itheta_q30 = ec_dec_uint(ctx->ext_ec, (1<<extra_bits)-1);
856
         }
857
         itheta_q30 -= (1<<(extra_bits-1))-1;
858
         itheta_q30 = (itheta<<16) + itheta_q30*(opus_int64)(1<<30)/(qn*((1<<extra_bits)-1));
859
         /* Hard bounds on itheta (can only trigger on corrupted bitstreams). */
860
         itheta_q30 = IMAX(0, IMIN(itheta_q30, 1073741824));
861
         *ext_b -= ec_tell_frac(ctx->ext_ec) - ext_tell;
862
      } else {
863
         itheta_q30 = (opus_int32)itheta<<16;
864
      }
865
#endif
866
1.54M
      if (encode && stereo)
867
426k
      {
868
426k
         if (itheta==0)
869
79.7k
            intensity_stereo(m, X, Y, bandE, i, N);
870
347k
         else
871
347k
            stereo_split(X, Y, N);
872
426k
      }
873
      /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate.
874
               Let's do that at higher complexity */
875
1.54M
   } else if (stereo) {
876
884k
      if (encode)
877
260k
      {
878
260k
         inv = itheta > 8192 && !ctx->disable_inv;
879
260k
         if (inv)
880
62.7k
         {
881
62.7k
            int j;
882
1.13M
            for (j=0;j<N;j++)
883
1.07M
               Y[j] = -Y[j];
884
62.7k
         }
885
260k
         intensity_stereo(m, X, Y, bandE, i, N);
886
260k
      }
887
884k
      if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
888
207k
      {
889
207k
         if (encode)
890
138k
            ec_enc_bit_logp(ec, inv, 2);
891
68.7k
         else
892
68.7k
            inv = ec_dec_bit_logp(ec, 2);
893
207k
      } else
894
677k
         inv = 0;
895
      /* inv flag override to avoid problems with downmixing. */
896
884k
      if (ctx->disable_inv)
897
415k
         inv = 0;
898
884k
      itheta = 0;
899
884k
      itheta_q30 = 0;
900
884k
   }
901
2.42M
   qalloc = ec_tell_frac(ec) - tell;
902
2.42M
   *b -= qalloc;
903
904
2.42M
   if (itheta == 0)
905
1.04M
   {
906
1.04M
      imid = 32767;
907
1.04M
      iside = 0;
908
1.04M
      *fill &= (1<<B)-1;
909
1.04M
      delta = -16384;
910
1.38M
   } else if (itheta == 16384)
911
178k
   {
912
178k
      imid = 0;
913
178k
      iside = 32767;
914
178k
      *fill &= ((1<<B)-1)<<B;
915
178k
      delta = 16384;
916
1.20M
   } else {
917
1.20M
      imid = bitexact_cos((opus_int16)itheta);
918
1.20M
      iside = bitexact_cos((opus_int16)(16384-itheta));
919
      /* This is the mid vs side allocation that minimizes squared error
920
         in that band. */
921
1.20M
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
922
1.20M
   }
923
924
2.42M
   sctx->inv = inv;
925
2.42M
   sctx->imid = imid;
926
2.42M
   sctx->iside = iside;
927
2.42M
   sctx->delta = delta;
928
2.42M
   sctx->itheta = itheta;
929
#ifdef ENABLE_QEXT
930
   sctx->itheta_q30 = itheta_q30;
931
#endif
932
2.42M
   sctx->qalloc = qalloc;
933
2.42M
}
bands.c:compute_theta
Line
Count
Source
704
193M
{
705
193M
   int qn;
706
193M
   int itheta=0;
707
193M
   int itheta_q30=0;
708
193M
   int delta;
709
193M
   int imid, iside;
710
193M
   int qalloc;
711
193M
   int pulse_cap;
712
193M
   int offset;
713
193M
   opus_int32 tell;
714
193M
   int inv=0;
715
193M
   int encode;
716
193M
   const CELTMode *m;
717
193M
   int i;
718
193M
   int intensity;
719
193M
   ec_ctx *ec;
720
193M
   const celt_ener *bandE;
721
722
193M
   encode = ctx->encode;
723
193M
   m = ctx->m;
724
193M
   i = ctx->i;
725
193M
   intensity = ctx->intensity;
726
193M
   ec = ctx->ec;
727
193M
   bandE = ctx->bandE;
728
729
   /* Decide on the resolution to give to the split parameter theta */
730
193M
   pulse_cap = m->logN[i]+LM*(1<<BITRES);
731
193M
   offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
732
193M
   qn = compute_qn(N, *b, offset, pulse_cap, stereo);
733
193M
   if (stereo && i>=intensity)
734
97.4M
      qn = 1;
735
193M
   if (encode)
736
192M
   {
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
192M
      itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch);
742
192M
      itheta = itheta_q30>>16;
743
192M
   }
744
193M
   tell = ec_tell_frac(ec);
745
193M
   if (qn!=1)
746
93.3M
   {
747
93.3M
      if (encode)
748
92.8M
      {
749
92.8M
         if (!stereo || ctx->theta_round == 0)
750
90.8M
         {
751
90.8M
            itheta = (itheta*(opus_int32)qn+8192)>>14;
752
90.8M
            if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
753
53.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
53.3k
               int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
758
53.3k
               imid = bitexact_cos((opus_int16)unquantized);
759
53.3k
               iside = bitexact_cos((opus_int16)(16384-unquantized));
760
53.3k
               delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
761
53.3k
               if (delta > *b)
762
318
                  itheta = qn;
763
53.0k
               else if (delta < -*b)
764
290
                  itheta = 0;
765
53.3k
            }
766
90.8M
         } else {
767
2.01M
            int down;
768
            /* Bias quantization towards itheta=0 and itheta=16384. */
769
2.01M
            int bias = itheta > 8192 ? 32767/qn : -32767/qn;
770
2.01M
            down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
771
2.01M
            if (ctx->theta_round < 0)
772
1.00M
               itheta = down;
773
1.00M
            else
774
1.00M
               itheta = down+1;
775
2.01M
         }
776
92.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
93.3M
      if (stereo && N>2)
780
16.5M
      {
781
16.5M
         int p0 = 3;
782
16.5M
         int x = itheta;
783
16.5M
         int x0 = qn/2;
784
16.5M
         int ft = p0*(x0+1) + x0;
785
         /* Use a probability of p0 up to itheta=8192 and then use 1 after */
786
16.5M
         if (encode)
787
16.5M
         {
788
16.5M
            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
16.5M
         } else {
790
43.4k
            int fs;
791
43.4k
            fs=ec_decode(ec,ft);
792
43.4k
            if (fs<(x0+1)*p0)
793
29.6k
               x=fs/p0;
794
13.7k
            else
795
13.7k
               x=x0+1+(fs-(x0+1)*p0);
796
43.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
43.4k
            itheta = x;
798
43.4k
         }
799
76.7M
      } else if (B0>1 || stereo) {
800
         /* Uniform pdf */
801
5.65M
         if (encode)
802
5.48M
            ec_enc_uint(ec, itheta, qn+1);
803
173k
         else
804
173k
            itheta = ec_dec_uint(ec, qn+1);
805
71.1M
      } else {
806
71.1M
         int fs=1, ft;
807
71.1M
         ft = ((qn>>1)+1)*((qn>>1)+1);
808
71.1M
         if (encode)
809
70.8M
         {
810
70.8M
            int fl;
811
812
70.8M
            fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
813
70.8M
            fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
814
70.8M
             ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
815
816
70.8M
            ec_encode(ec, fl, fl+fs, ft);
817
70.8M
         } else {
818
            /* Triangular pdf */
819
242k
            int fl=0;
820
242k
            int fm;
821
242k
            fm = ec_decode(ec, ft);
822
823
242k
            if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
824
123k
            {
825
123k
               itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
826
123k
               fs = itheta + 1;
827
123k
               fl = itheta*(itheta + 1)>>1;
828
123k
            }
829
118k
            else
830
118k
            {
831
118k
               itheta = (2*(qn + 1)
832
118k
                - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
833
118k
               fs = qn + 1 - itheta;
834
118k
               fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
835
118k
            }
836
837
242k
            ec_dec_update(ec, fl, fl+fs, ft);
838
242k
         }
839
71.1M
      }
840
93.3M
      celt_assert(itheta>=0);
841
93.3M
      itheta = celt_udiv((opus_int32)itheta*16384, qn);
842
93.3M
#ifdef ENABLE_QEXT
843
93.3M
      *ext_b = IMIN(*ext_b, ctx->ext_total_bits - (opus_int32)ec_tell_frac(ctx->ext_ec));
844
93.3M
      if (*ext_b >= 2*N<<BITRES && ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec)-1 > 2<<BITRES) {
845
1.47M
         int extra_bits;
846
1.47M
         int ext_tell = ec_tell_frac(ctx->ext_ec);
847
1.47M
         extra_bits = IMIN(12, IMAX(2, celt_sudiv(*ext_b, (2*N-1)<<BITRES)));
848
1.47M
         if (encode) {
849
1.45M
            itheta_q30 = itheta_q30 - (itheta<<16);
850
1.45M
            itheta_q30 = (itheta_q30*(opus_int64)qn*((1<<extra_bits)-1)+(1<<29))>>30;
851
1.45M
            itheta_q30 += (1<<(extra_bits-1))-1;
852
1.45M
            itheta_q30 = IMAX(0, IMIN((1<<extra_bits)-2, itheta_q30));
853
1.45M
            ec_enc_uint(ctx->ext_ec, itheta_q30, (1<<extra_bits)-1);
854
1.45M
         } else {
855
27.9k
            itheta_q30 = ec_dec_uint(ctx->ext_ec, (1<<extra_bits)-1);
856
27.9k
         }
857
1.47M
         itheta_q30 -= (1<<(extra_bits-1))-1;
858
1.47M
         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
1.47M
         itheta_q30 = IMAX(0, IMIN(itheta_q30, 1073741824));
861
1.47M
         *ext_b -= ec_tell_frac(ctx->ext_ec) - ext_tell;
862
91.8M
      } else {
863
91.8M
         itheta_q30 = (opus_int32)itheta<<16;
864
91.8M
      }
865
93.3M
#endif
866
93.3M
      if (encode && stereo)
867
18.0M
      {
868
18.0M
         if (itheta==0)
869
16.2M
            intensity_stereo(m, X, Y, bandE, i, N);
870
1.82M
         else
871
1.82M
            stereo_split(X, Y, N);
872
18.0M
      }
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
99.7M
   } else if (stereo) {
876
99.7M
      if (encode)
877
99.1M
      {
878
99.1M
         inv = itheta > 8192 && !ctx->disable_inv;
879
99.1M
         if (inv)
880
91.1k
         {
881
91.1k
            int j;
882
1.89M
            for (j=0;j<N;j++)
883
1.80M
               Y[j] = -Y[j];
884
91.1k
         }
885
99.1M
         intensity_stereo(m, X, Y, bandE, i, N);
886
99.1M
      }
887
99.7M
      if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
888
6.97M
      {
889
6.97M
         if (encode)
890
6.88M
            ec_enc_bit_logp(ec, inv, 2);
891
90.1k
         else
892
90.1k
            inv = ec_dec_bit_logp(ec, 2);
893
6.97M
      } else
894
92.7M
         inv = 0;
895
      /* inv flag override to avoid problems with downmixing. */
896
99.7M
      if (ctx->disable_inv)
897
40.8M
         inv = 0;
898
99.7M
      itheta = 0;
899
99.7M
      itheta_q30 = 0;
900
99.7M
   }
901
193M
   qalloc = ec_tell_frac(ec) - tell;
902
193M
   *b -= qalloc;
903
904
193M
   if (itheta == 0)
905
177M
   {
906
177M
      imid = 32767;
907
177M
      iside = 0;
908
177M
      *fill &= (1<<B)-1;
909
177M
      delta = -16384;
910
177M
   } else if (itheta == 16384)
911
497k
   {
912
497k
      imid = 0;
913
497k
      iside = 32767;
914
497k
      *fill &= ((1<<B)-1)<<B;
915
497k
      delta = 16384;
916
15.0M
   } else {
917
15.0M
      imid = bitexact_cos((opus_int16)itheta);
918
15.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
15.0M
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
922
15.0M
   }
923
924
193M
   sctx->inv = inv;
925
193M
   sctx->imid = imid;
926
193M
   sctx->iside = iside;
927
193M
   sctx->delta = delta;
928
193M
   sctx->itheta = itheta;
929
193M
#ifdef ENABLE_QEXT
930
193M
   sctx->itheta_q30 = itheta_q30;
931
193M
#endif
932
193M
   sctx->qalloc = qalloc;
933
193M
}
934
static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y,
935
      celt_norm *lowband_out)
936
554M
{
937
554M
   int c;
938
554M
   int stereo;
939
554M
   celt_norm *x = X;
940
554M
   int encode;
941
554M
   ec_ctx *ec;
942
943
554M
   encode = ctx->encode;
944
554M
   ec = ctx->ec;
945
946
554M
   stereo = Y != NULL;
947
590M
   c=0; do {
948
590M
      int sign=0;
949
590M
      if (ctx->remaining_bits>=1<<BITRES)
950
90.7M
      {
951
90.7M
         if (encode)
952
90.1M
         {
953
90.1M
            sign = x[0]<0;
954
90.1M
            ec_enc_bits(ec, sign, 1);
955
90.1M
         } else {
956
583k
            sign = ec_dec_bits(ec, 1);
957
583k
         }
958
90.7M
         ctx->remaining_bits -= 1<<BITRES;
959
90.7M
      }
960
590M
      if (ctx->resynth)
961
3.97M
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
962
590M
      x = Y;
963
590M
   } while (++c<1+stereo);
964
554M
   if (lowband_out)
965
554M
      lowband_out[0] = SHR32(X[0],4);
966
554M
   return 1;
967
554M
}
bands.c:quant_band_n1
Line
Count
Source
936
277M
{
937
277M
   int c;
938
277M
   int stereo;
939
277M
   celt_norm *x = X;
940
277M
   int encode;
941
277M
   ec_ctx *ec;
942
943
277M
   encode = ctx->encode;
944
277M
   ec = ctx->ec;
945
946
277M
   stereo = Y != NULL;
947
295M
   c=0; do {
948
295M
      int sign=0;
949
295M
      if (ctx->remaining_bits>=1<<BITRES)
950
45.3M
      {
951
45.3M
         if (encode)
952
45.0M
         {
953
45.0M
            sign = x[0]<0;
954
45.0M
            ec_enc_bits(ec, sign, 1);
955
45.0M
         } else {
956
291k
            sign = ec_dec_bits(ec, 1);
957
291k
         }
958
45.3M
         ctx->remaining_bits -= 1<<BITRES;
959
45.3M
      }
960
295M
      if (ctx->resynth)
961
1.98M
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
962
295M
      x = Y;
963
295M
   } while (++c<1+stereo);
964
277M
   if (lowband_out)
965
277M
      lowband_out[0] = SHR32(X[0],4);
966
277M
   return 1;
967
277M
}
bands.c:quant_band_n1
Line
Count
Source
936
277M
{
937
277M
   int c;
938
277M
   int stereo;
939
277M
   celt_norm *x = X;
940
277M
   int encode;
941
277M
   ec_ctx *ec;
942
943
277M
   encode = ctx->encode;
944
277M
   ec = ctx->ec;
945
946
277M
   stereo = Y != NULL;
947
295M
   c=0; do {
948
295M
      int sign=0;
949
295M
      if (ctx->remaining_bits>=1<<BITRES)
950
45.3M
      {
951
45.3M
         if (encode)
952
45.0M
         {
953
45.0M
            sign = x[0]<0;
954
45.0M
            ec_enc_bits(ec, sign, 1);
955
45.0M
         } else {
956
291k
            sign = ec_dec_bits(ec, 1);
957
291k
         }
958
45.3M
         ctx->remaining_bits -= 1<<BITRES;
959
45.3M
      }
960
295M
      if (ctx->resynth)
961
1.98M
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
962
295M
      x = Y;
963
295M
   } while (++c<1+stereo);
964
277M
   if (lowband_out)
965
277M
      lowband_out[0] = SHR32(X[0],4);
966
277M
   return 1;
967
277M
}
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
1.87G
{
979
1.87G
   const unsigned char *cache;
980
1.87G
   int q;
981
1.87G
   int curr_bits;
982
1.87G
   int imid=0, iside=0;
983
1.87G
   int B0=B;
984
1.87G
   opus_val32 mid=0, side=0;
985
1.87G
   unsigned cm=0;
986
1.87G
   celt_norm *Y=NULL;
987
1.87G
   int encode;
988
1.87G
   const CELTMode *m;
989
1.87G
   int i;
990
1.87G
   int spread;
991
1.87G
   ec_ctx *ec;
992
993
1.87G
   encode = ctx->encode;
994
1.87G
   m = ctx->m;
995
1.87G
   i = ctx->i;
996
1.87G
   spread = ctx->spread;
997
1.87G
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
1.87G
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
1.87G
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
152M
   {
1003
152M
      int mbits, sbits, delta;
1004
152M
      int itheta;
1005
152M
      int qalloc;
1006
152M
      struct split_ctx sctx;
1007
152M
      celt_norm *next_lowband2=NULL;
1008
152M
      opus_int32 rebalance;
1009
1010
152M
      N >>= 1;
1011
152M
      Y = X+N;
1012
152M
      LM -= 1;
1013
152M
      if (B==1)
1014
142M
         fill = (fill&1)|(fill<<1);
1015
152M
      B = (B+1)>>1;
1016
1017
152M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
152M
      imid = sctx.imid;
1019
152M
      iside = sctx.iside;
1020
152M
      delta = sctx.delta;
1021
152M
      itheta = sctx.itheta;
1022
152M
      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
1.07M
      mid = SHL32(EXTEND32(imid), 16);
1031
1.07M
      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
152M
      if (B0>1 && (itheta&0x3fff))
1047
5.00M
      {
1048
5.00M
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
2.29M
            delta -= delta>>(4-LM);
1051
2.71M
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
2.71M
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
5.00M
      }
1055
152M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
152M
      sbits = b-mbits;
1057
152M
      ctx->remaining_bits -= qalloc;
1058
1059
152M
      if (lowband)
1060
2.64M
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
152M
      rebalance = ctx->remaining_bits;
1063
152M
      if (mbits >= sbits)
1064
140M
      {
1065
140M
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
140M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
140M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
140M
         if (rebalance > 3<<BITRES && itheta!=0)
1069
5.65M
            sbits += rebalance - (3<<BITRES);
1070
140M
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
140M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
140M
      } else {
1073
12.4M
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
12.4M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
12.4M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
12.4M
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
3.37M
            mbits += rebalance - (3<<BITRES);
1078
12.4M
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
12.4M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
12.4M
      }
1081
1.71G
   } else {
1082
#ifdef ENABLE_QEXT
1083
      int extra_bits;
1084
      int ext_remaining_bits;
1085
1.70G
      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.70G
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1088
1.69G
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1089
1.69G
         extra_bits = IMAX(extra_bits-1, 0);
1090
1.69G
      }
1091
1.70G
      extra_bits = IMIN(12, extra_bits);
1092
#endif
1093
      /* This is the basic no-split case */
1094
1.71G
      q = bits2pulses(m, i, LM, b);
1095
1.71G
      curr_bits = pulses2bits(m, i, LM, q);
1096
1.71G
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
1.73G
      while (ctx->remaining_bits < 0 && q > 0)
1100
13.2M
      {
1101
13.2M
         ctx->remaining_bits += curr_bits;
1102
13.2M
         q--;
1103
13.2M
         curr_bits = pulses2bits(m, i, LM, q);
1104
13.2M
         ctx->remaining_bits -= curr_bits;
1105
13.2M
      }
1106
1107
1.71G
      if (q!=0)
1108
363M
      {
1109
357M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
363M
         if (encode)
1113
360M
         {
1114
360M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
360M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
360M
                           ctx->arch);
1117
360M
         } else {
1118
3.41M
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
3.41M
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
3.41M
         }
1121
#ifdef ENABLE_QEXT
1122
1.34G
      } else if (ext_b > 2*N<<BITRES)
1123
959k
      {
1124
959k
         extra_bits = ext_b/(N-1)>>BITRES;
1125
959k
         ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec);
1126
959k
         if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1127
73.8k
            extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1128
73.8k
            extra_bits = IMAX(extra_bits-1, 0);
1129
73.8k
         }
1130
959k
         extra_bits = IMIN(14, extra_bits);
1131
959k
         if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth);
1132
19.2k
         else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain);
1133
#endif
1134
1.35G
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
1.35G
         int j;
1137
1.35G
         if (ctx->resynth)
1138
115M
         {
1139
115M
            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
115M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
115M
            fill &= cm_mask;
1144
115M
            if (!fill)
1145
57.5M
            {
1146
57.5M
               OPUS_CLEAR(X, N);
1147
58.3M
            } else {
1148
58.3M
               if (lowband == NULL)
1149
3.77M
               {
1150
                  /* Noise */
1151
31.9M
                  for (j=0;j<N;j++)
1152
28.1M
                  {
1153
28.1M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
28.1M
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
28.1M
                  }
1156
3.77M
                  cm = cm_mask;
1157
54.6M
               } else {
1158
                  /* Folded spectrum */
1159
861M
                  for (j=0;j<N;j++)
1160
807M
                  {
1161
807M
                     opus_val16 tmp;
1162
807M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
807M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
807M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
807M
                     X[j] = lowband[j]+tmp;
1167
807M
                  }
1168
54.6M
                  cm = fill;
1169
54.6M
               }
1170
58.3M
               renormalise_vector(X, N, gain, ctx->arch);
1171
58.3M
            }
1172
115M
         }
1173
1.35G
      }
1174
1.71G
   }
1175
1176
1.87G
   return cm;
1177
1.87G
}
bands.c:quant_partition
Line
Count
Source
978
7.17M
{
979
7.17M
   const unsigned char *cache;
980
7.17M
   int q;
981
7.17M
   int curr_bits;
982
7.17M
   int imid=0, iside=0;
983
7.17M
   int B0=B;
984
7.17M
   opus_val32 mid=0, side=0;
985
7.17M
   unsigned cm=0;
986
7.17M
   celt_norm *Y=NULL;
987
7.17M
   int encode;
988
7.17M
   const CELTMode *m;
989
7.17M
   int i;
990
7.17M
   int spread;
991
7.17M
   ec_ctx *ec;
992
993
7.17M
   encode = ctx->encode;
994
7.17M
   m = ctx->m;
995
7.17M
   i = ctx->i;
996
7.17M
   spread = ctx->spread;
997
7.17M
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
7.17M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
7.17M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
1.07M
   {
1003
1.07M
      int mbits, sbits, delta;
1004
1.07M
      int itheta;
1005
1.07M
      int qalloc;
1006
1.07M
      struct split_ctx sctx;
1007
1.07M
      celt_norm *next_lowband2=NULL;
1008
1.07M
      opus_int32 rebalance;
1009
1010
1.07M
      N >>= 1;
1011
1.07M
      Y = X+N;
1012
1.07M
      LM -= 1;
1013
1.07M
      if (B==1)
1014
324k
         fill = (fill&1)|(fill<<1);
1015
1.07M
      B = (B+1)>>1;
1016
1017
1.07M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
1.07M
      imid = sctx.imid;
1019
1.07M
      iside = sctx.iside;
1020
1.07M
      delta = sctx.delta;
1021
1.07M
      itheta = sctx.itheta;
1022
1.07M
      qalloc = sctx.qalloc;
1023
1.07M
#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
1.07M
      mid = SHL32(EXTEND32(imid), 16);
1031
1.07M
      side = SHL32(EXTEND32(iside), 16);
1032
1.07M
# endif
1033
#else
1034
# ifdef ENABLE_QEXT
1035
      (void)imid;
1036
      (void)iside;
1037
      mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1038
      side = celt_cos_norm2(1.f-sctx.itheta_q30*(1.f/(1<<30)));
1039
# else
1040
      mid = (1.f/32768)*imid;
1041
      side = (1.f/32768)*iside;
1042
# endif
1043
#endif
1044
1045
      /* Give more bits to low-energy MDCTs than they would otherwise deserve */
1046
1.07M
      if (B0>1 && (itheta&0x3fff))
1047
571k
      {
1048
571k
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
293k
            delta -= delta>>(4-LM);
1051
278k
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
278k
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
571k
      }
1055
1.07M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
1.07M
      sbits = b-mbits;
1057
1.07M
      ctx->remaining_bits -= qalloc;
1058
1059
1.07M
      if (lowband)
1060
417k
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
1.07M
      rebalance = ctx->remaining_bits;
1063
1.07M
      if (mbits >= sbits)
1064
482k
      {
1065
482k
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
482k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
482k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
482k
         if (rebalance > 3<<BITRES && itheta!=0)
1069
160k
            sbits += rebalance - (3<<BITRES);
1070
482k
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
482k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
589k
      } else {
1073
589k
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
589k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
589k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
589k
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
190k
            mbits += rebalance - (3<<BITRES);
1078
589k
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
589k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
589k
      }
1081
6.09M
   } 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
6.09M
      q = bits2pulses(m, i, LM, b);
1095
6.09M
      curr_bits = pulses2bits(m, i, LM, q);
1096
6.09M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
6.15M
      while (ctx->remaining_bits < 0 && q > 0)
1100
60.7k
      {
1101
60.7k
         ctx->remaining_bits += curr_bits;
1102
60.7k
         q--;
1103
60.7k
         curr_bits = pulses2bits(m, i, LM, q);
1104
60.7k
         ctx->remaining_bits -= curr_bits;
1105
60.7k
      }
1106
1107
6.09M
      if (q!=0)
1108
3.09M
      {
1109
3.09M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
3.09M
         if (encode)
1113
2.37M
         {
1114
2.37M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
2.37M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
2.37M
                           ctx->arch);
1117
2.37M
         } else {
1118
727k
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
727k
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
727k
         }
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
3.09M
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
2.99M
         int j;
1137
2.99M
         if (ctx->resynth)
1138
2.30M
         {
1139
2.30M
            unsigned cm_mask;
1140
            /* B can be as large as 16, so this shift might overflow an int on a
1141
               16-bit platform; use a long to get defined behavior.*/
1142
2.30M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
2.30M
            fill &= cm_mask;
1144
2.30M
            if (!fill)
1145
737k
            {
1146
737k
               OPUS_CLEAR(X, N);
1147
1.56M
            } else {
1148
1.56M
               if (lowband == NULL)
1149
63.8k
               {
1150
                  /* Noise */
1151
979k
                  for (j=0;j<N;j++)
1152
915k
                  {
1153
915k
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
915k
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
915k
                  }
1156
63.8k
                  cm = cm_mask;
1157
1.50M
               } else {
1158
                  /* Folded spectrum */
1159
21.8M
                  for (j=0;j<N;j++)
1160
20.3M
                  {
1161
20.3M
                     opus_val16 tmp;
1162
20.3M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
20.3M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
20.3M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
20.3M
                     X[j] = lowband[j]+tmp;
1167
20.3M
                  }
1168
1.50M
                  cm = fill;
1169
1.50M
               }
1170
1.56M
               renormalise_vector(X, N, gain, ctx->arch);
1171
1.56M
            }
1172
2.30M
         }
1173
2.99M
      }
1174
6.09M
   }
1175
1176
7.17M
   return cm;
1177
7.17M
}
bands.c:quant_partition
Line
Count
Source
978
928M
{
979
928M
   const unsigned char *cache;
980
928M
   int q;
981
928M
   int curr_bits;
982
928M
   int imid=0, iside=0;
983
928M
   int B0=B;
984
928M
   opus_val32 mid=0, side=0;
985
928M
   unsigned cm=0;
986
928M
   celt_norm *Y=NULL;
987
928M
   int encode;
988
928M
   const CELTMode *m;
989
928M
   int i;
990
928M
   int spread;
991
928M
   ec_ctx *ec;
992
993
928M
   encode = ctx->encode;
994
928M
   m = ctx->m;
995
928M
   i = ctx->i;
996
928M
   spread = ctx->spread;
997
928M
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
928M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
928M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
75.2M
   {
1003
75.2M
      int mbits, sbits, delta;
1004
75.2M
      int itheta;
1005
75.2M
      int qalloc;
1006
75.2M
      struct split_ctx sctx;
1007
75.2M
      celt_norm *next_lowband2=NULL;
1008
75.2M
      opus_int32 rebalance;
1009
1010
75.2M
      N >>= 1;
1011
75.2M
      Y = X+N;
1012
75.2M
      LM -= 1;
1013
75.2M
      if (B==1)
1014
71.1M
         fill = (fill&1)|(fill<<1);
1015
75.2M
      B = (B+1)>>1;
1016
1017
75.2M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
75.2M
      imid = sctx.imid;
1019
75.2M
      iside = sctx.iside;
1020
75.2M
      delta = sctx.delta;
1021
75.2M
      itheta = sctx.itheta;
1022
75.2M
      qalloc = sctx.qalloc;
1023
75.2M
#ifdef FIXED_POINT
1024
75.2M
# ifdef ENABLE_QEXT
1025
75.2M
      (void)imid;
1026
75.2M
      (void)iside;
1027
75.2M
      mid = celt_cos_norm32(sctx.itheta_q30);
1028
75.2M
      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
75.2M
      if (B0>1 && (itheta&0x3fff))
1047
1.92M
      {
1048
1.92M
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
851k
            delta -= delta>>(4-LM);
1051
1.07M
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
1.07M
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
1.92M
      }
1055
75.2M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
75.2M
      sbits = b-mbits;
1057
75.2M
      ctx->remaining_bits -= qalloc;
1058
1059
75.2M
      if (lowband)
1060
904k
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
75.2M
      rebalance = ctx->remaining_bits;
1063
75.2M
      if (mbits >= sbits)
1064
69.6M
      {
1065
69.6M
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
69.6M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
69.6M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
69.6M
         if (rebalance > 3<<BITRES && itheta!=0)
1069
2.66M
            sbits += rebalance - (3<<BITRES);
1070
69.6M
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
69.6M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
69.6M
      } else {
1073
5.61M
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
5.61M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
5.61M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
5.61M
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
1.49M
            mbits += rebalance - (3<<BITRES);
1078
5.61M
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
5.61M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
5.61M
      }
1081
853M
   } else {
1082
853M
#ifdef ENABLE_QEXT
1083
853M
      int extra_bits;
1084
853M
      int ext_remaining_bits;
1085
853M
      extra_bits = ext_b/(N-1)>>BITRES;
1086
853M
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1087
853M
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1088
847M
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1089
847M
         extra_bits = IMAX(extra_bits-1, 0);
1090
847M
      }
1091
853M
      extra_bits = IMIN(12, extra_bits);
1092
853M
#endif
1093
      /* This is the basic no-split case */
1094
853M
      q = bits2pulses(m, i, LM, b);
1095
853M
      curr_bits = pulses2bits(m, i, LM, q);
1096
853M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
860M
      while (ctx->remaining_bits < 0 && q > 0)
1100
6.57M
      {
1101
6.57M
         ctx->remaining_bits += curr_bits;
1102
6.57M
         q--;
1103
6.57M
         curr_bits = pulses2bits(m, i, LM, q);
1104
6.57M
         ctx->remaining_bits -= curr_bits;
1105
6.57M
      }
1106
1107
853M
      if (q!=0)
1108
178M
      {
1109
178M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
178M
         if (encode)
1113
177M
         {
1114
177M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
177M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
177M
                           ctx->arch);
1117
177M
         } else {
1118
979k
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
979k
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
979k
         }
1121
178M
#ifdef ENABLE_QEXT
1122
674M
      } else if (ext_b > 2*N<<BITRES)
1123
479k
      {
1124
479k
         extra_bits = ext_b/(N-1)>>BITRES;
1125
479k
         ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec);
1126
479k
         if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1127
36.9k
            extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1128
36.9k
            extra_bits = IMAX(extra_bits-1, 0);
1129
36.9k
         }
1130
479k
         extra_bits = IMIN(14, extra_bits);
1131
479k
         if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth);
1132
9.60k
         else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain);
1133
479k
#endif
1134
674M
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
674M
         int j;
1137
674M
         if (ctx->resynth)
1138
55.6M
         {
1139
55.6M
            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
55.6M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
55.6M
            fill &= cm_mask;
1144
55.6M
            if (!fill)
1145
28.0M
            {
1146
28.0M
               OPUS_CLEAR(X, N);
1147
28.0M
            } else {
1148
27.6M
               if (lowband == NULL)
1149
1.82M
               {
1150
                  /* Noise */
1151
15.0M
                  for (j=0;j<N;j++)
1152
13.1M
                  {
1153
13.1M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
13.1M
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
13.1M
                  }
1156
1.82M
                  cm = cm_mask;
1157
25.8M
               } else {
1158
                  /* Folded spectrum */
1159
409M
                  for (j=0;j<N;j++)
1160
383M
                  {
1161
383M
                     opus_val16 tmp;
1162
383M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
383M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
383M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
383M
                     X[j] = lowband[j]+tmp;
1167
383M
                  }
1168
25.8M
                  cm = fill;
1169
25.8M
               }
1170
27.6M
               renormalise_vector(X, N, gain, ctx->arch);
1171
27.6M
            }
1172
55.6M
         }
1173
674M
      }
1174
853M
   }
1175
1176
928M
   return cm;
1177
928M
}
bands.c:quant_partition
Line
Count
Source
978
928M
{
979
928M
   const unsigned char *cache;
980
928M
   int q;
981
928M
   int curr_bits;
982
928M
   int imid=0, iside=0;
983
928M
   int B0=B;
984
928M
   opus_val32 mid=0, side=0;
985
928M
   unsigned cm=0;
986
928M
   celt_norm *Y=NULL;
987
928M
   int encode;
988
928M
   const CELTMode *m;
989
928M
   int i;
990
928M
   int spread;
991
928M
   ec_ctx *ec;
992
993
928M
   encode = ctx->encode;
994
928M
   m = ctx->m;
995
928M
   i = ctx->i;
996
928M
   spread = ctx->spread;
997
928M
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
928M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
928M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
75.2M
   {
1003
75.2M
      int mbits, sbits, delta;
1004
75.2M
      int itheta;
1005
75.2M
      int qalloc;
1006
75.2M
      struct split_ctx sctx;
1007
75.2M
      celt_norm *next_lowband2=NULL;
1008
75.2M
      opus_int32 rebalance;
1009
1010
75.2M
      N >>= 1;
1011
75.2M
      Y = X+N;
1012
75.2M
      LM -= 1;
1013
75.2M
      if (B==1)
1014
71.1M
         fill = (fill&1)|(fill<<1);
1015
75.2M
      B = (B+1)>>1;
1016
1017
75.2M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
75.2M
      imid = sctx.imid;
1019
75.2M
      iside = sctx.iside;
1020
75.2M
      delta = sctx.delta;
1021
75.2M
      itheta = sctx.itheta;
1022
75.2M
      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
75.2M
# ifdef ENABLE_QEXT
1035
75.2M
      (void)imid;
1036
75.2M
      (void)iside;
1037
75.2M
      mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1038
75.2M
      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
75.2M
#endif
1044
1045
      /* Give more bits to low-energy MDCTs than they would otherwise deserve */
1046
75.2M
      if (B0>1 && (itheta&0x3fff))
1047
1.92M
      {
1048
1.92M
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
851k
            delta -= delta>>(4-LM);
1051
1.07M
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
1.07M
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
1.92M
      }
1055
75.2M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
75.2M
      sbits = b-mbits;
1057
75.2M
      ctx->remaining_bits -= qalloc;
1058
1059
75.2M
      if (lowband)
1060
904k
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
75.2M
      rebalance = ctx->remaining_bits;
1063
75.2M
      if (mbits >= sbits)
1064
69.6M
      {
1065
69.6M
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
69.6M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
69.6M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
69.6M
         if (rebalance > 3<<BITRES && itheta!=0)
1069
2.66M
            sbits += rebalance - (3<<BITRES);
1070
69.6M
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
69.6M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
69.6M
      } else {
1073
5.61M
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
5.61M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
5.61M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
5.61M
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
1.49M
            mbits += rebalance - (3<<BITRES);
1078
5.61M
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
5.61M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
5.61M
      }
1081
853M
   } else {
1082
853M
#ifdef ENABLE_QEXT
1083
853M
      int extra_bits;
1084
853M
      int ext_remaining_bits;
1085
853M
      extra_bits = ext_b/(N-1)>>BITRES;
1086
853M
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1087
853M
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1088
847M
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1089
847M
         extra_bits = IMAX(extra_bits-1, 0);
1090
847M
      }
1091
853M
      extra_bits = IMIN(12, extra_bits);
1092
853M
#endif
1093
      /* This is the basic no-split case */
1094
853M
      q = bits2pulses(m, i, LM, b);
1095
853M
      curr_bits = pulses2bits(m, i, LM, q);
1096
853M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
860M
      while (ctx->remaining_bits < 0 && q > 0)
1100
6.57M
      {
1101
6.57M
         ctx->remaining_bits += curr_bits;
1102
6.57M
         q--;
1103
6.57M
         curr_bits = pulses2bits(m, i, LM, q);
1104
6.57M
         ctx->remaining_bits -= curr_bits;
1105
6.57M
      }
1106
1107
853M
      if (q!=0)
1108
178M
      {
1109
178M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
178M
         if (encode)
1113
177M
         {
1114
177M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
177M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
177M
                           ctx->arch);
1117
177M
         } else {
1118
979k
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
979k
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
979k
         }
1121
178M
#ifdef ENABLE_QEXT
1122
674M
      } else if (ext_b > 2*N<<BITRES)
1123
479k
      {
1124
479k
         extra_bits = ext_b/(N-1)>>BITRES;
1125
479k
         ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec);
1126
479k
         if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1127
36.9k
            extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1128
36.9k
            extra_bits = IMAX(extra_bits-1, 0);
1129
36.9k
         }
1130
479k
         extra_bits = IMIN(14, extra_bits);
1131
479k
         if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth);
1132
9.60k
         else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain);
1133
479k
#endif
1134
674M
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
674M
         int j;
1137
674M
         if (ctx->resynth)
1138
55.6M
         {
1139
55.6M
            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
55.6M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
55.6M
            fill &= cm_mask;
1144
55.6M
            if (!fill)
1145
28.0M
            {
1146
28.0M
               OPUS_CLEAR(X, N);
1147
28.0M
            } else {
1148
27.6M
               if (lowband == NULL)
1149
1.82M
               {
1150
                  /* Noise */
1151
15.0M
                  for (j=0;j<N;j++)
1152
13.1M
                  {
1153
13.1M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
13.1M
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
13.1M
                  }
1156
1.82M
                  cm = cm_mask;
1157
25.8M
               } else {
1158
                  /* Folded spectrum */
1159
409M
                  for (j=0;j<N;j++)
1160
383M
                  {
1161
383M
                     opus_val16 tmp;
1162
383M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
383M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
383M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
383M
                     X[j] = lowband[j]+tmp;
1167
383M
                  }
1168
25.8M
                  cm = fill;
1169
25.8M
               }
1170
27.6M
               renormalise_vector(X, N, gain, ctx->arch);
1171
27.6M
            }
1172
55.6M
         }
1173
674M
      }
1174
853M
   }
1175
1176
928M
   return cm;
1177
928M
}
bands.c:quant_partition
Line
Count
Source
978
7.17M
{
979
7.17M
   const unsigned char *cache;
980
7.17M
   int q;
981
7.17M
   int curr_bits;
982
7.17M
   int imid=0, iside=0;
983
7.17M
   int B0=B;
984
7.17M
   opus_val32 mid=0, side=0;
985
7.17M
   unsigned cm=0;
986
7.17M
   celt_norm *Y=NULL;
987
7.17M
   int encode;
988
7.17M
   const CELTMode *m;
989
7.17M
   int i;
990
7.17M
   int spread;
991
7.17M
   ec_ctx *ec;
992
993
7.17M
   encode = ctx->encode;
994
7.17M
   m = ctx->m;
995
7.17M
   i = ctx->i;
996
7.17M
   spread = ctx->spread;
997
7.17M
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
7.17M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
7.17M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
1.07M
   {
1003
1.07M
      int mbits, sbits, delta;
1004
1.07M
      int itheta;
1005
1.07M
      int qalloc;
1006
1.07M
      struct split_ctx sctx;
1007
1.07M
      celt_norm *next_lowband2=NULL;
1008
1.07M
      opus_int32 rebalance;
1009
1010
1.07M
      N >>= 1;
1011
1.07M
      Y = X+N;
1012
1.07M
      LM -= 1;
1013
1.07M
      if (B==1)
1014
324k
         fill = (fill&1)|(fill<<1);
1015
1.07M
      B = (B+1)>>1;
1016
1017
1.07M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
1.07M
      imid = sctx.imid;
1019
1.07M
      iside = sctx.iside;
1020
1.07M
      delta = sctx.delta;
1021
1.07M
      itheta = sctx.itheta;
1022
1.07M
      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
1.07M
      mid = (1.f/32768)*imid;
1041
1.07M
      side = (1.f/32768)*iside;
1042
1.07M
# endif
1043
1.07M
#endif
1044
1045
      /* Give more bits to low-energy MDCTs than they would otherwise deserve */
1046
1.07M
      if (B0>1 && (itheta&0x3fff))
1047
571k
      {
1048
571k
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
293k
            delta -= delta>>(4-LM);
1051
278k
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
278k
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
571k
      }
1055
1.07M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
1.07M
      sbits = b-mbits;
1057
1.07M
      ctx->remaining_bits -= qalloc;
1058
1059
1.07M
      if (lowband)
1060
417k
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
1.07M
      rebalance = ctx->remaining_bits;
1063
1.07M
      if (mbits >= sbits)
1064
482k
      {
1065
482k
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
482k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
482k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
482k
         if (rebalance > 3<<BITRES && itheta!=0)
1069
160k
            sbits += rebalance - (3<<BITRES);
1070
482k
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
482k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
589k
      } else {
1073
589k
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
589k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
589k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
589k
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
190k
            mbits += rebalance - (3<<BITRES);
1078
589k
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
589k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
589k
      }
1081
6.09M
   } 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
6.09M
      q = bits2pulses(m, i, LM, b);
1095
6.09M
      curr_bits = pulses2bits(m, i, LM, q);
1096
6.09M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
6.15M
      while (ctx->remaining_bits < 0 && q > 0)
1100
60.7k
      {
1101
60.7k
         ctx->remaining_bits += curr_bits;
1102
60.7k
         q--;
1103
60.7k
         curr_bits = pulses2bits(m, i, LM, q);
1104
60.7k
         ctx->remaining_bits -= curr_bits;
1105
60.7k
      }
1106
1107
6.09M
      if (q!=0)
1108
3.09M
      {
1109
3.09M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
3.09M
         if (encode)
1113
2.37M
         {
1114
2.37M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
2.37M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
2.37M
                           ctx->arch);
1117
2.37M
         } else {
1118
727k
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
727k
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
727k
         }
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
3.09M
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
2.99M
         int j;
1137
2.99M
         if (ctx->resynth)
1138
2.30M
         {
1139
2.30M
            unsigned cm_mask;
1140
            /* B can be as large as 16, so this shift might overflow an int on a
1141
               16-bit platform; use a long to get defined behavior.*/
1142
2.30M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
2.30M
            fill &= cm_mask;
1144
2.30M
            if (!fill)
1145
737k
            {
1146
737k
               OPUS_CLEAR(X, N);
1147
1.56M
            } else {
1148
1.56M
               if (lowband == NULL)
1149
63.8k
               {
1150
                  /* Noise */
1151
979k
                  for (j=0;j<N;j++)
1152
915k
                  {
1153
915k
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
915k
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
915k
                  }
1156
63.8k
                  cm = cm_mask;
1157
1.50M
               } else {
1158
                  /* Folded spectrum */
1159
21.8M
                  for (j=0;j<N;j++)
1160
20.3M
                  {
1161
20.3M
                     opus_val16 tmp;
1162
20.3M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
20.3M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
20.3M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
20.3M
                     X[j] = lowband[j]+tmp;
1167
20.3M
                  }
1168
1.50M
                  cm = fill;
1169
1.50M
               }
1170
1.56M
               renormalise_vector(X, N, gain, ctx->arch);
1171
1.56M
            }
1172
2.30M
         }
1173
2.99M
      }
1174
6.09M
   }
1175
1176
7.17M
   return cm;
1177
7.17M
}
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
187k
{
1182
187k
   celt_assert(LM>=0);
1183
187k
   ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1184
187k
   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
187k
   if (LM==0 || b<=2*N<<BITRES) {
1187
108k
      int res, ret;
1188
108k
      b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits);
1189
      /* Resolution left after taking into account coding the cube face. */
1190
108k
      res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES;
1191
108k
      res = IMIN(14, IMAX(0, res));
1192
108k
      if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth);
1193
108k
      else ret = cubic_unquant(X, N, res, B, ec, gain);
1194
108k
      ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1195
108k
      return ret;
1196
108k
   } else {
1197
78.7k
      celt_norm *Y;
1198
78.7k
      opus_int32 itheta_q30;
1199
78.7k
      opus_val32 g1, g2;
1200
78.7k
      opus_int32 theta_res;
1201
78.7k
      opus_int32 qtheta;
1202
78.7k
      int delta;
1203
78.7k
      int b1, b2;
1204
78.7k
      int cm;
1205
78.7k
      int N0;
1206
78.7k
      N0 = N;
1207
78.7k
      N >>= 1;
1208
78.7k
      Y = X+N;
1209
78.7k
      LM -= 1;
1210
78.7k
      B = (B+1)>>1;
1211
78.7k
      theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1);
1212
78.7k
      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
78.4k
      } else {
1217
78.4k
         qtheta = ec_dec_uint(ec, (1<<theta_res)+1);
1218
78.4k
      }
1219
78.7k
      itheta_q30 = qtheta<<(30-theta_res);
1220
78.7k
      b -= theta_res<<BITRES;
1221
78.7k
      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
78.7k
      if (itheta_q30 == 0) {
1231
2.45k
         b1=b;
1232
2.45k
         b2=0;
1233
76.2k
      } else if (itheta_q30==1073741824) {
1234
2.73k
         b1=0;
1235
2.73k
         b2=b;
1236
73.5k
      } else {
1237
73.5k
         b1 = IMIN(b, IMAX(0, (b-delta)/2));
1238
73.5k
         b2 = b-b1;
1239
73.5k
      }
1240
78.7k
      cm  = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode);
1241
78.7k
      cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode);
1242
78.7k
      return cm;
1243
78.7k
   }
1244
187k
}
bands.c:cubic_quant_partition
Line
Count
Source
1181
93.6k
{
1182
93.6k
   celt_assert(LM>=0);
1183
93.6k
   ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1184
93.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
93.6k
   if (LM==0 || b<=2*N<<BITRES) {
1187
54.2k
      int res, ret;
1188
54.2k
      b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits);
1189
      /* Resolution left after taking into account coding the cube face. */
1190
54.2k
      res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES;
1191
54.2k
      res = IMIN(14, IMAX(0, res));
1192
54.2k
      if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth);
1193
54.0k
      else ret = cubic_unquant(X, N, res, B, ec, gain);
1194
54.2k
      ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1195
54.2k
      return ret;
1196
54.2k
   } else {
1197
39.3k
      celt_norm *Y;
1198
39.3k
      opus_int32 itheta_q30;
1199
39.3k
      opus_val32 g1, g2;
1200
39.3k
      opus_int32 theta_res;
1201
39.3k
      opus_int32 qtheta;
1202
39.3k
      int delta;
1203
39.3k
      int b1, b2;
1204
39.3k
      int cm;
1205
39.3k
      int N0;
1206
39.3k
      N0 = N;
1207
39.3k
      N >>= 1;
1208
39.3k
      Y = X+N;
1209
39.3k
      LM -= 1;
1210
39.3k
      B = (B+1)>>1;
1211
39.3k
      theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1);
1212
39.3k
      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
39.2k
      } else {
1217
39.2k
         qtheta = ec_dec_uint(ec, (1<<theta_res)+1);
1218
39.2k
      }
1219
39.3k
      itheta_q30 = qtheta<<(30-theta_res);
1220
39.3k
      b -= theta_res<<BITRES;
1221
39.3k
      delta = (N0-1) * 23 * ((itheta_q30>>16)-8192) >> (17-BITRES);
1222
1223
39.3k
#ifdef FIXED_POINT
1224
39.3k
      g1 = celt_cos_norm32(itheta_q30);
1225
39.3k
      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
39.3k
      if (itheta_q30 == 0) {
1231
1.22k
         b1=b;
1232
1.22k
         b2=0;
1233
38.1k
      } else if (itheta_q30==1073741824) {
1234
1.36k
         b1=0;
1235
1.36k
         b2=b;
1236
36.7k
      } else {
1237
36.7k
         b1 = IMIN(b, IMAX(0, (b-delta)/2));
1238
36.7k
         b2 = b-b1;
1239
36.7k
      }
1240
39.3k
      cm  = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode);
1241
39.3k
      cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode);
1242
39.3k
      return cm;
1243
39.3k
   }
1244
93.6k
}
bands.c:cubic_quant_partition
Line
Count
Source
1181
93.6k
{
1182
93.6k
   celt_assert(LM>=0);
1183
93.6k
   ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1184
93.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
93.6k
   if (LM==0 || b<=2*N<<BITRES) {
1187
54.2k
      int res, ret;
1188
54.2k
      b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits);
1189
      /* Resolution left after taking into account coding the cube face. */
1190
54.2k
      res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES;
1191
54.2k
      res = IMIN(14, IMAX(0, res));
1192
54.2k
      if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth);
1193
54.0k
      else ret = cubic_unquant(X, N, res, B, ec, gain);
1194
54.2k
      ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1195
54.2k
      return ret;
1196
54.2k
   } else {
1197
39.3k
      celt_norm *Y;
1198
39.3k
      opus_int32 itheta_q30;
1199
39.3k
      opus_val32 g1, g2;
1200
39.3k
      opus_int32 theta_res;
1201
39.3k
      opus_int32 qtheta;
1202
39.3k
      int delta;
1203
39.3k
      int b1, b2;
1204
39.3k
      int cm;
1205
39.3k
      int N0;
1206
39.3k
      N0 = N;
1207
39.3k
      N >>= 1;
1208
39.3k
      Y = X+N;
1209
39.3k
      LM -= 1;
1210
39.3k
      B = (B+1)>>1;
1211
39.3k
      theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1);
1212
39.3k
      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
39.2k
      } else {
1217
39.2k
         qtheta = ec_dec_uint(ec, (1<<theta_res)+1);
1218
39.2k
      }
1219
39.3k
      itheta_q30 = qtheta<<(30-theta_res);
1220
39.3k
      b -= theta_res<<BITRES;
1221
39.3k
      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
39.3k
      g1 = celt_cos_norm2(itheta_q30*(1.f/(1<<30)));
1228
39.3k
      g2 = celt_cos_norm2(1.f-itheta_q30*(1.f/(1<<30)));
1229
39.3k
#endif
1230
39.3k
      if (itheta_q30 == 0) {
1231
1.22k
         b1=b;
1232
1.22k
         b2=0;
1233
38.1k
      } else if (itheta_q30==1073741824) {
1234
1.36k
         b1=0;
1235
1.36k
         b2=b;
1236
36.7k
      } else {
1237
36.7k
         b1 = IMIN(b, IMAX(0, (b-delta)/2));
1238
36.7k
         b2 = b-b1;
1239
36.7k
      }
1240
39.3k
      cm  = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode);
1241
39.3k
      cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode);
1242
39.3k
      return cm;
1243
39.3k
   }
1244
93.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
2.08G
{
1254
2.08G
   int N0=N;
1255
2.08G
   int N_B=N;
1256
2.08G
   int N_B0;
1257
2.08G
   int B0=B;
1258
2.08G
   int time_divide=0;
1259
2.08G
   int recombine=0;
1260
2.08G
   int longBlocks;
1261
2.08G
   unsigned cm=0;
1262
2.08G
   int k;
1263
2.08G
   int encode;
1264
2.08G
   int tf_change;
1265
1266
2.08G
   encode = ctx->encode;
1267
2.08G
   tf_change = ctx->tf_change;
1268
1269
2.08G
   longBlocks = B0==1;
1270
1271
2.08G
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
2.08G
   if (N==1)
1275
517M
   {
1276
517M
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
517M
   }
1278
1279
1.56G
   if (tf_change>0)
1280
12.5M
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
1.56G
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
2.68M
   {
1285
2.68M
      OPUS_COPY(lowband_scratch, lowband, N);
1286
2.68M
      lowband = lowband_scratch;
1287
2.68M
   }
1288
1289
1.59G
   for (k=0;k<recombine;k++)
1290
24.0M
   {
1291
24.0M
      static const unsigned char bit_interleave_table[16]={
1292
24.0M
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
24.0M
      };
1294
24.0M
      if (encode)
1295
22.0M
         haar1(X, N>>k, 1<<k);
1296
24.0M
      if (lowband)
1297
1.45M
         haar1(lowband, N>>k, 1<<k);
1298
24.0M
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
24.0M
   }
1300
1.56G
   B>>=recombine;
1301
1.56G
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
1.57G
   while ((N_B&1) == 0 && tf_change<0)
1305
5.91M
   {
1306
5.91M
      if (encode)
1307
4.74M
         haar1(X, N_B, B);
1308
5.91M
      if (lowband)
1309
1.50M
         haar1(lowband, N_B, B);
1310
5.91M
      fill |= fill<<B;
1311
5.91M
      B <<= 1;
1312
5.91M
      N_B >>= 1;
1313
5.91M
      time_divide++;
1314
5.91M
      tf_change++;
1315
5.91M
   }
1316
1.56G
   B0=B;
1317
1.56G
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
1.56G
   if (B0>1)
1321
25.2M
   {
1322
25.2M
      if (encode)
1323
24.0M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
25.2M
      if (lowband)
1325
2.08M
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
25.2M
   }
1327
1328
#ifdef ENABLE_QEXT
1329
1.55G
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1330
29.7k
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1331
29.7k
   } else
1332
1.55G
#endif
1333
1.55G
   {
1334
1.55G
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
1.55G
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
1.56G
   if (ctx->resynth)
1339
126M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
126M
      if (B0>1)
1342
3.67M
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
126M
      N_B = N_B0;
1346
126M
      B = B0;
1347
128M
      for (k=0;k<time_divide;k++)
1348
2.52M
      {
1349
2.52M
         B >>= 1;
1350
2.52M
         N_B <<= 1;
1351
2.52M
         cm |= cm>>B;
1352
2.52M
         haar1(X, N_B, B);
1353
2.52M
      }
1354
1355
128M
      for (k=0;k<recombine;k++)
1356
2.27M
      {
1357
2.27M
         static const unsigned char bit_deinterleave_table[16]={
1358
2.27M
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
2.27M
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
2.27M
         };
1361
2.27M
         cm = bit_deinterleave_table[cm];
1362
2.27M
         haar1(X, N0>>k, 1<<k);
1363
2.27M
      }
1364
126M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
126M
      if (lowband_out)
1368
62.9M
      {
1369
62.9M
         int j;
1370
62.9M
         opus_val16 n;
1371
62.9M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
820M
         for (j=0;j<N0;j++)
1373
757M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
62.9M
      }
1375
126M
      cm &= (1<<B)-1;
1376
126M
   }
1377
1.56G
   return cm;
1378
2.08G
}
bands.c:quant_band
Line
Count
Source
1253
5.86M
{
1254
5.86M
   int N0=N;
1255
5.86M
   int N_B=N;
1256
5.86M
   int N_B0;
1257
5.86M
   int B0=B;
1258
5.86M
   int time_divide=0;
1259
5.86M
   int recombine=0;
1260
5.86M
   int longBlocks;
1261
5.86M
   unsigned cm=0;
1262
5.86M
   int k;
1263
5.86M
   int encode;
1264
5.86M
   int tf_change;
1265
1266
5.86M
   encode = ctx->encode;
1267
5.86M
   tf_change = ctx->tf_change;
1268
1269
5.86M
   longBlocks = B0==1;
1270
1271
5.86M
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
5.86M
   if (N==1)
1275
838k
   {
1276
838k
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
838k
   }
1278
1279
5.02M
   if (tf_change>0)
1280
433k
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
5.02M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
610k
   {
1285
610k
      OPUS_COPY(lowband_scratch, lowband, N);
1286
610k
      lowband = lowband_scratch;
1287
610k
   }
1288
1289
5.76M
   for (k=0;k<recombine;k++)
1290
735k
   {
1291
735k
      static const unsigned char bit_interleave_table[16]={
1292
735k
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
735k
      };
1294
735k
      if (encode)
1295
146k
         haar1(X, N>>k, 1<<k);
1296
735k
      if (lowband)
1297
417k
         haar1(lowband, N>>k, 1<<k);
1298
735k
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
735k
   }
1300
5.02M
   B>>=recombine;
1301
5.02M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
5.85M
   while ((N_B&1) == 0 && tf_change<0)
1305
832k
   {
1306
832k
      if (encode)
1307
570k
         haar1(X, N_B, B);
1308
832k
      if (lowband)
1309
304k
         haar1(lowband, N_B, B);
1310
832k
      fill |= fill<<B;
1311
832k
      B <<= 1;
1312
832k
      N_B >>= 1;
1313
832k
      time_divide++;
1314
832k
      tf_change++;
1315
832k
   }
1316
5.02M
   B0=B;
1317
5.02M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
5.02M
   if (B0>1)
1321
1.58M
   {
1322
1.58M
      if (encode)
1323
1.32M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
1.58M
      if (lowband)
1325
433k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
1.58M
   }
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
5.02M
   {
1334
5.02M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
5.02M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
5.02M
   if (ctx->resynth)
1339
3.20M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
3.20M
      if (B0>1)
1342
754k
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
3.20M
      N_B = N_B0;
1346
3.20M
      B = B0;
1347
3.72M
      for (k=0;k<time_divide;k++)
1348
513k
      {
1349
513k
         B >>= 1;
1350
513k
         N_B <<= 1;
1351
513k
         cm |= cm>>B;
1352
513k
         haar1(X, N_B, B);
1353
513k
      }
1354
1355
3.86M
      for (k=0;k<recombine;k++)
1356
655k
      {
1357
655k
         static const unsigned char bit_deinterleave_table[16]={
1358
655k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
655k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
655k
         };
1361
655k
         cm = bit_deinterleave_table[cm];
1362
655k
         haar1(X, N0>>k, 1<<k);
1363
655k
      }
1364
3.20M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
3.20M
      if (lowband_out)
1368
2.17M
      {
1369
2.17M
         int j;
1370
2.17M
         opus_val16 n;
1371
2.17M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
23.4M
         for (j=0;j<N0;j++)
1373
21.3M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
2.17M
      }
1375
3.20M
      cm &= (1<<B)-1;
1376
3.20M
   }
1377
5.02M
   return cm;
1378
5.86M
}
bands.c:quant_band
Line
Count
Source
1253
1.03G
{
1254
1.03G
   int N0=N;
1255
1.03G
   int N_B=N;
1256
1.03G
   int N_B0;
1257
1.03G
   int B0=B;
1258
1.03G
   int time_divide=0;
1259
1.03G
   int recombine=0;
1260
1.03G
   int longBlocks;
1261
1.03G
   unsigned cm=0;
1262
1.03G
   int k;
1263
1.03G
   int encode;
1264
1.03G
   int tf_change;
1265
1266
1.03G
   encode = ctx->encode;
1267
1.03G
   tf_change = ctx->tf_change;
1268
1269
1.03G
   longBlocks = B0==1;
1270
1271
1.03G
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
1.03G
   if (N==1)
1275
258M
   {
1276
258M
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
258M
   }
1278
1279
778M
   if (tf_change>0)
1280
5.81M
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
778M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
733k
   {
1285
733k
      OPUS_COPY(lowband_scratch, lowband, N);
1286
733k
      lowband = lowband_scratch;
1287
733k
   }
1288
1289
789M
   for (k=0;k<recombine;k++)
1290
11.2M
   {
1291
11.2M
      static const unsigned char bit_interleave_table[16]={
1292
11.2M
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
11.2M
      };
1294
11.2M
      if (encode)
1295
10.9M
         haar1(X, N>>k, 1<<k);
1296
11.2M
      if (lowband)
1297
311k
         haar1(lowband, N>>k, 1<<k);
1298
11.2M
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
11.2M
   }
1300
778M
   B>>=recombine;
1301
778M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
780M
   while ((N_B&1) == 0 && tf_change<0)
1305
2.12M
   {
1306
2.12M
      if (encode)
1307
1.80M
         haar1(X, N_B, B);
1308
2.12M
      if (lowband)
1309
446k
         haar1(lowband, N_B, B);
1310
2.12M
      fill |= fill<<B;
1311
2.12M
      B <<= 1;
1312
2.12M
      N_B >>= 1;
1313
2.12M
      time_divide++;
1314
2.12M
      tf_change++;
1315
2.12M
   }
1316
778M
   B0=B;
1317
778M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
778M
   if (B0>1)
1321
11.0M
   {
1322
11.0M
      if (encode)
1323
10.7M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
11.0M
      if (lowband)
1325
609k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
11.0M
   }
1327
1328
778M
#ifdef ENABLE_QEXT
1329
778M
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1330
14.8k
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1331
14.8k
   } else
1332
778M
#endif
1333
778M
   {
1334
778M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
778M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
778M
   if (ctx->resynth)
1339
59.8M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
59.8M
      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
59.8M
      N_B = N_B0;
1346
59.8M
      B = B0;
1347
60.6M
      for (k=0;k<time_divide;k++)
1348
751k
      {
1349
751k
         B >>= 1;
1350
751k
         N_B <<= 1;
1351
751k
         cm |= cm>>B;
1352
751k
         haar1(X, N_B, B);
1353
751k
      }
1354
1355
60.3M
      for (k=0;k<recombine;k++)
1356
484k
      {
1357
484k
         static const unsigned char bit_deinterleave_table[16]={
1358
484k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
484k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
484k
         };
1361
484k
         cm = bit_deinterleave_table[cm];
1362
484k
         haar1(X, N0>>k, 1<<k);
1363
484k
      }
1364
59.8M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
59.8M
      if (lowband_out)
1368
29.3M
      {
1369
29.3M
         int j;
1370
29.3M
         opus_val16 n;
1371
29.3M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
386M
         for (j=0;j<N0;j++)
1373
357M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
29.3M
      }
1375
59.8M
      cm &= (1<<B)-1;
1376
59.8M
   }
1377
778M
   return cm;
1378
1.03G
}
bands.c:quant_band
Line
Count
Source
1253
1.03G
{
1254
1.03G
   int N0=N;
1255
1.03G
   int N_B=N;
1256
1.03G
   int N_B0;
1257
1.03G
   int B0=B;
1258
1.03G
   int time_divide=0;
1259
1.03G
   int recombine=0;
1260
1.03G
   int longBlocks;
1261
1.03G
   unsigned cm=0;
1262
1.03G
   int k;
1263
1.03G
   int encode;
1264
1.03G
   int tf_change;
1265
1266
1.03G
   encode = ctx->encode;
1267
1.03G
   tf_change = ctx->tf_change;
1268
1269
1.03G
   longBlocks = B0==1;
1270
1271
1.03G
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
1.03G
   if (N==1)
1275
258M
   {
1276
258M
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
258M
   }
1278
1279
778M
   if (tf_change>0)
1280
5.81M
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
778M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
733k
   {
1285
733k
      OPUS_COPY(lowband_scratch, lowband, N);
1286
733k
      lowband = lowband_scratch;
1287
733k
   }
1288
1289
789M
   for (k=0;k<recombine;k++)
1290
11.2M
   {
1291
11.2M
      static const unsigned char bit_interleave_table[16]={
1292
11.2M
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
11.2M
      };
1294
11.2M
      if (encode)
1295
10.9M
         haar1(X, N>>k, 1<<k);
1296
11.2M
      if (lowband)
1297
311k
         haar1(lowband, N>>k, 1<<k);
1298
11.2M
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
11.2M
   }
1300
778M
   B>>=recombine;
1301
778M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
780M
   while ((N_B&1) == 0 && tf_change<0)
1305
2.12M
   {
1306
2.12M
      if (encode)
1307
1.80M
         haar1(X, N_B, B);
1308
2.12M
      if (lowband)
1309
446k
         haar1(lowband, N_B, B);
1310
2.12M
      fill |= fill<<B;
1311
2.12M
      B <<= 1;
1312
2.12M
      N_B >>= 1;
1313
2.12M
      time_divide++;
1314
2.12M
      tf_change++;
1315
2.12M
   }
1316
778M
   B0=B;
1317
778M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
778M
   if (B0>1)
1321
11.0M
   {
1322
11.0M
      if (encode)
1323
10.7M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
11.0M
      if (lowband)
1325
609k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
11.0M
   }
1327
1328
778M
#ifdef ENABLE_QEXT
1329
778M
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1330
14.8k
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1331
14.8k
   } else
1332
778M
#endif
1333
778M
   {
1334
778M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
778M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
778M
   if (ctx->resynth)
1339
59.8M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
59.8M
      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
59.8M
      N_B = N_B0;
1346
59.8M
      B = B0;
1347
60.6M
      for (k=0;k<time_divide;k++)
1348
751k
      {
1349
751k
         B >>= 1;
1350
751k
         N_B <<= 1;
1351
751k
         cm |= cm>>B;
1352
751k
         haar1(X, N_B, B);
1353
751k
      }
1354
1355
60.3M
      for (k=0;k<recombine;k++)
1356
484k
      {
1357
484k
         static const unsigned char bit_deinterleave_table[16]={
1358
484k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
484k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
484k
         };
1361
484k
         cm = bit_deinterleave_table[cm];
1362
484k
         haar1(X, N0>>k, 1<<k);
1363
484k
      }
1364
59.8M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
59.8M
      if (lowband_out)
1368
29.3M
      {
1369
29.3M
         int j;
1370
29.3M
         opus_val16 n;
1371
29.3M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
386M
         for (j=0;j<N0;j++)
1373
357M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
29.3M
      }
1375
59.8M
      cm &= (1<<B)-1;
1376
59.8M
   }
1377
778M
   return cm;
1378
1.03G
}
bands.c:quant_band
Line
Count
Source
1253
5.86M
{
1254
5.86M
   int N0=N;
1255
5.86M
   int N_B=N;
1256
5.86M
   int N_B0;
1257
5.86M
   int B0=B;
1258
5.86M
   int time_divide=0;
1259
5.86M
   int recombine=0;
1260
5.86M
   int longBlocks;
1261
5.86M
   unsigned cm=0;
1262
5.86M
   int k;
1263
5.86M
   int encode;
1264
5.86M
   int tf_change;
1265
1266
5.86M
   encode = ctx->encode;
1267
5.86M
   tf_change = ctx->tf_change;
1268
1269
5.86M
   longBlocks = B0==1;
1270
1271
5.86M
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
5.86M
   if (N==1)
1275
838k
   {
1276
838k
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
838k
   }
1278
1279
5.02M
   if (tf_change>0)
1280
433k
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
5.02M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
610k
   {
1285
610k
      OPUS_COPY(lowband_scratch, lowband, N);
1286
610k
      lowband = lowband_scratch;
1287
610k
   }
1288
1289
5.76M
   for (k=0;k<recombine;k++)
1290
735k
   {
1291
735k
      static const unsigned char bit_interleave_table[16]={
1292
735k
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
735k
      };
1294
735k
      if (encode)
1295
146k
         haar1(X, N>>k, 1<<k);
1296
735k
      if (lowband)
1297
417k
         haar1(lowband, N>>k, 1<<k);
1298
735k
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
735k
   }
1300
5.02M
   B>>=recombine;
1301
5.02M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
5.85M
   while ((N_B&1) == 0 && tf_change<0)
1305
832k
   {
1306
832k
      if (encode)
1307
570k
         haar1(X, N_B, B);
1308
832k
      if (lowband)
1309
304k
         haar1(lowband, N_B, B);
1310
832k
      fill |= fill<<B;
1311
832k
      B <<= 1;
1312
832k
      N_B >>= 1;
1313
832k
      time_divide++;
1314
832k
      tf_change++;
1315
832k
   }
1316
5.02M
   B0=B;
1317
5.02M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
5.02M
   if (B0>1)
1321
1.58M
   {
1322
1.58M
      if (encode)
1323
1.32M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
1.58M
      if (lowband)
1325
433k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
1.58M
   }
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
5.02M
   {
1334
5.02M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
5.02M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
5.02M
   if (ctx->resynth)
1339
3.20M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
3.20M
      if (B0>1)
1342
754k
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
3.20M
      N_B = N_B0;
1346
3.20M
      B = B0;
1347
3.72M
      for (k=0;k<time_divide;k++)
1348
513k
      {
1349
513k
         B >>= 1;
1350
513k
         N_B <<= 1;
1351
513k
         cm |= cm>>B;
1352
513k
         haar1(X, N_B, B);
1353
513k
      }
1354
1355
3.86M
      for (k=0;k<recombine;k++)
1356
655k
      {
1357
655k
         static const unsigned char bit_deinterleave_table[16]={
1358
655k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
655k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
655k
         };
1361
655k
         cm = bit_deinterleave_table[cm];
1362
655k
         haar1(X, N0>>k, 1<<k);
1363
655k
      }
1364
3.20M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
3.20M
      if (lowband_out)
1368
2.17M
      {
1369
2.17M
         int j;
1370
2.17M
         opus_val16 n;
1371
2.17M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
23.4M
         for (j=0;j<N0;j++)
1373
21.3M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
2.17M
      }
1375
3.20M
      cm &= (1<<B)-1;
1376
3.20M
   }
1377
5.02M
   return cm;
1378
5.86M
}
1379
1380
#ifdef FIXED_POINT
1381
241M
#define MIN_STEREO_ENERGY 2
1382
#else
1383
241M
#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
274M
{
1393
274M
   int imid=0, iside=0;
1394
274M
   int inv = 0;
1395
274M
   opus_val32 mid=0, side=0;
1396
274M
   unsigned cm=0;
1397
274M
   int mbits, sbits, delta;
1398
274M
   int itheta;
1399
274M
   int qalloc;
1400
274M
   struct split_ctx sctx;
1401
274M
   int orig_fill;
1402
274M
   int encode;
1403
274M
   ec_ctx *ec;
1404
1405
274M
   encode = ctx->encode;
1406
274M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
274M
   if (N==1)
1410
36.2M
   {
1411
36.2M
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
36.2M
   }
1413
1414
238M
   orig_fill = fill;
1415
1416
238M
   if (encode) {
1417
235M
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
224M
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
224M
         else OPUS_COPY(X, Y, N);
1420
224M
      }
1421
235M
   }
1422
238M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
238M
   inv = sctx.inv;
1424
238M
   imid = sctx.imid;
1425
238M
   iside = sctx.iside;
1426
238M
   delta = sctx.delta;
1427
238M
   itheta = sctx.itheta;
1428
238M
   qalloc = sctx.qalloc;
1429
#ifdef FIXED_POINT
1430
# ifdef ENABLE_QEXT
1431
   (void)imid;
1432
   (void)iside;
1433
   mid = celt_cos_norm32(sctx.itheta_q30);
1434
   side = celt_cos_norm32((1<<30)-sctx.itheta_q30);
1435
# else
1436
1.35M
   mid = SHL32(EXTEND32(imid), 16);
1437
1.35M
   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
238M
   if (N==2)
1455
51.4M
   {
1456
51.4M
      int c;
1457
51.4M
      int sign=0;
1458
51.4M
      celt_norm *x2, *y2;
1459
51.4M
      mbits = b;
1460
51.4M
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
51.4M
      if (itheta != 0 && itheta != 16384)
1463
341k
         sbits = 1<<BITRES;
1464
51.4M
      mbits -= sbits;
1465
51.4M
      c = itheta > 8192;
1466
51.4M
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
51.4M
      x2 = c ? Y : X;
1469
51.4M
      y2 = c ? X : Y;
1470
51.4M
      if (sbits)
1471
341k
      {
1472
341k
         if (encode)
1473
294k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
294k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
294k
            ec_enc_bits(ec, sign, 1);
1478
294k
         } else {
1479
46.7k
            sign = ec_dec_bits(ec, 1);
1480
46.7k
         }
1481
341k
      }
1482
51.4M
      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
51.4M
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
51.4M
            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
51.4M
      y2[0] = -sign*x2[1];
1490
51.4M
      y2[1] = sign*x2[0];
1491
51.4M
      if (ctx->resynth)
1492
3.41M
      {
1493
3.41M
         celt_norm tmp;
1494
3.41M
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
3.41M
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
3.41M
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
3.41M
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
3.41M
         tmp = X[0];
1499
3.41M
         X[0] = SUB32(tmp,Y[0]);
1500
3.41M
         Y[0] = ADD32(tmp,Y[0]);
1501
3.41M
         tmp = X[1];
1502
3.41M
         X[1] = SUB32(tmp,Y[1]);
1503
3.41M
         Y[1] = ADD32(tmp,Y[1]);
1504
3.41M
      }
1505
186M
   } else {
1506
      /* "Normal" split code */
1507
186M
      opus_int32 rebalance;
1508
1509
186M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
186M
      sbits = b-mbits;
1511
186M
      ctx->remaining_bits -= qalloc;
1512
1513
186M
      rebalance = ctx->remaining_bits;
1514
186M
      if (mbits >= sbits)
1515
185M
      {
1516
#ifdef ENABLE_QEXT
1517
         int qext_extra = 0;
1518
         /* Reallocate any mid bits that cannot be used to extra mid bits. */
1519
183M
         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
185M
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
185M
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
185M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
185M
         if (rebalance > 3<<BITRES && itheta!=0)
1527
121k
            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
183M
         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
185M
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
185M
      } else {
1536
#ifdef ENABLE_QEXT
1537
         int qext_extra = 0;
1538
         /* Reallocate any side bits that cannot be used to extra side bits. */
1539
1.28M
         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.56M
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
1.56M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
1.56M
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
41.8k
            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
1.28M
         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.56M
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
1.56M
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
1.56M
      }
1556
186M
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
238M
   if (ctx->resynth)
1561
62.1M
   {
1562
62.1M
      if (N!=2)
1563
58.7M
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
62.1M
      if (inv)
1565
153k
      {
1566
153k
         int j;
1567
2.55M
         for (j=0;j<N;j++)
1568
2.39M
            Y[j] = -Y[j];
1569
153k
      }
1570
62.1M
   }
1571
238M
   return cm;
1572
274M
}
bands.c:quant_band_stereo
Line
Count
Source
1392
1.65M
{
1393
1.65M
   int imid=0, iside=0;
1394
1.65M
   int inv = 0;
1395
1.65M
   opus_val32 mid=0, side=0;
1396
1.65M
   unsigned cm=0;
1397
1.65M
   int mbits, sbits, delta;
1398
1.65M
   int itheta;
1399
1.65M
   int qalloc;
1400
1.65M
   struct split_ctx sctx;
1401
1.65M
   int orig_fill;
1402
1.65M
   int encode;
1403
1.65M
   ec_ctx *ec;
1404
1405
1.65M
   encode = ctx->encode;
1406
1.65M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
1.65M
   if (N==1)
1410
303k
   {
1411
303k
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
303k
   }
1413
1414
1.35M
   orig_fill = fill;
1415
1416
1.35M
   if (encode) {
1417
687k
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
80.8k
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
72.4k
         else OPUS_COPY(X, Y, N);
1420
80.8k
      }
1421
687k
   }
1422
1.35M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
1.35M
   inv = sctx.inv;
1424
1.35M
   imid = sctx.imid;
1425
1.35M
   iside = sctx.iside;
1426
1.35M
   delta = sctx.delta;
1427
1.35M
   itheta = sctx.itheta;
1428
1.35M
   qalloc = sctx.qalloc;
1429
1.35M
#ifdef FIXED_POINT
1430
# ifdef ENABLE_QEXT
1431
   (void)imid;
1432
   (void)iside;
1433
   mid = celt_cos_norm32(sctx.itheta_q30);
1434
   side = celt_cos_norm32((1<<30)-sctx.itheta_q30);
1435
# else
1436
1.35M
   mid = SHL32(EXTEND32(imid), 16);
1437
1.35M
   side = SHL32(EXTEND32(iside), 16);
1438
1.35M
# endif
1439
#else
1440
# ifdef ENABLE_QEXT
1441
   (void)imid;
1442
   (void)iside;
1443
   mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1444
   side = celt_cos_norm2(1.f-sctx.itheta_q30*(1.f/(1<<30)));
1445
# else
1446
   mid = (1.f/32768)*imid;
1447
   side = (1.f/32768)*iside;
1448
# endif
1449
#endif
1450
1451
   /* This is a special case for N=2 that only works for stereo and takes
1452
      advantage of the fact that mid and side are orthogonal to encode
1453
      the side with just one bit. */
1454
1.35M
   if (N==2)
1455
333k
   {
1456
333k
      int c;
1457
333k
      int sign=0;
1458
333k
      celt_norm *x2, *y2;
1459
333k
      mbits = b;
1460
333k
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
333k
      if (itheta != 0 && itheta != 16384)
1463
76.1k
         sbits = 1<<BITRES;
1464
333k
      mbits -= sbits;
1465
333k
      c = itheta > 8192;
1466
333k
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
333k
      x2 = c ? Y : X;
1469
333k
      y2 = c ? X : Y;
1470
333k
      if (sbits)
1471
76.1k
      {
1472
76.1k
         if (encode)
1473
65.9k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
65.9k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
65.9k
            ec_enc_bits(ec, sign, 1);
1478
65.9k
         } else {
1479
10.1k
            sign = ec_dec_bits(ec, 1);
1480
10.1k
         }
1481
76.1k
      }
1482
333k
      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
333k
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
333k
            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
333k
      y2[0] = -sign*x2[1];
1490
333k
      y2[1] = sign*x2[0];
1491
333k
      if (ctx->resynth)
1492
286k
      {
1493
286k
         celt_norm tmp;
1494
286k
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
286k
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
286k
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
286k
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
286k
         tmp = X[0];
1499
286k
         X[0] = SUB32(tmp,Y[0]);
1500
286k
         Y[0] = ADD32(tmp,Y[0]);
1501
286k
         tmp = X[1];
1502
286k
         X[1] = SUB32(tmp,Y[1]);
1503
286k
         Y[1] = ADD32(tmp,Y[1]);
1504
286k
      }
1505
1.01M
   } else {
1506
      /* "Normal" split code */
1507
1.01M
      opus_int32 rebalance;
1508
1509
1.01M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
1.01M
      sbits = b-mbits;
1511
1.01M
      ctx->remaining_bits -= qalloc;
1512
1513
1.01M
      rebalance = ctx->remaining_bits;
1514
1.01M
      if (mbits >= sbits)
1515
874k
      {
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
874k
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
874k
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
874k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
874k
         if (rebalance > 3<<BITRES && itheta!=0)
1527
13.0k
            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
874k
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
874k
      } 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
144k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
144k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
144k
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
9.24k
            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
144k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
144k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
144k
      }
1556
1.01M
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
1.35M
   if (ctx->resynth)
1561
1.13M
   {
1562
1.13M
      if (N!=2)
1563
850k
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
1.13M
      if (inv)
1565
30.5k
      {
1566
30.5k
         int j;
1567
471k
         for (j=0;j<N;j++)
1568
440k
            Y[j] = -Y[j];
1569
30.5k
      }
1570
1.13M
   }
1571
1.35M
   return cm;
1572
1.65M
}
bands.c:quant_band_stereo
Line
Count
Source
1392
135M
{
1393
135M
   int imid=0, iside=0;
1394
135M
   int inv = 0;
1395
135M
   opus_val32 mid=0, side=0;
1396
135M
   unsigned cm=0;
1397
135M
   int mbits, sbits, delta;
1398
135M
   int itheta;
1399
135M
   int qalloc;
1400
135M
   struct split_ctx sctx;
1401
135M
   int orig_fill;
1402
135M
   int encode;
1403
135M
   ec_ctx *ec;
1404
1405
135M
   encode = ctx->encode;
1406
135M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
135M
   if (N==1)
1410
17.8M
   {
1411
17.8M
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
17.8M
   }
1413
1414
117M
   orig_fill = fill;
1415
1416
117M
   if (encode) {
1417
117M
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
111M
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
111M
         else OPUS_COPY(X, Y, N);
1420
111M
      }
1421
117M
   }
1422
117M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
117M
   inv = sctx.inv;
1424
117M
   imid = sctx.imid;
1425
117M
   iside = sctx.iside;
1426
117M
   delta = sctx.delta;
1427
117M
   itheta = sctx.itheta;
1428
117M
   qalloc = sctx.qalloc;
1429
117M
#ifdef FIXED_POINT
1430
117M
# ifdef ENABLE_QEXT
1431
117M
   (void)imid;
1432
117M
   (void)iside;
1433
117M
   mid = celt_cos_norm32(sctx.itheta_q30);
1434
117M
   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
117M
   if (N==2)
1455
25.3M
   {
1456
25.3M
      int c;
1457
25.3M
      int sign=0;
1458
25.3M
      celt_norm *x2, *y2;
1459
25.3M
      mbits = b;
1460
25.3M
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
25.3M
      if (itheta != 0 && itheta != 16384)
1463
94.4k
         sbits = 1<<BITRES;
1464
25.3M
      mbits -= sbits;
1465
25.3M
      c = itheta > 8192;
1466
25.3M
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
25.3M
      x2 = c ? Y : X;
1469
25.3M
      y2 = c ? X : Y;
1470
25.3M
      if (sbits)
1471
94.4k
      {
1472
94.4k
         if (encode)
1473
81.2k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
81.2k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
81.2k
            ec_enc_bits(ec, sign, 1);
1478
81.2k
         } else {
1479
13.2k
            sign = ec_dec_bits(ec, 1);
1480
13.2k
         }
1481
94.4k
      }
1482
25.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
25.3M
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
25.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
25.3M
      y2[0] = -sign*x2[1];
1490
25.3M
      y2[1] = sign*x2[0];
1491
25.3M
      if (ctx->resynth)
1492
1.41M
      {
1493
1.41M
         celt_norm tmp;
1494
1.41M
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
1.41M
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
1.41M
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
1.41M
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
1.41M
         tmp = X[0];
1499
1.41M
         X[0] = SUB32(tmp,Y[0]);
1500
1.41M
         Y[0] = ADD32(tmp,Y[0]);
1501
1.41M
         tmp = X[1];
1502
1.41M
         X[1] = SUB32(tmp,Y[1]);
1503
1.41M
         Y[1] = ADD32(tmp,Y[1]);
1504
1.41M
      }
1505
92.4M
   } else {
1506
      /* "Normal" split code */
1507
92.4M
      opus_int32 rebalance;
1508
1509
92.4M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
92.4M
      sbits = b-mbits;
1511
92.4M
      ctx->remaining_bits -= qalloc;
1512
1513
92.4M
      rebalance = ctx->remaining_bits;
1514
92.4M
      if (mbits >= sbits)
1515
91.8M
      {
1516
91.8M
#ifdef ENABLE_QEXT
1517
91.8M
         int qext_extra = 0;
1518
         /* Reallocate any mid bits that cannot be used to extra mid bits. */
1519
91.8M
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, mbits - cap[ctx->i]/2));
1520
91.8M
#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
91.8M
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
91.8M
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
91.8M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
91.8M
         if (rebalance > 3<<BITRES && itheta!=0)
1527
47.7k
            sbits += rebalance - (3<<BITRES);
1528
91.8M
#ifdef ENABLE_QEXT
1529
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the mid. */
1530
91.8M
         if (ctx->extra_bands) sbits = IMIN(sbits, ctx->remaining_bits);
1531
91.8M
#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
91.8M
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
91.8M
      } else {
1536
640k
#ifdef ENABLE_QEXT
1537
640k
         int qext_extra = 0;
1538
         /* Reallocate any side bits that cannot be used to extra side bits. */
1539
640k
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, sbits - cap[ctx->i]/2));
1540
640k
#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
640k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
640k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
640k
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
11.6k
            mbits += rebalance - (3<<BITRES);
1547
640k
#ifdef ENABLE_QEXT
1548
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the side. */
1549
640k
         if (ctx->extra_bands) mbits = IMIN(mbits, ctx->remaining_bits);
1550
640k
#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
640k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
640k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
640k
      }
1556
92.4M
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
117M
   if (ctx->resynth)
1561
29.9M
   {
1562
29.9M
      if (N!=2)
1563
28.5M
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
29.9M
      if (inv)
1565
45.9k
      {
1566
45.9k
         int j;
1567
803k
         for (j=0;j<N;j++)
1568
757k
            Y[j] = -Y[j];
1569
45.9k
      }
1570
29.9M
   }
1571
117M
   return cm;
1572
135M
}
bands.c:quant_band_stereo
Line
Count
Source
1392
135M
{
1393
135M
   int imid=0, iside=0;
1394
135M
   int inv = 0;
1395
135M
   opus_val32 mid=0, side=0;
1396
135M
   unsigned cm=0;
1397
135M
   int mbits, sbits, delta;
1398
135M
   int itheta;
1399
135M
   int qalloc;
1400
135M
   struct split_ctx sctx;
1401
135M
   int orig_fill;
1402
135M
   int encode;
1403
135M
   ec_ctx *ec;
1404
1405
135M
   encode = ctx->encode;
1406
135M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
135M
   if (N==1)
1410
17.8M
   {
1411
17.8M
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
17.8M
   }
1413
1414
117M
   orig_fill = fill;
1415
1416
117M
   if (encode) {
1417
117M
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
111M
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
111M
         else OPUS_COPY(X, Y, N);
1420
111M
      }
1421
117M
   }
1422
117M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
117M
   inv = sctx.inv;
1424
117M
   imid = sctx.imid;
1425
117M
   iside = sctx.iside;
1426
117M
   delta = sctx.delta;
1427
117M
   itheta = sctx.itheta;
1428
117M
   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
117M
# ifdef ENABLE_QEXT
1441
117M
   (void)imid;
1442
117M
   (void)iside;
1443
117M
   mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1444
117M
   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
117M
#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
117M
   if (N==2)
1455
25.3M
   {
1456
25.3M
      int c;
1457
25.3M
      int sign=0;
1458
25.3M
      celt_norm *x2, *y2;
1459
25.3M
      mbits = b;
1460
25.3M
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
25.3M
      if (itheta != 0 && itheta != 16384)
1463
94.4k
         sbits = 1<<BITRES;
1464
25.3M
      mbits -= sbits;
1465
25.3M
      c = itheta > 8192;
1466
25.3M
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
25.3M
      x2 = c ? Y : X;
1469
25.3M
      y2 = c ? X : Y;
1470
25.3M
      if (sbits)
1471
94.4k
      {
1472
94.4k
         if (encode)
1473
81.2k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
81.2k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
81.2k
            ec_enc_bits(ec, sign, 1);
1478
81.2k
         } else {
1479
13.2k
            sign = ec_dec_bits(ec, 1);
1480
13.2k
         }
1481
94.4k
      }
1482
25.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
25.3M
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
25.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
25.3M
      y2[0] = -sign*x2[1];
1490
25.3M
      y2[1] = sign*x2[0];
1491
25.3M
      if (ctx->resynth)
1492
1.41M
      {
1493
1.41M
         celt_norm tmp;
1494
1.41M
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
1.41M
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
1.41M
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
1.41M
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
1.41M
         tmp = X[0];
1499
1.41M
         X[0] = SUB32(tmp,Y[0]);
1500
1.41M
         Y[0] = ADD32(tmp,Y[0]);
1501
1.41M
         tmp = X[1];
1502
1.41M
         X[1] = SUB32(tmp,Y[1]);
1503
1.41M
         Y[1] = ADD32(tmp,Y[1]);
1504
1.41M
      }
1505
92.4M
   } else {
1506
      /* "Normal" split code */
1507
92.4M
      opus_int32 rebalance;
1508
1509
92.4M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
92.4M
      sbits = b-mbits;
1511
92.4M
      ctx->remaining_bits -= qalloc;
1512
1513
92.4M
      rebalance = ctx->remaining_bits;
1514
92.4M
      if (mbits >= sbits)
1515
91.8M
      {
1516
91.8M
#ifdef ENABLE_QEXT
1517
91.8M
         int qext_extra = 0;
1518
         /* Reallocate any mid bits that cannot be used to extra mid bits. */
1519
91.8M
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, mbits - cap[ctx->i]/2));
1520
91.8M
#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
91.8M
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
91.8M
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
91.8M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
91.8M
         if (rebalance > 3<<BITRES && itheta!=0)
1527
47.7k
            sbits += rebalance - (3<<BITRES);
1528
91.8M
#ifdef ENABLE_QEXT
1529
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the mid. */
1530
91.8M
         if (ctx->extra_bands) sbits = IMIN(sbits, ctx->remaining_bits);
1531
91.8M
#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
91.8M
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
91.8M
      } else {
1536
640k
#ifdef ENABLE_QEXT
1537
640k
         int qext_extra = 0;
1538
         /* Reallocate any side bits that cannot be used to extra side bits. */
1539
640k
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, sbits - cap[ctx->i]/2));
1540
640k
#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
640k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
640k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
640k
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
11.6k
            mbits += rebalance - (3<<BITRES);
1547
640k
#ifdef ENABLE_QEXT
1548
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the side. */
1549
640k
         if (ctx->extra_bands) mbits = IMIN(mbits, ctx->remaining_bits);
1550
640k
#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
640k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
640k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
640k
      }
1556
92.4M
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
117M
   if (ctx->resynth)
1561
29.9M
   {
1562
29.9M
      if (N!=2)
1563
28.5M
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
29.9M
      if (inv)
1565
45.9k
      {
1566
45.9k
         int j;
1567
803k
         for (j=0;j<N;j++)
1568
757k
            Y[j] = -Y[j];
1569
45.9k
      }
1570
29.9M
   }
1571
117M
   return cm;
1572
135M
}
bands.c:quant_band_stereo
Line
Count
Source
1392
1.65M
{
1393
1.65M
   int imid=0, iside=0;
1394
1.65M
   int inv = 0;
1395
1.65M
   opus_val32 mid=0, side=0;
1396
1.65M
   unsigned cm=0;
1397
1.65M
   int mbits, sbits, delta;
1398
1.65M
   int itheta;
1399
1.65M
   int qalloc;
1400
1.65M
   struct split_ctx sctx;
1401
1.65M
   int orig_fill;
1402
1.65M
   int encode;
1403
1.65M
   ec_ctx *ec;
1404
1405
1.65M
   encode = ctx->encode;
1406
1.65M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
1.65M
   if (N==1)
1410
303k
   {
1411
303k
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
303k
   }
1413
1414
1.35M
   orig_fill = fill;
1415
1416
1.35M
   if (encode) {
1417
687k
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
80.8k
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
72.4k
         else OPUS_COPY(X, Y, N);
1420
80.8k
      }
1421
687k
   }
1422
1.35M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
1.35M
   inv = sctx.inv;
1424
1.35M
   imid = sctx.imid;
1425
1.35M
   iside = sctx.iside;
1426
1.35M
   delta = sctx.delta;
1427
1.35M
   itheta = sctx.itheta;
1428
1.35M
   qalloc = sctx.qalloc;
1429
#ifdef FIXED_POINT
1430
# ifdef ENABLE_QEXT
1431
   (void)imid;
1432
   (void)iside;
1433
   mid = celt_cos_norm32(sctx.itheta_q30);
1434
   side = celt_cos_norm32((1<<30)-sctx.itheta_q30);
1435
# else
1436
   mid = SHL32(EXTEND32(imid), 16);
1437
   side = SHL32(EXTEND32(iside), 16);
1438
# endif
1439
#else
1440
# ifdef ENABLE_QEXT
1441
   (void)imid;
1442
   (void)iside;
1443
   mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1444
   side = celt_cos_norm2(1.f-sctx.itheta_q30*(1.f/(1<<30)));
1445
# else
1446
1.35M
   mid = (1.f/32768)*imid;
1447
1.35M
   side = (1.f/32768)*iside;
1448
1.35M
# endif
1449
1.35M
#endif
1450
1451
   /* This is a special case for N=2 that only works for stereo and takes
1452
      advantage of the fact that mid and side are orthogonal to encode
1453
      the side with just one bit. */
1454
1.35M
   if (N==2)
1455
333k
   {
1456
333k
      int c;
1457
333k
      int sign=0;
1458
333k
      celt_norm *x2, *y2;
1459
333k
      mbits = b;
1460
333k
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
333k
      if (itheta != 0 && itheta != 16384)
1463
76.1k
         sbits = 1<<BITRES;
1464
333k
      mbits -= sbits;
1465
333k
      c = itheta > 8192;
1466
333k
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
333k
      x2 = c ? Y : X;
1469
333k
      y2 = c ? X : Y;
1470
333k
      if (sbits)
1471
76.1k
      {
1472
76.1k
         if (encode)
1473
65.9k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
65.9k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
65.9k
            ec_enc_bits(ec, sign, 1);
1478
65.9k
         } else {
1479
10.1k
            sign = ec_dec_bits(ec, 1);
1480
10.1k
         }
1481
76.1k
      }
1482
333k
      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
333k
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
333k
            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
333k
      y2[0] = -sign*x2[1];
1490
333k
      y2[1] = sign*x2[0];
1491
333k
      if (ctx->resynth)
1492
286k
      {
1493
286k
         celt_norm tmp;
1494
286k
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
286k
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
286k
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
286k
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
286k
         tmp = X[0];
1499
286k
         X[0] = SUB32(tmp,Y[0]);
1500
286k
         Y[0] = ADD32(tmp,Y[0]);
1501
286k
         tmp = X[1];
1502
286k
         X[1] = SUB32(tmp,Y[1]);
1503
286k
         Y[1] = ADD32(tmp,Y[1]);
1504
286k
      }
1505
1.01M
   } else {
1506
      /* "Normal" split code */
1507
1.01M
      opus_int32 rebalance;
1508
1509
1.01M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
1.01M
      sbits = b-mbits;
1511
1.01M
      ctx->remaining_bits -= qalloc;
1512
1513
1.01M
      rebalance = ctx->remaining_bits;
1514
1.01M
      if (mbits >= sbits)
1515
874k
      {
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
874k
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
874k
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
874k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
874k
         if (rebalance > 3<<BITRES && itheta!=0)
1527
13.0k
            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
874k
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
874k
      } 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
144k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
144k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
144k
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
9.24k
            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
144k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
144k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
144k
      }
1556
1.01M
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
1.35M
   if (ctx->resynth)
1561
1.13M
   {
1562
1.13M
      if (N!=2)
1563
850k
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
1.13M
      if (inv)
1565
30.5k
      {
1566
30.5k
         int j;
1567
471k
         for (j=0;j<N;j++)
1568
440k
            Y[j] = -Y[j];
1569
30.5k
      }
1570
1.13M
   }
1571
1.35M
   return cm;
1572
1.65M
}
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
67.3M
{
1577
67.3M
   int n1, n2;
1578
67.3M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1579
67.3M
   n1 = M*(eBands[start+1]-eBands[start]);
1580
67.3M
   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
67.3M
   OPUS_COPY(&norm[n1], &norm[2*n1 - n2], n2-n1);
1584
67.3M
   if (dual_stereo)
1585
253k
      OPUS_COPY(&norm2[n1], &norm2[2*n1 - n2], n2-n1);
1586
67.3M
}
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
134M
{
1598
134M
   int i;
1599
134M
   opus_int32 remaining_bits;
1600
134M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
134M
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
134M
   VARDECL(celt_norm, _norm);
1603
134M
   VARDECL(celt_norm, _lowband_scratch);
1604
134M
   VARDECL(celt_norm, X_save);
1605
134M
   VARDECL(celt_norm, Y_save);
1606
134M
   VARDECL(celt_norm, X_save2);
1607
134M
   VARDECL(celt_norm, Y_save2);
1608
134M
   VARDECL(celt_norm, norm_save2);
1609
134M
   VARDECL(unsigned char, bytes_save);
1610
134M
   int resynth_alloc;
1611
134M
   celt_norm *lowband_scratch;
1612
134M
   int B;
1613
134M
   int M;
1614
134M
   int lowband_offset;
1615
134M
   int update_lowband = 1;
1616
134M
   int C = Y_ != NULL ? 2 : 1;
1617
134M
   int norm_offset;
1618
134M
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1619
#ifdef RESYNTH
1620
   int resynth = 1;
1621
#else
1622
134M
   int resynth = !encode || theta_rdo;
1623
134M
#endif
1624
134M
   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
134M
   SAVE_STACK;
1632
1633
134M
   M = 1<<LM;
1634
134M
   B = shortBlocks ? M : 1;
1635
134M
   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
134M
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1639
134M
   norm = _norm;
1640
134M
   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
134M
   if (encode && resynth)
1646
3.83M
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1647
130M
   else
1648
130M
      resynth_alloc = ALLOC_NONE;
1649
134M
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1650
134M
   if (encode && resynth)
1651
3.83M
      lowband_scratch = _lowband_scratch;
1652
130M
   else
1653
130M
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1654
134M
   ALLOC(X_save, resynth_alloc, celt_norm);
1655
134M
   ALLOC(Y_save, resynth_alloc, celt_norm);
1656
134M
   ALLOC(X_save2, resynth_alloc, celt_norm);
1657
134M
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1658
134M
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1659
1660
134M
   lowband_offset = 0;
1661
134M
   ctx.bandE = bandE;
1662
134M
   ctx.ec = ec;
1663
134M
   ctx.encode = encode;
1664
134M
   ctx.intensity = intensity;
1665
134M
   ctx.m = m;
1666
134M
   ctx.seed = *seed;
1667
134M
   ctx.spread = spread;
1668
134M
   ctx.arch = arch;
1669
134M
   ctx.disable_inv = disable_inv;
1670
134M
   ctx.resynth = resynth;
1671
134M
   ctx.theta_round = 0;
1672
#ifdef ENABLE_QEXT
1673
   ctx.ext_ec = ext_ec;
1674
   ctx.ext_total_bits = ext_total_bits;
1675
133M
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1676
133M
   if (ctx.extra_bands) theta_rdo = 0;
1677
133M
   ALLOC(ext_bytes_save, theta_rdo ? QEXT_PACKET_SIZE_CAP : ALLOC_NONE, unsigned char);
1678
#endif
1679
134M
   ALLOC(bytes_save, theta_rdo ? 1275 : ALLOC_NONE, unsigned char);
1680
1681
   /* Avoid injecting noise in the first band on transients. */
1682
134M
   ctx.avoid_split_noise = B > 1;
1683
2.05G
   for (i=start;i<end;i++)
1684
1.92G
   {
1685
1.92G
      opus_int32 tell;
1686
1.92G
      int b;
1687
1.92G
      int N;
1688
1.92G
      opus_int32 curr_balance;
1689
1.92G
      int effective_lowband=-1;
1690
1.92G
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1691
1.92G
      int tf_change=0;
1692
1.92G
      unsigned x_cm;
1693
1.92G
      unsigned y_cm;
1694
1.92G
      int last;
1695
1696
1.92G
      ctx.i = i;
1697
1.92G
      last = (i==end-1);
1698
1699
1.92G
      X = X_+M*eBands[i];
1700
1.92G
      if (Y_!=NULL)
1701
276M
         Y = Y_+M*eBands[i];
1702
1.64G
      else
1703
1.64G
         Y = NULL;
1704
1.92G
      N = M*eBands[i+1]-M*eBands[i];
1705
1.92G
      celt_assert(N > 0);
1706
1.92G
      tell = ec_tell_frac(ec);
1707
1708
      /* Compute how many bits we want to allocate to this band */
1709
1.92G
      if (i != start)
1710
1.79G
         balance -= tell;
1711
1.92G
      remaining_bits = total_bits-tell-1;
1712
1.92G
      ctx.remaining_bits = remaining_bits;
1713
#ifdef ENABLE_QEXT
1714
1.91G
      if (i != start) {
1715
1.78G
         ext_balance += extra_pulses[i-1] + ext_tell;
1716
1.78G
      }
1717
      ext_tell = ec_tell_frac(ext_ec);
1718
      ctx.extra_bits = extra_pulses[i];
1719
1.91G
      if (i != start)
1720
1.78G
         ext_balance -= ext_tell;
1721
1.91G
      if (i <= codedBands-1)
1722
504M
      {
1723
504M
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1724
504M
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1725
1.41G
      } else {
1726
1.41G
         ext_b = 0;
1727
1.41G
      }
1728
#endif
1729
1.92G
      if (i <= codedBands-1)
1730
508M
      {
1731
508M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1732
508M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1733
1.41G
      } else {
1734
1.41G
         b = 0;
1735
1.41G
      }
1736
1737
1.92G
#ifndef DISABLE_UPDATE_DRAFT
1738
1.92G
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1739
8.67M
            lowband_offset = i;
1740
1.92G
      if (i == start+1)
1741
134M
         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.92G
      tf_change = tf_res[i];
1748
1.92G
      ctx.tf_change = tf_change;
1749
1.92G
      if (i>=m->effEBands)
1750
47.4k
      {
1751
47.4k
         X=norm;
1752
47.4k
         if (Y_!=NULL)
1753
37.9k
            Y = norm;
1754
47.4k
         lowband_scratch = NULL;
1755
47.4k
      }
1756
1.92G
      if (last && !theta_rdo)
1757
130M
         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.92G
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1762
61.7M
      {
1763
61.7M
         int fold_start;
1764
61.7M
         int fold_end;
1765
61.7M
         int fold_i;
1766
         /* This ensures we never repeat spectral content within one band */
1767
61.7M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1768
61.7M
         fold_start = lowband_offset;
1769
64.1M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1770
61.7M
         fold_end = lowband_offset-1;
1771
61.7M
#ifndef DISABLE_UPDATE_DRAFT
1772
130M
         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
61.7M
         x_cm = y_cm = 0;
1777
132M
         fold_i = fold_start; do {
1778
132M
           x_cm |= collapse_masks[fold_i*C+0];
1779
132M
           y_cm |= collapse_masks[fold_i*C+C-1];
1780
132M
         } while (++fold_i<fold_end);
1781
61.7M
      }
1782
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1783
         always) be non-zero. */
1784
1.86G
      else
1785
1.86G
         x_cm = y_cm = (1<<B)-1;
1786
1787
1.92G
      if (dual_stereo && i==intensity)
1788
349k
      {
1789
349k
         int j;
1790
1791
         /* Switch off dual stereo to do intensity. */
1792
349k
         dual_stereo = 0;
1793
349k
         if (resynth)
1794
1.16M
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1795
1.13M
               norm[j] = HALF32(norm[j]+norm2[j]);
1796
349k
      }
1797
1.92G
      if (dual_stereo)
1798
5.75M
      {
1799
5.75M
         x_cm = quant_band(&ctx, X, N, b/2, B,
1800
5.75M
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1801
5.75M
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1802
5.75M
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1803
5.75M
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1804
5.75M
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1805
1.91G
      } else {
1806
1.91G
         if (Y!=NULL)
1807
271M
         {
1808
271M
            if (theta_rdo && i < intensity)
1809
3.54M
            {
1810
3.54M
               ec_ctx ec_save, ec_save2;
1811
3.54M
               struct band_ctx ctx_save, ctx_save2;
1812
3.54M
               opus_val32 dist0, dist1;
1813
3.54M
               unsigned cm, cm2;
1814
3.54M
               int nstart_bytes, nend_bytes, save_bytes;
1815
3.54M
               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
3.54M
               opus_val16 w[2];
1822
3.54M
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1823
               /* Make a copy. */
1824
3.54M
               cm = x_cm|y_cm;
1825
3.54M
               ec_save = *ec;
1826
#ifdef ENABLE_QEXT
1827
               ext_ec_save = *ext_ec;
1828
#endif
1829
3.54M
               ctx_save = ctx;
1830
3.54M
               OPUS_COPY(X_save, X, N);
1831
3.54M
               OPUS_COPY(Y_save, Y, N);
1832
               /* Encode and round down. */
1833
3.54M
               ctx.theta_round = -1;
1834
3.54M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1835
3.54M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1836
3.54M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1837
3.54M
               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
3.54M
               cm2 = x_cm;
1841
3.54M
               ec_save2 = *ec;
1842
#ifdef ENABLE_QEXT
1843
               ext_ec_save2 = *ext_ec;
1844
#endif
1845
3.54M
               ctx_save2 = ctx;
1846
3.54M
               OPUS_COPY(X_save2, X, N);
1847
3.54M
               OPUS_COPY(Y_save2, Y, N);
1848
3.54M
               if (!last)
1849
3.52M
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1850
3.54M
               nstart_bytes = ec_save.offs;
1851
3.54M
               nend_bytes = ec_save.storage;
1852
3.54M
               bytes_buf = ec_save.buf+nstart_bytes;
1853
3.54M
               save_bytes = nend_bytes-nstart_bytes;
1854
3.54M
               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
3.05M
               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
3.05M
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1861
#endif
1862
               /* Restore */
1863
3.54M
               *ec = ec_save;
1864
#ifdef ENABLE_QEXT
1865
               *ext_ec = ext_ec_save;
1866
#endif
1867
3.54M
               ctx = ctx_save;
1868
3.54M
               OPUS_COPY(X, X_save, N);
1869
3.54M
               OPUS_COPY(Y, Y_save, N);
1870
3.54M
#ifndef DISABLE_UPDATE_DRAFT
1871
3.54M
               if (i == start+1)
1872
346k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1873
3.54M
#endif
1874
               /* Encode and round up. */
1875
3.54M
               ctx.theta_round = 1;
1876
3.54M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1877
3.54M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1878
3.54M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1879
3.54M
               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
3.54M
               if (dist0 >= dist1) {
1881
3.12M
                  x_cm = cm2;
1882
3.12M
                  *ec = ec_save2;
1883
#ifdef ENABLE_QEXT
1884
                  *ext_ec = ext_ec_save2;
1885
#endif
1886
3.12M
                  ctx = ctx_save2;
1887
3.12M
                  OPUS_COPY(X, X_save2, N);
1888
3.12M
                  OPUS_COPY(Y, Y_save2, N);
1889
3.12M
                  if (!last)
1890
3.12M
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1891
3.12M
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1892
#ifdef ENABLE_QEXT
1893
2.78M
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1894
#endif
1895
3.12M
               }
1896
267M
            } else {
1897
267M
               ctx.theta_round = 0;
1898
267M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1899
267M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1900
267M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1901
267M
            }
1902
1.64G
         } else {
1903
1.64G
            x_cm = quant_band(&ctx, X, N, b, B,
1904
1.64G
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1905
1.64G
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1906
1.64G
         }
1907
1.91G
         y_cm = x_cm;
1908
1.91G
      }
1909
1.92G
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1910
1.92G
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1911
1.92G
      balance += pulses[i] + tell;
1912
1913
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1914
1.92G
      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.92G
      ctx.avoid_split_noise = 0;
1918
1.92G
   }
1919
134M
   *seed = ctx.seed;
1920
1921
134M
   RESTORE_STACK;
1922
134M
}
quant_all_bands
Line
Count
Source
1597
318k
{
1598
318k
   int i;
1599
318k
   opus_int32 remaining_bits;
1600
318k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
318k
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
318k
   VARDECL(celt_norm, _norm);
1603
318k
   VARDECL(celt_norm, _lowband_scratch);
1604
318k
   VARDECL(celt_norm, X_save);
1605
318k
   VARDECL(celt_norm, Y_save);
1606
318k
   VARDECL(celt_norm, X_save2);
1607
318k
   VARDECL(celt_norm, Y_save2);
1608
318k
   VARDECL(celt_norm, norm_save2);
1609
318k
   VARDECL(unsigned char, bytes_save);
1610
318k
   int resynth_alloc;
1611
318k
   celt_norm *lowband_scratch;
1612
318k
   int B;
1613
318k
   int M;
1614
318k
   int lowband_offset;
1615
318k
   int update_lowband = 1;
1616
318k
   int C = Y_ != NULL ? 2 : 1;
1617
318k
   int norm_offset;
1618
318k
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1619
#ifdef RESYNTH
1620
   int resynth = 1;
1621
#else
1622
318k
   int resynth = !encode || theta_rdo;
1623
318k
#endif
1624
318k
   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
318k
   SAVE_STACK;
1632
1633
318k
   M = 1<<LM;
1634
318k
   B = shortBlocks ? M : 1;
1635
318k
   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
318k
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1639
318k
   norm = _norm;
1640
318k
   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
318k
   if (encode && resynth)
1646
27.6k
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1647
291k
   else
1648
291k
      resynth_alloc = ALLOC_NONE;
1649
318k
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1650
318k
   if (encode && resynth)
1651
27.6k
      lowband_scratch = _lowband_scratch;
1652
291k
   else
1653
291k
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1654
318k
   ALLOC(X_save, resynth_alloc, celt_norm);
1655
318k
   ALLOC(Y_save, resynth_alloc, celt_norm);
1656
318k
   ALLOC(X_save2, resynth_alloc, celt_norm);
1657
318k
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1658
318k
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1659
1660
318k
   lowband_offset = 0;
1661
318k
   ctx.bandE = bandE;
1662
318k
   ctx.ec = ec;
1663
318k
   ctx.encode = encode;
1664
318k
   ctx.intensity = intensity;
1665
318k
   ctx.m = m;
1666
318k
   ctx.seed = *seed;
1667
318k
   ctx.spread = spread;
1668
318k
   ctx.arch = arch;
1669
318k
   ctx.disable_inv = disable_inv;
1670
318k
   ctx.resynth = resynth;
1671
318k
   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
318k
   ALLOC(bytes_save, theta_rdo ? 1275 : ALLOC_NONE, unsigned char);
1680
1681
   /* Avoid injecting noise in the first band on transients. */
1682
318k
   ctx.avoid_split_noise = B > 1;
1683
5.03M
   for (i=start;i<end;i++)
1684
4.71M
   {
1685
4.71M
      opus_int32 tell;
1686
4.71M
      int b;
1687
4.71M
      int N;
1688
4.71M
      opus_int32 curr_balance;
1689
4.71M
      int effective_lowband=-1;
1690
4.71M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1691
4.71M
      int tf_change=0;
1692
4.71M
      unsigned x_cm;
1693
4.71M
      unsigned y_cm;
1694
4.71M
      int last;
1695
1696
4.71M
      ctx.i = i;
1697
4.71M
      last = (i==end-1);
1698
1699
4.71M
      X = X_+M*eBands[i];
1700
4.71M
      if (Y_!=NULL)
1701
1.60M
         Y = Y_+M*eBands[i];
1702
3.11M
      else
1703
3.11M
         Y = NULL;
1704
4.71M
      N = M*eBands[i+1]-M*eBands[i];
1705
4.71M
      celt_assert(N > 0);
1706
4.71M
      tell = ec_tell_frac(ec);
1707
1708
      /* Compute how many bits we want to allocate to this band */
1709
4.71M
      if (i != start)
1710
4.39M
         balance -= tell;
1711
4.71M
      remaining_bits = total_bits-tell-1;
1712
4.71M
      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
4.71M
      if (i <= codedBands-1)
1730
2.33M
      {
1731
2.33M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1732
2.33M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1733
2.37M
      } else {
1734
2.37M
         b = 0;
1735
2.37M
      }
1736
1737
4.71M
#ifndef DISABLE_UPDATE_DRAFT
1738
4.71M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1739
759k
            lowband_offset = i;
1740
4.71M
      if (i == start+1)
1741
318k
         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
4.71M
      tf_change = tf_res[i];
1748
4.71M
      ctx.tf_change = tf_change;
1749
4.71M
      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
4.71M
      if (last && !theta_rdo)
1757
291k
         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
4.71M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1762
2.54M
      {
1763
2.54M
         int fold_start;
1764
2.54M
         int fold_end;
1765
2.54M
         int fold_i;
1766
         /* This ensures we never repeat spectral content within one band */
1767
2.54M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1768
2.54M
         fold_start = lowband_offset;
1769
2.85M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1770
2.54M
         fold_end = lowband_offset-1;
1771
2.54M
#ifndef DISABLE_UPDATE_DRAFT
1772
6.42M
         while(++fold_end < i && M*eBands[fold_end] < effective_lowband+norm_offset+N);
1773
#else
1774
         while(M*eBands[++fold_end] < effective_lowband+norm_offset+N);
1775
#endif
1776
2.54M
         x_cm = y_cm = 0;
1777
6.73M
         fold_i = fold_start; do {
1778
6.73M
           x_cm |= collapse_masks[fold_i*C+0];
1779
6.73M
           y_cm |= collapse_masks[fold_i*C+C-1];
1780
6.73M
         } while (++fold_i<fold_end);
1781
2.54M
      }
1782
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1783
         always) be non-zero. */
1784
2.16M
      else
1785
2.16M
         x_cm = y_cm = (1<<B)-1;
1786
1787
4.71M
      if (dual_stereo && i==intensity)
1788
15.9k
      {
1789
15.9k
         int j;
1790
1791
         /* Switch off dual stereo to do intensity. */
1792
15.9k
         dual_stereo = 0;
1793
15.9k
         if (resynth)
1794
223k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1795
216k
               norm[j] = HALF32(norm[j]+norm2[j]);
1796
15.9k
      }
1797
4.71M
      if (dual_stereo)
1798
189k
      {
1799
189k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1800
189k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1801
189k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1802
189k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1803
189k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1804
189k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1805
4.52M
      } else {
1806
4.52M
         if (Y!=NULL)
1807
1.41M
         {
1808
1.41M
            if (theta_rdo && i < intensity)
1809
244k
            {
1810
244k
               ec_ctx ec_save, ec_save2;
1811
244k
               struct band_ctx ctx_save, ctx_save2;
1812
244k
               opus_val32 dist0, dist1;
1813
244k
               unsigned cm, cm2;
1814
244k
               int nstart_bytes, nend_bytes, save_bytes;
1815
244k
               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
244k
               opus_val16 w[2];
1822
244k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1823
               /* Make a copy. */
1824
244k
               cm = x_cm|y_cm;
1825
244k
               ec_save = *ec;
1826
#ifdef ENABLE_QEXT
1827
               ext_ec_save = *ext_ec;
1828
#endif
1829
244k
               ctx_save = ctx;
1830
244k
               OPUS_COPY(X_save, X, N);
1831
244k
               OPUS_COPY(Y_save, Y, N);
1832
               /* Encode and round down. */
1833
244k
               ctx.theta_round = -1;
1834
244k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1835
244k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1836
244k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1837
244k
               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
244k
               cm2 = x_cm;
1841
244k
               ec_save2 = *ec;
1842
#ifdef ENABLE_QEXT
1843
               ext_ec_save2 = *ext_ec;
1844
#endif
1845
244k
               ctx_save2 = ctx;
1846
244k
               OPUS_COPY(X_save2, X, N);
1847
244k
               OPUS_COPY(Y_save2, Y, N);
1848
244k
               if (!last)
1849
242k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1850
244k
               nstart_bytes = ec_save.offs;
1851
244k
               nend_bytes = ec_save.storage;
1852
244k
               bytes_buf = ec_save.buf+nstart_bytes;
1853
244k
               save_bytes = nend_bytes-nstart_bytes;
1854
244k
               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
244k
               *ec = ec_save;
1864
#ifdef ENABLE_QEXT
1865
               *ext_ec = ext_ec_save;
1866
#endif
1867
244k
               ctx = ctx_save;
1868
244k
               OPUS_COPY(X, X_save, N);
1869
244k
               OPUS_COPY(Y, Y_save, N);
1870
244k
#ifndef DISABLE_UPDATE_DRAFT
1871
244k
               if (i == start+1)
1872
20.9k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1873
244k
#endif
1874
               /* Encode and round up. */
1875
244k
               ctx.theta_round = 1;
1876
244k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1877
244k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1878
244k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1879
244k
               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
244k
               if (dist0 >= dist1) {
1881
170k
                  x_cm = cm2;
1882
170k
                  *ec = ec_save2;
1883
#ifdef ENABLE_QEXT
1884
                  *ext_ec = ext_ec_save2;
1885
#endif
1886
170k
                  ctx = ctx_save2;
1887
170k
                  OPUS_COPY(X, X_save2, N);
1888
170k
                  OPUS_COPY(Y, Y_save2, N);
1889
170k
                  if (!last)
1890
168k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1891
170k
                  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
170k
               }
1896
1.16M
            } else {
1897
1.16M
               ctx.theta_round = 0;
1898
1.16M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1899
1.16M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1900
1.16M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1901
1.16M
            }
1902
3.11M
         } else {
1903
3.11M
            x_cm = quant_band(&ctx, X, N, b, B,
1904
3.11M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1905
3.11M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1906
3.11M
         }
1907
4.52M
         y_cm = x_cm;
1908
4.52M
      }
1909
4.71M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1910
4.71M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1911
4.71M
      balance += pulses[i] + tell;
1912
1913
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1914
4.71M
      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
4.71M
      ctx.avoid_split_noise = 0;
1918
4.71M
   }
1919
318k
   *seed = ctx.seed;
1920
1921
318k
   RESTORE_STACK;
1922
318k
}
quant_all_bands
Line
Count
Source
1597
66.8M
{
1598
66.8M
   int i;
1599
66.8M
   opus_int32 remaining_bits;
1600
66.8M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
66.8M
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
66.8M
   VARDECL(celt_norm, _norm);
1603
66.8M
   VARDECL(celt_norm, _lowband_scratch);
1604
66.8M
   VARDECL(celt_norm, X_save);
1605
66.8M
   VARDECL(celt_norm, Y_save);
1606
66.8M
   VARDECL(celt_norm, X_save2);
1607
66.8M
   VARDECL(celt_norm, Y_save2);
1608
66.8M
   VARDECL(celt_norm, norm_save2);
1609
66.8M
   VARDECL(unsigned char, bytes_save);
1610
66.8M
   int resynth_alloc;
1611
66.8M
   celt_norm *lowband_scratch;
1612
66.8M
   int B;
1613
66.8M
   int M;
1614
66.8M
   int lowband_offset;
1615
66.8M
   int update_lowband = 1;
1616
66.8M
   int C = Y_ != NULL ? 2 : 1;
1617
66.8M
   int norm_offset;
1618
66.8M
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1619
#ifdef RESYNTH
1620
   int resynth = 1;
1621
#else
1622
66.8M
   int resynth = !encode || theta_rdo;
1623
66.8M
#endif
1624
66.8M
   struct band_ctx ctx;
1625
66.8M
#ifdef ENABLE_QEXT
1626
66.8M
   int ext_b;
1627
66.8M
   opus_int32 ext_balance=0;
1628
66.8M
   opus_int32 ext_tell=0;
1629
66.8M
   VARDECL(unsigned char, ext_bytes_save);
1630
66.8M
#endif
1631
66.8M
   SAVE_STACK;
1632
1633
66.8M
   M = 1<<LM;
1634
66.8M
   B = shortBlocks ? M : 1;
1635
66.8M
   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
66.8M
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1639
66.8M
   norm = _norm;
1640
66.8M
   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
66.8M
   if (encode && resynth)
1646
1.88M
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1647
64.9M
   else
1648
64.9M
      resynth_alloc = ALLOC_NONE;
1649
66.8M
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1650
66.8M
   if (encode && resynth)
1651
1.88M
      lowband_scratch = _lowband_scratch;
1652
64.9M
   else
1653
64.9M
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1654
66.8M
   ALLOC(X_save, resynth_alloc, celt_norm);
1655
66.8M
   ALLOC(Y_save, resynth_alloc, celt_norm);
1656
66.8M
   ALLOC(X_save2, resynth_alloc, celt_norm);
1657
66.8M
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1658
66.8M
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1659
1660
66.8M
   lowband_offset = 0;
1661
66.8M
   ctx.bandE = bandE;
1662
66.8M
   ctx.ec = ec;
1663
66.8M
   ctx.encode = encode;
1664
66.8M
   ctx.intensity = intensity;
1665
66.8M
   ctx.m = m;
1666
66.8M
   ctx.seed = *seed;
1667
66.8M
   ctx.spread = spread;
1668
66.8M
   ctx.arch = arch;
1669
66.8M
   ctx.disable_inv = disable_inv;
1670
66.8M
   ctx.resynth = resynth;
1671
66.8M
   ctx.theta_round = 0;
1672
66.8M
#ifdef ENABLE_QEXT
1673
66.8M
   ctx.ext_ec = ext_ec;
1674
66.8M
   ctx.ext_total_bits = ext_total_bits;
1675
66.8M
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1676
66.8M
   if (ctx.extra_bands) theta_rdo = 0;
1677
66.8M
   ALLOC(ext_bytes_save, theta_rdo ? QEXT_PACKET_SIZE_CAP : ALLOC_NONE, unsigned char);
1678
66.8M
#endif
1679
66.8M
   ALLOC(bytes_save, theta_rdo ? 1275 : ALLOC_NONE, unsigned char);
1680
1681
   /* Avoid injecting noise in the first band on transients. */
1682
66.8M
   ctx.avoid_split_noise = B > 1;
1683
1.02G
   for (i=start;i<end;i++)
1684
957M
   {
1685
957M
      opus_int32 tell;
1686
957M
      int b;
1687
957M
      int N;
1688
957M
      opus_int32 curr_balance;
1689
957M
      int effective_lowband=-1;
1690
957M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1691
957M
      int tf_change=0;
1692
957M
      unsigned x_cm;
1693
957M
      unsigned y_cm;
1694
957M
      int last;
1695
1696
957M
      ctx.i = i;
1697
957M
      last = (i==end-1);
1698
1699
957M
      X = X_+M*eBands[i];
1700
957M
      if (Y_!=NULL)
1701
136M
         Y = Y_+M*eBands[i];
1702
820M
      else
1703
820M
         Y = NULL;
1704
957M
      N = M*eBands[i+1]-M*eBands[i];
1705
957M
      celt_assert(N > 0);
1706
957M
      tell = ec_tell_frac(ec);
1707
1708
      /* Compute how many bits we want to allocate to this band */
1709
957M
      if (i != start)
1710
890M
         balance -= tell;
1711
957M
      remaining_bits = total_bits-tell-1;
1712
957M
      ctx.remaining_bits = remaining_bits;
1713
957M
#ifdef ENABLE_QEXT
1714
957M
      if (i != start) {
1715
890M
         ext_balance += extra_pulses[i-1] + ext_tell;
1716
890M
      }
1717
957M
      ext_tell = ec_tell_frac(ext_ec);
1718
957M
      ctx.extra_bits = extra_pulses[i];
1719
957M
      if (i != start)
1720
890M
         ext_balance -= ext_tell;
1721
957M
      if (i <= codedBands-1)
1722
252M
      {
1723
252M
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1724
252M
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1725
705M
      } else {
1726
705M
         ext_b = 0;
1727
705M
      }
1728
957M
#endif
1729
957M
      if (i <= codedBands-1)
1730
252M
      {
1731
252M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1732
252M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1733
705M
      } else {
1734
705M
         b = 0;
1735
705M
      }
1736
1737
957M
#ifndef DISABLE_UPDATE_DRAFT
1738
957M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1739
3.57M
            lowband_offset = i;
1740
957M
      if (i == start+1)
1741
66.8M
         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
957M
      tf_change = tf_res[i];
1748
957M
      ctx.tf_change = tf_change;
1749
957M
      if (i>=m->effEBands)
1750
23.7k
      {
1751
23.7k
         X=norm;
1752
23.7k
         if (Y_!=NULL)
1753
18.9k
            Y = norm;
1754
23.7k
         lowband_scratch = NULL;
1755
23.7k
      }
1756
957M
      if (last && !theta_rdo)
1757
64.9M
         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
957M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1762
28.3M
      {
1763
28.3M
         int fold_start;
1764
28.3M
         int fold_end;
1765
28.3M
         int fold_i;
1766
         /* This ensures we never repeat spectral content within one band */
1767
28.3M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1768
28.3M
         fold_start = lowband_offset;
1769
29.2M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1770
28.3M
         fold_end = lowband_offset-1;
1771
28.3M
#ifndef DISABLE_UPDATE_DRAFT
1772
58.7M
         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
28.3M
         x_cm = y_cm = 0;
1777
59.6M
         fold_i = fold_start; do {
1778
59.6M
           x_cm |= collapse_masks[fold_i*C+0];
1779
59.6M
           y_cm |= collapse_masks[fold_i*C+C-1];
1780
59.6M
         } while (++fold_i<fold_end);
1781
28.3M
      }
1782
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1783
         always) be non-zero. */
1784
929M
      else
1785
929M
         x_cm = y_cm = (1<<B)-1;
1786
1787
957M
      if (dual_stereo && i==intensity)
1788
158k
      {
1789
158k
         int j;
1790
1791
         /* Switch off dual stereo to do intensity. */
1792
158k
         dual_stereo = 0;
1793
158k
         if (resynth)
1794
358k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1795
350k
               norm[j] = HALF32(norm[j]+norm2[j]);
1796
158k
      }
1797
957M
      if (dual_stereo)
1798
2.68M
      {
1799
2.68M
         x_cm = quant_band(&ctx, X, N, b/2, B,
1800
2.68M
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1801
2.68M
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1802
2.68M
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1803
2.68M
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1804
2.68M
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1805
954M
      } else {
1806
954M
         if (Y!=NULL)
1807
134M
         {
1808
134M
            if (theta_rdo && i < intensity)
1809
1.52M
            {
1810
1.52M
               ec_ctx ec_save, ec_save2;
1811
1.52M
               struct band_ctx ctx_save, ctx_save2;
1812
1.52M
               opus_val32 dist0, dist1;
1813
1.52M
               unsigned cm, cm2;
1814
1.52M
               int nstart_bytes, nend_bytes, save_bytes;
1815
1.52M
               unsigned char *bytes_buf;
1816
1.52M
#ifdef ENABLE_QEXT
1817
1.52M
               ec_ctx ext_ec_save, ext_ec_save2;
1818
1.52M
               unsigned char *ext_bytes_buf;
1819
1.52M
               int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes;
1820
1.52M
#endif
1821
1.52M
               opus_val16 w[2];
1822
1.52M
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1823
               /* Make a copy. */
1824
1.52M
               cm = x_cm|y_cm;
1825
1.52M
               ec_save = *ec;
1826
1.52M
#ifdef ENABLE_QEXT
1827
1.52M
               ext_ec_save = *ext_ec;
1828
1.52M
#endif
1829
1.52M
               ctx_save = ctx;
1830
1.52M
               OPUS_COPY(X_save, X, N);
1831
1.52M
               OPUS_COPY(Y_save, Y, N);
1832
               /* Encode and round down. */
1833
1.52M
               ctx.theta_round = -1;
1834
1.52M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1835
1.52M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1836
1.52M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1837
1.52M
               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
1.52M
               cm2 = x_cm;
1841
1.52M
               ec_save2 = *ec;
1842
1.52M
#ifdef ENABLE_QEXT
1843
1.52M
               ext_ec_save2 = *ext_ec;
1844
1.52M
#endif
1845
1.52M
               ctx_save2 = ctx;
1846
1.52M
               OPUS_COPY(X_save2, X, N);
1847
1.52M
               OPUS_COPY(Y_save2, Y, N);
1848
1.52M
               if (!last)
1849
1.52M
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1850
1.52M
               nstart_bytes = ec_save.offs;
1851
1.52M
               nend_bytes = ec_save.storage;
1852
1.52M
               bytes_buf = ec_save.buf+nstart_bytes;
1853
1.52M
               save_bytes = nend_bytes-nstart_bytes;
1854
1.52M
               OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1855
1.52M
#ifdef ENABLE_QEXT
1856
1.52M
               ext_nstart_bytes = ext_ec_save.offs;
1857
1.52M
               ext_nend_bytes = ext_ec_save.storage;
1858
1.52M
               ext_bytes_buf = ext_ec_save.buf!=NULL ? ext_ec_save.buf+ext_nstart_bytes : NULL;
1859
1.52M
               ext_save_bytes = ext_nend_bytes-ext_nstart_bytes;
1860
1.52M
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1861
1.52M
#endif
1862
               /* Restore */
1863
1.52M
               *ec = ec_save;
1864
1.52M
#ifdef ENABLE_QEXT
1865
1.52M
               *ext_ec = ext_ec_save;
1866
1.52M
#endif
1867
1.52M
               ctx = ctx_save;
1868
1.52M
               OPUS_COPY(X, X_save, N);
1869
1.52M
               OPUS_COPY(Y, Y_save, N);
1870
1.52M
#ifndef DISABLE_UPDATE_DRAFT
1871
1.52M
               if (i == start+1)
1872
152k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1873
1.52M
#endif
1874
               /* Encode and round up. */
1875
1.52M
               ctx.theta_round = 1;
1876
1.52M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1877
1.52M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1878
1.52M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1879
1.52M
               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
1.52M
               if (dist0 >= dist1) {
1881
1.39M
                  x_cm = cm2;
1882
1.39M
                  *ec = ec_save2;
1883
1.39M
#ifdef ENABLE_QEXT
1884
1.39M
                  *ext_ec = ext_ec_save2;
1885
1.39M
#endif
1886
1.39M
                  ctx = ctx_save2;
1887
1.39M
                  OPUS_COPY(X, X_save2, N);
1888
1.39M
                  OPUS_COPY(Y, Y_save2, N);
1889
1.39M
                  if (!last)
1890
1.39M
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1891
1.39M
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1892
1.39M
#ifdef ENABLE_QEXT
1893
1.39M
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1894
1.39M
#endif
1895
1.39M
               }
1896
132M
            } else {
1897
132M
               ctx.theta_round = 0;
1898
132M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1899
132M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1900
132M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1901
132M
            }
1902
820M
         } else {
1903
820M
            x_cm = quant_band(&ctx, X, N, b, B,
1904
820M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1905
820M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1906
820M
         }
1907
954M
         y_cm = x_cm;
1908
954M
      }
1909
957M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1910
957M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1911
957M
      balance += pulses[i] + tell;
1912
1913
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1914
957M
      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
957M
      ctx.avoid_split_noise = 0;
1918
957M
   }
1919
66.8M
   *seed = ctx.seed;
1920
1921
66.8M
   RESTORE_STACK;
1922
66.8M
}
quant_all_bands
Line
Count
Source
1597
66.8M
{
1598
66.8M
   int i;
1599
66.8M
   opus_int32 remaining_bits;
1600
66.8M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
66.8M
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
66.8M
   VARDECL(celt_norm, _norm);
1603
66.8M
   VARDECL(celt_norm, _lowband_scratch);
1604
66.8M
   VARDECL(celt_norm, X_save);
1605
66.8M
   VARDECL(celt_norm, Y_save);
1606
66.8M
   VARDECL(celt_norm, X_save2);
1607
66.8M
   VARDECL(celt_norm, Y_save2);
1608
66.8M
   VARDECL(celt_norm, norm_save2);
1609
66.8M
   VARDECL(unsigned char, bytes_save);
1610
66.8M
   int resynth_alloc;
1611
66.8M
   celt_norm *lowband_scratch;
1612
66.8M
   int B;
1613
66.8M
   int M;
1614
66.8M
   int lowband_offset;
1615
66.8M
   int update_lowband = 1;
1616
66.8M
   int C = Y_ != NULL ? 2 : 1;
1617
66.8M
   int norm_offset;
1618
66.8M
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1619
#ifdef RESYNTH
1620
   int resynth = 1;
1621
#else
1622
66.8M
   int resynth = !encode || theta_rdo;
1623
66.8M
#endif
1624
66.8M
   struct band_ctx ctx;
1625
66.8M
#ifdef ENABLE_QEXT
1626
66.8M
   int ext_b;
1627
66.8M
   opus_int32 ext_balance=0;
1628
66.8M
   opus_int32 ext_tell=0;
1629
66.8M
   VARDECL(unsigned char, ext_bytes_save);
1630
66.8M
#endif
1631
66.8M
   SAVE_STACK;
1632
1633
66.8M
   M = 1<<LM;
1634
66.8M
   B = shortBlocks ? M : 1;
1635
66.8M
   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
66.8M
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1639
66.8M
   norm = _norm;
1640
66.8M
   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
66.8M
   if (encode && resynth)
1646
1.88M
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1647
64.9M
   else
1648
64.9M
      resynth_alloc = ALLOC_NONE;
1649
66.8M
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1650
66.8M
   if (encode && resynth)
1651
1.88M
      lowband_scratch = _lowband_scratch;
1652
64.9M
   else
1653
64.9M
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1654
66.8M
   ALLOC(X_save, resynth_alloc, celt_norm);
1655
66.8M
   ALLOC(Y_save, resynth_alloc, celt_norm);
1656
66.8M
   ALLOC(X_save2, resynth_alloc, celt_norm);
1657
66.8M
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1658
66.8M
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1659
1660
66.8M
   lowband_offset = 0;
1661
66.8M
   ctx.bandE = bandE;
1662
66.8M
   ctx.ec = ec;
1663
66.8M
   ctx.encode = encode;
1664
66.8M
   ctx.intensity = intensity;
1665
66.8M
   ctx.m = m;
1666
66.8M
   ctx.seed = *seed;
1667
66.8M
   ctx.spread = spread;
1668
66.8M
   ctx.arch = arch;
1669
66.8M
   ctx.disable_inv = disable_inv;
1670
66.8M
   ctx.resynth = resynth;
1671
66.8M
   ctx.theta_round = 0;
1672
66.8M
#ifdef ENABLE_QEXT
1673
66.8M
   ctx.ext_ec = ext_ec;
1674
66.8M
   ctx.ext_total_bits = ext_total_bits;
1675
66.8M
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1676
66.8M
   if (ctx.extra_bands) theta_rdo = 0;
1677
66.8M
   ALLOC(ext_bytes_save, theta_rdo ? QEXT_PACKET_SIZE_CAP : ALLOC_NONE, unsigned char);
1678
66.8M
#endif
1679
66.8M
   ALLOC(bytes_save, theta_rdo ? 1275 : ALLOC_NONE, unsigned char);
1680
1681
   /* Avoid injecting noise in the first band on transients. */
1682
66.8M
   ctx.avoid_split_noise = B > 1;
1683
1.02G
   for (i=start;i<end;i++)
1684
957M
   {
1685
957M
      opus_int32 tell;
1686
957M
      int b;
1687
957M
      int N;
1688
957M
      opus_int32 curr_balance;
1689
957M
      int effective_lowband=-1;
1690
957M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1691
957M
      int tf_change=0;
1692
957M
      unsigned x_cm;
1693
957M
      unsigned y_cm;
1694
957M
      int last;
1695
1696
957M
      ctx.i = i;
1697
957M
      last = (i==end-1);
1698
1699
957M
      X = X_+M*eBands[i];
1700
957M
      if (Y_!=NULL)
1701
136M
         Y = Y_+M*eBands[i];
1702
820M
      else
1703
820M
         Y = NULL;
1704
957M
      N = M*eBands[i+1]-M*eBands[i];
1705
957M
      celt_assert(N > 0);
1706
957M
      tell = ec_tell_frac(ec);
1707
1708
      /* Compute how many bits we want to allocate to this band */
1709
957M
      if (i != start)
1710
890M
         balance -= tell;
1711
957M
      remaining_bits = total_bits-tell-1;
1712
957M
      ctx.remaining_bits = remaining_bits;
1713
957M
#ifdef ENABLE_QEXT
1714
957M
      if (i != start) {
1715
890M
         ext_balance += extra_pulses[i-1] + ext_tell;
1716
890M
      }
1717
957M
      ext_tell = ec_tell_frac(ext_ec);
1718
957M
      ctx.extra_bits = extra_pulses[i];
1719
957M
      if (i != start)
1720
890M
         ext_balance -= ext_tell;
1721
957M
      if (i <= codedBands-1)
1722
252M
      {
1723
252M
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1724
252M
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1725
705M
      } else {
1726
705M
         ext_b = 0;
1727
705M
      }
1728
957M
#endif
1729
957M
      if (i <= codedBands-1)
1730
252M
      {
1731
252M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1732
252M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1733
705M
      } else {
1734
705M
         b = 0;
1735
705M
      }
1736
1737
957M
#ifndef DISABLE_UPDATE_DRAFT
1738
957M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1739
3.57M
            lowband_offset = i;
1740
957M
      if (i == start+1)
1741
66.8M
         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
957M
      tf_change = tf_res[i];
1748
957M
      ctx.tf_change = tf_change;
1749
957M
      if (i>=m->effEBands)
1750
23.7k
      {
1751
23.7k
         X=norm;
1752
23.7k
         if (Y_!=NULL)
1753
18.9k
            Y = norm;
1754
23.7k
         lowband_scratch = NULL;
1755
23.7k
      }
1756
957M
      if (last && !theta_rdo)
1757
64.9M
         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
957M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1762
28.3M
      {
1763
28.3M
         int fold_start;
1764
28.3M
         int fold_end;
1765
28.3M
         int fold_i;
1766
         /* This ensures we never repeat spectral content within one band */
1767
28.3M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1768
28.3M
         fold_start = lowband_offset;
1769
29.2M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1770
28.3M
         fold_end = lowband_offset-1;
1771
28.3M
#ifndef DISABLE_UPDATE_DRAFT
1772
58.7M
         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
28.3M
         x_cm = y_cm = 0;
1777
59.6M
         fold_i = fold_start; do {
1778
59.6M
           x_cm |= collapse_masks[fold_i*C+0];
1779
59.6M
           y_cm |= collapse_masks[fold_i*C+C-1];
1780
59.6M
         } while (++fold_i<fold_end);
1781
28.3M
      }
1782
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1783
         always) be non-zero. */
1784
929M
      else
1785
929M
         x_cm = y_cm = (1<<B)-1;
1786
1787
957M
      if (dual_stereo && i==intensity)
1788
158k
      {
1789
158k
         int j;
1790
1791
         /* Switch off dual stereo to do intensity. */
1792
158k
         dual_stereo = 0;
1793
158k
         if (resynth)
1794
358k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1795
350k
               norm[j] = HALF32(norm[j]+norm2[j]);
1796
158k
      }
1797
957M
      if (dual_stereo)
1798
2.68M
      {
1799
2.68M
         x_cm = quant_band(&ctx, X, N, b/2, B,
1800
2.68M
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1801
2.68M
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1802
2.68M
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1803
2.68M
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1804
2.68M
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1805
954M
      } else {
1806
954M
         if (Y!=NULL)
1807
134M
         {
1808
134M
            if (theta_rdo && i < intensity)
1809
1.52M
            {
1810
1.52M
               ec_ctx ec_save, ec_save2;
1811
1.52M
               struct band_ctx ctx_save, ctx_save2;
1812
1.52M
               opus_val32 dist0, dist1;
1813
1.52M
               unsigned cm, cm2;
1814
1.52M
               int nstart_bytes, nend_bytes, save_bytes;
1815
1.52M
               unsigned char *bytes_buf;
1816
1.52M
#ifdef ENABLE_QEXT
1817
1.52M
               ec_ctx ext_ec_save, ext_ec_save2;
1818
1.52M
               unsigned char *ext_bytes_buf;
1819
1.52M
               int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes;
1820
1.52M
#endif
1821
1.52M
               opus_val16 w[2];
1822
1.52M
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1823
               /* Make a copy. */
1824
1.52M
               cm = x_cm|y_cm;
1825
1.52M
               ec_save = *ec;
1826
1.52M
#ifdef ENABLE_QEXT
1827
1.52M
               ext_ec_save = *ext_ec;
1828
1.52M
#endif
1829
1.52M
               ctx_save = ctx;
1830
1.52M
               OPUS_COPY(X_save, X, N);
1831
1.52M
               OPUS_COPY(Y_save, Y, N);
1832
               /* Encode and round down. */
1833
1.52M
               ctx.theta_round = -1;
1834
1.52M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1835
1.52M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1836
1.52M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1837
1.52M
               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
1.52M
               cm2 = x_cm;
1841
1.52M
               ec_save2 = *ec;
1842
1.52M
#ifdef ENABLE_QEXT
1843
1.52M
               ext_ec_save2 = *ext_ec;
1844
1.52M
#endif
1845
1.52M
               ctx_save2 = ctx;
1846
1.52M
               OPUS_COPY(X_save2, X, N);
1847
1.52M
               OPUS_COPY(Y_save2, Y, N);
1848
1.52M
               if (!last)
1849
1.52M
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1850
1.52M
               nstart_bytes = ec_save.offs;
1851
1.52M
               nend_bytes = ec_save.storage;
1852
1.52M
               bytes_buf = ec_save.buf+nstart_bytes;
1853
1.52M
               save_bytes = nend_bytes-nstart_bytes;
1854
1.52M
               OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1855
1.52M
#ifdef ENABLE_QEXT
1856
1.52M
               ext_nstart_bytes = ext_ec_save.offs;
1857
1.52M
               ext_nend_bytes = ext_ec_save.storage;
1858
1.52M
               ext_bytes_buf = ext_ec_save.buf!=NULL ? ext_ec_save.buf+ext_nstart_bytes : NULL;
1859
1.52M
               ext_save_bytes = ext_nend_bytes-ext_nstart_bytes;
1860
1.52M
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1861
1.52M
#endif
1862
               /* Restore */
1863
1.52M
               *ec = ec_save;
1864
1.52M
#ifdef ENABLE_QEXT
1865
1.52M
               *ext_ec = ext_ec_save;
1866
1.52M
#endif
1867
1.52M
               ctx = ctx_save;
1868
1.52M
               OPUS_COPY(X, X_save, N);
1869
1.52M
               OPUS_COPY(Y, Y_save, N);
1870
1.52M
#ifndef DISABLE_UPDATE_DRAFT
1871
1.52M
               if (i == start+1)
1872
152k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1873
1.52M
#endif
1874
               /* Encode and round up. */
1875
1.52M
               ctx.theta_round = 1;
1876
1.52M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1877
1.52M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1878
1.52M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1879
1.52M
               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
1.52M
               if (dist0 >= dist1) {
1881
1.39M
                  x_cm = cm2;
1882
1.39M
                  *ec = ec_save2;
1883
1.39M
#ifdef ENABLE_QEXT
1884
1.39M
                  *ext_ec = ext_ec_save2;
1885
1.39M
#endif
1886
1.39M
                  ctx = ctx_save2;
1887
1.39M
                  OPUS_COPY(X, X_save2, N);
1888
1.39M
                  OPUS_COPY(Y, Y_save2, N);
1889
1.39M
                  if (!last)
1890
1.39M
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1891
1.39M
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1892
1.39M
#ifdef ENABLE_QEXT
1893
1.39M
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1894
1.39M
#endif
1895
1.39M
               }
1896
132M
            } else {
1897
132M
               ctx.theta_round = 0;
1898
132M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1899
132M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1900
132M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1901
132M
            }
1902
820M
         } else {
1903
820M
            x_cm = quant_band(&ctx, X, N, b, B,
1904
820M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1905
820M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1906
820M
         }
1907
954M
         y_cm = x_cm;
1908
954M
      }
1909
957M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1910
957M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1911
957M
      balance += pulses[i] + tell;
1912
1913
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1914
957M
      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
957M
      ctx.avoid_split_noise = 0;
1918
957M
   }
1919
66.8M
   *seed = ctx.seed;
1920
1921
66.8M
   RESTORE_STACK;
1922
66.8M
}
quant_all_bands
Line
Count
Source
1597
318k
{
1598
318k
   int i;
1599
318k
   opus_int32 remaining_bits;
1600
318k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
318k
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
318k
   VARDECL(celt_norm, _norm);
1603
318k
   VARDECL(celt_norm, _lowband_scratch);
1604
318k
   VARDECL(celt_norm, X_save);
1605
318k
   VARDECL(celt_norm, Y_save);
1606
318k
   VARDECL(celt_norm, X_save2);
1607
318k
   VARDECL(celt_norm, Y_save2);
1608
318k
   VARDECL(celt_norm, norm_save2);
1609
318k
   VARDECL(unsigned char, bytes_save);
1610
318k
   int resynth_alloc;
1611
318k
   celt_norm *lowband_scratch;
1612
318k
   int B;
1613
318k
   int M;
1614
318k
   int lowband_offset;
1615
318k
   int update_lowband = 1;
1616
318k
   int C = Y_ != NULL ? 2 : 1;
1617
318k
   int norm_offset;
1618
318k
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1619
#ifdef RESYNTH
1620
   int resynth = 1;
1621
#else
1622
318k
   int resynth = !encode || theta_rdo;
1623
318k
#endif
1624
318k
   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
318k
   SAVE_STACK;
1632
1633
318k
   M = 1<<LM;
1634
318k
   B = shortBlocks ? M : 1;
1635
318k
   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
318k
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1639
318k
   norm = _norm;
1640
318k
   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
318k
   if (encode && resynth)
1646
27.6k
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1647
291k
   else
1648
291k
      resynth_alloc = ALLOC_NONE;
1649
318k
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1650
318k
   if (encode && resynth)
1651
27.6k
      lowband_scratch = _lowband_scratch;
1652
291k
   else
1653
291k
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1654
318k
   ALLOC(X_save, resynth_alloc, celt_norm);
1655
318k
   ALLOC(Y_save, resynth_alloc, celt_norm);
1656
318k
   ALLOC(X_save2, resynth_alloc, celt_norm);
1657
318k
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1658
318k
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1659
1660
318k
   lowband_offset = 0;
1661
318k
   ctx.bandE = bandE;
1662
318k
   ctx.ec = ec;
1663
318k
   ctx.encode = encode;
1664
318k
   ctx.intensity = intensity;
1665
318k
   ctx.m = m;
1666
318k
   ctx.seed = *seed;
1667
318k
   ctx.spread = spread;
1668
318k
   ctx.arch = arch;
1669
318k
   ctx.disable_inv = disable_inv;
1670
318k
   ctx.resynth = resynth;
1671
318k
   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
318k
   ALLOC(bytes_save, theta_rdo ? 1275 : ALLOC_NONE, unsigned char);
1680
1681
   /* Avoid injecting noise in the first band on transients. */
1682
318k
   ctx.avoid_split_noise = B > 1;
1683
5.03M
   for (i=start;i<end;i++)
1684
4.71M
   {
1685
4.71M
      opus_int32 tell;
1686
4.71M
      int b;
1687
4.71M
      int N;
1688
4.71M
      opus_int32 curr_balance;
1689
4.71M
      int effective_lowband=-1;
1690
4.71M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1691
4.71M
      int tf_change=0;
1692
4.71M
      unsigned x_cm;
1693
4.71M
      unsigned y_cm;
1694
4.71M
      int last;
1695
1696
4.71M
      ctx.i = i;
1697
4.71M
      last = (i==end-1);
1698
1699
4.71M
      X = X_+M*eBands[i];
1700
4.71M
      if (Y_!=NULL)
1701
1.60M
         Y = Y_+M*eBands[i];
1702
3.11M
      else
1703
3.11M
         Y = NULL;
1704
4.71M
      N = M*eBands[i+1]-M*eBands[i];
1705
4.71M
      celt_assert(N > 0);
1706
4.71M
      tell = ec_tell_frac(ec);
1707
1708
      /* Compute how many bits we want to allocate to this band */
1709
4.71M
      if (i != start)
1710
4.39M
         balance -= tell;
1711
4.71M
      remaining_bits = total_bits-tell-1;
1712
4.71M
      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
4.71M
      if (i <= codedBands-1)
1730
2.33M
      {
1731
2.33M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1732
2.33M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1733
2.37M
      } else {
1734
2.37M
         b = 0;
1735
2.37M
      }
1736
1737
4.71M
#ifndef DISABLE_UPDATE_DRAFT
1738
4.71M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1739
759k
            lowband_offset = i;
1740
4.71M
      if (i == start+1)
1741
318k
         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
4.71M
      tf_change = tf_res[i];
1748
4.71M
      ctx.tf_change = tf_change;
1749
4.71M
      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
4.71M
      if (last && !theta_rdo)
1757
291k
         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
4.71M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1762
2.54M
      {
1763
2.54M
         int fold_start;
1764
2.54M
         int fold_end;
1765
2.54M
         int fold_i;
1766
         /* This ensures we never repeat spectral content within one band */
1767
2.54M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1768
2.54M
         fold_start = lowband_offset;
1769
2.85M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1770
2.54M
         fold_end = lowband_offset-1;
1771
2.54M
#ifndef DISABLE_UPDATE_DRAFT
1772
6.42M
         while(++fold_end < i && M*eBands[fold_end] < effective_lowband+norm_offset+N);
1773
#else
1774
         while(M*eBands[++fold_end] < effective_lowband+norm_offset+N);
1775
#endif
1776
2.54M
         x_cm = y_cm = 0;
1777
6.73M
         fold_i = fold_start; do {
1778
6.73M
           x_cm |= collapse_masks[fold_i*C+0];
1779
6.73M
           y_cm |= collapse_masks[fold_i*C+C-1];
1780
6.73M
         } while (++fold_i<fold_end);
1781
2.54M
      }
1782
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1783
         always) be non-zero. */
1784
2.16M
      else
1785
2.16M
         x_cm = y_cm = (1<<B)-1;
1786
1787
4.71M
      if (dual_stereo && i==intensity)
1788
15.9k
      {
1789
15.9k
         int j;
1790
1791
         /* Switch off dual stereo to do intensity. */
1792
15.9k
         dual_stereo = 0;
1793
15.9k
         if (resynth)
1794
223k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1795
216k
               norm[j] = HALF32(norm[j]+norm2[j]);
1796
15.9k
      }
1797
4.71M
      if (dual_stereo)
1798
189k
      {
1799
189k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1800
189k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1801
189k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1802
189k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1803
189k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1804
189k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1805
4.52M
      } else {
1806
4.52M
         if (Y!=NULL)
1807
1.41M
         {
1808
1.41M
            if (theta_rdo && i < intensity)
1809
244k
            {
1810
244k
               ec_ctx ec_save, ec_save2;
1811
244k
               struct band_ctx ctx_save, ctx_save2;
1812
244k
               opus_val32 dist0, dist1;
1813
244k
               unsigned cm, cm2;
1814
244k
               int nstart_bytes, nend_bytes, save_bytes;
1815
244k
               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
244k
               opus_val16 w[2];
1822
244k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1823
               /* Make a copy. */
1824
244k
               cm = x_cm|y_cm;
1825
244k
               ec_save = *ec;
1826
#ifdef ENABLE_QEXT
1827
               ext_ec_save = *ext_ec;
1828
#endif
1829
244k
               ctx_save = ctx;
1830
244k
               OPUS_COPY(X_save, X, N);
1831
244k
               OPUS_COPY(Y_save, Y, N);
1832
               /* Encode and round down. */
1833
244k
               ctx.theta_round = -1;
1834
244k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1835
244k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1836
244k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1837
244k
               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
244k
               cm2 = x_cm;
1841
244k
               ec_save2 = *ec;
1842
#ifdef ENABLE_QEXT
1843
               ext_ec_save2 = *ext_ec;
1844
#endif
1845
244k
               ctx_save2 = ctx;
1846
244k
               OPUS_COPY(X_save2, X, N);
1847
244k
               OPUS_COPY(Y_save2, Y, N);
1848
244k
               if (!last)
1849
242k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1850
244k
               nstart_bytes = ec_save.offs;
1851
244k
               nend_bytes = ec_save.storage;
1852
244k
               bytes_buf = ec_save.buf+nstart_bytes;
1853
244k
               save_bytes = nend_bytes-nstart_bytes;
1854
244k
               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
244k
               *ec = ec_save;
1864
#ifdef ENABLE_QEXT
1865
               *ext_ec = ext_ec_save;
1866
#endif
1867
244k
               ctx = ctx_save;
1868
244k
               OPUS_COPY(X, X_save, N);
1869
244k
               OPUS_COPY(Y, Y_save, N);
1870
244k
#ifndef DISABLE_UPDATE_DRAFT
1871
244k
               if (i == start+1)
1872
20.9k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1873
244k
#endif
1874
               /* Encode and round up. */
1875
244k
               ctx.theta_round = 1;
1876
244k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1877
244k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1878
244k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1879
244k
               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
244k
               if (dist0 >= dist1) {
1881
170k
                  x_cm = cm2;
1882
170k
                  *ec = ec_save2;
1883
#ifdef ENABLE_QEXT
1884
                  *ext_ec = ext_ec_save2;
1885
#endif
1886
170k
                  ctx = ctx_save2;
1887
170k
                  OPUS_COPY(X, X_save2, N);
1888
170k
                  OPUS_COPY(Y, Y_save2, N);
1889
170k
                  if (!last)
1890
168k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1891
170k
                  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
170k
               }
1896
1.16M
            } else {
1897
1.16M
               ctx.theta_round = 0;
1898
1.16M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1899
1.16M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1900
1.16M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1901
1.16M
            }
1902
3.11M
         } else {
1903
3.11M
            x_cm = quant_band(&ctx, X, N, b, B,
1904
3.11M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1905
3.11M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1906
3.11M
         }
1907
4.52M
         y_cm = x_cm;
1908
4.52M
      }
1909
4.71M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1910
4.71M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1911
4.71M
      balance += pulses[i] + tell;
1912
1913
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1914
4.71M
      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
4.71M
      ctx.avoid_split_noise = 0;
1918
4.71M
   }
1919
318k
   *seed = ctx.seed;
1920
1921
318k
   RESTORE_STACK;
1922
318k
}