aacdec.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: aacdec.c,v 1.1 2005/02/26 01:47:31 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), Ken Cooke (kenc@real.com)
  40. * February 2005
  41. *
  42. * aacdec.c - platform-independent top level decoder API
  43. **************************************************************************************/
  44. #include "aaccommon.h"
  45. //#include "profile.h"
  46. #define PROFILE_START(x)
  47. #define PROFILE_END()
  48. /**************************************************************************************
  49. * Function: AACInitDecoder
  50. *
  51. * Description: allocate memory for platform-specific data
  52. * clear all the user-accessible fields
  53. * initialize SBR decoder if enabled
  54. *
  55. * Inputs: none
  56. *
  57. * Outputs: none
  58. *
  59. * Return: handle to AAC decoder instance, 0 if malloc fails
  60. **************************************************************************************/
  61. HAACDecoder AACInitDecoder(void)
  62. {
  63. AACDecInfo *aacDecInfo;
  64. aacDecInfo = AllocateBuffers();
  65. if (!aacDecInfo)
  66. return 0;
  67. #ifdef AAC_ENABLE_SBR
  68. if (InitSBR(aacDecInfo)) {
  69. AACFreeDecoder(aacDecInfo);
  70. return 0;
  71. }
  72. #endif
  73. return (HAACDecoder)aacDecInfo;
  74. }
  75. HAACDecoder AACInitDecoderPre(void *ptr, int sz)
  76. {
  77. AACDecInfo *aacDecInfo;
  78. aacDecInfo = AllocateBuffersPre(&ptr, &sz);
  79. if (!aacDecInfo)
  80. return 0;
  81. #ifdef AAC_ENABLE_SBR
  82. if (InitSBRPre(aacDecInfo, &ptr, &sz)) {
  83. return 0;
  84. }
  85. #endif
  86. return (HAACDecoder)aacDecInfo;
  87. }
  88. /**************************************************************************************
  89. * Function: AACFreeDecoder
  90. *
  91. * Description: free platform-specific data allocated by AACInitDecoder
  92. * free SBR decoder if enabled
  93. *
  94. * Inputs: valid AAC decoder instance pointer (HAACDecoder)
  95. *
  96. * Outputs: none
  97. *
  98. * Return: none
  99. **************************************************************************************/
  100. void AACFreeDecoder(HAACDecoder hAACDecoder)
  101. {
  102. AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
  103. if (!aacDecInfo)
  104. return;
  105. #ifdef AAC_ENABLE_SBR
  106. FreeSBR(aacDecInfo);
  107. #endif
  108. FreeBuffers(aacDecInfo);
  109. }
  110. /**************************************************************************************
  111. * Function: AACFindSyncWord
  112. *
  113. * Description: locate the next byte-alinged sync word in the raw AAC stream
  114. *
  115. * Inputs: buffer to search for sync word
  116. * max number of bytes to search in buffer
  117. *
  118. * Outputs: none
  119. *
  120. * Return: offset to first sync word (bytes from start of buf)
  121. * -1 if sync not found after searching nBytes
  122. **************************************************************************************/
  123. int AACFindSyncWord(unsigned char *buf, int nBytes)
  124. {
  125. int i;
  126. /* find byte-aligned syncword (12 bits = 0xFFF) */
  127. for (i = 0; i < nBytes - 1; i++) {
  128. if ( (buf[i+0] & SYNCWORDH) == SYNCWORDH && (buf[i+1] & SYNCWORDL) == SYNCWORDL )
  129. return i;
  130. }
  131. return -1;
  132. }
  133. /**************************************************************************************
  134. * Function: AACGetLastFrameInfo
  135. *
  136. * Description: get info about last AAC frame decoded (number of samples decoded,
  137. * sample rate, bit rate, etc.)
  138. *
  139. * Inputs: valid AAC decoder instance pointer (HAACDecoder)
  140. * pointer to AACFrameInfo struct
  141. *
  142. * Outputs: filled-in AACFrameInfo struct
  143. *
  144. * Return: none
  145. *
  146. * Notes: call this right after calling AACDecode()
  147. **************************************************************************************/
  148. void AACGetLastFrameInfo(HAACDecoder hAACDecoder, AACFrameInfo *aacFrameInfo)
  149. {
  150. AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
  151. if (!aacDecInfo) {
  152. aacFrameInfo->bitRate = 0;
  153. aacFrameInfo->nChans = 0;
  154. aacFrameInfo->sampRateCore = 0;
  155. aacFrameInfo->sampRateOut = 0;
  156. aacFrameInfo->bitsPerSample = 0;
  157. aacFrameInfo->outputSamps = 0;
  158. aacFrameInfo->profile = 0;
  159. aacFrameInfo->tnsUsed = 0;
  160. aacFrameInfo->pnsUsed = 0;
  161. } else {
  162. aacFrameInfo->bitRate = aacDecInfo->bitRate;
  163. aacFrameInfo->nChans = aacDecInfo->nChans;
  164. aacFrameInfo->sampRateCore = aacDecInfo->sampRate;
  165. aacFrameInfo->sampRateOut = aacDecInfo->sampRate * (aacDecInfo->sbrEnabled ? 2 : 1);
  166. aacFrameInfo->bitsPerSample = 16;
  167. aacFrameInfo->outputSamps = aacDecInfo->nChans * AAC_MAX_NSAMPS * (aacDecInfo->sbrEnabled ? 2 : 1);
  168. aacFrameInfo->profile = aacDecInfo->profile;
  169. aacFrameInfo->tnsUsed = aacDecInfo->tnsUsed;
  170. aacFrameInfo->pnsUsed = aacDecInfo->pnsUsed;
  171. }
  172. }
  173. /**************************************************************************************
  174. * Function: AACSetRawBlockParams
  175. *
  176. * Description: set internal state variables for decoding a stream of raw data blocks
  177. *
  178. * Inputs: valid AAC decoder instance pointer (HAACDecoder)
  179. * flag indicating source of parameters
  180. * AACFrameInfo struct, with the members nChans, sampRate, and profile
  181. * optionally filled-in
  182. *
  183. * Outputs: updated codec state
  184. *
  185. * Return: 0 if successful, error code (< 0) if error
  186. *
  187. * Notes: if copyLast == 1, then the codec sets up its internal state (for
  188. * decoding raw blocks) based on previously-decoded ADTS header info
  189. * if copyLast == 0, then the codec uses the values passed in
  190. * aacFrameInfo to configure its internal state (useful when the
  191. * source is MP4 format, for example)
  192. **************************************************************************************/
  193. int AACSetRawBlockParams(HAACDecoder hAACDecoder, int copyLast, AACFrameInfo *aacFrameInfo)
  194. {
  195. AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
  196. if (!aacDecInfo)
  197. return ERR_AAC_NULL_POINTER;
  198. aacDecInfo->format = AAC_FF_RAW;
  199. if (copyLast)
  200. return SetRawBlockParams(aacDecInfo, 1, 0, 0, 0);
  201. else
  202. return SetRawBlockParams(aacDecInfo, 0, aacFrameInfo->nChans, aacFrameInfo->sampRateCore, aacFrameInfo->profile);
  203. }
  204. /**************************************************************************************
  205. * Function: AACFlushCodec
  206. *
  207. * Description: flush internal codec state (after seeking, for example)
  208. *
  209. * Inputs: valid AAC decoder instance pointer (HAACDecoder)
  210. *
  211. * Outputs: updated state variables in aacDecInfo
  212. *
  213. * Return: 0 if successful, error code (< 0) if error
  214. **************************************************************************************/
  215. int AACFlushCodec(HAACDecoder hAACDecoder)
  216. {
  217. int ch;
  218. AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
  219. if (!aacDecInfo)
  220. return ERR_AAC_NULL_POINTER;
  221. /* reset common state variables which change per-frame
  222. * don't touch state variables which are (usually) constant for entire clip
  223. * (nChans, sampRate, profile, format, sbrEnabled)
  224. */
  225. aacDecInfo->prevBlockID = AAC_ID_INVALID;
  226. aacDecInfo->currBlockID = AAC_ID_INVALID;
  227. aacDecInfo->currInstTag = -1;
  228. for (ch = 0; ch < MAX_NCHANS_ELEM; ch++)
  229. aacDecInfo->sbDeinterleaveReqd[ch] = 0;
  230. aacDecInfo->adtsBlocksLeft = 0;
  231. aacDecInfo->tnsUsed = 0;
  232. aacDecInfo->pnsUsed = 0;
  233. /* reset internal codec state (flush overlap buffers, etc.) */
  234. FlushCodec(aacDecInfo);
  235. #ifdef AAC_ENABLE_SBR
  236. FlushCodecSBR(aacDecInfo);
  237. #endif
  238. return ERR_AAC_NONE;
  239. }
  240. /**************************************************************************************
  241. * Function: AACDecode
  242. *
  243. * Description: decode AAC frame
  244. *
  245. * Inputs: valid AAC decoder instance pointer (HAACDecoder)
  246. * double pointer to buffer of AAC data
  247. * pointer to number of valid bytes remaining in inbuf
  248. * pointer to outbuf, big enough to hold one frame of decoded PCM samples
  249. * (outbuf must be double-sized if SBR enabled)
  250. *
  251. * Outputs: PCM data in outbuf, interleaved LRLRLR... if stereo
  252. * number of output samples = 1024 per channel (2048 if SBR enabled)
  253. * updated inbuf pointer
  254. * updated bytesLeft
  255. *
  256. * Return: 0 if successful, error code (< 0) if error
  257. *
  258. * Notes: inbuf pointer and bytesLeft are not updated until whole frame is
  259. * successfully decoded, so if ERR_AAC_INDATA_UNDERFLOW is returned
  260. * just call AACDecode again with more data in inbuf
  261. **************************************************************************************/
  262. int AACDecode(HAACDecoder hAACDecoder, unsigned char **inbuf, int *bytesLeft, short *outbuf)
  263. {
  264. int err, offset, bitOffset, bitsAvail;
  265. int ch, baseChan, elementChans;
  266. unsigned char *inptr;
  267. AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
  268. #ifdef AAC_ENABLE_SBR
  269. int baseChanSBR, elementChansSBR;
  270. #endif
  271. if (!aacDecInfo)
  272. return ERR_AAC_NULL_POINTER;
  273. /* make local copies (see "Notes" above) */
  274. inptr = *inbuf;
  275. bitOffset = 0;
  276. bitsAvail = (*bytesLeft) << 3;
  277. /* first time through figure out what the file format is */
  278. if (aacDecInfo->format == AAC_FF_Unknown) {
  279. if (bitsAvail < 32)
  280. return ERR_AAC_INDATA_UNDERFLOW;
  281. if (IS_ADIF(inptr)) {
  282. /* unpack ADIF header */
  283. aacDecInfo->format = AAC_FF_ADIF;
  284. err = UnpackADIFHeader(aacDecInfo, &inptr, &bitOffset, &bitsAvail);
  285. if (err)
  286. return err;
  287. } else {
  288. /* assume ADTS by default */
  289. aacDecInfo->format = AAC_FF_ADTS;
  290. }
  291. }
  292. /* if ADTS, search for start of next frame */
  293. if (aacDecInfo->format == AAC_FF_ADTS) {
  294. /* can have 1-4 raw data blocks per ADTS frame (header only present for first one) */
  295. if (aacDecInfo->adtsBlocksLeft == 0) {
  296. offset = AACFindSyncWord(inptr, bitsAvail >> 3);
  297. if (offset < 0)
  298. return ERR_AAC_INDATA_UNDERFLOW;
  299. inptr += offset;
  300. bitsAvail -= (offset << 3);
  301. err = UnpackADTSHeader(aacDecInfo, &inptr, &bitOffset, &bitsAvail);
  302. if (err)
  303. return err;
  304. if (aacDecInfo->nChans == -1) {
  305. /* figure out implicit channel mapping if necessary */
  306. err = GetADTSChannelMapping(aacDecInfo, inptr, bitOffset, bitsAvail);
  307. if (err)
  308. return err;
  309. }
  310. }
  311. aacDecInfo->adtsBlocksLeft--;
  312. } else if (aacDecInfo->format == AAC_FF_RAW) {
  313. err = PrepareRawBlock(aacDecInfo);
  314. if (err)
  315. return err;
  316. }
  317. /* check for valid number of channels */
  318. if (aacDecInfo->nChans > AAC_MAX_NCHANS || aacDecInfo->nChans <= 0)
  319. return ERR_AAC_NCHANS_TOO_HIGH;
  320. /* will be set later if active in this frame */
  321. aacDecInfo->tnsUsed = 0;
  322. aacDecInfo->pnsUsed = 0;
  323. bitOffset = 0;
  324. baseChan = 0;
  325. #ifdef AAC_ENABLE_SBR
  326. baseChanSBR = 0;
  327. #endif
  328. do {
  329. /* parse next syntactic element */
  330. err = DecodeNextElement(aacDecInfo, &inptr, &bitOffset, &bitsAvail);
  331. if (err)
  332. return err;
  333. elementChans = elementNumChans[aacDecInfo->currBlockID];
  334. if (baseChan + elementChans > AAC_MAX_NCHANS)
  335. return ERR_AAC_NCHANS_TOO_HIGH;
  336. /* noiseless decoder and dequantizer */
  337. for (ch = 0; ch < elementChans; ch++) {
  338. PROFILE_START("noiseless decoder");
  339. err = DecodeNoiselessData(aacDecInfo, &inptr, &bitOffset, &bitsAvail, ch);
  340. PROFILE_END();
  341. if (err)
  342. return err;
  343. PROFILE_START("dequant");
  344. if (Dequantize(aacDecInfo, ch))
  345. return ERR_AAC_DEQUANT;
  346. PROFILE_END();
  347. }
  348. PROFILE_START("mid-side and intensity stereo");
  349. /* mid-side and intensity stereo */
  350. if (aacDecInfo->currBlockID == AAC_ID_CPE) {
  351. if (StereoProcess(aacDecInfo))
  352. return ERR_AAC_STEREO_PROCESS;
  353. }
  354. PROFILE_END();
  355. /* PNS, TNS, inverse transform */
  356. for (ch = 0; ch < elementChans; ch++) {
  357. PROFILE_START("PNS");
  358. if (PNS(aacDecInfo, ch))
  359. return ERR_AAC_PNS;
  360. PROFILE_END();
  361. if (aacDecInfo->sbDeinterleaveReqd[ch]) {
  362. /* deinterleave short blocks, if required */
  363. if (DeinterleaveShortBlocks(aacDecInfo, ch))
  364. return ERR_AAC_SHORT_BLOCK_DEINT;
  365. aacDecInfo->sbDeinterleaveReqd[ch] = 0;
  366. }
  367. PROFILE_START("TNS");
  368. if (TNSFilter(aacDecInfo, ch))
  369. return ERR_AAC_TNS;
  370. PROFILE_END();
  371. PROFILE_START("IMDCT");
  372. if (IMDCT(aacDecInfo, ch, baseChan + ch, outbuf))
  373. return ERR_AAC_IMDCT;
  374. PROFILE_END();
  375. }
  376. #ifdef AAC_ENABLE_SBR
  377. if (aacDecInfo->sbrEnabled && (aacDecInfo->currBlockID == AAC_ID_FIL || aacDecInfo->currBlockID == AAC_ID_LFE)) {
  378. if (aacDecInfo->currBlockID == AAC_ID_LFE)
  379. elementChansSBR = elementNumChans[AAC_ID_LFE];
  380. else if (aacDecInfo->currBlockID == AAC_ID_FIL && (aacDecInfo->prevBlockID == AAC_ID_SCE || aacDecInfo->prevBlockID == AAC_ID_CPE))
  381. elementChansSBR = elementNumChans[aacDecInfo->prevBlockID];
  382. else
  383. elementChansSBR = 0;
  384. if (baseChanSBR + elementChansSBR > AAC_MAX_NCHANS)
  385. return ERR_AAC_SBR_NCHANS_TOO_HIGH;
  386. /* parse SBR extension data if present (contained in a fill element) */
  387. if (DecodeSBRBitstream(aacDecInfo, baseChanSBR))
  388. return ERR_AAC_SBR_BITSTREAM;
  389. /* apply SBR */
  390. if (DecodeSBRData(aacDecInfo, baseChanSBR, outbuf))
  391. return ERR_AAC_SBR_DATA;
  392. baseChanSBR += elementChansSBR;
  393. }
  394. #endif
  395. baseChan += elementChans;
  396. } while (aacDecInfo->currBlockID != AAC_ID_END);
  397. /* byte align after each raw_data_block */
  398. if (bitOffset) {
  399. inptr++;
  400. bitsAvail -= (8-bitOffset);
  401. bitOffset = 0;
  402. if (bitsAvail < 0)
  403. return ERR_AAC_INDATA_UNDERFLOW;
  404. }
  405. /* update pointers */
  406. aacDecInfo->frameCount++;
  407. *bytesLeft -= (inptr - *inbuf);
  408. *inbuf = inptr;
  409. return ERR_AAC_NONE;
  410. }