Home | History | Annotate | Download | only in src
      1 /* -----------------------------------------------------------------------------
      2 Software License for The Fraunhofer FDK AAC Codec Library for Android
      3 
      4  Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Frderung der angewandten
      5 Forschung e.V. All rights reserved.
      6 
      7  1.    INTRODUCTION
      8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
      9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
     10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
     11 a wide variety of Android devices.
     12 
     13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
     14 general perceptual audio codecs. AAC-ELD is considered the best-performing
     15 full-bandwidth communications codec by independent studies and is widely
     16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
     17 specifications.
     18 
     19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
     20 those of Fraunhofer) may be obtained through Via Licensing
     21 (www.vialicensing.com) or through the respective patent owners individually for
     22 the purpose of encoding or decoding bit streams in products that are compliant
     23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
     24 Android devices already license these patent claims through Via Licensing or
     25 directly from the patent owners, and therefore FDK AAC Codec software may
     26 already be covered under those patent licenses when it is used for those
     27 licensed purposes only.
     28 
     29 Commercially-licensed AAC software libraries, including floating-point versions
     30 with enhanced sound quality, are also available from Fraunhofer. Users are
     31 encouraged to check the Fraunhofer website for additional applications
     32 information and documentation.
     33 
     34 2.    COPYRIGHT LICENSE
     35 
     36 Redistribution and use in source and binary forms, with or without modification,
     37 are permitted without payment of copyright license fees provided that you
     38 satisfy the following conditions:
     39 
     40 You must retain the complete text of this software license in redistributions of
     41 the FDK AAC Codec or your modifications thereto in source code form.
     42 
     43 You must retain the complete text of this software license in the documentation
     44 and/or other materials provided with redistributions of the FDK AAC Codec or
     45 your modifications thereto in binary form. You must make available free of
     46 charge copies of the complete source code of the FDK AAC Codec and your
     47 modifications thereto to recipients of copies in binary form.
     48 
     49 The name of Fraunhofer may not be used to endorse or promote products derived
     50 from this library without prior written permission.
     51 
     52 You may not charge copyright license fees for anyone to use, copy or distribute
     53 the FDK AAC Codec software or your modifications thereto.
     54 
     55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
     56 that you changed the software and the date of any change. For modified versions
     57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
     58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
     59 AAC Codec Library for Android."
     60 
     61 3.    NO PATENT LICENSE
     62 
     63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
     64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
     65 Fraunhofer provides no warranty of patent non-infringement with respect to this
     66 software.
     67 
     68 You may use this FDK AAC Codec software or modifications thereto only for
     69 purposes that are authorized by appropriate patent licenses.
     70 
     71 4.    DISCLAIMER
     72 
     73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
     74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
     75 including but not limited to the implied warranties of merchantability and
     76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
     78 or consequential damages, including but not limited to procurement of substitute
     79 goods or services; loss of use, data, or profits, or business interruption,
     80 however caused and on any theory of liability, whether in contract, strict
     81 liability, or tort (including negligence), arising in any way out of the use of
     82 this software, even if advised of the possibility of such damage.
     83 
     84 5.    CONTACT INFORMATION
     85 
     86 Fraunhofer Institute for Integrated Circuits IIS
     87 Attention: Audio and Multimedia Departments - FDK AAC LL
     88 Am Wolfsmantel 33
     89 91058 Erlangen, Germany
     90 
     91 www.iis.fraunhofer.de/amm
     92 amm-info (at) iis.fraunhofer.de
     93 ----------------------------------------------------------------------------- */
     94 
     95 /**************************** AAC decoder library ******************************
     96 
     97    Author(s):   Josef Hoepfl
     98 
     99    Description:
    100 
    101 *******************************************************************************/
    102 
    103 /*!
    104   \page default General Overview of the AAC Decoder Implementation
    105 
    106   The main entry point to decode a AAC frame is CAacDecoder_DecodeFrame(). It
    107   handles the different transport multiplexes and bitstream formats supported by
    108   this implementation. It extracts the AAC_raw_data_blocks from these bitstreams
    109   to further process then in the actual decoding stages.
    110 
    111   Note: Click on a function of file in the above image to see details about the
    112   function. Also note, that this is just an overview of the most important
    113   functions and not a complete call graph.
    114 
    115   <h2>1 Bitstream deformatter</h2>
    116   The basic bit stream parser function CChannelElement_Read() is called. It uses
    117   other subcalls in order to parse and unpack the bitstreams. Note, that this
    118   includes huffmann decoding of the coded spectral data. This operation can be
    119   computational significant specifically at higher bitrates. Optimization is
    120   likely in CBlock_ReadSpectralData().
    121 
    122   The bitstream deformatter also includes many bitfield operations. Profiling on
    123   the target will determine required optimizations.
    124 
    125   <h2>2 Actual decoding to retain the time domain output</h2>
    126   The basic bitstream deformatter function CChannelElement_Decode() for CPE
    127   elements and SCE elements are called. Except for the stereo processing (2.1)
    128   which is only used for CPE elements, the function calls for CPE or SCE are
    129   similar, except that CPE always processes to independent channels while SCE
    130   only processes one channel.
    131 
    132   Often there is the distinction between long blocks and short blocks. However,
    133   computational expensive functions that ususally require optimization are being
    134   shared by these two groups,
    135 
    136   <h3>2.1 Stereo processing for CPE elements</h3>
    137   CChannelPairElement_Decode() first calles the joint stereo  tools in
    138   stereo.cpp when required.
    139 
    140   <h3>2.2 Scaling of spectral data</h3>
    141   CBlock_ScaleSpectralData().
    142 
    143   <h3>2.3 Apply additional coding tools</h3>
    144   ApplyTools() calles the PNS tools in case of MPEG-4 bitstreams, and TNS
    145   filtering CTns_Apply() for MPEG-2 and MPEG-4 bitstreams. The function
    146   TnsFilterIIR() which is called by CTns_Apply() (2.3.1) might require some
    147   optimization.
    148 
    149   <h2>3 Frequency-To-Time conversion</h3>
    150   The filterbank is called using CBlock_FrequencyToTime() using the MDCT module
    151   from the FDK Tools
    152 
    153 */
    154 
    155 #include "aacdecoder.h"
    156 
    157 #include "aac_rom.h"
    158 #include "aac_ram.h"
    159 #include "channel.h"
    160 #include "FDK_audio.h"
    161 
    162 #include "aacdec_pns.h"
    163 
    164 #include "sbrdecoder.h"
    165 
    166 #include "sac_dec_lib.h"
    167 
    168 #include "aacdec_hcr.h"
    169 #include "rvlc.h"
    170 
    171 #include "usacdec_lpd.h"
    172 
    173 #include "ac_arith_coder.h"
    174 
    175 #include "tpdec_lib.h"
    176 
    177 #include "conceal.h"
    178 
    179 #include "FDK_crc.h"
    180 #define PS_IS_EXPLICITLY_DISABLED(aot, flags) \
    181   (((aot) == AOT_DRM_AAC) && !(flags & AC_PS_PRESENT))
    182 
    183 #define IS_STEREO_SBR(el_id, stereoConfigIndex)            \
    184   (((el_id) == ID_USAC_CPE && (stereoConfigIndex) == 0) || \
    185    ((el_id) == ID_USAC_CPE && (stereoConfigIndex) == 3))
    186 
    187 void CAacDecoder_SyncQmfMode(HANDLE_AACDECODER self) {
    188   FDK_ASSERT(
    189       !((self->flags[0] & AC_MPS_PRESENT) && (self->flags[0] & AC_PS_PRESENT)));
    190 
    191   /* Assign user requested mode */
    192   self->qmfModeCurr = self->qmfModeUser;
    193 
    194   if (IS_USAC(self->streamInfo.aot)) {
    195     self->qmfModeCurr = MODE_HQ;
    196   }
    197 
    198   if (self->qmfModeCurr == NOT_DEFINED) {
    199     if ((IS_LOWDELAY(self->streamInfo.aot) &&
    200          (self->flags[0] & AC_MPS_PRESENT)) ||
    201         ((self->streamInfo.aacNumChannels == 1) &&
    202          ((CAN_DO_PS(self->streamInfo.aot) &&
    203            !(self->flags[0] & AC_MPS_PRESENT)) ||
    204           (IS_USAC(self->streamInfo.aot))))) {
    205       self->qmfModeCurr = MODE_HQ;
    206     } else {
    207       self->qmfModeCurr = MODE_LP;
    208     }
    209   }
    210 
    211   if (self->mpsEnableCurr) {
    212     if (IS_LOWDELAY(self->streamInfo.aot) &&
    213         (self->qmfModeCurr == MODE_LP)) { /* Overrule user requested QMF mode */
    214       self->qmfModeCurr = MODE_HQ;
    215     }
    216     /* Set and check if MPS decoder allows the current mode */
    217     switch (mpegSurroundDecoder_SetParam(
    218         (CMpegSurroundDecoder *)self->pMpegSurroundDecoder,
    219         SACDEC_PARTIALLY_COMPLEX, self->qmfModeCurr == MODE_LP)) {
    220       case MPS_OK:
    221         break;
    222       case MPS_INVALID_PARAMETER: { /* Only one mode supported. Find out which
    223                                        one: */
    224         LIB_INFO libInfo[FDK_MODULE_LAST];
    225         UINT mpsCaps;
    226 
    227         FDKinitLibInfo(libInfo);
    228         mpegSurroundDecoder_GetLibInfo(libInfo);
    229         mpsCaps = FDKlibInfo_getCapabilities(libInfo, FDK_MPSDEC);
    230 
    231         if (((mpsCaps & CAPF_MPS_LP) && (self->qmfModeCurr == MODE_LP)) ||
    232             ((mpsCaps & CAPF_MPS_HQ) &&
    233              (self->qmfModeCurr ==
    234               MODE_HQ))) { /* MPS decoder does support the requested mode. */
    235           break;
    236         }
    237       } /* Fall-through: */
    238       default:
    239         if (self->qmfModeUser == NOT_DEFINED) {
    240           /* Revert in case mpegSurroundDecoder_SetParam() fails. */
    241           self->qmfModeCurr =
    242               (self->qmfModeCurr == MODE_LP) ? MODE_HQ : MODE_LP;
    243         } else {
    244           /* in case specific mode was requested we disable MPS and playout the
    245            * downmix */
    246           self->mpsEnableCurr = 0;
    247         }
    248     }
    249   }
    250 
    251   /* Set SBR to current QMF mode. Error does not matter. */
    252   sbrDecoder_SetParam(self->hSbrDecoder, SBR_QMF_MODE,
    253                       (self->qmfModeCurr == MODE_LP));
    254   self->psPossible =
    255       ((CAN_DO_PS(self->streamInfo.aot) &&
    256         !PS_IS_EXPLICITLY_DISABLED(self->streamInfo.aot, self->flags[0]) &&
    257         self->streamInfo.aacNumChannels == 1 &&
    258         !(self->flags[0] & AC_MPS_PRESENT))) &&
    259       self->qmfModeCurr == MODE_HQ;
    260   FDK_ASSERT(!((self->flags[0] & AC_MPS_PRESENT) && self->psPossible));
    261 }
    262 
    263 void CAacDecoder_SignalInterruption(HANDLE_AACDECODER self) {
    264   if (self->flags[0] & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) {
    265     int i;
    266 
    267     for (i = 0; i < fMin(self->aacChannels, (8)); i++) {
    268       if (self->pAacDecoderStaticChannelInfo
    269               [i]) { /* number of active channels can be smaller */
    270         self->pAacDecoderStaticChannelInfo[i]->hArCo->m_numberLinesPrev = 0;
    271       }
    272     }
    273   }
    274 }
    275 
    276 /*!
    277   \brief Calculates the number of element channels
    278 
    279   \type  channel type
    280   \usacStereoConfigIndex  usac stereo config index
    281 
    282   \return  element channels
    283 */
    284 static int CAacDecoder_GetELChannels(MP4_ELEMENT_ID type,
    285                                      UCHAR usacStereoConfigIndex) {
    286   int el_channels = 0;
    287 
    288   switch (type) {
    289     case ID_USAC_CPE:
    290       if (usacStereoConfigIndex == 1) {
    291         el_channels = 1;
    292       } else {
    293         el_channels = 2;
    294       }
    295       break;
    296     case ID_CPE:
    297       el_channels = 2;
    298       break;
    299     case ID_USAC_SCE:
    300     case ID_USAC_LFE:
    301     case ID_SCE:
    302     case ID_LFE:
    303       el_channels = 1;
    304       break;
    305     default:
    306       el_channels = 0;
    307       break;
    308   }
    309 
    310   return el_channels;
    311 }
    312 
    313 /*!
    314   \brief Reset ancillary data struct. Call before parsing a new frame.
    315 
    316   \ancData Pointer to ancillary data structure
    317 
    318   \return  Error code
    319 */
    320 static AAC_DECODER_ERROR CAacDecoder_AncDataReset(CAncData *ancData) {
    321   int i;
    322   for (i = 0; i < 8; i++) {
    323     ancData->offset[i] = 0;
    324   }
    325   ancData->nrElements = 0;
    326 
    327   return AAC_DEC_OK;
    328 }
    329 
    330 /*!
    331   \brief Initialize ancillary buffer
    332 
    333   \ancData Pointer to ancillary data structure
    334   \buffer Pointer to (external) anc data buffer
    335   \size Size of the buffer pointed on by buffer in bytes
    336 
    337   \return  Error code
    338 */
    339 AAC_DECODER_ERROR CAacDecoder_AncDataInit(CAncData *ancData,
    340                                           unsigned char *buffer, int size) {
    341   if (size >= 0) {
    342     ancData->buffer = buffer;
    343     ancData->bufferSize = size;
    344 
    345     CAacDecoder_AncDataReset(ancData);
    346 
    347     return AAC_DEC_OK;
    348   }
    349 
    350   return AAC_DEC_ANC_DATA_ERROR;
    351 }
    352 
    353 /*!
    354   \brief Get one ancillary data element
    355 
    356   \ancData Pointer to ancillary data structure
    357   \index Index of the anc data element to get
    358   \ptr Pointer to a buffer receiving a pointer to the requested anc data element
    359   \size Pointer to a buffer receiving the length of the requested anc data
    360   element in bytes
    361 
    362   \return  Error code
    363 */
    364 AAC_DECODER_ERROR CAacDecoder_AncDataGet(CAncData *ancData, int index,
    365                                          unsigned char **ptr, int *size) {
    366   AAC_DECODER_ERROR error = AAC_DEC_OK;
    367 
    368   *ptr = NULL;
    369   *size = 0;
    370 
    371   if (index >= 0 && index < 8 - 1 && index < ancData->nrElements) {
    372     *ptr = &ancData->buffer[ancData->offset[index]];
    373     *size = ancData->offset[index + 1] - ancData->offset[index];
    374   }
    375 
    376   return error;
    377 }
    378 
    379 /*!
    380   \brief Parse ancillary data
    381 
    382   \ancData Pointer to ancillary data structure
    383   \hBs Handle to FDK bitstream
    384   \ancBytes Length of ancillary data to read from the bitstream
    385 
    386   \return  Error code
    387 */
    388 static AAC_DECODER_ERROR CAacDecoder_AncDataParse(CAncData *ancData,
    389                                                   HANDLE_FDK_BITSTREAM hBs,
    390                                                   const int ancBytes) {
    391   AAC_DECODER_ERROR error = AAC_DEC_OK;
    392   int readBytes = 0;
    393 
    394   if (ancData->buffer != NULL) {
    395     if (ancBytes > 0) {
    396       /* write ancillary data to external buffer */
    397       int offset = ancData->offset[ancData->nrElements];
    398 
    399       if ((offset + ancBytes) > ancData->bufferSize) {
    400         error = AAC_DEC_TOO_SMALL_ANC_BUFFER;
    401       } else if (ancData->nrElements >= 8 - 1) {
    402         error = AAC_DEC_TOO_MANY_ANC_ELEMENTS;
    403       } else {
    404         int i;
    405 
    406         for (i = 0; i < ancBytes; i++) {
    407           ancData->buffer[i + offset] = FDKreadBits(hBs, 8);
    408           readBytes++;
    409         }
    410 
    411         ancData->nrElements++;
    412         ancData->offset[ancData->nrElements] =
    413             ancBytes + ancData->offset[ancData->nrElements - 1];
    414       }
    415     }
    416   }
    417 
    418   readBytes = ancBytes - readBytes;
    419 
    420   if (readBytes > 0) {
    421     /* skip data */
    422     FDKpushFor(hBs, readBytes << 3);
    423   }
    424 
    425   return error;
    426 }
    427 
    428 /*!
    429   \brief Read Stream Data Element
    430 
    431   \bs Bitstream Handle
    432 
    433   \return  Error code
    434 */
    435 static AAC_DECODER_ERROR CDataStreamElement_Read(HANDLE_AACDECODER self,
    436                                                  HANDLE_FDK_BITSTREAM bs,
    437                                                  UCHAR *elementInstanceTag,
    438                                                  UINT alignmentAnchor) {
    439   AAC_DECODER_ERROR error = AAC_DEC_OK;
    440   UINT dseBits;
    441   INT dataStart;
    442   int dataByteAlignFlag, count;
    443 
    444   FDK_ASSERT(self != NULL);
    445 
    446   int crcReg = transportDec_CrcStartReg(self->hInput, 0);
    447 
    448   /* Element Instance Tag */
    449   *elementInstanceTag = FDKreadBits(bs, 4);
    450   /* Data Byte Align Flag */
    451   dataByteAlignFlag = FDKreadBits(bs, 1);
    452 
    453   count = FDKreadBits(bs, 8);
    454 
    455   if (count == 255) {
    456     count += FDKreadBits(bs, 8); /* EscCount */
    457   }
    458   dseBits = count * 8;
    459 
    460   if (dataByteAlignFlag) {
    461     FDKbyteAlign(bs, alignmentAnchor);
    462   }
    463 
    464   dataStart = (INT)FDKgetValidBits(bs);
    465 
    466   error = CAacDecoder_AncDataParse(&self->ancData, bs, count);
    467   transportDec_CrcEndReg(self->hInput, crcReg);
    468 
    469   {
    470     /* Move to the beginning of the data chunk */
    471     FDKpushBack(bs, dataStart - (INT)FDKgetValidBits(bs));
    472 
    473     /* Read Anc data if available */
    474     aacDecoder_drcMarkPayload(self->hDrcInfo, bs, DVB_DRC_ANC_DATA);
    475   }
    476 
    477   {
    478     PCMDMX_ERROR dmxErr = PCMDMX_OK;
    479 
    480     /* Move to the beginning of the data chunk */
    481     FDKpushBack(bs, dataStart - (INT)FDKgetValidBits(bs));
    482 
    483     /* Read DMX meta-data */
    484     dmxErr = pcmDmx_Parse(self->hPcmUtils, bs, dseBits, 0 /* not mpeg2 */);
    485     if (error == AAC_DEC_OK && dmxErr != PCMDMX_OK) {
    486       error = AAC_DEC_UNKNOWN;
    487     }
    488   }
    489 
    490   /* Move to the very end of the element. */
    491   FDKpushBiDirectional(bs, (INT)FDKgetValidBits(bs) - dataStart + (INT)dseBits);
    492 
    493   return error;
    494 }
    495 
    496 /*!
    497   \brief Read Program Config Element
    498 
    499   \bs Bitstream Handle
    500   \pTp Transport decoder handle for CRC handling
    501   \pce Pointer to PCE buffer
    502   \channelConfig Current channel configuration
    503   \alignAnchor Anchor for byte alignment
    504 
    505   \return  PCE status (-1: fail, 0: no new PCE, 1: PCE updated, 2: PCE updated
    506   need re-config).
    507 */
    508 static int CProgramConfigElement_Read(HANDLE_FDK_BITSTREAM bs,
    509                                       HANDLE_TRANSPORTDEC pTp,
    510                                       CProgramConfig *pce,
    511                                       const UINT channelConfig,
    512                                       const UINT alignAnchor) {
    513   int pceStatus = 0;
    514   int crcReg;
    515 
    516   /* read PCE to temporal buffer first */
    517   C_ALLOC_SCRATCH_START(tmpPce, CProgramConfig, 1);
    518 
    519   CProgramConfig_Init(tmpPce);
    520 
    521   crcReg = transportDec_CrcStartReg(pTp, 0);
    522 
    523   CProgramConfig_Read(tmpPce, bs, alignAnchor);
    524 
    525   transportDec_CrcEndReg(pTp, crcReg);
    526 
    527   if (CProgramConfig_IsValid(tmpPce) && (tmpPce->Profile == 1)) {
    528     if (!CProgramConfig_IsValid(pce) && (channelConfig > 0)) {
    529       /* Create a standard channel config PCE to compare with */
    530       CProgramConfig_GetDefault(pce, channelConfig);
    531     }
    532 
    533     if (CProgramConfig_IsValid(pce)) {
    534       /* Compare the new and the old PCE (tags ignored) */
    535       switch (CProgramConfig_Compare(pce, tmpPce)) {
    536         case 1: /* Channel configuration not changed. Just new metadata. */
    537           FDKmemcpy(pce, tmpPce,
    538                     sizeof(CProgramConfig)); /* Store the complete PCE */
    539           pceStatus = 1; /* New PCE but no change of config */
    540           break;
    541         case 2: /* The number of channels are identical but not the config */
    542           if (channelConfig == 0) {
    543             FDKmemcpy(pce, tmpPce,
    544                       sizeof(CProgramConfig)); /* Store the complete PCE */
    545             pceStatus = 2; /* Decoder needs re-configuration */
    546           }
    547           break;
    548         case -1: /* The channel configuration is completely different */
    549           pceStatus = -1; /* Not supported! */
    550           break;
    551         case 0: /* Nothing to do because PCE matches the old one exactly. */
    552         default:
    553           /* pceStatus = 0; */
    554           break;
    555       }
    556     }
    557   }
    558 
    559   C_ALLOC_SCRATCH_END(tmpPce, CProgramConfig, 1);
    560 
    561   return pceStatus;
    562 }
    563 
    564 /*!
    565   \brief Prepares crossfade for USAC DASH IPF config change
    566 
    567   \pTimeData             Pointer to time data
    568   \pTimeDataFlush        Pointer to flushed time data
    569   \numChannels           Number of channels
    570   \frameSize             Size of frame
    571   \interleaved           Indicates if time data is interleaved
    572 
    573   \return  Error code
    574 */
    575 LINKSPEC_CPP AAC_DECODER_ERROR CAacDecoder_PrepareCrossFade(
    576     const INT_PCM *pTimeData, INT_PCM **pTimeDataFlush, const INT numChannels,
    577     const INT frameSize, const INT interleaved) {
    578   int i, ch, s1, s2;
    579   AAC_DECODER_ERROR ErrorStatus;
    580 
    581   ErrorStatus = AAC_DEC_OK;
    582 
    583   if (interleaved) {
    584     s1 = 1;
    585     s2 = numChannels;
    586   } else {
    587     s1 = frameSize;
    588     s2 = 1;
    589   }
    590 
    591   for (ch = 0; ch < numChannels; ch++) {
    592     const INT_PCM *pIn = &pTimeData[ch * s1];
    593     for (i = 0; i < TIME_DATA_FLUSH_SIZE; i++) {
    594       pTimeDataFlush[ch][i] = *pIn;
    595       pIn += s2;
    596     }
    597   }
    598 
    599   return ErrorStatus;
    600 }
    601 
    602 /*!
    603   \brief Applies crossfade for USAC DASH IPF config change
    604 
    605   \pTimeData             Pointer to time data
    606   \pTimeDataFlush        Pointer to flushed time data
    607   \numChannels           Number of channels
    608   \frameSize             Size of frame
    609   \interleaved           Indicates if time data is interleaved
    610 
    611   \return  Error code
    612 */
    613 LINKSPEC_CPP AAC_DECODER_ERROR CAacDecoder_ApplyCrossFade(
    614     INT_PCM *pTimeData, INT_PCM **pTimeDataFlush, const INT numChannels,
    615     const INT frameSize, const INT interleaved) {
    616   int i, ch, s1, s2;
    617   AAC_DECODER_ERROR ErrorStatus;
    618 
    619   ErrorStatus = AAC_DEC_OK;
    620 
    621   if (interleaved) {
    622     s1 = 1;
    623     s2 = numChannels;
    624   } else {
    625     s1 = frameSize;
    626     s2 = 1;
    627   }
    628 
    629   for (ch = 0; ch < numChannels; ch++) {
    630     INT_PCM *pIn = &pTimeData[ch * s1];
    631     for (i = 0; i < TIME_DATA_FLUSH_SIZE; i++) {
    632       FIXP_SGL alpha = (FIXP_SGL)i
    633                        << (FRACT_BITS - 1 - TIME_DATA_FLUSH_SIZE_SF);
    634       FIXP_DBL time = FX_PCM2FX_DBL(*pIn);
    635       FIXP_DBL timeFlush = FX_PCM2FX_DBL(pTimeDataFlush[ch][i]);
    636 
    637       *pIn = (INT_PCM)(FIXP_PCM)FX_DBL2FX_PCM(
    638           timeFlush - fMult(timeFlush, alpha) + fMult(time, alpha));
    639       pIn += s2;
    640     }
    641   }
    642 
    643   return ErrorStatus;
    644 }
    645 
    646 /*!
    647   \brief Parse PreRoll Extension Payload
    648 
    649   \self             Handle of AAC decoder
    650   \numPrerollAU     Number of preRoll AUs
    651   \prerollAUOffset  Offset to each preRoll AU
    652   \prerollAULength  Length of each preRoll AU
    653 
    654   \return  Error code
    655 */
    656 LINKSPEC_CPP AAC_DECODER_ERROR CAacDecoder_PreRollExtensionPayloadParse(
    657     HANDLE_AACDECODER self, UINT *numPrerollAU, UINT *prerollAUOffset,
    658     UINT *prerollAULength) {
    659   FDK_BITSTREAM bs;
    660   HANDLE_FDK_BITSTREAM hBs;
    661   AAC_DECODER_ERROR ErrorStatus;
    662 
    663   INT auStartAnchor;
    664   UINT independencyFlag;
    665   UINT extPayloadPresentFlag;
    666   UINT useDefaultLengthFlag;
    667   UINT configLength = 0;
    668   UINT preRollPossible = 1;
    669   UINT i;
    670   UCHAR configChanged = 0;
    671   UCHAR config[TP_USAC_MAX_CONFIG_LEN] = {0};
    672   UCHAR
    673   implicitExplicitCfgDiff = 0; /* in case implicit and explicit config is
    674                                   equal preroll AU's should be processed
    675                                   after decoder reset */
    676 
    677   ErrorStatus = AAC_DEC_OK;
    678 
    679   hBs = transportDec_GetBitstream(self->hInput, 0);
    680   bs = *hBs;
    681 
    682   auStartAnchor = (INT)FDKgetValidBits(hBs);
    683   if (auStartAnchor <= 0) {
    684     ErrorStatus = AAC_DEC_NOT_ENOUGH_BITS;
    685     goto bail;
    686   }
    687 
    688   /* Independency flag */
    689   FDKreadBit(hBs);
    690 
    691   /* Payload present flag of extension ID_EXT_ELE_AUDIOPREROLL must be one */
    692   extPayloadPresentFlag = FDKreadBits(hBs, 1);
    693   if (!extPayloadPresentFlag) {
    694     preRollPossible = 0;
    695   }
    696 
    697   /* Default length flag of extension ID_EXT_ELE_AUDIOPREROLL must be zero */
    698   useDefaultLengthFlag = FDKreadBits(hBs, 1);
    699   if (useDefaultLengthFlag) {
    700     preRollPossible = 0;
    701   }
    702 
    703   if (preRollPossible) { /* extPayloadPresentFlag && !useDefaultLengthFlag */
    704     /* Read overall ext payload length, useDefaultLengthFlag must be zero.  */
    705     escapedValue(hBs, 8, 16, 0);
    706 
    707     /* Read RSVD60 Config size */
    708     configLength = escapedValue(hBs, 4, 4, 8);
    709 
    710     /* Avoid decoding pre roll frames if there was no config change and no
    711      * config is included in the pre roll ext payload. */
    712   }
    713 
    714   /* If pre roll not possible then exit. */
    715   if (preRollPossible == 0) {
    716     /* Sanity check: if flushing is switched on, preRollPossible must be 1 */
    717     if (self->flushStatus != AACDEC_FLUSH_OFF) {
    718       /* Mismatch of current payload and flushing status */
    719       self->flushStatus = AACDEC_FLUSH_OFF;
    720       ErrorStatus = AAC_DEC_PARSE_ERROR;
    721     }
    722     goto bail;
    723   }
    724 
    725   if (self->flags[0] & AC_USAC) {
    726     if (configLength > 0) {
    727       /* DASH IPF USAC Config Change: Read new config and compare with current
    728        * config. Apply reconfiguration if config's are different. */
    729       for (i = 0; i < configLength; i++) {
    730         config[i] = FDKreadBits(hBs, 8);
    731       }
    732       TRANSPORTDEC_ERROR terr;
    733       terr = transportDec_InBandConfig(self->hInput, config, configLength,
    734                                        self->buildUpStatus, &configChanged, 0,
    735                                        &implicitExplicitCfgDiff);
    736       if (terr != TRANSPORTDEC_OK) {
    737         ErrorStatus = AAC_DEC_PARSE_ERROR;
    738         goto bail;
    739       }
    740     }
    741   }
    742 
    743   /* For the first frame buildUpStatus is not set and no flushing is performed
    744    * but preroll AU's should processed. */
    745   /* For USAC there is no idle state. */
    746   if ((self->streamInfo.numChannels == 0) && !implicitExplicitCfgDiff &&
    747       (self->flags[0] & AC_USAC)) {
    748     self->buildUpStatus = AACDEC_USAC_BUILD_UP_ON;
    749     /* sanity check: if buildUp status on -> flushing must be off */
    750     if (self->flushStatus != AACDEC_FLUSH_OFF) {
    751       self->flushStatus = AACDEC_FLUSH_OFF;
    752       ErrorStatus = AAC_DEC_PARSE_ERROR;
    753       goto bail;
    754     }
    755   }
    756 
    757   if (self->flags[0] & AC_USAC) {
    758     /* We are interested in preroll AUs if an explicit or an implicit config
    759      * change is signalized in other words if the build up status is set. */
    760     if (self->buildUpStatus == AACDEC_USAC_BUILD_UP_ON) {
    761       self->applyCrossfade |= FDKreadBit(hBs);
    762       FDKreadBit(hBs); /* reserved */
    763       /* Read num preroll AU's */
    764       *numPrerollAU = escapedValue(hBs, 2, 4, 0);
    765       /* check limits for USAC */
    766       if (*numPrerollAU > AACDEC_MAX_NUM_PREROLL_AU_USAC) {
    767         *numPrerollAU = 0;
    768         ErrorStatus = AAC_DEC_PARSE_ERROR;
    769         goto bail;
    770       }
    771     }
    772   }
    773 
    774   for (i = 0; i < *numPrerollAU; i++) {
    775     /* For every AU get length and offset in the bitstream */
    776     prerollAULength[i] = escapedValue(hBs, 16, 16, 0);
    777     if (prerollAULength[i] > 0) {
    778       prerollAUOffset[i] = auStartAnchor - FDKgetValidBits(hBs);
    779       independencyFlag = FDKreadBit(hBs);
    780       if (i == 0 && !independencyFlag) {
    781         *numPrerollAU = 0;
    782         ErrorStatus = AAC_DEC_PARSE_ERROR;
    783         goto bail;
    784       }
    785       FDKpushFor(hBs, prerollAULength[i] * 8 - 1);
    786       self->prerollAULength[i] = (prerollAULength[i] * 8) + prerollAUOffset[i];
    787     } else {
    788       *numPrerollAU = 0;
    789       ErrorStatus = AAC_DEC_PARSE_ERROR; /* Something is wrong */
    790       goto bail;
    791     }
    792   }
    793 
    794 bail:
    795 
    796   *hBs = bs;
    797 
    798   return ErrorStatus;
    799 }
    800 
    801 /*!
    802   \brief Parse Extension Payload
    803 
    804   \self Handle of AAC decoder
    805   \count Pointer to bit counter.
    806   \previous_element ID of previous element (required by some extension payloads)
    807 
    808   \return  Error code
    809 */
    810 static AAC_DECODER_ERROR CAacDecoder_ExtPayloadParse(
    811     HANDLE_AACDECODER self, HANDLE_FDK_BITSTREAM hBs, int *count,
    812     MP4_ELEMENT_ID previous_element, int elIndex, int fIsFillElement) {
    813   AAC_DECODER_ERROR error = AAC_DEC_OK;
    814   EXT_PAYLOAD_TYPE extension_type;
    815   int bytes = (*count) >> 3;
    816   int crcFlag = 0;
    817 
    818   if (*count < 4) {
    819     return AAC_DEC_PARSE_ERROR;
    820   } else if ((INT)FDKgetValidBits(hBs) < *count) {
    821     return AAC_DEC_DECODE_FRAME_ERROR;
    822   }
    823 
    824   extension_type =
    825       (EXT_PAYLOAD_TYPE)FDKreadBits(hBs, 4); /* bs_extension_type */
    826   *count -= 4;
    827 
    828   /* For ELD, the SBR signaling is explicit and parsed in
    829      aacDecoder_ParseExplicitMpsAndSbr(), therefore skip SBR if implicit
    830      present. */
    831   if ((self->flags[0] & AC_ELD) && ((extension_type == EXT_SBR_DATA_CRC) ||
    832                                     (extension_type == EXT_SBR_DATA))) {
    833     extension_type = EXT_FIL; /* skip sbr data */
    834   }
    835 
    836   switch (extension_type) {
    837     case EXT_DYNAMIC_RANGE: {
    838       INT readBits =
    839           aacDecoder_drcMarkPayload(self->hDrcInfo, hBs, MPEG_DRC_EXT_DATA);
    840 
    841       if (readBits > *count) { /* Read too much. Something went wrong! */
    842         error = AAC_DEC_PARSE_ERROR;
    843       }
    844       *count -= readBits;
    845     } break;
    846     case EXT_UNI_DRC: {
    847       DRC_DEC_ERROR drcErr = DRC_DEC_OK;
    848       DRC_DEC_CODEC_MODE drcDecCodecMode = DRC_DEC_CODEC_MODE_UNDEFINED;
    849       INT nBitsRemaining = FDKgetValidBits(hBs);
    850       INT readBits;
    851 
    852       switch (self->streamInfo.aot) {
    853         case AOT_AAC_LC:
    854         case AOT_SBR:
    855         case AOT_PS:
    856           drcDecCodecMode = DRC_DEC_MPEG_4_AAC;
    857           break;
    858         default:
    859           error = AAC_DEC_PARSE_ERROR;
    860           goto bail;
    861       }
    862 
    863       drcErr = FDK_drcDec_SetCodecMode(self->hUniDrcDecoder, drcDecCodecMode);
    864       if (drcErr) {
    865         error = AAC_DEC_PARSE_ERROR;
    866         goto bail;
    867       }
    868 
    869       drcErr = FDK_drcDec_ReadUniDrc(self->hUniDrcDecoder, hBs);
    870       if (drcErr) {
    871         error = AAC_DEC_PARSE_ERROR;
    872         goto bail;
    873       }
    874       readBits = (INT)nBitsRemaining - (INT)FDKgetValidBits(hBs);
    875       if (readBits > *count) { /* Read too much. Something went wrong! */
    876         error = AAC_DEC_PARSE_ERROR;
    877       }
    878       *count -= readBits;
    879       /* Skip any trailing bits */
    880       FDKpushFor(hBs, *count);
    881       *count = 0;
    882     } break;
    883     case EXT_LDSAC_DATA:
    884     case EXT_SAC_DATA:
    885       /* Read MPEG Surround Extension payload */
    886       {
    887         int err, mpsSampleRate, mpsFrameSize;
    888 
    889         if (self->flags[0] & AC_PS_PRESENT) {
    890           error = AAC_DEC_PARSE_ERROR;
    891           goto bail;
    892         }
    893 
    894         /* Handle SBR dual rate case */
    895         if (self->streamInfo.extSamplingRate != 0) {
    896           mpsSampleRate = self->streamInfo.extSamplingRate;
    897           mpsFrameSize = self->streamInfo.aacSamplesPerFrame *
    898                          (self->streamInfo.extSamplingRate /
    899                           self->streamInfo.aacSampleRate);
    900         } else {
    901           mpsSampleRate = self->streamInfo.aacSampleRate;
    902           mpsFrameSize = self->streamInfo.aacSamplesPerFrame;
    903         }
    904         /* Setting of internal MPS state; may be reset in
    905            CAacDecoder_SyncQmfMode if decoder is unable to decode with user
    906            defined qmfMode */
    907         if (!(self->flags[0] & (AC_USAC | AC_RSVD50 | AC_ELD))) {
    908           self->mpsEnableCurr = self->mpsEnableUser;
    909         }
    910         if (self->mpsEnableCurr) {
    911           if (!self->qmfDomain.globalConf.qmfDomainExplicitConfig) {
    912             /* if not done yet, allocate full MPEG Surround decoder instance */
    913             if (mpegSurroundDecoder_IsFullMpegSurroundDecoderInstanceAvailable(
    914                     (CMpegSurroundDecoder *)self->pMpegSurroundDecoder) ==
    915                 SAC_INSTANCE_NOT_FULL_AVAILABLE) {
    916               if (mpegSurroundDecoder_Open(
    917                       (CMpegSurroundDecoder **)&self->pMpegSurroundDecoder, -1,
    918                       &self->qmfDomain)) {
    919                 return AAC_DEC_OUT_OF_MEMORY;
    920               }
    921             }
    922           }
    923           err = mpegSurroundDecoder_Parse(
    924               (CMpegSurroundDecoder *)self->pMpegSurroundDecoder, hBs, count,
    925               self->streamInfo.aot, mpsSampleRate, mpsFrameSize,
    926               self->flags[0] & AC_INDEP);
    927           if (err == MPS_OK) {
    928             self->flags[0] |= AC_MPS_PRESENT;
    929           } else {
    930             error = AAC_DEC_PARSE_ERROR;
    931           }
    932         }
    933         /* Skip any trailing bytes */
    934         FDKpushFor(hBs, *count);
    935         *count = 0;
    936       }
    937       break;
    938 
    939     case EXT_SBR_DATA_CRC:
    940       crcFlag = 1;
    941     case EXT_SBR_DATA:
    942       if (IS_CHANNEL_ELEMENT(previous_element)) {
    943         SBR_ERROR sbrError;
    944         UCHAR configMode = 0;
    945         UCHAR configChanged = 0;
    946 
    947         CAacDecoder_SyncQmfMode(self);
    948 
    949         configMode |= AC_CM_ALLOC_MEM;
    950 
    951         sbrError = sbrDecoder_InitElement(
    952             self->hSbrDecoder, self->streamInfo.aacSampleRate,
    953             self->streamInfo.extSamplingRate,
    954             self->streamInfo.aacSamplesPerFrame, self->streamInfo.aot,
    955             previous_element, elIndex,
    956             2, /* Signalize that harmonicSBR shall be ignored in the config
    957                   change detection */
    958             0, configMode, &configChanged, self->downscaleFactor);
    959 
    960         if (sbrError == SBRDEC_OK) {
    961           sbrError = sbrDecoder_Parse(self->hSbrDecoder, hBs,
    962                                       self->pDrmBsBuffer, self->drmBsBufferSize,
    963                                       count, *count, crcFlag, previous_element,
    964                                       elIndex, self->flags[0], self->elFlags);
    965           /* Enable SBR for implicit SBR signalling but only if no severe error
    966            * happend. */
    967           if ((sbrError == SBRDEC_OK) || (sbrError == SBRDEC_PARSE_ERROR)) {
    968             self->sbrEnabled = 1;
    969           }
    970         } else {
    971           /* Do not try to apply SBR because initializing the element failed. */
    972           self->sbrEnabled = 0;
    973         }
    974         /* Citation from ISO/IEC 14496-3 chapter 4.5.2.1.5.2
    975         Fill elements containing an extension_payload() with an extension_type
    976         of EXT_SBR_DATA or EXT_SBR_DATA_CRC shall not contain any other
    977         extension_payload of any other extension_type.
    978         */
    979         if (fIsFillElement) {
    980           FDKpushBiDirectional(hBs, *count);
    981           *count = 0;
    982         } else {
    983           /* If this is not a fill element with a known length, we are screwed
    984            * and further parsing makes no sense. */
    985           if (sbrError != SBRDEC_OK) {
    986             self->frameOK = 0;
    987           }
    988         }
    989       } else {
    990         error = AAC_DEC_PARSE_ERROR;
    991       }
    992       break;
    993 
    994     case EXT_FILL_DATA: {
    995       int temp;
    996 
    997       temp = FDKreadBits(hBs, 4);
    998       bytes--;
    999       if (temp != 0) {
   1000         error = AAC_DEC_PARSE_ERROR;
   1001         break;
   1002       }
   1003       while (bytes > 0) {
   1004         temp = FDKreadBits(hBs, 8);
   1005         bytes--;
   1006         if (temp != 0xa5) {
   1007           error = AAC_DEC_PARSE_ERROR;
   1008           break;
   1009         }
   1010       }
   1011       *count = bytes << 3;
   1012     } break;
   1013 
   1014     case EXT_DATA_ELEMENT: {
   1015       int dataElementVersion;
   1016 
   1017       dataElementVersion = FDKreadBits(hBs, 4);
   1018       *count -= 4;
   1019       if (dataElementVersion == 0) /* ANC_DATA */
   1020       {
   1021         int temp, dataElementLength = 0;
   1022         do {
   1023           temp = FDKreadBits(hBs, 8);
   1024           *count -= 8;
   1025           dataElementLength += temp;
   1026         } while (temp == 255);
   1027 
   1028         CAacDecoder_AncDataParse(&self->ancData, hBs, dataElementLength);
   1029         *count -= (dataElementLength << 3);
   1030       } else {
   1031         /* align = 0 */
   1032         error = AAC_DEC_PARSE_ERROR;
   1033         goto bail;
   1034       }
   1035     } break;
   1036 
   1037     case EXT_DATA_LENGTH:
   1038       if (!fIsFillElement /* Makes no sens to have an additional length in a
   1039                              fill ...   */
   1040           &&
   1041           (self->flags[0] &
   1042            AC_ER)) /* ... element because this extension payload type was ... */
   1043       { /* ... created to circumvent the missing length in ER-Syntax. */
   1044         int bitCnt, len = FDKreadBits(hBs, 4);
   1045         *count -= 4;
   1046 
   1047         if (len == 15) {
   1048           int add_len = FDKreadBits(hBs, 8);
   1049           *count -= 8;
   1050           len += add_len;
   1051 
   1052           if (add_len == 255) {
   1053             len += FDKreadBits(hBs, 16);
   1054             *count -= 16;
   1055           }
   1056         }
   1057         len <<= 3;
   1058         bitCnt = len;
   1059 
   1060         if ((EXT_PAYLOAD_TYPE)FDKreadBits(hBs, 4) == EXT_DATA_LENGTH) {
   1061           /* Check NOTE 2: The extension_payload() included here must
   1062                            not have extension_type == EXT_DATA_LENGTH. */
   1063           error = AAC_DEC_PARSE_ERROR;
   1064           goto bail;
   1065         } else {
   1066           /* rewind and call myself again. */
   1067           FDKpushBack(hBs, 4);
   1068 
   1069           error = CAacDecoder_ExtPayloadParse(
   1070               self, hBs, &bitCnt, previous_element, elIndex,
   1071               1); /* Treat same as fill element */
   1072 
   1073           *count -= len - bitCnt;
   1074         }
   1075         /* Note: the fall through in case the if statement above is not taken is
   1076          * intentional. */
   1077         break;
   1078       }
   1079 
   1080     case EXT_FIL:
   1081 
   1082     default:
   1083       /* align = 4 */
   1084       FDKpushFor(hBs, *count);
   1085       *count = 0;
   1086       break;
   1087   }
   1088 
   1089 bail:
   1090   if ((error != AAC_DEC_OK) &&
   1091       fIsFillElement) { /* Skip the remaining extension bytes */
   1092     FDKpushBiDirectional(hBs, *count);
   1093     *count = 0;
   1094     /* Patch error code because decoding can go on. */
   1095     error = AAC_DEC_OK;
   1096     /* Be sure that parsing errors have been stored. */
   1097   }
   1098   return error;
   1099 }
   1100 
   1101 static AAC_DECODER_ERROR aacDecoder_ParseExplicitMpsAndSbr(
   1102     HANDLE_AACDECODER self, HANDLE_FDK_BITSTREAM bs,
   1103     const MP4_ELEMENT_ID previous_element, const int previous_element_index,
   1104     const int element_index, const int el_cnt[]) {
   1105   AAC_DECODER_ERROR ErrorStatus = AAC_DEC_OK;
   1106   INT bitCnt = 0;
   1107 
   1108   /* get the remaining bits of this frame */
   1109   bitCnt = transportDec_GetAuBitsRemaining(self->hInput, 0);
   1110 
   1111   if ((bitCnt > 0) && (self->flags[0] & AC_SBR_PRESENT) &&
   1112       (self->flags[0] & (AC_USAC | AC_RSVD50 | AC_ELD | AC_DRM))) {
   1113     SBR_ERROR err = SBRDEC_OK;
   1114     int chElIdx, numChElements = el_cnt[ID_SCE] + el_cnt[ID_CPE] +
   1115                                  el_cnt[ID_LFE] + el_cnt[ID_USAC_SCE] +
   1116                                  el_cnt[ID_USAC_CPE] + el_cnt[ID_USAC_LFE];
   1117 
   1118     if (self->flags[0] & AC_USAC) {
   1119       chElIdx = numChElements - 1;
   1120     } else {
   1121       chElIdx = 0; /* ELD case */
   1122     }
   1123 
   1124     for (; chElIdx < numChElements; chElIdx += 1) {
   1125       MP4_ELEMENT_ID sbrType;
   1126       if (self->flags[0] & (AC_USAC)) {
   1127         FDK_ASSERT((self->elements[element_index] == ID_USAC_SCE) ||
   1128                    (self->elements[element_index] == ID_USAC_CPE));
   1129         sbrType = IS_STEREO_SBR(self->elements[element_index],
   1130                                 self->usacStereoConfigIndex[element_index])
   1131                       ? ID_CPE
   1132                       : ID_SCE;
   1133       } else
   1134         sbrType = self->elements[chElIdx];
   1135       err = sbrDecoder_Parse(self->hSbrDecoder, bs, self->pDrmBsBuffer,
   1136                              self->drmBsBufferSize, &bitCnt, -1,
   1137                              self->flags[0] & AC_SBRCRC, sbrType, chElIdx,
   1138                              self->flags[0], self->elFlags);
   1139       if (err != SBRDEC_OK) {
   1140         break;
   1141       }
   1142     }
   1143     switch (err) {
   1144       case SBRDEC_PARSE_ERROR:
   1145         /* Can not go on parsing because we do not
   1146             know the length of the SBR extension data. */
   1147         FDKpushFor(bs, bitCnt);
   1148         bitCnt = 0;
   1149         break;
   1150       case SBRDEC_OK:
   1151         self->sbrEnabled = 1;
   1152         break;
   1153       default:
   1154         self->frameOK = 0;
   1155         break;
   1156     }
   1157   }
   1158 
   1159   if ((bitCnt > 0) && (self->flags[0] & (AC_USAC | AC_RSVD50))) {
   1160     if ((self->flags[0] & AC_MPS_PRESENT) ||
   1161         (self->elFlags[element_index] & AC_EL_USAC_MPS212)) {
   1162       int err;
   1163 
   1164       err = mpegSurroundDecoder_ParseNoHeader(
   1165           (CMpegSurroundDecoder *)self->pMpegSurroundDecoder, bs, &bitCnt,
   1166           self->flags[0] & AC_INDEP);
   1167       if (err != MPS_OK) {
   1168         self->frameOK = 0;
   1169         ErrorStatus = AAC_DEC_PARSE_ERROR;
   1170       }
   1171     }
   1172   }
   1173 
   1174   if (self->flags[0] & AC_DRM) {
   1175     if ((bitCnt = (INT)FDKgetValidBits(bs)) != 0) {
   1176       FDKpushBiDirectional(bs, bitCnt);
   1177     }
   1178   }
   1179 
   1180   if (!(self->flags[0] & (AC_USAC | AC_RSVD50 | AC_DRM))) {
   1181     while (bitCnt > 7) {
   1182       ErrorStatus = CAacDecoder_ExtPayloadParse(
   1183           self, bs, &bitCnt, previous_element, previous_element_index, 0);
   1184       if (ErrorStatus != AAC_DEC_OK) {
   1185         self->frameOK = 0;
   1186         ErrorStatus = AAC_DEC_PARSE_ERROR;
   1187         break;
   1188       }
   1189     }
   1190   }
   1191   return ErrorStatus;
   1192 }
   1193 
   1194 /*  Stream Configuration and Information.
   1195 
   1196     This class holds configuration and information data for a stream to be
   1197    decoded. It provides the calling application as well as the decoder with
   1198    substantial information, e.g. profile, sampling rate, number of channels
   1199    found in the bitstream etc.
   1200 */
   1201 static void CStreamInfoInit(CStreamInfo *pStreamInfo) {
   1202   pStreamInfo->aacSampleRate = 0;
   1203   pStreamInfo->profile = -1;
   1204   pStreamInfo->aot = AOT_NONE;
   1205 
   1206   pStreamInfo->channelConfig = -1;
   1207   pStreamInfo->bitRate = 0;
   1208   pStreamInfo->aacSamplesPerFrame = 0;
   1209 
   1210   pStreamInfo->extAot = AOT_NONE;
   1211   pStreamInfo->extSamplingRate = 0;
   1212 
   1213   pStreamInfo->flags = 0;
   1214 
   1215   pStreamInfo->epConfig = -1; /* default: no ER */
   1216 
   1217   pStreamInfo->numChannels = 0;
   1218   pStreamInfo->sampleRate = 0;
   1219   pStreamInfo->frameSize = 0;
   1220 
   1221   pStreamInfo->outputDelay = 0;
   1222 
   1223   /* DRC */
   1224   pStreamInfo->drcProgRefLev =
   1225       -1; /* set program reference level to not indicated */
   1226   pStreamInfo->drcPresMode = -1; /* default: presentation mode not indicated */
   1227 }
   1228 
   1229 /*!
   1230   \brief Initialization of AacDecoderChannelInfo
   1231 
   1232   The function initializes the pointers to AacDecoderChannelInfo for each
   1233   channel, set the start values for window shape and window sequence of
   1234   overlap&add to zero, set the overlap buffer to zero and initializes the
   1235   pointers to the window coefficients. \param bsFormat is the format of the AAC
   1236   bitstream
   1237 
   1238   \return  AACDECODER instance
   1239 */
   1240 LINKSPEC_CPP HANDLE_AACDECODER CAacDecoder_Open(
   1241     TRANSPORT_TYPE bsFormat) /*!< bitstream format (adif,adts,loas,...). */
   1242 {
   1243   HANDLE_AACDECODER self;
   1244 
   1245   self = GetAacDecoder();
   1246   if (self == NULL) {
   1247     goto bail;
   1248   }
   1249 
   1250   FDK_QmfDomain_ClearRequested(&self->qmfDomain.globalConf);
   1251 
   1252   /* Assign channel mapping info arrays (doing so removes dependency of settings
   1253    * header in API header). */
   1254   self->streamInfo.pChannelIndices = self->channelIndices;
   1255   self->streamInfo.pChannelType = self->channelType;
   1256   self->downscaleFactor = 1;
   1257   self->downscaleFactorInBS = 1;
   1258 
   1259   /* initialize anc data */
   1260   CAacDecoder_AncDataInit(&self->ancData, NULL, 0);
   1261 
   1262   /* initialize stream info */
   1263   CStreamInfoInit(&self->streamInfo);
   1264 
   1265   /* initialize progam config */
   1266   CProgramConfig_Init(&self->pce);
   1267 
   1268   /* initialize error concealment common data */
   1269   CConcealment_InitCommonData(&self->concealCommonData);
   1270   self->concealMethodUser = ConcealMethodNone; /* undefined -> auto mode */
   1271 
   1272   self->hDrcInfo = GetDrcInfo();
   1273   if (self->hDrcInfo == NULL) {
   1274     goto bail;
   1275   }
   1276   /* Init common DRC structure */
   1277   aacDecoder_drcInit(self->hDrcInfo);
   1278   /* Set default frame delay */
   1279   aacDecoder_drcSetParam(self->hDrcInfo, DRC_BS_DELAY,
   1280                          CConcealment_GetDelay(&self->concealCommonData));
   1281 
   1282   self->workBufferCore2 = GetWorkBufferCore2();
   1283   if (self->workBufferCore2 == NULL) goto bail;
   1284 
   1285   /* When RSVD60 is active use dedicated memory for core decoding */
   1286   self->pTimeData2 = GetWorkBufferCore5();
   1287   self->timeData2Size = GetRequiredMemWorkBufferCore5();
   1288   if (self->pTimeData2 == NULL) {
   1289     goto bail;
   1290   }
   1291 
   1292   return self;
   1293 
   1294 bail:
   1295   CAacDecoder_Close(self);
   1296 
   1297   return NULL;
   1298 }
   1299 
   1300 /* Revert CAacDecoder_Init() */
   1301 static void CAacDecoder_DeInit(HANDLE_AACDECODER self,
   1302                                const int subStreamIndex) {
   1303   int ch;
   1304   int aacChannelOffset = 0, aacChannels = (8);
   1305   int numElements = (((8)) + (8)), elementOffset = 0;
   1306 
   1307   if (self == NULL) return;
   1308 
   1309   {
   1310     self->ascChannels[0] = 0;
   1311     self->elements[0] = ID_END;
   1312   }
   1313 
   1314   for (ch = aacChannelOffset; ch < aacChannelOffset + aacChannels; ch++) {
   1315     if (self->pAacDecoderChannelInfo[ch] != NULL) {
   1316       if (self->pAacDecoderChannelInfo[ch]->pComStaticData != NULL) {
   1317         if (self->pAacDecoderChannelInfo[ch]
   1318                 ->pComStaticData->pWorkBufferCore1 != NULL) {
   1319           if (ch == aacChannelOffset) {
   1320             FreeWorkBufferCore1(&self->pAacDecoderChannelInfo[ch]
   1321                                      ->pComStaticData->pWorkBufferCore1);
   1322           }
   1323         }
   1324         if (self->pAacDecoderChannelInfo[ch]
   1325                 ->pComStaticData->cplxPredictionData != NULL) {
   1326           FreeCplxPredictionData(&self->pAacDecoderChannelInfo[ch]
   1327                                       ->pComStaticData->cplxPredictionData);
   1328         }
   1329         /* Avoid double free of linked pComStaticData in case of CPE by settings
   1330          * pointer to NULL. */
   1331         if (ch < (8) - 1) {
   1332           if ((self->pAacDecoderChannelInfo[ch + 1] != NULL) &&
   1333               (self->pAacDecoderChannelInfo[ch + 1]->pComStaticData ==
   1334                self->pAacDecoderChannelInfo[ch]->pComStaticData)) {
   1335             self->pAacDecoderChannelInfo[ch + 1]->pComStaticData = NULL;
   1336           }
   1337         }
   1338         FDKfree(self->pAacDecoderChannelInfo[ch]->pComStaticData);
   1339         self->pAacDecoderChannelInfo[ch]->pComStaticData = NULL;
   1340       }
   1341       if (self->pAacDecoderChannelInfo[ch]->pComData != NULL) {
   1342         /* Avoid double free of linked pComData in case of CPE by settings
   1343          * pointer to NULL. */
   1344         if (ch < (8) - 1) {
   1345           if ((self->pAacDecoderChannelInfo[ch + 1] != NULL) &&
   1346               (self->pAacDecoderChannelInfo[ch + 1]->pComData ==
   1347                self->pAacDecoderChannelInfo[ch]->pComData)) {
   1348             self->pAacDecoderChannelInfo[ch + 1]->pComData = NULL;
   1349           }
   1350         }
   1351         if (ch == aacChannelOffset) {
   1352           FreeWorkBufferCore6(
   1353               (SCHAR **)&self->pAacDecoderChannelInfo[ch]->pComData);
   1354         } else {
   1355           FDKafree(self->pAacDecoderChannelInfo[ch]->pComData);
   1356         }
   1357         self->pAacDecoderChannelInfo[ch]->pComData = NULL;
   1358       }
   1359     }
   1360     if (self->pAacDecoderStaticChannelInfo[ch] != NULL) {
   1361       if (self->pAacDecoderStaticChannelInfo[ch]->pOverlapBuffer != NULL) {
   1362         FreeOverlapBuffer(
   1363             &self->pAacDecoderStaticChannelInfo[ch]->pOverlapBuffer);
   1364       }
   1365       if (self->pAacDecoderStaticChannelInfo[ch]->hArCo != NULL) {
   1366         CArco_Destroy(self->pAacDecoderStaticChannelInfo[ch]->hArCo);
   1367       }
   1368       FreeAacDecoderStaticChannelInfo(&self->pAacDecoderStaticChannelInfo[ch]);
   1369     }
   1370     if (self->pAacDecoderChannelInfo[ch] != NULL) {
   1371       FreeAacDecoderChannelInfo(&self->pAacDecoderChannelInfo[ch]);
   1372     }
   1373   }
   1374 
   1375   {
   1376     int el;
   1377     for (el = elementOffset; el < elementOffset + numElements; el++) {
   1378       if (self->cpeStaticData[el] != NULL) {
   1379         FreeCpePersistentData(&self->cpeStaticData[el]);
   1380       }
   1381     }
   1382   }
   1383 
   1384   FDK_Delay_Destroy(&self->usacResidualDelay);
   1385 
   1386   self->aacChannels = 0;
   1387   self->streamInfo.aacSampleRate = 0;
   1388   self->streamInfo.sampleRate = 0;
   1389   /* This samplerate value is checked for configuration change, not the others
   1390    * above. */
   1391   self->samplingRateInfo[subStreamIndex].samplingRate = 0;
   1392 }
   1393 
   1394 /*!
   1395  * \brief CAacDecoder_CtrlCFGChange Set config change parameters.
   1396  *
   1397  * \param self           [i]   handle to AACDECODER structure
   1398  * \param flushStatus    [i]   flush status: on|off
   1399  * \param flushCnt       [i]   flush frame counter
   1400  * \param buildUpStatus  [i]   build up status: on|off
   1401  * \param buildUpCnt     [i]   build up frame counter
   1402  *
   1403  * \return error
   1404  */
   1405 LINKSPEC_CPP AAC_DECODER_ERROR CAacDecoder_CtrlCFGChange(HANDLE_AACDECODER self,
   1406                                                          UCHAR flushStatus,
   1407                                                          SCHAR flushCnt,
   1408                                                          UCHAR buildUpStatus,
   1409                                                          SCHAR buildUpCnt) {
   1410   AAC_DECODER_ERROR err = AAC_DEC_OK;
   1411 
   1412   self->flushStatus = flushStatus;
   1413   self->flushCnt = flushCnt;
   1414   self->buildUpStatus = buildUpStatus;
   1415   self->buildUpCnt = buildUpCnt;
   1416 
   1417   return (err);
   1418 }
   1419 
   1420 /*!
   1421  * \brief CAacDecoder_FreeMem Free config dependent AAC memory.
   1422  *
   1423  * \param self       [i]   handle to AACDECODER structure
   1424  *
   1425  * \return error
   1426  */
   1427 LINKSPEC_CPP AAC_DECODER_ERROR CAacDecoder_FreeMem(HANDLE_AACDECODER self,
   1428                                                    const int subStreamIndex) {
   1429   AAC_DECODER_ERROR err = AAC_DEC_OK;
   1430 
   1431   CAacDecoder_DeInit(self, subStreamIndex);
   1432 
   1433   return (err);
   1434 }
   1435 
   1436 /* Destroy aac decoder */
   1437 LINKSPEC_CPP void CAacDecoder_Close(HANDLE_AACDECODER self) {
   1438   if (self == NULL) return;
   1439 
   1440   CAacDecoder_DeInit(self, 0);
   1441 
   1442   {
   1443     int ch;
   1444     for (ch = 0; ch < (8); ch++) {
   1445       if (self->pTimeDataFlush[ch] != NULL) {
   1446         FreeTimeDataFlush(&self->pTimeDataFlush[ch]);
   1447       }
   1448     }
   1449   }
   1450 
   1451   if (self->hDrcInfo) {
   1452     FreeDrcInfo(&self->hDrcInfo);
   1453   }
   1454 
   1455   /* Free WorkBufferCore2 */
   1456   if (self->workBufferCore2 != NULL) {
   1457     FreeWorkBufferCore2(&self->workBufferCore2);
   1458   }
   1459   if (self->pTimeData2 != NULL) {
   1460     FreeWorkBufferCore5(&self->pTimeData2);
   1461   }
   1462 
   1463   FDK_QmfDomain_Close(&self->qmfDomain);
   1464 
   1465   FreeAacDecoder(&self);
   1466 }
   1467 
   1468 /*!
   1469   \brief Initialization of decoder instance
   1470 
   1471   The function initializes the decoder.
   1472 
   1473   \return  error status: 0 for success, <>0 for unsupported configurations
   1474 */
   1475 LINKSPEC_CPP AAC_DECODER_ERROR
   1476 CAacDecoder_Init(HANDLE_AACDECODER self, const CSAudioSpecificConfig *asc,
   1477                  UCHAR configMode, UCHAR *configChanged) {
   1478   AAC_DECODER_ERROR err = AAC_DEC_OK;
   1479   INT ascChannels, ascChanged = 0;
   1480   AACDEC_RENDER_MODE initRenderMode = AACDEC_RENDER_INVALID;
   1481   SCHAR usacStereoConfigIndex = -1;
   1482   int usacResidualDelayCompSamples = 0;
   1483   int elementOffset, aacChannelsOffset, aacChannelsOffsetIdx;
   1484   const int streamIndex = 0;
   1485   INT flushChannels = 0;
   1486 
   1487   if (!self) return AAC_DEC_INVALID_HANDLE;
   1488 
   1489   UCHAR downscaleFactor = self->downscaleFactor;
   1490   UCHAR downscaleFactorInBS = self->downscaleFactorInBS;
   1491 
   1492   // set profile and check for supported aot
   1493   // leave profile on default (=-1) for all other supported MPEG-4 aot's except
   1494   // aot=2 (=AAC-LC)
   1495   switch (asc->m_aot) {
   1496     case AOT_AAC_LC:
   1497       self->streamInfo.profile = 1;
   1498     case AOT_ER_AAC_SCAL:
   1499       if (asc->m_sc.m_gaSpecificConfig.m_layer > 0) {
   1500         /* aac_scalable_extension_element() currently not supported. */
   1501         return AAC_DEC_UNSUPPORTED_FORMAT;
   1502       }
   1503     case AOT_SBR:
   1504     case AOT_PS:
   1505     case AOT_ER_AAC_LC:
   1506     case AOT_ER_AAC_LD:
   1507     case AOT_DRM_AAC:
   1508     case AOT_DRM_SURROUND:
   1509       initRenderMode = AACDEC_RENDER_IMDCT;
   1510       break;
   1511     case AOT_ER_AAC_ELD:
   1512       initRenderMode = AACDEC_RENDER_ELDFB;
   1513       break;
   1514     case AOT_USAC:
   1515       initRenderMode = AACDEC_RENDER_IMDCT;
   1516       break;
   1517     default:
   1518       return AAC_DEC_UNSUPPORTED_AOT;
   1519   }
   1520 
   1521   if (CProgramConfig_IsValid(&self->pce) && (asc->m_channelConfiguration > 0)) {
   1522     /* Compare the stored (old) PCE with a default PCE created from the (new)
   1523        channel_config (on a temporal buffer) to find out wheter we can keep it
   1524        (and its metadata) or not. */
   1525     int pceCmpResult;
   1526     C_ALLOC_SCRATCH_START(tmpPce, CProgramConfig, 1);
   1527 
   1528     CProgramConfig_GetDefault(tmpPce, asc->m_channelConfiguration);
   1529     pceCmpResult = CProgramConfig_Compare(&self->pce, tmpPce);
   1530     if ((pceCmpResult < 0) /* Reset if PCEs are completely different ... */
   1531         ||
   1532         (pceCmpResult > 1)) { /*            ... or have a different layout. */
   1533       CProgramConfig_Init(&self->pce);
   1534     } /* Otherwise keep the PCE (and its metadata). */
   1535     C_ALLOC_SCRATCH_END(tmpPce, CProgramConfig, 1);
   1536   } else {
   1537     CProgramConfig_Init(&self->pce);
   1538   }
   1539 
   1540   /* set channels */
   1541   switch (asc->m_channelConfiguration) {
   1542     case 0:
   1543       switch (asc->m_aot) {
   1544         case AOT_USAC:
   1545           self->chMapIndex = 0;
   1546           ascChannels = asc->m_sc.m_usacConfig.m_nUsacChannels;
   1547           break;
   1548         default:
   1549           /* get channels from program config (ASC) */
   1550           if (CProgramConfig_IsValid(&asc->m_progrConfigElement)) {
   1551             ascChannels = asc->m_progrConfigElement.NumChannels;
   1552             if (ascChannels > 0) {
   1553               int el_tmp;
   1554               /* valid number of channels -> copy program config element (PCE)
   1555                * from ASC */
   1556               FDKmemcpy(&self->pce, &asc->m_progrConfigElement,
   1557                         sizeof(CProgramConfig));
   1558               /* Built element table */
   1559               el_tmp = CProgramConfig_GetElementTable(
   1560                   &asc->m_progrConfigElement, self->elements, (((8)) + (8)),
   1561                   &self->chMapIndex);
   1562               for (; el_tmp < (3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 1);
   1563                    el_tmp++) {
   1564                 self->elements[el_tmp] = ID_NONE;
   1565               }
   1566             } else {
   1567               return AAC_DEC_UNSUPPORTED_CHANNELCONFIG;
   1568             }
   1569           } else {
   1570             self->chMapIndex = 0;
   1571             return AAC_DEC_UNSUPPORTED_CHANNELCONFIG;
   1572           }
   1573           break;
   1574       }
   1575       break;
   1576     case 1:
   1577     case 2:
   1578     case 3:
   1579     case 4:
   1580     case 5:
   1581     case 6:
   1582       ascChannels = asc->m_channelConfiguration;
   1583       break;
   1584     case 11:
   1585       ascChannels = 7;
   1586       break;
   1587     case 7:
   1588     case 12:
   1589     case 14:
   1590       ascChannels = 8;
   1591       break;
   1592     default:
   1593       return AAC_DEC_UNSUPPORTED_CHANNELCONFIG;
   1594   }
   1595 
   1596   if (asc->m_aot == AOT_USAC) {
   1597     flushChannels = fMin(ascChannels, (8));
   1598     INT numChannel;
   1599     pcmDmx_GetParam(self->hPcmUtils, MIN_NUMBER_OF_OUTPUT_CHANNELS,
   1600                     &numChannel);
   1601     flushChannels = fMin(fMax(numChannel, flushChannels), (8));
   1602   }
   1603 
   1604   if (IS_USAC(asc->m_aot)) {
   1605     for (int el = 0; el < (INT)asc->m_sc.m_usacConfig.m_usacNumElements; el++) {
   1606       /* fix number of core channels aka ascChannels for stereoConfigIndex = 1
   1607        * cases */
   1608       if (asc->m_sc.m_usacConfig.element[el].m_stereoConfigIndex == 1) {
   1609         ascChannels--; /* stereoConfigIndex == 1 stereo cases do actually
   1610                           contain only a mono core channel. */
   1611       } else if (asc->m_sc.m_usacConfig.element[el].m_stereoConfigIndex == 2) {
   1612         /* In this case it is necessary to follow up the DMX signal delay caused
   1613            by HBE also with the residual signal (2nd core channel). The SBR
   1614            overlap delay is not regarded here, this is handled by the MPS212
   1615            implementation.
   1616         */
   1617         if (asc->m_sc.m_usacConfig.element[el].m_harmonicSBR) {
   1618           usacResidualDelayCompSamples += asc->m_samplesPerFrame;
   1619         }
   1620         if (asc->m_sc.m_usacConfig.m_coreSbrFrameLengthIndex == 4) {
   1621           usacResidualDelayCompSamples +=
   1622               6 * 16; /* difference between 12 SBR
   1623                          overlap slots from SBR and 6
   1624                          slots delayed in MPS212 */
   1625         }
   1626       }
   1627     }
   1628   }
   1629 
   1630   aacChannelsOffset = 0;
   1631   aacChannelsOffsetIdx = 0;
   1632   elementOffset = 0;
   1633   if (configMode & AC_CM_ALLOC_MEM) {
   1634     if ((ascChannels <= 0) ||
   1635         (asc->m_channelConfiguration > AACDEC_MAX_CH_CONF)) {
   1636       return AAC_DEC_UNSUPPORTED_CHANNELCONFIG;
   1637     }
   1638     if ((ascChannels + aacChannelsOffsetIdx) > ((8) * 2)) {
   1639       return AAC_DEC_UNSUPPORTED_CHANNELCONFIG;
   1640     }
   1641     if ((ascChannels + aacChannelsOffset) > (8)) {
   1642       return AAC_DEC_UNSUPPORTED_CHANNELCONFIG;
   1643     }
   1644   }
   1645 
   1646   /* Set syntax flags */
   1647   self->flags[streamIndex] = 0;
   1648   { FDKmemclear(self->elFlags, sizeof(self->elFlags)); }
   1649 
   1650   if ((asc->m_channelConfiguration > 0) || IS_USAC(asc->m_aot)) {
   1651     if (IS_USAC(asc->m_aot)) {
   1652       /* copy pointer to usac config
   1653         (this is preliminary since there's an ongoing discussion about storing
   1654         the config-part of the bitstream rather than the complete decoded
   1655         configuration) */
   1656       self->pUsacConfig[streamIndex] = &asc->m_sc.m_usacConfig;
   1657 
   1658       /* copy list of elements */
   1659       if (self->pUsacConfig[streamIndex]->m_usacNumElements >
   1660           (3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 1)) {
   1661         goto bail;
   1662       }
   1663 
   1664       if (self->numUsacElements[streamIndex] !=
   1665           asc->m_sc.m_usacConfig.m_usacNumElements) {
   1666         ascChanged = 1;
   1667       }
   1668 
   1669       if (configMode & AC_CM_ALLOC_MEM) {
   1670         self->numUsacElements[streamIndex] =
   1671             asc->m_sc.m_usacConfig.m_usacNumElements;
   1672       }
   1673 
   1674       self->mpsEnableCurr = 0;
   1675       for (int _el = 0;
   1676            _el < (int)self->pUsacConfig[streamIndex]->m_usacNumElements;
   1677            _el++) {
   1678         int el = _el + elementOffset;
   1679         if (self->elements[el] !=
   1680             self->pUsacConfig[streamIndex]->element[_el].usacElementType) {
   1681           ascChanged = 1;
   1682         }
   1683         if (self->usacStereoConfigIndex[el] !=
   1684             asc->m_sc.m_usacConfig.element[_el].m_stereoConfigIndex) {
   1685           ascChanged = 1;
   1686         }
   1687         if (configMode & AC_CM_ALLOC_MEM) {
   1688           self->elements[el] =
   1689               self->pUsacConfig[streamIndex]->element[_el].usacElementType;
   1690           /* for Unified Stereo Coding */
   1691           self->usacStereoConfigIndex[el] =
   1692               asc->m_sc.m_usacConfig.element[_el].m_stereoConfigIndex;
   1693           if (self->elements[el] == ID_USAC_CPE) {
   1694             self->mpsEnableCurr |= self->usacStereoConfigIndex[el] ? 1 : 0;
   1695           }
   1696         }
   1697 
   1698         self->elFlags[el] |=
   1699             (asc->m_sc.m_usacConfig.element[_el].m_noiseFilling)
   1700                 ? AC_EL_USAC_NOISE
   1701                 : 0;
   1702         self->elFlags[el] |=
   1703             (asc->m_sc.m_usacConfig.element[_el].m_stereoConfigIndex > 0)
   1704                 ? AC_EL_USAC_MPS212
   1705                 : 0;
   1706         self->elFlags[el] |= (asc->m_sc.m_usacConfig.element[_el].m_interTes)
   1707                                  ? AC_EL_USAC_ITES
   1708                                  : 0;
   1709         self->elFlags[el] |=
   1710             (asc->m_sc.m_usacConfig.element[_el].m_pvc) ? AC_EL_USAC_PVC : 0;
   1711         self->elFlags[el] |=
   1712             (asc->m_sc.m_usacConfig.element[_el].usacElementType == ID_USAC_LFE)
   1713                 ? AC_EL_USAC_LFE
   1714                 : 0;
   1715         self->elFlags[el] |=
   1716             (asc->m_sc.m_usacConfig.element[_el].usacElementType == ID_USAC_LFE)
   1717                 ? AC_EL_LFE
   1718                 : 0;
   1719         if ((asc->m_sc.m_usacConfig.element[_el].usacElementType ==
   1720              ID_USAC_CPE) &&
   1721             ((self->usacStereoConfigIndex[el] == 0))) {
   1722           self->elFlags[el] |= AC_EL_USAC_CP_POSSIBLE;
   1723         }
   1724       }
   1725 
   1726       self->hasAudioPreRoll = 0;
   1727       if (self->pUsacConfig[streamIndex]->m_usacNumElements) {
   1728         self->hasAudioPreRoll = asc->m_sc.m_usacConfig.element[0]
   1729                                     .extElement.usacExtElementHasAudioPreRoll;
   1730       }
   1731       if (configMode & AC_CM_ALLOC_MEM) {
   1732         self->elements[elementOffset +
   1733                        self->pUsacConfig[streamIndex]->m_usacNumElements] =
   1734             ID_END;
   1735       }
   1736     } else {
   1737       /* Initialize constant mappings for channel config 1-7 */
   1738       int i;
   1739       for (i = 0; i < AACDEC_CH_ELEMENTS_TAB_SIZE; i++) {
   1740         self->elements[i] = elementsTab[asc->m_channelConfiguration - 1][i];
   1741       }
   1742       for (; i < (3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 1); i++) {
   1743         self->elements[i] = ID_NONE;
   1744       }
   1745     }
   1746 
   1747     {
   1748       int ch;
   1749 
   1750       for (ch = 0; ch < ascChannels; ch++) {
   1751         self->chMapping[ch] = ch;
   1752       }
   1753       for (; ch < (8); ch++) {
   1754         self->chMapping[ch] = 255;
   1755       }
   1756     }
   1757 
   1758     self->chMapIndex = asc->m_channelConfiguration;
   1759   } else {
   1760     if (CProgramConfig_IsValid(&asc->m_progrConfigElement)) {
   1761       /* Set matrix mixdown infos if available from PCE. */
   1762       pcmDmx_SetMatrixMixdownFromPce(
   1763           self->hPcmUtils, asc->m_progrConfigElement.MatrixMixdownIndexPresent,
   1764           asc->m_progrConfigElement.MatrixMixdownIndex,
   1765           asc->m_progrConfigElement.PseudoSurroundEnable);
   1766     }
   1767   }
   1768 
   1769   self->streamInfo.channelConfig = asc->m_channelConfiguration;
   1770 
   1771   if (self->streamInfo.aot != asc->m_aot) {
   1772     if (configMode & AC_CM_ALLOC_MEM) {
   1773       self->streamInfo.aot = asc->m_aot;
   1774     }
   1775     ascChanged = 1;
   1776   }
   1777 
   1778   if (asc->m_aot == AOT_ER_AAC_ELD &&
   1779       asc->m_sc.m_eldSpecificConfig.m_downscaledSamplingFrequency != 0) {
   1780     if (self->samplingRateInfo[0].samplingRate !=
   1781             asc->m_sc.m_eldSpecificConfig.m_downscaledSamplingFrequency ||
   1782         self->samplingRateInfo[0].samplingRate * self->downscaleFactor !=
   1783             asc->m_samplingFrequency) {
   1784       /* get downscaledSamplingFrequency from ESC and compute the downscale
   1785        * factor */
   1786       downscaleFactorInBS =
   1787           asc->m_samplingFrequency /
   1788           asc->m_sc.m_eldSpecificConfig.m_downscaledSamplingFrequency;
   1789       if (downscaleFactorInBS == 1 || downscaleFactorInBS == 2 ||
   1790           downscaleFactorInBS == 3 || downscaleFactorInBS == 4) {
   1791         downscaleFactor = downscaleFactorInBS;
   1792       }
   1793     }
   1794   } else {
   1795     downscaleFactorInBS = 1;
   1796     downscaleFactor = 1;
   1797   }
   1798 
   1799   if (self->downscaleFactorInBS != downscaleFactorInBS) {
   1800     if (configMode & AC_CM_ALLOC_MEM) {
   1801       self->downscaleFactorInBS = downscaleFactorInBS;
   1802       self->downscaleFactor = downscaleFactor;
   1803     }
   1804     ascChanged = 1;
   1805   }
   1806 
   1807   if ((INT)asc->m_samplesPerFrame % downscaleFactor != 0) {
   1808     return AAC_DEC_UNSUPPORTED_SAMPLINGRATE; /* frameSize/dsf must be an integer
   1809                                                 number */
   1810   }
   1811 
   1812   self->streamInfo.bitRate = 0;
   1813 
   1814   if (asc->m_aot == AOT_ER_AAC_ELD) {
   1815     if (self->useLdQmfTimeAlign !=
   1816         asc->m_sc.m_eldSpecificConfig.m_useLdQmfTimeAlign) {
   1817       ascChanged = 1;
   1818     }
   1819     if (configMode & AC_CM_ALLOC_MEM) {
   1820       self->useLdQmfTimeAlign =
   1821           asc->m_sc.m_eldSpecificConfig.m_useLdQmfTimeAlign;
   1822     }
   1823   }
   1824 
   1825   self->streamInfo.extAot = asc->m_extensionAudioObjectType;
   1826   if (self->streamInfo.extSamplingRate !=
   1827       (INT)asc->m_extensionSamplingFrequency) {
   1828     ascChanged = 1;
   1829   }
   1830   if (configMode & AC_CM_ALLOC_MEM) {
   1831     self->streamInfo.extSamplingRate = asc->m_extensionSamplingFrequency;
   1832   }
   1833   self->flags[streamIndex] |= (asc->m_sbrPresentFlag) ? AC_SBR_PRESENT : 0;
   1834   self->flags[streamIndex] |= (asc->m_psPresentFlag) ? AC_PS_PRESENT : 0;
   1835   if (asc->m_sbrPresentFlag) {
   1836     self->sbrEnabled = 1;
   1837     self->sbrEnabledPrev = 1;
   1838   } else {
   1839     self->sbrEnabled = 0;
   1840     self->sbrEnabledPrev = 0;
   1841   }
   1842   if (self->sbrEnabled && asc->m_extensionSamplingFrequency) {
   1843     if (downscaleFactor != 1 && (downscaleFactor)&1) {
   1844       return AAC_DEC_UNSUPPORTED_SAMPLINGRATE; /* SBR needs an even downscale
   1845                                                   factor */
   1846     }
   1847     if (configMode & AC_CM_ALLOC_MEM) {
   1848       self->streamInfo.extSamplingRate =
   1849           self->streamInfo.extSamplingRate / self->downscaleFactor;
   1850     }
   1851   }
   1852 
   1853   /* --------- vcb11 ------------ */
   1854   self->flags[streamIndex] |= (asc->m_vcb11Flag) ? AC_ER_VCB11 : 0;
   1855 
   1856   /* ---------- rvlc ------------ */
   1857   self->flags[streamIndex] |= (asc->m_rvlcFlag) ? AC_ER_RVLC : 0;
   1858 
   1859   /* ----------- hcr ------------ */
   1860   self->flags[streamIndex] |= (asc->m_hcrFlag) ? AC_ER_HCR : 0;
   1861 
   1862   if (asc->m_aot == AOT_ER_AAC_ELD) {
   1863     self->mpsEnableCurr = 0;
   1864     self->flags[streamIndex] |= AC_ELD;
   1865     self->flags[streamIndex] |=
   1866         (asc->m_sbrPresentFlag)
   1867             ? AC_SBR_PRESENT
   1868             : 0; /* Need to set the SBR flag for backward-compatibility
   1869        reasons. Even if SBR is not supported. */
   1870     self->flags[streamIndex] |=
   1871         (asc->m_sc.m_eldSpecificConfig.m_sbrCrcFlag) ? AC_SBRCRC : 0;
   1872     self->flags[streamIndex] |=
   1873         (asc->m_sc.m_eldSpecificConfig.m_useLdQmfTimeAlign) ? AC_MPS_PRESENT
   1874                                                             : 0;
   1875     if (self->mpsApplicable) {
   1876       self->mpsEnableCurr = asc->m_sc.m_eldSpecificConfig.m_useLdQmfTimeAlign;
   1877     }
   1878   }
   1879   self->flags[streamIndex] |= (asc->m_aot == AOT_ER_AAC_LD) ? AC_LD : 0;
   1880   self->flags[streamIndex] |= (asc->m_epConfig >= 0) ? AC_ER : 0;
   1881 
   1882   if (asc->m_aot == AOT_USAC) {
   1883     self->flags[streamIndex] |= AC_USAC;
   1884     self->flags[streamIndex] |=
   1885         (asc->m_sc.m_usacConfig.element[0].m_stereoConfigIndex > 0)
   1886             ? AC_MPS_PRESENT
   1887             : 0;
   1888   }
   1889   if (asc->m_aot == AOT_DRM_AAC) {
   1890     self->flags[streamIndex] |= AC_DRM | AC_SBRCRC | AC_SCALABLE;
   1891   }
   1892   if (asc->m_aot == AOT_DRM_SURROUND) {
   1893     self->flags[streamIndex] |=
   1894         AC_DRM | AC_SBRCRC | AC_SCALABLE | AC_MPS_PRESENT;
   1895     FDK_ASSERT(!asc->m_psPresentFlag);
   1896   }
   1897   if ((asc->m_aot == AOT_AAC_SCAL) || (asc->m_aot == AOT_ER_AAC_SCAL)) {
   1898     self->flags[streamIndex] |= AC_SCALABLE;
   1899   }
   1900 
   1901   if ((asc->m_epConfig >= 0) && (asc->m_channelConfiguration <= 0)) {
   1902     /* we have to know the number of channels otherwise no decoding is possible
   1903      */
   1904     return AAC_DEC_UNSUPPORTED_ER_FORMAT;
   1905   }
   1906 
   1907   self->streamInfo.epConfig = asc->m_epConfig;
   1908   /* self->hInput->asc.m_epConfig = asc->m_epConfig; */
   1909 
   1910   if (asc->m_epConfig > 1) return AAC_DEC_UNSUPPORTED_ER_FORMAT;
   1911 
   1912   /* Check if samplerate changed. */
   1913   if ((self->samplingRateInfo[streamIndex].samplingRate !=
   1914        asc->m_samplingFrequency) ||
   1915       (self->streamInfo.aacSamplesPerFrame !=
   1916        (INT)asc->m_samplesPerFrame / downscaleFactor)) {
   1917     AAC_DECODER_ERROR error;
   1918 
   1919     ascChanged = 1;
   1920 
   1921     if (configMode & AC_CM_ALLOC_MEM) {
   1922       /* Update samplerate info. */
   1923       error = getSamplingRateInfo(
   1924           &self->samplingRateInfo[streamIndex], asc->m_samplesPerFrame,
   1925           asc->m_samplingFrequencyIndex, asc->m_samplingFrequency);
   1926       if (error != AAC_DEC_OK) {
   1927         return error;
   1928       }
   1929       self->streamInfo.aacSampleRate =
   1930           self->samplingRateInfo[0].samplingRate / self->downscaleFactor;
   1931       self->streamInfo.aacSamplesPerFrame =
   1932           asc->m_samplesPerFrame / self->downscaleFactor;
   1933     }
   1934   }
   1935 
   1936   /* Check if amount of channels has changed. */
   1937   if (self->ascChannels[streamIndex] != ascChannels) {
   1938     ascChanged = 1;
   1939   }
   1940 
   1941   /* detect config change */
   1942   if (configMode & AC_CM_DET_CFG_CHANGE) {
   1943     if (ascChanged != 0) {
   1944       *configChanged = 1;
   1945     }
   1946     return err;
   1947   }
   1948 
   1949   /* set AC_USAC_SCFGI3 globally if any usac element uses */
   1950   switch (asc->m_aot) {
   1951     case AOT_USAC:
   1952       if (self->sbrEnabled) {
   1953         for (int _el = 0;
   1954              _el < (int)self->pUsacConfig[streamIndex]->m_usacNumElements;
   1955              _el++) {
   1956           int el = elementOffset + _el;
   1957           if (IS_USAC_CHANNEL_ELEMENT(self->elements[el])) {
   1958             if (usacStereoConfigIndex < 0) {
   1959               usacStereoConfigIndex = self->usacStereoConfigIndex[el];
   1960             } else {
   1961               if ((usacStereoConfigIndex != self->usacStereoConfigIndex[el]) ||
   1962                   (self->usacStereoConfigIndex[el] > 0)) {
   1963                 goto bail;
   1964               }
   1965             }
   1966           }
   1967         }
   1968 
   1969         if (usacStereoConfigIndex < 0) {
   1970           goto bail;
   1971         }
   1972 
   1973         if (usacStereoConfigIndex == 3) {
   1974           self->flags[streamIndex] |= AC_USAC_SCFGI3;
   1975         }
   1976       }
   1977       break;
   1978     default:
   1979       break;
   1980   }
   1981 
   1982   if (*configChanged) {
   1983     /* Set up QMF domain for AOTs with explicit signalling of SBR and or MPS.
   1984        This is to be able to play out the first frame alway with the correct
   1985        frame size and sampling rate even in case of concealment.
   1986     */
   1987     switch (asc->m_aot) {
   1988       case AOT_USAC:
   1989         if (self->sbrEnabled) {
   1990           const UCHAR map_sbrRatio_2_nAnaBands[] = {16, 24, 32};
   1991 
   1992           FDK_ASSERT(asc->m_sc.m_usacConfig.m_sbrRatioIndex > 0);
   1993           FDK_ASSERT(streamIndex == 0);
   1994 
   1995           self->qmfDomain.globalConf.nInputChannels_requested = ascChannels;
   1996           self->qmfDomain.globalConf.nOutputChannels_requested =
   1997               (usacStereoConfigIndex == 1) ? 2 : ascChannels;
   1998           self->qmfDomain.globalConf.flags_requested = 0;
   1999           self->qmfDomain.globalConf.nBandsAnalysis_requested =
   2000               map_sbrRatio_2_nAnaBands[asc->m_sc.m_usacConfig.m_sbrRatioIndex -
   2001                                        1];
   2002           self->qmfDomain.globalConf.nBandsSynthesis_requested = 64;
   2003           self->qmfDomain.globalConf.nQmfTimeSlots_requested =
   2004               (asc->m_sc.m_usacConfig.m_sbrRatioIndex == 1) ? 64 : 32;
   2005           self->qmfDomain.globalConf.nQmfOvTimeSlots_requested =
   2006               (asc->m_sc.m_usacConfig.m_sbrRatioIndex == 1) ? 12 : 6;
   2007           self->qmfDomain.globalConf.nQmfProcBands_requested = 64;
   2008           self->qmfDomain.globalConf.nQmfProcChannels_requested = 1;
   2009           self->qmfDomain.globalConf.parkChannel =
   2010               (usacStereoConfigIndex == 3) ? 1 : 0;
   2011           self->qmfDomain.globalConf.parkChannel_requested =
   2012               (usacStereoConfigIndex == 3) ? 1 : 0;
   2013           self->qmfDomain.globalConf.qmfDomainExplicitConfig = 1;
   2014         }
   2015         break;
   2016       case AOT_ER_AAC_ELD:
   2017         if (self->mpsEnableCurr &&
   2018             asc->m_sc.m_eldSpecificConfig.m_useLdQmfTimeAlign) {
   2019           SAC_INPUT_CONFIG sac_interface =
   2020               (self->sbrEnabled && self->hSbrDecoder) ? SAC_INTERFACE_QMF
   2021                                                       : SAC_INTERFACE_TIME;
   2022           mpegSurroundDecoder_ConfigureQmfDomain(
   2023               (CMpegSurroundDecoder *)self->pMpegSurroundDecoder, sac_interface,
   2024               (UINT)self->streamInfo.aacSampleRate, asc->m_aot);
   2025           self->qmfDomain.globalConf.qmfDomainExplicitConfig = 1;
   2026         }
   2027         break;
   2028       default:
   2029         self->qmfDomain.globalConf.qmfDomainExplicitConfig =
   2030             0; /* qmfDomain is initialized by SBR and MPS init functions if
   2031                   required */
   2032         break;
   2033     }
   2034 
   2035     /* Allocate all memory structures for each channel */
   2036     {
   2037       int ch = aacChannelsOffset;
   2038       for (int _ch = 0; _ch < ascChannels; _ch++) {
   2039         if (ch >= (8)) {
   2040           goto bail;
   2041         }
   2042         self->pAacDecoderChannelInfo[ch] = GetAacDecoderChannelInfo(ch);
   2043         /* This is temporary until the DynamicData is split into two or more
   2044            regions! The memory could be reused after completed core decoding. */
   2045         if (self->pAacDecoderChannelInfo[ch] == NULL) {
   2046           goto bail;
   2047         }
   2048         ch++;
   2049       }
   2050 
   2051       int chIdx = aacChannelsOffsetIdx;
   2052       ch = aacChannelsOffset;
   2053       int _numElements;
   2054       _numElements = (((8)) + (8));
   2055       if (self->flags[streamIndex] & (AC_RSV603DA | AC_USAC)) {
   2056         _numElements = (int)asc->m_sc.m_usacConfig.m_usacNumElements;
   2057       }
   2058       if (self->flags[streamIndex] & (AC_ER | AC_LD | AC_ELD)) {
   2059         _numElements = (asc->m_channelConfiguration == 7)
   2060                            ? 8
   2061                            : asc->m_channelConfiguration;
   2062       }
   2063       for (int _el = 0; _el < _numElements; _el++) {
   2064         int el_channels = 0;
   2065         int el = elementOffset + _el;
   2066 
   2067         if (self->flags[streamIndex] &
   2068             (AC_ELD | AC_RSV603DA | AC_USAC | AC_RSVD50)) {
   2069           if (ch >= ascChannels) {
   2070             break;
   2071           }
   2072         }
   2073 
   2074         switch (self->elements[el]) {
   2075           case ID_SCE:
   2076           case ID_CPE:
   2077           case ID_LFE:
   2078           case ID_USAC_SCE:
   2079           case ID_USAC_CPE:
   2080           case ID_USAC_LFE:
   2081 
   2082             el_channels = CAacDecoder_GetELChannels(
   2083                 self->elements[el], self->usacStereoConfigIndex[el]);
   2084 
   2085             {
   2086               self->pAacDecoderChannelInfo[ch]->pComStaticData =
   2087                   (CAacDecoderCommonStaticData *)FDKcalloc(
   2088                       1, sizeof(CAacDecoderCommonStaticData));
   2089               if (self->pAacDecoderChannelInfo[ch]->pComStaticData == NULL) {
   2090                 goto bail;
   2091               }
   2092               if (ch == aacChannelsOffset) {
   2093                 self->pAacDecoderChannelInfo[ch]->pComData =
   2094                     (CAacDecoderCommonData *)GetWorkBufferCore6();
   2095                 self->pAacDecoderChannelInfo[ch]
   2096                     ->pComStaticData->pWorkBufferCore1 = GetWorkBufferCore1();
   2097               } else {
   2098                 self->pAacDecoderChannelInfo[ch]->pComData =
   2099                     (CAacDecoderCommonData *)FDKaalloc(
   2100                         sizeof(CAacDecoderCommonData), ALIGNMENT_DEFAULT);
   2101                 self->pAacDecoderChannelInfo[ch]
   2102                     ->pComStaticData->pWorkBufferCore1 =
   2103                     self->pAacDecoderChannelInfo[aacChannelsOffset]
   2104                         ->pComStaticData->pWorkBufferCore1;
   2105               }
   2106               if ((self->pAacDecoderChannelInfo[ch]->pComData == NULL) ||
   2107                   (self->pAacDecoderChannelInfo[ch]
   2108                        ->pComStaticData->pWorkBufferCore1 == NULL)) {
   2109                 goto bail;
   2110               }
   2111               self->pAacDecoderChannelInfo[ch]->pDynData =
   2112                   &(self->pAacDecoderChannelInfo[ch]
   2113                         ->pComData->pAacDecoderDynamicData[0]);
   2114               self->pAacDecoderChannelInfo[ch]->pSpectralCoefficient =
   2115                   (SPECTRAL_PTR)&self->workBufferCore2[ch * 1024];
   2116 
   2117               if (el_channels == 2) {
   2118                 FDK_ASSERT(ch < (8) - 1);
   2119                 self->pAacDecoderChannelInfo[ch + 1]->pComData =
   2120                     self->pAacDecoderChannelInfo[ch]->pComData;
   2121                 self->pAacDecoderChannelInfo[ch + 1]->pComStaticData =
   2122                     self->pAacDecoderChannelInfo[ch]->pComStaticData;
   2123                 self->pAacDecoderChannelInfo[ch + 1]
   2124                     ->pComStaticData->pWorkBufferCore1 =
   2125                     self->pAacDecoderChannelInfo[ch]
   2126                         ->pComStaticData->pWorkBufferCore1;
   2127                 self->pAacDecoderChannelInfo[ch + 1]->pDynData =
   2128                     &(self->pAacDecoderChannelInfo[ch]
   2129                           ->pComData->pAacDecoderDynamicData[1]);
   2130                 self->pAacDecoderChannelInfo[ch + 1]->pSpectralCoefficient =
   2131                     (SPECTRAL_PTR)&self->workBufferCore2[(ch + 1) * 1024];
   2132               }
   2133 
   2134               ch += el_channels;
   2135             }
   2136             chIdx += el_channels;
   2137             break;
   2138 
   2139           default:
   2140             break;
   2141         }
   2142 
   2143         if (self->elements[el] == ID_END) {
   2144           break;
   2145         }
   2146 
   2147         el++;
   2148       }
   2149 
   2150       chIdx = aacChannelsOffsetIdx;
   2151       ch = aacChannelsOffset;
   2152       for (int _ch = 0; _ch < ascChannels; _ch++) {
   2153         /* Allocate persistent channel memory */
   2154         {
   2155           self->pAacDecoderStaticChannelInfo[ch] =
   2156               GetAacDecoderStaticChannelInfo(ch);
   2157           if (self->pAacDecoderStaticChannelInfo[ch] == NULL) {
   2158             goto bail;
   2159           }
   2160           self->pAacDecoderStaticChannelInfo[ch]->pOverlapBuffer =
   2161               GetOverlapBuffer(ch); /* This area size depends on the AOT */
   2162           if (self->pAacDecoderStaticChannelInfo[ch]->pOverlapBuffer == NULL) {
   2163             goto bail;
   2164           }
   2165           if (self->flags[streamIndex] &
   2166               (AC_USAC | AC_RSVD50 | AC_RSV603DA /*|AC_BSAC*/)) {
   2167             self->pAacDecoderStaticChannelInfo[ch]->hArCo = CArco_Create();
   2168             if (self->pAacDecoderStaticChannelInfo[ch]->hArCo == NULL) {
   2169               goto bail;
   2170             }
   2171           }
   2172 
   2173           if (!(self->flags[streamIndex] & (AC_USAC | AC_RSV603DA))) {
   2174             CPns_UpdateNoiseState(
   2175                 &self->pAacDecoderChannelInfo[ch]->data.aac.PnsData,
   2176                 &self->pAacDecoderStaticChannelInfo[ch]->pnsCurrentSeed,
   2177                 self->pAacDecoderChannelInfo[ch]->pComData->pnsRandomSeed);
   2178           }
   2179           ch++;
   2180         }
   2181         chIdx++;
   2182       }
   2183 
   2184       if (self->flags[streamIndex] & AC_USAC) {
   2185         for (int _ch = 0; _ch < flushChannels; _ch++) {
   2186           ch = aacChannelsOffset + _ch;
   2187           if (self->pTimeDataFlush[ch] == NULL) {
   2188             self->pTimeDataFlush[ch] = GetTimeDataFlush(ch);
   2189             if (self->pTimeDataFlush[ch] == NULL) {
   2190               goto bail;
   2191             }
   2192           }
   2193         }
   2194       }
   2195 
   2196       if (self->flags[streamIndex] & (AC_USAC | AC_RSV603DA)) {
   2197         int complexStereoPredPossible = 0;
   2198         ch = aacChannelsOffset;
   2199         chIdx = aacChannelsOffsetIdx;
   2200         for (int _el2 = 0; _el2 < (int)asc->m_sc.m_usacConfig.m_usacNumElements;
   2201              _el2++) {
   2202           int el2 = elementOffset + _el2;
   2203           int elCh = 0, ch2;
   2204 
   2205           if ((self->elements[el2] == ID_USAC_CPE) &&
   2206               !(self->usacStereoConfigIndex[el2] == 1)) {
   2207             elCh = 2;
   2208           } else if (IS_CHANNEL_ELEMENT(self->elements[el2])) {
   2209             elCh = 1;
   2210           }
   2211 
   2212           if (self->elFlags[el2] & AC_EL_USAC_CP_POSSIBLE) {
   2213             complexStereoPredPossible = 1;
   2214             if (self->cpeStaticData[el2] == NULL) {
   2215               self->cpeStaticData[el2] = GetCpePersistentData();
   2216               if (self->cpeStaticData[el2] == NULL) {
   2217                 goto bail;
   2218               }
   2219             }
   2220           }
   2221 
   2222           for (ch2 = 0; ch2 < elCh; ch2++) {
   2223             /* Hook element specific cpeStaticData into channel specific
   2224              * aacDecoderStaticChannelInfo */
   2225             self->pAacDecoderStaticChannelInfo[ch]->pCpeStaticData =
   2226                 self->cpeStaticData[el2];
   2227             if (self->pAacDecoderStaticChannelInfo[ch]->pCpeStaticData !=
   2228                 NULL) {
   2229               self->pAacDecoderStaticChannelInfo[ch]
   2230                   ->pCpeStaticData->jointStereoPersistentData
   2231                   .spectralCoeffs[ch2] =
   2232                   self->pAacDecoderStaticChannelInfo[ch]
   2233                       ->concealmentInfo.spectralCoefficient;
   2234               self->pAacDecoderStaticChannelInfo[ch]
   2235                   ->pCpeStaticData->jointStereoPersistentData.specScale[ch2] =
   2236                   self->pAacDecoderStaticChannelInfo[ch]
   2237                       ->concealmentInfo.specScale;
   2238               self->pAacDecoderStaticChannelInfo[ch]
   2239                   ->pCpeStaticData->jointStereoPersistentData.scratchBuffer =
   2240                   (FIXP_DBL *)self->pTimeData2;
   2241             }
   2242             chIdx++;
   2243             ch++;
   2244           } /* for each channel in current element */
   2245           if (complexStereoPredPossible && (elCh == 2)) {
   2246             /* needed once for all channels */
   2247             if (self->pAacDecoderChannelInfo[ch - 1]
   2248                     ->pComStaticData->cplxPredictionData == NULL) {
   2249               self->pAacDecoderChannelInfo[ch - 1]
   2250                   ->pComStaticData->cplxPredictionData =
   2251                   GetCplxPredictionData();
   2252             }
   2253             if (self->pAacDecoderChannelInfo[ch - 1]
   2254                     ->pComStaticData->cplxPredictionData == NULL) {
   2255               goto bail;
   2256             }
   2257           }
   2258           if (elCh > 0) {
   2259             self->pAacDecoderStaticChannelInfo[ch - elCh]->nfRandomSeed =
   2260                 (ULONG)0x3039;
   2261             if (self->elements[el2] == ID_USAC_CPE) {
   2262               if (asc->m_sc.m_usacConfig.element[el2].m_stereoConfigIndex !=
   2263                   1) {
   2264                 self->pAacDecoderStaticChannelInfo[ch - elCh + 1]
   2265                     ->nfRandomSeed = (ULONG)0x10932;
   2266               }
   2267             }
   2268           }
   2269         } /* for each element */
   2270       }
   2271 
   2272       if (ascChannels != self->aacChannels) {
   2273         /* Make allocated channel count persistent in decoder context. */
   2274         self->aacChannels = aacChannelsOffset + ch;
   2275       }
   2276     }
   2277 
   2278     if (usacResidualDelayCompSamples) {
   2279       INT delayErr = FDK_Delay_Create(&self->usacResidualDelay,
   2280                                       (USHORT)usacResidualDelayCompSamples, 1);
   2281       if (delayErr) {
   2282         goto bail;
   2283       }
   2284     }
   2285 
   2286     /* Make amount of signalled channels persistent in decoder context. */
   2287     self->ascChannels[streamIndex] = ascChannels;
   2288     /* Init the previous channel count values. This is required to avoid a
   2289        mismatch of memory accesses in the error concealment module and the
   2290        allocated channel structures in this function. */
   2291     self->aacChannelsPrev = 0;
   2292   }
   2293 
   2294   if (self->pAacDecoderChannelInfo[0] != NULL) {
   2295     self->pDrmBsBuffer = self->pAacDecoderChannelInfo[0]
   2296                              ->pComStaticData->pWorkBufferCore1->DrmBsBuffer;
   2297     self->drmBsBufferSize = DRM_BS_BUFFER_SIZE;
   2298   }
   2299 
   2300   /* Update structures */
   2301   if (*configChanged) {
   2302     /* Things to be done for each channel, which do not involve allocating
   2303        memory. Doing these things only on the channels needed for the current
   2304        configuration (ascChannels) could lead to memory access violation later
   2305        (error concealment). */
   2306     int ch = 0;
   2307     int chIdx = 0;
   2308     for (int _ch = 0; _ch < self->ascChannels[streamIndex]; _ch++) {
   2309       switch (self->streamInfo.aot) {
   2310         case AOT_ER_AAC_ELD:
   2311         case AOT_ER_AAC_LD:
   2312           self->pAacDecoderChannelInfo[ch]->granuleLength =
   2313               self->streamInfo.aacSamplesPerFrame;
   2314           break;
   2315         default:
   2316           self->pAacDecoderChannelInfo[ch]->granuleLength =
   2317               self->streamInfo.aacSamplesPerFrame / 8;
   2318           break;
   2319       }
   2320       self->pAacDecoderChannelInfo[ch]->renderMode = initRenderMode;
   2321 
   2322       mdct_init(&self->pAacDecoderStaticChannelInfo[ch]->IMdct,
   2323                 self->pAacDecoderStaticChannelInfo[ch]->pOverlapBuffer,
   2324                 OverlapBufferSize);
   2325 
   2326       self->pAacDecoderStaticChannelInfo[ch]->last_core_mode = FD_LONG;
   2327       self->pAacDecoderStaticChannelInfo[ch]->last_lpd_mode = 255;
   2328 
   2329       self->pAacDecoderStaticChannelInfo[ch]->last_tcx_pitch = L_DIV;
   2330 
   2331       /* Reset DRC control data for this channel */
   2332       aacDecoder_drcInitChannelData(
   2333           &self->pAacDecoderStaticChannelInfo[ch]->drcData);
   2334 
   2335       /* Delete mixdown metadata from the past */
   2336       pcmDmx_Reset(self->hPcmUtils, PCMDMX_RESET_BS_DATA);
   2337 
   2338       /* Reset concealment only if ASC changed. Otherwise it will be done with
   2339          any config callback. E.g. every time the LATM SMC is present. */
   2340       CConcealment_InitChannelData(
   2341           &self->pAacDecoderStaticChannelInfo[ch]->concealmentInfo,
   2342           &self->concealCommonData, initRenderMode,
   2343           self->streamInfo.aacSamplesPerFrame);
   2344       ch++;
   2345       chIdx++;
   2346     }
   2347   }
   2348 
   2349   /* Update externally visible copy of flags */
   2350   self->streamInfo.flags = self->flags[0];
   2351 
   2352   if (*configChanged) {
   2353     int drcDecSampleRate, drcDecFrameSize;
   2354 
   2355     if (self->streamInfo.extSamplingRate != 0) {
   2356       drcDecSampleRate = self->streamInfo.extSamplingRate;
   2357       drcDecFrameSize = (self->streamInfo.aacSamplesPerFrame *
   2358                          self->streamInfo.extSamplingRate) /
   2359                         self->streamInfo.aacSampleRate;
   2360     } else {
   2361       drcDecSampleRate = self->streamInfo.aacSampleRate;
   2362       drcDecFrameSize = self->streamInfo.aacSamplesPerFrame;
   2363     }
   2364 
   2365     if (FDK_drcDec_Init(self->hUniDrcDecoder, drcDecFrameSize, drcDecSampleRate,
   2366                         self->aacChannels) != 0)
   2367       goto bail;
   2368   }
   2369 
   2370   if (asc->m_aot == AOT_USAC) {
   2371     pcmLimiter_SetAttack(self->hLimiter, (5));
   2372     pcmLimiter_SetThreshold(self->hLimiter, FL2FXCONST_DBL(0.89125094f));
   2373   }
   2374 
   2375   return err;
   2376 
   2377 bail:
   2378   CAacDecoder_DeInit(self, 0);
   2379   return AAC_DEC_OUT_OF_MEMORY;
   2380 }
   2381 
   2382 LINKSPEC_CPP AAC_DECODER_ERROR CAacDecoder_DecodeFrame(
   2383     HANDLE_AACDECODER self, const UINT flags, FIXP_PCM *pTimeData,
   2384     const INT timeDataSize, const int timeDataChannelOffset) {
   2385   AAC_DECODER_ERROR ErrorStatus = AAC_DEC_OK;
   2386 
   2387   CProgramConfig *pce;
   2388   HANDLE_FDK_BITSTREAM bs = transportDec_GetBitstream(self->hInput, 0);
   2389 
   2390   MP4_ELEMENT_ID type = ID_NONE; /* Current element type */
   2391   INT aacChannels = 0; /* Channel counter for channels found in the bitstream */
   2392   const int streamIndex = 0; /* index of the current substream */
   2393 
   2394   INT auStartAnchor = (INT)FDKgetValidBits(
   2395       bs); /* AU start bit buffer position for AU byte alignment */
   2396 
   2397   INT checkSampleRate = self->streamInfo.aacSampleRate;
   2398 
   2399   INT CConceal_TDFading_Applied[(8)] = {
   2400       0}; /* Initialize status of Time Domain fading */
   2401 
   2402   if (self->aacChannels <= 0) {
   2403     return AAC_DEC_UNSUPPORTED_CHANNELCONFIG;
   2404   }
   2405 
   2406   /* Any supported base layer valid AU will require more than 16 bits. */
   2407   if ((transportDec_GetAuBitsRemaining(self->hInput, 0) < 15) &&
   2408       (flags & (AACDEC_CONCEAL | AACDEC_FLUSH)) == 0) {
   2409     self->frameOK = 0;
   2410     ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR;
   2411   }
   2412 
   2413   /* Reset Program Config structure */
   2414   pce = &self->pce;
   2415   CProgramConfig_Reset(pce);
   2416 
   2417   CAacDecoder_AncDataReset(&self->ancData);
   2418   if (!(flags & (AACDEC_CONCEAL | AACDEC_FLUSH)) &&
   2419       !(self->flags[0] & (AC_USAC | AC_RSV603DA))) {
   2420     int ch;
   2421     if (self->streamInfo.channelConfig == 0) {
   2422       /* Init Channel/Element mapping table */
   2423       for (ch = 0; ch < (8); ch++) {
   2424         self->chMapping[ch] = 255;
   2425       }
   2426       if (!CProgramConfig_IsValid(pce)) {
   2427         int el;
   2428         for (el = 0; el < (3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 1);
   2429              el++) {
   2430           self->elements[el] = ID_NONE;
   2431         }
   2432       }
   2433     }
   2434   }
   2435 
   2436   if (self->downscaleFactor > 1 && (self->flags[0] & AC_ELD)) {
   2437     self->flags[0] |= AC_ELD_DOWNSCALE;
   2438   } else {
   2439     self->flags[0] &= ~AC_ELD_DOWNSCALE;
   2440   }
   2441   /* unsupported dsf (aacSampleRate has not yet been divided by dsf) -> divide
   2442    */
   2443   if (self->downscaleFactorInBS > 1 &&
   2444       (self->flags[0] & AC_ELD_DOWNSCALE) == 0) {
   2445     checkSampleRate =
   2446         self->streamInfo.aacSampleRate / self->downscaleFactorInBS;
   2447   }
   2448 
   2449   /* Check sampling frequency  */
   2450   if (self->streamInfo.aacSampleRate <= 0) {
   2451     /* Instance maybe uninitialized! */
   2452     return AAC_DEC_UNSUPPORTED_SAMPLINGRATE;
   2453   }
   2454   switch (checkSampleRate) {
   2455     case 96000:
   2456     case 88200:
   2457     case 64000:
   2458     case 16000:
   2459     case 12000:
   2460     case 11025:
   2461     case 8000:
   2462     case 7350:
   2463     case 48000:
   2464     case 44100:
   2465     case 32000:
   2466     case 24000:
   2467     case 22050:
   2468       break;
   2469     default:
   2470       if (!(self->flags[0] & (AC_USAC | AC_RSVD50 | AC_RSV603DA))) {
   2471         return AAC_DEC_UNSUPPORTED_SAMPLINGRATE;
   2472       }
   2473       break;
   2474   }
   2475 
   2476   if (flags & AACDEC_CLRHIST) {
   2477     if (!(self->flags[0] & AC_USAC)) {
   2478       int ch;
   2479       /* Clear history */
   2480       for (ch = 0; ch < self->aacChannels; ch++) {
   2481         /* Reset concealment */
   2482         CConcealment_InitChannelData(
   2483             &self->pAacDecoderStaticChannelInfo[ch]->concealmentInfo,
   2484             &self->concealCommonData,
   2485             self->pAacDecoderChannelInfo[0]->renderMode,
   2486             self->streamInfo.aacSamplesPerFrame);
   2487         /* Clear overlap-add buffers to avoid clicks. */
   2488         FDKmemclear(self->pAacDecoderStaticChannelInfo[ch]->pOverlapBuffer,
   2489                     OverlapBufferSize * sizeof(FIXP_DBL));
   2490       }
   2491       if (self->streamInfo.channelConfig > 0) {
   2492         /* Declare the possibly adopted old PCE (with outdated metadata)
   2493          * invalid. */
   2494         CProgramConfig_Init(pce);
   2495       }
   2496     }
   2497   }
   2498 
   2499   int pceRead = 0; /* Flag indicating a PCE in the current raw_data_block() */
   2500 
   2501   INT hdaacDecoded = 0;
   2502   MP4_ELEMENT_ID previous_element =
   2503       ID_END; /* Last element ID (required for extension payload mapping */
   2504   UCHAR previous_element_index = 0; /* Canonical index of last element */
   2505   int element_count =
   2506       0; /* Element counter for elements found in the bitstream */
   2507   int channel_element_count = 0; /* Channel element counter */
   2508   MP4_ELEMENT_ID
   2509   channel_elements[(3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) +
   2510                     1)];     /* Channel elements in bit stream order. */
   2511   int el_cnt[ID_LAST] = {0}; /* element counter ( robustness ) */
   2512   int element_count_prev_streams =
   2513       0; /* Element count of all previous sub streams. */
   2514 
   2515   while ((type != ID_END) && (!(flags & (AACDEC_CONCEAL | AACDEC_FLUSH))) &&
   2516          self->frameOK) {
   2517     int el_channels;
   2518 
   2519     if (!(self->flags[0] &
   2520           (AC_USAC | AC_RSVD50 | AC_RSV603DA | AC_ELD | AC_SCALABLE | AC_ER)))
   2521       type = (MP4_ELEMENT_ID)FDKreadBits(bs, 3);
   2522     else
   2523       type = self->elements[element_count];
   2524 
   2525     if ((self->flags[streamIndex] & (AC_USAC | AC_RSVD50) &&
   2526          element_count == 0) ||
   2527         (self->flags[streamIndex] & AC_RSV603DA)) {
   2528       self->flags[streamIndex] &= ~AC_INDEP;
   2529 
   2530       if (FDKreadBit(bs)) {
   2531         self->flags[streamIndex] |= AC_INDEP;
   2532       }
   2533 
   2534       int ch = aacChannels;
   2535       for (int chIdx = aacChannels; chIdx < self->ascChannels[streamIndex];
   2536            chIdx++) {
   2537         {
   2538           /* Robustness check */
   2539           if (ch >= self->aacChannels) {
   2540             return AAC_DEC_UNKNOWN;
   2541           }
   2542 
   2543           /* if last frame was broken and this frame is no independent frame,
   2544            * correct decoding is impossible we need to trigger concealment */
   2545           if ((CConcealment_GetLastFrameOk(
   2546                    &self->pAacDecoderStaticChannelInfo[ch]->concealmentInfo,
   2547                    1) == 0) &&
   2548               !(self->flags[streamIndex] & AC_INDEP)) {
   2549             self->frameOK = 0;
   2550           }
   2551           ch++;
   2552         }
   2553       }
   2554     }
   2555 
   2556     if ((INT)FDKgetValidBits(bs) < 0) {
   2557       self->frameOK = 0;
   2558     }
   2559 
   2560     switch (type) {
   2561       case ID_SCE:
   2562       case ID_CPE:
   2563       case ID_LFE:
   2564       case ID_USAC_SCE:
   2565       case ID_USAC_CPE:
   2566       case ID_USAC_LFE:
   2567 
   2568         el_channels = CAacDecoder_GetELChannels(
   2569             type, self->usacStereoConfigIndex[element_count]);
   2570 
   2571         /*
   2572           Consistency check
   2573          */
   2574         {
   2575           int totalAscChannels = 0;
   2576 
   2577           for (int i = 0; i < (1 * 1); i++) {
   2578             totalAscChannels += self->ascChannels[i];
   2579           }
   2580           if ((el_cnt[type] >= (totalAscChannels >> (el_channels - 1))) ||
   2581               (aacChannels > (totalAscChannels - el_channels))) {
   2582             ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR;
   2583             self->frameOK = 0;
   2584             break;
   2585           }
   2586         }
   2587 
   2588         if (!(self->flags[streamIndex] & (AC_USAC | AC_RSVD50 | AC_RSV603DA))) {
   2589           int ch;
   2590           for (ch = 0; ch < el_channels; ch += 1) {
   2591             CPns_ResetData(&self->pAacDecoderChannelInfo[aacChannels + ch]
   2592                                 ->data.aac.PnsData,
   2593                            &self->pAacDecoderChannelInfo[aacChannels + ch]
   2594                                 ->pComData->pnsInterChannelData);
   2595           }
   2596         }
   2597 
   2598         if (self->frameOK) {
   2599           ErrorStatus = CChannelElement_Read(
   2600               bs, &self->pAacDecoderChannelInfo[aacChannels],
   2601               &self->pAacDecoderStaticChannelInfo[aacChannels],
   2602               self->streamInfo.aot, &self->samplingRateInfo[streamIndex],
   2603               self->flags[streamIndex], self->elFlags[element_count],
   2604               self->streamInfo.aacSamplesPerFrame, el_channels,
   2605               self->streamInfo.epConfig, self->hInput);
   2606           if (ErrorStatus != AAC_DEC_OK) {
   2607             self->frameOK = 0;
   2608           }
   2609         }
   2610 
   2611         if (self->frameOK) {
   2612           /* Lookup the element and decode it only if it belongs to the current
   2613            * program */
   2614           if (CProgramConfig_LookupElement(
   2615                   pce, self->streamInfo.channelConfig,
   2616                   self->pAacDecoderChannelInfo[aacChannels]->ElementInstanceTag,
   2617                   aacChannels, self->chMapping, self->channelType,
   2618                   self->channelIndices, (8), &previous_element_index,
   2619                   self->elements, type)) {
   2620             channel_elements[channel_element_count++] = type;
   2621             aacChannels += el_channels;
   2622           } else {
   2623             self->frameOK = 0;
   2624           }
   2625           /* Create SBR element for SBR for upsampling for LFE elements,
   2626              and if SBR was implicitly signaled, because the first frame(s)
   2627              may not contain SBR payload (broken encoder, bit errors). */
   2628           if (self->frameOK &&
   2629               ((self->flags[streamIndex] & AC_SBR_PRESENT) ||
   2630                (self->sbrEnabled == 1)) &&
   2631               !(self->flags[streamIndex] &
   2632                 AC_USAC) /* Is done during explicit config set up */
   2633           ) {
   2634             SBR_ERROR sbrError;
   2635             UCHAR configMode = 0;
   2636             UCHAR configChanged = 0;
   2637             configMode |= AC_CM_ALLOC_MEM;
   2638 
   2639             sbrError = sbrDecoder_InitElement(
   2640                 self->hSbrDecoder, self->streamInfo.aacSampleRate,
   2641                 self->streamInfo.extSamplingRate,
   2642                 self->streamInfo.aacSamplesPerFrame, self->streamInfo.aot, type,
   2643                 previous_element_index, 2, /* Signalize that harmonicSBR shall
   2644                                               be ignored in the config change
   2645                                               detection */
   2646                 0, configMode, &configChanged, self->downscaleFactor);
   2647             if (sbrError != SBRDEC_OK) {
   2648               /* Do not try to apply SBR because initializing the element
   2649                * failed. */
   2650               self->sbrEnabled = 0;
   2651             }
   2652           }
   2653         }
   2654 
   2655         el_cnt[type]++;
   2656         if (self->frameOK && (self->flags[streamIndex] & AC_USAC) &&
   2657             (type == ID_USAC_CPE || type == ID_USAC_SCE)) {
   2658           ErrorStatus = aacDecoder_ParseExplicitMpsAndSbr(
   2659               self, bs, previous_element, previous_element_index, element_count,
   2660               el_cnt);
   2661           if (ErrorStatus != AAC_DEC_OK) {
   2662             self->frameOK = 0;
   2663           }
   2664         }
   2665         break;
   2666 
   2667       case ID_CCE:
   2668         /*
   2669           Consistency check
   2670         */
   2671         if (el_cnt[type] > self->ascChannels[streamIndex]) {
   2672           ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR;
   2673           self->frameOK = 0;
   2674           break;
   2675         }
   2676 
   2677         if (self->frameOK) {
   2678           CAacDecoderCommonData commonData;
   2679           CAacDecoderCommonStaticData commonStaticData;
   2680           CWorkBufferCore1 workBufferCore1;
   2681           commonStaticData.pWorkBufferCore1 = &workBufferCore1;
   2682           /* memory for spectral lines temporal on scratch */
   2683           C_AALLOC_SCRATCH_START(mdctSpec, FIXP_DBL, 1024);
   2684 
   2685           /* create dummy channel for CCE parsing on stack */
   2686           CAacDecoderChannelInfo tmpAacDecoderChannelInfo,
   2687               *pTmpAacDecoderChannelInfo;
   2688 
   2689           FDKmemclear(mdctSpec, 1024 * sizeof(FIXP_DBL));
   2690 
   2691           tmpAacDecoderChannelInfo.pDynData = commonData.pAacDecoderDynamicData;
   2692           tmpAacDecoderChannelInfo.pComData = &commonData;
   2693           tmpAacDecoderChannelInfo.pComStaticData = &commonStaticData;
   2694           tmpAacDecoderChannelInfo.pSpectralCoefficient =
   2695               (SPECTRAL_PTR)mdctSpec;
   2696           /* Assume AAC-LC */
   2697           tmpAacDecoderChannelInfo.granuleLength =
   2698               self->streamInfo.aacSamplesPerFrame / 8;
   2699           /* Reset PNS data. */
   2700           CPns_ResetData(
   2701               &tmpAacDecoderChannelInfo.data.aac.PnsData,
   2702               &tmpAacDecoderChannelInfo.pComData->pnsInterChannelData);
   2703           pTmpAacDecoderChannelInfo = &tmpAacDecoderChannelInfo;
   2704           /* do CCE parsing */
   2705           ErrorStatus = CChannelElement_Read(
   2706               bs, &pTmpAacDecoderChannelInfo, NULL, self->streamInfo.aot,
   2707               &self->samplingRateInfo[streamIndex], self->flags[streamIndex],
   2708               AC_EL_GA_CCE, self->streamInfo.aacSamplesPerFrame, 1,
   2709               self->streamInfo.epConfig, self->hInput);
   2710 
   2711           C_AALLOC_SCRATCH_END(mdctSpec, FIXP_DBL, 1024);
   2712 
   2713           if (ErrorStatus) {
   2714             self->frameOK = 0;
   2715           }
   2716 
   2717           if (self->frameOK) {
   2718             /* Lookup the element and decode it only if it belongs to the
   2719              * current program */
   2720             if (CProgramConfig_LookupElement(
   2721                     pce, self->streamInfo.channelConfig,
   2722                     pTmpAacDecoderChannelInfo->ElementInstanceTag, 0,
   2723                     self->chMapping, self->channelType, self->channelIndices,
   2724                     (8), &previous_element_index, self->elements, type)) {
   2725               /* decoding of CCE not supported */
   2726             } else {
   2727               self->frameOK = 0;
   2728             }
   2729           }
   2730         }
   2731         el_cnt[type]++;
   2732         break;
   2733 
   2734       case ID_DSE: {
   2735         UCHAR element_instance_tag;
   2736 
   2737         CDataStreamElement_Read(self, bs, &element_instance_tag, auStartAnchor);
   2738 
   2739         if (!CProgramConfig_LookupElement(
   2740                 pce, self->streamInfo.channelConfig, element_instance_tag, 0,
   2741                 self->chMapping, self->channelType, self->channelIndices, (8),
   2742                 &previous_element_index, self->elements, type)) {
   2743           /* most likely an error in bitstream occured */
   2744           // self->frameOK = 0;
   2745         }
   2746       } break;
   2747 
   2748       case ID_PCE: {
   2749         int result = CProgramConfigElement_Read(bs, self->hInput, pce,
   2750                                                 self->streamInfo.channelConfig,
   2751                                                 auStartAnchor);
   2752         if (result < 0) {
   2753           /* Something went wrong */
   2754           ErrorStatus = AAC_DEC_PARSE_ERROR;
   2755           self->frameOK = 0;
   2756         } else if (result > 1) {
   2757           /* Built element table */
   2758           int elIdx = CProgramConfig_GetElementTable(
   2759               pce, self->elements, (((8)) + (8)), &self->chMapIndex);
   2760           /* Reset the remaining tabs */
   2761           for (; elIdx < (3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 1);
   2762                elIdx++) {
   2763             self->elements[elIdx] = ID_NONE;
   2764           }
   2765           /* Make new number of channel persistent */
   2766           self->ascChannels[streamIndex] = pce->NumChannels;
   2767           /* If PCE is not first element conceal this frame to avoid
   2768            * inconsistencies */
   2769           if (element_count != 0) {
   2770             self->frameOK = 0;
   2771           }
   2772         }
   2773         pceRead = (result >= 0) ? 1 : 0;
   2774       } break;
   2775 
   2776       case ID_FIL: {
   2777         int bitCnt = FDKreadBits(bs, 4); /* bs_count */
   2778 
   2779         if (bitCnt == 15) {
   2780           int esc_count = FDKreadBits(bs, 8); /* bs_esc_count */
   2781           bitCnt = esc_count + 14;
   2782         }
   2783 
   2784         /* Convert to bits */
   2785         bitCnt <<= 3;
   2786 
   2787         while (bitCnt > 0) {
   2788           ErrorStatus = CAacDecoder_ExtPayloadParse(
   2789               self, bs, &bitCnt, previous_element, previous_element_index, 1);
   2790           if (ErrorStatus != AAC_DEC_OK) {
   2791             self->frameOK = 0;
   2792             break;
   2793           }
   2794         }
   2795       } break;
   2796 
   2797       case ID_EXT:
   2798         ErrorStatus = aacDecoder_ParseExplicitMpsAndSbr(
   2799             self, bs, previous_element, previous_element_index, element_count,
   2800             el_cnt);
   2801         break;
   2802 
   2803       case ID_USAC_EXT: {
   2804         /* parse extension element payload
   2805            q.v. rsv603daExtElement() ISO/IEC DIS 23008-3  Table 30
   2806            or   UsacExElement() ISO/IEC FDIS 23003-3:2011(E)  Table 21
   2807          */
   2808         int usacExtElementPayloadLength;
   2809         /* int usacExtElementStart, usacExtElementStop; */
   2810 
   2811         if (FDKreadBit(bs)) {   /* usacExtElementPresent */
   2812           if (FDKreadBit(bs)) { /* usacExtElementUseDefaultLength */
   2813             usacExtElementPayloadLength =
   2814                 self->pUsacConfig[streamIndex]
   2815                     ->element[element_count - element_count_prev_streams]
   2816                     .extElement.usacExtElementDefaultLength;
   2817           } else {
   2818             usacExtElementPayloadLength = FDKreadBits(bs, 8);
   2819             if (usacExtElementPayloadLength == (UINT)(1 << 8) - 1) {
   2820               UINT valueAdd = FDKreadBits(bs, 16);
   2821               usacExtElementPayloadLength += (INT)valueAdd - 2;
   2822             }
   2823           }
   2824           if (usacExtElementPayloadLength > 0) {
   2825             int usacExtBitPos;
   2826 
   2827             if (self->pUsacConfig[streamIndex]
   2828                     ->element[element_count - element_count_prev_streams]
   2829                     .extElement.usacExtElementPayloadFrag) {
   2830               /* usacExtElementStart = */ FDKreadBit(bs);
   2831               /* usacExtElementStop = */ FDKreadBit(bs);
   2832             } else {
   2833               /* usacExtElementStart = 1; */
   2834               /* usacExtElementStop = 1; */
   2835             }
   2836 
   2837             usacExtBitPos = (INT)FDKgetValidBits(bs);
   2838 
   2839             USAC_EXT_ELEMENT_TYPE usacExtElementType =
   2840                 self->pUsacConfig[streamIndex]
   2841                     ->element[element_count - element_count_prev_streams]
   2842                     .extElement.usacExtElementType;
   2843 
   2844             switch (usacExtElementType) {
   2845               case ID_EXT_ELE_UNI_DRC: /* uniDrcGain() */
   2846                 if (streamIndex == 0) {
   2847                   int drcErr;
   2848 
   2849                   drcErr = FDK_drcDec_ReadUniDrcGain(self->hUniDrcDecoder, bs);
   2850                   if (drcErr != 0) {
   2851                     ErrorStatus = AAC_DEC_PARSE_ERROR;
   2852                   }
   2853                 }
   2854                 break;
   2855 
   2856               default:
   2857                 break;
   2858             }
   2859 
   2860             /* Skip any remaining bits of extension payload */
   2861             usacExtBitPos = (usacExtElementPayloadLength * 8) -
   2862                             (usacExtBitPos - (INT)FDKgetValidBits(bs));
   2863             if (usacExtBitPos < 0) {
   2864               self->frameOK = 0;
   2865               ErrorStatus = AAC_DEC_PARSE_ERROR;
   2866             }
   2867             FDKpushBiDirectional(bs, usacExtBitPos);
   2868           }
   2869         }
   2870       } break;
   2871       case ID_END:
   2872       case ID_USAC_END:
   2873         break;
   2874 
   2875       default:
   2876         ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR;
   2877         self->frameOK = 0;
   2878         break;
   2879     }
   2880 
   2881     previous_element = type;
   2882     element_count++;
   2883 
   2884   } /* while ( (type != ID_END) ... ) */
   2885 
   2886   if (!(flags & (AACDEC_CONCEAL | AACDEC_FLUSH))) {
   2887     /* float decoder checks if bitsLeft is in range 0-7; only prerollAUs are
   2888      * byteAligned with respect to the first bit */
   2889     /* Byte alignment with respect to the first bit of the raw_data_block(). */
   2890     if (!(self->flags[streamIndex] & (AC_RSVD50 | AC_USAC)) ||
   2891         (self->prerollAULength[self->accessUnit]) /* indicates preroll */
   2892     ) {
   2893       FDKbyteAlign(bs, auStartAnchor);
   2894     }
   2895 
   2896     /* Check if all bits of the raw_data_block() have been read. */
   2897     if (transportDec_GetAuBitsTotal(self->hInput, 0) > 0) {
   2898       INT unreadBits = transportDec_GetAuBitsRemaining(self->hInput, 0);
   2899       /* for pre-roll frames pre-roll length has to be used instead of total AU
   2900        * lenght */
   2901       /* unreadBits regarding preroll bounds */
   2902       if (self->prerollAULength[self->accessUnit]) {
   2903         unreadBits = unreadBits - transportDec_GetAuBitsTotal(self->hInput, 0) +
   2904                      (INT)self->prerollAULength[self->accessUnit];
   2905       }
   2906       if (((self->flags[streamIndex] & (AC_RSVD50 | AC_USAC)) &&
   2907            ((unreadBits < 0) || (unreadBits > 7)) &&
   2908            !(self->prerollAULength[self->accessUnit])) ||
   2909           ((!(self->flags[streamIndex] & (AC_RSVD50 | AC_USAC)) ||
   2910             (self->prerollAULength[self->accessUnit])) &&
   2911            (unreadBits != 0))) {
   2912         if ((((unreadBits < 0) || (unreadBits > 7)) && self->frameOK) &&
   2913             ((transportDec_GetFormat(self->hInput) == TT_DRM) &&
   2914              (self->flags[streamIndex] & AC_USAC))) {
   2915           /* Set frame OK because of fill bits. */
   2916           self->frameOK = 1;
   2917         } else {
   2918           self->frameOK = 0;
   2919         }
   2920 
   2921         /* Do not overwrite current error */
   2922         if (ErrorStatus == AAC_DEC_OK && self->frameOK == 0) {
   2923           ErrorStatus = AAC_DEC_PARSE_ERROR;
   2924         }
   2925         /* Always put the bitbuffer at the right position after the current
   2926          * Access Unit. */
   2927         FDKpushBiDirectional(bs, unreadBits);
   2928       }
   2929     }
   2930 
   2931     /* Check the last element. The terminator (ID_END) has to be the last one
   2932      * (even if ER syntax is used). */
   2933     if (self->frameOK && type != ID_END) {
   2934       /* Do not overwrite current error */
   2935       if (ErrorStatus == AAC_DEC_OK) {
   2936         ErrorStatus = AAC_DEC_PARSE_ERROR;
   2937       }
   2938       self->frameOK = 0;
   2939     }
   2940   }
   2941 
   2942   if (!(flags & (AACDEC_CONCEAL | AACDEC_FLUSH)) && self->frameOK) {
   2943     channel_elements[channel_element_count++] = ID_END;
   2944   }
   2945   element_count = 0;
   2946   aacChannels = 0;
   2947   type = ID_NONE;
   2948   previous_element_index = 0;
   2949 
   2950   while (type != ID_END &&
   2951          element_count < (3 * ((8) * 2) + (((8) * 2)) / 2 + 4 * (1) + 1)) {
   2952     int el_channels;
   2953 
   2954     if ((flags & (AACDEC_CONCEAL | AACDEC_FLUSH)) || !self->frameOK) {
   2955       channel_elements[element_count] = self->elements[element_count];
   2956       if (channel_elements[element_count] == ID_NONE) {
   2957         channel_elements[element_count] = ID_END;
   2958       }
   2959     }
   2960 
   2961     if (self->flags[streamIndex] & (AC_USAC | AC_RSV603DA | AC_BSAC)) {
   2962       type = self->elements[element_count];
   2963     } else {
   2964       type = channel_elements[element_count];
   2965     }
   2966 
   2967     if (!(flags & (AACDEC_CONCEAL | AACDEC_FLUSH)) && self->frameOK) {
   2968       switch (type) {
   2969         case ID_SCE:
   2970         case ID_CPE:
   2971         case ID_LFE:
   2972         case ID_USAC_SCE:
   2973         case ID_USAC_CPE:
   2974         case ID_USAC_LFE:
   2975 
   2976           el_channels = CAacDecoder_GetELChannels(
   2977               type, self->usacStereoConfigIndex[element_count]);
   2978 
   2979           if (!hdaacDecoded) {
   2980             if (self->pAacDecoderStaticChannelInfo[aacChannels]
   2981                     ->pCpeStaticData != NULL) {
   2982               self->pAacDecoderStaticChannelInfo[aacChannels]
   2983                   ->pCpeStaticData->jointStereoPersistentData.scratchBuffer =
   2984                   (FIXP_DBL *)pTimeData;
   2985             }
   2986             CChannelElement_Decode(
   2987                 &self->pAacDecoderChannelInfo[aacChannels],
   2988                 &self->pAacDecoderStaticChannelInfo[aacChannels],
   2989                 &self->samplingRateInfo[streamIndex], self->flags[streamIndex],
   2990                 self->elFlags[element_count], el_channels);
   2991           }
   2992           aacChannels += el_channels;
   2993           break;
   2994         case ID_NONE:
   2995           type = ID_END;
   2996           break;
   2997         default:
   2998           break;
   2999       }
   3000     }
   3001     element_count++;
   3002   }
   3003 
   3004   /* More AAC channels than specified by the ASC not allowed. */
   3005   if ((aacChannels == 0 || aacChannels > self->aacChannels) &&
   3006       !(flags & (AACDEC_CONCEAL | AACDEC_FLUSH))) {
   3007     /* Do not overwrite current error */
   3008     if (ErrorStatus == AAC_DEC_OK) {
   3009       ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR;
   3010     }
   3011     self->frameOK = 0;
   3012     aacChannels = 0;
   3013   }
   3014 
   3015   if (TRANSPORTDEC_OK != transportDec_CrcCheck(self->hInput)) {
   3016     ErrorStatus = AAC_DEC_CRC_ERROR;
   3017     self->frameOK = 0;
   3018   }
   3019 
   3020   /* Ensure that in case of concealment a proper error status is set. */
   3021   if ((self->frameOK == 0) && (ErrorStatus == AAC_DEC_OK)) {
   3022     ErrorStatus = AAC_DEC_DECODE_FRAME_ERROR;
   3023   }
   3024 
   3025   if (self->frameOK && (flags & AACDEC_FLUSH)) {
   3026     aacChannels = self->aacChannelsPrev;
   3027     /* Because the downmix could be active, its necessary to restore the channel
   3028      * type and indices. */
   3029     FDKmemcpy(self->channelType, self->channelTypePrev,
   3030               (8) * sizeof(AUDIO_CHANNEL_TYPE)); /* restore */
   3031     FDKmemcpy(self->channelIndices, self->channelIndicesPrev,
   3032               (8) * sizeof(UCHAR)); /* restore */
   3033     self->sbrEnabled = self->sbrEnabledPrev;
   3034   } else {
   3035     /* store or restore the number of channels and the corresponding info */
   3036     if (self->frameOK && !(flags & AACDEC_CONCEAL)) {
   3037       self->aacChannelsPrev = aacChannels; /* store */
   3038       FDKmemcpy(self->channelTypePrev, self->channelType,
   3039                 (8) * sizeof(AUDIO_CHANNEL_TYPE)); /* store */
   3040       FDKmemcpy(self->channelIndicesPrev, self->channelIndices,
   3041                 (8) * sizeof(UCHAR)); /* store */
   3042       self->sbrEnabledPrev = self->sbrEnabled;
   3043     } else {
   3044       if (self->aacChannels > 0) {
   3045         if ((self->buildUpStatus == AACDEC_RSV60_BUILD_UP_ON) ||
   3046             (self->buildUpStatus == AACDEC_RSV60_BUILD_UP_ON_IN_BAND) ||
   3047             (self->buildUpStatus == AACDEC_USAC_BUILD_UP_ON)) {
   3048           aacChannels = self->aacChannels;
   3049           self->aacChannelsPrev = aacChannels; /* store */
   3050         } else {
   3051           aacChannels = self->aacChannelsPrev; /* restore */
   3052         }
   3053         FDKmemcpy(self->channelType, self->channelTypePrev,
   3054                   (8) * sizeof(AUDIO_CHANNEL_TYPE)); /* restore */
   3055         FDKmemcpy(self->channelIndices, self->channelIndicesPrev,
   3056                   (8) * sizeof(UCHAR)); /* restore */
   3057         self->sbrEnabled = self->sbrEnabledPrev;
   3058       }
   3059     }
   3060   }
   3061 
   3062   /* Update number of output channels */
   3063   self->streamInfo.aacNumChannels = aacChannels;
   3064 
   3065   /* Ensure consistency of IS_OUTPUT_VALID() macro. */
   3066   if (aacChannels == 0) {
   3067     ErrorStatus = AAC_DEC_UNKNOWN;
   3068   }
   3069 
   3070   if (pceRead == 1 && CProgramConfig_IsValid(pce)) {
   3071     /* Set matrix mixdown infos if available from PCE. */
   3072     pcmDmx_SetMatrixMixdownFromPce(
   3073         self->hPcmUtils, pce->MatrixMixdownIndexPresent,
   3074         pce->MatrixMixdownIndex, pce->PseudoSurroundEnable);
   3075     ;
   3076   }
   3077 
   3078   /* If there is no valid data to transfrom into time domain, return. */
   3079   if (!IS_OUTPUT_VALID(ErrorStatus)) {
   3080     return ErrorStatus;
   3081   }
   3082 
   3083   /* Setup the output channel mapping. The table below shows the three
   3084    * possibilities: # | chCfg | PCE | chMapIndex
   3085    *  ---+-------+-----+------------------
   3086    *   1 |  > 0  |  no | chCfg
   3087    *   2 |   0   | yes | cChCfg
   3088    *   3 |   0   |  no | aacChannels || 0
   3089    *  ---+-------+-----+--------+------------------
   3090    *  Where chCfg is the channel configuration index from ASC and cChCfg is a
   3091    * corresponding chCfg derived from a given PCE. The variable aacChannels
   3092    * represents the number of channel found during bitstream decoding. Due to
   3093    * the structure of the mapping table it can only be used for mapping if its
   3094    * value is smaller than 7. Otherwise we use the fallback (0) which is a
   3095    * simple pass-through. The possibility #3 should appear only with MPEG-2
   3096    * (ADTS) streams. This is mode is called "implicit channel mapping".
   3097    */
   3098   if ((self->streamInfo.channelConfig == 0) && !pce->isValid) {
   3099     self->chMapIndex = (aacChannels < 7) ? aacChannels : 0;
   3100   }
   3101 
   3102   /*
   3103     Inverse transform
   3104   */
   3105   {
   3106     int c, cIdx;
   3107     int mapped, fCopyChMap = 1;
   3108     UCHAR drcChMap[(8)];
   3109 
   3110     if ((self->streamInfo.channelConfig == 0) && CProgramConfig_IsValid(pce)) {
   3111       /* ISO/IEC 14496-3 says:
   3112            If a PCE is present, the exclude_mask bits correspond to the audio
   3113          channels in the SCE, CPE, CCE and LFE syntax elements in the order of
   3114          their appearance in the PCE. In the case of a CPE, the first
   3115          transmitted mask bit corresponds to the first channel in the CPE, the
   3116          second transmitted mask bit to the second channel. In the case of a
   3117          CCE, a mask bit is transmitted only if the coupling channel is
   3118          specified to be an independently switched coupling channel. Thus we
   3119          have to convert the internal channel mapping from "canonical" MPEG to
   3120          PCE order: */
   3121       UCHAR tmpChMap[(8)];
   3122       if (CProgramConfig_GetPceChMap(pce, tmpChMap, (8)) == 0) {
   3123         for (c = 0; c < aacChannels; c += 1) {
   3124           drcChMap[c] =
   3125               (self->chMapping[c] == 255) ? 255 : tmpChMap[self->chMapping[c]];
   3126         }
   3127         fCopyChMap = 0;
   3128       }
   3129     }
   3130     if (fCopyChMap != 0) {
   3131       FDKmemcpy(drcChMap, self->chMapping, (8) * sizeof(UCHAR));
   3132     }
   3133 
   3134     /* Turn on/off DRC modules level normalization in digital domain depending
   3135      * on the limiter status. */
   3136     aacDecoder_drcSetParam(self->hDrcInfo, APPLY_NORMALIZATION,
   3137                            (self->limiterEnableCurr) ? 0 : 1);
   3138 
   3139     /* deactivate legacy DRC in case uniDrc is active, i.e. uniDrc payload is
   3140      * present and one of DRC or Loudness Normalization is switched on */
   3141     aacDecoder_drcSetParam(
   3142         self->hDrcInfo, UNIDRC_PRECEDENCE,
   3143         FDK_drcDec_GetParam(self->hUniDrcDecoder, DRC_DEC_IS_ACTIVE));
   3144 
   3145     /* Extract DRC control data and map it to channels (without bitstream delay)
   3146      */
   3147     mapped = aacDecoder_drcProlog(
   3148         self->hDrcInfo, bs, self->pAacDecoderStaticChannelInfo,
   3149         pce->ElementInstanceTag, drcChMap, aacChannels);
   3150     if (mapped > 0) {
   3151       /* If at least one DRC thread has been mapped to a channel threre was DRC
   3152        * data in the bitstream. */
   3153       self->flags[streamIndex] |= AC_DRC_PRESENT;
   3154     }
   3155 
   3156     /* Create a reverse mapping table */
   3157     UCHAR Reverse_chMapping[((8) * 2)];
   3158     for (c = 0; c < aacChannels; c++) {
   3159       int d;
   3160       for (d = 0; d < aacChannels - 1; d++) {
   3161         if (self->chMapping[d] == c) {
   3162           break;
   3163         }
   3164       }
   3165       Reverse_chMapping[c] = d;
   3166     }
   3167 
   3168     int el;
   3169     int el_channels;
   3170     c = 0;
   3171     cIdx = 0;
   3172     el_channels = 0;
   3173     for (el = 0; el < element_count; el++) {
   3174       int frameOk_butConceal =
   3175           0; /* Force frame concealment during mute release active state. */
   3176       int concealApplyReturnCode;
   3177 
   3178       if (self->flags[streamIndex] & (AC_USAC | AC_RSV603DA | AC_BSAC)) {
   3179         type = self->elements[el];
   3180       } else {
   3181         type = channel_elements[el];
   3182       }
   3183 
   3184       {
   3185         int nElementChannels;
   3186 
   3187         nElementChannels =
   3188             CAacDecoder_GetELChannels(type, self->usacStereoConfigIndex[el]);
   3189 
   3190         el_channels += nElementChannels;
   3191 
   3192         if (nElementChannels == 0) {
   3193           continue;
   3194         }
   3195       }
   3196 
   3197       int offset;
   3198       int elCh = 0;
   3199       /* "c" iterates in canonical MPEG channel order */
   3200       for (; cIdx < el_channels; c++, cIdx++, elCh++) {
   3201         /* Robustness check */
   3202         if (c >= aacChannels) {
   3203           return AAC_DEC_UNKNOWN;
   3204         }
   3205 
   3206         CAacDecoderChannelInfo *pAacDecoderChannelInfo =
   3207             self->pAacDecoderChannelInfo[c];
   3208         CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo =
   3209             self->pAacDecoderStaticChannelInfo[c];
   3210 
   3211         /* Setup offset for time buffer traversal. */
   3212         {
   3213           pAacDecoderStaticChannelInfo =
   3214               self->pAacDecoderStaticChannelInfo[Reverse_chMapping[c]];
   3215           offset =
   3216               FDK_chMapDescr_getMapValue(
   3217                   &self->mapDescr, Reverse_chMapping[cIdx], self->chMapIndex) *
   3218               timeDataChannelOffset;
   3219         }
   3220 
   3221         if (flags & AACDEC_FLUSH) {
   3222           /* Clear pAacDecoderChannelInfo->pSpectralCoefficient because with
   3223            * AACDEC_FLUSH set it contains undefined data. */
   3224           FDKmemclear(pAacDecoderChannelInfo->pSpectralCoefficient,
   3225                       sizeof(FIXP_DBL) * self->streamInfo.aacSamplesPerFrame);
   3226         }
   3227 
   3228         /* if The ics info is not valid and it will be stored and used in the
   3229          * following concealment method, mark the frame as erroneous */
   3230         {
   3231           CIcsInfo *pIcsInfo = &pAacDecoderChannelInfo->icsInfo;
   3232           CConcealmentInfo *hConcealmentInfo =
   3233               &pAacDecoderStaticChannelInfo->concealmentInfo;
   3234           const int mute_release_active =
   3235               (self->frameOK && !(flags & AACDEC_CONCEAL)) &&
   3236               ((hConcealmentInfo->concealState >= ConcealState_Mute) &&
   3237                (hConcealmentInfo->cntValidFrames + 1 <=
   3238                 hConcealmentInfo->pConcealParams->numMuteReleaseFrames));
   3239           const int icsIsInvalid = (GetScaleFactorBandsTransmitted(pIcsInfo) >
   3240                                     GetScaleFactorBandsTotal(pIcsInfo));
   3241           const int icsInfoUsedinFadeOut =
   3242               !(pAacDecoderChannelInfo->renderMode == AACDEC_RENDER_LPD &&
   3243                 pAacDecoderStaticChannelInfo->last_lpd_mode == 0);
   3244           if (icsInfoUsedinFadeOut && icsIsInvalid && !mute_release_active) {
   3245             self->frameOK = 0;
   3246           }
   3247         }
   3248 
   3249         /*
   3250           Conceal defective spectral data
   3251         */
   3252         {
   3253           CAacDecoderChannelInfo **ppAacDecoderChannelInfo =
   3254               &pAacDecoderChannelInfo;
   3255           CAacDecoderStaticChannelInfo **ppAacDecoderStaticChannelInfo =
   3256               &pAacDecoderStaticChannelInfo;
   3257           {
   3258             concealApplyReturnCode = CConcealment_Apply(
   3259                 &(*ppAacDecoderStaticChannelInfo)->concealmentInfo,
   3260                 *ppAacDecoderChannelInfo, *ppAacDecoderStaticChannelInfo,
   3261                 &self->samplingRateInfo[streamIndex],
   3262                 self->streamInfo.aacSamplesPerFrame,
   3263                 pAacDecoderStaticChannelInfo->last_lpd_mode,
   3264                 (self->frameOK && !(flags & AACDEC_CONCEAL)),
   3265                 self->flags[streamIndex]);
   3266           }
   3267         }
   3268         if (concealApplyReturnCode == -1) {
   3269           frameOk_butConceal = 1;
   3270         }
   3271 
   3272         if (flags & (AACDEC_INTR)) {
   3273           /* Reset DRC control data for this channel */
   3274           aacDecoder_drcInitChannelData(&pAacDecoderStaticChannelInfo->drcData);
   3275         }
   3276         if (flags & (AACDEC_CLRHIST)) {
   3277           if (!(self->flags[0] & AC_USAC)) {
   3278             /* Reset DRC control data for this channel */
   3279             aacDecoder_drcInitChannelData(
   3280                 &pAacDecoderStaticChannelInfo->drcData);
   3281           }
   3282         }
   3283         /* The DRC module demands to be called with the gain field holding the
   3284          * gain scale. */
   3285         self->extGain[0] = (FIXP_DBL)TDL_GAIN_SCALING;
   3286         /* DRC processing */
   3287         aacDecoder_drcApply(
   3288             self->hDrcInfo, self->hSbrDecoder, pAacDecoderChannelInfo,
   3289             &pAacDecoderStaticChannelInfo->drcData, self->extGain, c,
   3290             self->streamInfo.aacSamplesPerFrame, self->sbrEnabled
   3291 
   3292         );
   3293 
   3294         if (timeDataSize < timeDataChannelOffset * self->aacChannels) {
   3295           ErrorStatus = AAC_DEC_OUTPUT_BUFFER_TOO_SMALL;
   3296           break;
   3297         }
   3298         if (self->flushStatus && (self->flushCnt > 0) &&
   3299             !(flags & AACDEC_CONCEAL)) {
   3300           FDKmemclear(pTimeData + offset,
   3301                       sizeof(FIXP_PCM) * self->streamInfo.aacSamplesPerFrame);
   3302         } else
   3303           switch (pAacDecoderChannelInfo->renderMode) {
   3304             case AACDEC_RENDER_IMDCT:
   3305 
   3306               CBlock_FrequencyToTime(
   3307                   pAacDecoderStaticChannelInfo, pAacDecoderChannelInfo,
   3308                   pTimeData + offset, self->streamInfo.aacSamplesPerFrame,
   3309                   (self->frameOK && !(flags & AACDEC_CONCEAL) &&
   3310                    !frameOk_butConceal),
   3311                   pAacDecoderChannelInfo->pComStaticData->pWorkBufferCore1
   3312                       ->mdctOutTemp,
   3313                   self->elFlags[el], elCh);
   3314 
   3315               self->extGainDelay = self->streamInfo.aacSamplesPerFrame;
   3316               break;
   3317             case AACDEC_RENDER_ELDFB: {
   3318               CBlock_FrequencyToTimeLowDelay(
   3319                   pAacDecoderStaticChannelInfo, pAacDecoderChannelInfo,
   3320                   pTimeData + offset, self->streamInfo.aacSamplesPerFrame);
   3321               self->extGainDelay =
   3322                   (self->streamInfo.aacSamplesPerFrame * 2 -
   3323                    self->streamInfo.aacSamplesPerFrame / 2 - 1) /
   3324                   2;
   3325             } break;
   3326             case AACDEC_RENDER_LPD:
   3327 
   3328               CLpd_RenderTimeSignal(
   3329                   pAacDecoderStaticChannelInfo, pAacDecoderChannelInfo,
   3330                   pTimeData + offset, self->streamInfo.aacSamplesPerFrame,
   3331                   &self->samplingRateInfo[streamIndex],
   3332                   (self->frameOK && !(flags & AACDEC_CONCEAL) &&
   3333                    !frameOk_butConceal),
   3334                   flags, self->flags[streamIndex]);
   3335 
   3336               self->extGainDelay = self->streamInfo.aacSamplesPerFrame;
   3337               break;
   3338             default:
   3339               ErrorStatus = AAC_DEC_UNKNOWN;
   3340               break;
   3341           }
   3342         /* TimeDomainFading */
   3343         if (!CConceal_TDFading_Applied[c]) {
   3344           CConceal_TDFading_Applied[c] = CConcealment_TDFading(
   3345               self->streamInfo.aacSamplesPerFrame,
   3346               &self->pAacDecoderStaticChannelInfo[c], pTimeData + offset, 0);
   3347           if (c + 1 < (8) && c < aacChannels - 1) {
   3348             /* update next TDNoise Seed to avoid muting in case of Parametric
   3349              * Stereo */
   3350             self->pAacDecoderStaticChannelInfo[c + 1]
   3351                 ->concealmentInfo.TDNoiseSeed =
   3352                 self->pAacDecoderStaticChannelInfo[c]
   3353                     ->concealmentInfo.TDNoiseSeed;
   3354           }
   3355         }
   3356       }
   3357     }
   3358 
   3359     if (self->flags[streamIndex] & AC_USAC) {
   3360       int bsPseudoLr = 0;
   3361       mpegSurroundDecoder_IsPseudoLR(
   3362           (CMpegSurroundDecoder *)self->pMpegSurroundDecoder, &bsPseudoLr);
   3363       /* ISO/IEC 23003-3, 7.11.2.6 Modification of core decoder output (pseudo
   3364        * LR) */
   3365       if ((aacChannels == 2) && bsPseudoLr) {
   3366         int i, offset2;
   3367         const FIXP_SGL invSqrt2 = FL2FXCONST_SGL(0.707106781186547f);
   3368         FIXP_PCM *pTD = pTimeData;
   3369 
   3370         offset2 = timeDataChannelOffset;
   3371 
   3372         for (i = 0; i < self->streamInfo.aacSamplesPerFrame; i++) {
   3373           FIXP_DBL L = FX_PCM2FX_DBL(pTD[0]);
   3374           FIXP_DBL R = FX_PCM2FX_DBL(pTD[offset2]);
   3375           L = fMult(L, invSqrt2);
   3376           R = fMult(R, invSqrt2);
   3377 #if (SAMPLE_BITS == 16)
   3378           pTD[0] = FX_DBL2FX_PCM(fAddSaturate(L + R, (FIXP_DBL)0x8000));
   3379           pTD[offset2] = FX_DBL2FX_PCM(fAddSaturate(L - R, (FIXP_DBL)0x8000));
   3380 #else
   3381           pTD[0] = FX_DBL2FX_PCM(L + R);
   3382           pTD[offset2] = FX_DBL2FX_PCM(L - R);
   3383 #endif
   3384           pTD++;
   3385         }
   3386       }
   3387     }
   3388 
   3389     /* Extract DRC control data and map it to channels (with bitstream delay) */
   3390     mapped = aacDecoder_drcEpilog(
   3391         self->hDrcInfo, bs, self->pAacDecoderStaticChannelInfo,
   3392         pce->ElementInstanceTag, drcChMap, aacChannels);
   3393     if (mapped > 0) {
   3394       /* If at least one DRC thread has been mapped to a channel threre was DRC
   3395        * data in the bitstream. */
   3396       self->flags[streamIndex] |= AC_DRC_PRESENT;
   3397     }
   3398   }
   3399 
   3400   /* Add additional concealment delay */
   3401   self->streamInfo.outputDelay +=
   3402       CConcealment_GetDelay(&self->concealCommonData) *
   3403       self->streamInfo.aacSamplesPerFrame;
   3404 
   3405   /* Map DRC data to StreamInfo structure */
   3406   aacDecoder_drcGetInfo(self->hDrcInfo, &self->streamInfo.drcPresMode,
   3407                         &self->streamInfo.drcProgRefLev);
   3408 
   3409   /* Reorder channel type information tables.  */
   3410   if (!(self->flags[0] & AC_RSV603DA)) {
   3411     AUDIO_CHANNEL_TYPE types[(8)];
   3412     UCHAR idx[(8)];
   3413     int c;
   3414     int mapValue;
   3415 
   3416     FDK_ASSERT(sizeof(self->channelType) == sizeof(types));
   3417     FDK_ASSERT(sizeof(self->channelIndices) == sizeof(idx));
   3418 
   3419     FDKmemcpy(types, self->channelType, sizeof(types));
   3420     FDKmemcpy(idx, self->channelIndices, sizeof(idx));
   3421 
   3422     for (c = 0; c < aacChannels; c++) {
   3423       mapValue =
   3424           FDK_chMapDescr_getMapValue(&self->mapDescr, c, self->chMapIndex);
   3425       self->channelType[mapValue] = types[c];
   3426       self->channelIndices[mapValue] = idx[c];
   3427     }
   3428   }
   3429 
   3430   self->blockNumber++;
   3431 
   3432   return ErrorStatus;
   3433 }
   3434 
   3435 /*!
   3436   \brief returns the streaminfo pointer
   3437 
   3438   The function hands back a pointer to the streaminfo structure
   3439 
   3440   \return pointer to the struct
   3441 */
   3442 LINKSPEC_CPP CStreamInfo *CAacDecoder_GetStreamInfo(HANDLE_AACDECODER self) {
   3443   if (!self) {
   3444     return NULL;
   3445   }
   3446   return &self->streamInfo;
   3447 }
   3448