1
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4
5 � Copyright 1995 - 2013 Fraunhofer-Gesellschaft zur F�rderung der angewandten Forschung e.V.
6 All rights reserved.
7
8 1. INTRODUCTION
9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
12
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
16 of the MPEG specifications.
17
18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
24
25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
27 applications information and documentation.
28
29 2. COPYRIGHT LICENSE
30
31 Redistribution and use in source and binary forms, with or without modification, are permitted without
32 payment of copyright license fees provided that you satisfy the following conditions:
33
34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
35 your modifications thereto in source code form.
36
37 You must retain the complete text of this software license in the documentation and/or other materials
38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
40 modifications thereto to recipients of copies in binary form.
41
42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
43 prior written permission.
44
45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
46 software or your modifications thereto.
47
48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
49 and the date of any change. For modified versions of the FDK AAC Codec, the term
50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
52
53 3. NO PATENT LICENSE
54
55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
57 respect to this software.
58
59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
60 by appropriate patent licenses.
61
62 4. DISCLAIMER
63
64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
69 or business interruption, however caused and on any theory of liability, whether in contract, strict
70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
71 advised of the possibility of such damage.
72
73 5. CONTACT INFORMATION
74
75 Fraunhofer Institute for Integrated Circuits IIS
76 Attention: Audio and Multimedia Departments - FDK AAC LL
77 Am Wolfsmantel 33
78 91058 Erlangen, Germany
79
80 www.iis.fraunhofer.de/amm
81 amm-info@iis.fraunhofer.de
82 ----------------------------------------------------------------------------------------------------------- */
83
84 /***************************** MPEG-4 AAC Decoder **************************
85
86 Author(s): Josef Hoepfl
87 Description:
88
89 ******************************************************************************/
90
91
92 /*!
93 \page default General Overview of the AAC Decoder Implementation
94
95 The main entry point to decode a AAC frame is CAacDecoder_DecodeFrame(). It handles the different
96 transport multiplexes and bitstream formats supported by this implementation. It extracts the
97 AAC_raw_data_blocks from these bitstreams to further process then in the actual decoding stages.
98
99 Note: Click on a function of file in the above image to see details about the function. Also note, that
100 this is just an overview of the most important functions and not a complete call graph.
101
102 <h2>1 Bitstream deformatter</h2>
103 The basic bit stream parser function CChannelElement_Read() is called. It uses other subcalls in order
104 to parse and unpack the bitstreams. Note, that this includes huffmann decoding of the coded spectral data.
105 This operation can be computational significant specifically at higher bitrates. Optimization is likely in
106 CBlock_ReadSpectralData().
107
108 The bitstream deformatter also includes many bitfield operations. Profiling on the target will determine
109 required optimizations.
110
111 <h2>2 Actual decoding to retain the time domain output</h2>
112 The basic bitstream deformatter function CChannelElement_Decode() for CPE elements and SCE elements are called.
113 Except for the stereo processing (2.1) which is only used for CPE elements, the function calls for CPE or SCE
114 are similar, except that CPE always processes to independent channels while SCE only processes one channel.
115
116 Often there is the distinction between long blocks and short blocks. However, computational expensive functions
117 that ususally require optimization are being shared by these two groups,
118
119 <h3>2.1 Stereo processing for CPE elements</h3>
120 CChannelPairElement_Decode() first calles the joint stereo tools in stereo.cpp when required.
121
122 <h3>2.2 Scaling of spectral data</h3>
123 CBlock_ScaleSpectralData().
124
125 <h3>2.3 Apply additional coding tools</h3>
126 ApplyTools() calles the PNS tools in case of MPEG-4 bitstreams, and TNS filtering CTns_Apply() for MPEG-2 and MPEG-4 bitstreams.
127 The function TnsFilterIIR() which is called by CTns_Apply() (2.3.1) might require some optimization.
128
129 <h2>3 Frequency-To-Time conversion</h3>
130 The filterbank is called using CBlock_FrequencyToTime() using the MDCT module from the FDK Tools
131
132 */
133
134
135
136 #include "aacdecoder.h"
137
138 #include "aac_rom.h"
139 #include "aac_ram.h"
140 #include "channel.h"
141 #include "FDK_audio.h"
142
143 #include "FDK_tools_rom.h"
144
145 #include "aacdec_pns.h"
146
147 #include "sbrdecoder.h"
148
149
150
151
152 #include "aacdec_hcr.h"
153 #include "rvlc.h"
154
155
156 #include "tpdec_lib.h"
157
158 #include "conceal.h"
159
160
161
162 #define CAN_DO_PS(aot) \
163 ((aot) == AOT_AAC_LC \
164 || (aot) == AOT_SBR \
165 || (aot) == AOT_PS \
166 || (aot) == AOT_ER_BSAC \
167 || (aot) == AOT_DRM_AAC)
168
169 #define IS_USAC(aot) \
170 ((aot) == AOT_USAC \
171 || (aot) == AOT_RSVD50)
172
173 #define IS_LOWDELAY(aot) \
174 ((aot) == AOT_ER_AAC_LD \
175 || (aot) == AOT_ER_AAC_ELD)
176
CAacDecoder_SyncQmfMode(HANDLE_AACDECODER self)177 void CAacDecoder_SyncQmfMode(HANDLE_AACDECODER self)
178 {
179
180 /* Assign user requested mode */
181 self->qmfModeCurr = self->qmfModeUser;
182
183 if ( self->qmfModeCurr == NOT_DEFINED )
184 {
185 if ( (IS_LOWDELAY(self->streamInfo.aot) && (self->flags & AC_MPS_PRESENT))
186 || ( (self->streamInfo.aacNumChannels == 1)
187 && ( (CAN_DO_PS(self->streamInfo.aot) && !(self->flags & AC_MPS_PRESENT))
188 || ( IS_USAC(self->streamInfo.aot) && (self->flags & AC_MPS_PRESENT)) ) ) )
189 {
190 self->qmfModeCurr = MODE_HQ;
191 } else {
192 self->qmfModeCurr = MODE_LP;
193 }
194 }
195
196
197 /* Set SBR to current QMF mode. Error does not matter. */
198 sbrDecoder_SetParam(self->hSbrDecoder, SBR_QMF_MODE, (self->qmfModeCurr == MODE_LP));
199 self->psPossible = ((CAN_DO_PS(self->streamInfo.aot) && self->streamInfo.aacNumChannels == 1 && ! (self->flags & AC_MPS_PRESENT))) && self->qmfModeCurr == MODE_HQ ;
200 FDK_ASSERT( ! ( (self->flags & AC_MPS_PRESENT) && self->psPossible ) );
201 }
202
CAacDecoder_SignalInterruption(HANDLE_AACDECODER self)203 void CAacDecoder_SignalInterruption(HANDLE_AACDECODER self)
204 {
205 }
206
207 /*!
208 \brief Reset ancillary data struct. Call before parsing a new frame.
209
210 \ancData Pointer to ancillary data structure
211
212 \return Error code
213 */
CAacDecoder_AncDataReset(CAncData * ancData)214 static AAC_DECODER_ERROR CAacDecoder_AncDataReset(CAncData *ancData)
215 {
216 int i;
217 for (i=0; i<8; i++)
218 {
219 ancData->offset[i] = 0;
220 }
221 ancData->nrElements = 0;
222
223 return AAC_DEC_OK;
224 }
225
226 /*!
227 \brief Initialize ancillary buffer
228
229 \ancData Pointer to ancillary data structure
230 \buffer Pointer to (external) anc data buffer
231 \size Size of the buffer pointed on by buffer in bytes
232
233 \return Error code
234 */
CAacDecoder_AncDataInit(CAncData * ancData,unsigned char * buffer,int size)235 AAC_DECODER_ERROR CAacDecoder_AncDataInit(CAncData *ancData, unsigned char *buffer, int size)
236 {
237 if (size >= 0) {
238 ancData->buffer = buffer;
239 ancData->bufferSize = size;
240
241 CAacDecoder_AncDataReset(ancData);
242
243 return AAC_DEC_OK;
244 }
245
246 return AAC_DEC_ANC_DATA_ERROR;
247 }
248
249 /*!
250 \brief Get one ancillary data element
251
252 \ancData Pointer to ancillary data structure
253 \index Index of the anc data element to get
254 \ptr Pointer to a buffer receiving a pointer to the requested anc data element
255 \size Pointer to a buffer receiving the length of the requested anc data element in bytes
256
257 \return Error code
258 */
CAacDecoder_AncDataGet(CAncData * ancData,int index,unsigned char ** ptr,int * size)259 AAC_DECODER_ERROR CAacDecoder_AncDataGet(CAncData *ancData, int index, unsigned char **ptr, int *size)
260 {
261 AAC_DECODER_ERROR error = AAC_DEC_OK;
262
263 *ptr = NULL;
264 *size = 0;
265
266 if (index >= 0 && index < 8 && index < ancData->nrElements)
267 {
268 *ptr = &ancData->buffer[ancData->offset[index]];
269 *size = ancData->offset[index+1] - ancData->offset[index];
270 }
271
272 return error;
273 }
274
275
276 /*!
277 \brief Parse ancillary data
278
279 \ancData Pointer to ancillary data structure
280 \hBs Handle to FDK bitstream
281 \ancBytes Length of ancillary data to read from the bitstream
282
283 \return Error code
284 */
285 static
CAacDecoder_AncDataParse(CAncData * ancData,HANDLE_FDK_BITSTREAM hBs,const int ancBytes)286 AAC_DECODER_ERROR CAacDecoder_AncDataParse (
287 CAncData *ancData,
288 HANDLE_FDK_BITSTREAM hBs,
289 const int ancBytes )
290 {
291 AAC_DECODER_ERROR error = AAC_DEC_OK;
292 int readBytes = 0;
293
294 if (ancData->buffer != NULL)
295 {
296 if (ancBytes > 0) {
297 /* write ancillary data to external buffer */
298 int offset = ancData->offset[ancData->nrElements];
299
300 if ((offset + ancBytes) > ancData->bufferSize)
301 {
302 error = AAC_DEC_TOO_SMALL_ANC_BUFFER;
303 }
304 else if (ancData->nrElements >= 8-1)
305 {
306 error = AAC_DEC_TOO_MANY_ANC_ELEMENTS;
307 }
308 else
309 {
310 int i;
311
312 for (i = 0; i < ancBytes; i++) {
313 ancData->buffer[i+offset] = FDKreadBits(hBs, 8);
314 readBytes++;
315 }
316
317 ancData->nrElements++;
318 ancData->offset[ancData->nrElements] = ancBytes + ancData->offset[ancData->nrElements-1];
319 }
320 }
321 }
322
323 readBytes = ancBytes - readBytes;
324
325 if (readBytes > 0) {
326 /* skip data */
327 FDKpushFor(hBs, readBytes<<3);
328 }
329
330 return error;
331 }
332
333 /*!
334 \brief Read Stream Data Element
335
336 \bs Bitstream Handle
337
338 \return Error code
339 */
CDataStreamElement_Read(HANDLE_AACDECODER self,HANDLE_FDK_BITSTREAM bs,UCHAR * elementInstanceTag,UINT alignmentAnchor)340 static AAC_DECODER_ERROR CDataStreamElement_Read (
341 HANDLE_AACDECODER self,
342 HANDLE_FDK_BITSTREAM bs,
343 UCHAR *elementInstanceTag,
344 UINT alignmentAnchor )
345 {
346 HANDLE_TRANSPORTDEC pTp;
347 CAncData *ancData;
348 AAC_DECODER_ERROR error = AAC_DEC_OK;
349 UINT dataStart, dseBits;
350 int dataByteAlignFlag, count;
351
352 FDK_ASSERT(self != NULL);
353
354 ancData = &self->ancData;
355 pTp = self->hInput;
356
357 int crcReg = transportDec_CrcStartReg(pTp, 0);
358
359 /* Element Instance Tag */
360 *elementInstanceTag = FDKreadBits(bs,4);
361 /* Data Byte Align Flag */
362 dataByteAlignFlag = FDKreadBits(bs,1);
363
364 count = FDKreadBits(bs,8);
365
366 if (count == 255) {
367 count += FDKreadBits(bs,8); /* EscCount */
368 }
369 dseBits = count*8;
370
371 if (dataByteAlignFlag) {
372 FDKbyteAlign(bs, alignmentAnchor);
373 }
374
375 dataStart = FDKgetValidBits(bs);
376
377 error = CAacDecoder_AncDataParse(ancData, bs, count);
378 transportDec_CrcEndReg(pTp, crcReg);
379
380 {
381 /* Move to the beginning of the data junk */
382 FDKpushBack(bs, dataStart-FDKgetValidBits(bs));
383
384 /* Read Anc data if available */
385 aacDecoder_drcMarkPayload( self->hDrcInfo, bs, DVB_DRC_ANC_DATA );
386 }
387
388 {
389 PCMDMX_ERROR dmxErr = PCMDMX_OK;
390
391 /* Move to the beginning of the data junk */
392 FDKpushBack(bs, dataStart-FDKgetValidBits(bs));
393
394 /* Read DMX meta-data */
395 dmxErr = pcmDmx_Parse (
396 self->hPcmUtils,
397 bs,
398 dseBits,
399 0 /* not mpeg2 */ );
400 }
401
402 /* Move to the very end of the element. */
403 FDKpushBiDirectional(bs, FDKgetValidBits(bs)-dataStart+dseBits);
404
405 return error;
406 }
407
408 #ifdef TP_PCE_ENABLE
409 /*!
410 \brief Read Program Config Element
411
412 \bs Bitstream Handle
413 \pTp Transport decoder handle for CRC handling
414 \pce Pointer to PCE buffer
415 \channelConfig Current channel configuration
416 \alignAnchor Anchor for byte alignment
417
418 \return PCE status (-1: fail, 0: no new PCE, 1: PCE updated, 2: PCE updated need re-config).
419 */
CProgramConfigElement_Read(HANDLE_FDK_BITSTREAM bs,HANDLE_TRANSPORTDEC pTp,CProgramConfig * pce,const UINT channelConfig,const UINT alignAnchor)420 static int CProgramConfigElement_Read (
421 HANDLE_FDK_BITSTREAM bs,
422 HANDLE_TRANSPORTDEC pTp,
423 CProgramConfig *pce,
424 const UINT channelConfig,
425 const UINT alignAnchor )
426 {
427 int pceStatus = 0;
428 int crcReg;
429
430 /* read PCE to temporal buffer first */
431 C_ALLOC_SCRATCH_START(tmpPce, CProgramConfig, 1);
432
433 CProgramConfig_Init(tmpPce);
434 CProgramConfig_Reset(tmpPce);
435
436 crcReg = transportDec_CrcStartReg(pTp, 0);
437
438 CProgramConfig_Read(tmpPce, bs, alignAnchor);
439
440 transportDec_CrcEndReg(pTp, crcReg);
441
442 if ( CProgramConfig_IsValid(tmpPce)
443 && (tmpPce->Profile == 1) )
444 {
445 if ( !pce->isValid && (channelConfig > 0) ) {
446 /* Create a standard channel config PCE to compare with */
447 CProgramConfig_GetDefault( pce, channelConfig );
448 }
449
450 if (pce->isValid) {
451 /* Compare the new and the old PCE (tags ignored) */
452 switch ( CProgramConfig_Compare( pce, tmpPce ) )
453 {
454 case 1: /* Channel configuration not changed. Just new metadata. */
455 FDKmemcpy(pce, tmpPce, sizeof(CProgramConfig)); /* Store the complete PCE */
456 pceStatus = 1; /* New PCE but no change of config */
457 break;
458 case 2: /* The number of channels are identical but not the config */
459 if (channelConfig == 0) {
460 FDKmemcpy(pce, tmpPce, sizeof(CProgramConfig)); /* Store the complete PCE */
461 pceStatus = 2; /* Decoder needs re-configuration */
462 }
463 break;
464 case -1: /* The channel configuration is completely different */
465 pceStatus = -1; /* Not supported! */
466 break;
467 case 0: /* Nothing to do because PCE matches the old one exactly. */
468 default:
469 /* pceStatus = 0; */
470 break;
471 }
472 }
473 }
474
475 C_ALLOC_SCRATCH_END(tmpPce, CProgramConfig, 1);
476
477 return pceStatus;
478 }
479 #endif /* TP_PCE_ENABLE */
480
481 /*!
482 \brief Parse Extension Payload
483
484 \self Handle of AAC decoder
485 \count Pointer to bit counter.
486 \previous_element ID of previous element (required by some extension payloads)
487
488 \return Error code
489 */
490 static
CAacDecoder_ExtPayloadParse(HANDLE_AACDECODER self,HANDLE_FDK_BITSTREAM hBs,int * count,MP4_ELEMENT_ID previous_element,int elIndex,int fIsFillElement)491 AAC_DECODER_ERROR CAacDecoder_ExtPayloadParse (HANDLE_AACDECODER self,
492 HANDLE_FDK_BITSTREAM hBs,
493 int *count,
494 MP4_ELEMENT_ID previous_element,
495 int elIndex,
496 int fIsFillElement)
497 {
498 AAC_DECODER_ERROR error = AAC_DEC_OK;
499 EXT_PAYLOAD_TYPE extension_type;
500 int bytes = (*count) >> 3;
501 int crcFlag = 0;
502
503 if (*count < 4) {
504 return AAC_DEC_PARSE_ERROR;
505 } else if ((INT)FDKgetValidBits(hBs) < *count) {
506 return AAC_DEC_DECODE_FRAME_ERROR;
507 }
508
509 extension_type = (EXT_PAYLOAD_TYPE) FDKreadBits(hBs, 4); /* bs_extension_type */
510 *count -= 4;
511
512 switch (extension_type)
513 {
514 case EXT_DYNAMIC_RANGE:
515 {
516 INT readBits = aacDecoder_drcMarkPayload( self->hDrcInfo, hBs, MPEG_DRC_EXT_DATA );
517
518 if (readBits > *count)
519 { /* Read too much. Something went wrong! */
520 error = AAC_DEC_PARSE_ERROR;
521 }
522 *count -= readBits;
523 }
524 break;
525
526
527 case EXT_SBR_DATA_CRC:
528 crcFlag = 1;
529 case EXT_SBR_DATA:
530 if (IS_CHANNEL_ELEMENT(previous_element)) {
531 SBR_ERROR sbrError;
532
533 CAacDecoder_SyncQmfMode(self);
534
535 sbrError = sbrDecoder_InitElement(
536 self->hSbrDecoder,
537 self->streamInfo.aacSampleRate,
538 self->streamInfo.extSamplingRate,
539 self->streamInfo.aacSamplesPerFrame,
540 self->streamInfo.aot,
541 previous_element,
542 elIndex
543 );
544
545 if (sbrError == SBRDEC_OK) {
546 sbrError = sbrDecoder_Parse (
547 self->hSbrDecoder,
548 hBs,
549 count,
550 *count,
551 crcFlag,
552 previous_element,
553 elIndex,
554 self->flags & AC_INDEP );
555 /* Enable SBR for implicit SBR signalling. */
556 if (sbrError == SBRDEC_OK) {
557 self->sbrEnabled = 1;
558 }
559 } else {
560 /* Do not try to apply SBR because initializing the element failed. */
561 self->sbrEnabled = 0;
562 }
563 /* Citation from ISO/IEC 14496-3 chapter 4.5.2.1.5.2
564 Fill elements containing an extension_payload() with an extension_type of EXT_SBR_DATA
565 or EXT_SBR_DATA_CRC shall not contain any other extension_payload of any other extension_type.
566 */
567 if (fIsFillElement) {
568 FDKpushBiDirectional(hBs, *count);
569 *count = 0;
570 } else {
571 /* If this is not a fill element with a known length, we are screwed an no further parsing makes sense. */
572 if (sbrError != SBRDEC_OK) {
573 self->frameOK = 0;
574 }
575 }
576 } else {
577 error = AAC_DEC_PARSE_ERROR;
578 }
579 break;
580
581 case EXT_FILL_DATA:
582 {
583 int temp;
584
585 temp = FDKreadBits(hBs,4);
586 bytes--;
587 if (temp != 0) {
588 error = AAC_DEC_PARSE_ERROR;
589 break;
590 }
591 while (bytes > 0) {
592 temp = FDKreadBits(hBs,8);
593 bytes--;
594 if (temp != 0xa5) {
595 error = AAC_DEC_PARSE_ERROR;
596 break;
597 }
598 }
599 *count = bytes<<3;
600 }
601 break;
602
603 case EXT_DATA_ELEMENT:
604 {
605 int dataElementVersion;
606
607 dataElementVersion = FDKreadBits(hBs,4);
608 *count -= 4;
609 if (dataElementVersion == 0) /* ANC_DATA */
610 {
611 int temp, dataElementLength = 0;
612 do {
613 temp = FDKreadBits(hBs,8);
614 *count -= 8;
615 dataElementLength += temp;
616 } while (temp == 255 );
617
618 CAacDecoder_AncDataParse(&self->ancData, hBs, dataElementLength);
619 *count -= (dataElementLength<<3);
620 } else {
621 /* align = 0 */
622 error = AAC_DEC_PARSE_ERROR;
623 goto bail;
624 }
625 }
626 break;
627
628 case EXT_DATA_LENGTH:
629 if ( !fIsFillElement /* Makes no sens to have an additional length in a fill ... */
630 && (self->flags & AC_ER) ) /* ... element because this extension payload type was ... */
631 { /* ... created to circumvent the missing length in ER-Syntax. */
632 int bitCnt, len = FDKreadBits(hBs, 4);
633 *count -= 4;
634
635 if (len == 15) {
636 int add_len = FDKreadBits(hBs, 8);
637 *count -= 8;
638 len += add_len;
639
640 if (add_len == 255) {
641 len += FDKreadBits(hBs, 16);
642 *count -= 16;
643 }
644 }
645 len <<= 3;
646 bitCnt = len;
647
648 if ( (EXT_PAYLOAD_TYPE)FDKreadBits(hBs, 4) == EXT_DATA_LENGTH ) {
649 /* Check NOTE 2: The extension_payload() included here must
650 not have extension_type == EXT_DATA_LENGTH. */
651 error = AAC_DEC_PARSE_ERROR;
652 } else {
653 /* rewind and call myself again. */
654 FDKpushBack(hBs, 4);
655
656 error =
657 CAacDecoder_ExtPayloadParse (
658 self,
659 hBs,
660 &bitCnt,
661 previous_element,
662 elIndex,
663 0 );
664
665 *count -= len - bitCnt;
666 }
667 /* Note: the fall through in case the if statement above is not taken is intentional. */
668 break;
669 }
670
671 case EXT_FIL:
672
673 default:
674 /* align = 4 */
675 FDKpushFor(hBs, *count);
676 *count = 0;
677 break;
678 }
679
680 bail:
681 if ( (error != AAC_DEC_OK)
682 && fIsFillElement )
683 { /* Skip the remaining extension bytes */
684 FDKpushBiDirectional(hBs, *count);
685 *count = 0;
686 /* Patch error code because decoding can go on. */
687 error = AAC_DEC_OK;
688 /* Be sure that parsing errors have been stored. */
689 }
690 return error;
691 }
692
693 /* Stream Configuration and Information.
694
695 This class holds configuration and information data for a stream to be decoded. It
696 provides the calling application as well as the decoder with substantial information,
697 e.g. profile, sampling rate, number of channels found in the bitstream etc.
698 */
699 static
CStreamInfoInit(CStreamInfo * pStreamInfo)700 void CStreamInfoInit(CStreamInfo *pStreamInfo)
701 {
702 pStreamInfo->aacSampleRate = 0;
703 pStreamInfo->profile = -1;
704 pStreamInfo->aot = AOT_NONE;
705
706 pStreamInfo->channelConfig = -1;
707 pStreamInfo->bitRate = 0;
708 pStreamInfo->aacSamplesPerFrame = 0;
709
710 pStreamInfo->extAot = AOT_NONE;
711 pStreamInfo->extSamplingRate = 0;
712
713 pStreamInfo->flags = 0;
714
715 pStreamInfo->epConfig = -1; /* default is no ER */
716
717 pStreamInfo->numChannels = 0;
718 pStreamInfo->sampleRate = 0;
719 pStreamInfo->frameSize = 0;
720
721 pStreamInfo->outputDelay = 0;
722
723 /* DRC */
724 pStreamInfo->drcProgRefLev = -1; /* set program reference level to not indicated */
725 pStreamInfo->drcPresMode = -1; /* default: presentation mode not indicated */
726 }
727
728 /*!
729 \brief Initialization of AacDecoderChannelInfo
730
731 The function initializes the pointers to AacDecoderChannelInfo for each channel,
732 set the start values for window shape and window sequence of overlap&add to zero,
733 set the overlap buffer to zero and initializes the pointers to the window coefficients.
734 \param bsFormat is the format of the AAC bitstream
735
736 \return AACDECODER instance
737 */
CAacDecoder_Open(TRANSPORT_TYPE bsFormat)738 LINKSPEC_CPP HANDLE_AACDECODER CAacDecoder_Open(TRANSPORT_TYPE bsFormat) /*!< bitstream format (adif,adts,loas,...). */
739 {
740 HANDLE_AACDECODER self;
741
742 self = GetAacDecoder();
743 if (self == NULL) {
744 goto bail;
745 }
746
747 /* Assign channel mapping info arrays (doing so removes dependency of settings header in API header). */
748 self->streamInfo.pChannelIndices = self->channelIndices;
749 self->streamInfo.pChannelType = self->channelType;
750
751 /* set default output mode */
752 self->outputInterleaved = 1; /* interleaved */
753
754 /* initialize anc data */
755 CAacDecoder_AncDataInit(&self->ancData, NULL, 0);
756
757 /* initialize stream info */
758 CStreamInfoInit(&self->streamInfo);
759
760 /* initialize error concealment common data */
761 CConcealment_InitCommonData(&self->concealCommonData);
762
763 self->hDrcInfo = GetDrcInfo();
764 if (self->hDrcInfo == NULL) {
765 goto bail;
766 }
767 /* Init common DRC structure */
768 aacDecoder_drcInit( self->hDrcInfo );
769 /* Set default frame delay */
770 aacDecoder_drcSetParam (
771 self->hDrcInfo,
772 DRC_BS_DELAY,
773 CConcealment_GetDelay(&self->concealCommonData)
774 );
775
776
777 self->aacCommonData.workBufferCore1 = GetWorkBufferCore1();
778 self->aacCommonData.workBufferCore2 = GetWorkBufferCore2();
779 if (self->aacCommonData.workBufferCore1 == NULL
780 ||self->aacCommonData.workBufferCore2 == NULL )
781 goto bail;
782
783 return self;
784
785 bail:
786 CAacDecoder_Close( self );
787
788 return NULL;
789 }
790
791 /* Destroy aac decoder */
CAacDecoder_Close(HANDLE_AACDECODER self)792 LINKSPEC_CPP void CAacDecoder_Close(HANDLE_AACDECODER self)
793 {
794 int ch;
795
796 if (self == NULL)
797 return;
798
799 for (ch=0; ch<(8); ch++) {
800 if (self->pAacDecoderStaticChannelInfo[ch] != NULL) {
801 if (self->pAacDecoderStaticChannelInfo[ch]->pOverlapBuffer != NULL) {
802 FreeOverlapBuffer (&self->pAacDecoderStaticChannelInfo[ch]->pOverlapBuffer);
803 }
804 if (self->pAacDecoderStaticChannelInfo[ch] != NULL) {
805 FreeAacDecoderStaticChannelInfo (&self->pAacDecoderStaticChannelInfo[ch]);
806 }
807 }
808 if (self->pAacDecoderChannelInfo[ch] != NULL) {
809 FreeAacDecoderChannelInfo (&self->pAacDecoderChannelInfo[ch]);
810 }
811 }
812
813 self->aacChannels = 0;
814
815 if (self->hDrcInfo) {
816 FreeDrcInfo(&self->hDrcInfo);
817 }
818
819 if (self->aacCommonData.workBufferCore1 != NULL) {
820 FreeWorkBufferCore1 (&self->aacCommonData.workBufferCore1);
821 }
822 if (self->aacCommonData.workBufferCore2 != NULL) {
823 FreeWorkBufferCore2 (&self->aacCommonData.workBufferCore2);
824 }
825
826 FreeAacDecoder ( &self);
827 }
828
829
830 /*!
831 \brief Initialization of decoder instance
832
833 The function initializes the decoder.
834
835 \return error status: 0 for success, <>0 for unsupported configurations
836 */
CAacDecoder_Init(HANDLE_AACDECODER self,const CSAudioSpecificConfig * asc)837 LINKSPEC_CPP AAC_DECODER_ERROR CAacDecoder_Init(HANDLE_AACDECODER self, const CSAudioSpecificConfig *asc)
838 {
839 AAC_DECODER_ERROR err = AAC_DEC_OK;
840 INT ascChannels, ch, ascChanged = 0;
841
842 if (!self)
843 return AAC_DEC_INVALID_HANDLE;
844
845 // set profile and check for supported aot
846 // leave profile on default (=-1) for all other supported MPEG-4 aot's except aot=2 (=AAC-LC)
847 switch (asc->m_aot) {
848 case AOT_AAC_LC:
849 self->streamInfo.profile = 1;
850 break;
851
852 case AOT_SBR:
853 case AOT_PS:
854 case AOT_ER_AAC_LD:
855 case AOT_ER_AAC_ELD:
856 break;
857
858 default:
859 return AAC_DEC_UNSUPPORTED_AOT;
860 }
861
862 CProgramConfig_Init(&self->pce);
863
864 /* set channels */
865 switch (asc->m_channelConfiguration) {
866 case 0:
867 #ifdef TP_PCE_ENABLE
868 /* get channels from program config (ASC) */
869 if (CProgramConfig_IsValid(&asc->m_progrConfigElement)) {
870 ascChannels = asc->m_progrConfigElement.NumChannels;
871 if (ascChannels > 0) {
872 int el;
873 /* valid number of channels -> copy program config element (PCE) from ASC */
874 FDKmemcpy(&self->pce, &asc->m_progrConfigElement, sizeof(CProgramConfig));
875 /* Built element table */
876 el = CProgramConfig_GetElementTable(&asc->m_progrConfigElement, self->elements, (8), &self->chMapIndex);
877 for (; el<(8); el++) {
878 self->elements[el] = ID_NONE;
879 }
880 } else {
881 return AAC_DEC_UNSUPPORTED_CHANNELCONFIG;
882 }
883 } else {
884 self->chMapIndex = 0;
885 if (transportDec_GetFormat(self->hInput) == TT_MP4_ADTS) {
886 /* set default max_channels for memory allocation because in implicit channel mapping mode
887 we don't know the actual number of channels until we processed at least one raw_data_block(). */
888 ascChannels = (8);
889 } else {
890 return AAC_DEC_UNSUPPORTED_CHANNELCONFIG;
891 }
892 }
893 #else /* TP_PCE_ENABLE */
894 return AAC_DEC_UNSUPPORTED_CHANNELCONFIG;
895 #endif /* TP_PCE_ENABLE */
896 break;
897 case 1: case 2: case 3: case 4: case 5: case 6:
898 ascChannels = asc->m_channelConfiguration;
899 break;
900 case 11:
901 ascChannels = 7;
902 break;
903 case 7: case 12: case 14:
904 ascChannels = 8;
905 break;
906 default:
907 return AAC_DEC_UNSUPPORTED_CHANNELCONFIG;
908 }
909
910 if (ascChannels > (8)) {
911 return AAC_DEC_UNSUPPORTED_CHANNELCONFIG;
912 }
913
914 /* Initialize constant mappings for channel config 1-7 */
915 if (asc->m_channelConfiguration > 0) {
916 int el;
917 FDKmemcpy(self->elements, elementsTab[asc->m_channelConfiguration-1], sizeof(MP4_ELEMENT_ID)*FDKmin(7,(8)));
918 for (el=7; el<(8); el++) {
919 self->elements[el] = ID_NONE;
920 }
921 for (ch=0; ch<ascChannels; ch++) {
922 self->chMapping[ch] = ch;
923 }
924 for (; ch<(8); ch++) {
925 self->chMapping[ch] = 255;
926 }
927 self->chMapIndex = asc->m_channelConfiguration;
928 }
929 #ifdef TP_PCE_ENABLE
930 else {
931 if (CProgramConfig_IsValid(&asc->m_progrConfigElement)) {
932 /* Set matrix mixdown infos if available from PCE. */
933 pcmDmx_SetMatrixMixdownFromPce ( self->hPcmUtils,
934 asc->m_progrConfigElement.MatrixMixdownIndexPresent,
935 asc->m_progrConfigElement.MatrixMixdownIndex,
936 asc->m_progrConfigElement.PseudoSurroundEnable );
937 }
938 }
939 #endif
940
941 self->streamInfo.channelConfig = asc->m_channelConfiguration;
942
943 if (self->streamInfo.aot != asc->m_aot) {
944 self->streamInfo.aot = asc->m_aot;
945 ascChanged = 1;
946 }
947
948 if (self->streamInfo.aacSamplesPerFrame != (INT)asc->m_samplesPerFrame) {
949 self->streamInfo.aacSamplesPerFrame = asc->m_samplesPerFrame;
950 ascChanged = 1;
951 }
952
953 self->streamInfo.bitRate = 0;
954
955 /* Set syntax flags */
956 self->flags = 0;
957
958 self->streamInfo.extAot = asc->m_extensionAudioObjectType;
959 self->streamInfo.extSamplingRate = asc->m_extensionSamplingFrequency;
960 self->flags |= (asc->m_sbrPresentFlag) ? AC_SBR_PRESENT : 0;
961 self->flags |= (asc->m_psPresentFlag) ? AC_PS_PRESENT : 0;
962 self->sbrEnabled = 0;
963
964 /* --------- vcb11 ------------ */
965 self->flags |= (asc->m_vcb11Flag) ? AC_ER_VCB11 : 0;
966
967 /* ---------- rvlc ------------ */
968 self->flags |= (asc->m_rvlcFlag) ? AC_ER_RVLC : 0;
969
970 /* ----------- hcr ------------ */
971 self->flags |= (asc->m_hcrFlag) ? AC_ER_HCR : 0;
972
973 if (asc->m_aot == AOT_ER_AAC_ELD) {
974 self->flags |= AC_ELD;
975 self->flags |= (asc->m_sc.m_eldSpecificConfig.m_sbrCrcFlag) ? AC_SBRCRC : 0;
976 self->flags |= (asc->m_sc.m_eldSpecificConfig.m_useLdQmfTimeAlign) ? AC_LD_MPS : 0;
977 }
978 self->flags |= (asc->m_aot == AOT_ER_AAC_LD) ? AC_LD : 0;
979 self->flags |= (asc->m_epConfig >= 0) ? AC_ER : 0;
980
981
982 if (asc->m_sbrPresentFlag) {
983 self->sbrEnabled = 1;
984 self->sbrEnabledPrev = 1;
985 }
986 if (asc->m_psPresentFlag) {
987 self->flags |= AC_PS_PRESENT;
988 }
989
990 if ( (asc->m_epConfig >= 0)
991 && (asc->m_channelConfiguration <= 0) ) {
992 /* we have to know the number of channels otherwise no decoding is possible */
993 return AAC_DEC_UNSUPPORTED_ER_FORMAT;
994 }
995
996 self->streamInfo.epConfig = asc->m_epConfig;
997 /* self->hInput->asc.m_epConfig = asc->m_epConfig; */
998
999 if (asc->m_epConfig > 1)
1000 return AAC_DEC_UNSUPPORTED_ER_FORMAT;
1001
1002 /* Check if samplerate changed. */
1003 if (self->streamInfo.aacSampleRate != (INT)asc->m_samplingFrequency) {
1004 AAC_DECODER_ERROR error;
1005
1006 ascChanged = 1;
1007
1008 /* Update samplerate info. */
1009 error = getSamplingRateInfo(&self->samplingRateInfo, asc->m_samplesPerFrame, asc->m_samplingFrequencyIndex, asc->m_samplingFrequency);
1010 if (error != AAC_DEC_OK) {
1011 return error;
1012 }
1013 self->streamInfo.aacSampleRate = self->samplingRateInfo.samplingRate;
1014 }
1015
1016 /* Check if amount of channels has changed. */
1017 if (self->ascChannels != ascChannels)
1018 {
1019 ascChanged = 1;
1020
1021 /* Allocate all memory structures for each channel */
1022 {
1023 for (ch = 0; ch < ascChannels; ch++) {
1024 CAacDecoderDynamicData *aacDecoderDynamicData = &self->aacCommonData.workBufferCore1->pAacDecoderDynamicData[ch%2];
1025
1026 /* initialize pointer to CAacDecoderChannelInfo */
1027 if (self->pAacDecoderChannelInfo[ch] == NULL) {
1028 self->pAacDecoderChannelInfo[ch] = GetAacDecoderChannelInfo(ch);
1029 /* This is temporary until the DynamicData is split into two or more regions!
1030 The memory could be reused after completed core decoding. */
1031 if (self->pAacDecoderChannelInfo[ch] == NULL) {
1032 goto bail;
1033 }
1034 /* Hook shared work memory into channel data structure */
1035 self->pAacDecoderChannelInfo[ch]->pDynData = aacDecoderDynamicData;
1036 self->pAacDecoderChannelInfo[ch]->pComData = &self->aacCommonData;
1037 }
1038
1039 /* Allocate persistent channel memory */
1040 if (self->pAacDecoderStaticChannelInfo[ch] == NULL) {
1041 self->pAacDecoderStaticChannelInfo[ch] = GetAacDecoderStaticChannelInfo(ch);
1042 if (self->pAacDecoderStaticChannelInfo[ch] == NULL) {
1043 goto bail;
1044 }
1045 self->pAacDecoderStaticChannelInfo[ch]->pOverlapBuffer = GetOverlapBuffer(ch); /* This area size depends on the AOT */
1046 if (self->pAacDecoderStaticChannelInfo[ch]->pOverlapBuffer == NULL) {
1047 goto bail;
1048 }
1049 self->pAacDecoderChannelInfo[ch]->pSpectralCoefficient = (SPECTRAL_PTR) &self->aacCommonData.workBufferCore2[ch*1024];
1050
1051 }
1052 CPns_InitPns(&self->pAacDecoderChannelInfo[ch]->data.aac.PnsData, &self->aacCommonData.pnsInterChannelData, &self->aacCommonData.pnsCurrentSeed, self->aacCommonData.pnsRandomSeed);
1053 }
1054
1055 if (ascChannels > self->aacChannels)
1056 {
1057 /* Make allocated channel count persistent in decoder context. */
1058 self->aacChannels = ascChannels;
1059 }
1060
1061 HcrInitRom(&self->aacCommonData.overlay.aac.erHcrInfo);
1062 setHcrType(&self->aacCommonData.overlay.aac.erHcrInfo, ID_SCE);
1063 }
1064
1065 /* Make amount of signalled channels persistent in decoder context. */
1066 self->ascChannels = ascChannels;
1067 }
1068
1069 /* Update structures */
1070 if (ascChanged) {
1071
1072 /* Things to be done for each channel, which do not involve allocating memory.
1073 Doing these things only on the channels needed for the current configuration
1074 (ascChannels) could lead to memory access violation later (error concealment). */
1075 for (ch = 0; ch < self->aacChannels; ch++) {
1076 switch (self->streamInfo.aot) {
1077 case AOT_ER_AAC_ELD:
1078 case AOT_ER_AAC_LD:
1079 self->pAacDecoderChannelInfo[ch]->granuleLength = self->streamInfo.aacSamplesPerFrame;
1080 break;
1081 default:
1082 self->pAacDecoderChannelInfo[ch]->granuleLength = self->streamInfo.aacSamplesPerFrame / 8;
1083 break;
1084 }
1085 mdct_init( &self->pAacDecoderStaticChannelInfo[ch]->IMdct,
1086 self->pAacDecoderStaticChannelInfo[ch]->pOverlapBuffer,
1087 OverlapBufferSize );
1088
1089
1090 /* Reset DRC control data for this channel */
1091 aacDecoder_drcInitChannelData ( &self->pAacDecoderStaticChannelInfo[ch]->drcData );
1092
1093 /* Reset concealment only if ASC changed. Otherwise it will be done with any config callback.
1094 E.g. every time the LATM SMC is present. */
1095 CConcealment_InitChannelData(&self->pAacDecoderStaticChannelInfo[ch]->concealmentInfo,
1096 &self->concealCommonData,
1097 self->streamInfo.aacSamplesPerFrame );
1098 }
1099 }
1100
1101 /* Update externally visible copy of flags */
1102 self->streamInfo.flags = self->flags;
1103
1104 return err;
1105
1106 bail:
1107 aacDecoder_Close( self );
1108 return AAC_DEC_OUT_OF_MEMORY;
1109 }
1110
1111
CAacDecoder_DecodeFrame(HANDLE_AACDECODER self,const UINT flags,INT_PCM * pTimeData,const INT timeDataSize,const INT interleaved)1112 LINKSPEC_CPP AAC_DECODER_ERROR CAacDecoder_DecodeFrame(
1113 HANDLE_AACDECODER self,
1114 const UINT flags,
1115 INT_PCM *pTimeData,
1116 const INT timeDataSize,
1117 const INT interleaved
1118 )
1119 {
1120 AAC_DECODER_ERROR ErrorStatus = AAC_DEC_OK;
1121
1122 CProgramConfig *pce;
1123 HANDLE_FDK_BITSTREAM bs = transportDec_GetBitstream(self->hInput, 0);
1124
1125 MP4_ELEMENT_ID type = ID_NONE; /* Current element type */
1126 INT aacChannels=0; /* Channel counter for channels found in the bitstream */
1127 int chOutMapIdx; /* Output channel mapping index (see comment below) */
1128
1129 INT auStartAnchor = (INT)FDKgetValidBits(bs); /* AU start bit buffer position for AU byte alignment */
1130
1131 self->frameOK = 1;
1132
1133 /* Any supported base layer valid AU will require more than 16 bits. */
1134 if ( (transportDec_GetAuBitsRemaining(self->hInput, 0) < 15) && (flags & (AACDEC_CONCEAL|AACDEC_FLUSH)) == 0) {
1135 self->frameOK = 0;
1136 ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR;
1137 }
1138
1139
1140 /* Reset Program Config structure */
1141 pce = &self->pce;
1142 CProgramConfig_Reset(pce);
1143
1144 CAacDecoder_AncDataReset(&self->ancData);
1145
1146 {
1147 int ch;
1148
1149 if (self->streamInfo.channelConfig == 0) {
1150 /* Init Channel/Element mapping table */
1151 for (ch=0; ch<(8); ch++) {
1152 self->chMapping[ch] = 255;
1153 }
1154 if (!CProgramConfig_IsValid(pce)) {
1155 int el;
1156 for (el=0; el<(8); el++) {
1157 self->elements[el] = ID_NONE;
1158 }
1159 }
1160 }
1161 }
1162
1163 /* Check sampling frequency */
1164 switch ( self->streamInfo.aacSampleRate ) {
1165 case 16000:
1166 case 12000:
1167 case 11025:
1168 case 8000:
1169 case 7350:
1170 case 48000:
1171 case 44100:
1172 case 32000:
1173 case 24000:
1174 case 22050:
1175 break;
1176 default:
1177 if ( ! (self->flags & (AC_USAC|AC_RSVD50)) ) {
1178 return AAC_DEC_UNSUPPORTED_SAMPLINGRATE;
1179 }
1180 break;
1181 }
1182
1183
1184 if ( flags & AACDEC_CLRHIST )
1185 {
1186 int ch;
1187 /* Clear history */
1188 for (ch = 0; ch < self->aacChannels; ch++) {
1189 /* Reset concealment */
1190 CConcealment_InitChannelData(&self->pAacDecoderStaticChannelInfo[ch]->concealmentInfo,
1191 &self->concealCommonData,
1192 self->streamInfo.aacSamplesPerFrame );
1193 /* Clear overlap-add buffers to avoid clicks. */
1194 FDKmemclear(self->pAacDecoderStaticChannelInfo[ch]->pOverlapBuffer, OverlapBufferSize*sizeof(FIXP_DBL));
1195 }
1196 }
1197
1198
1199
1200 #ifdef TP_PCE_ENABLE
1201 int pceRead = 0; /* Flag indicating a PCE in the current raw_data_block() */
1202 #endif
1203
1204
1205 INT hdaacDecoded = 0;
1206 MP4_ELEMENT_ID previous_element = ID_END; /* Last element ID (required for extension payload mapping */
1207 UCHAR previous_element_index = 0; /* Canonical index of last element */
1208 int element_count = 0; /* Element counter for elements found in the bitstream */
1209 int el_cnt[ID_LAST] = { 0 }; /* element counter ( robustness ) */
1210
1211 while ( (type != ID_END) && (! (flags & (AACDEC_CONCEAL | AACDEC_FLUSH))) && self->frameOK )
1212 {
1213 int el_channels;
1214
1215 if (! (self->flags & (AC_USAC|AC_RSVD50|AC_ELD|AC_SCALABLE|AC_ER)))
1216 type = (MP4_ELEMENT_ID) FDKreadBits(bs,3);
1217 else
1218 type = self->elements[element_count];
1219
1220 setHcrType(&self->aacCommonData.overlay.aac.erHcrInfo, type);
1221
1222
1223 if ((INT)FDKgetValidBits(bs) < 0)
1224 self->frameOK = 0;
1225
1226 switch (type)
1227 {
1228 case ID_SCE:
1229 case ID_CPE:
1230 case ID_LFE:
1231 /*
1232 Consistency check
1233 */
1234
1235 if (type == ID_CPE) {
1236 el_channels = 2;
1237 } else {
1238 el_channels = 1;
1239 }
1240
1241 if ( (el_cnt[type] >= (self->ascChannels>>(el_channels-1))) || (aacChannels > (self->ascChannels-el_channels)) ) {
1242 ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR;
1243 self->frameOK = 0;
1244 break;
1245 }
1246
1247 if ( !(self->flags & (AC_USAC|AC_RSVD50)) ) {
1248 int ch;
1249 for (ch=0; ch < el_channels; ch+=1) {
1250 CPns_ResetData(&self->pAacDecoderChannelInfo[aacChannels+ch]->data.aac.PnsData,
1251 &self->pAacDecoderChannelInfo[aacChannels+ch]->pComData->pnsInterChannelData);
1252 }
1253 }
1254
1255 if(self->frameOK) {
1256 ErrorStatus = CChannelElement_Read( bs,
1257 &self->pAacDecoderChannelInfo[aacChannels],
1258 &self->pAacDecoderStaticChannelInfo[aacChannels],
1259 self->streamInfo.aot,
1260 &self->samplingRateInfo,
1261 self->flags,
1262 self->streamInfo.aacSamplesPerFrame,
1263 el_channels,
1264 self->streamInfo.epConfig,
1265 self->hInput
1266 );
1267 if (ErrorStatus) {
1268 self->frameOK = 0;
1269 }
1270 }
1271
1272
1273 if ( self->frameOK) {
1274 /* Lookup the element and decode it only if it belongs to the current program */
1275 if ( CProgramConfig_LookupElement(
1276 pce,
1277 self->streamInfo.channelConfig,
1278 self->pAacDecoderChannelInfo[aacChannels]->ElementInstanceTag,
1279 aacChannels,
1280 self->chMapping,
1281 self->channelType,
1282 self->channelIndices,
1283 &previous_element_index,
1284 self->elements,
1285 type) )
1286 {
1287 if ( !hdaacDecoded ) {
1288 CChannelElement_Decode(
1289 &self->pAacDecoderChannelInfo[aacChannels],
1290 &self->pAacDecoderStaticChannelInfo[aacChannels],
1291 &self->samplingRateInfo,
1292 self->flags,
1293 el_channels
1294 );
1295 }
1296 aacChannels += 1;
1297 if (type == ID_CPE) {
1298 aacChannels += 1;
1299 }
1300 }
1301 else {
1302 self->frameOK = 0;
1303 }
1304 /* Create SBR element for SBR for upsampling for LFE elements,
1305 and if SBR was explicitly signaled, because the first frame(s)
1306 may not contain SBR payload (broken encoder, bit errors). */
1307 if ( (self->flags & AC_SBR_PRESENT) || (self->sbrEnabled == 1) )
1308 {
1309 SBR_ERROR sbrError;
1310
1311 sbrError = sbrDecoder_InitElement(
1312 self->hSbrDecoder,
1313 self->streamInfo.aacSampleRate,
1314 self->streamInfo.extSamplingRate,
1315 self->streamInfo.aacSamplesPerFrame,
1316 self->streamInfo.aot,
1317 type,
1318 previous_element_index
1319 );
1320 if (sbrError != SBRDEC_OK) {
1321 /* Do not try to apply SBR because initializing the element failed. */
1322 self->sbrEnabled = 0;
1323 }
1324 }
1325 }
1326
1327 el_cnt[type]++;
1328 break;
1329
1330 case ID_CCE:
1331 /*
1332 Consistency check
1333 */
1334 if ( el_cnt[type] > self->ascChannels ) {
1335 ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR;
1336 self->frameOK = 0;
1337 break;
1338 }
1339
1340 if (self->frameOK)
1341 {
1342 /* memory for spectral lines temporal on scratch */
1343 C_ALLOC_SCRATCH_START(mdctSpec, FIXP_DBL, 1024);
1344
1345 /* create dummy channel for CCE parsing on stack */
1346 CAacDecoderChannelInfo tmpAacDecoderChannelInfo, *pTmpAacDecoderChannelInfo;
1347
1348 FDKmemclear(mdctSpec, 1024*sizeof(FIXP_DBL));
1349
1350 tmpAacDecoderChannelInfo.pDynData = self->aacCommonData.workBufferCore1->pAacDecoderDynamicData;
1351 tmpAacDecoderChannelInfo.pComData = &self->aacCommonData;
1352 tmpAacDecoderChannelInfo.pSpectralCoefficient = (SPECTRAL_PTR)mdctSpec;
1353 /* Assume AAC-LC */
1354 tmpAacDecoderChannelInfo.granuleLength = self->streamInfo.aacSamplesPerFrame / 8;
1355
1356 /* Reset PNS data. */
1357 CPns_ResetData(&tmpAacDecoderChannelInfo.data.aac.PnsData, &tmpAacDecoderChannelInfo.pComData->pnsInterChannelData);
1358
1359 pTmpAacDecoderChannelInfo = &tmpAacDecoderChannelInfo;
1360 /* do CCE parsing */
1361 ErrorStatus = CChannelElement_Read( bs,
1362 &pTmpAacDecoderChannelInfo,
1363 NULL,
1364 self->streamInfo.aot,
1365 &self->samplingRateInfo,
1366 self->flags,
1367 self->streamInfo.aacSamplesPerFrame,
1368 1,
1369 self->streamInfo.epConfig,
1370 self->hInput
1371 );
1372
1373 C_ALLOC_SCRATCH_END(mdctSpec, FIXP_DBL, 1024);
1374
1375 if (ErrorStatus) {
1376 self->frameOK = 0;
1377 }
1378
1379 if (self->frameOK) {
1380 /* Lookup the element and decode it only if it belongs to the current program */
1381 if (CProgramConfig_LookupElement(
1382 pce,
1383 self->streamInfo.channelConfig,
1384 pTmpAacDecoderChannelInfo->ElementInstanceTag,
1385 0,
1386 self->chMapping,
1387 self->channelType,
1388 self->channelIndices,
1389 &previous_element_index,
1390 self->elements,
1391 type) )
1392 {
1393 /* decoding of CCE not supported */
1394 }
1395 else {
1396 self->frameOK = 0;
1397 }
1398 }
1399 }
1400 el_cnt[type]++;
1401 break;
1402
1403 case ID_DSE:
1404 {
1405 UCHAR element_instance_tag;
1406
1407 CDataStreamElement_Read( self,
1408 bs,
1409 &element_instance_tag,
1410 auStartAnchor );
1411
1412 if (!CProgramConfig_LookupElement(
1413 pce,
1414 self->streamInfo.channelConfig,
1415 element_instance_tag,
1416 0,
1417 self->chMapping,
1418 self->channelType,
1419 self->channelIndices,
1420 &previous_element_index,
1421 self->elements,
1422 type) )
1423 {
1424 /* most likely an error in bitstream occured */
1425 //self->frameOK = 0;
1426 }
1427 }
1428 break;
1429
1430 #ifdef TP_PCE_ENABLE
1431 case ID_PCE:
1432 {
1433 int result = CProgramConfigElement_Read(
1434 bs,
1435 self->hInput,
1436 pce,
1437 self->streamInfo.channelConfig,
1438 auStartAnchor );
1439 if ( result < 0 ) {
1440 /* Something went wrong */
1441 ErrorStatus = AAC_DEC_PARSE_ERROR;
1442 self->frameOK = 0;
1443 }
1444 else if ( result > 1 ) {
1445 /* Built element table */
1446 int elIdx = CProgramConfig_GetElementTable(pce, self->elements, (8), &self->chMapIndex);
1447 /* Reset the remaining tabs */
1448 for ( ; elIdx<(8); elIdx++) {
1449 self->elements[elIdx] = ID_NONE;
1450 }
1451 /* Make new number of channel persistant */
1452 self->ascChannels = pce->NumChannels;
1453 /* If PCE is not first element conceal this frame to avoid inconsistencies */
1454 if ( element_count != 0 ) {
1455 self->frameOK = 0;
1456 }
1457 }
1458 pceRead = (result>=0) ? 1 : 0;
1459 }
1460 break;
1461 #endif /* TP_PCE_ENABLE */
1462
1463 case ID_FIL:
1464 {
1465 int bitCnt = FDKreadBits(bs,4); /* bs_count */
1466
1467 if (bitCnt == 15)
1468 {
1469 int esc_count = FDKreadBits(bs,8); /* bs_esc_count */
1470 bitCnt = esc_count + 14;
1471 }
1472
1473 /* Convert to bits */
1474 bitCnt <<= 3;
1475
1476 while (bitCnt > 0) {
1477 ErrorStatus = CAacDecoder_ExtPayloadParse(self, bs, &bitCnt, previous_element, previous_element_index, 1);
1478 if (ErrorStatus != AAC_DEC_OK) {
1479 self->frameOK = 0;
1480 break;
1481 }
1482 }
1483 }
1484 break;
1485
1486 case ID_EXT:
1487 {
1488 INT bitCnt = 0;
1489
1490 /* get the remaining bits of this frame */
1491 bitCnt = transportDec_GetAuBitsRemaining(self->hInput, 0);
1492
1493 if ( (bitCnt > 0) && (self->flags & AC_SBR_PRESENT) && (self->flags & (AC_USAC|AC_RSVD50|AC_ELD)) )
1494 {
1495 SBR_ERROR err = SBRDEC_OK;
1496 int elIdx, numChElements = el_cnt[ID_SCE] + el_cnt[ID_CPE];
1497
1498 for (elIdx = 0; elIdx < numChElements; elIdx += 1)
1499 {
1500 err = sbrDecoder_Parse (
1501 self->hSbrDecoder,
1502 bs,
1503 &bitCnt,
1504 -1,
1505 self->flags & AC_SBRCRC,
1506 self->elements[elIdx],
1507 elIdx,
1508 self->flags & AC_INDEP );
1509
1510 if (err != SBRDEC_OK) {
1511 break;
1512 }
1513 }
1514 switch (err) {
1515 case SBRDEC_PARSE_ERROR:
1516 /* Can not go on parsing because we do not
1517 know the length of the SBR extension data. */
1518 FDKpushFor(bs, bitCnt);
1519 bitCnt = 0;
1520 break;
1521 case SBRDEC_OK:
1522 self->sbrEnabled = 1;
1523 break;
1524 default:
1525 self->frameOK = 0;
1526 break;
1527 }
1528 }
1529
1530
1531 if ( ! (self->flags & (AC_USAC|AC_RSVD50|AC_DRM)) )
1532 {
1533 while ( bitCnt > 7 ) {
1534 ErrorStatus = CAacDecoder_ExtPayloadParse(self, bs, &bitCnt, previous_element, previous_element_index, 0);
1535 if (ErrorStatus != AAC_DEC_OK) {
1536 self->frameOK = 0;
1537 ErrorStatus = AAC_DEC_PARSE_ERROR;
1538 break;
1539 }
1540 }
1541 }
1542 }
1543 break;
1544
1545 case ID_END:
1546 break;
1547
1548 default:
1549 ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR;
1550 self->frameOK = 0;
1551 break;
1552 }
1553
1554 previous_element = type;
1555 element_count++;
1556
1557 } /* while ( (type != ID_END) ... ) */
1558
1559 if ( !(flags & (AACDEC_CONCEAL|AACDEC_FLUSH)) )
1560 {
1561 /* Byte alignment with respect to the first bit of the raw_data_block(). */
1562 {
1563 FDKbyteAlign(bs, auStartAnchor);
1564 }
1565
1566 /* Check if all bits of the raw_data_block() have been read. */
1567 if ( transportDec_GetAuBitsTotal(self->hInput, 0) > 0 ) {
1568 INT unreadBits = transportDec_GetAuBitsRemaining(self->hInput, 0);
1569 if ( unreadBits != 0 ) {
1570
1571 self->frameOK = 0;
1572 /* Do not overwrite current error */
1573 if (ErrorStatus == AAC_DEC_OK && self->frameOK == 0) {
1574 ErrorStatus = AAC_DEC_PARSE_ERROR;
1575 }
1576 /* Always put the bitbuffer at the right position after the current Access Unit. */
1577 FDKpushBiDirectional(bs, unreadBits);
1578 }
1579 }
1580
1581 /* Check the last element. The terminator (ID_END) has to be the last one (even if ER syntax is used). */
1582 if ( self->frameOK && type != ID_END ) {
1583 /* Do not overwrite current error */
1584 if (ErrorStatus == AAC_DEC_OK) {
1585 ErrorStatus = AAC_DEC_PARSE_ERROR;
1586 }
1587 self->frameOK = 0;
1588 }
1589 }
1590
1591 /* More AAC channels than specified by the ASC not allowed. */
1592 if ( (aacChannels == 0 || aacChannels > self->aacChannels) && !(flags & (AACDEC_CONCEAL|AACDEC_FLUSH)) ) {
1593 {
1594 /* Do not overwrite current error */
1595 if (ErrorStatus == AAC_DEC_OK) {
1596 ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR;
1597 }
1598 self->frameOK = 0;
1599 }
1600 aacChannels = 0;
1601 }
1602 else if ( aacChannels > self->ascChannels ) {
1603 /* Do not overwrite current error */
1604 if (ErrorStatus == AAC_DEC_OK) {
1605 ErrorStatus = AAC_DEC_UNSUPPORTED_FORMAT;
1606 }
1607 self->frameOK = 0;
1608 aacChannels = 0;
1609 }
1610
1611 if ( TRANSPORTDEC_OK != transportDec_CrcCheck(self->hInput) )
1612 {
1613 self->frameOK=0;
1614 }
1615
1616 /* store or restore the number of channels and the corresponding info */
1617 if ( self->frameOK && !(flags &(AACDEC_CONCEAL|AACDEC_FLUSH)) ) {
1618 self->aacChannelsPrev = aacChannels; /* store */
1619 FDKmemcpy(self->channelTypePrev, self->channelType, (8)*sizeof(AUDIO_CHANNEL_TYPE)); /* store */
1620 FDKmemcpy(self->channelIndicesPrev, self->channelIndices, (8)*sizeof(UCHAR)); /* store */
1621 self->sbrEnabledPrev = self->sbrEnabled;
1622 } else {
1623 if (self->aacChannels > 0) {
1624 aacChannels = self->aacChannelsPrev; /* restore */
1625 FDKmemcpy(self->channelType, self->channelTypePrev, (8)*sizeof(AUDIO_CHANNEL_TYPE)); /* restore */
1626 FDKmemcpy(self->channelIndices, self->channelIndicesPrev, (8)*sizeof(UCHAR)); /* restore */
1627 self->sbrEnabled = self->sbrEnabledPrev;
1628 }
1629 }
1630
1631 /* Update number of output channels */
1632 self->streamInfo.aacNumChannels = aacChannels;
1633
1634 #ifdef TP_PCE_ENABLE
1635 if (pceRead == 1 && CProgramConfig_IsValid(pce)) {
1636 /* Set matrix mixdown infos if available from PCE. */
1637 pcmDmx_SetMatrixMixdownFromPce ( self->hPcmUtils,
1638 pce->MatrixMixdownIndexPresent,
1639 pce->MatrixMixdownIndex,
1640 pce->PseudoSurroundEnable );
1641 }
1642 #endif
1643
1644 /* If there is no valid data to transfrom into time domain, return. */
1645 if ( ! IS_OUTPUT_VALID(ErrorStatus) ) {
1646 return ErrorStatus;
1647 }
1648
1649 /* Setup the output channel mapping. The table below shows the four possibilities:
1650 * # | chCfg | PCE | cChCfg | chOutMapIdx
1651 * ---+-------+-----+--------+------------------
1652 * 1 | > 0 | no | - | chCfg
1653 * 2 | 0 | yes | > 0 | cChCfg
1654 * 3 | 0 | yes | 0 | aacChannels || 0
1655 * 4 | 0 | no | - | aacChannels || 0
1656 * ---+-------+-----+--------+------------------
1657 * Where chCfg is the channel configuration index from ASC and cChCfg is a corresponding chCfg
1658 * derived from a given PCE. The variable aacChannels represents the number of channel found
1659 * during bitstream decoding. Due to the structure of the mapping table it can only be used for
1660 * mapping if its value is smaller than 7. Otherwise we use the fallback (0) which is a simple
1661 * pass-through. The possibility #4 should appear only with MPEG-2 (ADTS) streams. This is
1662 * mode is called "implicit channel mapping".
1663 */
1664 chOutMapIdx = ((self->chMapIndex==0) && (aacChannels<7)) ? aacChannels : self->chMapIndex;
1665
1666 /*
1667 Inverse transform
1668 */
1669 {
1670 int stride, offset, c;
1671
1672 /* Turn on/off DRC modules level normalization in digital domain depending on the limiter status. */
1673 aacDecoder_drcSetParam( self->hDrcInfo, APPLY_NORMALIZATION, (self->limiterEnableCurr) ? 0 : 1 );
1674 /* Extract DRC control data and map it to channels (without bitstream delay) */
1675 aacDecoder_drcProlog (
1676 self->hDrcInfo,
1677 bs,
1678 self->pAacDecoderStaticChannelInfo,
1679 self->pce.ElementInstanceTag,
1680 self->chMapping,
1681 aacChannels
1682 );
1683
1684 /* "c" iterates in canonical MPEG channel order */
1685 for (c=0; c < aacChannels; c++)
1686 {
1687 CAacDecoderChannelInfo *pAacDecoderChannelInfo;
1688
1689 /* Select correct pAacDecoderChannelInfo for current channel */
1690 if (self->chMapping[c] >= aacChannels) {
1691 pAacDecoderChannelInfo = self->pAacDecoderChannelInfo[c];
1692 } else {
1693 pAacDecoderChannelInfo = self->pAacDecoderChannelInfo[self->chMapping[c]];
1694 }
1695
1696 /* Setup offset and stride for time buffer traversal. */
1697 if (interleaved) {
1698 stride = aacChannels;
1699 offset = self->channelOutputMapping[chOutMapIdx][c];
1700 } else {
1701 stride = 1;
1702 offset = self->channelOutputMapping[chOutMapIdx][c] * self->streamInfo.aacSamplesPerFrame;
1703 }
1704
1705
1706 if ( flags&AACDEC_FLUSH ) {
1707 /* Clear pAacDecoderChannelInfo->pSpectralCoefficient because with AACDEC_FLUSH set it contains undefined data. */
1708 FDKmemclear(pAacDecoderChannelInfo->pSpectralCoefficient, sizeof(FIXP_DBL)*self->streamInfo.aacSamplesPerFrame);
1709 }
1710
1711 /*
1712 Conceal defective spectral data
1713 */
1714 CConcealment_Apply(&self->pAacDecoderStaticChannelInfo[c]->concealmentInfo,
1715 pAacDecoderChannelInfo,
1716 self->pAacDecoderStaticChannelInfo[c],
1717 &self->samplingRateInfo,
1718 self->streamInfo.aacSamplesPerFrame,
1719 0,
1720 (self->frameOK && !(flags&AACDEC_CONCEAL)),
1721 self->flags
1722 );
1723
1724
1725 if (flags & (AACDEC_INTR|AACDEC_CLRHIST)) {
1726 /* Reset DRC control data for this channel */
1727 aacDecoder_drcInitChannelData ( &self->pAacDecoderStaticChannelInfo[c]->drcData );
1728 }
1729 /* The DRC module demands to be called with the gain field holding the gain scale. */
1730 self->extGain[0] = (FIXP_DBL)TDL_GAIN_SCALING;
1731 /* DRC processing */
1732 aacDecoder_drcApply (
1733 self->hDrcInfo,
1734 self->hSbrDecoder,
1735 pAacDecoderChannelInfo,
1736 &self->pAacDecoderStaticChannelInfo[c]->drcData,
1737 self->extGain,
1738 c,
1739 self->streamInfo.aacSamplesPerFrame,
1740 self->sbrEnabled
1741 );
1742
1743 switch (pAacDecoderChannelInfo->renderMode)
1744 {
1745 case AACDEC_RENDER_IMDCT:
1746 CBlock_FrequencyToTime(
1747 self->pAacDecoderStaticChannelInfo[c],
1748 pAacDecoderChannelInfo,
1749 pTimeData + offset,
1750 self->streamInfo.aacSamplesPerFrame,
1751 stride,
1752 (self->frameOK && !(flags&AACDEC_CONCEAL)),
1753 self->aacCommonData.workBufferCore1->mdctOutTemp
1754 );
1755 self->extGainDelay = self->streamInfo.aacSamplesPerFrame;
1756 break;
1757 case AACDEC_RENDER_ELDFB:
1758 CBlock_FrequencyToTimeLowDelay(
1759 self->pAacDecoderStaticChannelInfo[c],
1760 pAacDecoderChannelInfo,
1761 pTimeData + offset,
1762 self->streamInfo.aacSamplesPerFrame,
1763 stride
1764 );
1765 self->extGainDelay = (self->streamInfo.aacSamplesPerFrame*2 - self->streamInfo.aacSamplesPerFrame/2 - 1)/2;
1766 break;
1767 default:
1768 ErrorStatus = AAC_DEC_UNKNOWN;
1769 break;
1770 }
1771 if ( flags&AACDEC_FLUSH ) {
1772 FDKmemclear(pAacDecoderChannelInfo->pSpectralCoefficient, sizeof(FIXP_DBL)*self->streamInfo.aacSamplesPerFrame);
1773 FDKmemclear(self->pAacDecoderStaticChannelInfo[c]->pOverlapBuffer, OverlapBufferSize*sizeof(FIXP_DBL));
1774 }
1775 }
1776
1777
1778 /* Extract DRC control data and map it to channels (with bitstream delay) */
1779 aacDecoder_drcEpilog (
1780 self->hDrcInfo,
1781 bs,
1782 self->pAacDecoderStaticChannelInfo,
1783 self->pce.ElementInstanceTag,
1784 self->chMapping,
1785 aacChannels
1786 );
1787 }
1788
1789 /* Add additional concealment delay */
1790 self->streamInfo.outputDelay += CConcealment_GetDelay(&self->concealCommonData) * self->streamInfo.aacSamplesPerFrame;
1791
1792 /* Map DRC data to StreamInfo structure */
1793 aacDecoder_drcGetInfo (
1794 self->hDrcInfo,
1795 &self->streamInfo.drcPresMode,
1796 &self->streamInfo.drcProgRefLev
1797 );
1798
1799 /* Reorder channel type information tables. */
1800 {
1801 AUDIO_CHANNEL_TYPE types[(8)];
1802 UCHAR idx[(8)];
1803 int c;
1804
1805 FDK_ASSERT(sizeof(self->channelType) == sizeof(types));
1806 FDK_ASSERT(sizeof(self->channelIndices) == sizeof(idx));
1807
1808 FDKmemcpy(types, self->channelType, sizeof(types));
1809 FDKmemcpy(idx, self->channelIndices, sizeof(idx));
1810
1811 for (c=0; c<aacChannels; c++) {
1812 self->channelType[self->channelOutputMapping[chOutMapIdx][c]] = types[c];
1813 self->channelIndices[self->channelOutputMapping[chOutMapIdx][c]] = idx[c];
1814 }
1815 }
1816
1817 self->blockNumber++;
1818
1819 return ErrorStatus;
1820 }
1821
1822 /*!
1823 \brief returns the streaminfo pointer
1824
1825 The function hands back a pointer to the streaminfo structure
1826
1827 \return pointer to the struct
1828 */
CAacDecoder_GetStreamInfo(HANDLE_AACDECODER self)1829 LINKSPEC_CPP CStreamInfo* CAacDecoder_GetStreamInfo ( HANDLE_AACDECODER self )
1830 {
1831 if (!self) {
1832 return NULL;
1833 }
1834 return &self->streamInfo;
1835 }
1836
1837
1838
1839
1840