media/libopus/celt/rate.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial