huffman.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: RCSL 1.0/RPSL 1.0
  3. *
  4. * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
  5. *
  6. * The contents of this file, and the files included with this file, are
  7. * subject to the current version of the RealNetworks Public Source License
  8. * Version 1.0 (the "RPSL") available at
  9. * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10. * the file under the RealNetworks Community Source License Version 1.0
  11. * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
  12. * in which case the RCSL will apply. You may also obtain the license terms
  13. * directly from RealNetworks. You may not use this file except in
  14. * compliance with the RPSL or, if you have a valid RCSL with RealNetworks
  15. * applicable to this file, the RCSL. Please see the applicable RPSL or
  16. * RCSL for the rights, obligations and limitations governing use of the
  17. * contents of the file.
  18. *
  19. * This file is part of the Helix DNA Technology. RealNetworks is the
  20. * developer of the Original Code and owns the copyrights in the portions
  21. * it created.
  22. *
  23. * This file, and the files included with this file, is distributed and made
  24. * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  25. * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  26. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
  27. * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  28. *
  29. * Technology Compatibility Kit Test Suite(s) Location:
  30. * http://www.helixcommunity.org/content/tck
  31. *
  32. * Contributor(s):
  33. *
  34. * ***** END LICENSE BLOCK ***** */
  35. /**************************************************************************************
  36. * Fixed-point MP3 decoder
  37. * Jon Recker (jrecker@real.com), Ken Cooke (kenc@real.com)
  38. * July 2003
  39. *
  40. * huffman.c - Huffman decoding of transform coefficients
  41. **************************************************************************************/
  42. #include <contrib/hxmp3/coder.h>
  43. /* helper macros - see comments in hufftabs.c about the format of the huffman tables */
  44. #define GetMaxbits(x) ((int)( (((unsigned short)(x)) >> 0) & 0x000f))
  45. #define GetHLen(x) ((int)( (((unsigned short)(x)) >> 12) & 0x000f))
  46. #define GetCWY(x) ((int)( (((unsigned short)(x)) >> 8) & 0x000f))
  47. #define GetCWX(x) ((int)( (((unsigned short)(x)) >> 4) & 0x000f))
  48. #define GetSignBits(x) ((int)( (((unsigned short)(x)) >> 0) & 0x000f))
  49. #define GetHLenQ(x) ((int)( (((unsigned char)(x)) >> 4) & 0x0f))
  50. #define GetCWVQ(x) ((int)( (((unsigned char)(x)) >> 3) & 0x01))
  51. #define GetCWWQ(x) ((int)( (((unsigned char)(x)) >> 2) & 0x01))
  52. #define GetCWXQ(x) ((int)( (((unsigned char)(x)) >> 1) & 0x01))
  53. #define GetCWYQ(x) ((int)( (((unsigned char)(x)) >> 0) & 0x01))
  54. /* apply sign of s to the positive number x (save in MSB, will do two's complement in dequant) */
  55. #define ApplySign(x, s) { (x) |= ((s) & 0x80000000); }
  56. /**************************************************************************************
  57. * Function: DecodeHuffmanPairs
  58. *
  59. * Description: decode 2-way vector Huffman codes in the "bigValues" region of spectrum
  60. *
  61. * Inputs: valid BitStreamInfo struct, pointing to start of pair-wise codes
  62. * pointer to xy buffer to received decoded values
  63. * number of codewords to decode
  64. * index of Huffman table to use
  65. * number of bits remaining in bitstream
  66. *
  67. * Outputs: pairs of decoded coefficients in vwxy
  68. * updated BitStreamInfo struct
  69. *
  70. * Return: number of bits used, or -1 if out of bits
  71. *
  72. * Notes: assumes that nVals is an even number
  73. * si_huff.bit tests every Huffman codeword in every table (though not
  74. * necessarily all linBits outputs for x,y > 15)
  75. **************************************************************************************/
  76. static int DecodeHuffmanPairs(int *xy, int nVals, int tabIdx, int bitsLeft, unsigned char *buf, int bitOffset)
  77. {
  78. int i, x, y;
  79. int cachedBits, padBits, len, startBits, linBits, maxBits, minBits;
  80. HuffTabType tabType;
  81. unsigned short cw, *tBase, *tCurr;
  82. unsigned int cache;
  83. if(nVals <= 0)
  84. return 0;
  85. if (bitsLeft < 0)
  86. return -1;
  87. startBits = bitsLeft;
  88. tBase = (unsigned short *)(huffTable + huffTabOffset[tabIdx]);
  89. linBits = huffTabLookup[tabIdx].linBits;
  90. tabType = huffTabLookup[tabIdx].tabType;
  91. ASSERT(!(nVals & 0x01));
  92. ASSERT(tabIdx < HUFF_PAIRTABS);
  93. ASSERT(tabIdx >= 0);
  94. ASSERT(tabType != invalidTab);
  95. /* initially fill cache with any partial byte */
  96. cache = 0;
  97. cachedBits = (8 - bitOffset) & 0x07;
  98. if (cachedBits)
  99. cache = (unsigned int)(*buf++) << (32 - cachedBits);
  100. bitsLeft -= cachedBits;
  101. if (tabType == noBits) {
  102. /* table 0, no data, x = y = 0 */
  103. for (i = 0; i < nVals; i+=2) {
  104. xy[i+0] = 0;
  105. xy[i+1] = 0;
  106. }
  107. return 0;
  108. } else if (tabType == oneShot) {
  109. /* single lookup, no escapes */
  110. maxBits = GetMaxbits(tBase[0]);
  111. tBase++;
  112. padBits = 0;
  113. while (nVals > 0) {
  114. /* refill cache - assumes cachedBits <= 16 */
  115. if (bitsLeft >= 16) {
  116. /* load 2 new bytes into left-justified cache */
  117. cache |= (unsigned int)(*buf++) << (24 - cachedBits);
  118. cache |= (unsigned int)(*buf++) << (16 - cachedBits);
  119. cachedBits += 16;
  120. bitsLeft -= 16;
  121. } else {
  122. /* last time through, pad cache with zeros and drain cache */
  123. if (cachedBits + bitsLeft <= 0) return -1;
  124. if (bitsLeft > 0) cache |= (unsigned int)(*buf++) << (24 - cachedBits);
  125. if (bitsLeft > 8) cache |= (unsigned int)(*buf++) << (16 - cachedBits);
  126. cachedBits += bitsLeft;
  127. bitsLeft = 0;
  128. cache &= (signed int)0x80000000 >> (cachedBits - 1);
  129. padBits = 11;
  130. cachedBits += padBits; /* okay if this is > 32 (0's automatically shifted in from right) */
  131. }
  132. /* largest maxBits = 9, plus 2 for sign bits, so make sure cache has at least 11 bits */
  133. while (nVals > 0 && cachedBits >= 11 ) {
  134. cw = tBase[cache >> (32 - maxBits)];
  135. len = GetHLen(cw);
  136. cachedBits -= len;
  137. cache <<= len;
  138. x = GetCWX(cw); if (x) {ApplySign(x, cache); cache <<= 1; cachedBits--;}
  139. y = GetCWY(cw); if (y) {ApplySign(y, cache); cache <<= 1; cachedBits--;}
  140. /* ran out of bits - should never have consumed padBits */
  141. if (cachedBits < padBits)
  142. return -1;
  143. *xy++ = x;
  144. *xy++ = y;
  145. nVals -= 2;
  146. }
  147. }
  148. bitsLeft += (cachedBits - padBits);
  149. return (startBits - bitsLeft);
  150. } else if (tabType == loopLinbits || tabType == loopNoLinbits) {
  151. tCurr = tBase;
  152. padBits = 0;
  153. while (nVals > 0) {
  154. /* refill cache - assumes cachedBits <= 16 */
  155. if (bitsLeft >= 16) {
  156. /* load 2 new bytes into left-justified cache */
  157. cache |= (unsigned int)(*buf++) << (24 - cachedBits);
  158. cache |= (unsigned int)(*buf++) << (16 - cachedBits);
  159. cachedBits += 16;
  160. bitsLeft -= 16;
  161. } else {
  162. /* last time through, pad cache with zeros and drain cache */
  163. if (cachedBits + bitsLeft <= 0) return -1;
  164. if (bitsLeft > 0) cache |= (unsigned int)(*buf++) << (24 - cachedBits);
  165. if (bitsLeft > 8) cache |= (unsigned int)(*buf++) << (16 - cachedBits);
  166. cachedBits += bitsLeft;
  167. bitsLeft = 0;
  168. cache &= (signed int)0x80000000 >> (cachedBits - 1);
  169. padBits = 11;
  170. cachedBits += padBits; /* okay if this is > 32 (0's automatically shifted in from right) */
  171. }
  172. /* largest maxBits = 9, plus 2 for sign bits, so make sure cache has at least 11 bits */
  173. while (nVals > 0 && cachedBits >= 11 ) {
  174. maxBits = GetMaxbits(tCurr[0]);
  175. cw = tCurr[(cache >> (32 - maxBits)) + 1];
  176. len = GetHLen(cw);
  177. if (!len) {
  178. cachedBits -= maxBits;
  179. cache <<= maxBits;
  180. tCurr += cw;
  181. continue;
  182. }
  183. cachedBits -= len;
  184. cache <<= len;
  185. x = GetCWX(cw);
  186. y = GetCWY(cw);
  187. if (x == 15 && tabType == loopLinbits) {
  188. minBits = linBits + 1 + (y ? 1 : 0);
  189. if (cachedBits + bitsLeft < minBits)
  190. return -1;
  191. while (cachedBits < minBits) {
  192. cache |= (unsigned int)(*buf++) << (24 - cachedBits);
  193. cachedBits += 8;
  194. bitsLeft -= 8;
  195. }
  196. if (bitsLeft < 0) {
  197. cachedBits += bitsLeft;
  198. bitsLeft = 0;
  199. cache &= (signed int)0x80000000 >> (cachedBits - 1);
  200. }
  201. x += (int)(cache >> (32 - linBits));
  202. cachedBits -= linBits;
  203. cache <<= linBits;
  204. }
  205. if (x) {ApplySign(x, cache); cache <<= 1; cachedBits--;}
  206. if (y == 15 && tabType == loopLinbits) {
  207. minBits = linBits + 1;
  208. if (cachedBits + bitsLeft < minBits)
  209. return -1;
  210. while (cachedBits < minBits) {
  211. cache |= (unsigned int)(*buf++) << (24 - cachedBits);
  212. cachedBits += 8;
  213. bitsLeft -= 8;
  214. }
  215. if (bitsLeft < 0) {
  216. cachedBits += bitsLeft;
  217. bitsLeft = 0;
  218. cache &= (signed int)0x80000000 >> (cachedBits - 1);
  219. }
  220. y += (int)(cache >> (32 - linBits));
  221. cachedBits -= linBits;
  222. cache <<= linBits;
  223. }
  224. if (y) {ApplySign(y, cache); cache <<= 1; cachedBits--;}
  225. /* ran out of bits - should never have consumed padBits */
  226. if (cachedBits < padBits)
  227. return -1;
  228. *xy++ = x;
  229. *xy++ = y;
  230. nVals -= 2;
  231. tCurr = tBase;
  232. }
  233. }
  234. bitsLeft += (cachedBits - padBits);
  235. return (startBits - bitsLeft);
  236. }
  237. /* error in bitstream - trying to access unused Huffman table */
  238. return -1;
  239. }
  240. /**************************************************************************************
  241. * Function: DecodeHuffmanQuads
  242. *
  243. * Description: decode 4-way vector Huffman codes in the "count1" region of spectrum
  244. *
  245. * Inputs: valid BitStreamInfo struct, pointing to start of quadword codes
  246. * pointer to vwxy buffer to received decoded values
  247. * maximum number of codewords to decode
  248. * index of quadword table (0 = table A, 1 = table B)
  249. * number of bits remaining in bitstream
  250. *
  251. * Outputs: quadruples of decoded coefficients in vwxy
  252. * updated BitStreamInfo struct
  253. *
  254. * Return: index of the first "zero_part" value (index of the first sample
  255. * of the quad word after which all samples are 0)
  256. *
  257. * Notes: si_huff.bit tests every vwxy output in both quad tables
  258. **************************************************************************************/
  259. static int DecodeHuffmanQuads(int *vwxy, int nVals, int tabIdx, int bitsLeft, unsigned char *buf, int bitOffset)
  260. {
  261. int i, v, w, x, y;
  262. int len, maxBits, cachedBits, padBits;
  263. unsigned int cache;
  264. unsigned char cw, *tBase;
  265. if (bitsLeft <= 0)
  266. return 0;
  267. tBase = (unsigned char *)quadTable + quadTabOffset[tabIdx];
  268. maxBits = quadTabMaxBits[tabIdx];
  269. /* initially fill cache with any partial byte */
  270. cache = 0;
  271. cachedBits = (8 - bitOffset) & 0x07;
  272. if (cachedBits)
  273. cache = (unsigned int)(*buf++) << (32 - cachedBits);
  274. bitsLeft -= cachedBits;
  275. i = padBits = 0;
  276. while (i < (nVals - 3)) {
  277. /* refill cache - assumes cachedBits <= 16 */
  278. if (bitsLeft >= 16) {
  279. /* load 2 new bytes into left-justified cache */
  280. cache |= (unsigned int)(*buf++) << (24 - cachedBits);
  281. cache |= (unsigned int)(*buf++) << (16 - cachedBits);
  282. cachedBits += 16;
  283. bitsLeft -= 16;
  284. } else {
  285. /* last time through, pad cache with zeros and drain cache */
  286. if (cachedBits + bitsLeft <= 0) return i;
  287. if (bitsLeft > 0) cache |= (unsigned int)(*buf++) << (24 - cachedBits);
  288. if (bitsLeft > 8) cache |= (unsigned int)(*buf++) << (16 - cachedBits);
  289. cachedBits += bitsLeft;
  290. bitsLeft = 0;
  291. cache &= (signed int)0x80000000 >> (cachedBits - 1);
  292. padBits = 10;
  293. cachedBits += padBits; /* okay if this is > 32 (0's automatically shifted in from right) */
  294. }
  295. /* largest maxBits = 6, plus 4 for sign bits, so make sure cache has at least 10 bits */
  296. while (i < (nVals - 3) && cachedBits >= 10 ) {
  297. cw = tBase[cache >> (32 - maxBits)];
  298. len = GetHLenQ(cw);
  299. cachedBits -= len;
  300. cache <<= len;
  301. v = GetCWVQ(cw); if(v) {ApplySign(v, cache); cache <<= 1; cachedBits--;}
  302. w = GetCWWQ(cw); if(w) {ApplySign(w, cache); cache <<= 1; cachedBits--;}
  303. x = GetCWXQ(cw); if(x) {ApplySign(x, cache); cache <<= 1; cachedBits--;}
  304. y = GetCWYQ(cw); if(y) {ApplySign(y, cache); cache <<= 1; cachedBits--;}
  305. /* ran out of bits - okay (means we're done) */
  306. if (cachedBits < padBits)
  307. return i;
  308. *vwxy++ = v;
  309. *vwxy++ = w;
  310. *vwxy++ = x;
  311. *vwxy++ = y;
  312. i += 4;
  313. }
  314. }
  315. /* decoded max number of quad values */
  316. return i;
  317. }
  318. /**************************************************************************************
  319. * Function: DecodeHuffman
  320. *
  321. * Description: decode one granule, one channel worth of Huffman codes
  322. *
  323. * Inputs: MP3DecInfo structure filled by UnpackFrameHeader(), UnpackSideInfo(),
  324. * and UnpackScaleFactors() (for this granule)
  325. * buffer pointing to start of Huffman data in MP3 frame
  326. * pointer to bit offset (0-7) indicating starting bit in buf[0]
  327. * number of bits in the Huffman data section of the frame
  328. * (could include padding bits)
  329. * index of current granule and channel
  330. *
  331. * Outputs: decoded coefficients in hi->huffDecBuf[ch] (hi pointer in mp3DecInfo)
  332. * updated bitOffset
  333. *
  334. * Return: length (in bytes) of Huffman codes
  335. * bitOffset also returned in parameter (0 = MSB, 7 = LSB of
  336. * byte located at buf + offset)
  337. * -1 if null input pointers, huffBlockBits < 0, or decoder runs
  338. * out of bits prematurely (invalid bitstream)
  339. **************************************************************************************/
  340. int DecodeHuffman(MP3DecInfo *mp3DecInfo, unsigned char *buf, int *bitOffset, int huffBlockBits, int gr, int ch)
  341. {
  342. int r1Start, r2Start, rEnd[4]; /* region boundaries */
  343. int i, w, bitsUsed, bitsLeft;
  344. unsigned char *startBuf = buf;
  345. FrameHeader *fh;
  346. SideInfo *si;
  347. SideInfoSub *sis;
  348. HuffmanInfo *hi;
  349. /* validate pointers */
  350. if (!mp3DecInfo || !mp3DecInfo->FrameHeaderPS || !mp3DecInfo->SideInfoPS || !mp3DecInfo->ScaleFactorInfoPS || !mp3DecInfo->HuffmanInfoPS)
  351. return -1;
  352. fh = ((FrameHeader *)(mp3DecInfo->FrameHeaderPS));
  353. si = ((SideInfo *)(mp3DecInfo->SideInfoPS));
  354. sis = &si->sis[gr][ch];
  355. hi = (HuffmanInfo*)(mp3DecInfo->HuffmanInfoPS);
  356. if (huffBlockBits < 0)
  357. return -1;
  358. /* figure out region boundaries (the first 2*bigVals coefficients divided into 3 regions) */
  359. if (sis->winSwitchFlag && sis->blockType == 2) {
  360. if (sis->mixedBlock == 0) {
  361. r1Start = fh->sfBand->s[(sis->region0Count + 1)/3] * 3;
  362. } else {
  363. if (fh->ver == MPEG1) {
  364. r1Start = fh->sfBand->l[sis->region0Count + 1];
  365. } else {
  366. /* see MPEG2 spec for explanation */
  367. w = fh->sfBand->s[4] - fh->sfBand->s[3];
  368. r1Start = fh->sfBand->l[6] + 2*w;
  369. }
  370. }
  371. r2Start = MAX_NSAMP; /* short blocks don't have region 2 */
  372. } else {
  373. r1Start = fh->sfBand->l[sis->region0Count + 1];
  374. r2Start = fh->sfBand->l[sis->region0Count + 1 + sis->region1Count + 1];
  375. }
  376. /* offset rEnd index by 1 so first region = rEnd[1] - rEnd[0], etc. */
  377. rEnd[3] = MIN(MAX_NSAMP, 2 * sis->nBigvals);
  378. rEnd[2] = MIN(r2Start, rEnd[3]);
  379. rEnd[1] = MIN(r1Start, rEnd[3]);
  380. rEnd[0] = 0;
  381. /* rounds up to first all-zero pair (we don't check last pair for (x,y) == (non-zero, zero)) */
  382. hi->nonZeroBound[ch] = rEnd[3];
  383. /* decode Huffman pairs (rEnd[i] are always even numbers) */
  384. bitsLeft = huffBlockBits;
  385. for (i = 0; i < 3; i++) {
  386. bitsUsed = DecodeHuffmanPairs(hi->huffDecBuf[ch] + rEnd[i], rEnd[i+1] - rEnd[i], sis->tableSelect[i], bitsLeft, buf, *bitOffset);
  387. if (bitsUsed < 0 || bitsUsed > bitsLeft) /* error - overran end of bitstream */
  388. return -1;
  389. /* update bitstream position */
  390. buf += (bitsUsed + *bitOffset) >> 3;
  391. *bitOffset = (bitsUsed + *bitOffset) & 0x07;
  392. bitsLeft -= bitsUsed;
  393. }
  394. /* decode Huffman quads (if any) */
  395. hi->nonZeroBound[ch] += DecodeHuffmanQuads(hi->huffDecBuf[ch] + rEnd[3], MAX_NSAMP - rEnd[3], sis->count1TableSelect, bitsLeft, buf, *bitOffset);
  396. ASSERT(hi->nonZeroBound[ch] <= MAX_NSAMP);
  397. for (i = hi->nonZeroBound[ch]; i < MAX_NSAMP; i++)
  398. hi->huffDecBuf[ch][i] = 0;
  399. /* If bits used for 576 samples < huffBlockBits, then the extras are considered
  400. * to be stuffing bits (throw away, but need to return correct bitstream position)
  401. */
  402. buf += (bitsLeft + *bitOffset) >> 3;
  403. *bitOffset = (bitsLeft + *bitOffset) & 0x07;
  404. return (buf - startBuf);
  405. }