Coverage Report

Created: 2026-01-16 07:17

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
110k
{
48
110k
   int i;
49
1.41M
   for (i=0;i<N;i++)
50
1.40M
   {
51
1.40M
      if (val < thresholds[i])
52
108k
         break;
53
1.40M
   }
54
110k
   if (i>prev && val < thresholds[prev]+hysteresis[prev])
55
1.03k
      i=prev;
56
110k
   if (i<prev && val > thresholds[prev-1]-hysteresis[prev-1])
57
739
      i=prev;
58
110k
   return i;
59
110k
}
60
61
opus_uint32 celt_lcg_rand(opus_uint32 seed)
62
50.7M
{
63
50.7M
   return 1664525 * seed + 1013904223;
64
50.7M
}
65
66
/* This is a cos() approximation designed to be bit-exact on any platform. Bit exactness
67
   with this approximation is important because it has an impact on the bit allocation */
68
opus_int16 bitexact_cos(opus_int16 x)
69
5.26M
{
70
5.26M
   opus_int32 tmp;
71
5.26M
   opus_int16 x2;
72
5.26M
   tmp = (4096+((opus_int32)(x)*(x)))>>13;
73
5.26M
   celt_sig_assert(tmp<=32767);
74
5.26M
   x2 = tmp;
75
5.26M
   x2 = (32767-x2) + FRAC_MUL16(x2, (-7651 + FRAC_MUL16(x2, (8277 + FRAC_MUL16(-626, x2)))));
76
5.26M
   celt_sig_assert(x2<=32766);
77
5.26M
   return 1+x2;
78
5.26M
}
79
80
int bitexact_log2tan(int isin,int icos)
81
2.63M
{
82
2.63M
   int lc;
83
2.63M
   int ls;
84
2.63M
   lc=EC_ILOG(icos);
85
2.63M
   ls=EC_ILOG(isin);
86
2.63M
   icos<<=15-lc;
87
2.63M
   isin<<=15-ls;
88
2.63M
   return (ls-lc)*(1<<11)
89
2.63M
         +FRAC_MUL16(isin, FRAC_MUL16(isin, -2597) + 7932)
90
2.63M
         -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932);
91
2.63M
}
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
231k
{
97
231k
   int i, c, N;
98
231k
   const opus_int16 *eBands = m->eBands;
99
231k
   (void)arch;
100
231k
   N = m->shortMdctSize<<LM;
101
312k
   c=0; do {
102
4.99M
      for (i=0;i<end;i++)
103
4.68M
      {
104
4.68M
         int j;
105
4.68M
         opus_val32 maxval=0;
106
4.68M
         opus_val32 sum = 0;
107
108
4.68M
         maxval = celt_maxabs32(&X[c*N+(eBands[i]<<LM)], (eBands[i+1]-eBands[i])<<LM);
109
4.68M
         if (maxval > 0)
110
3.81M
         {
111
3.81M
            int shift = IMAX(0, 30 - celt_ilog2(maxval+(maxval>>14)+1) - ((((m->logN[i]+7)>>BITRES)+LM+1)>>1));
112
39.2M
            j=eBands[i]<<LM; do {
113
39.2M
               opus_val32 x = SHL32(X[j+c*N],shift);
114
39.2M
               sum = ADD32(sum, MULT32_32_Q31(x, x));
115
39.2M
            } while (++j<eBands[i+1]<<LM);
116
3.81M
            bandE[i+c*m->nbEBands] = MAX32(maxval, PSHR32(celt_sqrt32(SHR32(sum,1)), shift));
117
3.81M
         } else {
118
866k
            bandE[i+c*m->nbEBands] = EPSILON;
119
866k
         }
120
4.68M
      }
121
312k
   } while (++c<C);
122
231k
}
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
175k
{
127
175k
   int i, c, N;
128
175k
   const opus_int16 *eBands = m->eBands;
129
175k
   N = M*m->shortMdctSize;
130
237k
   c=0; do {
131
3.46M
      i=0; do {
132
3.46M
         int j,shift;
133
3.46M
         opus_val32 E;
134
3.46M
         opus_val32 g;
135
3.46M
         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
3.46M
         if (E < 10) E += EPSILON;
139
3.46M
         shift = 30-celt_zlog2(E);
140
3.46M
         E = SHL32(E, shift);
141
3.46M
         g = celt_rcp_norm32(E);
142
32.0M
         j=M*eBands[i]; do {
143
32.0M
            X[j+c*N] = PSHR32(MULT32_32_Q31(g, SHL32(freq[j+c*N], shift)), 30-NORM_SHIFT);
144
32.0M
         } while (++j<M*eBands[i+1]);
145
3.46M
      } while (++i<end);
146
237k
   } while (++c<C);
147
175k
}
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
202k
{
153
202k
   int i, c, N;
154
202k
   const opus_int16 *eBands = m->eBands;
155
202k
   N = m->shortMdctSize<<LM;
156
263k
   c=0; do {
157
4.28M
      for (i=0;i<end;i++)
158
4.02M
      {
159
4.02M
         opus_val32 sum;
160
4.02M
         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.02M
         bandE[i+c*m->nbEBands] = celt_sqrt(sum);
162
         /*printf ("%f ", bandE[i+c*m->nbEBands]);*/
163
4.02M
      }
164
263k
   } while (++c<C);
165
   /*printf ("\n");*/
166
202k
}
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
160k
{
171
160k
   int i, c, N;
172
160k
   const opus_int16 *eBands = m->eBands;
173
160k
   N = M*m->shortMdctSize;
174
208k
   c=0; do {
175
3.32M
      for (i=0;i<end;i++)
176
3.11M
      {
177
3.11M
         int j;
178
3.11M
         opus_val16 g = 1.f/(1e-27f+bandE[i+c*m->nbEBands]);
179
34.9M
         for (j=M*eBands[i];j<M*eBands[i+1];j++)
180
31.8M
            X[j+c*N] = freq[j+c*N]*g;
181
3.11M
      }
182
208k
   } while (++c<C);
183
160k
}
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
479k
{
192
479k
   int i, N;
193
479k
   int bound;
194
479k
   celt_sig * OPUS_RESTRICT f;
195
479k
   const celt_norm * OPUS_RESTRICT x;
196
479k
   const opus_int16 *eBands = m->eBands;
197
479k
   N = M*m->shortMdctSize;
198
479k
   bound = M*eBands[end];
199
479k
   if (downsample!=1)
200
310k
      bound = IMIN(bound, N/downsample);
201
479k
   if (silence)
202
60.9k
   {
203
60.9k
      bound = 0;
204
60.9k
      start = end = 0;
205
60.9k
   }
206
479k
   f = freq;
207
479k
   x = X+M*eBands[start];
208
479k
   if (start != 0)
209
63.8k
   {
210
11.7M
      for (i=0;i<M*eBands[start];i++)
211
11.6M
         *f++ = 0;
212
416k
   } else {
213
416k
      f += M*eBands[start];
214
416k
   }
215
6.89M
   for (i=start;i<end;i++)
216
6.41M
   {
217
6.41M
      int j, band_end;
218
6.41M
      opus_val32 g;
219
6.41M
      celt_glog lg;
220
#ifdef FIXED_POINT
221
      int shift;
222
#endif
223
6.41M
      j=M*eBands[i];
224
6.41M
      band_end = M*eBands[i+1];
225
6.41M
      lg = ADD32(bandLogE[i], SHL32((opus_val32)eMeans[i],DB_SHIFT-4));
226
#ifndef FIXED_POINT
227
2.80M
      g = celt_exp2_db(MIN32(32.f, lg));
228
#else
229
      /* Handle the integer part of the log energy */
230
3.61M
      shift = 17-(lg>>DB_SHIFT);
231
3.61M
      if (shift>=31)
232
62.5k
      {
233
62.5k
         shift=0;
234
62.5k
         g=0;
235
3.55M
      } else {
236
         /* Handle the fractional part. */
237
3.55M
         g = SHL32(celt_exp2_db_frac((lg&((1<<DB_SHIFT)-1))), 2);
238
3.55M
      }
239
      /* Handle extreme gains with negative shift. */
240
3.61M
      if (shift<0)
241
44.1k
      {
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
44.1k
         g = 2147483647;
246
44.1k
         shift = 0;
247
44.1k
      }
248
#endif
249
63.0M
      do {
250
63.0M
         *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift);
251
63.0M
         x++;
252
63.0M
      } while (++j<band_end);
253
6.41M
   }
254
479k
   celt_assert(start <= end);
255
479k
   OPUS_CLEAR(&freq[bound], N-bound);
256
479k
}
denormalise_bands
Line
Count
Source
191
257k
{
192
257k
   int i, N;
193
257k
   int bound;
194
257k
   celt_sig * OPUS_RESTRICT f;
195
257k
   const celt_norm * OPUS_RESTRICT x;
196
257k
   const opus_int16 *eBands = m->eBands;
197
257k
   N = M*m->shortMdctSize;
198
257k
   bound = M*eBands[end];
199
257k
   if (downsample!=1)
200
161k
      bound = IMIN(bound, N/downsample);
201
257k
   if (silence)
202
28.0k
   {
203
28.0k
      bound = 0;
204
28.0k
      start = end = 0;
205
28.0k
   }
206
257k
   f = freq;
207
257k
   x = X+M*eBands[start];
208
257k
   if (start != 0)
209
30.9k
   {
210
5.60M
      for (i=0;i<M*eBands[start];i++)
211
5.57M
         *f++ = 0;
212
226k
   } else {
213
226k
      f += M*eBands[start];
214
226k
   }
215
3.87M
   for (i=start;i<end;i++)
216
3.61M
   {
217
3.61M
      int j, band_end;
218
3.61M
      opus_val32 g;
219
3.61M
      celt_glog lg;
220
3.61M
#ifdef FIXED_POINT
221
3.61M
      int shift;
222
3.61M
#endif
223
3.61M
      j=M*eBands[i];
224
3.61M
      band_end = M*eBands[i+1];
225
3.61M
      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.61M
      shift = 17-(lg>>DB_SHIFT);
231
3.61M
      if (shift>=31)
232
62.5k
      {
233
62.5k
         shift=0;
234
62.5k
         g=0;
235
3.55M
      } else {
236
         /* Handle the fractional part. */
237
3.55M
         g = SHL32(celt_exp2_db_frac((lg&((1<<DB_SHIFT)-1))), 2);
238
3.55M
      }
239
      /* Handle extreme gains with negative shift. */
240
3.61M
      if (shift<0)
241
44.1k
      {
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
44.1k
         g = 2147483647;
246
44.1k
         shift = 0;
247
44.1k
      }
248
3.61M
#endif
249
33.6M
      do {
250
33.6M
         *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift);
251
33.6M
         x++;
252
33.6M
      } while (++j<band_end);
253
3.61M
   }
254
257k
   celt_assert(start <= end);
255
257k
   OPUS_CLEAR(&freq[bound], N-bound);
256
257k
}
denormalise_bands
Line
Count
Source
191
222k
{
192
222k
   int i, N;
193
222k
   int bound;
194
222k
   celt_sig * OPUS_RESTRICT f;
195
222k
   const celt_norm * OPUS_RESTRICT x;
196
222k
   const opus_int16 *eBands = m->eBands;
197
222k
   N = M*m->shortMdctSize;
198
222k
   bound = M*eBands[end];
199
222k
   if (downsample!=1)
200
149k
      bound = IMIN(bound, N/downsample);
201
222k
   if (silence)
202
32.9k
   {
203
32.9k
      bound = 0;
204
32.9k
      start = end = 0;
205
32.9k
   }
206
222k
   f = freq;
207
222k
   x = X+M*eBands[start];
208
222k
   if (start != 0)
209
32.8k
   {
210
6.14M
      for (i=0;i<M*eBands[start];i++)
211
6.11M
         *f++ = 0;
212
189k
   } else {
213
189k
      f += M*eBands[start];
214
189k
   }
215
3.02M
   for (i=start;i<end;i++)
216
2.80M
   {
217
2.80M
      int j, band_end;
218
2.80M
      opus_val32 g;
219
2.80M
      celt_glog lg;
220
#ifdef FIXED_POINT
221
      int shift;
222
#endif
223
2.80M
      j=M*eBands[i];
224
2.80M
      band_end = M*eBands[i+1];
225
2.80M
      lg = ADD32(bandLogE[i], SHL32((opus_val32)eMeans[i],DB_SHIFT-4));
226
2.80M
#ifndef FIXED_POINT
227
2.80M
      g = celt_exp2_db(MIN32(32.f, lg));
228
#else
229
      /* Handle the integer part of the log energy */
230
      shift = 17-(lg>>DB_SHIFT);
231
      if (shift>=31)
232
      {
233
         shift=0;
234
         g=0;
235
      } else {
236
         /* Handle the fractional part. */
237
         g = SHL32(celt_exp2_db_frac((lg&((1<<DB_SHIFT)-1))), 2);
238
      }
239
      /* Handle extreme gains with negative shift. */
240
      if (shift<0)
241
      {
242
         /* To avoid overflow, we're
243
            capping the gain here, which is equivalent to a cap of 18 on lg.
244
            This shouldn't trigger unless the bitstream is already corrupted. */
245
         g = 2147483647;
246
         shift = 0;
247
      }
248
#endif
249
29.3M
      do {
250
29.3M
         *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift);
251
29.3M
         x++;
252
29.3M
      } while (++j<band_end);
253
2.80M
   }
254
222k
   celt_assert(start <= end);
255
222k
   OPUS_CLEAR(&freq[bound], N-bound);
256
222k
}
257
258
/* This prevents energy collapse for transients with multiple short MDCTs */
259
void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_masks, int LM, int C, int size,
260
      int start, int end, const celt_glog *logE, const celt_glog *prev1logE,
261
      const celt_glog *prev2logE, const int *pulses, opus_uint32 seed, int encode, int arch)
262
6.73k
{
263
6.73k
   int c, i, j, k;
264
114k
   for (i=start;i<end;i++)
265
107k
   {
266
107k
      int N0;
267
107k
      opus_val16 thresh, sqrt_1;
268
107k
      int depth;
269
#ifdef FIXED_POINT
270
      int shift;
271
      opus_val32 thresh32;
272
#endif
273
274
107k
      N0 = m->eBands[i+1]-m->eBands[i];
275
      /* depth in 1/8 bits */
276
107k
      celt_sig_assert(pulses[i]>=0);
277
107k
      depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i]))>>LM;
278
279
#ifdef FIXED_POINT
280
56.2k
      thresh32 = SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1);
281
56.2k
      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
56.2k
         t = SHL32(t, (7-shift)<<1);
287
         sqrt_1 = celt_rsqrt_norm(t);
288
      }
289
#else
290
      thresh = .5f*celt_exp2(-.125f*depth);
291
51.4k
      sqrt_1 = celt_rsqrt(N0<<LM);
292
#endif
293
294
107k
      c=0; do
295
187k
      {
296
187k
         celt_norm *X;
297
187k
         celt_glog prev1;
298
187k
         celt_glog prev2;
299
187k
         opus_val32 Ediff;
300
187k
         celt_norm r;
301
187k
         int renormalize=0;
302
187k
         prev1 = prev1logE[c*m->nbEBands+i];
303
187k
         prev2 = prev2logE[c*m->nbEBands+i];
304
187k
         if (!encode && C==1)
305
27.7k
         {
306
27.7k
            prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]);
307
27.7k
            prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]);
308
27.7k
         }
309
187k
         Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2);
310
187k
         Ediff = MAX32(0, Ediff);
311
312
#ifdef FIXED_POINT
313
96.1k
         if (Ediff < GCONST(16.f))
314
16.9k
         {
315
16.9k
            opus_val32 r32 = SHR32(celt_exp2_db(-Ediff),1);
316
16.9k
            r = 2*MIN16(16383,r32);
317
79.2k
         } else {
318
79.2k
            r = 0;
319
79.2k
         }
320
96.1k
         if (LM==3)
321
15.9k
            r = MULT16_16_Q14(23170, MIN32(23169, r));
322
96.1k
         r = SHR16(MIN16(thresh, r),1);
323
96.1k
         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
91.4k
         r = 2.f*celt_exp2_db(-Ediff);
328
91.4k
         if (LM==3)
329
14.8k
            r *= 1.41421356f;
330
91.4k
         r = MIN16(thresh, r);
331
         r = r*sqrt_1;
332
#endif
333
187k
         X = X_+c*size+(m->eBands[i]<<LM);
334
1.06M
         for (k=0;k<1<<LM;k++)
335
873k
         {
336
            /* Detect collapse */
337
873k
            if (!(collapse_masks[i*C+c]&1<<k))
338
88.2k
            {
339
               /* Fill with noise */
340
339k
               for (j=0;j<N0;j++)
341
251k
               {
342
251k
                  seed = celt_lcg_rand(seed);
343
251k
                  X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
344
251k
               }
345
88.2k
               renormalize = 1;
346
88.2k
            }
347
873k
         }
348
         /* We just added some energy, so we need to renormalise */
349
187k
         if (renormalize)
350
33.8k
            renormalise_vector(X, N0<<LM, Q31ONE, arch);
351
187k
      } while (++c<C);
352
107k
   }
353
6.73k
}
anti_collapse
Line
Count
Source
262
3.54k
{
263
3.54k
   int c, i, j, k;
264
59.7k
   for (i=start;i<end;i++)
265
56.2k
   {
266
56.2k
      int N0;
267
56.2k
      opus_val16 thresh, sqrt_1;
268
56.2k
      int depth;
269
56.2k
#ifdef FIXED_POINT
270
56.2k
      int shift;
271
56.2k
      opus_val32 thresh32;
272
56.2k
#endif
273
274
56.2k
      N0 = m->eBands[i+1]-m->eBands[i];
275
      /* depth in 1/8 bits */
276
56.2k
      celt_sig_assert(pulses[i]>=0);
277
56.2k
      depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i]))>>LM;
278
279
56.2k
#ifdef FIXED_POINT
280
56.2k
      thresh32 = SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1);
281
56.2k
      thresh = MULT16_32_Q15(QCONST16(0.5f, 15), MIN32(32767,thresh32));
282
56.2k
      {
283
56.2k
         opus_val32 t;
284
56.2k
         t = N0<<LM;
285
56.2k
         shift = celt_ilog2(t)>>1;
286
56.2k
         t = SHL32(t, (7-shift)<<1);
287
56.2k
         sqrt_1 = celt_rsqrt_norm(t);
288
56.2k
      }
289
#else
290
      thresh = .5f*celt_exp2(-.125f*depth);
291
      sqrt_1 = celt_rsqrt(N0<<LM);
292
#endif
293
294
56.2k
      c=0; do
295
96.1k
      {
296
96.1k
         celt_norm *X;
297
96.1k
         celt_glog prev1;
298
96.1k
         celt_glog prev2;
299
96.1k
         opus_val32 Ediff;
300
96.1k
         celt_norm r;
301
96.1k
         int renormalize=0;
302
96.1k
         prev1 = prev1logE[c*m->nbEBands+i];
303
96.1k
         prev2 = prev2logE[c*m->nbEBands+i];
304
96.1k
         if (!encode && C==1)
305
16.2k
         {
306
16.2k
            prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]);
307
16.2k
            prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]);
308
16.2k
         }
309
96.1k
         Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2);
310
96.1k
         Ediff = MAX32(0, Ediff);
311
312
96.1k
#ifdef FIXED_POINT
313
96.1k
         if (Ediff < GCONST(16.f))
314
16.9k
         {
315
16.9k
            opus_val32 r32 = SHR32(celt_exp2_db(-Ediff),1);
316
16.9k
            r = 2*MIN16(16383,r32);
317
79.2k
         } else {
318
79.2k
            r = 0;
319
79.2k
         }
320
96.1k
         if (LM==3)
321
15.9k
            r = MULT16_16_Q14(23170, MIN32(23169, r));
322
96.1k
         r = SHR16(MIN16(thresh, r),1);
323
96.1k
         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
96.1k
         X = X_+c*size+(m->eBands[i]<<LM);
334
544k
         for (k=0;k<1<<LM;k++)
335
448k
         {
336
            /* Detect collapse */
337
448k
            if (!(collapse_masks[i*C+c]&1<<k))
338
50.0k
            {
339
               /* Fill with noise */
340
191k
               for (j=0;j<N0;j++)
341
141k
               {
342
141k
                  seed = celt_lcg_rand(seed);
343
141k
                  X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
344
141k
               }
345
50.0k
               renormalize = 1;
346
50.0k
            }
347
448k
         }
348
         /* We just added some energy, so we need to renormalise */
349
96.1k
         if (renormalize)
350
18.0k
            renormalise_vector(X, N0<<LM, Q31ONE, arch);
351
96.1k
      } while (++c<C);
352
56.2k
   }
353
3.54k
}
anti_collapse
Line
Count
Source
262
3.19k
{
263
3.19k
   int c, i, j, k;
264
54.6k
   for (i=start;i<end;i++)
265
51.4k
   {
266
51.4k
      int N0;
267
51.4k
      opus_val16 thresh, sqrt_1;
268
51.4k
      int depth;
269
#ifdef FIXED_POINT
270
      int shift;
271
      opus_val32 thresh32;
272
#endif
273
274
51.4k
      N0 = m->eBands[i+1]-m->eBands[i];
275
      /* depth in 1/8 bits */
276
51.4k
      celt_sig_assert(pulses[i]>=0);
277
51.4k
      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
51.4k
      thresh = .5f*celt_exp2(-.125f*depth);
291
51.4k
      sqrt_1 = celt_rsqrt(N0<<LM);
292
51.4k
#endif
293
294
51.4k
      c=0; do
295
91.4k
      {
296
91.4k
         celt_norm *X;
297
91.4k
         celt_glog prev1;
298
91.4k
         celt_glog prev2;
299
91.4k
         opus_val32 Ediff;
300
91.4k
         celt_norm r;
301
91.4k
         int renormalize=0;
302
91.4k
         prev1 = prev1logE[c*m->nbEBands+i];
303
91.4k
         prev2 = prev2logE[c*m->nbEBands+i];
304
91.4k
         if (!encode && C==1)
305
11.4k
         {
306
11.4k
            prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]);
307
11.4k
            prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]);
308
11.4k
         }
309
91.4k
         Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2);
310
91.4k
         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
91.4k
         r = 2.f*celt_exp2_db(-Ediff);
328
91.4k
         if (LM==3)
329
14.8k
            r *= 1.41421356f;
330
91.4k
         r = MIN16(thresh, r);
331
91.4k
         r = r*sqrt_1;
332
91.4k
#endif
333
91.4k
         X = X_+c*size+(m->eBands[i]<<LM);
334
516k
         for (k=0;k<1<<LM;k++)
335
424k
         {
336
            /* Detect collapse */
337
424k
            if (!(collapse_masks[i*C+c]&1<<k))
338
38.1k
            {
339
               /* Fill with noise */
340
147k
               for (j=0;j<N0;j++)
341
109k
               {
342
109k
                  seed = celt_lcg_rand(seed);
343
109k
                  X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
344
109k
               }
345
38.1k
               renormalize = 1;
346
38.1k
            }
347
424k
         }
348
         /* We just added some energy, so we need to renormalise */
349
91.4k
         if (renormalize)
350
15.8k
            renormalise_vector(X, N0<<LM, Q31ONE, arch);
351
91.4k
      } while (++c<C);
352
51.4k
   }
353
3.19k
}
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
459k
{
364
459k
   celt_ener minE;
365
#ifdef FIXED_POINT
366
   int shift;
367
#endif
368
459k
   minE = MIN32(Ex, Ey);
369
   /* Adjustment to make the weights a bit more conservative. */
370
459k
   Ex = ADD32(Ex, minE/3);
371
459k
   Ey = ADD32(Ey, minE/3);
372
#ifdef FIXED_POINT
373
286k
   shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
374
#endif
375
459k
   w[0] = VSHR32(Ex, shift);
376
459k
   w[1] = VSHR32(Ey, shift);
377
459k
}
bands.c:compute_channel_weights
Line
Count
Source
363
286k
{
364
286k
   celt_ener minE;
365
286k
#ifdef FIXED_POINT
366
286k
   int shift;
367
286k
#endif
368
286k
   minE = MIN32(Ex, Ey);
369
   /* Adjustment to make the weights a bit more conservative. */
370
286k
   Ex = ADD32(Ex, minE/3);
371
286k
   Ey = ADD32(Ey, minE/3);
372
286k
#ifdef FIXED_POINT
373
286k
   shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
374
286k
#endif
375
286k
   w[0] = VSHR32(Ex, shift);
376
286k
   w[1] = VSHR32(Ey, shift);
377
286k
}
bands.c:compute_channel_weights
Line
Count
Source
363
173k
{
364
173k
   celt_ener minE;
365
#ifdef FIXED_POINT
366
   int shift;
367
#endif
368
173k
   minE = MIN32(Ex, Ey);
369
   /* Adjustment to make the weights a bit more conservative. */
370
173k
   Ex = ADD32(Ex, minE/3);
371
173k
   Ey = ADD32(Ey, minE/3);
372
#ifdef FIXED_POINT
373
   shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
374
#endif
375
173k
   w[0] = VSHR32(Ex, shift);
376
173k
   w[1] = VSHR32(Ey, shift);
377
173k
}
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
677k
{
381
677k
   int i = bandID;
382
677k
   int j;
383
677k
   opus_val16 a1, a2;
384
677k
   opus_val16 left, right;
385
677k
   opus_val16 norm;
386
#ifdef FIXED_POINT
387
363k
   int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
388
#endif
389
677k
   left = VSHR32(bandE[i],shift);
390
677k
   right = VSHR32(bandE[i+m->nbEBands],shift);
391
677k
   norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
392
#ifdef FIXED_POINT
393
363k
   left = MIN32(left, norm-1);
394
363k
   right = MIN32(right, norm-1);
395
#endif
396
677k
   a1 = DIV32_16(SHL32(EXTEND32(left),15),norm);
397
677k
   a2 = DIV32_16(SHL32(EXTEND32(right),15),norm);
398
10.5M
   for (j=0;j<N;j++)
399
9.91M
   {
400
9.91M
      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
9.91M
   }
403
677k
}
bands.c:intensity_stereo
Line
Count
Source
380
363k
{
381
363k
   int i = bandID;
382
363k
   int j;
383
363k
   opus_val16 a1, a2;
384
363k
   opus_val16 left, right;
385
363k
   opus_val16 norm;
386
363k
#ifdef FIXED_POINT
387
363k
   int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
388
363k
#endif
389
363k
   left = VSHR32(bandE[i],shift);
390
363k
   right = VSHR32(bandE[i+m->nbEBands],shift);
391
363k
   norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
392
363k
#ifdef FIXED_POINT
393
363k
   left = MIN32(left, norm-1);
394
363k
   right = MIN32(right, norm-1);
395
363k
#endif
396
363k
   a1 = DIV32_16(SHL32(EXTEND32(left),15),norm);
397
363k
   a2 = DIV32_16(SHL32(EXTEND32(right),15),norm);
398
5.53M
   for (j=0;j<N;j++)
399
5.16M
   {
400
5.16M
      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
5.16M
   }
403
363k
}
bands.c:intensity_stereo
Line
Count
Source
380
313k
{
381
313k
   int i = bandID;
382
313k
   int j;
383
313k
   opus_val16 a1, a2;
384
313k
   opus_val16 left, right;
385
313k
   opus_val16 norm;
386
#ifdef FIXED_POINT
387
   int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
388
#endif
389
313k
   left = VSHR32(bandE[i],shift);
390
313k
   right = VSHR32(bandE[i+m->nbEBands],shift);
391
313k
   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
313k
   a1 = DIV32_16(SHL32(EXTEND32(left),15),norm);
397
313k
   a2 = DIV32_16(SHL32(EXTEND32(right),15),norm);
398
5.06M
   for (j=0;j<N;j++)
399
4.75M
   {
400
4.75M
      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.75M
   }
403
313k
}
404
405
static void stereo_split(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, int N)
406
1.35M
{
407
1.35M
   int j;
408
12.8M
   for (j=0;j<N;j++)
409
11.4M
   {
410
11.4M
      opus_val32 r, l;
411
11.4M
      l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]);
412
11.4M
      r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]);
413
11.4M
      X[j] = ADD32(l, r);
414
11.4M
      Y[j] = SUB32(r, l);
415
11.4M
   }
416
1.35M
}
bands.c:stereo_split
Line
Count
Source
406
675k
{
407
675k
   int j;
408
6.40M
   for (j=0;j<N;j++)
409
5.73M
   {
410
5.73M
      opus_val32 r, l;
411
5.73M
      l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]);
412
5.73M
      r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]);
413
5.73M
      X[j] = ADD32(l, r);
414
5.73M
      Y[j] = SUB32(r, l);
415
5.73M
   }
416
675k
}
bands.c:stereo_split
Line
Count
Source
406
675k
{
407
675k
   int j;
408
6.40M
   for (j=0;j<N;j++)
409
5.73M
   {
410
5.73M
      opus_val32 r, l;
411
5.73M
      l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]);
412
5.73M
      r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]);
413
5.73M
      X[j] = ADD32(l, r);
414
5.73M
      Y[j] = SUB32(r, l);
415
5.73M
   }
416
675k
}
417
418
static void stereo_merge(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, opus_val32 mid, int N, int arch)
419
1.65M
{
420
1.65M
   int j;
421
1.65M
   opus_val32 xp=0, side=0;
422
1.65M
   opus_val32 El, Er;
423
#ifdef FIXED_POINT
424
   int kl, kr;
425
#endif
426
1.65M
   opus_val32 t, lgain, rgain;
427
428
   /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
429
1.65M
   xp = celt_inner_prod_norm_shift(Y, X, N, arch);
430
1.65M
   side = celt_inner_prod_norm_shift(Y, Y, N, arch);
431
   /* Compensating for the mid normalization */
432
1.65M
   xp = MULT32_32_Q31(mid, xp);
433
   /* mid and side are in Q15, not Q14 like X and Y */
434
1.65M
   El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp;
435
1.65M
   Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp;
436
1.65M
   if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
437
4.24k
   {
438
4.24k
      OPUS_COPY(Y, X, N);
439
4.24k
      return;
440
4.24k
   }
441
442
#ifdef FIXED_POINT
443
952k
   kl = celt_ilog2(El)>>1;
444
952k
   kr = celt_ilog2(Er)>>1;
445
952k
#endif
446
1.64M
   t = VSHR32(El, (kl<<1)-29);
447
694k
   lgain = celt_rsqrt_norm32(t);
448
1.64M
   t = VSHR32(Er, (kr<<1)-29);
449
694k
   rgain = celt_rsqrt_norm32(t);
450
451
#ifdef FIXED_POINT
452
952k
   if (kl < 7)
453
0
      kl = 7;
454
952k
   if (kr < 7)
455
0
      kr = 7;
456
#endif
457
458
29.3M
   for (j=0;j<N;j++)
459
27.7M
   {
460
27.7M
      celt_norm r, l;
461
      /* Apply mid scaling (side is already scaled) */
462
27.7M
      l = MULT32_32_Q31(mid, X[j]);
463
27.7M
      r = Y[j];
464
27.7M
      X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15);
465
27.7M
      Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15);
466
27.7M
   }
467
694k
}
bands.c:stereo_merge
Line
Count
Source
419
955k
{
420
955k
   int j;
421
955k
   opus_val32 xp=0, side=0;
422
955k
   opus_val32 El, Er;
423
955k
#ifdef FIXED_POINT
424
955k
   int kl, kr;
425
955k
#endif
426
955k
   opus_val32 t, lgain, rgain;
427
428
   /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
429
955k
   xp = celt_inner_prod_norm_shift(Y, X, N, arch);
430
955k
   side = celt_inner_prod_norm_shift(Y, Y, N, arch);
431
   /* Compensating for the mid normalization */
432
955k
   xp = MULT32_32_Q31(mid, xp);
433
   /* mid and side are in Q15, not Q14 like X and Y */
434
955k
   El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp;
435
955k
   Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp;
436
955k
   if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
437
3.02k
   {
438
3.02k
      OPUS_COPY(Y, X, N);
439
3.02k
      return;
440
3.02k
   }
441
442
952k
#ifdef FIXED_POINT
443
952k
   kl = celt_ilog2(El)>>1;
444
952k
   kr = celt_ilog2(Er)>>1;
445
952k
#endif
446
952k
   t = VSHR32(El, (kl<<1)-29);
447
952k
   lgain = celt_rsqrt_norm32(t);
448
952k
   t = VSHR32(Er, (kr<<1)-29);
449
952k
   rgain = celt_rsqrt_norm32(t);
450
451
952k
#ifdef FIXED_POINT
452
952k
   if (kl < 7)
453
0
      kl = 7;
454
952k
   if (kr < 7)
455
0
      kr = 7;
456
952k
#endif
457
458
16.7M
   for (j=0;j<N;j++)
459
15.8M
   {
460
15.8M
      celt_norm r, l;
461
      /* Apply mid scaling (side is already scaled) */
462
15.8M
      l = MULT32_32_Q31(mid, X[j]);
463
15.8M
      r = Y[j];
464
15.8M
      X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15);
465
15.8M
      Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15);
466
15.8M
   }
467
952k
}
bands.c:stereo_merge
Line
Count
Source
419
696k
{
420
696k
   int j;
421
696k
   opus_val32 xp=0, side=0;
422
696k
   opus_val32 El, Er;
423
#ifdef FIXED_POINT
424
   int kl, kr;
425
#endif
426
696k
   opus_val32 t, lgain, rgain;
427
428
   /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
429
696k
   xp = celt_inner_prod_norm_shift(Y, X, N, arch);
430
696k
   side = celt_inner_prod_norm_shift(Y, Y, N, arch);
431
   /* Compensating for the mid normalization */
432
696k
   xp = MULT32_32_Q31(mid, xp);
433
   /* mid and side are in Q15, not Q14 like X and Y */
434
696k
   El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp;
435
696k
   Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp;
436
696k
   if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
437
1.22k
   {
438
1.22k
      OPUS_COPY(Y, X, N);
439
1.22k
      return;
440
1.22k
   }
441
442
#ifdef FIXED_POINT
443
   kl = celt_ilog2(El)>>1;
444
   kr = celt_ilog2(Er)>>1;
445
#endif
446
694k
   t = VSHR32(El, (kl<<1)-29);
447
694k
   lgain = celt_rsqrt_norm32(t);
448
694k
   t = VSHR32(Er, (kr<<1)-29);
449
694k
   rgain = celt_rsqrt_norm32(t);
450
451
#ifdef FIXED_POINT
452
   if (kl < 7)
453
      kl = 7;
454
   if (kr < 7)
455
      kr = 7;
456
#endif
457
458
12.5M
   for (j=0;j<N;j++)
459
11.8M
   {
460
11.8M
      celt_norm r, l;
461
      /* Apply mid scaling (side is already scaled) */
462
11.8M
      l = MULT32_32_Q31(mid, X[j]);
463
11.8M
      r = Y[j];
464
11.8M
      X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15);
465
11.8M
      Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15);
466
11.8M
   }
467
694k
}
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
196k
{
474
196k
   int i, c, N0;
475
196k
   int sum = 0, nbBands=0;
476
196k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
477
196k
   int decision;
478
196k
   int hf_sum=0;
479
480
196k
   celt_assert(end>0);
481
482
196k
   N0 = M*m->shortMdctSize;
483
484
196k
   if (M*(eBands[end]-eBands[end-1]) <= 8)
485
122k
      return SPREAD_NONE;
486
100k
   c=0; do {
487
1.74M
      for (i=0;i<end;i++)
488
1.64M
      {
489
1.64M
         int j, N, tmp=0;
490
1.64M
         int tcount[3] = {0,0,0};
491
1.64M
         const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
492
1.64M
         N = M*(eBands[i+1]-eBands[i]);
493
1.64M
         if (N<=8)
494
1.15M
            continue;
495
         /* Compute rough CDF of |x[j]| */
496
12.9M
         for (j=0;j<N;j++)
497
12.4M
         {
498
12.4M
            opus_val32 x2N; /* Q13 */
499
500
12.4M
            x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N);
501
12.4M
            if (x2N < QCONST16(0.25f,13))
502
6.93M
               tcount[0]++;
503
12.4M
            if (x2N < QCONST16(0.0625f,13))
504
5.18M
               tcount[1]++;
505
12.4M
            if (x2N < QCONST16(0.015625f,13))
506
4.05M
               tcount[2]++;
507
12.4M
         }
508
509
         /* Only include four last bands (8 kHz and up) */
510
493k
         if (i>m->nbEBands-4)
511
70.1k
            hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
512
493k
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
513
493k
         sum += tmp*spread_weight[i];
514
493k
         nbBands+=spread_weight[i];
515
493k
      }
516
100k
   } while (++c<C);
517
518
74.0k
   if (update_hf)
519
6.46k
   {
520
6.46k
      if (hf_sum)
521
3.80k
         hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
522
6.46k
      *hf_average = (*hf_average+hf_sum)>>1;
523
6.46k
      hf_sum = *hf_average;
524
6.46k
      if (*tapset_decision==2)
525
268
         hf_sum += 4;
526
6.20k
      else if (*tapset_decision==0)
527
5.83k
         hf_sum -= 4;
528
6.46k
      if (hf_sum > 22)
529
522
         *tapset_decision=2;
530
5.94k
      else if (hf_sum > 18)
531
522
         *tapset_decision=1;
532
5.42k
      else
533
5.42k
         *tapset_decision=0;
534
6.46k
   }
535
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
536
74.0k
   celt_assert(nbBands>0); /* end has to be non-zero */
537
74.0k
   celt_assert(sum>=0);
538
74.0k
   sum = celt_udiv((opus_int32)sum<<8, nbBands);
539
   /* Recursive averaging */
540
74.0k
   sum = (sum+*average)>>1;
541
74.0k
   *average = sum;
542
   /* Hysteresis */
543
74.0k
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
544
74.0k
   if (sum < 80)
545
2.88k
   {
546
2.88k
      decision = SPREAD_AGGRESSIVE;
547
71.1k
   } else if (sum < 256)
548
51.5k
   {
549
51.5k
      decision = SPREAD_NORMAL;
550
51.5k
   } else if (sum < 384)
551
7.03k
   {
552
7.03k
      decision = SPREAD_LIGHT;
553
12.6k
   } else {
554
12.6k
      decision = SPREAD_NONE;
555
12.6k
   }
556
#ifdef FUZZING
557
   decision = rand()&0x3;
558
   *tapset_decision=rand()%3;
559
#endif
560
74.0k
   return decision;
561
74.0k
}
spreading_decision
Line
Count
Source
473
98.1k
{
474
98.1k
   int i, c, N0;
475
98.1k
   int sum = 0, nbBands=0;
476
98.1k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
477
98.1k
   int decision;
478
98.1k
   int hf_sum=0;
479
480
98.1k
   celt_assert(end>0);
481
482
98.1k
   N0 = M*m->shortMdctSize;
483
484
98.1k
   if (M*(eBands[end]-eBands[end-1]) <= 8)
485
61.1k
      return SPREAD_NONE;
486
50.4k
   c=0; do {
487
872k
      for (i=0;i<end;i++)
488
821k
      {
489
821k
         int j, N, tmp=0;
490
821k
         int tcount[3] = {0,0,0};
491
821k
         const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
492
821k
         N = M*(eBands[i+1]-eBands[i]);
493
821k
         if (N<=8)
494
575k
            continue;
495
         /* Compute rough CDF of |x[j]| */
496
6.48M
         for (j=0;j<N;j++)
497
6.23M
         {
498
6.23M
            opus_val32 x2N; /* Q13 */
499
500
6.23M
            x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N);
501
6.23M
            if (x2N < QCONST16(0.25f,13))
502
3.46M
               tcount[0]++;
503
6.23M
            if (x2N < QCONST16(0.0625f,13))
504
2.59M
               tcount[1]++;
505
6.23M
            if (x2N < QCONST16(0.015625f,13))
506
2.02M
               tcount[2]++;
507
6.23M
         }
508
509
         /* Only include four last bands (8 kHz and up) */
510
246k
         if (i>m->nbEBands-4)
511
35.0k
            hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
512
246k
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
513
246k
         sum += tmp*spread_weight[i];
514
246k
         nbBands+=spread_weight[i];
515
246k
      }
516
50.4k
   } while (++c<C);
517
518
37.0k
   if (update_hf)
519
3.23k
   {
520
3.23k
      if (hf_sum)
521
1.90k
         hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
522
3.23k
      *hf_average = (*hf_average+hf_sum)>>1;
523
3.23k
      hf_sum = *hf_average;
524
3.23k
      if (*tapset_decision==2)
525
134
         hf_sum += 4;
526
3.10k
      else if (*tapset_decision==0)
527
2.91k
         hf_sum -= 4;
528
3.23k
      if (hf_sum > 22)
529
261
         *tapset_decision=2;
530
2.97k
      else if (hf_sum > 18)
531
261
         *tapset_decision=1;
532
2.71k
      else
533
2.71k
         *tapset_decision=0;
534
3.23k
   }
535
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
536
37.0k
   celt_assert(nbBands>0); /* end has to be non-zero */
537
37.0k
   celt_assert(sum>=0);
538
37.0k
   sum = celt_udiv((opus_int32)sum<<8, nbBands);
539
   /* Recursive averaging */
540
37.0k
   sum = (sum+*average)>>1;
541
37.0k
   *average = sum;
542
   /* Hysteresis */
543
37.0k
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
544
37.0k
   if (sum < 80)
545
1.44k
   {
546
1.44k
      decision = SPREAD_AGGRESSIVE;
547
35.5k
   } else if (sum < 256)
548
25.7k
   {
549
25.7k
      decision = SPREAD_NORMAL;
550
25.7k
   } else if (sum < 384)
551
3.51k
   {
552
3.51k
      decision = SPREAD_LIGHT;
553
6.32k
   } else {
554
6.32k
      decision = SPREAD_NONE;
555
6.32k
   }
556
#ifdef FUZZING
557
   decision = rand()&0x3;
558
   *tapset_decision=rand()%3;
559
#endif
560
37.0k
   return decision;
561
37.0k
}
spreading_decision
Line
Count
Source
473
98.1k
{
474
98.1k
   int i, c, N0;
475
98.1k
   int sum = 0, nbBands=0;
476
98.1k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
477
98.1k
   int decision;
478
98.1k
   int hf_sum=0;
479
480
98.1k
   celt_assert(end>0);
481
482
98.1k
   N0 = M*m->shortMdctSize;
483
484
98.1k
   if (M*(eBands[end]-eBands[end-1]) <= 8)
485
61.1k
      return SPREAD_NONE;
486
50.4k
   c=0; do {
487
872k
      for (i=0;i<end;i++)
488
821k
      {
489
821k
         int j, N, tmp=0;
490
821k
         int tcount[3] = {0,0,0};
491
821k
         const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
492
821k
         N = M*(eBands[i+1]-eBands[i]);
493
821k
         if (N<=8)
494
575k
            continue;
495
         /* Compute rough CDF of |x[j]| */
496
6.48M
         for (j=0;j<N;j++)
497
6.23M
         {
498
6.23M
            opus_val32 x2N; /* Q13 */
499
500
6.23M
            x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N);
501
6.23M
            if (x2N < QCONST16(0.25f,13))
502
3.46M
               tcount[0]++;
503
6.23M
            if (x2N < QCONST16(0.0625f,13))
504
2.59M
               tcount[1]++;
505
6.23M
            if (x2N < QCONST16(0.015625f,13))
506
2.02M
               tcount[2]++;
507
6.23M
         }
508
509
         /* Only include four last bands (8 kHz and up) */
510
246k
         if (i>m->nbEBands-4)
511
35.0k
            hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
512
246k
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
513
246k
         sum += tmp*spread_weight[i];
514
246k
         nbBands+=spread_weight[i];
515
246k
      }
516
50.4k
   } while (++c<C);
517
518
37.0k
   if (update_hf)
519
3.23k
   {
520
3.23k
      if (hf_sum)
521
1.90k
         hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
522
3.23k
      *hf_average = (*hf_average+hf_sum)>>1;
523
3.23k
      hf_sum = *hf_average;
524
3.23k
      if (*tapset_decision==2)
525
134
         hf_sum += 4;
526
3.10k
      else if (*tapset_decision==0)
527
2.91k
         hf_sum -= 4;
528
3.23k
      if (hf_sum > 22)
529
261
         *tapset_decision=2;
530
2.97k
      else if (hf_sum > 18)
531
261
         *tapset_decision=1;
532
2.71k
      else
533
2.71k
         *tapset_decision=0;
534
3.23k
   }
535
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
536
37.0k
   celt_assert(nbBands>0); /* end has to be non-zero */
537
37.0k
   celt_assert(sum>=0);
538
37.0k
   sum = celt_udiv((opus_int32)sum<<8, nbBands);
539
   /* Recursive averaging */
540
37.0k
   sum = (sum+*average)>>1;
541
37.0k
   *average = sum;
542
   /* Hysteresis */
543
37.0k
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
544
37.0k
   if (sum < 80)
545
1.44k
   {
546
1.44k
      decision = SPREAD_AGGRESSIVE;
547
35.5k
   } else if (sum < 256)
548
25.7k
   {
549
25.7k
      decision = SPREAD_NORMAL;
550
25.7k
   } else if (sum < 384)
551
3.51k
   {
552
3.51k
      decision = SPREAD_LIGHT;
553
6.32k
   } else {
554
6.32k
      decision = SPREAD_NONE;
555
6.32k
   }
556
#ifdef FUZZING
557
   decision = rand()&0x3;
558
   *tapset_decision=rand()%3;
559
#endif
560
37.0k
   return decision;
561
37.0k
}
562
563
/* Indexing table for converting from natural Hadamard to ordery Hadamard
564
   This is essentially a bit-reversed Gray, on top of which we've added
565
   an inversion of the order because we want the DC at the end rather than
566
   the beginning. The lines are for N=2, 4, 8, 16 */
567
static const int ordery_table[] = {
568
       1,  0,
569
       3,  0,  2,  1,
570
       7,  0,  4,  3,  6,  1,  5,  2,
571
      15,  0,  8,  7, 12,  3, 11,  4, 14,  1,  9,  6, 13,  2, 10,  5,
572
};
573
574
static void deinterleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
575
3.50M
{
576
3.50M
   int i,j;
577
3.50M
   VARDECL(celt_norm, tmp);
578
3.50M
   int N;
579
3.50M
   SAVE_STACK;
580
3.50M
   N = N0*stride;
581
3.50M
   ALLOC(tmp, N, celt_norm);
582
3.50M
   celt_assert(stride>0);
583
3.50M
   if (hadamard)
584
621k
   {
585
621k
      const int *ordery = ordery_table+stride-2;
586
2.84M
      for (i=0;i<stride;i++)
587
2.22M
      {
588
10.6M
         for (j=0;j<N0;j++)
589
8.45M
            tmp[ordery[i]*N0+j] = X[j*stride+i];
590
2.22M
      }
591
2.88M
   } else {
592
24.1M
      for (i=0;i<stride;i++)
593
58.0M
         for (j=0;j<N0;j++)
594
36.8M
            tmp[i*N0+j] = X[j*stride+i];
595
2.88M
   }
596
3.50M
   OPUS_COPY(X, tmp, N);
597
3.50M
   RESTORE_STACK;
598
3.50M
}
599
600
static void interleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
601
1.54M
{
602
1.54M
   int i,j;
603
1.54M
   VARDECL(celt_norm, tmp);
604
1.54M
   int N;
605
1.54M
   SAVE_STACK;
606
1.54M
   N = N0*stride;
607
1.54M
   ALLOC(tmp, N, celt_norm);
608
1.54M
   if (hadamard)
609
500k
   {
610
500k
      const int *ordery = ordery_table+stride-2;
611
2.21M
      for (i=0;i<stride;i++)
612
8.49M
         for (j=0;j<N0;j++)
613
6.78M
            tmp[j*stride+i] = X[ordery[i]*N0+j];
614
1.04M
   } else {
615
8.35M
      for (i=0;i<stride;i++)
616
20.3M
         for (j=0;j<N0;j++)
617
13.0M
            tmp[j*stride+i] = X[i*N0+j];
618
1.04M
   }
619
1.54M
   OPUS_COPY(X, tmp, N);
620
1.54M
   RESTORE_STACK;
621
1.54M
}
622
623
void haar1(celt_norm *X, int N0, int stride)
624
20.8M
{
625
20.8M
   int i, j;
626
20.8M
   N0 >>= 1;
627
74.6M
   for (i=0;i<stride;i++)
628
224M
      for (j=0;j<N0;j++)
629
170M
      {
630
170M
         opus_val32 tmp1, tmp2;
631
170M
         tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]);
632
170M
         tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]);
633
170M
         X[stride*2*j+i] = ADD32(tmp1, tmp2);
634
170M
         X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2);
635
170M
      }
636
20.8M
}
haar1
Line
Count
Source
624
10.4M
{
625
10.4M
   int i, j;
626
10.4M
   N0 >>= 1;
627
37.3M
   for (i=0;i<stride;i++)
628
112M
      for (j=0;j<N0;j++)
629
85.4M
      {
630
85.4M
         opus_val32 tmp1, tmp2;
631
85.4M
         tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]);
632
85.4M
         tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]);
633
85.4M
         X[stride*2*j+i] = ADD32(tmp1, tmp2);
634
85.4M
         X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2);
635
85.4M
      }
636
10.4M
}
haar1
Line
Count
Source
624
10.4M
{
625
10.4M
   int i, j;
626
10.4M
   N0 >>= 1;
627
37.3M
   for (i=0;i<stride;i++)
628
112M
      for (j=0;j<N0;j++)
629
85.4M
      {
630
85.4M
         opus_val32 tmp1, tmp2;
631
85.4M
         tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]);
632
85.4M
         tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]);
633
85.4M
         X[stride*2*j+i] = ADD32(tmp1, tmp2);
634
85.4M
         X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2);
635
85.4M
      }
636
10.4M
}
637
638
static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo)
639
4.96M
{
640
4.96M
   static const opus_int16 exp2_table8[8] =
641
4.96M
      {16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048};
642
4.96M
   int qn, qb;
643
4.96M
   int N2 = 2*N-1;
644
4.96M
   if (stereo && N==2)
645
639k
      N2--;
646
   /* The upper limit ensures that in a stereo split with itheta==16384, we'll
647
       always have enough bits left over to code at least one pulse in the
648
       side; otherwise it would collapse, since it doesn't get folded. */
649
4.96M
   qb = celt_sudiv(b+N2*offset, N2);
650
4.96M
   qb = IMIN(b-pulse_cap-(4<<BITRES), qb);
651
652
4.96M
   qb = IMIN(8<<BITRES, qb);
653
654
4.96M
   if (qb<(1<<BITRES>>1)) {
655
1.44M
      qn = 1;
656
3.52M
   } else {
657
3.52M
      qn = exp2_table8[qb&0x7]>>(14-(qb>>BITRES));
658
3.52M
      qn = (qn+1)>>1<<1;
659
3.52M
   }
660
4.96M
   celt_assert(qn <= 256);
661
4.96M
   return qn;
662
4.96M
}
663
664
struct band_ctx {
665
   int encode;
666
   int resynth;
667
   const CELTMode *m;
668
   int i;
669
   int intensity;
670
   int spread;
671
   int tf_change;
672
   ec_ctx *ec;
673
   opus_int32 remaining_bits;
674
   const celt_ener *bandE;
675
   opus_uint32 seed;
676
   int arch;
677
   int theta_round;
678
   int disable_inv;
679
   int avoid_split_noise;
680
#ifdef ENABLE_QEXT
681
   ec_ctx *ext_ec;
682
   int extra_bits;
683
   opus_int32 ext_total_bits;
684
   int extra_bands;
685
#endif
686
};
687
688
struct split_ctx {
689
   int inv;
690
   int imid;
691
   int iside;
692
   int delta;
693
   int itheta;
694
#ifdef ENABLE_QEXT
695
   int itheta_q30;
696
#endif
697
   int qalloc;
698
};
699
700
static void compute_theta(struct band_ctx *ctx, struct split_ctx *sctx,
701
      celt_norm *X, celt_norm *Y, int N, int *b, int B, int B0,
702
      int LM,
703
      int stereo, int *fill ARG_QEXT(int *ext_b))
704
4.96M
{
705
4.96M
   int qn;
706
4.96M
   int itheta=0;
707
4.96M
   int itheta_q30=0;
708
4.96M
   int delta;
709
4.96M
   int imid, iside;
710
4.96M
   int qalloc;
711
4.96M
   int pulse_cap;
712
4.96M
   int offset;
713
4.96M
   opus_int32 tell;
714
4.96M
   int inv=0;
715
4.96M
   int encode;
716
4.96M
   const CELTMode *m;
717
4.96M
   int i;
718
4.96M
   int intensity;
719
4.96M
   ec_ctx *ec;
720
4.96M
   const celt_ener *bandE;
721
722
4.96M
   encode = ctx->encode;
723
4.96M
   m = ctx->m;
724
4.96M
   i = ctx->i;
725
4.96M
   intensity = ctx->intensity;
726
4.96M
   ec = ctx->ec;
727
4.96M
   bandE = ctx->bandE;
728
729
   /* Decide on the resolution to give to the split parameter theta */
730
4.96M
   pulse_cap = m->logN[i]+LM*(1<<BITRES);
731
4.96M
   offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
732
4.96M
   qn = compute_qn(N, *b, offset, pulse_cap, stereo);
733
4.96M
   if (stereo && i>=intensity)
734
1.66M
      qn = 1;
735
4.96M
   if (encode)
736
2.91M
   {
737
      /* theta is the atan() of the ratio between the (normalized)
738
         side and mid. With just that parameter, we can re-scale both
739
         mid and side because we know that 1) they have unit norm and
740
         2) they are orthogonal. */
741
2.91M
      itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch);
742
2.91M
      itheta = itheta_q30>>16;
743
2.91M
   }
744
4.96M
   tell = ec_tell_frac(ec);
745
4.96M
   if (qn!=1)
746
3.22M
   {
747
3.22M
      if (encode)
748
2.38M
      {
749
2.38M
         if (!stereo || ctx->theta_round == 0)
750
1.76M
         {
751
1.76M
            itheta = (itheta*(opus_int32)qn+8192)>>14;
752
1.76M
            if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
753
58.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
58.3k
               int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
758
58.3k
               imid = bitexact_cos((opus_int16)unquantized);
759
58.3k
               iside = bitexact_cos((opus_int16)(16384-unquantized));
760
58.3k
               delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
761
58.3k
               if (delta > *b)
762
545
                  itheta = qn;
763
57.7k
               else if (delta < -*b)
764
456
                  itheta = 0;
765
58.3k
            }
766
1.76M
         } else {
767
621k
            int down;
768
            /* Bias quantization towards itheta=0 and itheta=16384. */
769
621k
            int bias = itheta > 8192 ? 32767/qn : -32767/qn;
770
621k
            down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
771
621k
            if (ctx->theta_round < 0)
772
310k
               itheta = down;
773
310k
            else
774
310k
               itheta = down+1;
775
621k
         }
776
2.38M
      }
777
      /* Entropy coding of the angle. We use a uniform pdf for the
778
         time split, a step for stereo, and a triangular one for the rest. */
779
3.22M
      if (stereo && N>2)
780
672k
      {
781
672k
         int p0 = 3;
782
672k
         int x = itheta;
783
672k
         int x0 = qn/2;
784
672k
         int ft = p0*(x0+1) + x0;
785
         /* Use a probability of p0 up to itheta=8192 and then use 1 after */
786
672k
         if (encode)
787
605k
         {
788
605k
            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
605k
         } else {
790
67.4k
            int fs;
791
67.4k
            fs=ec_decode(ec,ft);
792
67.4k
            if (fs<(x0+1)*p0)
793
47.4k
               x=fs/p0;
794
20.0k
            else
795
20.0k
               x=x0+1+(fs-(x0+1)*p0);
796
67.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
67.4k
            itheta = x;
798
67.4k
         }
799
2.55M
      } else if (B0>1 || stereo) {
800
         /* Uniform pdf */
801
1.78M
         if (encode)
802
1.44M
            ec_enc_uint(ec, itheta, qn+1);
803
341k
         else
804
341k
            itheta = ec_dec_uint(ec, qn+1);
805
1.78M
      } else {
806
762k
         int fs=1, ft;
807
762k
         ft = ((qn>>1)+1)*((qn>>1)+1);
808
762k
         if (encode)
809
331k
         {
810
331k
            int fl;
811
812
331k
            fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
813
331k
            fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
814
331k
             ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
815
816
331k
            ec_encode(ec, fl, fl+fs, ft);
817
431k
         } else {
818
            /* Triangular pdf */
819
431k
            int fl=0;
820
431k
            int fm;
821
431k
            fm = ec_decode(ec, ft);
822
823
431k
            if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
824
219k
            {
825
219k
               itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
826
219k
               fs = itheta + 1;
827
219k
               fl = itheta*(itheta + 1)>>1;
828
219k
            }
829
211k
            else
830
211k
            {
831
211k
               itheta = (2*(qn + 1)
832
211k
                - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
833
211k
               fs = qn + 1 - itheta;
834
211k
               fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
835
211k
            }
836
837
431k
            ec_dec_update(ec, fl, fl+fs, ft);
838
431k
         }
839
762k
      }
840
3.22M
      celt_assert(itheta>=0);
841
3.22M
      itheta = celt_udiv((opus_int32)itheta*16384, qn);
842
#ifdef ENABLE_QEXT
843
1.77M
      *ext_b = IMIN(*ext_b, ctx->ext_total_bits - (opus_int32)ec_tell_frac(ctx->ext_ec));
844
1.77M
      if (*ext_b >= 2*N<<BITRES && ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec)-1 > 2<<BITRES) {
845
29.9k
         int extra_bits;
846
29.9k
         int ext_tell = ec_tell_frac(ctx->ext_ec);
847
29.9k
         extra_bits = IMIN(12, IMAX(2, celt_sudiv(*ext_b, (2*N-1)<<BITRES)));
848
29.9k
         if (encode) {
849
0
            itheta_q30 = itheta_q30 - (itheta<<16);
850
0
            itheta_q30 = (itheta_q30*(opus_int64)qn*((1<<extra_bits)-1)+(1<<29))>>30;
851
0
            itheta_q30 += (1<<(extra_bits-1))-1;
852
0
            itheta_q30 = IMAX(0, IMIN((1<<extra_bits)-2, itheta_q30));
853
0
            ec_enc_uint(ctx->ext_ec, itheta_q30, (1<<extra_bits)-1);
854
29.9k
         } else {
855
29.9k
            itheta_q30 = ec_dec_uint(ctx->ext_ec, (1<<extra_bits)-1);
856
29.9k
         }
857
29.9k
         itheta_q30 -= (1<<(extra_bits-1))-1;
858
29.9k
         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
29.9k
         itheta_q30 = IMAX(0, IMIN(itheta_q30, 1073741824));
861
29.9k
         *ext_b -= ec_tell_frac(ctx->ext_ec) - ext_tell;
862
1.74M
      } else {
863
1.74M
         itheta_q30 = (opus_int32)itheta<<16;
864
1.74M
      }
865
#endif
866
3.22M
      if (encode && stereo)
867
823k
      {
868
823k
         if (itheta==0)
869
148k
            intensity_stereo(m, X, Y, bandE, i, N);
870
675k
         else
871
675k
            stereo_split(X, Y, N);
872
823k
      }
873
      /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate.
874
               Let's do that at higher complexity */
875
3.22M
   } else if (stereo) {
876
1.74M
      if (encode)
877
528k
      {
878
528k
         inv = itheta > 8192 && !ctx->disable_inv;
879
528k
         if (inv)
880
133k
         {
881
133k
            int j;
882
2.38M
            for (j=0;j<N;j++)
883
2.25M
               Y[j] = -Y[j];
884
133k
         }
885
528k
         intensity_stereo(m, X, Y, bandE, i, N);
886
528k
      }
887
1.74M
      if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
888
406k
      {
889
406k
         if (encode)
890
252k
            ec_enc_bit_logp(ec, inv, 2);
891
154k
         else
892
154k
            inv = ec_dec_bit_logp(ec, 2);
893
406k
      } else
894
1.33M
         inv = 0;
895
      /* inv flag override to avoid problems with downmixing. */
896
1.74M
      if (ctx->disable_inv)
897
852k
         inv = 0;
898
1.74M
      itheta = 0;
899
1.74M
      itheta_q30 = 0;
900
1.74M
   }
901
4.96M
   qalloc = ec_tell_frac(ec) - tell;
902
4.96M
   *b -= qalloc;
903
904
4.96M
   if (itheta == 0)
905
2.05M
   {
906
2.05M
      imid = 32767;
907
2.05M
      iside = 0;
908
2.05M
      *fill &= (1<<B)-1;
909
2.05M
      delta = -16384;
910
2.91M
   } else if (itheta == 16384)
911
342k
   {
912
342k
      imid = 0;
913
342k
      iside = 32767;
914
342k
      *fill &= ((1<<B)-1)<<B;
915
342k
      delta = 16384;
916
2.57M
   } else {
917
2.57M
      imid = bitexact_cos((opus_int16)itheta);
918
2.57M
      iside = bitexact_cos((opus_int16)(16384-itheta));
919
      /* This is the mid vs side allocation that minimizes squared error
920
         in that band. */
921
2.57M
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
922
2.57M
   }
923
924
4.96M
   sctx->inv = inv;
925
4.96M
   sctx->imid = imid;
926
4.96M
   sctx->iside = iside;
927
4.96M
   sctx->delta = delta;
928
4.96M
   sctx->itheta = itheta;
929
#ifdef ENABLE_QEXT
930
   sctx->itheta_q30 = itheta_q30;
931
#endif
932
4.96M
   sctx->qalloc = qalloc;
933
4.96M
}
bands.c:compute_theta
Line
Count
Source
704
2.34M
{
705
2.34M
   int qn;
706
2.34M
   int itheta=0;
707
2.34M
   int itheta_q30=0;
708
2.34M
   int delta;
709
2.34M
   int imid, iside;
710
2.34M
   int qalloc;
711
2.34M
   int pulse_cap;
712
2.34M
   int offset;
713
2.34M
   opus_int32 tell;
714
2.34M
   int inv=0;
715
2.34M
   int encode;
716
2.34M
   const CELTMode *m;
717
2.34M
   int i;
718
2.34M
   int intensity;
719
2.34M
   ec_ctx *ec;
720
2.34M
   const celt_ener *bandE;
721
722
2.34M
   encode = ctx->encode;
723
2.34M
   m = ctx->m;
724
2.34M
   i = ctx->i;
725
2.34M
   intensity = ctx->intensity;
726
2.34M
   ec = ctx->ec;
727
2.34M
   bandE = ctx->bandE;
728
729
   /* Decide on the resolution to give to the split parameter theta */
730
2.34M
   pulse_cap = m->logN[i]+LM*(1<<BITRES);
731
2.34M
   offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
732
2.34M
   qn = compute_qn(N, *b, offset, pulse_cap, stereo);
733
2.34M
   if (stereo && i>=intensity)
734
859k
      qn = 1;
735
2.34M
   if (encode)
736
1.35M
   {
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.35M
      itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch);
742
1.35M
      itheta = itheta_q30>>16;
743
1.35M
   }
744
2.34M
   tell = ec_tell_frac(ec);
745
2.34M
   if (qn!=1)
746
1.44M
   {
747
1.44M
      if (encode)
748
1.10M
      {
749
1.10M
         if (!stereo || ctx->theta_round == 0)
750
795k
         {
751
795k
            itheta = (itheta*(opus_int32)qn+8192)>>14;
752
795k
            if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
753
26.2k
            {
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
26.2k
               int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
758
26.2k
               imid = bitexact_cos((opus_int16)unquantized);
759
26.2k
               iside = bitexact_cos((opus_int16)(16384-unquantized));
760
26.2k
               delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
761
26.2k
               if (delta > *b)
762
250
                  itheta = qn;
763
26.0k
               else if (delta < -*b)
764
179
                  itheta = 0;
765
26.2k
            }
766
795k
         } else {
767
307k
            int down;
768
            /* Bias quantization towards itheta=0 and itheta=16384. */
769
307k
            int bias = itheta > 8192 ? 32767/qn : -32767/qn;
770
307k
            down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
771
307k
            if (ctx->theta_round < 0)
772
153k
               itheta = down;
773
153k
            else
774
153k
               itheta = down+1;
775
307k
         }
776
1.10M
      }
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.44M
      if (stereo && N>2)
780
312k
      {
781
312k
         int p0 = 3;
782
312k
         int x = itheta;
783
312k
         int x0 = qn/2;
784
312k
         int ft = p0*(x0+1) + x0;
785
         /* Use a probability of p0 up to itheta=8192 and then use 1 after */
786
312k
         if (encode)
787
286k
         {
788
286k
            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
286k
         } else {
790
26.4k
            int fs;
791
26.4k
            fs=ec_decode(ec,ft);
792
26.4k
            if (fs<(x0+1)*p0)
793
18.7k
               x=fs/p0;
794
7.77k
            else
795
7.77k
               x=x0+1+(fs-(x0+1)*p0);
796
26.4k
            ec_dec_update(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft);
797
26.4k
            itheta = x;
798
26.4k
         }
799
1.13M
      } else if (B0>1 || stereo) {
800
         /* Uniform pdf */
801
824k
         if (encode)
802
668k
            ec_enc_uint(ec, itheta, qn+1);
803
156k
         else
804
156k
            itheta = ec_dec_uint(ec, qn+1);
805
824k
      } else {
806
310k
         int fs=1, ft;
807
310k
         ft = ((qn>>1)+1)*((qn>>1)+1);
808
310k
         if (encode)
809
148k
         {
810
148k
            int fl;
811
812
148k
            fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
813
148k
            fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
814
148k
             ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
815
816
148k
            ec_encode(ec, fl, fl+fs, ft);
817
161k
         } else {
818
            /* Triangular pdf */
819
161k
            int fl=0;
820
161k
            int fm;
821
161k
            fm = ec_decode(ec, ft);
822
823
161k
            if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
824
81.6k
            {
825
81.6k
               itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
826
81.6k
               fs = itheta + 1;
827
81.6k
               fl = itheta*(itheta + 1)>>1;
828
81.6k
            }
829
79.8k
            else
830
79.8k
            {
831
79.8k
               itheta = (2*(qn + 1)
832
79.8k
                - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
833
79.8k
               fs = qn + 1 - itheta;
834
79.8k
               fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
835
79.8k
            }
836
837
161k
            ec_dec_update(ec, fl, fl+fs, ft);
838
161k
         }
839
310k
      }
840
1.44M
      celt_assert(itheta>=0);
841
1.44M
      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.44M
      if (encode && stereo)
867
397k
      {
868
397k
         if (itheta==0)
869
72.6k
            intensity_stereo(m, X, Y, bandE, i, N);
870
324k
         else
871
324k
            stereo_split(X, Y, N);
872
397k
      }
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.44M
   } else if (stereo) {
876
896k
      if (encode)
877
254k
      {
878
254k
         inv = itheta > 8192 && !ctx->disable_inv;
879
254k
         if (inv)
880
64.0k
         {
881
64.0k
            int j;
882
1.12M
            for (j=0;j<N;j++)
883
1.06M
               Y[j] = -Y[j];
884
64.0k
         }
885
254k
         intensity_stereo(m, X, Y, bandE, i, N);
886
254k
      }
887
896k
      if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
888
192k
      {
889
192k
         if (encode)
890
125k
            ec_enc_bit_logp(ec, inv, 2);
891
66.1k
         else
892
66.1k
            inv = ec_dec_bit_logp(ec, 2);
893
192k
      } else
894
704k
         inv = 0;
895
      /* inv flag override to avoid problems with downmixing. */
896
896k
      if (ctx->disable_inv)
897
429k
         inv = 0;
898
896k
      itheta = 0;
899
896k
      itheta_q30 = 0;
900
896k
   }
901
2.34M
   qalloc = ec_tell_frac(ec) - tell;
902
2.34M
   *b -= qalloc;
903
904
2.34M
   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.30M
   } else if (itheta == 16384)
911
160k
   {
912
160k
      imid = 0;
913
160k
      iside = 32767;
914
160k
      *fill &= ((1<<B)-1)<<B;
915
160k
      delta = 16384;
916
1.14M
   } else {
917
1.14M
      imid = bitexact_cos((opus_int16)itheta);
918
1.14M
      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.14M
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
922
1.14M
   }
923
924
2.34M
   sctx->inv = inv;
925
2.34M
   sctx->imid = imid;
926
2.34M
   sctx->iside = iside;
927
2.34M
   sctx->delta = delta;
928
2.34M
   sctx->itheta = itheta;
929
#ifdef ENABLE_QEXT
930
   sctx->itheta_q30 = itheta_q30;
931
#endif
932
2.34M
   sctx->qalloc = qalloc;
933
2.34M
}
bands.c:compute_theta
Line
Count
Source
704
2.62M
{
705
2.62M
   int qn;
706
2.62M
   int itheta=0;
707
2.62M
   int itheta_q30=0;
708
2.62M
   int delta;
709
2.62M
   int imid, iside;
710
2.62M
   int qalloc;
711
2.62M
   int pulse_cap;
712
2.62M
   int offset;
713
2.62M
   opus_int32 tell;
714
2.62M
   int inv=0;
715
2.62M
   int encode;
716
2.62M
   const CELTMode *m;
717
2.62M
   int i;
718
2.62M
   int intensity;
719
2.62M
   ec_ctx *ec;
720
2.62M
   const celt_ener *bandE;
721
722
2.62M
   encode = ctx->encode;
723
2.62M
   m = ctx->m;
724
2.62M
   i = ctx->i;
725
2.62M
   intensity = ctx->intensity;
726
2.62M
   ec = ctx->ec;
727
2.62M
   bandE = ctx->bandE;
728
729
   /* Decide on the resolution to give to the split parameter theta */
730
2.62M
   pulse_cap = m->logN[i]+LM*(1<<BITRES);
731
2.62M
   offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
732
2.62M
   qn = compute_qn(N, *b, offset, pulse_cap, stereo);
733
2.62M
   if (stereo && i>=intensity)
734
801k
      qn = 1;
735
2.62M
   if (encode)
736
1.55M
   {
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.55M
      itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch);
742
1.55M
      itheta = itheta_q30>>16;
743
1.55M
   }
744
2.62M
   tell = ec_tell_frac(ec);
745
2.62M
   if (qn!=1)
746
1.77M
   {
747
1.77M
      if (encode)
748
1.27M
      {
749
1.27M
         if (!stereo || ctx->theta_round == 0)
750
965k
         {
751
965k
            itheta = (itheta*(opus_int32)qn+8192)>>14;
752
965k
            if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
753
32.0k
            {
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
32.0k
               int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
758
32.0k
               imid = bitexact_cos((opus_int16)unquantized);
759
32.0k
               iside = bitexact_cos((opus_int16)(16384-unquantized));
760
32.0k
               delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
761
32.0k
               if (delta > *b)
762
295
                  itheta = qn;
763
31.7k
               else if (delta < -*b)
764
277
                  itheta = 0;
765
32.0k
            }
766
965k
         } else {
767
314k
            int down;
768
            /* Bias quantization towards itheta=0 and itheta=16384. */
769
314k
            int bias = itheta > 8192 ? 32767/qn : -32767/qn;
770
314k
            down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
771
314k
            if (ctx->theta_round < 0)
772
157k
               itheta = down;
773
157k
            else
774
157k
               itheta = down+1;
775
314k
         }
776
1.27M
      }
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.77M
      if (stereo && N>2)
780
360k
      {
781
360k
         int p0 = 3;
782
360k
         int x = itheta;
783
360k
         int x0 = qn/2;
784
360k
         int ft = p0*(x0+1) + x0;
785
         /* Use a probability of p0 up to itheta=8192 and then use 1 after */
786
360k
         if (encode)
787
319k
         {
788
319k
            ec_encode(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft);
789
319k
         } else {
790
40.9k
            int fs;
791
40.9k
            fs=ec_decode(ec,ft);
792
40.9k
            if (fs<(x0+1)*p0)
793
28.7k
               x=fs/p0;
794
12.2k
            else
795
12.2k
               x=x0+1+(fs-(x0+1)*p0);
796
40.9k
            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
40.9k
            itheta = x;
798
40.9k
         }
799
1.41M
      } else if (B0>1 || stereo) {
800
         /* Uniform pdf */
801
963k
         if (encode)
802
778k
            ec_enc_uint(ec, itheta, qn+1);
803
184k
         else
804
184k
            itheta = ec_dec_uint(ec, qn+1);
805
963k
      } else {
806
452k
         int fs=1, ft;
807
452k
         ft = ((qn>>1)+1)*((qn>>1)+1);
808
452k
         if (encode)
809
182k
         {
810
182k
            int fl;
811
812
182k
            fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
813
182k
            fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
814
182k
             ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
815
816
182k
            ec_encode(ec, fl, fl+fs, ft);
817
269k
         } else {
818
            /* Triangular pdf */
819
269k
            int fl=0;
820
269k
            int fm;
821
269k
            fm = ec_decode(ec, ft);
822
823
269k
            if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
824
138k
            {
825
138k
               itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
826
138k
               fs = itheta + 1;
827
138k
               fl = itheta*(itheta + 1)>>1;
828
138k
            }
829
131k
            else
830
131k
            {
831
131k
               itheta = (2*(qn + 1)
832
131k
                - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
833
131k
               fs = qn + 1 - itheta;
834
131k
               fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
835
131k
            }
836
837
269k
            ec_dec_update(ec, fl, fl+fs, ft);
838
269k
         }
839
452k
      }
840
1.77M
      celt_assert(itheta>=0);
841
1.77M
      itheta = celt_udiv((opus_int32)itheta*16384, qn);
842
1.77M
#ifdef ENABLE_QEXT
843
1.77M
      *ext_b = IMIN(*ext_b, ctx->ext_total_bits - (opus_int32)ec_tell_frac(ctx->ext_ec));
844
1.77M
      if (*ext_b >= 2*N<<BITRES && ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec)-1 > 2<<BITRES) {
845
29.9k
         int extra_bits;
846
29.9k
         int ext_tell = ec_tell_frac(ctx->ext_ec);
847
29.9k
         extra_bits = IMIN(12, IMAX(2, celt_sudiv(*ext_b, (2*N-1)<<BITRES)));
848
29.9k
         if (encode) {
849
0
            itheta_q30 = itheta_q30 - (itheta<<16);
850
0
            itheta_q30 = (itheta_q30*(opus_int64)qn*((1<<extra_bits)-1)+(1<<29))>>30;
851
0
            itheta_q30 += (1<<(extra_bits-1))-1;
852
0
            itheta_q30 = IMAX(0, IMIN((1<<extra_bits)-2, itheta_q30));
853
0
            ec_enc_uint(ctx->ext_ec, itheta_q30, (1<<extra_bits)-1);
854
29.9k
         } else {
855
29.9k
            itheta_q30 = ec_dec_uint(ctx->ext_ec, (1<<extra_bits)-1);
856
29.9k
         }
857
29.9k
         itheta_q30 -= (1<<(extra_bits-1))-1;
858
29.9k
         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
29.9k
         itheta_q30 = IMAX(0, IMIN(itheta_q30, 1073741824));
861
29.9k
         *ext_b -= ec_tell_frac(ctx->ext_ec) - ext_tell;
862
1.74M
      } else {
863
1.74M
         itheta_q30 = (opus_int32)itheta<<16;
864
1.74M
      }
865
1.77M
#endif
866
1.77M
      if (encode && stereo)
867
425k
      {
868
425k
         if (itheta==0)
869
75.4k
            intensity_stereo(m, X, Y, bandE, i, N);
870
350k
         else
871
350k
            stereo_split(X, Y, N);
872
425k
      }
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.77M
   } else if (stereo) {
876
847k
      if (encode)
877
274k
      {
878
274k
         inv = itheta > 8192 && !ctx->disable_inv;
879
274k
         if (inv)
880
69.9k
         {
881
69.9k
            int j;
882
1.25M
            for (j=0;j<N;j++)
883
1.18M
               Y[j] = -Y[j];
884
69.9k
         }
885
274k
         intensity_stereo(m, X, Y, bandE, i, N);
886
274k
      }
887
847k
      if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
888
214k
      {
889
214k
         if (encode)
890
126k
            ec_enc_bit_logp(ec, inv, 2);
891
88.3k
         else
892
88.3k
            inv = ec_dec_bit_logp(ec, 2);
893
214k
      } else
894
632k
         inv = 0;
895
      /* inv flag override to avoid problems with downmixing. */
896
847k
      if (ctx->disable_inv)
897
423k
         inv = 0;
898
847k
      itheta = 0;
899
847k
      itheta_q30 = 0;
900
847k
   }
901
2.62M
   qalloc = ec_tell_frac(ec) - tell;
902
2.62M
   *b -= qalloc;
903
904
2.62M
   if (itheta == 0)
905
1.01M
   {
906
1.01M
      imid = 32767;
907
1.01M
      iside = 0;
908
1.01M
      *fill &= (1<<B)-1;
909
1.01M
      delta = -16384;
910
1.61M
   } else if (itheta == 16384)
911
181k
   {
912
181k
      imid = 0;
913
181k
      iside = 32767;
914
181k
      *fill &= ((1<<B)-1)<<B;
915
181k
      delta = 16384;
916
1.43M
   } else {
917
1.43M
      imid = bitexact_cos((opus_int16)itheta);
918
1.43M
      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.43M
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
922
1.43M
   }
923
924
2.62M
   sctx->inv = inv;
925
2.62M
   sctx->imid = imid;
926
2.62M
   sctx->iside = iside;
927
2.62M
   sctx->delta = delta;
928
2.62M
   sctx->itheta = itheta;
929
2.62M
#ifdef ENABLE_QEXT
930
2.62M
   sctx->itheta_q30 = itheta_q30;
931
2.62M
#endif
932
2.62M
   sctx->qalloc = qalloc;
933
2.62M
}
934
static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y,
935
      celt_norm *lowband_out)
936
4.62M
{
937
4.62M
   int c;
938
4.62M
   int stereo;
939
4.62M
   celt_norm *x = X;
940
4.62M
   int encode;
941
4.62M
   ec_ctx *ec;
942
943
4.62M
   encode = ctx->encode;
944
4.62M
   ec = ctx->ec;
945
946
4.62M
   stereo = Y != NULL;
947
5.90M
   c=0; do {
948
5.90M
      int sign=0;
949
5.90M
      if (ctx->remaining_bits>=1<<BITRES)
950
3.01M
      {
951
3.01M
         if (encode)
952
2.45M
         {
953
2.45M
            sign = x[0]<0;
954
2.45M
            ec_enc_bits(ec, sign, 1);
955
2.45M
         } else {
956
557k
            sign = ec_dec_bits(ec, 1);
957
557k
         }
958
3.01M
         ctx->remaining_bits -= 1<<BITRES;
959
3.01M
      }
960
5.90M
      if (ctx->resynth)
961
3.83M
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
962
5.90M
      x = Y;
963
5.90M
   } while (++c<1+stereo);
964
4.62M
   if (lowband_out)
965
4.62M
      lowband_out[0] = SHR32(X[0],4);
966
4.62M
   return 1;
967
4.62M
}
bands.c:quant_band_n1
Line
Count
Source
936
2.31M
{
937
2.31M
   int c;
938
2.31M
   int stereo;
939
2.31M
   celt_norm *x = X;
940
2.31M
   int encode;
941
2.31M
   ec_ctx *ec;
942
943
2.31M
   encode = ctx->encode;
944
2.31M
   ec = ctx->ec;
945
946
2.31M
   stereo = Y != NULL;
947
2.95M
   c=0; do {
948
2.95M
      int sign=0;
949
2.95M
      if (ctx->remaining_bits>=1<<BITRES)
950
1.50M
      {
951
1.50M
         if (encode)
952
1.22M
         {
953
1.22M
            sign = x[0]<0;
954
1.22M
            ec_enc_bits(ec, sign, 1);
955
1.22M
         } else {
956
278k
            sign = ec_dec_bits(ec, 1);
957
278k
         }
958
1.50M
         ctx->remaining_bits -= 1<<BITRES;
959
1.50M
      }
960
2.95M
      if (ctx->resynth)
961
1.91M
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
962
2.95M
      x = Y;
963
2.95M
   } while (++c<1+stereo);
964
2.31M
   if (lowband_out)
965
2.31M
      lowband_out[0] = SHR32(X[0],4);
966
2.31M
   return 1;
967
2.31M
}
bands.c:quant_band_n1
Line
Count
Source
936
2.31M
{
937
2.31M
   int c;
938
2.31M
   int stereo;
939
2.31M
   celt_norm *x = X;
940
2.31M
   int encode;
941
2.31M
   ec_ctx *ec;
942
943
2.31M
   encode = ctx->encode;
944
2.31M
   ec = ctx->ec;
945
946
2.31M
   stereo = Y != NULL;
947
2.95M
   c=0; do {
948
2.95M
      int sign=0;
949
2.95M
      if (ctx->remaining_bits>=1<<BITRES)
950
1.50M
      {
951
1.50M
         if (encode)
952
1.22M
         {
953
1.22M
            sign = x[0]<0;
954
1.22M
            ec_enc_bits(ec, sign, 1);
955
1.22M
         } else {
956
278k
            sign = ec_dec_bits(ec, 1);
957
278k
         }
958
1.50M
         ctx->remaining_bits -= 1<<BITRES;
959
1.50M
      }
960
2.95M
      if (ctx->resynth)
961
1.91M
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
962
2.95M
      x = Y;
963
2.95M
   } while (++c<1+stereo);
964
2.31M
   if (lowband_out)
965
2.31M
      lowband_out[0] = SHR32(X[0],4);
966
2.31M
   return 1;
967
2.31M
}
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
29.4M
{
979
29.4M
   const unsigned char *cache;
980
29.4M
   int q;
981
29.4M
   int curr_bits;
982
29.4M
   int imid=0, iside=0;
983
29.4M
   int B0=B;
984
29.4M
   opus_val32 mid=0, side=0;
985
29.4M
   unsigned cm=0;
986
29.4M
   celt_norm *Y=NULL;
987
29.4M
   int encode;
988
29.4M
   const CELTMode *m;
989
29.4M
   int i;
990
29.4M
   int spread;
991
29.4M
   ec_ctx *ec;
992
993
29.4M
   encode = ctx->encode;
994
29.4M
   m = ctx->m;
995
29.4M
   i = ctx->i;
996
29.4M
   spread = ctx->spread;
997
29.4M
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
29.4M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
29.4M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
4.60M
   {
1003
4.60M
      int mbits, sbits, delta;
1004
4.60M
      int itheta;
1005
4.60M
      int qalloc;
1006
4.60M
      struct split_ctx sctx;
1007
4.60M
      celt_norm *next_lowband2=NULL;
1008
4.60M
      opus_int32 rebalance;
1009
1010
4.60M
      N >>= 1;
1011
4.60M
      Y = X+N;
1012
4.60M
      LM -= 1;
1013
4.60M
      if (B==1)
1014
1.52M
         fill = (fill&1)|(fill<<1);
1015
4.60M
      B = (B+1)>>1;
1016
1017
4.60M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
4.60M
      imid = sctx.imid;
1019
4.60M
      iside = sctx.iside;
1020
4.60M
      delta = sctx.delta;
1021
4.60M
      itheta = sctx.itheta;
1022
4.60M
      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.01M
      mid = SHL32(EXTEND32(imid), 16);
1031
1.01M
      side = SHL32(EXTEND32(iside), 16);
1032
# endif
1033
#else
1034
# ifdef ENABLE_QEXT
1035
      (void)imid;
1036
      (void)iside;
1037
      mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1038
      side = celt_cos_norm2(1.f-sctx.itheta_q30*(1.f/(1<<30)));
1039
# else
1040
      mid = (1.f/32768)*imid;
1041
      side = (1.f/32768)*iside;
1042
# endif
1043
#endif
1044
1045
      /* Give more bits to low-energy MDCTs than they would otherwise deserve */
1046
4.60M
      if (B0>1 && (itheta&0x3fff))
1047
2.39M
      {
1048
2.39M
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
1.22M
            delta -= delta>>(4-LM);
1051
1.17M
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
1.17M
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
2.39M
      }
1055
4.60M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
4.60M
      sbits = b-mbits;
1057
4.60M
      ctx->remaining_bits -= qalloc;
1058
1059
4.60M
      if (lowband)
1060
1.88M
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
4.60M
      rebalance = ctx->remaining_bits;
1063
4.60M
      if (mbits >= sbits)
1064
2.11M
      {
1065
2.11M
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
2.11M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
2.11M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
2.11M
         if (rebalance > 3<<BITRES && itheta!=0)
1069
738k
            sbits += rebalance - (3<<BITRES);
1070
2.11M
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
2.11M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
2.49M
      } else {
1073
2.49M
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
2.49M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
2.49M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
2.49M
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
850k
            mbits += rebalance - (3<<BITRES);
1078
2.49M
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
2.49M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
2.49M
      }
1081
24.8M
   } else {
1082
#ifdef ENABLE_QEXT
1083
      int extra_bits;
1084
      int ext_remaining_bits;
1085
12.9M
      extra_bits = ext_b/(N-1)>>BITRES;
1086
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1087
12.9M
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1088
12.6M
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1089
12.6M
         extra_bits = IMAX(extra_bits-1, 0);
1090
12.6M
      }
1091
12.9M
      extra_bits = IMIN(12, extra_bits);
1092
#endif
1093
      /* This is the basic no-split case */
1094
24.8M
      q = bits2pulses(m, i, LM, b);
1095
24.8M
      curr_bits = pulses2bits(m, i, LM, q);
1096
24.8M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
25.1M
      while (ctx->remaining_bits < 0 && q > 0)
1100
251k
      {
1101
251k
         ctx->remaining_bits += curr_bits;
1102
251k
         q--;
1103
251k
         curr_bits = pulses2bits(m, i, LM, q);
1104
251k
         ctx->remaining_bits -= curr_bits;
1105
251k
      }
1106
1107
24.8M
      if (q!=0)
1108
12.8M
      {
1109
6.97M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
12.8M
         if (encode)
1113
9.36M
         {
1114
9.36M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
9.36M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
9.36M
                           ctx->arch);
1117
9.36M
         } else {
1118
3.49M
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
3.49M
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
3.49M
         }
1121
#ifdef ENABLE_QEXT
1122
5.99M
      } else if (ext_b > 2*N<<BITRES)
1123
16.4k
      {
1124
16.4k
         extra_bits = ext_b/(N-1)>>BITRES;
1125
16.4k
         ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec);
1126
16.4k
         if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1127
2.45k
            extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1128
2.45k
            extra_bits = IMAX(extra_bits-1, 0);
1129
2.45k
         }
1130
16.4k
         extra_bits = IMIN(14, extra_bits);
1131
16.4k
         if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth);
1132
16.4k
         else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain);
1133
#endif
1134
12.0M
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
12.0M
         int j;
1137
12.0M
         if (ctx->resynth)
1138
9.13M
         {
1139
9.13M
            unsigned cm_mask;
1140
            /* B can be as large as 16, so this shift might overflow an int on a
1141
               16-bit platform; use a long to get defined behavior.*/
1142
9.13M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
9.13M
            fill &= cm_mask;
1144
9.13M
            if (!fill)
1145
2.83M
            {
1146
2.83M
               OPUS_CLEAR(X, N);
1147
6.30M
            } else {
1148
6.30M
               if (lowband == NULL)
1149
297k
               {
1150
                  /* Noise */
1151
4.67M
                  for (j=0;j<N;j++)
1152
4.38M
                  {
1153
4.38M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
4.38M
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
4.38M
                  }
1156
297k
                  cm = cm_mask;
1157
6.00M
               } else {
1158
                  /* Folded spectrum */
1159
83.3M
                  for (j=0;j<N;j++)
1160
77.3M
                  {
1161
77.3M
                     opus_val16 tmp;
1162
77.3M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
77.3M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
77.3M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
77.3M
                     X[j] = lowband[j]+tmp;
1167
77.3M
                  }
1168
6.00M
                  cm = fill;
1169
6.00M
               }
1170
6.30M
               renormalise_vector(X, N, gain, ctx->arch);
1171
6.30M
            }
1172
9.13M
         }
1173
12.0M
      }
1174
24.8M
   }
1175
1176
29.4M
   return cm;
1177
29.4M
}
bands.c:quant_partition
Line
Count
Source
978
6.97M
{
979
6.97M
   const unsigned char *cache;
980
6.97M
   int q;
981
6.97M
   int curr_bits;
982
6.97M
   int imid=0, iside=0;
983
6.97M
   int B0=B;
984
6.97M
   opus_val32 mid=0, side=0;
985
6.97M
   unsigned cm=0;
986
6.97M
   celt_norm *Y=NULL;
987
6.97M
   int encode;
988
6.97M
   const CELTMode *m;
989
6.97M
   int i;
990
6.97M
   int spread;
991
6.97M
   ec_ctx *ec;
992
993
6.97M
   encode = ctx->encode;
994
6.97M
   m = ctx->m;
995
6.97M
   i = ctx->i;
996
6.97M
   spread = ctx->spread;
997
6.97M
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
6.97M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
6.97M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
1.01M
   {
1003
1.01M
      int mbits, sbits, delta;
1004
1.01M
      int itheta;
1005
1.01M
      int qalloc;
1006
1.01M
      struct split_ctx sctx;
1007
1.01M
      celt_norm *next_lowband2=NULL;
1008
1.01M
      opus_int32 rebalance;
1009
1010
1.01M
      N >>= 1;
1011
1.01M
      Y = X+N;
1012
1.01M
      LM -= 1;
1013
1.01M
      if (B==1)
1014
310k
         fill = (fill&1)|(fill<<1);
1015
1.01M
      B = (B+1)>>1;
1016
1017
1.01M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
1.01M
      imid = sctx.imid;
1019
1.01M
      iside = sctx.iside;
1020
1.01M
      delta = sctx.delta;
1021
1.01M
      itheta = sctx.itheta;
1022
1.01M
      qalloc = sctx.qalloc;
1023
1.01M
#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.01M
      mid = SHL32(EXTEND32(imid), 16);
1031
1.01M
      side = SHL32(EXTEND32(iside), 16);
1032
1.01M
# 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.01M
      if (B0>1 && (itheta&0x3fff))
1047
540k
      {
1048
540k
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
276k
            delta -= delta>>(4-LM);
1051
264k
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
264k
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
540k
      }
1055
1.01M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
1.01M
      sbits = b-mbits;
1057
1.01M
      ctx->remaining_bits -= qalloc;
1058
1059
1.01M
      if (lowband)
1060
397k
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
1.01M
      rebalance = ctx->remaining_bits;
1063
1.01M
      if (mbits >= sbits)
1064
459k
      {
1065
459k
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
459k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
459k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
459k
         if (rebalance > 3<<BITRES && itheta!=0)
1069
150k
            sbits += rebalance - (3<<BITRES);
1070
459k
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
459k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
551k
      } else {
1073
551k
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
551k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
551k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
551k
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
176k
            mbits += rebalance - (3<<BITRES);
1078
551k
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
551k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
551k
      }
1081
5.96M
   } else {
1082
#ifdef ENABLE_QEXT
1083
      int extra_bits;
1084
      int ext_remaining_bits;
1085
      extra_bits = ext_b/(N-1)>>BITRES;
1086
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1087
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1088
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1089
         extra_bits = IMAX(extra_bits-1, 0);
1090
      }
1091
      extra_bits = IMIN(12, extra_bits);
1092
#endif
1093
      /* This is the basic no-split case */
1094
5.96M
      q = bits2pulses(m, i, LM, b);
1095
5.96M
      curr_bits = pulses2bits(m, i, LM, q);
1096
5.96M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
6.01M
      while (ctx->remaining_bits < 0 && q > 0)
1100
58.0k
      {
1101
58.0k
         ctx->remaining_bits += curr_bits;
1102
58.0k
         q--;
1103
58.0k
         curr_bits = pulses2bits(m, i, LM, q);
1104
58.0k
         ctx->remaining_bits -= curr_bits;
1105
58.0k
      }
1106
1107
5.96M
      if (q!=0)
1108
2.93M
      {
1109
2.93M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
2.93M
         if (encode)
1113
2.22M
         {
1114
2.22M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
2.22M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
2.22M
                           ctx->arch);
1117
2.22M
         } else {
1118
713k
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
713k
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
713k
         }
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.02M
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
3.02M
         int j;
1137
3.02M
         if (ctx->resynth)
1138
2.31M
         {
1139
2.31M
            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.31M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
2.31M
            fill &= cm_mask;
1144
2.31M
            if (!fill)
1145
723k
            {
1146
723k
               OPUS_CLEAR(X, N);
1147
1.59M
            } else {
1148
1.59M
               if (lowband == NULL)
1149
63.3k
               {
1150
                  /* Noise */
1151
939k
                  for (j=0;j<N;j++)
1152
876k
                  {
1153
876k
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
876k
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
876k
                  }
1156
63.3k
                  cm = cm_mask;
1157
1.53M
               } else {
1158
                  /* Folded spectrum */
1159
21.7M
                  for (j=0;j<N;j++)
1160
20.2M
                  {
1161
20.2M
                     opus_val16 tmp;
1162
20.2M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
20.2M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
20.2M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
20.2M
                     X[j] = lowband[j]+tmp;
1167
20.2M
                  }
1168
1.53M
                  cm = fill;
1169
1.53M
               }
1170
1.59M
               renormalise_vector(X, N, gain, ctx->arch);
1171
1.59M
            }
1172
2.31M
         }
1173
3.02M
      }
1174
5.96M
   }
1175
1176
6.97M
   return cm;
1177
6.97M
}
bands.c:quant_partition
Line
Count
Source
978
7.77M
{
979
7.77M
   const unsigned char *cache;
980
7.77M
   int q;
981
7.77M
   int curr_bits;
982
7.77M
   int imid=0, iside=0;
983
7.77M
   int B0=B;
984
7.77M
   opus_val32 mid=0, side=0;
985
7.77M
   unsigned cm=0;
986
7.77M
   celt_norm *Y=NULL;
987
7.77M
   int encode;
988
7.77M
   const CELTMode *m;
989
7.77M
   int i;
990
7.77M
   int spread;
991
7.77M
   ec_ctx *ec;
992
993
7.77M
   encode = ctx->encode;
994
7.77M
   m = ctx->m;
995
7.77M
   i = ctx->i;
996
7.77M
   spread = ctx->spread;
997
7.77M
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
7.77M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
7.77M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
1.29M
   {
1003
1.29M
      int mbits, sbits, delta;
1004
1.29M
      int itheta;
1005
1.29M
      int qalloc;
1006
1.29M
      struct split_ctx sctx;
1007
1.29M
      celt_norm *next_lowband2=NULL;
1008
1.29M
      opus_int32 rebalance;
1009
1010
1.29M
      N >>= 1;
1011
1.29M
      Y = X+N;
1012
1.29M
      LM -= 1;
1013
1.29M
      if (B==1)
1014
452k
         fill = (fill&1)|(fill<<1);
1015
1.29M
      B = (B+1)>>1;
1016
1017
1.29M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
1.29M
      imid = sctx.imid;
1019
1.29M
      iside = sctx.iside;
1020
1.29M
      delta = sctx.delta;
1021
1.29M
      itheta = sctx.itheta;
1022
1.29M
      qalloc = sctx.qalloc;
1023
1.29M
#ifdef FIXED_POINT
1024
1.29M
# ifdef ENABLE_QEXT
1025
1.29M
      (void)imid;
1026
1.29M
      (void)iside;
1027
1.29M
      mid = celt_cos_norm32(sctx.itheta_q30);
1028
1.29M
      side = celt_cos_norm32((1<<30)-sctx.itheta_q30);
1029
# else
1030
      mid = SHL32(EXTEND32(imid), 16);
1031
      side = SHL32(EXTEND32(iside), 16);
1032
# endif
1033
#else
1034
# ifdef ENABLE_QEXT
1035
      (void)imid;
1036
      (void)iside;
1037
      mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1038
      side = celt_cos_norm2(1.f-sctx.itheta_q30*(1.f/(1<<30)));
1039
# else
1040
      mid = (1.f/32768)*imid;
1041
      side = (1.f/32768)*iside;
1042
# endif
1043
#endif
1044
1045
      /* Give more bits to low-energy MDCTs than they would otherwise deserve */
1046
1.29M
      if (B0>1 && (itheta&0x3fff))
1047
659k
      {
1048
659k
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
334k
            delta -= delta>>(4-LM);
1051
324k
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
324k
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
659k
      }
1055
1.29M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
1.29M
      sbits = b-mbits;
1057
1.29M
      ctx->remaining_bits -= qalloc;
1058
1059
1.29M
      if (lowband)
1060
544k
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
1.29M
      rebalance = ctx->remaining_bits;
1063
1.29M
      if (mbits >= sbits)
1064
596k
      {
1065
596k
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
596k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
596k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
596k
         if (rebalance > 3<<BITRES && itheta!=0)
1069
218k
            sbits += rebalance - (3<<BITRES);
1070
596k
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
596k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
693k
      } else {
1073
693k
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
693k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
693k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
693k
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
248k
            mbits += rebalance - (3<<BITRES);
1078
693k
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
693k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
693k
      }
1081
6.48M
   } else {
1082
6.48M
#ifdef ENABLE_QEXT
1083
6.48M
      int extra_bits;
1084
6.48M
      int ext_remaining_bits;
1085
6.48M
      extra_bits = ext_b/(N-1)>>BITRES;
1086
6.48M
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1087
6.48M
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1088
6.34M
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1089
6.34M
         extra_bits = IMAX(extra_bits-1, 0);
1090
6.34M
      }
1091
6.48M
      extra_bits = IMIN(12, extra_bits);
1092
6.48M
#endif
1093
      /* This is the basic no-split case */
1094
6.48M
      q = bits2pulses(m, i, LM, b);
1095
6.48M
      curr_bits = pulses2bits(m, i, LM, q);
1096
6.48M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
6.55M
      while (ctx->remaining_bits < 0 && q > 0)
1100
67.7k
      {
1101
67.7k
         ctx->remaining_bits += curr_bits;
1102
67.7k
         q--;
1103
67.7k
         curr_bits = pulses2bits(m, i, LM, q);
1104
67.7k
         ctx->remaining_bits -= curr_bits;
1105
67.7k
      }
1106
1107
6.48M
      if (q!=0)
1108
3.48M
      {
1109
3.48M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
3.48M
         if (encode)
1113
2.45M
         {
1114
2.45M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
2.45M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
2.45M
                           ctx->arch);
1117
2.45M
         } else {
1118
1.03M
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
1.03M
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
1.03M
         }
1121
3.48M
#ifdef ENABLE_QEXT
1122
3.48M
      } else if (ext_b > 2*N<<BITRES)
1123
8.22k
      {
1124
8.22k
         extra_bits = ext_b/(N-1)>>BITRES;
1125
8.22k
         ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec);
1126
8.22k
         if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1127
1.22k
            extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1128
1.22k
            extra_bits = IMAX(extra_bits-1, 0);
1129
1.22k
         }
1130
8.22k
         extra_bits = IMIN(14, extra_bits);
1131
8.22k
         if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth);
1132
8.22k
         else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain);
1133
8.22k
#endif
1134
2.99M
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
2.99M
         int j;
1137
2.99M
         if (ctx->resynth)
1138
2.24M
         {
1139
2.24M
            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.24M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
2.24M
            fill &= cm_mask;
1144
2.24M
            if (!fill)
1145
693k
            {
1146
693k
               OPUS_CLEAR(X, N);
1147
1.55M
            } else {
1148
1.55M
               if (lowband == NULL)
1149
85.3k
               {
1150
                  /* Noise */
1151
1.39M
                  for (j=0;j<N;j++)
1152
1.31M
                  {
1153
1.31M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
1.31M
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
1.31M
                  }
1156
85.3k
                  cm = cm_mask;
1157
1.46M
               } else {
1158
                  /* Folded spectrum */
1159
19.9M
                  for (j=0;j<N;j++)
1160
18.4M
                  {
1161
18.4M
                     opus_val16 tmp;
1162
18.4M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
18.4M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
18.4M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
18.4M
                     X[j] = lowband[j]+tmp;
1167
18.4M
                  }
1168
1.46M
                  cm = fill;
1169
1.46M
               }
1170
1.55M
               renormalise_vector(X, N, gain, ctx->arch);
1171
1.55M
            }
1172
2.24M
         }
1173
2.99M
      }
1174
6.48M
   }
1175
1176
7.77M
   return cm;
1177
7.77M
}
bands.c:quant_partition
Line
Count
Source
978
7.77M
{
979
7.77M
   const unsigned char *cache;
980
7.77M
   int q;
981
7.77M
   int curr_bits;
982
7.77M
   int imid=0, iside=0;
983
7.77M
   int B0=B;
984
7.77M
   opus_val32 mid=0, side=0;
985
7.77M
   unsigned cm=0;
986
7.77M
   celt_norm *Y=NULL;
987
7.77M
   int encode;
988
7.77M
   const CELTMode *m;
989
7.77M
   int i;
990
7.77M
   int spread;
991
7.77M
   ec_ctx *ec;
992
993
7.77M
   encode = ctx->encode;
994
7.77M
   m = ctx->m;
995
7.77M
   i = ctx->i;
996
7.77M
   spread = ctx->spread;
997
7.77M
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
7.77M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
7.77M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
1.29M
   {
1003
1.29M
      int mbits, sbits, delta;
1004
1.29M
      int itheta;
1005
1.29M
      int qalloc;
1006
1.29M
      struct split_ctx sctx;
1007
1.29M
      celt_norm *next_lowband2=NULL;
1008
1.29M
      opus_int32 rebalance;
1009
1010
1.29M
      N >>= 1;
1011
1.29M
      Y = X+N;
1012
1.29M
      LM -= 1;
1013
1.29M
      if (B==1)
1014
452k
         fill = (fill&1)|(fill<<1);
1015
1.29M
      B = (B+1)>>1;
1016
1017
1.29M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
1.29M
      imid = sctx.imid;
1019
1.29M
      iside = sctx.iside;
1020
1.29M
      delta = sctx.delta;
1021
1.29M
      itheta = sctx.itheta;
1022
1.29M
      qalloc = sctx.qalloc;
1023
#ifdef FIXED_POINT
1024
# ifdef ENABLE_QEXT
1025
      (void)imid;
1026
      (void)iside;
1027
      mid = celt_cos_norm32(sctx.itheta_q30);
1028
      side = celt_cos_norm32((1<<30)-sctx.itheta_q30);
1029
# else
1030
      mid = SHL32(EXTEND32(imid), 16);
1031
      side = SHL32(EXTEND32(iside), 16);
1032
# endif
1033
#else
1034
1.29M
# ifdef ENABLE_QEXT
1035
1.29M
      (void)imid;
1036
1.29M
      (void)iside;
1037
1.29M
      mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1038
1.29M
      side = celt_cos_norm2(1.f-sctx.itheta_q30*(1.f/(1<<30)));
1039
# else
1040
      mid = (1.f/32768)*imid;
1041
      side = (1.f/32768)*iside;
1042
# endif
1043
1.29M
#endif
1044
1045
      /* Give more bits to low-energy MDCTs than they would otherwise deserve */
1046
1.29M
      if (B0>1 && (itheta&0x3fff))
1047
659k
      {
1048
659k
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
334k
            delta -= delta>>(4-LM);
1051
324k
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
324k
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
659k
      }
1055
1.29M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
1.29M
      sbits = b-mbits;
1057
1.29M
      ctx->remaining_bits -= qalloc;
1058
1059
1.29M
      if (lowband)
1060
544k
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
1.29M
      rebalance = ctx->remaining_bits;
1063
1.29M
      if (mbits >= sbits)
1064
596k
      {
1065
596k
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
596k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
596k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
596k
         if (rebalance > 3<<BITRES && itheta!=0)
1069
218k
            sbits += rebalance - (3<<BITRES);
1070
596k
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
596k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
693k
      } else {
1073
693k
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
693k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
693k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
693k
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
248k
            mbits += rebalance - (3<<BITRES);
1078
693k
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
693k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
693k
      }
1081
6.48M
   } else {
1082
6.48M
#ifdef ENABLE_QEXT
1083
6.48M
      int extra_bits;
1084
6.48M
      int ext_remaining_bits;
1085
6.48M
      extra_bits = ext_b/(N-1)>>BITRES;
1086
6.48M
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1087
6.48M
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1088
6.34M
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1089
6.34M
         extra_bits = IMAX(extra_bits-1, 0);
1090
6.34M
      }
1091
6.48M
      extra_bits = IMIN(12, extra_bits);
1092
6.48M
#endif
1093
      /* This is the basic no-split case */
1094
6.48M
      q = bits2pulses(m, i, LM, b);
1095
6.48M
      curr_bits = pulses2bits(m, i, LM, q);
1096
6.48M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
6.55M
      while (ctx->remaining_bits < 0 && q > 0)
1100
67.7k
      {
1101
67.7k
         ctx->remaining_bits += curr_bits;
1102
67.7k
         q--;
1103
67.7k
         curr_bits = pulses2bits(m, i, LM, q);
1104
67.7k
         ctx->remaining_bits -= curr_bits;
1105
67.7k
      }
1106
1107
6.48M
      if (q!=0)
1108
3.48M
      {
1109
3.48M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
3.48M
         if (encode)
1113
2.45M
         {
1114
2.45M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
2.45M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
2.45M
                           ctx->arch);
1117
2.45M
         } else {
1118
1.03M
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
1.03M
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
1.03M
         }
1121
3.48M
#ifdef ENABLE_QEXT
1122
3.48M
      } else if (ext_b > 2*N<<BITRES)
1123
8.22k
      {
1124
8.22k
         extra_bits = ext_b/(N-1)>>BITRES;
1125
8.22k
         ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec);
1126
8.22k
         if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1127
1.22k
            extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1128
1.22k
            extra_bits = IMAX(extra_bits-1, 0);
1129
1.22k
         }
1130
8.22k
         extra_bits = IMIN(14, extra_bits);
1131
8.22k
         if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth);
1132
8.22k
         else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain);
1133
8.22k
#endif
1134
2.99M
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
2.99M
         int j;
1137
2.99M
         if (ctx->resynth)
1138
2.24M
         {
1139
2.24M
            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.24M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
2.24M
            fill &= cm_mask;
1144
2.24M
            if (!fill)
1145
693k
            {
1146
693k
               OPUS_CLEAR(X, N);
1147
1.55M
            } else {
1148
1.55M
               if (lowband == NULL)
1149
85.3k
               {
1150
                  /* Noise */
1151
1.39M
                  for (j=0;j<N;j++)
1152
1.31M
                  {
1153
1.31M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
1.31M
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
1.31M
                  }
1156
85.3k
                  cm = cm_mask;
1157
1.46M
               } else {
1158
                  /* Folded spectrum */
1159
19.9M
                  for (j=0;j<N;j++)
1160
18.4M
                  {
1161
18.4M
                     opus_val16 tmp;
1162
18.4M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
18.4M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
18.4M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
18.4M
                     X[j] = lowband[j]+tmp;
1167
18.4M
                  }
1168
1.46M
                  cm = fill;
1169
1.46M
               }
1170
1.55M
               renormalise_vector(X, N, gain, ctx->arch);
1171
1.55M
            }
1172
2.24M
         }
1173
2.99M
      }
1174
6.48M
   }
1175
1176
7.77M
   return cm;
1177
7.77M
}
bands.c:quant_partition
Line
Count
Source
978
6.97M
{
979
6.97M
   const unsigned char *cache;
980
6.97M
   int q;
981
6.97M
   int curr_bits;
982
6.97M
   int imid=0, iside=0;
983
6.97M
   int B0=B;
984
6.97M
   opus_val32 mid=0, side=0;
985
6.97M
   unsigned cm=0;
986
6.97M
   celt_norm *Y=NULL;
987
6.97M
   int encode;
988
6.97M
   const CELTMode *m;
989
6.97M
   int i;
990
6.97M
   int spread;
991
6.97M
   ec_ctx *ec;
992
993
6.97M
   encode = ctx->encode;
994
6.97M
   m = ctx->m;
995
6.97M
   i = ctx->i;
996
6.97M
   spread = ctx->spread;
997
6.97M
   ec = ctx->ec;
998
999
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1000
6.97M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1001
6.97M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1002
1.01M
   {
1003
1.01M
      int mbits, sbits, delta;
1004
1.01M
      int itheta;
1005
1.01M
      int qalloc;
1006
1.01M
      struct split_ctx sctx;
1007
1.01M
      celt_norm *next_lowband2=NULL;
1008
1.01M
      opus_int32 rebalance;
1009
1010
1.01M
      N >>= 1;
1011
1.01M
      Y = X+N;
1012
1.01M
      LM -= 1;
1013
1.01M
      if (B==1)
1014
310k
         fill = (fill&1)|(fill<<1);
1015
1.01M
      B = (B+1)>>1;
1016
1017
1.01M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1018
1.01M
      imid = sctx.imid;
1019
1.01M
      iside = sctx.iside;
1020
1.01M
      delta = sctx.delta;
1021
1.01M
      itheta = sctx.itheta;
1022
1.01M
      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.01M
      mid = (1.f/32768)*imid;
1041
1.01M
      side = (1.f/32768)*iside;
1042
1.01M
# endif
1043
1.01M
#endif
1044
1045
      /* Give more bits to low-energy MDCTs than they would otherwise deserve */
1046
1.01M
      if (B0>1 && (itheta&0x3fff))
1047
540k
      {
1048
540k
         if (itheta > 8192)
1049
            /* Rough approximation for pre-echo masking */
1050
276k
            delta -= delta>>(4-LM);
1051
264k
         else
1052
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1053
264k
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1054
540k
      }
1055
1.01M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1056
1.01M
      sbits = b-mbits;
1057
1.01M
      ctx->remaining_bits -= qalloc;
1058
1059
1.01M
      if (lowband)
1060
397k
         next_lowband2 = lowband+N; /* >32-bit split case */
1061
1062
1.01M
      rebalance = ctx->remaining_bits;
1063
1.01M
      if (mbits >= sbits)
1064
459k
      {
1065
459k
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1066
459k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1067
459k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1068
459k
         if (rebalance > 3<<BITRES && itheta!=0)
1069
150k
            sbits += rebalance - (3<<BITRES);
1070
459k
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1071
459k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1072
551k
      } else {
1073
551k
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1074
551k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1075
551k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1076
551k
         if (rebalance > 3<<BITRES && itheta!=16384)
1077
176k
            mbits += rebalance - (3<<BITRES);
1078
551k
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1079
551k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1080
551k
      }
1081
5.96M
   } else {
1082
#ifdef ENABLE_QEXT
1083
      int extra_bits;
1084
      int ext_remaining_bits;
1085
      extra_bits = ext_b/(N-1)>>BITRES;
1086
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1087
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1088
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1089
         extra_bits = IMAX(extra_bits-1, 0);
1090
      }
1091
      extra_bits = IMIN(12, extra_bits);
1092
#endif
1093
      /* This is the basic no-split case */
1094
5.96M
      q = bits2pulses(m, i, LM, b);
1095
5.96M
      curr_bits = pulses2bits(m, i, LM, q);
1096
5.96M
      ctx->remaining_bits -= curr_bits;
1097
1098
      /* Ensures we can never bust the budget */
1099
6.01M
      while (ctx->remaining_bits < 0 && q > 0)
1100
58.0k
      {
1101
58.0k
         ctx->remaining_bits += curr_bits;
1102
58.0k
         q--;
1103
58.0k
         curr_bits = pulses2bits(m, i, LM, q);
1104
58.0k
         ctx->remaining_bits -= curr_bits;
1105
58.0k
      }
1106
1107
5.96M
      if (q!=0)
1108
2.93M
      {
1109
2.93M
         int K = get_pulses(q);
1110
1111
         /* Finally do the actual quantization */
1112
2.93M
         if (encode)
1113
2.22M
         {
1114
2.22M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1115
2.22M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1116
2.22M
                           ctx->arch);
1117
2.22M
         } else {
1118
713k
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1119
713k
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1120
713k
         }
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.02M
      } else {
1135
         /* If there's no pulse, fill the band anyway */
1136
3.02M
         int j;
1137
3.02M
         if (ctx->resynth)
1138
2.31M
         {
1139
2.31M
            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.31M
            cm_mask = (unsigned)(1UL<<B)-1;
1143
2.31M
            fill &= cm_mask;
1144
2.31M
            if (!fill)
1145
723k
            {
1146
723k
               OPUS_CLEAR(X, N);
1147
1.59M
            } else {
1148
1.59M
               if (lowband == NULL)
1149
63.3k
               {
1150
                  /* Noise */
1151
939k
                  for (j=0;j<N;j++)
1152
876k
                  {
1153
876k
                     ctx->seed = celt_lcg_rand(ctx->seed);
1154
876k
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1155
876k
                  }
1156
63.3k
                  cm = cm_mask;
1157
1.53M
               } else {
1158
                  /* Folded spectrum */
1159
21.7M
                  for (j=0;j<N;j++)
1160
20.2M
                  {
1161
20.2M
                     opus_val16 tmp;
1162
20.2M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1163
                     /* About 48 dB below the "normal" folding level */
1164
20.2M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1165
20.2M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1166
20.2M
                     X[j] = lowband[j]+tmp;
1167
20.2M
                  }
1168
1.53M
                  cm = fill;
1169
1.53M
               }
1170
1.59M
               renormalise_vector(X, N, gain, ctx->arch);
1171
1.59M
            }
1172
2.31M
         }
1173
3.02M
      }
1174
5.96M
   }
1175
1176
6.97M
   return cm;
1177
6.97M
}
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
177k
{
1182
177k
   celt_assert(LM>=0);
1183
177k
   ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1184
177k
   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
177k
   if (LM==0 || b<=2*N<<BITRES) {
1187
102k
      int res, ret;
1188
102k
      b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits);
1189
      /* Resolution left after taking into account coding the cube face. */
1190
102k
      res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES;
1191
102k
      res = IMIN(14, IMAX(0, res));
1192
102k
      if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth);
1193
102k
      else ret = cubic_unquant(X, N, res, B, ec, gain);
1194
102k
      ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1195
102k
      return ret;
1196
102k
   } else {
1197
74.8k
      celt_norm *Y;
1198
74.8k
      opus_int32 itheta_q30;
1199
74.8k
      opus_val32 g1, g2;
1200
74.8k
      opus_int32 theta_res;
1201
74.8k
      opus_int32 qtheta;
1202
74.8k
      int delta;
1203
74.8k
      int b1, b2;
1204
74.8k
      int cm;
1205
74.8k
      int N0;
1206
74.8k
      N0 = N;
1207
74.8k
      N >>= 1;
1208
74.8k
      Y = X+N;
1209
74.8k
      LM -= 1;
1210
74.8k
      B = (B+1)>>1;
1211
74.8k
      theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1);
1212
74.8k
      if (encode) {
1213
0
         itheta_q30 = stereo_itheta(X, Y, 0, N, ctx->arch);
1214
0
         qtheta = (itheta_q30+(1<<(29-theta_res)))>>(30-theta_res);
1215
0
         ec_enc_uint(ec, qtheta, (1<<theta_res)+1);
1216
74.8k
      } else {
1217
74.8k
         qtheta = ec_dec_uint(ec, (1<<theta_res)+1);
1218
74.8k
      }
1219
74.8k
      itheta_q30 = qtheta<<(30-theta_res);
1220
74.8k
      b -= theta_res<<BITRES;
1221
74.8k
      delta = (N0-1) * 23 * ((itheta_q30>>16)-8192) >> (17-BITRES);
1222
1223
#ifdef FIXED_POINT
1224
      g1 = celt_cos_norm32(itheta_q30);
1225
      g2 = celt_cos_norm32((1<<30)-itheta_q30);
1226
#else
1227
      g1 = celt_cos_norm2(itheta_q30*(1.f/(1<<30)));
1228
      g2 = celt_cos_norm2(1.f-itheta_q30*(1.f/(1<<30)));
1229
#endif
1230
74.8k
      if (itheta_q30 == 0) {
1231
2.25k
         b1=b;
1232
2.25k
         b2=0;
1233
72.5k
      } else if (itheta_q30==1073741824) {
1234
2.44k
         b1=0;
1235
2.44k
         b2=b;
1236
70.1k
      } else {
1237
70.1k
         b1 = IMIN(b, IMAX(0, (b-delta)/2));
1238
70.1k
         b2 = b-b1;
1239
70.1k
      }
1240
74.8k
      cm  = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode);
1241
74.8k
      cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode);
1242
74.8k
      return cm;
1243
74.8k
   }
1244
177k
}
bands.c:cubic_quant_partition
Line
Count
Source
1181
88.5k
{
1182
88.5k
   celt_assert(LM>=0);
1183
88.5k
   ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1184
88.5k
   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
88.5k
   if (LM==0 || b<=2*N<<BITRES) {
1187
51.1k
      int res, ret;
1188
51.1k
      b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits);
1189
      /* Resolution left after taking into account coding the cube face. */
1190
51.1k
      res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES;
1191
51.1k
      res = IMIN(14, IMAX(0, res));
1192
51.1k
      if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth);
1193
51.1k
      else ret = cubic_unquant(X, N, res, B, ec, gain);
1194
51.1k
      ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1195
51.1k
      return ret;
1196
51.1k
   } else {
1197
37.4k
      celt_norm *Y;
1198
37.4k
      opus_int32 itheta_q30;
1199
37.4k
      opus_val32 g1, g2;
1200
37.4k
      opus_int32 theta_res;
1201
37.4k
      opus_int32 qtheta;
1202
37.4k
      int delta;
1203
37.4k
      int b1, b2;
1204
37.4k
      int cm;
1205
37.4k
      int N0;
1206
37.4k
      N0 = N;
1207
37.4k
      N >>= 1;
1208
37.4k
      Y = X+N;
1209
37.4k
      LM -= 1;
1210
37.4k
      B = (B+1)>>1;
1211
37.4k
      theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1);
1212
37.4k
      if (encode) {
1213
0
         itheta_q30 = stereo_itheta(X, Y, 0, N, ctx->arch);
1214
0
         qtheta = (itheta_q30+(1<<(29-theta_res)))>>(30-theta_res);
1215
0
         ec_enc_uint(ec, qtheta, (1<<theta_res)+1);
1216
37.4k
      } else {
1217
37.4k
         qtheta = ec_dec_uint(ec, (1<<theta_res)+1);
1218
37.4k
      }
1219
37.4k
      itheta_q30 = qtheta<<(30-theta_res);
1220
37.4k
      b -= theta_res<<BITRES;
1221
37.4k
      delta = (N0-1) * 23 * ((itheta_q30>>16)-8192) >> (17-BITRES);
1222
1223
37.4k
#ifdef FIXED_POINT
1224
37.4k
      g1 = celt_cos_norm32(itheta_q30);
1225
37.4k
      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
37.4k
      if (itheta_q30 == 0) {
1231
1.12k
         b1=b;
1232
1.12k
         b2=0;
1233
36.2k
      } else if (itheta_q30==1073741824) {
1234
1.22k
         b1=0;
1235
1.22k
         b2=b;
1236
35.0k
      } else {
1237
35.0k
         b1 = IMIN(b, IMAX(0, (b-delta)/2));
1238
35.0k
         b2 = b-b1;
1239
35.0k
      }
1240
37.4k
      cm  = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode);
1241
37.4k
      cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode);
1242
37.4k
      return cm;
1243
37.4k
   }
1244
88.5k
}
bands.c:cubic_quant_partition
Line
Count
Source
1181
88.5k
{
1182
88.5k
   celt_assert(LM>=0);
1183
88.5k
   ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1184
88.5k
   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
88.5k
   if (LM==0 || b<=2*N<<BITRES) {
1187
51.1k
      int res, ret;
1188
51.1k
      b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits);
1189
      /* Resolution left after taking into account coding the cube face. */
1190
51.1k
      res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES;
1191
51.1k
      res = IMIN(14, IMAX(0, res));
1192
51.1k
      if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth);
1193
51.1k
      else ret = cubic_unquant(X, N, res, B, ec, gain);
1194
51.1k
      ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1195
51.1k
      return ret;
1196
51.1k
   } else {
1197
37.4k
      celt_norm *Y;
1198
37.4k
      opus_int32 itheta_q30;
1199
37.4k
      opus_val32 g1, g2;
1200
37.4k
      opus_int32 theta_res;
1201
37.4k
      opus_int32 qtheta;
1202
37.4k
      int delta;
1203
37.4k
      int b1, b2;
1204
37.4k
      int cm;
1205
37.4k
      int N0;
1206
37.4k
      N0 = N;
1207
37.4k
      N >>= 1;
1208
37.4k
      Y = X+N;
1209
37.4k
      LM -= 1;
1210
37.4k
      B = (B+1)>>1;
1211
37.4k
      theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1);
1212
37.4k
      if (encode) {
1213
0
         itheta_q30 = stereo_itheta(X, Y, 0, N, ctx->arch);
1214
0
         qtheta = (itheta_q30+(1<<(29-theta_res)))>>(30-theta_res);
1215
0
         ec_enc_uint(ec, qtheta, (1<<theta_res)+1);
1216
37.4k
      } else {
1217
37.4k
         qtheta = ec_dec_uint(ec, (1<<theta_res)+1);
1218
37.4k
      }
1219
37.4k
      itheta_q30 = qtheta<<(30-theta_res);
1220
37.4k
      b -= theta_res<<BITRES;
1221
37.4k
      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
37.4k
      g1 = celt_cos_norm2(itheta_q30*(1.f/(1<<30)));
1228
37.4k
      g2 = celt_cos_norm2(1.f-itheta_q30*(1.f/(1<<30)));
1229
37.4k
#endif
1230
37.4k
      if (itheta_q30 == 0) {
1231
1.12k
         b1=b;
1232
1.12k
         b2=0;
1233
36.2k
      } else if (itheta_q30==1073741824) {
1234
1.22k
         b1=0;
1235
1.22k
         b2=b;
1236
35.0k
      } else {
1237
35.0k
         b1 = IMIN(b, IMAX(0, (b-delta)/2));
1238
35.0k
         b2 = b-b1;
1239
35.0k
      }
1240
37.4k
      cm  = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode);
1241
37.4k
      cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode);
1242
37.4k
      return cm;
1243
37.4k
   }
1244
88.5k
}
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
23.6M
{
1254
23.6M
   int N0=N;
1255
23.6M
   int N_B=N;
1256
23.6M
   int N_B0;
1257
23.6M
   int B0=B;
1258
23.6M
   int time_divide=0;
1259
23.6M
   int recombine=0;
1260
23.6M
   int longBlocks;
1261
23.6M
   unsigned cm=0;
1262
23.6M
   int k;
1263
23.6M
   int encode;
1264
23.6M
   int tf_change;
1265
1266
23.6M
   encode = ctx->encode;
1267
23.6M
   tf_change = ctx->tf_change;
1268
1269
23.6M
   longBlocks = B0==1;
1270
1271
23.6M
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
23.6M
   if (N==1)
1275
3.35M
   {
1276
3.35M
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
3.35M
   }
1278
1279
20.3M
   if (tf_change>0)
1280
1.49M
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
20.3M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
2.35M
   {
1285
2.35M
      OPUS_COPY(lowband_scratch, lowband, N);
1286
2.35M
      lowband = lowband_scratch;
1287
2.35M
   }
1288
1289
22.7M
   for (k=0;k<recombine;k++)
1290
2.45M
   {
1291
2.45M
      static const unsigned char bit_interleave_table[16]={
1292
2.45M
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
2.45M
      };
1294
2.45M
      if (encode)
1295
724k
         haar1(X, N>>k, 1<<k);
1296
2.45M
      if (lowband)
1297
1.30M
         haar1(lowband, N>>k, 1<<k);
1298
2.45M
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
2.45M
   }
1300
20.3M
   B>>=recombine;
1301
20.3M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
23.8M
   while ((N_B&1) == 0 && tf_change<0)
1305
3.50M
   {
1306
3.50M
      if (encode)
1307
2.29M
         haar1(X, N_B, B);
1308
3.50M
      if (lowband)
1309
1.35M
         haar1(lowband, N_B, B);
1310
3.50M
      fill |= fill<<B;
1311
3.50M
      B <<= 1;
1312
3.50M
      N_B >>= 1;
1313
3.50M
      time_divide++;
1314
3.50M
      tf_change++;
1315
3.50M
   }
1316
20.3M
   B0=B;
1317
20.3M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
20.3M
   if (B0>1)
1321
6.38M
   {
1322
6.38M
      if (encode)
1323
5.19M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
6.38M
      if (lowband)
1325
1.82M
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
6.38M
   }
1327
1328
#ifdef ENABLE_QEXT
1329
10.4M
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1330
27.4k
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1331
27.4k
   } else
1332
10.3M
#endif
1333
10.3M
   {
1334
10.3M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
10.3M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
20.3M
   if (ctx->resynth)
1339
13.0M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
13.0M
      if (B0>1)
1342
3.09M
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
13.0M
      N_B = N_B0;
1346
13.0M
      B = B0;
1347
15.2M
      for (k=0;k<time_divide;k++)
1348
2.21M
      {
1349
2.21M
         B >>= 1;
1350
2.21M
         N_B <<= 1;
1351
2.21M
         cm |= cm>>B;
1352
2.21M
         haar1(X, N_B, B);
1353
2.21M
      }
1354
1355
15.1M
      for (k=0;k<recombine;k++)
1356
2.05M
      {
1357
2.05M
         static const unsigned char bit_deinterleave_table[16]={
1358
2.05M
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
2.05M
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
2.05M
         };
1361
2.05M
         cm = bit_deinterleave_table[cm];
1362
2.05M
         haar1(X, N0>>k, 1<<k);
1363
2.05M
      }
1364
13.0M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
13.0M
      if (lowband_out)
1368
9.01M
      {
1369
9.01M
         int j;
1370
9.01M
         opus_val16 n;
1371
9.01M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
94.3M
         for (j=0;j<N0;j++)
1373
85.3M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
9.01M
      }
1375
13.0M
      cm &= (1<<B)-1;
1376
13.0M
   }
1377
20.3M
   return cm;
1378
23.6M
}
bands.c:quant_band
Line
Count
Source
1253
5.79M
{
1254
5.79M
   int N0=N;
1255
5.79M
   int N_B=N;
1256
5.79M
   int N_B0;
1257
5.79M
   int B0=B;
1258
5.79M
   int time_divide=0;
1259
5.79M
   int recombine=0;
1260
5.79M
   int longBlocks;
1261
5.79M
   unsigned cm=0;
1262
5.79M
   int k;
1263
5.79M
   int encode;
1264
5.79M
   int tf_change;
1265
1266
5.79M
   encode = ctx->encode;
1267
5.79M
   tf_change = ctx->tf_change;
1268
1269
5.79M
   longBlocks = B0==1;
1270
1271
5.79M
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
5.79M
   if (N==1)
1275
850k
   {
1276
850k
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
850k
   }
1278
1279
4.94M
   if (tf_change>0)
1280
431k
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
4.94M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
593k
   {
1285
593k
      OPUS_COPY(lowband_scratch, lowband, N);
1286
593k
      lowband = lowband_scratch;
1287
593k
   }
1288
1289
5.69M
   for (k=0;k<recombine;k++)
1290
741k
   {
1291
741k
      static const unsigned char bit_interleave_table[16]={
1292
741k
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
741k
      };
1294
741k
      if (encode)
1295
169k
         haar1(X, N>>k, 1<<k);
1296
741k
      if (lowband)
1297
413k
         haar1(lowband, N>>k, 1<<k);
1298
741k
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
741k
   }
1300
4.94M
   B>>=recombine;
1301
4.94M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
5.76M
   while ((N_B&1) == 0 && tf_change<0)
1305
812k
   {
1306
812k
      if (encode)
1307
544k
         haar1(X, N_B, B);
1308
812k
      if (lowband)
1309
301k
         haar1(lowband, N_B, B);
1310
812k
      fill |= fill<<B;
1311
812k
      B <<= 1;
1312
812k
      N_B >>= 1;
1313
812k
      time_divide++;
1314
812k
      tf_change++;
1315
812k
   }
1316
4.94M
   B0=B;
1317
4.94M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
4.94M
   if (B0>1)
1321
1.52M
   {
1322
1.52M
      if (encode)
1323
1.26M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
1.52M
      if (lowband)
1325
418k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
1.52M
   }
1327
1328
#ifdef ENABLE_QEXT
1329
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1330
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1331
   } else
1332
#endif
1333
4.94M
   {
1334
4.94M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
4.94M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
4.94M
   if (ctx->resynth)
1339
3.19M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
3.19M
      if (B0>1)
1342
719k
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
3.19M
      N_B = N_B0;
1346
3.19M
      B = B0;
1347
3.69M
      for (k=0;k<time_divide;k++)
1348
504k
      {
1349
504k
         B >>= 1;
1350
504k
         N_B <<= 1;
1351
504k
         cm |= cm>>B;
1352
504k
         haar1(X, N_B, B);
1353
504k
      }
1354
1355
3.84M
      for (k=0;k<recombine;k++)
1356
650k
      {
1357
650k
         static const unsigned char bit_deinterleave_table[16]={
1358
650k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
650k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
650k
         };
1361
650k
         cm = bit_deinterleave_table[cm];
1362
650k
         haar1(X, N0>>k, 1<<k);
1363
650k
      }
1364
3.19M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
3.19M
      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.0M
         for (j=0;j<N0;j++)
1373
20.9M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
2.17M
      }
1375
3.19M
      cm &= (1<<B)-1;
1376
3.19M
   }
1377
4.94M
   return cm;
1378
5.79M
}
bands.c:quant_band
Line
Count
Source
1253
6.03M
{
1254
6.03M
   int N0=N;
1255
6.03M
   int N_B=N;
1256
6.03M
   int N_B0;
1257
6.03M
   int B0=B;
1258
6.03M
   int time_divide=0;
1259
6.03M
   int recombine=0;
1260
6.03M
   int longBlocks;
1261
6.03M
   unsigned cm=0;
1262
6.03M
   int k;
1263
6.03M
   int encode;
1264
6.03M
   int tf_change;
1265
1266
6.03M
   encode = ctx->encode;
1267
6.03M
   tf_change = ctx->tf_change;
1268
1269
6.03M
   longBlocks = B0==1;
1270
1271
6.03M
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
6.03M
   if (N==1)
1275
825k
   {
1276
825k
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
825k
   }
1278
1279
5.20M
   if (tf_change>0)
1280
314k
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
5.20M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
585k
   {
1285
585k
      OPUS_COPY(lowband_scratch, lowband, N);
1286
585k
      lowband = lowband_scratch;
1287
585k
   }
1288
1289
5.69M
   for (k=0;k<recombine;k++)
1290
487k
   {
1291
487k
      static const unsigned char bit_interleave_table[16]={
1292
487k
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
487k
      };
1294
487k
      if (encode)
1295
192k
         haar1(X, N>>k, 1<<k);
1296
487k
      if (lowband)
1297
237k
         haar1(lowband, N>>k, 1<<k);
1298
487k
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
487k
   }
1300
5.20M
   B>>=recombine;
1301
5.20M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
6.14M
   while ((N_B&1) == 0 && tf_change<0)
1305
939k
   {
1306
939k
      if (encode)
1307
602k
         haar1(X, N_B, B);
1308
939k
      if (lowband)
1309
377k
         haar1(lowband, N_B, B);
1310
939k
      fill |= fill<<B;
1311
939k
      B <<= 1;
1312
939k
      N_B >>= 1;
1313
939k
      time_divide++;
1314
939k
      tf_change++;
1315
939k
   }
1316
5.20M
   B0=B;
1317
5.20M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
5.20M
   if (B0>1)
1321
1.67M
   {
1322
1.67M
      if (encode)
1323
1.33M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
1.67M
      if (lowband)
1325
492k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
1.67M
   }
1327
1328
5.20M
#ifdef ENABLE_QEXT
1329
5.20M
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1330
13.7k
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1331
13.7k
   } else
1332
5.19M
#endif
1333
5.19M
   {
1334
5.19M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
5.19M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
5.20M
   if (ctx->resynth)
1339
3.33M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
3.33M
      if (B0>1)
1342
828k
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
3.33M
      N_B = N_B0;
1346
3.33M
      B = B0;
1347
3.94M
      for (k=0;k<time_divide;k++)
1348
605k
      {
1349
605k
         B >>= 1;
1350
605k
         N_B <<= 1;
1351
605k
         cm |= cm>>B;
1352
605k
         haar1(X, N_B, B);
1353
605k
      }
1354
1355
3.71M
      for (k=0;k<recombine;k++)
1356
378k
      {
1357
378k
         static const unsigned char bit_deinterleave_table[16]={
1358
378k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
378k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
378k
         };
1361
378k
         cm = bit_deinterleave_table[cm];
1362
378k
         haar1(X, N0>>k, 1<<k);
1363
378k
      }
1364
3.33M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
3.33M
      if (lowband_out)
1368
2.32M
      {
1369
2.32M
         int j;
1370
2.32M
         opus_val16 n;
1371
2.32M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
24.1M
         for (j=0;j<N0;j++)
1373
21.7M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
2.32M
      }
1375
3.33M
      cm &= (1<<B)-1;
1376
3.33M
   }
1377
5.20M
   return cm;
1378
6.03M
}
bands.c:quant_band
Line
Count
Source
1253
6.03M
{
1254
6.03M
   int N0=N;
1255
6.03M
   int N_B=N;
1256
6.03M
   int N_B0;
1257
6.03M
   int B0=B;
1258
6.03M
   int time_divide=0;
1259
6.03M
   int recombine=0;
1260
6.03M
   int longBlocks;
1261
6.03M
   unsigned cm=0;
1262
6.03M
   int k;
1263
6.03M
   int encode;
1264
6.03M
   int tf_change;
1265
1266
6.03M
   encode = ctx->encode;
1267
6.03M
   tf_change = ctx->tf_change;
1268
1269
6.03M
   longBlocks = B0==1;
1270
1271
6.03M
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
6.03M
   if (N==1)
1275
825k
   {
1276
825k
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
825k
   }
1278
1279
5.20M
   if (tf_change>0)
1280
314k
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
5.20M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
585k
   {
1285
585k
      OPUS_COPY(lowband_scratch, lowband, N);
1286
585k
      lowband = lowband_scratch;
1287
585k
   }
1288
1289
5.69M
   for (k=0;k<recombine;k++)
1290
487k
   {
1291
487k
      static const unsigned char bit_interleave_table[16]={
1292
487k
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
487k
      };
1294
487k
      if (encode)
1295
192k
         haar1(X, N>>k, 1<<k);
1296
487k
      if (lowband)
1297
237k
         haar1(lowband, N>>k, 1<<k);
1298
487k
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
487k
   }
1300
5.20M
   B>>=recombine;
1301
5.20M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
6.14M
   while ((N_B&1) == 0 && tf_change<0)
1305
939k
   {
1306
939k
      if (encode)
1307
602k
         haar1(X, N_B, B);
1308
939k
      if (lowband)
1309
377k
         haar1(lowband, N_B, B);
1310
939k
      fill |= fill<<B;
1311
939k
      B <<= 1;
1312
939k
      N_B >>= 1;
1313
939k
      time_divide++;
1314
939k
      tf_change++;
1315
939k
   }
1316
5.20M
   B0=B;
1317
5.20M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
5.20M
   if (B0>1)
1321
1.67M
   {
1322
1.67M
      if (encode)
1323
1.33M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
1.67M
      if (lowband)
1325
492k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
1.67M
   }
1327
1328
5.20M
#ifdef ENABLE_QEXT
1329
5.20M
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1330
13.7k
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1331
13.7k
   } else
1332
5.19M
#endif
1333
5.19M
   {
1334
5.19M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
5.19M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
5.20M
   if (ctx->resynth)
1339
3.33M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
3.33M
      if (B0>1)
1342
828k
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
3.33M
      N_B = N_B0;
1346
3.33M
      B = B0;
1347
3.94M
      for (k=0;k<time_divide;k++)
1348
605k
      {
1349
605k
         B >>= 1;
1350
605k
         N_B <<= 1;
1351
605k
         cm |= cm>>B;
1352
605k
         haar1(X, N_B, B);
1353
605k
      }
1354
1355
3.71M
      for (k=0;k<recombine;k++)
1356
378k
      {
1357
378k
         static const unsigned char bit_deinterleave_table[16]={
1358
378k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
378k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
378k
         };
1361
378k
         cm = bit_deinterleave_table[cm];
1362
378k
         haar1(X, N0>>k, 1<<k);
1363
378k
      }
1364
3.33M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
3.33M
      if (lowband_out)
1368
2.32M
      {
1369
2.32M
         int j;
1370
2.32M
         opus_val16 n;
1371
2.32M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1372
24.1M
         for (j=0;j<N0;j++)
1373
21.7M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
2.32M
      }
1375
3.33M
      cm &= (1<<B)-1;
1376
3.33M
   }
1377
5.20M
   return cm;
1378
6.03M
}
bands.c:quant_band
Line
Count
Source
1253
5.79M
{
1254
5.79M
   int N0=N;
1255
5.79M
   int N_B=N;
1256
5.79M
   int N_B0;
1257
5.79M
   int B0=B;
1258
5.79M
   int time_divide=0;
1259
5.79M
   int recombine=0;
1260
5.79M
   int longBlocks;
1261
5.79M
   unsigned cm=0;
1262
5.79M
   int k;
1263
5.79M
   int encode;
1264
5.79M
   int tf_change;
1265
1266
5.79M
   encode = ctx->encode;
1267
5.79M
   tf_change = ctx->tf_change;
1268
1269
5.79M
   longBlocks = B0==1;
1270
1271
5.79M
   N_B = celt_udiv(N_B, B);
1272
1273
   /* Special case for one sample */
1274
5.79M
   if (N==1)
1275
850k
   {
1276
850k
      return quant_band_n1(ctx, X, NULL, lowband_out);
1277
850k
   }
1278
1279
4.94M
   if (tf_change>0)
1280
431k
      recombine = tf_change;
1281
   /* Band recombining to increase frequency resolution */
1282
1283
4.94M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1284
593k
   {
1285
593k
      OPUS_COPY(lowband_scratch, lowband, N);
1286
593k
      lowband = lowband_scratch;
1287
593k
   }
1288
1289
5.69M
   for (k=0;k<recombine;k++)
1290
741k
   {
1291
741k
      static const unsigned char bit_interleave_table[16]={
1292
741k
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1293
741k
      };
1294
741k
      if (encode)
1295
169k
         haar1(X, N>>k, 1<<k);
1296
741k
      if (lowband)
1297
413k
         haar1(lowband, N>>k, 1<<k);
1298
741k
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1299
741k
   }
1300
4.94M
   B>>=recombine;
1301
4.94M
   N_B<<=recombine;
1302
1303
   /* Increasing the time resolution */
1304
5.76M
   while ((N_B&1) == 0 && tf_change<0)
1305
812k
   {
1306
812k
      if (encode)
1307
544k
         haar1(X, N_B, B);
1308
812k
      if (lowband)
1309
301k
         haar1(lowband, N_B, B);
1310
812k
      fill |= fill<<B;
1311
812k
      B <<= 1;
1312
812k
      N_B >>= 1;
1313
812k
      time_divide++;
1314
812k
      tf_change++;
1315
812k
   }
1316
4.94M
   B0=B;
1317
4.94M
   N_B0 = N_B;
1318
1319
   /* Reorganize the samples in time order instead of frequency order */
1320
4.94M
   if (B0>1)
1321
1.52M
   {
1322
1.52M
      if (encode)
1323
1.26M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1324
1.52M
      if (lowband)
1325
418k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1326
1.52M
   }
1327
1328
#ifdef ENABLE_QEXT
1329
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1330
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1331
   } else
1332
#endif
1333
4.94M
   {
1334
4.94M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1335
4.94M
   }
1336
1337
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1338
4.94M
   if (ctx->resynth)
1339
3.19M
   {
1340
      /* Undo the sample reorganization going from time order to frequency order */
1341
3.19M
      if (B0>1)
1342
719k
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1343
1344
      /* Undo time-freq changes that we did earlier */
1345
3.19M
      N_B = N_B0;
1346
3.19M
      B = B0;
1347
3.69M
      for (k=0;k<time_divide;k++)
1348
504k
      {
1349
504k
         B >>= 1;
1350
504k
         N_B <<= 1;
1351
504k
         cm |= cm>>B;
1352
504k
         haar1(X, N_B, B);
1353
504k
      }
1354
1355
3.84M
      for (k=0;k<recombine;k++)
1356
650k
      {
1357
650k
         static const unsigned char bit_deinterleave_table[16]={
1358
650k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1359
650k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1360
650k
         };
1361
650k
         cm = bit_deinterleave_table[cm];
1362
650k
         haar1(X, N0>>k, 1<<k);
1363
650k
      }
1364
3.19M
      B<<=recombine;
1365
1366
      /* Scale output for later folding */
1367
3.19M
      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.0M
         for (j=0;j<N0;j++)
1373
20.9M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1374
2.17M
      }
1375
3.19M
      cm &= (1<<B)-1;
1376
3.19M
   }
1377
4.94M
   return cm;
1378
5.79M
}
1379
1380
#ifdef FIXED_POINT
1381
3.91M
#define MIN_STEREO_ENERGY 2
1382
#else
1383
3.91M
#define MIN_STEREO_ENERGY 1e-10f
1384
#endif
1385
1386
/* This function is responsible for encoding and decoding a band for the stereo case. */
1387
static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm *Y,
1388
      int N, int b, int B, celt_norm *lowband,
1389
      int LM, celt_norm *lowband_out,
1390
      celt_norm *lowband_scratch, int fill
1391
      ARG_QEXT(int ext_b) ARG_QEXT(const int *cap))
1392
6.60M
{
1393
6.60M
   int imid=0, iside=0;
1394
6.60M
   int inv = 0;
1395
6.60M
   opus_val32 mid=0, side=0;
1396
6.60M
   unsigned cm=0;
1397
6.60M
   int mbits, sbits, delta;
1398
6.60M
   int itheta;
1399
6.60M
   int qalloc;
1400
6.60M
   struct split_ctx sctx;
1401
6.60M
   int orig_fill;
1402
6.60M
   int encode;
1403
6.60M
   ec_ctx *ec;
1404
1405
6.60M
   encode = ctx->encode;
1406
6.60M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
6.60M
   if (N==1)
1410
1.27M
   {
1411
1.27M
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
1.27M
   }
1413
1414
5.33M
   orig_fill = fill;
1415
1416
5.33M
   if (encode) {
1417
2.70M
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
321k
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
291k
         else OPUS_COPY(X, Y, N);
1420
321k
      }
1421
2.70M
   }
1422
5.33M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
5.33M
   inv = sctx.inv;
1424
5.33M
   imid = sctx.imid;
1425
5.33M
   iside = sctx.iside;
1426
5.33M
   delta = sctx.delta;
1427
5.33M
   itheta = sctx.itheta;
1428
5.33M
   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.33M
   mid = SHL32(EXTEND32(imid), 16);
1437
1.33M
   side = SHL32(EXTEND32(iside), 16);
1438
# endif
1439
#else
1440
# ifdef ENABLE_QEXT
1441
   (void)imid;
1442
   (void)iside;
1443
   mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1444
   side = celt_cos_norm2(1.f-sctx.itheta_q30*(1.f/(1<<30)));
1445
# else
1446
   mid = (1.f/32768)*imid;
1447
   side = (1.f/32768)*iside;
1448
# endif
1449
#endif
1450
1451
   /* This is a special case for N=2 that only works for stereo and takes
1452
      advantage of the fact that mid and side are orthogonal to encode
1453
      the side with just one bit. */
1454
5.33M
   if (N==2)
1455
1.27M
   {
1456
1.27M
      int c;
1457
1.27M
      int sign=0;
1458
1.27M
      celt_norm *x2, *y2;
1459
1.27M
      mbits = b;
1460
1.27M
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
1.27M
      if (itheta != 0 && itheta != 16384)
1463
309k
         sbits = 1<<BITRES;
1464
1.27M
      mbits -= sbits;
1465
1.27M
      c = itheta > 8192;
1466
1.27M
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
1.27M
      x2 = c ? Y : X;
1469
1.27M
      y2 = c ? X : Y;
1470
1.27M
      if (sbits)
1471
309k
      {
1472
309k
         if (encode)
1473
263k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
263k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
263k
            ec_enc_bits(ec, sign, 1);
1478
263k
         } else {
1479
45.3k
            sign = ec_dec_bits(ec, 1);
1480
45.3k
         }
1481
309k
      }
1482
1.27M
      sign = 1-2*sign;
1483
      /* We use orig_fill here because we want to fold the side, but if
1484
         itheta==16384, we'll have cleared the low bits of fill. */
1485
1.27M
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
1.27M
            lowband_scratch, orig_fill ARG_QEXT(ext_b));
1487
      /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse),
1488
         and there's no need to worry about mixing with the other channel. */
1489
1.27M
      y2[0] = -sign*x2[1];
1490
1.27M
      y2[1] = sign*x2[0];
1491
1.27M
      if (ctx->resynth)
1492
1.10M
      {
1493
1.10M
         celt_norm tmp;
1494
1.10M
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
1.10M
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
1.10M
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
1.10M
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
1.10M
         tmp = X[0];
1499
1.10M
         X[0] = SUB32(tmp,Y[0]);
1500
1.10M
         Y[0] = ADD32(tmp,Y[0]);
1501
1.10M
         tmp = X[1];
1502
1.10M
         X[1] = SUB32(tmp,Y[1]);
1503
1.10M
         Y[1] = ADD32(tmp,Y[1]);
1504
1.10M
      }
1505
4.05M
   } else {
1506
      /* "Normal" split code */
1507
4.05M
      opus_int32 rebalance;
1508
1509
4.05M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
4.05M
      sbits = b-mbits;
1511
4.05M
      ctx->remaining_bits -= qalloc;
1512
1513
4.05M
      rebalance = ctx->remaining_bits;
1514
4.05M
      if (mbits >= sbits)
1515
3.49M
      {
1516
#ifdef ENABLE_QEXT
1517
         int qext_extra = 0;
1518
         /* Reallocate any mid bits that cannot be used to extra mid bits. */
1519
1.76M
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, mbits - cap[ctx->i]/2));
1520
#endif
1521
         /* In stereo mode, we do not apply a scaling to the mid because we need the normalized
1522
            mid for folding later. */
1523
3.49M
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
3.49M
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
3.49M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
3.49M
         if (rebalance > 3<<BITRES && itheta!=0)
1527
61.3k
            sbits += rebalance - (3<<BITRES);
1528
#ifdef ENABLE_QEXT
1529
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the mid. */
1530
1.76M
         if (ctx->extra_bands) sbits = IMIN(sbits, ctx->remaining_bits);
1531
#endif
1532
         /* For a stereo split, the high bits of fill are always zero, so no
1533
            folding will be done to the side. */
1534
3.49M
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
3.49M
      } else {
1536
#ifdef ENABLE_QEXT
1537
         int qext_extra = 0;
1538
         /* Reallocate any side bits that cannot be used to extra side bits. */
1539
295k
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, sbits - cap[ctx->i]/2));
1540
#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
560k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
560k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
560k
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
36.1k
            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
295k
         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
560k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
560k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
560k
      }
1556
4.05M
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
5.33M
   if (ctx->resynth)
1561
4.40M
   {
1562
4.40M
      if (N!=2)
1563
3.30M
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
4.40M
      if (inv)
1565
116k
      {
1566
116k
         int j;
1567
1.68M
         for (j=0;j<N;j++)
1568
1.56M
            Y[j] = -Y[j];
1569
116k
      }
1570
4.40M
   }
1571
5.33M
   return cm;
1572
6.60M
}
bands.c:quant_band_stereo
Line
Count
Source
1392
1.62M
{
1393
1.62M
   int imid=0, iside=0;
1394
1.62M
   int inv = 0;
1395
1.62M
   opus_val32 mid=0, side=0;
1396
1.62M
   unsigned cm=0;
1397
1.62M
   int mbits, sbits, delta;
1398
1.62M
   int itheta;
1399
1.62M
   int qalloc;
1400
1.62M
   struct split_ctx sctx;
1401
1.62M
   int orig_fill;
1402
1.62M
   int encode;
1403
1.62M
   ec_ctx *ec;
1404
1405
1.62M
   encode = ctx->encode;
1406
1.62M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
1.62M
   if (N==1)
1410
295k
   {
1411
295k
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
295k
   }
1413
1414
1.33M
   orig_fill = fill;
1415
1416
1.33M
   if (encode) {
1417
651k
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
78.7k
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
70.8k
         else OPUS_COPY(X, Y, N);
1420
78.7k
      }
1421
651k
   }
1422
1.33M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
1.33M
   inv = sctx.inv;
1424
1.33M
   imid = sctx.imid;
1425
1.33M
   iside = sctx.iside;
1426
1.33M
   delta = sctx.delta;
1427
1.33M
   itheta = sctx.itheta;
1428
1.33M
   qalloc = sctx.qalloc;
1429
1.33M
#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.33M
   mid = SHL32(EXTEND32(imid), 16);
1437
1.33M
   side = SHL32(EXTEND32(iside), 16);
1438
1.33M
# 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.33M
   if (N==2)
1455
334k
   {
1456
334k
      int c;
1457
334k
      int sign=0;
1458
334k
      celt_norm *x2, *y2;
1459
334k
      mbits = b;
1460
334k
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
334k
      if (itheta != 0 && itheta != 16384)
1463
75.0k
         sbits = 1<<BITRES;
1464
334k
      mbits -= sbits;
1465
334k
      c = itheta > 8192;
1466
334k
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
334k
      x2 = c ? Y : X;
1469
334k
      y2 = c ? X : Y;
1470
334k
      if (sbits)
1471
75.0k
      {
1472
75.0k
         if (encode)
1473
65.7k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
65.7k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
65.7k
            ec_enc_bits(ec, sign, 1);
1478
65.7k
         } else {
1479
9.26k
            sign = ec_dec_bits(ec, 1);
1480
9.26k
         }
1481
75.0k
      }
1482
334k
      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
334k
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
334k
            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
334k
      y2[0] = -sign*x2[1];
1490
334k
      y2[1] = sign*x2[0];
1491
334k
      if (ctx->resynth)
1492
291k
      {
1493
291k
         celt_norm tmp;
1494
291k
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
291k
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
291k
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
291k
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
291k
         tmp = X[0];
1499
291k
         X[0] = SUB32(tmp,Y[0]);
1500
291k
         Y[0] = ADD32(tmp,Y[0]);
1501
291k
         tmp = X[1];
1502
291k
         X[1] = SUB32(tmp,Y[1]);
1503
291k
         Y[1] = ADD32(tmp,Y[1]);
1504
291k
      }
1505
998k
   } else {
1506
      /* "Normal" split code */
1507
998k
      opus_int32 rebalance;
1508
1509
998k
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
998k
      sbits = b-mbits;
1511
998k
      ctx->remaining_bits -= qalloc;
1512
1513
998k
      rebalance = ctx->remaining_bits;
1514
998k
      if (mbits >= sbits)
1515
865k
      {
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
865k
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
865k
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
865k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
865k
         if (rebalance > 3<<BITRES && itheta!=0)
1527
11.2k
            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
865k
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
865k
      } 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
132k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
132k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
132k
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
7.63k
            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
132k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
132k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
132k
      }
1556
998k
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
1.33M
   if (ctx->resynth)
1561
1.12M
   {
1562
1.12M
      if (N!=2)
1563
832k
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
1.12M
      if (inv)
1565
28.7k
      {
1566
28.7k
         int j;
1567
425k
         for (j=0;j<N;j++)
1568
397k
            Y[j] = -Y[j];
1569
28.7k
      }
1570
1.12M
   }
1571
1.33M
   return cm;
1572
1.62M
}
bands.c:quant_band_stereo
Line
Count
Source
1392
1.67M
{
1393
1.67M
   int imid=0, iside=0;
1394
1.67M
   int inv = 0;
1395
1.67M
   opus_val32 mid=0, side=0;
1396
1.67M
   unsigned cm=0;
1397
1.67M
   int mbits, sbits, delta;
1398
1.67M
   int itheta;
1399
1.67M
   int qalloc;
1400
1.67M
   struct split_ctx sctx;
1401
1.67M
   int orig_fill;
1402
1.67M
   int encode;
1403
1.67M
   ec_ctx *ec;
1404
1405
1.67M
   encode = ctx->encode;
1406
1.67M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
1.67M
   if (N==1)
1410
342k
   {
1411
342k
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
342k
   }
1413
1414
1.33M
   orig_fill = fill;
1415
1416
1.33M
   if (encode) {
1417
700k
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
82.1k
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
74.6k
         else OPUS_COPY(X, Y, N);
1420
82.1k
      }
1421
700k
   }
1422
1.33M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
1.33M
   inv = sctx.inv;
1424
1.33M
   imid = sctx.imid;
1425
1.33M
   iside = sctx.iside;
1426
1.33M
   delta = sctx.delta;
1427
1.33M
   itheta = sctx.itheta;
1428
1.33M
   qalloc = sctx.qalloc;
1429
1.33M
#ifdef FIXED_POINT
1430
1.33M
# ifdef ENABLE_QEXT
1431
1.33M
   (void)imid;
1432
1.33M
   (void)iside;
1433
1.33M
   mid = celt_cos_norm32(sctx.itheta_q30);
1434
1.33M
   side = celt_cos_norm32((1<<30)-sctx.itheta_q30);
1435
# else
1436
   mid = SHL32(EXTEND32(imid), 16);
1437
   side = SHL32(EXTEND32(iside), 16);
1438
# endif
1439
#else
1440
# ifdef ENABLE_QEXT
1441
   (void)imid;
1442
   (void)iside;
1443
   mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1444
   side = celt_cos_norm2(1.f-sctx.itheta_q30*(1.f/(1<<30)));
1445
# else
1446
   mid = (1.f/32768)*imid;
1447
   side = (1.f/32768)*iside;
1448
# endif
1449
#endif
1450
1451
   /* This is a special case for N=2 that only works for stereo and takes
1452
      advantage of the fact that mid and side are orthogonal to encode
1453
      the side with just one bit. */
1454
1.33M
   if (N==2)
1455
304k
   {
1456
304k
      int c;
1457
304k
      int sign=0;
1458
304k
      celt_norm *x2, *y2;
1459
304k
      mbits = b;
1460
304k
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
304k
      if (itheta != 0 && itheta != 16384)
1463
79.5k
         sbits = 1<<BITRES;
1464
304k
      mbits -= sbits;
1465
304k
      c = itheta > 8192;
1466
304k
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
304k
      x2 = c ? Y : X;
1469
304k
      y2 = c ? X : Y;
1470
304k
      if (sbits)
1471
79.5k
      {
1472
79.5k
         if (encode)
1473
66.1k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
66.1k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
66.1k
            ec_enc_bits(ec, sign, 1);
1478
66.1k
         } else {
1479
13.4k
            sign = ec_dec_bits(ec, 1);
1480
13.4k
         }
1481
79.5k
      }
1482
304k
      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
304k
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
304k
            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
304k
      y2[0] = -sign*x2[1];
1490
304k
      y2[1] = sign*x2[0];
1491
304k
      if (ctx->resynth)
1492
260k
      {
1493
260k
         celt_norm tmp;
1494
260k
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
260k
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
260k
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
260k
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
260k
         tmp = X[0];
1499
260k
         X[0] = SUB32(tmp,Y[0]);
1500
260k
         Y[0] = ADD32(tmp,Y[0]);
1501
260k
         tmp = X[1];
1502
260k
         X[1] = SUB32(tmp,Y[1]);
1503
260k
         Y[1] = ADD32(tmp,Y[1]);
1504
260k
      }
1505
1.02M
   } else {
1506
      /* "Normal" split code */
1507
1.02M
      opus_int32 rebalance;
1508
1509
1.02M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
1.02M
      sbits = b-mbits;
1511
1.02M
      ctx->remaining_bits -= qalloc;
1512
1513
1.02M
      rebalance = ctx->remaining_bits;
1514
1.02M
      if (mbits >= sbits)
1515
880k
      {
1516
880k
#ifdef ENABLE_QEXT
1517
880k
         int qext_extra = 0;
1518
         /* Reallocate any mid bits that cannot be used to extra mid bits. */
1519
880k
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, mbits - cap[ctx->i]/2));
1520
880k
#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
880k
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
880k
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
880k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
880k
         if (rebalance > 3<<BITRES && itheta!=0)
1527
19.4k
            sbits += rebalance - (3<<BITRES);
1528
880k
#ifdef ENABLE_QEXT
1529
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the mid. */
1530
880k
         if (ctx->extra_bands) sbits = IMIN(sbits, ctx->remaining_bits);
1531
880k
#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
880k
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
880k
      } else {
1536
147k
#ifdef ENABLE_QEXT
1537
147k
         int qext_extra = 0;
1538
         /* Reallocate any side bits that cannot be used to extra side bits. */
1539
147k
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, sbits - cap[ctx->i]/2));
1540
147k
#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
147k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
147k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
147k
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
10.4k
            mbits += rebalance - (3<<BITRES);
1547
147k
#ifdef ENABLE_QEXT
1548
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the side. */
1549
147k
         if (ctx->extra_bands) mbits = IMIN(mbits, ctx->remaining_bits);
1550
147k
#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
147k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
147k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
147k
      }
1556
1.02M
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
1.33M
   if (ctx->resynth)
1561
1.07M
   {
1562
1.07M
      if (N!=2)
1563
819k
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
1.07M
      if (inv)
1565
29.5k
      {
1566
29.5k
         int j;
1567
415k
         for (j=0;j<N;j++)
1568
386k
            Y[j] = -Y[j];
1569
29.5k
      }
1570
1.07M
   }
1571
1.33M
   return cm;
1572
1.67M
}
bands.c:quant_band_stereo
Line
Count
Source
1392
1.67M
{
1393
1.67M
   int imid=0, iside=0;
1394
1.67M
   int inv = 0;
1395
1.67M
   opus_val32 mid=0, side=0;
1396
1.67M
   unsigned cm=0;
1397
1.67M
   int mbits, sbits, delta;
1398
1.67M
   int itheta;
1399
1.67M
   int qalloc;
1400
1.67M
   struct split_ctx sctx;
1401
1.67M
   int orig_fill;
1402
1.67M
   int encode;
1403
1.67M
   ec_ctx *ec;
1404
1405
1.67M
   encode = ctx->encode;
1406
1.67M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
1.67M
   if (N==1)
1410
342k
   {
1411
342k
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
342k
   }
1413
1414
1.33M
   orig_fill = fill;
1415
1416
1.33M
   if (encode) {
1417
700k
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
82.1k
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
74.6k
         else OPUS_COPY(X, Y, N);
1420
82.1k
      }
1421
700k
   }
1422
1.33M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
1.33M
   inv = sctx.inv;
1424
1.33M
   imid = sctx.imid;
1425
1.33M
   iside = sctx.iside;
1426
1.33M
   delta = sctx.delta;
1427
1.33M
   itheta = sctx.itheta;
1428
1.33M
   qalloc = sctx.qalloc;
1429
#ifdef FIXED_POINT
1430
# ifdef ENABLE_QEXT
1431
   (void)imid;
1432
   (void)iside;
1433
   mid = celt_cos_norm32(sctx.itheta_q30);
1434
   side = celt_cos_norm32((1<<30)-sctx.itheta_q30);
1435
# else
1436
   mid = SHL32(EXTEND32(imid), 16);
1437
   side = SHL32(EXTEND32(iside), 16);
1438
# endif
1439
#else
1440
1.33M
# ifdef ENABLE_QEXT
1441
1.33M
   (void)imid;
1442
1.33M
   (void)iside;
1443
1.33M
   mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1444
1.33M
   side = celt_cos_norm2(1.f-sctx.itheta_q30*(1.f/(1<<30)));
1445
# else
1446
   mid = (1.f/32768)*imid;
1447
   side = (1.f/32768)*iside;
1448
# endif
1449
1.33M
#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.33M
   if (N==2)
1455
304k
   {
1456
304k
      int c;
1457
304k
      int sign=0;
1458
304k
      celt_norm *x2, *y2;
1459
304k
      mbits = b;
1460
304k
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
304k
      if (itheta != 0 && itheta != 16384)
1463
79.5k
         sbits = 1<<BITRES;
1464
304k
      mbits -= sbits;
1465
304k
      c = itheta > 8192;
1466
304k
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
304k
      x2 = c ? Y : X;
1469
304k
      y2 = c ? X : Y;
1470
304k
      if (sbits)
1471
79.5k
      {
1472
79.5k
         if (encode)
1473
66.1k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
66.1k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
66.1k
            ec_enc_bits(ec, sign, 1);
1478
66.1k
         } else {
1479
13.4k
            sign = ec_dec_bits(ec, 1);
1480
13.4k
         }
1481
79.5k
      }
1482
304k
      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
304k
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
304k
            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
304k
      y2[0] = -sign*x2[1];
1490
304k
      y2[1] = sign*x2[0];
1491
304k
      if (ctx->resynth)
1492
260k
      {
1493
260k
         celt_norm tmp;
1494
260k
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
260k
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
260k
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
260k
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
260k
         tmp = X[0];
1499
260k
         X[0] = SUB32(tmp,Y[0]);
1500
260k
         Y[0] = ADD32(tmp,Y[0]);
1501
260k
         tmp = X[1];
1502
260k
         X[1] = SUB32(tmp,Y[1]);
1503
260k
         Y[1] = ADD32(tmp,Y[1]);
1504
260k
      }
1505
1.02M
   } else {
1506
      /* "Normal" split code */
1507
1.02M
      opus_int32 rebalance;
1508
1509
1.02M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
1.02M
      sbits = b-mbits;
1511
1.02M
      ctx->remaining_bits -= qalloc;
1512
1513
1.02M
      rebalance = ctx->remaining_bits;
1514
1.02M
      if (mbits >= sbits)
1515
880k
      {
1516
880k
#ifdef ENABLE_QEXT
1517
880k
         int qext_extra = 0;
1518
         /* Reallocate any mid bits that cannot be used to extra mid bits. */
1519
880k
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, mbits - cap[ctx->i]/2));
1520
880k
#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
880k
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
880k
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
880k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
880k
         if (rebalance > 3<<BITRES && itheta!=0)
1527
19.4k
            sbits += rebalance - (3<<BITRES);
1528
880k
#ifdef ENABLE_QEXT
1529
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the mid. */
1530
880k
         if (ctx->extra_bands) sbits = IMIN(sbits, ctx->remaining_bits);
1531
880k
#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
880k
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
880k
      } else {
1536
147k
#ifdef ENABLE_QEXT
1537
147k
         int qext_extra = 0;
1538
         /* Reallocate any side bits that cannot be used to extra side bits. */
1539
147k
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, sbits - cap[ctx->i]/2));
1540
147k
#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
147k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
147k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
147k
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
10.4k
            mbits += rebalance - (3<<BITRES);
1547
147k
#ifdef ENABLE_QEXT
1548
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the side. */
1549
147k
         if (ctx->extra_bands) mbits = IMIN(mbits, ctx->remaining_bits);
1550
147k
#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
147k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
147k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
147k
      }
1556
1.02M
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
1.33M
   if (ctx->resynth)
1561
1.07M
   {
1562
1.07M
      if (N!=2)
1563
819k
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
1.07M
      if (inv)
1565
29.5k
      {
1566
29.5k
         int j;
1567
415k
         for (j=0;j<N;j++)
1568
386k
            Y[j] = -Y[j];
1569
29.5k
      }
1570
1.07M
   }
1571
1.33M
   return cm;
1572
1.67M
}
bands.c:quant_band_stereo
Line
Count
Source
1392
1.62M
{
1393
1.62M
   int imid=0, iside=0;
1394
1.62M
   int inv = 0;
1395
1.62M
   opus_val32 mid=0, side=0;
1396
1.62M
   unsigned cm=0;
1397
1.62M
   int mbits, sbits, delta;
1398
1.62M
   int itheta;
1399
1.62M
   int qalloc;
1400
1.62M
   struct split_ctx sctx;
1401
1.62M
   int orig_fill;
1402
1.62M
   int encode;
1403
1.62M
   ec_ctx *ec;
1404
1405
1.62M
   encode = ctx->encode;
1406
1.62M
   ec = ctx->ec;
1407
1408
   /* Special case for one sample */
1409
1.62M
   if (N==1)
1410
295k
   {
1411
295k
      return quant_band_n1(ctx, X, Y, lowband_out);
1412
295k
   }
1413
1414
1.33M
   orig_fill = fill;
1415
1416
1.33M
   if (encode) {
1417
651k
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1418
78.7k
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1419
70.8k
         else OPUS_COPY(X, Y, N);
1420
78.7k
      }
1421
651k
   }
1422
1.33M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1423
1.33M
   inv = sctx.inv;
1424
1.33M
   imid = sctx.imid;
1425
1.33M
   iside = sctx.iside;
1426
1.33M
   delta = sctx.delta;
1427
1.33M
   itheta = sctx.itheta;
1428
1.33M
   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.33M
   mid = (1.f/32768)*imid;
1447
1.33M
   side = (1.f/32768)*iside;
1448
1.33M
# endif
1449
1.33M
#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.33M
   if (N==2)
1455
334k
   {
1456
334k
      int c;
1457
334k
      int sign=0;
1458
334k
      celt_norm *x2, *y2;
1459
334k
      mbits = b;
1460
334k
      sbits = 0;
1461
      /* Only need one bit for the side. */
1462
334k
      if (itheta != 0 && itheta != 16384)
1463
75.0k
         sbits = 1<<BITRES;
1464
334k
      mbits -= sbits;
1465
334k
      c = itheta > 8192;
1466
334k
      ctx->remaining_bits -= qalloc+sbits;
1467
1468
334k
      x2 = c ? Y : X;
1469
334k
      y2 = c ? X : Y;
1470
334k
      if (sbits)
1471
75.0k
      {
1472
75.0k
         if (encode)
1473
65.7k
         {
1474
            /* Here we only need to encode a sign for the side. */
1475
            /* FIXME: Need to increase fixed-point precision? */
1476
65.7k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1477
65.7k
            ec_enc_bits(ec, sign, 1);
1478
65.7k
         } else {
1479
9.26k
            sign = ec_dec_bits(ec, 1);
1480
9.26k
         }
1481
75.0k
      }
1482
334k
      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
334k
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1486
334k
            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
334k
      y2[0] = -sign*x2[1];
1490
334k
      y2[1] = sign*x2[0];
1491
334k
      if (ctx->resynth)
1492
291k
      {
1493
291k
         celt_norm tmp;
1494
291k
         X[0] = MULT32_32_Q31(mid, X[0]);
1495
291k
         X[1] = MULT32_32_Q31(mid, X[1]);
1496
291k
         Y[0] = MULT32_32_Q31(side, Y[0]);
1497
291k
         Y[1] = MULT32_32_Q31(side, Y[1]);
1498
291k
         tmp = X[0];
1499
291k
         X[0] = SUB32(tmp,Y[0]);
1500
291k
         Y[0] = ADD32(tmp,Y[0]);
1501
291k
         tmp = X[1];
1502
291k
         X[1] = SUB32(tmp,Y[1]);
1503
291k
         Y[1] = ADD32(tmp,Y[1]);
1504
291k
      }
1505
998k
   } else {
1506
      /* "Normal" split code */
1507
998k
      opus_int32 rebalance;
1508
1509
998k
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1510
998k
      sbits = b-mbits;
1511
998k
      ctx->remaining_bits -= qalloc;
1512
1513
998k
      rebalance = ctx->remaining_bits;
1514
998k
      if (mbits >= sbits)
1515
865k
      {
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
865k
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1524
865k
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1525
865k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1526
865k
         if (rebalance > 3<<BITRES && itheta!=0)
1527
11.2k
            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
865k
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1535
865k
      } 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
132k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1544
132k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1545
132k
         if (rebalance > 3<<BITRES && itheta!=16384)
1546
7.63k
            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
132k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1554
132k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1555
132k
      }
1556
998k
   }
1557
1558
1559
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1560
1.33M
   if (ctx->resynth)
1561
1.12M
   {
1562
1.12M
      if (N!=2)
1563
832k
         stereo_merge(X, Y, mid, N, ctx->arch);
1564
1.12M
      if (inv)
1565
28.7k
      {
1566
28.7k
         int j;
1567
425k
         for (j=0;j<N;j++)
1568
397k
            Y[j] = -Y[j];
1569
28.7k
      }
1570
1.12M
   }
1571
1.33M
   return cm;
1572
1.62M
}
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
686k
{
1577
686k
   int n1, n2;
1578
686k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1579
686k
   n1 = M*(eBands[start+1]-eBands[start]);
1580
686k
   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
686k
   OPUS_COPY(&norm[n1], &norm[2*n1 - n2], n2-n1);
1584
686k
   if (dual_stereo)
1585
38.5k
      OPUS_COPY(&norm2[n1], &norm2[2*n1 - n2], n2-n1);
1586
686k
}
1587
#endif
1588
1589
void quant_all_bands(int encode, const CELTMode *m, int start, int end,
1590
      celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks,
1591
      const celt_ener *bandE, int *pulses, int shortBlocks, int spread,
1592
      int dual_stereo, int intensity, int *tf_res, opus_int32 total_bits,
1593
      opus_int32 balance, ec_ctx *ec, int LM, int codedBands,
1594
      opus_uint32 *seed, int complexity, int arch, int disable_inv
1595
      ARG_QEXT(ec_ctx *ext_ec) ARG_QEXT(int *extra_pulses)
1596
      ARG_QEXT(opus_int32 ext_total_bits) ARG_QEXT(const int *cap))
1597
1.29M
{
1598
1.29M
   int i;
1599
1.29M
   opus_int32 remaining_bits;
1600
1.29M
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
1.29M
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
1.29M
   VARDECL(celt_norm, _norm);
1603
1.29M
   VARDECL(celt_norm, _lowband_scratch);
1604
1.29M
   VARDECL(celt_norm, X_save);
1605
1.29M
   VARDECL(celt_norm, Y_save);
1606
1.29M
   VARDECL(celt_norm, X_save2);
1607
1.29M
   VARDECL(celt_norm, Y_save2);
1608
1.29M
   VARDECL(celt_norm, norm_save2);
1609
1.29M
   int resynth_alloc;
1610
1.29M
   celt_norm *lowband_scratch;
1611
1.29M
   int B;
1612
1.29M
   int M;
1613
1.29M
   int lowband_offset;
1614
1.29M
   int update_lowband = 1;
1615
1.29M
   int C = Y_ != NULL ? 2 : 1;
1616
1.29M
   int norm_offset;
1617
1.29M
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1618
#ifdef RESYNTH
1619
   int resynth = 1;
1620
#else
1621
1.29M
   int resynth = !encode || theta_rdo;
1622
1.29M
#endif
1623
1.29M
   struct band_ctx ctx;
1624
#ifdef ENABLE_QEXT
1625
   int ext_b;
1626
   opus_int32 ext_balance=0;
1627
   opus_int32 ext_tell=0;
1628
#endif
1629
1.29M
   SAVE_STACK;
1630
1631
1.29M
   M = 1<<LM;
1632
1.29M
   B = shortBlocks ? M : 1;
1633
1.29M
   norm_offset = M*eBands[start];
1634
   /* No need to allocate norm for the last band because we don't need an
1635
      output in that band. */
1636
1.29M
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1637
1.29M
   norm = _norm;
1638
1.29M
   norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset;
1639
1640
   /* For decoding, we can use the last band as scratch space because we don't need that
1641
      scratch space for the last band and we don't care about the data there until we're
1642
      decoding the last band. */
1643
1.29M
   if (encode && resynth)
1644
101k
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1645
1.19M
   else
1646
1.19M
      resynth_alloc = ALLOC_NONE;
1647
1.29M
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1648
1.29M
   if (encode && resynth)
1649
101k
      lowband_scratch = _lowband_scratch;
1650
1.19M
   else
1651
1.19M
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1652
1.29M
   ALLOC(X_save, resynth_alloc, celt_norm);
1653
1.29M
   ALLOC(Y_save, resynth_alloc, celt_norm);
1654
1.29M
   ALLOC(X_save2, resynth_alloc, celt_norm);
1655
1.29M
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1656
1.29M
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1657
1658
1.29M
   lowband_offset = 0;
1659
1.29M
   ctx.bandE = bandE;
1660
1.29M
   ctx.ec = ec;
1661
1.29M
   ctx.encode = encode;
1662
1.29M
   ctx.intensity = intensity;
1663
1.29M
   ctx.m = m;
1664
1.29M
   ctx.seed = *seed;
1665
1.29M
   ctx.spread = spread;
1666
1.29M
   ctx.arch = arch;
1667
1.29M
   ctx.disable_inv = disable_inv;
1668
1.29M
   ctx.resynth = resynth;
1669
1.29M
   ctx.theta_round = 0;
1670
#ifdef ENABLE_QEXT
1671
   ctx.ext_ec = ext_ec;
1672
   ctx.ext_total_bits = ext_total_bits;
1673
664k
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1674
664k
   if (ctx.extra_bands) theta_rdo = 0;
1675
#endif
1676
   /* Avoid injecting noise in the first band on transients. */
1677
1.29M
   ctx.avoid_split_noise = B > 1;
1678
20.5M
   for (i=start;i<end;i++)
1679
19.2M
   {
1680
19.2M
      opus_int32 tell;
1681
19.2M
      int b;
1682
19.2M
      int N;
1683
19.2M
      opus_int32 curr_balance;
1684
19.2M
      int effective_lowband=-1;
1685
19.2M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1686
19.2M
      int tf_change=0;
1687
19.2M
      unsigned x_cm;
1688
19.2M
      unsigned y_cm;
1689
19.2M
      int last;
1690
1691
19.2M
      ctx.i = i;
1692
19.2M
      last = (i==end-1);
1693
1694
19.2M
      X = X_+M*eBands[i];
1695
19.2M
      if (Y_!=NULL)
1696
6.45M
         Y = Y_+M*eBands[i];
1697
12.7M
      else
1698
12.7M
         Y = NULL;
1699
19.2M
      N = M*eBands[i+1]-M*eBands[i];
1700
19.2M
      celt_assert(N > 0);
1701
19.2M
      tell = ec_tell_frac(ec);
1702
1703
      /* Compute how many bits we want to allocate to this band */
1704
19.2M
      if (i != start)
1705
17.9M
         balance -= tell;
1706
19.2M
      remaining_bits = total_bits-tell-1;
1707
19.2M
      ctx.remaining_bits = remaining_bits;
1708
#ifdef ENABLE_QEXT
1709
9.83M
      if (i != start) {
1710
9.17M
         ext_balance += extra_pulses[i-1] + ext_tell;
1711
9.17M
      }
1712
      ext_tell = ec_tell_frac(ext_ec);
1713
      ctx.extra_bits = extra_pulses[i];
1714
9.83M
      if (i != start)
1715
9.17M
         ext_balance -= ext_tell;
1716
9.83M
      if (i <= codedBands-1)
1717
5.14M
      {
1718
5.14M
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1719
5.14M
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1720
5.14M
      } else {
1721
4.69M
         ext_b = 0;
1722
4.69M
      }
1723
#endif
1724
19.2M
      if (i <= codedBands-1)
1725
9.62M
      {
1726
9.62M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1727
9.62M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1728
9.62M
      } else {
1729
9.58M
         b = 0;
1730
9.58M
      }
1731
1732
19.2M
#ifndef DISABLE_UPDATE_DRAFT
1733
19.2M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1734
3.26M
            lowband_offset = i;
1735
19.2M
      if (i == start+1)
1736
1.29M
         special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1737
#else
1738
      if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0))
1739
            lowband_offset = i;
1740
#endif
1741
1742
19.2M
      tf_change = tf_res[i];
1743
19.2M
      ctx.tf_change = tf_change;
1744
19.2M
      if (i>=m->effEBands)
1745
40.7k
      {
1746
40.7k
         X=norm;
1747
40.7k
         if (Y_!=NULL)
1748
30.7k
            Y = norm;
1749
40.7k
         lowband_scratch = NULL;
1750
40.7k
      }
1751
19.2M
      if (last && !theta_rdo)
1752
1.19M
         lowband_scratch = NULL;
1753
1754
      /* Get a conservative estimate of the collapse_mask's for the bands we're
1755
         going to be folding from. */
1756
19.2M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1757
10.3M
      {
1758
10.3M
         int fold_start;
1759
10.3M
         int fold_end;
1760
10.3M
         int fold_i;
1761
         /* This ensures we never repeat spectral content within one band */
1762
10.3M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1763
10.3M
         fold_start = lowband_offset;
1764
11.8M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1765
10.3M
         fold_end = lowband_offset-1;
1766
10.3M
#ifndef DISABLE_UPDATE_DRAFT
1767
25.9M
         while(++fold_end < i && M*eBands[fold_end] < effective_lowband+norm_offset+N);
1768
#else
1769
         while(M*eBands[++fold_end] < effective_lowband+norm_offset+N);
1770
#endif
1771
10.3M
         x_cm = y_cm = 0;
1772
27.3M
         fold_i = fold_start; do {
1773
27.3M
           x_cm |= collapse_masks[fold_i*C+0];
1774
27.3M
           y_cm |= collapse_masks[fold_i*C+C-1];
1775
27.3M
         } while (++fold_i<fold_end);
1776
10.3M
      }
1777
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1778
         always) be non-zero. */
1779
8.81M
      else
1780
8.81M
         x_cm = y_cm = (1<<B)-1;
1781
1782
19.2M
      if (dual_stereo && i==intensity)
1783
65.1k
      {
1784
65.1k
         int j;
1785
1786
         /* Switch off dual stereo to do intensity. */
1787
65.1k
         dual_stereo = 0;
1788
65.1k
         if (resynth)
1789
1.17M
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1790
1.14M
               norm[j] = HALF32(norm[j]+norm2[j]);
1791
65.1k
      }
1792
19.2M
      if (dual_stereo)
1793
764k
      {
1794
764k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1795
764k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1796
764k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1797
764k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1798
764k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1799
764k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1800
18.4M
      } else {
1801
18.4M
         if (Y!=NULL)
1802
5.68M
         {
1803
5.68M
            if (theta_rdo && i < intensity)
1804
919k
            {
1805
919k
               ec_ctx ec_save, ec_save2;
1806
919k
               struct band_ctx ctx_save, ctx_save2;
1807
919k
               opus_val32 dist0, dist1;
1808
919k
               unsigned cm, cm2;
1809
919k
               int nstart_bytes, nend_bytes, save_bytes;
1810
919k
               unsigned char *bytes_buf;
1811
919k
               unsigned char bytes_save[1275];
1812
#ifdef ENABLE_QEXT
1813
               ec_ctx ext_ec_save, ext_ec_save2;
1814
               unsigned char *ext_bytes_buf;
1815
               int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes;
1816
               unsigned char ext_bytes_save[QEXT_PACKET_SIZE_CAP];
1817
#endif
1818
919k
               opus_val16 w[2];
1819
919k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1820
               /* Make a copy. */
1821
919k
               cm = x_cm|y_cm;
1822
919k
               ec_save = *ec;
1823
#ifdef ENABLE_QEXT
1824
               ext_ec_save = *ext_ec;
1825
#endif
1826
919k
               ctx_save = ctx;
1827
919k
               OPUS_COPY(X_save, X, N);
1828
919k
               OPUS_COPY(Y_save, Y, N);
1829
               /* Encode and round down. */
1830
919k
               ctx.theta_round = -1;
1831
919k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1832
919k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1833
919k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1834
919k
               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));
1835
1836
               /* Save first result. */
1837
919k
               cm2 = x_cm;
1838
919k
               ec_save2 = *ec;
1839
#ifdef ENABLE_QEXT
1840
               ext_ec_save2 = *ext_ec;
1841
#endif
1842
919k
               ctx_save2 = ctx;
1843
919k
               OPUS_COPY(X_save2, X, N);
1844
919k
               OPUS_COPY(Y_save2, Y, N);
1845
919k
               if (!last)
1846
908k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1847
919k
               nstart_bytes = ec_save.offs;
1848
919k
               nend_bytes = ec_save.storage;
1849
919k
               bytes_buf = ec_save.buf+nstart_bytes;
1850
919k
               save_bytes = nend_bytes-nstart_bytes;
1851
919k
               OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1852
#ifdef ENABLE_QEXT
1853
               ext_nstart_bytes = ext_ec_save.offs;
1854
               ext_nend_bytes = ext_ec_save.storage;
1855
459k
               ext_bytes_buf = ext_ec_save.buf!=NULL ? ext_ec_save.buf+ext_nstart_bytes : NULL;
1856
               ext_save_bytes = ext_nend_bytes-ext_nstart_bytes;
1857
459k
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1858
#endif
1859
               /* Restore */
1860
919k
               *ec = ec_save;
1861
#ifdef ENABLE_QEXT
1862
               *ext_ec = ext_ec_save;
1863
#endif
1864
919k
               ctx = ctx_save;
1865
919k
               OPUS_COPY(X, X_save, N);
1866
919k
               OPUS_COPY(Y, Y_save, N);
1867
919k
#ifndef DISABLE_UPDATE_DRAFT
1868
919k
               if (i == start+1)
1869
77.6k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1870
919k
#endif
1871
               /* Encode and round up. */
1872
919k
               ctx.theta_round = 1;
1873
919k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1874
919k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1875
919k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1876
919k
               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));
1877
919k
               if (dist0 >= dist1) {
1878
630k
                  x_cm = cm2;
1879
630k
                  *ec = ec_save2;
1880
#ifdef ENABLE_QEXT
1881
                  *ext_ec = ext_ec_save2;
1882
#endif
1883
630k
                  ctx = ctx_save2;
1884
630k
                  OPUS_COPY(X, X_save2, N);
1885
630k
                  OPUS_COPY(Y, Y_save2, N);
1886
630k
                  if (!last)
1887
624k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1888
630k
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1889
#ifdef ENABLE_QEXT
1890
311k
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1891
#endif
1892
630k
               }
1893
4.76M
            } else {
1894
4.76M
               ctx.theta_round = 0;
1895
4.76M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1896
4.76M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1897
4.76M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1898
4.76M
            }
1899
12.7M
         } else {
1900
12.7M
            x_cm = quant_band(&ctx, X, N, b, B,
1901
12.7M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1902
12.7M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1903
12.7M
         }
1904
18.4M
         y_cm = x_cm;
1905
18.4M
      }
1906
19.2M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1907
19.2M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1908
19.2M
      balance += pulses[i] + tell;
1909
1910
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1911
19.2M
      update_lowband = b>(N<<BITRES);
1912
      /* We only need to avoid noise on a split for the first band. After that, we
1913
         have folding. */
1914
19.2M
      ctx.avoid_split_noise = 0;
1915
19.2M
   }
1916
1.29M
   *seed = ctx.seed;
1917
1918
1.29M
   RESTORE_STACK;
1919
1.29M
}
quant_all_bands
Line
Count
Source
1597
315k
{
1598
315k
   int i;
1599
315k
   opus_int32 remaining_bits;
1600
315k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
315k
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
315k
   VARDECL(celt_norm, _norm);
1603
315k
   VARDECL(celt_norm, _lowband_scratch);
1604
315k
   VARDECL(celt_norm, X_save);
1605
315k
   VARDECL(celt_norm, Y_save);
1606
315k
   VARDECL(celt_norm, X_save2);
1607
315k
   VARDECL(celt_norm, Y_save2);
1608
315k
   VARDECL(celt_norm, norm_save2);
1609
315k
   int resynth_alloc;
1610
315k
   celt_norm *lowband_scratch;
1611
315k
   int B;
1612
315k
   int M;
1613
315k
   int lowband_offset;
1614
315k
   int update_lowband = 1;
1615
315k
   int C = Y_ != NULL ? 2 : 1;
1616
315k
   int norm_offset;
1617
315k
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1618
#ifdef RESYNTH
1619
   int resynth = 1;
1620
#else
1621
315k
   int resynth = !encode || theta_rdo;
1622
315k
#endif
1623
315k
   struct band_ctx ctx;
1624
#ifdef ENABLE_QEXT
1625
   int ext_b;
1626
   opus_int32 ext_balance=0;
1627
   opus_int32 ext_tell=0;
1628
#endif
1629
315k
   SAVE_STACK;
1630
1631
315k
   M = 1<<LM;
1632
315k
   B = shortBlocks ? M : 1;
1633
315k
   norm_offset = M*eBands[start];
1634
   /* No need to allocate norm for the last band because we don't need an
1635
      output in that band. */
1636
315k
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1637
315k
   norm = _norm;
1638
315k
   norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset;
1639
1640
   /* For decoding, we can use the last band as scratch space because we don't need that
1641
      scratch space for the last band and we don't care about the data there until we're
1642
      decoding the last band. */
1643
315k
   if (encode && resynth)
1644
25.5k
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1645
289k
   else
1646
289k
      resynth_alloc = ALLOC_NONE;
1647
315k
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1648
315k
   if (encode && resynth)
1649
25.5k
      lowband_scratch = _lowband_scratch;
1650
289k
   else
1651
289k
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1652
315k
   ALLOC(X_save, resynth_alloc, celt_norm);
1653
315k
   ALLOC(Y_save, resynth_alloc, celt_norm);
1654
315k
   ALLOC(X_save2, resynth_alloc, celt_norm);
1655
315k
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1656
315k
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1657
1658
315k
   lowband_offset = 0;
1659
315k
   ctx.bandE = bandE;
1660
315k
   ctx.ec = ec;
1661
315k
   ctx.encode = encode;
1662
315k
   ctx.intensity = intensity;
1663
315k
   ctx.m = m;
1664
315k
   ctx.seed = *seed;
1665
315k
   ctx.spread = spread;
1666
315k
   ctx.arch = arch;
1667
315k
   ctx.disable_inv = disable_inv;
1668
315k
   ctx.resynth = resynth;
1669
315k
   ctx.theta_round = 0;
1670
#ifdef ENABLE_QEXT
1671
   ctx.ext_ec = ext_ec;
1672
   ctx.ext_total_bits = ext_total_bits;
1673
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1674
   if (ctx.extra_bands) theta_rdo = 0;
1675
#endif
1676
   /* Avoid injecting noise in the first band on transients. */
1677
315k
   ctx.avoid_split_noise = B > 1;
1678
5.00M
   for (i=start;i<end;i++)
1679
4.68M
   {
1680
4.68M
      opus_int32 tell;
1681
4.68M
      int b;
1682
4.68M
      int N;
1683
4.68M
      opus_int32 curr_balance;
1684
4.68M
      int effective_lowband=-1;
1685
4.68M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1686
4.68M
      int tf_change=0;
1687
4.68M
      unsigned x_cm;
1688
4.68M
      unsigned y_cm;
1689
4.68M
      int last;
1690
1691
4.68M
      ctx.i = i;
1692
4.68M
      last = (i==end-1);
1693
1694
4.68M
      X = X_+M*eBands[i];
1695
4.68M
      if (Y_!=NULL)
1696
1.57M
         Y = Y_+M*eBands[i];
1697
3.10M
      else
1698
3.10M
         Y = NULL;
1699
4.68M
      N = M*eBands[i+1]-M*eBands[i];
1700
4.68M
      celt_assert(N > 0);
1701
4.68M
      tell = ec_tell_frac(ec);
1702
1703
      /* Compute how many bits we want to allocate to this band */
1704
4.68M
      if (i != start)
1705
4.36M
         balance -= tell;
1706
4.68M
      remaining_bits = total_bits-tell-1;
1707
4.68M
      ctx.remaining_bits = remaining_bits;
1708
#ifdef ENABLE_QEXT
1709
      if (i != start) {
1710
         ext_balance += extra_pulses[i-1] + ext_tell;
1711
      }
1712
      ext_tell = ec_tell_frac(ext_ec);
1713
      ctx.extra_bits = extra_pulses[i];
1714
      if (i != start)
1715
         ext_balance -= ext_tell;
1716
      if (i <= codedBands-1)
1717
      {
1718
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1719
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1720
      } else {
1721
         ext_b = 0;
1722
      }
1723
#endif
1724
4.68M
      if (i <= codedBands-1)
1725
2.23M
      {
1726
2.23M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1727
2.23M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1728
2.44M
      } else {
1729
2.44M
         b = 0;
1730
2.44M
      }
1731
1732
4.68M
#ifndef DISABLE_UPDATE_DRAFT
1733
4.68M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1734
740k
            lowband_offset = i;
1735
4.68M
      if (i == start+1)
1736
315k
         special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1737
#else
1738
      if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0))
1739
            lowband_offset = i;
1740
#endif
1741
1742
4.68M
      tf_change = tf_res[i];
1743
4.68M
      ctx.tf_change = tf_change;
1744
4.68M
      if (i>=m->effEBands)
1745
0
      {
1746
0
         X=norm;
1747
0
         if (Y_!=NULL)
1748
0
            Y = norm;
1749
0
         lowband_scratch = NULL;
1750
0
      }
1751
4.68M
      if (last && !theta_rdo)
1752
289k
         lowband_scratch = NULL;
1753
1754
      /* Get a conservative estimate of the collapse_mask's for the bands we're
1755
         going to be folding from. */
1756
4.68M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1757
2.57M
      {
1758
2.57M
         int fold_start;
1759
2.57M
         int fold_end;
1760
2.57M
         int fold_i;
1761
         /* This ensures we never repeat spectral content within one band */
1762
2.57M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1763
2.57M
         fold_start = lowband_offset;
1764
2.87M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1765
2.57M
         fold_end = lowband_offset-1;
1766
2.57M
#ifndef DISABLE_UPDATE_DRAFT
1767
6.57M
         while(++fold_end < i && M*eBands[fold_end] < effective_lowband+norm_offset+N);
1768
#else
1769
         while(M*eBands[++fold_end] < effective_lowband+norm_offset+N);
1770
#endif
1771
2.57M
         x_cm = y_cm = 0;
1772
6.87M
         fold_i = fold_start; do {
1773
6.87M
           x_cm |= collapse_masks[fold_i*C+0];
1774
6.87M
           y_cm |= collapse_masks[fold_i*C+C-1];
1775
6.87M
         } while (++fold_i<fold_end);
1776
2.57M
      }
1777
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1778
         always) be non-zero. */
1779
2.11M
      else
1780
2.11M
         x_cm = y_cm = (1<<B)-1;
1781
1782
4.68M
      if (dual_stereo && i==intensity)
1783
15.3k
      {
1784
15.3k
         int j;
1785
1786
         /* Switch off dual stereo to do intensity. */
1787
15.3k
         dual_stereo = 0;
1788
15.3k
         if (resynth)
1789
221k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1790
215k
               norm[j] = HALF32(norm[j]+norm2[j]);
1791
15.3k
      }
1792
4.68M
      if (dual_stereo)
1793
181k
      {
1794
181k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1795
181k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1796
181k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1797
181k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1798
181k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1799
181k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1800
4.50M
      } else {
1801
4.50M
         if (Y!=NULL)
1802
1.39M
         {
1803
1.39M
            if (theta_rdo && i < intensity)
1804
229k
            {
1805
229k
               ec_ctx ec_save, ec_save2;
1806
229k
               struct band_ctx ctx_save, ctx_save2;
1807
229k
               opus_val32 dist0, dist1;
1808
229k
               unsigned cm, cm2;
1809
229k
               int nstart_bytes, nend_bytes, save_bytes;
1810
229k
               unsigned char *bytes_buf;
1811
229k
               unsigned char bytes_save[1275];
1812
#ifdef ENABLE_QEXT
1813
               ec_ctx ext_ec_save, ext_ec_save2;
1814
               unsigned char *ext_bytes_buf;
1815
               int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes;
1816
               unsigned char ext_bytes_save[QEXT_PACKET_SIZE_CAP];
1817
#endif
1818
229k
               opus_val16 w[2];
1819
229k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1820
               /* Make a copy. */
1821
229k
               cm = x_cm|y_cm;
1822
229k
               ec_save = *ec;
1823
#ifdef ENABLE_QEXT
1824
               ext_ec_save = *ext_ec;
1825
#endif
1826
229k
               ctx_save = ctx;
1827
229k
               OPUS_COPY(X_save, X, N);
1828
229k
               OPUS_COPY(Y_save, Y, N);
1829
               /* Encode and round down. */
1830
229k
               ctx.theta_round = -1;
1831
229k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1832
229k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1833
229k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1834
229k
               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));
1835
1836
               /* Save first result. */
1837
229k
               cm2 = x_cm;
1838
229k
               ec_save2 = *ec;
1839
#ifdef ENABLE_QEXT
1840
               ext_ec_save2 = *ext_ec;
1841
#endif
1842
229k
               ctx_save2 = ctx;
1843
229k
               OPUS_COPY(X_save2, X, N);
1844
229k
               OPUS_COPY(Y_save2, Y, N);
1845
229k
               if (!last)
1846
227k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1847
229k
               nstart_bytes = ec_save.offs;
1848
229k
               nend_bytes = ec_save.storage;
1849
229k
               bytes_buf = ec_save.buf+nstart_bytes;
1850
229k
               save_bytes = nend_bytes-nstart_bytes;
1851
229k
               OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1852
#ifdef ENABLE_QEXT
1853
               ext_nstart_bytes = ext_ec_save.offs;
1854
               ext_nend_bytes = ext_ec_save.storage;
1855
               ext_bytes_buf = ext_ec_save.buf!=NULL ? ext_ec_save.buf+ext_nstart_bytes : NULL;
1856
               ext_save_bytes = ext_nend_bytes-ext_nstart_bytes;
1857
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1858
#endif
1859
               /* Restore */
1860
229k
               *ec = ec_save;
1861
#ifdef ENABLE_QEXT
1862
               *ext_ec = ext_ec_save;
1863
#endif
1864
229k
               ctx = ctx_save;
1865
229k
               OPUS_COPY(X, X_save, N);
1866
229k
               OPUS_COPY(Y, Y_save, N);
1867
229k
#ifndef DISABLE_UPDATE_DRAFT
1868
229k
               if (i == start+1)
1869
19.5k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1870
229k
#endif
1871
               /* Encode and round up. */
1872
229k
               ctx.theta_round = 1;
1873
229k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1874
229k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1875
229k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1876
229k
               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));
1877
229k
               if (dist0 >= dist1) {
1878
159k
                  x_cm = cm2;
1879
159k
                  *ec = ec_save2;
1880
#ifdef ENABLE_QEXT
1881
                  *ext_ec = ext_ec_save2;
1882
#endif
1883
159k
                  ctx = ctx_save2;
1884
159k
                  OPUS_COPY(X, X_save2, N);
1885
159k
                  OPUS_COPY(Y, Y_save2, N);
1886
159k
                  if (!last)
1887
158k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1888
159k
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1889
#ifdef ENABLE_QEXT
1890
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1891
#endif
1892
159k
               }
1893
1.16M
            } else {
1894
1.16M
               ctx.theta_round = 0;
1895
1.16M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1896
1.16M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1897
1.16M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1898
1.16M
            }
1899
3.10M
         } else {
1900
3.10M
            x_cm = quant_band(&ctx, X, N, b, B,
1901
3.10M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1902
3.10M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1903
3.10M
         }
1904
4.50M
         y_cm = x_cm;
1905
4.50M
      }
1906
4.68M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1907
4.68M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1908
4.68M
      balance += pulses[i] + tell;
1909
1910
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1911
4.68M
      update_lowband = b>(N<<BITRES);
1912
      /* We only need to avoid noise on a split for the first band. After that, we
1913
         have folding. */
1914
4.68M
      ctx.avoid_split_noise = 0;
1915
4.68M
   }
1916
315k
   *seed = ctx.seed;
1917
1918
315k
   RESTORE_STACK;
1919
315k
}
quant_all_bands
Line
Count
Source
1597
332k
{
1598
332k
   int i;
1599
332k
   opus_int32 remaining_bits;
1600
332k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
332k
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
332k
   VARDECL(celt_norm, _norm);
1603
332k
   VARDECL(celt_norm, _lowband_scratch);
1604
332k
   VARDECL(celt_norm, X_save);
1605
332k
   VARDECL(celt_norm, Y_save);
1606
332k
   VARDECL(celt_norm, X_save2);
1607
332k
   VARDECL(celt_norm, Y_save2);
1608
332k
   VARDECL(celt_norm, norm_save2);
1609
332k
   int resynth_alloc;
1610
332k
   celt_norm *lowband_scratch;
1611
332k
   int B;
1612
332k
   int M;
1613
332k
   int lowband_offset;
1614
332k
   int update_lowband = 1;
1615
332k
   int C = Y_ != NULL ? 2 : 1;
1616
332k
   int norm_offset;
1617
332k
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1618
#ifdef RESYNTH
1619
   int resynth = 1;
1620
#else
1621
332k
   int resynth = !encode || theta_rdo;
1622
332k
#endif
1623
332k
   struct band_ctx ctx;
1624
332k
#ifdef ENABLE_QEXT
1625
332k
   int ext_b;
1626
332k
   opus_int32 ext_balance=0;
1627
332k
   opus_int32 ext_tell=0;
1628
332k
#endif
1629
332k
   SAVE_STACK;
1630
1631
332k
   M = 1<<LM;
1632
332k
   B = shortBlocks ? M : 1;
1633
332k
   norm_offset = M*eBands[start];
1634
   /* No need to allocate norm for the last band because we don't need an
1635
      output in that band. */
1636
332k
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1637
332k
   norm = _norm;
1638
332k
   norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset;
1639
1640
   /* For decoding, we can use the last band as scratch space because we don't need that
1641
      scratch space for the last band and we don't care about the data there until we're
1642
      decoding the last band. */
1643
332k
   if (encode && resynth)
1644
25.3k
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1645
306k
   else
1646
306k
      resynth_alloc = ALLOC_NONE;
1647
332k
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1648
332k
   if (encode && resynth)
1649
25.3k
      lowband_scratch = _lowband_scratch;
1650
306k
   else
1651
306k
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1652
332k
   ALLOC(X_save, resynth_alloc, celt_norm);
1653
332k
   ALLOC(Y_save, resynth_alloc, celt_norm);
1654
332k
   ALLOC(X_save2, resynth_alloc, celt_norm);
1655
332k
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1656
332k
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1657
1658
332k
   lowband_offset = 0;
1659
332k
   ctx.bandE = bandE;
1660
332k
   ctx.ec = ec;
1661
332k
   ctx.encode = encode;
1662
332k
   ctx.intensity = intensity;
1663
332k
   ctx.m = m;
1664
332k
   ctx.seed = *seed;
1665
332k
   ctx.spread = spread;
1666
332k
   ctx.arch = arch;
1667
332k
   ctx.disable_inv = disable_inv;
1668
332k
   ctx.resynth = resynth;
1669
332k
   ctx.theta_round = 0;
1670
332k
#ifdef ENABLE_QEXT
1671
332k
   ctx.ext_ec = ext_ec;
1672
332k
   ctx.ext_total_bits = ext_total_bits;
1673
332k
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1674
332k
   if (ctx.extra_bands) theta_rdo = 0;
1675
332k
#endif
1676
   /* Avoid injecting noise in the first band on transients. */
1677
332k
   ctx.avoid_split_noise = B > 1;
1678
5.25M
   for (i=start;i<end;i++)
1679
4.91M
   {
1680
4.91M
      opus_int32 tell;
1681
4.91M
      int b;
1682
4.91M
      int N;
1683
4.91M
      opus_int32 curr_balance;
1684
4.91M
      int effective_lowband=-1;
1685
4.91M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1686
4.91M
      int tf_change=0;
1687
4.91M
      unsigned x_cm;
1688
4.91M
      unsigned y_cm;
1689
4.91M
      int last;
1690
1691
4.91M
      ctx.i = i;
1692
4.91M
      last = (i==end-1);
1693
1694
4.91M
      X = X_+M*eBands[i];
1695
4.91M
      if (Y_!=NULL)
1696
1.64M
         Y = Y_+M*eBands[i];
1697
3.27M
      else
1698
3.27M
         Y = NULL;
1699
4.91M
      N = M*eBands[i+1]-M*eBands[i];
1700
4.91M
      celt_assert(N > 0);
1701
4.91M
      tell = ec_tell_frac(ec);
1702
1703
      /* Compute how many bits we want to allocate to this band */
1704
4.91M
      if (i != start)
1705
4.58M
         balance -= tell;
1706
4.91M
      remaining_bits = total_bits-tell-1;
1707
4.91M
      ctx.remaining_bits = remaining_bits;
1708
4.91M
#ifdef ENABLE_QEXT
1709
4.91M
      if (i != start) {
1710
4.58M
         ext_balance += extra_pulses[i-1] + ext_tell;
1711
4.58M
      }
1712
4.91M
      ext_tell = ec_tell_frac(ext_ec);
1713
4.91M
      ctx.extra_bits = extra_pulses[i];
1714
4.91M
      if (i != start)
1715
4.58M
         ext_balance -= ext_tell;
1716
4.91M
      if (i <= codedBands-1)
1717
2.57M
      {
1718
2.57M
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1719
2.57M
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1720
2.57M
      } else {
1721
2.34M
         ext_b = 0;
1722
2.34M
      }
1723
4.91M
#endif
1724
4.91M
      if (i <= codedBands-1)
1725
2.57M
      {
1726
2.57M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1727
2.57M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1728
2.57M
      } else {
1729
2.34M
         b = 0;
1730
2.34M
      }
1731
1732
4.91M
#ifndef DISABLE_UPDATE_DRAFT
1733
4.91M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1734
893k
            lowband_offset = i;
1735
4.91M
      if (i == start+1)
1736
332k
         special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1737
#else
1738
      if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0))
1739
            lowband_offset = i;
1740
#endif
1741
1742
4.91M
      tf_change = tf_res[i];
1743
4.91M
      ctx.tf_change = tf_change;
1744
4.91M
      if (i>=m->effEBands)
1745
20.3k
      {
1746
20.3k
         X=norm;
1747
20.3k
         if (Y_!=NULL)
1748
15.3k
            Y = norm;
1749
20.3k
         lowband_scratch = NULL;
1750
20.3k
      }
1751
4.91M
      if (last && !theta_rdo)
1752
306k
         lowband_scratch = NULL;
1753
1754
      /* Get a conservative estimate of the collapse_mask's for the bands we're
1755
         going to be folding from. */
1756
4.91M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1757
2.62M
      {
1758
2.62M
         int fold_start;
1759
2.62M
         int fold_end;
1760
2.62M
         int fold_i;
1761
         /* This ensures we never repeat spectral content within one band */
1762
2.62M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1763
2.62M
         fold_start = lowband_offset;
1764
3.04M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1765
2.62M
         fold_end = lowband_offset-1;
1766
2.62M
#ifndef DISABLE_UPDATE_DRAFT
1767
6.39M
         while(++fold_end < i && M*eBands[fold_end] < effective_lowband+norm_offset+N);
1768
#else
1769
         while(M*eBands[++fold_end] < effective_lowband+norm_offset+N);
1770
#endif
1771
2.62M
         x_cm = y_cm = 0;
1772
6.81M
         fold_i = fold_start; do {
1773
6.81M
           x_cm |= collapse_masks[fold_i*C+0];
1774
6.81M
           y_cm |= collapse_masks[fold_i*C+C-1];
1775
6.81M
         } while (++fold_i<fold_end);
1776
2.62M
      }
1777
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1778
         always) be non-zero. */
1779
2.29M
      else
1780
2.29M
         x_cm = y_cm = (1<<B)-1;
1781
1782
4.91M
      if (dual_stereo && i==intensity)
1783
17.2k
      {
1784
17.2k
         int j;
1785
1786
         /* Switch off dual stereo to do intensity. */
1787
17.2k
         dual_stereo = 0;
1788
17.2k
         if (resynth)
1789
364k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1790
355k
               norm[j] = HALF32(norm[j]+norm2[j]);
1791
17.2k
      }
1792
4.91M
      if (dual_stereo)
1793
200k
      {
1794
200k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1795
200k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1796
200k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1797
200k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1798
200k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1799
200k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1800
4.71M
      } else {
1801
4.71M
         if (Y!=NULL)
1802
1.44M
         {
1803
1.44M
            if (theta_rdo && i < intensity)
1804
229k
            {
1805
229k
               ec_ctx ec_save, ec_save2;
1806
229k
               struct band_ctx ctx_save, ctx_save2;
1807
229k
               opus_val32 dist0, dist1;
1808
229k
               unsigned cm, cm2;
1809
229k
               int nstart_bytes, nend_bytes, save_bytes;
1810
229k
               unsigned char *bytes_buf;
1811
229k
               unsigned char bytes_save[1275];
1812
229k
#ifdef ENABLE_QEXT
1813
229k
               ec_ctx ext_ec_save, ext_ec_save2;
1814
229k
               unsigned char *ext_bytes_buf;
1815
229k
               int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes;
1816
229k
               unsigned char ext_bytes_save[QEXT_PACKET_SIZE_CAP];
1817
229k
#endif
1818
229k
               opus_val16 w[2];
1819
229k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1820
               /* Make a copy. */
1821
229k
               cm = x_cm|y_cm;
1822
229k
               ec_save = *ec;
1823
229k
#ifdef ENABLE_QEXT
1824
229k
               ext_ec_save = *ext_ec;
1825
229k
#endif
1826
229k
               ctx_save = ctx;
1827
229k
               OPUS_COPY(X_save, X, N);
1828
229k
               OPUS_COPY(Y_save, Y, N);
1829
               /* Encode and round down. */
1830
229k
               ctx.theta_round = -1;
1831
229k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1832
229k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1833
229k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1834
229k
               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));
1835
1836
               /* Save first result. */
1837
229k
               cm2 = x_cm;
1838
229k
               ec_save2 = *ec;
1839
229k
#ifdef ENABLE_QEXT
1840
229k
               ext_ec_save2 = *ext_ec;
1841
229k
#endif
1842
229k
               ctx_save2 = ctx;
1843
229k
               OPUS_COPY(X_save2, X, N);
1844
229k
               OPUS_COPY(Y_save2, Y, N);
1845
229k
               if (!last)
1846
226k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1847
229k
               nstart_bytes = ec_save.offs;
1848
229k
               nend_bytes = ec_save.storage;
1849
229k
               bytes_buf = ec_save.buf+nstart_bytes;
1850
229k
               save_bytes = nend_bytes-nstart_bytes;
1851
229k
               OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1852
229k
#ifdef ENABLE_QEXT
1853
229k
               ext_nstart_bytes = ext_ec_save.offs;
1854
229k
               ext_nend_bytes = ext_ec_save.storage;
1855
229k
               ext_bytes_buf = ext_ec_save.buf!=NULL ? ext_ec_save.buf+ext_nstart_bytes : NULL;
1856
229k
               ext_save_bytes = ext_nend_bytes-ext_nstart_bytes;
1857
229k
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1858
229k
#endif
1859
               /* Restore */
1860
229k
               *ec = ec_save;
1861
229k
#ifdef ENABLE_QEXT
1862
229k
               *ext_ec = ext_ec_save;
1863
229k
#endif
1864
229k
               ctx = ctx_save;
1865
229k
               OPUS_COPY(X, X_save, N);
1866
229k
               OPUS_COPY(Y, Y_save, N);
1867
229k
#ifndef DISABLE_UPDATE_DRAFT
1868
229k
               if (i == start+1)
1869
19.3k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1870
229k
#endif
1871
               /* Encode and round up. */
1872
229k
               ctx.theta_round = 1;
1873
229k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1874
229k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1875
229k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1876
229k
               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));
1877
229k
               if (dist0 >= dist1) {
1878
155k
                  x_cm = cm2;
1879
155k
                  *ec = ec_save2;
1880
155k
#ifdef ENABLE_QEXT
1881
155k
                  *ext_ec = ext_ec_save2;
1882
155k
#endif
1883
155k
                  ctx = ctx_save2;
1884
155k
                  OPUS_COPY(X, X_save2, N);
1885
155k
                  OPUS_COPY(Y, Y_save2, N);
1886
155k
                  if (!last)
1887
154k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1888
155k
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1889
155k
#ifdef ENABLE_QEXT
1890
155k
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1891
155k
#endif
1892
155k
               }
1893
1.21M
            } else {
1894
1.21M
               ctx.theta_round = 0;
1895
1.21M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1896
1.21M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1897
1.21M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1898
1.21M
            }
1899
3.27M
         } else {
1900
3.27M
            x_cm = quant_band(&ctx, X, N, b, B,
1901
3.27M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1902
3.27M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1903
3.27M
         }
1904
4.71M
         y_cm = x_cm;
1905
4.71M
      }
1906
4.91M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1907
4.91M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1908
4.91M
      balance += pulses[i] + tell;
1909
1910
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1911
4.91M
      update_lowband = b>(N<<BITRES);
1912
      /* We only need to avoid noise on a split for the first band. After that, we
1913
         have folding. */
1914
4.91M
      ctx.avoid_split_noise = 0;
1915
4.91M
   }
1916
332k
   *seed = ctx.seed;
1917
1918
332k
   RESTORE_STACK;
1919
332k
}
quant_all_bands
Line
Count
Source
1597
332k
{
1598
332k
   int i;
1599
332k
   opus_int32 remaining_bits;
1600
332k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
332k
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
332k
   VARDECL(celt_norm, _norm);
1603
332k
   VARDECL(celt_norm, _lowband_scratch);
1604
332k
   VARDECL(celt_norm, X_save);
1605
332k
   VARDECL(celt_norm, Y_save);
1606
332k
   VARDECL(celt_norm, X_save2);
1607
332k
   VARDECL(celt_norm, Y_save2);
1608
332k
   VARDECL(celt_norm, norm_save2);
1609
332k
   int resynth_alloc;
1610
332k
   celt_norm *lowband_scratch;
1611
332k
   int B;
1612
332k
   int M;
1613
332k
   int lowband_offset;
1614
332k
   int update_lowband = 1;
1615
332k
   int C = Y_ != NULL ? 2 : 1;
1616
332k
   int norm_offset;
1617
332k
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1618
#ifdef RESYNTH
1619
   int resynth = 1;
1620
#else
1621
332k
   int resynth = !encode || theta_rdo;
1622
332k
#endif
1623
332k
   struct band_ctx ctx;
1624
332k
#ifdef ENABLE_QEXT
1625
332k
   int ext_b;
1626
332k
   opus_int32 ext_balance=0;
1627
332k
   opus_int32 ext_tell=0;
1628
332k
#endif
1629
332k
   SAVE_STACK;
1630
1631
332k
   M = 1<<LM;
1632
332k
   B = shortBlocks ? M : 1;
1633
332k
   norm_offset = M*eBands[start];
1634
   /* No need to allocate norm for the last band because we don't need an
1635
      output in that band. */
1636
332k
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1637
332k
   norm = _norm;
1638
332k
   norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset;
1639
1640
   /* For decoding, we can use the last band as scratch space because we don't need that
1641
      scratch space for the last band and we don't care about the data there until we're
1642
      decoding the last band. */
1643
332k
   if (encode && resynth)
1644
25.3k
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1645
306k
   else
1646
306k
      resynth_alloc = ALLOC_NONE;
1647
332k
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1648
332k
   if (encode && resynth)
1649
25.3k
      lowband_scratch = _lowband_scratch;
1650
306k
   else
1651
306k
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1652
332k
   ALLOC(X_save, resynth_alloc, celt_norm);
1653
332k
   ALLOC(Y_save, resynth_alloc, celt_norm);
1654
332k
   ALLOC(X_save2, resynth_alloc, celt_norm);
1655
332k
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1656
332k
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1657
1658
332k
   lowband_offset = 0;
1659
332k
   ctx.bandE = bandE;
1660
332k
   ctx.ec = ec;
1661
332k
   ctx.encode = encode;
1662
332k
   ctx.intensity = intensity;
1663
332k
   ctx.m = m;
1664
332k
   ctx.seed = *seed;
1665
332k
   ctx.spread = spread;
1666
332k
   ctx.arch = arch;
1667
332k
   ctx.disable_inv = disable_inv;
1668
332k
   ctx.resynth = resynth;
1669
332k
   ctx.theta_round = 0;
1670
332k
#ifdef ENABLE_QEXT
1671
332k
   ctx.ext_ec = ext_ec;
1672
332k
   ctx.ext_total_bits = ext_total_bits;
1673
332k
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1674
332k
   if (ctx.extra_bands) theta_rdo = 0;
1675
332k
#endif
1676
   /* Avoid injecting noise in the first band on transients. */
1677
332k
   ctx.avoid_split_noise = B > 1;
1678
5.25M
   for (i=start;i<end;i++)
1679
4.91M
   {
1680
4.91M
      opus_int32 tell;
1681
4.91M
      int b;
1682
4.91M
      int N;
1683
4.91M
      opus_int32 curr_balance;
1684
4.91M
      int effective_lowband=-1;
1685
4.91M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1686
4.91M
      int tf_change=0;
1687
4.91M
      unsigned x_cm;
1688
4.91M
      unsigned y_cm;
1689
4.91M
      int last;
1690
1691
4.91M
      ctx.i = i;
1692
4.91M
      last = (i==end-1);
1693
1694
4.91M
      X = X_+M*eBands[i];
1695
4.91M
      if (Y_!=NULL)
1696
1.64M
         Y = Y_+M*eBands[i];
1697
3.27M
      else
1698
3.27M
         Y = NULL;
1699
4.91M
      N = M*eBands[i+1]-M*eBands[i];
1700
4.91M
      celt_assert(N > 0);
1701
4.91M
      tell = ec_tell_frac(ec);
1702
1703
      /* Compute how many bits we want to allocate to this band */
1704
4.91M
      if (i != start)
1705
4.58M
         balance -= tell;
1706
4.91M
      remaining_bits = total_bits-tell-1;
1707
4.91M
      ctx.remaining_bits = remaining_bits;
1708
4.91M
#ifdef ENABLE_QEXT
1709
4.91M
      if (i != start) {
1710
4.58M
         ext_balance += extra_pulses[i-1] + ext_tell;
1711
4.58M
      }
1712
4.91M
      ext_tell = ec_tell_frac(ext_ec);
1713
4.91M
      ctx.extra_bits = extra_pulses[i];
1714
4.91M
      if (i != start)
1715
4.58M
         ext_balance -= ext_tell;
1716
4.91M
      if (i <= codedBands-1)
1717
2.57M
      {
1718
2.57M
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1719
2.57M
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1720
2.57M
      } else {
1721
2.34M
         ext_b = 0;
1722
2.34M
      }
1723
4.91M
#endif
1724
4.91M
      if (i <= codedBands-1)
1725
2.57M
      {
1726
2.57M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1727
2.57M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1728
2.57M
      } else {
1729
2.34M
         b = 0;
1730
2.34M
      }
1731
1732
4.91M
#ifndef DISABLE_UPDATE_DRAFT
1733
4.91M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1734
893k
            lowband_offset = i;
1735
4.91M
      if (i == start+1)
1736
332k
         special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1737
#else
1738
      if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0))
1739
            lowband_offset = i;
1740
#endif
1741
1742
4.91M
      tf_change = tf_res[i];
1743
4.91M
      ctx.tf_change = tf_change;
1744
4.91M
      if (i>=m->effEBands)
1745
20.3k
      {
1746
20.3k
         X=norm;
1747
20.3k
         if (Y_!=NULL)
1748
15.3k
            Y = norm;
1749
20.3k
         lowband_scratch = NULL;
1750
20.3k
      }
1751
4.91M
      if (last && !theta_rdo)
1752
306k
         lowband_scratch = NULL;
1753
1754
      /* Get a conservative estimate of the collapse_mask's for the bands we're
1755
         going to be folding from. */
1756
4.91M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1757
2.62M
      {
1758
2.62M
         int fold_start;
1759
2.62M
         int fold_end;
1760
2.62M
         int fold_i;
1761
         /* This ensures we never repeat spectral content within one band */
1762
2.62M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1763
2.62M
         fold_start = lowband_offset;
1764
3.04M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1765
2.62M
         fold_end = lowband_offset-1;
1766
2.62M
#ifndef DISABLE_UPDATE_DRAFT
1767
6.39M
         while(++fold_end < i && M*eBands[fold_end] < effective_lowband+norm_offset+N);
1768
#else
1769
         while(M*eBands[++fold_end] < effective_lowband+norm_offset+N);
1770
#endif
1771
2.62M
         x_cm = y_cm = 0;
1772
6.81M
         fold_i = fold_start; do {
1773
6.81M
           x_cm |= collapse_masks[fold_i*C+0];
1774
6.81M
           y_cm |= collapse_masks[fold_i*C+C-1];
1775
6.81M
         } while (++fold_i<fold_end);
1776
2.62M
      }
1777
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1778
         always) be non-zero. */
1779
2.29M
      else
1780
2.29M
         x_cm = y_cm = (1<<B)-1;
1781
1782
4.91M
      if (dual_stereo && i==intensity)
1783
17.2k
      {
1784
17.2k
         int j;
1785
1786
         /* Switch off dual stereo to do intensity. */
1787
17.2k
         dual_stereo = 0;
1788
17.2k
         if (resynth)
1789
364k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1790
355k
               norm[j] = HALF32(norm[j]+norm2[j]);
1791
17.2k
      }
1792
4.91M
      if (dual_stereo)
1793
200k
      {
1794
200k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1795
200k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1796
200k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1797
200k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1798
200k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1799
200k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1800
4.71M
      } else {
1801
4.71M
         if (Y!=NULL)
1802
1.44M
         {
1803
1.44M
            if (theta_rdo && i < intensity)
1804
229k
            {
1805
229k
               ec_ctx ec_save, ec_save2;
1806
229k
               struct band_ctx ctx_save, ctx_save2;
1807
229k
               opus_val32 dist0, dist1;
1808
229k
               unsigned cm, cm2;
1809
229k
               int nstart_bytes, nend_bytes, save_bytes;
1810
229k
               unsigned char *bytes_buf;
1811
229k
               unsigned char bytes_save[1275];
1812
229k
#ifdef ENABLE_QEXT
1813
229k
               ec_ctx ext_ec_save, ext_ec_save2;
1814
229k
               unsigned char *ext_bytes_buf;
1815
229k
               int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes;
1816
229k
               unsigned char ext_bytes_save[QEXT_PACKET_SIZE_CAP];
1817
229k
#endif
1818
229k
               opus_val16 w[2];
1819
229k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1820
               /* Make a copy. */
1821
229k
               cm = x_cm|y_cm;
1822
229k
               ec_save = *ec;
1823
229k
#ifdef ENABLE_QEXT
1824
229k
               ext_ec_save = *ext_ec;
1825
229k
#endif
1826
229k
               ctx_save = ctx;
1827
229k
               OPUS_COPY(X_save, X, N);
1828
229k
               OPUS_COPY(Y_save, Y, N);
1829
               /* Encode and round down. */
1830
229k
               ctx.theta_round = -1;
1831
229k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1832
229k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1833
229k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1834
229k
               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));
1835
1836
               /* Save first result. */
1837
229k
               cm2 = x_cm;
1838
229k
               ec_save2 = *ec;
1839
229k
#ifdef ENABLE_QEXT
1840
229k
               ext_ec_save2 = *ext_ec;
1841
229k
#endif
1842
229k
               ctx_save2 = ctx;
1843
229k
               OPUS_COPY(X_save2, X, N);
1844
229k
               OPUS_COPY(Y_save2, Y, N);
1845
229k
               if (!last)
1846
226k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1847
229k
               nstart_bytes = ec_save.offs;
1848
229k
               nend_bytes = ec_save.storage;
1849
229k
               bytes_buf = ec_save.buf+nstart_bytes;
1850
229k
               save_bytes = nend_bytes-nstart_bytes;
1851
229k
               OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1852
229k
#ifdef ENABLE_QEXT
1853
229k
               ext_nstart_bytes = ext_ec_save.offs;
1854
229k
               ext_nend_bytes = ext_ec_save.storage;
1855
229k
               ext_bytes_buf = ext_ec_save.buf!=NULL ? ext_ec_save.buf+ext_nstart_bytes : NULL;
1856
229k
               ext_save_bytes = ext_nend_bytes-ext_nstart_bytes;
1857
229k
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1858
229k
#endif
1859
               /* Restore */
1860
229k
               *ec = ec_save;
1861
229k
#ifdef ENABLE_QEXT
1862
229k
               *ext_ec = ext_ec_save;
1863
229k
#endif
1864
229k
               ctx = ctx_save;
1865
229k
               OPUS_COPY(X, X_save, N);
1866
229k
               OPUS_COPY(Y, Y_save, N);
1867
229k
#ifndef DISABLE_UPDATE_DRAFT
1868
229k
               if (i == start+1)
1869
19.3k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1870
229k
#endif
1871
               /* Encode and round up. */
1872
229k
               ctx.theta_round = 1;
1873
229k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1874
229k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1875
229k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1876
229k
               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));
1877
229k
               if (dist0 >= dist1) {
1878
155k
                  x_cm = cm2;
1879
155k
                  *ec = ec_save2;
1880
155k
#ifdef ENABLE_QEXT
1881
155k
                  *ext_ec = ext_ec_save2;
1882
155k
#endif
1883
155k
                  ctx = ctx_save2;
1884
155k
                  OPUS_COPY(X, X_save2, N);
1885
155k
                  OPUS_COPY(Y, Y_save2, N);
1886
155k
                  if (!last)
1887
154k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1888
155k
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1889
155k
#ifdef ENABLE_QEXT
1890
155k
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1891
155k
#endif
1892
155k
               }
1893
1.21M
            } else {
1894
1.21M
               ctx.theta_round = 0;
1895
1.21M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1896
1.21M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1897
1.21M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1898
1.21M
            }
1899
3.27M
         } else {
1900
3.27M
            x_cm = quant_band(&ctx, X, N, b, B,
1901
3.27M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1902
3.27M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1903
3.27M
         }
1904
4.71M
         y_cm = x_cm;
1905
4.71M
      }
1906
4.91M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1907
4.91M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1908
4.91M
      balance += pulses[i] + tell;
1909
1910
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1911
4.91M
      update_lowband = b>(N<<BITRES);
1912
      /* We only need to avoid noise on a split for the first band. After that, we
1913
         have folding. */
1914
4.91M
      ctx.avoid_split_noise = 0;
1915
4.91M
   }
1916
332k
   *seed = ctx.seed;
1917
1918
332k
   RESTORE_STACK;
1919
332k
}
quant_all_bands
Line
Count
Source
1597
315k
{
1598
315k
   int i;
1599
315k
   opus_int32 remaining_bits;
1600
315k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1601
315k
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1602
315k
   VARDECL(celt_norm, _norm);
1603
315k
   VARDECL(celt_norm, _lowband_scratch);
1604
315k
   VARDECL(celt_norm, X_save);
1605
315k
   VARDECL(celt_norm, Y_save);
1606
315k
   VARDECL(celt_norm, X_save2);
1607
315k
   VARDECL(celt_norm, Y_save2);
1608
315k
   VARDECL(celt_norm, norm_save2);
1609
315k
   int resynth_alloc;
1610
315k
   celt_norm *lowband_scratch;
1611
315k
   int B;
1612
315k
   int M;
1613
315k
   int lowband_offset;
1614
315k
   int update_lowband = 1;
1615
315k
   int C = Y_ != NULL ? 2 : 1;
1616
315k
   int norm_offset;
1617
315k
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1618
#ifdef RESYNTH
1619
   int resynth = 1;
1620
#else
1621
315k
   int resynth = !encode || theta_rdo;
1622
315k
#endif
1623
315k
   struct band_ctx ctx;
1624
#ifdef ENABLE_QEXT
1625
   int ext_b;
1626
   opus_int32 ext_balance=0;
1627
   opus_int32 ext_tell=0;
1628
#endif
1629
315k
   SAVE_STACK;
1630
1631
315k
   M = 1<<LM;
1632
315k
   B = shortBlocks ? M : 1;
1633
315k
   norm_offset = M*eBands[start];
1634
   /* No need to allocate norm for the last band because we don't need an
1635
      output in that band. */
1636
315k
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1637
315k
   norm = _norm;
1638
315k
   norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset;
1639
1640
   /* For decoding, we can use the last band as scratch space because we don't need that
1641
      scratch space for the last band and we don't care about the data there until we're
1642
      decoding the last band. */
1643
315k
   if (encode && resynth)
1644
25.5k
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1645
289k
   else
1646
289k
      resynth_alloc = ALLOC_NONE;
1647
315k
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1648
315k
   if (encode && resynth)
1649
25.5k
      lowband_scratch = _lowband_scratch;
1650
289k
   else
1651
289k
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1652
315k
   ALLOC(X_save, resynth_alloc, celt_norm);
1653
315k
   ALLOC(Y_save, resynth_alloc, celt_norm);
1654
315k
   ALLOC(X_save2, resynth_alloc, celt_norm);
1655
315k
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1656
315k
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1657
1658
315k
   lowband_offset = 0;
1659
315k
   ctx.bandE = bandE;
1660
315k
   ctx.ec = ec;
1661
315k
   ctx.encode = encode;
1662
315k
   ctx.intensity = intensity;
1663
315k
   ctx.m = m;
1664
315k
   ctx.seed = *seed;
1665
315k
   ctx.spread = spread;
1666
315k
   ctx.arch = arch;
1667
315k
   ctx.disable_inv = disable_inv;
1668
315k
   ctx.resynth = resynth;
1669
315k
   ctx.theta_round = 0;
1670
#ifdef ENABLE_QEXT
1671
   ctx.ext_ec = ext_ec;
1672
   ctx.ext_total_bits = ext_total_bits;
1673
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1674
   if (ctx.extra_bands) theta_rdo = 0;
1675
#endif
1676
   /* Avoid injecting noise in the first band on transients. */
1677
315k
   ctx.avoid_split_noise = B > 1;
1678
5.00M
   for (i=start;i<end;i++)
1679
4.68M
   {
1680
4.68M
      opus_int32 tell;
1681
4.68M
      int b;
1682
4.68M
      int N;
1683
4.68M
      opus_int32 curr_balance;
1684
4.68M
      int effective_lowband=-1;
1685
4.68M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1686
4.68M
      int tf_change=0;
1687
4.68M
      unsigned x_cm;
1688
4.68M
      unsigned y_cm;
1689
4.68M
      int last;
1690
1691
4.68M
      ctx.i = i;
1692
4.68M
      last = (i==end-1);
1693
1694
4.68M
      X = X_+M*eBands[i];
1695
4.68M
      if (Y_!=NULL)
1696
1.57M
         Y = Y_+M*eBands[i];
1697
3.10M
      else
1698
3.10M
         Y = NULL;
1699
4.68M
      N = M*eBands[i+1]-M*eBands[i];
1700
4.68M
      celt_assert(N > 0);
1701
4.68M
      tell = ec_tell_frac(ec);
1702
1703
      /* Compute how many bits we want to allocate to this band */
1704
4.68M
      if (i != start)
1705
4.36M
         balance -= tell;
1706
4.68M
      remaining_bits = total_bits-tell-1;
1707
4.68M
      ctx.remaining_bits = remaining_bits;
1708
#ifdef ENABLE_QEXT
1709
      if (i != start) {
1710
         ext_balance += extra_pulses[i-1] + ext_tell;
1711
      }
1712
      ext_tell = ec_tell_frac(ext_ec);
1713
      ctx.extra_bits = extra_pulses[i];
1714
      if (i != start)
1715
         ext_balance -= ext_tell;
1716
      if (i <= codedBands-1)
1717
      {
1718
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1719
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1720
      } else {
1721
         ext_b = 0;
1722
      }
1723
#endif
1724
4.68M
      if (i <= codedBands-1)
1725
2.23M
      {
1726
2.23M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1727
2.23M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1728
2.44M
      } else {
1729
2.44M
         b = 0;
1730
2.44M
      }
1731
1732
4.68M
#ifndef DISABLE_UPDATE_DRAFT
1733
4.68M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1734
740k
            lowband_offset = i;
1735
4.68M
      if (i == start+1)
1736
315k
         special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1737
#else
1738
      if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0))
1739
            lowband_offset = i;
1740
#endif
1741
1742
4.68M
      tf_change = tf_res[i];
1743
4.68M
      ctx.tf_change = tf_change;
1744
4.68M
      if (i>=m->effEBands)
1745
0
      {
1746
0
         X=norm;
1747
0
         if (Y_!=NULL)
1748
0
            Y = norm;
1749
0
         lowband_scratch = NULL;
1750
0
      }
1751
4.68M
      if (last && !theta_rdo)
1752
289k
         lowband_scratch = NULL;
1753
1754
      /* Get a conservative estimate of the collapse_mask's for the bands we're
1755
         going to be folding from. */
1756
4.68M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1757
2.57M
      {
1758
2.57M
         int fold_start;
1759
2.57M
         int fold_end;
1760
2.57M
         int fold_i;
1761
         /* This ensures we never repeat spectral content within one band */
1762
2.57M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1763
2.57M
         fold_start = lowband_offset;
1764
2.87M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1765
2.57M
         fold_end = lowband_offset-1;
1766
2.57M
#ifndef DISABLE_UPDATE_DRAFT
1767
6.57M
         while(++fold_end < i && M*eBands[fold_end] < effective_lowband+norm_offset+N);
1768
#else
1769
         while(M*eBands[++fold_end] < effective_lowband+norm_offset+N);
1770
#endif
1771
2.57M
         x_cm = y_cm = 0;
1772
6.87M
         fold_i = fold_start; do {
1773
6.87M
           x_cm |= collapse_masks[fold_i*C+0];
1774
6.87M
           y_cm |= collapse_masks[fold_i*C+C-1];
1775
6.87M
         } while (++fold_i<fold_end);
1776
2.57M
      }
1777
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1778
         always) be non-zero. */
1779
2.11M
      else
1780
2.11M
         x_cm = y_cm = (1<<B)-1;
1781
1782
4.68M
      if (dual_stereo && i==intensity)
1783
15.3k
      {
1784
15.3k
         int j;
1785
1786
         /* Switch off dual stereo to do intensity. */
1787
15.3k
         dual_stereo = 0;
1788
15.3k
         if (resynth)
1789
221k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1790
215k
               norm[j] = HALF32(norm[j]+norm2[j]);
1791
15.3k
      }
1792
4.68M
      if (dual_stereo)
1793
181k
      {
1794
181k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1795
181k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1796
181k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1797
181k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1798
181k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1799
181k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1800
4.50M
      } else {
1801
4.50M
         if (Y!=NULL)
1802
1.39M
         {
1803
1.39M
            if (theta_rdo && i < intensity)
1804
229k
            {
1805
229k
               ec_ctx ec_save, ec_save2;
1806
229k
               struct band_ctx ctx_save, ctx_save2;
1807
229k
               opus_val32 dist0, dist1;
1808
229k
               unsigned cm, cm2;
1809
229k
               int nstart_bytes, nend_bytes, save_bytes;
1810
229k
               unsigned char *bytes_buf;
1811
229k
               unsigned char bytes_save[1275];
1812
#ifdef ENABLE_QEXT
1813
               ec_ctx ext_ec_save, ext_ec_save2;
1814
               unsigned char *ext_bytes_buf;
1815
               int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes;
1816
               unsigned char ext_bytes_save[QEXT_PACKET_SIZE_CAP];
1817
#endif
1818
229k
               opus_val16 w[2];
1819
229k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1820
               /* Make a copy. */
1821
229k
               cm = x_cm|y_cm;
1822
229k
               ec_save = *ec;
1823
#ifdef ENABLE_QEXT
1824
               ext_ec_save = *ext_ec;
1825
#endif
1826
229k
               ctx_save = ctx;
1827
229k
               OPUS_COPY(X_save, X, N);
1828
229k
               OPUS_COPY(Y_save, Y, N);
1829
               /* Encode and round down. */
1830
229k
               ctx.theta_round = -1;
1831
229k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1832
229k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1833
229k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1834
229k
               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));
1835
1836
               /* Save first result. */
1837
229k
               cm2 = x_cm;
1838
229k
               ec_save2 = *ec;
1839
#ifdef ENABLE_QEXT
1840
               ext_ec_save2 = *ext_ec;
1841
#endif
1842
229k
               ctx_save2 = ctx;
1843
229k
               OPUS_COPY(X_save2, X, N);
1844
229k
               OPUS_COPY(Y_save2, Y, N);
1845
229k
               if (!last)
1846
227k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1847
229k
               nstart_bytes = ec_save.offs;
1848
229k
               nend_bytes = ec_save.storage;
1849
229k
               bytes_buf = ec_save.buf+nstart_bytes;
1850
229k
               save_bytes = nend_bytes-nstart_bytes;
1851
229k
               OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1852
#ifdef ENABLE_QEXT
1853
               ext_nstart_bytes = ext_ec_save.offs;
1854
               ext_nend_bytes = ext_ec_save.storage;
1855
               ext_bytes_buf = ext_ec_save.buf!=NULL ? ext_ec_save.buf+ext_nstart_bytes : NULL;
1856
               ext_save_bytes = ext_nend_bytes-ext_nstart_bytes;
1857
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1858
#endif
1859
               /* Restore */
1860
229k
               *ec = ec_save;
1861
#ifdef ENABLE_QEXT
1862
               *ext_ec = ext_ec_save;
1863
#endif
1864
229k
               ctx = ctx_save;
1865
229k
               OPUS_COPY(X, X_save, N);
1866
229k
               OPUS_COPY(Y, Y_save, N);
1867
229k
#ifndef DISABLE_UPDATE_DRAFT
1868
229k
               if (i == start+1)
1869
19.5k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1870
229k
#endif
1871
               /* Encode and round up. */
1872
229k
               ctx.theta_round = 1;
1873
229k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1874
229k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1875
229k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1876
229k
               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));
1877
229k
               if (dist0 >= dist1) {
1878
159k
                  x_cm = cm2;
1879
159k
                  *ec = ec_save2;
1880
#ifdef ENABLE_QEXT
1881
                  *ext_ec = ext_ec_save2;
1882
#endif
1883
159k
                  ctx = ctx_save2;
1884
159k
                  OPUS_COPY(X, X_save2, N);
1885
159k
                  OPUS_COPY(Y, Y_save2, N);
1886
159k
                  if (!last)
1887
158k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1888
159k
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1889
#ifdef ENABLE_QEXT
1890
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1891
#endif
1892
159k
               }
1893
1.16M
            } else {
1894
1.16M
               ctx.theta_round = 0;
1895
1.16M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1896
1.16M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1897
1.16M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1898
1.16M
            }
1899
3.10M
         } else {
1900
3.10M
            x_cm = quant_band(&ctx, X, N, b, B,
1901
3.10M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1902
3.10M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1903
3.10M
         }
1904
4.50M
         y_cm = x_cm;
1905
4.50M
      }
1906
4.68M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1907
4.68M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1908
4.68M
      balance += pulses[i] + tell;
1909
1910
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1911
4.68M
      update_lowband = b>(N<<BITRES);
1912
      /* We only need to avoid noise on a split for the first band. After that, we
1913
         have folding. */
1914
4.68M
      ctx.avoid_split_noise = 0;
1915
4.68M
   }
1916
315k
   *seed = ctx.seed;
1917
1918
315k
   RESTORE_STACK;
1919
315k
}