quant_bands.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 FOUNDATION OR
  17. 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 "quant_bands.h"
  29. #include "laplace.h"
  30. #include <math.h>
  31. #include "os_support.h"
  32. #include "arch.h"
  33. #include "mathops.h"
  34. #include "stack_alloc.h"
  35. #include "rate.h"
  36. #ifdef FIXED_POINT
  37. /* Mean energy in each band quantized in Q6 */
  38. static const signed char eMeans[25] = {
  39. 103,100, 92, 85, 81,
  40. 77, 72, 70, 78, 75,
  41. 73, 71, 78, 74, 69,
  42. 72, 70, 74, 76, 71,
  43. 60, 60, 60, 60, 60
  44. };
  45. #else
  46. /* Mean energy in each band quantized in Q6 and converted back to float */
  47. static const opus_val16 eMeans[25] = {
  48. 6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f,
  49. 4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f,
  50. 4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f,
  51. 4.500000f, 4.375000f, 4.625000f, 4.750000f, 4.437500f,
  52. 3.750000f, 3.750000f, 3.750000f, 3.750000f, 3.750000f
  53. };
  54. #endif
  55. /* prediction coefficients: 0.9, 0.8, 0.65, 0.5 */
  56. #ifdef FIXED_POINT
  57. static const opus_val16 pred_coef[4] = {29440, 26112, 21248, 16384};
  58. static const opus_val16 beta_coef[4] = {30147, 22282, 12124, 6554};
  59. static const opus_val16 beta_intra = 4915;
  60. #else
  61. static const opus_val16 pred_coef[4] = {29440/32768., 26112/32768., 21248/32768., 16384/32768.};
  62. static const opus_val16 beta_coef[4] = {30147/32768., 22282/32768., 12124/32768., 6554/32768.};
  63. static const opus_val16 beta_intra = 4915/32768.;
  64. #endif
  65. /*Parameters of the Laplace-like probability models used for the coarse energy.
  66. There is one pair of parameters for each frame size, prediction type
  67. (inter/intra), and band number.
  68. The first number of each pair is the probability of 0, and the second is the
  69. decay rate, both in Q8 precision.*/
  70. static const unsigned char e_prob_model[4][2][42] = {
  71. /*120 sample frames.*/
  72. {
  73. /*Inter*/
  74. {
  75. 72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128,
  76. 64, 128, 92, 78, 92, 79, 92, 78, 90, 79, 116, 41, 115, 40,
  77. 114, 40, 132, 26, 132, 26, 145, 17, 161, 12, 176, 10, 177, 11
  78. },
  79. /*Intra*/
  80. {
  81. 24, 179, 48, 138, 54, 135, 54, 132, 53, 134, 56, 133, 55, 132,
  82. 55, 132, 61, 114, 70, 96, 74, 88, 75, 88, 87, 74, 89, 66,
  83. 91, 67, 100, 59, 108, 50, 120, 40, 122, 37, 97, 43, 78, 50
  84. }
  85. },
  86. /*240 sample frames.*/
  87. {
  88. /*Inter*/
  89. {
  90. 83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73, 93, 74,
  91. 93, 74, 109, 40, 114, 36, 117, 34, 117, 34, 143, 17, 145, 18,
  92. 146, 19, 162, 12, 165, 10, 178, 7, 189, 6, 190, 8, 177, 9
  93. },
  94. /*Intra*/
  95. {
  96. 23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91,
  97. 73, 91, 78, 89, 86, 80, 92, 66, 93, 64, 102, 59, 103, 60,
  98. 104, 60, 117, 52, 123, 44, 138, 35, 133, 31, 97, 38, 77, 45
  99. }
  100. },
  101. /*480 sample frames.*/
  102. {
  103. /*Inter*/
  104. {
  105. 61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38,
  106. 112, 38, 124, 26, 132, 27, 136, 19, 140, 20, 155, 14, 159, 16,
  107. 158, 18, 170, 13, 177, 10, 187, 8, 192, 6, 175, 9, 159, 10
  108. },
  109. /*Intra*/
  110. {
  111. 21, 178, 59, 110, 71, 86, 75, 85, 84, 83, 91, 66, 88, 73,
  112. 87, 72, 92, 75, 98, 72, 105, 58, 107, 54, 115, 52, 114, 55,
  113. 112, 56, 129, 51, 132, 40, 150, 33, 140, 29, 98, 35, 77, 42
  114. }
  115. },
  116. /*960 sample frames.*/
  117. {
  118. /*Inter*/
  119. {
  120. 42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32, 120, 36,
  121. 119, 33, 127, 33, 134, 34, 139, 21, 147, 23, 152, 20, 158, 25,
  122. 154, 26, 166, 21, 173, 16, 184, 13, 184, 10, 150, 13, 139, 15
  123. },
  124. /*Intra*/
  125. {
  126. 22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72,
  127. 96, 67, 101, 73, 107, 72, 113, 55, 118, 52, 125, 52, 118, 52,
  128. 117, 55, 135, 49, 137, 39, 157, 32, 145, 29, 97, 33, 77, 40
  129. }
  130. }
  131. };
  132. static const unsigned char small_energy_icdf[3]={2,1,0};
  133. static opus_val32 loss_distortion(const opus_val16 *eBands, opus_val16 *oldEBands, int start, int end, int len, int C)
  134. {
  135. int c, i;
  136. opus_val32 dist = 0;
  137. c=0; do {
  138. for (i=start;i<end;i++)
  139. {
  140. opus_val16 d = SHR16(SUB16(eBands[i+c*len], oldEBands[i+c*len]),2);
  141. dist = MAC16_16(dist, d,d);
  142. }
  143. } while (++c<C);
  144. return MIN32(200,SHR32(dist,2*DB_SHIFT-4));
  145. }
  146. static int quant_coarse_energy_impl(const CELTMode *m, int start, int end,
  147. const opus_val16 *eBands, opus_val16 *oldEBands,
  148. opus_int32 budget, opus_int32 tell,
  149. const unsigned char *prob_model, opus_val16 *error, ec_enc *enc,
  150. int _C, int LM, int intra, opus_val16 max_decay)
  151. {
  152. const int C = CHANNELS(_C);
  153. int i, c;
  154. int badness = 0;
  155. opus_val32 prev[2] = {0,0};
  156. opus_val16 coef;
  157. opus_val16 beta;
  158. if (tell+3 <= budget)
  159. ec_enc_bit_logp(enc, intra, 3);
  160. if (intra)
  161. {
  162. coef = 0;
  163. beta = beta_intra;
  164. } else {
  165. beta = beta_coef[LM];
  166. coef = pred_coef[LM];
  167. }
  168. /* Encode at a fixed coarse resolution */
  169. for (i=start;i<end;i++)
  170. {
  171. c=0;
  172. do {
  173. int bits_left;
  174. int qi, qi0;
  175. opus_val32 q;
  176. opus_val16 x;
  177. opus_val32 f, tmp;
  178. opus_val16 oldE;
  179. opus_val16 decay_bound;
  180. x = eBands[i+c*m->nbEBands];
  181. oldE = MAX16(-QCONST16(9.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]);
  182. #ifdef FIXED_POINT
  183. f = SHL32(EXTEND32(x),7) - PSHR32(MULT16_16(coef,oldE), 8) - prev[c];
  184. /* Rounding to nearest integer here is really important! */
  185. qi = (f+QCONST32(.5f,DB_SHIFT+7))>>(DB_SHIFT+7);
  186. decay_bound = EXTRACT16(MAX32(-QCONST16(28.f,DB_SHIFT),
  187. SUB32((opus_val32)oldEBands[i+c*m->nbEBands],max_decay)));
  188. #else
  189. f = x-coef*oldE-prev[c];
  190. /* Rounding to nearest integer here is really important! */
  191. qi = (int)floor(.5f+f);
  192. decay_bound = MAX16(-QCONST16(28.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]) - max_decay;
  193. #endif
  194. /* Prevent the energy from going down too quickly (e.g. for bands
  195. that have just one bin) */
  196. if (qi < 0 && x < decay_bound)
  197. {
  198. qi += (int)SHR16(SUB16(decay_bound,x), DB_SHIFT);
  199. if (qi > 0)
  200. qi = 0;
  201. }
  202. qi0 = qi;
  203. /* If we don't have enough bits to encode all the energy, just assume
  204. something safe. */
  205. tell = ec_tell(enc);
  206. bits_left = budget-tell-3*C*(end-i);
  207. if (i!=start && bits_left < 30)
  208. {
  209. if (bits_left < 24)
  210. qi = IMIN(1, qi);
  211. if (bits_left < 16)
  212. qi = IMAX(-1, qi);
  213. }
  214. if (budget-tell >= 15)
  215. {
  216. int pi;
  217. pi = 2*IMIN(i,20);
  218. ec_laplace_encode(enc, &qi,
  219. prob_model[pi]<<7, prob_model[pi+1]<<6);
  220. }
  221. else if(budget-tell >= 2)
  222. {
  223. qi = IMAX(-1, IMIN(qi, 1));
  224. ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2);
  225. }
  226. else if(budget-tell >= 1)
  227. {
  228. qi = IMIN(0, qi);
  229. ec_enc_bit_logp(enc, -qi, 1);
  230. }
  231. else
  232. qi = -1;
  233. error[i+c*m->nbEBands] = PSHR32(f,7) - SHL16(qi,DB_SHIFT);
  234. badness += abs(qi0-qi);
  235. q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
  236. tmp = PSHR32(MULT16_16(coef,oldE),8) + prev[c] + SHL32(q,7);
  237. #ifdef FIXED_POINT
  238. tmp = MAX32(-QCONST32(28.f, DB_SHIFT+7), tmp);
  239. #endif
  240. oldEBands[i+c*m->nbEBands] = PSHR32(tmp, 7);
  241. prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8));
  242. } while (++c < C);
  243. }
  244. return badness;
  245. }
  246. void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
  247. const opus_val16 *eBands, opus_val16 *oldEBands, opus_uint32 budget,
  248. opus_val16 *error, ec_enc *enc, int _C, int LM, int nbAvailableBytes,
  249. int force_intra, opus_val32 *delayedIntra, int two_pass, int loss_rate)
  250. {
  251. const int C = CHANNELS(_C);
  252. int intra;
  253. opus_val16 max_decay;
  254. VARDECL(opus_val16, oldEBands_intra);
  255. VARDECL(opus_val16, error_intra);
  256. ec_enc enc_start_state;
  257. opus_uint32 tell;
  258. int badness1=0;
  259. opus_int32 intra_bias;
  260. opus_val32 new_distortion;
  261. SAVE_STACK;
  262. intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C);
  263. intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512));
  264. new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C);
  265. tell = ec_tell(enc);
  266. if (tell+3 > budget)
  267. two_pass = intra = 0;
  268. /* Encode the global flags using a simple probability model
  269. (first symbols in the stream) */
  270. #ifdef FIXED_POINT
  271. max_decay = MIN32(QCONST16(16.f,DB_SHIFT), SHL32(EXTEND32(nbAvailableBytes),DB_SHIFT-3));
  272. #else
  273. max_decay = MIN32(16.f, .125f*nbAvailableBytes);
  274. #endif
  275. enc_start_state = *enc;
  276. ALLOC(oldEBands_intra, C*m->nbEBands, opus_val16);
  277. ALLOC(error_intra, C*m->nbEBands, opus_val16);
  278. OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
  279. if (two_pass || intra)
  280. {
  281. badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
  282. tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay);
  283. }
  284. if (!intra)
  285. {
  286. unsigned char *intra_buf;
  287. ec_enc enc_intra_state;
  288. opus_int32 tell_intra;
  289. opus_uint32 nstart_bytes;
  290. opus_uint32 nintra_bytes;
  291. int badness2;
  292. VARDECL(unsigned char, intra_bits);
  293. tell_intra = ec_tell_frac(enc);
  294. enc_intra_state = *enc;
  295. nstart_bytes = ec_range_bytes(&enc_start_state);
  296. nintra_bytes = ec_range_bytes(&enc_intra_state);
  297. intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes;
  298. ALLOC(intra_bits, nintra_bytes-nstart_bytes, unsigned char);
  299. /* Copy bits from intra bit-stream */
  300. OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes);
  301. *enc = enc_start_state;
  302. badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
  303. tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay);
  304. if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra)))
  305. {
  306. *enc = enc_intra_state;
  307. /* Copy intra bits to bit-stream */
  308. OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes);
  309. OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
  310. OPUS_COPY(error, error_intra, C*m->nbEBands);
  311. intra = 1;
  312. }
  313. } else {
  314. OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
  315. OPUS_COPY(error, error_intra, C*m->nbEBands);
  316. }
  317. if (intra)
  318. *delayedIntra = new_distortion;
  319. else
  320. *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra),
  321. new_distortion);
  322. RESTORE_STACK;
  323. }
  324. void quant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, ec_enc *enc, int _C)
  325. {
  326. int i, c;
  327. const int C = CHANNELS(_C);
  328. /* Encode finer resolution */
  329. for (i=start;i<end;i++)
  330. {
  331. opus_int16 frac = 1<<fine_quant[i];
  332. if (fine_quant[i] <= 0)
  333. continue;
  334. c=0;
  335. do {
  336. int q2;
  337. opus_val16 offset;
  338. #ifdef FIXED_POINT
  339. /* Has to be without rounding */
  340. q2 = (error[i+c*m->nbEBands]+QCONST16(.5f,DB_SHIFT))>>(DB_SHIFT-fine_quant[i]);
  341. #else
  342. q2 = (int)floor((error[i+c*m->nbEBands]+.5f)*frac);
  343. #endif
  344. if (q2 > frac-1)
  345. q2 = frac-1;
  346. if (q2<0)
  347. q2 = 0;
  348. ec_enc_bits(enc, q2, fine_quant[i]);
  349. #ifdef FIXED_POINT
  350. offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5f,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT));
  351. #else
  352. offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f;
  353. #endif
  354. oldEBands[i+c*m->nbEBands] += offset;
  355. error[i+c*m->nbEBands] -= offset;
  356. /*printf ("%f ", error[i] - offset);*/
  357. } while (++c < C);
  358. }
  359. }
  360. void quant_energy_finalise(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, int *fine_priority, int bits_left, ec_enc *enc, int _C)
  361. {
  362. int i, prio, c;
  363. const int C = CHANNELS(_C);
  364. /* Use up the remaining bits */
  365. for (prio=0;prio<2;prio++)
  366. {
  367. for (i=start;i<end && bits_left>=C ;i++)
  368. {
  369. if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
  370. continue;
  371. c=0;
  372. do {
  373. int q2;
  374. opus_val16 offset;
  375. q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
  376. ec_enc_bits(enc, q2, 1);
  377. #ifdef FIXED_POINT
  378. offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5f,DB_SHIFT),fine_quant[i]+1);
  379. #else
  380. offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
  381. #endif
  382. oldEBands[i+c*m->nbEBands] += offset;
  383. bits_left--;
  384. } while (++c < C);
  385. }
  386. }
  387. }
  388. void unquant_coarse_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int intra, ec_dec *dec, int _C, int LM)
  389. {
  390. const unsigned char *prob_model = e_prob_model[LM][intra];
  391. int i, c;
  392. opus_val32 prev[2] = {0, 0};
  393. opus_val16 coef;
  394. opus_val16 beta;
  395. const int C = CHANNELS(_C);
  396. opus_int32 budget;
  397. opus_int32 tell;
  398. if (intra)
  399. {
  400. coef = 0;
  401. beta = beta_intra;
  402. } else {
  403. beta = beta_coef[LM];
  404. coef = pred_coef[LM];
  405. }
  406. budget = dec->storage*8;
  407. /* Decode at a fixed coarse resolution */
  408. for (i=start;i<end;i++)
  409. {
  410. c=0;
  411. do {
  412. int qi;
  413. opus_val32 q;
  414. opus_val32 tmp;
  415. tell = ec_tell(dec);
  416. if(budget-tell>=15)
  417. {
  418. int pi;
  419. pi = 2*IMIN(i,20);
  420. qi = ec_laplace_decode(dec,
  421. prob_model[pi]<<7, prob_model[pi+1]<<6);
  422. }
  423. else if(budget-tell>=2)
  424. {
  425. qi = ec_dec_icdf(dec, small_energy_icdf, 2);
  426. qi = (qi>>1)^-(qi&1);
  427. }
  428. else if(budget-tell>=1)
  429. {
  430. qi = -ec_dec_bit_logp(dec, 1);
  431. }
  432. else
  433. qi = -1;
  434. q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
  435. oldEBands[i+c*m->nbEBands] = MAX16(-QCONST16(9.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]);
  436. tmp = PSHR32(MULT16_16(coef,oldEBands[i+c*m->nbEBands]),8) + prev[c] + SHL32(q,7);
  437. #ifdef FIXED_POINT
  438. tmp = MAX32(-QCONST32(28.f, DB_SHIFT+7), tmp);
  439. #endif
  440. oldEBands[i+c*m->nbEBands] = PSHR32(tmp, 7);
  441. prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8));
  442. } while (++c < C);
  443. }
  444. }
  445. void unquant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int *fine_quant, ec_dec *dec, int _C)
  446. {
  447. int i, c;
  448. const int C = CHANNELS(_C);
  449. /* Decode finer resolution */
  450. for (i=start;i<end;i++)
  451. {
  452. if (fine_quant[i] <= 0)
  453. continue;
  454. c=0;
  455. do {
  456. int q2;
  457. opus_val16 offset;
  458. q2 = ec_dec_bits(dec, fine_quant[i]);
  459. #ifdef FIXED_POINT
  460. offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5f,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT));
  461. #else
  462. offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f;
  463. #endif
  464. oldEBands[i+c*m->nbEBands] += offset;
  465. } while (++c < C);
  466. }
  467. }
  468. void unquant_energy_finalise(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int *fine_quant, int *fine_priority, int bits_left, ec_dec *dec, int _C)
  469. {
  470. int i, prio, c;
  471. const int C = CHANNELS(_C);
  472. /* Use up the remaining bits */
  473. for (prio=0;prio<2;prio++)
  474. {
  475. for (i=start;i<end && bits_left>=C ;i++)
  476. {
  477. if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
  478. continue;
  479. c=0;
  480. do {
  481. int q2;
  482. opus_val16 offset;
  483. q2 = ec_dec_bits(dec, 1);
  484. #ifdef FIXED_POINT
  485. offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5f,DB_SHIFT),fine_quant[i]+1);
  486. #else
  487. offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
  488. #endif
  489. oldEBands[i+c*m->nbEBands] += offset;
  490. bits_left--;
  491. } while (++c < C);
  492. }
  493. }
  494. }
  495. void log2Amp(const CELTMode *m, int start, int end,
  496. celt_ener *eBands, opus_val16 *oldEBands, int _C)
  497. {
  498. int c, i;
  499. const int C = CHANNELS(_C);
  500. c=0;
  501. do {
  502. for (i=0;i<start;i++)
  503. eBands[i+c*m->nbEBands] = 0;
  504. for (;i<end;i++)
  505. {
  506. opus_val16 lg = ADD16(oldEBands[i+c*m->nbEBands],
  507. SHL16((opus_val16)eMeans[i],6));
  508. eBands[i+c*m->nbEBands] = PSHR32(celt_exp2(lg),4);
  509. }
  510. for (;i<m->nbEBands;i++)
  511. eBands[i+c*m->nbEBands] = 0;
  512. } while (++c < C);
  513. }
  514. void amp2Log2(const CELTMode *m, int effEnd, int end,
  515. celt_ener *bandE, opus_val16 *bandLogE, int _C)
  516. {
  517. int c, i;
  518. const int C = CHANNELS(_C);
  519. c=0;
  520. do {
  521. for (i=0;i<effEnd;i++)
  522. bandLogE[i+c*m->nbEBands] =
  523. celt_log2(SHL32(bandE[i+c*m->nbEBands],2))
  524. - SHL16((opus_val16)eMeans[i],6);
  525. for (i=effEnd;i<end;i++)
  526. bandLogE[c*m->nbEBands+i] = -QCONST16(14.f,DB_SHIFT);
  527. } while (++c < C);
  528. }