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 surround decoder library *************************
     96 
     97    Author(s):
     98 
     99    Description: SAC Decoder Library Interface
    100 
    101 *******************************************************************************/
    102 
    103 #include "sac_dec_lib.h"
    104 #include "sac_dec_interface.h"
    105 #include "sac_dec.h"
    106 #include "sac_bitdec.h"
    107 #include "FDK_matrixCalloc.h"
    108 
    109 #define MPS_DATA_BUFFER_SIZE (2048)
    110 
    111 /**
    112  * \brief MPEG Surround data indication.
    113  **/
    114 typedef enum {
    115   MPEGS_ANCTYPE_FRAME = 0, /*!< MPEG Surround frame, see ISO/IEC 23003-1 */
    116   MPEGS_ANCTYPE_HEADER_AND_FRAME = 1, /*!< MPEG Surround header and MPEG
    117                                          Surround frame, see ISO/IEC 23003-1 */
    118   MPEGS_ANCTYPE_RESERVED_1 = 2,       /*!< reserved, see ISO/IEC 23003-1 */
    119   MPEGS_ANCTYPE_RESERVED_2 = 3        /*!< reserved, see ISO/IEC 23003-1*/
    120 } MPEGS_ANCTYPE;
    121 
    122 /**
    123  * \brief MPEG Surround data segment indication.
    124  **/
    125 typedef enum {
    126   MPEGS_CONTINUE = 0, /*!< Indicates if data segment continues a data block. */
    127   MPEGS_STOP = 1,     /*!< Indicates if data segment ends a data block. */
    128   MPEGS_START = 2,    /*!< Indicates if data segment begins a data block. */
    129   MPEGS_START_STOP =
    130       3 /*!< Indicates if data segment begins and ends a data block. */
    131 } MPEGS_ANCSTARTSTOP;
    132 
    133 /**
    134  * \brief MPEG Surround synchronizaiton state.
    135  *
    136  *  CAUTION: Changing the enumeration values can break the sync mechanism
    137  *because it is based on comparing the state values.
    138  **/
    139 typedef enum {
    140   MPEGS_SYNC_LOST =
    141       0, /*!< Indicates lost sync because of current discontinuity. */
    142   MPEGS_SYNC_FOUND = 1,   /*!< Parsed a valid header and (re)intialization was
    143                              successfully completed. */
    144   MPEGS_SYNC_COMPLETE = 2 /*!< In sync and continuous. Found an independent
    145                              frame in addition to MPEGS_SYNC_FOUND.
    146                                Precondition: MPEGS_SYNC_FOUND. */
    147 } MPEGS_SYNCSTATE;
    148 
    149 /**
    150  * \brief MPEG Surround operation mode.
    151  **/
    152 typedef enum {
    153   MPEGS_OPMODE_EMM = 0,           /*!< Mode: Enhanced Matrix Mode (Blind) */
    154   MPEGS_OPMODE_MPS_PAYLOAD = 1,   /*!< Mode: Normal, Stereo or Binaural */
    155   MPEGS_OPMODE_NO_MPS_PAYLOAD = 2 /*!< Mode: no MPEG Surround payload */
    156 } MPEGS_OPMODE;
    157 
    158 /**
    159  * \brief MPEG Surround init flags.
    160  **/
    161 typedef enum {
    162   MPEGS_INIT_OK = 0x00000000, /*!< indicate correct initialization */
    163   MPEGS_INIT_ENFORCE_REINIT =
    164       0x00000001, /*!< indicate complete initialization */
    165 
    166   MPEGS_INIT_CHANGE_OUTPUT_MODE =
    167       0x00000010, /*!< indicate change of the output mode */
    168   MPEGS_INIT_CHANGE_PARTIALLY_COMPLEX =
    169       0x00000020, /*!< indicate change of low power/high quality */
    170   MPEGS_INIT_CHANGE_TIME_FREQ_INTERFACE =
    171       0x00000040, /*!< indicate change of qmf/time interface */
    172   MPEGS_INIT_CHANGE_HEADER = 0x00000080, /*!< indicate change of header */
    173 
    174   MPEGS_INIT_ERROR_PAYLOAD =
    175       0x00000100, /*!< indicate payload/ancType/ancStartStop error */
    176 
    177   MPEGS_INIT_BS_INTERRUPTION =
    178       0x00001000, /*!< indicate bitstream interruption  */
    179   MPEGS_INIT_CLEAR_HISTORY =
    180       0x00002000, /*!< indicate that all states shall be cleared */
    181 
    182   /* Re-initialization of submodules */
    183 
    184   MPEGS_INIT_CHANGE_CONCEAL_PARAMS = 0x00100000, /*!< indicate a change of at
    185                                                     least one error concealment
    186                                                     param */
    187 
    188   /* No re-initialization needed, currently not used */
    189   MPEGS_INIT_CHANGE_BYPASS_MODE =
    190       0x01000000, /*!< indicate change of bypass mode */
    191 
    192   /* Re-initialization needed, currently not used */
    193   MPEGS_INIT_ERROR_ANC_TYPE = 0x10000000, /*!< indicate ancType error*/
    194   MPEGS_INIT_ERROR_ANC_STARTSTOP =
    195       0x20000000 /*!< indicate ancStartStop error */
    196 } MPEGS_INIT_FLAGS;
    197 
    198 struct MpegSurroundDecoder {
    199   HANDLE_FDK_QMF_DOMAIN pQmfDomain;
    200   UCHAR mpsData[MPS_DATA_BUFFER_SIZE]; /* Buffer for MPS payload accross more
    201                                           than one segment */
    202   INT mpsDataBits;                     /* Amount of bits in mpsData */
    203   /* MPEG Surround decoder */
    204   SPATIAL_SPECIFIC_CONFIG spatialSpecificConfig[1]; /* SSC delay line which is
    205                                                        used during decoding */
    206   spatialDec *pSpatialDec;
    207   SPATIAL_SPECIFIC_CONFIG
    208   spatialSpecificConfigBackup; /* SSC used while parsing */
    209 
    210   /* Creation parameter */
    211   UCHAR mpegSurroundDecoderLevel;
    212   /* Run-time parameter */
    213   UCHAR mpegSurroundSscIsGlobalCfg; /* Flag telling that the SSC
    214                                        (::spatialSpecificConfig) is a
    215                                        out-of-band configuration. */
    216   UCHAR mpegSurroundUseTimeInterface;
    217 
    218   SPATIAL_BS_FRAME
    219   bsFrames[1];         /* Bitstream Structs that contain data read from the
    220                           SpatialFrame() bitstream element */
    221   BS_LL_STATE llState; /* Bit stream parser state memory */
    222   UCHAR bsFrameParse;  /* Current parse frame context index */
    223   UCHAR bsFrameDecode; /* Current decode/apply frame context index */
    224   UCHAR bsFrameDelay;  /* Amount of frames delay between parsing and processing.
    225                           Required i.e. for interpolation error concealment. */
    226 
    227   /* User prameters */
    228   SPATIALDEC_PARAM mpegSurroundUserParams;
    229 
    230   /* Internal flags */
    231   SPATIAL_DEC_UPMIX_TYPE upmixType;
    232   int initFlags[1];
    233   MPEGS_ANCSTARTSTOP ancStartStopPrev;
    234   MPEGS_SYNCSTATE fOnSync[1];
    235 
    236   /* Inital decoder configuration */
    237   SPATIAL_DEC_CONFIG decConfig;
    238 };
    239 
    240 static SACDEC_ERROR sscParseCheck(const SPATIAL_SPECIFIC_CONFIG *pSsc);
    241 
    242 /**
    243  * \brief Get the number of QMF bands from the sampling frequency (in Hz)
    244  **/
    245 static int mpegSurroundDecoder_GetNrOfQmfBands(
    246     const SPATIAL_SPECIFIC_CONFIG *pSsc, UINT sampleRate) {
    247   UINT samplingFrequency = sampleRate;
    248   int qmfBands = 64;
    249 
    250   if (pSsc != NULL) {
    251     switch (pSsc->coreCodec) {
    252       case AOT_USAC:
    253         if ((pSsc->stereoConfigIndex == 3)) {
    254           static const UCHAR mapIdx2QmfBands[3] = {24, 32, 16};
    255           FDK_ASSERT((pSsc->coreSbrFrameLengthIndex >= 2) &&
    256                      (pSsc->coreSbrFrameLengthIndex <= 4));
    257           qmfBands = mapIdx2QmfBands[pSsc->coreSbrFrameLengthIndex - 2];
    258         }
    259         return qmfBands;
    260       default:
    261         samplingFrequency = pSsc->samplingFreq;
    262         break;
    263     }
    264   }
    265 
    266   /* number of QMF bands depend on sampling frequency, see FDIS 23003-1:2006
    267    * Chapter 6.3.3 */
    268   if (samplingFrequency < 27713) {
    269     qmfBands = 32;
    270   }
    271   if (samplingFrequency > 55426) {
    272     qmfBands = 128;
    273   }
    274 
    275   return qmfBands;
    276 }
    277 
    278 /**
    279  * \brief Analyse init flags
    280  **/
    281 static int mpegSurroundDecoder_CalcInitFlags(SPATIAL_SPECIFIC_CONFIG *pSsc1,
    282                                              SPATIAL_SPECIFIC_CONFIG *pSsc2,
    283                                              int upmixTypeFlag,
    284                                              int binauralQualityFlag,
    285                                              int partiallyComplexFlag,
    286                                              int *ctrlFlags) {
    287   /* Analyse core coder */
    288   if (pSsc1->coreCodec != pSsc2->coreCodec) {
    289     *ctrlFlags |= MASK_MPEGS_INIT_ALL_STATES;
    290     *ctrlFlags |= MASK_MPEGS_INIT_ALL_PARAMS;
    291   } else {
    292     /* Analyse elements for initialization of space analysis qmf filterbank */
    293     if ((partiallyComplexFlag) || (pSsc1->treeConfig != pSsc2->treeConfig) ||
    294         (pSsc1->samplingFreq != pSsc2->samplingFreq)) {
    295       *ctrlFlags |= MPEGS_INIT_STATES_ANA_QMF_FILTER;
    296       *ctrlFlags |= MPEGS_INIT_STATES_ANA_HYB_FILTER;
    297     }
    298 
    299     /* Analyse elements for initialization of space synthesis qmf filterbank */
    300     if ((upmixTypeFlag) || (partiallyComplexFlag) ||
    301         (pSsc1->treeConfig != pSsc2->treeConfig) ||
    302         (pSsc1->samplingFreq != pSsc2->samplingFreq) ||
    303         (pSsc1->bsFixedGainDMX != pSsc2->bsFixedGainDMX)) {
    304       *ctrlFlags |= MPEGS_INIT_STATES_SYN_QMF_FILTER;
    305     }
    306 
    307     /* Analyse elements for initialization of decorrelator */
    308     if ((upmixTypeFlag) || (partiallyComplexFlag) ||
    309         (pSsc1->treeConfig != pSsc2->treeConfig) ||
    310         (pSsc1->samplingFreq != pSsc2->samplingFreq) ||
    311         (pSsc1->decorrConfig != pSsc2->decorrConfig)) {
    312       *ctrlFlags |= MPEGS_INIT_STATES_DECORRELATOR;
    313     }
    314 
    315     /* Analyse elements for initialization of m1 and m2 calculation */
    316     if ((upmixTypeFlag) || (binauralQualityFlag) ||
    317         (pSsc1->treeConfig != pSsc2->treeConfig) ||
    318         (pSsc1->samplingFreq != pSsc2->samplingFreq))
    319 
    320     {
    321       *ctrlFlags |= MPEGS_INIT_STATES_M1M2;
    322     }
    323 
    324     /* Analyse elements for initialization of GES */
    325     if ((upmixTypeFlag) || (pSsc1->treeConfig != pSsc2->treeConfig) ||
    326         (pSsc1->tempShapeConfig != pSsc2->tempShapeConfig)) {
    327       *ctrlFlags |= MPEGS_INIT_STATES_GES;
    328     }
    329 
    330     /* Analyse elements for initialization of FDreverb */
    331     if ((upmixTypeFlag) || (binauralQualityFlag) || (partiallyComplexFlag) ||
    332         (pSsc1->samplingFreq != pSsc2->samplingFreq) ||
    333         (pSsc1->nTimeSlots != pSsc2->nTimeSlots)) {
    334       *ctrlFlags |= MPEGS_INIT_STATES_REVERB;
    335     }
    336 
    337     /* Reset previous frame data whenever the config changes */
    338     if (*ctrlFlags & MPEGS_INIT_CONFIG) {
    339       *ctrlFlags |= MPEGS_INIT_STATES_PARAM;
    340     }
    341   }
    342 
    343   return MPS_OK;
    344 }
    345 
    346 /**
    347  * \brief Reset MPEG Surround status info
    348  **/
    349 static void updateMpegSurroundDecoderStatus(
    350     CMpegSurroundDecoder *pMpegSurroundDecoder, int initFlags,
    351     MPEGS_SYNCSTATE fOnSync, MPEGS_ANCSTARTSTOP ancStartStopPrev) {
    352   pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
    353       initFlags;
    354   if ((pMpegSurroundDecoder->mpegSurroundSscIsGlobalCfg != 0) &&
    355       (pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] >=
    356        MPEGS_SYNC_FOUND) &&
    357       (fOnSync < MPEGS_SYNC_FOUND)) {
    358     pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
    359         MPEGS_SYNC_FOUND;
    360   } else {
    361     pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
    362         fOnSync;
    363   }
    364   pMpegSurroundDecoder->ancStartStopPrev = ancStartStopPrev;
    365 }
    366 
    367 static SACDEC_ERROR mpegSurroundDecoder_Create(
    368     CMpegSurroundDecoder **pMpegSurroundDecoder, int stereoConfigIndex,
    369     HANDLE_FDK_QMF_DOMAIN pQmfDomain);
    370 
    371 SAC_INSTANCE_AVAIL
    372 mpegSurroundDecoder_IsFullMpegSurroundDecoderInstanceAvailable(
    373     CMpegSurroundDecoder *pMpegSurroundDecoder) {
    374   SAC_INSTANCE_AVAIL instanceAvailable = SAC_INSTANCE_NOT_FULL_AVAILABLE;
    375 
    376   if (pMpegSurroundDecoder->pSpatialDec != NULL) {
    377     instanceAvailable = SAC_INSTANCE_FULL_AVAILABLE;
    378   }
    379 
    380   return instanceAvailable;
    381 }
    382 
    383 SACDEC_ERROR mpegSurroundDecoder_Open(
    384     CMpegSurroundDecoder **pMpegSurroundDecoder, int stereoConfigIndex,
    385     HANDLE_FDK_QMF_DOMAIN pQmfDomain) {
    386   SACDEC_ERROR error;
    387 
    388   error = mpegSurroundDecoder_Create(pMpegSurroundDecoder, stereoConfigIndex,
    389                                      pQmfDomain);
    390 
    391   return error;
    392 }
    393 
    394 /**
    395  * \brief  Renamed function from getUpmixType to check_UParam_Build_DecConfig.
    396  *         This function checks if user params, decoder config and SSC are valid
    397  *         and if the decoder build can handle all this settings.
    398  *         The upmix type may be modified by this function.
    399  *         It is called in initMpegSurroundDecoder() after the ssc parse check,
    400  *         to have all checks in one place and to ensure these checks are always
    401  *         performed if config changes (inband and out-of-band).
    402  *
    403  * \param pUserParams  User data handle.
    404  * \param pDecConfig   decoder config handle.
    405  * \param pSsc         spatial specific config handle.
    406  * \param pUpmixType   upmix type which is set by this function
    407  *
    408  * \return  MPS_OK on sucess, and else on failure.
    409  */
    410 static SACDEC_ERROR check_UParam_Build_DecConfig(
    411     SPATIALDEC_PARAM const *pUserParams, SPATIAL_DEC_CONFIG const *pDecConfig,
    412     const SPATIAL_SPECIFIC_CONFIG *pSsc, SPATIAL_DEC_UPMIX_TYPE *pUpmixType) {
    413   int dmxChannels, outChannels, maxNumOutChannels;
    414 
    415   FDK_ASSERT(pUserParams != NULL);
    416   FDK_ASSERT(pUpmixType != NULL);
    417 
    418   /* checks if implementation can handle the Ssc */
    419 
    420   switch (pSsc->treeConfig) {
    421     case SPATIALDEC_MODE_RSVD7: /* 212 */
    422       dmxChannels = 1;
    423       outChannels = 2;
    424       break;
    425     default:
    426       return MPS_UNSUPPORTED_CONFIG;
    427   }
    428 
    429   /* ------------------------------------------- */
    430 
    431   /* Analyse pDecConfig params */
    432   switch (pDecConfig->binauralMode) {
    433     case BINAURAL_NONE:
    434       break;
    435     default:
    436       return MPS_UNSUPPORTED_CONFIG;
    437   }
    438 
    439   switch (pDecConfig->decoderMode) {
    440     case EXT_HQ_ONLY:
    441       break;
    442     default:
    443       return MPS_UNSUPPORTED_CONFIG;
    444   }
    445 
    446   switch (pDecConfig->maxNumOutputChannels) {
    447     case OUTPUT_CHANNELS_DEFAULT:
    448       /* No special restrictions -> Get the level restriction: */
    449       switch (pDecConfig->decoderLevel) {
    450         case DECODER_LEVEL_0:
    451           maxNumOutChannels = 2;
    452           break;
    453         default:
    454           return MPS_UNSUPPORTED_CONFIG;
    455       }
    456       break;
    457     case OUTPUT_CHANNELS_2_0:
    458       maxNumOutChannels = 2;
    459       break;
    460     default:
    461       return MPS_UNSUPPORTED_CONFIG;
    462   }
    463   /* ------------------------- */
    464 
    465   /* check if we can handle user params */
    466   if (pUserParams->blindEnable == 1) {
    467     return MPS_UNSUPPORTED_CONFIG;
    468   }
    469   {
    470     switch ((SAC_DEC_OUTPUT_MODE)pUserParams->outputMode) {
    471       case SACDEC_OUT_MODE_NORMAL:
    472         if (maxNumOutChannels >= outChannels) {
    473           *pUpmixType = UPMIX_TYPE_NORMAL;
    474         } else {
    475           { *pUpmixType = UPMIX_TYPE_BYPASS; }
    476         }
    477         break;
    478       case SACDEC_OUT_MODE_STEREO:
    479         if (dmxChannels == 1) {
    480           if (outChannels == 2) {
    481             *pUpmixType = UPMIX_TYPE_NORMAL;
    482           }
    483         } else {
    484           *pUpmixType = UPMIX_TYPE_BYPASS;
    485         }
    486         break;
    487       case SACDEC_OUT_MODE_6CHANNEL:
    488         if (outChannels > 6) {
    489           { *pUpmixType = UPMIX_TYPE_BYPASS; }
    490         } else {
    491           *pUpmixType = UPMIX_TYPE_NORMAL;
    492         }
    493         break;
    494       default:
    495         return MPS_UNSUPPORTED_CONFIG;
    496     }
    497   }
    498 
    499   return MPS_OK;
    500 }
    501 
    502 /**
    503  * \brief Init MPEG Surround decoder.
    504  **/
    505 static SACDEC_ERROR initMpegSurroundDecoder(
    506     CMpegSurroundDecoder *pMpegSurroundDecoder) {
    507   SACDEC_ERROR err;
    508   int initFlags = MPEGS_INIT_NONE, initFlagsDec;
    509   int upmixTypeCurr = pMpegSurroundDecoder->upmixType;
    510 
    511   FDK_ASSERT(pMpegSurroundDecoder != NULL);
    512 
    513   SPATIAL_SPECIFIC_CONFIG *const pSSCinput =
    514       &pMpegSurroundDecoder->spatialSpecificConfigBackup;
    515   SPATIAL_SPECIFIC_CONFIG *const pSSCtarget =
    516       &pMpegSurroundDecoder
    517            ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameDecode];
    518   initFlagsDec =
    519       pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode];
    520 
    521   if (pSSCinput->coreCodec != AOT_USAC) {
    522     /* here we check if we have a valid Ssc */
    523     err = sscParseCheck(pSSCinput);
    524     if (err != MPS_OK) goto bail;
    525   }
    526 
    527   /* here we check if Ssc matches build; also check UParams and DecConfig */
    528   /* if desired upmixType is changes                                      */
    529   err = check_UParam_Build_DecConfig(
    530       &pMpegSurroundDecoder->mpegSurroundUserParams,
    531       &pMpegSurroundDecoder->decConfig, pSSCinput,
    532       &pMpegSurroundDecoder->upmixType);
    533   if (err != MPS_OK) goto bail;
    534 
    535   /* init config */
    536   if (initFlagsDec & MPEGS_INIT_CHANGE_HEADER) {
    537     initFlags |= MPEGS_INIT_CONFIG;
    538   }
    539   /* init all states */
    540   if (initFlagsDec & MPEGS_INIT_CLEAR_HISTORY) {
    541     initFlags |= MASK_MPEGS_INIT_ALL_STATES;
    542   }
    543   if (initFlagsDec & MPEGS_INIT_CHANGE_CONCEAL_PARAMS) {
    544     initFlags |= MPEGS_INIT_PARAMS_ERROR_CONCEALMENT;
    545   }
    546 
    547   if (initFlagsDec & MPEGS_INIT_ENFORCE_REINIT) {
    548     /* init all states */
    549     initFlags |= MASK_MPEGS_INIT_ALL_STATES;
    550     initFlags |= MASK_MPEGS_INIT_ALL_PARAMS;
    551   } else {
    552     /* analyse states which have to be initialized */
    553     mpegSurroundDecoder_CalcInitFlags(
    554         pSSCtarget, pSSCinput,
    555         (upmixTypeCurr !=
    556          pMpegSurroundDecoder->upmixType), /* upmixType changed */
    557         0, (initFlagsDec & MPEGS_INIT_CHANGE_PARTIALLY_COMPLEX) ? 1 : 0,
    558         &initFlags);
    559   }
    560 
    561   {
    562     int nrOfQmfBands;
    563     FDKmemcpy(pSSCtarget, pSSCinput, sizeof(SPATIAL_SPECIFIC_CONFIG));
    564 
    565     nrOfQmfBands = mpegSurroundDecoder_GetNrOfQmfBands(
    566         pSSCtarget, pSSCtarget->samplingFreq);
    567     err = FDK_SpatialDecInit(
    568         pMpegSurroundDecoder->pSpatialDec,
    569         &pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameDecode],
    570         pSSCtarget, nrOfQmfBands, pMpegSurroundDecoder->upmixType,
    571         &pMpegSurroundDecoder->mpegSurroundUserParams, initFlags);
    572 
    573     if (err != MPS_OK) goto bail;
    574 
    575     /* Signal that we got a header and can go on decoding */
    576     if (err == MPS_OK) {
    577       initFlagsDec = MPEGS_INIT_OK;
    578       {
    579         pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
    580             MPEGS_SYNC_FOUND;
    581       }
    582     }
    583   }
    584 
    585 bail:
    586   pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] =
    587       initFlagsDec;
    588   return err;
    589 }
    590 
    591 /**
    592  * \brief Init MPEG Surround decoder.
    593  **/
    594 SACDEC_ERROR mpegSurroundDecoder_Init(
    595     CMpegSurroundDecoder *pMpegSurroundDecoder) {
    596   SACDEC_ERROR err = MPS_OK;
    597 
    598   if (pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode]) {
    599     err = initMpegSurroundDecoder(pMpegSurroundDecoder);
    600   }
    601   return err;
    602 }
    603 
    604 /**
    605  * \brief Open MPEG Surround decoder.
    606  **/
    607 static SACDEC_ERROR mpegSurroundDecoder_Create(
    608     CMpegSurroundDecoder **pMpegSurroundDecoder, int stereoConfigIndex,
    609     HANDLE_FDK_QMF_DOMAIN pQmfDomain) {
    610   SACDEC_ERROR err = MPS_OK;
    611   CMpegSurroundDecoder *sacDec = NULL;
    612   spatialDec *self = NULL;
    613 
    614   /* decoderLevel  decoderMode  maxNumOutputChannels  binauralMode */
    615   static const SPATIAL_DEC_CONFIG decConfig = {
    616       (CFG_LEVEL)(0), EXT_HQ_ONLY, OUTPUT_CHANNELS_DEFAULT, BINAURAL_NONE};
    617 
    618   if (*pMpegSurroundDecoder == NULL) {
    619     FDK_ALLOCATE_MEMORY_1D(*pMpegSurroundDecoder, 1, CMpegSurroundDecoder)
    620 
    621     for (int i = 0; i < 1; i++) {
    622       err = SpatialDecCreateBsFrame(&(*pMpegSurroundDecoder)->bsFrames[i],
    623                                     &(*pMpegSurroundDecoder)->llState);
    624       if (err != MPS_OK) {
    625         sacDec = *pMpegSurroundDecoder;
    626         goto bail;
    627       }
    628     }
    629     (*pMpegSurroundDecoder)->pQmfDomain = pQmfDomain;
    630 
    631     (*pMpegSurroundDecoder)->bsFrameDelay = 1;
    632     (*pMpegSurroundDecoder)->bsFrameParse = 0;
    633     (*pMpegSurroundDecoder)->bsFrameDecode = 0;
    634 
    635     return err;
    636   } else {
    637     sacDec = *pMpegSurroundDecoder;
    638   }
    639 
    640   if (sacDec->pSpatialDec == NULL) {
    641     if ((self = FDK_SpatialDecOpen(&decConfig, stereoConfigIndex)) == NULL) {
    642       err = MPS_OUTOFMEMORY;
    643       goto bail;
    644     }
    645   } else {
    646     self = sacDec->pSpatialDec;
    647   }
    648 
    649   self->pQmfDomain = sacDec->pQmfDomain;
    650 
    651   sacDec->pSpatialDec = self;
    652 
    653   /* default parameter set */
    654   sacDec->mpegSurroundUserParams.outputMode = SACDEC_OUT_MODE_NORMAL;
    655   sacDec->mpegSurroundUserParams.blindEnable = 0;
    656   sacDec->mpegSurroundUserParams.bypassMode = 0;
    657   sacDec->mpegSurroundUserParams.concealMethod = 1;
    658   sacDec->mpegSurroundUserParams.concealNumKeepFrames = 10;
    659   sacDec->mpegSurroundUserParams.concealFadeOutSlopeLength = 5;
    660   sacDec->mpegSurroundUserParams.concealFadeInSlopeLength = 5;
    661   sacDec->mpegSurroundUserParams.concealNumReleaseFrames = 3;
    662   sacDec->mpegSurroundSscIsGlobalCfg = 0;
    663   sacDec->mpegSurroundUseTimeInterface = 1;
    664   sacDec->mpegSurroundDecoderLevel = decConfig.decoderLevel;
    665 
    666   sacDec->upmixType = UPMIX_TYPE_NORMAL;
    667 
    668   /* signalize spatial decoder re-initalization */
    669   updateMpegSurroundDecoderStatus(sacDec, MPEGS_INIT_ENFORCE_REINIT,
    670                                   MPEGS_SYNC_LOST, MPEGS_STOP);
    671 
    672   /* return decoder instance */
    673   *pMpegSurroundDecoder = sacDec;
    674   sacDec->decConfig = decConfig;
    675 
    676   SpatialDecInitParserContext(sacDec->pSpatialDec);
    677 
    678   return err;
    679 
    680 bail:
    681   if (sacDec != NULL) {
    682     mpegSurroundDecoder_Close(sacDec);
    683   }
    684   *pMpegSurroundDecoder = NULL;
    685   if (err == MPS_OK) {
    686     return MPS_OUTOFMEMORY;
    687   } else {
    688     return err;
    689   }
    690 }
    691 
    692 /**
    693  * \brief Config MPEG Surround decoder.
    694  **/
    695 SACDEC_ERROR mpegSurroundDecoder_Config(
    696     CMpegSurroundDecoder *pMpegSurroundDecoder, HANDLE_FDK_BITSTREAM hBs,
    697     AUDIO_OBJECT_TYPE coreCodec, INT samplingRate, INT stereoConfigIndex,
    698     INT coreSbrFrameLengthIndex, INT configBytes, const UCHAR configMode,
    699     UCHAR *configChanged) {
    700   SACDEC_ERROR err = MPS_OK;
    701   SPATIAL_SPECIFIC_CONFIG spatialSpecificConfig;
    702 
    703   switch (coreCodec) {
    704     case AOT_DRM_USAC:
    705     case AOT_USAC:
    706       if (configMode == AC_CM_DET_CFG_CHANGE) {
    707         /* In config detection mode write spatial specific config parameters
    708          * into temporarily allocated structure */
    709         err = SpatialDecParseMps212Config(
    710             hBs, &spatialSpecificConfig, samplingRate, coreCodec,
    711             stereoConfigIndex, coreSbrFrameLengthIndex);
    712       } else {
    713         err = SpatialDecParseMps212Config(
    714             hBs, &pMpegSurroundDecoder->spatialSpecificConfigBackup,
    715             samplingRate, coreCodec, stereoConfigIndex,
    716             coreSbrFrameLengthIndex);
    717       }
    718       break;
    719     case AOT_ER_AAC_ELD:
    720     case AOT_ER_AAC_LD:
    721       if (configMode == AC_CM_DET_CFG_CHANGE) {
    722         /* In config detection mode write spatial specific config parameters
    723          * into temporarily allocated structure */
    724         err = SpatialDecParseSpecificConfig(hBs, &spatialSpecificConfig,
    725                                             configBytes, coreCodec);
    726       } else {
    727         err = SpatialDecParseSpecificConfig(
    728             hBs, &pMpegSurroundDecoder->spatialSpecificConfigBackup,
    729             configBytes, coreCodec);
    730       }
    731       break;
    732     default:
    733       err = MPS_UNSUPPORTED_FORMAT;
    734       break;
    735   }
    736 
    737   if (err != MPS_OK) {
    738     goto bail;
    739   }
    740 
    741   if (configMode & AC_CM_DET_CFG_CHANGE) {
    742     return err;
    743   }
    744 
    745   if (configMode & AC_CM_ALLOC_MEM) {
    746     if (*configChanged) {
    747       if ((err = mpegSurroundDecoder_Open(&pMpegSurroundDecoder,
    748                                           stereoConfigIndex, NULL))) {
    749         return err;
    750       }
    751     }
    752   }
    753 
    754   {
    755     SPATIAL_SPECIFIC_CONFIG *sscParse =
    756         &pMpegSurroundDecoder
    757              ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameParse];
    758 
    759     if (FDK_SpatialDecCompareSpatialSpecificConfigHeader(
    760             &pMpegSurroundDecoder->spatialSpecificConfigBackup, sscParse)) {
    761       pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameParse] |=
    762           MPEGS_INIT_CHANGE_HEADER;
    763       /* Error resilience code */
    764       if (pMpegSurroundDecoder->pSpatialDec == NULL) {
    765         err = MPS_NOTOK;
    766         goto bail;
    767       }
    768       SpatialDecInitParserContext(pMpegSurroundDecoder->pSpatialDec);
    769       pMpegSurroundDecoder->pSpatialDec->pConfigCurrent =
    770           &pMpegSurroundDecoder
    771                ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameDecode];
    772     }
    773   }
    774 
    775   if (err == MPS_OK) {
    776     /* We got a valid out-of-band configuration so label it accordingly. */
    777     pMpegSurroundDecoder->mpegSurroundSscIsGlobalCfg = 1;
    778   }
    779 
    780 bail:
    781   return err;
    782 }
    783 
    784 /**
    785  * \brief Determine MPEG Surround operation mode.
    786  **/
    787 static MPEGS_OPMODE mpegSurroundOperationMode(
    788     CMpegSurroundDecoder *pMpegSurroundDecoder, int mpsDataBits) {
    789   MPEGS_OPMODE mode;
    790 
    791   {
    792     if ((mpsDataBits > 0) &&
    793         (pMpegSurroundDecoder->mpegSurroundUserParams.blindEnable == 0)) {
    794       mode = MPEGS_OPMODE_MPS_PAYLOAD; /* Mode: Normal, Stereo or Binaural */
    795     } else {
    796       mode = MPEGS_OPMODE_NO_MPS_PAYLOAD; /* Mode: No MPEG Surround Payload */
    797       updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
    798                                       MPEGS_INIT_ERROR_PAYLOAD, MPEGS_SYNC_LOST,
    799                                       MPEGS_STOP);
    800     }
    801   }
    802 
    803   return (mode);
    804 }
    805 
    806 /**
    807  * \brief  Check ssc for parse errors.
    808  *         This one is called in initMpegSurroundDecoder()
    809  *         to ensure checking of inband and out-of-band mps configs.
    810  *         Only parse errors checked here! Check for valid config is done
    811  *         in check_UParam_Build_DecConfig()!
    812  *
    813  * \param pSsc         spatial specific config handle.
    814  *
    815  * \return  MPS_OK on sucess, and else on parse error.
    816  */
    817 static SACDEC_ERROR sscParseCheck(const SPATIAL_SPECIFIC_CONFIG *pSsc) {
    818   SACDEC_ERROR err = MPS_OK;
    819 
    820   if (pSsc->samplingFreq > 96000) return MPS_PARSE_ERROR;
    821   if (pSsc->samplingFreq < 8000) return MPS_PARSE_ERROR;
    822 
    823   switch (pSsc->freqRes) {
    824     case SPATIALDEC_FREQ_RES_28:
    825     case SPATIALDEC_FREQ_RES_20:
    826     case SPATIALDEC_FREQ_RES_14:
    827     case SPATIALDEC_FREQ_RES_10:
    828     case SPATIALDEC_FREQ_RES_23:
    829     case SPATIALDEC_FREQ_RES_15:
    830     case SPATIALDEC_FREQ_RES_12:
    831     case SPATIALDEC_FREQ_RES_9:
    832     case SPATIALDEC_FREQ_RES_7:
    833     case SPATIALDEC_FREQ_RES_5:
    834     case SPATIALDEC_FREQ_RES_4:
    835       break;
    836     case SPATIALDEC_FREQ_RES_40: /* 40 doesn't exist in ISO/IEC 23003-1 */
    837     default:
    838       return MPS_PARSE_ERROR;
    839   }
    840 
    841   if ((pSsc->treeConfig < 0) || (pSsc->treeConfig > 7)) {
    842     return MPS_PARSE_ERROR;
    843   }
    844 
    845   if ((pSsc->quantMode < 0) || (pSsc->quantMode > 2)) {
    846     return MPS_PARSE_ERROR;
    847   }
    848 
    849   if (pSsc->tempShapeConfig == 3) {
    850     return MPS_PARSE_ERROR;
    851   }
    852 
    853   if (pSsc->decorrConfig == 3) {
    854     return MPS_PARSE_ERROR;
    855   }
    856 
    857   /* now we are sure there were no parsing errors */
    858 
    859   return err;
    860 }
    861 
    862 /**
    863  * \brief  Check number of time slots
    864  *
    865  * Basically the mps frame length must be a multiple of the core coder frame
    866  * length. The below table shows all valid configurations in detail. See ISO/IEC
    867  * 23003-1: "Table 4A - Allowed values for bsFrameLength in the Baseline MPEG
    868  * Surround Profile"
    869  *
    870  * Downmix Coder        Downmix Code      Allowed values for bsFrameLength
    871  * Allowed frame sizes for normal, downsampled and upsampled MPS Framelength
    872  *                      (QMF Samples)
    873  *
    874  * AAC 1024                  16           15, 31, 47, 63 1024  2048  3072  4096
    875  * downsampled MPS           32           31, 63 1024  2048 upsampled MPS
    876  * 8            7, 15, 23, 31, 39, 47, 55, 63, 71    1024  2048  3072  4096
    877  * 5120  6144  7168  8192  9216
    878  *
    879  * AAC 960                   15           14, 29, 44, 59 960  1920  2880  3840
    880  * downsampled MPS           30           29, 59 960  1920 upsampled MPS
    881  * 7,5           14, 29, 44, 59                        1920  3840  5760  7680
    882  *
    883  * HE-AAC 1024/2048          32           31, 63 2048  4096 downsampled MPS
    884  * 64           63                                    2048 upsampled MPS
    885  * 16           15, 31, 47, 63                        2048  4096  6144  8192
    886  *
    887  * HE-AAC 960/1920           30           29, 59 1920  3840 downsampled MPS
    888  * 60           59                                    1920 upsampled MPS
    889  * 15           14, 29, 44, 59                        1920  3840  5760  7680
    890  *
    891  * BSAC                      16           15, 31, 47, 63 1024  2048  3072  4096
    892  * downsampled MPS           32           31, 63 1024  2048 upsampled MPS
    893  * 8            7, 15, 23, 31, 39, 47, 55, 63, 71    1024  2048  3072  4096
    894  * 5120  6144  7168  8192  9216
    895  *
    896  * BSAC with SBR             32           31, 63 2048  4096 downsampled MPS
    897  * 64           63                                    2048 upsampled MPS
    898  * 16           15, 31, 47, 63                        2048  4096  6144  8192
    899  *
    900  * AAC LD 512                 8            7, 15, 23, 31, 39, 47, 55, 63, 71
    901  * 512  1024  1536  2048  2560  3072  3584  4096  4608 downsampled MPS
    902  * 16           15, 31, 47, 63                         512  1024  1536  2048
    903  *
    904  * AAC ELD 512                8            7, 15, 23, 31, 39, 47, 55, 63, 71
    905  * 512  1024  1536  2048  2560  3072  3584  4096  4608 downsampled MPS
    906  * 16           15, 31, 47, 63                         512  1024  1536  2048
    907  *
    908  * AAC ELD with SBR 512/1024 16           15, 31, 47, 63 1024  2048  3072  4096
    909  * downsampled MPS           32           31, 63 1024  2048 upsampled MPS
    910  * 8            7, 15, 23, 31, 39, 47, 55, 63, 71    1024  2048  3072  4096
    911  * 5120  6144  7168  8192  9216
    912  *
    913  * MPEG1/2 Layer II          18           17, 35, 53, 71 1152  2304  3456  4608
    914  * downsampled MPS           36           35, 71 1152  2304
    915  *
    916  * MPEG1/2 Layer III         18           17, 35, 53, 71 1152  2304  3456  4608
    917  * downsampled MPS           36           35, 71 1152  2304
    918  *
    919  * \param frameLength
    920  * \param qmfBands
    921  * \param timeSlots
    922  *
    923  * \return  error code
    924  */
    925 SACDEC_ERROR checkTimeSlots(int frameLength, int qmfBands, int timeSlots) {
    926   int len;
    927   int maxFrameLength;
    928 
    929   if (qmfBands == 64) {
    930     /* normal MPEG Surround */
    931     switch (frameLength) {
    932       case 960:
    933       case 1920:
    934         maxFrameLength = 3840;
    935         break;
    936       case 1024:
    937       case 2048:
    938         maxFrameLength = 4096;
    939         break;
    940       case 512:
    941       case 1152:
    942         maxFrameLength = 4608;
    943         break;
    944       default:
    945         return MPS_PARSE_ERROR;
    946     }
    947   } else if (qmfBands == 32) {
    948     /* downsampled MPEG Surround */
    949     switch (frameLength) {
    950       case 960:
    951       case 1920:
    952         maxFrameLength = 1920;
    953         break;
    954       case 512:
    955       case 1024:
    956       case 2048:
    957         maxFrameLength = 2048;
    958         break;
    959       case 1152:
    960         maxFrameLength = 2304;
    961         break;
    962       default:
    963         return MPS_PARSE_ERROR;
    964     }
    965   } else if (qmfBands == 128) {
    966     /* upsampled MPEG Surround */
    967     switch (frameLength) {
    968       case 1920:
    969         maxFrameLength = 7680;
    970         break;
    971       case 1024:
    972         maxFrameLength = 9216;
    973         break;
    974       case 2048:
    975         maxFrameLength = 8192;
    976         break;
    977       case 512:
    978       case 960:
    979       case 1152:
    980       /* no break, no support for upsampled MPEG Surround */
    981       default:
    982         return MPS_PARSE_ERROR;
    983     }
    984   } else {
    985     return MPS_PARSE_ERROR;
    986   }
    987 
    988   len = frameLength;
    989 
    990   while (len <= maxFrameLength) {
    991     if (len == timeSlots * qmfBands) {
    992       return MPS_OK;
    993     }
    994     len += frameLength;
    995   }
    996   return MPS_PARSE_ERROR;
    997 }
    998 
    999 /**
   1000  * \brief  Check ssc for consistency (e.g. bit errors could cause trouble)
   1001  *         First of currently two ssc-checks.
   1002  *         This (old) one is called in mpegSurroundDecoder_Apply()
   1003  *         only if inband mps config is contained in stream.
   1004  *
   1005  *         New ssc check is split in two functions sscParseCheck() and
   1006  * check_UParam_Build_DecConfig(). sscParseCheck() checks only for correct
   1007  * parsing. check_UParam_Build_DecConfig() is used to check if we have a
   1008  * valid config. Both are called in initMpegSurroundDecoder() to ensure
   1009  * checking of inband and out-of-band mps configs.
   1010  *
   1011  *         If this function can be integrated into the new functions.
   1012  *         We can remove this one.
   1013  *
   1014  * \param pSsc         spatial specific config handle.
   1015  * \param frameLength
   1016  * \param sampleRate
   1017  *
   1018  * \return  MPS_OK on sucess, and else on failure.
   1019  */
   1020 static SACDEC_ERROR sscCheckInBand(SPATIAL_SPECIFIC_CONFIG *pSsc,
   1021                                    int frameLength, int sampleRate) {
   1022   SACDEC_ERROR err = MPS_OK;
   1023   int qmfBands;
   1024 
   1025   FDK_ASSERT(pSsc != NULL);
   1026 
   1027   /* core fs and mps fs must match */
   1028   if (pSsc->samplingFreq != sampleRate) {
   1029     err = MPS_PARSE_ERROR /* MPEGSDEC_SSC_PARSE_ERROR */;
   1030   }
   1031 
   1032   qmfBands = mpegSurroundDecoder_GetNrOfQmfBands(pSsc, pSsc->samplingFreq);
   1033 
   1034   if (checkTimeSlots(frameLength, qmfBands, pSsc->nTimeSlots) != MPS_OK) {
   1035     err = MPS_PARSE_ERROR;
   1036   }
   1037 
   1038   return err;
   1039 }
   1040 
   1041 SACDEC_ERROR
   1042 mpegSurroundDecoder_ConfigureQmfDomain(
   1043     CMpegSurroundDecoder *pMpegSurroundDecoder,
   1044     SAC_INPUT_CONFIG sac_dec_interface, UINT coreSamplingRate,
   1045     AUDIO_OBJECT_TYPE coreCodec) {
   1046   SACDEC_ERROR err = MPS_OK;
   1047   FDK_QMF_DOMAIN_GC *pGC = NULL;
   1048 
   1049   if (pMpegSurroundDecoder == NULL) {
   1050     return MPS_INVALID_HANDLE;
   1051   }
   1052 
   1053   FDK_ASSERT(pMpegSurroundDecoder->pSpatialDec);
   1054 
   1055   pGC = &pMpegSurroundDecoder->pQmfDomain->globalConf;
   1056   if (pMpegSurroundDecoder->mpegSurroundSscIsGlobalCfg) {
   1057     SPATIAL_SPECIFIC_CONFIG *pSSC =
   1058         &pMpegSurroundDecoder->spatialSpecificConfigBackup;
   1059     if (sac_dec_interface == SAC_INTERFACE_TIME) {
   1060       /* For SAC_INTERFACE_QMF these parameters are set by SBR. */
   1061       pGC->nBandsAnalysis_requested = mpegSurroundDecoder_GetNrOfQmfBands(
   1062           pSSC, coreSamplingRate); /* coreSamplingRate == outputSamplingRate for
   1063                                       SAC_INTERFACE_TIME */
   1064       pGC->nBandsSynthesis_requested = pGC->nBandsAnalysis_requested;
   1065       pGC->nInputChannels_requested =
   1066           fMax((UINT)pSSC->nInputChannels, (UINT)pGC->nInputChannels_requested);
   1067     }
   1068     pGC->nOutputChannels_requested =
   1069         fMax((UINT)pSSC->nOutputChannels, (UINT)pGC->nOutputChannels_requested);
   1070   } else {
   1071     if (sac_dec_interface == SAC_INTERFACE_TIME) {
   1072       /* For SAC_INTERFACE_QMF these parameters are set by SBR. */
   1073       pGC->nBandsAnalysis_requested = mpegSurroundDecoder_GetNrOfQmfBands(
   1074           NULL, coreSamplingRate); /* coreSamplingRate == outputSamplingRate for
   1075                                       SAC_INTERFACE_TIME */
   1076       pGC->nBandsSynthesis_requested = pGC->nBandsAnalysis_requested;
   1077       pGC->nInputChannels_requested =
   1078           pMpegSurroundDecoder->pSpatialDec->createParams.maxNumInputChannels;
   1079     }
   1080     pGC->nOutputChannels_requested =
   1081         pMpegSurroundDecoder->pSpatialDec->createParams.maxNumOutputChannels;
   1082   }
   1083   pGC->nQmfProcBands_requested = 64;
   1084   pGC->nQmfProcChannels_requested =
   1085       fMin((INT)pGC->nInputChannels_requested,
   1086            pMpegSurroundDecoder->pSpatialDec->createParams.maxNumInputChannels);
   1087 
   1088   if (coreCodec == AOT_ER_AAC_ELD) {
   1089     pGC->flags_requested |= QMF_FLAG_MPSLDFB;
   1090   }
   1091 
   1092   return err;
   1093 }
   1094 
   1095 /**
   1096  * \brief Decode MPEG Surround frame.
   1097  **/
   1098 int mpegSurroundDecoder_ParseNoHeader(
   1099     CMpegSurroundDecoder *pMpegSurroundDecoder, HANDLE_FDK_BITSTREAM hBs,
   1100     int *pMpsDataBits, int fGlobalIndependencyFlag) {
   1101   SACDEC_ERROR err = MPS_OK;
   1102   SPATIAL_SPECIFIC_CONFIG *sscParse;
   1103   int bitsAvail, numSacBits;
   1104 
   1105   if (pMpegSurroundDecoder == NULL || hBs == NULL) {
   1106     return MPS_INVALID_HANDLE;
   1107   }
   1108 
   1109   sscParse = &pMpegSurroundDecoder
   1110                   ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameParse];
   1111 
   1112   bitsAvail = FDKgetValidBits(hBs);
   1113 
   1114   /* First spatial specific config is parsed into spatialSpecificConfigBackup,
   1115    * second spatialSpecificConfigBackup is copied into
   1116    * spatialSpecificConfig[bsFrameDecode] */
   1117   if (pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameParse]) {
   1118     FDKmemcpy(sscParse, &pMpegSurroundDecoder->spatialSpecificConfigBackup,
   1119               sizeof(SPATIAL_SPECIFIC_CONFIG));
   1120     pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameParse] =
   1121         MPEGS_SYNC_FOUND;
   1122   }
   1123 
   1124   if (bitsAvail <= 0) {
   1125     err = MPS_PARSE_ERROR;
   1126   } else {
   1127     err = SpatialDecParseFrameData(
   1128         pMpegSurroundDecoder->pSpatialDec,
   1129         &pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse],
   1130         hBs, sscParse, (UPMIXTYPE)pMpegSurroundDecoder->upmixType,
   1131         fGlobalIndependencyFlag);
   1132     if (err == MPS_OK) {
   1133       pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse]
   1134           .newBsData = 1;
   1135     }
   1136   }
   1137 
   1138   numSacBits = bitsAvail - (INT)FDKgetValidBits(hBs);
   1139 
   1140   if (numSacBits > bitsAvail) {
   1141     pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse]
   1142         .newBsData = 0;
   1143     err = MPS_PARSE_ERROR;
   1144   }
   1145 
   1146   *pMpsDataBits -= numSacBits;
   1147 
   1148   return err;
   1149 }
   1150 
   1151 /**
   1152  * \brief Check, if ancType is valid.
   1153  **/
   1154 static int isValidAncType(CMpegSurroundDecoder *pMpegSurroundDecoder,
   1155                           int ancType) {
   1156   int ret = 1;
   1157 
   1158   if ((ancType != MPEGS_ANCTYPE_HEADER_AND_FRAME) &&
   1159       (ancType != MPEGS_ANCTYPE_FRAME)) {
   1160     ret = 0;
   1161   }
   1162 
   1163   if (ret == 0) {
   1164     updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
   1165                                     MPEGS_INIT_ERROR_PAYLOAD, MPEGS_SYNC_LOST,
   1166                                     MPEGS_STOP);
   1167   }
   1168 
   1169   return (ret);
   1170 }
   1171 
   1172 /**
   1173  * \brief Check, if ancStartStop is valid.
   1174  **/
   1175 static int isValidAncStartStop(CMpegSurroundDecoder *pMpegSurroundDecoder,
   1176                                int ancStartStop) {
   1177   int ret = 1;
   1178 
   1179   switch (ancStartStop) {
   1180     case MPEGS_START:
   1181       /* Sequence start - start and continue - start not allowed */
   1182       if ((pMpegSurroundDecoder->ancStartStopPrev == MPEGS_START) ||
   1183           (pMpegSurroundDecoder->ancStartStopPrev == MPEGS_CONTINUE)) {
   1184         ret = 0;
   1185       }
   1186       break;
   1187 
   1188     case MPEGS_STOP:
   1189       /* MPS payload of the previous frame must be valid if current type is stop
   1190          Sequence startstop - stop and stop - stop not allowed
   1191          Sequence startstop - continue and stop - continue are allowed */
   1192       if ((pMpegSurroundDecoder->ancStartStopPrev == MPEGS_STOP) ||
   1193           (pMpegSurroundDecoder->ancStartStopPrev == MPEGS_START_STOP)) {
   1194         ret = 0;
   1195       }
   1196       break;
   1197 
   1198     case MPEGS_CONTINUE:
   1199     case MPEGS_START_STOP:
   1200       /* No error detection possible for this states */
   1201       break;
   1202   }
   1203 
   1204   if (ret == 0) {
   1205     updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
   1206                                     MPEGS_INIT_ERROR_PAYLOAD, MPEGS_SYNC_LOST,
   1207                                     MPEGS_STOP);
   1208   } else {
   1209     pMpegSurroundDecoder->ancStartStopPrev = (MPEGS_ANCSTARTSTOP)ancStartStop;
   1210   }
   1211 
   1212   return (ret);
   1213 }
   1214 
   1215 int mpegSurroundDecoder_Parse(CMpegSurroundDecoder *pMpegSurroundDecoder,
   1216                               HANDLE_FDK_BITSTREAM hBs, int *pMpsDataBits,
   1217                               AUDIO_OBJECT_TYPE coreCodec, int sampleRate,
   1218                               int frameSize, int fGlobalIndependencyFlag) {
   1219   SACDEC_ERROR err = MPS_OK;
   1220   SPATIAL_SPECIFIC_CONFIG *sscParse;
   1221   SPATIAL_BS_FRAME *bsFrame;
   1222   HANDLE_FDK_BITSTREAM hMpsBsData = NULL;
   1223   FDK_BITSTREAM mpsBsData;
   1224   int mpsDataBits = *pMpsDataBits;
   1225   int mpsBsBits;
   1226   MPEGS_ANCTYPE ancType;
   1227   MPEGS_ANCSTARTSTOP ancStartStop;
   1228 
   1229   if (pMpegSurroundDecoder == NULL) {
   1230     return MPS_INVALID_HANDLE;
   1231   }
   1232 
   1233   FDK_ASSERT(pMpegSurroundDecoder->pSpatialDec);
   1234 
   1235   mpsBsBits = FDKgetValidBits(hBs);
   1236 
   1237   sscParse = &pMpegSurroundDecoder
   1238                   ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameParse];
   1239   bsFrame = &pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse];
   1240 
   1241   /*
   1242      Find operation mode of mpeg surround decoder:
   1243      - MPEGS_OPMODE_EMM:            Mode: Enhanced Matrix Mode (Blind)
   1244      - MPEGS_OPMODE_MPS_PAYLOAD:    Mode: Normal, Stereo or Binaural
   1245      - MPEGS_OPMODE_NO_MPS_PAYLOAD: Mode: No MpegSurround Payload
   1246   */
   1247   {
   1248     /* Parse ancType and ancStartStop */
   1249     ancType = (MPEGS_ANCTYPE)FDKreadBits(hBs, 2);
   1250     ancStartStop = (MPEGS_ANCSTARTSTOP)FDKreadBits(hBs, 2);
   1251     mpsDataBits -= 4;
   1252 
   1253     /* Set valid anc type flag, if ancType signals a payload with either header
   1254      * and frame or frame */
   1255     if (isValidAncType(pMpegSurroundDecoder, ancType)) {
   1256       /* Set valid anc startstop flag, if transmitted sequence is not illegal */
   1257       if (isValidAncStartStop(pMpegSurroundDecoder, ancStartStop)) {
   1258         switch (ancStartStop) {
   1259           case MPEGS_START:
   1260             /* Assuming that core coder frame size (AAC) is smaller than MPS
   1261                coder frame size. Save audio data for next frame. */
   1262             if (mpsDataBits > MPS_DATA_BUFFER_SIZE * 8) {
   1263               err = MPS_NOTOK;
   1264               goto bail;
   1265             }
   1266             for (int i = 0; i < mpsDataBits / 8; i++) {
   1267               pMpegSurroundDecoder->mpsData[i] = FDKreadBits(hBs, 8);
   1268             }
   1269             pMpegSurroundDecoder->mpsDataBits = mpsDataBits;
   1270             break;
   1271 
   1272           case MPEGS_CONTINUE:
   1273           case MPEGS_STOP:
   1274             /* Assuming that core coder frame size (AAC) is smaller than MPS
   1275                coder frame size. Save audio data for next frame. */
   1276             if ((pMpegSurroundDecoder->mpsDataBits + mpsDataBits) >
   1277                 MPS_DATA_BUFFER_SIZE * 8) {
   1278               err = MPS_NOTOK;
   1279               goto bail;
   1280             }
   1281             for (int i = 0; i < mpsDataBits / 8; i++) {
   1282               pMpegSurroundDecoder
   1283                   ->mpsData[(pMpegSurroundDecoder->mpsDataBits / 8) + i] =
   1284                   FDKreadBits(hBs, 8);
   1285             }
   1286             pMpegSurroundDecoder->mpsDataBits += mpsDataBits;
   1287             FDKinitBitStream(&mpsBsData, pMpegSurroundDecoder->mpsData,
   1288                              MAX_BUFSIZE_BYTES,
   1289                              pMpegSurroundDecoder->mpsDataBits, BS_READER);
   1290             hMpsBsData = &mpsBsData;
   1291             break;
   1292 
   1293           case MPEGS_START_STOP:
   1294             pMpegSurroundDecoder->mpsDataBits = mpsDataBits;
   1295             hMpsBsData = hBs;
   1296             break;
   1297 
   1298           default:
   1299             FDK_ASSERT(0);
   1300         }
   1301 
   1302         if ((ancStartStop == MPEGS_STOP) ||
   1303             (ancStartStop == MPEGS_START_STOP)) {
   1304           switch (ancType) {
   1305             case MPEGS_ANCTYPE_HEADER_AND_FRAME: {
   1306               int parseResult, bitsRead;
   1307               SPATIAL_SPECIFIC_CONFIG spatialSpecificConfigTmp =
   1308                   pMpegSurroundDecoder->spatialSpecificConfigBackup;
   1309 
   1310               /* Parse spatial specific config */
   1311               bitsRead = FDKgetValidBits(hMpsBsData);
   1312 
   1313               err = SpatialDecParseSpecificConfigHeader(
   1314                   hMpsBsData,
   1315                   &pMpegSurroundDecoder->spatialSpecificConfigBackup, coreCodec,
   1316                   pMpegSurroundDecoder->upmixType);
   1317 
   1318               bitsRead = (bitsRead - FDKgetValidBits(hMpsBsData));
   1319               parseResult = ((err == MPS_OK) ? bitsRead : -bitsRead);
   1320 
   1321               if (parseResult < 0) {
   1322                 parseResult = -parseResult;
   1323                 err = MPS_PARSE_ERROR;
   1324               } else if (err == MPS_OK) {
   1325                 /* Check SSC for consistency (e.g. bit errors could cause
   1326                  * trouble) */
   1327                 err = sscCheckInBand(
   1328                     &pMpegSurroundDecoder->spatialSpecificConfigBackup,
   1329                     frameSize, sampleRate);
   1330               }
   1331               if (err != MPS_OK) {
   1332                 pMpegSurroundDecoder->spatialSpecificConfigBackup =
   1333                     spatialSpecificConfigTmp;
   1334                 break;
   1335               }
   1336 
   1337               pMpegSurroundDecoder->mpsDataBits -= parseResult;
   1338 
   1339               /* Initiate re-initialization, if header has changed */
   1340               if (FDK_SpatialDecCompareSpatialSpecificConfigHeader(
   1341                       &pMpegSurroundDecoder->spatialSpecificConfigBackup,
   1342                       sscParse) == MPS_UNEQUAL_SSC) {
   1343                 pMpegSurroundDecoder
   1344                     ->initFlags[pMpegSurroundDecoder->bsFrameParse] |=
   1345                     MPEGS_INIT_CHANGE_HEADER;
   1346                 SpatialDecInitParserContext(pMpegSurroundDecoder->pSpatialDec);
   1347                 /* We found a valid in-band configuration. Therefore any
   1348                  * previous config is invalid now. */
   1349                 pMpegSurroundDecoder->mpegSurroundSscIsGlobalCfg = 0;
   1350               }
   1351             }
   1352             case MPEGS_ANCTYPE_FRAME:
   1353 
   1354               if (pMpegSurroundDecoder
   1355                       ->initFlags[pMpegSurroundDecoder->bsFrameParse] &
   1356                   MPEGS_INIT_ERROR_PAYLOAD) {
   1357                 err = MPS_PARSE_ERROR;
   1358                 break;
   1359               }
   1360 
   1361               /* First spatial specific config is parsed into
   1362                * spatialSpecificConfigBackup, second spatialSpecificConfigBackup
   1363                * is copied into spatialSpecificConfig[bsFrameDecode] */
   1364               if (pMpegSurroundDecoder
   1365                       ->initFlags[pMpegSurroundDecoder->bsFrameParse]) {
   1366                 FDKmemcpy(sscParse,
   1367                           &pMpegSurroundDecoder->spatialSpecificConfigBackup,
   1368                           sizeof(SPATIAL_SPECIFIC_CONFIG));
   1369                 pMpegSurroundDecoder
   1370                     ->fOnSync[pMpegSurroundDecoder->bsFrameParse] =
   1371                     MPEGS_SYNC_FOUND;
   1372               }
   1373 
   1374               if (pMpegSurroundDecoder
   1375                       ->fOnSync[pMpegSurroundDecoder->bsFrameParse] >=
   1376                   MPEGS_SYNC_FOUND) {
   1377                 int nbits = 0, bitsAvail;
   1378 
   1379                 if (err != MPS_OK) {
   1380                   break;
   1381                 }
   1382 
   1383                 bitsAvail = FDKgetValidBits(hMpsBsData);
   1384 
   1385                 if (bitsAvail <= 0) {
   1386                   err = MPS_PARSE_ERROR;
   1387                 } else {
   1388                   err = SpatialDecParseFrameData(
   1389                       pMpegSurroundDecoder->pSpatialDec, bsFrame, hMpsBsData,
   1390                       sscParse, (UPMIXTYPE)pMpegSurroundDecoder->upmixType,
   1391                       fGlobalIndependencyFlag);
   1392                   if (err == MPS_OK) {
   1393                     bsFrame->newBsData = 1;
   1394                   }
   1395                 }
   1396 
   1397                 nbits = bitsAvail - (INT)FDKgetValidBits(hMpsBsData);
   1398 
   1399                 if ((nbits > bitsAvail) ||
   1400                     (nbits > pMpegSurroundDecoder->mpsDataBits) ||
   1401                     (pMpegSurroundDecoder->mpsDataBits > nbits + 7 &&
   1402                      !IS_LOWDELAY(coreCodec))) {
   1403                   bsFrame->newBsData = 0;
   1404                   err = MPS_PARSE_ERROR;
   1405                   break;
   1406                 }
   1407                 pMpegSurroundDecoder->mpsDataBits -= nbits;
   1408               }
   1409               break;
   1410 
   1411             default: /* added to avoid compiler warning */
   1412               err = MPS_NOTOK;
   1413               break; /* added to avoid compiler warning */
   1414           }          /* switch (ancType) */
   1415 
   1416           if (err == MPS_OK) {
   1417             pMpegSurroundDecoder->ancStartStopPrev = ancStartStop;
   1418           } else {
   1419             updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
   1420                                             MPEGS_INIT_ERROR_PAYLOAD,
   1421                                             MPEGS_SYNC_LOST, MPEGS_STOP);
   1422             pMpegSurroundDecoder->mpsDataBits = 0;
   1423           }
   1424         } /* (ancStartStop == MPEGS_STOP) || (ancStartStop == MPEGS_START_STOP)
   1425            */
   1426       }   /* validAncStartStop */
   1427     }     /* validAncType */
   1428   }
   1429 
   1430 bail:
   1431 
   1432   *pMpsDataBits -= (mpsBsBits - FDKgetValidBits(hBs));
   1433 
   1434   return err;
   1435 }
   1436 
   1437 int mpegSurroundDecoder_Apply(CMpegSurroundDecoder *pMpegSurroundDecoder,
   1438                               INT_PCM *input, PCM_MPS *pTimeData,
   1439                               const int timeDataSize, int timeDataFrameSize,
   1440                               int *nChannels, int *frameSize, int sampleRate,
   1441                               AUDIO_OBJECT_TYPE coreCodec,
   1442                               AUDIO_CHANNEL_TYPE channelType[],
   1443                               UCHAR channelIndices[],
   1444                               const FDK_channelMapDescr *const mapDescr) {
   1445   SACDEC_ERROR err = MPS_OK;
   1446   PCM_MPS *pTimeOut = pTimeData;
   1447   UINT initControlFlags = 0, controlFlags = 0;
   1448   int timeDataRequiredSize = 0;
   1449   int newData;
   1450 
   1451   if (pMpegSurroundDecoder == NULL) {
   1452     return MPS_INVALID_HANDLE;
   1453   }
   1454 
   1455   FDK_ASSERT(pMpegSurroundDecoder->pSpatialDec);
   1456 
   1457   if (!FDK_chMapDescr_isValid(mapDescr)) {
   1458     return MPS_INVALID_HANDLE;
   1459   }
   1460 
   1461   if ((*nChannels <= 0) || (*nChannels > 2)) {
   1462     return MPS_NOTOK;
   1463   }
   1464 
   1465   pMpegSurroundDecoder->pSpatialDec->pConfigCurrent =
   1466       &pMpegSurroundDecoder
   1467            ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameDecode];
   1468   newData = pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameParse]
   1469                 .newBsData;
   1470 
   1471   switch (mpegSurroundOperationMode(pMpegSurroundDecoder, 1000)) {
   1472     case MPEGS_OPMODE_MPS_PAYLOAD:
   1473       if (pMpegSurroundDecoder
   1474               ->initFlags[pMpegSurroundDecoder->bsFrameDecode]) {
   1475         err = initMpegSurroundDecoder(pMpegSurroundDecoder);
   1476       }
   1477 
   1478       if (err == MPS_OK) {
   1479         if ((pMpegSurroundDecoder
   1480                  ->fOnSync[pMpegSurroundDecoder->bsFrameDecode] !=
   1481              MPEGS_SYNC_COMPLETE) &&
   1482             (pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameDecode]
   1483                  .bsIndependencyFlag == 1)) {
   1484           /* We got a valid header and independently decodeable frame data.
   1485               -> Go to the next sync level and start processing. */
   1486           pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
   1487               MPEGS_SYNC_COMPLETE;
   1488         }
   1489       } else {
   1490         /* We got a valid config header but found an error while parsing the
   1491            bitstream. Wait for the next independent frame and apply error
   1492            conealment in the meantime. */
   1493         pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
   1494             MPEGS_SYNC_FOUND;
   1495         controlFlags |= MPEGS_CONCEAL;
   1496         err = MPS_OK;
   1497       }
   1498       /*
   1499          Concealment:
   1500          - Bitstream is available, no sync found during bitstream processing
   1501          - Bitstream is available, sync lost due to corrupted bitstream
   1502          - Bitstream is available, sync found but no independent frame
   1503       */
   1504       if (pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] !=
   1505           MPEGS_SYNC_COMPLETE) {
   1506         controlFlags |= MPEGS_CONCEAL;
   1507       }
   1508       break;
   1509 
   1510     case MPEGS_OPMODE_NO_MPS_PAYLOAD:
   1511       /* Concealment: No bitstream is available */
   1512       controlFlags |= MPEGS_CONCEAL;
   1513       break;
   1514 
   1515     default:
   1516       err = MPS_NOTOK;
   1517   }
   1518 
   1519   if (err != MPS_OK) {
   1520     goto bail;
   1521   }
   1522 
   1523   /*
   1524    * Force BypassMode if choosen by user
   1525    */
   1526   if (pMpegSurroundDecoder->mpegSurroundUserParams.bypassMode) {
   1527     controlFlags |= MPEGS_BYPASSMODE;
   1528   }
   1529 
   1530   if (pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode]) {
   1531     int startWithDfltCfg = 0;
   1532     /*
   1533      * Init with a default configuration if we came here and are still not
   1534      * initialized.
   1535      */
   1536     if (pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] &
   1537         MPEGS_INIT_ENFORCE_REINIT) {
   1538       /* Get default spatial specific config */
   1539       if (FDK_SpatialDecInitDefaultSpatialSpecificConfig(
   1540               &pMpegSurroundDecoder->spatialSpecificConfigBackup, coreCodec,
   1541               *nChannels, sampleRate,
   1542               *frameSize /
   1543                   mpegSurroundDecoder_GetNrOfQmfBands(NULL, sampleRate),
   1544               pMpegSurroundDecoder->mpegSurroundDecoderLevel,
   1545               pMpegSurroundDecoder->mpegSurroundUserParams.blindEnable)) {
   1546         err = MPS_NOTOK;
   1547         goto bail;
   1548       }
   1549 
   1550       /* Initiate re-initialization, if header has changed */
   1551       if (FDK_SpatialDecCompareSpatialSpecificConfigHeader(
   1552               &pMpegSurroundDecoder->spatialSpecificConfigBackup,
   1553               &pMpegSurroundDecoder->spatialSpecificConfig
   1554                    [pMpegSurroundDecoder->bsFrameDecode]) == MPS_UNEQUAL_SSC) {
   1555         pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
   1556             MPEGS_INIT_CHANGE_HEADER;
   1557         SpatialDecInitParserContext(pMpegSurroundDecoder->pSpatialDec);
   1558       }
   1559 
   1560       startWithDfltCfg = 1;
   1561     }
   1562 
   1563     /* First spatial specific config is parsed into spatialSpecificConfigBackup,
   1564      * second spatialSpecificConfigBackup is copied into spatialSpecificConfig
   1565      */
   1566     err = initMpegSurroundDecoder(pMpegSurroundDecoder);
   1567 
   1568     if (startWithDfltCfg) {
   1569       /* initialized with default config, but no sync found */
   1570       /* maybe use updateMpegSurroundDecoderStatus later on */
   1571       pMpegSurroundDecoder->fOnSync[pMpegSurroundDecoder->bsFrameDecode] =
   1572           MPEGS_SYNC_LOST;
   1573     }
   1574 
   1575     /* Since we do not have state MPEGS_SYNC_COMPLETE apply concealment */
   1576     controlFlags |= MPEGS_CONCEAL;
   1577 
   1578     if (err != MPS_OK) {
   1579       goto bail;
   1580     }
   1581   }
   1582 
   1583   /*
   1584    * Process MPEG Surround Audio
   1585    */
   1586   initControlFlags = controlFlags;
   1587 
   1588   /* Check that provided output buffer is large enough. */
   1589   timeDataRequiredSize =
   1590       (timeDataFrameSize *
   1591        pMpegSurroundDecoder->pSpatialDec->numOutputChannelsAT *
   1592        pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsSynthesis) /
   1593       pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsAnalysis;
   1594   if (timeDataSize < timeDataRequiredSize) {
   1595     err = MPS_OUTPUT_BUFFER_TOO_SMALL;
   1596     goto bail;
   1597   }
   1598 
   1599   if ((pMpegSurroundDecoder->pSpatialDec->pConfigCurrent->syntaxFlags &
   1600        SACDEC_SYNTAX_USAC) &&
   1601       (pMpegSurroundDecoder->pSpatialDec->stereoConfigIndex > 1)) {
   1602     FDK_ASSERT(timeDataRequiredSize >= timeDataFrameSize * *nChannels);
   1603     /* Place samples comprising QMF time slots spaced at QMF output Band raster
   1604      * to allow slot wise processing */
   1605     int timeDataFrameSizeOut =
   1606         (timeDataFrameSize *
   1607          pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsSynthesis) /
   1608         pMpegSurroundDecoder->pQmfDomain->globalConf.nBandsAnalysis;
   1609     pMpegSurroundDecoder->pQmfDomain->globalConf.TDinput =
   1610         pTimeData + timeDataFrameSizeOut - timeDataFrameSize;
   1611     for (int i = *nChannels - 1; i >= 0; i--) {
   1612       FDKmemmove(pTimeData + (i + 1) * timeDataFrameSizeOut - timeDataFrameSize,
   1613                  pTimeData + timeDataFrameSize * i,
   1614                  sizeof(PCM_MPS) * timeDataFrameSize);
   1615       FDKmemclear(pTimeData + i * timeDataFrameSizeOut,
   1616                   sizeof(PCM_MPS) * (timeDataFrameSizeOut - timeDataFrameSize));
   1617     }
   1618   } else {
   1619     if (pMpegSurroundDecoder->mpegSurroundUseTimeInterface) {
   1620       FDKmemcpy(input, pTimeData,
   1621                 sizeof(INT_PCM) * (*nChannels) * (*frameSize));
   1622       pMpegSurroundDecoder->pQmfDomain->globalConf.TDinput = input;
   1623     }
   1624   }
   1625 
   1626   /*
   1627    * Process MPEG Surround Audio
   1628    */
   1629   err = SpatialDecApplyFrame(
   1630       pMpegSurroundDecoder->pSpatialDec,
   1631       &pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameDecode],
   1632       pMpegSurroundDecoder->mpegSurroundUseTimeInterface ? INPUTMODE_TIME
   1633                                                          : INPUTMODE_QMF_SBR,
   1634       pMpegSurroundDecoder->pQmfDomain->globalConf.TDinput, NULL, NULL,
   1635       pTimeOut, *frameSize, &controlFlags, *nChannels, mapDescr);
   1636   *nChannels = pMpegSurroundDecoder->pSpatialDec->numOutputChannelsAT;
   1637 
   1638   if (err !=
   1639       MPS_OK) { /* A fatal error occured. Go back to start and try again: */
   1640     updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
   1641                                     MPEGS_INIT_ENFORCE_REINIT, MPEGS_SYNC_LOST,
   1642                                     MPEGS_STOP);
   1643     *frameSize =
   1644         0; /* Declare that framework can not use the data in pTimeOut. */
   1645   } else {
   1646     if (((controlFlags & MPEGS_CONCEAL) &&
   1647          !(initControlFlags & MPEGS_CONCEAL)) ||
   1648         (pMpegSurroundDecoder->pSpatialDec->errInt !=
   1649          MPS_OK)) { /* Account for errors that occured in
   1650                        SpatialDecApplyFrame(): */
   1651       updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
   1652                                       MPEGS_INIT_ERROR_PAYLOAD, MPEGS_SYNC_LOST,
   1653                                       MPEGS_STOP);
   1654     }
   1655   }
   1656 
   1657   if ((err == MPS_OK) && !(controlFlags & MPEGS_BYPASSMODE) &&
   1658       !(pMpegSurroundDecoder->upmixType == UPMIX_TYPE_BYPASS)) {
   1659     SpatialDecChannelProperties(pMpegSurroundDecoder->pSpatialDec, channelType,
   1660                                 channelIndices, mapDescr);
   1661   }
   1662 
   1663 bail:
   1664 
   1665   if (newData) {
   1666     /* numParameterSetsPrev shall only be read in the decode process, because of
   1667        that we can update this state variable here */
   1668     pMpegSurroundDecoder->pSpatialDec->numParameterSetsPrev =
   1669         pMpegSurroundDecoder->bsFrames[pMpegSurroundDecoder->bsFrameDecode]
   1670             .numParameterSets;
   1671   }
   1672 
   1673   return (err);
   1674 }
   1675 
   1676 /**
   1677  * \brief Free config dependent MPEG Surround memory.
   1678  **/
   1679 SACDEC_ERROR mpegSurroundDecoder_FreeMem(
   1680     CMpegSurroundDecoder *pMpegSurroundDecoder) {
   1681   SACDEC_ERROR err = MPS_OK;
   1682 
   1683   if (pMpegSurroundDecoder != NULL) {
   1684     FDK_SpatialDecClose(pMpegSurroundDecoder->pSpatialDec);
   1685     pMpegSurroundDecoder->pSpatialDec = NULL;
   1686   }
   1687 
   1688   return err;
   1689 }
   1690 
   1691 /**
   1692  * \brief Close MPEG Surround decoder.
   1693  **/
   1694 void mpegSurroundDecoder_Close(CMpegSurroundDecoder *pMpegSurroundDecoder) {
   1695   if (pMpegSurroundDecoder != NULL) {
   1696     FDK_SpatialDecClose(pMpegSurroundDecoder->pSpatialDec);
   1697     pMpegSurroundDecoder->pSpatialDec = NULL;
   1698 
   1699     for (int i = 0; i < 1; i++) {
   1700       SpatialDecCloseBsFrame(&pMpegSurroundDecoder->bsFrames[i]);
   1701     }
   1702 
   1703     FDK_FREE_MEMORY_1D(pMpegSurroundDecoder);
   1704   }
   1705 }
   1706 
   1707 #define SACDEC_VL0 2
   1708 #define SACDEC_VL1 0
   1709 #define SACDEC_VL2 0
   1710 
   1711 int mpegSurroundDecoder_GetLibInfo(LIB_INFO *info) {
   1712   int i;
   1713 
   1714   if (info == NULL) {
   1715     return -1;
   1716   }
   1717 
   1718   /* search for next free tab */
   1719   for (i = 0; i < FDK_MODULE_LAST; i++) {
   1720     if (info[i].module_id == FDK_NONE) break;
   1721   }
   1722   if (i == FDK_MODULE_LAST) return -1;
   1723 
   1724   info += i;
   1725 
   1726   info->module_id = FDK_MPSDEC;
   1727 #ifdef __ANDROID__
   1728   info->build_date = "";
   1729   info->build_time = "";
   1730 #else
   1731   info->build_date = __DATE__;
   1732   info->build_time = __TIME__;
   1733 #endif
   1734   info->title = "MPEG Surround Decoder";
   1735   info->version = LIB_VERSION(SACDEC_VL0, SACDEC_VL1, SACDEC_VL2);
   1736   LIB_VERSION_STRING(info);
   1737   info->flags = 0 | CAPF_MPS_LD | CAPF_MPS_USAC | CAPF_MPS_HQ |
   1738                 CAPF_MPS_1CH_IN | CAPF_MPS_2CH_OUT; /* end flags */
   1739 
   1740   return 0;
   1741 }
   1742 
   1743 SACDEC_ERROR mpegSurroundDecoder_SetParam(
   1744     CMpegSurroundDecoder *pMpegSurroundDecoder, const SACDEC_PARAM param,
   1745     const INT value) {
   1746   SACDEC_ERROR err = MPS_OK;
   1747   SPATIALDEC_PARAM *pUserParams = NULL;
   1748 
   1749   /* check decoder handle */
   1750   if (pMpegSurroundDecoder != NULL) {
   1751     /* init local shortcuts */
   1752     pUserParams = &pMpegSurroundDecoder->mpegSurroundUserParams;
   1753   } else {
   1754     err = MPS_INVALID_HANDLE;
   1755     /* check the parameter values before exiting. */
   1756   }
   1757 
   1758   /* apply param value */
   1759   switch (param) {
   1760     case SACDEC_OUTPUT_MODE:
   1761       switch ((SAC_DEC_OUTPUT_MODE)value) {
   1762         case SACDEC_OUT_MODE_NORMAL:
   1763         case SACDEC_OUT_MODE_STEREO:
   1764           break;
   1765         default:
   1766           err = MPS_INVALID_PARAMETER;
   1767       }
   1768       if (err == MPS_OK) {
   1769         if (0) {
   1770           err = MPS_INVALID_PARAMETER;
   1771         } else if (pUserParams->outputMode != (UCHAR)value) {
   1772           pUserParams->outputMode = (UCHAR)value;
   1773           pMpegSurroundDecoder
   1774               ->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
   1775               MPEGS_INIT_CHANGE_OUTPUT_MODE;
   1776         }
   1777       }
   1778       break;
   1779 
   1780     case SACDEC_INTERFACE:
   1781       if (value < 0 || value > 1) {
   1782         err = MPS_INVALID_PARAMETER;
   1783       }
   1784       if (err != MPS_OK) {
   1785         goto bail;
   1786       }
   1787       if (pMpegSurroundDecoder->mpegSurroundUseTimeInterface != (UCHAR)value) {
   1788         pMpegSurroundDecoder->mpegSurroundUseTimeInterface = (UCHAR)value;
   1789         pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
   1790             MPEGS_INIT_CHANGE_TIME_FREQ_INTERFACE;
   1791       }
   1792       break;
   1793 
   1794     case SACDEC_BS_INTERRUPTION:
   1795       if ((err == MPS_OK) && (value != 0)) {
   1796         updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
   1797                                         MPEGS_INIT_BS_INTERRUPTION,
   1798                                         MPEGS_SYNC_LOST, MPEGS_STOP);
   1799       }
   1800       break;
   1801 
   1802     case SACDEC_CLEAR_HISTORY:
   1803       if ((err == MPS_OK) && (value != 0)) {
   1804         /* Just reset the states and go on. */
   1805         updateMpegSurroundDecoderStatus(pMpegSurroundDecoder,
   1806                                         MPEGS_INIT_CLEAR_HISTORY,
   1807                                         MPEGS_SYNC_LOST, MPEGS_STOP);
   1808       }
   1809       break;
   1810 
   1811     case SACDEC_CONCEAL_NUM_KEEP_FRAMES:
   1812       if (value < 0) { /* Check valid value range */
   1813         err = MPS_INVALID_PARAMETER;
   1814       }
   1815       if (err != MPS_OK) {
   1816         goto bail;
   1817       }
   1818       if (pUserParams->concealNumKeepFrames != (UINT)value) {
   1819         pUserParams->concealNumKeepFrames = (UINT)value;
   1820         pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
   1821             MPEGS_INIT_CHANGE_CONCEAL_PARAMS;
   1822       }
   1823       break;
   1824 
   1825     case SACDEC_CONCEAL_FADE_OUT_SLOPE_LENGTH:
   1826       if (value < 0) { /* Check valid value range */
   1827         err = MPS_INVALID_PARAMETER;
   1828       }
   1829       if (err != MPS_OK) {
   1830         goto bail;
   1831       }
   1832       if (pUserParams->concealFadeOutSlopeLength != (UINT)value) {
   1833         pUserParams->concealFadeOutSlopeLength = (UINT)value;
   1834         pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
   1835             MPEGS_INIT_CHANGE_CONCEAL_PARAMS;
   1836       }
   1837       break;
   1838 
   1839     case SACDEC_CONCEAL_FADE_IN_SLOPE_LENGTH:
   1840       if (value < 0) { /* Check valid value range */
   1841         err = MPS_INVALID_PARAMETER;
   1842       }
   1843       if (err != MPS_OK) {
   1844         goto bail;
   1845       }
   1846       if (pUserParams->concealFadeInSlopeLength != (UINT)value) {
   1847         pUserParams->concealFadeInSlopeLength = (UINT)value;
   1848         pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
   1849             MPEGS_INIT_CHANGE_CONCEAL_PARAMS;
   1850       }
   1851       break;
   1852 
   1853     case SACDEC_CONCEAL_NUM_RELEASE_FRAMES:
   1854       if (value < 0) { /* Check valid value range */
   1855         err = MPS_INVALID_PARAMETER;
   1856       }
   1857       if (err != MPS_OK) {
   1858         goto bail;
   1859       }
   1860       if (pUserParams->concealNumReleaseFrames != (UINT)value) {
   1861         pUserParams->concealNumReleaseFrames = (UINT)value;
   1862         pMpegSurroundDecoder->initFlags[pMpegSurroundDecoder->bsFrameDecode] |=
   1863             MPEGS_INIT_CHANGE_CONCEAL_PARAMS;
   1864       }
   1865       break;
   1866 
   1867     default:
   1868       err = MPS_INVALID_PARAMETER;
   1869       break;
   1870   } /* switch(param) */
   1871 
   1872 bail:
   1873   return err;
   1874 }
   1875 
   1876 SACDEC_ERROR mpegSurroundDecoder_IsPseudoLR(
   1877     CMpegSurroundDecoder *pMpegSurroundDecoder, int *bsPseudoLr) {
   1878   if (pMpegSurroundDecoder != NULL) {
   1879     const SPATIAL_SPECIFIC_CONFIG *sscDecode =
   1880         &pMpegSurroundDecoder
   1881              ->spatialSpecificConfig[pMpegSurroundDecoder->bsFrameDecode];
   1882     *bsPseudoLr = (int)sscDecode->bsPseudoLr;
   1883     return MPS_OK;
   1884   } else
   1885     return MPS_INVALID_HANDLE;
   1886 }
   1887 
   1888 /**
   1889  * \brief Get the signal delay caused by the MPEG Surround decoder module.
   1890  **/
   1891 UINT mpegSurroundDecoder_GetDelay(const CMpegSurroundDecoder *self) {
   1892   INT outputDelay = 0;
   1893 
   1894   if (self != NULL) {
   1895     const SPATIAL_SPECIFIC_CONFIG *sscDecode =
   1896         &self->spatialSpecificConfig[self->bsFrameDecode];
   1897     AUDIO_OBJECT_TYPE coreCodec = sscDecode->coreCodec;
   1898 
   1899     /* See chapter 4.5 (delay and synchronization) of ISO/IEC FDIS 23003-1 and
   1900        chapter 5.4.3 of ISO/IEC FDIS 23003-2 for details on the following
   1901        figures. */
   1902 
   1903     if (coreCodec > AOT_NULL_OBJECT) {
   1904       if (IS_LOWDELAY(coreCodec)) {
   1905         /* All low delay variants (ER-AAC-(E)LD): */
   1906         outputDelay += 256;
   1907       } else if (!IS_USAC(coreCodec)) {
   1908         /* By the method of elimination this is the GA (AAC-LC, HE-AAC, ...)
   1909          * branch: */
   1910         outputDelay += 320 + 257; /* cos to exp delay + QMF synthesis */
   1911         if (self->mpegSurroundUseTimeInterface) {
   1912           outputDelay += 320 + 384; /* QMF and hybrid analysis */
   1913         }
   1914       }
   1915     }
   1916   }
   1917 
   1918   return (outputDelay);
   1919 }
   1920