Coverage Report

Created: 2025-07-01 07:05

/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
75.2k
{
52
75.2k
   int i;
53
968k
   for (i=0;i<N;i++)
54
966k
   {
55
966k
      if (val < thresholds[i])
56
73.8k
         break;
57
966k
   }
58
75.2k
   if (i>prev && val < thresholds[prev]+hysteresis[prev])
59
707
      i=prev;
60
75.2k
   if (i<prev && val > thresholds[prev-1]-hysteresis[prev-1])
61
550
      i=prev;
62
75.2k
   return i;
63
75.2k
}
64
65
opus_uint32 celt_lcg_rand(opus_uint32 seed)
66
46.9M
{
67
46.9M
   return 1664525 * seed + 1013904223;
68
46.9M
}
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
4.02M
{
74
4.02M
   opus_int32 tmp;
75
4.02M
   opus_int16 x2;
76
4.02M
   tmp = (4096+((opus_int32)(x)*(x)))>>13;
77
4.02M
   celt_sig_assert(tmp<=32767);
78
4.02M
   x2 = tmp;
79
4.02M
   x2 = (32767-x2) + FRAC_MUL16(x2, (-7651 + FRAC_MUL16(x2, (8277 + FRAC_MUL16(-626, x2)))));
80
4.02M
   celt_sig_assert(x2<=32766);
81
4.02M
   return 1+x2;
82
4.02M
}
83
84
int bitexact_log2tan(int isin,int icos)
85
2.01M
{
86
2.01M
   int lc;
87
2.01M
   int ls;
88
2.01M
   lc=EC_ILOG(icos);
89
2.01M
   ls=EC_ILOG(isin);
90
2.01M
   icos<<=15-lc;
91
2.01M
   isin<<=15-ls;
92
2.01M
   return (ls-lc)*(1<<11)
93
2.01M
         +FRAC_MUL16(isin, FRAC_MUL16(isin, -2597) + 7932)
94
2.01M
         -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932);
95
2.01M
}
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
122k
{
101
122k
   int i, c, N;
102
122k
   const opus_int16 *eBands = m->eBands;
103
122k
   (void)arch;
104
122k
   N = m->shortMdctSize<<LM;
105
164k
   c=0; do {
106
2.67M
      for (i=0;i<end;i++)
107
2.51M
      {
108
2.51M
         int j;
109
2.51M
         opus_val32 maxval=0;
110
2.51M
         opus_val32 sum = 0;
111
112
2.51M
         maxval = celt_maxabs32(&X[c*N+(eBands[i]<<LM)], (eBands[i+1]-eBands[i])<<LM);
113
2.51M
         if (maxval > 0)
114
2.10M
         {
115
2.10M
            int shift = IMAX(0, 30 - celt_ilog2(maxval+(maxval>>14)+1) - ((((m->logN[i]+7)>>BITRES)+LM+1)>>1));
116
22.0M
            j=eBands[i]<<LM; do {
117
22.0M
               opus_val32 x = SHL32(X[j+c*N],shift);
118
22.0M
               sum = ADD32(sum, MULT32_32_Q31(x, x));
119
22.0M
            } while (++j<eBands[i+1]<<LM);
120
2.10M
            bandE[i+c*m->nbEBands] = MAX32(maxval, PSHR32(celt_sqrt32(SHR32(sum,1)), shift));
121
2.10M
         } else {
122
406k
            bandE[i+c*m->nbEBands] = EPSILON;
123
406k
         }
124
2.51M
      }
125
164k
   } while (++c<C);
126
122k
}
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
88.7k
{
131
88.7k
   int i, c, N;
132
88.7k
   const opus_int16 *eBands = m->eBands;
133
88.7k
   N = M*m->shortMdctSize;
134
120k
   c=0; do {
135
1.78M
      i=0; do {
136
1.78M
         int j,shift;
137
1.78M
         opus_val32 E;
138
1.78M
         opus_val32 g;
139
1.78M
         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
1.78M
         if (E < 10) E += EPSILON;
143
1.78M
         shift = 30-celt_zlog2(E);
144
1.78M
         E = SHL32(E, shift);
145
1.78M
         g = celt_rcp_norm32(E);
146
16.1M
         j=M*eBands[i]; do {
147
16.1M
            X[j+c*N] = PSHR32(MULT32_32_Q31(g, SHL32(freq[j+c*N], shift)), 30-NORM_SHIFT);
148
16.1M
         } while (++j<M*eBands[i+1]);
149
1.78M
      } while (++i<end);
150
120k
   } while (++c<C);
151
88.7k
}
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
135k
{
157
135k
   int i, c, N;
158
135k
   const opus_int16 *eBands = m->eBands;
159
135k
   N = m->shortMdctSize<<LM;
160
190k
   c=0; do {
161
3.08M
      for (i=0;i<end;i++)
162
2.89M
      {
163
2.89M
         opus_val32 sum;
164
2.89M
         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
2.89M
         bandE[i+c*m->nbEBands] = celt_sqrt(sum);
166
         /*printf ("%f ", bandE[i+c*m->nbEBands]);*/
167
2.89M
      }
168
190k
   } while (++c<C);
169
   /*printf ("\n");*/
170
135k
}
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
107k
{
175
107k
   int i, c, N;
176
107k
   const opus_int16 *eBands = m->eBands;
177
107k
   N = M*m->shortMdctSize;
178
150k
   c=0; do {
179
2.39M
      for (i=0;i<end;i++)
180
2.24M
      {
181
2.24M
         int j;
182
2.24M
         opus_val16 g = 1.f/(1e-27f+bandE[i+c*m->nbEBands]);
183
26.0M
         for (j=M*eBands[i];j<M*eBands[i+1];j++)
184
23.7M
            X[j+c*N] = freq[j+c*N]*g;
185
2.24M
      }
186
150k
   } while (++c<C);
187
107k
}
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
454k
{
196
454k
   int i, N;
197
454k
   int bound;
198
454k
   celt_sig * OPUS_RESTRICT f;
199
454k
   const celt_norm * OPUS_RESTRICT x;
200
454k
   const opus_int16 *eBands = m->eBands;
201
454k
   N = M*m->shortMdctSize;
202
454k
   bound = M*eBands[end];
203
454k
   if (downsample!=1)
204
289k
      bound = IMIN(bound, N/downsample);
205
454k
   if (silence)
206
60.2k
   {
207
60.2k
      bound = 0;
208
60.2k
      start = end = 0;
209
60.2k
   }
210
454k
   f = freq;
211
454k
   x = X+M*eBands[start];
212
454k
   if (start != 0)
213
64.3k
   {
214
11.7M
      for (i=0;i<M*eBands[start];i++)
215
11.6M
         *f++ = 0;
216
390k
   } else {
217
390k
      f += M*eBands[start];
218
390k
   }
219
6.40M
   for (i=start;i<end;i++)
220
5.95M
   {
221
5.95M
      int j, band_end;
222
5.95M
      opus_val32 g;
223
5.95M
      celt_glog lg;
224
#ifdef FIXED_POINT
225
      int shift;
226
#endif
227
5.95M
      j=M*eBands[i];
228
5.95M
      band_end = M*eBands[i+1];
229
5.95M
      lg = ADD32(bandLogE[i], SHL32((opus_val32)eMeans[i],DB_SHIFT-4));
230
#ifndef FIXED_POINT
231
2.57M
      g = celt_exp2_db(MIN32(32.f, lg));
232
#else
233
      /* Handle the integer part of the log energy */
234
3.37M
      shift = 17-(lg>>DB_SHIFT);
235
3.37M
      if (shift>=31)
236
60.0k
      {
237
60.0k
         shift=0;
238
60.0k
         g=0;
239
3.31M
      } else {
240
         /* Handle the fractional part. */
241
3.31M
         g = SHL32(celt_exp2_db_frac((lg&((1<<DB_SHIFT)-1))), 2);
242
3.31M
      }
243
      /* Handle extreme gains with negative shift. */
244
3.37M
      if (shift<0)
245
42.6k
      {
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
42.6k
         g = 2147483647;
250
42.6k
         shift = 0;
251
42.6k
      }
252
#endif
253
61.7M
      do {
254
61.7M
         *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift);
255
61.7M
         x++;
256
61.7M
      } while (++j<band_end);
257
5.95M
   }
258
454k
   celt_assert(start <= end);
259
454k
   OPUS_CLEAR(&freq[bound], N-bound);
260
454k
}
denormalise_bands
Line
Count
Source
195
244k
{
196
244k
   int i, N;
197
244k
   int bound;
198
244k
   celt_sig * OPUS_RESTRICT f;
199
244k
   const celt_norm * OPUS_RESTRICT x;
200
244k
   const opus_int16 *eBands = m->eBands;
201
244k
   N = M*m->shortMdctSize;
202
244k
   bound = M*eBands[end];
203
244k
   if (downsample!=1)
204
148k
      bound = IMIN(bound, N/downsample);
205
244k
   if (silence)
206
26.1k
   {
207
26.1k
      bound = 0;
208
26.1k
      start = end = 0;
209
26.1k
   }
210
244k
   f = freq;
211
244k
   x = X+M*eBands[start];
212
244k
   if (start != 0)
213
31.7k
   {
214
5.69M
      for (i=0;i<M*eBands[start];i++)
215
5.66M
         *f++ = 0;
216
212k
   } else {
217
212k
      f += M*eBands[start];
218
212k
   }
219
3.62M
   for (i=start;i<end;i++)
220
3.37M
   {
221
3.37M
      int j, band_end;
222
3.37M
      opus_val32 g;
223
3.37M
      celt_glog lg;
224
3.37M
#ifdef FIXED_POINT
225
3.37M
      int shift;
226
3.37M
#endif
227
3.37M
      j=M*eBands[i];
228
3.37M
      band_end = M*eBands[i+1];
229
3.37M
      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.37M
      shift = 17-(lg>>DB_SHIFT);
235
3.37M
      if (shift>=31)
236
60.0k
      {
237
60.0k
         shift=0;
238
60.0k
         g=0;
239
3.31M
      } else {
240
         /* Handle the fractional part. */
241
3.31M
         g = SHL32(celt_exp2_db_frac((lg&((1<<DB_SHIFT)-1))), 2);
242
3.31M
      }
243
      /* Handle extreme gains with negative shift. */
244
3.37M
      if (shift<0)
245
42.6k
      {
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
42.6k
         g = 2147483647;
250
42.6k
         shift = 0;
251
42.6k
      }
252
3.37M
#endif
253
33.1M
      do {
254
33.1M
         *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift);
255
33.1M
         x++;
256
33.1M
      } while (++j<band_end);
257
3.37M
   }
258
244k
   celt_assert(start <= end);
259
244k
   OPUS_CLEAR(&freq[bound], N-bound);
260
244k
}
denormalise_bands
Line
Count
Source
195
210k
{
196
210k
   int i, N;
197
210k
   int bound;
198
210k
   celt_sig * OPUS_RESTRICT f;
199
210k
   const celt_norm * OPUS_RESTRICT x;
200
210k
   const opus_int16 *eBands = m->eBands;
201
210k
   N = M*m->shortMdctSize;
202
210k
   bound = M*eBands[end];
203
210k
   if (downsample!=1)
204
140k
      bound = IMIN(bound, N/downsample);
205
210k
   if (silence)
206
34.1k
   {
207
34.1k
      bound = 0;
208
34.1k
      start = end = 0;
209
34.1k
   }
210
210k
   f = freq;
211
210k
   x = X+M*eBands[start];
212
210k
   if (start != 0)
213
32.6k
   {
214
6.05M
      for (i=0;i<M*eBands[start];i++)
215
6.02M
         *f++ = 0;
216
177k
   } else {
217
177k
      f += M*eBands[start];
218
177k
   }
219
2.78M
   for (i=start;i<end;i++)
220
2.57M
   {
221
2.57M
      int j, band_end;
222
2.57M
      opus_val32 g;
223
2.57M
      celt_glog lg;
224
#ifdef FIXED_POINT
225
      int shift;
226
#endif
227
2.57M
      j=M*eBands[i];
228
2.57M
      band_end = M*eBands[i+1];
229
2.57M
      lg = ADD32(bandLogE[i], SHL32((opus_val32)eMeans[i],DB_SHIFT-4));
230
2.57M
#ifndef FIXED_POINT
231
2.57M
      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.6M
      do {
254
28.6M
         *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift);
255
28.6M
         x++;
256
28.6M
      } while (++j<band_end);
257
2.57M
   }
258
210k
   celt_assert(start <= end);
259
210k
   OPUS_CLEAR(&freq[bound], N-bound);
260
210k
}
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.53k
{
267
6.53k
   int c, i, j, k;
268
112k
   for (i=start;i<end;i++)
269
105k
   {
270
105k
      int N0;
271
105k
      opus_val16 thresh, sqrt_1;
272
105k
      int depth;
273
#ifdef FIXED_POINT
274
      int shift;
275
      opus_val32 thresh32;
276
#endif
277
278
105k
      N0 = m->eBands[i+1]-m->eBands[i];
279
      /* depth in 1/8 bits */
280
105k
      celt_sig_assert(pulses[i]>=0);
281
105k
      depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i]))>>LM;
282
283
#ifdef FIXED_POINT
284
54.3k
      thresh32 = SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1);
285
54.3k
      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.3k
         t = SHL32(t, (7-shift)<<1);
291
         sqrt_1 = celt_rsqrt_norm(t);
292
      }
293
#else
294
      thresh = .5f*celt_exp2(-.125f*depth);
295
51.1k
      sqrt_1 = celt_rsqrt(N0<<LM);
296
#endif
297
298
105k
      c=0; do
299
185k
      {
300
185k
         celt_norm *X;
301
185k
         celt_glog prev1;
302
185k
         celt_glog prev2;
303
185k
         opus_val32 Ediff;
304
185k
         celt_norm r;
305
185k
         int renormalize=0;
306
185k
         prev1 = prev1logE[c*m->nbEBands+i];
307
185k
         prev2 = prev2logE[c*m->nbEBands+i];
308
185k
         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
185k
         Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2);
314
185k
         Ediff = MAX32(0, Ediff);
315
316
#ifdef FIXED_POINT
317
94.0k
         if (Ediff < GCONST(16.f))
318
16.3k
         {
319
16.3k
            opus_val32 r32 = SHR32(celt_exp2_db(-Ediff),1);
320
16.3k
            r = 2*MIN16(16383,r32);
321
77.7k
         } else {
322
77.7k
            r = 0;
323
77.7k
         }
324
94.0k
         if (LM==3)
325
17.8k
            r = MULT16_16_Q14(23170, MIN32(23169, r));
326
94.0k
         r = SHR16(MIN16(thresh, r),1);
327
94.0k
         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
91.0k
         r = 2.f*celt_exp2_db(-Ediff);
332
91.0k
         if (LM==3)
333
15.4k
            r *= 1.41421356f;
334
91.0k
         r = MIN16(thresh, r);
335
         r = r*sqrt_1;
336
#endif
337
185k
         X = X_+c*size+(m->eBands[i]<<LM);
338
1.05M
         for (k=0;k<1<<LM;k++)
339
873k
         {
340
            /* Detect collapse */
341
873k
            if (!(collapse_masks[i*C+c]&1<<k))
342
97.8k
            {
343
               /* Fill with noise */
344
381k
               for (j=0;j<N0;j++)
345
283k
               {
346
283k
                  seed = celt_lcg_rand(seed);
347
283k
                  X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
348
283k
               }
349
97.8k
               renormalize = 1;
350
97.8k
            }
351
873k
         }
352
         /* We just added some energy, so we need to renormalise */
353
185k
         if (renormalize)
354
36.1k
            renormalise_vector(X, N0<<LM, Q31ONE, arch);
355
185k
      } while (++c<C);
356
105k
   }
357
6.53k
}
anti_collapse
Line
Count
Source
266
3.38k
{
267
3.38k
   int c, i, j, k;
268
57.7k
   for (i=start;i<end;i++)
269
54.3k
   {
270
54.3k
      int N0;
271
54.3k
      opus_val16 thresh, sqrt_1;
272
54.3k
      int depth;
273
54.3k
#ifdef FIXED_POINT
274
54.3k
      int shift;
275
54.3k
      opus_val32 thresh32;
276
54.3k
#endif
277
278
54.3k
      N0 = m->eBands[i+1]-m->eBands[i];
279
      /* depth in 1/8 bits */
280
54.3k
      celt_sig_assert(pulses[i]>=0);
281
54.3k
      depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i]))>>LM;
282
283
54.3k
#ifdef FIXED_POINT
284
54.3k
      thresh32 = SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1);
285
54.3k
      thresh = MULT16_32_Q15(QCONST16(0.5f, 15), MIN32(32767,thresh32));
286
54.3k
      {
287
54.3k
         opus_val32 t;
288
54.3k
         t = N0<<LM;
289
54.3k
         shift = celt_ilog2(t)>>1;
290
54.3k
         t = SHL32(t, (7-shift)<<1);
291
54.3k
         sqrt_1 = celt_rsqrt_norm(t);
292
54.3k
      }
293
#else
294
      thresh = .5f*celt_exp2(-.125f*depth);
295
      sqrt_1 = celt_rsqrt(N0<<LM);
296
#endif
297
298
54.3k
      c=0; do
299
94.0k
      {
300
94.0k
         celt_norm *X;
301
94.0k
         celt_glog prev1;
302
94.0k
         celt_glog prev2;
303
94.0k
         opus_val32 Ediff;
304
94.0k
         celt_norm r;
305
94.0k
         int renormalize=0;
306
94.0k
         prev1 = prev1logE[c*m->nbEBands+i];
307
94.0k
         prev2 = prev2logE[c*m->nbEBands+i];
308
94.0k
         if (!encode && C==1)
309
14.7k
         {
310
14.7k
            prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]);
311
14.7k
            prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]);
312
14.7k
         }
313
94.0k
         Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2);
314
94.0k
         Ediff = MAX32(0, Ediff);
315
316
94.0k
#ifdef FIXED_POINT
317
94.0k
         if (Ediff < GCONST(16.f))
318
16.3k
         {
319
16.3k
            opus_val32 r32 = SHR32(celt_exp2_db(-Ediff),1);
320
16.3k
            r = 2*MIN16(16383,r32);
321
77.7k
         } else {
322
77.7k
            r = 0;
323
77.7k
         }
324
94.0k
         if (LM==3)
325
17.8k
            r = MULT16_16_Q14(23170, MIN32(23169, r));
326
94.0k
         r = SHR16(MIN16(thresh, r),1);
327
94.0k
         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.0k
         X = X_+c*size+(m->eBands[i]<<LM);
338
541k
         for (k=0;k<1<<LM;k++)
339
447k
         {
340
            /* Detect collapse */
341
447k
            if (!(collapse_masks[i*C+c]&1<<k))
342
55.7k
            {
343
               /* Fill with noise */
344
217k
               for (j=0;j<N0;j++)
345
162k
               {
346
162k
                  seed = celt_lcg_rand(seed);
347
162k
                  X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
348
162k
               }
349
55.7k
               renormalize = 1;
350
55.7k
            }
351
447k
         }
352
         /* We just added some energy, so we need to renormalise */
353
94.0k
         if (renormalize)
354
19.7k
            renormalise_vector(X, N0<<LM, Q31ONE, arch);
355
94.0k
      } while (++c<C);
356
54.3k
   }
357
3.38k
}
anti_collapse
Line
Count
Source
266
3.14k
{
267
3.14k
   int c, i, j, k;
268
54.2k
   for (i=start;i<end;i++)
269
51.1k
   {
270
51.1k
      int N0;
271
51.1k
      opus_val16 thresh, sqrt_1;
272
51.1k
      int depth;
273
#ifdef FIXED_POINT
274
      int shift;
275
      opus_val32 thresh32;
276
#endif
277
278
51.1k
      N0 = m->eBands[i+1]-m->eBands[i];
279
      /* depth in 1/8 bits */
280
51.1k
      celt_sig_assert(pulses[i]>=0);
281
51.1k
      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
51.1k
      thresh = .5f*celt_exp2(-.125f*depth);
295
51.1k
      sqrt_1 = celt_rsqrt(N0<<LM);
296
51.1k
#endif
297
298
51.1k
      c=0; do
299
91.0k
      {
300
91.0k
         celt_norm *X;
301
91.0k
         celt_glog prev1;
302
91.0k
         celt_glog prev2;
303
91.0k
         opus_val32 Ediff;
304
91.0k
         celt_norm r;
305
91.0k
         int renormalize=0;
306
91.0k
         prev1 = prev1logE[c*m->nbEBands+i];
307
91.0k
         prev2 = prev2logE[c*m->nbEBands+i];
308
91.0k
         if (!encode && C==1)
309
11.2k
         {
310
11.2k
            prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]);
311
11.2k
            prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]);
312
11.2k
         }
313
91.0k
         Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2);
314
91.0k
         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
91.0k
         r = 2.f*celt_exp2_db(-Ediff);
332
91.0k
         if (LM==3)
333
15.4k
            r *= 1.41421356f;
334
91.0k
         r = MIN16(thresh, r);
335
91.0k
         r = r*sqrt_1;
336
91.0k
#endif
337
91.0k
         X = X_+c*size+(m->eBands[i]<<LM);
338
517k
         for (k=0;k<1<<LM;k++)
339
426k
         {
340
            /* Detect collapse */
341
426k
            if (!(collapse_masks[i*C+c]&1<<k))
342
42.0k
            {
343
               /* Fill with noise */
344
163k
               for (j=0;j<N0;j++)
345
121k
               {
346
121k
                  seed = celt_lcg_rand(seed);
347
121k
                  X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
348
121k
               }
349
42.0k
               renormalize = 1;
350
42.0k
            }
351
426k
         }
352
         /* We just added some energy, so we need to renormalise */
353
91.0k
         if (renormalize)
354
16.3k
            renormalise_vector(X, N0<<LM, Q31ONE, arch);
355
91.0k
      } while (++c<C);
356
51.1k
   }
357
3.14k
}
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
309k
{
368
309k
   celt_ener minE;
369
#ifdef FIXED_POINT
370
   int shift;
371
#endif
372
309k
   minE = MIN32(Ex, Ey);
373
   /* Adjustment to make the weights a bit more conservative. */
374
309k
   Ex = ADD32(Ex, minE/3);
375
309k
   Ey = ADD32(Ey, minE/3);
376
#ifdef FIXED_POINT
377
150k
   shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
378
#endif
379
309k
   w[0] = VSHR32(Ex, shift);
380
309k
   w[1] = VSHR32(Ey, shift);
381
309k
}
bands.c:compute_channel_weights
Line
Count
Source
367
150k
{
368
150k
   celt_ener minE;
369
150k
#ifdef FIXED_POINT
370
150k
   int shift;
371
150k
#endif
372
150k
   minE = MIN32(Ex, Ey);
373
   /* Adjustment to make the weights a bit more conservative. */
374
150k
   Ex = ADD32(Ex, minE/3);
375
150k
   Ey = ADD32(Ey, minE/3);
376
150k
#ifdef FIXED_POINT
377
150k
   shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
378
150k
#endif
379
150k
   w[0] = VSHR32(Ex, shift);
380
150k
   w[1] = VSHR32(Ey, shift);
381
150k
}
bands.c:compute_channel_weights
Line
Count
Source
367
158k
{
368
158k
   celt_ener minE;
369
#ifdef FIXED_POINT
370
   int shift;
371
#endif
372
158k
   minE = MIN32(Ex, Ey);
373
   /* Adjustment to make the weights a bit more conservative. */
374
158k
   Ex = ADD32(Ex, minE/3);
375
158k
   Ey = ADD32(Ey, minE/3);
376
#ifdef FIXED_POINT
377
   shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14;
378
#endif
379
158k
   w[0] = VSHR32(Ex, shift);
380
158k
   w[1] = VSHR32(Ey, shift);
381
158k
}
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
451k
{
385
451k
   int i = bandID;
386
451k
   int j;
387
451k
   opus_val16 a1, a2;
388
451k
   opus_val16 left, right;
389
451k
   opus_val16 norm;
390
#ifdef FIXED_POINT
391
175k
   int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
392
#endif
393
451k
   left = VSHR32(bandE[i],shift);
394
451k
   right = VSHR32(bandE[i+m->nbEBands],shift);
395
451k
   norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
396
#ifdef FIXED_POINT
397
175k
   left = MIN32(left, norm-1);
398
175k
   right = MIN32(right, norm-1);
399
#endif
400
451k
   a1 = DIV32_16(SHL32(EXTEND32(left),15),norm);
401
451k
   a2 = DIV32_16(SHL32(EXTEND32(right),15),norm);
402
6.95M
   for (j=0;j<N;j++)
403
6.50M
   {
404
6.50M
      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
6.50M
   }
407
451k
}
bands.c:intensity_stereo
Line
Count
Source
384
175k
{
385
175k
   int i = bandID;
386
175k
   int j;
387
175k
   opus_val16 a1, a2;
388
175k
   opus_val16 left, right;
389
175k
   opus_val16 norm;
390
175k
#ifdef FIXED_POINT
391
175k
   int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
392
175k
#endif
393
175k
   left = VSHR32(bandE[i],shift);
394
175k
   right = VSHR32(bandE[i+m->nbEBands],shift);
395
175k
   norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
396
175k
#ifdef FIXED_POINT
397
175k
   left = MIN32(left, norm-1);
398
175k
   right = MIN32(right, norm-1);
399
175k
#endif
400
175k
   a1 = DIV32_16(SHL32(EXTEND32(left),15),norm);
401
175k
   a2 = DIV32_16(SHL32(EXTEND32(right),15),norm);
402
2.58M
   for (j=0;j<N;j++)
403
2.40M
   {
404
2.40M
      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
2.40M
   }
407
175k
}
bands.c:intensity_stereo
Line
Count
Source
384
276k
{
385
276k
   int i = bandID;
386
276k
   int j;
387
276k
   opus_val16 a1, a2;
388
276k
   opus_val16 left, right;
389
276k
   opus_val16 norm;
390
#ifdef FIXED_POINT
391
   int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13;
392
#endif
393
276k
   left = VSHR32(bandE[i],shift);
394
276k
   right = VSHR32(bandE[i+m->nbEBands],shift);
395
276k
   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
276k
   a1 = DIV32_16(SHL32(EXTEND32(left),15),norm);
401
276k
   a2 = DIV32_16(SHL32(EXTEND32(right),15),norm);
402
4.37M
   for (j=0;j<N;j++)
403
4.09M
   {
404
4.09M
      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.09M
   }
407
276k
}
408
409
static void stereo_split(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, int N)
410
932k
{
411
932k
   int j;
412
8.52M
   for (j=0;j<N;j++)
413
7.58M
   {
414
7.58M
      opus_val32 r, l;
415
7.58M
      l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]);
416
7.58M
      r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]);
417
7.58M
      X[j] = ADD32(l, r);
418
7.58M
      Y[j] = SUB32(r, l);
419
7.58M
   }
420
932k
}
bands.c:stereo_split
Line
Count
Source
410
466k
{
411
466k
   int j;
412
4.26M
   for (j=0;j<N;j++)
413
3.79M
   {
414
3.79M
      opus_val32 r, l;
415
3.79M
      l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]);
416
3.79M
      r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]);
417
3.79M
      X[j] = ADD32(l, r);
418
3.79M
      Y[j] = SUB32(r, l);
419
3.79M
   }
420
466k
}
bands.c:stereo_split
Line
Count
Source
410
466k
{
411
466k
   int j;
412
4.26M
   for (j=0;j<N;j++)
413
3.79M
   {
414
3.79M
      opus_val32 r, l;
415
3.79M
      l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]);
416
3.79M
      r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]);
417
3.79M
      X[j] = ADD32(l, r);
418
3.79M
      Y[j] = SUB32(r, l);
419
3.79M
   }
420
466k
}
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.43M
{
424
1.43M
   int j;
425
1.43M
   opus_val32 xp=0, side=0;
426
1.43M
   opus_val32 El, Er;
427
#ifdef FIXED_POINT
428
   int kl, kr;
429
#endif
430
1.43M
   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.43M
   xp = celt_inner_prod_norm_shift(Y, X, N, arch);
434
1.43M
   side = celt_inner_prod_norm_shift(Y, Y, N, arch);
435
   /* Compensating for the mid normalization */
436
1.43M
   xp = MULT32_32_Q31(mid, xp);
437
   /* mid and side are in Q15, not Q14 like X and Y */
438
1.43M
   El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp;
439
1.43M
   Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp;
440
1.43M
   if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
441
2.81k
   {
442
2.81k
      OPUS_COPY(Y, X, N);
443
2.81k
      return;
444
2.81k
   }
445
446
#ifdef FIXED_POINT
447
776k
   kl = celt_ilog2(El)>>1;
448
776k
   kr = celt_ilog2(Er)>>1;
449
776k
#endif
450
1.43M
   t = VSHR32(El, (kl<<1)-29);
451
654k
   lgain = celt_rsqrt_norm32(t);
452
1.43M
   t = VSHR32(Er, (kr<<1)-29);
453
654k
   rgain = celt_rsqrt_norm32(t);
454
455
#ifdef FIXED_POINT
456
776k
   if (kl < 7)
457
0
      kl = 7;
458
776k
   if (kr < 7)
459
0
      kr = 7;
460
#endif
461
462
26.6M
   for (j=0;j<N;j++)
463
25.2M
   {
464
25.2M
      celt_norm r, l;
465
      /* Apply mid scaling (side is already scaled) */
466
25.2M
      l = MULT32_32_Q31(mid, X[j]);
467
25.2M
      r = Y[j];
468
25.2M
      X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15);
469
25.2M
      Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15);
470
25.2M
   }
471
654k
}
bands.c:stereo_merge
Line
Count
Source
423
778k
{
424
778k
   int j;
425
778k
   opus_val32 xp=0, side=0;
426
778k
   opus_val32 El, Er;
427
778k
#ifdef FIXED_POINT
428
778k
   int kl, kr;
429
778k
#endif
430
778k
   opus_val32 t, lgain, rgain;
431
432
   /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
433
778k
   xp = celt_inner_prod_norm_shift(Y, X, N, arch);
434
778k
   side = celt_inner_prod_norm_shift(Y, Y, N, arch);
435
   /* Compensating for the mid normalization */
436
778k
   xp = MULT32_32_Q31(mid, xp);
437
   /* mid and side are in Q15, not Q14 like X and Y */
438
778k
   El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp;
439
778k
   Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp;
440
778k
   if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
441
1.66k
   {
442
1.66k
      OPUS_COPY(Y, X, N);
443
1.66k
      return;
444
1.66k
   }
445
446
776k
#ifdef FIXED_POINT
447
776k
   kl = celt_ilog2(El)>>1;
448
776k
   kr = celt_ilog2(Er)>>1;
449
776k
#endif
450
776k
   t = VSHR32(El, (kl<<1)-29);
451
776k
   lgain = celt_rsqrt_norm32(t);
452
776k
   t = VSHR32(Er, (kr<<1)-29);
453
776k
   rgain = celt_rsqrt_norm32(t);
454
455
776k
#ifdef FIXED_POINT
456
776k
   if (kl < 7)
457
0
      kl = 7;
458
776k
   if (kr < 7)
459
0
      kr = 7;
460
776k
#endif
461
462
14.4M
   for (j=0;j<N;j++)
463
13.6M
   {
464
13.6M
      celt_norm r, l;
465
      /* Apply mid scaling (side is already scaled) */
466
13.6M
      l = MULT32_32_Q31(mid, X[j]);
467
13.6M
      r = Y[j];
468
13.6M
      X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15);
469
13.6M
      Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15);
470
13.6M
   }
471
776k
}
bands.c:stereo_merge
Line
Count
Source
423
655k
{
424
655k
   int j;
425
655k
   opus_val32 xp=0, side=0;
426
655k
   opus_val32 El, Er;
427
#ifdef FIXED_POINT
428
   int kl, kr;
429
#endif
430
655k
   opus_val32 t, lgain, rgain;
431
432
   /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
433
655k
   xp = celt_inner_prod_norm_shift(Y, X, N, arch);
434
655k
   side = celt_inner_prod_norm_shift(Y, Y, N, arch);
435
   /* Compensating for the mid normalization */
436
655k
   xp = MULT32_32_Q31(mid, xp);
437
   /* mid and side are in Q15, not Q14 like X and Y */
438
655k
   El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp;
439
655k
   Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp;
440
655k
   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
654k
   t = VSHR32(El, (kl<<1)-29);
451
654k
   lgain = celt_rsqrt_norm32(t);
452
654k
   t = VSHR32(Er, (kr<<1)-29);
453
654k
   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.2M
   for (j=0;j<N;j++)
463
11.5M
   {
464
11.5M
      celt_norm r, l;
465
      /* Apply mid scaling (side is already scaled) */
466
11.5M
      l = MULT32_32_Q31(mid, X[j]);
467
11.5M
      r = Y[j];
468
11.5M
      X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15);
469
11.5M
      Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15);
470
11.5M
   }
471
654k
}
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
111k
{
478
111k
   int i, c, N0;
479
111k
   int sum = 0, nbBands=0;
480
111k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
481
111k
   int decision;
482
111k
   int hf_sum=0;
483
484
111k
   celt_assert(end>0);
485
486
111k
   N0 = M*m->shortMdctSize;
487
488
111k
   if (M*(eBands[end]-eBands[end-1]) <= 8)
489
69.8k
      return SPREAD_NONE;
490
58.9k
   c=0; do {
491
1.03M
      for (i=0;i<end;i++)
492
974k
      {
493
974k
         int j, N, tmp=0;
494
974k
         int tcount[3] = {0,0,0};
495
974k
         const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
496
974k
         N = M*(eBands[i+1]-eBands[i]);
497
974k
         if (N<=8)
498
698k
            continue;
499
         /* Compute rough CDF of |x[j]| */
500
7.12M
         for (j=0;j<N;j++)
501
6.85M
         {
502
6.85M
            opus_val32 x2N; /* Q13 */
503
504
6.85M
            x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N);
505
6.85M
            if (x2N < QCONST16(0.25f,13))
506
3.71M
               tcount[0]++;
507
6.85M
            if (x2N < QCONST16(0.0625f,13))
508
2.76M
               tcount[1]++;
509
6.85M
            if (x2N < QCONST16(0.015625f,13))
510
2.14M
               tcount[2]++;
511
6.85M
         }
512
513
         /* Only include four last bands (8 kHz and up) */
514
275k
         if (i>m->nbEBands-4)
515
43.5k
            hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
516
275k
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
517
275k
         sum += tmp*spread_weight[i];
518
275k
         nbBands+=spread_weight[i];
519
275k
      }
520
58.9k
   } while (++c<C);
521
522
41.9k
   if (update_hf)
523
3.13k
   {
524
3.13k
      if (hf_sum)
525
1.87k
         hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
526
3.13k
      *hf_average = (*hf_average+hf_sum)>>1;
527
3.13k
      hf_sum = *hf_average;
528
3.13k
      if (*tapset_decision==2)
529
90
         hf_sum += 4;
530
3.04k
      else if (*tapset_decision==0)
531
2.92k
         hf_sum -= 4;
532
3.13k
      if (hf_sum > 22)
533
188
         *tapset_decision=2;
534
2.95k
      else if (hf_sum > 18)
535
194
         *tapset_decision=1;
536
2.75k
      else
537
2.75k
         *tapset_decision=0;
538
3.13k
   }
539
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
540
41.9k
   celt_assert(nbBands>0); /* end has to be non-zero */
541
41.9k
   celt_assert(sum>=0);
542
41.9k
   sum = celt_udiv((opus_int32)sum<<8, nbBands);
543
   /* Recursive averaging */
544
41.9k
   sum = (sum+*average)>>1;
545
41.9k
   *average = sum;
546
   /* Hysteresis */
547
41.9k
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
548
41.9k
   if (sum < 80)
549
1.47k
   {
550
1.47k
      decision = SPREAD_AGGRESSIVE;
551
40.4k
   } else if (sum < 256)
552
30.0k
   {
553
30.0k
      decision = SPREAD_NORMAL;
554
30.0k
   } else if (sum < 384)
555
3.98k
   {
556
3.98k
      decision = SPREAD_LIGHT;
557
6.40k
   } else {
558
6.40k
      decision = SPREAD_NONE;
559
6.40k
   }
560
#ifdef FUZZING
561
   decision = rand()&0x3;
562
   *tapset_decision=rand()%3;
563
#endif
564
41.9k
   return decision;
565
41.9k
}
spreading_decision
Line
Count
Source
477
55.8k
{
478
55.8k
   int i, c, N0;
479
55.8k
   int sum = 0, nbBands=0;
480
55.8k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
481
55.8k
   int decision;
482
55.8k
   int hf_sum=0;
483
484
55.8k
   celt_assert(end>0);
485
486
55.8k
   N0 = M*m->shortMdctSize;
487
488
55.8k
   if (M*(eBands[end]-eBands[end-1]) <= 8)
489
34.9k
      return SPREAD_NONE;
490
29.4k
   c=0; do {
491
516k
      for (i=0;i<end;i++)
492
487k
      {
493
487k
         int j, N, tmp=0;
494
487k
         int tcount[3] = {0,0,0};
495
487k
         const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
496
487k
         N = M*(eBands[i+1]-eBands[i]);
497
487k
         if (N<=8)
498
349k
            continue;
499
         /* Compute rough CDF of |x[j]| */
500
3.56M
         for (j=0;j<N;j++)
501
3.42M
         {
502
3.42M
            opus_val32 x2N; /* Q13 */
503
504
3.42M
            x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N);
505
3.42M
            if (x2N < QCONST16(0.25f,13))
506
1.85M
               tcount[0]++;
507
3.42M
            if (x2N < QCONST16(0.0625f,13))
508
1.38M
               tcount[1]++;
509
3.42M
            if (x2N < QCONST16(0.015625f,13))
510
1.07M
               tcount[2]++;
511
3.42M
         }
512
513
         /* Only include four last bands (8 kHz and up) */
514
137k
         if (i>m->nbEBands-4)
515
21.7k
            hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
516
137k
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
517
137k
         sum += tmp*spread_weight[i];
518
137k
         nbBands+=spread_weight[i];
519
137k
      }
520
29.4k
   } while (++c<C);
521
522
20.9k
   if (update_hf)
523
1.56k
   {
524
1.56k
      if (hf_sum)
525
935
         hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
526
1.56k
      *hf_average = (*hf_average+hf_sum)>>1;
527
1.56k
      hf_sum = *hf_average;
528
1.56k
      if (*tapset_decision==2)
529
45
         hf_sum += 4;
530
1.52k
      else if (*tapset_decision==0)
531
1.46k
         hf_sum -= 4;
532
1.56k
      if (hf_sum > 22)
533
94
         *tapset_decision=2;
534
1.47k
      else if (hf_sum > 18)
535
97
         *tapset_decision=1;
536
1.37k
      else
537
1.37k
         *tapset_decision=0;
538
1.56k
   }
539
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
540
20.9k
   celt_assert(nbBands>0); /* end has to be non-zero */
541
20.9k
   celt_assert(sum>=0);
542
20.9k
   sum = celt_udiv((opus_int32)sum<<8, nbBands);
543
   /* Recursive averaging */
544
20.9k
   sum = (sum+*average)>>1;
545
20.9k
   *average = sum;
546
   /* Hysteresis */
547
20.9k
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
548
20.9k
   if (sum < 80)
549
737
   {
550
737
      decision = SPREAD_AGGRESSIVE;
551
20.2k
   } else if (sum < 256)
552
15.0k
   {
553
15.0k
      decision = SPREAD_NORMAL;
554
15.0k
   } else if (sum < 384)
555
1.99k
   {
556
1.99k
      decision = SPREAD_LIGHT;
557
3.20k
   } else {
558
3.20k
      decision = SPREAD_NONE;
559
3.20k
   }
560
#ifdef FUZZING
561
   decision = rand()&0x3;
562
   *tapset_decision=rand()%3;
563
#endif
564
20.9k
   return decision;
565
20.9k
}
spreading_decision
Line
Count
Source
477
55.8k
{
478
55.8k
   int i, c, N0;
479
55.8k
   int sum = 0, nbBands=0;
480
55.8k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
481
55.8k
   int decision;
482
55.8k
   int hf_sum=0;
483
484
55.8k
   celt_assert(end>0);
485
486
55.8k
   N0 = M*m->shortMdctSize;
487
488
55.8k
   if (M*(eBands[end]-eBands[end-1]) <= 8)
489
34.9k
      return SPREAD_NONE;
490
29.4k
   c=0; do {
491
516k
      for (i=0;i<end;i++)
492
487k
      {
493
487k
         int j, N, tmp=0;
494
487k
         int tcount[3] = {0,0,0};
495
487k
         const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0;
496
487k
         N = M*(eBands[i+1]-eBands[i]);
497
487k
         if (N<=8)
498
349k
            continue;
499
         /* Compute rough CDF of |x[j]| */
500
3.56M
         for (j=0;j<N;j++)
501
3.42M
         {
502
3.42M
            opus_val32 x2N; /* Q13 */
503
504
3.42M
            x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N);
505
3.42M
            if (x2N < QCONST16(0.25f,13))
506
1.85M
               tcount[0]++;
507
3.42M
            if (x2N < QCONST16(0.0625f,13))
508
1.38M
               tcount[1]++;
509
3.42M
            if (x2N < QCONST16(0.015625f,13))
510
1.07M
               tcount[2]++;
511
3.42M
         }
512
513
         /* Only include four last bands (8 kHz and up) */
514
137k
         if (i>m->nbEBands-4)
515
21.7k
            hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N);
516
137k
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
517
137k
         sum += tmp*spread_weight[i];
518
137k
         nbBands+=spread_weight[i];
519
137k
      }
520
29.4k
   } while (++c<C);
521
522
20.9k
   if (update_hf)
523
1.56k
   {
524
1.56k
      if (hf_sum)
525
935
         hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end));
526
1.56k
      *hf_average = (*hf_average+hf_sum)>>1;
527
1.56k
      hf_sum = *hf_average;
528
1.56k
      if (*tapset_decision==2)
529
45
         hf_sum += 4;
530
1.52k
      else if (*tapset_decision==0)
531
1.46k
         hf_sum -= 4;
532
1.56k
      if (hf_sum > 22)
533
94
         *tapset_decision=2;
534
1.47k
      else if (hf_sum > 18)
535
97
         *tapset_decision=1;
536
1.37k
      else
537
1.37k
         *tapset_decision=0;
538
1.56k
   }
539
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
540
20.9k
   celt_assert(nbBands>0); /* end has to be non-zero */
541
20.9k
   celt_assert(sum>=0);
542
20.9k
   sum = celt_udiv((opus_int32)sum<<8, nbBands);
543
   /* Recursive averaging */
544
20.9k
   sum = (sum+*average)>>1;
545
20.9k
   *average = sum;
546
   /* Hysteresis */
547
20.9k
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
548
20.9k
   if (sum < 80)
549
737
   {
550
737
      decision = SPREAD_AGGRESSIVE;
551
20.2k
   } else if (sum < 256)
552
15.0k
   {
553
15.0k
      decision = SPREAD_NORMAL;
554
15.0k
   } else if (sum < 384)
555
1.99k
   {
556
1.99k
      decision = SPREAD_LIGHT;
557
3.20k
   } else {
558
3.20k
      decision = SPREAD_NONE;
559
3.20k
   }
560
#ifdef FUZZING
561
   decision = rand()&0x3;
562
   *tapset_decision=rand()%3;
563
#endif
564
20.9k
   return decision;
565
20.9k
}
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
2.48M
{
580
2.48M
   int i,j;
581
2.48M
   VARDECL(celt_norm, tmp);
582
2.48M
   int N;
583
2.48M
   SAVE_STACK;
584
2.48M
   N = N0*stride;
585
2.48M
   ALLOC(tmp, N, celt_norm);
586
2.48M
   celt_assert(stride>0);
587
2.48M
   if (hadamard)
588
488k
   {
589
488k
      const int *ordery = ordery_table+stride-2;
590
2.15M
      for (i=0;i<stride;i++)
591
1.67M
      {
592
7.95M
         for (j=0;j<N0;j++)
593
6.28M
            tmp[ordery[i]*N0+j] = X[j*stride+i];
594
1.67M
      }
595
1.99M
   } else {
596
16.4M
      for (i=0;i<stride;i++)
597
40.0M
         for (j=0;j<N0;j++)
598
25.6M
            tmp[i*N0+j] = X[j*stride+i];
599
1.99M
   }
600
2.48M
   OPUS_COPY(X, tmp, N);
601
2.48M
   RESTORE_STACK;
602
2.48M
}
603
604
static void interleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
605
1.25M
{
606
1.25M
   int i,j;
607
1.25M
   VARDECL(celt_norm, tmp);
608
1.25M
   int N;
609
1.25M
   SAVE_STACK;
610
1.25M
   N = N0*stride;
611
1.25M
   ALLOC(tmp, N, celt_norm);
612
1.25M
   if (hadamard)
613
454k
   {
614
454k
      const int *ordery = ordery_table+stride-2;
615
1.96M
      for (i=0;i<stride;i++)
616
7.50M
         for (j=0;j<N0;j++)
617
6.00M
            tmp[j*stride+i] = X[ordery[i]*N0+j];
618
803k
   } else {
619
6.21M
      for (i=0;i<stride;i++)
620
15.8M
         for (j=0;j<N0;j++)
621
10.3M
            tmp[j*stride+i] = X[i*N0+j];
622
803k
   }
623
1.25M
   OPUS_COPY(X, tmp, N);
624
1.25M
   RESTORE_STACK;
625
1.25M
}
626
627
void haar1(celt_norm *X, int N0, int stride)
628
14.8M
{
629
14.8M
   int i, j;
630
14.8M
   N0 >>= 1;
631
52.0M
   for (i=0;i<stride;i++)
632
161M
      for (j=0;j<N0;j++)
633
124M
      {
634
124M
         opus_val32 tmp1, tmp2;
635
124M
         tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]);
636
124M
         tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]);
637
124M
         X[stride*2*j+i] = ADD32(tmp1, tmp2);
638
124M
         X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2);
639
124M
      }
640
14.8M
}
haar1
Line
Count
Source
628
7.44M
{
629
7.44M
   int i, j;
630
7.44M
   N0 >>= 1;
631
26.0M
   for (i=0;i<stride;i++)
632
80.9M
      for (j=0;j<N0;j++)
633
62.3M
      {
634
62.3M
         opus_val32 tmp1, tmp2;
635
62.3M
         tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]);
636
62.3M
         tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]);
637
62.3M
         X[stride*2*j+i] = ADD32(tmp1, tmp2);
638
62.3M
         X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2);
639
62.3M
      }
640
7.44M
}
haar1
Line
Count
Source
628
7.44M
{
629
7.44M
   int i, j;
630
7.44M
   N0 >>= 1;
631
26.0M
   for (i=0;i<stride;i++)
632
80.9M
      for (j=0;j<N0;j++)
633
62.3M
      {
634
62.3M
         opus_val32 tmp1, tmp2;
635
62.3M
         tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]);
636
62.3M
         tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]);
637
62.3M
         X[stride*2*j+i] = ADD32(tmp1, tmp2);
638
62.3M
         X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2);
639
62.3M
      }
640
7.44M
}
641
642
static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo)
643
4.00M
{
644
4.00M
   static const opus_int16 exp2_table8[8] =
645
4.00M
      {16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048};
646
4.00M
   int qn, qb;
647
4.00M
   int N2 = 2*N-1;
648
4.00M
   if (stereo && N==2)
649
546k
      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.00M
   qb = celt_sudiv(b+N2*offset, N2);
654
4.00M
   qb = IMIN(b-pulse_cap-(4<<BITRES), qb);
655
656
4.00M
   qb = IMIN(8<<BITRES, qb);
657
658
4.00M
   if (qb<(1<<BITRES>>1)) {
659
1.33M
      qn = 1;
660
2.66M
   } else {
661
2.66M
      qn = exp2_table8[qb&0x7]>>(14-(qb>>BITRES));
662
2.66M
      qn = (qn+1)>>1<<1;
663
2.66M
   }
664
4.00M
   celt_assert(qn <= 256);
665
4.00M
   return qn;
666
4.00M
}
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.00M
{
709
4.00M
   int qn;
710
4.00M
   int itheta=0;
711
4.00M
   int itheta_q30=0;
712
4.00M
   int delta;
713
4.00M
   int imid, iside;
714
4.00M
   int qalloc;
715
4.00M
   int pulse_cap;
716
4.00M
   int offset;
717
4.00M
   opus_int32 tell;
718
4.00M
   int inv=0;
719
4.00M
   int encode;
720
4.00M
   const CELTMode *m;
721
4.00M
   int i;
722
4.00M
   int intensity;
723
4.00M
   ec_ctx *ec;
724
4.00M
   const celt_ener *bandE;
725
726
4.00M
   encode = ctx->encode;
727
4.00M
   m = ctx->m;
728
4.00M
   i = ctx->i;
729
4.00M
   intensity = ctx->intensity;
730
4.00M
   ec = ctx->ec;
731
4.00M
   bandE = ctx->bandE;
732
733
   /* Decide on the resolution to give to the split parameter theta */
734
4.00M
   pulse_cap = m->logN[i]+LM*(1<<BITRES);
735
4.00M
   offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
736
4.00M
   qn = compute_qn(N, *b, offset, pulse_cap, stereo);
737
4.00M
   if (stereo && i>=intensity)
738
1.52M
      qn = 1;
739
4.00M
   if (encode)
740
1.96M
   {
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.96M
      itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch);
746
1.96M
      itheta = itheta_q30>>16;
747
1.96M
   }
748
4.00M
   tell = ec_tell_frac(ec);
749
4.00M
   if (qn!=1)
750
2.41M
   {
751
2.41M
      if (encode)
752
1.60M
      {
753
1.60M
         if (!stereo || ctx->theta_round == 0)
754
1.18M
         {
755
1.18M
            itheta = (itheta*(opus_int32)qn+8192)>>14;
756
1.18M
            if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
757
41.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
41.2k
               int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
762
41.2k
               imid = bitexact_cos((opus_int16)unquantized);
763
41.2k
               iside = bitexact_cos((opus_int16)(16384-unquantized));
764
41.2k
               delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
765
41.2k
               if (delta > *b)
766
329
                  itheta = qn;
767
40.9k
               else if (delta < -*b)
768
274
                  itheta = 0;
769
41.2k
            }
770
1.18M
         } else {
771
422k
            int down;
772
            /* Bias quantization towards itheta=0 and itheta=16384. */
773
422k
            int bias = itheta > 8192 ? 32767/qn : -32767/qn;
774
422k
            down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
775
422k
            if (ctx->theta_round < 0)
776
211k
               itheta = down;
777
211k
            else
778
211k
               itheta = down+1;
779
422k
         }
780
1.60M
      }
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
2.41M
      if (stereo && N>2)
784
468k
      {
785
468k
         int p0 = 3;
786
468k
         int x = itheta;
787
468k
         int x0 = qn/2;
788
468k
         int ft = p0*(x0+1) + x0;
789
         /* Use a probability of p0 up to itheta=8192 and then use 1 after */
790
468k
         if (encode)
791
406k
         {
792
406k
            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
406k
         } else {
794
61.8k
            int fs;
795
61.8k
            fs=ec_decode(ec,ft);
796
61.8k
            if (fs<(x0+1)*p0)
797
43.7k
               x=fs/p0;
798
18.1k
            else
799
18.1k
               x=x0+1+(fs-(x0+1)*p0);
800
61.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
61.8k
            itheta = x;
802
61.8k
         }
803
1.94M
      } else if (B0>1 || stereo) {
804
         /* Uniform pdf */
805
1.31M
         if (encode)
806
985k
            ec_enc_uint(ec, itheta, qn+1);
807
326k
         else
808
326k
            itheta = ec_dec_uint(ec, qn+1);
809
1.31M
      } else {
810
633k
         int fs=1, ft;
811
633k
         ft = ((qn>>1)+1)*((qn>>1)+1);
812
633k
         if (encode)
813
214k
         {
814
214k
            int fl;
815
816
214k
            fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
817
214k
            fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
818
214k
             ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
819
820
214k
            ec_encode(ec, fl, fl+fs, ft);
821
418k
         } else {
822
            /* Triangular pdf */
823
418k
            int fl=0;
824
418k
            int fm;
825
418k
            fm = ec_decode(ec, ft);
826
827
418k
            if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
828
213k
            {
829
213k
               itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
830
213k
               fs = itheta + 1;
831
213k
               fl = itheta*(itheta + 1)>>1;
832
213k
            }
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
418k
            ec_dec_update(ec, fl, fl+fs, ft);
842
418k
         }
843
633k
      }
844
2.41M
      celt_assert(itheta>=0);
845
2.41M
      itheta = celt_udiv((opus_int32)itheta*16384, qn);
846
#ifdef ENABLE_QEXT
847
1.58M
      *ext_b = IMIN(*ext_b, ctx->ext_total_bits - (opus_int32)ec_tell_frac(ctx->ext_ec));
848
1.58M
      if (*ext_b >= 2*N<<BITRES && ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec)-1 > 2<<BITRES) {
849
33.1k
         int extra_bits;
850
33.1k
         int ext_tell = ec_tell_frac(ctx->ext_ec);
851
33.1k
         extra_bits = IMIN(12, IMAX(2, celt_sudiv(*ext_b, (2*N-1)<<BITRES)));
852
33.1k
         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
33.1k
         } else {
859
33.1k
            itheta_q30 = ec_dec_uint(ctx->ext_ec, (1<<extra_bits)-1);
860
33.1k
         }
861
33.1k
         itheta_q30 -= (1<<(extra_bits-1))-1;
862
33.1k
         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
33.1k
         itheta_q30 = IMAX(0, IMIN(itheta_q30, 1073741824));
865
33.1k
         *ext_b -= ec_tell_frac(ctx->ext_ec) - ext_tell;
866
1.55M
      } else {
867
1.55M
         itheta_q30 = (opus_int32)itheta<<16;
868
1.55M
      }
869
#endif
870
2.41M
      if (encode && stereo)
871
561k
      {
872
561k
         if (itheta==0)
873
95.0k
            intensity_stereo(m, X, Y, bandE, i, N);
874
466k
         else
875
466k
            stereo_split(X, Y, N);
876
561k
      }
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
2.41M
   } else if (stereo) {
880
1.58M
      if (encode)
881
356k
      {
882
356k
         inv = itheta > 8192 && !ctx->disable_inv;
883
356k
         if (inv)
884
100k
         {
885
100k
            int j;
886
1.72M
            for (j=0;j<N;j++)
887
1.62M
               Y[j] = -Y[j];
888
100k
         }
889
356k
         intensity_stereo(m, X, Y, bandE, i, N);
890
356k
      }
891
1.58M
      if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
892
336k
      {
893
336k
         if (encode)
894
169k
            ec_enc_bit_logp(ec, inv, 2);
895
166k
         else
896
166k
            inv = ec_dec_bit_logp(ec, 2);
897
336k
      } else
898
1.25M
         inv = 0;
899
      /* inv flag override to avoid problems with downmixing. */
900
1.58M
      if (ctx->disable_inv)
901
845k
         inv = 0;
902
1.58M
      itheta = 0;
903
1.58M
      itheta_q30 = 0;
904
1.58M
   }
905
4.00M
   qalloc = ec_tell_frac(ec) - tell;
906
4.00M
   *b -= qalloc;
907
908
4.00M
   if (itheta == 0)
909
1.79M
   {
910
1.79M
      imid = 32767;
911
1.79M
      iside = 0;
912
1.79M
      *fill &= (1<<B)-1;
913
1.79M
      delta = -16384;
914
2.20M
   } else if (itheta == 16384)
915
239k
   {
916
239k
      imid = 0;
917
239k
      iside = 32767;
918
239k
      *fill &= ((1<<B)-1)<<B;
919
239k
      delta = 16384;
920
1.97M
   } else {
921
1.97M
      imid = bitexact_cos((opus_int16)itheta);
922
1.97M
      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.97M
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
926
1.97M
   }
927
928
4.00M
   sctx->inv = inv;
929
4.00M
   sctx->imid = imid;
930
4.00M
   sctx->iside = iside;
931
4.00M
   sctx->delta = delta;
932
4.00M
   sctx->itheta = itheta;
933
#ifdef ENABLE_QEXT
934
   sctx->itheta_q30 = itheta_q30;
935
#endif
936
4.00M
   sctx->qalloc = qalloc;
937
4.00M
}
bands.c:compute_theta
Line
Count
Source
708
1.59M
{
709
1.59M
   int qn;
710
1.59M
   int itheta=0;
711
1.59M
   int itheta_q30=0;
712
1.59M
   int delta;
713
1.59M
   int imid, iside;
714
1.59M
   int qalloc;
715
1.59M
   int pulse_cap;
716
1.59M
   int offset;
717
1.59M
   opus_int32 tell;
718
1.59M
   int inv=0;
719
1.59M
   int encode;
720
1.59M
   const CELTMode *m;
721
1.59M
   int i;
722
1.59M
   int intensity;
723
1.59M
   ec_ctx *ec;
724
1.59M
   const celt_ener *bandE;
725
726
1.59M
   encode = ctx->encode;
727
1.59M
   m = ctx->m;
728
1.59M
   i = ctx->i;
729
1.59M
   intensity = ctx->intensity;
730
1.59M
   ec = ctx->ec;
731
1.59M
   bandE = ctx->bandE;
732
733
   /* Decide on the resolution to give to the split parameter theta */
734
1.59M
   pulse_cap = m->logN[i]+LM*(1<<BITRES);
735
1.59M
   offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
736
1.59M
   qn = compute_qn(N, *b, offset, pulse_cap, stereo);
737
1.59M
   if (stereo && i>=intensity)
738
746k
      qn = 1;
739
1.59M
   if (encode)
740
625k
   {
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
625k
      itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch);
746
625k
      itheta = itheta_q30>>16;
747
625k
   }
748
1.59M
   tell = ec_tell_frac(ec);
749
1.59M
   if (qn!=1)
750
830k
   {
751
830k
      if (encode)
752
502k
      {
753
502k
         if (!stereo || ctx->theta_round == 0)
754
382k
         {
755
382k
            itheta = (itheta*(opus_int32)qn+8192)>>14;
756
382k
            if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
757
14.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
14.2k
               int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
762
14.2k
               imid = bitexact_cos((opus_int16)unquantized);
763
14.2k
               iside = bitexact_cos((opus_int16)(16384-unquantized));
764
14.2k
               delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
765
14.2k
               if (delta > *b)
766
144
                  itheta = qn;
767
14.0k
               else if (delta < -*b)
768
113
                  itheta = 0;
769
14.2k
            }
770
382k
         } else {
771
119k
            int down;
772
            /* Bias quantization towards itheta=0 and itheta=16384. */
773
119k
            int bias = itheta > 8192 ? 32767/qn : -32767/qn;
774
119k
            down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
775
119k
            if (ctx->theta_round < 0)
776
59.9k
               itheta = down;
777
59.9k
            else
778
59.9k
               itheta = down+1;
779
119k
         }
780
502k
      }
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
830k
      if (stereo && N>2)
784
142k
      {
785
142k
         int p0 = 3;
786
142k
         int x = itheta;
787
142k
         int x0 = qn/2;
788
142k
         int ft = p0*(x0+1) + x0;
789
         /* Use a probability of p0 up to itheta=8192 and then use 1 after */
790
142k
         if (encode)
791
117k
         {
792
117k
            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
117k
         } else {
794
25.2k
            int fs;
795
25.2k
            fs=ec_decode(ec,ft);
796
25.2k
            if (fs<(x0+1)*p0)
797
17.7k
               x=fs/p0;
798
7.54k
            else
799
7.54k
               x=x0+1+(fs-(x0+1)*p0);
800
25.2k
            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
25.2k
            itheta = x;
802
25.2k
         }
803
688k
      } else if (B0>1 || stereo) {
804
         /* Uniform pdf */
805
458k
         if (encode)
806
318k
            ec_enc_uint(ec, itheta, qn+1);
807
139k
         else
808
139k
            itheta = ec_dec_uint(ec, qn+1);
809
458k
      } else {
810
229k
         int fs=1, ft;
811
229k
         ft = ((qn>>1)+1)*((qn>>1)+1);
812
229k
         if (encode)
813
65.7k
         {
814
65.7k
            int fl;
815
816
65.7k
            fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
817
65.7k
            fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
818
65.7k
             ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
819
820
65.7k
            ec_encode(ec, fl, fl+fs, ft);
821
164k
         } else {
822
            /* Triangular pdf */
823
164k
            int fl=0;
824
164k
            int fm;
825
164k
            fm = ec_decode(ec, ft);
826
827
164k
            if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
828
83.0k
            {
829
83.0k
               itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
830
83.0k
               fs = itheta + 1;
831
83.0k
               fl = itheta*(itheta + 1)>>1;
832
83.0k
            }
833
81.1k
            else
834
81.1k
            {
835
81.1k
               itheta = (2*(qn + 1)
836
81.1k
                - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
837
81.1k
               fs = qn + 1 - itheta;
838
81.1k
               fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
839
81.1k
            }
840
841
164k
            ec_dec_update(ec, fl, fl+fs, ft);
842
164k
         }
843
229k
      }
844
830k
      celt_assert(itheta>=0);
845
830k
      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
830k
      if (encode && stereo)
871
161k
      {
872
161k
         if (itheta==0)
873
25.9k
            intensity_stereo(m, X, Y, bandE, i, N);
874
135k
         else
875
135k
            stereo_split(X, Y, N);
876
161k
      }
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
830k
   } else if (stereo) {
880
764k
      if (encode)
881
123k
      {
882
123k
         inv = itheta > 8192 && !ctx->disable_inv;
883
123k
         if (inv)
884
38.3k
         {
885
38.3k
            int j;
886
677k
            for (j=0;j<N;j++)
887
639k
               Y[j] = -Y[j];
888
38.3k
         }
889
123k
         intensity_stereo(m, X, Y, bandE, i, N);
890
123k
      }
891
764k
      if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
892
114k
      {
893
114k
         if (encode)
894
50.7k
            ec_enc_bit_logp(ec, inv, 2);
895
63.4k
         else
896
63.4k
            inv = ec_dec_bit_logp(ec, 2);
897
114k
      } else
898
650k
         inv = 0;
899
      /* inv flag override to avoid problems with downmixing. */
900
764k
      if (ctx->disable_inv)
901
397k
         inv = 0;
902
764k
      itheta = 0;
903
764k
      itheta_q30 = 0;
904
764k
   }
905
1.59M
   qalloc = ec_tell_frac(ec) - tell;
906
1.59M
   *b -= qalloc;
907
908
1.59M
   if (itheta == 0)
909
825k
   {
910
825k
      imid = 32767;
911
825k
      iside = 0;
912
825k
      *fill &= (1<<B)-1;
913
825k
      delta = -16384;
914
825k
   } else if (itheta == 16384)
915
80.9k
   {
916
80.9k
      imid = 0;
917
80.9k
      iside = 32767;
918
80.9k
      *fill &= ((1<<B)-1)<<B;
919
80.9k
      delta = 16384;
920
689k
   } else {
921
689k
      imid = bitexact_cos((opus_int16)itheta);
922
689k
      iside = bitexact_cos((opus_int16)(16384-itheta));
923
      /* This is the mid vs side allocation that minimizes squared error
924
         in that band. */
925
689k
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
926
689k
   }
927
928
1.59M
   sctx->inv = inv;
929
1.59M
   sctx->imid = imid;
930
1.59M
   sctx->iside = iside;
931
1.59M
   sctx->delta = delta;
932
1.59M
   sctx->itheta = itheta;
933
#ifdef ENABLE_QEXT
934
   sctx->itheta_q30 = itheta_q30;
935
#endif
936
1.59M
   sctx->qalloc = qalloc;
937
1.59M
}
bands.c:compute_theta
Line
Count
Source
708
2.40M
{
709
2.40M
   int qn;
710
2.40M
   int itheta=0;
711
2.40M
   int itheta_q30=0;
712
2.40M
   int delta;
713
2.40M
   int imid, iside;
714
2.40M
   int qalloc;
715
2.40M
   int pulse_cap;
716
2.40M
   int offset;
717
2.40M
   opus_int32 tell;
718
2.40M
   int inv=0;
719
2.40M
   int encode;
720
2.40M
   const CELTMode *m;
721
2.40M
   int i;
722
2.40M
   int intensity;
723
2.40M
   ec_ctx *ec;
724
2.40M
   const celt_ener *bandE;
725
726
2.40M
   encode = ctx->encode;
727
2.40M
   m = ctx->m;
728
2.40M
   i = ctx->i;
729
2.40M
   intensity = ctx->intensity;
730
2.40M
   ec = ctx->ec;
731
2.40M
   bandE = ctx->bandE;
732
733
   /* Decide on the resolution to give to the split parameter theta */
734
2.40M
   pulse_cap = m->logN[i]+LM*(1<<BITRES);
735
2.40M
   offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
736
2.40M
   qn = compute_qn(N, *b, offset, pulse_cap, stereo);
737
2.40M
   if (stereo && i>=intensity)
738
780k
      qn = 1;
739
2.40M
   if (encode)
740
1.33M
   {
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.33M
      itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch);
746
1.33M
      itheta = itheta_q30>>16;
747
1.33M
   }
748
2.40M
   tell = ec_tell_frac(ec);
749
2.40M
   if (qn!=1)
750
1.58M
   {
751
1.58M
      if (encode)
752
1.10M
      {
753
1.10M
         if (!stereo || ctx->theta_round == 0)
754
802k
         {
755
802k
            itheta = (itheta*(opus_int32)qn+8192)>>14;
756
802k
            if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn)
757
27.0k
            {
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.0k
               int unquantized = celt_udiv((opus_int32)itheta*16384, qn);
762
27.0k
               imid = bitexact_cos((opus_int16)unquantized);
763
27.0k
               iside = bitexact_cos((opus_int16)(16384-unquantized));
764
27.0k
               delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
765
27.0k
               if (delta > *b)
766
185
                  itheta = qn;
767
26.8k
               else if (delta < -*b)
768
161
                  itheta = 0;
769
27.0k
            }
770
802k
         } else {
771
303k
            int down;
772
            /* Bias quantization towards itheta=0 and itheta=16384. */
773
303k
            int bias = itheta > 8192 ? 32767/qn : -32767/qn;
774
303k
            down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14));
775
303k
            if (ctx->theta_round < 0)
776
151k
               itheta = down;
777
151k
            else
778
151k
               itheta = down+1;
779
303k
         }
780
1.10M
      }
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.58M
      if (stereo && N>2)
784
325k
      {
785
325k
         int p0 = 3;
786
325k
         int x = itheta;
787
325k
         int x0 = qn/2;
788
325k
         int ft = p0*(x0+1) + x0;
789
         /* Use a probability of p0 up to itheta=8192 and then use 1 after */
790
325k
         if (encode)
791
289k
         {
792
289k
            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
289k
         } else {
794
36.5k
            int fs;
795
36.5k
            fs=ec_decode(ec,ft);
796
36.5k
            if (fs<(x0+1)*p0)
797
25.9k
               x=fs/p0;
798
10.6k
            else
799
10.6k
               x=x0+1+(fs-(x0+1)*p0);
800
36.5k
            ec_dec_update(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft);
801
36.5k
            itheta = x;
802
36.5k
         }
803
1.25M
      } else if (B0>1 || stereo) {
804
         /* Uniform pdf */
805
854k
         if (encode)
806
666k
            ec_enc_uint(ec, itheta, qn+1);
807
187k
         else
808
187k
            itheta = ec_dec_uint(ec, qn+1);
809
854k
      } else {
810
403k
         int fs=1, ft;
811
403k
         ft = ((qn>>1)+1)*((qn>>1)+1);
812
403k
         if (encode)
813
149k
         {
814
149k
            int fl;
815
816
149k
            fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
817
149k
            fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
818
149k
             ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
819
820
149k
            ec_encode(ec, fl, fl+fs, ft);
821
254k
         } else {
822
            /* Triangular pdf */
823
254k
            int fl=0;
824
254k
            int fm;
825
254k
            fm = ec_decode(ec, ft);
826
827
254k
            if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
828
130k
            {
829
130k
               itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1;
830
130k
               fs = itheta + 1;
831
130k
               fl = itheta*(itheta + 1)>>1;
832
130k
            }
833
124k
            else
834
124k
            {
835
124k
               itheta = (2*(qn + 1)
836
124k
                - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1;
837
124k
               fs = qn + 1 - itheta;
838
124k
               fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
839
124k
            }
840
841
254k
            ec_dec_update(ec, fl, fl+fs, ft);
842
254k
         }
843
403k
      }
844
1.58M
      celt_assert(itheta>=0);
845
1.58M
      itheta = celt_udiv((opus_int32)itheta*16384, qn);
846
1.58M
#ifdef ENABLE_QEXT
847
1.58M
      *ext_b = IMIN(*ext_b, ctx->ext_total_bits - (opus_int32)ec_tell_frac(ctx->ext_ec));
848
1.58M
      if (*ext_b >= 2*N<<BITRES && ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec)-1 > 2<<BITRES) {
849
33.1k
         int extra_bits;
850
33.1k
         int ext_tell = ec_tell_frac(ctx->ext_ec);
851
33.1k
         extra_bits = IMIN(12, IMAX(2, celt_sudiv(*ext_b, (2*N-1)<<BITRES)));
852
33.1k
         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
33.1k
         } else {
859
33.1k
            itheta_q30 = ec_dec_uint(ctx->ext_ec, (1<<extra_bits)-1);
860
33.1k
         }
861
33.1k
         itheta_q30 -= (1<<(extra_bits-1))-1;
862
33.1k
         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
33.1k
         itheta_q30 = IMAX(0, IMIN(itheta_q30, 1073741824));
865
33.1k
         *ext_b -= ec_tell_frac(ctx->ext_ec) - ext_tell;
866
1.55M
      } else {
867
1.55M
         itheta_q30 = (opus_int32)itheta<<16;
868
1.55M
      }
869
1.58M
#endif
870
1.58M
      if (encode && stereo)
871
399k
      {
872
399k
         if (itheta==0)
873
69.0k
            intensity_stereo(m, X, Y, bandE, i, N);
874
330k
         else
875
330k
            stereo_split(X, Y, N);
876
399k
      }
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.58M
   } else if (stereo) {
880
822k
      if (encode)
881
233k
      {
882
233k
         inv = itheta > 8192 && !ctx->disable_inv;
883
233k
         if (inv)
884
61.9k
         {
885
61.9k
            int j;
886
1.04M
            for (j=0;j<N;j++)
887
986k
               Y[j] = -Y[j];
888
61.9k
         }
889
233k
         intensity_stereo(m, X, Y, bandE, i, N);
890
233k
      }
891
822k
      if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES)
892
221k
      {
893
221k
         if (encode)
894
118k
            ec_enc_bit_logp(ec, inv, 2);
895
103k
         else
896
103k
            inv = ec_dec_bit_logp(ec, 2);
897
221k
      } else
898
600k
         inv = 0;
899
      /* inv flag override to avoid problems with downmixing. */
900
822k
      if (ctx->disable_inv)
901
447k
         inv = 0;
902
822k
      itheta = 0;
903
822k
      itheta_q30 = 0;
904
822k
   }
905
2.40M
   qalloc = ec_tell_frac(ec) - tell;
906
2.40M
   *b -= qalloc;
907
908
2.40M
   if (itheta == 0)
909
966k
   {
910
966k
      imid = 32767;
911
966k
      iside = 0;
912
966k
      *fill &= (1<<B)-1;
913
966k
      delta = -16384;
914
1.43M
   } else if (itheta == 16384)
915
158k
   {
916
158k
      imid = 0;
917
158k
      iside = 32767;
918
158k
      *fill &= ((1<<B)-1)<<B;
919
158k
      delta = 16384;
920
1.28M
   } else {
921
1.28M
      imid = bitexact_cos((opus_int16)itheta);
922
1.28M
      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.28M
      delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid));
926
1.28M
   }
927
928
2.40M
   sctx->inv = inv;
929
2.40M
   sctx->imid = imid;
930
2.40M
   sctx->iside = iside;
931
2.40M
   sctx->delta = delta;
932
2.40M
   sctx->itheta = itheta;
933
2.40M
#ifdef ENABLE_QEXT
934
2.40M
   sctx->itheta_q30 = itheta_q30;
935
2.40M
#endif
936
2.40M
   sctx->qalloc = qalloc;
937
2.40M
}
938
static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y,
939
      celt_norm *lowband_out)
940
3.19M
{
941
3.19M
   int c;
942
3.19M
   int stereo;
943
3.19M
   celt_norm *x = X;
944
3.19M
   int encode;
945
3.19M
   ec_ctx *ec;
946
947
3.19M
   encode = ctx->encode;
948
3.19M
   ec = ctx->ec;
949
950
3.19M
   stereo = Y != NULL;
951
4.19M
   c=0; do {
952
4.19M
      int sign=0;
953
4.19M
      if (ctx->remaining_bits>=1<<BITRES)
954
2.06M
      {
955
2.06M
         if (encode)
956
1.50M
         {
957
1.50M
            sign = x[0]<0;
958
1.50M
            ec_enc_bits(ec, sign, 1);
959
1.50M
         } else {
960
560k
            sign = ec_dec_bits(ec, 1);
961
560k
         }
962
2.06M
         ctx->remaining_bits -= 1<<BITRES;
963
2.06M
      }
964
4.19M
      if (ctx->resynth)
965
3.02M
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
966
4.19M
      x = Y;
967
4.19M
   } while (++c<1+stereo);
968
3.19M
   if (lowband_out)
969
3.19M
      lowband_out[0] = SHR32(X[0],4);
970
3.19M
   return 1;
971
3.19M
}
bands.c:quant_band_n1
Line
Count
Source
940
1.59M
{
941
1.59M
   int c;
942
1.59M
   int stereo;
943
1.59M
   celt_norm *x = X;
944
1.59M
   int encode;
945
1.59M
   ec_ctx *ec;
946
947
1.59M
   encode = ctx->encode;
948
1.59M
   ec = ctx->ec;
949
950
1.59M
   stereo = Y != NULL;
951
2.09M
   c=0; do {
952
2.09M
      int sign=0;
953
2.09M
      if (ctx->remaining_bits>=1<<BITRES)
954
1.03M
      {
955
1.03M
         if (encode)
956
753k
         {
957
753k
            sign = x[0]<0;
958
753k
            ec_enc_bits(ec, sign, 1);
959
753k
         } else {
960
280k
            sign = ec_dec_bits(ec, 1);
961
280k
         }
962
1.03M
         ctx->remaining_bits -= 1<<BITRES;
963
1.03M
      }
964
2.09M
      if (ctx->resynth)
965
1.51M
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
966
2.09M
      x = Y;
967
2.09M
   } while (++c<1+stereo);
968
1.59M
   if (lowband_out)
969
1.59M
      lowband_out[0] = SHR32(X[0],4);
970
1.59M
   return 1;
971
1.59M
}
bands.c:quant_band_n1
Line
Count
Source
940
1.59M
{
941
1.59M
   int c;
942
1.59M
   int stereo;
943
1.59M
   celt_norm *x = X;
944
1.59M
   int encode;
945
1.59M
   ec_ctx *ec;
946
947
1.59M
   encode = ctx->encode;
948
1.59M
   ec = ctx->ec;
949
950
1.59M
   stereo = Y != NULL;
951
2.09M
   c=0; do {
952
2.09M
      int sign=0;
953
2.09M
      if (ctx->remaining_bits>=1<<BITRES)
954
1.03M
      {
955
1.03M
         if (encode)
956
753k
         {
957
753k
            sign = x[0]<0;
958
753k
            ec_enc_bits(ec, sign, 1);
959
753k
         } else {
960
280k
            sign = ec_dec_bits(ec, 1);
961
280k
         }
962
1.03M
         ctx->remaining_bits -= 1<<BITRES;
963
1.03M
      }
964
2.09M
      if (ctx->resynth)
965
1.51M
         x[0] = sign ? -NORM_SCALING : NORM_SCALING;
966
2.09M
      x = Y;
967
2.09M
   } while (++c<1+stereo);
968
1.59M
   if (lowband_out)
969
1.59M
      lowband_out[0] = SHR32(X[0],4);
970
1.59M
   return 1;
971
1.59M
}
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
22.8M
{
983
22.8M
   const unsigned char *cache;
984
22.8M
   int q;
985
22.8M
   int curr_bits;
986
22.8M
   int imid=0, iside=0;
987
22.8M
   int B0=B;
988
22.8M
   opus_val32 mid=0, side=0;
989
22.8M
   unsigned cm=0;
990
22.8M
   celt_norm *Y=NULL;
991
22.8M
   int encode;
992
22.8M
   const CELTMode *m;
993
22.8M
   int i;
994
22.8M
   int spread;
995
22.8M
   ec_ctx *ec;
996
997
22.8M
   encode = ctx->encode;
998
22.8M
   m = ctx->m;
999
22.8M
   i = ctx->i;
1000
22.8M
   spread = ctx->spread;
1001
22.8M
   ec = ctx->ec;
1002
1003
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1004
22.8M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1005
22.8M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1006
3.51M
   {
1007
3.51M
      int mbits, sbits, delta;
1008
3.51M
      int itheta;
1009
3.51M
      int qalloc;
1010
3.51M
      struct split_ctx sctx;
1011
3.51M
      celt_norm *next_lowband2=NULL;
1012
3.51M
      opus_int32 rebalance;
1013
1014
3.51M
      N >>= 1;
1015
3.51M
      Y = X+N;
1016
3.51M
      LM -= 1;
1017
3.51M
      if (B==1)
1018
1.26M
         fill = (fill&1)|(fill<<1);
1019
3.51M
      B = (B+1)>>1;
1020
1021
3.51M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1022
3.51M
      imid = sctx.imid;
1023
3.51M
      iside = sctx.iside;
1024
3.51M
      delta = sctx.delta;
1025
3.51M
      itheta = sctx.itheta;
1026
3.51M
      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
630k
      mid = SHL32(EXTEND32(imid), 16);
1035
630k
      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
3.51M
      if (B0>1 && (itheta&0x3fff))
1051
1.79M
      {
1052
1.79M
         if (itheta > 8192)
1053
            /* Rough approximation for pre-echo masking */
1054
925k
            delta -= delta>>(4-LM);
1055
866k
         else
1056
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1057
866k
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1058
1.79M
      }
1059
3.51M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1060
3.51M
      sbits = b-mbits;
1061
3.51M
      ctx->remaining_bits -= qalloc;
1062
1063
3.51M
      if (lowband)
1064
1.59M
         next_lowband2 = lowband+N; /* >32-bit split case */
1065
1066
3.51M
      rebalance = ctx->remaining_bits;
1067
3.51M
      if (mbits >= sbits)
1068
1.61M
      {
1069
1.61M
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1070
1.61M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1071
1.61M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1072
1.61M
         if (rebalance > 3<<BITRES && itheta!=0)
1073
610k
            sbits += rebalance - (3<<BITRES);
1074
1.61M
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1075
1.61M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1076
1.89M
      } else {
1077
1.89M
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1078
1.89M
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1079
1.89M
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1080
1.89M
         if (rebalance > 3<<BITRES && itheta!=16384)
1081
697k
            mbits += rebalance - (3<<BITRES);
1082
1.89M
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1083
1.89M
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1084
1.89M
      }
1085
19.3M
   } else {
1086
#ifdef ENABLE_QEXT
1087
      int extra_bits;
1088
      int ext_remaining_bits;
1089
10.8M
      extra_bits = ext_b/(N-1)>>BITRES;
1090
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1091
10.8M
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1092
10.5M
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1093
10.5M
         extra_bits = IMAX(extra_bits-1, 0);
1094
10.5M
      }
1095
10.8M
      extra_bits = IMIN(12, extra_bits);
1096
#endif
1097
      /* This is the basic no-split case */
1098
19.3M
      q = bits2pulses(m, i, LM, b);
1099
19.3M
      curr_bits = pulses2bits(m, i, LM, q);
1100
19.3M
      ctx->remaining_bits -= curr_bits;
1101
1102
      /* Ensures we can never bust the budget */
1103
19.5M
      while (ctx->remaining_bits < 0 && q > 0)
1104
186k
      {
1105
186k
         ctx->remaining_bits += curr_bits;
1106
186k
         q--;
1107
186k
         curr_bits = pulses2bits(m, i, LM, q);
1108
186k
         ctx->remaining_bits -= curr_bits;
1109
186k
      }
1110
1111
19.3M
      if (q!=0)
1112
9.54M
      {
1113
6.14M
         int K = get_pulses(q);
1114
1115
         /* Finally do the actual quantization */
1116
9.54M
         if (encode)
1117
6.00M
         {
1118
6.00M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1119
6.00M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1120
6.00M
                           ctx->arch);
1121
6.00M
         } else {
1122
3.53M
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1123
3.53M
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1124
3.53M
         }
1125
#ifdef ENABLE_QEXT
1126
4.71M
      } else if (ext_b > 2*N<<BITRES)
1127
17.4k
      {
1128
17.4k
         extra_bits = ext_b/(N-1)>>BITRES;
1129
17.4k
         ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec);
1130
17.4k
         if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1131
2.12k
            extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1132
2.12k
            extra_bits = IMAX(extra_bits-1, 0);
1133
2.12k
         }
1134
17.4k
         extra_bits = IMIN(14, extra_bits);
1135
17.4k
         if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth);
1136
17.4k
         else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain);
1137
#endif
1138
9.76M
      } else {
1139
         /* If there's no pulse, fill the band anyway */
1140
9.76M
         int j;
1141
9.76M
         if (ctx->resynth)
1142
8.05M
         {
1143
8.05M
            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.05M
            cm_mask = (unsigned)(1UL<<B)-1;
1147
8.05M
            fill &= cm_mask;
1148
8.05M
            if (!fill)
1149
2.52M
            {
1150
2.52M
               OPUS_CLEAR(X, N);
1151
5.52M
            } else {
1152
5.52M
               if (lowband == NULL)
1153
235k
               {
1154
                  /* Noise */
1155
3.90M
                  for (j=0;j<N;j++)
1156
3.66M
                  {
1157
3.66M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1158
3.66M
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1159
3.66M
                  }
1160
235k
                  cm = cm_mask;
1161
5.28M
               } else {
1162
                  /* Folded spectrum */
1163
76.3M
                  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.28M
                  cm = fill;
1173
5.28M
               }
1174
5.52M
               renormalise_vector(X, N, gain, ctx->arch);
1175
5.52M
            }
1176
8.05M
         }
1177
9.76M
      }
1178
19.3M
   }
1179
1180
22.8M
   return cm;
1181
22.8M
}
bands.c:quant_partition
Line
Count
Source
982
4.86M
{
983
4.86M
   const unsigned char *cache;
984
4.86M
   int q;
985
4.86M
   int curr_bits;
986
4.86M
   int imid=0, iside=0;
987
4.86M
   int B0=B;
988
4.86M
   opus_val32 mid=0, side=0;
989
4.86M
   unsigned cm=0;
990
4.86M
   celt_norm *Y=NULL;
991
4.86M
   int encode;
992
4.86M
   const CELTMode *m;
993
4.86M
   int i;
994
4.86M
   int spread;
995
4.86M
   ec_ctx *ec;
996
997
4.86M
   encode = ctx->encode;
998
4.86M
   m = ctx->m;
999
4.86M
   i = ctx->i;
1000
4.86M
   spread = ctx->spread;
1001
4.86M
   ec = ctx->ec;
1002
1003
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1004
4.86M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1005
4.86M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1006
630k
   {
1007
630k
      int mbits, sbits, delta;
1008
630k
      int itheta;
1009
630k
      int qalloc;
1010
630k
      struct split_ctx sctx;
1011
630k
      celt_norm *next_lowband2=NULL;
1012
630k
      opus_int32 rebalance;
1013
1014
630k
      N >>= 1;
1015
630k
      Y = X+N;
1016
630k
      LM -= 1;
1017
630k
      if (B==1)
1018
229k
         fill = (fill&1)|(fill<<1);
1019
630k
      B = (B+1)>>1;
1020
1021
630k
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1022
630k
      imid = sctx.imid;
1023
630k
      iside = sctx.iside;
1024
630k
      delta = sctx.delta;
1025
630k
      itheta = sctx.itheta;
1026
630k
      qalloc = sctx.qalloc;
1027
630k
#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
630k
      mid = SHL32(EXTEND32(imid), 16);
1035
630k
      side = SHL32(EXTEND32(iside), 16);
1036
630k
# 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
630k
      if (B0>1 && (itheta&0x3fff))
1051
321k
      {
1052
321k
         if (itheta > 8192)
1053
            /* Rough approximation for pre-echo masking */
1054
164k
            delta -= delta>>(4-LM);
1055
156k
         else
1056
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1057
156k
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1058
321k
      }
1059
630k
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1060
630k
      sbits = b-mbits;
1061
630k
      ctx->remaining_bits -= qalloc;
1062
1063
630k
      if (lowband)
1064
305k
         next_lowband2 = lowband+N; /* >32-bit split case */
1065
1066
630k
      rebalance = ctx->remaining_bits;
1067
630k
      if (mbits >= sbits)
1068
290k
      {
1069
290k
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1070
290k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1071
290k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1072
290k
         if (rebalance > 3<<BITRES && itheta!=0)
1073
116k
            sbits += rebalance - (3<<BITRES);
1074
290k
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1075
290k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1076
340k
      } else {
1077
340k
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1078
340k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1079
340k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1080
340k
         if (rebalance > 3<<BITRES && itheta!=16384)
1081
132k
            mbits += rebalance - (3<<BITRES);
1082
340k
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1083
340k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1084
340k
      }
1085
4.23M
   } 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
4.23M
      q = bits2pulses(m, i, LM, b);
1099
4.23M
      curr_bits = pulses2bits(m, i, LM, q);
1100
4.23M
      ctx->remaining_bits -= curr_bits;
1101
1102
      /* Ensures we can never bust the budget */
1103
4.26M
      while (ctx->remaining_bits < 0 && q > 0)
1104
34.3k
      {
1105
34.3k
         ctx->remaining_bits += curr_bits;
1106
34.3k
         q--;
1107
34.3k
         curr_bits = pulses2bits(m, i, LM, q);
1108
34.3k
         ctx->remaining_bits -= curr_bits;
1109
34.3k
      }
1110
1111
4.23M
      if (q!=0)
1112
1.69M
      {
1113
1.69M
         int K = get_pulses(q);
1114
1115
         /* Finally do the actual quantization */
1116
1.69M
         if (encode)
1117
1.01M
         {
1118
1.01M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1119
1.01M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1120
1.01M
                           ctx->arch);
1121
1.01M
         } else {
1122
687k
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1123
687k
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1124
687k
         }
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
2.53M
      } else {
1139
         /* If there's no pulse, fill the band anyway */
1140
2.53M
         int j;
1141
2.53M
         if (ctx->resynth)
1142
2.14M
         {
1143
2.14M
            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.14M
            cm_mask = (unsigned)(1UL<<B)-1;
1147
2.14M
            fill &= cm_mask;
1148
2.14M
            if (!fill)
1149
582k
            {
1150
582k
               OPUS_CLEAR(X, N);
1151
1.56M
            } else {
1152
1.56M
               if (lowband == NULL)
1153
61.5k
               {
1154
                  /* Noise */
1155
931k
                  for (j=0;j<N;j++)
1156
869k
                  {
1157
869k
                     ctx->seed = celt_lcg_rand(ctx->seed);
1158
869k
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1159
869k
                  }
1160
61.5k
                  cm = cm_mask;
1161
1.50M
               } else {
1162
                  /* Folded spectrum */
1163
21.2M
                  for (j=0;j<N;j++)
1164
19.7M
                  {
1165
19.7M
                     opus_val16 tmp;
1166
19.7M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1167
                     /* About 48 dB below the "normal" folding level */
1168
19.7M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1169
19.7M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1170
19.7M
                     X[j] = lowband[j]+tmp;
1171
19.7M
                  }
1172
1.50M
                  cm = fill;
1173
1.50M
               }
1174
1.56M
               renormalise_vector(X, N, gain, ctx->arch);
1175
1.56M
            }
1176
2.14M
         }
1177
2.53M
      }
1178
4.23M
   }
1179
1180
4.86M
   return cm;
1181
4.86M
}
bands.c:quant_partition
Line
Count
Source
982
6.55M
{
983
6.55M
   const unsigned char *cache;
984
6.55M
   int q;
985
6.55M
   int curr_bits;
986
6.55M
   int imid=0, iside=0;
987
6.55M
   int B0=B;
988
6.55M
   opus_val32 mid=0, side=0;
989
6.55M
   unsigned cm=0;
990
6.55M
   celt_norm *Y=NULL;
991
6.55M
   int encode;
992
6.55M
   const CELTMode *m;
993
6.55M
   int i;
994
6.55M
   int spread;
995
6.55M
   ec_ctx *ec;
996
997
6.55M
   encode = ctx->encode;
998
6.55M
   m = ctx->m;
999
6.55M
   i = ctx->i;
1000
6.55M
   spread = ctx->spread;
1001
6.55M
   ec = ctx->ec;
1002
1003
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1004
6.55M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1005
6.55M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1006
1.12M
   {
1007
1.12M
      int mbits, sbits, delta;
1008
1.12M
      int itheta;
1009
1.12M
      int qalloc;
1010
1.12M
      struct split_ctx sctx;
1011
1.12M
      celt_norm *next_lowband2=NULL;
1012
1.12M
      opus_int32 rebalance;
1013
1014
1.12M
      N >>= 1;
1015
1.12M
      Y = X+N;
1016
1.12M
      LM -= 1;
1017
1.12M
      if (B==1)
1018
403k
         fill = (fill&1)|(fill<<1);
1019
1.12M
      B = (B+1)>>1;
1020
1021
1.12M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1022
1.12M
      imid = sctx.imid;
1023
1.12M
      iside = sctx.iside;
1024
1.12M
      delta = sctx.delta;
1025
1.12M
      itheta = sctx.itheta;
1026
1.12M
      qalloc = sctx.qalloc;
1027
1.12M
#ifdef FIXED_POINT
1028
1.12M
# ifdef ENABLE_QEXT
1029
1.12M
      (void)imid;
1030
1.12M
      (void)iside;
1031
1.12M
      mid = celt_cos_norm32(sctx.itheta_q30);
1032
1.12M
      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.12M
      if (B0>1 && (itheta&0x3fff))
1051
574k
      {
1052
574k
         if (itheta > 8192)
1053
            /* Rough approximation for pre-echo masking */
1054
297k
            delta -= delta>>(4-LM);
1055
276k
         else
1056
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1057
276k
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1058
574k
      }
1059
1.12M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1060
1.12M
      sbits = b-mbits;
1061
1.12M
      ctx->remaining_bits -= qalloc;
1062
1063
1.12M
      if (lowband)
1064
491k
         next_lowband2 = lowband+N; /* >32-bit split case */
1065
1066
1.12M
      rebalance = ctx->remaining_bits;
1067
1.12M
      if (mbits >= sbits)
1068
519k
      {
1069
519k
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1070
519k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1071
519k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1072
519k
         if (rebalance > 3<<BITRES && itheta!=0)
1073
188k
            sbits += rebalance - (3<<BITRES);
1074
519k
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1075
519k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1076
605k
      } else {
1077
605k
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1078
605k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1079
605k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1080
605k
         if (rebalance > 3<<BITRES && itheta!=16384)
1081
216k
            mbits += rebalance - (3<<BITRES);
1082
605k
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1083
605k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1084
605k
      }
1085
5.42M
   } else {
1086
5.42M
#ifdef ENABLE_QEXT
1087
5.42M
      int extra_bits;
1088
5.42M
      int ext_remaining_bits;
1089
5.42M
      extra_bits = ext_b/(N-1)>>BITRES;
1090
5.42M
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1091
5.42M
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1092
5.26M
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1093
5.26M
         extra_bits = IMAX(extra_bits-1, 0);
1094
5.26M
      }
1095
5.42M
      extra_bits = IMIN(12, extra_bits);
1096
5.42M
#endif
1097
      /* This is the basic no-split case */
1098
5.42M
      q = bits2pulses(m, i, LM, b);
1099
5.42M
      curr_bits = pulses2bits(m, i, LM, q);
1100
5.42M
      ctx->remaining_bits -= curr_bits;
1101
1102
      /* Ensures we can never bust the budget */
1103
5.48M
      while (ctx->remaining_bits < 0 && q > 0)
1104
58.7k
      {
1105
58.7k
         ctx->remaining_bits += curr_bits;
1106
58.7k
         q--;
1107
58.7k
         curr_bits = pulses2bits(m, i, LM, q);
1108
58.7k
         ctx->remaining_bits -= curr_bits;
1109
58.7k
      }
1110
1111
5.42M
      if (q!=0)
1112
3.07M
      {
1113
3.07M
         int K = get_pulses(q);
1114
1115
         /* Finally do the actual quantization */
1116
3.07M
         if (encode)
1117
1.99M
         {
1118
1.99M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1119
1.99M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1120
1.99M
                           ctx->arch);
1121
1.99M
         } else {
1122
1.08M
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1123
1.08M
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1124
1.08M
         }
1125
3.07M
#ifdef ENABLE_QEXT
1126
3.07M
      } else if (ext_b > 2*N<<BITRES)
1127
8.73k
      {
1128
8.73k
         extra_bits = ext_b/(N-1)>>BITRES;
1129
8.73k
         ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec);
1130
8.73k
         if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1131
1.06k
            extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1132
1.06k
            extra_bits = IMAX(extra_bits-1, 0);
1133
1.06k
         }
1134
8.73k
         extra_bits = IMIN(14, extra_bits);
1135
8.73k
         if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth);
1136
8.73k
         else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain);
1137
8.73k
#endif
1138
2.34M
      } else {
1139
         /* If there's no pulse, fill the band anyway */
1140
2.34M
         int j;
1141
2.34M
         if (ctx->resynth)
1142
1.87M
         {
1143
1.87M
            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.87M
            cm_mask = (unsigned)(1UL<<B)-1;
1147
1.87M
            fill &= cm_mask;
1148
1.87M
            if (!fill)
1149
681k
            {
1150
681k
               OPUS_CLEAR(X, N);
1151
1.19M
            } else {
1152
1.19M
               if (lowband == NULL)
1153
56.0k
               {
1154
                  /* Noise */
1155
1.01M
                  for (j=0;j<N;j++)
1156
963k
                  {
1157
963k
                     ctx->seed = celt_lcg_rand(ctx->seed);
1158
963k
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1159
963k
                  }
1160
56.0k
                  cm = cm_mask;
1161
1.13M
               } else {
1162
                  /* Folded spectrum */
1163
16.9M
                  for (j=0;j<N;j++)
1164
15.7M
                  {
1165
15.7M
                     opus_val16 tmp;
1166
15.7M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1167
                     /* About 48 dB below the "normal" folding level */
1168
15.7M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1169
15.7M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1170
15.7M
                     X[j] = lowband[j]+tmp;
1171
15.7M
                  }
1172
1.13M
                  cm = fill;
1173
1.13M
               }
1174
1.19M
               renormalise_vector(X, N, gain, ctx->arch);
1175
1.19M
            }
1176
1.87M
         }
1177
2.34M
      }
1178
5.42M
   }
1179
1180
6.55M
   return cm;
1181
6.55M
}
bands.c:quant_partition
Line
Count
Source
982
6.55M
{
983
6.55M
   const unsigned char *cache;
984
6.55M
   int q;
985
6.55M
   int curr_bits;
986
6.55M
   int imid=0, iside=0;
987
6.55M
   int B0=B;
988
6.55M
   opus_val32 mid=0, side=0;
989
6.55M
   unsigned cm=0;
990
6.55M
   celt_norm *Y=NULL;
991
6.55M
   int encode;
992
6.55M
   const CELTMode *m;
993
6.55M
   int i;
994
6.55M
   int spread;
995
6.55M
   ec_ctx *ec;
996
997
6.55M
   encode = ctx->encode;
998
6.55M
   m = ctx->m;
999
6.55M
   i = ctx->i;
1000
6.55M
   spread = ctx->spread;
1001
6.55M
   ec = ctx->ec;
1002
1003
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1004
6.55M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1005
6.55M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1006
1.12M
   {
1007
1.12M
      int mbits, sbits, delta;
1008
1.12M
      int itheta;
1009
1.12M
      int qalloc;
1010
1.12M
      struct split_ctx sctx;
1011
1.12M
      celt_norm *next_lowband2=NULL;
1012
1.12M
      opus_int32 rebalance;
1013
1014
1.12M
      N >>= 1;
1015
1.12M
      Y = X+N;
1016
1.12M
      LM -= 1;
1017
1.12M
      if (B==1)
1018
403k
         fill = (fill&1)|(fill<<1);
1019
1.12M
      B = (B+1)>>1;
1020
1021
1.12M
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1022
1.12M
      imid = sctx.imid;
1023
1.12M
      iside = sctx.iside;
1024
1.12M
      delta = sctx.delta;
1025
1.12M
      itheta = sctx.itheta;
1026
1.12M
      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.12M
# ifdef ENABLE_QEXT
1039
1.12M
      (void)imid;
1040
1.12M
      (void)iside;
1041
1.12M
      mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30)));
1042
1.12M
      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.12M
#endif
1048
1049
      /* Give more bits to low-energy MDCTs than they would otherwise deserve */
1050
1.12M
      if (B0>1 && (itheta&0x3fff))
1051
574k
      {
1052
574k
         if (itheta > 8192)
1053
            /* Rough approximation for pre-echo masking */
1054
297k
            delta -= delta>>(4-LM);
1055
276k
         else
1056
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1057
276k
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1058
574k
      }
1059
1.12M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1060
1.12M
      sbits = b-mbits;
1061
1.12M
      ctx->remaining_bits -= qalloc;
1062
1063
1.12M
      if (lowband)
1064
491k
         next_lowband2 = lowband+N; /* >32-bit split case */
1065
1066
1.12M
      rebalance = ctx->remaining_bits;
1067
1.12M
      if (mbits >= sbits)
1068
519k
      {
1069
519k
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1070
519k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1071
519k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1072
519k
         if (rebalance > 3<<BITRES && itheta!=0)
1073
188k
            sbits += rebalance - (3<<BITRES);
1074
519k
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1075
519k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1076
605k
      } else {
1077
605k
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1078
605k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1079
605k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1080
605k
         if (rebalance > 3<<BITRES && itheta!=16384)
1081
216k
            mbits += rebalance - (3<<BITRES);
1082
605k
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1083
605k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1084
605k
      }
1085
5.42M
   } else {
1086
5.42M
#ifdef ENABLE_QEXT
1087
5.42M
      int extra_bits;
1088
5.42M
      int ext_remaining_bits;
1089
5.42M
      extra_bits = ext_b/(N-1)>>BITRES;
1090
5.42M
      ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec);
1091
5.42M
      if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1092
5.26M
         extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1093
5.26M
         extra_bits = IMAX(extra_bits-1, 0);
1094
5.26M
      }
1095
5.42M
      extra_bits = IMIN(12, extra_bits);
1096
5.42M
#endif
1097
      /* This is the basic no-split case */
1098
5.42M
      q = bits2pulses(m, i, LM, b);
1099
5.42M
      curr_bits = pulses2bits(m, i, LM, q);
1100
5.42M
      ctx->remaining_bits -= curr_bits;
1101
1102
      /* Ensures we can never bust the budget */
1103
5.48M
      while (ctx->remaining_bits < 0 && q > 0)
1104
58.7k
      {
1105
58.7k
         ctx->remaining_bits += curr_bits;
1106
58.7k
         q--;
1107
58.7k
         curr_bits = pulses2bits(m, i, LM, q);
1108
58.7k
         ctx->remaining_bits -= curr_bits;
1109
58.7k
      }
1110
1111
5.42M
      if (q!=0)
1112
3.07M
      {
1113
3.07M
         int K = get_pulses(q);
1114
1115
         /* Finally do the actual quantization */
1116
3.07M
         if (encode)
1117
1.99M
         {
1118
1.99M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1119
1.99M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1120
1.99M
                           ctx->arch);
1121
1.99M
         } else {
1122
1.08M
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1123
1.08M
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1124
1.08M
         }
1125
3.07M
#ifdef ENABLE_QEXT
1126
3.07M
      } else if (ext_b > 2*N<<BITRES)
1127
8.73k
      {
1128
8.73k
         extra_bits = ext_b/(N-1)>>BITRES;
1129
8.73k
         ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec);
1130
8.73k
         if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) {
1131
1.06k
            extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES;
1132
1.06k
            extra_bits = IMAX(extra_bits-1, 0);
1133
1.06k
         }
1134
8.73k
         extra_bits = IMIN(14, extra_bits);
1135
8.73k
         if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth);
1136
8.73k
         else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain);
1137
8.73k
#endif
1138
2.34M
      } else {
1139
         /* If there's no pulse, fill the band anyway */
1140
2.34M
         int j;
1141
2.34M
         if (ctx->resynth)
1142
1.87M
         {
1143
1.87M
            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.87M
            cm_mask = (unsigned)(1UL<<B)-1;
1147
1.87M
            fill &= cm_mask;
1148
1.87M
            if (!fill)
1149
681k
            {
1150
681k
               OPUS_CLEAR(X, N);
1151
1.19M
            } else {
1152
1.19M
               if (lowband == NULL)
1153
56.0k
               {
1154
                  /* Noise */
1155
1.01M
                  for (j=0;j<N;j++)
1156
963k
                  {
1157
963k
                     ctx->seed = celt_lcg_rand(ctx->seed);
1158
963k
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1159
963k
                  }
1160
56.0k
                  cm = cm_mask;
1161
1.13M
               } else {
1162
                  /* Folded spectrum */
1163
16.9M
                  for (j=0;j<N;j++)
1164
15.7M
                  {
1165
15.7M
                     opus_val16 tmp;
1166
15.7M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1167
                     /* About 48 dB below the "normal" folding level */
1168
15.7M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1169
15.7M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1170
15.7M
                     X[j] = lowband[j]+tmp;
1171
15.7M
                  }
1172
1.13M
                  cm = fill;
1173
1.13M
               }
1174
1.19M
               renormalise_vector(X, N, gain, ctx->arch);
1175
1.19M
            }
1176
1.87M
         }
1177
2.34M
      }
1178
5.42M
   }
1179
1180
6.55M
   return cm;
1181
6.55M
}
bands.c:quant_partition
Line
Count
Source
982
4.86M
{
983
4.86M
   const unsigned char *cache;
984
4.86M
   int q;
985
4.86M
   int curr_bits;
986
4.86M
   int imid=0, iside=0;
987
4.86M
   int B0=B;
988
4.86M
   opus_val32 mid=0, side=0;
989
4.86M
   unsigned cm=0;
990
4.86M
   celt_norm *Y=NULL;
991
4.86M
   int encode;
992
4.86M
   const CELTMode *m;
993
4.86M
   int i;
994
4.86M
   int spread;
995
4.86M
   ec_ctx *ec;
996
997
4.86M
   encode = ctx->encode;
998
4.86M
   m = ctx->m;
999
4.86M
   i = ctx->i;
1000
4.86M
   spread = ctx->spread;
1001
4.86M
   ec = ctx->ec;
1002
1003
   /* If we need 1.5 more bit than we can produce, split the band in two. */
1004
4.86M
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
1005
4.86M
   if (LM != -1 && b > cache[cache[0]]+12 && N>2)
1006
630k
   {
1007
630k
      int mbits, sbits, delta;
1008
630k
      int itheta;
1009
630k
      int qalloc;
1010
630k
      struct split_ctx sctx;
1011
630k
      celt_norm *next_lowband2=NULL;
1012
630k
      opus_int32 rebalance;
1013
1014
630k
      N >>= 1;
1015
630k
      Y = X+N;
1016
630k
      LM -= 1;
1017
630k
      if (B==1)
1018
229k
         fill = (fill&1)|(fill<<1);
1019
630k
      B = (B+1)>>1;
1020
1021
630k
      compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b));
1022
630k
      imid = sctx.imid;
1023
630k
      iside = sctx.iside;
1024
630k
      delta = sctx.delta;
1025
630k
      itheta = sctx.itheta;
1026
630k
      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
630k
      mid = (1.f/32768)*imid;
1045
630k
      side = (1.f/32768)*iside;
1046
630k
# endif
1047
630k
#endif
1048
1049
      /* Give more bits to low-energy MDCTs than they would otherwise deserve */
1050
630k
      if (B0>1 && (itheta&0x3fff))
1051
321k
      {
1052
321k
         if (itheta > 8192)
1053
            /* Rough approximation for pre-echo masking */
1054
164k
            delta -= delta>>(4-LM);
1055
156k
         else
1056
            /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */
1057
156k
            delta = IMIN(0, delta + (N<<BITRES>>(5-LM)));
1058
321k
      }
1059
630k
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1060
630k
      sbits = b-mbits;
1061
630k
      ctx->remaining_bits -= qalloc;
1062
1063
630k
      if (lowband)
1064
305k
         next_lowband2 = lowband+N; /* >32-bit split case */
1065
1066
630k
      rebalance = ctx->remaining_bits;
1067
630k
      if (mbits >= sbits)
1068
290k
      {
1069
290k
         cm = quant_partition(ctx, X, N, mbits, B, lowband, LM,
1070
290k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1071
290k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1072
290k
         if (rebalance > 3<<BITRES && itheta!=0)
1073
116k
            sbits += rebalance - (3<<BITRES);
1074
290k
         cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1075
290k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1076
340k
      } else {
1077
340k
         cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM,
1078
340k
               MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1);
1079
340k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1080
340k
         if (rebalance > 3<<BITRES && itheta!=16384)
1081
132k
            mbits += rebalance - (3<<BITRES);
1082
340k
         cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM,
1083
340k
               MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2));
1084
340k
      }
1085
4.23M
   } 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
4.23M
      q = bits2pulses(m, i, LM, b);
1099
4.23M
      curr_bits = pulses2bits(m, i, LM, q);
1100
4.23M
      ctx->remaining_bits -= curr_bits;
1101
1102
      /* Ensures we can never bust the budget */
1103
4.26M
      while (ctx->remaining_bits < 0 && q > 0)
1104
34.3k
      {
1105
34.3k
         ctx->remaining_bits += curr_bits;
1106
34.3k
         q--;
1107
34.3k
         curr_bits = pulses2bits(m, i, LM, q);
1108
34.3k
         ctx->remaining_bits -= curr_bits;
1109
34.3k
      }
1110
1111
4.23M
      if (q!=0)
1112
1.69M
      {
1113
1.69M
         int K = get_pulses(q);
1114
1115
         /* Finally do the actual quantization */
1116
1.69M
         if (encode)
1117
1.01M
         {
1118
1.01M
            cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth
1119
1.01M
                           ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits),
1120
1.01M
                           ctx->arch);
1121
1.01M
         } else {
1122
687k
            cm = alg_unquant(X, N, K, spread, B, ec, gain
1123
687k
                             ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits));
1124
687k
         }
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
2.53M
      } else {
1139
         /* If there's no pulse, fill the band anyway */
1140
2.53M
         int j;
1141
2.53M
         if (ctx->resynth)
1142
2.14M
         {
1143
2.14M
            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.14M
            cm_mask = (unsigned)(1UL<<B)-1;
1147
2.14M
            fill &= cm_mask;
1148
2.14M
            if (!fill)
1149
582k
            {
1150
582k
               OPUS_CLEAR(X, N);
1151
1.56M
            } else {
1152
1.56M
               if (lowband == NULL)
1153
61.5k
               {
1154
                  /* Noise */
1155
931k
                  for (j=0;j<N;j++)
1156
869k
                  {
1157
869k
                     ctx->seed = celt_lcg_rand(ctx->seed);
1158
869k
                     X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14);
1159
869k
                  }
1160
61.5k
                  cm = cm_mask;
1161
1.50M
               } else {
1162
                  /* Folded spectrum */
1163
21.2M
                  for (j=0;j<N;j++)
1164
19.7M
                  {
1165
19.7M
                     opus_val16 tmp;
1166
19.7M
                     ctx->seed = celt_lcg_rand(ctx->seed);
1167
                     /* About 48 dB below the "normal" folding level */
1168
19.7M
                     tmp = QCONST16(1.0f/256, NORM_SHIFT-4);
1169
19.7M
                     tmp = (ctx->seed)&0x8000 ? tmp : -tmp;
1170
19.7M
                     X[j] = lowband[j]+tmp;
1171
19.7M
                  }
1172
1.50M
                  cm = fill;
1173
1.50M
               }
1174
1.56M
               renormalise_vector(X, N, gain, ctx->arch);
1175
1.56M
            }
1176
2.14M
         }
1177
2.53M
      }
1178
4.23M
   }
1179
1180
4.86M
   return cm;
1181
4.86M
}
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
186k
{
1186
186k
   celt_assert(LM>=0);
1187
186k
   ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1188
186k
   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
186k
   if (LM==0 || b<=2*N<<BITRES) {
1191
105k
      int res, ret;
1192
105k
      b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits);
1193
      /* Resolution left after taking into account coding the cube face. */
1194
105k
      res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES;
1195
105k
      res = IMIN(14, IMAX(0, res));
1196
105k
      if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth);
1197
105k
      else ret = cubic_unquant(X, N, res, B, ec, gain);
1198
105k
      ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1199
105k
      return ret;
1200
105k
   } else {
1201
81.4k
      celt_norm *Y;
1202
81.4k
      opus_int32 itheta_q30;
1203
81.4k
      opus_val32 g1, g2;
1204
81.4k
      opus_int32 theta_res;
1205
81.4k
      opus_int32 qtheta;
1206
81.4k
      int delta;
1207
81.4k
      int b1, b2;
1208
81.4k
      int cm;
1209
81.4k
      int N0;
1210
81.4k
      N0 = N;
1211
81.4k
      N >>= 1;
1212
81.4k
      Y = X+N;
1213
81.4k
      LM -= 1;
1214
81.4k
      B = (B+1)>>1;
1215
81.4k
      theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1);
1216
81.4k
      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
81.4k
      } else {
1221
81.4k
         qtheta = ec_dec_uint(ec, (1<<theta_res)+1);
1222
81.4k
      }
1223
81.4k
      itheta_q30 = qtheta<<(30-theta_res);
1224
81.4k
      b -= theta_res<<BITRES;
1225
81.4k
      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
81.4k
      if (itheta_q30 == 0) {
1235
2.06k
         b1=b;
1236
2.06k
         b2=0;
1237
79.4k
      } else if (itheta_q30==1073741824) {
1238
2.40k
         b1=0;
1239
2.40k
         b2=b;
1240
77.0k
      } else {
1241
77.0k
         b1 = IMIN(b, IMAX(0, (b-delta)/2));
1242
77.0k
         b2 = b-b1;
1243
77.0k
      }
1244
81.4k
      cm  = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode);
1245
81.4k
      cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode);
1246
81.4k
      return cm;
1247
81.4k
   }
1248
186k
}
bands.c:cubic_quant_partition
Line
Count
Source
1185
93.4k
{
1186
93.4k
   celt_assert(LM>=0);
1187
93.4k
   ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1188
93.4k
   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
93.4k
   if (LM==0 || b<=2*N<<BITRES) {
1191
52.6k
      int res, ret;
1192
52.6k
      b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits);
1193
      /* Resolution left after taking into account coding the cube face. */
1194
52.6k
      res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES;
1195
52.6k
      res = IMIN(14, IMAX(0, res));
1196
52.6k
      if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth);
1197
52.6k
      else ret = cubic_unquant(X, N, res, B, ec, gain);
1198
52.6k
      ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1199
52.6k
      return ret;
1200
52.6k
   } else {
1201
40.7k
      celt_norm *Y;
1202
40.7k
      opus_int32 itheta_q30;
1203
40.7k
      opus_val32 g1, g2;
1204
40.7k
      opus_int32 theta_res;
1205
40.7k
      opus_int32 qtheta;
1206
40.7k
      int delta;
1207
40.7k
      int b1, b2;
1208
40.7k
      int cm;
1209
40.7k
      int N0;
1210
40.7k
      N0 = N;
1211
40.7k
      N >>= 1;
1212
40.7k
      Y = X+N;
1213
40.7k
      LM -= 1;
1214
40.7k
      B = (B+1)>>1;
1215
40.7k
      theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1);
1216
40.7k
      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
40.7k
      } else {
1221
40.7k
         qtheta = ec_dec_uint(ec, (1<<theta_res)+1);
1222
40.7k
      }
1223
40.7k
      itheta_q30 = qtheta<<(30-theta_res);
1224
40.7k
      b -= theta_res<<BITRES;
1225
40.7k
      delta = (N0-1) * 23 * ((itheta_q30>>16)-8192) >> (17-BITRES);
1226
1227
40.7k
#ifdef FIXED_POINT
1228
40.7k
      g1 = celt_cos_norm32(itheta_q30);
1229
40.7k
      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
40.7k
      if (itheta_q30 == 0) {
1235
1.03k
         b1=b;
1236
1.03k
         b2=0;
1237
39.7k
      } else if (itheta_q30==1073741824) {
1238
1.20k
         b1=0;
1239
1.20k
         b2=b;
1240
38.5k
      } else {
1241
38.5k
         b1 = IMIN(b, IMAX(0, (b-delta)/2));
1242
38.5k
         b2 = b-b1;
1243
38.5k
      }
1244
40.7k
      cm  = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode);
1245
40.7k
      cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode);
1246
40.7k
      return cm;
1247
40.7k
   }
1248
93.4k
}
bands.c:cubic_quant_partition
Line
Count
Source
1185
93.4k
{
1186
93.4k
   celt_assert(LM>=0);
1187
93.4k
   ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1188
93.4k
   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
93.4k
   if (LM==0 || b<=2*N<<BITRES) {
1191
52.6k
      int res, ret;
1192
52.6k
      b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits);
1193
      /* Resolution left after taking into account coding the cube face. */
1194
52.6k
      res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES;
1195
52.6k
      res = IMIN(14, IMAX(0, res));
1196
52.6k
      if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth);
1197
52.6k
      else ret = cubic_unquant(X, N, res, B, ec, gain);
1198
52.6k
      ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec);
1199
52.6k
      return ret;
1200
52.6k
   } else {
1201
40.7k
      celt_norm *Y;
1202
40.7k
      opus_int32 itheta_q30;
1203
40.7k
      opus_val32 g1, g2;
1204
40.7k
      opus_int32 theta_res;
1205
40.7k
      opus_int32 qtheta;
1206
40.7k
      int delta;
1207
40.7k
      int b1, b2;
1208
40.7k
      int cm;
1209
40.7k
      int N0;
1210
40.7k
      N0 = N;
1211
40.7k
      N >>= 1;
1212
40.7k
      Y = X+N;
1213
40.7k
      LM -= 1;
1214
40.7k
      B = (B+1)>>1;
1215
40.7k
      theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1);
1216
40.7k
      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
40.7k
      } else {
1221
40.7k
         qtheta = ec_dec_uint(ec, (1<<theta_res)+1);
1222
40.7k
      }
1223
40.7k
      itheta_q30 = qtheta<<(30-theta_res);
1224
40.7k
      b -= theta_res<<BITRES;
1225
40.7k
      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
40.7k
      g1 = celt_cos_norm2(itheta_q30*(1.f/(1<<30)));
1232
40.7k
      g2 = celt_cos_norm2(1.f-itheta_q30*(1.f/(1<<30)));
1233
40.7k
#endif
1234
40.7k
      if (itheta_q30 == 0) {
1235
1.03k
         b1=b;
1236
1.03k
         b2=0;
1237
39.7k
      } else if (itheta_q30==1073741824) {
1238
1.20k
         b1=0;
1239
1.20k
         b2=b;
1240
38.5k
      } else {
1241
38.5k
         b1 = IMIN(b, IMAX(0, (b-delta)/2));
1242
38.5k
         b2 = b-b1;
1243
38.5k
      }
1244
40.7k
      cm  = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode);
1245
40.7k
      cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode);
1246
40.7k
      return cm;
1247
40.7k
   }
1248
93.4k
}
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
18.0M
{
1258
18.0M
   int N0=N;
1259
18.0M
   int N_B=N;
1260
18.0M
   int N_B0;
1261
18.0M
   int B0=B;
1262
18.0M
   int time_divide=0;
1263
18.0M
   int recombine=0;
1264
18.0M
   int longBlocks;
1265
18.0M
   unsigned cm=0;
1266
18.0M
   int k;
1267
18.0M
   int encode;
1268
18.0M
   int tf_change;
1269
1270
18.0M
   encode = ctx->encode;
1271
18.0M
   tf_change = ctx->tf_change;
1272
1273
18.0M
   longBlocks = B0==1;
1274
1275
18.0M
   N_B = celt_udiv(N_B, B);
1276
1277
   /* Special case for one sample */
1278
18.0M
   if (N==1)
1279
2.19M
   {
1280
2.19M
      return quant_band_n1(ctx, X, NULL, lowband_out);
1281
2.19M
   }
1282
1283
15.8M
   if (tf_change>0)
1284
1.25M
      recombine = tf_change;
1285
   /* Band recombining to increase frequency resolution */
1286
1287
15.8M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1288
2.00M
   {
1289
2.00M
      OPUS_COPY(lowband_scratch, lowband, N);
1290
2.00M
      lowband = lowband_scratch;
1291
2.00M
   }
1292
1293
17.9M
   for (k=0;k<recombine;k++)
1294
2.11M
   {
1295
2.11M
      static const unsigned char bit_interleave_table[16]={
1296
2.11M
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1297
2.11M
      };
1298
2.11M
      if (encode)
1299
502k
         haar1(X, N>>k, 1<<k);
1300
2.11M
      if (lowband)
1301
1.16M
         haar1(lowband, N>>k, 1<<k);
1302
2.11M
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1303
2.11M
   }
1304
15.8M
   B>>=recombine;
1305
15.8M
   N_B<<=recombine;
1306
1307
   /* Increasing the time resolution */
1308
18.5M
   while ((N_B&1) == 0 && tf_change<0)
1309
2.68M
   {
1310
2.68M
      if (encode)
1311
1.45M
         haar1(X, N_B, B);
1312
2.68M
      if (lowband)
1313
1.19M
         haar1(lowband, N_B, B);
1314
2.68M
      fill |= fill<<B;
1315
2.68M
      B <<= 1;
1316
2.68M
      N_B >>= 1;
1317
2.68M
      time_divide++;
1318
2.68M
      tf_change++;
1319
2.68M
   }
1320
15.8M
   B0=B;
1321
15.8M
   N_B0 = N_B;
1322
1323
   /* Reorganize the samples in time order instead of frequency order */
1324
15.8M
   if (B0>1)
1325
4.63M
   {
1326
4.63M
      if (encode)
1327
3.42M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1328
4.63M
      if (lowband)
1329
1.54M
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1330
4.63M
   }
1331
1332
#ifdef ENABLE_QEXT
1333
8.63M
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1334
23.8k
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1335
23.8k
   } else
1336
8.60M
#endif
1337
8.60M
   {
1338
8.60M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1339
8.60M
   }
1340
1341
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1342
15.8M
   if (ctx->resynth)
1343
11.4M
   {
1344
      /* Undo the sample reorganization going from time order to frequency order */
1345
11.4M
      if (B0>1)
1346
2.51M
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1347
1348
      /* Undo time-freq changes that we did earlier */
1349
11.4M
      N_B = N_B0;
1350
11.4M
      B = B0;
1351
13.3M
      for (k=0;k<time_divide;k++)
1352
1.88M
      {
1353
1.88M
         B >>= 1;
1354
1.88M
         N_B <<= 1;
1355
1.88M
         cm |= cm>>B;
1356
1.88M
         haar1(X, N_B, B);
1357
1.88M
      }
1358
1359
13.3M
      for (k=0;k<recombine;k++)
1360
1.83M
      {
1361
1.83M
         static const unsigned char bit_deinterleave_table[16]={
1362
1.83M
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1363
1.83M
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1364
1.83M
         };
1365
1.83M
         cm = bit_deinterleave_table[cm];
1366
1.83M
         haar1(X, N0>>k, 1<<k);
1367
1.83M
      }
1368
11.4M
      B<<=recombine;
1369
1370
      /* Scale output for later folding */
1371
11.4M
      if (lowband_out)
1372
7.96M
      {
1373
7.96M
         int j;
1374
7.96M
         opus_val16 n;
1375
7.96M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1376
84.3M
         for (j=0;j<N0;j++)
1377
76.4M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1378
7.96M
      }
1379
11.4M
      cm &= (1<<B)-1;
1380
11.4M
   }
1381
15.8M
   return cm;
1382
18.0M
}
bands.c:quant_band
Line
Count
Source
1257
4.26M
{
1258
4.26M
   int N0=N;
1259
4.26M
   int N_B=N;
1260
4.26M
   int N_B0;
1261
4.26M
   int B0=B;
1262
4.26M
   int time_divide=0;
1263
4.26M
   int recombine=0;
1264
4.26M
   int longBlocks;
1265
4.26M
   unsigned cm=0;
1266
4.26M
   int k;
1267
4.26M
   int encode;
1268
4.26M
   int tf_change;
1269
1270
4.26M
   encode = ctx->encode;
1271
4.26M
   tf_change = ctx->tf_change;
1272
1273
4.26M
   longBlocks = B0==1;
1274
1275
4.26M
   N_B = celt_udiv(N_B, B);
1276
1277
   /* Special case for one sample */
1278
4.26M
   if (N==1)
1279
661k
   {
1280
661k
      return quant_band_n1(ctx, X, NULL, lowband_out);
1281
661k
   }
1282
1283
3.60M
   if (tf_change>0)
1284
355k
      recombine = tf_change;
1285
   /* Band recombining to increase frequency resolution */
1286
1287
3.60M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1288
434k
   {
1289
434k
      OPUS_COPY(lowband_scratch, lowband, N);
1290
434k
      lowband = lowband_scratch;
1291
434k
   }
1292
1293
4.23M
   for (k=0;k<recombine;k++)
1294
630k
   {
1295
630k
      static const unsigned char bit_interleave_table[16]={
1296
630k
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1297
630k
      };
1298
630k
      if (encode)
1299
82.3k
         haar1(X, N>>k, 1<<k);
1300
630k
      if (lowband)
1301
375k
         haar1(lowband, N>>k, 1<<k);
1302
630k
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1303
630k
   }
1304
3.60M
   B>>=recombine;
1305
3.60M
   N_B<<=recombine;
1306
1307
   /* Increasing the time resolution */
1308
4.11M
   while ((N_B&1) == 0 && tf_change<0)
1309
508k
   {
1310
508k
      if (encode)
1311
246k
         haar1(X, N_B, B);
1312
508k
      if (lowband)
1313
233k
         haar1(lowband, N_B, B);
1314
508k
      fill |= fill<<B;
1315
508k
      B <<= 1;
1316
508k
      N_B >>= 1;
1317
508k
      time_divide++;
1318
508k
      tf_change++;
1319
508k
   }
1320
3.60M
   B0=B;
1321
3.60M
   N_B0 = N_B;
1322
1323
   /* Reorganize the samples in time order instead of frequency order */
1324
3.60M
   if (B0>1)
1325
824k
   {
1326
824k
      if (encode)
1327
578k
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1328
824k
      if (lowband)
1329
275k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1330
824k
   }
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
3.60M
   {
1338
3.60M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1339
3.60M
   }
1340
1341
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1342
3.60M
   if (ctx->resynth)
1343
2.72M
   {
1344
      /* Undo the sample reorganization going from time order to frequency order */
1345
2.72M
      if (B0>1)
1346
438k
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1347
1348
      /* Undo time-freq changes that we did earlier */
1349
2.72M
      N_B = N_B0;
1350
2.72M
      B = B0;
1351
3.09M
      for (k=0;k<time_divide;k++)
1352
363k
      {
1353
363k
         B >>= 1;
1354
363k
         N_B <<= 1;
1355
363k
         cm |= cm>>B;
1356
363k
         haar1(X, N_B, B);
1357
363k
      }
1358
1359
3.31M
      for (k=0;k<recombine;k++)
1360
582k
      {
1361
582k
         static const unsigned char bit_deinterleave_table[16]={
1362
582k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1363
582k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1364
582k
         };
1365
582k
         cm = bit_deinterleave_table[cm];
1366
582k
         haar1(X, N0>>k, 1<<k);
1367
582k
      }
1368
2.72M
      B<<=recombine;
1369
1370
      /* Scale output for later folding */
1371
2.72M
      if (lowband_out)
1372
1.93M
      {
1373
1.93M
         int j;
1374
1.93M
         opus_val16 n;
1375
1.93M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1376
20.7M
         for (j=0;j<N0;j++)
1377
18.7M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1378
1.93M
      }
1379
2.72M
      cm &= (1<<B)-1;
1380
2.72M
   }
1381
3.60M
   return cm;
1382
4.26M
}
bands.c:quant_band
Line
Count
Source
1257
4.75M
{
1258
4.75M
   int N0=N;
1259
4.75M
   int N_B=N;
1260
4.75M
   int N_B0;
1261
4.75M
   int B0=B;
1262
4.75M
   int time_divide=0;
1263
4.75M
   int recombine=0;
1264
4.75M
   int longBlocks;
1265
4.75M
   unsigned cm=0;
1266
4.75M
   int k;
1267
4.75M
   int encode;
1268
4.75M
   int tf_change;
1269
1270
4.75M
   encode = ctx->encode;
1271
4.75M
   tf_change = ctx->tf_change;
1272
1273
4.75M
   longBlocks = B0==1;
1274
1275
4.75M
   N_B = celt_udiv(N_B, B);
1276
1277
   /* Special case for one sample */
1278
4.75M
   if (N==1)
1279
438k
   {
1280
438k
      return quant_band_n1(ctx, X, NULL, lowband_out);
1281
438k
   }
1282
1283
4.31M
   if (tf_change>0)
1284
272k
      recombine = tf_change;
1285
   /* Band recombining to increase frequency resolution */
1286
1287
4.31M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1288
568k
   {
1289
568k
      OPUS_COPY(lowband_scratch, lowband, N);
1290
568k
      lowband = lowband_scratch;
1291
568k
   }
1292
1293
4.74M
   for (k=0;k<recombine;k++)
1294
428k
   {
1295
428k
      static const unsigned char bit_interleave_table[16]={
1296
428k
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1297
428k
      };
1298
428k
      if (encode)
1299
168k
         haar1(X, N>>k, 1<<k);
1300
428k
      if (lowband)
1301
207k
         haar1(lowband, N>>k, 1<<k);
1302
428k
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1303
428k
   }
1304
4.31M
   B>>=recombine;
1305
4.31M
   N_B<<=recombine;
1306
1307
   /* Increasing the time resolution */
1308
5.15M
   while ((N_B&1) == 0 && tf_change<0)
1309
834k
   {
1310
834k
      if (encode)
1311
480k
         haar1(X, N_B, B);
1312
834k
      if (lowband)
1313
366k
         haar1(lowband, N_B, B);
1314
834k
      fill |= fill<<B;
1315
834k
      B <<= 1;
1316
834k
      N_B >>= 1;
1317
834k
      time_divide++;
1318
834k
      tf_change++;
1319
834k
   }
1320
4.31M
   B0=B;
1321
4.31M
   N_B0 = N_B;
1322
1323
   /* Reorganize the samples in time order instead of frequency order */
1324
4.31M
   if (B0>1)
1325
1.49M
   {
1326
1.49M
      if (encode)
1327
1.13M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1328
1.49M
      if (lowband)
1329
496k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1330
1.49M
   }
1331
1332
4.31M
#ifdef ENABLE_QEXT
1333
4.31M
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1334
11.9k
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1335
11.9k
   } else
1336
4.30M
#endif
1337
4.30M
   {
1338
4.30M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1339
4.30M
   }
1340
1341
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1342
4.31M
   if (ctx->resynth)
1343
3.01M
   {
1344
      /* Undo the sample reorganization going from time order to frequency order */
1345
3.01M
      if (B0>1)
1346
819k
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1347
1348
      /* Undo time-freq changes that we did earlier */
1349
3.01M
      N_B = N_B0;
1350
3.01M
      B = B0;
1351
3.59M
      for (k=0;k<time_divide;k++)
1352
581k
      {
1353
581k
         B >>= 1;
1354
581k
         N_B <<= 1;
1355
581k
         cm |= cm>>B;
1356
581k
         haar1(X, N_B, B);
1357
581k
      }
1358
1359
3.34M
      for (k=0;k<recombine;k++)
1360
335k
      {
1361
335k
         static const unsigned char bit_deinterleave_table[16]={
1362
335k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1363
335k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1364
335k
         };
1365
335k
         cm = bit_deinterleave_table[cm];
1366
335k
         haar1(X, N0>>k, 1<<k);
1367
335k
      }
1368
3.01M
      B<<=recombine;
1369
1370
      /* Scale output for later folding */
1371
3.01M
      if (lowband_out)
1372
2.04M
      {
1373
2.04M
         int j;
1374
2.04M
         opus_val16 n;
1375
2.04M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1376
21.4M
         for (j=0;j<N0;j++)
1377
19.4M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1378
2.04M
      }
1379
3.01M
      cm &= (1<<B)-1;
1380
3.01M
   }
1381
4.31M
   return cm;
1382
4.75M
}
bands.c:quant_band
Line
Count
Source
1257
4.75M
{
1258
4.75M
   int N0=N;
1259
4.75M
   int N_B=N;
1260
4.75M
   int N_B0;
1261
4.75M
   int B0=B;
1262
4.75M
   int time_divide=0;
1263
4.75M
   int recombine=0;
1264
4.75M
   int longBlocks;
1265
4.75M
   unsigned cm=0;
1266
4.75M
   int k;
1267
4.75M
   int encode;
1268
4.75M
   int tf_change;
1269
1270
4.75M
   encode = ctx->encode;
1271
4.75M
   tf_change = ctx->tf_change;
1272
1273
4.75M
   longBlocks = B0==1;
1274
1275
4.75M
   N_B = celt_udiv(N_B, B);
1276
1277
   /* Special case for one sample */
1278
4.75M
   if (N==1)
1279
438k
   {
1280
438k
      return quant_band_n1(ctx, X, NULL, lowband_out);
1281
438k
   }
1282
1283
4.31M
   if (tf_change>0)
1284
272k
      recombine = tf_change;
1285
   /* Band recombining to increase frequency resolution */
1286
1287
4.31M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1288
568k
   {
1289
568k
      OPUS_COPY(lowband_scratch, lowband, N);
1290
568k
      lowband = lowband_scratch;
1291
568k
   }
1292
1293
4.74M
   for (k=0;k<recombine;k++)
1294
428k
   {
1295
428k
      static const unsigned char bit_interleave_table[16]={
1296
428k
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1297
428k
      };
1298
428k
      if (encode)
1299
168k
         haar1(X, N>>k, 1<<k);
1300
428k
      if (lowband)
1301
207k
         haar1(lowband, N>>k, 1<<k);
1302
428k
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1303
428k
   }
1304
4.31M
   B>>=recombine;
1305
4.31M
   N_B<<=recombine;
1306
1307
   /* Increasing the time resolution */
1308
5.15M
   while ((N_B&1) == 0 && tf_change<0)
1309
834k
   {
1310
834k
      if (encode)
1311
480k
         haar1(X, N_B, B);
1312
834k
      if (lowband)
1313
366k
         haar1(lowband, N_B, B);
1314
834k
      fill |= fill<<B;
1315
834k
      B <<= 1;
1316
834k
      N_B >>= 1;
1317
834k
      time_divide++;
1318
834k
      tf_change++;
1319
834k
   }
1320
4.31M
   B0=B;
1321
4.31M
   N_B0 = N_B;
1322
1323
   /* Reorganize the samples in time order instead of frequency order */
1324
4.31M
   if (B0>1)
1325
1.49M
   {
1326
1.49M
      if (encode)
1327
1.13M
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1328
1.49M
      if (lowband)
1329
496k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1330
1.49M
   }
1331
1332
4.31M
#ifdef ENABLE_QEXT
1333
4.31M
   if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) {
1334
11.9k
      cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode);
1335
11.9k
   } else
1336
4.30M
#endif
1337
4.30M
   {
1338
4.30M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1339
4.30M
   }
1340
1341
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1342
4.31M
   if (ctx->resynth)
1343
3.01M
   {
1344
      /* Undo the sample reorganization going from time order to frequency order */
1345
3.01M
      if (B0>1)
1346
819k
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1347
1348
      /* Undo time-freq changes that we did earlier */
1349
3.01M
      N_B = N_B0;
1350
3.01M
      B = B0;
1351
3.59M
      for (k=0;k<time_divide;k++)
1352
581k
      {
1353
581k
         B >>= 1;
1354
581k
         N_B <<= 1;
1355
581k
         cm |= cm>>B;
1356
581k
         haar1(X, N_B, B);
1357
581k
      }
1358
1359
3.34M
      for (k=0;k<recombine;k++)
1360
335k
      {
1361
335k
         static const unsigned char bit_deinterleave_table[16]={
1362
335k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1363
335k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1364
335k
         };
1365
335k
         cm = bit_deinterleave_table[cm];
1366
335k
         haar1(X, N0>>k, 1<<k);
1367
335k
      }
1368
3.01M
      B<<=recombine;
1369
1370
      /* Scale output for later folding */
1371
3.01M
      if (lowband_out)
1372
2.04M
      {
1373
2.04M
         int j;
1374
2.04M
         opus_val16 n;
1375
2.04M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1376
21.4M
         for (j=0;j<N0;j++)
1377
19.4M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1378
2.04M
      }
1379
3.01M
      cm &= (1<<B)-1;
1380
3.01M
   }
1381
4.31M
   return cm;
1382
4.75M
}
bands.c:quant_band
Line
Count
Source
1257
4.26M
{
1258
4.26M
   int N0=N;
1259
4.26M
   int N_B=N;
1260
4.26M
   int N_B0;
1261
4.26M
   int B0=B;
1262
4.26M
   int time_divide=0;
1263
4.26M
   int recombine=0;
1264
4.26M
   int longBlocks;
1265
4.26M
   unsigned cm=0;
1266
4.26M
   int k;
1267
4.26M
   int encode;
1268
4.26M
   int tf_change;
1269
1270
4.26M
   encode = ctx->encode;
1271
4.26M
   tf_change = ctx->tf_change;
1272
1273
4.26M
   longBlocks = B0==1;
1274
1275
4.26M
   N_B = celt_udiv(N_B, B);
1276
1277
   /* Special case for one sample */
1278
4.26M
   if (N==1)
1279
661k
   {
1280
661k
      return quant_band_n1(ctx, X, NULL, lowband_out);
1281
661k
   }
1282
1283
3.60M
   if (tf_change>0)
1284
355k
      recombine = tf_change;
1285
   /* Band recombining to increase frequency resolution */
1286
1287
3.60M
   if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
1288
434k
   {
1289
434k
      OPUS_COPY(lowband_scratch, lowband, N);
1290
434k
      lowband = lowband_scratch;
1291
434k
   }
1292
1293
4.23M
   for (k=0;k<recombine;k++)
1294
630k
   {
1295
630k
      static const unsigned char bit_interleave_table[16]={
1296
630k
            0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
1297
630k
      };
1298
630k
      if (encode)
1299
82.3k
         haar1(X, N>>k, 1<<k);
1300
630k
      if (lowband)
1301
375k
         haar1(lowband, N>>k, 1<<k);
1302
630k
      fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
1303
630k
   }
1304
3.60M
   B>>=recombine;
1305
3.60M
   N_B<<=recombine;
1306
1307
   /* Increasing the time resolution */
1308
4.11M
   while ((N_B&1) == 0 && tf_change<0)
1309
508k
   {
1310
508k
      if (encode)
1311
246k
         haar1(X, N_B, B);
1312
508k
      if (lowband)
1313
233k
         haar1(lowband, N_B, B);
1314
508k
      fill |= fill<<B;
1315
508k
      B <<= 1;
1316
508k
      N_B >>= 1;
1317
508k
      time_divide++;
1318
508k
      tf_change++;
1319
508k
   }
1320
3.60M
   B0=B;
1321
3.60M
   N_B0 = N_B;
1322
1323
   /* Reorganize the samples in time order instead of frequency order */
1324
3.60M
   if (B0>1)
1325
824k
   {
1326
824k
      if (encode)
1327
578k
         deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1328
824k
      if (lowband)
1329
275k
         deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
1330
824k
   }
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
3.60M
   {
1338
3.60M
      cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b));
1339
3.60M
   }
1340
1341
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1342
3.60M
   if (ctx->resynth)
1343
2.72M
   {
1344
      /* Undo the sample reorganization going from time order to frequency order */
1345
2.72M
      if (B0>1)
1346
438k
         interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
1347
1348
      /* Undo time-freq changes that we did earlier */
1349
2.72M
      N_B = N_B0;
1350
2.72M
      B = B0;
1351
3.09M
      for (k=0;k<time_divide;k++)
1352
363k
      {
1353
363k
         B >>= 1;
1354
363k
         N_B <<= 1;
1355
363k
         cm |= cm>>B;
1356
363k
         haar1(X, N_B, B);
1357
363k
      }
1358
1359
3.31M
      for (k=0;k<recombine;k++)
1360
582k
      {
1361
582k
         static const unsigned char bit_deinterleave_table[16]={
1362
582k
               0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F,
1363
582k
               0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF
1364
582k
         };
1365
582k
         cm = bit_deinterleave_table[cm];
1366
582k
         haar1(X, N0>>k, 1<<k);
1367
582k
      }
1368
2.72M
      B<<=recombine;
1369
1370
      /* Scale output for later folding */
1371
2.72M
      if (lowband_out)
1372
1.93M
      {
1373
1.93M
         int j;
1374
1.93M
         opus_val16 n;
1375
1.93M
         n = celt_sqrt(SHL32(EXTEND32(N0),22));
1376
20.7M
         for (j=0;j<N0;j++)
1377
18.7M
            lowband_out[j] = MULT16_32_Q15(n,X[j]);
1378
1.93M
      }
1379
2.72M
      cm &= (1<<B)-1;
1380
2.72M
   }
1381
3.60M
   return cm;
1382
4.26M
}
1383
1384
#ifdef FIXED_POINT
1385
2.68M
#define MIN_STEREO_ENERGY 2
1386
#else
1387
2.68M
#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
5.48M
{
1397
5.48M
   int imid=0, iside=0;
1398
5.48M
   int inv = 0;
1399
5.48M
   opus_val32 mid=0, side=0;
1400
5.48M
   unsigned cm=0;
1401
5.48M
   int mbits, sbits, delta;
1402
5.48M
   int itheta;
1403
5.48M
   int qalloc;
1404
5.48M
   struct split_ctx sctx;
1405
5.48M
   int orig_fill;
1406
5.48M
   int encode;
1407
5.48M
   ec_ctx *ec;
1408
1409
5.48M
   encode = ctx->encode;
1410
5.48M
   ec = ctx->ec;
1411
1412
   /* Special case for one sample */
1413
5.48M
   if (N==1)
1414
997k
   {
1415
997k
      return quant_band_n1(ctx, X, Y, lowband_out);
1416
997k
   }
1417
1418
4.49M
   orig_fill = fill;
1419
1420
4.49M
   if (encode) {
1421
1.83M
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1422
153k
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1423
135k
         else OPUS_COPY(X, Y, N);
1424
153k
      }
1425
1.83M
   }
1426
4.49M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1427
4.49M
   inv = sctx.inv;
1428
4.49M
   imid = sctx.imid;
1429
4.49M
   iside = sctx.iside;
1430
4.49M
   delta = sctx.delta;
1431
4.49M
   itheta = sctx.itheta;
1432
4.49M
   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
965k
   mid = SHL32(EXTEND32(imid), 16);
1441
965k
   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
4.49M
   if (N==2)
1459
1.09M
   {
1460
1.09M
      int c;
1461
1.09M
      int sign=0;
1462
1.09M
      celt_norm *x2, *y2;
1463
1.09M
      mbits = b;
1464
1.09M
      sbits = 0;
1465
      /* Only need one bit for the side. */
1466
1.09M
      if (itheta != 0 && itheta != 16384)
1467
239k
         sbits = 1<<BITRES;
1468
1.09M
      mbits -= sbits;
1469
1.09M
      c = itheta > 8192;
1470
1.09M
      ctx->remaining_bits -= qalloc+sbits;
1471
1472
1.09M
      x2 = c ? Y : X;
1473
1.09M
      y2 = c ? X : Y;
1474
1.09M
      if (sbits)
1475
239k
      {
1476
239k
         if (encode)
1477
186k
         {
1478
            /* Here we only need to encode a sign for the side. */
1479
            /* FIXME: Need to increase fixed-point precision? */
1480
186k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1481
186k
            ec_enc_bits(ec, sign, 1);
1482
186k
         } else {
1483
53.1k
            sign = ec_dec_bits(ec, 1);
1484
53.1k
         }
1485
239k
      }
1486
1.09M
      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.09M
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1490
1.09M
            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.09M
      y2[0] = -sign*x2[1];
1494
1.09M
      y2[1] = sign*x2[0];
1495
1.09M
      if (ctx->resynth)
1496
973k
      {
1497
973k
         celt_norm tmp;
1498
973k
         X[0] = MULT32_32_Q31(mid, X[0]);
1499
973k
         X[1] = MULT32_32_Q31(mid, X[1]);
1500
973k
         Y[0] = MULT32_32_Q31(side, Y[0]);
1501
973k
         Y[1] = MULT32_32_Q31(side, Y[1]);
1502
973k
         tmp = X[0];
1503
973k
         X[0] = SUB32(tmp,Y[0]);
1504
973k
         Y[0] = ADD32(tmp,Y[0]);
1505
973k
         tmp = X[1];
1506
973k
         X[1] = SUB32(tmp,Y[1]);
1507
973k
         Y[1] = ADD32(tmp,Y[1]);
1508
973k
      }
1509
3.39M
   } else {
1510
      /* "Normal" split code */
1511
3.39M
      opus_int32 rebalance;
1512
1513
3.39M
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1514
3.39M
      sbits = b-mbits;
1515
3.39M
      ctx->remaining_bits -= qalloc;
1516
1517
3.39M
      rebalance = ctx->remaining_bits;
1518
3.39M
      if (mbits >= sbits)
1519
2.99M
      {
1520
#ifdef ENABLE_QEXT
1521
         int qext_extra = 0;
1522
         /* Reallocate any mid bits that cannot be used to extra mid bits. */
1523
1.67M
         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
2.99M
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1528
2.99M
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1529
2.99M
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1530
2.99M
         if (rebalance > 3<<BITRES && itheta!=0)
1531
46.3k
            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.67M
         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
2.99M
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1539
2.99M
      } else {
1540
#ifdef ENABLE_QEXT
1541
         int qext_extra = 0;
1542
         /* Reallocate any side bits that cannot be used to extra side bits. */
1543
275k
         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
403k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1548
403k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1549
403k
         if (rebalance > 3<<BITRES && itheta!=16384)
1550
28.2k
            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
275k
         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
403k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1558
403k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1559
403k
      }
1560
3.39M
   }
1561
1562
1563
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1564
4.49M
   if (ctx->resynth)
1565
3.84M
   {
1566
3.84M
      if (N!=2)
1567
2.86M
         stereo_merge(X, Y, mid, N, ctx->arch);
1568
3.84M
      if (inv)
1569
94.4k
      {
1570
94.4k
         int j;
1571
1.29M
         for (j=0;j<N;j++)
1572
1.20M
            Y[j] = -Y[j];
1573
94.4k
      }
1574
3.84M
   }
1575
4.49M
   return cm;
1576
5.48M
}
bands.c:quant_band_stereo
Line
Count
Source
1396
1.13M
{
1397
1.13M
   int imid=0, iside=0;
1398
1.13M
   int inv = 0;
1399
1.13M
   opus_val32 mid=0, side=0;
1400
1.13M
   unsigned cm=0;
1401
1.13M
   int mbits, sbits, delta;
1402
1.13M
   int itheta;
1403
1.13M
   int qalloc;
1404
1.13M
   struct split_ctx sctx;
1405
1.13M
   int orig_fill;
1406
1.13M
   int encode;
1407
1.13M
   ec_ctx *ec;
1408
1409
1.13M
   encode = ctx->encode;
1410
1.13M
   ec = ctx->ec;
1411
1412
   /* Special case for one sample */
1413
1.13M
   if (N==1)
1414
173k
   {
1415
173k
      return quant_band_n1(ctx, X, Y, lowband_out);
1416
173k
   }
1417
1418
965k
   orig_fill = fill;
1419
1420
965k
   if (encode) {
1421
284k
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1422
16.4k
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1423
13.2k
         else OPUS_COPY(X, Y, N);
1424
16.4k
      }
1425
284k
   }
1426
965k
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1427
965k
   inv = sctx.inv;
1428
965k
   imid = sctx.imid;
1429
965k
   iside = sctx.iside;
1430
965k
   delta = sctx.delta;
1431
965k
   itheta = sctx.itheta;
1432
965k
   qalloc = sctx.qalloc;
1433
965k
#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
965k
   mid = SHL32(EXTEND32(imid), 16);
1441
965k
   side = SHL32(EXTEND32(iside), 16);
1442
965k
# 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
965k
   if (N==2)
1459
241k
   {
1460
241k
      int c;
1461
241k
      int sign=0;
1462
241k
      celt_norm *x2, *y2;
1463
241k
      mbits = b;
1464
241k
      sbits = 0;
1465
      /* Only need one bit for the side. */
1466
241k
      if (itheta != 0 && itheta != 16384)
1467
34.6k
         sbits = 1<<BITRES;
1468
241k
      mbits -= sbits;
1469
241k
      c = itheta > 8192;
1470
241k
      ctx->remaining_bits -= qalloc+sbits;
1471
1472
241k
      x2 = c ? Y : X;
1473
241k
      y2 = c ? X : Y;
1474
241k
      if (sbits)
1475
34.6k
      {
1476
34.6k
         if (encode)
1477
25.0k
         {
1478
            /* Here we only need to encode a sign for the side. */
1479
            /* FIXME: Need to increase fixed-point precision? */
1480
25.0k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1481
25.0k
            ec_enc_bits(ec, sign, 1);
1482
25.0k
         } else {
1483
9.58k
            sign = ec_dec_bits(ec, 1);
1484
9.58k
         }
1485
34.6k
      }
1486
241k
      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
241k
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1490
241k
            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
241k
      y2[0] = -sign*x2[1];
1494
241k
      y2[1] = sign*x2[0];
1495
241k
      if (ctx->resynth)
1496
224k
      {
1497
224k
         celt_norm tmp;
1498
224k
         X[0] = MULT32_32_Q31(mid, X[0]);
1499
224k
         X[1] = MULT32_32_Q31(mid, X[1]);
1500
224k
         Y[0] = MULT32_32_Q31(side, Y[0]);
1501
224k
         Y[1] = MULT32_32_Q31(side, Y[1]);
1502
224k
         tmp = X[0];
1503
224k
         X[0] = SUB32(tmp,Y[0]);
1504
224k
         Y[0] = ADD32(tmp,Y[0]);
1505
224k
         tmp = X[1];
1506
224k
         X[1] = SUB32(tmp,Y[1]);
1507
224k
         Y[1] = ADD32(tmp,Y[1]);
1508
224k
      }
1509
723k
   } else {
1510
      /* "Normal" split code */
1511
723k
      opus_int32 rebalance;
1512
1513
723k
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1514
723k
      sbits = b-mbits;
1515
723k
      ctx->remaining_bits -= qalloc;
1516
1517
723k
      rebalance = ctx->remaining_bits;
1518
723k
      if (mbits >= sbits)
1519
660k
      {
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
660k
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1528
660k
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1529
660k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1530
660k
         if (rebalance > 3<<BITRES && itheta!=0)
1531
6.70k
            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
660k
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1539
660k
      } 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
63.5k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1548
63.5k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1549
63.5k
         if (rebalance > 3<<BITRES && itheta!=16384)
1550
4.51k
            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
63.5k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1558
63.5k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1559
63.5k
      }
1560
723k
   }
1561
1562
1563
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1564
965k
   if (ctx->resynth)
1565
857k
   {
1566
857k
      if (N!=2)
1567
633k
         stereo_merge(X, Y, mid, N, ctx->arch);
1568
857k
      if (inv)
1569
17.6k
      {
1570
17.6k
         int j;
1571
246k
         for (j=0;j<N;j++)
1572
229k
            Y[j] = -Y[j];
1573
17.6k
      }
1574
857k
   }
1575
965k
   return cm;
1576
1.13M
}
bands.c:quant_band_stereo
Line
Count
Source
1396
1.60M
{
1397
1.60M
   int imid=0, iside=0;
1398
1.60M
   int inv = 0;
1399
1.60M
   opus_val32 mid=0, side=0;
1400
1.60M
   unsigned cm=0;
1401
1.60M
   int mbits, sbits, delta;
1402
1.60M
   int itheta;
1403
1.60M
   int qalloc;
1404
1.60M
   struct split_ctx sctx;
1405
1.60M
   int orig_fill;
1406
1.60M
   int encode;
1407
1.60M
   ec_ctx *ec;
1408
1409
1.60M
   encode = ctx->encode;
1410
1.60M
   ec = ctx->ec;
1411
1412
   /* Special case for one sample */
1413
1.60M
   if (N==1)
1414
325k
   {
1415
325k
      return quant_band_n1(ctx, X, Y, lowband_out);
1416
325k
   }
1417
1418
1.28M
   orig_fill = fill;
1419
1420
1.28M
   if (encode) {
1421
632k
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1422
60.2k
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1423
54.5k
         else OPUS_COPY(X, Y, N);
1424
60.2k
      }
1425
632k
   }
1426
1.28M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1427
1.28M
   inv = sctx.inv;
1428
1.28M
   imid = sctx.imid;
1429
1.28M
   iside = sctx.iside;
1430
1.28M
   delta = sctx.delta;
1431
1.28M
   itheta = sctx.itheta;
1432
1.28M
   qalloc = sctx.qalloc;
1433
1.28M
#ifdef FIXED_POINT
1434
1.28M
# ifdef ENABLE_QEXT
1435
1.28M
   (void)imid;
1436
1.28M
   (void)iside;
1437
1.28M
   mid = celt_cos_norm32(sctx.itheta_q30);
1438
1.28M
   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.28M
   if (N==2)
1459
305k
   {
1460
305k
      int c;
1461
305k
      int sign=0;
1462
305k
      celt_norm *x2, *y2;
1463
305k
      mbits = b;
1464
305k
      sbits = 0;
1465
      /* Only need one bit for the side. */
1466
305k
      if (itheta != 0 && itheta != 16384)
1467
85.1k
         sbits = 1<<BITRES;
1468
305k
      mbits -= sbits;
1469
305k
      c = itheta > 8192;
1470
305k
      ctx->remaining_bits -= qalloc+sbits;
1471
1472
305k
      x2 = c ? Y : X;
1473
305k
      y2 = c ? X : Y;
1474
305k
      if (sbits)
1475
85.1k
      {
1476
85.1k
         if (encode)
1477
68.1k
         {
1478
            /* Here we only need to encode a sign for the side. */
1479
            /* FIXME: Need to increase fixed-point precision? */
1480
68.1k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1481
68.1k
            ec_enc_bits(ec, sign, 1);
1482
68.1k
         } else {
1483
17.0k
            sign = ec_dec_bits(ec, 1);
1484
17.0k
         }
1485
85.1k
      }
1486
305k
      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
305k
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1490
305k
            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
305k
      y2[0] = -sign*x2[1];
1494
305k
      y2[1] = sign*x2[0];
1495
305k
      if (ctx->resynth)
1496
262k
      {
1497
262k
         celt_norm tmp;
1498
262k
         X[0] = MULT32_32_Q31(mid, X[0]);
1499
262k
         X[1] = MULT32_32_Q31(mid, X[1]);
1500
262k
         Y[0] = MULT32_32_Q31(side, Y[0]);
1501
262k
         Y[1] = MULT32_32_Q31(side, Y[1]);
1502
262k
         tmp = X[0];
1503
262k
         X[0] = SUB32(tmp,Y[0]);
1504
262k
         Y[0] = ADD32(tmp,Y[0]);
1505
262k
         tmp = X[1];
1506
262k
         X[1] = SUB32(tmp,Y[1]);
1507
262k
         Y[1] = ADD32(tmp,Y[1]);
1508
262k
      }
1509
975k
   } else {
1510
      /* "Normal" split code */
1511
975k
      opus_int32 rebalance;
1512
1513
975k
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1514
975k
      sbits = b-mbits;
1515
975k
      ctx->remaining_bits -= qalloc;
1516
1517
975k
      rebalance = ctx->remaining_bits;
1518
975k
      if (mbits >= sbits)
1519
837k
      {
1520
837k
#ifdef ENABLE_QEXT
1521
837k
         int qext_extra = 0;
1522
         /* Reallocate any mid bits that cannot be used to extra mid bits. */
1523
837k
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, mbits - cap[ctx->i]/2));
1524
837k
#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
837k
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1528
837k
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1529
837k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1530
837k
         if (rebalance > 3<<BITRES && itheta!=0)
1531
16.4k
            sbits += rebalance - (3<<BITRES);
1532
837k
#ifdef ENABLE_QEXT
1533
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the mid. */
1534
837k
         if (ctx->extra_bands) sbits = IMIN(sbits, ctx->remaining_bits);
1535
837k
#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
837k
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1539
837k
      } else {
1540
137k
#ifdef ENABLE_QEXT
1541
137k
         int qext_extra = 0;
1542
         /* Reallocate any side bits that cannot be used to extra side bits. */
1543
137k
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, sbits - cap[ctx->i]/2));
1544
137k
#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
137k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1548
137k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1549
137k
         if (rebalance > 3<<BITRES && itheta!=16384)
1550
9.63k
            mbits += rebalance - (3<<BITRES);
1551
137k
#ifdef ENABLE_QEXT
1552
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the side. */
1553
137k
         if (ctx->extra_bands) mbits = IMIN(mbits, ctx->remaining_bits);
1554
137k
#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
137k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1558
137k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1559
137k
      }
1560
975k
   }
1561
1562
1563
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1564
1.28M
   if (ctx->resynth)
1565
1.06M
   {
1566
1.06M
      if (N!=2)
1567
800k
         stereo_merge(X, Y, mid, N, ctx->arch);
1568
1.06M
      if (inv)
1569
29.6k
      {
1570
29.6k
         int j;
1571
401k
         for (j=0;j<N;j++)
1572
371k
            Y[j] = -Y[j];
1573
29.6k
      }
1574
1.06M
   }
1575
1.28M
   return cm;
1576
1.60M
}
bands.c:quant_band_stereo
Line
Count
Source
1396
1.60M
{
1397
1.60M
   int imid=0, iside=0;
1398
1.60M
   int inv = 0;
1399
1.60M
   opus_val32 mid=0, side=0;
1400
1.60M
   unsigned cm=0;
1401
1.60M
   int mbits, sbits, delta;
1402
1.60M
   int itheta;
1403
1.60M
   int qalloc;
1404
1.60M
   struct split_ctx sctx;
1405
1.60M
   int orig_fill;
1406
1.60M
   int encode;
1407
1.60M
   ec_ctx *ec;
1408
1409
1.60M
   encode = ctx->encode;
1410
1.60M
   ec = ctx->ec;
1411
1412
   /* Special case for one sample */
1413
1.60M
   if (N==1)
1414
325k
   {
1415
325k
      return quant_band_n1(ctx, X, Y, lowband_out);
1416
325k
   }
1417
1418
1.28M
   orig_fill = fill;
1419
1420
1.28M
   if (encode) {
1421
632k
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1422
60.2k
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1423
54.5k
         else OPUS_COPY(X, Y, N);
1424
60.2k
      }
1425
632k
   }
1426
1.28M
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1427
1.28M
   inv = sctx.inv;
1428
1.28M
   imid = sctx.imid;
1429
1.28M
   iside = sctx.iside;
1430
1.28M
   delta = sctx.delta;
1431
1.28M
   itheta = sctx.itheta;
1432
1.28M
   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.28M
# ifdef ENABLE_QEXT
1445
1.28M
   (void)imid;
1446
1.28M
   (void)iside;
1447
1.28M
   mid = cos(.5*M_PI*sctx.itheta_q30*(1.f/(1<<30)));
1448
1.28M
   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.28M
#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.28M
   if (N==2)
1459
305k
   {
1460
305k
      int c;
1461
305k
      int sign=0;
1462
305k
      celt_norm *x2, *y2;
1463
305k
      mbits = b;
1464
305k
      sbits = 0;
1465
      /* Only need one bit for the side. */
1466
305k
      if (itheta != 0 && itheta != 16384)
1467
85.1k
         sbits = 1<<BITRES;
1468
305k
      mbits -= sbits;
1469
305k
      c = itheta > 8192;
1470
305k
      ctx->remaining_bits -= qalloc+sbits;
1471
1472
305k
      x2 = c ? Y : X;
1473
305k
      y2 = c ? X : Y;
1474
305k
      if (sbits)
1475
85.1k
      {
1476
85.1k
         if (encode)
1477
68.1k
         {
1478
            /* Here we only need to encode a sign for the side. */
1479
            /* FIXME: Need to increase fixed-point precision? */
1480
68.1k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1481
68.1k
            ec_enc_bits(ec, sign, 1);
1482
68.1k
         } else {
1483
17.0k
            sign = ec_dec_bits(ec, 1);
1484
17.0k
         }
1485
85.1k
      }
1486
305k
      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
305k
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1490
305k
            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
305k
      y2[0] = -sign*x2[1];
1494
305k
      y2[1] = sign*x2[0];
1495
305k
      if (ctx->resynth)
1496
262k
      {
1497
262k
         celt_norm tmp;
1498
262k
         X[0] = MULT32_32_Q31(mid, X[0]);
1499
262k
         X[1] = MULT32_32_Q31(mid, X[1]);
1500
262k
         Y[0] = MULT32_32_Q31(side, Y[0]);
1501
262k
         Y[1] = MULT32_32_Q31(side, Y[1]);
1502
262k
         tmp = X[0];
1503
262k
         X[0] = SUB32(tmp,Y[0]);
1504
262k
         Y[0] = ADD32(tmp,Y[0]);
1505
262k
         tmp = X[1];
1506
262k
         X[1] = SUB32(tmp,Y[1]);
1507
262k
         Y[1] = ADD32(tmp,Y[1]);
1508
262k
      }
1509
975k
   } else {
1510
      /* "Normal" split code */
1511
975k
      opus_int32 rebalance;
1512
1513
975k
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1514
975k
      sbits = b-mbits;
1515
975k
      ctx->remaining_bits -= qalloc;
1516
1517
975k
      rebalance = ctx->remaining_bits;
1518
975k
      if (mbits >= sbits)
1519
837k
      {
1520
837k
#ifdef ENABLE_QEXT
1521
837k
         int qext_extra = 0;
1522
         /* Reallocate any mid bits that cannot be used to extra mid bits. */
1523
837k
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, mbits - cap[ctx->i]/2));
1524
837k
#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
837k
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1528
837k
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1529
837k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1530
837k
         if (rebalance > 3<<BITRES && itheta!=0)
1531
16.4k
            sbits += rebalance - (3<<BITRES);
1532
837k
#ifdef ENABLE_QEXT
1533
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the mid. */
1534
837k
         if (ctx->extra_bands) sbits = IMIN(sbits, ctx->remaining_bits);
1535
837k
#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
837k
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1539
837k
      } else {
1540
137k
#ifdef ENABLE_QEXT
1541
137k
         int qext_extra = 0;
1542
         /* Reallocate any side bits that cannot be used to extra side bits. */
1543
137k
         if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, sbits - cap[ctx->i]/2));
1544
137k
#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
137k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1548
137k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1549
137k
         if (rebalance > 3<<BITRES && itheta!=16384)
1550
9.63k
            mbits += rebalance - (3<<BITRES);
1551
137k
#ifdef ENABLE_QEXT
1552
         /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the side. */
1553
137k
         if (ctx->extra_bands) mbits = IMIN(mbits, ctx->remaining_bits);
1554
137k
#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
137k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1558
137k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1559
137k
      }
1560
975k
   }
1561
1562
1563
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1564
1.28M
   if (ctx->resynth)
1565
1.06M
   {
1566
1.06M
      if (N!=2)
1567
800k
         stereo_merge(X, Y, mid, N, ctx->arch);
1568
1.06M
      if (inv)
1569
29.6k
      {
1570
29.6k
         int j;
1571
401k
         for (j=0;j<N;j++)
1572
371k
            Y[j] = -Y[j];
1573
29.6k
      }
1574
1.06M
   }
1575
1.28M
   return cm;
1576
1.60M
}
bands.c:quant_band_stereo
Line
Count
Source
1396
1.13M
{
1397
1.13M
   int imid=0, iside=0;
1398
1.13M
   int inv = 0;
1399
1.13M
   opus_val32 mid=0, side=0;
1400
1.13M
   unsigned cm=0;
1401
1.13M
   int mbits, sbits, delta;
1402
1.13M
   int itheta;
1403
1.13M
   int qalloc;
1404
1.13M
   struct split_ctx sctx;
1405
1.13M
   int orig_fill;
1406
1.13M
   int encode;
1407
1.13M
   ec_ctx *ec;
1408
1409
1.13M
   encode = ctx->encode;
1410
1.13M
   ec = ctx->ec;
1411
1412
   /* Special case for one sample */
1413
1.13M
   if (N==1)
1414
173k
   {
1415
173k
      return quant_band_n1(ctx, X, Y, lowband_out);
1416
173k
   }
1417
1418
965k
   orig_fill = fill;
1419
1420
965k
   if (encode) {
1421
284k
      if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) {
1422
16.4k
         if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N);
1423
13.2k
         else OPUS_COPY(X, Y, N);
1424
16.4k
      }
1425
284k
   }
1426
965k
   compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b));
1427
965k
   inv = sctx.inv;
1428
965k
   imid = sctx.imid;
1429
965k
   iside = sctx.iside;
1430
965k
   delta = sctx.delta;
1431
965k
   itheta = sctx.itheta;
1432
965k
   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
965k
   mid = (1.f/32768)*imid;
1451
965k
   side = (1.f/32768)*iside;
1452
965k
# endif
1453
965k
#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
965k
   if (N==2)
1459
241k
   {
1460
241k
      int c;
1461
241k
      int sign=0;
1462
241k
      celt_norm *x2, *y2;
1463
241k
      mbits = b;
1464
241k
      sbits = 0;
1465
      /* Only need one bit for the side. */
1466
241k
      if (itheta != 0 && itheta != 16384)
1467
34.6k
         sbits = 1<<BITRES;
1468
241k
      mbits -= sbits;
1469
241k
      c = itheta > 8192;
1470
241k
      ctx->remaining_bits -= qalloc+sbits;
1471
1472
241k
      x2 = c ? Y : X;
1473
241k
      y2 = c ? X : Y;
1474
241k
      if (sbits)
1475
34.6k
      {
1476
34.6k
         if (encode)
1477
25.0k
         {
1478
            /* Here we only need to encode a sign for the side. */
1479
            /* FIXME: Need to increase fixed-point precision? */
1480
25.0k
            sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0;
1481
25.0k
            ec_enc_bits(ec, sign, 1);
1482
25.0k
         } else {
1483
9.58k
            sign = ec_dec_bits(ec, 1);
1484
9.58k
         }
1485
34.6k
      }
1486
241k
      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
241k
      cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1490
241k
            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
241k
      y2[0] = -sign*x2[1];
1494
241k
      y2[1] = sign*x2[0];
1495
241k
      if (ctx->resynth)
1496
224k
      {
1497
224k
         celt_norm tmp;
1498
224k
         X[0] = MULT32_32_Q31(mid, X[0]);
1499
224k
         X[1] = MULT32_32_Q31(mid, X[1]);
1500
224k
         Y[0] = MULT32_32_Q31(side, Y[0]);
1501
224k
         Y[1] = MULT32_32_Q31(side, Y[1]);
1502
224k
         tmp = X[0];
1503
224k
         X[0] = SUB32(tmp,Y[0]);
1504
224k
         Y[0] = ADD32(tmp,Y[0]);
1505
224k
         tmp = X[1];
1506
224k
         X[1] = SUB32(tmp,Y[1]);
1507
224k
         Y[1] = ADD32(tmp,Y[1]);
1508
224k
      }
1509
723k
   } else {
1510
      /* "Normal" split code */
1511
723k
      opus_int32 rebalance;
1512
1513
723k
      mbits = IMAX(0, IMIN(b, (b-delta)/2));
1514
723k
      sbits = b-mbits;
1515
723k
      ctx->remaining_bits -= qalloc;
1516
1517
723k
      rebalance = ctx->remaining_bits;
1518
723k
      if (mbits >= sbits)
1519
660k
      {
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
660k
         cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1528
660k
               lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra));
1529
660k
         rebalance = mbits - (rebalance-ctx->remaining_bits);
1530
660k
         if (rebalance > 3<<BITRES && itheta!=0)
1531
6.70k
            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
660k
         cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra));
1539
660k
      } 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
63.5k
         cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra));
1548
63.5k
         rebalance = sbits - (rebalance-ctx->remaining_bits);
1549
63.5k
         if (rebalance > 3<<BITRES && itheta!=16384)
1550
4.51k
            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
63.5k
         cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE,
1558
63.5k
               lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra));
1559
63.5k
      }
1560
723k
   }
1561
1562
1563
   /* This code is used by the decoder and by the resynthesis-enabled encoder */
1564
965k
   if (ctx->resynth)
1565
857k
   {
1566
857k
      if (N!=2)
1567
633k
         stereo_merge(X, Y, mid, N, ctx->arch);
1568
857k
      if (inv)
1569
17.6k
      {
1570
17.6k
         int j;
1571
246k
         for (j=0;j<N;j++)
1572
229k
            Y[j] = -Y[j];
1573
17.6k
      }
1574
857k
   }
1575
965k
   return cm;
1576
1.13M
}
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
509k
{
1581
509k
   int n1, n2;
1582
509k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1583
509k
   n1 = M*(eBands[start+1]-eBands[start]);
1584
509k
   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
509k
   OPUS_COPY(&norm[n1], &norm[2*n1 - n2], n2-n1);
1588
509k
   if (dual_stereo)
1589
32.4k
      OPUS_COPY(&norm2[n1], &norm2[2*n1 - n2], n2-n1);
1590
509k
}
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
966k
{
1602
966k
   int i;
1603
966k
   opus_int32 remaining_bits;
1604
966k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1605
966k
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1606
966k
   VARDECL(celt_norm, _norm);
1607
966k
   VARDECL(celt_norm, _lowband_scratch);
1608
966k
   VARDECL(celt_norm, X_save);
1609
966k
   VARDECL(celt_norm, Y_save);
1610
966k
   VARDECL(celt_norm, X_save2);
1611
966k
   VARDECL(celt_norm, Y_save2);
1612
966k
   VARDECL(celt_norm, norm_save2);
1613
966k
   int resynth_alloc;
1614
966k
   celt_norm *lowband_scratch;
1615
966k
   int B;
1616
966k
   int M;
1617
966k
   int lowband_offset;
1618
966k
   int update_lowband = 1;
1619
966k
   int C = Y_ != NULL ? 2 : 1;
1620
966k
   int norm_offset;
1621
966k
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1622
#ifdef RESYNTH
1623
   int resynth = 1;
1624
#else
1625
966k
   int resynth = !encode || theta_rdo;
1626
966k
#endif
1627
966k
   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
966k
   SAVE_STACK;
1634
1635
966k
   M = 1<<LM;
1636
966k
   B = shortBlocks ? M : 1;
1637
966k
   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
966k
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1641
966k
   norm = _norm;
1642
966k
   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
966k
   if (encode && resynth)
1648
68.0k
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1649
898k
   else
1650
898k
      resynth_alloc = ALLOC_NONE;
1651
966k
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1652
966k
   if (encode && resynth)
1653
68.0k
      lowband_scratch = _lowband_scratch;
1654
898k
   else
1655
898k
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1656
966k
   ALLOC(X_save, resynth_alloc, celt_norm);
1657
966k
   ALLOC(Y_save, resynth_alloc, celt_norm);
1658
966k
   ALLOC(X_save2, resynth_alloc, celt_norm);
1659
966k
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1660
966k
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1661
1662
966k
   lowband_offset = 0;
1663
966k
   ctx.bandE = bandE;
1664
966k
   ctx.ec = ec;
1665
966k
   ctx.encode = encode;
1666
966k
   ctx.intensity = intensity;
1667
966k
   ctx.m = m;
1668
966k
   ctx.seed = *seed;
1669
966k
   ctx.spread = spread;
1670
966k
   ctx.arch = arch;
1671
966k
   ctx.disable_inv = disable_inv;
1672
966k
   ctx.resynth = resynth;
1673
966k
   ctx.theta_round = 0;
1674
#ifdef ENABLE_QEXT
1675
   ctx.ext_ec = ext_ec;
1676
   ctx.ext_total_bits = ext_total_bits;
1677
506k
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1678
506k
   if (ctx.extra_bands) theta_rdo = 0;
1679
#endif
1680
   /* Avoid injecting noise in the first band on transients. */
1681
966k
   ctx.avoid_split_noise = B > 1;
1682
15.3M
   for (i=start;i<end;i++)
1683
14.3M
   {
1684
14.3M
      opus_int32 tell;
1685
14.3M
      int b;
1686
14.3M
      int N;
1687
14.3M
      opus_int32 curr_balance;
1688
14.3M
      int effective_lowband=-1;
1689
14.3M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1690
14.3M
      int tf_change=0;
1691
14.3M
      unsigned x_cm;
1692
14.3M
      unsigned y_cm;
1693
14.3M
      int last;
1694
1695
14.3M
      ctx.i = i;
1696
14.3M
      last = (i==end-1);
1697
1698
14.3M
      X = X_+M*eBands[i];
1699
14.3M
      if (Y_!=NULL)
1700
5.49M
         Y = Y_+M*eBands[i];
1701
8.90M
      else
1702
8.90M
         Y = NULL;
1703
14.3M
      N = M*eBands[i+1]-M*eBands[i];
1704
14.3M
      celt_assert(N > 0);
1705
14.3M
      tell = ec_tell_frac(ec);
1706
1707
      /* Compute how many bits we want to allocate to this band */
1708
14.3M
      if (i != start)
1709
13.4M
         balance -= tell;
1710
14.3M
      remaining_bits = total_bits-tell-1;
1711
14.3M
      ctx.remaining_bits = remaining_bits;
1712
#ifdef ENABLE_QEXT
1713
7.35M
      if (i != start) {
1714
6.85M
         ext_balance += extra_pulses[i-1] + ext_tell;
1715
6.85M
      }
1716
      ext_tell = ec_tell_frac(ext_ec);
1717
      ctx.extra_bits = extra_pulses[i];
1718
7.35M
      if (i != start)
1719
6.85M
         ext_balance -= ext_tell;
1720
7.35M
      if (i <= codedBands-1)
1721
4.26M
      {
1722
4.26M
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1723
4.26M
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1724
4.26M
      } else {
1725
3.08M
         ext_b = 0;
1726
3.08M
      }
1727
#endif
1728
14.3M
      if (i <= codedBands-1)
1729
6.93M
      {
1730
6.93M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1731
6.93M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1732
7.45M
      } else {
1733
7.45M
         b = 0;
1734
7.45M
      }
1735
1736
14.3M
#ifndef DISABLE_UPDATE_DRAFT
1737
14.3M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1738
2.99M
            lowband_offset = i;
1739
14.3M
      if (i == start+1)
1740
966k
         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
14.3M
      tf_change = tf_res[i];
1747
14.3M
      ctx.tf_change = tf_change;
1748
14.3M
      if (i>=m->effEBands)
1749
33.6k
      {
1750
33.6k
         X=norm;
1751
33.6k
         if (Y_!=NULL)
1752
27.1k
            Y = norm;
1753
33.6k
         lowband_scratch = NULL;
1754
33.6k
      }
1755
14.3M
      if (last && !theta_rdo)
1756
898k
         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
14.3M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1761
9.13M
      {
1762
9.13M
         int fold_start;
1763
9.13M
         int fold_end;
1764
9.13M
         int fold_i;
1765
         /* This ensures we never repeat spectral content within one band */
1766
9.13M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1767
9.13M
         fold_start = lowband_offset;
1768
10.5M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1769
9.13M
         fold_end = lowband_offset-1;
1770
9.13M
#ifndef DISABLE_UPDATE_DRAFT
1771
22.6M
         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.13M
         x_cm = y_cm = 0;
1776
24.0M
         fold_i = fold_start; do {
1777
24.0M
           x_cm |= collapse_masks[fold_i*C+0];
1778
24.0M
           y_cm |= collapse_masks[fold_i*C+C-1];
1779
24.0M
         } while (++fold_i<fold_end);
1780
9.13M
      }
1781
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1782
         always) be non-zero. */
1783
5.26M
      else
1784
5.26M
         x_cm = y_cm = (1<<B)-1;
1785
1786
14.3M
      if (dual_stereo && i==intensity)
1787
55.2k
      {
1788
55.2k
         int j;
1789
1790
         /* Switch off dual stereo to do intensity. */
1791
55.2k
         dual_stereo = 0;
1792
55.2k
         if (resynth)
1793
1.23M
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1794
1.20M
               norm[j] = HALF32(norm[j]+norm2[j]);
1795
55.2k
      }
1796
14.3M
      if (dual_stereo)
1797
620k
      {
1798
620k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1799
620k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1800
620k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1801
620k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1802
620k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1803
620k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1804
13.7M
      } else {
1805
13.7M
         if (Y!=NULL)
1806
4.87M
         {
1807
4.87M
            if (theta_rdo && i < intensity)
1808
618k
            {
1809
618k
               ec_ctx ec_save, ec_save2;
1810
618k
               struct band_ctx ctx_save, ctx_save2;
1811
618k
               opus_val32 dist0, dist1;
1812
618k
               unsigned cm, cm2;
1813
618k
               int nstart_bytes, nend_bytes, save_bytes;
1814
618k
               unsigned char *bytes_buf;
1815
618k
               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
618k
               opus_val16 w[2];
1823
618k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1824
               /* Make a copy. */
1825
618k
               cm = x_cm|y_cm;
1826
618k
               ec_save = *ec;
1827
#ifdef ENABLE_QEXT
1828
               ext_ec_save = *ext_ec;
1829
#endif
1830
618k
               ctx_save = ctx;
1831
618k
               OPUS_COPY(X_save, X, N);
1832
618k
               OPUS_COPY(Y_save, Y, N);
1833
               /* Encode and round down. */
1834
618k
               ctx.theta_round = -1;
1835
618k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1836
618k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1837
618k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1838
618k
               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
618k
               cm2 = x_cm;
1842
618k
               ec_save2 = *ec;
1843
#ifdef ENABLE_QEXT
1844
               ext_ec_save2 = *ext_ec;
1845
#endif
1846
618k
               ctx_save2 = ctx;
1847
618k
               OPUS_COPY(X_save2, X, N);
1848
618k
               OPUS_COPY(Y_save2, Y, N);
1849
618k
               if (!last)
1850
610k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1851
618k
               nstart_bytes = ec_save.offs;
1852
618k
               nend_bytes = ec_save.storage;
1853
618k
               bytes_buf = ec_save.buf+nstart_bytes;
1854
618k
               save_bytes = nend_bytes-nstart_bytes;
1855
618k
               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
447k
               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
447k
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1862
#endif
1863
               /* Restore */
1864
618k
               *ec = ec_save;
1865
#ifdef ENABLE_QEXT
1866
               *ext_ec = ext_ec_save;
1867
#endif
1868
618k
               ctx = ctx_save;
1869
618k
               OPUS_COPY(X, X_save, N);
1870
618k
               OPUS_COPY(Y, Y_save, N);
1871
618k
#ifndef DISABLE_UPDATE_DRAFT
1872
618k
               if (i == start+1)
1873
51.3k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1874
618k
#endif
1875
               /* Encode and round up. */
1876
618k
               ctx.theta_round = 1;
1877
618k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1878
618k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1879
618k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1880
618k
               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
618k
               if (dist0 >= dist1) {
1882
418k
                  x_cm = cm2;
1883
418k
                  *ec = ec_save2;
1884
#ifdef ENABLE_QEXT
1885
                  *ext_ec = ext_ec_save2;
1886
#endif
1887
418k
                  ctx = ctx_save2;
1888
418k
                  OPUS_COPY(X, X_save2, N);
1889
418k
                  OPUS_COPY(Y, Y_save2, N);
1890
418k
                  if (!last)
1891
414k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1892
418k
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1893
#ifdef ENABLE_QEXT
1894
306k
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1895
#endif
1896
418k
               }
1897
4.25M
            } else {
1898
4.25M
               ctx.theta_round = 0;
1899
4.25M
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1900
4.25M
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1901
4.25M
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1902
4.25M
            }
1903
8.90M
         } else {
1904
8.90M
            x_cm = quant_band(&ctx, X, N, b, B,
1905
8.90M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1906
8.90M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1907
8.90M
         }
1908
13.7M
         y_cm = x_cm;
1909
13.7M
      }
1910
14.3M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1911
14.3M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1912
14.3M
      balance += pulses[i] + tell;
1913
1914
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1915
14.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
14.3M
      ctx.avoid_split_noise = 0;
1919
14.3M
   }
1920
966k
   *seed = ctx.seed;
1921
1922
966k
   RESTORE_STACK;
1923
966k
}
quant_all_bands
Line
Count
Source
1601
230k
{
1602
230k
   int i;
1603
230k
   opus_int32 remaining_bits;
1604
230k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1605
230k
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1606
230k
   VARDECL(celt_norm, _norm);
1607
230k
   VARDECL(celt_norm, _lowband_scratch);
1608
230k
   VARDECL(celt_norm, X_save);
1609
230k
   VARDECL(celt_norm, Y_save);
1610
230k
   VARDECL(celt_norm, X_save2);
1611
230k
   VARDECL(celt_norm, Y_save2);
1612
230k
   VARDECL(celt_norm, norm_save2);
1613
230k
   int resynth_alloc;
1614
230k
   celt_norm *lowband_scratch;
1615
230k
   int B;
1616
230k
   int M;
1617
230k
   int lowband_offset;
1618
230k
   int update_lowband = 1;
1619
230k
   int C = Y_ != NULL ? 2 : 1;
1620
230k
   int norm_offset;
1621
230k
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1622
#ifdef RESYNTH
1623
   int resynth = 1;
1624
#else
1625
230k
   int resynth = !encode || theta_rdo;
1626
230k
#endif
1627
230k
   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
230k
   SAVE_STACK;
1634
1635
230k
   M = 1<<LM;
1636
230k
   B = shortBlocks ? M : 1;
1637
230k
   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
230k
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1641
230k
   norm = _norm;
1642
230k
   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
230k
   if (encode && resynth)
1648
10.1k
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1649
220k
   else
1650
220k
      resynth_alloc = ALLOC_NONE;
1651
230k
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1652
230k
   if (encode && resynth)
1653
10.1k
      lowband_scratch = _lowband_scratch;
1654
220k
   else
1655
220k
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1656
230k
   ALLOC(X_save, resynth_alloc, celt_norm);
1657
230k
   ALLOC(Y_save, resynth_alloc, celt_norm);
1658
230k
   ALLOC(X_save2, resynth_alloc, celt_norm);
1659
230k
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1660
230k
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1661
1662
230k
   lowband_offset = 0;
1663
230k
   ctx.bandE = bandE;
1664
230k
   ctx.ec = ec;
1665
230k
   ctx.encode = encode;
1666
230k
   ctx.intensity = intensity;
1667
230k
   ctx.m = m;
1668
230k
   ctx.seed = *seed;
1669
230k
   ctx.spread = spread;
1670
230k
   ctx.arch = arch;
1671
230k
   ctx.disable_inv = disable_inv;
1672
230k
   ctx.resynth = resynth;
1673
230k
   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
230k
   ctx.avoid_split_noise = B > 1;
1682
3.75M
   for (i=start;i<end;i++)
1683
3.52M
   {
1684
3.52M
      opus_int32 tell;
1685
3.52M
      int b;
1686
3.52M
      int N;
1687
3.52M
      opus_int32 curr_balance;
1688
3.52M
      int effective_lowband=-1;
1689
3.52M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1690
3.52M
      int tf_change=0;
1691
3.52M
      unsigned x_cm;
1692
3.52M
      unsigned y_cm;
1693
3.52M
      int last;
1694
1695
3.52M
      ctx.i = i;
1696
3.52M
      last = (i==end-1);
1697
1698
3.52M
      X = X_+M*eBands[i];
1699
3.52M
      if (Y_!=NULL)
1700
1.16M
         Y = Y_+M*eBands[i];
1701
2.35M
      else
1702
2.35M
         Y = NULL;
1703
3.52M
      N = M*eBands[i+1]-M*eBands[i];
1704
3.52M
      celt_assert(N > 0);
1705
3.52M
      tell = ec_tell_frac(ec);
1706
1707
      /* Compute how many bits we want to allocate to this band */
1708
3.52M
      if (i != start)
1709
3.29M
         balance -= tell;
1710
3.52M
      remaining_bits = total_bits-tell-1;
1711
3.52M
      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
3.52M
      if (i <= codedBands-1)
1729
1.33M
      {
1730
1.33M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1731
1.33M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1732
2.18M
      } else {
1733
2.18M
         b = 0;
1734
2.18M
      }
1735
1736
3.52M
#ifndef DISABLE_UPDATE_DRAFT
1737
3.52M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1738
582k
            lowband_offset = i;
1739
3.52M
      if (i == start+1)
1740
230k
         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
3.52M
      tf_change = tf_res[i];
1747
3.52M
      ctx.tf_change = tf_change;
1748
3.52M
      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
3.52M
      if (last && !theta_rdo)
1756
220k
         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
3.52M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1761
2.39M
      {
1762
2.39M
         int fold_start;
1763
2.39M
         int fold_end;
1764
2.39M
         int fold_i;
1765
         /* This ensures we never repeat spectral content within one band */
1766
2.39M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1767
2.39M
         fold_start = lowband_offset;
1768
2.65M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1769
2.39M
         fold_end = lowband_offset-1;
1770
2.39M
#ifndef DISABLE_UPDATE_DRAFT
1771
6.39M
         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.39M
         x_cm = y_cm = 0;
1776
6.65M
         fold_i = fold_start; do {
1777
6.65M
           x_cm |= collapse_masks[fold_i*C+0];
1778
6.65M
           y_cm |= collapse_masks[fold_i*C+C-1];
1779
6.65M
         } while (++fold_i<fold_end);
1780
2.39M
      }
1781
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1782
         always) be non-zero. */
1783
1.12M
      else
1784
1.12M
         x_cm = y_cm = (1<<B)-1;
1785
1786
3.52M
      if (dual_stereo && i==intensity)
1787
10.0k
      {
1788
10.0k
         int j;
1789
1790
         /* Switch off dual stereo to do intensity. */
1791
10.0k
         dual_stereo = 0;
1792
10.0k
         if (resynth)
1793
219k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1794
213k
               norm[j] = HALF32(norm[j]+norm2[j]);
1795
10.0k
      }
1796
3.52M
      if (dual_stereo)
1797
108k
      {
1798
108k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1799
108k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1800
108k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1801
108k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1802
108k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1803
108k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1804
3.41M
      } else {
1805
3.41M
         if (Y!=NULL)
1806
1.05M
         {
1807
1.05M
            if (theta_rdo && i < intensity)
1808
85.2k
            {
1809
85.2k
               ec_ctx ec_save, ec_save2;
1810
85.2k
               struct band_ctx ctx_save, ctx_save2;
1811
85.2k
               opus_val32 dist0, dist1;
1812
85.2k
               unsigned cm, cm2;
1813
85.2k
               int nstart_bytes, nend_bytes, save_bytes;
1814
85.2k
               unsigned char *bytes_buf;
1815
85.2k
               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
85.2k
               opus_val16 w[2];
1823
85.2k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1824
               /* Make a copy. */
1825
85.2k
               cm = x_cm|y_cm;
1826
85.2k
               ec_save = *ec;
1827
#ifdef ENABLE_QEXT
1828
               ext_ec_save = *ext_ec;
1829
#endif
1830
85.2k
               ctx_save = ctx;
1831
85.2k
               OPUS_COPY(X_save, X, N);
1832
85.2k
               OPUS_COPY(Y_save, Y, N);
1833
               /* Encode and round down. */
1834
85.2k
               ctx.theta_round = -1;
1835
85.2k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1836
85.2k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1837
85.2k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1838
85.2k
               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
85.2k
               cm2 = x_cm;
1842
85.2k
               ec_save2 = *ec;
1843
#ifdef ENABLE_QEXT
1844
               ext_ec_save2 = *ext_ec;
1845
#endif
1846
85.2k
               ctx_save2 = ctx;
1847
85.2k
               OPUS_COPY(X_save2, X, N);
1848
85.2k
               OPUS_COPY(Y_save2, Y, N);
1849
85.2k
               if (!last)
1850
84.3k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1851
85.2k
               nstart_bytes = ec_save.offs;
1852
85.2k
               nend_bytes = ec_save.storage;
1853
85.2k
               bytes_buf = ec_save.buf+nstart_bytes;
1854
85.2k
               save_bytes = nend_bytes-nstart_bytes;
1855
85.2k
               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
85.2k
               *ec = ec_save;
1865
#ifdef ENABLE_QEXT
1866
               *ext_ec = ext_ec_save;
1867
#endif
1868
85.2k
               ctx = ctx_save;
1869
85.2k
               OPUS_COPY(X, X_save, N);
1870
85.2k
               OPUS_COPY(Y, Y_save, N);
1871
85.2k
#ifndef DISABLE_UPDATE_DRAFT
1872
85.2k
               if (i == start+1)
1873
7.11k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1874
85.2k
#endif
1875
               /* Encode and round up. */
1876
85.2k
               ctx.theta_round = 1;
1877
85.2k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1878
85.2k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1879
85.2k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1880
85.2k
               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
85.2k
               if (dist0 >= dist1) {
1882
55.9k
                  x_cm = cm2;
1883
55.9k
                  *ec = ec_save2;
1884
#ifdef ENABLE_QEXT
1885
                  *ext_ec = ext_ec_save2;
1886
#endif
1887
55.9k
                  ctx = ctx_save2;
1888
55.9k
                  OPUS_COPY(X, X_save2, N);
1889
55.9k
                  OPUS_COPY(Y, Y_save2, N);
1890
55.9k
                  if (!last)
1891
55.5k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1892
55.9k
                  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
55.9k
               }
1897
967k
            } else {
1898
967k
               ctx.theta_round = 0;
1899
967k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1900
967k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1901
967k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1902
967k
            }
1903
2.35M
         } else {
1904
2.35M
            x_cm = quant_band(&ctx, X, N, b, B,
1905
2.35M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1906
2.35M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1907
2.35M
         }
1908
3.41M
         y_cm = x_cm;
1909
3.41M
      }
1910
3.52M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1911
3.52M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1912
3.52M
      balance += pulses[i] + tell;
1913
1914
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1915
3.52M
      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
3.52M
      ctx.avoid_split_noise = 0;
1919
3.52M
   }
1920
230k
   *seed = ctx.seed;
1921
1922
230k
   RESTORE_STACK;
1923
230k
}
quant_all_bands
Line
Count
Source
1601
253k
{
1602
253k
   int i;
1603
253k
   opus_int32 remaining_bits;
1604
253k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1605
253k
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1606
253k
   VARDECL(celt_norm, _norm);
1607
253k
   VARDECL(celt_norm, _lowband_scratch);
1608
253k
   VARDECL(celt_norm, X_save);
1609
253k
   VARDECL(celt_norm, Y_save);
1610
253k
   VARDECL(celt_norm, X_save2);
1611
253k
   VARDECL(celt_norm, Y_save2);
1612
253k
   VARDECL(celt_norm, norm_save2);
1613
253k
   int resynth_alloc;
1614
253k
   celt_norm *lowband_scratch;
1615
253k
   int B;
1616
253k
   int M;
1617
253k
   int lowband_offset;
1618
253k
   int update_lowband = 1;
1619
253k
   int C = Y_ != NULL ? 2 : 1;
1620
253k
   int norm_offset;
1621
253k
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1622
#ifdef RESYNTH
1623
   int resynth = 1;
1624
#else
1625
253k
   int resynth = !encode || theta_rdo;
1626
253k
#endif
1627
253k
   struct band_ctx ctx;
1628
253k
#ifdef ENABLE_QEXT
1629
253k
   int ext_b;
1630
253k
   opus_int32 ext_balance=0;
1631
253k
   opus_int32 ext_tell=0;
1632
253k
#endif
1633
253k
   SAVE_STACK;
1634
1635
253k
   M = 1<<LM;
1636
253k
   B = shortBlocks ? M : 1;
1637
253k
   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
253k
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1641
253k
   norm = _norm;
1642
253k
   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
253k
   if (encode && resynth)
1648
23.8k
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1649
229k
   else
1650
229k
      resynth_alloc = ALLOC_NONE;
1651
253k
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1652
253k
   if (encode && resynth)
1653
23.8k
      lowband_scratch = _lowband_scratch;
1654
229k
   else
1655
229k
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1656
253k
   ALLOC(X_save, resynth_alloc, celt_norm);
1657
253k
   ALLOC(Y_save, resynth_alloc, celt_norm);
1658
253k
   ALLOC(X_save2, resynth_alloc, celt_norm);
1659
253k
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1660
253k
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1661
1662
253k
   lowband_offset = 0;
1663
253k
   ctx.bandE = bandE;
1664
253k
   ctx.ec = ec;
1665
253k
   ctx.encode = encode;
1666
253k
   ctx.intensity = intensity;
1667
253k
   ctx.m = m;
1668
253k
   ctx.seed = *seed;
1669
253k
   ctx.spread = spread;
1670
253k
   ctx.arch = arch;
1671
253k
   ctx.disable_inv = disable_inv;
1672
253k
   ctx.resynth = resynth;
1673
253k
   ctx.theta_round = 0;
1674
253k
#ifdef ENABLE_QEXT
1675
253k
   ctx.ext_ec = ext_ec;
1676
253k
   ctx.ext_total_bits = ext_total_bits;
1677
253k
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1678
253k
   if (ctx.extra_bands) theta_rdo = 0;
1679
253k
#endif
1680
   /* Avoid injecting noise in the first band on transients. */
1681
253k
   ctx.avoid_split_noise = B > 1;
1682
3.93M
   for (i=start;i<end;i++)
1683
3.67M
   {
1684
3.67M
      opus_int32 tell;
1685
3.67M
      int b;
1686
3.67M
      int N;
1687
3.67M
      opus_int32 curr_balance;
1688
3.67M
      int effective_lowband=-1;
1689
3.67M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1690
3.67M
      int tf_change=0;
1691
3.67M
      unsigned x_cm;
1692
3.67M
      unsigned y_cm;
1693
3.67M
      int last;
1694
1695
3.67M
      ctx.i = i;
1696
3.67M
      last = (i==end-1);
1697
1698
3.67M
      X = X_+M*eBands[i];
1699
3.67M
      if (Y_!=NULL)
1700
1.58M
         Y = Y_+M*eBands[i];
1701
2.09M
      else
1702
2.09M
         Y = NULL;
1703
3.67M
      N = M*eBands[i+1]-M*eBands[i];
1704
3.67M
      celt_assert(N > 0);
1705
3.67M
      tell = ec_tell_frac(ec);
1706
1707
      /* Compute how many bits we want to allocate to this band */
1708
3.67M
      if (i != start)
1709
3.42M
         balance -= tell;
1710
3.67M
      remaining_bits = total_bits-tell-1;
1711
3.67M
      ctx.remaining_bits = remaining_bits;
1712
3.67M
#ifdef ENABLE_QEXT
1713
3.67M
      if (i != start) {
1714
3.42M
         ext_balance += extra_pulses[i-1] + ext_tell;
1715
3.42M
      }
1716
3.67M
      ext_tell = ec_tell_frac(ext_ec);
1717
3.67M
      ctx.extra_bits = extra_pulses[i];
1718
3.67M
      if (i != start)
1719
3.42M
         ext_balance -= ext_tell;
1720
3.67M
      if (i <= codedBands-1)
1721
2.13M
      {
1722
2.13M
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1723
2.13M
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1724
2.13M
      } else {
1725
1.54M
         ext_b = 0;
1726
1.54M
      }
1727
3.67M
#endif
1728
3.67M
      if (i <= codedBands-1)
1729
2.13M
      {
1730
2.13M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1731
2.13M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1732
2.13M
      } else {
1733
1.54M
         b = 0;
1734
1.54M
      }
1735
1736
3.67M
#ifndef DISABLE_UPDATE_DRAFT
1737
3.67M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1738
915k
            lowband_offset = i;
1739
3.67M
      if (i == start+1)
1740
253k
         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
3.67M
      tf_change = tf_res[i];
1747
3.67M
      ctx.tf_change = tf_change;
1748
3.67M
      if (i>=m->effEBands)
1749
16.8k
      {
1750
16.8k
         X=norm;
1751
16.8k
         if (Y_!=NULL)
1752
13.5k
            Y = norm;
1753
16.8k
         lowband_scratch = NULL;
1754
16.8k
      }
1755
3.67M
      if (last && !theta_rdo)
1756
229k
         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
3.67M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1761
2.17M
      {
1762
2.17M
         int fold_start;
1763
2.17M
         int fold_end;
1764
2.17M
         int fold_i;
1765
         /* This ensures we never repeat spectral content within one band */
1766
2.17M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1767
2.17M
         fold_start = lowband_offset;
1768
2.59M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1769
2.17M
         fold_end = lowband_offset-1;
1770
2.17M
#ifndef DISABLE_UPDATE_DRAFT
1771
4.95M
         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.17M
         x_cm = y_cm = 0;
1776
5.38M
         fold_i = fold_start; do {
1777
5.38M
           x_cm |= collapse_masks[fold_i*C+0];
1778
5.38M
           y_cm |= collapse_masks[fold_i*C+C-1];
1779
5.38M
         } while (++fold_i<fold_end);
1780
2.17M
      }
1781
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1782
         always) be non-zero. */
1783
1.50M
      else
1784
1.50M
         x_cm = y_cm = (1<<B)-1;
1785
1786
3.67M
      if (dual_stereo && i==intensity)
1787
17.5k
      {
1788
17.5k
         int j;
1789
1790
         /* Switch off dual stereo to do intensity. */
1791
17.5k
         dual_stereo = 0;
1792
17.5k
         if (resynth)
1793
400k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1794
391k
               norm[j] = HALF32(norm[j]+norm2[j]);
1795
17.5k
      }
1796
3.67M
      if (dual_stereo)
1797
201k
      {
1798
201k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1799
201k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1800
201k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1801
201k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1802
201k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1803
201k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1804
3.47M
      } else {
1805
3.47M
         if (Y!=NULL)
1806
1.38M
         {
1807
1.38M
            if (theta_rdo && i < intensity)
1808
223k
            {
1809
223k
               ec_ctx ec_save, ec_save2;
1810
223k
               struct band_ctx ctx_save, ctx_save2;
1811
223k
               opus_val32 dist0, dist1;
1812
223k
               unsigned cm, cm2;
1813
223k
               int nstart_bytes, nend_bytes, save_bytes;
1814
223k
               unsigned char *bytes_buf;
1815
223k
               unsigned char bytes_save[1275];
1816
223k
#ifdef ENABLE_QEXT
1817
223k
               ec_ctx ext_ec_save, ext_ec_save2;
1818
223k
               unsigned char *ext_bytes_buf;
1819
223k
               int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes;
1820
223k
               unsigned char ext_bytes_save[QEXT_PACKET_SIZE_CAP];
1821
223k
#endif
1822
223k
               opus_val16 w[2];
1823
223k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1824
               /* Make a copy. */
1825
223k
               cm = x_cm|y_cm;
1826
223k
               ec_save = *ec;
1827
223k
#ifdef ENABLE_QEXT
1828
223k
               ext_ec_save = *ext_ec;
1829
223k
#endif
1830
223k
               ctx_save = ctx;
1831
223k
               OPUS_COPY(X_save, X, N);
1832
223k
               OPUS_COPY(Y_save, Y, N);
1833
               /* Encode and round down. */
1834
223k
               ctx.theta_round = -1;
1835
223k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1836
223k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1837
223k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1838
223k
               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
223k
               cm2 = x_cm;
1842
223k
               ec_save2 = *ec;
1843
223k
#ifdef ENABLE_QEXT
1844
223k
               ext_ec_save2 = *ext_ec;
1845
223k
#endif
1846
223k
               ctx_save2 = ctx;
1847
223k
               OPUS_COPY(X_save2, X, N);
1848
223k
               OPUS_COPY(Y_save2, Y, N);
1849
223k
               if (!last)
1850
220k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1851
223k
               nstart_bytes = ec_save.offs;
1852
223k
               nend_bytes = ec_save.storage;
1853
223k
               bytes_buf = ec_save.buf+nstart_bytes;
1854
223k
               save_bytes = nend_bytes-nstart_bytes;
1855
223k
               OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1856
223k
#ifdef ENABLE_QEXT
1857
223k
               ext_nstart_bytes = ext_ec_save.offs;
1858
223k
               ext_nend_bytes = ext_ec_save.storage;
1859
223k
               ext_bytes_buf = ext_ec_save.buf!=NULL ? ext_ec_save.buf+ext_nstart_bytes : NULL;
1860
223k
               ext_save_bytes = ext_nend_bytes-ext_nstart_bytes;
1861
223k
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1862
223k
#endif
1863
               /* Restore */
1864
223k
               *ec = ec_save;
1865
223k
#ifdef ENABLE_QEXT
1866
223k
               *ext_ec = ext_ec_save;
1867
223k
#endif
1868
223k
               ctx = ctx_save;
1869
223k
               OPUS_COPY(X, X_save, N);
1870
223k
               OPUS_COPY(Y, Y_save, N);
1871
223k
#ifndef DISABLE_UPDATE_DRAFT
1872
223k
               if (i == start+1)
1873
18.5k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1874
223k
#endif
1875
               /* Encode and round up. */
1876
223k
               ctx.theta_round = 1;
1877
223k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1878
223k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1879
223k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1880
223k
               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
223k
               if (dist0 >= dist1) {
1882
153k
                  x_cm = cm2;
1883
153k
                  *ec = ec_save2;
1884
153k
#ifdef ENABLE_QEXT
1885
153k
                  *ext_ec = ext_ec_save2;
1886
153k
#endif
1887
153k
                  ctx = ctx_save2;
1888
153k
                  OPUS_COPY(X, X_save2, N);
1889
153k
                  OPUS_COPY(Y, Y_save2, N);
1890
153k
                  if (!last)
1891
151k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1892
153k
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1893
153k
#ifdef ENABLE_QEXT
1894
153k
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1895
153k
#endif
1896
153k
               }
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
2.09M
         } else {
1904
2.09M
            x_cm = quant_band(&ctx, X, N, b, B,
1905
2.09M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1906
2.09M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1907
2.09M
         }
1908
3.47M
         y_cm = x_cm;
1909
3.47M
      }
1910
3.67M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1911
3.67M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1912
3.67M
      balance += pulses[i] + tell;
1913
1914
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1915
3.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
3.67M
      ctx.avoid_split_noise = 0;
1919
3.67M
   }
1920
253k
   *seed = ctx.seed;
1921
1922
253k
   RESTORE_STACK;
1923
253k
}
quant_all_bands
Line
Count
Source
1601
253k
{
1602
253k
   int i;
1603
253k
   opus_int32 remaining_bits;
1604
253k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1605
253k
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1606
253k
   VARDECL(celt_norm, _norm);
1607
253k
   VARDECL(celt_norm, _lowband_scratch);
1608
253k
   VARDECL(celt_norm, X_save);
1609
253k
   VARDECL(celt_norm, Y_save);
1610
253k
   VARDECL(celt_norm, X_save2);
1611
253k
   VARDECL(celt_norm, Y_save2);
1612
253k
   VARDECL(celt_norm, norm_save2);
1613
253k
   int resynth_alloc;
1614
253k
   celt_norm *lowband_scratch;
1615
253k
   int B;
1616
253k
   int M;
1617
253k
   int lowband_offset;
1618
253k
   int update_lowband = 1;
1619
253k
   int C = Y_ != NULL ? 2 : 1;
1620
253k
   int norm_offset;
1621
253k
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1622
#ifdef RESYNTH
1623
   int resynth = 1;
1624
#else
1625
253k
   int resynth = !encode || theta_rdo;
1626
253k
#endif
1627
253k
   struct band_ctx ctx;
1628
253k
#ifdef ENABLE_QEXT
1629
253k
   int ext_b;
1630
253k
   opus_int32 ext_balance=0;
1631
253k
   opus_int32 ext_tell=0;
1632
253k
#endif
1633
253k
   SAVE_STACK;
1634
1635
253k
   M = 1<<LM;
1636
253k
   B = shortBlocks ? M : 1;
1637
253k
   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
253k
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1641
253k
   norm = _norm;
1642
253k
   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
253k
   if (encode && resynth)
1648
23.8k
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1649
229k
   else
1650
229k
      resynth_alloc = ALLOC_NONE;
1651
253k
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1652
253k
   if (encode && resynth)
1653
23.8k
      lowband_scratch = _lowband_scratch;
1654
229k
   else
1655
229k
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1656
253k
   ALLOC(X_save, resynth_alloc, celt_norm);
1657
253k
   ALLOC(Y_save, resynth_alloc, celt_norm);
1658
253k
   ALLOC(X_save2, resynth_alloc, celt_norm);
1659
253k
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1660
253k
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1661
1662
253k
   lowband_offset = 0;
1663
253k
   ctx.bandE = bandE;
1664
253k
   ctx.ec = ec;
1665
253k
   ctx.encode = encode;
1666
253k
   ctx.intensity = intensity;
1667
253k
   ctx.m = m;
1668
253k
   ctx.seed = *seed;
1669
253k
   ctx.spread = spread;
1670
253k
   ctx.arch = arch;
1671
253k
   ctx.disable_inv = disable_inv;
1672
253k
   ctx.resynth = resynth;
1673
253k
   ctx.theta_round = 0;
1674
253k
#ifdef ENABLE_QEXT
1675
253k
   ctx.ext_ec = ext_ec;
1676
253k
   ctx.ext_total_bits = ext_total_bits;
1677
253k
   ctx.extra_bands = end == NB_QEXT_BANDS || end == 2;
1678
253k
   if (ctx.extra_bands) theta_rdo = 0;
1679
253k
#endif
1680
   /* Avoid injecting noise in the first band on transients. */
1681
253k
   ctx.avoid_split_noise = B > 1;
1682
3.93M
   for (i=start;i<end;i++)
1683
3.67M
   {
1684
3.67M
      opus_int32 tell;
1685
3.67M
      int b;
1686
3.67M
      int N;
1687
3.67M
      opus_int32 curr_balance;
1688
3.67M
      int effective_lowband=-1;
1689
3.67M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1690
3.67M
      int tf_change=0;
1691
3.67M
      unsigned x_cm;
1692
3.67M
      unsigned y_cm;
1693
3.67M
      int last;
1694
1695
3.67M
      ctx.i = i;
1696
3.67M
      last = (i==end-1);
1697
1698
3.67M
      X = X_+M*eBands[i];
1699
3.67M
      if (Y_!=NULL)
1700
1.58M
         Y = Y_+M*eBands[i];
1701
2.09M
      else
1702
2.09M
         Y = NULL;
1703
3.67M
      N = M*eBands[i+1]-M*eBands[i];
1704
3.67M
      celt_assert(N > 0);
1705
3.67M
      tell = ec_tell_frac(ec);
1706
1707
      /* Compute how many bits we want to allocate to this band */
1708
3.67M
      if (i != start)
1709
3.42M
         balance -= tell;
1710
3.67M
      remaining_bits = total_bits-tell-1;
1711
3.67M
      ctx.remaining_bits = remaining_bits;
1712
3.67M
#ifdef ENABLE_QEXT
1713
3.67M
      if (i != start) {
1714
3.42M
         ext_balance += extra_pulses[i-1] + ext_tell;
1715
3.42M
      }
1716
3.67M
      ext_tell = ec_tell_frac(ext_ec);
1717
3.67M
      ctx.extra_bits = extra_pulses[i];
1718
3.67M
      if (i != start)
1719
3.42M
         ext_balance -= ext_tell;
1720
3.67M
      if (i <= codedBands-1)
1721
2.13M
      {
1722
2.13M
         opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i));
1723
2.13M
         ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance)));
1724
2.13M
      } else {
1725
1.54M
         ext_b = 0;
1726
1.54M
      }
1727
3.67M
#endif
1728
3.67M
      if (i <= codedBands-1)
1729
2.13M
      {
1730
2.13M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1731
2.13M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1732
2.13M
      } else {
1733
1.54M
         b = 0;
1734
1.54M
      }
1735
1736
3.67M
#ifndef DISABLE_UPDATE_DRAFT
1737
3.67M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1738
915k
            lowband_offset = i;
1739
3.67M
      if (i == start+1)
1740
253k
         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
3.67M
      tf_change = tf_res[i];
1747
3.67M
      ctx.tf_change = tf_change;
1748
3.67M
      if (i>=m->effEBands)
1749
16.8k
      {
1750
16.8k
         X=norm;
1751
16.8k
         if (Y_!=NULL)
1752
13.5k
            Y = norm;
1753
16.8k
         lowband_scratch = NULL;
1754
16.8k
      }
1755
3.67M
      if (last && !theta_rdo)
1756
229k
         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
3.67M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1761
2.17M
      {
1762
2.17M
         int fold_start;
1763
2.17M
         int fold_end;
1764
2.17M
         int fold_i;
1765
         /* This ensures we never repeat spectral content within one band */
1766
2.17M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1767
2.17M
         fold_start = lowband_offset;
1768
2.59M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1769
2.17M
         fold_end = lowband_offset-1;
1770
2.17M
#ifndef DISABLE_UPDATE_DRAFT
1771
4.95M
         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.17M
         x_cm = y_cm = 0;
1776
5.38M
         fold_i = fold_start; do {
1777
5.38M
           x_cm |= collapse_masks[fold_i*C+0];
1778
5.38M
           y_cm |= collapse_masks[fold_i*C+C-1];
1779
5.38M
         } while (++fold_i<fold_end);
1780
2.17M
      }
1781
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1782
         always) be non-zero. */
1783
1.50M
      else
1784
1.50M
         x_cm = y_cm = (1<<B)-1;
1785
1786
3.67M
      if (dual_stereo && i==intensity)
1787
17.5k
      {
1788
17.5k
         int j;
1789
1790
         /* Switch off dual stereo to do intensity. */
1791
17.5k
         dual_stereo = 0;
1792
17.5k
         if (resynth)
1793
400k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1794
391k
               norm[j] = HALF32(norm[j]+norm2[j]);
1795
17.5k
      }
1796
3.67M
      if (dual_stereo)
1797
201k
      {
1798
201k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1799
201k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1800
201k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1801
201k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1802
201k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1803
201k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1804
3.47M
      } else {
1805
3.47M
         if (Y!=NULL)
1806
1.38M
         {
1807
1.38M
            if (theta_rdo && i < intensity)
1808
223k
            {
1809
223k
               ec_ctx ec_save, ec_save2;
1810
223k
               struct band_ctx ctx_save, ctx_save2;
1811
223k
               opus_val32 dist0, dist1;
1812
223k
               unsigned cm, cm2;
1813
223k
               int nstart_bytes, nend_bytes, save_bytes;
1814
223k
               unsigned char *bytes_buf;
1815
223k
               unsigned char bytes_save[1275];
1816
223k
#ifdef ENABLE_QEXT
1817
223k
               ec_ctx ext_ec_save, ext_ec_save2;
1818
223k
               unsigned char *ext_bytes_buf;
1819
223k
               int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes;
1820
223k
               unsigned char ext_bytes_save[QEXT_PACKET_SIZE_CAP];
1821
223k
#endif
1822
223k
               opus_val16 w[2];
1823
223k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1824
               /* Make a copy. */
1825
223k
               cm = x_cm|y_cm;
1826
223k
               ec_save = *ec;
1827
223k
#ifdef ENABLE_QEXT
1828
223k
               ext_ec_save = *ext_ec;
1829
223k
#endif
1830
223k
               ctx_save = ctx;
1831
223k
               OPUS_COPY(X_save, X, N);
1832
223k
               OPUS_COPY(Y_save, Y, N);
1833
               /* Encode and round down. */
1834
223k
               ctx.theta_round = -1;
1835
223k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1836
223k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1837
223k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1838
223k
               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
223k
               cm2 = x_cm;
1842
223k
               ec_save2 = *ec;
1843
223k
#ifdef ENABLE_QEXT
1844
223k
               ext_ec_save2 = *ext_ec;
1845
223k
#endif
1846
223k
               ctx_save2 = ctx;
1847
223k
               OPUS_COPY(X_save2, X, N);
1848
223k
               OPUS_COPY(Y_save2, Y, N);
1849
223k
               if (!last)
1850
220k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1851
223k
               nstart_bytes = ec_save.offs;
1852
223k
               nend_bytes = ec_save.storage;
1853
223k
               bytes_buf = ec_save.buf+nstart_bytes;
1854
223k
               save_bytes = nend_bytes-nstart_bytes;
1855
223k
               OPUS_COPY(bytes_save, bytes_buf, save_bytes);
1856
223k
#ifdef ENABLE_QEXT
1857
223k
               ext_nstart_bytes = ext_ec_save.offs;
1858
223k
               ext_nend_bytes = ext_ec_save.storage;
1859
223k
               ext_bytes_buf = ext_ec_save.buf!=NULL ? ext_ec_save.buf+ext_nstart_bytes : NULL;
1860
223k
               ext_save_bytes = ext_nend_bytes-ext_nstart_bytes;
1861
223k
               if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes);
1862
223k
#endif
1863
               /* Restore */
1864
223k
               *ec = ec_save;
1865
223k
#ifdef ENABLE_QEXT
1866
223k
               *ext_ec = ext_ec_save;
1867
223k
#endif
1868
223k
               ctx = ctx_save;
1869
223k
               OPUS_COPY(X, X_save, N);
1870
223k
               OPUS_COPY(Y, Y_save, N);
1871
223k
#ifndef DISABLE_UPDATE_DRAFT
1872
223k
               if (i == start+1)
1873
18.5k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1874
223k
#endif
1875
               /* Encode and round up. */
1876
223k
               ctx.theta_round = 1;
1877
223k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1878
223k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1879
223k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1880
223k
               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
223k
               if (dist0 >= dist1) {
1882
153k
                  x_cm = cm2;
1883
153k
                  *ec = ec_save2;
1884
153k
#ifdef ENABLE_QEXT
1885
153k
                  *ext_ec = ext_ec_save2;
1886
153k
#endif
1887
153k
                  ctx = ctx_save2;
1888
153k
                  OPUS_COPY(X, X_save2, N);
1889
153k
                  OPUS_COPY(Y, Y_save2, N);
1890
153k
                  if (!last)
1891
151k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1892
153k
                  OPUS_COPY(bytes_buf, bytes_save, save_bytes);
1893
153k
#ifdef ENABLE_QEXT
1894
153k
                  if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes);
1895
153k
#endif
1896
153k
               }
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
2.09M
         } else {
1904
2.09M
            x_cm = quant_band(&ctx, X, N, b, B,
1905
2.09M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1906
2.09M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1907
2.09M
         }
1908
3.47M
         y_cm = x_cm;
1909
3.47M
      }
1910
3.67M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1911
3.67M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1912
3.67M
      balance += pulses[i] + tell;
1913
1914
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1915
3.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
3.67M
      ctx.avoid_split_noise = 0;
1919
3.67M
   }
1920
253k
   *seed = ctx.seed;
1921
1922
253k
   RESTORE_STACK;
1923
253k
}
quant_all_bands
Line
Count
Source
1601
230k
{
1602
230k
   int i;
1603
230k
   opus_int32 remaining_bits;
1604
230k
   const opus_int16 * OPUS_RESTRICT eBands = m->eBands;
1605
230k
   celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2;
1606
230k
   VARDECL(celt_norm, _norm);
1607
230k
   VARDECL(celt_norm, _lowband_scratch);
1608
230k
   VARDECL(celt_norm, X_save);
1609
230k
   VARDECL(celt_norm, Y_save);
1610
230k
   VARDECL(celt_norm, X_save2);
1611
230k
   VARDECL(celt_norm, Y_save2);
1612
230k
   VARDECL(celt_norm, norm_save2);
1613
230k
   int resynth_alloc;
1614
230k
   celt_norm *lowband_scratch;
1615
230k
   int B;
1616
230k
   int M;
1617
230k
   int lowband_offset;
1618
230k
   int update_lowband = 1;
1619
230k
   int C = Y_ != NULL ? 2 : 1;
1620
230k
   int norm_offset;
1621
230k
   int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8;
1622
#ifdef RESYNTH
1623
   int resynth = 1;
1624
#else
1625
230k
   int resynth = !encode || theta_rdo;
1626
230k
#endif
1627
230k
   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
230k
   SAVE_STACK;
1634
1635
230k
   M = 1<<LM;
1636
230k
   B = shortBlocks ? M : 1;
1637
230k
   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
230k
   ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm);
1641
230k
   norm = _norm;
1642
230k
   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
230k
   if (encode && resynth)
1648
10.1k
      resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]);
1649
220k
   else
1650
220k
      resynth_alloc = ALLOC_NONE;
1651
230k
   ALLOC(_lowband_scratch, resynth_alloc, celt_norm);
1652
230k
   if (encode && resynth)
1653
10.1k
      lowband_scratch = _lowband_scratch;
1654
220k
   else
1655
220k
      lowband_scratch = X_+M*eBands[m->effEBands-1];
1656
230k
   ALLOC(X_save, resynth_alloc, celt_norm);
1657
230k
   ALLOC(Y_save, resynth_alloc, celt_norm);
1658
230k
   ALLOC(X_save2, resynth_alloc, celt_norm);
1659
230k
   ALLOC(Y_save2, resynth_alloc, celt_norm);
1660
230k
   ALLOC(norm_save2, resynth_alloc, celt_norm);
1661
1662
230k
   lowband_offset = 0;
1663
230k
   ctx.bandE = bandE;
1664
230k
   ctx.ec = ec;
1665
230k
   ctx.encode = encode;
1666
230k
   ctx.intensity = intensity;
1667
230k
   ctx.m = m;
1668
230k
   ctx.seed = *seed;
1669
230k
   ctx.spread = spread;
1670
230k
   ctx.arch = arch;
1671
230k
   ctx.disable_inv = disable_inv;
1672
230k
   ctx.resynth = resynth;
1673
230k
   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
230k
   ctx.avoid_split_noise = B > 1;
1682
3.75M
   for (i=start;i<end;i++)
1683
3.52M
   {
1684
3.52M
      opus_int32 tell;
1685
3.52M
      int b;
1686
3.52M
      int N;
1687
3.52M
      opus_int32 curr_balance;
1688
3.52M
      int effective_lowband=-1;
1689
3.52M
      celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y;
1690
3.52M
      int tf_change=0;
1691
3.52M
      unsigned x_cm;
1692
3.52M
      unsigned y_cm;
1693
3.52M
      int last;
1694
1695
3.52M
      ctx.i = i;
1696
3.52M
      last = (i==end-1);
1697
1698
3.52M
      X = X_+M*eBands[i];
1699
3.52M
      if (Y_!=NULL)
1700
1.16M
         Y = Y_+M*eBands[i];
1701
2.35M
      else
1702
2.35M
         Y = NULL;
1703
3.52M
      N = M*eBands[i+1]-M*eBands[i];
1704
3.52M
      celt_assert(N > 0);
1705
3.52M
      tell = ec_tell_frac(ec);
1706
1707
      /* Compute how many bits we want to allocate to this band */
1708
3.52M
      if (i != start)
1709
3.29M
         balance -= tell;
1710
3.52M
      remaining_bits = total_bits-tell-1;
1711
3.52M
      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
3.52M
      if (i <= codedBands-1)
1729
1.33M
      {
1730
1.33M
         curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i));
1731
1.33M
         b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance)));
1732
2.18M
      } else {
1733
2.18M
         b = 0;
1734
2.18M
      }
1735
1736
3.52M
#ifndef DISABLE_UPDATE_DRAFT
1737
3.52M
      if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0))
1738
582k
            lowband_offset = i;
1739
3.52M
      if (i == start+1)
1740
230k
         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
3.52M
      tf_change = tf_res[i];
1747
3.52M
      ctx.tf_change = tf_change;
1748
3.52M
      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
3.52M
      if (last && !theta_rdo)
1756
220k
         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
3.52M
      if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0))
1761
2.39M
      {
1762
2.39M
         int fold_start;
1763
2.39M
         int fold_end;
1764
2.39M
         int fold_i;
1765
         /* This ensures we never repeat spectral content within one band */
1766
2.39M
         effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N);
1767
2.39M
         fold_start = lowband_offset;
1768
2.65M
         while(M*eBands[--fold_start] > effective_lowband+norm_offset);
1769
2.39M
         fold_end = lowband_offset-1;
1770
2.39M
#ifndef DISABLE_UPDATE_DRAFT
1771
6.39M
         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.39M
         x_cm = y_cm = 0;
1776
6.65M
         fold_i = fold_start; do {
1777
6.65M
           x_cm |= collapse_masks[fold_i*C+0];
1778
6.65M
           y_cm |= collapse_masks[fold_i*C+C-1];
1779
6.65M
         } while (++fold_i<fold_end);
1780
2.39M
      }
1781
      /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost
1782
         always) be non-zero. */
1783
1.12M
      else
1784
1.12M
         x_cm = y_cm = (1<<B)-1;
1785
1786
3.52M
      if (dual_stereo && i==intensity)
1787
10.0k
      {
1788
10.0k
         int j;
1789
1790
         /* Switch off dual stereo to do intensity. */
1791
10.0k
         dual_stereo = 0;
1792
10.0k
         if (resynth)
1793
219k
            for (j=0;j<M*eBands[i]-norm_offset;j++)
1794
213k
               norm[j] = HALF32(norm[j]+norm2[j]);
1795
10.0k
      }
1796
3.52M
      if (dual_stereo)
1797
108k
      {
1798
108k
         x_cm = quant_band(&ctx, X, N, b/2, B,
1799
108k
               effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1800
108k
               last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2));
1801
108k
         y_cm = quant_band(&ctx, Y, N, b/2, B,
1802
108k
               effective_lowband != -1 ? norm2+effective_lowband : NULL, LM,
1803
108k
               last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2));
1804
3.41M
      } else {
1805
3.41M
         if (Y!=NULL)
1806
1.05M
         {
1807
1.05M
            if (theta_rdo && i < intensity)
1808
85.2k
            {
1809
85.2k
               ec_ctx ec_save, ec_save2;
1810
85.2k
               struct band_ctx ctx_save, ctx_save2;
1811
85.2k
               opus_val32 dist0, dist1;
1812
85.2k
               unsigned cm, cm2;
1813
85.2k
               int nstart_bytes, nend_bytes, save_bytes;
1814
85.2k
               unsigned char *bytes_buf;
1815
85.2k
               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
85.2k
               opus_val16 w[2];
1823
85.2k
               compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w);
1824
               /* Make a copy. */
1825
85.2k
               cm = x_cm|y_cm;
1826
85.2k
               ec_save = *ec;
1827
#ifdef ENABLE_QEXT
1828
               ext_ec_save = *ext_ec;
1829
#endif
1830
85.2k
               ctx_save = ctx;
1831
85.2k
               OPUS_COPY(X_save, X, N);
1832
85.2k
               OPUS_COPY(Y_save, Y, N);
1833
               /* Encode and round down. */
1834
85.2k
               ctx.theta_round = -1;
1835
85.2k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1836
85.2k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1837
85.2k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1838
85.2k
               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
85.2k
               cm2 = x_cm;
1842
85.2k
               ec_save2 = *ec;
1843
#ifdef ENABLE_QEXT
1844
               ext_ec_save2 = *ext_ec;
1845
#endif
1846
85.2k
               ctx_save2 = ctx;
1847
85.2k
               OPUS_COPY(X_save2, X, N);
1848
85.2k
               OPUS_COPY(Y_save2, Y, N);
1849
85.2k
               if (!last)
1850
84.3k
                  OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N);
1851
85.2k
               nstart_bytes = ec_save.offs;
1852
85.2k
               nend_bytes = ec_save.storage;
1853
85.2k
               bytes_buf = ec_save.buf+nstart_bytes;
1854
85.2k
               save_bytes = nend_bytes-nstart_bytes;
1855
85.2k
               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
85.2k
               *ec = ec_save;
1865
#ifdef ENABLE_QEXT
1866
               *ext_ec = ext_ec_save;
1867
#endif
1868
85.2k
               ctx = ctx_save;
1869
85.2k
               OPUS_COPY(X, X_save, N);
1870
85.2k
               OPUS_COPY(Y, Y_save, N);
1871
85.2k
#ifndef DISABLE_UPDATE_DRAFT
1872
85.2k
               if (i == start+1)
1873
7.11k
                  special_hybrid_folding(m, norm, norm2, start, M, dual_stereo);
1874
85.2k
#endif
1875
               /* Encode and round up. */
1876
85.2k
               ctx.theta_round = 1;
1877
85.2k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1878
85.2k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1879
85.2k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1880
85.2k
               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
85.2k
               if (dist0 >= dist1) {
1882
55.9k
                  x_cm = cm2;
1883
55.9k
                  *ec = ec_save2;
1884
#ifdef ENABLE_QEXT
1885
                  *ext_ec = ext_ec_save2;
1886
#endif
1887
55.9k
                  ctx = ctx_save2;
1888
55.9k
                  OPUS_COPY(X, X_save2, N);
1889
55.9k
                  OPUS_COPY(Y, Y_save2, N);
1890
55.9k
                  if (!last)
1891
55.5k
                     OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N);
1892
55.9k
                  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
55.9k
               }
1897
967k
            } else {
1898
967k
               ctx.theta_round = 0;
1899
967k
               x_cm = quant_band_stereo(&ctx, X, Y, N, b, B,
1900
967k
                     effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1901
967k
                     last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap));
1902
967k
            }
1903
2.35M
         } else {
1904
2.35M
            x_cm = quant_band(&ctx, X, N, b, B,
1905
2.35M
                  effective_lowband != -1 ? norm+effective_lowband : NULL, LM,
1906
2.35M
                  last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b));
1907
2.35M
         }
1908
3.41M
         y_cm = x_cm;
1909
3.41M
      }
1910
3.52M
      collapse_masks[i*C+0] = (unsigned char)x_cm;
1911
3.52M
      collapse_masks[i*C+C-1] = (unsigned char)y_cm;
1912
3.52M
      balance += pulses[i] + tell;
1913
1914
      /* Update the folding position only as long as we have 1 bit/sample depth. */
1915
3.52M
      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
3.52M
      ctx.avoid_split_noise = 0;
1919
3.52M
   }
1920
230k
   *seed = ctx.seed;
1921
1922
230k
   RESTORE_STACK;
1923
230k
}
1924