sbrhuff.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: sbrhuff.c,v 1.1 2005/02/26 01:47:35 jrecker Exp $
  3. *
  4. * Portions Copyright (c) 1995-2005 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file,
  7. * are subject to the current version of the RealNetworks Public
  8. * Source License (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the current version of the RealNetworks Community
  11. * Source License (the "RCSL") available at
  12. * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13. * will apply. You may also obtain the license terms directly from
  14. * RealNetworks. You may not use this file except in compliance with
  15. * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16. * to this file, the RCSL. Please see the applicable RPSL or RCSL for
  17. * the rights, obligations and limitations governing use of the
  18. * contents of the file.
  19. *
  20. * This file is part of the Helix DNA Technology. RealNetworks is the
  21. * developer of the Original Code and owns the copyrights in the
  22. * portions it created.
  23. *
  24. * This file, and the files included with this file, is distributed
  25. * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  26. * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  27. * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  28. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  29. * ENJOYMENT OR NON-INFRINGEMENT.
  30. *
  31. * Technology Compatibility Kit Test Suite(s) Location:
  32. * http://www.helixcommunity.org/content/tck
  33. *
  34. * Contributor(s):
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /**************************************************************************************
  38. * Fixed-point HE-AAC decoder
  39. * Jon Recker (jrecker@real.com)
  40. * February 2005
  41. *
  42. * sbrhuff.c - functions for unpacking Huffman-coded envelope and noise data
  43. **************************************************************************************/
  44. #include "sbr.h"
  45. #include "assembly.h"
  46. /**************************************************************************************
  47. * Function: DecodeHuffmanScalar
  48. *
  49. * Description: decode one Huffman symbol from bitstream
  50. *
  51. * Inputs: pointers to Huffman table and info struct
  52. * left-aligned bit buffer with >= huffTabInfo->maxBits bits
  53. *
  54. * Outputs: decoded symbol in *val
  55. *
  56. * Return: number of bits in symbol
  57. *
  58. * Notes: assumes canonical Huffman codes:
  59. * first CW always 0, we have "count" CW's of length "nBits" bits
  60. * starting CW for codes of length nBits+1 =
  61. * (startCW[nBits] + count[nBits]) << 1
  62. * if there are no codes at nBits, then we just keep << 1 each time
  63. * (since count[nBits] = 0)
  64. **************************************************************************************/
  65. static int DecodeHuffmanScalar(const signed /*short*/ int *huffTab, const HuffInfo *huffTabInfo, unsigned int bitBuf, signed int *val)
  66. {
  67. unsigned int count, start, shift, t;
  68. const unsigned int /*char*/ *countPtr;
  69. const signed int /*short*/ *map;
  70. map = huffTab + huffTabInfo->offset;
  71. countPtr = huffTabInfo->count;
  72. start = 0;
  73. count = 0;
  74. shift = 32;
  75. do {
  76. start += count;
  77. start <<= 1;
  78. map += count;
  79. count = *countPtr++;
  80. shift--;
  81. t = (bitBuf >> shift) - start;
  82. } while (t >= count);
  83. *val = (signed int)map[t];
  84. return (countPtr - huffTabInfo->count);
  85. }
  86. /**************************************************************************************
  87. * Function: DecodeOneSymbol
  88. *
  89. * Description: dequantize one Huffman symbol from bitstream,
  90. * using table huffTabSBR[huffTabIndex]
  91. *
  92. * Inputs: BitStreamInfo struct pointing to start of next Huffman codeword
  93. * index of Huffman table
  94. *
  95. * Outputs: bitstream advanced by number of bits in codeword
  96. *
  97. * Return: one decoded symbol
  98. **************************************************************************************/
  99. static int DecodeOneSymbol(BitStreamInfo *bsi, int huffTabIndex)
  100. {
  101. int nBits, val;
  102. unsigned int bitBuf;
  103. const HuffInfo *hi;
  104. hi = &(huffTabSBRInfo[huffTabIndex]);
  105. bitBuf = GetBitsNoAdvance(bsi, hi->maxBits) << (32 - hi->maxBits);
  106. nBits = DecodeHuffmanScalar(huffTabSBR, hi, bitBuf, &val);
  107. AdvanceBitstream(bsi, nBits);
  108. return val;
  109. }
  110. /* [1.0, sqrt(2)], format = Q29 (one guard bit for decoupling) */
  111. static const int envDQTab[2] PROGMEM = {0x20000000, 0x2d413ccc};
  112. /**************************************************************************************
  113. * Function: DequantizeEnvelope
  114. *
  115. * Description: dequantize envelope scalefactors
  116. *
  117. * Inputs: number of scalefactors to process
  118. * amplitude resolution flag for this frame (0 or 1)
  119. * quantized envelope scalefactors
  120. *
  121. * Outputs: dequantized envelope scalefactors
  122. *
  123. * Return: extra int bits in output (6 + expMax)
  124. * in other words, output format = Q(FBITS_OUT_DQ_ENV - (6 + expMax))
  125. *
  126. * Notes: dequantized scalefactors have at least 2 GB
  127. **************************************************************************************/
  128. static int DequantizeEnvelope(int nBands, int ampRes, signed char *envQuant, int *envDequant)
  129. {
  130. int exp, expMax, i, scalei;
  131. if (nBands <= 0)
  132. return 0;
  133. /* scan for largest dequant value (do separately from envelope decoding to keep code cleaner) */
  134. expMax = 0;
  135. for (i = 0; i < nBands; i++) {
  136. if (envQuant[i] > expMax)
  137. expMax = envQuant[i];
  138. }
  139. /* dequantized envelope gains
  140. * envDequant = 64*2^(envQuant / alpha) = 2^(6 + envQuant / alpha)
  141. * if ampRes == 0, alpha = 2 and range of envQuant = [0, 127]
  142. * if ampRes == 1, alpha = 1 and range of envQuant = [0, 63]
  143. * also if coupling is on, envDequant is scaled by something in range [0, 2]
  144. * so range of envDequant = [2^6, 2^69] (no coupling), [2^6, 2^70] (with coupling)
  145. *
  146. * typical range (from observation) of envQuant/alpha = [0, 27] --> largest envQuant ~= 2^33
  147. * output: Q(29 - (6 + expMax))
  148. *
  149. * reference: 14496-3:2001(E)/4.6.18.3.5 and 14496-4:200X/FPDAM8/5.6.5.1.2.1.5
  150. */
  151. if (ampRes) {
  152. do {
  153. exp = *envQuant++;
  154. scalei = MIN(expMax - exp, 31);
  155. *envDequant++ = envDQTab[0] >> scalei;
  156. } while (--nBands);
  157. return (6 + expMax);
  158. } else {
  159. expMax >>= 1;
  160. do {
  161. exp = *envQuant++;
  162. scalei = MIN(expMax - (exp >> 1), 31);
  163. *envDequant++ = envDQTab[exp & 0x01] >> scalei;
  164. } while (--nBands);
  165. return (6 + expMax);
  166. }
  167. }
  168. /**************************************************************************************
  169. * Function: DequantizeNoise
  170. *
  171. * Description: dequantize noise scalefactors
  172. *
  173. * Inputs: number of scalefactors to process
  174. * quantized noise scalefactors
  175. *
  176. * Outputs: dequantized noise scalefactors, format = Q(FBITS_OUT_DQ_NOISE)
  177. *
  178. * Return: none
  179. *
  180. * Notes: dequantized scalefactors have at least 2 GB
  181. **************************************************************************************/
  182. static void DequantizeNoise(int nBands, signed char *noiseQuant, int *noiseDequant)
  183. {
  184. int exp, scalei;
  185. if (nBands <= 0)
  186. return;
  187. /* dequantize noise floor gains (4.6.18.3.5):
  188. * noiseDequant = 2^(NOISE_FLOOR_OFFSET - noiseQuant)
  189. *
  190. * range of noiseQuant = [0, 30] (see 4.6.18.3.6), NOISE_FLOOR_OFFSET = 6
  191. * so range of noiseDequant = [2^-24, 2^6]
  192. */
  193. do {
  194. exp = *noiseQuant++;
  195. scalei = NOISE_FLOOR_OFFSET - exp + FBITS_OUT_DQ_NOISE; /* 6 + 24 - exp, exp = [0,30] */
  196. if (scalei < 0)
  197. *noiseDequant++ = 0;
  198. else if (scalei < 30)
  199. *noiseDequant++ = 1 << scalei;
  200. else
  201. *noiseDequant++ = 0x3fffffff; /* leave 2 GB */
  202. } while (--nBands);
  203. }
  204. /**************************************************************************************
  205. * Function: DecodeSBREnvelope
  206. *
  207. * Description: decode delta Huffman coded envelope scalefactors from bitstream
  208. *
  209. * Inputs: BitStreamInfo struct pointing to start of env data
  210. * initialized PSInfoSBR struct
  211. * initialized SBRGrid struct for this channel
  212. * initialized SBRFreq struct for this SCE/CPE block
  213. * initialized SBRChan struct for this channel
  214. * index of current channel (0 for SCE, 0 or 1 for CPE)
  215. *
  216. * Outputs: dequantized env scalefactors for left channel (before decoupling)
  217. * dequantized env scalefactors for right channel (if coupling off)
  218. * or raw decoded env scalefactors for right channel (if coupling on)
  219. *
  220. * Return: none
  221. **************************************************************************************/
  222. void DecodeSBREnvelope(BitStreamInfo *bsi, PSInfoSBR *psi, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch)
  223. {
  224. int huffIndexTime, huffIndexFreq, env, envStartBits, band, nBands, sf, lastEnv;
  225. int freqRes, freqResPrev, dShift, i;
  226. if (psi->couplingFlag && ch) {
  227. dShift = 1;
  228. if (sbrGrid->ampResFrame) {
  229. huffIndexTime = HuffTabSBR_tEnv30b;
  230. huffIndexFreq = HuffTabSBR_fEnv30b;
  231. envStartBits = 5;
  232. } else {
  233. huffIndexTime = HuffTabSBR_tEnv15b;
  234. huffIndexFreq = HuffTabSBR_fEnv15b;
  235. envStartBits = 6;
  236. }
  237. } else {
  238. dShift = 0;
  239. if (sbrGrid->ampResFrame) {
  240. huffIndexTime = HuffTabSBR_tEnv30;
  241. huffIndexFreq = HuffTabSBR_fEnv30;
  242. envStartBits = 6;
  243. } else {
  244. huffIndexTime = HuffTabSBR_tEnv15;
  245. huffIndexFreq = HuffTabSBR_fEnv15;
  246. envStartBits = 7;
  247. }
  248. }
  249. /* range of envDataQuant[] = [0, 127] (see comments in DequantizeEnvelope() for reference) */
  250. for (env = 0; env < sbrGrid->numEnv; env++) {
  251. nBands = (sbrGrid->freqRes[env] ? sbrFreq->nHigh : sbrFreq->nLow);
  252. freqRes = (sbrGrid->freqRes[env]);
  253. freqResPrev = (env == 0 ? sbrGrid->freqResPrev : sbrGrid->freqRes[env-1]);
  254. lastEnv = (env == 0 ? sbrGrid->numEnvPrev-1 : env-1);
  255. if (lastEnv < 0)
  256. lastEnv = 0; /* first frame */
  257. ASSERT(nBands <= MAX_QMF_BANDS);
  258. if (sbrChan->deltaFlagEnv[env] == 0) {
  259. /* delta coding in freq */
  260. sf = GetBits(bsi, envStartBits) << dShift;
  261. sbrChan->envDataQuant[env][0] = sf;
  262. for (band = 1; band < nBands; band++) {
  263. sf = DecodeOneSymbol(bsi, huffIndexFreq) << dShift;
  264. sbrChan->envDataQuant[env][band] = sf + sbrChan->envDataQuant[env][band-1];
  265. }
  266. } else if (freqRes == freqResPrev) {
  267. /* delta coding in time - same freq resolution for both frames */
  268. for (band = 0; band < nBands; band++) {
  269. sf = DecodeOneSymbol(bsi, huffIndexTime) << dShift;
  270. sbrChan->envDataQuant[env][band] = sf + sbrChan->envDataQuant[lastEnv][band];
  271. }
  272. } else if (freqRes == 0 && freqResPrev == 1) {
  273. /* delta coding in time - low freq resolution for new frame, high freq resolution for old frame */
  274. for (band = 0; band < nBands; band++) {
  275. sf = DecodeOneSymbol(bsi, huffIndexTime) << dShift;
  276. sbrChan->envDataQuant[env][band] = sf;
  277. for (i = 0; i < sbrFreq->nHigh; i++) {
  278. if (sbrFreq->freqHigh[i] == sbrFreq->freqLow[band]) {
  279. sbrChan->envDataQuant[env][band] += sbrChan->envDataQuant[lastEnv][i];
  280. break;
  281. }
  282. }
  283. }
  284. } else if (freqRes == 1 && freqResPrev == 0) {
  285. /* delta coding in time - high freq resolution for new frame, low freq resolution for old frame */
  286. for (band = 0; band < nBands; band++) {
  287. sf = DecodeOneSymbol(bsi, huffIndexTime) << dShift;
  288. sbrChan->envDataQuant[env][band] = sf;
  289. for (i = 0; i < sbrFreq->nLow; i++) {
  290. if (sbrFreq->freqLow[i] <= sbrFreq->freqHigh[band] && sbrFreq->freqHigh[band] < sbrFreq->freqLow[i+1] ) {
  291. sbrChan->envDataQuant[env][band] += sbrChan->envDataQuant[lastEnv][i];
  292. break;
  293. }
  294. }
  295. }
  296. }
  297. /* skip coupling channel */
  298. if (ch != 1 || psi->couplingFlag != 1)
  299. psi->envDataDequantScale[ch][env] = DequantizeEnvelope(nBands, sbrGrid->ampResFrame, sbrChan->envDataQuant[env], psi->envDataDequant[ch][env]);
  300. }
  301. sbrGrid->numEnvPrev = sbrGrid->numEnv;
  302. sbrGrid->freqResPrev = sbrGrid->freqRes[sbrGrid->numEnv-1];
  303. }
  304. /**************************************************************************************
  305. * Function: DecodeSBRNoise
  306. *
  307. * Description: decode delta Huffman coded noise scalefactors from bitstream
  308. *
  309. * Inputs: BitStreamInfo struct pointing to start of noise data
  310. * initialized PSInfoSBR struct
  311. * initialized SBRGrid struct for this channel
  312. * initialized SBRFreq struct for this SCE/CPE block
  313. * initialized SBRChan struct for this channel
  314. * index of current channel (0 for SCE, 0 or 1 for CPE)
  315. *
  316. * Outputs: dequantized noise scalefactors for left channel (before decoupling)
  317. * dequantized noise scalefactors for right channel (if coupling off)
  318. * or raw decoded noise scalefactors for right channel (if coupling on)
  319. *
  320. * Return: none
  321. **************************************************************************************/
  322. void DecodeSBRNoise(BitStreamInfo *bsi, PSInfoSBR *psi, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChan, int ch)
  323. {
  324. int huffIndexTime, huffIndexFreq, noiseFloor, band, dShift, sf, lastNoiseFloor;
  325. if (psi->couplingFlag && ch) {
  326. dShift = 1;
  327. huffIndexTime = HuffTabSBR_tNoise30b;
  328. huffIndexFreq = HuffTabSBR_fNoise30b;
  329. } else {
  330. dShift = 0;
  331. huffIndexTime = HuffTabSBR_tNoise30;
  332. huffIndexFreq = HuffTabSBR_fNoise30;
  333. }
  334. for (noiseFloor = 0; noiseFloor < sbrGrid->numNoiseFloors; noiseFloor++) {
  335. lastNoiseFloor = (noiseFloor == 0 ? sbrGrid->numNoiseFloorsPrev-1 : noiseFloor-1);
  336. if (lastNoiseFloor < 0)
  337. lastNoiseFloor = 0; /* first frame */
  338. ASSERT(sbrFreq->numNoiseFloorBands <= MAX_QMF_BANDS);
  339. if (sbrChan->deltaFlagNoise[noiseFloor] == 0) {
  340. /* delta coding in freq */
  341. sbrChan->noiseDataQuant[noiseFloor][0] = GetBits(bsi, 5) << dShift;
  342. for (band = 1; band < sbrFreq->numNoiseFloorBands; band++) {
  343. sf = DecodeOneSymbol(bsi, huffIndexFreq) << dShift;
  344. sbrChan->noiseDataQuant[noiseFloor][band] = sf + sbrChan->noiseDataQuant[noiseFloor][band-1];
  345. }
  346. } else {
  347. /* delta coding in time */
  348. for (band = 0; band < sbrFreq->numNoiseFloorBands; band++) {
  349. sf = DecodeOneSymbol(bsi, huffIndexTime) << dShift;
  350. sbrChan->noiseDataQuant[noiseFloor][band] = sf + sbrChan->noiseDataQuant[lastNoiseFloor][band];
  351. }
  352. }
  353. /* skip coupling channel */
  354. if (ch != 1 || psi->couplingFlag != 1)
  355. DequantizeNoise(sbrFreq->numNoiseFloorBands, sbrChan->noiseDataQuant[noiseFloor], psi->noiseDataDequant[ch][noiseFloor]);
  356. }
  357. sbrGrid->numNoiseFloorsPrev = sbrGrid->numNoiseFloors;
  358. }
  359. /* dqTabCouple[i] = 2 / (1 + 2^(12 - i)), format = Q30 */
  360. static const int dqTabCouple[25] PROGMEM = {
  361. 0x0007ff80, 0x000ffe00, 0x001ff802, 0x003fe010, 0x007f8080, 0x00fe03f8, 0x01f81f82, 0x03e0f83e,
  362. 0x07878788, 0x0e38e38e, 0x1999999a, 0x2aaaaaab, 0x40000000, 0x55555555, 0x66666666, 0x71c71c72,
  363. 0x78787878, 0x7c1f07c2, 0x7e07e07e, 0x7f01fc08, 0x7f807f80, 0x7fc01ff0, 0x7fe007fe, 0x7ff00200,
  364. 0x7ff80080,
  365. };
  366. /**************************************************************************************
  367. * Function: UncoupleSBREnvelope
  368. *
  369. * Description: scale dequantized envelope scalefactors according to channel
  370. * coupling rules
  371. *
  372. * Inputs: initialized PSInfoSBR struct including
  373. * dequantized envelope data for left channel
  374. * initialized SBRGrid struct for this channel
  375. * initialized SBRFreq struct for this SCE/CPE block
  376. * initialized SBRChan struct for right channel including
  377. * quantized envelope scalefactors
  378. *
  379. * Outputs: dequantized envelope data for left channel (after decoupling)
  380. * dequantized envelope data for right channel (after decoupling)
  381. *
  382. * Return: none
  383. **************************************************************************************/
  384. void UncoupleSBREnvelope(PSInfoSBR *psi, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChanR)
  385. {
  386. int env, band, nBands, scalei, E_1;
  387. scalei = (sbrGrid->ampResFrame ? 0 : 1);
  388. for (env = 0; env < sbrGrid->numEnv; env++) {
  389. nBands = (sbrGrid->freqRes[env] ? sbrFreq->nHigh : sbrFreq->nLow);
  390. psi->envDataDequantScale[1][env] = psi->envDataDequantScale[0][env]; /* same scalefactor for L and R */
  391. for (band = 0; band < nBands; band++) {
  392. /* clip E_1 to [0, 24] (scalefactors approach 0 or 2) */
  393. E_1 = sbrChanR->envDataQuant[env][band] >> scalei;
  394. if (E_1 < 0) E_1 = 0;
  395. if (E_1 > 24) E_1 = 24;
  396. /* envDataDequant[0] has 1 GB, so << by 2 is okay */
  397. psi->envDataDequant[1][env][band] = MULSHIFT32(psi->envDataDequant[0][env][band], dqTabCouple[24 - E_1]) << 2;
  398. psi->envDataDequant[0][env][band] = MULSHIFT32(psi->envDataDequant[0][env][band], dqTabCouple[E_1]) << 2;
  399. }
  400. }
  401. }
  402. /**************************************************************************************
  403. * Function: UncoupleSBRNoise
  404. *
  405. * Description: scale dequantized noise floor scalefactors according to channel
  406. * coupling rules
  407. *
  408. * Inputs: initialized PSInfoSBR struct including
  409. * dequantized noise data for left channel
  410. * initialized SBRGrid struct for this channel
  411. * initialized SBRFreq struct for this SCE/CPE block
  412. * initialized SBRChan struct for this channel including
  413. * quantized noise scalefactors
  414. *
  415. * Outputs: dequantized noise data for left channel (after decoupling)
  416. * dequantized noise data for right channel (after decoupling)
  417. *
  418. * Return: none
  419. **************************************************************************************/
  420. void UncoupleSBRNoise(PSInfoSBR *psi, SBRGrid *sbrGrid, SBRFreq *sbrFreq, SBRChan *sbrChanR)
  421. {
  422. int noiseFloor, band, Q_1;
  423. for (noiseFloor = 0; noiseFloor < sbrGrid->numNoiseFloors; noiseFloor++) {
  424. for (band = 0; band < sbrFreq->numNoiseFloorBands; band++) {
  425. /* Q_1 should be in range [0, 24] according to 4.6.18.3.6, but check to make sure */
  426. Q_1 = sbrChanR->noiseDataQuant[noiseFloor][band];
  427. if (Q_1 < 0) Q_1 = 0;
  428. if (Q_1 > 24) Q_1 = 24;
  429. /* noiseDataDequant[0] has 1 GB, so << by 2 is okay */
  430. psi->noiseDataDequant[1][noiseFloor][band] = MULSHIFT32(psi->noiseDataDequant[0][noiseFloor][band], dqTabCouple[24 - Q_1]) << 2;
  431. psi->noiseDataDequant[0][noiseFloor][band] = MULSHIFT32(psi->noiseDataDequant[0][noiseFloor][band], dqTabCouple[Q_1]) << 2;
  432. }
  433. }
  434. }