filefmt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: filefmt.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. * filefmt.c - ADIF and ADTS header decoding, raw block handling
  43. **************************************************************************************/
  44. #include "coder.h"
  45. /**************************************************************************************
  46. * Function: UnpackADTSHeader
  47. *
  48. * Description: parse the ADTS frame header and initialize decoder state
  49. *
  50. * Inputs: valid AACDecInfo struct
  51. * double pointer to buffer with complete ADTS frame header (byte aligned)
  52. * header size = 7 bytes, plus 2 if CRC
  53. *
  54. * Outputs: filled in ADTS struct
  55. * updated buffer pointer
  56. * updated bit offset
  57. * updated number of available bits
  58. *
  59. * Return: 0 if successful, error code (< 0) if error
  60. *
  61. * TODO: test CRC
  62. * verify that fixed fields don't change between frames
  63. **************************************************************************************/
  64. int UnpackADTSHeader(AACDecInfo *aacDecInfo, unsigned char **buf, int *bitOffset, int *bitsAvail)
  65. {
  66. int bitsUsed;
  67. PSInfoBase *psi;
  68. BitStreamInfo bsi;
  69. ADTSHeader *fhADTS;
  70. /* validate pointers */
  71. if (!aacDecInfo || !aacDecInfo->psInfoBase)
  72. return ERR_AAC_NULL_POINTER;
  73. psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
  74. fhADTS = &(psi->fhADTS);
  75. /* init bitstream reader */
  76. SetBitstreamPointer(&bsi, (*bitsAvail + 7) >> 3, *buf);
  77. GetBits(&bsi, *bitOffset);
  78. /* verify that first 12 bits of header are syncword */
  79. if (GetBits(&bsi, 12) != 0x0fff) {
  80. return ERR_AAC_INVALID_ADTS_HEADER;
  81. }
  82. /* fixed fields - should not change from frame to frame */
  83. fhADTS->id = GetBits(&bsi, 1);
  84. fhADTS->layer = GetBits(&bsi, 2);
  85. fhADTS->protectBit = GetBits(&bsi, 1);
  86. fhADTS->profile = GetBits(&bsi, 2);
  87. fhADTS->sampRateIdx = GetBits(&bsi, 4);
  88. fhADTS->privateBit = GetBits(&bsi, 1);
  89. fhADTS->channelConfig = GetBits(&bsi, 3);
  90. fhADTS->origCopy = GetBits(&bsi, 1);
  91. fhADTS->home = GetBits(&bsi, 1);
  92. /* variable fields - can change from frame to frame */
  93. fhADTS->copyBit = GetBits(&bsi, 1);
  94. fhADTS->copyStart = GetBits(&bsi, 1);
  95. fhADTS->frameLength = GetBits(&bsi, 13);
  96. fhADTS->bufferFull = GetBits(&bsi, 11);
  97. fhADTS->numRawDataBlocks = GetBits(&bsi, 2) + 1;
  98. /* note - MPEG4 spec, correction 1 changes how CRC is handled when protectBit == 0 and numRawDataBlocks > 1 */
  99. if (fhADTS->protectBit == 0)
  100. fhADTS->crcCheckWord = GetBits(&bsi, 16);
  101. /* byte align */
  102. ByteAlignBitstream(&bsi); /* should always be aligned anyway */
  103. /* check validity of header */
  104. if (fhADTS->layer != 0 || fhADTS->profile != AAC_PROFILE_LC ||
  105. fhADTS->sampRateIdx >= NUM_SAMPLE_RATES || fhADTS->channelConfig >= NUM_DEF_CHAN_MAPS)
  106. return ERR_AAC_INVALID_ADTS_HEADER;
  107. #ifndef AAC_ENABLE_MPEG4
  108. if (fhADTS->id != 1)
  109. return ERR_AAC_MPEG4_UNSUPPORTED;
  110. #endif
  111. /* update codec info */
  112. psi->sampRateIdx = fhADTS->sampRateIdx;
  113. if (!psi->useImpChanMap)
  114. psi->nChans = channelMapTab[fhADTS->channelConfig];
  115. /* syntactic element fields will be read from bitstream for each element */
  116. aacDecInfo->prevBlockID = AAC_ID_INVALID;
  117. aacDecInfo->currBlockID = AAC_ID_INVALID;
  118. aacDecInfo->currInstTag = -1;
  119. /* fill in user-accessible data (TODO - calc bitrate, handle tricky channel config cases) */
  120. aacDecInfo->bitRate = 0;
  121. aacDecInfo->nChans = psi->nChans;
  122. aacDecInfo->sampRate = sampRateTab[psi->sampRateIdx];
  123. aacDecInfo->profile = fhADTS->profile;
  124. aacDecInfo->sbrEnabled = 0;
  125. aacDecInfo->adtsBlocksLeft = fhADTS->numRawDataBlocks;
  126. /* update bitstream reader */
  127. bitsUsed = CalcBitsUsed(&bsi, *buf, *bitOffset);
  128. *buf += (bitsUsed + *bitOffset) >> 3;
  129. *bitOffset = (bitsUsed + *bitOffset) & 0x07;
  130. *bitsAvail -= bitsUsed ;
  131. if (*bitsAvail < 0)
  132. return ERR_AAC_INDATA_UNDERFLOW;
  133. return ERR_AAC_NONE;
  134. }
  135. /**************************************************************************************
  136. * Function: GetADTSChannelMapping
  137. *
  138. * Description: determine the number of channels from implicit mapping rules
  139. *
  140. * Inputs: valid AACDecInfo struct
  141. * pointer to start of raw_data_block
  142. * bit offset
  143. * bits available
  144. *
  145. * Outputs: updated number of channels
  146. *
  147. * Return: 0 if successful, error code (< 0) if error
  148. *
  149. * Notes: calculates total number of channels using rules in 14496-3, 4.5.1.2.1
  150. * does not attempt to deduce speaker geometry
  151. **************************************************************************************/
  152. int GetADTSChannelMapping(AACDecInfo *aacDecInfo, unsigned char *buf, int bitOffset, int bitsAvail)
  153. {
  154. int ch, nChans, elementChans, err;
  155. PSInfoBase *psi;
  156. /* validate pointers */
  157. if (!aacDecInfo || !aacDecInfo->psInfoBase)
  158. return ERR_AAC_NULL_POINTER;
  159. psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
  160. nChans = 0;
  161. do {
  162. /* parse next syntactic element */
  163. err = DecodeNextElement(aacDecInfo, &buf, &bitOffset, &bitsAvail);
  164. if (err)
  165. return err;
  166. elementChans = elementNumChans[aacDecInfo->currBlockID];
  167. nChans += elementChans;
  168. for (ch = 0; ch < elementChans; ch++) {
  169. err = DecodeNoiselessData(aacDecInfo, &buf, &bitOffset, &bitsAvail, ch);
  170. if (err)
  171. return err;
  172. }
  173. } while (aacDecInfo->currBlockID != AAC_ID_END);
  174. if (nChans <= 0)
  175. return ERR_AAC_CHANNEL_MAP;
  176. /* update number of channels in codec state and user-accessible info structs */
  177. psi->nChans = nChans;
  178. aacDecInfo->nChans = psi->nChans;
  179. psi->useImpChanMap = 1;
  180. return ERR_AAC_NONE;
  181. }
  182. /**************************************************************************************
  183. * Function: GetNumChannelsADIF
  184. *
  185. * Description: get number of channels from program config elements in an ADIF file
  186. *
  187. * Inputs: array of filled-in program config element structures
  188. * number of PCE's
  189. *
  190. * Outputs: none
  191. *
  192. * Return: total number of channels in file
  193. * -1 if error (invalid number of PCE's or unsupported mode)
  194. **************************************************************************************/
  195. static int GetNumChannelsADIF(ProgConfigElement *fhPCE, int nPCE)
  196. {
  197. int i, j, nChans;
  198. if (nPCE < 1 || nPCE > MAX_NUM_PCE_ADIF)
  199. return -1;
  200. nChans = 0;
  201. for (i = 0; i < nPCE; i++) {
  202. /* for now: only support LC, no channel coupling */
  203. if (fhPCE[i].profile != AAC_PROFILE_LC || fhPCE[i].numCCE > 0)
  204. return -1;
  205. /* add up number of channels in all channel elements (assume all single-channel) */
  206. nChans += fhPCE[i].numFCE;
  207. nChans += fhPCE[i].numSCE;
  208. nChans += fhPCE[i].numBCE;
  209. nChans += fhPCE[i].numLCE;
  210. /* add one more for every element which is a channel pair */
  211. for (j = 0; j < fhPCE[i].numFCE; j++) {
  212. if (CHAN_ELEM_IS_CPE(fhPCE[i].fce[j]))
  213. nChans++;
  214. }
  215. for (j = 0; j < fhPCE[i].numSCE; j++) {
  216. if (CHAN_ELEM_IS_CPE(fhPCE[i].sce[j]))
  217. nChans++;
  218. }
  219. for (j = 0; j < fhPCE[i].numBCE; j++) {
  220. if (CHAN_ELEM_IS_CPE(fhPCE[i].bce[j]))
  221. nChans++;
  222. }
  223. }
  224. return nChans;
  225. }
  226. /**************************************************************************************
  227. * Function: GetSampleRateIdxADIF
  228. *
  229. * Description: get sampling rate index from program config elements in an ADIF file
  230. *
  231. * Inputs: array of filled-in program config element structures
  232. * number of PCE's
  233. *
  234. * Outputs: none
  235. *
  236. * Return: sample rate of file
  237. * -1 if error (invalid number of PCE's or sample rate mismatch)
  238. **************************************************************************************/
  239. static int GetSampleRateIdxADIF(ProgConfigElement *fhPCE, int nPCE)
  240. {
  241. int i, idx;
  242. if (nPCE < 1 || nPCE > MAX_NUM_PCE_ADIF)
  243. return -1;
  244. /* make sure all PCE's have the same sample rate */
  245. idx = fhPCE[0].sampRateIdx;
  246. for (i = 1; i < nPCE; i++) {
  247. if (fhPCE[i].sampRateIdx != idx)
  248. return -1;
  249. }
  250. return idx;
  251. }
  252. /**************************************************************************************
  253. * Function: UnpackADIFHeader
  254. *
  255. * Description: parse the ADIF file header and initialize decoder state
  256. *
  257. * Inputs: valid AACDecInfo struct
  258. * double pointer to buffer with complete ADIF header
  259. * (starting at 'A' in 'ADIF' tag)
  260. * pointer to bit offset
  261. * pointer to number of valid bits remaining in inbuf
  262. *
  263. * Outputs: filled-in ADIF struct
  264. * updated buffer pointer
  265. * updated bit offset
  266. * updated number of available bits
  267. *
  268. * Return: 0 if successful, error code (< 0) if error
  269. **************************************************************************************/
  270. int UnpackADIFHeader(AACDecInfo *aacDecInfo, unsigned char **buf, int *bitOffset, int *bitsAvail)
  271. {
  272. int i, bitsUsed;
  273. PSInfoBase *psi;
  274. BitStreamInfo bsi;
  275. ADIFHeader *fhADIF;
  276. ProgConfigElement *pce;
  277. /* validate pointers */
  278. if (!aacDecInfo || !aacDecInfo->psInfoBase)
  279. return ERR_AAC_NULL_POINTER;
  280. psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
  281. /* init bitstream reader */
  282. SetBitstreamPointer(&bsi, (*bitsAvail + 7) >> 3, *buf);
  283. GetBits(&bsi, *bitOffset);
  284. /* unpack ADIF file header */
  285. fhADIF = &(psi->fhADIF);
  286. pce = psi->pce;
  287. /* verify that first 32 bits of header are "ADIF" */
  288. if (GetBits(&bsi, 8) != 'A' || GetBits(&bsi, 8) != 'D' || GetBits(&bsi, 8) != 'I' || GetBits(&bsi, 8) != 'F')
  289. return ERR_AAC_INVALID_ADIF_HEADER;
  290. /* read ADIF header fields */
  291. fhADIF->copyBit = GetBits(&bsi, 1);
  292. if (fhADIF->copyBit) {
  293. for (i = 0; i < ADIF_COPYID_SIZE; i++)
  294. fhADIF->copyID[i] = GetBits(&bsi, 8);
  295. }
  296. fhADIF->origCopy = GetBits(&bsi, 1);
  297. fhADIF->home = GetBits(&bsi, 1);
  298. fhADIF->bsType = GetBits(&bsi, 1);
  299. fhADIF->bitRate = GetBits(&bsi, 23);
  300. fhADIF->numPCE = GetBits(&bsi, 4) + 1; /* add 1 (so range = [1, 16]) */
  301. if (fhADIF->bsType == 0)
  302. fhADIF->bufferFull = GetBits(&bsi, 20);
  303. /* parse all program config elements */
  304. for (i = 0; i < fhADIF->numPCE; i++)
  305. DecodeProgramConfigElement(pce + i, &bsi);
  306. /* byte align */
  307. ByteAlignBitstream(&bsi);
  308. /* update codec info */
  309. psi->nChans = GetNumChannelsADIF(pce, fhADIF->numPCE);
  310. psi->sampRateIdx = GetSampleRateIdxADIF(pce, fhADIF->numPCE);
  311. /* check validity of header */
  312. if (psi->nChans < 0 || psi->sampRateIdx < 0 || psi->sampRateIdx >= NUM_SAMPLE_RATES)
  313. return ERR_AAC_INVALID_ADIF_HEADER;
  314. /* syntactic element fields will be read from bitstream for each element */
  315. aacDecInfo->prevBlockID = AAC_ID_INVALID;
  316. aacDecInfo->currBlockID = AAC_ID_INVALID;
  317. aacDecInfo->currInstTag = -1;
  318. /* fill in user-accessible data */
  319. aacDecInfo->bitRate = 0;
  320. aacDecInfo->nChans = psi->nChans;
  321. aacDecInfo->sampRate = sampRateTab[psi->sampRateIdx];
  322. aacDecInfo->profile = pce[0].profile;
  323. aacDecInfo->sbrEnabled = 0;
  324. /* update bitstream reader */
  325. bitsUsed = CalcBitsUsed(&bsi, *buf, *bitOffset);
  326. *buf += (bitsUsed + *bitOffset) >> 3;
  327. *bitOffset = (bitsUsed + *bitOffset) & 0x07;
  328. *bitsAvail -= bitsUsed ;
  329. if (*bitsAvail < 0)
  330. return ERR_AAC_INDATA_UNDERFLOW;
  331. return ERR_AAC_NONE;
  332. }
  333. /**************************************************************************************
  334. * Function: SetRawBlockParams
  335. *
  336. * Description: set internal state variables for decoding a stream of raw data blocks
  337. *
  338. * Inputs: valid AACDecInfo struct
  339. * flag indicating source of parameters (from previous headers or passed
  340. * explicitly by caller)
  341. * number of channels
  342. * sample rate
  343. * profile ID
  344. *
  345. * Outputs: updated state variables in aacDecInfo
  346. *
  347. * Return: 0 if successful, error code (< 0) if error
  348. *
  349. * Notes: if copyLast == 1, then psi->nChans, psi->sampRateIdx, and
  350. * aacDecInfo->profile are not changed (it's assumed that we already
  351. * set them, such as by a previous call to UnpackADTSHeader())
  352. * if copyLast == 0, then the parameters we passed in are used instead
  353. **************************************************************************************/
  354. int SetRawBlockParams(AACDecInfo *aacDecInfo, int copyLast, int nChans, int sampRate, int profile)
  355. {
  356. int idx;
  357. PSInfoBase *psi;
  358. /* validate pointers */
  359. if (!aacDecInfo || !aacDecInfo->psInfoBase)
  360. return ERR_AAC_NULL_POINTER;
  361. psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
  362. if (!copyLast) {
  363. aacDecInfo->profile = profile;
  364. psi->nChans = nChans;
  365. for (idx = 0; idx < NUM_SAMPLE_RATES; idx++) {
  366. if (sampRate == sampRateTab[idx]) {
  367. psi->sampRateIdx = idx;
  368. break;
  369. }
  370. }
  371. if (idx == NUM_SAMPLE_RATES)
  372. return ERR_AAC_INVALID_FRAME;
  373. }
  374. aacDecInfo->nChans = psi->nChans;
  375. aacDecInfo->sampRate = sampRateTab[psi->sampRateIdx];
  376. /* check validity of header */
  377. if (psi->sampRateIdx >= NUM_SAMPLE_RATES || psi->sampRateIdx < 0 || aacDecInfo->profile != AAC_PROFILE_LC)
  378. return ERR_AAC_RAWBLOCK_PARAMS;
  379. return ERR_AAC_NONE;
  380. }
  381. /**************************************************************************************
  382. * Function: PrepareRawBlock
  383. *
  384. * Description: reset per-block state variables for raw blocks (no ADTS/ADIF headers)
  385. *
  386. * Inputs: valid AACDecInfo struct
  387. *
  388. * Outputs: updated state variables in aacDecInfo
  389. *
  390. * Return: 0 if successful, error code (< 0) if error
  391. **************************************************************************************/
  392. int PrepareRawBlock(AACDecInfo *aacDecInfo)
  393. {
  394. // PSInfoBase *psi;
  395. /* validate pointers */
  396. if (!aacDecInfo || !aacDecInfo->psInfoBase)
  397. return ERR_AAC_NULL_POINTER;
  398. // psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
  399. /* syntactic element fields will be read from bitstream for each element */
  400. aacDecInfo->prevBlockID = AAC_ID_INVALID;
  401. aacDecInfo->currBlockID = AAC_ID_INVALID;
  402. aacDecInfo->currInstTag = -1;
  403. /* fill in user-accessible data */
  404. aacDecInfo->bitRate = 0;
  405. aacDecInfo->sbrEnabled = 0;
  406. return ERR_AAC_NONE;
  407. }
  408. /**************************************************************************************
  409. * Function: FlushCodec
  410. *
  411. * Description: flush internal codec state (after seeking, for example)
  412. *
  413. * Inputs: valid AACDecInfo struct
  414. *
  415. * Outputs: updated state variables in aacDecInfo
  416. *
  417. * Return: 0 if successful, error code (< 0) if error
  418. *
  419. * Notes: only need to clear data which is persistent between frames
  420. * (such as overlap buffer)
  421. **************************************************************************************/
  422. int FlushCodec(AACDecInfo *aacDecInfo)
  423. {
  424. PSInfoBase *psi;
  425. /* validate pointers */
  426. if (!aacDecInfo || !aacDecInfo->psInfoBase)
  427. return ERR_AAC_NULL_POINTER;
  428. psi = (PSInfoBase *)(aacDecInfo->psInfoBase);
  429. ClearBuffer(psi->overlap, AAC_MAX_NCHANS * AAC_MAX_NSAMPS * sizeof(int));
  430. ClearBuffer(psi->prevWinShape, AAC_MAX_NCHANS * sizeof(int));
  431. return ERR_AAC_NONE;
  432. }