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 /******************* MPEG transport format decoder library *********************
     96 
     97    Author(s):   Daniel Homm
     98 
     99    Description:
    100 
    101 *******************************************************************************/
    102 
    103 #include "tpdec_lib.h"
    104 #include "tp_data.h"
    105 
    106 #include "FDK_crc.h"
    107 
    108 #include "common_fix.h"
    109 
    110 /**
    111  * The following arrays provide the IDs of the consecutive elements for each
    112  * channel configuration. Every channel_configuration has to be finalized with
    113  * ID_NONE.
    114  */
    115 static const MP4_ELEMENT_ID channel_configuration_0[] = {ID_NONE};
    116 static const MP4_ELEMENT_ID channel_configuration_1[] = {ID_SCE, ID_NONE};
    117 static const MP4_ELEMENT_ID channel_configuration_2[] = {ID_CPE, ID_NONE};
    118 static const MP4_ELEMENT_ID channel_configuration_3[] = {ID_SCE, ID_CPE,
    119                                                          ID_NONE};
    120 static const MP4_ELEMENT_ID channel_configuration_4[] = {ID_SCE, ID_CPE, ID_SCE,
    121                                                          ID_NONE};
    122 static const MP4_ELEMENT_ID channel_configuration_5[] = {ID_SCE, ID_CPE, ID_CPE,
    123                                                          ID_NONE};
    124 static const MP4_ELEMENT_ID channel_configuration_6[] = {ID_SCE, ID_CPE, ID_CPE,
    125                                                          ID_LFE, ID_NONE};
    126 static const MP4_ELEMENT_ID channel_configuration_7[] = {
    127     ID_SCE, ID_CPE, ID_CPE, ID_CPE, ID_LFE, ID_NONE};
    128 static const MP4_ELEMENT_ID channel_configuration_8[] = {
    129     ID_NONE}; /* reserved */
    130 static const MP4_ELEMENT_ID channel_configuration_9[] = {
    131     ID_NONE}; /* reserved */
    132 static const MP4_ELEMENT_ID channel_configuration_10[] = {
    133     ID_NONE}; /* reserved */
    134 static const MP4_ELEMENT_ID channel_configuration_11[] = {
    135     ID_SCE, ID_CPE, ID_CPE, ID_SCE, ID_LFE, ID_NONE};
    136 static const MP4_ELEMENT_ID channel_configuration_12[] = {
    137     ID_SCE, ID_CPE, ID_CPE, ID_CPE, ID_LFE, ID_NONE};
    138 static const MP4_ELEMENT_ID channel_configuration_13[] = {
    139     ID_SCE, ID_CPE, ID_CPE, ID_CPE, ID_CPE, ID_SCE, ID_LFE, ID_LFE, ID_SCE,
    140     ID_CPE, ID_CPE, ID_SCE, ID_CPE, ID_SCE, ID_SCE, ID_CPE, ID_NONE};
    141 static const MP4_ELEMENT_ID channel_configuration_14[] = {
    142     ID_SCE, ID_CPE, ID_CPE, ID_LAST, ID_CPE, ID_NONE};
    143 
    144 static const MP4_ELEMENT_ID *channel_configuration_array[] = {
    145     channel_configuration_0,  channel_configuration_1,
    146     channel_configuration_2,  channel_configuration_3,
    147     channel_configuration_4,  channel_configuration_5,
    148     channel_configuration_6,  channel_configuration_7,
    149     channel_configuration_8,  channel_configuration_9,
    150     channel_configuration_10, channel_configuration_11,
    151     channel_configuration_12, channel_configuration_13,
    152     channel_configuration_14};
    153 
    154 #define TP_USAC_MAX_CHANNEL_CONFIGURATION_INDEX (13)
    155 #define SC_CHANNEL_CONFIG_TAB_SIZE (TP_USAC_MAX_CHANNEL_CONFIGURATION_INDEX + 1)
    156 
    157 /* channel config structure used for sanity check */
    158 typedef struct {
    159   SCHAR nCh;  /* number of channels */
    160   SCHAR nSCE; /* number of SCE's */
    161   SCHAR nCPE; /* number of CPE's */
    162   SCHAR nLFE; /* number of LFE's */
    163 } SC_CHANNEL_CONFIG;
    164 
    165 static const SC_CHANNEL_CONFIG sc_chan_config_tab[SC_CHANNEL_CONFIG_TAB_SIZE] =
    166     {
    167         /* nCh, nSCE, nCPE, nLFE,     cci */
    168         {0, 0, 0, 0}, /*  0 */
    169         {1, 1, 0, 0}, /*  1 */
    170         {2, 0, 1, 0}, /*  2 */
    171         {3, 1, 1, 0}, /*  3 */
    172         {4, 2, 1, 0}, /*  4 */
    173         {5, 1, 2, 0}, /*  5 */
    174         {6, 1, 2, 1}, /*  6 */
    175         {8, 1, 3, 1}, /*  7 */
    176         {2, 2, 0, 0}, /*  8 */
    177         {3, 1, 1, 0}, /*  9 */
    178         {4, 0, 2, 0}, /* 10 */
    179         {7, 2, 2, 1}, /* 11 */
    180         {8, 1, 3, 1}, /* 12 */
    181         {24, 6, 8, 2} /* 13 */
    182 };
    183 
    184 void CProgramConfig_Reset(CProgramConfig *pPce) { pPce->elCounter = 0; }
    185 
    186 void CProgramConfig_Init(CProgramConfig *pPce) {
    187   FDKmemclear(pPce, sizeof(CProgramConfig));
    188   pPce->SamplingFrequencyIndex = 0xf;
    189 }
    190 
    191 int CProgramConfig_IsValid(const CProgramConfig *pPce) {
    192   return ((pPce->isValid) ? 1 : 0);
    193 }
    194 
    195 #define PCE_HEIGHT_EXT_SYNC (0xAC)
    196 
    197 /*
    198  * Read the extension for height info.
    199  * return 0 if successfull,
    200  *       -1 if the CRC failed,
    201  *       -2 if invalid HeightInfo.
    202  */
    203 static int CProgramConfig_ReadHeightExt(CProgramConfig *pPce,
    204                                         HANDLE_FDK_BITSTREAM bs,
    205                                         int *const bytesAvailable,
    206                                         const UINT alignmentAnchor) {
    207   int err = 0;
    208   FDK_CRCINFO crcInfo; /* CRC state info */
    209   INT crcReg;
    210   FDKcrcInit(&crcInfo, 0x07, 0xFF, 8);
    211   crcReg = FDKcrcStartReg(&crcInfo, bs, 0);
    212   UINT startAnchor = FDKgetValidBits(bs);
    213 
    214   FDK_ASSERT(pPce != NULL);
    215   FDK_ASSERT(bs != NULL);
    216   FDK_ASSERT(bytesAvailable != NULL);
    217 
    218   if ((startAnchor >= 24) && (*bytesAvailable >= 3) &&
    219       (FDKreadBits(bs, 8) == PCE_HEIGHT_EXT_SYNC)) {
    220     int i;
    221 
    222     for (i = 0; i < pPce->NumFrontChannelElements; i++) {
    223       if ((pPce->FrontElementHeightInfo[i] = (UCHAR)FDKreadBits(bs, 2)) >=
    224           PC_NUM_HEIGHT_LAYER) {
    225         err = -2; /* height information is out of the valid range */
    226       }
    227     }
    228     for (i = 0; i < pPce->NumSideChannelElements; i++) {
    229       if ((pPce->SideElementHeightInfo[i] = (UCHAR)FDKreadBits(bs, 2)) >=
    230           PC_NUM_HEIGHT_LAYER) {
    231         err = -2; /* height information is out of the valid range */
    232       }
    233     }
    234     for (i = 0; i < pPce->NumBackChannelElements; i++) {
    235       if ((pPce->BackElementHeightInfo[i] = (UCHAR)FDKreadBits(bs, 2)) >=
    236           PC_NUM_HEIGHT_LAYER) {
    237         err = -2; /* height information is out of the valid range */
    238       }
    239     }
    240     FDKbyteAlign(bs, alignmentAnchor);
    241 
    242     FDKcrcEndReg(&crcInfo, bs, crcReg);
    243     if ((USHORT)FDKreadBits(bs, 8) != FDKcrcGetCRC(&crcInfo)) {
    244       /* CRC failed */
    245       err = -1;
    246     }
    247     if (err != 0) {
    248       /* Reset whole height information in case an error occured during parsing.
    249          The return value ensures that pPce->isValid is set to 0 and implicit
    250          channel mapping is used. */
    251       FDKmemclear(pPce->FrontElementHeightInfo,
    252                   sizeof(pPce->FrontElementHeightInfo));
    253       FDKmemclear(pPce->SideElementHeightInfo,
    254                   sizeof(pPce->SideElementHeightInfo));
    255       FDKmemclear(pPce->BackElementHeightInfo,
    256                   sizeof(pPce->BackElementHeightInfo));
    257     }
    258   } else {
    259     /* No valid extension data found -> restore the initial bitbuffer state */
    260     FDKpushBack(bs, (INT)startAnchor - (INT)FDKgetValidBits(bs));
    261   }
    262 
    263   /* Always report the bytes read. */
    264   *bytesAvailable -= ((INT)startAnchor - (INT)FDKgetValidBits(bs)) >> 3;
    265 
    266   return (err);
    267 }
    268 
    269 void CProgramConfig_Read(CProgramConfig *pPce, HANDLE_FDK_BITSTREAM bs,
    270                          UINT alignmentAnchor) {
    271   int i, err = 0;
    272   int commentBytes;
    273 
    274   pPce->NumEffectiveChannels = 0;
    275   pPce->NumChannels = 0;
    276   pPce->ElementInstanceTag = (UCHAR)FDKreadBits(bs, 4);
    277   pPce->Profile = (UCHAR)FDKreadBits(bs, 2);
    278   pPce->SamplingFrequencyIndex = (UCHAR)FDKreadBits(bs, 4);
    279   pPce->NumFrontChannelElements = (UCHAR)FDKreadBits(bs, 4);
    280   pPce->NumSideChannelElements = (UCHAR)FDKreadBits(bs, 4);
    281   pPce->NumBackChannelElements = (UCHAR)FDKreadBits(bs, 4);
    282   pPce->NumLfeChannelElements = (UCHAR)FDKreadBits(bs, 2);
    283   pPce->NumAssocDataElements = (UCHAR)FDKreadBits(bs, 3);
    284   pPce->NumValidCcElements = (UCHAR)FDKreadBits(bs, 4);
    285 
    286   if ((pPce->MonoMixdownPresent = (UCHAR)FDKreadBits(bs, 1)) != 0) {
    287     pPce->MonoMixdownElementNumber = (UCHAR)FDKreadBits(bs, 4);
    288   }
    289 
    290   if ((pPce->StereoMixdownPresent = (UCHAR)FDKreadBits(bs, 1)) != 0) {
    291     pPce->StereoMixdownElementNumber = (UCHAR)FDKreadBits(bs, 4);
    292   }
    293 
    294   if ((pPce->MatrixMixdownIndexPresent = (UCHAR)FDKreadBits(bs, 1)) != 0) {
    295     pPce->MatrixMixdownIndex = (UCHAR)FDKreadBits(bs, 2);
    296     pPce->PseudoSurroundEnable = (UCHAR)FDKreadBits(bs, 1);
    297   }
    298 
    299   for (i = 0; i < pPce->NumFrontChannelElements; i++) {
    300     pPce->FrontElementIsCpe[i] = (UCHAR)FDKreadBits(bs, 1);
    301     pPce->FrontElementTagSelect[i] = (UCHAR)FDKreadBits(bs, 4);
    302     pPce->NumChannels += pPce->FrontElementIsCpe[i] ? 2 : 1;
    303   }
    304 
    305   for (i = 0; i < pPce->NumSideChannelElements; i++) {
    306     pPce->SideElementIsCpe[i] = (UCHAR)FDKreadBits(bs, 1);
    307     pPce->SideElementTagSelect[i] = (UCHAR)FDKreadBits(bs, 4);
    308     pPce->NumChannels += pPce->SideElementIsCpe[i] ? 2 : 1;
    309   }
    310 
    311   for (i = 0; i < pPce->NumBackChannelElements; i++) {
    312     pPce->BackElementIsCpe[i] = (UCHAR)FDKreadBits(bs, 1);
    313     pPce->BackElementTagSelect[i] = (UCHAR)FDKreadBits(bs, 4);
    314     pPce->NumChannels += pPce->BackElementIsCpe[i] ? 2 : 1;
    315   }
    316 
    317   pPce->NumEffectiveChannels = pPce->NumChannels;
    318 
    319   for (i = 0; i < pPce->NumLfeChannelElements; i++) {
    320     pPce->LfeElementTagSelect[i] = (UCHAR)FDKreadBits(bs, 4);
    321     pPce->NumChannels += 1;
    322   }
    323 
    324   for (i = 0; i < pPce->NumAssocDataElements; i++) {
    325     pPce->AssocDataElementTagSelect[i] = (UCHAR)FDKreadBits(bs, 4);
    326   }
    327 
    328   for (i = 0; i < pPce->NumValidCcElements; i++) {
    329     pPce->CcElementIsIndSw[i] = (UCHAR)FDKreadBits(bs, 1);
    330     pPce->ValidCcElementTagSelect[i] = (UCHAR)FDKreadBits(bs, 4);
    331   }
    332 
    333   FDKbyteAlign(bs, alignmentAnchor);
    334 
    335   pPce->CommentFieldBytes = (UCHAR)FDKreadBits(bs, 8);
    336   commentBytes = pPce->CommentFieldBytes;
    337 
    338   /* Search for height info extension and read it if available */
    339   err = CProgramConfig_ReadHeightExt(pPce, bs, &commentBytes, alignmentAnchor);
    340 
    341   for (i = 0; i < commentBytes; i++) {
    342     UCHAR text;
    343 
    344     text = (UCHAR)FDKreadBits(bs, 8);
    345 
    346     if (i < PC_COMMENTLENGTH) {
    347       pPce->Comment[i] = text;
    348     }
    349   }
    350 
    351   pPce->isValid = (err) ? 0 : 1;
    352 }
    353 
    354 /*
    355  * Compare two program configurations.
    356  * Returns the result of the comparison:
    357  *  -1 - completely different
    358  *   0 - completely equal
    359  *   1 - different but same channel configuration
    360  *   2 - different channel configuration but same number of channels
    361  */
    362 int CProgramConfig_Compare(const CProgramConfig *const pPce1,
    363                            const CProgramConfig *const pPce2) {
    364   int result = 0; /* Innocent until proven false. */
    365 
    366   if (FDKmemcmp(pPce1, pPce2, sizeof(CProgramConfig)) !=
    367       0) { /* Configurations are not completely equal.
    368               So look into details and analyse the channel configurations: */
    369     result = -1;
    370 
    371     if (pPce1->NumChannels ==
    372         pPce2->NumChannels) { /* Now the logic changes. We first assume to have
    373                                  the same channel configuration and then prove
    374                                  if this assumption is true. */
    375       result = 1;
    376 
    377       /* Front channels */
    378       if (pPce1->NumFrontChannelElements != pPce2->NumFrontChannelElements) {
    379         result = 2; /* different number of front channel elements */
    380       } else {
    381         int el, numCh1 = 0, numCh2 = 0;
    382         for (el = 0; el < pPce1->NumFrontChannelElements; el += 1) {
    383           if (pPce1->FrontElementHeightInfo[el] !=
    384               pPce2->FrontElementHeightInfo[el]) {
    385             result = 2; /* different height info */
    386             break;
    387           }
    388           numCh1 += pPce1->FrontElementIsCpe[el] ? 2 : 1;
    389           numCh2 += pPce2->FrontElementIsCpe[el] ? 2 : 1;
    390         }
    391         if (numCh1 != numCh2) {
    392           result = 2; /* different number of front channels */
    393         }
    394       }
    395       /* Side channels */
    396       if (pPce1->NumSideChannelElements != pPce2->NumSideChannelElements) {
    397         result = 2; /* different number of side channel elements */
    398       } else {
    399         int el, numCh1 = 0, numCh2 = 0;
    400         for (el = 0; el < pPce1->NumSideChannelElements; el += 1) {
    401           if (pPce1->SideElementHeightInfo[el] !=
    402               pPce2->SideElementHeightInfo[el]) {
    403             result = 2; /* different height info */
    404             break;
    405           }
    406           numCh1 += pPce1->SideElementIsCpe[el] ? 2 : 1;
    407           numCh2 += pPce2->SideElementIsCpe[el] ? 2 : 1;
    408         }
    409         if (numCh1 != numCh2) {
    410           result = 2; /* different number of side channels */
    411         }
    412       }
    413       /* Back channels */
    414       if (pPce1->NumBackChannelElements != pPce2->NumBackChannelElements) {
    415         result = 2; /* different number of back channel elements */
    416       } else {
    417         int el, numCh1 = 0, numCh2 = 0;
    418         for (el = 0; el < pPce1->NumBackChannelElements; el += 1) {
    419           if (pPce1->BackElementHeightInfo[el] !=
    420               pPce2->BackElementHeightInfo[el]) {
    421             result = 2; /* different height info */
    422             break;
    423           }
    424           numCh1 += pPce1->BackElementIsCpe[el] ? 2 : 1;
    425           numCh2 += pPce2->BackElementIsCpe[el] ? 2 : 1;
    426         }
    427         if (numCh1 != numCh2) {
    428           result = 2; /* different number of back channels */
    429         }
    430       }
    431       /* LFE channels */
    432       if (pPce1->NumLfeChannelElements != pPce2->NumLfeChannelElements) {
    433         result = 2; /* different number of lfe channels */
    434       }
    435       /* LFEs are always SCEs so we don't need to count the channels. */
    436     }
    437   }
    438 
    439   return result;
    440 }
    441 
    442 void CProgramConfig_GetDefault(CProgramConfig *pPce, const UINT channelConfig) {
    443   FDK_ASSERT(pPce != NULL);
    444 
    445   /* Init PCE */
    446   CProgramConfig_Init(pPce);
    447   pPce->Profile =
    448       1; /* Set AAC LC because it is the only supported object type. */
    449 
    450   switch (channelConfig) {
    451     /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    452     case 32: /* 7.1 side channel configuration as defined in FDK_audio.h */
    453       pPce->NumFrontChannelElements = 2;
    454       pPce->FrontElementIsCpe[0] = 0;
    455       pPce->FrontElementIsCpe[1] = 1;
    456       pPce->NumSideChannelElements = 1;
    457       pPce->SideElementIsCpe[0] = 1;
    458       pPce->NumBackChannelElements = 1;
    459       pPce->BackElementIsCpe[0] = 1;
    460       pPce->NumLfeChannelElements = 1;
    461       pPce->NumChannels = 8;
    462       pPce->NumEffectiveChannels = 7;
    463       pPce->isValid = 1;
    464       break;
    465     /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    466     case 12: /* 3/0/4.1ch surround back */
    467       pPce->BackElementIsCpe[1] = 1;
    468       pPce->NumChannels += 1;
    469       pPce->NumEffectiveChannels += 1;
    470     case 11: /* 3/0/3.1ch */
    471       pPce->NumFrontChannelElements += 2;
    472       pPce->FrontElementIsCpe[0] = 0;
    473       pPce->FrontElementIsCpe[1] = 1;
    474       pPce->NumBackChannelElements += 2;
    475       pPce->BackElementIsCpe[0] = 1;
    476       pPce->BackElementIsCpe[1] += 0;
    477       pPce->NumLfeChannelElements += 1;
    478       pPce->NumChannels += 7;
    479       pPce->NumEffectiveChannels += 6;
    480       pPce->isValid = 1;
    481       break;
    482     /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    483     case 14:                               /* 2/0/0-3/0/2-0.1ch front height */
    484       pPce->FrontElementHeightInfo[2] = 1; /* Top speaker */
    485     case 7:                                /* 5/0/2.1ch front */
    486       pPce->NumFrontChannelElements += 1;
    487       pPce->FrontElementIsCpe[2] = 1;
    488       pPce->NumChannels += 2;
    489       pPce->NumEffectiveChannels += 2;
    490     case 6: /* 3/0/2.1ch */
    491       pPce->NumLfeChannelElements += 1;
    492       pPce->NumChannels += 1;
    493     case 5: /* 3/0/2.0ch */
    494     case 4: /* 3/0/1.0ch */
    495       pPce->NumBackChannelElements += 1;
    496       pPce->BackElementIsCpe[0] = (channelConfig > 4) ? 1 : 0;
    497       pPce->NumChannels += (channelConfig > 4) ? 2 : 1;
    498       pPce->NumEffectiveChannels += (channelConfig > 4) ? 2 : 1;
    499     case 3: /* 3/0/0.0ch */
    500       pPce->NumFrontChannelElements += 1;
    501       pPce->FrontElementIsCpe[1] = 1;
    502       pPce->NumChannels += 2;
    503       pPce->NumEffectiveChannels += 2;
    504     case 1: /* 1/0/0.0ch */
    505       pPce->NumFrontChannelElements += 1;
    506       pPce->FrontElementIsCpe[0] = 0;
    507       pPce->NumChannels += 1;
    508       pPce->NumEffectiveChannels += 1;
    509       pPce->isValid = 1;
    510       break;
    511     /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    512     case 2: /* 2/0/0.ch */
    513       pPce->NumFrontChannelElements = 1;
    514       pPce->FrontElementIsCpe[0] = 1;
    515       pPce->NumChannels += 2;
    516       pPce->NumEffectiveChannels += 2;
    517       pPce->isValid = 1;
    518       break;
    519     /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    520     default:
    521       pPce->isValid = 0; /* To be explicit! */
    522       break;
    523   }
    524 
    525   if (pPce->isValid) {
    526     /* Create valid element instance tags */
    527     int el, elTagSce = 0, elTagCpe = 0;
    528 
    529     for (el = 0; el < pPce->NumFrontChannelElements; el += 1) {
    530       pPce->FrontElementTagSelect[el] =
    531           (pPce->FrontElementIsCpe[el]) ? elTagCpe++ : elTagSce++;
    532     }
    533     for (el = 0; el < pPce->NumSideChannelElements; el += 1) {
    534       pPce->SideElementTagSelect[el] =
    535           (pPce->SideElementIsCpe[el]) ? elTagCpe++ : elTagSce++;
    536     }
    537     for (el = 0; el < pPce->NumBackChannelElements; el += 1) {
    538       pPce->BackElementTagSelect[el] =
    539           (pPce->BackElementIsCpe[el]) ? elTagCpe++ : elTagSce++;
    540     }
    541     elTagSce = 0;
    542     for (el = 0; el < pPce->NumLfeChannelElements; el += 1) {
    543       pPce->LfeElementTagSelect[el] = elTagSce++;
    544     }
    545   }
    546 }
    547 
    548 /**
    549  * \brief get implicit audio channel type for given channelConfig and MPEG
    550  * ordered channel index
    551  * \param channelConfig MPEG channelConfiguration from 1 upto 14
    552  * \param index MPEG channel order index
    553  * \return audio channel type.
    554  */
    555 static void getImplicitAudioChannelTypeAndIndex(AUDIO_CHANNEL_TYPE *chType,
    556                                                 UCHAR *chIndex,
    557                                                 UINT channelConfig,
    558                                                 UINT index) {
    559   if (index < 3) {
    560     *chType = ACT_FRONT;
    561     *chIndex = index;
    562   } else {
    563     switch (channelConfig) {
    564       case 4: /* SCE, CPE, SCE */
    565       case 5: /* SCE, CPE, CPE */
    566       case 6: /* SCE, CPE, CPE, LFE */
    567         switch (index) {
    568           case 3:
    569           case 4:
    570             *chType = ACT_BACK;
    571             *chIndex = index - 3;
    572             break;
    573           case 5:
    574             *chType = ACT_LFE;
    575             *chIndex = 0;
    576             break;
    577         }
    578         break;
    579       case 7: /* SCE,CPE,CPE,CPE,LFE */
    580         switch (index) {
    581           case 3:
    582           case 4:
    583             *chType = ACT_FRONT;
    584             *chIndex = index;
    585             break;
    586           case 5:
    587           case 6:
    588             *chType = ACT_BACK;
    589             *chIndex = index - 5;
    590             break;
    591           case 7:
    592             *chType = ACT_LFE;
    593             *chIndex = 0;
    594             break;
    595         }
    596         break;
    597       case 11: /* SCE,CPE,CPE,SCE,LFE */
    598         if (index < 6) {
    599           *chType = ACT_BACK;
    600           *chIndex = index - 3;
    601         } else {
    602           *chType = ACT_LFE;
    603           *chIndex = 0;
    604         }
    605         break;
    606       case 12: /* SCE,CPE,CPE,CPE,LFE */
    607         if (index < 7) {
    608           *chType = ACT_BACK;
    609           *chIndex = index - 3;
    610         } else {
    611           *chType = ACT_LFE;
    612           *chIndex = 0;
    613         }
    614         break;
    615       case 14: /* SCE,CPE,CPE,LFE,CPE */
    616         switch (index) {
    617           case 3:
    618           case 4:
    619             *chType = ACT_BACK;
    620             *chIndex = index - 3;
    621             break;
    622           case 5:
    623             *chType = ACT_LFE;
    624             *chIndex = 0;
    625             break;
    626           case 6:
    627           case 7:
    628             *chType = ACT_FRONT_TOP;
    629             *chIndex = index - 6; /* handle the top layer independently */
    630             break;
    631         }
    632         break;
    633       default:
    634         *chType = ACT_NONE;
    635         break;
    636     }
    637   }
    638 }
    639 
    640 int CProgramConfig_LookupElement(CProgramConfig *pPce, UINT channelConfig,
    641                                  const UINT tag, const UINT channelIdx,
    642                                  UCHAR chMapping[], AUDIO_CHANNEL_TYPE chType[],
    643                                  UCHAR chIndex[], const UINT chDescrLen,
    644                                  UCHAR *elMapping, MP4_ELEMENT_ID elList[],
    645                                  MP4_ELEMENT_ID elType) {
    646   if (channelConfig > 0) {
    647     /* Constant channel mapping must have
    648        been set during initialization. */
    649     if (IS_CHANNEL_ELEMENT(elType)) {
    650       *elMapping = pPce->elCounter;
    651       if (elList[pPce->elCounter] != elType &&
    652           !IS_USAC_CHANNEL_ELEMENT(elType)) {
    653         /* Not in the list */
    654         if ((channelConfig == 2) &&
    655             (elType == ID_SCE)) { /* This scenario occurs with HE-AAC v2 streams
    656                                      of buggy encoders. In other decoder
    657                                      implementations decoding of this kind of
    658                                      streams is desired. */
    659           channelConfig = 1;
    660         } else if ((elList[pPce->elCounter] == ID_LFE) &&
    661                    (elType ==
    662                     ID_SCE)) { /* Decode bitstreams which wrongly use ID_SCE
    663                                   instead of ID_LFE element type. */
    664           ;
    665         } else {
    666           return 0;
    667         }
    668       }
    669       /* Assume all front channels */
    670       getImplicitAudioChannelTypeAndIndex(
    671           &chType[channelIdx], &chIndex[channelIdx], channelConfig, channelIdx);
    672       if (elType == ID_CPE || elType == ID_USAC_CPE) {
    673         chType[channelIdx + 1] = chType[channelIdx];
    674         chIndex[channelIdx + 1] = chIndex[channelIdx] + 1;
    675       }
    676       pPce->elCounter++;
    677     }
    678     /* Accept all non-channel elements, too. */
    679     return 1;
    680   } else {
    681     if ((!pPce->isValid) || (pPce->NumChannels > chDescrLen)) {
    682       /* Implicit channel mapping. */
    683       if (IS_USAC_CHANNEL_ELEMENT(elType)) {
    684         *elMapping = pPce->elCounter++;
    685       } else if (IS_MP4_CHANNEL_ELEMENT(elType)) {
    686         /* Store all channel element IDs */
    687         elList[pPce->elCounter] = elType;
    688         *elMapping = pPce->elCounter++;
    689       }
    690     } else {
    691       /* Accept the additional channel(s), only if the tag is in the lists */
    692       int isCpe = 0, i;
    693       /* Element counter */
    694       int ec[PC_NUM_HEIGHT_LAYER] = {0};
    695       /* Channel counters */
    696       int cc[PC_NUM_HEIGHT_LAYER] = {0};
    697       int fc[PC_NUM_HEIGHT_LAYER] = {0}; /* front channel counter */
    698       int sc[PC_NUM_HEIGHT_LAYER] = {0}; /* side channel counter */
    699       int bc[PC_NUM_HEIGHT_LAYER] = {0}; /* back channel counter */
    700       int lc = 0;                        /* lfe channel counter */
    701 
    702       /* General MPEG (PCE) composition rules:
    703          - Over all:
    704              <normal height channels><top height channels><bottom height
    705          channels>
    706          - Within each height layer:
    707              <front channels><side channels><back channels>
    708          - Exception:
    709              The LFE channels have no height info and thus they are arranged at
    710          the very end of the normal height layer channels.
    711        */
    712 
    713       switch (elType) {
    714         case ID_CPE:
    715           isCpe = 1;
    716         case ID_SCE:
    717           /* search in front channels */
    718           for (i = 0; i < pPce->NumFrontChannelElements; i++) {
    719             int heightLayer = pPce->FrontElementHeightInfo[i];
    720             if (isCpe == pPce->FrontElementIsCpe[i] &&
    721                 pPce->FrontElementTagSelect[i] == tag) {
    722               int h, elIdx = ec[heightLayer], chIdx = cc[heightLayer];
    723               AUDIO_CHANNEL_TYPE aChType =
    724                   (AUDIO_CHANNEL_TYPE)((heightLayer << 4) | ACT_FRONT);
    725               for (h = heightLayer - 1; h >= 0; h -= 1) {
    726                 int el;
    727                 /* Count front channels/elements */
    728                 for (el = 0; el < pPce->NumFrontChannelElements; el += 1) {
    729                   if (pPce->FrontElementHeightInfo[el] == h) {
    730                     elIdx += 1;
    731                     chIdx += (pPce->FrontElementIsCpe[el]) ? 2 : 1;
    732                   }
    733                 }
    734                 /* Count side channels/elements */
    735                 for (el = 0; el < pPce->NumSideChannelElements; el += 1) {
    736                   if (pPce->SideElementHeightInfo[el] == h) {
    737                     elIdx += 1;
    738                     chIdx += (pPce->SideElementIsCpe[el]) ? 2 : 1;
    739                   }
    740                 }
    741                 /* Count back channels/elements */
    742                 for (el = 0; el < pPce->NumBackChannelElements; el += 1) {
    743                   if (pPce->BackElementHeightInfo[el] == h) {
    744                     elIdx += 1;
    745                     chIdx += (pPce->BackElementIsCpe[el]) ? 2 : 1;
    746                   }
    747                 }
    748                 if (h == 0) { /* normal height */
    749                   elIdx += pPce->NumLfeChannelElements;
    750                   chIdx += pPce->NumLfeChannelElements;
    751                 }
    752               }
    753               chMapping[chIdx] = channelIdx;
    754               chType[chIdx] = aChType;
    755               chIndex[chIdx] = fc[heightLayer];
    756               if (isCpe) {
    757                 chMapping[chIdx + 1] = channelIdx + 1;
    758                 chType[chIdx + 1] = aChType;
    759                 chIndex[chIdx + 1] = fc[heightLayer] + 1;
    760               }
    761               *elMapping = elIdx;
    762               return 1;
    763             }
    764             ec[heightLayer] += 1;
    765             if (pPce->FrontElementIsCpe[i]) {
    766               cc[heightLayer] += 2;
    767               fc[heightLayer] += 2;
    768             } else {
    769               cc[heightLayer] += 1;
    770               fc[heightLayer] += 1;
    771             }
    772           }
    773           /* search in side channels */
    774           for (i = 0; i < pPce->NumSideChannelElements; i++) {
    775             int heightLayer = pPce->SideElementHeightInfo[i];
    776             if (isCpe == pPce->SideElementIsCpe[i] &&
    777                 pPce->SideElementTagSelect[i] == tag) {
    778               int h, elIdx = ec[heightLayer], chIdx = cc[heightLayer];
    779               AUDIO_CHANNEL_TYPE aChType =
    780                   (AUDIO_CHANNEL_TYPE)((heightLayer << 4) | ACT_SIDE);
    781               for (h = heightLayer - 1; h >= 0; h -= 1) {
    782                 int el;
    783                 /* Count front channels/elements */
    784                 for (el = 0; el < pPce->NumFrontChannelElements; el += 1) {
    785                   if (pPce->FrontElementHeightInfo[el] == h) {
    786                     elIdx += 1;
    787                     chIdx += (pPce->FrontElementIsCpe[el]) ? 2 : 1;
    788                   }
    789                 }
    790                 /* Count side channels/elements */
    791                 for (el = 0; el < pPce->NumSideChannelElements; el += 1) {
    792                   if (pPce->SideElementHeightInfo[el] == h) {
    793                     elIdx += 1;
    794                     chIdx += (pPce->SideElementIsCpe[el]) ? 2 : 1;
    795                   }
    796                 }
    797                 /* Count back channels/elements */
    798                 for (el = 0; el < pPce->NumBackChannelElements; el += 1) {
    799                   if (pPce->BackElementHeightInfo[el] == h) {
    800                     elIdx += 1;
    801                     chIdx += (pPce->BackElementIsCpe[el]) ? 2 : 1;
    802                   }
    803                 }
    804                 if (h ==
    805                     0) { /* LFE channels belong to the normal height layer */
    806                   elIdx += pPce->NumLfeChannelElements;
    807                   chIdx += pPce->NumLfeChannelElements;
    808                 }
    809               }
    810               chMapping[chIdx] = channelIdx;
    811               chType[chIdx] = aChType;
    812               chIndex[chIdx] = sc[heightLayer];
    813               if (isCpe) {
    814                 chMapping[chIdx + 1] = channelIdx + 1;
    815                 chType[chIdx + 1] = aChType;
    816                 chIndex[chIdx + 1] = sc[heightLayer] + 1;
    817               }
    818               *elMapping = elIdx;
    819               return 1;
    820             }
    821             ec[heightLayer] += 1;
    822             if (pPce->SideElementIsCpe[i]) {
    823               cc[heightLayer] += 2;
    824               sc[heightLayer] += 2;
    825             } else {
    826               cc[heightLayer] += 1;
    827               sc[heightLayer] += 1;
    828             }
    829           }
    830           /* search in back channels */
    831           for (i = 0; i < pPce->NumBackChannelElements; i++) {
    832             int heightLayer = pPce->BackElementHeightInfo[i];
    833             if (isCpe == pPce->BackElementIsCpe[i] &&
    834                 pPce->BackElementTagSelect[i] == tag) {
    835               int h, elIdx = ec[heightLayer], chIdx = cc[heightLayer];
    836               AUDIO_CHANNEL_TYPE aChType =
    837                   (AUDIO_CHANNEL_TYPE)((heightLayer << 4) | ACT_BACK);
    838               for (h = heightLayer - 1; h >= 0; h -= 1) {
    839                 int el;
    840                 /* Count front channels/elements */
    841                 for (el = 0; el < pPce->NumFrontChannelElements; el += 1) {
    842                   if (pPce->FrontElementHeightInfo[el] == h) {
    843                     elIdx += 1;
    844                     chIdx += (pPce->FrontElementIsCpe[el]) ? 2 : 1;
    845                   }
    846                 }
    847                 /* Count side channels/elements */
    848                 for (el = 0; el < pPce->NumSideChannelElements; el += 1) {
    849                   if (pPce->SideElementHeightInfo[el] == h) {
    850                     elIdx += 1;
    851                     chIdx += (pPce->SideElementIsCpe[el]) ? 2 : 1;
    852                   }
    853                 }
    854                 /* Count back channels/elements */
    855                 for (el = 0; el < pPce->NumBackChannelElements; el += 1) {
    856                   if (pPce->BackElementHeightInfo[el] == h) {
    857                     elIdx += 1;
    858                     chIdx += (pPce->BackElementIsCpe[el]) ? 2 : 1;
    859                   }
    860                 }
    861                 if (h == 0) { /* normal height */
    862                   elIdx += pPce->NumLfeChannelElements;
    863                   chIdx += pPce->NumLfeChannelElements;
    864                 }
    865               }
    866               chMapping[chIdx] = channelIdx;
    867               chType[chIdx] = aChType;
    868               chIndex[chIdx] = bc[heightLayer];
    869               if (isCpe) {
    870                 chMapping[chIdx + 1] = channelIdx + 1;
    871                 chType[chIdx + 1] = aChType;
    872                 chIndex[chIdx + 1] = bc[heightLayer] + 1;
    873               }
    874               *elMapping = elIdx;
    875               return 1;
    876             }
    877             ec[heightLayer] += 1;
    878             if (pPce->BackElementIsCpe[i]) {
    879               cc[heightLayer] += 2;
    880               bc[heightLayer] += 2;
    881             } else {
    882               cc[heightLayer] += 1;
    883               bc[heightLayer] += 1;
    884             }
    885           }
    886           break;
    887 
    888         case ID_LFE: { /* Unfortunately we have to go through all normal height
    889                           layer elements to get the position of the LFE
    890                           channels. Start with counting the front
    891                           channels/elements at normal height */
    892           for (i = 0; i < pPce->NumFrontChannelElements; i += 1) {
    893             int heightLayer = pPce->FrontElementHeightInfo[i];
    894             ec[heightLayer] += 1;
    895             cc[heightLayer] += (pPce->FrontElementIsCpe[i]) ? 2 : 1;
    896           }
    897           /* Count side channels/elements at normal height */
    898           for (i = 0; i < pPce->NumSideChannelElements; i += 1) {
    899             int heightLayer = pPce->SideElementHeightInfo[i];
    900             ec[heightLayer] += 1;
    901             cc[heightLayer] += (pPce->SideElementIsCpe[i]) ? 2 : 1;
    902           }
    903           /* Count back channels/elements at normal height */
    904           for (i = 0; i < pPce->NumBackChannelElements; i += 1) {
    905             int heightLayer = pPce->BackElementHeightInfo[i];
    906             ec[heightLayer] += 1;
    907             cc[heightLayer] += (pPce->BackElementIsCpe[i]) ? 2 : 1;
    908           }
    909 
    910           /* search in lfe channels */
    911           for (i = 0; i < pPce->NumLfeChannelElements; i++) {
    912             int elIdx =
    913                 ec[0]; /* LFE channels belong to the normal height layer */
    914             int chIdx = cc[0];
    915             if (pPce->LfeElementTagSelect[i] == tag) {
    916               chMapping[chIdx] = channelIdx;
    917               *elMapping = elIdx;
    918               chType[chIdx] = ACT_LFE;
    919               chIndex[chIdx] = lc;
    920               return 1;
    921             }
    922             ec[0] += 1;
    923             cc[0] += 1;
    924             lc += 1;
    925           }
    926         } break;
    927 
    928         /* Non audio elements */
    929         case ID_CCE:
    930           /* search in cce channels */
    931           for (i = 0; i < pPce->NumValidCcElements; i++) {
    932             if (pPce->ValidCcElementTagSelect[i] == tag) {
    933               return 1;
    934             }
    935           }
    936           break;
    937         case ID_DSE:
    938           /* search associated data elements */
    939           for (i = 0; i < pPce->NumAssocDataElements; i++) {
    940             if (pPce->AssocDataElementTagSelect[i] == tag) {
    941               return 1;
    942             }
    943           }
    944           break;
    945         default:
    946           return 0;
    947       }
    948       return 0; /* not found in any list */
    949     }
    950   }
    951 
    952   return 1;
    953 }
    954 
    955 #define SPEAKER_PLANE_NORMAL 0
    956 #define SPEAKER_PLANE_TOP 1
    957 #define SPEAKER_PLANE_BOTTOM 2
    958 
    959 void CProgramConfig_GetChannelDescription(const UINT chConfig,
    960                                           const CProgramConfig *pPce,
    961                                           AUDIO_CHANNEL_TYPE chType[],
    962                                           UCHAR chIndex[]) {
    963   FDK_ASSERT(chType != NULL);
    964   FDK_ASSERT(chIndex != NULL);
    965 
    966   if ((chConfig == 0) && (pPce != NULL)) {
    967     if (pPce->isValid) {
    968       int spkPlane, chIdx = 0;
    969       for (spkPlane = SPEAKER_PLANE_NORMAL; spkPlane <= SPEAKER_PLANE_BOTTOM;
    970            spkPlane += 1) {
    971         int elIdx, grpChIdx = 0;
    972         for (elIdx = 0; elIdx < pPce->NumFrontChannelElements; elIdx += 1) {
    973           if (pPce->FrontElementHeightInfo[elIdx] == spkPlane) {
    974             chType[chIdx] = (AUDIO_CHANNEL_TYPE)((spkPlane << 4) | ACT_FRONT);
    975             chIndex[chIdx++] = grpChIdx++;
    976             if (pPce->FrontElementIsCpe[elIdx]) {
    977               chType[chIdx] = (AUDIO_CHANNEL_TYPE)((spkPlane << 4) | ACT_FRONT);
    978               chIndex[chIdx++] = grpChIdx++;
    979             }
    980           }
    981         }
    982         grpChIdx = 0;
    983         for (elIdx = 0; elIdx < pPce->NumSideChannelElements; elIdx += 1) {
    984           if (pPce->SideElementHeightInfo[elIdx] == spkPlane) {
    985             chType[chIdx] = (AUDIO_CHANNEL_TYPE)((spkPlane << 4) | ACT_SIDE);
    986             chIndex[chIdx++] = grpChIdx++;
    987             if (pPce->SideElementIsCpe[elIdx]) {
    988               chType[chIdx] = (AUDIO_CHANNEL_TYPE)((spkPlane << 4) | ACT_SIDE);
    989               chIndex[chIdx++] = grpChIdx++;
    990             }
    991           }
    992         }
    993         grpChIdx = 0;
    994         for (elIdx = 0; elIdx < pPce->NumBackChannelElements; elIdx += 1) {
    995           if (pPce->BackElementHeightInfo[elIdx] == spkPlane) {
    996             chType[chIdx] = (AUDIO_CHANNEL_TYPE)((spkPlane << 4) | ACT_BACK);
    997             chIndex[chIdx++] = grpChIdx++;
    998             if (pPce->BackElementIsCpe[elIdx]) {
    999               chType[chIdx] = (AUDIO_CHANNEL_TYPE)((spkPlane << 4) | ACT_BACK);
   1000               chIndex[chIdx++] = grpChIdx++;
   1001             }
   1002           }
   1003         }
   1004         grpChIdx = 0;
   1005         if (spkPlane == SPEAKER_PLANE_NORMAL) {
   1006           for (elIdx = 0; elIdx < pPce->NumLfeChannelElements; elIdx += 1) {
   1007             chType[chIdx] = ACT_LFE;
   1008             chIndex[chIdx++] = grpChIdx++;
   1009           }
   1010         }
   1011       }
   1012     }
   1013   } else {
   1014     int chIdx;
   1015     for (chIdx = 0; chIdx < getNumberOfTotalChannels(chConfig); chIdx += 1) {
   1016       getImplicitAudioChannelTypeAndIndex(&chType[chIdx], &chIndex[chIdx],
   1017                                           chConfig, chIdx);
   1018     }
   1019   }
   1020 }
   1021 
   1022 int CProgramConfig_GetPceChMap(const CProgramConfig *pPce, UCHAR pceChMap[],
   1023                                const UINT pceChMapLen) {
   1024   const UCHAR *nElements = &pPce->NumFrontChannelElements;
   1025   const UCHAR *elHeight[3], *elIsCpe[3];
   1026   unsigned chIdx, plane, grp, offset, totCh[3], numCh[3][4];
   1027 
   1028   FDK_ASSERT(pPce != NULL);
   1029   FDK_ASSERT(pceChMap != NULL);
   1030 
   1031   /* Init counter: */
   1032   FDKmemclear(totCh, 3 * sizeof(unsigned));
   1033   FDKmemclear(numCh, 3 * 4 * sizeof(unsigned));
   1034 
   1035   /* Analyse PCE: */
   1036   elHeight[0] = pPce->FrontElementHeightInfo;
   1037   elIsCpe[0] = pPce->FrontElementIsCpe;
   1038   elHeight[1] = pPce->SideElementHeightInfo;
   1039   elIsCpe[1] = pPce->SideElementIsCpe;
   1040   elHeight[2] = pPce->BackElementHeightInfo;
   1041   elIsCpe[2] = pPce->BackElementIsCpe;
   1042 
   1043   for (plane = 0; plane <= SPEAKER_PLANE_BOTTOM; plane += 1) {
   1044     for (grp = 0; grp < 3; grp += 1) { /* front, side, back */
   1045       unsigned el;
   1046       for (el = 0; el < nElements[grp]; el += 1) {
   1047         if (elHeight[grp][el] == plane) {
   1048           unsigned elCh = elIsCpe[grp][el] ? 2 : 1;
   1049           numCh[plane][grp] += elCh;
   1050           totCh[plane] += elCh;
   1051         }
   1052       }
   1053     }
   1054     if (plane == SPEAKER_PLANE_NORMAL) {
   1055       unsigned elCh = pPce->NumLfeChannelElements;
   1056       numCh[plane][grp] += elCh;
   1057       totCh[plane] += elCh;
   1058     }
   1059   }
   1060   /* Sanity checks: */
   1061   chIdx = totCh[SPEAKER_PLANE_NORMAL] + totCh[SPEAKER_PLANE_TOP] +
   1062           totCh[SPEAKER_PLANE_BOTTOM];
   1063   if (chIdx > pceChMapLen) {
   1064     return -1;
   1065   }
   1066 
   1067   /* Create map: */
   1068   offset = grp = 0;
   1069   unsigned grpThresh = numCh[SPEAKER_PLANE_NORMAL][grp];
   1070   for (chIdx = 0; chIdx < totCh[SPEAKER_PLANE_NORMAL]; chIdx += 1) {
   1071     while ((chIdx >= grpThresh) && (grp < 3)) {
   1072       offset += numCh[1][grp] + numCh[2][grp];
   1073       grp += 1;
   1074       grpThresh += numCh[SPEAKER_PLANE_NORMAL][grp];
   1075     }
   1076     pceChMap[chIdx] = chIdx + offset;
   1077   }
   1078   offset = 0;
   1079   for (grp = 0; grp < 4; grp += 1) { /* front, side, back and lfe */
   1080     offset += numCh[SPEAKER_PLANE_NORMAL][grp];
   1081     for (plane = SPEAKER_PLANE_TOP; plane <= SPEAKER_PLANE_BOTTOM; plane += 1) {
   1082       unsigned mapCh;
   1083       for (mapCh = 0; mapCh < numCh[plane][grp]; mapCh += 1) {
   1084         pceChMap[chIdx++] = offset;
   1085         offset += 1;
   1086       }
   1087     }
   1088   }
   1089   return 0;
   1090 }
   1091 
   1092 int CProgramConfig_GetElementTable(const CProgramConfig *pPce,
   1093                                    MP4_ELEMENT_ID elList[],
   1094                                    const INT elListSize, UCHAR *pChMapIdx) {
   1095   int i, el = 0;
   1096 
   1097   FDK_ASSERT(elList != NULL);
   1098   FDK_ASSERT(pChMapIdx != NULL);
   1099   FDK_ASSERT(pPce != NULL);
   1100 
   1101   *pChMapIdx = 0;
   1102 
   1103   if ((elListSize <
   1104        pPce->NumFrontChannelElements + pPce->NumSideChannelElements +
   1105            pPce->NumBackChannelElements + pPce->NumLfeChannelElements) ||
   1106       (pPce->NumChannels == 0)) {
   1107     return 0;
   1108   }
   1109 
   1110   for (i = 0; i < pPce->NumFrontChannelElements; i += 1) {
   1111     elList[el++] = (pPce->FrontElementIsCpe[i]) ? ID_CPE : ID_SCE;
   1112   }
   1113 
   1114   for (i = 0; i < pPce->NumSideChannelElements; i += 1) {
   1115     elList[el++] = (pPce->SideElementIsCpe[i]) ? ID_CPE : ID_SCE;
   1116   }
   1117 
   1118   for (i = 0; i < pPce->NumBackChannelElements; i += 1) {
   1119     elList[el++] = (pPce->BackElementIsCpe[i]) ? ID_CPE : ID_SCE;
   1120   }
   1121 
   1122   for (i = 0; i < pPce->NumLfeChannelElements; i += 1) {
   1123     elList[el++] = ID_LFE;
   1124   }
   1125 
   1126   /* Find an corresponding channel configuration if possible */
   1127   switch (pPce->NumChannels) {
   1128     case 1:
   1129     case 2:
   1130       /* One and two channels have no alternatives. */
   1131       *pChMapIdx = pPce->NumChannels;
   1132       break;
   1133     case 3:
   1134     case 4:
   1135     case 5:
   1136     case 6: { /* Test if the number of channels can be used as channel config:
   1137                */
   1138       C_ALLOC_SCRATCH_START(tmpPce, CProgramConfig, 1);
   1139       /* Create a PCE for the config to test ... */
   1140       CProgramConfig_GetDefault(tmpPce, pPce->NumChannels);
   1141       /* ... and compare it with the given one. */
   1142       *pChMapIdx = (!(CProgramConfig_Compare(pPce, tmpPce) & 0xE))
   1143                        ? pPce->NumChannels
   1144                        : 0;
   1145       /* If compare result is 0 or 1 we can be sure that it is channel
   1146        * config 11. */
   1147       C_ALLOC_SCRATCH_END(tmpPce, CProgramConfig, 1);
   1148     } break;
   1149     case 7: {
   1150       C_ALLOC_SCRATCH_START(tmpPce, CProgramConfig, 1);
   1151       /* Create a PCE for the config to test ... */
   1152       CProgramConfig_GetDefault(tmpPce, 11);
   1153       /* ... and compare it with the given one. */
   1154       *pChMapIdx = (!(CProgramConfig_Compare(pPce, tmpPce) & 0xE)) ? 11 : 0;
   1155       /* If compare result is 0 or 1 we can be sure that it is channel
   1156        * config 11. */
   1157       C_ALLOC_SCRATCH_END(tmpPce, CProgramConfig, 1);
   1158     } break;
   1159     case 8: { /* Try the four possible 7.1ch configurations. One after the
   1160                  other. */
   1161       UCHAR testCfg[4] = {32, 14, 12, 7};
   1162       C_ALLOC_SCRATCH_START(tmpPce, CProgramConfig, 1);
   1163       for (i = 0; i < 4; i += 1) {
   1164         /* Create a PCE for the config to test ... */
   1165         CProgramConfig_GetDefault(tmpPce, testCfg[i]);
   1166         /* ... and compare it with the given one. */
   1167         if (!(CProgramConfig_Compare(pPce, tmpPce) & 0xE)) {
   1168           /* If the compare result is 0 or 1 than the two channel configurations
   1169            * match. */
   1170           /* Explicit mapping of 7.1 side channel configuration to 7.1 rear
   1171            * channel mapping. */
   1172           *pChMapIdx = (testCfg[i] == 32) ? 12 : testCfg[i];
   1173         }
   1174       }
   1175       C_ALLOC_SCRATCH_END(tmpPce, CProgramConfig, 1);
   1176     } break;
   1177     default:
   1178       /* The PCE does not match any predefined channel configuration. */
   1179       *pChMapIdx = 0;
   1180       break;
   1181   }
   1182 
   1183   return el;
   1184 }
   1185 
   1186 static AUDIO_OBJECT_TYPE getAOT(HANDLE_FDK_BITSTREAM bs) {
   1187   int tmp = 0;
   1188 
   1189   tmp = FDKreadBits(bs, 5);
   1190   if (tmp == AOT_ESCAPE) {
   1191     int tmp2 = FDKreadBits(bs, 6);
   1192     tmp = 32 + tmp2;
   1193   }
   1194 
   1195   return (AUDIO_OBJECT_TYPE)tmp;
   1196 }
   1197 
   1198 static INT getSampleRate(HANDLE_FDK_BITSTREAM bs, UCHAR *index, int nBits) {
   1199   INT sampleRate;
   1200   int idx;
   1201 
   1202   idx = FDKreadBits(bs, nBits);
   1203   if (idx == (1 << nBits) - 1) {
   1204     if (FDKgetValidBits(bs) < 24) {
   1205       return 0;
   1206     }
   1207     sampleRate = FDKreadBits(bs, 24);
   1208   } else {
   1209     sampleRate = SamplingRateTable[idx];
   1210   }
   1211 
   1212   *index = idx;
   1213 
   1214   return sampleRate;
   1215 }
   1216 
   1217 static TRANSPORTDEC_ERROR GaSpecificConfig_Parse(CSGaSpecificConfig *self,
   1218                                                  CSAudioSpecificConfig *asc,
   1219                                                  HANDLE_FDK_BITSTREAM bs,
   1220                                                  UINT ascStartAnchor) {
   1221   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
   1222 
   1223   self->m_frameLengthFlag = FDKreadBits(bs, 1);
   1224 
   1225   self->m_dependsOnCoreCoder = FDKreadBits(bs, 1);
   1226 
   1227   if (self->m_dependsOnCoreCoder) self->m_coreCoderDelay = FDKreadBits(bs, 14);
   1228 
   1229   self->m_extensionFlag = FDKreadBits(bs, 1);
   1230 
   1231   if (asc->m_channelConfiguration == 0) {
   1232     CProgramConfig_Read(&asc->m_progrConfigElement, bs, ascStartAnchor);
   1233   }
   1234 
   1235   if ((asc->m_aot == AOT_AAC_SCAL) || (asc->m_aot == AOT_ER_AAC_SCAL)) {
   1236     self->m_layer = FDKreadBits(bs, 3);
   1237   }
   1238 
   1239   if (self->m_extensionFlag) {
   1240     if (asc->m_aot == AOT_ER_BSAC) {
   1241       self->m_numOfSubFrame = FDKreadBits(bs, 5);
   1242       self->m_layerLength = FDKreadBits(bs, 11);
   1243     }
   1244 
   1245     if ((asc->m_aot == AOT_ER_AAC_LC) || (asc->m_aot == AOT_ER_AAC_LTP) ||
   1246         (asc->m_aot == AOT_ER_AAC_SCAL) || (asc->m_aot == AOT_ER_AAC_LD)) {
   1247       asc->m_vcb11Flag = FDKreadBits(bs, 1); /* aacSectionDataResilienceFlag */
   1248       asc->m_rvlcFlag =
   1249           FDKreadBits(bs, 1); /* aacScalefactorDataResilienceFlag */
   1250       asc->m_hcrFlag = FDKreadBits(bs, 1); /* aacSpectralDataResilienceFlag */
   1251     }
   1252 
   1253     self->m_extensionFlag3 = FDKreadBits(bs, 1);
   1254   }
   1255   return (ErrorStatus);
   1256 }
   1257 
   1258 static INT skipSbrHeader(HANDLE_FDK_BITSTREAM hBs, int isUsac) {
   1259   /* Dummy parse SbrDfltHeader() */
   1260   INT dflt_header_extra1, dflt_header_extra2, bitsToSkip = 0;
   1261 
   1262   if (!isUsac) {
   1263     bitsToSkip = 6;
   1264     FDKpushFor(hBs, 6); /* amp res 1, xover freq 3, reserved 2 */
   1265   }
   1266   bitsToSkip += 8;
   1267   FDKpushFor(hBs, 8); /* start / stop freq */
   1268   bitsToSkip += 2;
   1269   dflt_header_extra1 = FDKreadBit(hBs);
   1270   dflt_header_extra2 = FDKreadBit(hBs);
   1271   bitsToSkip += 5 * dflt_header_extra1 + 6 * dflt_header_extra2;
   1272   FDKpushFor(hBs, 5 * dflt_header_extra1 + 6 * dflt_header_extra2);
   1273 
   1274   return bitsToSkip;
   1275 }
   1276 
   1277 static INT ld_sbr_header(CSAudioSpecificConfig *asc, const INT dsFactor,
   1278                          HANDLE_FDK_BITSTREAM hBs, CSTpCallBacks *cb) {
   1279   const int channelConfiguration = asc->m_channelConfiguration;
   1280   int i = 0, j = 0;
   1281   INT error = 0;
   1282   MP4_ELEMENT_ID element = ID_NONE;
   1283 
   1284   /* check whether the channelConfiguration is defined in
   1285    * channel_configuration_array */
   1286   if (channelConfiguration < 0 ||
   1287       channelConfiguration > (INT)(sizeof(channel_configuration_array) /
   1288                                        sizeof(MP4_ELEMENT_ID **) -
   1289                                    1)) {
   1290     return TRANSPORTDEC_PARSE_ERROR;
   1291   }
   1292 
   1293   /* read elements of the passed channel_configuration until there is ID_NONE */
   1294   while ((element = channel_configuration_array[channelConfiguration][j]) !=
   1295          ID_NONE) {
   1296     if (element == ID_SCE || element == ID_CPE) {
   1297       error |= cb->cbSbr(
   1298           cb->cbSbrData, hBs, asc->m_samplingFrequency / dsFactor,
   1299           asc->m_extensionSamplingFrequency / dsFactor,
   1300           asc->m_samplesPerFrame / dsFactor, AOT_ER_AAC_ELD, element, i++, 0, 0,
   1301           asc->configMode, &asc->SbrConfigChanged, dsFactor);
   1302       if (error != TRANSPORTDEC_OK) {
   1303         goto bail;
   1304       }
   1305     }
   1306     j++;
   1307   }
   1308 bail:
   1309   return error;
   1310 }
   1311 
   1312 static TRANSPORTDEC_ERROR EldSpecificConfig_Parse(CSAudioSpecificConfig *asc,
   1313                                                   HANDLE_FDK_BITSTREAM hBs,
   1314                                                   CSTpCallBacks *cb) {
   1315   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
   1316   CSEldSpecificConfig *esc = &asc->m_sc.m_eldSpecificConfig;
   1317   ASC_ELD_EXT_TYPE eldExtType;
   1318   int eldExtLen, len, cnt, ldSbrLen = 0, eldExtLenSum, numSbrHeader = 0,
   1319                            sbrIndex;
   1320 
   1321   unsigned char downscale_fill_nibble;
   1322 
   1323   FDKmemclear(esc, sizeof(CSEldSpecificConfig));
   1324 
   1325   esc->m_frameLengthFlag = FDKreadBits(hBs, 1);
   1326   if (esc->m_frameLengthFlag) {
   1327     asc->m_samplesPerFrame = 480;
   1328   } else {
   1329     asc->m_samplesPerFrame = 512;
   1330   }
   1331 
   1332   asc->m_vcb11Flag = FDKreadBits(hBs, 1);
   1333   asc->m_rvlcFlag = FDKreadBits(hBs, 1);
   1334   asc->m_hcrFlag = FDKreadBits(hBs, 1);
   1335 
   1336   esc->m_sbrPresentFlag = FDKreadBits(hBs, 1);
   1337 
   1338   if (esc->m_sbrPresentFlag == 1) {
   1339     esc->m_sbrSamplingRate =
   1340         FDKreadBits(hBs, 1); /* 0: single rate, 1: dual rate */
   1341     esc->m_sbrCrcFlag = FDKreadBits(hBs, 1);
   1342 
   1343     asc->m_extensionSamplingFrequency = asc->m_samplingFrequency
   1344                                         << esc->m_sbrSamplingRate;
   1345 
   1346     if (cb->cbSbr != NULL) {
   1347       /* ELD reduced delay mode: LD-SBR initialization has to know the downscale
   1348          information. Postpone LD-SBR initialization and read ELD extension
   1349          information first. */
   1350       switch (asc->m_channelConfiguration) {
   1351         case 1:
   1352         case 2:
   1353           numSbrHeader = 1;
   1354           break;
   1355         case 3:
   1356           numSbrHeader = 2;
   1357           break;
   1358         case 4:
   1359         case 5:
   1360         case 6:
   1361           numSbrHeader = 3;
   1362           break;
   1363         case 7:
   1364         case 11:
   1365         case 12:
   1366         case 14:
   1367           numSbrHeader = 4;
   1368           break;
   1369         default:
   1370           numSbrHeader = 0;
   1371           break;
   1372       }
   1373       for (sbrIndex = 0; sbrIndex < numSbrHeader; sbrIndex++) {
   1374         ldSbrLen += skipSbrHeader(hBs, 0);
   1375       }
   1376     } else {
   1377       return TRANSPORTDEC_UNSUPPORTED_FORMAT;
   1378     }
   1379   }
   1380   esc->m_useLdQmfTimeAlign = 0;
   1381 
   1382   /* new ELD syntax */
   1383   eldExtLenSum = FDKgetValidBits(hBs);
   1384   esc->m_downscaledSamplingFrequency = asc->m_samplingFrequency;
   1385   /* parse ExtTypeConfigData */
   1386   while (
   1387       ((eldExtType = (ASC_ELD_EXT_TYPE)FDKreadBits(hBs, 4)) != ELDEXT_TERM) &&
   1388       ((INT)FDKgetValidBits(hBs) >= 0)) {
   1389     eldExtLen = len = FDKreadBits(hBs, 4);
   1390     if (len == 0xf) {
   1391       len = FDKreadBits(hBs, 8);
   1392       eldExtLen += len;
   1393 
   1394       if (len == 0xff) {
   1395         len = FDKreadBits(hBs, 16);
   1396         eldExtLen += len;
   1397       }
   1398     }
   1399 
   1400     switch (eldExtType) {
   1401       case ELDEXT_LDSAC:
   1402         esc->m_useLdQmfTimeAlign = 1;
   1403         if (cb->cbSsc != NULL) {
   1404           ErrorStatus = (TRANSPORTDEC_ERROR)cb->cbSsc(
   1405               cb->cbSscData, hBs, asc->m_aot, asc->m_extensionSamplingFrequency,
   1406               1,  /* stereoConfigIndex */
   1407               -1, /* nTimeSlots: read from bitstream */
   1408               eldExtLen, asc->configMode, &asc->SacConfigChanged);
   1409           if (ErrorStatus != TRANSPORTDEC_OK) {
   1410             return TRANSPORTDEC_PARSE_ERROR;
   1411           }
   1412           break;
   1413         }
   1414 
   1415       /* fall-through */
   1416       default:
   1417         for (cnt = 0; cnt < eldExtLen; cnt++) {
   1418           FDKreadBits(hBs, 8);
   1419         }
   1420         break;
   1421 
   1422       case ELDEXT_DOWNSCALEINFO:
   1423         UCHAR tmpDownscaleFreqIdx;
   1424         esc->m_downscaledSamplingFrequency =
   1425             getSampleRate(hBs, &tmpDownscaleFreqIdx, 4);
   1426         if (esc->m_downscaledSamplingFrequency == 0) {
   1427           return TRANSPORTDEC_PARSE_ERROR;
   1428         }
   1429         downscale_fill_nibble = FDKreadBits(hBs, 4);
   1430         if (downscale_fill_nibble != 0x0) {
   1431           return TRANSPORTDEC_PARSE_ERROR;
   1432         }
   1433         break;
   1434     }
   1435   }
   1436 
   1437   if ((INT)FDKgetValidBits(hBs) < 0) {
   1438     return TRANSPORTDEC_PARSE_ERROR;
   1439   }
   1440 
   1441   if (esc->m_sbrPresentFlag == 1 && numSbrHeader != 0) {
   1442     INT dsFactor = 1; /* Downscale factor must be 1 or even for SBR */
   1443     if (esc->m_downscaledSamplingFrequency != 0) {
   1444       if (asc->m_samplingFrequency % esc->m_downscaledSamplingFrequency != 0) {
   1445         return TRANSPORTDEC_UNSUPPORTED_FORMAT;
   1446       }
   1447       dsFactor = asc->m_samplingFrequency / esc->m_downscaledSamplingFrequency;
   1448       if (dsFactor != 1 && (dsFactor)&1) {
   1449         return TRANSPORTDEC_UNSUPPORTED_FORMAT; /* SBR needs an even downscale
   1450                                                    factor */
   1451       }
   1452       if (dsFactor != 1 && dsFactor != 2 && dsFactor != 4) {
   1453         dsFactor = 1; /* don't apply dsf for not yet supported even dsfs */
   1454       }
   1455       if ((INT)asc->m_samplesPerFrame % dsFactor != 0) {
   1456         return TRANSPORTDEC_UNSUPPORTED_FORMAT; /* frameSize/dsf must be an
   1457                                                    integer number */
   1458       }
   1459     }
   1460     eldExtLenSum = eldExtLenSum - FDKgetValidBits(hBs);
   1461     FDKpushBack(hBs, eldExtLenSum + ldSbrLen);
   1462     if (0 != ld_sbr_header(asc, dsFactor, hBs, cb)) {
   1463       return TRANSPORTDEC_PARSE_ERROR;
   1464     }
   1465     FDKpushFor(hBs, eldExtLenSum);
   1466   }
   1467   return (ErrorStatus);
   1468 }
   1469 
   1470 /*
   1471 Subroutine to store config in UCHAR buffer. Bit stream position does not change.
   1472 */
   1473 static UINT StoreConfigAsBitstream(
   1474     HANDLE_FDK_BITSTREAM hBs, const INT configSize_bits, /* If < 0 (> 0) config
   1475                                                             to read is before
   1476                                                             (after) current bit
   1477                                                             stream position. */
   1478     UCHAR *configTargetBuffer, const USHORT configTargetBufferSize_bytes) {
   1479   FDK_BITSTREAM usacConf;
   1480   UINT const nBits = fAbs(configSize_bits);
   1481   UINT j, tmp;
   1482 
   1483   if (nBits > 8 * (UINT)configTargetBufferSize_bytes) {
   1484     return 1;
   1485   }
   1486   FDKmemclear(configTargetBuffer, configTargetBufferSize_bytes);
   1487 
   1488   FDKinitBitStream(&usacConf, configTargetBuffer, configTargetBufferSize_bytes,
   1489                    nBits, BS_WRITER);
   1490   if (configSize_bits < 0) {
   1491     FDKpushBack(hBs, nBits);
   1492   }
   1493   for (j = nBits; j > 31; j -= 32) {
   1494     tmp = FDKreadBits(hBs, 32);
   1495     FDKwriteBits(&usacConf, tmp, 32);
   1496   }
   1497   if (j > 0) {
   1498     tmp = FDKreadBits(hBs, j);
   1499     FDKwriteBits(&usacConf, tmp, j);
   1500   }
   1501   FDKsyncCache(&usacConf);
   1502   if (configSize_bits > 0) {
   1503     FDKpushBack(hBs, nBits);
   1504   }
   1505 
   1506   return 0;
   1507 }
   1508 
   1509 /* maps coreSbrFrameLengthIndex to coreCoderFrameLength */
   1510 static const USHORT usacFrameLength[8] = {768, 1024, 2048, 2048, 4096, 0, 0, 0};
   1511 /* maps coreSbrFrameLengthIndex to sbrRatioIndex */
   1512 static const UCHAR sbrRatioIndex[8] = {0, 0, 2, 3, 1, 0, 0, 0};
   1513 
   1514 /*
   1515   subroutine for parsing extension element configuration:
   1516   UsacExtElementConfig() q.v. ISO/IEC FDIS 23003-3:2011(E) Table 14
   1517   rsv603daExtElementConfig() q.v. ISO/IEC DIS 23008-3 Table 13
   1518 */
   1519 static TRANSPORTDEC_ERROR extElementConfig(CSUsacExtElementConfig *extElement,
   1520                                            HANDLE_FDK_BITSTREAM hBs,
   1521                                            const CSTpCallBacks *cb,
   1522                                            const UCHAR numSignalsInGroup,
   1523                                            const UINT coreFrameLength,
   1524                                            const int subStreamIndex,
   1525                                            const AUDIO_OBJECT_TYPE aot) {
   1526   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
   1527 
   1528   USAC_EXT_ELEMENT_TYPE usacExtElementType =
   1529       (USAC_EXT_ELEMENT_TYPE)escapedValue(hBs, 4, 8, 16);
   1530 
   1531   /* recurve extension elements which are invalid for USAC */
   1532   if (aot == AOT_USAC) {
   1533     switch (usacExtElementType) {
   1534       case ID_EXT_ELE_FILL:
   1535       case ID_EXT_ELE_MPEGS:
   1536       case ID_EXT_ELE_SAOC:
   1537       case ID_EXT_ELE_AUDIOPREROLL:
   1538       case ID_EXT_ELE_UNI_DRC:
   1539         break;
   1540       default:
   1541         usacExtElementType = ID_EXT_ELE_UNKNOWN;
   1542         break;
   1543     }
   1544   }
   1545 
   1546   extElement->usacExtElementType = usacExtElementType;
   1547   int usacExtElementConfigLength = escapedValue(hBs, 4, 8, 16);
   1548   extElement->usacExtElementConfigLength = (USHORT)usacExtElementConfigLength;
   1549   INT bsAnchor;
   1550 
   1551   if (FDKreadBit(hBs)) /* usacExtElementDefaultLengthPresent */
   1552     extElement->usacExtElementDefaultLength = escapedValue(hBs, 8, 16, 0) + 1;
   1553   else
   1554     extElement->usacExtElementDefaultLength = 0;
   1555 
   1556   extElement->usacExtElementPayloadFrag = FDKreadBit(hBs);
   1557 
   1558   bsAnchor = (INT)FDKgetValidBits(hBs);
   1559 
   1560   switch (usacExtElementType) {
   1561     case ID_EXT_ELE_UNKNOWN:
   1562     case ID_EXT_ELE_FILL:
   1563       break;
   1564     case ID_EXT_ELE_AUDIOPREROLL:
   1565       /* No configuration element */
   1566       extElement->usacExtElementHasAudioPreRoll = 1;
   1567       break;
   1568     case ID_EXT_ELE_UNI_DRC: {
   1569       if (cb->cbUniDrc != NULL) {
   1570         ErrorStatus = (TRANSPORTDEC_ERROR)cb->cbUniDrc(
   1571             cb->cbUniDrcData, hBs, usacExtElementConfigLength,
   1572             0, /* uniDrcConfig */
   1573             subStreamIndex, 0, aot);
   1574         if (ErrorStatus != TRANSPORTDEC_OK) {
   1575           return ErrorStatus;
   1576         }
   1577       }
   1578     } break;
   1579     default:
   1580       break;
   1581   }
   1582 
   1583   /* Adjust bit stream position. This is required because of byte alignment and
   1584    * unhandled extensions. */
   1585   {
   1586     INT left_bits = (usacExtElementConfigLength << 3) -
   1587                     (bsAnchor - (INT)FDKgetValidBits(hBs));
   1588     if (left_bits >= 0) {
   1589       FDKpushFor(hBs, left_bits);
   1590     } else {
   1591       /* parsed too many bits */
   1592       ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
   1593     }
   1594   }
   1595 
   1596   return ErrorStatus;
   1597 }
   1598 
   1599 /*
   1600   subroutine for parsing the USAC / RSVD60 configuration extension:
   1601   UsacConfigExtension() q.v. ISO/IEC FDIS 23003-3:2011(E) Table 15
   1602   rsv603daConfigExtension() q.v. ISO/IEC DIS 23008-3 Table 14
   1603 */
   1604 static TRANSPORTDEC_ERROR configExtension(CSUsacConfig *usc,
   1605                                           HANDLE_FDK_BITSTREAM hBs,
   1606                                           const CSTpCallBacks *cb) {
   1607   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
   1608 
   1609   int numConfigExtensions;
   1610   CONFIG_EXT_ID usacConfigExtType;
   1611   int usacConfigExtLength;
   1612 
   1613   numConfigExtensions = (int)escapedValue(hBs, 2, 4, 8) + 1;
   1614   for (int confExtIdx = 0; confExtIdx < numConfigExtensions; confExtIdx++) {
   1615     INT nbits;
   1616     int loudnessInfoSetConfigExtensionPosition = FDKgetValidBits(hBs);
   1617     usacConfigExtType = (CONFIG_EXT_ID)escapedValue(hBs, 4, 8, 16);
   1618     usacConfigExtLength = (int)escapedValue(hBs, 4, 8, 16);
   1619 
   1620     /* Start bit position of config extension */
   1621     nbits = (INT)FDKgetValidBits(hBs);
   1622 
   1623     /* Return an error in case the bitbuffer fill level is too low. */
   1624     if (nbits < usacConfigExtLength * 8) {
   1625       return TRANSPORTDEC_PARSE_ERROR;
   1626     }
   1627 
   1628     switch (usacConfigExtType) {
   1629       case ID_CONFIG_EXT_FILL:
   1630         for (int i = 0; i < usacConfigExtLength; i++) {
   1631           if (FDKreadBits(hBs, 8) != 0xa5) {
   1632             return TRANSPORTDEC_PARSE_ERROR;
   1633           }
   1634         }
   1635         break;
   1636       case ID_CONFIG_EXT_LOUDNESS_INFO: {
   1637         if (cb->cbUniDrc != NULL) {
   1638           ErrorStatus = (TRANSPORTDEC_ERROR)cb->cbUniDrc(
   1639               cb->cbUniDrcData, hBs, usacConfigExtLength,
   1640               1, /* loudnessInfoSet */
   1641               0, loudnessInfoSetConfigExtensionPosition, AOT_USAC);
   1642           if (ErrorStatus != TRANSPORTDEC_OK) {
   1643             return ErrorStatus;
   1644           }
   1645         }
   1646       } break;
   1647       default:
   1648         break;
   1649     }
   1650 
   1651     /* Skip remaining bits. If too many bits were parsed, assume error. */
   1652     usacConfigExtLength =
   1653         8 * usacConfigExtLength - (nbits - (INT)FDKgetValidBits(hBs));
   1654     if (usacConfigExtLength < 0) {
   1655       return TRANSPORTDEC_PARSE_ERROR;
   1656     }
   1657     FDKpushFor(hBs, usacConfigExtLength);
   1658   }
   1659 
   1660   return ErrorStatus;
   1661 }
   1662 
   1663 /* This function unifies decoder config parsing of USAC and RSV60:
   1664    rsv603daDecoderConfig() ISO/IEC DIS 23008-3   Table 8
   1665    UsacDecoderConfig()     ISO/IEC FDIS 23003-3  Table 6
   1666   */
   1667 static TRANSPORTDEC_ERROR UsacRsv60DecoderConfig_Parse(
   1668     CSAudioSpecificConfig *asc, HANDLE_FDK_BITSTREAM hBs,
   1669     const CSTpCallBacks *cb) {
   1670   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
   1671   CSUsacConfig *usc = &asc->m_sc.m_usacConfig;
   1672   int i, numberOfElements;
   1673   int channelElementIdx =
   1674       0; /* index for elements which contain audio channels (sce, cpe, lfe) */
   1675   SC_CHANNEL_CONFIG sc_chan_config = {0, 0, 0, 0};
   1676 
   1677   numberOfElements = (int)escapedValue(hBs, 4, 8, 16) + 1;
   1678   usc->m_usacNumElements = numberOfElements;
   1679   if (numberOfElements > TP_USAC_MAX_ELEMENTS) {
   1680     return TRANSPORTDEC_UNSUPPORTED_FORMAT;
   1681   }
   1682   usc->m_nUsacChannels = 0;
   1683   usc->m_channelConfigurationIndex = asc->m_channelConfiguration;
   1684 
   1685   if (asc->m_aot == AOT_USAC) {
   1686     sc_chan_config = sc_chan_config_tab[usc->m_channelConfigurationIndex];
   1687 
   1688     if (sc_chan_config.nCh > (SCHAR)TP_USAC_MAX_SPEAKERS) {
   1689       return TRANSPORTDEC_PARSE_ERROR;
   1690     }
   1691   }
   1692 
   1693   for (i = 0; i < numberOfElements; i++) {
   1694     MP4_ELEMENT_ID usacElementType = (MP4_ELEMENT_ID)(
   1695         FDKreadBits(hBs, 2) | USAC_ID_BIT); /* set USAC_ID_BIT to map
   1696                                                usacElementType to
   1697                                                MP4_ELEMENT_ID enum */
   1698     usc->element[i].usacElementType = usacElementType;
   1699 
   1700     /* sanity check: update element counter */
   1701     if (asc->m_aot == AOT_USAC) {
   1702       switch (usacElementType) {
   1703         case ID_USAC_SCE:
   1704           sc_chan_config.nSCE--;
   1705           break;
   1706         case ID_USAC_CPE:
   1707           sc_chan_config.nCPE--;
   1708           break;
   1709         case ID_USAC_LFE:
   1710           sc_chan_config.nLFE--;
   1711           break;
   1712         default:
   1713           break;
   1714       }
   1715       if (usc->m_channelConfigurationIndex) {
   1716         /* sanity check: no element counter may be smaller zero */
   1717         if (sc_chan_config.nCPE < 0 || sc_chan_config.nSCE < 0 ||
   1718             sc_chan_config.nLFE < 0) {
   1719           return TRANSPORTDEC_PARSE_ERROR;
   1720         }
   1721       }
   1722     }
   1723 
   1724     switch (usacElementType) {
   1725       case ID_USAC_SCE:
   1726         /* UsacCoreConfig() ISO/IEC FDIS 23003-3  Table 10 */
   1727         if (FDKreadBit(hBs)) { /* tw_mdct */
   1728           return TRANSPORTDEC_UNSUPPORTED_FORMAT;
   1729         }
   1730         usc->element[i].m_noiseFilling = FDKreadBits(hBs, 1);
   1731         /* end of UsacCoreConfig() */
   1732         if (usc->m_sbrRatioIndex > 0) {
   1733           if (cb->cbSbr == NULL) {
   1734             return TRANSPORTDEC_UNKOWN_ERROR;
   1735           }
   1736           /* SbrConfig() ISO/IEC FDIS 23003-3  Table 11 */
   1737           usc->element[i].m_harmonicSBR = FDKreadBit(hBs);
   1738           usc->element[i].m_interTes = FDKreadBit(hBs);
   1739           usc->element[i].m_pvc = FDKreadBit(hBs);
   1740           if (cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency,
   1741                         asc->m_extensionSamplingFrequency,
   1742                         asc->m_samplesPerFrame, asc->m_aot, ID_SCE,
   1743                         channelElementIdx, usc->element[i].m_harmonicSBR,
   1744                         usc->element[i].m_stereoConfigIndex, asc->configMode,
   1745                         &asc->SbrConfigChanged, 1)) {
   1746             return TRANSPORTDEC_PARSE_ERROR;
   1747           }
   1748           /* end of SbrConfig() */
   1749         }
   1750         usc->m_nUsacChannels += 1;
   1751         channelElementIdx++;
   1752         break;
   1753 
   1754       case ID_USAC_CPE:
   1755         /* UsacCoreConfig() ISO/IEC FDIS 23003-3  Table 10 */
   1756         if (FDKreadBit(hBs)) { /* tw_mdct */
   1757           return TRANSPORTDEC_UNSUPPORTED_FORMAT;
   1758         }
   1759         usc->element[i].m_noiseFilling = FDKreadBits(hBs, 1);
   1760         /* end of UsacCoreConfig() */
   1761         if (usc->m_sbrRatioIndex > 0) {
   1762           if (cb->cbSbr == NULL) return TRANSPORTDEC_UNKOWN_ERROR;
   1763           /* SbrConfig() ISO/IEC FDIS 23003-3 */
   1764           usc->element[i].m_harmonicSBR = FDKreadBit(hBs);
   1765           usc->element[i].m_interTes = FDKreadBit(hBs);
   1766           usc->element[i].m_pvc = FDKreadBit(hBs);
   1767           {
   1768             INT bitsToSkip = skipSbrHeader(hBs, 1);
   1769             /* read stereoConfigIndex */
   1770             usc->element[i].m_stereoConfigIndex = FDKreadBits(hBs, 2);
   1771             /* rewind */
   1772             FDKpushBack(hBs, bitsToSkip + 2);
   1773           }
   1774           {
   1775             MP4_ELEMENT_ID el_type =
   1776                 (usc->element[i].m_stereoConfigIndex == 1 ||
   1777                  usc->element[i].m_stereoConfigIndex == 2)
   1778                     ? ID_SCE
   1779                     : ID_CPE;
   1780             if (cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency,
   1781                           asc->m_extensionSamplingFrequency,
   1782                           asc->m_samplesPerFrame, asc->m_aot, el_type,
   1783                           channelElementIdx, usc->element[i].m_harmonicSBR,
   1784                           usc->element[i].m_stereoConfigIndex, asc->configMode,
   1785                           &asc->SbrConfigChanged, 1)) {
   1786               return TRANSPORTDEC_PARSE_ERROR;
   1787             }
   1788           }
   1789           /* end of SbrConfig() */
   1790 
   1791           usc->element[i].m_stereoConfigIndex =
   1792               FDKreadBits(hBs, 2); /* Needed in RM5 syntax */
   1793 
   1794           if (usc->element[i].m_stereoConfigIndex > 0) {
   1795             if (cb->cbSsc != NULL) {
   1796               /* Mps212Config() ISO/IEC FDIS 23003-3 */
   1797               if (cb->cbSsc(cb->cbSscData, hBs, asc->m_aot,
   1798                             asc->m_extensionSamplingFrequency,
   1799                             usc->element[i].m_stereoConfigIndex,
   1800                             usc->m_coreSbrFrameLengthIndex,
   1801                             0, /* don't know the length */
   1802                             asc->configMode, &asc->SacConfigChanged)) {
   1803                 return TRANSPORTDEC_PARSE_ERROR;
   1804               }
   1805               /* end of Mps212Config() */
   1806             } else {
   1807               return TRANSPORTDEC_UNKOWN_ERROR;
   1808             }
   1809           }
   1810         } else {
   1811           usc->element[i].m_stereoConfigIndex = 0;
   1812         }
   1813         usc->m_nUsacChannels += 2;
   1814 
   1815         channelElementIdx++;
   1816         break;
   1817 
   1818       case ID_USAC_LFE:
   1819         usc->element[i].m_noiseFilling = 0;
   1820         usc->m_nUsacChannels += 1;
   1821         if (usc->m_sbrRatioIndex > 0) {
   1822           /* Use SBR for upsampling */
   1823           if (cb->cbSbr == NULL) return ErrorStatus = TRANSPORTDEC_UNKOWN_ERROR;
   1824           usc->element[i].m_harmonicSBR = (UCHAR)0;
   1825           usc->element[i].m_interTes = (UCHAR)0;
   1826           usc->element[i].m_pvc = (UCHAR)0;
   1827           if (cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency,
   1828                         asc->m_extensionSamplingFrequency,
   1829                         asc->m_samplesPerFrame, asc->m_aot, ID_LFE,
   1830                         channelElementIdx, usc->element[i].m_harmonicSBR,
   1831                         usc->element[i].m_stereoConfigIndex, asc->configMode,
   1832                         &asc->SbrConfigChanged, 1)) {
   1833             return ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
   1834           }
   1835         }
   1836         channelElementIdx++;
   1837         break;
   1838 
   1839       case ID_USAC_EXT:
   1840         ErrorStatus = extElementConfig(&usc->element[i].extElement, hBs, cb, 0,
   1841                                        asc->m_samplesPerFrame, 0, asc->m_aot);
   1842 
   1843         if (ErrorStatus) {
   1844           return ErrorStatus;
   1845         }
   1846         break;
   1847 
   1848       default:
   1849         /* non USAC-element encountered */
   1850         return TRANSPORTDEC_PARSE_ERROR;
   1851     }
   1852   }
   1853 
   1854   if (asc->m_aot == AOT_USAC) {
   1855     if (usc->m_channelConfigurationIndex) {
   1856       /* sanity check: all element counter must be zero */
   1857       if (sc_chan_config.nCPE | sc_chan_config.nSCE | sc_chan_config.nLFE) {
   1858         return TRANSPORTDEC_PARSE_ERROR;
   1859       }
   1860     } else {
   1861       /* sanity check: number of audio channels shall be equal to or smaller
   1862        * than the accumulated sum of all channels */
   1863       if ((INT)(-2 * sc_chan_config.nCPE - sc_chan_config.nSCE -
   1864                 sc_chan_config.nLFE) < (INT)usc->numAudioChannels) {
   1865         return TRANSPORTDEC_PARSE_ERROR;
   1866       }
   1867     }
   1868   }
   1869 
   1870   return ErrorStatus;
   1871 }
   1872 
   1873 /* Mapping of coreSbrFrameLengthIndex defined by Table 70 in ISO/IEC 23003-3 */
   1874 static TRANSPORTDEC_ERROR UsacConfig_SetCoreSbrFrameLengthIndex(
   1875     CSAudioSpecificConfig *asc, int coreSbrFrameLengthIndex) {
   1876   int sbrRatioIndex_val;
   1877 
   1878   if (coreSbrFrameLengthIndex > 4) {
   1879     return TRANSPORTDEC_PARSE_ERROR; /* reserved values */
   1880   }
   1881   asc->m_sc.m_usacConfig.m_coreSbrFrameLengthIndex = coreSbrFrameLengthIndex;
   1882   asc->m_samplesPerFrame = usacFrameLength[coreSbrFrameLengthIndex];
   1883   sbrRatioIndex_val = sbrRatioIndex[coreSbrFrameLengthIndex];
   1884   asc->m_sc.m_usacConfig.m_sbrRatioIndex = sbrRatioIndex_val;
   1885 
   1886   if (sbrRatioIndex_val > 0) {
   1887     asc->m_sbrPresentFlag = 1;
   1888     asc->m_extensionSamplingFrequency = asc->m_samplingFrequency;
   1889     asc->m_extensionSamplingFrequencyIndex = asc->m_samplingFrequencyIndex;
   1890     switch (sbrRatioIndex_val) {
   1891       case 1: /* sbrRatio = 4:1 */
   1892         asc->m_samplingFrequency >>= 2;
   1893         asc->m_samplesPerFrame >>= 2;
   1894         break;
   1895       case 2: /* sbrRatio = 8:3 */
   1896         asc->m_samplingFrequency = (asc->m_samplingFrequency * 3) / 8;
   1897         asc->m_samplesPerFrame = (asc->m_samplesPerFrame * 3) / 8;
   1898         break;
   1899       case 3: /* sbrRatio = 2:1 */
   1900         asc->m_samplingFrequency >>= 1;
   1901         asc->m_samplesPerFrame >>= 1;
   1902         break;
   1903       default:
   1904         return TRANSPORTDEC_PARSE_ERROR;
   1905     }
   1906     asc->m_samplingFrequencyIndex =
   1907         getSamplingRateIndex(asc->m_samplingFrequency, 4);
   1908   }
   1909 
   1910   return TRANSPORTDEC_OK;
   1911 }
   1912 
   1913 static TRANSPORTDEC_ERROR UsacConfig_Parse(CSAudioSpecificConfig *asc,
   1914                                            HANDLE_FDK_BITSTREAM hBs,
   1915                                            CSTpCallBacks *cb) {
   1916   int usacSamplingFrequency, channelConfigurationIndex, coreSbrFrameLengthIndex;
   1917   TRANSPORTDEC_ERROR err = TRANSPORTDEC_OK;
   1918 
   1919   /* Start bit position of usacConfig */
   1920   INT nbits = (INT)FDKgetValidBits(hBs);
   1921 
   1922   usacSamplingFrequency = getSampleRate(hBs, &asc->m_samplingFrequencyIndex, 5);
   1923   asc->m_samplingFrequency = (UINT)usacSamplingFrequency;
   1924 
   1925   coreSbrFrameLengthIndex = FDKreadBits(hBs, 3);
   1926   if (UsacConfig_SetCoreSbrFrameLengthIndex(asc, coreSbrFrameLengthIndex) !=
   1927       TRANSPORTDEC_OK) {
   1928     return TRANSPORTDEC_PARSE_ERROR;
   1929   }
   1930 
   1931   channelConfigurationIndex = FDKreadBits(hBs, 5);
   1932   if (channelConfigurationIndex > 2) {
   1933     return TRANSPORTDEC_PARSE_ERROR; /* only channelConfigurationIndex = [1,2]
   1934                                         are supported */
   1935   }
   1936 
   1937   if (channelConfigurationIndex == 0) {
   1938     return TRANSPORTDEC_PARSE_ERROR; /* only channelConfigurationIndex = [1,2]
   1939                                         are supported */
   1940   }
   1941   asc->m_channelConfiguration = channelConfigurationIndex;
   1942 
   1943   err = UsacRsv60DecoderConfig_Parse(asc, hBs, cb);
   1944   if (err != TRANSPORTDEC_OK) {
   1945     return err;
   1946   }
   1947 
   1948   if (FDKreadBits(hBs, 1)) { /* usacConfigExtensionPresent */
   1949     err = configExtension(&asc->m_sc.m_usacConfig, hBs, cb);
   1950     if (err != TRANSPORTDEC_OK) {
   1951       return err;
   1952     }
   1953   }
   1954 
   1955   /* sanity check whether number of channels signaled in UsacDecoderConfig()
   1956      matches the number of channels required by channelConfigurationIndex */
   1957   if ((channelConfigurationIndex > 0) &&
   1958       (sc_chan_config_tab[channelConfigurationIndex].nCh !=
   1959        asc->m_sc.m_usacConfig.m_nUsacChannels)) {
   1960     return TRANSPORTDEC_PARSE_ERROR;
   1961   }
   1962 
   1963   /* Copy UsacConfig() to asc->m_sc.m_usacConfig.UsacConfig[] buffer. */
   1964   INT configSize_bits = (INT)FDKgetValidBits(hBs) - nbits;
   1965   StoreConfigAsBitstream(hBs, configSize_bits,
   1966                          asc->m_sc.m_usacConfig.UsacConfig,
   1967                          TP_USAC_MAX_CONFIG_LEN);
   1968   asc->m_sc.m_usacConfig.UsacConfigBits = fAbs(configSize_bits);
   1969 
   1970   return err;
   1971 }
   1972 
   1973 static TRANSPORTDEC_ERROR AudioSpecificConfig_ExtensionParse(
   1974     CSAudioSpecificConfig *self, HANDLE_FDK_BITSTREAM bs, CSTpCallBacks *cb) {
   1975   TP_ASC_EXTENSION_ID lastAscExt, ascExtId = ASCEXT_UNKOWN;
   1976   INT bitsAvailable = (INT)FDKgetValidBits(bs);
   1977 
   1978   while (bitsAvailable >= 11) {
   1979     lastAscExt = ascExtId;
   1980     ascExtId = (TP_ASC_EXTENSION_ID)FDKreadBits(bs, 11);
   1981     bitsAvailable -= 11;
   1982 
   1983     switch (ascExtId) {
   1984       case ASCEXT_SBR: /* 0x2b7 */
   1985         if ((self->m_extensionAudioObjectType != AOT_SBR) &&
   1986             (bitsAvailable >= 5)) {
   1987           self->m_extensionAudioObjectType = getAOT(bs);
   1988 
   1989           if ((self->m_extensionAudioObjectType == AOT_SBR) ||
   1990               (self->m_extensionAudioObjectType ==
   1991                AOT_ER_BSAC)) { /* Get SBR extension configuration */
   1992             self->m_sbrPresentFlag = FDKreadBits(bs, 1);
   1993             if (self->m_aot == AOT_USAC && self->m_sbrPresentFlag > 0 &&
   1994                 self->m_sc.m_usacConfig.m_sbrRatioIndex == 0) {
   1995               return TRANSPORTDEC_PARSE_ERROR;
   1996             }
   1997 
   1998             if (self->m_sbrPresentFlag == 1) {
   1999               self->m_extensionSamplingFrequency = getSampleRate(
   2000                   bs, &self->m_extensionSamplingFrequencyIndex, 4);
   2001 
   2002               if ((INT)self->m_extensionSamplingFrequency <= 0) {
   2003                 return TRANSPORTDEC_PARSE_ERROR;
   2004               }
   2005             }
   2006             if (self->m_extensionAudioObjectType == AOT_ER_BSAC) {
   2007               self->m_extensionChannelConfiguration = FDKreadBits(bs, 4);
   2008             }
   2009           }
   2010           /* Update counter because of variable length fields (AOT and sampling
   2011            * rate) */
   2012           bitsAvailable = (INT)FDKgetValidBits(bs);
   2013         }
   2014         break;
   2015       case ASCEXT_PS: /* 0x548 */
   2016         if ((lastAscExt == ASCEXT_SBR) &&
   2017             (self->m_extensionAudioObjectType == AOT_SBR) &&
   2018             (bitsAvailable > 0)) { /* Get PS extension configuration */
   2019           self->m_psPresentFlag = FDKreadBits(bs, 1);
   2020           bitsAvailable -= 1;
   2021         }
   2022         break;
   2023       case ASCEXT_MPS: /* 0x76a */
   2024         if (self->m_extensionAudioObjectType == AOT_MPEGS) break;
   2025       case ASCEXT_LDMPS: /* 0x7cc */
   2026         if ((ascExtId == ASCEXT_LDMPS) &&
   2027             (self->m_extensionAudioObjectType == AOT_LD_MPEGS))
   2028           break;
   2029         if (bitsAvailable >= 1) {
   2030           bitsAvailable -= 1;
   2031           if (FDKreadBits(bs, 1)) { /* self->m_mpsPresentFlag */
   2032             int sscLen = FDKreadBits(bs, 8);
   2033             bitsAvailable -= 8;
   2034             if (sscLen == 0xFF) {
   2035               sscLen += FDKreadBits(bs, 16);
   2036               bitsAvailable -= 16;
   2037             }
   2038             FDKpushFor(bs, sscLen); /* Skip SSC to be able to read the next
   2039                                        extension if there is one. */
   2040 
   2041             bitsAvailable -= sscLen * 8;
   2042           }
   2043         }
   2044         break;
   2045       case ASCEXT_SAOC:
   2046         if ((ascExtId == ASCEXT_SAOC) &&
   2047             (self->m_extensionAudioObjectType == AOT_SAOC))
   2048           break;
   2049         if (FDKreadBits(bs, 1)) { /* saocPresent */
   2050           int saocscLen = FDKreadBits(bs, 8);
   2051           bitsAvailable -= 8;
   2052           if (saocscLen == 0xFF) {
   2053             saocscLen += FDKreadBits(bs, 16);
   2054             bitsAvailable -= 16;
   2055           }
   2056           FDKpushFor(bs, saocscLen);
   2057           bitsAvailable -= saocscLen * 8;
   2058         }
   2059         break;
   2060       default:
   2061         /* Just ignore anything. */
   2062         return TRANSPORTDEC_OK;
   2063     }
   2064   }
   2065 
   2066   return TRANSPORTDEC_OK;
   2067 }
   2068 
   2069 /*
   2070  * API Functions
   2071  */
   2072 
   2073 void AudioSpecificConfig_Init(CSAudioSpecificConfig *asc) {
   2074   FDKmemclear(asc, sizeof(CSAudioSpecificConfig));
   2075 
   2076   /* Init all values that should not be zero. */
   2077   asc->m_aot = AOT_NONE;
   2078   asc->m_samplingFrequencyIndex = 0xf;
   2079   asc->m_epConfig = -1;
   2080   asc->m_extensionAudioObjectType = AOT_NULL_OBJECT;
   2081   CProgramConfig_Init(&asc->m_progrConfigElement);
   2082 }
   2083 
   2084 TRANSPORTDEC_ERROR AudioSpecificConfig_Parse(
   2085     CSAudioSpecificConfig *self, HANDLE_FDK_BITSTREAM bs,
   2086     int fExplicitBackwardCompatible, CSTpCallBacks *cb, UCHAR configMode,
   2087     UCHAR configChanged, AUDIO_OBJECT_TYPE m_aot) {
   2088   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
   2089   UINT ascStartAnchor = FDKgetValidBits(bs);
   2090   int frameLengthFlag = -1;
   2091 
   2092   AudioSpecificConfig_Init(self);
   2093 
   2094   self->configMode = configMode;
   2095   self->AacConfigChanged = configChanged;
   2096   self->SbrConfigChanged = configChanged;
   2097   self->SacConfigChanged = configChanged;
   2098 
   2099   if (m_aot != AOT_NULL_OBJECT) {
   2100     self->m_aot = m_aot;
   2101   } else {
   2102     self->m_aot = getAOT(bs);
   2103     self->m_samplingFrequency =
   2104         getSampleRate(bs, &self->m_samplingFrequencyIndex, 4);
   2105     if (self->m_samplingFrequency <= 0) {
   2106       return TRANSPORTDEC_PARSE_ERROR;
   2107     }
   2108 
   2109     self->m_channelConfiguration = FDKreadBits(bs, 4);
   2110 
   2111     /* SBR extension ( explicit non-backwards compatible mode ) */
   2112     self->m_sbrPresentFlag = 0;
   2113     self->m_psPresentFlag = 0;
   2114 
   2115     if (self->m_aot == AOT_SBR || self->m_aot == AOT_PS) {
   2116       self->m_extensionAudioObjectType = AOT_SBR;
   2117 
   2118       self->m_sbrPresentFlag = 1;
   2119       if (self->m_aot == AOT_PS) {
   2120         self->m_psPresentFlag = 1;
   2121       }
   2122 
   2123       self->m_extensionSamplingFrequency =
   2124           getSampleRate(bs, &self->m_extensionSamplingFrequencyIndex, 4);
   2125       self->m_aot = getAOT(bs);
   2126 
   2127       switch (self->m_aot) {
   2128         case AOT_AAC_LC:
   2129           break;
   2130         case AOT_ER_BSAC:
   2131           break;
   2132         default:
   2133           return TRANSPORTDEC_UNSUPPORTED_FORMAT;
   2134       }
   2135 
   2136       if (self->m_aot == AOT_ER_BSAC) {
   2137         self->m_extensionChannelConfiguration = FDKreadBits(bs, 4);
   2138       }
   2139     } else {
   2140       self->m_extensionAudioObjectType = AOT_NULL_OBJECT;
   2141     }
   2142   }
   2143 
   2144   /* Parse whatever specific configs */
   2145   switch (self->m_aot) {
   2146     case AOT_AAC_LC:
   2147     case AOT_AAC_SCAL:
   2148     case AOT_ER_AAC_LC:
   2149     case AOT_ER_AAC_LD:
   2150     case AOT_ER_AAC_SCAL:
   2151     case AOT_ER_BSAC:
   2152       if ((ErrorStatus = GaSpecificConfig_Parse(&self->m_sc.m_gaSpecificConfig,
   2153                                                 self, bs, ascStartAnchor)) !=
   2154           TRANSPORTDEC_OK) {
   2155         return (ErrorStatus);
   2156       }
   2157       frameLengthFlag = self->m_sc.m_gaSpecificConfig.m_frameLengthFlag;
   2158       break;
   2159     case AOT_MPEGS:
   2160       if (cb->cbSsc != NULL) {
   2161         if (cb->cbSsc(cb->cbSscData, bs, self->m_aot, self->m_samplingFrequency,
   2162                       1, -1, /* nTimeSlots: read from bitstream */
   2163                       0,     /* don't know the length */
   2164                       self->configMode, &self->SacConfigChanged)) {
   2165           return TRANSPORTDEC_UNSUPPORTED_FORMAT;
   2166         }
   2167       } else {
   2168         return TRANSPORTDEC_UNSUPPORTED_FORMAT;
   2169       }
   2170       break;
   2171     case AOT_ER_AAC_ELD:
   2172       if ((ErrorStatus = EldSpecificConfig_Parse(self, bs, cb)) !=
   2173           TRANSPORTDEC_OK) {
   2174         return (ErrorStatus);
   2175       }
   2176       frameLengthFlag = self->m_sc.m_eldSpecificConfig.m_frameLengthFlag;
   2177       self->m_sbrPresentFlag = self->m_sc.m_eldSpecificConfig.m_sbrPresentFlag;
   2178       self->m_extensionSamplingFrequency =
   2179           (self->m_sc.m_eldSpecificConfig.m_sbrSamplingRate + 1) *
   2180           self->m_samplingFrequency;
   2181       break;
   2182     case AOT_USAC:
   2183       if ((ErrorStatus = UsacConfig_Parse(self, bs, cb)) != TRANSPORTDEC_OK) {
   2184         return (ErrorStatus);
   2185       }
   2186       break;
   2187 
   2188     default:
   2189       return TRANSPORTDEC_UNSUPPORTED_FORMAT;
   2190   }
   2191 
   2192   /* Frame length */
   2193   switch (self->m_aot) {
   2194     case AOT_AAC_LC:
   2195     case AOT_AAC_SCAL:
   2196     case AOT_ER_AAC_LC:
   2197     case AOT_ER_AAC_SCAL:
   2198     case AOT_ER_BSAC:
   2199       /*case AOT_USAC:*/
   2200       if (!frameLengthFlag)
   2201         self->m_samplesPerFrame = 1024;
   2202       else
   2203         self->m_samplesPerFrame = 960;
   2204       break;
   2205     case AOT_ER_AAC_LD:
   2206       if (!frameLengthFlag)
   2207         self->m_samplesPerFrame = 512;
   2208       else
   2209         self->m_samplesPerFrame = 480;
   2210       break;
   2211     default:
   2212       break;
   2213   }
   2214 
   2215   switch (self->m_aot) {
   2216     case AOT_ER_AAC_LC:
   2217     case AOT_ER_AAC_LD:
   2218     case AOT_ER_AAC_ELD:
   2219     case AOT_ER_AAC_SCAL:
   2220     case AOT_ER_CELP:
   2221     case AOT_ER_HVXC:
   2222     case AOT_ER_BSAC:
   2223       self->m_epConfig = FDKreadBits(bs, 2);
   2224 
   2225       if (self->m_epConfig > 1) {
   2226         return TRANSPORTDEC_UNSUPPORTED_FORMAT;  // EPCONFIG;
   2227       }
   2228       break;
   2229     default:
   2230       break;
   2231   }
   2232 
   2233   if (fExplicitBackwardCompatible &&
   2234       (self->m_aot == AOT_AAC_LC || self->m_aot == AOT_ER_AAC_LD ||
   2235        self->m_aot == AOT_ER_BSAC)) {
   2236     ErrorStatus = AudioSpecificConfig_ExtensionParse(self, bs, cb);
   2237   }
   2238 
   2239   /* Copy config() to asc->config[] buffer. */
   2240   if ((ErrorStatus == TRANSPORTDEC_OK) && (self->m_aot == AOT_USAC)) {
   2241     INT configSize_bits = (INT)FDKgetValidBits(bs) - (INT)ascStartAnchor;
   2242     StoreConfigAsBitstream(bs, configSize_bits, self->config,
   2243                            TP_USAC_MAX_CONFIG_LEN);
   2244     self->configBits = fAbs(configSize_bits);
   2245   }
   2246 
   2247   return (ErrorStatus);
   2248 }
   2249 
   2250 static TRANSPORTDEC_ERROR Drm_xHEAACDecoderConfig(
   2251     CSAudioSpecificConfig *asc, HANDLE_FDK_BITSTREAM hBs, int audioMode,
   2252     CSTpCallBacks *cb /* use cb == NULL to signal config check only mode */
   2253 ) {
   2254   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
   2255   CSUsacConfig *usc = &asc->m_sc.m_usacConfig;
   2256   int elemIdx = 0;
   2257 
   2258   usc->element[elemIdx].m_stereoConfigIndex = 0;
   2259 
   2260   usc->m_usacNumElements = 1; /* Currently all extension elements are skipped
   2261                                  -> only one SCE or CPE. */
   2262 
   2263   switch (audioMode) {
   2264     case 0: /* mono: ID_USAC_SCE */
   2265       usc->element[elemIdx].usacElementType = ID_USAC_SCE;
   2266       usc->m_nUsacChannels = 1;
   2267       usc->element[elemIdx].m_noiseFilling = FDKreadBits(hBs, 1);
   2268       if (usc->m_sbrRatioIndex > 0) {
   2269         if (cb == NULL) {
   2270           return ErrorStatus;
   2271         }
   2272         if (cb->cbSbr != NULL) {
   2273           usc->element[elemIdx].m_harmonicSBR = FDKreadBit(hBs);
   2274           usc->element[elemIdx].m_interTes = FDKreadBit(hBs);
   2275           usc->element[elemIdx].m_pvc = FDKreadBit(hBs);
   2276           if (cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency,
   2277                         asc->m_extensionSamplingFrequency,
   2278                         asc->m_samplesPerFrame, asc->m_aot, ID_SCE, elemIdx,
   2279                         usc->element[elemIdx].m_harmonicSBR,
   2280                         usc->element[elemIdx].m_stereoConfigIndex,
   2281                         asc->configMode, &asc->SbrConfigChanged, 1)) {
   2282             return ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
   2283           }
   2284         }
   2285       }
   2286       break;
   2287     case 2: /* stereo: ID_USAC_CPE */
   2288       usc->element[elemIdx].usacElementType = ID_USAC_CPE;
   2289       usc->m_nUsacChannels = 2;
   2290       usc->element[elemIdx].m_noiseFilling = FDKreadBits(hBs, 1);
   2291       if (usc->m_sbrRatioIndex > 0) {
   2292         usc->element[elemIdx].m_harmonicSBR = FDKreadBit(hBs);
   2293         usc->element[elemIdx].m_interTes = FDKreadBit(hBs);
   2294         usc->element[elemIdx].m_pvc = FDKreadBit(hBs);
   2295         {
   2296           INT bitsToSkip = skipSbrHeader(hBs, 1);
   2297           /* read stereoConfigIndex */
   2298           usc->element[elemIdx].m_stereoConfigIndex = FDKreadBits(hBs, 2);
   2299           /* rewind */
   2300           FDKpushBack(hBs, bitsToSkip + 2);
   2301         }
   2302         /*
   2303         The application of the following tools is mutually exclusive per audio
   2304         stream configuration (see clause 5.3.2, xHE-AAC codec configuration):
   2305         - MPS212 parametric stereo tool with residual coding
   2306         (stereoConfigIndex>1); and
   2307         - QMF based Harmonic Transposer (harmonicSBR==1).
   2308         */
   2309         if ((usc->element[elemIdx].m_stereoConfigIndex > 1) &&
   2310             usc->element[elemIdx].m_harmonicSBR) {
   2311           return ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
   2312         }
   2313         /*
   2314         The 4:1 sbrRatio (sbrRatioIndex==1 in [11]) may only be employed:
   2315         - in mono operation; or
   2316         - in stereo operation if parametric stereo (MPS212) without residual
   2317         coding is applied, i.e. if stereoConfigIndex==1 (see clause 5.3.2,
   2318         xHE-AAC codec configuration).
   2319         */
   2320         if ((usc->m_sbrRatioIndex == 1) &&
   2321             (usc->element[elemIdx].m_stereoConfigIndex != 1)) {
   2322           return ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
   2323         }
   2324         if (cb == NULL) {
   2325           return ErrorStatus;
   2326         }
   2327         {
   2328           MP4_ELEMENT_ID el_type =
   2329               (usc->element[elemIdx].m_stereoConfigIndex == 1 ||
   2330                usc->element[elemIdx].m_stereoConfigIndex == 2)
   2331                   ? ID_SCE
   2332                   : ID_CPE;
   2333           if (cb->cbSbr == NULL) return ErrorStatus = TRANSPORTDEC_UNKOWN_ERROR;
   2334           if (cb->cbSbr(cb->cbSbrData, hBs, asc->m_samplingFrequency,
   2335                         asc->m_extensionSamplingFrequency,
   2336                         asc->m_samplesPerFrame, asc->m_aot, el_type, elemIdx,
   2337                         usc->element[elemIdx].m_harmonicSBR,
   2338                         usc->element[elemIdx].m_stereoConfigIndex,
   2339                         asc->configMode, &asc->SbrConfigChanged, 1)) {
   2340             return ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
   2341           }
   2342         }
   2343         /*usc->element[elemIdx].m_stereoConfigIndex =*/FDKreadBits(hBs, 2);
   2344         if (usc->element[elemIdx].m_stereoConfigIndex > 0) {
   2345           if (cb->cbSsc != NULL) {
   2346             ErrorStatus = (TRANSPORTDEC_ERROR)cb->cbSsc(
   2347                 cb->cbSscData, hBs,
   2348                 AOT_DRM_USAC, /* syntax differs from MPEG Mps212Config() */
   2349                 asc->m_extensionSamplingFrequency,
   2350                 usc->element[elemIdx].m_stereoConfigIndex,
   2351                 usc->m_coreSbrFrameLengthIndex, 0, /* don't know the length */
   2352                 asc->configMode, &asc->SacConfigChanged);
   2353           } else {
   2354             /* ErrorStatus = TRANSPORTDEC_UNSUPPORTED_FORMAT; */
   2355           }
   2356         }
   2357       }
   2358       break;
   2359     default:
   2360       return TRANSPORTDEC_PARSE_ERROR;
   2361   }
   2362 
   2363   return ErrorStatus;
   2364 }
   2365 
   2366 TRANSPORTDEC_ERROR Drm_xHEAACStaticConfig(
   2367     CSAudioSpecificConfig *asc, HANDLE_FDK_BITSTREAM bs, int audioMode,
   2368     CSTpCallBacks *cb /* use cb == NULL to signal config check only mode */
   2369 ) {
   2370   int coreSbrFrameLengthIndexDrm = FDKreadBits(bs, 2);
   2371   if (UsacConfig_SetCoreSbrFrameLengthIndex(
   2372           asc, coreSbrFrameLengthIndexDrm + 1) != TRANSPORTDEC_OK) {
   2373     return TRANSPORTDEC_PARSE_ERROR;
   2374   }
   2375 
   2376   asc->m_channelConfiguration = (audioMode) ? 2 : 1;
   2377 
   2378   if (Drm_xHEAACDecoderConfig(asc, bs, audioMode, cb) != TRANSPORTDEC_OK) {
   2379     return TRANSPORTDEC_PARSE_ERROR;
   2380   }
   2381 
   2382   return TRANSPORTDEC_OK;
   2383 }
   2384 
   2385 /* Mapping of DRM audio sampling rate field to MPEG usacSamplingFrequencyIndex
   2386  */
   2387 const UCHAR mapSr2MPEGIdx[8] = {
   2388     0x1b, /*  9.6 kHz */
   2389     0x09, /* 12.0 kHz */
   2390     0x08, /* 16.0 kHz */
   2391     0x17, /* 19.2 kHz */
   2392     0x06, /* 24.0 kHz */
   2393     0x05, /* 32.0 kHz */
   2394     0x12, /* 38.4 kHz */
   2395     0x03  /* 48.0 kHz */
   2396 };
   2397 
   2398 TRANSPORTDEC_ERROR DrmRawSdcAudioConfig_Parse(
   2399     CSAudioSpecificConfig *self, HANDLE_FDK_BITSTREAM bs,
   2400     CSTpCallBacks *cb, /* use cb == NULL to signal config check only mode */
   2401     UCHAR configMode, UCHAR configChanged) {
   2402   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
   2403 
   2404   AudioSpecificConfig_Init(self);
   2405 
   2406   if ((INT)FDKgetValidBits(bs) < 16) {
   2407     ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
   2408     goto bail;
   2409   } else {
   2410     /* DRM - Audio information data entity - type 9
   2411        - Short Id            2 bits (not part of the config buffer)
   2412        - Stream Id           2 bits (not part of the config buffer)
   2413        - audio coding        2 bits
   2414        - SBR flag            1 bit
   2415        - audio mode          2 bits
   2416        - audio sampling rate 3 bits
   2417        - text flag           1 bit
   2418        - enhancement flag    1 bit
   2419        - coder field         5 bits
   2420        - rfa                 1 bit  */
   2421 
   2422     int audioCoding, audioMode, cSamplingFreq, coderField, sfIdx, sbrFlag;
   2423 
   2424     self->configMode = configMode;
   2425     self->AacConfigChanged = configChanged;
   2426     self->SbrConfigChanged = configChanged;
   2427     self->SacConfigChanged = configChanged;
   2428 
   2429     /* Read the SDC field */
   2430     audioCoding = FDKreadBits(bs, 2);
   2431     sbrFlag = FDKreadBits(bs, 1);
   2432     audioMode = FDKreadBits(bs, 2);
   2433     cSamplingFreq = FDKreadBits(bs, 3); /* audio sampling rate */
   2434 
   2435     FDKreadBits(bs, 2); /* Text and enhancement flag */
   2436     coderField = FDKreadBits(bs, 5);
   2437     FDKreadBits(bs, 1); /* rfa */
   2438 
   2439     /* Evaluate configuration and fill the ASC */
   2440     if (audioCoding == 3) {
   2441       sfIdx = (int)mapSr2MPEGIdx[cSamplingFreq];
   2442       sbrFlag = 0; /* rfa */
   2443     } else {
   2444       switch (cSamplingFreq) {
   2445         case 0: /*  8 kHz */
   2446           sfIdx = 11;
   2447           break;
   2448         case 1: /* 12 kHz */
   2449           sfIdx = 9;
   2450           break;
   2451         case 2: /* 16 kHz */
   2452           sfIdx = 8;
   2453           break;
   2454         case 3: /* 24 kHz */
   2455           sfIdx = 6;
   2456           break;
   2457         case 5: /* 48 kHz */
   2458           sfIdx = 3;
   2459           break;
   2460         case 4: /* reserved */
   2461         case 6: /* reserved */
   2462         case 7: /* reserved */
   2463         default:
   2464           ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
   2465           goto bail;
   2466       }
   2467     }
   2468 
   2469     self->m_samplingFrequencyIndex = sfIdx;
   2470     self->m_samplingFrequency = SamplingRateTable[sfIdx];
   2471 
   2472     if (sbrFlag) {
   2473       UINT i;
   2474       int tmp = -1;
   2475       self->m_sbrPresentFlag = 1;
   2476       self->m_extensionAudioObjectType = AOT_SBR;
   2477       self->m_extensionSamplingFrequency = self->m_samplingFrequency << 1;
   2478       for (i = 0;
   2479            i < (sizeof(SamplingRateTable) / sizeof(SamplingRateTable[0]));
   2480            i++) {
   2481         if (SamplingRateTable[i] == self->m_extensionSamplingFrequency) {
   2482           tmp = i;
   2483           break;
   2484         }
   2485       }
   2486       self->m_extensionSamplingFrequencyIndex = tmp;
   2487     }
   2488 
   2489     switch (audioCoding) {
   2490       case 0: /* AAC */
   2491         if ((coderField >> 2) && (audioMode != 1)) {
   2492           self->m_aot = AOT_DRM_SURROUND; /* Set pseudo AOT for Drm Surround */
   2493         } else {
   2494           self->m_aot = AOT_DRM_AAC; /* Set pseudo AOT for Drm AAC */
   2495         }
   2496         switch (audioMode) {
   2497           case 1: /* parametric stereo */
   2498             self->m_psPresentFlag = 1;
   2499           case 0: /* mono */
   2500             self->m_channelConfiguration = 1;
   2501             break;
   2502           case 2: /* stereo */
   2503             self->m_channelConfiguration = 2;
   2504             break;
   2505           default:
   2506             ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
   2507             goto bail;
   2508         }
   2509         self->m_vcb11Flag = 1;
   2510         self->m_hcrFlag = 1;
   2511         self->m_samplesPerFrame = 960;
   2512         self->m_epConfig = 1;
   2513         break;
   2514       case 1: /* CELP */
   2515         self->m_aot = AOT_ER_CELP;
   2516         self->m_channelConfiguration = 1;
   2517         break;
   2518       case 2: /* HVXC */
   2519         self->m_aot = AOT_ER_HVXC;
   2520         self->m_channelConfiguration = 1;
   2521         break;
   2522       case 3: /* xHE-AAC */
   2523       {
   2524         /* payload is MPEG conform -> no pseudo DRM AOT needed */
   2525         self->m_aot = AOT_USAC;
   2526       }
   2527         switch (audioMode) {
   2528           case 0: /* mono */
   2529           case 2: /* stereo */
   2530             /* codec specific config 8n bits */
   2531             ErrorStatus = Drm_xHEAACStaticConfig(self, bs, audioMode, cb);
   2532             break;
   2533           default:
   2534             ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
   2535             goto bail;
   2536         }
   2537         break;
   2538       default:
   2539         ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
   2540         self->m_aot = AOT_NONE;
   2541         break;
   2542     }
   2543 
   2544     if (self->m_psPresentFlag && !self->m_sbrPresentFlag) {
   2545       ErrorStatus = TRANSPORTDEC_PARSE_ERROR;
   2546       goto bail;
   2547     }
   2548   }
   2549 
   2550 bail:
   2551   return (ErrorStatus);
   2552 }
   2553