Bug 7170947 Update AAC codec

From Fraunhofer:
* AAC Decoder

   - Stick to the written MPEG standard instead of the MPEG reference software
     in terms of reference level normalization. Always set the program reference
     level equal to the target level. This disables level normalization using a
     default level for streams without embedded metadata.
     Modified file(s):
        libAACdec\src\aacdec_drc.cpp

   - Fix downmix channel assignment when using a WAV output channel ordering.
     Modified file(s):
        libPCMutils\src\pcmutils_lib.cpp

   - Retain signal accuracy and prevent LSB alteration when no level correction
     needs to be done.
     Modified file(s):
        libAACdec\src\aacdec_drc.h
        libAACdec\src\aacdec_drc.cpp
        libSBRdec\src\sbrdecoder.cpp
        libSBRdec\src\sbr_dec.cpp
        libSBRdec\src\sbrdec_drc.cpp

   - Align metadata processing with reference implementation.
     Modified file(s):
        libAACdec\src\aacdec_drc.h
        libAACdec\src\aacdecoder.cpp

* AAC-Encoder

   - Prevent potential overflow in energy calculation after TNS processing.
     Modified file(s):
       libAACenc\src\band_nrg.cpp

   - Added saturation for number of relevant lines which are used in pe
     calculation.
     Modified file(s):
       libAACenc\src\line_pe.cpp

   - Removed obsolete files.
     Deleded file(s):
       libAACenc\src\tns_param.h
       libAACenc\src\tns_param.cpp

* FDK-Library

   - Added x86 Count Leading Zeros intrinsic.
     Modified file(s):
        libFDK\include\clz.h
     Added file(s):
        libFDK\include\x86\clz_x86.h

   - Fixed compilation for MIPS GCC-4.4 and higher.
     Modified file(s):
        libFDK\include\mips\cplx_mul.h
        libFDK\include\mips\fixmul_mips.h

Change-Id: I4be65f07f88d412224c7fddc3f054e8f451176cc
diff --git a/documentation/aacDecoder.pdf b/documentation/aacDecoder.pdf
index 0d42e79..a8cee17 100644
--- a/documentation/aacDecoder.pdf
+++ b/documentation/aacDecoder.pdf
Binary files differ
diff --git a/documentation/aacEncoder.pdf b/documentation/aacEncoder.pdf
index 2af41ae..84100eb 100644
--- a/documentation/aacEncoder.pdf
+++ b/documentation/aacEncoder.pdf
Binary files differ
diff --git a/libAACdec/src/aacdec_drc.cpp b/libAACdec/src/aacdec_drc.cpp
index c660b83..ebc6975 100644
--- a/libAACdec/src/aacdec_drc.cpp
+++ b/libAACdec/src/aacdec_drc.cpp
@@ -220,8 +220,9 @@
     else {
       /* ref_level must be between 0 and MAX_REFERENCE_LEVEL, inclusive */
       self->digitalNorm    = 1;
-      self->progRefLevel   = AACDEC_DRC_DEFAULT_REF_LEVEL;
       self->params.targetRefLevel = value;
+      self->progRefLevel = (SCHAR)value;  /* Set the program reference level equal to the target
+                                               level according to 4.5.2.7.3 of ISO/IEC 14496-3. */
     }
     break;
   case APPLY_HEAVY_COMPRESSION:
@@ -783,6 +784,7 @@
 {
   int band, top, bin, numBands;
   int bottom = 0;
+  int modifyBins = 0;
 
   FIXP_DBL max_mantissa;
   INT max_exponent;
@@ -937,6 +939,12 @@
       if (fact_exponent[band] < max_exponent) {
         fact_mantissa[band] >>= max_exponent - fact_exponent[band];
       }
+      if (fact_mantissa[band] != FL2FXCONST_DBL(0.5f)) {
+        modifyBins = 1;
+      }
+    }
+    if (max_exponent != 1) {
+      modifyBins = 1;
     }
   }
 
@@ -948,23 +956,28 @@
   {
     bottom = 0;
 
-    for (band = 0; band < numBands; band++)
+    if (!modifyBins) {
+      /* We don't have to modify the spectral bins because the fractional part of all factors is 0.5.
+         In order to keep accurancy we don't apply the factor but decrease the exponent instead. */
+      max_exponent -= 1;
+    } else
     {
-      top = fixMin((int)( (pDrcChData->bandTop[band]+1)<<2 ), aacFrameSize);   /* ... * DRC_BAND_MULT; */
+      for (band = 0; band < numBands; band++)
+      {
+        top = fixMin((int)( (pDrcChData->bandTop[band]+1)<<2 ), aacFrameSize);   /* ... * DRC_BAND_MULT; */
 
-      for (bin = bottom; bin < top; bin++) {
-        pSpectralCoefficient[bin] = fMult(pSpectralCoefficient[bin], fact_mantissa[band]);
+        for (bin = bottom; bin < top; bin++) {
+          pSpectralCoefficient[bin] = fMult(pSpectralCoefficient[bin], fact_mantissa[band]);
+        }
+
+        bottom = top;
       }
-
-      bottom = top;
     }
 
     /* above topmost DRC band gain factor is 1 */
     if (max_exponent > 0) {
-      FIXP_DBL fact = FL2FXCONST_DBL(0.5f) >> (max_exponent - 1);
-
-      for (bin = top; bin < aacFrameSize; bin++) {
-        pSpectralCoefficient[bin] = fMult(pSpectralCoefficient[bin], fact);
+      for (bin = bottom; bin < aacFrameSize; bin+=1) {
+        pSpectralCoefficient[bin] >>= max_exponent;
       }
     }
 
@@ -980,12 +993,13 @@
   }
   else {
     HANDLE_SBRDECODER hSbrDecoder = (HANDLE_SBRDECODER)pSbrDec;
+    UINT numBands = pDrcChData->numBands;
 
     /* feed factors into SBR decoder for application in QMF domain. */
     sbrDecoder_drcFeedChannel (
             hSbrDecoder,
             ch,
-            pDrcChData->numBands,
+            numBands,
             fact_mantissa,
             max_exponent,
             pDrcChData->drcInterpolationScheme,
diff --git a/libAACdec/src/aacdec_drc.h b/libAACdec/src/aacdec_drc.h
index 07e7eff..9c90e32 100644
--- a/libAACdec/src/aacdec_drc.h
+++ b/libAACdec/src/aacdec_drc.h
@@ -99,10 +99,7 @@
 #include "FDK_bitstream.h"
 
 #define AACDEC_DRC_DEFAULT_REF_LEVEL  ( 108 )   /* -27 dB below full scale (typical for movies) */
-#define AACDEC_DRC_DFLT_EXPIRY_FRAMES (  40 )   /* Default DRC data expiry time in AAC frames   */
-#define MAX_SBR_SYN_CHAN              (  64 )
-#define MAX_SBR_COLS                  (  32 )
-
+#define AACDEC_DRC_DFLT_EXPIRY_FRAMES (  50 )   /* Default DRC data expiry time in AAC frames   */
 
 /**
  * \brief DRC module setting parameters
diff --git a/libAACdec/src/aacdecoder.cpp b/libAACdec/src/aacdecoder.cpp
index 2843e9f..8d3c18d 100644
--- a/libAACdec/src/aacdecoder.cpp
+++ b/libAACdec/src/aacdecoder.cpp
@@ -865,6 +865,17 @@
       self->chMapping[ch] = 255;
     }
   }
+ #ifdef TP_PCE_ENABLE
+  else {
+    if (CProgramConfig_IsValid(&asc->m_progrConfigElement)) {
+      /* Set matrix mixdown infos if available from PCE. */
+      pcmDmx_SetMatrixMixdownFromPce ( self->hPcmUtils,
+                                       asc->m_progrConfigElement.MatrixMixdownIndexPresent,
+                                       asc->m_progrConfigElement.MatrixMixdownIndex,
+                                       asc->m_progrConfigElement.PseudoSurroundEnable );
+    }
+  }
+ #endif
 
   self->streamInfo.channelConfig = asc->m_channelConfiguration;
 
@@ -1565,7 +1576,7 @@
   self->streamInfo.numChannels = aacChannels;
 
  #ifdef TP_PCE_ENABLE
-  if (pceRead == 1 || CProgramConfig_IsValid(pce)) {
+  if (pceRead == 1 && CProgramConfig_IsValid(pce)) {
     /* Set matrix mixdown infos if available from PCE. */
     pcmDmx_SetMatrixMixdownFromPce ( self->hPcmUtils,
                                      pce->MatrixMixdownIndexPresent,
@@ -1673,7 +1684,7 @@
           break;
       }
       if ( flags&AACDEC_FLUSH ) {
-        FDKmemclear(pAacDecoderChannelInfo->pSpectralCoefficient, sizeof(FIXP_DBL)*self->streamInfo.aacSamplesPerFrame);
+          FDKmemclear(pAacDecoderChannelInfo->pSpectralCoefficient, sizeof(FIXP_DBL)*self->streamInfo.aacSamplesPerFrame);
         FDKmemclear(self->pAacDecoderStaticChannelInfo[c]->pOverlapBuffer, OverlapBufferSize*sizeof(FIXP_DBL));
       }
     }
diff --git a/libAACdec/src/aacdecoder_lib.cpp b/libAACdec/src/aacdecoder_lib.cpp
index 87f9ab0..a3db39e 100644
--- a/libAACdec/src/aacdecoder_lib.cpp
+++ b/libAACdec/src/aacdecoder_lib.cpp
@@ -110,7 +110,7 @@
 /* Decoder library info */
 #define AACDECODER_LIB_VL0 2
 #define AACDECODER_LIB_VL1 4
-#define AACDECODER_LIB_VL2 5
+#define AACDECODER_LIB_VL2 7
 #define AACDECODER_LIB_TITLE "AAC Decoder Lib"
 #define AACDECODER_LIB_BUILD_DATE __DATE__
 #define AACDECODER_LIB_BUILD_TIME __TIME__
diff --git a/libAACenc/src/aacenc_lib.cpp b/libAACenc/src/aacenc_lib.cpp
index a245358..07cfddc 100644
--- a/libAACenc/src/aacenc_lib.cpp
+++ b/libAACenc/src/aacenc_lib.cpp
@@ -98,7 +98,7 @@
 /* Encoder library info */
 #define AACENCODER_LIB_VL0 3
 #define AACENCODER_LIB_VL1 3
-#define AACENCODER_LIB_VL2 2
+#define AACENCODER_LIB_VL2 3
 #define AACENCODER_LIB_TITLE "AAC Encoder"
 #define AACENCODER_LIB_BUILD_DATE __DATE__
 #define AACENCODER_LIB_BUILD_TIME __TIME__
diff --git a/libAACenc/src/band_nrg.cpp b/libAACenc/src/band_nrg.cpp
index 458aa9c..0e46b45 100644
--- a/libAACenc/src/band_nrg.cpp
+++ b/libAACenc/src/band_nrg.cpp
@@ -260,21 +260,21 @@
 
   for(i=0; i<numBands; i++)
   {
-    int leadingBits = fixMax(0,sfbMaxScaleSpec[i]-4);            /* max sfbWidth = 96 ; 2^7=128 => 7/2 = 4 (spc*spc) */
+    int leadingBits = sfbMaxScaleSpec[i]-3;            /* max sfbWidth = 36 ; 2^6=64 => 6/2 = 3 (spc*spc) */
     FIXP_DBL tmp = FL2FXCONST_DBL(0.0);
     for (j=bandOffset[i];j<bandOffset[i+1];j++)
     {
-       FIXP_DBL spec = mdctSpectrum[j]<<leadingBits;
+       FIXP_DBL spec = scaleValue(mdctSpectrum[j],leadingBits);
        tmp = fPow2AddDiv2(tmp, spec);
     }
-    bandEnergy[i] = tmp<<1;
+    bandEnergy[i] = tmp;
   }
 
   for(i=0; i<numBands; i++)
   {
-      INT scale = 2*fixMax(0,sfbMaxScaleSpec[i]-4);            /* max sfbWidth = 96 ; 2^7=128 => 7/2 = 4 (spc*spc) */
-      scale = fixMin(scale,(DFRACT_BITS-1));
-      bandEnergy[i] >>= scale;
+      INT scale = (2*(sfbMaxScaleSpec[i]-3))-1;         /* max sfbWidth = 36 ; 2^6=64 => 6/2 = 3 (spc*spc) */
+      scale = fixMax(fixMin(scale,(DFRACT_BITS-1)),-(DFRACT_BITS-1));
+      bandEnergy[i] = scaleValueSaturate(bandEnergy[i], -scale);
   }
 }
 
@@ -343,7 +343,7 @@
     {
        /* using the minimal scaling of left and right channel can cause very small energies;
        check ldNrg before subtract scaling multiplication: fract*INT we don't need fMult */
-       
+
        int minus = scale*FL2FXCONST_DBL(1.0/64);
 
        if (bandEnergyMidLdData[i] != FL2FXCONST_DBL(-1.0f))
diff --git a/libAACenc/src/line_pe.cpp b/libAACenc/src/line_pe.cpp
index ed5ee7f..7014bcb 100644
--- a/libAACenc/src/line_pe.cpp
+++ b/libAACenc/src/line_pe.cpp
@@ -122,6 +122,8 @@
          avgFormFactorLdData = ((-sfbEnergyLdData[sfbGrp+sfb]>>1) + (CalcLdInt(sfbWidth)>>1))>>1;
          peChanData->sfbNLines[sfbGrp+sfb] =
            (INT)CalcInvLdData( (sfbFormFactorLdData[sfbGrp+sfb] + formFacScaling) + avgFormFactorLdData);
+         /* Make sure sfbNLines is never greater than sfbWidth due to unaccuracies (e.g. sfbEnergyLdData[sfbGrp+sfb] = 0x80000000) */
+         peChanData->sfbNLines[sfbGrp+sfb] = fMin(sfbWidth, peChanData->sfbNLines[sfbGrp+sfb]);
       }
       else {
          peChanData->sfbNLines[sfbGrp+sfb] = 0;
diff --git a/libAACenc/src/tns_param.cpp b/libAACenc/src/tns_param.cpp
deleted file mode 100644
index 3c04c51..0000000
--- a/libAACenc/src/tns_param.cpp
+++ /dev/null
@@ -1,93 +0,0 @@
-
-/* -----------------------------------------------------------------------------------------------------------
-Software License for The Fraunhofer FDK AAC Codec Library for Android
-
-© Copyright  1995 - 2012 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
-  All rights reserved.
-
- 1.    INTRODUCTION
-The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
-the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
-This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
-
-AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
-audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
-independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
-of the MPEG specifications.
-
-Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
-may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
-individually for the purpose of encoding or decoding bit streams in products that are compliant with
-the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
-these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
-software may already be covered under those patent licenses when it is used for those licensed purposes only.
-
-Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
-are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
-applications information and documentation.
-
-2.    COPYRIGHT LICENSE
-
-Redistribution and use in source and binary forms, with or without modification, are permitted without
-payment of copyright license fees provided that you satisfy the following conditions:
-
-You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
-your modifications thereto in source code form.
-
-You must retain the complete text of this software license in the documentation and/or other materials
-provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
-You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
-modifications thereto to recipients of copies in binary form.
-
-The name of Fraunhofer may not be used to endorse or promote products derived from this library without
-prior written permission.
-
-You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
-software or your modifications thereto.
-
-Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
-and the date of any change. For modified versions of the FDK AAC Codec, the term
-"Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
-"Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
-
-3.    NO PATENT LICENSE
-
-NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
-ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
-respect to this software.
-
-You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
-by appropriate patent licenses.
-
-4.    DISCLAIMER
-
-This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
-"AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
-of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
-CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
-including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
-or business interruption, however caused and on any theory of liability, whether in contract, strict
-liability, or tort (including negligence), arising in any way out of the use of this software, even if
-advised of the possibility of such damage.
-
-5.    CONTACT INFORMATION
-
-Fraunhofer Institute for Integrated Circuits IIS
-Attention: Audio and Multimedia Departments - FDK AAC LL
-Am Wolfsmantel 33
-91058 Erlangen, Germany
-
-www.iis.fraunhofer.de/amm
-amm-info@iis.fraunhofer.de
------------------------------------------------------------------------------------------------------------ */
-
-/******************************** MPEG Audio Encoder **************************
-
-   Initial author:       M.Werner
-   contents/description: TNS parameters
-
-******************************************************************************/
-
-#include "tns_param.h"
-
-
diff --git a/libFDK/include/FDK_archdef.h b/libFDK/include/FDK_archdef.h
index 22ec0ed..a963f55 100644
--- a/libFDK/include/FDK_archdef.h
+++ b/libFDK/include/FDK_archdef.h
@@ -155,7 +155,6 @@
 #endif
 
 
-
 /* Define preferred Multiplication type */
 #if defined(FDK_HIGH_PERFORMANCE) && !defined(FDK_HIGH_QUALITY) /* FDK_HIGH_PERFORMANCE */
 
diff --git a/libFDK/include/clz.h b/libFDK/include/clz.h
index 90cdb2b..38c5073 100644
--- a/libFDK/include/clz.h
+++ b/libFDK/include/clz.h
@@ -100,6 +100,9 @@
 #elif defined(__mips__)	/* cppp replaced: elif */
 #include "mips/clz_mips.h"
 
+#elif defined(__x86__)	/* cppp replaced: elif */
+#include "x86/clz_x86.h"
+
 #endif /* all cores */
 
 
diff --git a/libFDK/include/mips/cplx_mul.h b/libFDK/include/mips/cplx_mul.h
index 4e8f26f..fb777ce 100644
--- a/libFDK/include/mips/cplx_mul.h
+++ b/libFDK/include/mips/cplx_mul.h
@@ -107,10 +107,23 @@
                           FIXP_DBL  b_Re,
                           FIXP_DBL  b_Im)
 {
-   INT result;
-   result = (((long long)a_Re * b_Re) - ((long long) a_Im * b_Im)) >> 32;
+  INT result;
+
+   __asm__ ("mult %[a_Re], %[b_Re];\n"
+            "msub %[a_Im], %[b_Im];\n"
+            "mfhi %[result];\n"
+       : [result]"=r"(result)
+       : [a_Re]"d"(a_Re), [b_Re]"d"(b_Re),  [a_Im]"d"(a_Im), [b_Im]"d"(b_Im)
+       : "lo");
+
    *c_Re = result;
-   result = (((long long)a_Re * b_Im) - ((long long) a_Im * b_Re)) >> 32;
+
+   __asm__ ("mult %[a_Re], %[b_Im];\n"
+            "madd %[a_Im], %[b_Re];\n"
+            "mfhi %[result];\n"
+       : [result]"=r"(result) 
+       : [a_Re]"r"(a_Re), [b_Im]"r"(b_Im), [a_Im]"r"(a_Im), [b_Re]"r"(b_Re)
+       : "lo");
    *c_Im = result;
 }
 #endif
@@ -123,10 +136,24 @@
                       FIXP_DBL  b_Re,
                       FIXP_DBL  b_Im)
 {
-   INT result;
-   result = (((long long)a_Re * b_Re) - ((long long) a_Im * b_Im)) >> 32;
+  INT result;
+
+   __asm__ ("mult %[a_Re], %[b_Re];\n"
+            "msub %[a_Im], %[b_Im];\n"
+            "mfhi %[result];\n"
+            //"extr_w %[result], 31;\n"
+        : [result]"=r"(result)
+        : [a_Re]"r"(a_Re), [b_Re]"r"(b_Re), [a_Im]"r"(a_Im), [b_Im]"r"(b_Im)
+        : "lo");
    *c_Re = result<<1;
-   result = (((long long)a_Re * b_Im) - ((long long) a_Im * b_Re)) >> 32;
+
+   __asm__ ("mult %[a_Re], %[b_Im];\n"
+            "madd %[a_Im], %[b_Re];\n"
+            "mfhi %[result];\n"
+            //"extr_w %[result], 31;\n"
+        : [result]"=r"(result)
+        : [a_Re]"r"(a_Re), [b_Im]"r"(b_Im), [a_Im]"r"(a_Im), [b_Re]"r"(b_Re)
+        : "lo");
    *c_Im = result<<1;
 }
 #endif
diff --git a/libFDK/include/mips/fixmul_mips.h b/libFDK/include/mips/fixmul_mips.h
index 0e7af0d..62d059e 100644
--- a/libFDK/include/mips/fixmul_mips.h
+++ b/libFDK/include/mips/fixmul_mips.h
@@ -100,11 +100,14 @@
 
 inline INT fixmuldiv2_DD (const INT a, const INT b)
 {
-
-  return ((long long) a * b) >> 32;
+  INT result ;
+  result = ((long long)a * b)>>32;
+  return result ;
 }
 
 #endif /* (__GNUC__) && defined(__mips__) */
 
 #endif /* __mips__ */
 
+#define FUNCTION_fixmulBitExact_DD
+#define fixmulBitExact_DD fixmul_DD
diff --git a/libAACenc/src/tns_param.h b/libFDK/include/x86/clz_x86.h
similarity index 78%
rename from libAACenc/src/tns_param.h
rename to libFDK/include/x86/clz_x86.h
index b191b5c..db7970d 100644
--- a/libAACenc/src/tns_param.h
+++ b/libFDK/include/x86/clz_x86.h
@@ -81,16 +81,74 @@
 amm-info@iis.fraunhofer.de
 ----------------------------------------------------------------------------------------------------------- */
 
-/******************************** MPEG Audio Encoder **************************
+/***************************  Fraunhofer IIS FDK Tools  **********************
 
-   Initial author:       Alex Goeschel
-   contents/description: Temporal noise shaping
+   Author(s):
+   Description: fixed point intrinsics
 
 ******************************************************************************/
 
-#ifndef _TNS_PARAM_H
-#define _TNS_PARAM_H
+#if defined(__GNUC__) && (defined(__x86__) || defined(__x86_64__))
+
+  #define FUNCTION_fixnormz_D
+  #define FUNCTION_fixnorm_D
+
+  inline INT fixnormz_D(LONG value)
+  {
+    INT result;
+
+    if (value != 0) {
+      result = __builtin_clz(value);
+    } else {
+      result = 32;
+    }
+    return result;
+  }
+
+  inline INT fixnorm_D(LONG value)
+  {
+    INT result;
+    if (value == 0) {
+      return 0;
+    }
+    if (value < 0) {
+      value = ~value;
+    }
+    result =  fixnormz_D(value);
+    return result - 1;
+  }
 
 
+#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
 
-#endif /* _TNS_PARAM_H */
+#include <intrin.h>
+
+  #define FUNCTION_fixnormz_D
+  #define FUNCTION_fixnorm_D
+
+  inline INT fixnormz_D(LONG value)
+  {
+    unsigned long result = 0;
+    unsigned char err;
+    err = _BitScanReverse(&result, value);
+    if (err) {
+      return 31 - result;
+    } else {
+      return 32;
+    }
+  }
+
+  inline INT fixnorm_D(LONG value)
+  {
+    INT result;
+    if (value == 0) {
+      return 0;
+    }
+    if (value < 0) {
+      value = ~value;
+    }
+    result =  fixnormz_D(value);
+    return result - 1;
+  }
+
+#endif /* toolchain */
diff --git a/libFDK/src/FDK_core.cpp b/libFDK/src/FDK_core.cpp
index 98a051f..8a07aa9 100644
--- a/libFDK/src/FDK_core.cpp
+++ b/libFDK/src/FDK_core.cpp
@@ -93,7 +93,7 @@
 /* FDK tools library info */
 #define FDK_TOOLS_LIB_VL0 2
 #define FDK_TOOLS_LIB_VL1 2
-#define FDK_TOOLS_LIB_VL2 7
+#define FDK_TOOLS_LIB_VL2 8
 #define FDK_TOOLS_LIB_TITLE "FDK Tools"
 #define FDK_TOOLS_LIB_BUILD_DATE __DATE__
 #define FDK_TOOLS_LIB_BUILD_TIME __TIME__
diff --git a/libPCMutils/src/pcmutils_lib.cpp b/libPCMutils/src/pcmutils_lib.cpp
index 4272548..e0a9817 100644
--- a/libPCMutils/src/pcmutils_lib.cpp
+++ b/libPCMutils/src/pcmutils_lib.cpp
@@ -96,7 +96,7 @@
 
 /* Decoder library info */
 #define PCMDMX_LIB_VL0 2
-#define PCMDMX_LIB_VL1 2
+#define PCMDMX_LIB_VL1 3
 #define PCMDMX_LIB_VL2 1
 #define PCMDMX_LIB_TITLE "PCM Downmix Lib"
 #define PCMDMX_LIB_BUILD_DATE __DATE__
@@ -107,7 +107,7 @@
 #define PCM_DMX_MAX_CHANNELS            ( 8 )
 #define PCM_DMX_MAX_CHANNEL_GROUPS      ( 4 )
 #define PCM_DMX_MAX_CHANNELS_PER_GROUP  ( 3 )   /* The maximum over all groups */
-#define PCMDMX_DFLT_EXPIRY_FRAME        ( 40 )  /* At least 400ms (FL 960 @ 96kHz) */
+#define PCMDMX_DFLT_EXPIRY_FRAME        ( 50 )  /* At least 500ms (FL 960 @ 96kHz) */
 
 /* Fixed and unique channel group indices.
  * The last group index has to be smaller than PCM_DMX_MAX_CHANNEL_GROUPS. */
@@ -264,7 +264,6 @@
  * @param [in] The total number of channels of the given configuration.
  * @param [in] Array holding the corresponding channel types for each channel.
  * @param [in] Array holding the corresponding channel type indices for each channel.
- * @param [in] Array containing the channel mapping to be used (From MPEG PCE ordering to whatever is required).
  * @param [out] Array where the buffer offsets for each channel are stored into.
  * @returns Returns the packed channel mode.
  **/
@@ -273,7 +272,6 @@
         const INT                numChannels,                           /* in */
         const AUDIO_CHANNEL_TYPE channelType[],                         /* in */
         const UCHAR              channelIndices[],                      /* in */
-        const UCHAR              channelMapping[PCM_DMX_MAX_CHANNELS],  /* in */
         UCHAR                    offsetTable[PCM_DMX_MAX_CHANNELS]      /* out */
       )
 {
@@ -284,12 +282,12 @@
 
   FDK_ASSERT(channelType != NULL);
   FDK_ASSERT(channelIndices != NULL);
-  FDK_ASSERT(channelMapping != NULL);
   FDK_ASSERT(offsetTable != NULL);
 
   /* For details see ISO/IEC 13818-7:2005(E), 8.5.3 Channel configuration */
   FDKmemclear(numChInGrp, PCM_DMX_MAX_CHANNEL_GROUPS*sizeof(UCHAR));
   FDKmemset(offsetTable, 255, PCM_DMX_MAX_CHANNELS*sizeof(UCHAR));
+  FDKmemset(chIdx, 255, PCM_DMX_MAX_CHANNEL_GROUPS*PCM_DMX_MAX_CHANNELS_PER_GROUP*sizeof(UCHAR));
 
   /* Categorize channels */
   for (ch = 0; ch < numChannels; ch += 1) {
@@ -335,7 +333,7 @@
   if (numChInGrp[CH_GROUP_FRONT] & 0x1) {
     /* Odd number of front channels -> we have a center channel.
        In MPEG-4 the center has the index 0. */
-    offsetTable[CENTER_FRONT_CHANNEL] = channelMapping[chIdx[CH_GROUP_FRONT][0]];
+    offsetTable[CENTER_FRONT_CHANNEL] = chIdx[CH_GROUP_FRONT][0];
   }
 
   for (grpIdx = 0; grpIdx < PCM_DMX_MAX_CHANNEL_GROUPS; grpIdx += 1) {
@@ -367,7 +365,7 @@
 
     for ( ; ch < numChInGrp[grpIdx]; ch += 1) {
       if (ch < maxChannels) {
-        offsetTable[chMapPos] = channelMapping[chIdx[grpIdx][ch]];
+        offsetTable[chMapPos] = chIdx[grpIdx][ch];
         chMapPos += 1;
       } else {
         err = -1;
@@ -814,7 +812,6 @@
                    numInChannels,
                    channelType,
                    channelIndices,
-                   channelMapping[numInChannels],
                    inOffsetTable
                  );
   if (inChMode == CH_MODE_UNDEFINED) {
diff --git a/libSBRdec/src/sbr_dec.cpp b/libSBRdec/src/sbr_dec.cpp
index 5a8b320..bd3cd10 100644
--- a/libSBRdec/src/sbr_dec.cpp
+++ b/libSBRdec/src/sbr_dec.cpp
@@ -585,14 +585,16 @@
 
       int maxShift = 0;
 
-      if (hSbrDec->sbrDrcChannel.prevFact_exp > maxShift) {
-        maxShift = hSbrDec->sbrDrcChannel.prevFact_exp;
-      }
-      if (hSbrDec->sbrDrcChannel.currFact_exp > maxShift) {
-        maxShift = hSbrDec->sbrDrcChannel.currFact_exp;
-      }
-      if (hSbrDec->sbrDrcChannel.nextFact_exp > maxShift) {
-        maxShift = hSbrDec->sbrDrcChannel.nextFact_exp;
+      if (hSbrDec->sbrDrcChannel.enable != 0) {
+        if (hSbrDec->sbrDrcChannel.prevFact_exp > maxShift) {
+          maxShift = hSbrDec->sbrDrcChannel.prevFact_exp;
+        }
+        if (hSbrDec->sbrDrcChannel.currFact_exp > maxShift) {
+          maxShift = hSbrDec->sbrDrcChannel.currFact_exp;
+        }
+        if (hSbrDec->sbrDrcChannel.nextFact_exp > maxShift) {
+          maxShift = hSbrDec->sbrDrcChannel.nextFact_exp;
+        }
       }
 
       /* copy DRC data to right channel (with PS both channels use the same DRC gains) */
diff --git a/libSBRdec/src/sbrdec_drc.cpp b/libSBRdec/src/sbrdec_drc.cpp
index ce5247a..7497b66 100644
--- a/libSBRdec/src/sbrdec_drc.cpp
+++ b/libSBRdec/src/sbrdec_drc.cpp
@@ -115,17 +115,17 @@
   }
 
   for (band = 0; band < (64); band++) {
-    hDrcData->prevFact_mag[band] = (FIXP_DBL)MAXVAL_DBL /*FL2FXCONST_DBL(1.0f)*/;
+    hDrcData->prevFact_mag[band] = FL2FXCONST_DBL(0.5f);
   }
 
   for (band = 0; band < SBRDEC_MAX_DRC_BANDS; band++) {
-    hDrcData->currFact_mag[band] = (FIXP_DBL)MAXVAL_DBL /*FL2FXCONST_DBL(1.0f)*/;
-    hDrcData->nextFact_mag[band] = (FIXP_DBL)MAXVAL_DBL /*FL2FXCONST_DBL(1.0f)*/;
+    hDrcData->currFact_mag[band] = FL2FXCONST_DBL(0.5f);
+    hDrcData->nextFact_mag[band] = FL2FXCONST_DBL(0.5f);
   }
 
-  hDrcData->prevFact_exp = 0;
-  hDrcData->currFact_exp = 0;
-  hDrcData->nextFact_exp = 0;
+  hDrcData->prevFact_exp = 1;
+  hDrcData->currFact_exp = 1;
+  hDrcData->nextFact_exp = 1;
 
   hDrcData->numBandsCurr = 0;
   hDrcData->numBandsNext = 0;
@@ -238,7 +238,7 @@
       }
       else {
         if (j >= offset[hDrcData->drcInterpolationSchemeCurr - 1]) {
-          alphaValue = FL2FXCONST_DBL(1.0f);
+          alphaValue = (FIXP_DBL)MAXVAL_DBL;
         }
       }
     }
@@ -262,7 +262,7 @@
       }
       else {
         if (j >= offset[hDrcData->drcInterpolationSchemeNext - 1]) {
-          alphaValue = FL2FXCONST_DBL(1.0f);
+          alphaValue = (FIXP_DBL)MAXVAL_DBL;
         }
       }
 
@@ -301,7 +301,7 @@
       }
       else {
         if (j >= offset[hDrcData->drcInterpolationSchemeNext - 1]) {
-          alphaValue = FL2FXCONST_DBL(1.0f);
+          alphaValue = (FIXP_DBL)MAXVAL_DBL;
         }
       }
     }
@@ -322,7 +322,7 @@
   for (band = 0; band < (int)numBands; band++) {
     int bottomQmf, topQmf;
 
-    FIXP_DBL drcFact_mag = FL2FXCONST_DBL(1.0f);
+    FIXP_DBL drcFact_mag = (FIXP_DBL)MAXVAL_DBL;
 
     topMdct = (bandTop[band]+1) << 2;
 
@@ -361,7 +361,13 @@
         }
 
         /* interpolate */
-        drcFact_mag = fMult(alphaValue, drcFact2_mag) + fMult((FL2FXCONST_DBL(1.0f) - alphaValue), drcFact1_mag);
+        if (alphaValue == (FIXP_DBL)0) {
+          drcFact_mag = drcFact1_mag;
+        } else if (alphaValue == (FIXP_DBL)MAXVAL_DBL) {
+          drcFact_mag = drcFact2_mag;
+        } else {
+          drcFact_mag = fMult(alphaValue, drcFact2_mag) + fMult(((FIXP_DBL)MAXVAL_DBL - alphaValue), drcFact1_mag);
+        }
 
         /* apply scaling */
         qmfRealSlot[bin] = fMult(qmfRealSlot[bin], drcFact_mag);
@@ -481,6 +487,15 @@
   int col;
   int maxShift = 0;
 
+  if (hDrcData == NULL) {
+    return;
+  }
+  if ( (hDrcData->enable == 0)
+    || ((hDrcData->numBandsCurr == 0) && (hDrcData->numBandsNext == 0))
+     ) {
+    return;  /* Avoid changing the scaleFactor even though the processing is disabled. */
+  }
+
   /* get max scale factor */
   if (hDrcData->prevFact_exp > maxShift) {
     maxShift = hDrcData->prevFact_exp;
diff --git a/libSBRdec/src/sbrdec_drc.h b/libSBRdec/src/sbrdec_drc.h
index 2577e89..14deff7 100644
--- a/libSBRdec/src/sbrdec_drc.h
+++ b/libSBRdec/src/sbrdec_drc.h
@@ -94,6 +94,7 @@
 #include "sbrdecoder.h"
 
 
+
 #define SBRDEC_MAX_DRC_CHANNELS  (6)
 #define SBRDEC_MAX_DRC_BANDS     ( 16 )
 
diff --git a/libSBRdec/src/sbrdecoder.cpp b/libSBRdec/src/sbrdecoder.cpp
index a40e5ba..192bdd2 100644
--- a/libSBRdec/src/sbrdecoder.cpp
+++ b/libSBRdec/src/sbrdecoder.cpp
@@ -137,7 +137,7 @@
 /* Decoder library info */
 #define SBRDECODER_LIB_VL0 2
 #define SBRDECODER_LIB_VL1 1
-#define SBRDECODER_LIB_VL2 2
+#define SBRDECODER_LIB_VL2 3
 #define SBRDECODER_LIB_TITLE "SBR Decoder"
 #define SBRDECODER_LIB_BUILD_DATE __DATE__
 #define SBRDECODER_LIB_BUILD_TIME __TIME__
@@ -533,7 +533,7 @@
   FDKmemclear(self->pSbrElement[elementIndex]->frameErrorFlag, ((1)+1)*sizeof(UCHAR));
 
   /* Initialize this instance */
-  sbrError = sbrDecoder_ResetElement( 
+  sbrError = sbrDecoder_ResetElement(
           self,
           sampleRateIn,
           sampleRateOut,
@@ -577,7 +577,7 @@
         )
 {
   SBR_ERROR errorStatus = SBRDEC_OK;
- 
+
   /*
     change of control data, reset decoder
   */
@@ -818,6 +818,7 @@
                                       USHORT            *pBandTop )
 {
   SBRDEC_DRC_CHANNEL *pSbrDrcChannelData = NULL;
+  int band, isValidData = 0;
 
   if (self == NULL) {
     return SBRDEC_NOT_INITIALIZED;
@@ -826,10 +827,21 @@
     return SBRDEC_SET_PARAM_FAIL;
   }
 
+  /* Search for gain values different to 1.0f */
+  for (band = 0; band < numBands; band += 1) {
+    if ( !((pNextFact_mag[band] == FL2FXCONST_DBL(0.5))  && (nextFact_exp == 1))
+      && !((pNextFact_mag[band] == (FIXP_DBL)MAXVAL_DBL) && (nextFact_exp == 0)) ) {
+      isValidData = 1;
+      break;
+    }
+  }
+
   /* Find the right SBR channel */
   pSbrDrcChannelData = sbrDecoder_drcGetChannel( self, ch );
 
   if ( pSbrDrcChannelData != NULL ) {
+    if ( pSbrDrcChannelData->enable || isValidData )
+  { /* Activate processing only with real and valid data */
     int i;
 
     pSbrDrcChannelData->enable   = 1;
@@ -844,6 +856,7 @@
       pSbrDrcChannelData->nextFact_mag[i] = pNextFact_mag[i];
     }
   }
+  }
 
   return SBRDEC_OK;
 }