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