huffman.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: huffman.c,v 1.2 2005/05/24 16:01:55 albertofloyd 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. * huffman.c - Huffman decoding
  43. **************************************************************************************/
  44. #include "coder.h"
  45. /**************************************************************************************
  46. * Function: DecodeHuffmanScalar
  47. *
  48. * Description: decode one Huffman symbol from bitstream
  49. *
  50. * Inputs: pointers to Huffman table and info struct
  51. * left-aligned bit buffer with >= huffTabInfo->maxBits bits
  52. *
  53. * Outputs: decoded symbol in *val
  54. *
  55. * Return: number of bits in symbol
  56. *
  57. * Notes: assumes canonical Huffman codes:
  58. * first CW always 0, we have "count" CW's of length "nBits" bits
  59. * starting CW for codes of length nBits+1 =
  60. * (startCW[nBits] + count[nBits]) << 1
  61. * if there are no codes at nBits, then we just keep << 1 each time
  62. * (since count[nBits] = 0)
  63. **************************************************************************************/
  64. /* __attribute__ ((section (".data"))) */ int DecodeHuffmanScalar(const signed short *huffTab, const HuffInfo *huffTabInfo, unsigned int bitBuf, signed int *val)
  65. {
  66. unsigned int count, start, shift, t;
  67. const unsigned /*char*/ int *countPtr;
  68. const signed short *map;
  69. map = huffTab + huffTabInfo->offset;
  70. countPtr = huffTabInfo->count;
  71. start = 0;
  72. count = 0;
  73. shift = 32;
  74. do {
  75. start += count;
  76. start <<= 1;
  77. map += count;
  78. count = *countPtr++;
  79. shift--;
  80. t = (bitBuf >> shift) - start;
  81. } while (t >= count);
  82. *val = (signed int)pgm_read_word(&map[t]);
  83. return (countPtr - huffTabInfo->count);
  84. }
  85. #define APPLY_SIGN(v, s) {(v) ^= ((signed int)(s) >> 31); (v) -= ((signed int)(s) >> 31);}
  86. #define GET_QUAD_SIGNBITS(v) (((unsigned int)(v) << 17) >> 29) /* bits 14-12, unsigned */
  87. #define GET_QUAD_W(v) (((signed int)(v) << 20) >> 29) /* bits 11-9, sign-extend */
  88. #define GET_QUAD_X(v) (((signed int)(v) << 23) >> 29) /* bits 8-6, sign-extend */
  89. #define GET_QUAD_Y(v) (((signed int)(v) << 26) >> 29) /* bits 5-3, sign-extend */
  90. #define GET_QUAD_Z(v) (((signed int)(v) << 29) >> 29) /* bits 2-0, sign-extend */
  91. #define GET_PAIR_SIGNBITS(v) (((unsigned int)(v) << 20) >> 30) /* bits 11-10, unsigned */
  92. #define GET_PAIR_Y(v) (((signed int)(v) << 22) >> 27) /* bits 9-5, sign-extend */
  93. #define GET_PAIR_Z(v) (((signed int)(v) << 27) >> 27) /* bits 4-0, sign-extend */
  94. #define GET_ESC_SIGNBITS(v) (((unsigned int)(v) << 18) >> 30) /* bits 13-12, unsigned */
  95. #define GET_ESC_Y(v) (((signed int)(v) << 20) >> 26) /* bits 11-6, sign-extend */
  96. #define GET_ESC_Z(v) (((signed int)(v) << 26) >> 26) /* bits 5-0, sign-extend */
  97. /**************************************************************************************
  98. * Function: UnpackZeros
  99. *
  100. * Description: fill a section of coefficients with zeros
  101. *
  102. * Inputs: number of coefficients
  103. *
  104. * Outputs: nVals zeros, starting at coef
  105. *
  106. * Return: none
  107. *
  108. * Notes: assumes nVals is always a multiple of 4 because all scalefactor bands
  109. * are a multiple of 4 coefficients long
  110. **************************************************************************************/
  111. static void UnpackZeros(int nVals, int *coef)
  112. {
  113. while (nVals > 0) {
  114. *coef++ = 0;
  115. *coef++ = 0;
  116. *coef++ = 0;
  117. *coef++ = 0;
  118. nVals -= 4;
  119. }
  120. }
  121. /**************************************************************************************
  122. * Function: UnpackQuads
  123. *
  124. * Description: decode a section of 4-way vector Huffman coded coefficients
  125. *
  126. * Inputs BitStreamInfo struct pointing to start of codewords for this section
  127. * index of Huffman codebook
  128. * number of coefficients
  129. *
  130. * Outputs: nVals coefficients, starting at coef
  131. *
  132. * Return: none
  133. *
  134. * Notes: assumes nVals is always a multiple of 4 because all scalefactor bands
  135. * are a multiple of 4 coefficients long
  136. **************************************************************************************/
  137. /* __attribute__ ((section (".data"))) */ static void UnpackQuads(BitStreamInfo *bsi, int cb, int nVals, int *coef)
  138. {
  139. int w, x, y, z, maxBits, nCodeBits, nSignBits, val;
  140. unsigned int bitBuf;
  141. maxBits = huffTabSpecInfo[cb - HUFFTAB_SPEC_OFFSET].maxBits + 4;
  142. while (nVals > 0) {
  143. /* decode quad */
  144. bitBuf = GetBitsNoAdvance(bsi, maxBits) << (32 - maxBits);
  145. nCodeBits = DecodeHuffmanScalar(huffTabSpec, &huffTabSpecInfo[cb - HUFFTAB_SPEC_OFFSET], bitBuf, &val);
  146. w = GET_QUAD_W(val);
  147. x = GET_QUAD_X(val);
  148. y = GET_QUAD_Y(val);
  149. z = GET_QUAD_Z(val);
  150. bitBuf <<= nCodeBits;
  151. nSignBits = (int)GET_QUAD_SIGNBITS(val);
  152. AdvanceBitstream(bsi, nCodeBits + nSignBits);
  153. if (nSignBits) {
  154. if (w) {APPLY_SIGN(w, bitBuf); bitBuf <<= 1;}
  155. if (x) {APPLY_SIGN(x, bitBuf); bitBuf <<= 1;}
  156. if (y) {APPLY_SIGN(y, bitBuf); bitBuf <<= 1;}
  157. if (z) {APPLY_SIGN(z, bitBuf); bitBuf <<= 1;}
  158. }
  159. *coef++ = w; *coef++ = x; *coef++ = y; *coef++ = z;
  160. nVals -= 4;
  161. }
  162. }
  163. /**************************************************************************************
  164. * Function: UnpackPairsNoEsc
  165. *
  166. * Description: decode a section of 2-way vector Huffman coded coefficients,
  167. * using non-esc tables (5 through 10)
  168. *
  169. * Inputs BitStreamInfo struct pointing to start of codewords for this section
  170. * index of Huffman codebook (must not be the escape codebook)
  171. * number of coefficients
  172. *
  173. * Outputs: nVals coefficients, starting at coef
  174. *
  175. * Return: none
  176. *
  177. * Notes: assumes nVals is always a multiple of 2 because all scalefactor bands
  178. * are a multiple of 4 coefficients long
  179. **************************************************************************************/
  180. /* __attribute__ ((section (".data"))) */ static void UnpackPairsNoEsc(BitStreamInfo *bsi, int cb, int nVals, int *coef)
  181. {
  182. int y, z, maxBits, nCodeBits, nSignBits, val;
  183. unsigned int bitBuf;
  184. maxBits = huffTabSpecInfo[cb - HUFFTAB_SPEC_OFFSET].maxBits + 2;
  185. while (nVals > 0) {
  186. /* decode pair */
  187. bitBuf = GetBitsNoAdvance(bsi, maxBits) << (32 - maxBits);
  188. nCodeBits = DecodeHuffmanScalar(huffTabSpec, &huffTabSpecInfo[cb-HUFFTAB_SPEC_OFFSET], bitBuf, &val);
  189. y = GET_PAIR_Y(val);
  190. z = GET_PAIR_Z(val);
  191. bitBuf <<= nCodeBits;
  192. nSignBits = GET_PAIR_SIGNBITS(val);
  193. AdvanceBitstream(bsi, nCodeBits + nSignBits);
  194. if (nSignBits) {
  195. if (y) {APPLY_SIGN(y, bitBuf); bitBuf <<= 1;}
  196. if (z) {APPLY_SIGN(z, bitBuf); bitBuf <<= 1;}
  197. }
  198. *coef++ = y; *coef++ = z;
  199. nVals -= 2;
  200. }
  201. }
  202. /**************************************************************************************
  203. * Function: UnpackPairsEsc
  204. *
  205. * Description: decode a section of 2-way vector Huffman coded coefficients,
  206. * using esc table (11)
  207. *
  208. * Inputs BitStreamInfo struct pointing to start of codewords for this section
  209. * index of Huffman codebook (must be the escape codebook)
  210. * number of coefficients
  211. *
  212. * Outputs: nVals coefficients, starting at coef
  213. *
  214. * Return: none
  215. *
  216. * Notes: assumes nVals is always a multiple of 2 because all scalefactor bands
  217. * are a multiple of 4 coefficients long
  218. **************************************************************************************/
  219. /* __attribute__ ((section (".data"))) */ static void UnpackPairsEsc(BitStreamInfo *bsi, int cb, int nVals, int *coef)
  220. {
  221. int y, z, maxBits, nCodeBits, nSignBits, n, val;
  222. unsigned int bitBuf;
  223. maxBits = huffTabSpecInfo[cb - HUFFTAB_SPEC_OFFSET].maxBits + 2;
  224. while (nVals > 0) {
  225. /* decode pair with escape value */
  226. bitBuf = GetBitsNoAdvance(bsi, maxBits) << (32 - maxBits);
  227. nCodeBits = DecodeHuffmanScalar(huffTabSpec, &huffTabSpecInfo[cb-HUFFTAB_SPEC_OFFSET], bitBuf, &val);
  228. y = GET_ESC_Y(val);
  229. z = GET_ESC_Z(val);
  230. bitBuf <<= nCodeBits;
  231. nSignBits = GET_ESC_SIGNBITS(val);
  232. AdvanceBitstream(bsi, nCodeBits + nSignBits);
  233. if (y == 16) {
  234. n = 4;
  235. while (GetBits(bsi, 1) == 1)
  236. n++;
  237. y = (1 << n) + GetBits(bsi, n);
  238. }
  239. if (z == 16) {
  240. n = 4;
  241. while (GetBits(bsi, 1) == 1)
  242. n++;
  243. z = (1 << n) + GetBits(bsi, n);
  244. }
  245. if (nSignBits) {
  246. if (y) {APPLY_SIGN(y, bitBuf); bitBuf <<= 1;}
  247. if (z) {APPLY_SIGN(z, bitBuf); bitBuf <<= 1;}
  248. }
  249. *coef++ = y; *coef++ = z;
  250. nVals -= 2;
  251. }
  252. }
  253. /**************************************************************************************
  254. * Function: DecodeSpectrumLong
  255. *
  256. * Description: decode transform coefficients for frame with one long block
  257. *
  258. * Inputs: platform specific info struct
  259. * BitStreamInfo struct pointing to start of spectral data
  260. * (14496-3, table 4.4.29)
  261. * index of current channel
  262. *
  263. * Outputs: decoded, quantized coefficients for this channel
  264. *
  265. * Return: none
  266. *
  267. * Notes: adds in pulse data if present
  268. * fills coefficient buffer with zeros in any region not coded with
  269. * codebook in range [1, 11] (including sfb's above sfbMax)
  270. **************************************************************************************/
  271. /* __attribute__ ((section (".data"))) */ void DecodeSpectrumLong(PSInfoBase *psi, BitStreamInfo *bsi, int ch)
  272. {
  273. int i, sfb, cb, nVals, offset;
  274. const /*short*/ int *sfbTab;
  275. unsigned char *sfbCodeBook;
  276. int *coef;
  277. ICSInfo *icsInfo;
  278. PulseInfo *pi;
  279. coef = psi->coef[ch];
  280. icsInfo = (ch == 1 && psi->commonWin == 1) ? &(psi->icsInfo[0]) : &(psi->icsInfo[ch]);
  281. /* decode long block */
  282. sfbTab = sfBandTabLong + sfBandTabLongOffset[psi->sampRateIdx];
  283. sfbCodeBook = psi->sfbCodeBook[ch];
  284. for (sfb = 0; sfb < icsInfo->maxSFB; sfb++) {
  285. cb = *sfbCodeBook++;
  286. nVals = sfbTab[sfb+1] - sfbTab[sfb];
  287. if (cb == 0)
  288. UnpackZeros(nVals, coef);
  289. else if (cb <= 4)
  290. UnpackQuads(bsi, cb, nVals, coef);
  291. else if (cb <= 10)
  292. UnpackPairsNoEsc(bsi, cb, nVals, coef);
  293. else if (cb == 11)
  294. UnpackPairsEsc(bsi, cb, nVals, coef);
  295. else
  296. UnpackZeros(nVals, coef);
  297. coef += nVals;
  298. }
  299. /* fill with zeros above maxSFB */
  300. nVals = NSAMPS_LONG - sfbTab[sfb];
  301. UnpackZeros(nVals, coef);
  302. /* add pulse data, if present */
  303. pi = &psi->pulseInfo[ch];
  304. if (pi->pulseDataPresent) {
  305. coef = psi->coef[ch];
  306. offset = sfbTab[pi->startSFB];
  307. for (i = 0; i < pi->numPulse; i++) {
  308. offset += pi->offset[i];
  309. if (coef[offset] > 0)
  310. coef[offset] += pi->amp[i];
  311. else
  312. coef[offset] -= pi->amp[i];
  313. }
  314. ASSERT(offset < NSAMPS_LONG);
  315. }
  316. }
  317. /**************************************************************************************
  318. * Function: DecodeSpectrumShort
  319. *
  320. * Description: decode transform coefficients for frame with eight short blocks
  321. *
  322. * Inputs: platform specific info struct
  323. * BitStreamInfo struct pointing to start of spectral data
  324. * (14496-3, table 4.4.29)
  325. * index of current channel
  326. *
  327. * Outputs: decoded, quantized coefficients for this channel
  328. *
  329. * Return: none
  330. *
  331. * Notes: fills coefficient buffer with zeros in any region not coded with
  332. * codebook in range [1, 11] (including sfb's above sfbMax)
  333. * deinterleaves window groups into 8 windows
  334. **************************************************************************************/
  335. /* __attribute__ ((section (".data"))) */ void DecodeSpectrumShort(PSInfoBase *psi, BitStreamInfo *bsi, int ch)
  336. {
  337. int gp, cb, nVals=0, win, offset, sfb;
  338. const /*short*/ int *sfbTab;
  339. unsigned char *sfbCodeBook;
  340. int *coef;
  341. ICSInfo *icsInfo;
  342. coef = psi->coef[ch];
  343. icsInfo = (ch == 1 && psi->commonWin == 1) ? &(psi->icsInfo[0]) : &(psi->icsInfo[ch]);
  344. /* decode short blocks, deinterleaving in-place */
  345. sfbTab = sfBandTabShort + sfBandTabShortOffset[psi->sampRateIdx];
  346. sfbCodeBook = psi->sfbCodeBook[ch];
  347. for (gp = 0; gp < icsInfo->numWinGroup; gp++) {
  348. for (sfb = 0; sfb < icsInfo->maxSFB; sfb++) {
  349. nVals = sfbTab[sfb+1] - sfbTab[sfb];
  350. cb = *sfbCodeBook++;
  351. for (win = 0; win < icsInfo->winGroupLen[gp]; win++) {
  352. offset = win*NSAMPS_SHORT;
  353. if (cb == 0)
  354. UnpackZeros(nVals, coef + offset);
  355. else if (cb <= 4)
  356. UnpackQuads(bsi, cb, nVals, coef + offset);
  357. else if (cb <= 10)
  358. UnpackPairsNoEsc(bsi, cb, nVals, coef + offset);
  359. else if (cb == 11)
  360. UnpackPairsEsc(bsi, cb, nVals, coef + offset);
  361. else
  362. UnpackZeros(nVals, coef + offset);
  363. }
  364. coef += nVals;
  365. }
  366. /* fill with zeros above maxSFB */
  367. for (win = 0; win < icsInfo->winGroupLen[gp]; win++) {
  368. offset = win*NSAMPS_SHORT;
  369. nVals = NSAMPS_SHORT - sfbTab[sfb];
  370. UnpackZeros(nVals, coef + offset);
  371. }
  372. coef += nVals;
  373. coef += (icsInfo->winGroupLen[gp] - 1)*NSAMPS_SHORT;
  374. }
  375. ASSERT(coef == psi->coef[ch] + NSAMPS_LONG);
  376. }