rate.c 21 KB

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