2
0

noiseless.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: noiseless.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. * noiseless.c - decode channel info, scalefactors, quantized coefficients,
  43. * scalefactor band codebook, and TNS coefficients from bitstream
  44. **************************************************************************************/
  45. #include "coder.h"
  46. //#include "profile.h"
  47. //#define PROFILE_START(x)
  48. //#define PROFILE_END()
  49. /**************************************************************************************
  50. * Function: DecodeICSInfo
  51. *
  52. * Description: decode individual channel stream info
  53. *
  54. * Inputs: BitStreamInfo struct pointing to start of ICS info
  55. * (14496-3, table 4.4.6)
  56. * sample rate index
  57. *
  58. * Outputs: updated icsInfo struct
  59. *
  60. * Return: none
  61. **************************************************************************************/
  62. /* __attribute__ ((section (".data"))) */ void DecodeICSInfo(BitStreamInfo *bsi, ICSInfo *icsInfo, int sampRateIdx)
  63. {
  64. int sfb, g, mask;
  65. icsInfo->icsResBit = GetBits(bsi, 1);
  66. icsInfo->winSequence = GetBits(bsi, 2);
  67. icsInfo->winShape = GetBits(bsi, 1);
  68. if (icsInfo->winSequence == 2) {
  69. /* short block */
  70. icsInfo->maxSFB = GetBits(bsi, 4);
  71. icsInfo->sfGroup = GetBits(bsi, 7);
  72. icsInfo->numWinGroup = 1;
  73. icsInfo->winGroupLen[0] = 1;
  74. mask = 0x40; /* start with bit 6 */
  75. for (g = 0; g < 7; g++) {
  76. if (icsInfo->sfGroup & mask) {
  77. icsInfo->winGroupLen[icsInfo->numWinGroup - 1]++;
  78. } else {
  79. icsInfo->numWinGroup++;
  80. icsInfo->winGroupLen[icsInfo->numWinGroup - 1] = 1;
  81. }
  82. mask >>= 1;
  83. }
  84. } else {
  85. /* long block */
  86. icsInfo->maxSFB = GetBits(bsi, 6);
  87. icsInfo->predictorDataPresent = GetBits(bsi, 1);
  88. if (icsInfo->predictorDataPresent) {
  89. icsInfo->predictorReset = GetBits(bsi, 1);
  90. if (icsInfo->predictorReset)
  91. icsInfo->predictorResetGroupNum = GetBits(bsi, 5);
  92. for (sfb = 0; sfb < MIN(icsInfo->maxSFB, predSFBMax[sampRateIdx]); sfb++)
  93. icsInfo->predictionUsed[sfb] = GetBits(bsi, 1);
  94. }
  95. icsInfo->numWinGroup = 1;
  96. icsInfo->winGroupLen[0] = 1;
  97. }
  98. }
  99. /**************************************************************************************
  100. * Function: DecodeSectionData
  101. *
  102. * Description: decode section data (scale factor band groupings and
  103. * associated Huffman codebooks)
  104. *
  105. * Inputs: BitStreamInfo struct pointing to start of ICS info
  106. * (14496-3, table 4.4.25)
  107. * window sequence (short or long blocks)
  108. * number of window groups (1 for long blocks, 1-8 for short blocks)
  109. * max coded scalefactor band
  110. *
  111. * Outputs: index of Huffman codebook for each scalefactor band in each section
  112. *
  113. * Return: none
  114. *
  115. * Notes: sectCB, sectEnd, sfbCodeBook, ordered by window groups for short blocks
  116. **************************************************************************************/
  117. /* __attribute__ ((section (".data"))) */ static void DecodeSectionData(BitStreamInfo *bsi, int winSequence, int numWinGrp, int maxSFB, unsigned char *sfbCodeBook)
  118. {
  119. int g, cb, sfb;
  120. int sectLen, sectLenBits, sectLenIncr, sectEscapeVal;
  121. sectLenBits = (winSequence == 2 ? 3 : 5);
  122. sectEscapeVal = (1 << sectLenBits) - 1;
  123. for (g = 0; g < numWinGrp; g++) {
  124. sfb = 0;
  125. while (sfb < maxSFB) {
  126. cb = GetBits(bsi, 4); /* next section codebook */
  127. sectLen = 0;
  128. do {
  129. sectLenIncr = GetBits(bsi, sectLenBits);
  130. sectLen += sectLenIncr;
  131. } while (sectLenIncr == sectEscapeVal);
  132. sfb += sectLen;
  133. while (sectLen--)
  134. *sfbCodeBook++ = (unsigned char)cb;
  135. }
  136. ASSERT(sfb == maxSFB);
  137. }
  138. }
  139. /**************************************************************************************
  140. * Function: DecodeOneScaleFactor
  141. *
  142. * Description: decode one scalefactor using scalefactor Huffman codebook
  143. *
  144. * Inputs: BitStreamInfo struct pointing to start of next coded scalefactor
  145. *
  146. * Outputs: updated BitstreamInfo struct
  147. *
  148. * Return: one decoded scalefactor, including index_offset of -60
  149. **************************************************************************************/
  150. static int DecodeOneScaleFactor(BitStreamInfo *bsi)
  151. {
  152. int nBits, val;
  153. unsigned int bitBuf;
  154. /* decode next scalefactor from bitstream */
  155. bitBuf = GetBitsNoAdvance(bsi, huffTabScaleFactInfo.maxBits) << (32 - huffTabScaleFactInfo.maxBits);
  156. //PROFILE_START("DecodeHuffmanScalar");
  157. nBits = DecodeHuffmanScalar(huffTabScaleFact, &huffTabScaleFactInfo, bitBuf, &val);
  158. AdvanceBitstream(bsi, nBits);
  159. //PROFILE_END();
  160. return val;
  161. }
  162. /**************************************************************************************
  163. * Function: DecodeScaleFactors
  164. *
  165. * Description: decode scalefactors, PNS energy, and intensity stereo weights
  166. *
  167. * Inputs: BitStreamInfo struct pointing to start of ICS info
  168. * (14496-3, table 4.4.26)
  169. * number of window groups (1 for long blocks, 1-8 for short blocks)
  170. * max coded scalefactor band
  171. * global gain (starting value for differential scalefactor coding)
  172. * index of Huffman codebook for each scalefactor band in each section
  173. *
  174. * Outputs: decoded scalefactor for each section
  175. *
  176. * Return: none
  177. *
  178. * Notes: sfbCodeBook, scaleFactors ordered by window groups for short blocks
  179. * for section with codebook 13, scaleFactors buffer has decoded PNS
  180. * energy instead of regular scalefactor
  181. * for section with codebook 14 or 15, scaleFactors buffer has intensity
  182. * stereo weight instead of regular scalefactor
  183. **************************************************************************************/
  184. /* __attribute__ ((section (".data"))) */ static void DecodeScaleFactors(BitStreamInfo *bsi, int numWinGrp, int maxSFB, int globalGain,
  185. unsigned char *sfbCodeBook, short *scaleFactors)
  186. {
  187. int g, sfbCB, nrg, npf, val, sf, is;
  188. /* starting values for differential coding */
  189. sf = globalGain;
  190. is = 0;
  191. nrg = globalGain - 90 - 256;
  192. npf = 1;
  193. for (g = 0; g < numWinGrp * maxSFB; g++) {
  194. sfbCB = *sfbCodeBook++;
  195. if (sfbCB == 14 || sfbCB == 15) {
  196. /* intensity stereo - differential coding */
  197. val = DecodeOneScaleFactor(bsi);
  198. is += val;
  199. *scaleFactors++ = (short)is;
  200. } else if (sfbCB == 13) {
  201. /* PNS - first energy is directly coded, rest are Huffman coded (npf = noise_pcm_flag) */
  202. if (npf) {
  203. val = GetBits(bsi, 9);
  204. npf = 0;
  205. } else {
  206. val = DecodeOneScaleFactor(bsi);
  207. }
  208. nrg += val;
  209. *scaleFactors++ = (short)nrg;
  210. } else if (sfbCB >= 1 && sfbCB <= 11) {
  211. /* regular (non-zero) region - differential coding */
  212. val = DecodeOneScaleFactor(bsi);
  213. sf += val;
  214. *scaleFactors++ = (short)sf;
  215. } else {
  216. /* inactive scalefactor band if codebook 0 */
  217. *scaleFactors++ = 0;
  218. }
  219. }
  220. }
  221. /**************************************************************************************
  222. * Function: DecodePulseInfo
  223. *
  224. * Description: decode pulse information
  225. *
  226. * Inputs: BitStreamInfo struct pointing to start of pulse info
  227. * (14496-3, table 4.4.7)
  228. *
  229. * Outputs: updated PulseInfo struct
  230. *
  231. * Return: none
  232. **************************************************************************************/
  233. /* __attribute__ ((section (".data"))) */ static void DecodePulseInfo(BitStreamInfo *bsi, PulseInfo *pi)
  234. {
  235. int i;
  236. pi->numPulse = GetBits(bsi, 2) + 1; /* add 1 here */
  237. pi->startSFB = GetBits(bsi, 6);
  238. for (i = 0; i < pi->numPulse; i++) {
  239. pi->offset[i] = GetBits(bsi, 5);
  240. pi->amp[i] = GetBits(bsi, 4);
  241. }
  242. }
  243. /**************************************************************************************
  244. * Function: DecodeTNSInfo
  245. *
  246. * Description: decode TNS filter information
  247. *
  248. * Inputs: BitStreamInfo struct pointing to start of TNS info
  249. * (14496-3, table 4.4.27)
  250. * window sequence (short or long blocks)
  251. *
  252. * Outputs: updated TNSInfo struct
  253. * buffer of decoded (signed) TNS filter coefficients
  254. *
  255. * Return: none
  256. **************************************************************************************/
  257. static const signed char sgnMask[3] = {0x02, 0x04, 0x08};
  258. static const signed char negMask[3] = {~0x03, ~0x07, ~0x0f};
  259. static void DecodeTNSInfo(BitStreamInfo *bsi, int winSequence, TNSInfo *ti, signed char *tnsCoef)
  260. {
  261. int i, w, f, coefBits, compress;
  262. signed char c, s, n;
  263. unsigned char *filtLength, *filtOrder, *filtDir;
  264. filtLength = ti->length;
  265. filtOrder = ti->order;
  266. filtDir = ti->dir;
  267. if (winSequence == 2) {
  268. /* short blocks */
  269. for (w = 0; w < NWINDOWS_SHORT; w++) {
  270. ti->numFilt[w] = GetBits(bsi, 1);
  271. if (ti->numFilt[w]) {
  272. ti->coefRes[w] = GetBits(bsi, 1) + 3;
  273. *filtLength = GetBits(bsi, 4);
  274. *filtOrder = GetBits(bsi, 3);
  275. if (*filtOrder) {
  276. *filtDir++ = GetBits(bsi, 1);
  277. compress = GetBits(bsi, 1);
  278. coefBits = (int)ti->coefRes[w] - compress; /* 2, 3, or 4 */
  279. s = sgnMask[coefBits - 2];
  280. n = negMask[coefBits - 2];
  281. for (i = 0; i < *filtOrder; i++) {
  282. c = GetBits(bsi, coefBits);
  283. if (c & s) c |= n;
  284. *tnsCoef++ = c;
  285. }
  286. }
  287. filtLength++;
  288. filtOrder++;
  289. }
  290. }
  291. } else {
  292. /* long blocks */
  293. ti->numFilt[0] = GetBits(bsi, 2);
  294. if (ti->numFilt[0])
  295. ti->coefRes[0] = GetBits(bsi, 1) + 3;
  296. for (f = 0; f < ti->numFilt[0]; f++) {
  297. *filtLength = GetBits(bsi, 6);
  298. *filtOrder = GetBits(bsi, 5);
  299. if (*filtOrder) {
  300. *filtDir++ = GetBits(bsi, 1);
  301. compress = GetBits(bsi, 1);
  302. coefBits = (int)ti->coefRes[0] - compress; /* 2, 3, or 4 */
  303. s = sgnMask[coefBits - 2];
  304. n = negMask[coefBits - 2];
  305. for (i = 0; i < *filtOrder; i++) {
  306. c = GetBits(bsi, coefBits);
  307. if (c & s) c |= n;
  308. *tnsCoef++ = c;
  309. }
  310. }
  311. filtLength++;
  312. filtOrder++;
  313. }
  314. }
  315. }
  316. /* bitstream field lengths for gain control data:
  317. * gainBits[winSequence][0] = maxWindow (how many gain windows there are)
  318. * gainBits[winSequence][1] = locBitsZero (bits for alocCode if window == 0)
  319. * gainBits[winSequence][2] = locBits (bits for alocCode if window != 0)
  320. */
  321. static const unsigned char gainBits[4][3] = {
  322. {1, 5, 5}, /* long */
  323. {2, 4, 2}, /* start */
  324. {8, 2, 2}, /* short */
  325. {2, 4, 5}, /* stop */
  326. };
  327. /**************************************************************************************
  328. * Function: DecodeGainControlInfo
  329. *
  330. * Description: decode gain control information (SSR profile only)
  331. *
  332. * Inputs: BitStreamInfo struct pointing to start of gain control info
  333. * (14496-3, table 4.4.12)
  334. * window sequence (short or long blocks)
  335. *
  336. * Outputs: updated GainControlInfo struct
  337. *
  338. * Return: none
  339. **************************************************************************************/
  340. static void DecodeGainControlInfo(BitStreamInfo *bsi, int winSequence, GainControlInfo *gi)
  341. {
  342. int bd, wd, ad;
  343. int locBits, locBitsZero, maxWin;
  344. gi->maxBand = GetBits(bsi, 2);
  345. maxWin = (int)gainBits[winSequence][0];
  346. locBitsZero = (int)gainBits[winSequence][1];
  347. locBits = (int)gainBits[winSequence][2];
  348. for (bd = 1; bd <= gi->maxBand; bd++) {
  349. for (wd = 0; wd < maxWin; wd++) {
  350. gi->adjNum[bd][wd] = GetBits(bsi, 3);
  351. for (ad = 0; ad < gi->adjNum[bd][wd]; ad++) {
  352. gi->alevCode[bd][wd][ad] = GetBits(bsi, 4);
  353. gi->alocCode[bd][wd][ad] = GetBits(bsi, (wd == 0 ? locBitsZero : locBits));
  354. }
  355. }
  356. }
  357. }
  358. /**************************************************************************************
  359. * Function: DecodeICS
  360. *
  361. * Description: decode individual channel stream
  362. *
  363. * Inputs: platform specific info struct
  364. * BitStreamInfo struct pointing to start of individual channel stream
  365. * (14496-3, table 4.4.24)
  366. * index of current channel
  367. *
  368. * Outputs: updated section data, scale factor data, pulse data, TNS data,
  369. * and gain control data
  370. *
  371. * Return: none
  372. **************************************************************************************/
  373. static void DecodeICS(PSInfoBase *psi, BitStreamInfo *bsi, int ch)
  374. {
  375. int globalGain;
  376. ICSInfo *icsInfo;
  377. PulseInfo *pi;
  378. TNSInfo *ti;
  379. GainControlInfo *gi;
  380. icsInfo = (ch == 1 && psi->commonWin == 1) ? &(psi->icsInfo[0]) : &(psi->icsInfo[ch]);
  381. globalGain = GetBits(bsi, 8);
  382. if (!psi->commonWin)
  383. DecodeICSInfo(bsi, icsInfo, psi->sampRateIdx);
  384. DecodeSectionData(bsi, icsInfo->winSequence, icsInfo->numWinGroup, icsInfo->maxSFB, psi->sfbCodeBook[ch]);
  385. DecodeScaleFactors(bsi, icsInfo->numWinGroup, icsInfo->maxSFB, globalGain, psi->sfbCodeBook[ch], psi->scaleFactors[ch]);
  386. pi = &psi->pulseInfo[ch];
  387. pi->pulseDataPresent = GetBits(bsi, 1);
  388. if (pi->pulseDataPresent)
  389. DecodePulseInfo(bsi, pi);
  390. ti = &psi->tnsInfo[ch];
  391. ti->tnsDataPresent = GetBits(bsi, 1);
  392. if (ti->tnsDataPresent)
  393. DecodeTNSInfo(bsi, icsInfo->winSequence, ti, ti->coef);
  394. gi = &psi->gainControlInfo[ch];
  395. gi->gainControlDataPresent = GetBits(bsi, 1);
  396. if (gi->gainControlDataPresent)
  397. DecodeGainControlInfo(bsi, icsInfo->winSequence, gi);
  398. }
  399. /**************************************************************************************
  400. * Function: DecodeNoiselessData
  401. *
  402. * Description: decode noiseless data (side info and transform coefficients)
  403. *
  404. * Inputs: valid AACDecInfo struct
  405. * double pointer to buffer pointing to start of individual channel stream
  406. * (14496-3, table 4.4.24)
  407. * pointer to bit offset
  408. * pointer to number of valid bits remaining in buf
  409. * index of current channel
  410. *
  411. * Outputs: updated global gain, section data, scale factor data, pulse data,
  412. * TNS data, gain control data, and spectral data
  413. *
  414. * Return: 0 if successful, error code (< 0) if error
  415. **************************************************************************************/
  416. int DecodeNoiselessData(AACDecInfo *aacDecInfo, unsigned char **buf, int *bitOffset, int *bitsAvail, int ch)
  417. {
  418. int bitsUsed;
  419. BitStreamInfo bsi;
  420. PSInfoBase *psi;
  421. ICSInfo *icsInfo;
  422. /* validate pointers */
  423. if (!aacDecInfo || !aacDecInfo->psInfoBase)
  424. return ERR_AAC_NULL_POINTER;
  425. psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
  426. icsInfo = (ch == 1 && psi->commonWin == 1) ? &(psi->icsInfo[0]) : &(psi->icsInfo[ch]);
  427. SetBitstreamPointer(&bsi, (*bitsAvail+7) >> 3, *buf);
  428. GetBits(&bsi, *bitOffset);
  429. DecodeICS(psi, &bsi, ch);
  430. if (icsInfo->winSequence == 2)
  431. DecodeSpectrumShort(psi, &bsi, ch);
  432. else
  433. DecodeSpectrumLong(psi, &bsi, ch);
  434. bitsUsed = CalcBitsUsed(&bsi, *buf, *bitOffset);
  435. *buf += ((bitsUsed + *bitOffset) >> 3);
  436. *bitOffset = ((bitsUsed + *bitOffset) & 0x07);
  437. *bitsAvail -= bitsUsed;
  438. aacDecInfo->sbDeinterleaveReqd[ch] = 0;
  439. aacDecInfo->tnsUsed |= psi->tnsInfo[ch].tnsDataPresent; /* set flag if TNS used for any channel */
  440. return ERR_AAC_NONE;
  441. }