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