Coverage Report

Created: 2026-07-25 07:52

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
7.80M
#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
85.3k
{
254
85.3k
   opus_int32 psum;
255
85.3k
   int lo, hi;
256
85.3k
   int i, j;
257
85.3k
   int logM;
258
85.3k
   int stereo;
259
85.3k
   int codedBands=-1;
260
85.3k
   int alloc_floor;
261
85.3k
   opus_int32 left, percoeff;
262
85.3k
   int done;
263
85.3k
   opus_int32 balance;
264
85.3k
   SAVE_STACK;
265
266
85.3k
   alloc_floor = C<<BITRES;
267
85.3k
   stereo = C>1;
268
269
85.3k
   logM = LM<<BITRES;
270
85.3k
   lo = 0;
271
85.3k
   hi = 1<<ALLOC_STEPS;
272
597k
   for (i=0;i<ALLOC_STEPS;i++)
273
512k
   {
274
512k
      int mid = (lo+hi)>>1;
275
512k
      psum = 0;
276
512k
      done = 0;
277
6.61M
      for (j=end;j-->start;)
278
6.10M
      {
279
6.10M
         int tmp = bits1[j] + (mid*(opus_int32)bits2[j]>>ALLOC_STEPS);
280
6.10M
         if (tmp >= thresh[j] || done)
281
3.56M
         {
282
3.56M
            done = 1;
283
            /* Don't allocate more than we can actually use */
284
3.56M
            psum += IMIN(tmp, cap[j]);
285
3.56M
         } else {
286
2.53M
            if (tmp >= alloc_floor)
287
251k
               psum += alloc_floor;
288
2.53M
         }
289
6.10M
      }
290
512k
      if (psum > total)
291
236k
         hi = mid;
292
275k
      else
293
275k
         lo = mid;
294
512k
   }
295
85.3k
   psum = 0;
296
   /*printf ("interp bisection gave %d\n", lo);*/
297
85.3k
   done = 0;
298
1.10M
   for (j=end;j-->start;)
299
1.01M
   {
300
1.01M
      int tmp = bits1[j] + ((opus_int32)lo*bits2[j]>>ALLOC_STEPS);
301
1.01M
      if (tmp < thresh[j] && !done)
302
500k
      {
303
500k
         if (tmp >= alloc_floor)
304
25.5k
            tmp = alloc_floor;
305
474k
         else
306
474k
            tmp = 0;
307
500k
      } else
308
516k
         done = 1;
309
      /* Don't allocate more than we can actually use */
310
1.01M
      tmp = IMIN(tmp, cap[j]);
311
1.01M
      bits[j] = tmp;
312
1.01M
      psum += tmp;
313
1.01M
   }
314
315
   /* Decide which bands to skip, working backwards from the end. */
316
585k
   for (codedBands=end;;codedBands--)
317
671k
   {
318
671k
      int band_width;
319
671k
      int band_bits;
320
671k
      int rem;
321
671k
      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
671k
      if (j<=skip_start)
329
55.8k
      {
330
         /* Give the bit we reserved to end skipping back. */
331
55.8k
         total += skip_rsv;
332
55.8k
         break;
333
55.8k
      }
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
615k
      left = total-psum;
337
615k
      percoeff = celt_udiv(left, m->eBands[codedBands]-m->eBands[start]);
338
615k
      left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
339
615k
      rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0);
340
615k
      band_width = m->eBands[codedBands]-m->eBands[j];
341
615k
      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
615k
      if (band_bits >= IMAX(thresh[j], alloc_floor+(1<<BITRES)))
346
136k
      {
347
136k
         if (encode)
348
0
         {
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
0
            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
0
            if (codedBands > 17)
356
0
               depth_threshold = j<prev ? 7 : 9;
357
0
            else
358
0
               depth_threshold = 0;
359
#ifdef FUZZING
360
            (void)signalBandwidth;
361
            (void)depth_threshold;
362
            if ((rand()&0x1) == 0)
363
#else
364
0
            if (codedBands<=start+2 || (band_bits > (depth_threshold*band_width<<LM<<BITRES)>>4 && j<=signalBandwidth))
365
0
#endif
366
0
            {
367
0
               ec_enc_bit_logp(ec, 1, 1);
368
0
               break;
369
0
            }
370
0
            ec_enc_bit_logp(ec, 0, 1);
371
136k
         } else if (ec_dec_bit_logp(ec, 1)) {
372
29.5k
            break;
373
29.5k
         }
374
         /*We used a bit to skip this band.*/
375
107k
         psum += 1<<BITRES;
376
107k
         band_bits -= 1<<BITRES;
377
107k
      }
378
      /*Reclaim the bits originally allocated to this band.*/
379
585k
      psum -= bits[j]+intensity_rsv;
380
585k
      if (intensity_rsv > 0)
381
48.1k
         intensity_rsv = LOG2_FRAC_TABLE[j-start];
382
585k
      psum += intensity_rsv;
383
585k
      if (band_bits >= alloc_floor)
384
155k
      {
385
         /*If we have enough for a fine energy bit per channel, use it.*/
386
155k
         psum += alloc_floor;
387
155k
         bits[j] = alloc_floor;
388
430k
      } else {
389
         /*Otherwise this band gets nothing at all.*/
390
430k
         bits[j] = 0;
391
430k
      }
392
585k
   }
393
394
85.3k
   celt_assert(codedBands > start);
395
   /* Code the intensity and dual stereo parameters. */
396
85.3k
   if (intensity_rsv > 0)
397
12.0k
   {
398
12.0k
      if (encode)
399
0
      {
400
0
         *intensity = IMIN(*intensity, codedBands);
401
0
         ec_enc_uint(ec, *intensity-start, codedBands+1-start);
402
0
      }
403
12.0k
      else
404
12.0k
         *intensity = start+ec_dec_uint(ec, codedBands+1-start);
405
12.0k
   }
406
73.3k
   else
407
73.3k
      *intensity = 0;
408
85.3k
   if (*intensity <= start)
409
75.8k
   {
410
75.8k
      total += dual_stereo_rsv;
411
75.8k
      dual_stereo_rsv = 0;
412
75.8k
   }
413
85.3k
   if (dual_stereo_rsv > 0)
414
9.52k
   {
415
9.52k
      if (encode)
416
0
         ec_enc_bit_logp(ec, *dual_stereo, 1);
417
9.52k
      else
418
9.52k
         *dual_stereo = ec_dec_bit_logp(ec, 1);
419
9.52k
   }
420
75.8k
   else
421
75.8k
      *dual_stereo = 0;
422
423
   /* Allocate the remaining bits */
424
85.3k
   left = total-psum;
425
85.3k
   percoeff = celt_udiv(left, m->eBands[codedBands]-m->eBands[start]);
426
85.3k
   left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
427
516k
   for (j=start;j<codedBands;j++)
428
431k
      bits[j] += ((int)percoeff*(m->eBands[j+1]-m->eBands[j]));
429
516k
   for (j=start;j<codedBands;j++)
430
431k
   {
431
431k
      int tmp = (int)IMIN(left, m->eBands[j+1]-m->eBands[j]);
432
431k
      bits[j] += tmp;
433
431k
      left -= tmp;
434
431k
   }
435
   /*for (j=0;j<end;j++)printf("%d ", bits[j]);printf("\n");*/
436
437
85.3k
   balance = 0;
438
516k
   for (j=start;j<codedBands;j++)
439
431k
   {
440
431k
      int N0, N, den;
441
431k
      int offset;
442
431k
      int NClogN;
443
431k
      opus_int32 excess, bit;
444
445
431k
      celt_assert(bits[j] >= 0);
446
431k
      N0 = m->eBands[j+1]-m->eBands[j];
447
431k
      N=N0<<LM;
448
431k
      bit = (opus_int32)bits[j]+balance;
449
450
431k
      if (N>1)
451
385k
      {
452
385k
         excess = MAX32(bit-cap[j],0);
453
385k
         bits[j] = bit-excess;
454
455
         /* Compensate for the extra DoF in stereo */
456
385k
         den=(C*N+ ((C==2 && N>2 && !*dual_stereo && j<*intensity) ? 1 : 0));
457
458
385k
         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
385k
         offset = (NClogN>>1)-den*FINE_OFFSET;
463
464
         /* N=2 is the only point that doesn't match the curve */
465
385k
         if (N==2)
466
109k
            offset += den<<BITRES>>2;
467
468
         /* Changing the offset for allocating the second and third
469
             fine energy bit */
470
385k
         if (bits[j] + offset < den*2<<BITRES)
471
255k
            offset += NClogN>>2;
472
130k
         else if (bits[j] + offset < den*3<<BITRES)
473
21.6k
            offset += NClogN>>3;
474
475
         /* Divide with rounding */
476
385k
         ebits[j] = IMAX(0, (bits[j] + offset + (den<<(BITRES-1))));
477
385k
         ebits[j] = celt_udiv(ebits[j], den)>>BITRES;
478
479
         /* Make sure not to bust */
480
385k
         if (C*ebits[j] > (bits[j]>>BITRES))
481
22.6k
            ebits[j] = bits[j] >> stereo >> BITRES;
482
483
         /* More than that is useless because that's about as far as PVQ can go */
484
385k
         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
385k
         fine_priority[j] = ebits[j]*(den<<BITRES) >= bits[j]+offset;
489
490
         /* Remove the allocated fine bits; the rest are assigned to PVQ */
491
385k
         bits[j] -= C*ebits[j]<<BITRES;
492
493
385k
      } else {
494
         /* For N=1, all bits go to fine energy except for a single sign bit */
495
46.1k
         excess = MAX32(0,bit-(C<<BITRES));
496
46.1k
         bits[j] = bit-excess;
497
46.1k
         ebits[j] = 0;
498
46.1k
         fine_priority[j] = 1;
499
46.1k
      }
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
431k
      if(excess > 0)
505
99.5k
      {
506
99.5k
         int extra_fine;
507
99.5k
         int extra_bits;
508
99.5k
         extra_fine = IMIN(excess>>(stereo+BITRES),MAX_FINE_BITS-ebits[j]);
509
99.5k
         ebits[j] += extra_fine;
510
99.5k
         extra_bits = extra_fine*C<<BITRES;
511
99.5k
         fine_priority[j] = extra_bits >= excess-balance;
512
99.5k
         excess -= extra_bits;
513
99.5k
      }
514
431k
      balance = excess;
515
516
431k
      celt_assert(bits[j] >= 0);
517
431k
      celt_assert(ebits[j] >= 0);
518
431k
   }
519
   /* Save any remaining bits over the cap for the rebalancing in
520
       quant_all_bands(). */
521
85.3k
   *_balance = balance;
522
523
   /* The skipped bands use all their bits for fine energy. */
524
671k
   for (;j<end;j++)
525
585k
   {
526
585k
      ebits[j] = bits[j] >> stereo >> BITRES;
527
585k
      celt_assert(C*ebits[j]<<BITRES == bits[j]);
528
585k
      bits[j] = 0;
529
585k
      fine_priority[j] = ebits[j]<1;
530
585k
   }
531
85.3k
   RESTORE_STACK;
532
85.3k
   return codedBands;
533
85.3k
}
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
85.3k
{
538
85.3k
   int lo, hi, len, j;
539
85.3k
   int codedBands;
540
85.3k
   int skip_start;
541
85.3k
   int skip_rsv;
542
85.3k
   int intensity_rsv;
543
85.3k
   int dual_stereo_rsv;
544
85.3k
   VARDECL(int, bits1);
545
85.3k
   VARDECL(int, bits2);
546
85.3k
   VARDECL(int, thresh);
547
85.3k
   VARDECL(int, trim_offset);
548
85.3k
   SAVE_STACK;
549
550
85.3k
   total = IMAX(total, 0);
551
85.3k
   len = m->nbEBands;
552
85.3k
   skip_start = start;
553
   /* Reserve a bit to signal the end of manually skipped bands. */
554
85.3k
   skip_rsv = total >= 1<<BITRES ? 1<<BITRES : 0;
555
85.3k
   total -= skip_rsv;
556
   /* Reserve bits for the intensity and dual stereo parameters. */
557
85.3k
   intensity_rsv = dual_stereo_rsv = 0;
558
85.3k
   if (C==2)
559
27.1k
   {
560
27.1k
      intensity_rsv = LOG2_FRAC_TABLE[end-start];
561
27.1k
      if (intensity_rsv>total)
562
15.0k
         intensity_rsv = 0;
563
12.0k
      else
564
12.0k
      {
565
12.0k
         total -= intensity_rsv;
566
12.0k
         dual_stereo_rsv = total>=1<<BITRES ? 1<<BITRES : 0;
567
12.0k
         total -= dual_stereo_rsv;
568
12.0k
      }
569
27.1k
   }
570
85.3k
   ALLOC(bits1, len, int);
571
85.3k
   ALLOC(bits2, len, int);
572
85.3k
   ALLOC(thresh, len, int);
573
85.3k
   ALLOC(trim_offset, len, int);
574
575
1.10M
   for (j=start;j<end;j++)
576
1.01M
   {
577
      /* Below this threshold, we're sure not to allocate any PVQ bits */
578
1.01M
      thresh[j] = IMAX((C)<<BITRES, (3*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>4);
579
      /* Tilt of the allocation curve */
580
1.01M
      trim_offset[j] = C*(m->eBands[j+1]-m->eBands[j])*(alloc_trim-5-LM)*(end-j-1)
581
1.01M
            *(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.01M
      if ((m->eBands[j+1]-m->eBands[j])<<LM==1)
585
116k
         trim_offset[j] -= C<<BITRES;
586
1.01M
   }
587
85.3k
   lo = 1;
588
85.3k
   hi = m->nbAllocVectors - 1;
589
85.3k
   do
590
293k
   {
591
293k
      int done = 0;
592
293k
      int psum = 0;
593
293k
      int mid = (lo+hi) >> 1;
594
3.63M
      for (j=end;j-->start;)
595
3.34M
      {
596
3.34M
         int bitsj;
597
3.34M
         int N = m->eBands[j+1]-m->eBands[j];
598
3.34M
         bitsj = C*N*m->allocVectors[mid*len+j]<<LM>>2;
599
3.34M
         if (bitsj > 0)
600
2.99M
            bitsj = IMAX(0, bitsj + trim_offset[j]);
601
3.34M
         bitsj += offsets[j];
602
3.34M
         if (bitsj >= thresh[j] || done)
603
2.92M
         {
604
2.92M
            done = 1;
605
            /* Don't allocate more than we can actually use */
606
2.92M
            psum += IMIN(bitsj, cap[j]);
607
2.92M
         } else {
608
420k
            if (bitsj >= C<<BITRES)
609
47.6k
               psum += C<<BITRES;
610
420k
         }
611
3.34M
      }
612
293k
      if (psum > total)
613
180k
         hi = mid - 1;
614
112k
      else
615
112k
         lo = mid + 1;
616
      /*printf ("lo = %d, hi = %d\n", lo, hi);*/
617
293k
   }
618
293k
   while (lo <= hi);
619
85.3k
   hi = lo--;
620
   /*printf ("interp between %d and %d\n", lo, hi);*/
621
1.10M
   for (j=start;j<end;j++)
622
1.01M
   {
623
1.01M
      int bits1j, bits2j;
624
1.01M
      int N = m->eBands[j+1]-m->eBands[j];
625
1.01M
      bits1j = C*N*m->allocVectors[lo*len+j]<<LM>>2;
626
1.01M
      bits2j = hi>=m->nbAllocVectors ?
627
905k
            cap[j] : C*N*m->allocVectors[hi*len+j]<<LM>>2;
628
1.01M
      if (bits1j > 0)
629
379k
         bits1j = IMAX(0, bits1j + trim_offset[j]);
630
1.01M
      if (bits2j > 0)
631
882k
         bits2j = IMAX(0, bits2j + trim_offset[j]);
632
1.01M
      if (lo > 0)
633
465k
         bits1j += offsets[j];
634
1.01M
      bits2j += offsets[j];
635
1.01M
      if (offsets[j]>0)
636
10.9k
         skip_start = j;
637
1.01M
      bits2j = IMAX(0,bits2j-bits1j);
638
1.01M
      bits1[j] = bits1j;
639
1.01M
      bits2[j] = bits2j;
640
1.01M
   }
641
85.3k
   codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh, cap,
642
85.3k
         total, balance, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv,
643
85.3k
         pulses, ebits, fine_priority, C, LM, ec, encode, prev, signalBandwidth);
644
85.3k
   RESTORE_STACK;
645
85.3k
   return codedBands;
646
85.3k
}
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
static void ec_enc_depth(ec_enc *enc, opus_int32 depth, opus_int32 cap, opus_int32 *last) {
654
   int sym = 3;
655
   if (depth==*last) sym = 2;
656
   if (depth==cap) sym = 1;
657
   if (depth==0) sym = 0;
658
   if (*last == 0) {
659
      ec_enc_icdf(enc, IMIN(sym, 2), last_zero, 7);
660
   } else if (*last == cap) {
661
      ec_enc_icdf(enc, IMIN(sym, 2), last_cap, 7);
662
   } else {
663
      ec_enc_icdf(enc, sym, last_other, 7);
664
   }
665
   /* We accept some redundancy if depth==last (for last different from 0 and cap). */
666
   if (sym == 3) ec_enc_uint(enc, depth-1, cap);
667
   *last = depth;
668
}
669
670
static int ec_dec_depth(ec_dec *dec, opus_int32 cap, opus_int32 *last) {
671
   int depth, sym;
672
   if (*last == 0) {
673
      sym = ec_dec_icdf(dec, last_zero, 7);
674
      if (sym==2) sym=3;
675
   } else if (*last == cap) {
676
      sym = ec_dec_icdf(dec, last_cap, 7);
677
      if (sym==2) sym=3;
678
   } else {
679
      sym = ec_dec_icdf(dec, last_other, 7);
680
   }
681
   if (sym==0) depth=0;
682
   else if (sym==1) depth=cap;
683
   else if (sym==2) depth=*last;
684
   else depth = 1 + ec_dec_uint(dec, cap);
685
   *last = depth;
686
   return depth;
687
}
688
689
#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
{
692
   opus_val16 t0, t1, t2, t3, t4;
693
   t2 = x[2];
694
   if (x[0] > x[1])
695
   {
696
      t0 = x[1];
697
      t1 = x[0];
698
   } else {
699
      t0 = x[0];
700
      t1 = x[1];
701
   }
702
   if (x[3] > x[4])
703
   {
704
      t3 = x[4];
705
      t4 = x[3];
706
   } else {
707
      t3 = x[3];
708
      t4 = x[4];
709
   }
710
   if (t0 > t3)
711
   {
712
      MSWAP16(t0, t3);
713
      MSWAP16(t1, t4);
714
   }
715
   if (t2 > t1)
716
   {
717
      if (t1 < t3)
718
         return MIN16(t2, t3);
719
      else
720
         return MIN16(t4, t1);
721
   } else {
722
      if (t2 < t3)
723
         return MIN16(t1, t3);
724
      else
725
         return MIN16(t2, t4);
726
   }
727
}
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
{
732
   int i;
733
   opus_int32 last=0;
734
   opus_val32 sum;
735
   opus_val32 fill;
736
   int iter;
737
   int tot_bands;
738
   int tot_samples;
739
   VARDECL(int, depth);
740
   VARDECL(opus_int32, cap);
741
#ifdef FUZZING
742
   float depth_std;
743
#endif
744
   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
   if (qext_mode != NULL) {
750
      celt_assert(end==m->nbEBands);
751
      tot_bands = end + qext_end;
752
      tot_samples = (qext_mode->eBands[qext_end]-m->eBands[start])*C<<LM;
753
   } else {
754
      tot_bands = end;
755
      tot_samples = (m->eBands[end]-m->eBands[start])*C<<LM;
756
   }
757
   ALLOC(cap, tot_bands, opus_int32);
758
   for (i=start;i<end;i++) cap[i] = 14;
759
   if (qext_mode != NULL) {
760
      for (i=0;i<qext_end;i++) cap[end+i] = 14;
761
   }
762
   if (total <= 0) {
763
      for (i=start;i<m->nbEBands+qext_end;i++) {
764
         extra_pulses[i] = extra_equant[i] = 0;
765
      }
766
      RESTORE_STACK;
767
      return;
768
   }
769
   ALLOC(depth, tot_bands, int);
770
   if (encode) {
771
      VARDECL(opus_val16, flatE);
772
      VARDECL(int, Ncoef);
773
      VARDECL(opus_val16, min);
774
      VARDECL(opus_val16, follower);
775
      VARDECL(opus_val16, dyn_cap);
776
777
      ALLOC(flatE, tot_bands, opus_val16);
778
      ALLOC(min, tot_bands, opus_val16);
779
      ALLOC(Ncoef, tot_bands, int);
780
      for (i=start;i<end;i++) {
781
         Ncoef[i] = (m->eBands[i+1]-m->eBands[i])*C<<LM;
782
      }
783
      /* Remove the effect of band width, eMeans and pre-emphasis to compute the real (flat) spectrum. */
784
      for (i=start;i<end;i++) {
785
         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);
786
         min[i] = 0;
787
      }
788
      if (C==2) {
789
         for (i=start;i<end;i++) {
790
            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));
791
         }
792
      }
793
      if (qext_mode != NULL) {
794
         opus_val16 min_depth = 0;
795
         /* If we have enough bits, give at least 1 bit of depth to all higher bands up to 40 kHz. */
796
         if (total >= 3*C*(qext_mode->eBands[qext_end]-qext_mode->eBands[0])<<LM<<BITRES && (toneishness < QCONST32(.98f, 29) || tone_freq > QCONST16(1.33f, 13)))
797
            min_depth = QCONST16(1.f, 10);
798
         for (i=0;i<qext_end;i++) {
799
            Ncoef[end+i] = (qext_mode->eBands[i+1]-qext_mode->eBands[i])*C<<LM;
800
            min[end+i] = min_depth;
801
         }
802
         for (i=0;i<qext_end;i++) {
803
            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
         }
805
         if (C==2) {
806
            for (i=0;i<qext_end;i++) {
807
               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
            }
809
         }
810
      }
811
      ALLOC(follower, tot_bands, opus_val16);
812
      if (tot_bands - start >= 5) {
813
         for (i=start+2;i<tot_bands-2;i++) {
814
            follower[i] = median_of_5_val16(&flatE[i-2]);
815
         }
816
         follower[start] = follower[start+1] = follower[start+2];
817
         follower[tot_bands-1] = follower[tot_bands-2] = follower[tot_bands-3];
818
      } else {
819
         for (i=start;i<tot_bands;i++) follower[i] = flatE[i];
820
      }
821
      for (i=start+1;i<tot_bands;i++) {
822
         follower[i] = MAX16(follower[i], follower[i-1]-QCONST16(1.f, 10));
823
      }
824
      for (i=tot_bands-2;i>=start;i--) {
825
         follower[i] = MAX16(follower[i], follower[i+1]-QCONST16(1.f, 10));
826
      }
827
      if (qext_mode != NULL) {
828
         for (i=0;i<qext_end;i++) flatE[end+i] = flatE[end+i] + QCONST16(4.f, 10) + QCONST16(.3f, 10)*i;
829
         for (i=0;i<qext_end;i++) follower[end+i] = follower[end+i] + QCONST16(5.f, 10) + QCONST16(.6f, 10)*i;
830
      }
831
      flatE[end-4] += QCONST16(.25f, 10);
832
      flatE[end-3] += QCONST16(.5f, 10);
833
      flatE[end-2] += QCONST16(1.2f, 10);
834
      flatE[end-1] += QCONST16(2.f, 10);
835
      follower[end-4] += QCONST16(.25f, 10);
836
      follower[end-3] += QCONST16(.5f, 10);
837
      follower[end-2] += QCONST16(1.2f, 10);
838
      follower[end-1] += QCONST16(2.f, 10);
839
      ALLOC(dyn_cap, tot_bands, opus_val16);
840
      /* It's not really worth exceeding this "dynamic cap" that corresponds to about 20-bit
841
         resolution unless we have the bits to do so in all of the bands.*/
842
      for (i=0;i<tot_bands;i++) dyn_cap[i] = MAX32(0, MIN32(flatE[i]+QCONST16(9.f, 10), SHL32(cap[i], 10)));
843
      sum = 0;
844
      for (i=start;i<tot_bands;i++) {
845
         sum += MULT16_16(Ncoef[i], dyn_cap[i]);
846
      }
847
      total >>= BITRES;
848
      if (sum <= SHL32(total, 10)) {
849
         int dyn_tot_samples=0;
850
         opus_val32 overfill;
851
         for (i=start;i<tot_bands;i++) {
852
            if (dyn_cap[i] > 0) dyn_tot_samples += Ncoef[i];
853
         }
854
         dyn_tot_samples = IMAX(dyn_tot_samples, 1);
855
         overfill = (SHL32(total, 10) - sum)/dyn_tot_samples;
856
857
         for (i=start;i<tot_bands;i++) {
858
            if (dyn_cap[i] > 0) dyn_cap[i] = MIN32(SHL32(cap[i], 10), dyn_cap[i]+overfill);
859
         }
860
861
         for (i=start;i<tot_bands;i++) {
862
#ifdef FIXED_POINT
863
            depth[i] = PSHR32(dyn_cap[i], 10-2);
864
#else
865
            depth[i] = (int)floor(.5+4*dyn_cap[i]);
866
#endif
867
            if (ec_tell_frac(ec) + 80 < ec->storage*8<<BITRES)
868
               ec_enc_depth(ec, depth[i], 4*cap[i], &last);
869
            else
870
               depth[i] = 0;
871
         }
872
      } else {
873
         for (i=start;i<tot_bands;i++) flatE[i] -= MULT16_16_Q15(Q15ONE-PSHR32(toneishness, 14), follower[i]);
874
         /* Approximate fill level assuming all bands contribute fully. */
875
         sum = 0;
876
         for (i=start;i<tot_bands;i++) {
877
            sum += MULT16_16(Ncoef[i], flatE[i]);
878
         }
879
         fill = (SHL32(total, 10) + sum)/tot_samples;
880
         /* Iteratively refine the fill level considering the depth min and cap. */
881
         for (iter=0;iter<20;iter++) {
882
            sum = 0;
883
            for (i=start;i<tot_bands;i++)
884
               sum += Ncoef[i] * MIN32(dyn_cap[i], MAX32(min[i], flatE[i]-fill));
885
            fill -= (SHL32(total, 10) - sum)/tot_samples;
886
         }
887
         for (i=start;i<tot_bands;i++) {
888
#ifdef FIXED_POINT
889
            depth[i] = PSHR32(MIN32(dyn_cap[i], MAX32(min[i], flatE[i]-fill)), 10-2);
890
#else
891
            depth[i] = (int)floor(.5+4*MIN32(dyn_cap[i], MAX32(min[i], flatE[i]-fill)));
892
#endif
893
#ifdef FUZZING
894
            depth[i] = (int)-depth_std*log(1e-8+(float)rand()/(float)RAND_MAX);
895
            depth[i] = IMAX(0, IMIN(cap[i]<<2, depth[i]));
896
#endif
897
            if (ec_tell_frac(ec) + 80 < ec->storage*8<<BITRES)
898
               ec_enc_depth(ec, depth[i], 4*cap[i], &last);
899
            else
900
               depth[i] = 0;
901
         }
902
      }
903
   } else {
904
      for (i=start;i<tot_bands;i++) {
905
         if (ec_tell_frac(ec) + 80 < ec->storage*8<<BITRES)
906
            depth[i] = ec_dec_depth(ec, 4*cap[i], &last);
907
         else
908
            depth[i] = 0;
909
      }
910
   }
911
   for (i=start;i<end;i++) {
912
      extra_equant[i] = (depth[i]+3)>>2;
913
      extra_pulses[i] = ((((m->eBands[i+1]-m->eBands[i])<<LM)-1)*C * depth[i] * (1<<BITRES) + 2)>>2;
914
   }
915
   if (qext_mode) {
916
      for (i=0;i<qext_end;i++) {
917
         extra_equant[end+i] = (depth[end+i]+3)>>2;
918
         extra_pulses[end+i] = ((((qext_mode->eBands[i+1]-qext_mode->eBands[i])<<LM)-1)*C * depth[end+i] * (1<<BITRES) + 2)>>2;
919
      }
920
   }
921
   RESTORE_STACK;
922
}
923
#endif