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