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