Coverage Report

Created: 2025-07-23 07:59

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