2
0

sbr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Source last modified: $Id: sbr.c,v 1.3 2005/05/24 16:01:55 albertofloyd 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. * sbr.c - top level functions for SBR
  43. **************************************************************************************/
  44. #if defined(USE_DEFAULT_STDLIB) || defined(ESP_PLATFORM)
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #else
  48. #include "hlxclib/stdlib.h"
  49. #endif
  50. #include "sbr.h"
  51. /**************************************************************************************
  52. * Function: InitSBRState
  53. *
  54. * Description: initialize PSInfoSBR struct at start of stream or after flush
  55. *
  56. * Inputs: valid AACDecInfo struct
  57. *
  58. * Outputs: PSInfoSBR struct with proper initial state
  59. *
  60. * Return: none
  61. **************************************************************************************/
  62. static void InitSBRState(PSInfoSBR *psi)
  63. {
  64. int i, ch;
  65. unsigned char *c;
  66. if (!psi)
  67. return;
  68. /* clear SBR state structure */
  69. c = (unsigned char *)psi;
  70. for (i = 0; i < (int)sizeof(PSInfoSBR); i++)
  71. *c++ = 0;
  72. /* initialize non-zero state variables */
  73. for (ch = 0; ch < AAC_MAX_NCHANS; ch++) {
  74. psi->sbrChan[ch].reset = 1;
  75. psi->sbrChan[ch].laPrev = -1;
  76. }
  77. }
  78. /**************************************************************************************
  79. * Function: InitSBR
  80. *
  81. * Description: initialize SBR decoder
  82. *
  83. * Inputs: valid AACDecInfo struct
  84. *
  85. * Outputs: PSInfoSBR struct to hold SBR state information
  86. *
  87. * Return: 0 if successful, error code (< 0) if error
  88. *
  89. * Note: memory allocation for SBR is only done here
  90. **************************************************************************************/
  91. int InitSBR(AACDecInfo *aacDecInfo)
  92. {
  93. PSInfoSBR *psi;
  94. if (!aacDecInfo)
  95. return ERR_AAC_NULL_POINTER;
  96. /* allocate SBR state structure */
  97. psi = (PSInfoSBR *)malloc(sizeof(PSInfoSBR));
  98. if (!psi) {
  99. printf("OOM in SBR, can't allocate %d bytes\n", sizeof(PSInfoSBR));
  100. return ERR_AAC_SBR_INIT;
  101. }
  102. InitSBRState(psi);
  103. aacDecInfo->psInfoSBR = psi;
  104. return ERR_AAC_NONE;
  105. }
  106. int InitSBRPre(AACDecInfo *aacDecInfo, void **ptr, int *sz)
  107. {
  108. PSInfoSBR *psi;
  109. if (!aacDecInfo)
  110. return ERR_AAC_NULL_POINTER;
  111. /* allocate SBR state structure */
  112. psi = (PSInfoSBR *)*ptr;
  113. *sz -= sizeof(PSInfoSBR);
  114. if (*sz < 0) {
  115. printf("OOM in SBR, can't allocate %d bytes\n", sizeof(PSInfoSBR));
  116. return ERR_AAC_SBR_INIT;
  117. }
  118. InitSBRState(psi);
  119. *ptr = (void*)((char*)(*ptr) + sizeof(PSInfoSBR));
  120. aacDecInfo->psInfoSBR = psi;
  121. return ERR_AAC_NONE;
  122. }
  123. /**************************************************************************************
  124. * Function: FreeSBR
  125. *
  126. * Description: free SBR decoder
  127. *
  128. * Inputs: valid AACDecInfo struct
  129. *
  130. * Outputs: none
  131. *
  132. * Return: none
  133. *
  134. * Note: memory deallocation for SBR is only done here
  135. **************************************************************************************/
  136. void FreeSBR(AACDecInfo *aacDecInfo)
  137. {
  138. if (aacDecInfo && aacDecInfo->psInfoSBR)
  139. free(aacDecInfo->psInfoSBR);
  140. return;
  141. }
  142. /**************************************************************************************
  143. * Function: DecodeSBRBitstream
  144. *
  145. * Description: decode sideband information for SBR
  146. *
  147. * Inputs: valid AACDecInfo struct
  148. * fill buffer with SBR extension block
  149. * number of bytes in fill buffer
  150. * base output channel (range = [0, nChans-1])
  151. *
  152. * Outputs: initialized state structs (SBRHdr, SBRGrid, SBRFreq, SBRChan)
  153. *
  154. * Return: 0 if successful, error code (< 0) if error
  155. *
  156. * Notes: SBR payload should be in aacDecInfo->fillBuf
  157. * returns with no error if fill buffer is not an SBR extension block,
  158. * or if current block is not a fill block (e.g. for LFE upsampling)
  159. **************************************************************************************/
  160. int DecodeSBRBitstream(AACDecInfo *aacDecInfo, int chBase)
  161. {
  162. int headerFlag;
  163. BitStreamInfo bsi;
  164. PSInfoSBR *psi;
  165. /* validate pointers */
  166. if (!aacDecInfo || !aacDecInfo->psInfoSBR)
  167. return ERR_AAC_NULL_POINTER;
  168. psi = (PSInfoSBR *)(aacDecInfo->psInfoSBR);
  169. if (aacDecInfo->currBlockID != AAC_ID_FIL || (aacDecInfo->fillExtType != EXT_SBR_DATA && aacDecInfo->fillExtType != EXT_SBR_DATA_CRC))
  170. return ERR_AAC_NONE;
  171. SetBitstreamPointer(&bsi, aacDecInfo->fillCount, aacDecInfo->fillBuf);
  172. if (GetBits(&bsi, 4) != (unsigned int)aacDecInfo->fillExtType)
  173. return ERR_AAC_SBR_BITSTREAM;
  174. if (aacDecInfo->fillExtType == EXT_SBR_DATA_CRC)
  175. psi->crcCheckWord = GetBits(&bsi, 10);
  176. headerFlag = GetBits(&bsi, 1);
  177. if (headerFlag) {
  178. /* get sample rate index for output sample rate (2x base rate) */
  179. psi->sampRateIdx = GetSampRateIdx(2 * aacDecInfo->sampRate);
  180. if (psi->sampRateIdx < 0 || psi->sampRateIdx >= NUM_SAMPLE_RATES)
  181. return ERR_AAC_SBR_BITSTREAM;
  182. else if (psi->sampRateIdx >= NUM_SAMPLE_RATES_SBR)
  183. return ERR_AAC_SBR_SINGLERATE_UNSUPPORTED;
  184. /* reset flag = 1 if header values changed */
  185. if (UnpackSBRHeader(&bsi, &(psi->sbrHdr[chBase])))
  186. psi->sbrChan[chBase].reset = 1;
  187. /* first valid SBR header should always trigger CalcFreqTables(), since psi->reset was set in InitSBR() */
  188. if (psi->sbrChan[chBase].reset)
  189. CalcFreqTables(&(psi->sbrHdr[chBase+0]), &(psi->sbrFreq[chBase]), psi->sampRateIdx);
  190. /* copy and reset state to right channel for CPE */
  191. if (aacDecInfo->prevBlockID == AAC_ID_CPE)
  192. psi->sbrChan[chBase+1].reset = psi->sbrChan[chBase+0].reset;
  193. }
  194. /* if no header has been received, upsample only */
  195. if (psi->sbrHdr[chBase].count == 0)
  196. return ERR_AAC_NONE;
  197. if (aacDecInfo->prevBlockID == AAC_ID_SCE) {
  198. UnpackSBRSingleChannel(&bsi, psi, chBase);
  199. } else if (aacDecInfo->prevBlockID == AAC_ID_CPE) {
  200. UnpackSBRChannelPair(&bsi, psi, chBase);
  201. } else {
  202. return ERR_AAC_SBR_BITSTREAM;
  203. }
  204. ByteAlignBitstream(&bsi);
  205. return ERR_AAC_NONE;
  206. }
  207. /**************************************************************************************
  208. * Function: DecodeSBRData
  209. *
  210. * Description: apply SBR to one frame of PCM data
  211. *
  212. * Inputs: 1024 samples of decoded 32-bit PCM, before SBR
  213. * size of input PCM samples (must be 4 bytes)
  214. * number of fraction bits in input PCM samples
  215. * base output channel (range = [0, nChans-1])
  216. * initialized state structs (SBRHdr, SBRGrid, SBRFreq, SBRChan)
  217. *
  218. * Outputs: 2048 samples of decoded 16-bit PCM, after SBR
  219. *
  220. * Return: 0 if successful, error code (< 0) if error
  221. **************************************************************************************/
  222. int DecodeSBRData(AACDecInfo *aacDecInfo, int chBase, short *outbuf)
  223. {
  224. int k, l, ch, chBlock, qmfaBands, qmfsBands;
  225. int upsampleOnly, gbIdx, gbMask;
  226. int *inbuf;
  227. short *outptr;
  228. PSInfoSBR *psi;
  229. SBRHeader *sbrHdr;
  230. SBRGrid *sbrGrid;
  231. SBRFreq *sbrFreq;
  232. SBRChan *sbrChan;
  233. /* validate pointers */
  234. if (!aacDecInfo || !aacDecInfo->psInfoSBR)
  235. return ERR_AAC_NULL_POINTER;
  236. psi = (PSInfoSBR *)(aacDecInfo->psInfoSBR);
  237. /* same header and freq tables for both channels in CPE */
  238. sbrHdr = &(psi->sbrHdr[chBase]);
  239. sbrFreq = &(psi->sbrFreq[chBase]);
  240. /* upsample only if we haven't received an SBR header yet or if we have an LFE block */
  241. if (aacDecInfo->currBlockID == AAC_ID_LFE) {
  242. chBlock = 1;
  243. upsampleOnly = 1;
  244. } else if (aacDecInfo->currBlockID == AAC_ID_FIL) {
  245. if (aacDecInfo->prevBlockID == AAC_ID_SCE)
  246. chBlock = 1;
  247. else if (aacDecInfo->prevBlockID == AAC_ID_CPE)
  248. chBlock = 2;
  249. else
  250. return ERR_AAC_NONE;
  251. upsampleOnly = (sbrHdr->count == 0 ? 1 : 0);
  252. if (aacDecInfo->fillExtType != EXT_SBR_DATA && aacDecInfo->fillExtType != EXT_SBR_DATA_CRC)
  253. return ERR_AAC_NONE;
  254. } else {
  255. /* ignore non-SBR blocks */
  256. return ERR_AAC_NONE;
  257. }
  258. if (upsampleOnly) {
  259. sbrFreq->kStart = 32;
  260. sbrFreq->numQMFBands = 0;
  261. }
  262. for (ch = 0; ch < chBlock; ch++) {
  263. sbrGrid = &(psi->sbrGrid[chBase + ch]);
  264. sbrChan = &(psi->sbrChan[chBase + ch]);
  265. if (aacDecInfo->rawSampleBuf[ch] == 0 || aacDecInfo->rawSampleBytes != 4)
  266. return ERR_AAC_SBR_PCM_FORMAT;
  267. inbuf = (int *)aacDecInfo->rawSampleBuf[ch];
  268. outptr = outbuf + chBase + ch;
  269. /* restore delay buffers (could use ring buffer or keep in temp buffer for nChans == 1) */
  270. for (l = 0; l < HF_GEN; l++) {
  271. for (k = 0; k < 64; k++) {
  272. psi->XBuf[l][k][0] = psi->XBufDelay[chBase + ch][l][k][0];
  273. psi->XBuf[l][k][1] = psi->XBufDelay[chBase + ch][l][k][1];
  274. }
  275. }
  276. /* step 1 - analysis QMF */
  277. qmfaBands = sbrFreq->kStart;
  278. for (l = 0; l < 32; l++) {
  279. gbMask = QMFAnalysis(inbuf + l*32, psi->delayQMFA[chBase + ch], psi->XBuf[l + HF_GEN][0],
  280. aacDecInfo->rawSampleFBits, &(psi->delayIdxQMFA[chBase + ch]), qmfaBands);
  281. gbIdx = ((l + HF_GEN) >> 5) & 0x01;
  282. sbrChan->gbMask[gbIdx] |= gbMask; /* gbIdx = (0 if i < 32), (1 if i >= 32) */
  283. }
  284. if (upsampleOnly) {
  285. /* no SBR - just run synthesis QMF to upsample by 2x */
  286. qmfsBands = 32;
  287. for (l = 0; l < 32; l++) {
  288. /* step 4 - synthesis QMF */
  289. QMFSynthesis(psi->XBuf[l + HF_ADJ][0], psi->delayQMFS[chBase + ch], &(psi->delayIdxQMFS[chBase + ch]), qmfsBands, outptr, aacDecInfo->nChans);
  290. outptr += 64*aacDecInfo->nChans;
  291. }
  292. } else {
  293. /* if previous frame had lower SBR starting freq than current, zero out the synthesized QMF
  294. * bands so they aren't used as sources for patching
  295. * after patch generation, restore from delay buffer
  296. * can only happen after header reset
  297. */
  298. for (k = sbrFreq->kStartPrev; k < sbrFreq->kStart; k++) {
  299. for (l = 0; l < sbrGrid->envTimeBorder[0] + HF_ADJ; l++) {
  300. psi->XBuf[l][k][0] = 0;
  301. psi->XBuf[l][k][1] = 0;
  302. }
  303. }
  304. /* step 2 - HF generation */
  305. GenerateHighFreq(psi, sbrGrid, sbrFreq, sbrChan, ch);
  306. /* restore SBR bands that were cleared before patch generation (time slots 0, 1 no longer needed) */
  307. for (k = sbrFreq->kStartPrev; k < sbrFreq->kStart; k++) {
  308. for (l = HF_ADJ; l < sbrGrid->envTimeBorder[0] + HF_ADJ; l++) {
  309. psi->XBuf[l][k][0] = psi->XBufDelay[chBase + ch][l][k][0];
  310. psi->XBuf[l][k][1] = psi->XBufDelay[chBase + ch][l][k][1];
  311. }
  312. }
  313. /* step 3 - HF adjustment */
  314. AdjustHighFreq(psi, sbrHdr, sbrGrid, sbrFreq, sbrChan, ch);
  315. /* step 4 - synthesis QMF */
  316. qmfsBands = sbrFreq->kStartPrev + sbrFreq->numQMFBandsPrev;
  317. for (l = 0; l < sbrGrid->envTimeBorder[0]; l++) {
  318. /* if new envelope starts mid-frame, use old settings until start of first envelope in this frame */
  319. QMFSynthesis(psi->XBuf[l + HF_ADJ][0], psi->delayQMFS[chBase + ch], &(psi->delayIdxQMFS[chBase + ch]), qmfsBands, outptr, aacDecInfo->nChans);
  320. outptr += 64*aacDecInfo->nChans;
  321. }
  322. qmfsBands = sbrFreq->kStart + sbrFreq->numQMFBands;
  323. for ( ; l < 32; l++) {
  324. /* use new settings for rest of frame (usually the entire frame, unless the first envelope starts mid-frame) */
  325. QMFSynthesis(psi->XBuf[l + HF_ADJ][0], psi->delayQMFS[chBase + ch], &(psi->delayIdxQMFS[chBase + ch]), qmfsBands, outptr, aacDecInfo->nChans);
  326. outptr += 64*aacDecInfo->nChans;
  327. }
  328. }
  329. /* save delay */
  330. for (l = 0; l < HF_GEN; l++) {
  331. for (k = 0; k < 64; k++) {
  332. psi->XBufDelay[chBase + ch][l][k][0] = psi->XBuf[l+32][k][0];
  333. psi->XBufDelay[chBase + ch][l][k][1] = psi->XBuf[l+32][k][1];
  334. }
  335. }
  336. sbrChan->gbMask[0] = sbrChan->gbMask[1];
  337. sbrChan->gbMask[1] = 0;
  338. if (sbrHdr->count > 0)
  339. sbrChan->reset = 0;
  340. }
  341. sbrFreq->kStartPrev = sbrFreq->kStart;
  342. sbrFreq->numQMFBandsPrev = sbrFreq->numQMFBands;
  343. if (aacDecInfo->nChans > 0 && (chBase + ch) == aacDecInfo->nChans)
  344. psi->frameCount++;
  345. return ERR_AAC_NONE;
  346. }
  347. /**************************************************************************************
  348. * Function: FlushCodecSBR
  349. *
  350. * Description: flush internal SBR codec state (after seeking, for example)
  351. *
  352. * Inputs: valid AACDecInfo struct
  353. *
  354. * Outputs: updated state variables for SBR
  355. *
  356. * Return: 0 if successful, error code (< 0) if error
  357. *
  358. * Notes: SBR is heavily dependent on state from previous frames
  359. * (e.g. delta coded scalefactors, previous envelope boundaries, etc.)
  360. * On flush, we reset everything as if SBR had just been initialized
  361. * for the first time. This triggers "upsample-only" mode until
  362. * the first valid SBR header is received. Then SBR starts as usual.
  363. **************************************************************************************/
  364. int FlushCodecSBR(AACDecInfo *aacDecInfo)
  365. {
  366. PSInfoSBR *psi;
  367. /* validate pointers */
  368. if (!aacDecInfo || !aacDecInfo->psInfoSBR)
  369. return ERR_AAC_NULL_POINTER;
  370. psi = (PSInfoSBR *)(aacDecInfo->psInfoSBR);
  371. InitSBRState(psi);
  372. return 0;
  373. }