annotate src/opus-1.3/celt/rate.c @ 169:223a55898ab9 tip default

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