FDKv2 additional fixes
am: 9ab67882ec

Change-Id: Ia2ecfc503d1c2a429ad1c7dd5103263c02691a56
diff --git a/libAACdec/src/aacdecoder_lib.cpp b/libAACdec/src/aacdecoder_lib.cpp
index b7ea8df..e62d187 100644
--- a/libAACdec/src/aacdecoder_lib.cpp
+++ b/libAACdec/src/aacdecoder_lib.cpp
@@ -396,7 +396,8 @@
       /* MPS found but invalid or not decodable by this instance            */
       hAacDecoder->mpsEnableCurr = 0;
       hAacDecoder->mpsApplicable = 0;
-      if ((coreCodec == AOT_USAC) || IS_LOWDELAY(coreCodec)) {
+      if ((coreCodec == AOT_USAC) || (coreCodec == AOT_DRM_USAC) ||
+          IS_LOWDELAY(coreCodec)) {
         errTp = TRANSPORTDEC_PARSE_ERROR;
       } else {
         errTp = TRANSPORTDEC_OK;
@@ -1654,14 +1655,14 @@
                       self->streamInfo.frameSize;
 
           for (ch = 0; ch < self->streamInfo.numChannels; ch++) {
-            int mapValue = FDK_chMapDescr_getMapValue(
+            UCHAR mapValue = FDK_chMapDescr_getMapValue(
                 &self->mapDescr, (UCHAR)ch, self->chMapIndex);
-            reverseInChannelMap[mapValue] = ch;
+            if (mapValue < (8)) reverseInChannelMap[mapValue] = ch;
           }
           for (ch = 0; ch < (int)numDrcOutChannels; ch++) {
-            int mapValue = FDK_chMapDescr_getMapValue(
+            UCHAR mapValue = FDK_chMapDescr_getMapValue(
                 &self->mapDescr, (UCHAR)ch, numDrcOutChannels);
-            reverseOutChannelMap[mapValue] = ch;
+            if (mapValue < (8)) reverseOutChannelMap[mapValue] = ch;
           }
 
           /* The output of SBR and MPS is interleaved. Deinterleaving may be
diff --git a/libAACdec/src/channel.cpp b/libAACdec/src/channel.cpp
index dbbf58a..cfffd57 100644
--- a/libAACdec/src/channel.cpp
+++ b/libAACdec/src/channel.cpp
@@ -225,15 +225,18 @@
     /* apply intensity stereo */ /* modifies pAacDecoderChannelInfo[]->aSpecSfb
                                   */
     if (!(flags & (AC_USAC | AC_RSVD50 | AC_RSV603DA))) {
-      CJointStereo_ApplyIS(
-          pAacDecoderChannelInfo,
-          GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo,
-                                    pSamplingRateInfo),
-          GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
-          GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo),
-          GetScaleFactorBandsTransmitted(&pAacDecoderChannelInfo[L]->icsInfo),
-          pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow ? 1
-                                                                        : 0);
+      if ((pAacDecoderChannelInfo[L]->pDynData->RawDataInfo.CommonWindow ==
+           1) &&
+          (el_channels == 2)) {
+        CJointStereo_ApplyIS(
+            pAacDecoderChannelInfo,
+            GetScaleFactorBandOffsets(&pAacDecoderChannelInfo[L]->icsInfo,
+                                      pSamplingRateInfo),
+            GetWindowGroupLengthTable(&pAacDecoderChannelInfo[L]->icsInfo),
+            GetWindowGroups(&pAacDecoderChannelInfo[L]->icsInfo),
+            GetScaleFactorBandsTransmitted(
+                &pAacDecoderChannelInfo[L]->icsInfo));
+      }
     }
   } /* maybe_stereo */
 
diff --git a/libAACdec/src/channelinfo.h b/libAACdec/src/channelinfo.h
index 7c95e12..45a288f 100644
--- a/libAACdec/src/channelinfo.h
+++ b/libAACdec/src/channelinfo.h
@@ -478,15 +478,13 @@
   \param pWindowGroupLength pointer to window group length array.
   \param windowGroups number of window groups.
   \param scaleFactorBandsTransmitted number of transmitted scalefactor bands.
-  \param CommonWindow common window bit.
   \return  none
 */
 void CJointStereo_ApplyIS(CAacDecoderChannelInfo *pAacDecoderChannelInfo[2],
                           const short *pScaleFactorBandOffsets,
                           const UCHAR *pWindowGroupLength,
                           const int windowGroups,
-                          const int scaleFactorBandsTransmitted,
-                          const UINT CommonWindow);
+                          const int scaleFactorBandsTransmitted);
 
 /* aacdec_pns.cpp */
 int CPns_IsPnsUsed(const CPnsData *pPnsData, const int group, const int band);
diff --git a/libAACdec/src/stereo.cpp b/libAACdec/src/stereo.cpp
index bb4f050..eed826b 100644
--- a/libAACdec/src/stereo.cpp
+++ b/libAACdec/src/stereo.cpp
@@ -1177,8 +1177,7 @@
                           const SHORT *pScaleFactorBandOffsets,
                           const UCHAR *pWindowGroupLength,
                           const int windowGroups,
-                          const int scaleFactorBandsTransmitted,
-                          const UINT CommonWindow) {
+                          const int scaleFactorBandsTransmitted) {
   CJointStereoData *pJointStereoData =
       &pAacDecoderChannelInfo[L]->pComData->jointStereoData;
 
@@ -1228,7 +1227,7 @@
 
           rightScale[band] = leftScale[band] + msb + 1;
 
-          if (CommonWindow && (pJointStereoData->MsUsed[band] & groupMask)) {
+          if (pJointStereoData->MsUsed[band] & groupMask) {
             if (CodeBook[band] == INTENSITY_HCB) /* _NOT_ in-phase */
             {
               scale = -scale;
diff --git a/libMpegTPDec/src/tpdec_asc.cpp b/libMpegTPDec/src/tpdec_asc.cpp
index abe66e5..74beaa6 100644
--- a/libMpegTPDec/src/tpdec_asc.cpp
+++ b/libMpegTPDec/src/tpdec_asc.cpp
@@ -2488,7 +2488,7 @@
 
     switch (audioCoding) {
       case 0: /* AAC */
-        if (coderField >> 2) {
+        if ((coderField >> 2) && (audioMode != 1)) {
           self->m_aot = AOT_DRM_SURROUND; /* Set pseudo AOT for Drm Surround */
         } else {
           self->m_aot = AOT_DRM_AAC; /* Set pseudo AOT for Drm AAC */
diff --git a/libMpegTPDec/src/tpdec_latm.cpp b/libMpegTPDec/src/tpdec_latm.cpp
index b4f7372..2edf055 100644
--- a/libMpegTPDec/src/tpdec_latm.cpp
+++ b/libMpegTPDec/src/tpdec_latm.cpp
@@ -126,7 +126,6 @@
     CSTpCallBacks *pTpDecCallbacks, CSAudioSpecificConfig *pAsc,
     int *pfConfigFound) {
   TRANSPORTDEC_ERROR ErrorStatus = TRANSPORTDEC_OK;
-  UCHAR applyAsc = pLatmDemux->applyAsc;
 
   if (m_muxConfigPresent) {
     pLatmDemux->m_useSameStreamMux = FDKreadBits(bs, 1);
@@ -152,7 +151,12 @@
           goto bail;
         }
 
-        if (pLatmDemux->newCfgHasAudioPreRoll) {
+        /* Allow flushing only when audioPreroll functionality is enabled in
+         * current and new config otherwise the new config can be applied
+         * immediately. */
+        if (pAsc->m_sc.m_usacConfig.element[0]
+                .extElement.usacExtElementHasAudioPreRoll &&
+            pLatmDemux->newCfgHasAudioPreRoll) {
           pLatmDemux->newCfgHasAudioPreRoll = 0;
           /* with audioPreRoll we must flush before applying new cfg */
           pLatmDemux->applyAsc = 0;
@@ -223,7 +227,7 @@
 
 bail:
   if (ErrorStatus != TRANSPORTDEC_OK) {
-    pLatmDemux->applyAsc = applyAsc;
+    pLatmDemux->applyAsc = 1;
   }
 
   return (ErrorStatus);
diff --git a/libMpegTPDec/src/tpdec_lib.cpp b/libMpegTPDec/src/tpdec_lib.cpp
index 10e3352..306bec0 100644
--- a/libMpegTPDec/src/tpdec_lib.cpp
+++ b/libMpegTPDec/src/tpdec_lib.cpp
@@ -1151,6 +1151,12 @@
                                     &syncLayerFrameBits, &fConfigFound,
                                     &headerBits);
       if (TPDEC_IS_FATAL_ERROR(err)) {
+        /* Rewind - TPDEC_SYNCSKIP, in order to look for a synch one bit ahead
+         * next time. Ensure that the bit amount lands at a multiple of
+         * TPDEC_SYNCSKIP. */
+        FDKpushBiDirectional(
+            hBs, -headerBits + TPDEC_SYNCSKIP + (bitsAvail % TPDEC_SYNCSKIP));
+
         goto bail;
       }
     }
diff --git a/libSACdec/src/sac_dec_lib.cpp b/libSACdec/src/sac_dec_lib.cpp
index 56d8693..ebf9bee 100644
--- a/libSACdec/src/sac_dec_lib.cpp
+++ b/libSACdec/src/sac_dec_lib.cpp
@@ -698,6 +698,7 @@
     INT coreSbrFrameLengthIndex, INT configBytes, const UCHAR configMode,
     UCHAR *configChanged) {
   SACDEC_ERROR err = MPS_OK;
+  SPATIAL_SPECIFIC_CONFIG spatialSpecificConfig;
 
   switch (coreCodec) {
     case AOT_DRM_USAC:
@@ -705,7 +706,6 @@
       if (configMode == AC_CM_DET_CFG_CHANGE) {
         /* In config detection mode write spatial specific config parameters
          * into temporarily allocated structure */
-        SPATIAL_SPECIFIC_CONFIG spatialSpecificConfig;
         err = SpatialDecParseMps212Config(
             hBs, &spatialSpecificConfig, samplingRate, coreCodec,
             stereoConfigIndex, coreSbrFrameLengthIndex);
@@ -718,9 +718,16 @@
       break;
     case AOT_ER_AAC_ELD:
     case AOT_ER_AAC_LD:
-      err = SpatialDecParseSpecificConfig(
-          hBs, &pMpegSurroundDecoder->spatialSpecificConfigBackup, configBytes,
-          coreCodec);
+      if (configMode == AC_CM_DET_CFG_CHANGE) {
+        /* In config detection mode write spatial specific config parameters
+         * into temporarily allocated structure */
+        err = SpatialDecParseSpecificConfig(hBs, &spatialSpecificConfig,
+                                            configBytes, coreCodec);
+      } else {
+        err = SpatialDecParseSpecificConfig(
+            hBs, &pMpegSurroundDecoder->spatialSpecificConfigBackup,
+            configBytes, coreCodec);
+      }
       break;
     default:
       err = MPS_UNSUPPORTED_FORMAT;
diff --git a/libSACenc/src/sacenc_nlc_enc.cpp b/libSACenc/src/sacenc_nlc_enc.cpp
index ecb89b7..0ba6cc9 100644
--- a/libSACenc/src/sacenc_nlc_enc.cpp
+++ b/libSACenc/src/sacenc_nlc_enc.cpp
@@ -379,16 +379,22 @@
 
   switch (data_type) {
     case t_CLD:
-      part0 = fdk_sacenc_huffPart0Tab.cld[in_data[0]];
       pHuffTab = fdk_sacenc_huffCLDTab.h1D[dim1];
       break;
     case t_ICC:
-      part0 = fdk_sacenc_huffPart0Tab.icc[in_data[0]];
       pHuffTab = fdk_sacenc_huffICCTab.h1D[dim1];
       break;
   }
 
   if (p0_flag) {
+    switch (data_type) {
+      case t_CLD:
+        part0 = fdk_sacenc_huffPart0Tab.cld[in_data[0]];
+        break;
+      case t_ICC:
+        part0 = fdk_sacenc_huffPart0Tab.icc[in_data[0]];
+        break;
+    }
     huffBits += FDKwriteBits(strm, HUFF_VALUE(part0), HUFF_LENGTH(part0));
     offset = 1;
   }
diff --git a/libSBRenc/src/tran_det.cpp b/libSBRenc/src/tran_det.cpp
index ba9ae68..b6f1b9f 100644
--- a/libSBRenc/src/tran_det.cpp
+++ b/libSBRenc/src/tran_det.cpp
@@ -366,7 +366,7 @@
         accu += (EnergiesM[slotOut][j] >> scale[0]);
       }
     }
-    nrgTotal = accu >> (scaleEnergies[1] - scale[1]);
+    nrgTotal = fAddSaturate(nrgTotal, accu >> (scaleEnergies[1] - scale[1]));
   }
 
   return (nrgTotal);