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