1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/celt/rate.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,638 @@ 1.4 +/* Copyright (c) 2007-2008 CSIRO 1.5 + Copyright (c) 2007-2009 Xiph.Org Foundation 1.6 + Written by Jean-Marc Valin */ 1.7 +/* 1.8 + Redistribution and use in source and binary forms, with or without 1.9 + modification, are permitted provided that the following conditions 1.10 + are met: 1.11 + 1.12 + - Redistributions of source code must retain the above copyright 1.13 + notice, this list of conditions and the following disclaimer. 1.14 + 1.15 + - Redistributions in binary form must reproduce the above copyright 1.16 + notice, this list of conditions and the following disclaimer in the 1.17 + documentation and/or other materials provided with the distribution. 1.18 + 1.19 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.20 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.21 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.22 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 1.23 + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.24 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.25 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.26 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.27 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.28 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.29 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.30 +*/ 1.31 + 1.32 +#ifdef HAVE_CONFIG_H 1.33 +#include "config.h" 1.34 +#endif 1.35 + 1.36 +#include <math.h> 1.37 +#include "modes.h" 1.38 +#include "cwrs.h" 1.39 +#include "arch.h" 1.40 +#include "os_support.h" 1.41 + 1.42 +#include "entcode.h" 1.43 +#include "rate.h" 1.44 + 1.45 +static const unsigned char LOG2_FRAC_TABLE[24]={ 1.46 + 0, 1.47 + 8,13, 1.48 + 16,19,21,23, 1.49 + 24,26,27,28,29,30,31,32, 1.50 + 32,33,34,34,35,36,36,37,37 1.51 +}; 1.52 + 1.53 +#ifdef CUSTOM_MODES 1.54 + 1.55 +/*Determines if V(N,K) fits in a 32-bit unsigned integer. 1.56 + N and K are themselves limited to 15 bits.*/ 1.57 +static int fits_in32(int _n, int _k) 1.58 +{ 1.59 + static const opus_int16 maxN[15] = { 1.60 + 32767, 32767, 32767, 1476, 283, 109, 60, 40, 1.61 + 29, 24, 20, 18, 16, 14, 13}; 1.62 + static const opus_int16 maxK[15] = { 1.63 + 32767, 32767, 32767, 32767, 1172, 238, 95, 53, 1.64 + 36, 27, 22, 18, 16, 15, 13}; 1.65 + if (_n>=14) 1.66 + { 1.67 + if (_k>=14) 1.68 + return 0; 1.69 + else 1.70 + return _n <= maxN[_k]; 1.71 + } else { 1.72 + return _k <= maxK[_n]; 1.73 + } 1.74 +} 1.75 + 1.76 +void compute_pulse_cache(CELTMode *m, int LM) 1.77 +{ 1.78 + int C; 1.79 + int i; 1.80 + int j; 1.81 + int curr=0; 1.82 + int nbEntries=0; 1.83 + int entryN[100], entryK[100], entryI[100]; 1.84 + const opus_int16 *eBands = m->eBands; 1.85 + PulseCache *cache = &m->cache; 1.86 + opus_int16 *cindex; 1.87 + unsigned char *bits; 1.88 + unsigned char *cap; 1.89 + 1.90 + cindex = (opus_int16 *)opus_alloc(sizeof(cache->index[0])*m->nbEBands*(LM+2)); 1.91 + cache->index = cindex; 1.92 + 1.93 + /* Scan for all unique band sizes */ 1.94 + for (i=0;i<=LM+1;i++) 1.95 + { 1.96 + for (j=0;j<m->nbEBands;j++) 1.97 + { 1.98 + int k; 1.99 + int N = (eBands[j+1]-eBands[j])<<i>>1; 1.100 + cindex[i*m->nbEBands+j] = -1; 1.101 + /* Find other bands that have the same size */ 1.102 + for (k=0;k<=i;k++) 1.103 + { 1.104 + int n; 1.105 + for (n=0;n<m->nbEBands && (k!=i || n<j);n++) 1.106 + { 1.107 + if (N == (eBands[n+1]-eBands[n])<<k>>1) 1.108 + { 1.109 + cindex[i*m->nbEBands+j] = cindex[k*m->nbEBands+n]; 1.110 + break; 1.111 + } 1.112 + } 1.113 + } 1.114 + if (cache->index[i*m->nbEBands+j] == -1 && N!=0) 1.115 + { 1.116 + int K; 1.117 + entryN[nbEntries] = N; 1.118 + K = 0; 1.119 + while (fits_in32(N,get_pulses(K+1)) && K<MAX_PSEUDO) 1.120 + K++; 1.121 + entryK[nbEntries] = K; 1.122 + cindex[i*m->nbEBands+j] = curr; 1.123 + entryI[nbEntries] = curr; 1.124 + 1.125 + curr += K+1; 1.126 + nbEntries++; 1.127 + } 1.128 + } 1.129 + } 1.130 + bits = (unsigned char *)opus_alloc(sizeof(unsigned char)*curr); 1.131 + cache->bits = bits; 1.132 + cache->size = curr; 1.133 + /* Compute the cache for all unique sizes */ 1.134 + for (i=0;i<nbEntries;i++) 1.135 + { 1.136 + unsigned char *ptr = bits+entryI[i]; 1.137 + opus_int16 tmp[MAX_PULSES+1]; 1.138 + get_required_bits(tmp, entryN[i], get_pulses(entryK[i]), BITRES); 1.139 + for (j=1;j<=entryK[i];j++) 1.140 + ptr[j] = tmp[get_pulses(j)]-1; 1.141 + ptr[0] = entryK[i]; 1.142 + } 1.143 + 1.144 + /* Compute the maximum rate for each band at which we'll reliably use as 1.145 + many bits as we ask for. */ 1.146 + cache->caps = cap = (unsigned char *)opus_alloc(sizeof(cache->caps[0])*(LM+1)*2*m->nbEBands); 1.147 + for (i=0;i<=LM;i++) 1.148 + { 1.149 + for (C=1;C<=2;C++) 1.150 + { 1.151 + for (j=0;j<m->nbEBands;j++) 1.152 + { 1.153 + int N0; 1.154 + int max_bits; 1.155 + N0 = m->eBands[j+1]-m->eBands[j]; 1.156 + /* N=1 bands only have a sign bit and fine bits. */ 1.157 + if (N0<<i == 1) 1.158 + max_bits = C*(1+MAX_FINE_BITS)<<BITRES; 1.159 + else 1.160 + { 1.161 + const unsigned char *pcache; 1.162 + opus_int32 num; 1.163 + opus_int32 den; 1.164 + int LM0; 1.165 + int N; 1.166 + int offset; 1.167 + int ndof; 1.168 + int qb; 1.169 + int k; 1.170 + LM0 = 0; 1.171 + /* Even-sized bands bigger than N=2 can be split one more time. 1.172 + As of commit 44203907 all bands >1 are even, including custom modes.*/ 1.173 + if (N0 > 2) 1.174 + { 1.175 + N0>>=1; 1.176 + LM0--; 1.177 + } 1.178 + /* N0=1 bands can't be split down to N<2. */ 1.179 + else if (N0 <= 1) 1.180 + { 1.181 + LM0=IMIN(i,1); 1.182 + N0<<=LM0; 1.183 + } 1.184 + /* Compute the cost for the lowest-level PVQ of a fully split 1.185 + band. */ 1.186 + pcache = bits + cindex[(LM0+1)*m->nbEBands+j]; 1.187 + max_bits = pcache[pcache[0]]+1; 1.188 + /* Add in the cost of coding regular splits. */ 1.189 + N = N0; 1.190 + for(k=0;k<i-LM0;k++){ 1.191 + max_bits <<= 1; 1.192 + /* Offset the number of qtheta bits by log2(N)/2 1.193 + + QTHETA_OFFSET compared to their "fair share" of 1.194 + total/N */ 1.195 + offset = ((m->logN[j]+((LM0+k)<<BITRES))>>1)-QTHETA_OFFSET; 1.196 + /* The number of qtheta bits we'll allocate if the remainder 1.197 + is to be max_bits. 1.198 + The average measured cost for theta is 0.89701 times qb, 1.199 + approximated here as 459/512. */ 1.200 + num=459*(opus_int32)((2*N-1)*offset+max_bits); 1.201 + den=((opus_int32)(2*N-1)<<9)-459; 1.202 + qb = IMIN((num+(den>>1))/den, 57); 1.203 + celt_assert(qb >= 0); 1.204 + max_bits += qb; 1.205 + N <<= 1; 1.206 + } 1.207 + /* Add in the cost of a stereo split, if necessary. */ 1.208 + if (C==2) 1.209 + { 1.210 + max_bits <<= 1; 1.211 + offset = ((m->logN[j]+(i<<BITRES))>>1)-(N==2?QTHETA_OFFSET_TWOPHASE:QTHETA_OFFSET); 1.212 + ndof = 2*N-1-(N==2); 1.213 + /* The average measured cost for theta with the step PDF is 1.214 + 0.95164 times qb, approximated here as 487/512. */ 1.215 + num = (N==2?512:487)*(opus_int32)(max_bits+ndof*offset); 1.216 + den = ((opus_int32)ndof<<9)-(N==2?512:487); 1.217 + qb = IMIN((num+(den>>1))/den, (N==2?64:61)); 1.218 + celt_assert(qb >= 0); 1.219 + max_bits += qb; 1.220 + } 1.221 + /* Add the fine bits we'll use. */ 1.222 + /* Compensate for the extra DoF in stereo */ 1.223 + ndof = C*N + ((C==2 && N>2) ? 1 : 0); 1.224 + /* Offset the number of fine bits by log2(N)/2 + FINE_OFFSET 1.225 + compared to their "fair share" of total/N */ 1.226 + offset = ((m->logN[j] + (i<<BITRES))>>1)-FINE_OFFSET; 1.227 + /* N=2 is the only point that doesn't match the curve */ 1.228 + if (N==2) 1.229 + offset += 1<<BITRES>>2; 1.230 + /* The number of fine bits we'll allocate if the remainder is 1.231 + to be max_bits. */ 1.232 + num = max_bits+ndof*offset; 1.233 + den = (ndof-1)<<BITRES; 1.234 + qb = IMIN((num+(den>>1))/den, MAX_FINE_BITS); 1.235 + celt_assert(qb >= 0); 1.236 + max_bits += C*qb<<BITRES; 1.237 + } 1.238 + max_bits = (4*max_bits/(C*((m->eBands[j+1]-m->eBands[j])<<i)))-64; 1.239 + celt_assert(max_bits >= 0); 1.240 + celt_assert(max_bits < 256); 1.241 + *cap++ = (unsigned char)max_bits; 1.242 + } 1.243 + } 1.244 + } 1.245 +} 1.246 + 1.247 +#endif /* CUSTOM_MODES */ 1.248 + 1.249 +#define ALLOC_STEPS 6 1.250 + 1.251 +static OPUS_INLINE int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start, 1.252 + const int *bits1, const int *bits2, const int *thresh, const int *cap, opus_int32 total, opus_int32 *_balance, 1.253 + int skip_rsv, int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits, 1.254 + int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev, int signalBandwidth) 1.255 +{ 1.256 + opus_int32 psum; 1.257 + int lo, hi; 1.258 + int i, j; 1.259 + int logM; 1.260 + int stereo; 1.261 + int codedBands=-1; 1.262 + int alloc_floor; 1.263 + opus_int32 left, percoeff; 1.264 + int done; 1.265 + opus_int32 balance; 1.266 + SAVE_STACK; 1.267 + 1.268 + alloc_floor = C<<BITRES; 1.269 + stereo = C>1; 1.270 + 1.271 + logM = LM<<BITRES; 1.272 + lo = 0; 1.273 + hi = 1<<ALLOC_STEPS; 1.274 + for (i=0;i<ALLOC_STEPS;i++) 1.275 + { 1.276 + int mid = (lo+hi)>>1; 1.277 + psum = 0; 1.278 + done = 0; 1.279 + for (j=end;j-->start;) 1.280 + { 1.281 + int tmp = bits1[j] + (mid*(opus_int32)bits2[j]>>ALLOC_STEPS); 1.282 + if (tmp >= thresh[j] || done) 1.283 + { 1.284 + done = 1; 1.285 + /* Don't allocate more than we can actually use */ 1.286 + psum += IMIN(tmp, cap[j]); 1.287 + } else { 1.288 + if (tmp >= alloc_floor) 1.289 + psum += alloc_floor; 1.290 + } 1.291 + } 1.292 + if (psum > total) 1.293 + hi = mid; 1.294 + else 1.295 + lo = mid; 1.296 + } 1.297 + psum = 0; 1.298 + /*printf ("interp bisection gave %d\n", lo);*/ 1.299 + done = 0; 1.300 + for (j=end;j-->start;) 1.301 + { 1.302 + int tmp = bits1[j] + (lo*bits2[j]>>ALLOC_STEPS); 1.303 + if (tmp < thresh[j] && !done) 1.304 + { 1.305 + if (tmp >= alloc_floor) 1.306 + tmp = alloc_floor; 1.307 + else 1.308 + tmp = 0; 1.309 + } else 1.310 + done = 1; 1.311 + /* Don't allocate more than we can actually use */ 1.312 + tmp = IMIN(tmp, cap[j]); 1.313 + bits[j] = tmp; 1.314 + psum += tmp; 1.315 + } 1.316 + 1.317 + /* Decide which bands to skip, working backwards from the end. */ 1.318 + for (codedBands=end;;codedBands--) 1.319 + { 1.320 + int band_width; 1.321 + int band_bits; 1.322 + int rem; 1.323 + j = codedBands-1; 1.324 + /* Never skip the first band, nor a band that has been boosted by 1.325 + dynalloc. 1.326 + In the first case, we'd be coding a bit to signal we're going to waste 1.327 + all the other bits. 1.328 + In the second case, we'd be coding a bit to redistribute all the bits 1.329 + we just signaled should be cocentrated in this band. */ 1.330 + if (j<=skip_start) 1.331 + { 1.332 + /* Give the bit we reserved to end skipping back. */ 1.333 + total += skip_rsv; 1.334 + break; 1.335 + } 1.336 + /*Figure out how many left-over bits we would be adding to this band. 1.337 + This can include bits we've stolen back from higher, skipped bands.*/ 1.338 + left = total-psum; 1.339 + percoeff = left/(m->eBands[codedBands]-m->eBands[start]); 1.340 + left -= (m->eBands[codedBands]-m->eBands[start])*percoeff; 1.341 + rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0); 1.342 + band_width = m->eBands[codedBands]-m->eBands[j]; 1.343 + band_bits = (int)(bits[j] + percoeff*band_width + rem); 1.344 + /*Only code a skip decision if we're above the threshold for this band. 1.345 + Otherwise it is force-skipped. 1.346 + This ensures that we have enough bits to code the skip flag.*/ 1.347 + if (band_bits >= IMAX(thresh[j], alloc_floor+(1<<BITRES))) 1.348 + { 1.349 + if (encode) 1.350 + { 1.351 + /*This if() block is the only part of the allocation function that 1.352 + is not a mandatory part of the bitstream: any bands we choose to 1.353 + skip here must be explicitly signaled.*/ 1.354 + /*Choose a threshold with some hysteresis to keep bands from 1.355 + fluctuating in and out.*/ 1.356 +#ifdef FUZZING 1.357 + if ((rand()&0x1) == 0) 1.358 +#else 1.359 + if (codedBands<=start+2 || (band_bits > ((j<prev?7:9)*band_width<<LM<<BITRES)>>4 && j<=signalBandwidth)) 1.360 +#endif 1.361 + { 1.362 + ec_enc_bit_logp(ec, 1, 1); 1.363 + break; 1.364 + } 1.365 + ec_enc_bit_logp(ec, 0, 1); 1.366 + } else if (ec_dec_bit_logp(ec, 1)) { 1.367 + break; 1.368 + } 1.369 + /*We used a bit to skip this band.*/ 1.370 + psum += 1<<BITRES; 1.371 + band_bits -= 1<<BITRES; 1.372 + } 1.373 + /*Reclaim the bits originally allocated to this band.*/ 1.374 + psum -= bits[j]+intensity_rsv; 1.375 + if (intensity_rsv > 0) 1.376 + intensity_rsv = LOG2_FRAC_TABLE[j-start]; 1.377 + psum += intensity_rsv; 1.378 + if (band_bits >= alloc_floor) 1.379 + { 1.380 + /*If we have enough for a fine energy bit per channel, use it.*/ 1.381 + psum += alloc_floor; 1.382 + bits[j] = alloc_floor; 1.383 + } else { 1.384 + /*Otherwise this band gets nothing at all.*/ 1.385 + bits[j] = 0; 1.386 + } 1.387 + } 1.388 + 1.389 + celt_assert(codedBands > start); 1.390 + /* Code the intensity and dual stereo parameters. */ 1.391 + if (intensity_rsv > 0) 1.392 + { 1.393 + if (encode) 1.394 + { 1.395 + *intensity = IMIN(*intensity, codedBands); 1.396 + ec_enc_uint(ec, *intensity-start, codedBands+1-start); 1.397 + } 1.398 + else 1.399 + *intensity = start+ec_dec_uint(ec, codedBands+1-start); 1.400 + } 1.401 + else 1.402 + *intensity = 0; 1.403 + if (*intensity <= start) 1.404 + { 1.405 + total += dual_stereo_rsv; 1.406 + dual_stereo_rsv = 0; 1.407 + } 1.408 + if (dual_stereo_rsv > 0) 1.409 + { 1.410 + if (encode) 1.411 + ec_enc_bit_logp(ec, *dual_stereo, 1); 1.412 + else 1.413 + *dual_stereo = ec_dec_bit_logp(ec, 1); 1.414 + } 1.415 + else 1.416 + *dual_stereo = 0; 1.417 + 1.418 + /* Allocate the remaining bits */ 1.419 + left = total-psum; 1.420 + percoeff = left/(m->eBands[codedBands]-m->eBands[start]); 1.421 + left -= (m->eBands[codedBands]-m->eBands[start])*percoeff; 1.422 + for (j=start;j<codedBands;j++) 1.423 + bits[j] += ((int)percoeff*(m->eBands[j+1]-m->eBands[j])); 1.424 + for (j=start;j<codedBands;j++) 1.425 + { 1.426 + int tmp = (int)IMIN(left, m->eBands[j+1]-m->eBands[j]); 1.427 + bits[j] += tmp; 1.428 + left -= tmp; 1.429 + } 1.430 + /*for (j=0;j<end;j++)printf("%d ", bits[j]);printf("\n");*/ 1.431 + 1.432 + balance = 0; 1.433 + for (j=start;j<codedBands;j++) 1.434 + { 1.435 + int N0, N, den; 1.436 + int offset; 1.437 + int NClogN; 1.438 + opus_int32 excess, bit; 1.439 + 1.440 + celt_assert(bits[j] >= 0); 1.441 + N0 = m->eBands[j+1]-m->eBands[j]; 1.442 + N=N0<<LM; 1.443 + bit = (opus_int32)bits[j]+balance; 1.444 + 1.445 + if (N>1) 1.446 + { 1.447 + excess = MAX32(bit-cap[j],0); 1.448 + bits[j] = bit-excess; 1.449 + 1.450 + /* Compensate for the extra DoF in stereo */ 1.451 + den=(C*N+ ((C==2 && N>2 && !*dual_stereo && j<*intensity) ? 1 : 0)); 1.452 + 1.453 + NClogN = den*(m->logN[j] + logM); 1.454 + 1.455 + /* Offset for the number of fine bits by log2(N)/2 + FINE_OFFSET 1.456 + compared to their "fair share" of total/N */ 1.457 + offset = (NClogN>>1)-den*FINE_OFFSET; 1.458 + 1.459 + /* N=2 is the only point that doesn't match the curve */ 1.460 + if (N==2) 1.461 + offset += den<<BITRES>>2; 1.462 + 1.463 + /* Changing the offset for allocating the second and third 1.464 + fine energy bit */ 1.465 + if (bits[j] + offset < den*2<<BITRES) 1.466 + offset += NClogN>>2; 1.467 + else if (bits[j] + offset < den*3<<BITRES) 1.468 + offset += NClogN>>3; 1.469 + 1.470 + /* Divide with rounding */ 1.471 + ebits[j] = IMAX(0, (bits[j] + offset + (den<<(BITRES-1))) / (den<<BITRES)); 1.472 + 1.473 + /* Make sure not to bust */ 1.474 + if (C*ebits[j] > (bits[j]>>BITRES)) 1.475 + ebits[j] = bits[j] >> stereo >> BITRES; 1.476 + 1.477 + /* More than that is useless because that's about as far as PVQ can go */ 1.478 + ebits[j] = IMIN(ebits[j], MAX_FINE_BITS); 1.479 + 1.480 + /* If we rounded down or capped this band, make it a candidate for the 1.481 + final fine energy pass */ 1.482 + fine_priority[j] = ebits[j]*(den<<BITRES) >= bits[j]+offset; 1.483 + 1.484 + /* Remove the allocated fine bits; the rest are assigned to PVQ */ 1.485 + bits[j] -= C*ebits[j]<<BITRES; 1.486 + 1.487 + } else { 1.488 + /* For N=1, all bits go to fine energy except for a single sign bit */ 1.489 + excess = MAX32(0,bit-(C<<BITRES)); 1.490 + bits[j] = bit-excess; 1.491 + ebits[j] = 0; 1.492 + fine_priority[j] = 1; 1.493 + } 1.494 + 1.495 + /* Fine energy can't take advantage of the re-balancing in 1.496 + quant_all_bands(). 1.497 + Instead, do the re-balancing here.*/ 1.498 + if(excess > 0) 1.499 + { 1.500 + int extra_fine; 1.501 + int extra_bits; 1.502 + extra_fine = IMIN(excess>>(stereo+BITRES),MAX_FINE_BITS-ebits[j]); 1.503 + ebits[j] += extra_fine; 1.504 + extra_bits = extra_fine*C<<BITRES; 1.505 + fine_priority[j] = extra_bits >= excess-balance; 1.506 + excess -= extra_bits; 1.507 + } 1.508 + balance = excess; 1.509 + 1.510 + celt_assert(bits[j] >= 0); 1.511 + celt_assert(ebits[j] >= 0); 1.512 + } 1.513 + /* Save any remaining bits over the cap for the rebalancing in 1.514 + quant_all_bands(). */ 1.515 + *_balance = balance; 1.516 + 1.517 + /* The skipped bands use all their bits for fine energy. */ 1.518 + for (;j<end;j++) 1.519 + { 1.520 + ebits[j] = bits[j] >> stereo >> BITRES; 1.521 + celt_assert(C*ebits[j]<<BITRES == bits[j]); 1.522 + bits[j] = 0; 1.523 + fine_priority[j] = ebits[j]<1; 1.524 + } 1.525 + RESTORE_STACK; 1.526 + return codedBands; 1.527 +} 1.528 + 1.529 +int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stereo, 1.530 + 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) 1.531 +{ 1.532 + int lo, hi, len, j; 1.533 + int codedBands; 1.534 + int skip_start; 1.535 + int skip_rsv; 1.536 + int intensity_rsv; 1.537 + int dual_stereo_rsv; 1.538 + VARDECL(int, bits1); 1.539 + VARDECL(int, bits2); 1.540 + VARDECL(int, thresh); 1.541 + VARDECL(int, trim_offset); 1.542 + SAVE_STACK; 1.543 + 1.544 + total = IMAX(total, 0); 1.545 + len = m->nbEBands; 1.546 + skip_start = start; 1.547 + /* Reserve a bit to signal the end of manually skipped bands. */ 1.548 + skip_rsv = total >= 1<<BITRES ? 1<<BITRES : 0; 1.549 + total -= skip_rsv; 1.550 + /* Reserve bits for the intensity and dual stereo parameters. */ 1.551 + intensity_rsv = dual_stereo_rsv = 0; 1.552 + if (C==2) 1.553 + { 1.554 + intensity_rsv = LOG2_FRAC_TABLE[end-start]; 1.555 + if (intensity_rsv>total) 1.556 + intensity_rsv = 0; 1.557 + else 1.558 + { 1.559 + total -= intensity_rsv; 1.560 + dual_stereo_rsv = total>=1<<BITRES ? 1<<BITRES : 0; 1.561 + total -= dual_stereo_rsv; 1.562 + } 1.563 + } 1.564 + ALLOC(bits1, len, int); 1.565 + ALLOC(bits2, len, int); 1.566 + ALLOC(thresh, len, int); 1.567 + ALLOC(trim_offset, len, int); 1.568 + 1.569 + for (j=start;j<end;j++) 1.570 + { 1.571 + /* Below this threshold, we're sure not to allocate any PVQ bits */ 1.572 + thresh[j] = IMAX((C)<<BITRES, (3*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>4); 1.573 + /* Tilt of the allocation curve */ 1.574 + trim_offset[j] = C*(m->eBands[j+1]-m->eBands[j])*(alloc_trim-5-LM)*(end-j-1) 1.575 + *(1<<(LM+BITRES))>>6; 1.576 + /* Giving less resolution to single-coefficient bands because they get 1.577 + more benefit from having one coarse value per coefficient*/ 1.578 + if ((m->eBands[j+1]-m->eBands[j])<<LM==1) 1.579 + trim_offset[j] -= C<<BITRES; 1.580 + } 1.581 + lo = 1; 1.582 + hi = m->nbAllocVectors - 1; 1.583 + do 1.584 + { 1.585 + int done = 0; 1.586 + int psum = 0; 1.587 + int mid = (lo+hi) >> 1; 1.588 + for (j=end;j-->start;) 1.589 + { 1.590 + int bitsj; 1.591 + int N = m->eBands[j+1]-m->eBands[j]; 1.592 + bitsj = C*N*m->allocVectors[mid*len+j]<<LM>>2; 1.593 + if (bitsj > 0) 1.594 + bitsj = IMAX(0, bitsj + trim_offset[j]); 1.595 + bitsj += offsets[j]; 1.596 + if (bitsj >= thresh[j] || done) 1.597 + { 1.598 + done = 1; 1.599 + /* Don't allocate more than we can actually use */ 1.600 + psum += IMIN(bitsj, cap[j]); 1.601 + } else { 1.602 + if (bitsj >= C<<BITRES) 1.603 + psum += C<<BITRES; 1.604 + } 1.605 + } 1.606 + if (psum > total) 1.607 + hi = mid - 1; 1.608 + else 1.609 + lo = mid + 1; 1.610 + /*printf ("lo = %d, hi = %d\n", lo, hi);*/ 1.611 + } 1.612 + while (lo <= hi); 1.613 + hi = lo--; 1.614 + /*printf ("interp between %d and %d\n", lo, hi);*/ 1.615 + for (j=start;j<end;j++) 1.616 + { 1.617 + int bits1j, bits2j; 1.618 + int N = m->eBands[j+1]-m->eBands[j]; 1.619 + bits1j = C*N*m->allocVectors[lo*len+j]<<LM>>2; 1.620 + bits2j = hi>=m->nbAllocVectors ? 1.621 + cap[j] : C*N*m->allocVectors[hi*len+j]<<LM>>2; 1.622 + if (bits1j > 0) 1.623 + bits1j = IMAX(0, bits1j + trim_offset[j]); 1.624 + if (bits2j > 0) 1.625 + bits2j = IMAX(0, bits2j + trim_offset[j]); 1.626 + if (lo > 0) 1.627 + bits1j += offsets[j]; 1.628 + bits2j += offsets[j]; 1.629 + if (offsets[j]>0) 1.630 + skip_start = j; 1.631 + bits2j = IMAX(0,bits2j-bits1j); 1.632 + bits1[j] = bits1j; 1.633 + bits2[j] = bits2j; 1.634 + } 1.635 + codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh, cap, 1.636 + total, balance, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv, 1.637 + pulses, ebits, fine_priority, C, LM, ec, encode, prev, signalBandwidth); 1.638 + RESTORE_STACK; 1.639 + return codedBands; 1.640 +} 1.641 +