annotate src/opus-1.3/celt/rate.c @ 81:7029a4916348

Merge build update
author Chris Cannam
date Thu, 31 Oct 2019 13:36:58 +0000
parents 7aeed7906520
children
rev   line source
Chris@69 1 /* Copyright (c) 2007-2008 CSIRO
Chris@69 2 Copyright (c) 2007-2009 Xiph.Org Foundation
Chris@69 3 Written by Jean-Marc Valin */
Chris@69 4 /*
Chris@69 5 Redistribution and use in source and binary forms, with or without
Chris@69 6 modification, are permitted provided that the following conditions
Chris@69 7 are met:
Chris@69 8
Chris@69 9 - Redistributions of source code must retain the above copyright
Chris@69 10 notice, this list of conditions and the following disclaimer.
Chris@69 11
Chris@69 12 - Redistributions in binary form must reproduce the above copyright
Chris@69 13 notice, this list of conditions and the following disclaimer in the
Chris@69 14 documentation and/or other materials provided with the distribution.
Chris@69 15
Chris@69 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@69 17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@69 18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@69 19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
Chris@69 20 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Chris@69 21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Chris@69 22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Chris@69 23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Chris@69 24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Chris@69 25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Chris@69 26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@69 27 */
Chris@69 28
Chris@69 29 #ifdef HAVE_CONFIG_H
Chris@69 30 #include "config.h"
Chris@69 31 #endif
Chris@69 32
Chris@69 33 #include <math.h>
Chris@69 34 #include "modes.h"
Chris@69 35 #include "cwrs.h"
Chris@69 36 #include "arch.h"
Chris@69 37 #include "os_support.h"
Chris@69 38
Chris@69 39 #include "entcode.h"
Chris@69 40 #include "rate.h"
Chris@69 41
Chris@69 42 static const unsigned char LOG2_FRAC_TABLE[24]={
Chris@69 43 0,
Chris@69 44 8,13,
Chris@69 45 16,19,21,23,
Chris@69 46 24,26,27,28,29,30,31,32,
Chris@69 47 32,33,34,34,35,36,36,37,37
Chris@69 48 };
Chris@69 49
Chris@69 50 #ifdef CUSTOM_MODES
Chris@69 51
Chris@69 52 /*Determines if V(N,K) fits in a 32-bit unsigned integer.
Chris@69 53 N and K are themselves limited to 15 bits.*/
Chris@69 54 static int fits_in32(int _n, int _k)
Chris@69 55 {
Chris@69 56 static const opus_int16 maxN[15] = {
Chris@69 57 32767, 32767, 32767, 1476, 283, 109, 60, 40,
Chris@69 58 29, 24, 20, 18, 16, 14, 13};
Chris@69 59 static const opus_int16 maxK[15] = {
Chris@69 60 32767, 32767, 32767, 32767, 1172, 238, 95, 53,
Chris@69 61 36, 27, 22, 18, 16, 15, 13};
Chris@69 62 if (_n>=14)
Chris@69 63 {
Chris@69 64 if (_k>=14)
Chris@69 65 return 0;
Chris@69 66 else
Chris@69 67 return _n <= maxN[_k];
Chris@69 68 } else {
Chris@69 69 return _k <= maxK[_n];
Chris@69 70 }
Chris@69 71 }
Chris@69 72
Chris@69 73 void compute_pulse_cache(CELTMode *m, int LM)
Chris@69 74 {
Chris@69 75 int C;
Chris@69 76 int i;
Chris@69 77 int j;
Chris@69 78 int curr=0;
Chris@69 79 int nbEntries=0;
Chris@69 80 int entryN[100], entryK[100], entryI[100];
Chris@69 81 const opus_int16 *eBands = m->eBands;
Chris@69 82 PulseCache *cache = &m->cache;
Chris@69 83 opus_int16 *cindex;
Chris@69 84 unsigned char *bits;
Chris@69 85 unsigned char *cap;
Chris@69 86
Chris@69 87 cindex = (opus_int16 *)opus_alloc(sizeof(cache->index[0])*m->nbEBands*(LM+2));
Chris@69 88 cache->index = cindex;
Chris@69 89
Chris@69 90 /* Scan for all unique band sizes */
Chris@69 91 for (i=0;i<=LM+1;i++)
Chris@69 92 {
Chris@69 93 for (j=0;j<m->nbEBands;j++)
Chris@69 94 {
Chris@69 95 int k;
Chris@69 96 int N = (eBands[j+1]-eBands[j])<<i>>1;
Chris@69 97 cindex[i*m->nbEBands+j] = -1;
Chris@69 98 /* Find other bands that have the same size */
Chris@69 99 for (k=0;k<=i;k++)
Chris@69 100 {
Chris@69 101 int n;
Chris@69 102 for (n=0;n<m->nbEBands && (k!=i || n<j);n++)
Chris@69 103 {
Chris@69 104 if (N == (eBands[n+1]-eBands[n])<<k>>1)
Chris@69 105 {
Chris@69 106 cindex[i*m->nbEBands+j] = cindex[k*m->nbEBands+n];
Chris@69 107 break;
Chris@69 108 }
Chris@69 109 }
Chris@69 110 }
Chris@69 111 if (cache->index[i*m->nbEBands+j] == -1 && N!=0)
Chris@69 112 {
Chris@69 113 int K;
Chris@69 114 entryN[nbEntries] = N;
Chris@69 115 K = 0;
Chris@69 116 while (fits_in32(N,get_pulses(K+1)) && K<MAX_PSEUDO)
Chris@69 117 K++;
Chris@69 118 entryK[nbEntries] = K;
Chris@69 119 cindex[i*m->nbEBands+j] = curr;
Chris@69 120 entryI[nbEntries] = curr;
Chris@69 121
Chris@69 122 curr += K+1;
Chris@69 123 nbEntries++;
Chris@69 124 }
Chris@69 125 }
Chris@69 126 }
Chris@69 127 bits = (unsigned char *)opus_alloc(sizeof(unsigned char)*curr);
Chris@69 128 cache->bits = bits;
Chris@69 129 cache->size = curr;
Chris@69 130 /* Compute the cache for all unique sizes */
Chris@69 131 for (i=0;i<nbEntries;i++)
Chris@69 132 {
Chris@69 133 unsigned char *ptr = bits+entryI[i];
Chris@69 134 opus_int16 tmp[CELT_MAX_PULSES+1];
Chris@69 135 get_required_bits(tmp, entryN[i], get_pulses(entryK[i]), BITRES);
Chris@69 136 for (j=1;j<=entryK[i];j++)
Chris@69 137 ptr[j] = tmp[get_pulses(j)]-1;
Chris@69 138 ptr[0] = entryK[i];
Chris@69 139 }
Chris@69 140
Chris@69 141 /* Compute the maximum rate for each band at which we'll reliably use as
Chris@69 142 many bits as we ask for. */
Chris@69 143 cache->caps = cap = (unsigned char *)opus_alloc(sizeof(cache->caps[0])*(LM+1)*2*m->nbEBands);
Chris@69 144 for (i=0;i<=LM;i++)
Chris@69 145 {
Chris@69 146 for (C=1;C<=2;C++)
Chris@69 147 {
Chris@69 148 for (j=0;j<m->nbEBands;j++)
Chris@69 149 {
Chris@69 150 int N0;
Chris@69 151 int max_bits;
Chris@69 152 N0 = m->eBands[j+1]-m->eBands[j];
Chris@69 153 /* N=1 bands only have a sign bit and fine bits. */
Chris@69 154 if (N0<<i == 1)
Chris@69 155 max_bits = C*(1+MAX_FINE_BITS)<<BITRES;
Chris@69 156 else
Chris@69 157 {
Chris@69 158 const unsigned char *pcache;
Chris@69 159 opus_int32 num;
Chris@69 160 opus_int32 den;
Chris@69 161 int LM0;
Chris@69 162 int N;
Chris@69 163 int offset;
Chris@69 164 int ndof;
Chris@69 165 int qb;
Chris@69 166 int k;
Chris@69 167 LM0 = 0;
Chris@69 168 /* Even-sized bands bigger than N=2 can be split one more time.
Chris@69 169 As of commit 44203907 all bands >1 are even, including custom modes.*/
Chris@69 170 if (N0 > 2)
Chris@69 171 {
Chris@69 172 N0>>=1;
Chris@69 173 LM0--;
Chris@69 174 }
Chris@69 175 /* N0=1 bands can't be split down to N<2. */
Chris@69 176 else if (N0 <= 1)
Chris@69 177 {
Chris@69 178 LM0=IMIN(i,1);
Chris@69 179 N0<<=LM0;
Chris@69 180 }
Chris@69 181 /* Compute the cost for the lowest-level PVQ of a fully split
Chris@69 182 band. */
Chris@69 183 pcache = bits + cindex[(LM0+1)*m->nbEBands+j];
Chris@69 184 max_bits = pcache[pcache[0]]+1;
Chris@69 185 /* Add in the cost of coding regular splits. */
Chris@69 186 N = N0;
Chris@69 187 for(k=0;k<i-LM0;k++){
Chris@69 188 max_bits <<= 1;
Chris@69 189 /* Offset the number of qtheta bits by log2(N)/2
Chris@69 190 + QTHETA_OFFSET compared to their "fair share" of
Chris@69 191 total/N */
Chris@69 192 offset = ((m->logN[j]+((LM0+k)<<BITRES))>>1)-QTHETA_OFFSET;
Chris@69 193 /* The number of qtheta bits we'll allocate if the remainder
Chris@69 194 is to be max_bits.
Chris@69 195 The average measured cost for theta is 0.89701 times qb,
Chris@69 196 approximated here as 459/512. */
Chris@69 197 num=459*(opus_int32)((2*N-1)*offset+max_bits);
Chris@69 198 den=((opus_int32)(2*N-1)<<9)-459;
Chris@69 199 qb = IMIN((num+(den>>1))/den, 57);
Chris@69 200 celt_assert(qb >= 0);
Chris@69 201 max_bits += qb;
Chris@69 202 N <<= 1;
Chris@69 203 }
Chris@69 204 /* Add in the cost of a stereo split, if necessary. */
Chris@69 205 if (C==2)
Chris@69 206 {
Chris@69 207 max_bits <<= 1;
Chris@69 208 offset = ((m->logN[j]+(i<<BITRES))>>1)-(N==2?QTHETA_OFFSET_TWOPHASE:QTHETA_OFFSET);
Chris@69 209 ndof = 2*N-1-(N==2);
Chris@69 210 /* The average measured cost for theta with the step PDF is
Chris@69 211 0.95164 times qb, approximated here as 487/512. */
Chris@69 212 num = (N==2?512:487)*(opus_int32)(max_bits+ndof*offset);
Chris@69 213 den = ((opus_int32)ndof<<9)-(N==2?512:487);
Chris@69 214 qb = IMIN((num+(den>>1))/den, (N==2?64:61));
Chris@69 215 celt_assert(qb >= 0);
Chris@69 216 max_bits += qb;
Chris@69 217 }
Chris@69 218 /* Add the fine bits we'll use. */
Chris@69 219 /* Compensate for the extra DoF in stereo */
Chris@69 220 ndof = C*N + ((C==2 && N>2) ? 1 : 0);
Chris@69 221 /* Offset the number of fine bits by log2(N)/2 + FINE_OFFSET
Chris@69 222 compared to their "fair share" of total/N */
Chris@69 223 offset = ((m->logN[j] + (i<<BITRES))>>1)-FINE_OFFSET;
Chris@69 224 /* N=2 is the only point that doesn't match the curve */
Chris@69 225 if (N==2)
Chris@69 226 offset += 1<<BITRES>>2;
Chris@69 227 /* The number of fine bits we'll allocate if the remainder is
Chris@69 228 to be max_bits. */
Chris@69 229 num = max_bits+ndof*offset;
Chris@69 230 den = (ndof-1)<<BITRES;
Chris@69 231 qb = IMIN((num+(den>>1))/den, MAX_FINE_BITS);
Chris@69 232 celt_assert(qb >= 0);
Chris@69 233 max_bits += C*qb<<BITRES;
Chris@69 234 }
Chris@69 235 max_bits = (4*max_bits/(C*((m->eBands[j+1]-m->eBands[j])<<i)))-64;
Chris@69 236 celt_assert(max_bits >= 0);
Chris@69 237 celt_assert(max_bits < 256);
Chris@69 238 *cap++ = (unsigned char)max_bits;
Chris@69 239 }
Chris@69 240 }
Chris@69 241 }
Chris@69 242 }
Chris@69 243
Chris@69 244 #endif /* CUSTOM_MODES */
Chris@69 245
Chris@69 246 #define ALLOC_STEPS 6
Chris@69 247
Chris@69 248 static OPUS_INLINE int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start,
Chris@69 249 const int *bits1, const int *bits2, const int *thresh, const int *cap, opus_int32 total, opus_int32 *_balance,
Chris@69 250 int skip_rsv, int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits,
Chris@69 251 int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev, int signalBandwidth)
Chris@69 252 {
Chris@69 253 opus_int32 psum;
Chris@69 254 int lo, hi;
Chris@69 255 int i, j;
Chris@69 256 int logM;
Chris@69 257 int stereo;
Chris@69 258 int codedBands=-1;
Chris@69 259 int alloc_floor;
Chris@69 260 opus_int32 left, percoeff;
Chris@69 261 int done;
Chris@69 262 opus_int32 balance;
Chris@69 263 SAVE_STACK;
Chris@69 264
Chris@69 265 alloc_floor = C<<BITRES;
Chris@69 266 stereo = C>1;
Chris@69 267
Chris@69 268 logM = LM<<BITRES;
Chris@69 269 lo = 0;
Chris@69 270 hi = 1<<ALLOC_STEPS;
Chris@69 271 for (i=0;i<ALLOC_STEPS;i++)
Chris@69 272 {
Chris@69 273 int mid = (lo+hi)>>1;
Chris@69 274 psum = 0;
Chris@69 275 done = 0;
Chris@69 276 for (j=end;j-->start;)
Chris@69 277 {
Chris@69 278 int tmp = bits1[j] + (mid*(opus_int32)bits2[j]>>ALLOC_STEPS);
Chris@69 279 if (tmp >= thresh[j] || done)
Chris@69 280 {
Chris@69 281 done = 1;
Chris@69 282 /* Don't allocate more than we can actually use */
Chris@69 283 psum += IMIN(tmp, cap[j]);
Chris@69 284 } else {
Chris@69 285 if (tmp >= alloc_floor)
Chris@69 286 psum += alloc_floor;
Chris@69 287 }
Chris@69 288 }
Chris@69 289 if (psum > total)
Chris@69 290 hi = mid;
Chris@69 291 else
Chris@69 292 lo = mid;
Chris@69 293 }
Chris@69 294 psum = 0;
Chris@69 295 /*printf ("interp bisection gave %d\n", lo);*/
Chris@69 296 done = 0;
Chris@69 297 for (j=end;j-->start;)
Chris@69 298 {
Chris@69 299 int tmp = bits1[j] + ((opus_int32)lo*bits2[j]>>ALLOC_STEPS);
Chris@69 300 if (tmp < thresh[j] && !done)
Chris@69 301 {
Chris@69 302 if (tmp >= alloc_floor)
Chris@69 303 tmp = alloc_floor;
Chris@69 304 else
Chris@69 305 tmp = 0;
Chris@69 306 } else
Chris@69 307 done = 1;
Chris@69 308 /* Don't allocate more than we can actually use */
Chris@69 309 tmp = IMIN(tmp, cap[j]);
Chris@69 310 bits[j] = tmp;
Chris@69 311 psum += tmp;
Chris@69 312 }
Chris@69 313
Chris@69 314 /* Decide which bands to skip, working backwards from the end. */
Chris@69 315 for (codedBands=end;;codedBands--)
Chris@69 316 {
Chris@69 317 int band_width;
Chris@69 318 int band_bits;
Chris@69 319 int rem;
Chris@69 320 j = codedBands-1;
Chris@69 321 /* Never skip the first band, nor a band that has been boosted by
Chris@69 322 dynalloc.
Chris@69 323 In the first case, we'd be coding a bit to signal we're going to waste
Chris@69 324 all the other bits.
Chris@69 325 In the second case, we'd be coding a bit to redistribute all the bits
Chris@69 326 we just signaled should be cocentrated in this band. */
Chris@69 327 if (j<=skip_start)
Chris@69 328 {
Chris@69 329 /* Give the bit we reserved to end skipping back. */
Chris@69 330 total += skip_rsv;
Chris@69 331 break;
Chris@69 332 }
Chris@69 333 /*Figure out how many left-over bits we would be adding to this band.
Chris@69 334 This can include bits we've stolen back from higher, skipped bands.*/
Chris@69 335 left = total-psum;
Chris@69 336 percoeff = celt_udiv(left, m->eBands[codedBands]-m->eBands[start]);
Chris@69 337 left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
Chris@69 338 rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0);
Chris@69 339 band_width = m->eBands[codedBands]-m->eBands[j];
Chris@69 340 band_bits = (int)(bits[j] + percoeff*band_width + rem);
Chris@69 341 /*Only code a skip decision if we're above the threshold for this band.
Chris@69 342 Otherwise it is force-skipped.
Chris@69 343 This ensures that we have enough bits to code the skip flag.*/
Chris@69 344 if (band_bits >= IMAX(thresh[j], alloc_floor+(1<<BITRES)))
Chris@69 345 {
Chris@69 346 if (encode)
Chris@69 347 {
Chris@69 348 /*This if() block is the only part of the allocation function that
Chris@69 349 is not a mandatory part of the bitstream: any bands we choose to
Chris@69 350 skip here must be explicitly signaled.*/
Chris@69 351 int depth_threshold;
Chris@69 352 /*We choose a threshold with some hysteresis to keep bands from
Chris@69 353 fluctuating in and out, but we try not to fold below a certain point. */
Chris@69 354 if (codedBands > 17)
Chris@69 355 depth_threshold = j<prev ? 7 : 9;
Chris@69 356 else
Chris@69 357 depth_threshold = 0;
Chris@69 358 #ifdef FUZZING
Chris@69 359 if ((rand()&0x1) == 0)
Chris@69 360 #else
Chris@69 361 if (codedBands<=start+2 || (band_bits > (depth_threshold*band_width<<LM<<BITRES)>>4 && j<=signalBandwidth))
Chris@69 362 #endif
Chris@69 363 {
Chris@69 364 ec_enc_bit_logp(ec, 1, 1);
Chris@69 365 break;
Chris@69 366 }
Chris@69 367 ec_enc_bit_logp(ec, 0, 1);
Chris@69 368 } else if (ec_dec_bit_logp(ec, 1)) {
Chris@69 369 break;
Chris@69 370 }
Chris@69 371 /*We used a bit to skip this band.*/
Chris@69 372 psum += 1<<BITRES;
Chris@69 373 band_bits -= 1<<BITRES;
Chris@69 374 }
Chris@69 375 /*Reclaim the bits originally allocated to this band.*/
Chris@69 376 psum -= bits[j]+intensity_rsv;
Chris@69 377 if (intensity_rsv > 0)
Chris@69 378 intensity_rsv = LOG2_FRAC_TABLE[j-start];
Chris@69 379 psum += intensity_rsv;
Chris@69 380 if (band_bits >= alloc_floor)
Chris@69 381 {
Chris@69 382 /*If we have enough for a fine energy bit per channel, use it.*/
Chris@69 383 psum += alloc_floor;
Chris@69 384 bits[j] = alloc_floor;
Chris@69 385 } else {
Chris@69 386 /*Otherwise this band gets nothing at all.*/
Chris@69 387 bits[j] = 0;
Chris@69 388 }
Chris@69 389 }
Chris@69 390
Chris@69 391 celt_assert(codedBands > start);
Chris@69 392 /* Code the intensity and dual stereo parameters. */
Chris@69 393 if (intensity_rsv > 0)
Chris@69 394 {
Chris@69 395 if (encode)
Chris@69 396 {
Chris@69 397 *intensity = IMIN(*intensity, codedBands);
Chris@69 398 ec_enc_uint(ec, *intensity-start, codedBands+1-start);
Chris@69 399 }
Chris@69 400 else
Chris@69 401 *intensity = start+ec_dec_uint(ec, codedBands+1-start);
Chris@69 402 }
Chris@69 403 else
Chris@69 404 *intensity = 0;
Chris@69 405 if (*intensity <= start)
Chris@69 406 {
Chris@69 407 total += dual_stereo_rsv;
Chris@69 408 dual_stereo_rsv = 0;
Chris@69 409 }
Chris@69 410 if (dual_stereo_rsv > 0)
Chris@69 411 {
Chris@69 412 if (encode)
Chris@69 413 ec_enc_bit_logp(ec, *dual_stereo, 1);
Chris@69 414 else
Chris@69 415 *dual_stereo = ec_dec_bit_logp(ec, 1);
Chris@69 416 }
Chris@69 417 else
Chris@69 418 *dual_stereo = 0;
Chris@69 419
Chris@69 420 /* Allocate the remaining bits */
Chris@69 421 left = total-psum;
Chris@69 422 percoeff = celt_udiv(left, m->eBands[codedBands]-m->eBands[start]);
Chris@69 423 left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
Chris@69 424 for (j=start;j<codedBands;j++)
Chris@69 425 bits[j] += ((int)percoeff*(m->eBands[j+1]-m->eBands[j]));
Chris@69 426 for (j=start;j<codedBands;j++)
Chris@69 427 {
Chris@69 428 int tmp = (int)IMIN(left, m->eBands[j+1]-m->eBands[j]);
Chris@69 429 bits[j] += tmp;
Chris@69 430 left -= tmp;
Chris@69 431 }
Chris@69 432 /*for (j=0;j<end;j++)printf("%d ", bits[j]);printf("\n");*/
Chris@69 433
Chris@69 434 balance = 0;
Chris@69 435 for (j=start;j<codedBands;j++)
Chris@69 436 {
Chris@69 437 int N0, N, den;
Chris@69 438 int offset;
Chris@69 439 int NClogN;
Chris@69 440 opus_int32 excess, bit;
Chris@69 441
Chris@69 442 celt_assert(bits[j] >= 0);
Chris@69 443 N0 = m->eBands[j+1]-m->eBands[j];
Chris@69 444 N=N0<<LM;
Chris@69 445 bit = (opus_int32)bits[j]+balance;
Chris@69 446
Chris@69 447 if (N>1)
Chris@69 448 {
Chris@69 449 excess = MAX32(bit-cap[j],0);
Chris@69 450 bits[j] = bit-excess;
Chris@69 451
Chris@69 452 /* Compensate for the extra DoF in stereo */
Chris@69 453 den=(C*N+ ((C==2 && N>2 && !*dual_stereo && j<*intensity) ? 1 : 0));
Chris@69 454
Chris@69 455 NClogN = den*(m->logN[j] + logM);
Chris@69 456
Chris@69 457 /* Offset for the number of fine bits by log2(N)/2 + FINE_OFFSET
Chris@69 458 compared to their "fair share" of total/N */
Chris@69 459 offset = (NClogN>>1)-den*FINE_OFFSET;
Chris@69 460
Chris@69 461 /* N=2 is the only point that doesn't match the curve */
Chris@69 462 if (N==2)
Chris@69 463 offset += den<<BITRES>>2;
Chris@69 464
Chris@69 465 /* Changing the offset for allocating the second and third
Chris@69 466 fine energy bit */
Chris@69 467 if (bits[j] + offset < den*2<<BITRES)
Chris@69 468 offset += NClogN>>2;
Chris@69 469 else if (bits[j] + offset < den*3<<BITRES)
Chris@69 470 offset += NClogN>>3;
Chris@69 471
Chris@69 472 /* Divide with rounding */
Chris@69 473 ebits[j] = IMAX(0, (bits[j] + offset + (den<<(BITRES-1))));
Chris@69 474 ebits[j] = celt_udiv(ebits[j], den)>>BITRES;
Chris@69 475
Chris@69 476 /* Make sure not to bust */
Chris@69 477 if (C*ebits[j] > (bits[j]>>BITRES))
Chris@69 478 ebits[j] = bits[j] >> stereo >> BITRES;
Chris@69 479
Chris@69 480 /* More than that is useless because that's about as far as PVQ can go */
Chris@69 481 ebits[j] = IMIN(ebits[j], MAX_FINE_BITS);
Chris@69 482
Chris@69 483 /* If we rounded down or capped this band, make it a candidate for the
Chris@69 484 final fine energy pass */
Chris@69 485 fine_priority[j] = ebits[j]*(den<<BITRES) >= bits[j]+offset;
Chris@69 486
Chris@69 487 /* Remove the allocated fine bits; the rest are assigned to PVQ */
Chris@69 488 bits[j] -= C*ebits[j]<<BITRES;
Chris@69 489
Chris@69 490 } else {
Chris@69 491 /* For N=1, all bits go to fine energy except for a single sign bit */
Chris@69 492 excess = MAX32(0,bit-(C<<BITRES));
Chris@69 493 bits[j] = bit-excess;
Chris@69 494 ebits[j] = 0;
Chris@69 495 fine_priority[j] = 1;
Chris@69 496 }
Chris@69 497
Chris@69 498 /* Fine energy can't take advantage of the re-balancing in
Chris@69 499 quant_all_bands().
Chris@69 500 Instead, do the re-balancing here.*/
Chris@69 501 if(excess > 0)
Chris@69 502 {
Chris@69 503 int extra_fine;
Chris@69 504 int extra_bits;
Chris@69 505 extra_fine = IMIN(excess>>(stereo+BITRES),MAX_FINE_BITS-ebits[j]);
Chris@69 506 ebits[j] += extra_fine;
Chris@69 507 extra_bits = extra_fine*C<<BITRES;
Chris@69 508 fine_priority[j] = extra_bits >= excess-balance;
Chris@69 509 excess -= extra_bits;
Chris@69 510 }
Chris@69 511 balance = excess;
Chris@69 512
Chris@69 513 celt_assert(bits[j] >= 0);
Chris@69 514 celt_assert(ebits[j] >= 0);
Chris@69 515 }
Chris@69 516 /* Save any remaining bits over the cap for the rebalancing in
Chris@69 517 quant_all_bands(). */
Chris@69 518 *_balance = balance;
Chris@69 519
Chris@69 520 /* The skipped bands use all their bits for fine energy. */
Chris@69 521 for (;j<end;j++)
Chris@69 522 {
Chris@69 523 ebits[j] = bits[j] >> stereo >> BITRES;
Chris@69 524 celt_assert(C*ebits[j]<<BITRES == bits[j]);
Chris@69 525 bits[j] = 0;
Chris@69 526 fine_priority[j] = ebits[j]<1;
Chris@69 527 }
Chris@69 528 RESTORE_STACK;
Chris@69 529 return codedBands;
Chris@69 530 }
Chris@69 531
Chris@69 532 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,
Chris@69 533 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)
Chris@69 534 {
Chris@69 535 int lo, hi, len, j;
Chris@69 536 int codedBands;
Chris@69 537 int skip_start;
Chris@69 538 int skip_rsv;
Chris@69 539 int intensity_rsv;
Chris@69 540 int dual_stereo_rsv;
Chris@69 541 VARDECL(int, bits1);
Chris@69 542 VARDECL(int, bits2);
Chris@69 543 VARDECL(int, thresh);
Chris@69 544 VARDECL(int, trim_offset);
Chris@69 545 SAVE_STACK;
Chris@69 546
Chris@69 547 total = IMAX(total, 0);
Chris@69 548 len = m->nbEBands;
Chris@69 549 skip_start = start;
Chris@69 550 /* Reserve a bit to signal the end of manually skipped bands. */
Chris@69 551 skip_rsv = total >= 1<<BITRES ? 1<<BITRES : 0;
Chris@69 552 total -= skip_rsv;
Chris@69 553 /* Reserve bits for the intensity and dual stereo parameters. */
Chris@69 554 intensity_rsv = dual_stereo_rsv = 0;
Chris@69 555 if (C==2)
Chris@69 556 {
Chris@69 557 intensity_rsv = LOG2_FRAC_TABLE[end-start];
Chris@69 558 if (intensity_rsv>total)
Chris@69 559 intensity_rsv = 0;
Chris@69 560 else
Chris@69 561 {
Chris@69 562 total -= intensity_rsv;
Chris@69 563 dual_stereo_rsv = total>=1<<BITRES ? 1<<BITRES : 0;
Chris@69 564 total -= dual_stereo_rsv;
Chris@69 565 }
Chris@69 566 }
Chris@69 567 ALLOC(bits1, len, int);
Chris@69 568 ALLOC(bits2, len, int);
Chris@69 569 ALLOC(thresh, len, int);
Chris@69 570 ALLOC(trim_offset, len, int);
Chris@69 571
Chris@69 572 for (j=start;j<end;j++)
Chris@69 573 {
Chris@69 574 /* Below this threshold, we're sure not to allocate any PVQ bits */
Chris@69 575 thresh[j] = IMAX((C)<<BITRES, (3*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>4);
Chris@69 576 /* Tilt of the allocation curve */
Chris@69 577 trim_offset[j] = C*(m->eBands[j+1]-m->eBands[j])*(alloc_trim-5-LM)*(end-j-1)
Chris@69 578 *(1<<(LM+BITRES))>>6;
Chris@69 579 /* Giving less resolution to single-coefficient bands because they get
Chris@69 580 more benefit from having one coarse value per coefficient*/
Chris@69 581 if ((m->eBands[j+1]-m->eBands[j])<<LM==1)
Chris@69 582 trim_offset[j] -= C<<BITRES;
Chris@69 583 }
Chris@69 584 lo = 1;
Chris@69 585 hi = m->nbAllocVectors - 1;
Chris@69 586 do
Chris@69 587 {
Chris@69 588 int done = 0;
Chris@69 589 int psum = 0;
Chris@69 590 int mid = (lo+hi) >> 1;
Chris@69 591 for (j=end;j-->start;)
Chris@69 592 {
Chris@69 593 int bitsj;
Chris@69 594 int N = m->eBands[j+1]-m->eBands[j];
Chris@69 595 bitsj = C*N*m->allocVectors[mid*len+j]<<LM>>2;
Chris@69 596 if (bitsj > 0)
Chris@69 597 bitsj = IMAX(0, bitsj + trim_offset[j]);
Chris@69 598 bitsj += offsets[j];
Chris@69 599 if (bitsj >= thresh[j] || done)
Chris@69 600 {
Chris@69 601 done = 1;
Chris@69 602 /* Don't allocate more than we can actually use */
Chris@69 603 psum += IMIN(bitsj, cap[j]);
Chris@69 604 } else {
Chris@69 605 if (bitsj >= C<<BITRES)
Chris@69 606 psum += C<<BITRES;
Chris@69 607 }
Chris@69 608 }
Chris@69 609 if (psum > total)
Chris@69 610 hi = mid - 1;
Chris@69 611 else
Chris@69 612 lo = mid + 1;
Chris@69 613 /*printf ("lo = %d, hi = %d\n", lo, hi);*/
Chris@69 614 }
Chris@69 615 while (lo <= hi);
Chris@69 616 hi = lo--;
Chris@69 617 /*printf ("interp between %d and %d\n", lo, hi);*/
Chris@69 618 for (j=start;j<end;j++)
Chris@69 619 {
Chris@69 620 int bits1j, bits2j;
Chris@69 621 int N = m->eBands[j+1]-m->eBands[j];
Chris@69 622 bits1j = C*N*m->allocVectors[lo*len+j]<<LM>>2;
Chris@69 623 bits2j = hi>=m->nbAllocVectors ?
Chris@69 624 cap[j] : C*N*m->allocVectors[hi*len+j]<<LM>>2;
Chris@69 625 if (bits1j > 0)
Chris@69 626 bits1j = IMAX(0, bits1j + trim_offset[j]);
Chris@69 627 if (bits2j > 0)
Chris@69 628 bits2j = IMAX(0, bits2j + trim_offset[j]);
Chris@69 629 if (lo > 0)
Chris@69 630 bits1j += offsets[j];
Chris@69 631 bits2j += offsets[j];
Chris@69 632 if (offsets[j]>0)
Chris@69 633 skip_start = j;
Chris@69 634 bits2j = IMAX(0,bits2j-bits1j);
Chris@69 635 bits1[j] = bits1j;
Chris@69 636 bits2[j] = bits2j;
Chris@69 637 }
Chris@69 638 codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh, cap,
Chris@69 639 total, balance, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv,
Chris@69 640 pulses, ebits, fine_priority, C, LM, ec, encode, prev, signalBandwidth);
Chris@69 641 RESTORE_STACK;
Chris@69 642 return codedBands;
Chris@69 643 }
Chris@69 644