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 #include "channel.h"
    104 #include "aacdecoder.h"
    105 #include "block.h"
    106 #include "aacdec_tns.h"
    107 #include "FDK_bitstream.h"
    108 
    109 #include "conceal.h"
    110 
    111 #include "rvlc.h"
    112 
    113 #include "aacdec_hcr.h"
    114 
    115 #include "usacdec_lpd.h"
    116 #include "usacdec_fac.h"
    117 
    118 static void MapMidSideMaskToPnsCorrelation(
    119     CAacDecoderChannelInfo *pAacDecoderChannelInfo[2]) {
    120   int group;
    121 
    122   for (group = 0; group < pAacDecoderChannelInfo[L]->icsInfo.WindowGroups;
    123        group++) {
    124     UCHAR groupMask = 1 << group;
    125 
    126     for (UCHAR band = 0; band < pAacDecoderChannelInfo[L]->icsInfo.MaxSfBands;
    127          band++) {
    128       if (pAacDecoderChannelInfo[L]->pComData->jointStereoData.MsUsed[band] &
    129           groupMask) { /* channels are correlated */
    130         CPns_SetCorrelation(&pAacDecoderChannelInfo[L]->data.aac.PnsData, group,
    131                             band, 0);
    132 
    133         if (CPns_IsPnsUsed(&pAacDecoderChannelInfo[L]->data.aac.PnsData, group,
    134                            band) &&
    135             CPns_IsPnsUsed(&pAacDecoderChannelInfo[R]->data.aac.PnsData, group,
    136                            band))
    137           pAacDecoderChannelInfo[L]->pComData->jointStereoData.MsUsed[band] ^=
    138               groupMask; /* clear the groupMask-bit */
    139       }
    140     }
    141   }
    142 }
    143 
    144 static void Clean_Complex_Prediction_coefficients(
    145     CJointStereoPersistentData *pJointStereoPersistentData, int windowGroups,
    146     const int low_limit, const int high_limit) {
    147   for (int group = 0; group < windowGroups; group++) {
    148     for (int sfb = low_limit; sfb < high_limit; sfb++) {
    149       pJointStereoPersistentData->alpha_q_re_prev[group][sfb] = 0;
    150       pJointStereoPersistentData->alpha_q_im_prev[group][sfb] = 0;
    151     }
    152   }
    153 }
    154 
    155 /*!
    156   \brief Decode channel pair element
    157 
    158   The function decodes a channel pair element.
    159 
    160   \return  none
    161 */
    162 void CChannelElement_Decode(
    163     CAacDecoderChannelInfo
    164         *pAacDecoderChannelInfo[2], /*!< pointer to aac decoder channel info */
    165     CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[2],
    166     SamplingRateInfo *pSamplingRateInfo, UINT flags, UINT elFlags,
    167     int el_channels) {
    168   int ch = 0;
    169 
    170   int maxSfBandsL = 0, maxSfBandsR = 0;
    171   int maybe_jstereo = (el_channels > 1);
    172 
    173   if (flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA) && el_channels == 2) {
    174     if (pAacDecoderChannelInfo[L]->data.usac.core_mode ||
    175         pAacDecoderChannelInfo[R]->data.usac.core_mode) {
    176       maybe_jstereo = 0;
    177     }
    178   }
    179 
    180   if (maybe_jstereo) {
    181     maxSfBandsL =
    182         GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[L]->icsInfo);
    183     maxSfBandsR =
    184         GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[R]->icsInfo);
    185 
    186     /* apply ms */
    187     if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) {
    188       if (!(flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA))) {
    189         if (pAacDecoderChannelInfo[L]->data.aac.PnsData.PnsActive ||
    190             pAacDecoderChannelInfo[R]->data.aac.PnsData.PnsActive) {
    191           MapMidSideMaskToPnsCorrelation(pAacDecoderChannelInfo);
    192         }
    193       }
    194       /* if tns_on_lr == 1 run MS */ /* &&
    195                                         (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_active
    196                                         == 1) */
    197       if (((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
    198            (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
    199             1)) ||
    200           ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) == 0)) {
    201         int max_sfb_ste = (INT)(pAacDecoderChannelInfo[L]->icsInfo.max_sfb_ste);
    202 
    203         CJointStereo_ApplyMS(
    204             pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo,
    205             pAacDecoderChannelInfo[L]->pSpectralCoefficient,
    206             pAacDecoderChannelInfo[R]->pSpectralCoefficient,
    207             pAacDecoderChannelInfo[L]->pDynData->aSfbScale,
    208             pAacDecoderChannelInfo[R]->pDynData->aSfbScale,
    209             pAacDecoderChannelInfo[L]->specScale,
    210             pAacDecoderChannelInfo[R]->specScale,
    211             GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo,
    212                                       pSamplingRateInfo),
    213             GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
    214             GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo), max_sfb_ste,
    215             maxSfBandsL, maxSfBandsR,
    216             pAacDecoderChannelInfo[L]
    217                 ->pComData->jointStereoData.store_dmx_re_prev,
    218             &(pAacDecoderChannelInfo[L]
    219                   ->pComData->jointStereoData.store_dmx_re_prev_e),
    220             1);
    221 
    222       } /* if ( ((elFlags & AC_EL_USAC_CP_POSSIBLE).... */
    223     }   /* if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow)*/
    224 
    225     /* apply intensity stereo */ /* modifies pAacDecoderChannelInfo[]->aSpecSfb
    226                                   */
    227     if (!(flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA))) {
    228       if ((pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow ==
    229            1) &&
    230           (el_channels == 2)) {
    231         CJointStereo_ApplyIS(
    232             pAacDecoderChannelInfo,
    233             GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo,
    234                                       pSamplingRateInfo),
    235             GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
    236             GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo),
    237             GetScaleFactorBandsTransmitted(
    238                 &pAacDecoderChannelInfo[L]->icsInfo));
    239       }
    240     }
    241   } /* maybe_stereo */
    242 
    243   for (ch = 0; ch < el_channels; ch++) {
    244     if (pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_LPD) {
    245       /* Decode LPD data */
    246       CLpdChannelStream_Decode(pAacDecoderChannelInfo[ch],
    247                                pAacDecoderStaticChannelInfo[ch], flags);
    248     } else {
    249       UCHAR noSfbs =
    250           GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[ch]->icsInfo);
    251       /* For USAC common window: max_sfb of both channels may differ
    252        * (common_max_sfb == 0). */
    253       if ((maybe_jstereo == 1) &&
    254           (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow ==
    255            1)) {
    256         noSfbs = fMax(maxSfBandsL, maxSfBandsR);
    257       }
    258       int CP_active = 0;
    259       if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
    260         CP_active = pAacDecoderChannelInfo[ch]
    261                         ->pComData->jointStereoData.cplx_pred_flag;
    262       }
    263 
    264       /* Omit writing of pAacDecoderChannelInfo[ch]->specScale for complex
    265          stereo prediction since scaling has already been carried out. */
    266       int max_sfb_ste = (INT)(pAacDecoderChannelInfo[L]->icsInfo.max_sfb_ste);
    267 
    268       if ((!CP_active) || (CP_active && (max_sfb_ste < noSfbs)) ||
    269           ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
    270            (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
    271             0))) {
    272         CBlock_ScaleSpectralData(pAacDecoderChannelInfo[ch], noSfbs,
    273                                  pSamplingRateInfo);
    274 
    275         /*Active for the case of TNS applied before MS/CP*/
    276         if ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
    277             (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
    278              0)) {
    279           if (IsLongBlock(&pAacDecoderChannelInfo[ch]->icsInfo)) {
    280             for (int i = 0; i < noSfbs; i++) {
    281               pAacDecoderChannelInfo[ch]->pDynData->aSfbScale[i] =
    282                   pAacDecoderChannelInfo[ch]->specScale[0];
    283             }
    284           } else {
    285             for (int i = 0; i < 8; i++) {
    286               for (int j = 0; j < noSfbs; j++) {
    287                 pAacDecoderChannelInfo[ch]->pDynData->aSfbScale[i * 16 + j] =
    288                     pAacDecoderChannelInfo[ch]->specScale[i];
    289               }
    290             }
    291           }
    292         }
    293       }
    294     }
    295   } /* End "for (ch = 0; ch < el_channels; ch++)" */
    296 
    297   if (maybe_jstereo) {
    298     /* apply ms */
    299     if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) {
    300     } /* CommonWindow */
    301     else {
    302       if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
    303         FDKmemclear(
    304             pAacDecoderStaticChannelInfo[L]
    305                 ->pCpeStaticData->jointStereoPersistentData.alpha_q_re_prev,
    306             JointStereoMaximumGroups * JointStereoMaximumBands * sizeof(SHORT));
    307         FDKmemclear(
    308             pAacDecoderStaticChannelInfo[L]
    309                 ->pCpeStaticData->jointStereoPersistentData.alpha_q_im_prev,
    310             JointStereoMaximumGroups * JointStereoMaximumBands * sizeof(SHORT));
    311       }
    312     }
    313 
    314   } /* if (maybe_jstereo) */
    315 
    316   for (ch = 0; ch < el_channels; ch++) {
    317     if (pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_LPD) {
    318     } else {
    319       if (!(flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA))) {
    320         /* Use same seed for coupled channels (CPE) */
    321         int pnsCh = (ch > 0) ? L : ch;
    322         CPns_UpdateNoiseState(
    323             &pAacDecoderChannelInfo[ch]->data.aac.PnsData,
    324             pAacDecoderChannelInfo[pnsCh]->data.aac.PnsData.currentSeed,
    325             pAacDecoderChannelInfo[ch]->pComData->pnsRandomSeed);
    326       }
    327 
    328       if ((!(flags & (AC_USAC))) ||
    329           ((flags & (AC_USAC)) &&
    330            (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_active ==
    331             1)) ||
    332           (maybe_jstereo == 0)) {
    333         ApplyTools(
    334             pAacDecoderChannelInfo, pSamplingRateInfo, flags, elFlags, ch,
    335             pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow);
    336       }
    337     } /* End "} else" */
    338   }   /* End "for (ch = 0; ch < el_channels; ch++)" */
    339 
    340   if (maybe_jstereo) {
    341     /* apply ms */
    342     if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) {
    343       /* if tns_on_lr == 0 run MS */
    344       if ((flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) &&
    345           (pAacDecoderChannelInfo[L]->pDynData->specificTo.usac.tns_on_lr ==
    346            0)) {
    347         int max_sfb_ste = (INT)(pAacDecoderChannelInfo[L]->icsInfo.max_sfb_ste);
    348 
    349         CJointStereo_ApplyMS(
    350             pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo,
    351             pAacDecoderChannelInfo[L]->pSpectralCoefficient,
    352             pAacDecoderChannelInfo[R]->pSpectralCoefficient,
    353             pAacDecoderChannelInfo[L]->pDynData->aSfbScale,
    354             pAacDecoderChannelInfo[R]->pDynData->aSfbScale,
    355             pAacDecoderChannelInfo[L]->specScale,
    356             pAacDecoderChannelInfo[R]->specScale,
    357             GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo,
    358                                       pSamplingRateInfo),
    359             GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
    360             GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo), max_sfb_ste,
    361             maxSfBandsL, maxSfBandsR,
    362             pAacDecoderChannelInfo[L]
    363                 ->pComData->jointStereoData.store_dmx_re_prev,
    364             &(pAacDecoderChannelInfo[L]
    365                   ->pComData->jointStereoData.store_dmx_re_prev_e),
    366             1);
    367       }
    368 
    369     } /* if (pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow) */
    370 
    371   } /* if (maybe_jstereo) */
    372 
    373   for (ch = 0; ch < el_channels; ch++) {
    374     if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
    375       pAacDecoderStaticChannelInfo[L]
    376           ->pCpeStaticData->jointStereoPersistentData.clearSpectralCoeffs = 0;
    377     }
    378   }
    379 
    380   CRvlc_ElementCheck(pAacDecoderChannelInfo, pAacDecoderStaticChannelInfo,
    381                      flags, el_channels);
    382 }
    383 
    384 void CChannel_CodebookTableInit(
    385     CAacDecoderChannelInfo *pAacDecoderChannelInfo) {
    386   int b, w, maxBands, maxWindows;
    387   int maxSfb = GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo->icsInfo);
    388   UCHAR *pCodeBook = pAacDecoderChannelInfo->pDynData->aCodeBook;
    389 
    390   if (IsLongBlock(&pAacDecoderChannelInfo->icsInfo)) {
    391     maxBands = 64;
    392     maxWindows = 1;
    393   } else {
    394     maxBands = 16;
    395     maxWindows = 8;
    396   }
    397 
    398   for (w = 0; w < maxWindows; w++) {
    399     for (b = 0; b < maxSfb; b++) {
    400       pCodeBook[b] = ESCBOOK;
    401     }
    402     for (; b < maxBands; b++) {
    403       pCodeBook[b] = ZERO_HCB;
    404     }
    405     pCodeBook += maxBands;
    406   }
    407 }
    408 
    409 /*
    410  * Arbitrary order bitstream parser
    411  */
    412 AAC_DECODER_ERROR CChannelElement_Read(
    413     HANDLE_FDK_BITSTREAM hBs, CAacDecoderChannelInfo *pAacDecoderChannelInfo[],
    414     CAacDecoderStaticChannelInfo *pAacDecoderStaticChannelInfo[],
    415     const AUDIO_OBJECT_TYPE aot, SamplingRateInfo *pSamplingRateInfo,
    416     const UINT flags, const UINT elFlags, const UINT frame_length,
    417     const UCHAR numberOfChannels, const SCHAR epConfig,
    418     HANDLE_TRANSPORTDEC pTpDec) {
    419   AAC_DECODER_ERROR error = AAC_DEC_OK;
    420   const element_list_t *list;
    421   int i, ch, decision_bit;
    422   int crcReg1 = -1, crcReg2 = -1;
    423   int cplxPred;
    424   int ind_sw_cce_flag = 0, num_gain_element_lists = 0;
    425 
    426   FDK_ASSERT((numberOfChannels == 1) || (numberOfChannels == 2));
    427 
    428   /* Get channel element sequence table */
    429   list = getBitstreamElementList(aot, epConfig, numberOfChannels, 0, elFlags);
    430   if (list == NULL) {
    431     error = AAC_DEC_UNSUPPORTED_FORMAT;
    432     goto bail;
    433   }
    434 
    435   CTns_Reset(&pAacDecoderChannelInfo[0]->pDynData->TnsData);
    436   /* Set common window to 0 by default. If signalized in the bit stream it will
    437    * be overwritten later explicitely */
    438   pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow = 0;
    439   if (flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA)) {
    440     pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_active = 0;
    441     pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_on_lr = 0;
    442   }
    443   if (numberOfChannels == 2) {
    444     CTns_Reset(&pAacDecoderChannelInfo[1]->pDynData->TnsData);
    445     pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow = 0;
    446   }
    447 
    448   cplxPred = 0;
    449   if (pAacDecoderStaticChannelInfo != NULL) {
    450     if (elFlags & AC_EL_USAC_CP_POSSIBLE) {
    451       pAacDecoderChannelInfo[0]->pComData->jointStereoData.cplx_pred_flag = 0;
    452       cplxPred = 1;
    453     }
    454   }
    455 
    456   if (0 || (flags & (AC_ELD | AC_SCALABLE))) {
    457     pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow = 1;
    458     if (numberOfChannels == 2) {
    459       pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow =
    460           pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow;
    461     }
    462   }
    463 
    464   /* Iterate through sequence table */
    465   i = 0;
    466   ch = 0;
    467   decision_bit = 0;
    468   do {
    469     switch (list->id[i]) {
    470       case element_instance_tag:
    471         pAacDecoderChannelInfo[0]->ElementInstanceTag = FDKreadBits(hBs, 4);
    472         if (numberOfChannels == 2) {
    473           pAacDecoderChannelInfo[1]->ElementInstanceTag =
    474               pAacDecoderChannelInfo[0]->ElementInstanceTag;
    475         }
    476         break;
    477       case common_window:
    478         decision_bit =
    479             pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.CommonWindow =
    480                 FDKreadBits(hBs, 1);
    481         if (numberOfChannels == 2) {
    482           pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow =
    483               pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow;
    484         }
    485         break;
    486       case ics_info:
    487         /* store last window sequence (utilized in complex stereo prediction)
    488          * before reading new channel-info */
    489         if (cplxPred) {
    490           if (pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow) {
    491             pAacDecoderStaticChannelInfo[0]
    492                 ->pCpeStaticData->jointStereoPersistentData.winSeqPrev =
    493                 pAacDecoderChannelInfo[0]->icsInfo.WindowSequence;
    494             pAacDecoderStaticChannelInfo[0]
    495                 ->pCpeStaticData->jointStereoPersistentData.winShapePrev =
    496                 pAacDecoderChannelInfo[0]->icsInfo.WindowShape;
    497           }
    498         }
    499         /* Read individual channel info */
    500         error = IcsRead(hBs, &pAacDecoderChannelInfo[ch]->icsInfo,
    501                         pSamplingRateInfo, flags);
    502 
    503         if (elFlags & AC_EL_LFE &&
    504             GetWindowSequence(&pAacDecoderChannelInfo[ch]->icsInfo) !=
    505                 BLOCK_LONG) {
    506           error = AAC_DEC_PARSE_ERROR;
    507           break;
    508         }
    509 
    510         if (numberOfChannels == 2 &&
    511             pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow) {
    512           pAacDecoderChannelInfo[1]->icsInfo =
    513               pAacDecoderChannelInfo[0]->icsInfo;
    514         }
    515         break;
    516 
    517       case common_max_sfb:
    518         if (FDKreadBit(hBs) == 0) {
    519           error = IcsReadMaxSfb(hBs, &pAacDecoderChannelInfo[1]->icsInfo,
    520                                 pSamplingRateInfo);
    521         }
    522         break;
    523 
    524       case ltp_data_present:
    525         if (FDKreadBits(hBs, 1) != 0) {
    526           error = AAC_DEC_UNSUPPORTED_PREDICTION;
    527         }
    528         break;
    529 
    530       case ms:
    531 
    532         INT max_sfb_ste;
    533         INT max_sfb_ste_clear;
    534 
    535         max_sfb_ste = GetScaleMaxFactorBandsTransmitted(
    536             &pAacDecoderChannelInfo[0]->icsInfo,
    537             &pAacDecoderChannelInfo[1]->icsInfo);
    538 
    539         max_sfb_ste_clear = 64;
    540 
    541         pAacDecoderChannelInfo[0]->icsInfo.max_sfb_ste = (UCHAR)max_sfb_ste;
    542         pAacDecoderChannelInfo[1]->icsInfo.max_sfb_ste = (UCHAR)max_sfb_ste;
    543 
    544         if (flags & (AC_USAC | AC_RSV603DA) &&
    545             pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.CommonWindow ==
    546                 0) {
    547           Clean_Complex_Prediction_coefficients(
    548               &pAacDecoderStaticChannelInfo[0]
    549                    ->pCpeStaticData->jointStereoPersistentData,
    550               GetWindowGroups(&pAacDecoderChannelInfo[0]->icsInfo), 0, 64);
    551         }
    552 
    553         if (CJointStereo_Read(
    554                 hBs, &pAacDecoderChannelInfo[0]->pComData->jointStereoData,
    555                 GetWindowGroups(&pAacDecoderChannelInfo[0]->icsInfo),
    556                 max_sfb_ste, max_sfb_ste_clear,
    557                 /* jointStereoPersistentData and cplxPredictionData are only
    558                    available/allocated if cplxPred is active. */
    559                 ((cplxPred == 0) || (pAacDecoderStaticChannelInfo == NULL))
    560                     ? NULL
    561                     : &pAacDecoderStaticChannelInfo[0]
    562                            ->pCpeStaticData->jointStereoPersistentData,
    563                 ((cplxPred == 0) || (pAacDecoderChannelInfo[0] == NULL))
    564                     ? NULL
    565                     : pAacDecoderChannelInfo[0]
    566                           ->pComStaticData->cplxPredictionData,
    567                 cplxPred,
    568                 GetScaleFactorBandsTotal(&pAacDecoderChannelInfo[0]->icsInfo),
    569                 GetWindowSequence(&pAacDecoderChannelInfo[0]->icsInfo),
    570                 flags)) {
    571           error = AAC_DEC_PARSE_ERROR;
    572         }
    573 
    574         break;
    575 
    576       case global_gain:
    577         pAacDecoderChannelInfo[ch]->pDynData->RawDataInfo.GlobalGain =
    578             (UCHAR)FDKreadBits(hBs, 8);
    579         break;
    580 
    581       case section_data:
    582         error = CBlock_ReadSectionData(hBs, pAacDecoderChannelInfo[ch],
    583                                        pSamplingRateInfo, flags);
    584         break;
    585 
    586       case scale_factor_data_usac:
    587         pAacDecoderChannelInfo[ch]->currAliasingSymmetry = 0;
    588         /* Set active sfb codebook indexes to HCB_ESC to make them "active" */
    589         CChannel_CodebookTableInit(
    590             pAacDecoderChannelInfo[ch]); /*  equals ReadSectionData(self,
    591                                             bs) in float soft. block.c
    592                                             line: ~599 */
    593         /* Note: The missing "break" is intentional here, since we need to call
    594          * CBlock_ReadScaleFactorData(). */
    595 
    596       case scale_factor_data:
    597         if (flags & AC_ER_RVLC) {
    598           /* read RVLC data from bitstream (error sens. cat. 1) */
    599           CRvlc_Read(pAacDecoderChannelInfo[ch], hBs);
    600         } else {
    601           error = CBlock_ReadScaleFactorData(pAacDecoderChannelInfo[ch], hBs,
    602                                              flags);
    603         }
    604         break;
    605 
    606       case pulse:
    607         if (CPulseData_Read(
    608                 hBs,
    609                 &pAacDecoderChannelInfo[ch]->pDynData->specificTo.aac.PulseData,
    610                 pSamplingRateInfo->ScaleFactorBands_Long, /* pulse data is only
    611                                                              allowed to be
    612                                                              present in long
    613                                                              blocks! */
    614                 (void *)&pAacDecoderChannelInfo[ch]->icsInfo,
    615                 frame_length) != 0) {
    616           error = AAC_DEC_DECODE_FRAME_ERROR;
    617         }
    618         break;
    619       case tns_data_present:
    620         CTns_ReadDataPresentFlag(
    621             hBs, &pAacDecoderChannelInfo[ch]->pDynData->TnsData);
    622         if (elFlags & AC_EL_LFE &&
    623             pAacDecoderChannelInfo[ch]->pDynData->TnsData.DataPresent) {
    624           error = AAC_DEC_PARSE_ERROR;
    625         }
    626         break;
    627       case tns_data:
    628         /* tns_data_present is checked inside CTns_Read(). */
    629         error = CTns_Read(hBs, &pAacDecoderChannelInfo[ch]->pDynData->TnsData,
    630                           &pAacDecoderChannelInfo[ch]->icsInfo, flags);
    631 
    632         break;
    633 
    634       case gain_control_data:
    635         break;
    636 
    637       case gain_control_data_present:
    638         if (FDKreadBits(hBs, 1)) {
    639           error = AAC_DEC_UNSUPPORTED_GAIN_CONTROL_DATA;
    640         }
    641         break;
    642 
    643       case tw_data:
    644         break;
    645       case common_tw:
    646         break;
    647       case tns_data_present_usac:
    648         if (pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_active) {
    649           CTns_ReadDataPresentUsac(
    650               hBs, &pAacDecoderChannelInfo[0]->pDynData->TnsData,
    651               &pAacDecoderChannelInfo[1]->pDynData->TnsData,
    652               &pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_on_lr,
    653               &pAacDecoderChannelInfo[0]->icsInfo, flags, elFlags,
    654               pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow);
    655         } else {
    656           pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_on_lr =
    657               (UCHAR)1;
    658         }
    659         break;
    660       case core_mode:
    661         decision_bit = FDKreadBits(hBs, 1);
    662         pAacDecoderChannelInfo[ch]->data.usac.core_mode = decision_bit;
    663         if ((ch == 1) && (pAacDecoderChannelInfo[0]->data.usac.core_mode !=
    664                           pAacDecoderChannelInfo[1]->data.usac.core_mode)) {
    665           /* StereoCoreToolInfo(core_mode[ch] ) */
    666           pAacDecoderChannelInfo[0]->pDynData->RawDataInfo.CommonWindow = 0;
    667           pAacDecoderChannelInfo[1]->pDynData->RawDataInfo.CommonWindow = 0;
    668         }
    669         break;
    670       case tns_active:
    671         pAacDecoderChannelInfo[0]->pDynData->specificTo.usac.tns_active =
    672             FDKreadBit(hBs);
    673         break;
    674       case noise:
    675         if (elFlags & AC_EL_USAC_NOISE) {
    676           pAacDecoderChannelInfo[ch]
    677               ->pDynData->specificTo.usac.fd_noise_level_and_offset =
    678               FDKreadBits(hBs, 3 + 5); /* Noise level */
    679         }
    680         break;
    681       case lpd_channel_stream:
    682 
    683       {
    684         error = CLpdChannelStream_Read(/* = lpd_channel_stream() */
    685                                        hBs, pAacDecoderChannelInfo[ch],
    686                                        pAacDecoderStaticChannelInfo[ch],
    687                                        pSamplingRateInfo, flags);
    688       }
    689 
    690         pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_LPD;
    691         break;
    692       case fac_data: {
    693         int fFacDatPresent = FDKreadBit(hBs);
    694 
    695         /* Wee need a valid fac_data[0] even if no FAC data is present (as
    696          * temporal buffer) */
    697         pAacDecoderChannelInfo[ch]->data.usac.fac_data[0] =
    698             pAacDecoderChannelInfo[ch]->data.usac.fac_data0;
    699 
    700         if (fFacDatPresent) {
    701           if (elFlags & AC_EL_LFE) {
    702             error = AAC_DEC_PARSE_ERROR;
    703             break;
    704           }
    705           /* FAC data present, this frame is FD, so the last mode had to be
    706            * ACELP. */
    707           if (pAacDecoderStaticChannelInfo[ch]->last_core_mode != LPD ||
    708               pAacDecoderStaticChannelInfo[ch]->last_lpd_mode != 0) {
    709             pAacDecoderChannelInfo[ch]->data.usac.core_mode_last = LPD;
    710             pAacDecoderChannelInfo[ch]->data.usac.lpd_mode_last = 0;
    711             /* We can't change the past! So look to the future and go ahead! */
    712           }
    713           CLpd_FAC_Read(hBs, pAacDecoderChannelInfo[ch]->data.usac.fac_data[0],
    714                         pAacDecoderChannelInfo[ch]->data.usac.fac_data_e,
    715                         CLpd_FAC_getLength(
    716                             IsLongBlock(&pAacDecoderChannelInfo[ch]->icsInfo),
    717                             pAacDecoderChannelInfo[ch]->granuleLength),
    718                         1, 0);
    719         } else {
    720           if (pAacDecoderStaticChannelInfo[ch]->last_core_mode == LPD &&
    721               pAacDecoderStaticChannelInfo[ch]->last_lpd_mode == 0) {
    722             /* ACELP to FD transitons without FAC are possible. That is why we
    723             zero it out (i.e FAC will not be considered in the subsequent
    724             calculations */
    725             FDKmemclear(pAacDecoderChannelInfo[ch]->data.usac.fac_data0,
    726                         LFAC * sizeof(FIXP_DBL));
    727           }
    728         }
    729       } break;
    730       case esc2_rvlc:
    731         if (flags & AC_ER_RVLC) {
    732           CRvlc_Decode(pAacDecoderChannelInfo[ch],
    733                        pAacDecoderStaticChannelInfo[ch], hBs);
    734         }
    735         break;
    736 
    737       case esc1_hcr:
    738         if (flags & AC_ER_HCR) {
    739           CHcr_Read(hBs, pAacDecoderChannelInfo[ch],
    740                     numberOfChannels == 2 ? ID_CPE : ID_SCE);
    741         }
    742         break;
    743 
    744       case spectral_data:
    745         error = CBlock_ReadSpectralData(hBs, pAacDecoderChannelInfo[ch],
    746                                         pSamplingRateInfo, flags);
    747         if (flags & AC_ELD) {
    748           pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_ELDFB;
    749         } else {
    750           if (flags & AC_HDAAC) {
    751             pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_INTIMDCT;
    752           } else {
    753             pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_IMDCT;
    754           }
    755         }
    756         break;
    757 
    758       case ac_spectral_data:
    759         error = CBlock_ReadAcSpectralData(
    760             hBs, pAacDecoderChannelInfo[ch], pAacDecoderStaticChannelInfo[ch],
    761             pSamplingRateInfo, frame_length, flags);
    762         pAacDecoderChannelInfo[ch]->renderMode = AACDEC_RENDER_IMDCT;
    763         break;
    764 
    765       case coupled_elements: {
    766         int num_coupled_elements, c;
    767 
    768         ind_sw_cce_flag = FDKreadBit(hBs);
    769         num_coupled_elements = FDKreadBits(hBs, 3);
    770 
    771         for (c = 0; c < (num_coupled_elements + 1); c++) {
    772           int cc_target_is_cpe;
    773 
    774           num_gain_element_lists++;
    775           cc_target_is_cpe = FDKreadBit(hBs); /* cc_target_is_cpe[c] */
    776           FDKreadBits(hBs, 4);                /* cc_target_tag_select[c] */
    777 
    778           if (cc_target_is_cpe) {
    779             int cc_l, cc_r;
    780 
    781             cc_l = FDKreadBit(hBs); /* cc_l[c] */
    782             cc_r = FDKreadBit(hBs); /* cc_r[c] */
    783 
    784             if (cc_l && cc_r) {
    785               num_gain_element_lists++;
    786             }
    787           }
    788         }
    789         FDKreadBit(hBs);     /* cc_domain */
    790         FDKreadBit(hBs);     /* gain_element_sign  */
    791         FDKreadBits(hBs, 2); /* gain_element_scale */
    792       } break;
    793 
    794       case gain_element_lists: {
    795         const CodeBookDescription *hcb;
    796         UCHAR *pCodeBook;
    797         int c;
    798 
    799         hcb = &AACcodeBookDescriptionTable[BOOKSCL];
    800         pCodeBook = pAacDecoderChannelInfo[ch]->pDynData->aCodeBook;
    801 
    802         for (c = 1; c < num_gain_element_lists; c++) {
    803           int cge;
    804           if (ind_sw_cce_flag) {
    805             cge = 1;
    806           } else {
    807             cge = FDKreadBits(hBs, 1); /* common_gain_element_present[c] */
    808           }
    809           if (cge) {
    810             /* Huffman */
    811             CBlock_DecodeHuffmanWord(
    812                 hBs, hcb); /* hcod_sf[common_gain_element[c]] 1..19 */
    813           } else {
    814             int g, sfb;
    815             for (g = 0;
    816                  g < GetWindowGroups(&pAacDecoderChannelInfo[ch]->icsInfo);
    817                  g++) {
    818               for (sfb = 0; sfb < GetScaleFactorBandsTransmitted(
    819                                       &pAacDecoderChannelInfo[ch]->icsInfo);
    820                    sfb++) {
    821                 if (pCodeBook[sfb] != ZERO_HCB) {
    822                   /* Huffman */
    823                   CBlock_DecodeHuffmanWord(
    824                       hBs,
    825                       hcb); /* hcod_sf[dpcm_gain_element[c][g][sfb]] 1..19 */
    826                 }
    827               }
    828             }
    829           }
    830         }
    831       } break;
    832 
    833         /* CRC handling */
    834       case adtscrc_start_reg1:
    835         if (pTpDec != NULL) {
    836           crcReg1 = transportDec_CrcStartReg(pTpDec, 192);
    837         }
    838         break;
    839       case adtscrc_start_reg2:
    840         if (pTpDec != NULL) {
    841           crcReg2 = transportDec_CrcStartReg(pTpDec, 128);
    842         }
    843         break;
    844       case adtscrc_end_reg1:
    845       case drmcrc_end_reg:
    846         if (pTpDec != NULL) {
    847           transportDec_CrcEndReg(pTpDec, crcReg1);
    848           crcReg1 = -1;
    849         }
    850         break;
    851       case adtscrc_end_reg2:
    852         if (crcReg1 != -1) {
    853           error = AAC_DEC_DECODE_FRAME_ERROR;
    854         } else if (pTpDec != NULL) {
    855           transportDec_CrcEndReg(pTpDec, crcReg2);
    856           crcReg2 = -1;
    857         }
    858         break;
    859       case drmcrc_start_reg:
    860         if (pTpDec != NULL) {
    861           crcReg1 = transportDec_CrcStartReg(pTpDec, 0);
    862         }
    863         break;
    864 
    865         /* Non data cases */
    866       case next_channel:
    867         ch = (ch + 1) % numberOfChannels;
    868         break;
    869       case link_sequence:
    870         list = list->next[decision_bit];
    871         i = -1;
    872         break;
    873 
    874       default:
    875         error = AAC_DEC_UNSUPPORTED_FORMAT;
    876         break;
    877     }
    878 
    879     if (error != AAC_DEC_OK) {
    880       goto bail;
    881     }
    882 
    883     i++;
    884 
    885   } while (list->id[i] != end_of_sequence);
    886 
    887   for (ch = 0; ch < numberOfChannels; ch++) {
    888     if (pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_IMDCT ||
    889         pAacDecoderChannelInfo[ch]->renderMode == AACDEC_RENDER_ELDFB) {
    890       /* Shows which bands are empty. */
    891       UCHAR *band_is_noise =
    892           pAacDecoderChannelInfo[ch]->pDynData->band_is_noise;
    893       FDKmemset(band_is_noise, (UCHAR)1, sizeof(UCHAR) * (8 * 16));
    894 
    895       error = CBlock_InverseQuantizeSpectralData(
    896           pAacDecoderChannelInfo[ch], pSamplingRateInfo, band_is_noise, 1);
    897       if (error != AAC_DEC_OK) {
    898         return error;
    899       }
    900 
    901       if (elFlags & AC_EL_USAC_NOISE) {
    902         CBlock_ApplyNoise(pAacDecoderChannelInfo[ch], pSamplingRateInfo,
    903                           &pAacDecoderStaticChannelInfo[ch]->nfRandomSeed,
    904                           band_is_noise);
    905 
    906       } /* if (elFlags & AC_EL_USAC_NOISE) */
    907     }
    908   }
    909 
    910 bail:
    911   if (crcReg1 != -1 || crcReg2 != -1) {
    912     if (error == AAC_DEC_OK) {
    913       error = AAC_DEC_DECODE_FRAME_ERROR;
    914     }
    915     if (crcReg1 != -1) {
    916       transportDec_CrcEndReg(pTpDec, crcReg1);
    917     }
    918     if (crcReg2 != -1) {
    919       transportDec_CrcEndReg(pTpDec, crcReg2);
    920     }
    921   }
    922   return error;
    923 }
    924