scalfact.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. * June 2003
  39. *
  40. * scalfact.c - scalefactor unpacking functions
  41. **************************************************************************************/
  42. #include "coder.h"
  43. /* scale factor lengths (num bits) */
  44. static const char SFLenTab[16][2] = {
  45. {0, 0}, {0, 1},
  46. {0, 2}, {0, 3},
  47. {3, 0}, {1, 1},
  48. {1, 2}, {1, 3},
  49. {2, 1}, {2, 2},
  50. {2, 3}, {3, 1},
  51. {3, 2}, {3, 3},
  52. {4, 2}, {4, 3},
  53. };
  54. /**************************************************************************************
  55. * Function: UnpackSFMPEG1
  56. *
  57. * Description: unpack MPEG 1 scalefactors from bitstream
  58. *
  59. * Inputs: BitStreamInfo, SideInfoSub, ScaleFactorInfoSub structs for this
  60. * granule/channel
  61. * vector of scfsi flags from side info, length = 4 (MAX_SCFBD)
  62. * index of current granule
  63. * ScaleFactorInfoSub from granule 0 (for granule 1, if scfsi[i] is set,
  64. * then we just replicate the scale factors from granule 0 in the
  65. * i'th set of scalefactor bands)
  66. *
  67. * Outputs: updated BitStreamInfo struct
  68. * scalefactors in sfis (short and/or long arrays, as appropriate)
  69. *
  70. * Return: none
  71. *
  72. * Notes: set order of short blocks to s[band][window] instead of s[window][band]
  73. * so that we index through consectutive memory locations when unpacking
  74. * (make sure dequantizer follows same convention)
  75. * Illegal Intensity Position = 7 (always) for MPEG1 scale factors
  76. **************************************************************************************/
  77. static void UnpackSFMPEG1(BitStreamInfo *bsi, SideInfoSub *sis, ScaleFactorInfoSub *sfis, int *scfsi, int gr, ScaleFactorInfoSub *sfisGr0)
  78. {
  79. int sfb;
  80. int slen0, slen1;
  81. /* these can be 0, so make sure GetBits(bsi, 0) returns 0 (no >> 32 or anything) */
  82. slen0 = (int)SFLenTab[sis->sfCompress][0];
  83. slen1 = (int)SFLenTab[sis->sfCompress][1];
  84. if (sis->blockType == 2) {
  85. /* short block, type 2 (implies winSwitchFlag == 1) */
  86. if (sis->mixedBlock) {
  87. /* do long block portion */
  88. for (sfb = 0; sfb < 8; sfb++)
  89. sfis->l[sfb] = (char)GetBits(bsi, slen0);
  90. sfb = 3;
  91. } else {
  92. /* all short blocks */
  93. sfb = 0;
  94. }
  95. for ( ; sfb < 6; sfb++) {
  96. sfis->s[sfb][0] = (char)GetBits(bsi, slen0);
  97. sfis->s[sfb][1] = (char)GetBits(bsi, slen0);
  98. sfis->s[sfb][2] = (char)GetBits(bsi, slen0);
  99. }
  100. for ( ; sfb < 12; sfb++) {
  101. sfis->s[sfb][0] = (char)GetBits(bsi, slen1);
  102. sfis->s[sfb][1] = (char)GetBits(bsi, slen1);
  103. sfis->s[sfb][2] = (char)GetBits(bsi, slen1);
  104. }
  105. /* last sf band not transmitted */
  106. sfis->s[12][0] = sfis->s[12][1] = sfis->s[12][2] = 0;
  107. } else {
  108. /* long blocks, type 0, 1, or 3 */
  109. if(gr == 0) {
  110. /* first granule */
  111. for (sfb = 0; sfb < 11; sfb++)
  112. sfis->l[sfb] = (char)GetBits(bsi, slen0);
  113. for (sfb = 11; sfb < 21; sfb++)
  114. sfis->l[sfb] = (char)GetBits(bsi, slen1);
  115. return;
  116. } else {
  117. /* second granule
  118. * scfsi: 0 = different scalefactors for each granule, 1 = copy sf's from granule 0 into granule 1
  119. * for block type == 2, scfsi is always 0
  120. */
  121. sfb = 0;
  122. if(scfsi[0]) for( ; sfb < 6 ; sfb++) sfis->l[sfb] = sfisGr0->l[sfb];
  123. else for( ; sfb < 6 ; sfb++) sfis->l[sfb] = (char)GetBits(bsi, slen0);
  124. if(scfsi[1]) for( ; sfb <11 ; sfb++) sfis->l[sfb] = sfisGr0->l[sfb];
  125. else for( ; sfb <11 ; sfb++) sfis->l[sfb] = (char)GetBits(bsi, slen0);
  126. if(scfsi[2]) for( ; sfb <16 ; sfb++) sfis->l[sfb] = sfisGr0->l[sfb];
  127. else for( ; sfb <16 ; sfb++) sfis->l[sfb] = (char)GetBits(bsi, slen1);
  128. if(scfsi[3]) for( ; sfb <21 ; sfb++) sfis->l[sfb] = sfisGr0->l[sfb];
  129. else for( ; sfb <21 ; sfb++) sfis->l[sfb] = (char)GetBits(bsi, slen1);
  130. }
  131. /* last sf band not transmitted */
  132. sfis->l[21] = 0;
  133. sfis->l[22] = 0;
  134. }
  135. }
  136. /* NRTab[size + 3*is_right][block type][partition]
  137. * block type index: 0 = (bt0,bt1,bt3), 1 = bt2 non-mixed, 2 = bt2 mixed
  138. * partition: scale factor groups (sfb1 through sfb4)
  139. * for block type = 2 (mixed or non-mixed) / by 3 is rolled into this table
  140. * (for 3 short blocks per long block)
  141. * see 2.4.3.2 in MPEG 2 (low sample rate) spec
  142. * stuff rolled into this table:
  143. * NRTab[x][1][y] --> (NRTab[x][1][y]) / 3
  144. * NRTab[x][2][>=1] --> (NRTab[x][2][>=1]) / 3 (first partition is long block)
  145. */
  146. static const char NRTab[6][3][4] = {
  147. /* non-intensity stereo */
  148. { {6, 5, 5, 5},
  149. {3, 3, 3, 3}, /* includes / 3 */
  150. {6, 3, 3, 3}, /* includes / 3 except for first entry */
  151. },
  152. { {6, 5, 7, 3},
  153. {3, 3, 4, 2},
  154. {6, 3, 4, 2},
  155. },
  156. { {11, 10, 0, 0},
  157. {6, 6, 0, 0},
  158. {6, 3, 6, 0}, /* spec = [15,18,0,0], but 15 = 6L + 9S, so move 9/3=3 into col 1, 18/3=6 into col 2 and adj. slen[1,2] below */
  159. },
  160. /* intensity stereo, right chan */
  161. { {7, 7, 7, 0},
  162. {4, 4, 4, 0},
  163. {6, 5, 4, 0},
  164. },
  165. { {6, 6, 6, 3},
  166. {4, 3, 3, 2},
  167. {6, 4, 3, 2},
  168. },
  169. { {8, 8, 5, 0},
  170. {5, 4, 3, 0},
  171. {6, 6, 3, 0},
  172. }
  173. };
  174. /**************************************************************************************
  175. * Function: UnpackSFMPEG2
  176. *
  177. * Description: unpack MPEG 2 scalefactors from bitstream
  178. *
  179. * Inputs: BitStreamInfo, SideInfoSub, ScaleFactorInfoSub structs for this
  180. * granule/channel
  181. * index of current granule and channel
  182. * ScaleFactorInfoSub from this granule
  183. * modeExt field from frame header, to tell whether intensity stereo is on
  184. * ScaleFactorJS struct for storing IIP info used in Dequant()
  185. *
  186. * Outputs: updated BitStreamInfo struct
  187. * scalefactors in sfis (short and/or long arrays, as appropriate)
  188. * updated intensityScale and preFlag flags
  189. *
  190. * Return: none
  191. *
  192. * Notes: Illegal Intensity Position = (2^slen) - 1 for MPEG2 scale factors
  193. *
  194. * TODO: optimize the / and % stuff (only do one divide, get modulo x
  195. * with (x / m) * m, etc.)
  196. **************************************************************************************/
  197. static void UnpackSFMPEG2(BitStreamInfo *bsi, SideInfoSub *sis, ScaleFactorInfoSub *sfis, int gr, int ch, int modeExt, ScaleFactorJS *sfjs)
  198. {
  199. int i, sfb, sfcIdx, btIdx, nrIdx;// iipTest;
  200. int slen[4], nr[4];
  201. int sfCompress, preFlag, intensityScale;
  202. (void)gr;
  203. sfCompress = sis->sfCompress;
  204. preFlag = 0;
  205. intensityScale = 0;
  206. /* stereo mode bits (1 = on): bit 1 = mid-side on/off, bit 0 = intensity on/off */
  207. if (! ((modeExt & 0x01) && (ch == 1)) ) {
  208. /* in other words: if ((modeExt & 0x01) == 0 || ch == 0) */
  209. if (sfCompress < 400) {
  210. /* max slen = floor[(399/16) / 5] = 4 */
  211. slen[0] = (sfCompress >> 4) / 5;
  212. slen[1]= (sfCompress >> 4) % 5;
  213. slen[2]= (sfCompress & 0x0f) >> 2;
  214. slen[3]= (sfCompress & 0x03);
  215. sfcIdx = 0;
  216. } else if (sfCompress < 500) {
  217. /* max slen = floor[(99/4) / 5] = 4 */
  218. sfCompress -= 400;
  219. slen[0] = (sfCompress >> 2) / 5;
  220. slen[1]= (sfCompress >> 2) % 5;
  221. slen[2]= (sfCompress & 0x03);
  222. slen[3]= 0;
  223. sfcIdx = 1;
  224. } else {
  225. /* max slen = floor[11/3] = 3 (sfCompress = 9 bits in MPEG2) */
  226. sfCompress -= 500;
  227. slen[0] = sfCompress / 3;
  228. slen[1] = sfCompress % 3;
  229. slen[2] = slen[3] = 0;
  230. if (sis->mixedBlock) {
  231. /* adjust for long/short mix logic (see comment above in NRTab[] definition) */
  232. slen[2] = slen[1];
  233. slen[1] = slen[0];
  234. }
  235. preFlag = 1;
  236. sfcIdx = 2;
  237. }
  238. } else {
  239. /* intensity stereo ch = 1 (right) */
  240. intensityScale = sfCompress & 0x01;
  241. sfCompress >>= 1;
  242. if (sfCompress < 180) {
  243. /* max slen = floor[35/6] = 5 (from mod 36) */
  244. slen[0] = (sfCompress / 36);
  245. slen[1] = (sfCompress % 36) / 6;
  246. slen[2] = (sfCompress % 36) % 6;
  247. slen[3] = 0;
  248. sfcIdx = 3;
  249. } else if (sfCompress < 244) {
  250. /* max slen = floor[63/16] = 3 */
  251. sfCompress -= 180;
  252. slen[0] = (sfCompress & 0x3f) >> 4;
  253. slen[1] = (sfCompress & 0x0f) >> 2;
  254. slen[2] = (sfCompress & 0x03);
  255. slen[3] = 0;
  256. sfcIdx = 4;
  257. } else {
  258. /* max slen = floor[11/3] = 3 (max sfCompress >> 1 = 511/2 = 255) */
  259. sfCompress -= 244;
  260. slen[0] = (sfCompress / 3);
  261. slen[1] = (sfCompress % 3);
  262. slen[2] = slen[3] = 0;
  263. sfcIdx = 5;
  264. }
  265. }
  266. /* set index based on block type: (0,1,3) --> 0, (2 non-mixed) --> 1, (2 mixed) ---> 2 */
  267. btIdx = 0;
  268. if (sis->blockType == 2)
  269. btIdx = (sis->mixedBlock ? 2 : 1);
  270. for (i = 0; i < 4; i++)
  271. nr[i] = (int)NRTab[sfcIdx][btIdx][i];
  272. /* save intensity stereo scale factor info */
  273. if( (modeExt & 0x01) && (ch == 1) ) {
  274. for (i = 0; i < 4; i++) {
  275. sfjs->slen[i] = slen[i];
  276. sfjs->nr[i] = nr[i];
  277. }
  278. sfjs->intensityScale = intensityScale;
  279. }
  280. sis->preFlag = preFlag;
  281. /* short blocks */
  282. if(sis->blockType == 2) {
  283. if(sis->mixedBlock) {
  284. /* do long block portion */
  285. //iipTest = (1 << slen[0]) - 1;
  286. for (sfb=0; sfb < 6; sfb++) {
  287. sfis->l[sfb] = (char)GetBits(bsi, slen[0]);
  288. }
  289. sfb = 3; /* start sfb for short */
  290. nrIdx = 1;
  291. } else {
  292. /* all short blocks, so start nr, sfb at 0 */
  293. sfb = 0;
  294. nrIdx = 0;
  295. }
  296. /* remaining short blocks, sfb just keeps incrementing */
  297. for ( ; nrIdx <= 3; nrIdx++) {
  298. //iipTest = (1 << slen[nrIdx]) - 1;
  299. for (i=0; i < nr[nrIdx]; i++, sfb++) {
  300. sfis->s[sfb][0] = (char)GetBits(bsi, slen[nrIdx]);
  301. sfis->s[sfb][1] = (char)GetBits(bsi, slen[nrIdx]);
  302. sfis->s[sfb][2] = (char)GetBits(bsi, slen[nrIdx]);
  303. }
  304. }
  305. /* last sf band not transmitted */
  306. sfis->s[12][0] = sfis->s[12][1] = sfis->s[12][2] = 0;
  307. } else {
  308. /* long blocks */
  309. sfb = 0;
  310. for (nrIdx = 0; nrIdx <= 3; nrIdx++) {
  311. //iipTest = (1 << slen[nrIdx]) - 1;
  312. for(i=0; i < nr[nrIdx]; i++, sfb++) {
  313. sfis->l[sfb] = (char)GetBits(bsi, slen[nrIdx]);
  314. }
  315. }
  316. /* last sf band not transmitted */
  317. sfis->l[21] = sfis->l[22] = 0;
  318. }
  319. }
  320. /**************************************************************************************
  321. * Function: UnpackScaleFactors
  322. *
  323. * Description: parse the fields of the MP3 scale factor data section
  324. *
  325. * Inputs: MP3DecInfo structure filled by UnpackFrameHeader() and UnpackSideInfo()
  326. * buffer pointing to the MP3 scale factor data
  327. * pointer to bit offset (0-7) indicating starting bit in buf[0]
  328. * number of bits available in data buffer
  329. * index of current granule and channel
  330. *
  331. * Outputs: updated platform-specific ScaleFactorInfo struct
  332. * updated bitOffset
  333. *
  334. * Return: length (in bytes) of scale factor data, -1 if null input pointers
  335. **************************************************************************************/
  336. int UnpackScaleFactors(MP3DecInfo *mp3DecInfo, unsigned char *buf, int *bitOffset, int bitsAvail, int gr, int ch)
  337. {
  338. int bitsUsed;
  339. unsigned char *startBuf;
  340. BitStreamInfo bitStreamInfo, *bsi;
  341. FrameHeader *fh;
  342. SideInfo *si;
  343. ScaleFactorInfo *sfi;
  344. /* validate pointers */
  345. if (!mp3DecInfo || !mp3DecInfo->FrameHeaderPS || !mp3DecInfo->SideInfoPS || !mp3DecInfo->ScaleFactorInfoPS)
  346. return -1;
  347. fh = ((FrameHeader *)(mp3DecInfo->FrameHeaderPS));
  348. si = ((SideInfo *)(mp3DecInfo->SideInfoPS));
  349. sfi = ((ScaleFactorInfo *)(mp3DecInfo->ScaleFactorInfoPS));
  350. /* init GetBits reader */
  351. startBuf = buf;
  352. bsi = &bitStreamInfo;
  353. SetBitstreamPointer(bsi, (bitsAvail + *bitOffset + 7) / 8, buf);
  354. if (*bitOffset)
  355. GetBits(bsi, *bitOffset);
  356. if (fh->ver == MPEG1)
  357. UnpackSFMPEG1(bsi, &si->sis[gr][ch], &sfi->sfis[gr][ch], si->scfsi[ch], gr, &sfi->sfis[0][ch]);
  358. else
  359. UnpackSFMPEG2(bsi, &si->sis[gr][ch], &sfi->sfis[gr][ch], gr, ch, fh->modeExt, &sfi->sfjs);
  360. mp3DecInfo->part23Length[gr][ch] = si->sis[gr][ch].part23Length;
  361. bitsUsed = CalcBitsUsed(bsi, buf, *bitOffset);
  362. buf += (bitsUsed + *bitOffset) >> 3;
  363. *bitOffset = (bitsUsed + *bitOffset) & 0x07;
  364. return (buf - startBuf);
  365. }