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