Coverage Report

Created: 2026-04-12 08:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/celt/rate.c
Line
Count
Source
1
/* Copyright (c) 2007-2008 CSIRO
2
   Copyright (c) 2007-2009 Xiph.Org Foundation
3
   Written by Jean-Marc Valin */
4
/*
5
   Redistribution and use in source and binary forms, with or without
6
   modification, are permitted provided that the following conditions
7
   are met:
8
9
   - Redistributions of source code must retain the above copyright
10
   notice, this list of conditions and the following disclaimer.
11
12
   - Redistributions in binary form must reproduce the above copyright
13
   notice, this list of conditions and the following disclaimer in the
14
   documentation and/or other materials provided with the distribution.
15
16
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20
   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
*/
28
29
#ifdef HAVE_CONFIG_H
30
#include "config.h"
31
#endif
32
33
#include <math.h>
34
#include "modes.h"
35
#include "cwrs.h"
36
#include "arch.h"
37
#include "os_support.h"
38
39
#include "entcode.h"
40
#include "rate.h"
41
#include "quant_bands.h"
42
43
static const unsigned char LOG2_FRAC_TABLE[24]={
44
   0,
45
   8,13,
46
  16,19,21,23,
47
  24,26,27,28,29,30,31,32,
48
  32,33,34,34,35,36,36,37,37
49
};
50
51
#if defined(CUSTOM_MODES)
52
53
/*Determines if V(N,K) fits in a 32-bit unsigned integer.
54
  N and K are themselves limited to 15 bits.*/
55
static int fits_in32(int _n, int _k)
56
{
57
   static const opus_int16 maxN[15] = {
58
      32767, 32767, 32767, 1476, 283, 109,  60,  40,
59
       29,  24,  20,  18,  16,  14,  13};
60
   static const opus_int16 maxK[15] = {
61
      32767, 32767, 32767, 32767, 1172, 238,  95,  53,
62
       36,  27,  22,  18,  16,  15,  13};
63
   if (_n>=14)
64
   {
65
      if (_k>=14)
66
         return 0;
67
      else
68
         return _n <= maxN[_k];
69
   } else {
70
      return _k <= maxK[_n];
71
   }
72
}
73
74
void compute_pulse_cache(CELTMode *m, int LM)
75
{
76
   int C;
77
   int i;
78
   int j;
79
   int curr=0;
80
   int nbEntries=0;
81
   int entryN[100], entryK[100], entryI[100];
82
   const opus_int16 *eBands = m->eBands;
83
   PulseCache *cache = &m->cache;
84
   opus_int16 *cindex;
85
   unsigned char *bits;
86
   unsigned char *cap;
87
88
   cindex = (opus_int16 *)opus_alloc(sizeof(cache->index[0])*m->nbEBands*(LM+2));
89
   cache->index = cindex;
90
91
   /* Scan for all unique band sizes */
92
   for (i=0;i<=LM+1;i++)
93
   {
94
      for (j=0;j<m->nbEBands;j++)
95
      {
96
         int k;
97
         int N = (eBands[j+1]-eBands[j])<<i>>1;
98
         cindex[i*m->nbEBands+j] = -1;
99
         /* Find other bands that have the same size */
100
         for (k=0;k<=i;k++)
101
         {
102
            int n;
103
            for (n=0;n<m->nbEBands && (k!=i || n<j);n++)
104
            {
105
               if (N == (eBands[n+1]-eBands[n])<<k>>1)
106
               {
107
                  cindex[i*m->nbEBands+j] = cindex[k*m->nbEBands+n];
108
                  break;
109
               }
110
            }
111
         }
112
         if (cache->index[i*m->nbEBands+j] == -1 && N!=0)
113
         {
114
            int K;
115
            entryN[nbEntries] = N;
116
            K = 0;
117
            while (fits_in32(N,get_pulses(K+1)) && K<MAX_PSEUDO)
118
               K++;
119
            entryK[nbEntries] = K;
120
            cindex[i*m->nbEBands+j] = curr;
121
            entryI[nbEntries] = curr;
122
123
            curr += K+1;
124
            nbEntries++;
125
         }
126
      }
127
   }
128
   bits = (unsigned char *)opus_alloc(sizeof(unsigned char)*curr);
129
   cache->bits = bits;
130
   cache->size = curr;
131
   /* Compute the cache for all unique sizes */
132
   for (i=0;i<nbEntries;i++)
133
   {
134
      unsigned char *ptr = bits+entryI[i];
135
      opus_int16 tmp[CELT_MAX_PULSES+1];
136
      get_required_bits(tmp, entryN[i], get_pulses(entryK[i]), BITRES);
137
      for (j=1;j<=entryK[i];j++)
138
         ptr[j] = tmp[get_pulses(j)]-1;
139
      ptr[0] = entryK[i];
140
   }
141
142
   /* Compute the maximum rate for each band at which we'll reliably use as
143
       many bits as we ask for. */
144
   cache->caps = cap = (unsigned char *)opus_alloc(sizeof(cache->caps[0])*(LM+1)*2*m->nbEBands);
145
   for (i=0;i<=LM;i++)
146
   {
147
      for (C=1;C<=2;C++)
148
      {
149
         for (j=0;j<m->nbEBands;j++)
150
         {
151
            int N0;
152
            int max_bits;
153
            N0 = m->eBands[j+1]-m->eBands[j];
154
            /* N=1 bands only have a sign bit and fine bits. */
155
            if (N0<<i == 1)
156
               max_bits = C*(1+MAX_FINE_BITS)<<BITRES;
157
            else
158
            {
159
               const unsigned char *pcache;
160
               opus_int32           num;
161
               opus_int32           den;
162
               int                  LM0;
163
               int                  N;
164
               int                  offset;
165
               int                  ndof;
166
               int                  qb;
167
               int                  k;
168
               LM0 = 0;
169
               /* Even-sized bands bigger than N=2 can be split one more time.
170
                  As of commit 44203907 all bands >1 are even, including custom modes.*/
171
               if (N0 > 2)
172
               {
173
                  N0>>=1;
174
                  LM0--;
175
               }
176
               /* N0=1 bands can't be split down to N<2. */
177
               else if (N0 <= 1)
178
               {
179
                  LM0=IMIN(i,1);
180
                  N0<<=LM0;
181
               }
182
               /* Compute the cost for the lowest-level PVQ of a fully split
183
                   band. */
184
               pcache = bits + cindex[(LM0+1)*m->nbEBands+j];
185
               max_bits = pcache[pcache[0]]+1;
186
               /* Add in the cost of coding regular splits. */
187
               N = N0;
188
               for(k=0;k<i-LM0;k++){
189
                  max_bits <<= 1;
190
                  /* Offset the number of qtheta bits by log2(N)/2
191
                      + QTHETA_OFFSET compared to their "fair share" of
192
                      total/N */
193
                  offset = ((m->logN[j]+(opus_int32)((opus_uint32)(LM0+k)<<BITRES))>>1)-QTHETA_OFFSET;
194
                  /* The number of qtheta bits we'll allocate if the remainder
195
                      is to be max_bits.
196
                     The average measured cost for theta is 0.89701 times qb,
197
                      approximated here as 459/512. */
198
                  num=459*(opus_int32)((2*N-1)*offset+max_bits);
199
                  den=((opus_int32)(2*N-1)<<9)-459;
200
                  qb = IMIN((num+(den>>1))/den, 57);
201
                  celt_assert(qb >= 0);
202
                  max_bits += qb;
203
                  N <<= 1;
204
               }
205
               /* Add in the cost of a stereo split, if necessary. */
206
               if (C==2)
207
               {
208
                  max_bits <<= 1;
209
                  offset = ((m->logN[j]+(i<<BITRES))>>1)-(N==2?QTHETA_OFFSET_TWOPHASE:QTHETA_OFFSET);
210
                  ndof = 2*N-1-(N==2);
211
                  /* The average measured cost for theta with the step PDF is
212
                      0.95164 times qb, approximated here as 487/512. */
213
                  num = (N==2?512:487)*(opus_int32)(max_bits+ndof*offset);
214
                  den = ((opus_int32)ndof<<9)-(N==2?512:487);
215
                  qb = IMIN((num+(den>>1))/den, (N==2?64:61));
216
                  celt_assert(qb >= 0);
217
                  max_bits += qb;
218
               }
219
               /* Add the fine bits we'll use. */
220
               /* Compensate for the extra DoF in stereo */
221
               ndof = C*N + ((C==2 && N>2) ? 1 : 0);
222
               /* Offset the number of fine bits by log2(N)/2 + FINE_OFFSET
223
                   compared to their "fair share" of total/N */
224
               offset = ((m->logN[j] + (i<<BITRES))>>1)-FINE_OFFSET;
225
               /* N=2 is the only point that doesn't match the curve */
226
               if (N==2)
227
                  offset += 1<<BITRES>>2;
228
               /* The number of fine bits we'll allocate if the remainder is
229
                   to be max_bits. */
230
               num = max_bits+ndof*offset;
231
               den = (ndof-1)<<BITRES;
232
               qb = IMIN((num+(den>>1))/den, MAX_FINE_BITS);
233
               celt_assert(qb >= 0);
234
               max_bits += C*qb<<BITRES;
235
            }
236
            max_bits = (4*max_bits/(C*((m->eBands[j+1]-m->eBands[j])<<i)))-64;
237
            celt_assert(max_bits >= 0);
238
            celt_assert(max_bits < 256);
239
            *cap++ = (unsigned char)max_bits;
240
         }
241
      }
242
   }
243
}
244
245
#endif /* CUSTOM_MODES */
246
247
13.0G
#define ALLOC_STEPS 6
248
249
static OPUS_INLINE int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start,
250
      const int *bits1, const int *bits2, const int *thresh, const int *cap, opus_int32 total, opus_int32 *_balance,
251
      int skip_rsv, int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits,
252
      int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev, int signalBandwidth)
253
118M
{
254
118M
   opus_int32 psum;
255
118M
   int lo, hi;
256
118M
   int i, j;
257
118M
   int logM;
258
118M
   int stereo;
259
118M
   int codedBands=-1;
260
118M
   int alloc_floor;
261
118M
   opus_int32 left, percoeff;
262
118M
   int done;
263
118M
   opus_int32 balance;
264
118M
   SAVE_STACK;
265
266
118M
   alloc_floor = C<<BITRES;
267
118M
   stereo = C>1;
268
269
118M
   logM = LM<<BITRES;
270
118M
   lo = 0;
271
118M
   hi = 1<<ALLOC_STEPS;
272
830M
   for (i=0;i<ALLOC_STEPS;i++)
273
712M
   {
274
712M
      int mid = (lo+hi)>>1;
275
712M
      psum = 0;
276
712M
      done = 0;
277
11.0G
      for (j=end;j-->start;)
278
10.3G
      {
279
10.3G
         int tmp = bits1[j] + (mid*(opus_int32)bits2[j]>>ALLOC_STEPS);
280
10.3G
         if (tmp >= thresh[j] || done)
281
5.17G
         {
282
5.17G
            done = 1;
283
            /* Don't allocate more than we can actually use */
284
5.17G
            psum += IMIN(tmp, cap[j]);
285
5.19G
         } else {
286
5.19G
            if (tmp >= alloc_floor)
287
172M
               psum += alloc_floor;
288
5.19G
         }
289
10.3G
      }
290
712M
      if (psum > total)
291
312M
         hi = mid;
292
399M
      else
293
399M
         lo = mid;
294
712M
   }
295
118M
   psum = 0;
296
   /*printf ("interp bisection gave %d\n", lo);*/
297
118M
   done = 0;
298
1.84G
   for (j=end;j-->start;)
299
1.72G
   {
300
1.72G
      int tmp = bits1[j] + ((opus_int32)lo*bits2[j]>>ALLOC_STEPS);
301
1.72G
      if (tmp < thresh[j] && !done)
302
1.10G
      {
303
1.10G
         if (tmp >= alloc_floor)
304
7.41M
            tmp = alloc_floor;
305
1.09G
         else
306
1.09G
            tmp = 0;
307
1.10G
      } else
308
625M
         done = 1;
309
      /* Don't allocate more than we can actually use */
310
1.72G
      tmp = IMIN(tmp, cap[j]);
311
1.72G
      bits[j] = tmp;
312
1.72G
      psum += tmp;
313
1.72G
   }
314
315
   /* Decide which bands to skip, working backwards from the end. */
316
1.09G
   for (codedBands=end;;codedBands--)
317
1.21G
   {
318
1.21G
      int band_width;
319
1.21G
      int band_bits;
320
1.21G
      int rem;
321
1.21G
      j = codedBands-1;
322
      /* Never skip the first band, nor a band that has been boosted by
323
          dynalloc.
324
         In the first case, we'd be coding a bit to signal we're going to waste
325
          all the other bits.
326
         In the second case, we'd be coding a bit to redistribute all the bits
327
          we just signaled should be concentrated in this band. */
328
1.21G
      if (j<=skip_start)
329
76.6M
      {
330
         /* Give the bit we reserved to end skipping back. */
331
76.6M
         total += skip_rsv;
332
76.6M
         break;
333
76.6M
      }
334
      /*Figure out how many left-over bits we would be adding to this band.
335
        This can include bits we've stolen back from higher, skipped bands.*/
336
1.13G
      left = total-psum;
337
1.13G
      percoeff = celt_udiv(left, m->eBands[codedBands]-m->eBands[start]);
338
1.13G
      left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
339
1.13G
      rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0);
340
1.13G
      band_width = m->eBands[codedBands]-m->eBands[j];
341
1.13G
      band_bits = (int)(bits[j] + percoeff*band_width + rem);
342
      /*Only code a skip decision if we're above the threshold for this band.
343
        Otherwise it is force-skipped.
344
        This ensures that we have enough bits to code the skip flag.*/
345
1.13G
      if (band_bits >= IMAX(thresh[j], alloc_floor+(1<<BITRES)))
346
43.7M
      {
347
43.7M
         if (encode)
348
43.4M
         {
349
            /*This if() block is the only part of the allocation function that
350
               is not a mandatory part of the bitstream: any bands we choose to
351
               skip here must be explicitly signaled.*/
352
43.4M
            int depth_threshold;
353
            /*We choose a threshold with some hysteresis to keep bands from
354
               fluctuating in and out, but we try not to fold below a certain point. */
355
43.4M
            if (codedBands > 17)
356
5.83M
               depth_threshold = j<prev ? 7 : 9;
357
37.6M
            else
358
37.6M
               depth_threshold = 0;
359
#ifdef FUZZING
360
            (void)signalBandwidth;
361
            (void)depth_threshold;
362
            if ((rand()&0x1) == 0)
363
#else
364
43.4M
            if (codedBands<=start+2 || (band_bits > (depth_threshold*band_width<<LM<<BITRES)>>4 && j<=signalBandwidth))
365
41.9M
#endif
366
41.9M
            {
367
41.9M
               ec_enc_bit_logp(ec, 1, 1);
368
41.9M
               break;
369
41.9M
            }
370
1.55M
            ec_enc_bit_logp(ec, 0, 1);
371
1.55M
         } else if (ec_dec_bit_logp(ec, 1)) {
372
78.0k
            break;
373
78.0k
         }
374
         /*We used a bit to skip this band.*/
375
1.75M
         psum += 1<<BITRES;
376
1.75M
         band_bits -= 1<<BITRES;
377
1.75M
      }
378
      /*Reclaim the bits originally allocated to this band.*/
379
1.09G
      psum -= bits[j]+intensity_rsv;
380
1.09G
      if (intensity_rsv > 0)
381
45.3M
         intensity_rsv = LOG2_FRAC_TABLE[j-start];
382
1.09G
      psum += intensity_rsv;
383
1.09G
      if (band_bits >= alloc_floor)
384
49.6M
      {
385
         /*If we have enough for a fine energy bit per channel, use it.*/
386
49.6M
         psum += alloc_floor;
387
49.6M
         bits[j] = alloc_floor;
388
1.04G
      } else {
389
         /*Otherwise this band gets nothing at all.*/
390
1.04G
         bits[j] = 0;
391
1.04G
      }
392
1.09G
   }
393
394
118M
   celt_assert(codedBands > start);
395
   /* Code the intensity and dual stereo parameters. */
396
118M
   if (intensity_rsv > 0)
397
11.1M
   {
398
11.1M
      if (encode)
399
11.0M
      {
400
11.0M
         *intensity = IMIN(*intensity, codedBands);
401
11.0M
         ec_enc_uint(ec, *intensity-start, codedBands+1-start);
402
11.0M
      }
403
38.3k
      else
404
38.3k
         *intensity = start+ec_dec_uint(ec, codedBands+1-start);
405
11.1M
   }
406
107M
   else
407
107M
      *intensity = 0;
408
118M
   if (*intensity <= start)
409
107M
   {
410
107M
      total += dual_stereo_rsv;
411
107M
      dual_stereo_rsv = 0;
412
107M
   }
413
118M
   if (dual_stereo_rsv > 0)
414
10.6M
   {
415
10.6M
      if (encode)
416
10.6M
         ec_enc_bit_logp(ec, *dual_stereo, 1);
417
33.2k
      else
418
33.2k
         *dual_stereo = ec_dec_bit_logp(ec, 1);
419
10.6M
   }
420
108M
   else
421
108M
      *dual_stereo = 0;
422
423
   /* Allocate the remaining bits */
424
118M
   left = total-psum;
425
118M
   percoeff = celt_udiv(left, m->eBands[codedBands]-m->eBands[start]);
426
118M
   left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
427
750M
   for (j=start;j<codedBands;j++)
428
631M
      bits[j] += ((int)percoeff*(m->eBands[j+1]-m->eBands[j]));
429
750M
   for (j=start;j<codedBands;j++)
430
631M
   {
431
631M
      int tmp = (int)IMIN(left, m->eBands[j+1]-m->eBands[j]);
432
631M
      bits[j] += tmp;
433
631M
      left -= tmp;
434
631M
   }
435
   /*for (j=0;j<end;j++)printf("%d ", bits[j]);printf("\n");*/
436
437
118M
   balance = 0;
438
750M
   for (j=start;j<codedBands;j++)
439
631M
   {
440
631M
      int N0, N, den;
441
631M
      int offset;
442
631M
      int NClogN;
443
631M
      opus_int32 excess, bit;
444
445
631M
      celt_assert(bits[j] >= 0);
446
631M
      N0 = m->eBands[j+1]-m->eBands[j];
447
631M
      N=N0<<LM;
448
631M
      bit = (opus_int32)bits[j]+balance;
449
450
631M
      if (N>1)
451
453M
      {
452
453M
         excess = MAX32(bit-cap[j],0);
453
453M
         bits[j] = bit-excess;
454
455
         /* Compensate for the extra DoF in stereo */
456
453M
         den=(C*N+ ((C==2 && N>2 && !*dual_stereo && j<*intensity) ? 1 : 0));
457
458
453M
         NClogN = den*(m->logN[j] + logM);
459
460
         /* Offset for the number of fine bits by log2(N)/2 + FINE_OFFSET
461
            compared to their "fair share" of total/N */
462
453M
         offset = (NClogN>>1)-den*FINE_OFFSET;
463
464
         /* N=2 is the only point that doesn't match the curve */
465
453M
         if (N==2)
466
150M
            offset += den<<BITRES>>2;
467
468
         /* Changing the offset for allocating the second and third
469
             fine energy bit */
470
453M
         if (bits[j] + offset < den*2<<BITRES)
471
351M
            offset += NClogN>>2;
472
102M
         else if (bits[j] + offset < den*3<<BITRES)
473
35.2M
            offset += NClogN>>3;
474
475
         /* Divide with rounding */
476
453M
         ebits[j] = IMAX(0, (bits[j] + offset + (den<<(BITRES-1))));
477
453M
         ebits[j] = celt_udiv(ebits[j], den)>>BITRES;
478
479
         /* Make sure not to bust */
480
453M
         if (C*ebits[j] > (bits[j]>>BITRES))
481
65.3k
            ebits[j] = bits[j] >> stereo >> BITRES;
482
483
         /* More than that is useless because that's about as far as PVQ can go */
484
453M
         ebits[j] = IMIN(ebits[j], MAX_FINE_BITS);
485
486
         /* If we rounded down or capped this band, make it a candidate for the
487
             final fine energy pass */
488
453M
         fine_priority[j] = ebits[j]*(den<<BITRES) >= bits[j]+offset;
489
490
         /* Remove the allocated fine bits; the rest are assigned to PVQ */
491
453M
         bits[j] -= C*ebits[j]<<BITRES;
492
493
453M
      } else {
494
         /* For N=1, all bits go to fine energy except for a single sign bit */
495
178M
         excess = MAX32(0,bit-(C<<BITRES));
496
178M
         bits[j] = bit-excess;
497
178M
         ebits[j] = 0;
498
178M
         fine_priority[j] = 1;
499
178M
      }
500
501
      /* Fine energy can't take advantage of the re-balancing in
502
          quant_all_bands().
503
         Instead, do the re-balancing here.*/
504
631M
      if(excess > 0)
505
138M
      {
506
138M
         int extra_fine;
507
138M
         int extra_bits;
508
138M
         extra_fine = IMIN(excess>>(stereo+BITRES),MAX_FINE_BITS-ebits[j]);
509
138M
         ebits[j] += extra_fine;
510
138M
         extra_bits = extra_fine*C<<BITRES;
511
138M
         fine_priority[j] = extra_bits >= excess-balance;
512
138M
         excess -= extra_bits;
513
138M
      }
514
631M
      balance = excess;
515
516
631M
      celt_assert(bits[j] >= 0);
517
631M
      celt_assert(ebits[j] >= 0);
518
631M
   }
519
   /* Save any remaining bits over the cap for the rebalancing in
520
       quant_all_bands(). */
521
118M
   *_balance = balance;
522
523
   /* The skipped bands use all their bits for fine energy. */
524
1.21G
   for (;j<end;j++)
525
1.09G
   {
526
1.09G
      ebits[j] = bits[j] >> stereo >> BITRES;
527
1.09G
      celt_assert(C*ebits[j]<<BITRES == bits[j]);
528
1.09G
      bits[j] = 0;
529
1.09G
      fine_priority[j] = ebits[j]<1;
530
1.09G
   }
531
118M
   RESTORE_STACK;
532
118M
   return codedBands;
533
118M
}
534
535
int clt_compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stereo,
536
      opus_int32 total, opus_int32 *balance, int *pulses, int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev, int signalBandwidth)
537
118M
{
538
118M
   int lo, hi, len, j;
539
118M
   int codedBands;
540
118M
   int skip_start;
541
118M
   int skip_rsv;
542
118M
   int intensity_rsv;
543
118M
   int dual_stereo_rsv;
544
118M
   VARDECL(int, bits1);
545
118M
   VARDECL(int, bits2);
546
118M
   VARDECL(int, thresh);
547
118M
   VARDECL(int, trim_offset);
548
118M
   SAVE_STACK;
549
550
118M
   total = IMAX(total, 0);
551
118M
   len = m->nbEBands;
552
118M
   skip_start = start;
553
   /* Reserve a bit to signal the end of manually skipped bands. */
554
118M
   skip_rsv = total >= 1<<BITRES ? 1<<BITRES : 0;
555
118M
   total -= skip_rsv;
556
   /* Reserve bits for the intensity and dual stereo parameters. */
557
118M
   intensity_rsv = dual_stereo_rsv = 0;
558
118M
   if (C==2)
559
23.9M
   {
560
23.9M
      intensity_rsv = LOG2_FRAC_TABLE[end-start];
561
23.9M
      if (intensity_rsv>total)
562
12.8M
         intensity_rsv = 0;
563
11.1M
      else
564
11.1M
      {
565
11.1M
         total -= intensity_rsv;
566
11.1M
         dual_stereo_rsv = total>=1<<BITRES ? 1<<BITRES : 0;
567
11.1M
         total -= dual_stereo_rsv;
568
11.1M
      }
569
23.9M
   }
570
118M
   ALLOC(bits1, len, int);
571
118M
   ALLOC(bits2, len, int);
572
118M
   ALLOC(thresh, len, int);
573
118M
   ALLOC(trim_offset, len, int);
574
575
1.84G
   for (j=start;j<end;j++)
576
1.72G
   {
577
      /* Below this threshold, we're sure not to allocate any PVQ bits */
578
1.72G
      thresh[j] = IMAX((C)<<BITRES, (3*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>4);
579
      /* Tilt of the allocation curve */
580
1.72G
      trim_offset[j] = C*(m->eBands[j+1]-m->eBands[j])*(alloc_trim-5-LM)*(end-j-1)
581
1.72G
            *(1<<(LM+BITRES))>>6;
582
      /* Giving less resolution to single-coefficient bands because they get
583
         more benefit from having one coarse value per coefficient*/
584
1.72G
      if ((m->eBands[j+1]-m->eBands[j])<<LM==1)
585
516M
         trim_offset[j] -= C<<BITRES;
586
1.72G
   }
587
118M
   lo = 1;
588
118M
   hi = m->nbAllocVectors - 1;
589
118M
   do
590
372M
   {
591
372M
      int done = 0;
592
372M
      int psum = 0;
593
372M
      int mid = (lo+hi) >> 1;
594
5.80G
      for (j=end;j-->start;)
595
5.42G
      {
596
5.42G
         int bitsj;
597
5.42G
         int N = m->eBands[j+1]-m->eBands[j];
598
5.42G
         bitsj = C*N*m->allocVectors[mid*len+j]<<LM>>2;
599
5.42G
         if (bitsj > 0)
600
5.20G
            bitsj = IMAX(0, bitsj + trim_offset[j]);
601
5.42G
         bitsj += offsets[j];
602
5.42G
         if (bitsj >= thresh[j] || done)
603
5.17G
         {
604
5.17G
            done = 1;
605
            /* Don't allocate more than we can actually use */
606
5.17G
            psum += IMIN(bitsj, cap[j]);
607
5.17G
         } else {
608
254M
            if (bitsj >= C<<BITRES)
609
17.6M
               psum += C<<BITRES;
610
254M
         }
611
5.42G
      }
612
372M
      if (psum > total)
613
302M
         hi = mid - 1;
614
70.2M
      else
615
70.2M
         lo = mid + 1;
616
      /*printf ("lo = %d, hi = %d\n", lo, hi);*/
617
372M
   }
618
372M
   while (lo <= hi);
619
118M
   hi = lo--;
620
   /*printf ("interp between %d and %d\n", lo, hi);*/
621
1.84G
   for (j=start;j<end;j++)
622
1.72G
   {
623
1.72G
      int bits1j, bits2j;
624
1.72G
      int N = m->eBands[j+1]-m->eBands[j];
625
1.72G
      bits1j = C*N*m->allocVectors[lo*len+j]<<LM>>2;
626
1.72G
      bits2j = hi>=m->nbAllocVectors ?
627
1.67G
            cap[j] : C*N*m->allocVectors[hi*len+j]<<LM>>2;
628
1.72G
      if (bits1j > 0)
629
500M
         bits1j = IMAX(0, bits1j + trim_offset[j]);
630
1.72G
      if (bits2j > 0)
631
1.59G
         bits2j = IMAX(0, bits2j + trim_offset[j]);
632
1.72G
      if (lo > 0)
633
522M
         bits1j += offsets[j];
634
1.72G
      bits2j += offsets[j];
635
1.72G
      if (offsets[j]>0)
636
3.76M
         skip_start = j;
637
1.72G
      bits2j = IMAX(0,bits2j-bits1j);
638
1.72G
      bits1[j] = bits1j;
639
1.72G
      bits2[j] = bits2j;
640
1.72G
   }
641
118M
   codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh, cap,
642
118M
         total, balance, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv,
643
118M
         pulses, ebits, fine_priority, C, LM, ec, encode, prev, signalBandwidth);
644
118M
   RESTORE_STACK;
645
118M
   return codedBands;
646
118M
}
647
#ifdef ENABLE_QEXT
648
649
static const unsigned char last_zero[3] = {64, 50, 0};
650
static const unsigned char last_cap[3] = {110, 60, 0};
651
static const unsigned char last_other[4] = {120, 112, 70, 0};
652
653
2.20M
static void ec_enc_depth(ec_enc *enc, opus_int32 depth, opus_int32 cap, opus_int32 *last) {
654
2.20M
   int sym = 3;
655
2.20M
   if (depth==*last) sym = 2;
656
2.20M
   if (depth==cap) sym = 1;
657
2.20M
   if (depth==0) sym = 0;
658
2.20M
   if (*last == 0) {
659
1.25M
      ec_enc_icdf(enc, IMIN(sym, 2), last_zero, 7);
660
1.25M
   } else if (*last == cap) {
661
0
      ec_enc_icdf(enc, IMIN(sym, 2), last_cap, 7);
662
948k
   } else {
663
948k
      ec_enc_icdf(enc, sym, last_other, 7);
664
948k
   }
665
   /* We accept some redundancy if depth==last (for last different from 0 and cap). */
666
2.20M
   if (sym == 3) ec_enc_uint(enc, depth-1, cap);
667
2.20M
   *last = depth;
668
2.20M
}
669
670
138k
static int ec_dec_depth(ec_dec *dec, opus_int32 cap, opus_int32 *last) {
671
138k
   int depth, sym;
672
138k
   if (*last == 0) {
673
48.5k
      sym = ec_dec_icdf(dec, last_zero, 7);
674
48.5k
      if (sym==2) sym=3;
675
90.3k
   } else if (*last == cap) {
676
15.4k
      sym = ec_dec_icdf(dec, last_cap, 7);
677
15.4k
      if (sym==2) sym=3;
678
74.8k
   } else {
679
74.8k
      sym = ec_dec_icdf(dec, last_other, 7);
680
74.8k
   }
681
138k
   if (sym==0) depth=0;
682
96.6k
   else if (sym==1) depth=cap;
683
82.6k
   else if (sym==2) depth=*last;
684
56.4k
   else depth = 1 + ec_dec_uint(dec, cap);
685
138k
   *last = depth;
686
138k
   return depth;
687
138k
}
688
689
2.97M
#define MSWAP16(a,b) do {opus_val16 tmp = a;a=b;b=tmp;} while(0)
690
static opus_val16 median_of_5_val16(const opus_val16 *x)
691
1.70M
{
692
1.70M
   opus_val16 t0, t1, t2, t3, t4;
693
1.70M
   t2 = x[2];
694
1.70M
   if (x[0] > x[1])
695
1.45M
   {
696
1.45M
      t0 = x[1];
697
1.45M
      t1 = x[0];
698
1.45M
   } else {
699
247k
      t0 = x[0];
700
247k
      t1 = x[1];
701
247k
   }
702
1.70M
   if (x[3] > x[4])
703
1.34M
   {
704
1.34M
      t3 = x[4];
705
1.34M
      t4 = x[3];
706
1.34M
   } else {
707
358k
      t3 = x[3];
708
358k
      t4 = x[4];
709
358k
   }
710
1.70M
   if (t0 > t3)
711
1.48M
   {
712
1.48M
      MSWAP16(t0, t3);
713
1.48M
      MSWAP16(t1, t4);
714
1.48M
   }
715
1.70M
   if (t2 > t1)
716
1.21M
   {
717
1.21M
      if (t1 < t3)
718
1.10M
         return MIN16(t2, t3);
719
113k
      else
720
113k
         return MIN16(t4, t1);
721
1.21M
   } else {
722
484k
      if (t2 < t3)
723
357k
         return MIN16(t1, t3);
724
127k
      else
725
127k
         return MIN16(t2, t4);
726
484k
   }
727
1.70M
}
728
729
void clt_compute_extra_allocation(const CELTMode *m, const CELTMode *qext_mode, int start, int end, int qext_end, const celt_glog *bandLogE, const celt_glog *qext_bandLogE,
730
      opus_int32 total, int *extra_pulses, int *extra_equant, int C, int LM, ec_ctx *ec, int encode, opus_val16 tone_freq, opus_val32 toneishness)
731
138M
{
732
138M
   int i;
733
138M
   opus_int32 last=0;
734
138M
   opus_val32 sum;
735
138M
   opus_val32 fill;
736
138M
   int iter;
737
138M
   int tot_bands;
738
138M
   int tot_samples;
739
138M
   VARDECL(int, depth);
740
138M
   VARDECL(opus_int32, cap);
741
#ifdef FUZZING
742
   float depth_std;
743
#endif
744
138M
   SAVE_STACK;
745
#ifdef FUZZING
746
   depth_std = -10.f*log(1e-8+(float)rand()/(float)RAND_MAX);
747
   depth_std = FMAX(0, FMIN(48, depth_std));
748
#endif
749
138M
   if (qext_mode != NULL) {
750
26.2k
      celt_assert(end==m->nbEBands);
751
26.2k
      tot_bands = end + qext_end;
752
26.2k
      tot_samples = qext_mode->eBands[qext_end]*C<<LM;
753
138M
   } else {
754
138M
      tot_bands = end;
755
138M
      tot_samples = (m->eBands[end]-m->eBands[start])*C<<LM;
756
138M
   }
757
138M
   ALLOC(cap, tot_bands, opus_int32);
758
2.12G
   for (i=start;i<end;i++) cap[i] = 12;
759
138M
   if (qext_mode != NULL) {
760
245k
      for (i=0;i<qext_end;i++) cap[end+i] = 14;
761
26.2k
   }
762
138M
   if (total <= 0) {
763
3.04G
      for (i=start;i<m->nbEBands+qext_end;i++) {
764
2.90G
         extra_pulses[i] = extra_equant[i] = 0;
765
2.90G
      }
766
138M
      RESTORE_STACK;
767
138M
      return;
768
138M
   }
769
268k
   ALLOC(depth, tot_bands, int);
770
268k
   if (encode) {
771
251k
      VARDECL(opus_val16, flatE);
772
251k
      VARDECL(int, Ncoef);
773
251k
      VARDECL(opus_val16, min);
774
251k
      VARDECL(opus_val16, follower);
775
776
251k
      ALLOC(flatE, tot_bands, opus_val16);
777
251k
      ALLOC(min, tot_bands, opus_val16);
778
251k
      ALLOC(Ncoef, tot_bands, int);
779
4.54M
      for (i=start;i<end;i++) {
780
4.29M
         Ncoef[i] = (m->eBands[i+1]-m->eBands[i])*C<<LM;
781
4.29M
      }
782
      /* Remove the effect of band width, eMeans and pre-emphasis to compute the real (flat) spectrum. */
783
4.54M
      for (i=start;i<end;i++) {
784
4.29M
         flatE[i] = PSHR32(bandLogE[i] - GCONST(0.0625f)*m->logN[i] + SHL32(eMeans[i],DB_SHIFT-4) - GCONST(.0062f)*(i+5)*(i+5), DB_SHIFT-10);
785
4.29M
         min[i] = 0;
786
4.29M
      }
787
251k
      if (C==2) {
788
0
         for (i=start;i<end;i++) {
789
0
            flatE[i] = MAXG(flatE[i], PSHR32(bandLogE[m->nbEBands+i] - GCONST(0.0625f)*m->logN[i] + SHL32(eMeans[i],DB_SHIFT-4) - GCONST(.0062f)*(i+5)*(i+5), DB_SHIFT-10));
790
0
         }
791
0
      }
792
251k
      flatE[end-1] += QCONST16(2.f, 10);
793
251k
      if (qext_mode != NULL) {
794
8.92k
         opus_val16 min_depth = 0;
795
         /* If we have enough bits, give at least 1 bit of depth to all higher bands. */
796
8.92k
         if (total >= 3*C*(qext_mode->eBands[qext_end]-qext_mode->eBands[start])<<LM<<BITRES && (toneishness < QCONST32(.98f, 29) || tone_freq > 1.33f))
797
2
            min_depth = QCONST16(1.f, 10);
798
121k
         for (i=0;i<qext_end;i++) {
799
112k
            Ncoef[end+i] = (qext_mode->eBands[i+1]-qext_mode->eBands[i])*C<<LM;
800
112k
            min[end+i] = min_depth;
801
112k
         }
802
121k
         for (i=0;i<qext_end;i++) {
803
112k
            flatE[end+i] = PSHR32(qext_bandLogE[i] - GCONST(0.0625f)*qext_mode->logN[i] + SHL32(eMeans[i],DB_SHIFT-4) - GCONST(.0062f)*(end+i+5)*(end+i+5), DB_SHIFT-10);
804
112k
         }
805
8.92k
         if (C==2) {
806
0
            for (i=0;i<qext_end;i++) {
807
0
               flatE[end+i] = MAXG(flatE[end+i], PSHR32(qext_bandLogE[NB_QEXT_BANDS+i] - GCONST(0.0625f)*qext_mode->logN[i] + SHL32(eMeans[i],DB_SHIFT-4) - GCONST(.0062f)*(end+i+5)*(end+i+5), DB_SHIFT-10));
808
0
            }
809
0
         }
810
8.92k
      }
811
251k
      ALLOC(follower, tot_bands, opus_val16);
812
3.65M
      for (i=start+2;i<tot_bands-2;i++) {
813
3.40M
         follower[i] = median_of_5_val16(&flatE[i-2]);
814
3.40M
      }
815
251k
      follower[start] = follower[start+1] = follower[start+2];
816
251k
      follower[tot_bands-1] = follower[tot_bands-2] = follower[tot_bands-3];
817
4.40M
      for (i=start+1;i<tot_bands;i++) {
818
4.15M
         follower[i] = MAX16(follower[i], follower[i-1]-QCONST16(1.f, 10));
819
4.15M
      }
820
4.40M
      for (i=tot_bands-2;i>=start;i--) {
821
4.15M
         follower[i] = MAX16(follower[i], follower[i+1]-QCONST16(1.f, 10));
822
4.15M
      }
823
4.66M
      for (i=start;i<tot_bands;i++) flatE[i] -= MULT16_16_Q15(Q15ONE-PSHR32(toneishness, 14), follower[i]);
824
251k
      if (qext_mode != NULL) {
825
121k
         for (i=0;i<qext_end;i++) flatE[end+i] = flatE[end+i] + QCONST16(3.f, 10) + QCONST16(.2f, 10)*i;
826
8.92k
      }
827
      /* Approximate fill level assuming all bands contribute fully. */
828
251k
      sum = 0;
829
4.66M
      for (i=start;i<tot_bands;i++) {
830
4.40M
         sum += MULT16_16(Ncoef[i], flatE[i]);
831
4.40M
      }
832
251k
      total >>= BITRES;
833
251k
      fill = (SHL32(total, 10) + sum)/tot_samples;
834
      /* Iteratively refine the fill level considering the depth min and cap. */
835
2.76M
      for (iter=0;iter<10;iter++) {
836
2.51M
         sum = 0;
837
46.6M
         for (i=start;i<tot_bands;i++)
838
44.0M
            sum += Ncoef[i] * MIN32(SHL32(cap[i], 10), MAX32(min[i], flatE[i]-fill));
839
2.51M
         fill -= (SHL32(total, 10) - sum)/tot_samples;
840
2.51M
      }
841
4.66M
      for (i=start;i<tot_bands;i++) {
842
#ifdef FIXED_POINT
843
2.20M
         depth[i] = PSHR32(MIN32(SHL32(cap[i], 10), MAX32(min[i], flatE[i]-fill)), 10-2);
844
#else
845
2.20M
         depth[i] = (int)floor(.5+4*MIN32(SHL32(cap[i], 10), MAX32(min[i], flatE[i]-fill)));
846
#endif
847
#ifdef FUZZING
848
         depth[i] = (int)-depth_std*log(1e-8+(float)rand()/(float)RAND_MAX);
849
         depth[i] = IMAX(0, IMIN(cap[i]<<2, depth[i]));
850
#endif
851
4.40M
         if (ec_tell_frac(ec) + 80 < ec->storage*8<<BITRES)
852
4.40M
            ec_enc_depth(ec, depth[i], 4*cap[i], &last);
853
0
         else
854
0
            depth[i] = 0;
855
4.40M
      }
856
251k
   } else {
857
385k
      for (i=start;i<tot_bands;i++) {
858
368k
         if (ec_tell_frac(ec) + 80 < ec->storage*8<<BITRES)
859
277k
            depth[i] = ec_dec_depth(ec, 4*cap[i], &last);
860
90.4k
         else
861
90.4k
            depth[i] = 0;
862
368k
      }
863
16.9k
   }
864
4.85M
   for (i=start;i<end;i++) {
865
4.59M
      extra_equant[i] = (depth[i]+3)>>2;
866
4.59M
      extra_pulses[i] = ((((m->eBands[i+1]-m->eBands[i])<<LM)-1)*C * depth[i] * (1<<BITRES) + 2)>>2;
867
4.59M
   }
868
268k
   if (qext_mode) {
869
207k
      for (i=0;i<qext_end;i++) {
870
186k
         extra_equant[end+i] = (depth[end+i]+3)>>2;
871
186k
         extra_pulses[end+i] = ((((qext_mode->eBands[i+1]-qext_mode->eBands[i])<<LM)-1)*C * depth[end+i] * (1<<BITRES) + 2)>>2;
872
186k
      }
873
20.9k
   }
874
268k
   RESTORE_STACK;
875
268k
}
clt_compute_extra_allocation
Line
Count
Source
731
69.3M
{
732
69.3M
   int i;
733
69.3M
   opus_int32 last=0;
734
69.3M
   opus_val32 sum;
735
69.3M
   opus_val32 fill;
736
69.3M
   int iter;
737
69.3M
   int tot_bands;
738
69.3M
   int tot_samples;
739
69.3M
   VARDECL(int, depth);
740
69.3M
   VARDECL(opus_int32, cap);
741
#ifdef FUZZING
742
   float depth_std;
743
#endif
744
69.3M
   SAVE_STACK;
745
#ifdef FUZZING
746
   depth_std = -10.f*log(1e-8+(float)rand()/(float)RAND_MAX);
747
   depth_std = FMAX(0, FMIN(48, depth_std));
748
#endif
749
69.3M
   if (qext_mode != NULL) {
750
13.1k
      celt_assert(end==m->nbEBands);
751
13.1k
      tot_bands = end + qext_end;
752
13.1k
      tot_samples = qext_mode->eBands[qext_end]*C<<LM;
753
69.3M
   } else {
754
69.3M
      tot_bands = end;
755
69.3M
      tot_samples = (m->eBands[end]-m->eBands[start])*C<<LM;
756
69.3M
   }
757
69.3M
   ALLOC(cap, tot_bands, opus_int32);
758
1.06G
   for (i=start;i<end;i++) cap[i] = 12;
759
69.3M
   if (qext_mode != NULL) {
760
122k
      for (i=0;i<qext_end;i++) cap[end+i] = 14;
761
13.1k
   }
762
69.3M
   if (total <= 0) {
763
1.52G
      for (i=start;i<m->nbEBands+qext_end;i++) {
764
1.45G
         extra_pulses[i] = extra_equant[i] = 0;
765
1.45G
      }
766
69.2M
      RESTORE_STACK;
767
69.2M
      return;
768
69.2M
   }
769
134k
   ALLOC(depth, tot_bands, int);
770
134k
   if (encode) {
771
125k
      VARDECL(opus_val16, flatE);
772
125k
      VARDECL(int, Ncoef);
773
125k
      VARDECL(opus_val16, min);
774
125k
      VARDECL(opus_val16, follower);
775
776
125k
      ALLOC(flatE, tot_bands, opus_val16);
777
125k
      ALLOC(min, tot_bands, opus_val16);
778
125k
      ALLOC(Ncoef, tot_bands, int);
779
2.27M
      for (i=start;i<end;i++) {
780
2.14M
         Ncoef[i] = (m->eBands[i+1]-m->eBands[i])*C<<LM;
781
2.14M
      }
782
      /* Remove the effect of band width, eMeans and pre-emphasis to compute the real (flat) spectrum. */
783
2.27M
      for (i=start;i<end;i++) {
784
2.14M
         flatE[i] = PSHR32(bandLogE[i] - GCONST(0.0625f)*m->logN[i] + SHL32(eMeans[i],DB_SHIFT-4) - GCONST(.0062f)*(i+5)*(i+5), DB_SHIFT-10);
785
2.14M
         min[i] = 0;
786
2.14M
      }
787
125k
      if (C==2) {
788
0
         for (i=start;i<end;i++) {
789
0
            flatE[i] = MAXG(flatE[i], PSHR32(bandLogE[m->nbEBands+i] - GCONST(0.0625f)*m->logN[i] + SHL32(eMeans[i],DB_SHIFT-4) - GCONST(.0062f)*(i+5)*(i+5), DB_SHIFT-10));
790
0
         }
791
0
      }
792
125k
      flatE[end-1] += QCONST16(2.f, 10);
793
125k
      if (qext_mode != NULL) {
794
4.46k
         opus_val16 min_depth = 0;
795
         /* If we have enough bits, give at least 1 bit of depth to all higher bands. */
796
4.46k
         if (total >= 3*C*(qext_mode->eBands[qext_end]-qext_mode->eBands[start])<<LM<<BITRES && (toneishness < QCONST32(.98f, 29) || tone_freq > 1.33f))
797
1
            min_depth = QCONST16(1.f, 10);
798
60.8k
         for (i=0;i<qext_end;i++) {
799
56.3k
            Ncoef[end+i] = (qext_mode->eBands[i+1]-qext_mode->eBands[i])*C<<LM;
800
56.3k
            min[end+i] = min_depth;
801
56.3k
         }
802
60.8k
         for (i=0;i<qext_end;i++) {
803
56.3k
            flatE[end+i] = PSHR32(qext_bandLogE[i] - GCONST(0.0625f)*qext_mode->logN[i] + SHL32(eMeans[i],DB_SHIFT-4) - GCONST(.0062f)*(end+i+5)*(end+i+5), DB_SHIFT-10);
804
56.3k
         }
805
4.46k
         if (C==2) {
806
0
            for (i=0;i<qext_end;i++) {
807
0
               flatE[end+i] = MAXG(flatE[end+i], PSHR32(qext_bandLogE[NB_QEXT_BANDS+i] - GCONST(0.0625f)*qext_mode->logN[i] + SHL32(eMeans[i],DB_SHIFT-4) - GCONST(.0062f)*(end+i+5)*(end+i+5), DB_SHIFT-10));
808
0
            }
809
0
         }
810
4.46k
      }
811
125k
      ALLOC(follower, tot_bands, opus_val16);
812
1.82M
      for (i=start+2;i<tot_bands-2;i++) {
813
1.70M
         follower[i] = median_of_5_val16(&flatE[i-2]);
814
1.70M
      }
815
125k
      follower[start] = follower[start+1] = follower[start+2];
816
125k
      follower[tot_bands-1] = follower[tot_bands-2] = follower[tot_bands-3];
817
2.20M
      for (i=start+1;i<tot_bands;i++) {
818
2.07M
         follower[i] = MAX16(follower[i], follower[i-1]-QCONST16(1.f, 10));
819
2.07M
      }
820
2.20M
      for (i=tot_bands-2;i>=start;i--) {
821
2.07M
         follower[i] = MAX16(follower[i], follower[i+1]-QCONST16(1.f, 10));
822
2.07M
      }
823
2.33M
      for (i=start;i<tot_bands;i++) flatE[i] -= MULT16_16_Q15(Q15ONE-PSHR32(toneishness, 14), follower[i]);
824
125k
      if (qext_mode != NULL) {
825
60.8k
         for (i=0;i<qext_end;i++) flatE[end+i] = flatE[end+i] + QCONST16(3.f, 10) + QCONST16(.2f, 10)*i;
826
4.46k
      }
827
      /* Approximate fill level assuming all bands contribute fully. */
828
125k
      sum = 0;
829
2.33M
      for (i=start;i<tot_bands;i++) {
830
2.20M
         sum += MULT16_16(Ncoef[i], flatE[i]);
831
2.20M
      }
832
125k
      total >>= BITRES;
833
125k
      fill = (SHL32(total, 10) + sum)/tot_samples;
834
      /* Iteratively refine the fill level considering the depth min and cap. */
835
1.38M
      for (iter=0;iter<10;iter++) {
836
1.25M
         sum = 0;
837
23.3M
         for (i=start;i<tot_bands;i++)
838
22.0M
            sum += Ncoef[i] * MIN32(SHL32(cap[i], 10), MAX32(min[i], flatE[i]-fill));
839
1.25M
         fill -= (SHL32(total, 10) - sum)/tot_samples;
840
1.25M
      }
841
2.33M
      for (i=start;i<tot_bands;i++) {
842
2.20M
#ifdef FIXED_POINT
843
2.20M
         depth[i] = PSHR32(MIN32(SHL32(cap[i], 10), MAX32(min[i], flatE[i]-fill)), 10-2);
844
#else
845
         depth[i] = (int)floor(.5+4*MIN32(SHL32(cap[i], 10), MAX32(min[i], flatE[i]-fill)));
846
#endif
847
#ifdef FUZZING
848
         depth[i] = (int)-depth_std*log(1e-8+(float)rand()/(float)RAND_MAX);
849
         depth[i] = IMAX(0, IMIN(cap[i]<<2, depth[i]));
850
#endif
851
2.20M
         if (ec_tell_frac(ec) + 80 < ec->storage*8<<BITRES)
852
2.20M
            ec_enc_depth(ec, depth[i], 4*cap[i], &last);
853
0
         else
854
0
            depth[i] = 0;
855
2.20M
      }
856
125k
   } else {
857
192k
      for (i=start;i<tot_bands;i++) {
858
184k
         if (ec_tell_frac(ec) + 80 < ec->storage*8<<BITRES)
859
138k
            depth[i] = ec_dec_depth(ec, 4*cap[i], &last);
860
45.2k
         else
861
45.2k
            depth[i] = 0;
862
184k
      }
863
8.49k
   }
864
2.42M
   for (i=start;i<end;i++) {
865
2.29M
      extra_equant[i] = (depth[i]+3)>>2;
866
2.29M
      extra_pulses[i] = ((((m->eBands[i+1]-m->eBands[i])<<LM)-1)*C * depth[i] * (1<<BITRES) + 2)>>2;
867
2.29M
   }
868
134k
   if (qext_mode) {
869
103k
      for (i=0;i<qext_end;i++) {
870
93.2k
         extra_equant[end+i] = (depth[end+i]+3)>>2;
871
93.2k
         extra_pulses[end+i] = ((((qext_mode->eBands[i+1]-qext_mode->eBands[i])<<LM)-1)*C * depth[end+i] * (1<<BITRES) + 2)>>2;
872
93.2k
      }
873
10.4k
   }
874
134k
   RESTORE_STACK;
875
134k
}
clt_compute_extra_allocation
Line
Count
Source
731
69.3M
{
732
69.3M
   int i;
733
69.3M
   opus_int32 last=0;
734
69.3M
   opus_val32 sum;
735
69.3M
   opus_val32 fill;
736
69.3M
   int iter;
737
69.3M
   int tot_bands;
738
69.3M
   int tot_samples;
739
69.3M
   VARDECL(int, depth);
740
69.3M
   VARDECL(opus_int32, cap);
741
#ifdef FUZZING
742
   float depth_std;
743
#endif
744
69.3M
   SAVE_STACK;
745
#ifdef FUZZING
746
   depth_std = -10.f*log(1e-8+(float)rand()/(float)RAND_MAX);
747
   depth_std = FMAX(0, FMIN(48, depth_std));
748
#endif
749
69.3M
   if (qext_mode != NULL) {
750
13.1k
      celt_assert(end==m->nbEBands);
751
13.1k
      tot_bands = end + qext_end;
752
13.1k
      tot_samples = qext_mode->eBands[qext_end]*C<<LM;
753
69.3M
   } else {
754
69.3M
      tot_bands = end;
755
69.3M
      tot_samples = (m->eBands[end]-m->eBands[start])*C<<LM;
756
69.3M
   }
757
69.3M
   ALLOC(cap, tot_bands, opus_int32);
758
1.06G
   for (i=start;i<end;i++) cap[i] = 12;
759
69.3M
   if (qext_mode != NULL) {
760
122k
      for (i=0;i<qext_end;i++) cap[end+i] = 14;
761
13.1k
   }
762
69.3M
   if (total <= 0) {
763
1.52G
      for (i=start;i<m->nbEBands+qext_end;i++) {
764
1.45G
         extra_pulses[i] = extra_equant[i] = 0;
765
1.45G
      }
766
69.2M
      RESTORE_STACK;
767
69.2M
      return;
768
69.2M
   }
769
134k
   ALLOC(depth, tot_bands, int);
770
134k
   if (encode) {
771
125k
      VARDECL(opus_val16, flatE);
772
125k
      VARDECL(int, Ncoef);
773
125k
      VARDECL(opus_val16, min);
774
125k
      VARDECL(opus_val16, follower);
775
776
125k
      ALLOC(flatE, tot_bands, opus_val16);
777
125k
      ALLOC(min, tot_bands, opus_val16);
778
125k
      ALLOC(Ncoef, tot_bands, int);
779
2.27M
      for (i=start;i<end;i++) {
780
2.14M
         Ncoef[i] = (m->eBands[i+1]-m->eBands[i])*C<<LM;
781
2.14M
      }
782
      /* Remove the effect of band width, eMeans and pre-emphasis to compute the real (flat) spectrum. */
783
2.27M
      for (i=start;i<end;i++) {
784
2.14M
         flatE[i] = PSHR32(bandLogE[i] - GCONST(0.0625f)*m->logN[i] + SHL32(eMeans[i],DB_SHIFT-4) - GCONST(.0062f)*(i+5)*(i+5), DB_SHIFT-10);
785
2.14M
         min[i] = 0;
786
2.14M
      }
787
125k
      if (C==2) {
788
0
         for (i=start;i<end;i++) {
789
0
            flatE[i] = MAXG(flatE[i], PSHR32(bandLogE[m->nbEBands+i] - GCONST(0.0625f)*m->logN[i] + SHL32(eMeans[i],DB_SHIFT-4) - GCONST(.0062f)*(i+5)*(i+5), DB_SHIFT-10));
790
0
         }
791
0
      }
792
125k
      flatE[end-1] += QCONST16(2.f, 10);
793
125k
      if (qext_mode != NULL) {
794
4.46k
         opus_val16 min_depth = 0;
795
         /* If we have enough bits, give at least 1 bit of depth to all higher bands. */
796
4.46k
         if (total >= 3*C*(qext_mode->eBands[qext_end]-qext_mode->eBands[start])<<LM<<BITRES && (toneishness < QCONST32(.98f, 29) || tone_freq > 1.33f))
797
1
            min_depth = QCONST16(1.f, 10);
798
60.8k
         for (i=0;i<qext_end;i++) {
799
56.3k
            Ncoef[end+i] = (qext_mode->eBands[i+1]-qext_mode->eBands[i])*C<<LM;
800
56.3k
            min[end+i] = min_depth;
801
56.3k
         }
802
60.8k
         for (i=0;i<qext_end;i++) {
803
56.3k
            flatE[end+i] = PSHR32(qext_bandLogE[i] - GCONST(0.0625f)*qext_mode->logN[i] + SHL32(eMeans[i],DB_SHIFT-4) - GCONST(.0062f)*(end+i+5)*(end+i+5), DB_SHIFT-10);
804
56.3k
         }
805
4.46k
         if (C==2) {
806
0
            for (i=0;i<qext_end;i++) {
807
0
               flatE[end+i] = MAXG(flatE[end+i], PSHR32(qext_bandLogE[NB_QEXT_BANDS+i] - GCONST(0.0625f)*qext_mode->logN[i] + SHL32(eMeans[i],DB_SHIFT-4) - GCONST(.0062f)*(end+i+5)*(end+i+5), DB_SHIFT-10));
808
0
            }
809
0
         }
810
4.46k
      }
811
125k
      ALLOC(follower, tot_bands, opus_val16);
812
1.82M
      for (i=start+2;i<tot_bands-2;i++) {
813
1.70M
         follower[i] = median_of_5_val16(&flatE[i-2]);
814
1.70M
      }
815
125k
      follower[start] = follower[start+1] = follower[start+2];
816
125k
      follower[tot_bands-1] = follower[tot_bands-2] = follower[tot_bands-3];
817
2.20M
      for (i=start+1;i<tot_bands;i++) {
818
2.07M
         follower[i] = MAX16(follower[i], follower[i-1]-QCONST16(1.f, 10));
819
2.07M
      }
820
2.20M
      for (i=tot_bands-2;i>=start;i--) {
821
2.07M
         follower[i] = MAX16(follower[i], follower[i+1]-QCONST16(1.f, 10));
822
2.07M
      }
823
2.33M
      for (i=start;i<tot_bands;i++) flatE[i] -= MULT16_16_Q15(Q15ONE-PSHR32(toneishness, 14), follower[i]);
824
125k
      if (qext_mode != NULL) {
825
60.8k
         for (i=0;i<qext_end;i++) flatE[end+i] = flatE[end+i] + QCONST16(3.f, 10) + QCONST16(.2f, 10)*i;
826
4.46k
      }
827
      /* Approximate fill level assuming all bands contribute fully. */
828
125k
      sum = 0;
829
2.33M
      for (i=start;i<tot_bands;i++) {
830
2.20M
         sum += MULT16_16(Ncoef[i], flatE[i]);
831
2.20M
      }
832
125k
      total >>= BITRES;
833
125k
      fill = (SHL32(total, 10) + sum)/tot_samples;
834
      /* Iteratively refine the fill level considering the depth min and cap. */
835
1.38M
      for (iter=0;iter<10;iter++) {
836
1.25M
         sum = 0;
837
23.3M
         for (i=start;i<tot_bands;i++)
838
22.0M
            sum += Ncoef[i] * MIN32(SHL32(cap[i], 10), MAX32(min[i], flatE[i]-fill));
839
1.25M
         fill -= (SHL32(total, 10) - sum)/tot_samples;
840
1.25M
      }
841
2.33M
      for (i=start;i<tot_bands;i++) {
842
#ifdef FIXED_POINT
843
         depth[i] = PSHR32(MIN32(SHL32(cap[i], 10), MAX32(min[i], flatE[i]-fill)), 10-2);
844
#else
845
2.20M
         depth[i] = (int)floor(.5+4*MIN32(SHL32(cap[i], 10), MAX32(min[i], flatE[i]-fill)));
846
2.20M
#endif
847
#ifdef FUZZING
848
         depth[i] = (int)-depth_std*log(1e-8+(float)rand()/(float)RAND_MAX);
849
         depth[i] = IMAX(0, IMIN(cap[i]<<2, depth[i]));
850
#endif
851
2.20M
         if (ec_tell_frac(ec) + 80 < ec->storage*8<<BITRES)
852
2.20M
            ec_enc_depth(ec, depth[i], 4*cap[i], &last);
853
0
         else
854
0
            depth[i] = 0;
855
2.20M
      }
856
125k
   } else {
857
192k
      for (i=start;i<tot_bands;i++) {
858
184k
         if (ec_tell_frac(ec) + 80 < ec->storage*8<<BITRES)
859
138k
            depth[i] = ec_dec_depth(ec, 4*cap[i], &last);
860
45.2k
         else
861
45.2k
            depth[i] = 0;
862
184k
      }
863
8.49k
   }
864
2.42M
   for (i=start;i<end;i++) {
865
2.29M
      extra_equant[i] = (depth[i]+3)>>2;
866
2.29M
      extra_pulses[i] = ((((m->eBands[i+1]-m->eBands[i])<<LM)-1)*C * depth[i] * (1<<BITRES) + 2)>>2;
867
2.29M
   }
868
134k
   if (qext_mode) {
869
103k
      for (i=0;i<qext_end;i++) {
870
93.2k
         extra_equant[end+i] = (depth[end+i]+3)>>2;
871
93.2k
         extra_pulses[end+i] = ((((qext_mode->eBands[i+1]-qext_mode->eBands[i])<<LM)-1)*C * depth[end+i] * (1<<BITRES) + 2)>>2;
872
93.2k
      }
873
10.4k
   }
874
134k
   RESTORE_STACK;
875
134k
}
876
#endif