decelmnt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: decelmnt.c,v 1.1 2005/02/26 01:47:34 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. * decelmnt.c - syntactic element decoding
  43. **************************************************************************************/
  44. #include "coder.h"
  45. /**************************************************************************************
  46. * Function: DecodeSingleChannelElement
  47. *
  48. * Description: decode one SCE
  49. *
  50. * Inputs: BitStreamInfo struct pointing to start of SCE (14496-3, table 4.4.4)
  51. *
  52. * Outputs: updated element instance tag
  53. *
  54. * Return: 0 if successful, -1 if error
  55. *
  56. * Notes: doesn't decode individual channel stream (part of DecodeNoiselessData)
  57. **************************************************************************************/
  58. static int DecodeSingleChannelElement(AACDecInfo *aacDecInfo, BitStreamInfo *bsi)
  59. {
  60. /* validate pointers */
  61. if (!aacDecInfo || !aacDecInfo->psInfoBase)
  62. return -1;
  63. /* read instance tag */
  64. aacDecInfo->currInstTag = GetBits(bsi, NUM_INST_TAG_BITS);
  65. return 0;
  66. }
  67. /**************************************************************************************
  68. * Function: DecodeChannelPairElement
  69. *
  70. * Description: decode one CPE
  71. *
  72. * Inputs: BitStreamInfo struct pointing to start of CPE (14496-3, table 4.4.5)
  73. *
  74. * Outputs: updated element instance tag
  75. * updated commonWin
  76. * updated ICS info, if commonWin == 1
  77. * updated mid-side stereo info, if commonWin == 1
  78. *
  79. * Return: 0 if successful, -1 if error
  80. *
  81. * Notes: doesn't decode individual channel stream (part of DecodeNoiselessData)
  82. **************************************************************************************/
  83. static int DecodeChannelPairElement(AACDecInfo *aacDecInfo, BitStreamInfo *bsi)
  84. {
  85. int sfb, gp, maskOffset;
  86. unsigned char currBit, *maskPtr;
  87. PSInfoBase *psi;
  88. ICSInfo *icsInfo;
  89. /* validate pointers */
  90. if (!aacDecInfo || !aacDecInfo->psInfoBase)
  91. return -1;
  92. psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
  93. icsInfo = psi->icsInfo;
  94. /* read instance tag */
  95. aacDecInfo->currInstTag = GetBits(bsi, NUM_INST_TAG_BITS);
  96. /* read common window flag and mid-side info (if present)
  97. * store msMask bits in psi->msMaskBits[] as follows:
  98. * long blocks - pack bits for each SFB in range [0, maxSFB) starting with lsb of msMaskBits[0]
  99. * short blocks - pack bits for each SFB in range [0, maxSFB), for each group [0, 7]
  100. * msMaskPresent = 0 means no M/S coding
  101. * = 1 means psi->msMaskBits contains 1 bit per SFB to toggle M/S coding
  102. * = 2 means all SFB's are M/S coded (so psi->msMaskBits is not needed)
  103. */
  104. psi->commonWin = GetBits(bsi, 1);
  105. if (psi->commonWin) {
  106. DecodeICSInfo(bsi, icsInfo, psi->sampRateIdx);
  107. psi->msMaskPresent = GetBits(bsi, 2);
  108. if (psi->msMaskPresent == 1) {
  109. maskPtr = psi->msMaskBits;
  110. *maskPtr = 0;
  111. maskOffset = 0;
  112. for (gp = 0; gp < icsInfo->numWinGroup; gp++) {
  113. for (sfb = 0; sfb < icsInfo->maxSFB; sfb++) {
  114. currBit = (unsigned char)GetBits(bsi, 1);
  115. *maskPtr |= currBit << maskOffset;
  116. if (++maskOffset == 8) {
  117. maskPtr++;
  118. *maskPtr = 0;
  119. maskOffset = 0;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. return 0;
  126. }
  127. /**************************************************************************************
  128. * Function: DecodeLFEChannelElement
  129. *
  130. * Description: decode one LFE
  131. *
  132. * Inputs: BitStreamInfo struct pointing to start of LFE (14496-3, table 4.4.9)
  133. *
  134. * Outputs: updated element instance tag
  135. *
  136. * Return: 0 if successful, -1 if error
  137. *
  138. * Notes: doesn't decode individual channel stream (part of DecodeNoiselessData)
  139. **************************************************************************************/
  140. static int DecodeLFEChannelElement(AACDecInfo *aacDecInfo, BitStreamInfo *bsi)
  141. {
  142. /* validate pointers */
  143. if (!aacDecInfo || !aacDecInfo->psInfoBase)
  144. return -1;
  145. /* read instance tag */
  146. aacDecInfo->currInstTag = GetBits(bsi, NUM_INST_TAG_BITS);
  147. return 0;
  148. }
  149. /**************************************************************************************
  150. * Function: DecodeDataStreamElement
  151. *
  152. * Description: decode one DSE
  153. *
  154. * Inputs: BitStreamInfo struct pointing to start of DSE (14496-3, table 4.4.10)
  155. *
  156. * Outputs: updated element instance tag
  157. * filled in data stream buffer
  158. *
  159. * Return: 0 if successful, -1 if error
  160. **************************************************************************************/
  161. static int DecodeDataStreamElement(AACDecInfo *aacDecInfo, BitStreamInfo *bsi)
  162. {
  163. unsigned int byteAlign, dataCount;
  164. unsigned char *dataBuf;
  165. PSInfoBase *psi;
  166. /* validate pointers */
  167. if (!aacDecInfo || !aacDecInfo->psInfoBase)
  168. return -1;
  169. psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
  170. aacDecInfo->currInstTag = GetBits(bsi, NUM_INST_TAG_BITS);
  171. byteAlign = GetBits(bsi, 1);
  172. dataCount = GetBits(bsi, 8);
  173. if (dataCount == 255)
  174. dataCount += GetBits(bsi, 8);
  175. if (byteAlign)
  176. ByteAlignBitstream(bsi);
  177. psi->dataCount = dataCount;
  178. dataBuf = psi->dataBuf;
  179. while (dataCount--)
  180. *dataBuf++ = GetBits(bsi, 8);
  181. return 0;
  182. }
  183. /**************************************************************************************
  184. * Function: DecodeProgramConfigElement
  185. *
  186. * Description: decode one PCE
  187. *
  188. * Inputs: BitStreamInfo struct pointing to start of PCE (14496-3, table 4.4.2)
  189. *
  190. * Outputs: filled-in ProgConfigElement struct
  191. * updated BitStreamInfo struct
  192. *
  193. * Return: 0 if successful, error code (< 0) if error
  194. *
  195. * Notes: #define KEEP_PCE_COMMENTS to save the comment field of the PCE
  196. * (otherwise we just skip it in the bitstream, to save memory)
  197. **************************************************************************************/
  198. int DecodeProgramConfigElement(ProgConfigElement *pce, BitStreamInfo *bsi)
  199. {
  200. int i;
  201. pce->elemInstTag = GetBits(bsi, 4);
  202. pce->profile = GetBits(bsi, 2);
  203. pce->sampRateIdx = GetBits(bsi, 4);
  204. pce->numFCE = GetBits(bsi, 4);
  205. pce->numSCE = GetBits(bsi, 4);
  206. pce->numBCE = GetBits(bsi, 4);
  207. pce->numLCE = GetBits(bsi, 2);
  208. pce->numADE = GetBits(bsi, 3);
  209. pce->numCCE = GetBits(bsi, 4);
  210. pce->monoMixdown = GetBits(bsi, 1) << 4; /* present flag */
  211. if (pce->monoMixdown)
  212. pce->monoMixdown |= GetBits(bsi, 4); /* element number */
  213. pce->stereoMixdown = GetBits(bsi, 1) << 4; /* present flag */
  214. if (pce->stereoMixdown)
  215. pce->stereoMixdown |= GetBits(bsi, 4); /* element number */
  216. pce->matrixMixdown = GetBits(bsi, 1) << 4; /* present flag */
  217. if (pce->matrixMixdown) {
  218. pce->matrixMixdown |= GetBits(bsi, 2) << 1; /* index */
  219. pce->matrixMixdown |= GetBits(bsi, 1); /* pseudo-surround enable */
  220. }
  221. for (i = 0; i < pce->numFCE; i++) {
  222. pce->fce[i] = GetBits(bsi, 1) << 4; /* is_cpe flag */
  223. pce->fce[i] |= GetBits(bsi, 4); /* tag select */
  224. }
  225. for (i = 0; i < pce->numSCE; i++) {
  226. pce->sce[i] = GetBits(bsi, 1) << 4; /* is_cpe flag */
  227. pce->sce[i] |= GetBits(bsi, 4); /* tag select */
  228. }
  229. for (i = 0; i < pce->numBCE; i++) {
  230. pce->bce[i] = GetBits(bsi, 1) << 4; /* is_cpe flag */
  231. pce->bce[i] |= GetBits(bsi, 4); /* tag select */
  232. }
  233. for (i = 0; i < pce->numLCE; i++)
  234. pce->lce[i] = GetBits(bsi, 4); /* tag select */
  235. for (i = 0; i < pce->numADE; i++)
  236. pce->ade[i] = GetBits(bsi, 4); /* tag select */
  237. for (i = 0; i < pce->numCCE; i++) {
  238. pce->cce[i] = GetBits(bsi, 1) << 4; /* independent/dependent flag */
  239. pce->cce[i] |= GetBits(bsi, 4); /* tag select */
  240. }
  241. ByteAlignBitstream(bsi);
  242. #ifdef KEEP_PCE_COMMENTS
  243. pce->commentBytes = GetBits(bsi, 8);
  244. for (i = 0; i < pce->commentBytes; i++)
  245. pce->commentField[i] = GetBits(bsi, 8);
  246. #else
  247. /* eat comment bytes and throw away */
  248. i = GetBits(bsi, 8);
  249. while (i--)
  250. GetBits(bsi, 8);
  251. #endif
  252. return 0;
  253. }
  254. /**************************************************************************************
  255. * Function: DecodeFillElement
  256. *
  257. * Description: decode one fill element
  258. *
  259. * Inputs: BitStreamInfo struct pointing to start of fill element
  260. * (14496-3, table 4.4.11)
  261. *
  262. * Outputs: updated element instance tag
  263. * unpacked extension payload
  264. *
  265. * Return: 0 if successful, -1 if error
  266. **************************************************************************************/
  267. static int DecodeFillElement(AACDecInfo *aacDecInfo, BitStreamInfo *bsi)
  268. {
  269. unsigned int fillCount;
  270. unsigned char *fillBuf;
  271. PSInfoBase *psi;
  272. /* validate pointers */
  273. if (!aacDecInfo || !aacDecInfo->psInfoBase)
  274. return -1;
  275. psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
  276. fillCount = GetBits(bsi, 4);
  277. if (fillCount == 15)
  278. fillCount += (GetBits(bsi, 8) - 1);
  279. psi->fillCount = fillCount;
  280. fillBuf = psi->fillBuf;
  281. while (fillCount--)
  282. *fillBuf++ = GetBits(bsi, 8);
  283. aacDecInfo->currInstTag = -1; /* fill elements don't have instance tag */
  284. aacDecInfo->fillExtType = 0;
  285. #ifdef AAC_ENABLE_SBR
  286. /* check for SBR
  287. * aacDecInfo->sbrEnabled is sticky (reset each raw_data_block), so for multichannel
  288. * need to verify that all SCE/CPE/ICCE have valid SBR fill element following, and
  289. * must upsample by 2 for LFE
  290. */
  291. if (psi->fillCount > 0) {
  292. aacDecInfo->fillExtType = (int)((psi->fillBuf[0] >> 4) & 0x0f);
  293. if (aacDecInfo->fillExtType == EXT_SBR_DATA || aacDecInfo->fillExtType == EXT_SBR_DATA_CRC)
  294. aacDecInfo->sbrEnabled = 1;
  295. }
  296. #endif
  297. aacDecInfo->fillBuf = psi->fillBuf;
  298. aacDecInfo->fillCount = psi->fillCount;
  299. return 0;
  300. }
  301. /**************************************************************************************
  302. * Function: DecodeNextElement
  303. *
  304. * Description: decode next syntactic element in AAC frame
  305. *
  306. * Inputs: valid AACDecInfo struct
  307. * double pointer to buffer containing next element
  308. * pointer to bit offset
  309. * pointer to number of valid bits remaining in buf
  310. *
  311. * Outputs: type of element decoded (aacDecInfo->currBlockID)
  312. * type of element decoded last time (aacDecInfo->prevBlockID)
  313. * updated aacDecInfo state, depending on which element was decoded
  314. * updated buffer pointer
  315. * updated bit offset
  316. * updated number of available bits
  317. *
  318. * Return: 0 if successful, error code (< 0) if error
  319. **************************************************************************************/
  320. int DecodeNextElement(AACDecInfo *aacDecInfo, unsigned char **buf, int *bitOffset, int *bitsAvail)
  321. {
  322. int err, bitsUsed;
  323. PSInfoBase *psi;
  324. BitStreamInfo bsi;
  325. /* validate pointers */
  326. if (!aacDecInfo || !aacDecInfo->psInfoBase)
  327. return ERR_AAC_NULL_POINTER;
  328. psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
  329. /* init bitstream reader */
  330. SetBitstreamPointer(&bsi, (*bitsAvail + 7) >> 3, *buf);
  331. GetBits(&bsi, *bitOffset);
  332. /* read element ID (save last ID for SBR purposes) */
  333. aacDecInfo->prevBlockID = aacDecInfo->currBlockID;
  334. aacDecInfo->currBlockID = GetBits(&bsi, NUM_SYN_ID_BITS);
  335. /* set defaults (could be overwritten by DecodeXXXElement(), depending on currBlockID) */
  336. psi->commonWin = 0;
  337. err = 0;
  338. switch (aacDecInfo->currBlockID) {
  339. case AAC_ID_SCE:
  340. err = DecodeSingleChannelElement(aacDecInfo, &bsi);
  341. break;
  342. case AAC_ID_CPE:
  343. err = DecodeChannelPairElement(aacDecInfo, &bsi);
  344. break;
  345. case AAC_ID_CCE:
  346. /* TODO - implement CCE decoding */
  347. break;
  348. case AAC_ID_LFE:
  349. err = DecodeLFEChannelElement(aacDecInfo, &bsi);
  350. break;
  351. case AAC_ID_DSE:
  352. err = DecodeDataStreamElement(aacDecInfo, &bsi);
  353. break;
  354. case AAC_ID_PCE:
  355. err = DecodeProgramConfigElement(psi->pce + 0, &bsi);
  356. break;
  357. case AAC_ID_FIL:
  358. err = DecodeFillElement(aacDecInfo, &bsi);
  359. break;
  360. case AAC_ID_END:
  361. break;
  362. }
  363. if (err)
  364. return ERR_AAC_SYNTAX_ELEMENT;
  365. /* update bitstream reader */
  366. bitsUsed = CalcBitsUsed(&bsi, *buf, *bitOffset);
  367. *buf += (bitsUsed + *bitOffset) >> 3;
  368. *bitOffset = (bitsUsed + *bitOffset) & 0x07;
  369. *bitsAvail -= bitsUsed;
  370. if (*bitsAvail < 0)
  371. return ERR_AAC_INDATA_UNDERFLOW;
  372. return ERR_AAC_NONE;
  373. }