LCOV - code coverage report
Current view: top level - lib_enc - igf_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main @ e95243e9e67ddeb69dddf129509de1b3d95b402e Lines: 1290 1329 97.1 %
Date: 2025-09-14 03:13:15 Functions: 18 19 94.7 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #include <math.h>
      41             : #include "prot_fx.h"
      42             : #include "cnst.h"
      43             : #include "stat_enc.h"
      44             : #include "wmc_auto.h"
      45             : #include "prot_fx_enc.h"
      46             : 
      47             : 
      48             : #define INV_Log2_10_Q15 9864  /*1/log2(10) in Q15*/
      49             : #define INV_Log2_10_Q12 1233  /*1/log2(10) in Q12*/
      50             : #define INV_Log2_e_Q15  22713 /*1/log2(e) in Q15*/
      51             : /*-------------------------------------------------------------------*
      52             :  * IGF_write_bit_fx()
      53             :  *
      54             :  * write single bit to stream
      55             :  *-------------------------------------------------------------------*/
      56     7528490 : static void IGF_write_bit_fx(
      57             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
      58             :     Word16 *bitCount,      /* i/o: bit counter              */
      59             :     const Word16 value     /* i  : value                    */
      60             : )
      61             : {
      62     7528490 :     IF( hBstr )
      63             :     {
      64     3810248 :         push_next_indice( hBstr, value, 1 );
      65             :     }
      66             : 
      67     7528490 :     ( *bitCount ) = add( ( *bitCount ), 1 );
      68     7528490 :     move16();
      69             : 
      70     7528490 :     return;
      71             : }
      72             : 
      73             : /*-------------------------------------------------------------------*
      74             :  * IGF_write_bits()
      75             :  *
      76             :  * write bits to stream
      77             :  *-------------------------------------------------------------------*/
      78     6324620 : static void IGF_write_bits(
      79             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle */
      80             :     Word16 *bitCount,      /* i/o: bit counter              */
      81             :     const Word16 value,    /* i/o: value                    */
      82             :     Word16 bits            /* i  : number of bits           */
      83             : )
      84             : {
      85    12649240 :     WHILE( bits-- )
      86             :     {
      87     6324620 :         IF( s_and( value, shl( 1, bits ) ) == 0 )
      88             :         {
      89     2005671 :             IGF_write_bit_fx( hBstr, bitCount, 0 );
      90             :         }
      91             :         ELSE
      92             :         {
      93     4318949 :             IGF_write_bit_fx( hBstr, bitCount, 1 );
      94             :         }
      95             :     }
      96             : 
      97     6324620 :     return;
      98             : }
      99             : 
     100             : 
     101             : /*-------------------------------------------------------------------*
     102             :  * IGF_getCrest_new()
     103             :  *
     104             :  * crest factor calculation
     105             :  *-------------------------------------------------------------------*/
     106             : 
     107             : /*! r: crest factor */
     108     5565406 : static Word16 IGF_getCrest_new_fx(
     109             :     const Word16 *logSpec, /* i  : power spectrum           */
     110             :     const Word16 start,    /* i  : start subband index      */
     111             :     const Word16 stop,     /* i  : stop subband index       */
     112             :     Word16 *crest_exp      /*Stores the exponent of the result(return value)*/
     113             : )
     114             : {
     115             :     Word16 i;
     116             :     Word16 x;
     117             :     Word32 x_eff;
     118             :     Word16 x_max;
     119             :     Word16 exp;
     120             :     Word16 temp;
     121             :     Word16 temp_e;
     122             :     Word16 crest; /*1.0f in Q15*/
     123     5565406 :     x_eff = 0;
     124     5565406 :     x_max = 0;
     125     5565406 :     exp = 0;
     126     5565406 :     temp = 0;
     127     5565406 :     crest = 32767; /*1.0f in Q15*/
     128     5565406 :     move32();
     129     5565406 :     move16();
     130     5565406 :     move16();
     131     5565406 :     move16();
     132     5565406 :     move16();
     133             : 
     134   382473456 :     FOR( i = start; i < stop; i++ )
     135             :     {
     136   376908050 :         x = logSpec[i];
     137   376908050 :         move16();
     138   376908050 :         x_eff = L_mac0( x_eff, x, x );
     139             : 
     140   376908050 :         if ( GT_16( x, x_max ) )
     141             :         {
     142    17260350 :             x_max = x;
     143    17260350 :             move16();
     144             :         }
     145             :     }
     146             : 
     147     5565406 :     x_eff = BASOP_Util_Divide3216_Scale( x_eff, sub( stop, start ), &temp_e );
     148     5565406 :     temp_e = add( temp_e, 16 ); /*exp += 31 - 15 + 16(because x_eff is word32)*/
     149     5565406 :     x_eff = L_shr( x_eff, sub( 15, temp_e ) );
     150     5565406 :     temp_e = 15;
     151     5565406 :     move16();
     152     5565406 :     temp = Sqrt16( extract_l( x_eff ), &temp_e );
     153             : 
     154     5565406 :     test();
     155     5565406 :     IF( x_eff > 0 && x_max > 0 )
     156             :     {
     157     4935962 :         temp = BASOP_Util_Divide1616_Scale( x_max, temp, &exp );
     158     4935962 :         exp = add( exp, sub( 15, temp_e ) );
     159     4935962 :         IF( exp < 0 )
     160             :         {
     161           0 :             temp = shl( temp, exp );
     162           0 :             exp = 0;
     163           0 :             move16();
     164             :         }
     165     4935962 :         crest = s_max( shl_sat( 1, sub( 15, exp ) ), temp );
     166             :     }
     167     5565406 :     *crest_exp = exp;
     168     5565406 :     move16();
     169     5565406 :     return crest;
     170             : }
     171             : 
     172             : 
     173             : /*-------------------------------------------------------------------*
     174             :  * IGF_getSFM_new()
     175             :  *
     176             :  * calculates spectral flatness measurement
     177             :  *-------------------------------------------------------------------*/
     178             : 
     179             : /*! r: SFM value */
     180     5565406 : static Word16 IGF_getSFM_new_fx(
     181             :     const Word32 *powerSpectrum, /* i  : power spectrum            */
     182             :     const Word16 *logSpec,       /* i  : log of power spectrum     */
     183             :     const Word16 start,          /* i  : start subband index       */
     184             :     const Word16 stop,           /* i  : stop subband index        */
     185             :     Word16 *e_ps                 /*Stores exp related to power spectrum*/
     186             : )
     187             : {
     188             :     Word16 n;
     189             :     Word16 i;
     190             :     Word16 num;
     191             :     Word32 denom;
     192             :     Word16 numf;
     193             :     Word32 tmp;
     194             :     Word16 sfm;
     195             :     Word16 sfm_e;
     196             :     Word16 denom_e;
     197             :     Word16 numf_e; /*stores exponent for numf*/
     198             :     Word16 tmp_e;
     199             : 
     200     5565406 :     num = 0;
     201     5565406 :     denom = ONE_IN_Q30;
     202     5565406 :     denom_e = 1;
     203     5565406 :     sfm = MAX16B; // Q15
     204     5565406 :     move16();
     205     5565406 :     move32();
     206     5565406 :     move16();
     207     5565406 :     move16();
     208             : 
     209   382473456 :     FOR( i = start; i < stop; i++ )
     210             :     {
     211   376908050 :         tmp = powerSpectrum[i];
     212   376908050 :         n = logSpec[i /*-start*/];
     213   376908050 :         move32();
     214   376908050 :         move16();
     215   376908050 :         num = add( num, n );
     216   376908050 :         denom = BASOP_Util_Add_Mant32Exp( tmp, e_ps[i], denom, denom_e, &denom_e );
     217             :     }
     218             : 
     219     5565406 :     numf = BASOP_Util_Divide1616_Scale( num, sub( stop, start ), &numf_e );
     220     5565406 :     denom = BASOP_Util_Divide3216_Scale( denom, sub( stop, start ), &tmp_e );
     221     5565406 :     denom_e = add( add( denom_e, tmp_e ), 1 ); /*denom_e+tmp_e-15 +16(because type of denom is word32)*/
     222             : 
     223     5565406 :     IF( denom != 0 )
     224             :     {
     225     5565406 :         tmp = BASOP_util_Pow2( L_add( numf, shl_sat( 1, sub( 14, numf_e ) ) ), add( 16, numf_e ), &tmp_e );
     226     5565406 :         sfm = BASOP_Util_Divide3232_Scale( tmp, denom, &sfm_e );
     227     5565406 :         sfm_e = add( sfm_e, sub( tmp_e, denom_e ) );
     228     5565406 :         sfm = shl_sat( extract_l( L_min( sfm, L_shl_sat( 1, sub( 15, sfm_e ) ) ) ), sfm_e );
     229             :     }
     230             : 
     231     5565406 :     return sfm;
     232             : }
     233             : /*-------------------------------------------------------------------*
     234             :  * IGF_getTilt()
     235             :  *
     236             :  * calculates spectral tilt
     237             :  *-------------------------------------------------------------------*/
     238             : 
     239             : /*! r: spectral tilt value */
     240             : 
     241             : /*-------------------------------------------------------------------*
     242             :  * IGF_getTNR()
     243             :  *
     244             :  * calculates tonal-to-noise ratio
     245             :  *-------------------------------------------------------------------*/
     246             : 
     247             : /*! r: spectral tilt value */
     248             : /* Returns value with exponent as 9 and Q as 22*/
     249             : 
     250       13406 : static Word32 IGF_getTNR_ivas_fx(
     251             :     const Word32 *powerSpectrum, /* i  : energies               */
     252             :     const Word16 start,          /* i  : start subband index    */
     253             :     const Word16 stop,           /* i  : stop subband index     */
     254             :     const Word16 adap,           /* i  : SFB width adaptation   */
     255             :     Word16 *e_ps,                /*Stores exponent for powerSpectrum*/
     256             :     Word16 e_adap                /*Stores exponent for adap*/
     257             : )
     258             : {
     259             :     Word16 i;
     260             :     Word16 width;
     261             :     Word32 avg;
     262             :     Word32 tonal;
     263             :     Word16 tonal_e; /* holds exp for tonal*/
     264             :     Word32 noise;
     265             :     Word16 noise_e; /* holds exp for noise*/
     266             :     Word32 tonalToNoise;
     267             :     Word32 rootSpec[300];
     268             :     Word16 rootSpec_e[300]; /*rootSpec_e[i] holds exp for rootSpec[i]*/
     269             :     Word16 avg_e;           /* holds exp for avg*/
     270             :     Word16 tmp_e;
     271       13406 :     avg = 0;
     272       13406 :     tonal = 0;
     273       13406 :     noise = EPSILON_FX;
     274       13406 :     tonal_e = 0;
     275       13406 :     noise_e = 0;
     276       13406 :     avg_e = 0;
     277       13406 :     tmp_e = 0;
     278       13406 :     move32();
     279       13406 :     move32();
     280       13406 :     move32();
     281       13406 :     move16();
     282       13406 :     move16();
     283       13406 :     move16();
     284       13406 :     move16();
     285             : 
     286       13406 :     set32_fx( rootSpec, 0, 300 );
     287       13406 :     set16_fx( rootSpec_e, 0, 300 );
     288             : 
     289       13406 :     width = sub( stop, start );
     290      823024 :     FOR( i = start; i < stop; i++ )
     291             :     {
     292      809618 :         rootSpec_e[( i - start )] = e_ps[i];
     293      809618 :         move16();
     294      809618 :         rootSpec[( i - start )] = Sqrt32( powerSpectrum[i], &rootSpec_e[( i - start )] ); /*rootSpec[i - start] = sqrtf( powerSpectrum[i] );*/
     295      809618 :         move32();
     296      809618 :         avg = BASOP_Util_Add_Mant32Exp( avg, avg_e, rootSpec[( i - start )], rootSpec_e[( i - start )], &avg_e ); /*avg += rootSpec[i - start];resultant exponent is avg_e*/
     297             :     }
     298       13406 :     avg = BASOP_Util_Divide3216_Scale( avg, width, &tmp_e ); /*avg /= width;*/
     299       13406 :     avg_e = add( 16, sub( add( avg_e, tmp_e ), 15 ) );
     300             : 
     301      823024 :     FOR( i = start; i < stop; i++ )
     302             :     {
     303             :         Word16 normSpec_e;                                                                      /*stores resultant exponent for normSpec*/
     304      809618 :         Word16 normSpec = BASOP_Util_Divide3232_Scale( rootSpec[i - start], avg, &normSpec_e ); /*rootSpec[i - start] / avg;*/
     305      809618 :         normSpec_e = add( normSpec_e, sub( rootSpec_e[i - start], avg_e ) );
     306      809618 :         IF( GT_32( normSpec, L_add_sat( L_shl_sat( 1, sub( 15, normSpec_e ) ), L_shl_sat( adap, sub( e_adap, normSpec_e ) ) ) ) )
     307             :         {
     308       65055 :             tonal = BASOP_Util_Add_Mant32Exp( tonal, tonal_e, rootSpec[( i - start )], rootSpec_e[( i - start )], &tonal_e ); /*tonal += rootSpec[i - start];*/
     309             :         }
     310      744563 :         ELSE IF( LT_32( normSpec, L_shl_sat( 1, sub( 15, normSpec_e ) ) ) )
     311             :         {
     312      659709 :             noise = BASOP_Util_Add_Mant32Exp( noise, noise_e, rootSpec[( i - start )], rootSpec_e[( i - start )], &noise_e ); /*noise += rootSpec[i - start];*/
     313             :         }
     314             :     }
     315             : 
     316             :     /*tonalToNoise = 20.f * log10f( max( 1e-018f, tonal / noise ) )*/
     317       13406 :     IF( noise == 0 ) // To handle condition if denom = 0
     318             :     {
     319           0 :         tonalToNoise = imult3216( L_shr( L_add( L_shl( 18 /* log10f(1e-018f) */, Q25 ), Mpy_32_16_1( L_add( BASOP_Util_Log2( tonal ), L_shl( tonal_e, Q25 ) ) /*Q25*/, INV_Log2_10_Q15 ) /*25+15-15*/ ), 3 ) /*Q22*/, 20 );
     320             :     }
     321             :     ELSE
     322             :     {
     323       13406 :         Word16 temp = BASOP_Util_Divide3232_Scale( tonal, noise, &tmp_e ); /*tonal / noise*/
     324       13406 :         tmp_e = add( tmp_e, sub( tonal_e, noise_e ) );
     325       13406 :         IF( GE_16( temp, 1 ) )
     326             :         {
     327       13406 :             tonalToNoise = imult3216( Mult_32_16( L_add( BASOP_Util_Log2( temp ), L_shl( add( 16, tmp_e ), Q25 ) ) /*Q25*/, INV_Log2_10_Q12 ) /*25+12-15*/, 20 ); /*Q22*/
     328             :         }
     329             :         ELSE
     330             :         {
     331           0 :             tonalToNoise = -1509949440; /*-360.f Q22*/
     332           0 :             move32();
     333             :         }
     334             :     }
     335             : 
     336       13406 :     return tonalToNoise;
     337             : }
     338             : 
     339             : /*-------------------------------------------------------------------*
     340             :  * IGF_CalculateEnvelope()
     341             :  *
     342             :  * envelope estimation
     343             :  *-------------------------------------------------------------------*/
     344             : 
     345      510753 : static void IGF_CalculateEnvelope_ivas_fx(
     346             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i  : instance handle of IGF Encoder                  */
     347             :     Word32 *pMDCTSpectrum_fx,              /* i  : MDCT spectrum                                   */
     348             :     Word16 e_mdct,                         /* i  : exp of MDCT spectrum                  */
     349             :     Word32 *pPowerSpectrum_fx,             /* i  : MDCT^2 + MDST^2 spectrum, or estimate           */
     350             :     Word16 *e_ps,                          /* i  : exp of power spectrum                 */
     351             :     const Word16 igfGridIdx,               /* i  : IGF grid index                                  */
     352             :     const Word16 isTransient,              /* i  : flag indicating if transient is detected        */
     353             :     const Word16 last_core_acelp,          /* i  : indicator if last frame was ACELP core          */
     354             :     const Word16 element_mode,             /* i  : IVAS element_mode                               */
     355             :     const Word16 att                       /* i  : attenuation                                     */
     356             : )
     357             : {
     358             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
     359             :     H_IGF_GRID hGrid;
     360             :     Word16 *swb_offset;
     361             :     Word16 sfb;   /* this is the actual scalefactor band */
     362             :     Word16 width; /* this is width in subbands of the actual scalefactor band */
     363             :     Word16 tile_idx;
     364             :     Word16 strt_cpy;
     365             :     Word32 gain; /* the gain which has to be applied to the source tile to get the destination energy */
     366             :     Word16 sb;
     367             :     Word32 sfbEnergyR;
     368             :     Word32 sfbEnergyC; /* the energy of the destination region of the tile */
     369             :     Word32 sfbEnergyTileR;
     370             :     Word32 sfbEnergyTileC; /* the energy of the destination region of the tile */
     371             :     Word16 tmp, x, y;
     372             :     Word16 mean_x_fx, mean_y_fx;
     373             :     Word32 mean_xy_fx, mean_x2_fx;
     374             :     Word16 tmp_tb;
     375             :     Word16 tmp_sb;
     376             :     Word16 sfbCnt;
     377             :     Word16 sfm;
     378             :     Word16 crest;
     379             :     Word16 temp;
     380             :     Word16 mean_x_e, mean_y_e;   /*Stores exponent for mean_x and mean_y respectively*/
     381             :     Word16 mean_xy_e, mean_x2_e; /*stores exponent for mean_xy and mean_x2 respectively*/
     382             :     Word16 sfbEnergyTileR_e;     /*Exponent for sfbEnergyTileR*/
     383             :     Word16 sfbEnergyTileC_e;     /*Exponent for sfbEnergyTileC*/
     384             :     Word16 sfbEnergyC_e;         /*Exponent for sfbEnergyC*/
     385             :     Word16 sfbEnergyR_e;
     386             :     Word16 gain_e;    /*exponent for gain*/
     387             :     Word16 tmp_tb_e;  /*Stores exponent for tmp_tb*/
     388             :     Word16 tmp_sb_e;  /*stores exponent for tmp_sb*/
     389             :     Word16 crest_exp; /*Stores the exponent of the result(return value)*/
     390             :     Word16 sfm_exp;   /*stores exponent for ouput from sfm*/
     391             :     Word16 tmp_e;
     392             : 
     393      510753 :     hPrivateData = &hIGFEnc->igfData;
     394      510753 :     hGrid = &hPrivateData->igfInfo.grid[(Word16) igfGridIdx];
     395      510753 :     swb_offset = hGrid->swb_offset;
     396             : 
     397      510753 :     IF( element_mode > EVS_MONO )
     398             :     {
     399      510753 :         IF( igfGridIdx != IGF_GRID_LB_NORM )
     400             :         {
     401      166988 :             FOR( sfbCnt = 0; sfbCnt < sub( hGrid->sfbWrap[hGrid->nTiles], hGrid->sfbWrap[0] ); sfbCnt++ )
     402             :             {
     403             :                 /* reset filter */
     404      139346 :                 hPrivateData->prevSFM_FIR_SFB_TB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_FIR_TB_e[sfbCnt]
     405      139346 :                 hPrivateData->prevSFB_FIR_TB_e[sfbCnt] = 0;
     406      139346 :                 hPrivateData->prevSFM_IIR_SFB_TB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_IIR_TB_e[sfbCnt]
     407      139346 :                 hPrivateData->prevSFB_IIR_TB_e[sfbCnt] = 0;
     408      139346 :                 hPrivateData->prevSFM_FIR_SFB_SB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_FIR_SB_e[sfbCnt]
     409      139346 :                 hPrivateData->prevSFB_FIR_SB_e[sfbCnt] = 0;
     410      139346 :                 hPrivateData->prevSFM_IIR_SFB_SB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_IIR_SB_e[sfbCnt]
     411      139346 :                 hPrivateData->prevSFB_IIR_SB_e[sfbCnt] = 0;
     412      139346 :                 hPrivateData->prevDampingFactor_IIR_fx[sfbCnt] = MIN16B; /* -1.f in Q15*/
     413      139346 :                 hPrivateData->prevDampingFactor_IIR_e[sfbCnt] = 0;
     414      139346 :                 hPrivateData->dampingFactorSmoothing[sfbCnt] = 2;
     415      139346 :                 move16();
     416      139346 :                 move16();
     417      139346 :                 move16();
     418      139346 :                 move16();
     419      139346 :                 move16();
     420      139346 :                 move16();
     421      139346 :                 move16();
     422      139346 :                 move16();
     423      139346 :                 move16();
     424      139346 :                 move16();
     425      139346 :                 move16();
     426             :             }
     427             :         }
     428             :     }
     429             : 
     430      510753 :     IF( pPowerSpectrum_fx )
     431             :     {
     432   235101562 :         FOR( sb = hGrid->sbWrap[0]; sb < swb_offset[hGrid->sfbWrap[hGrid->nTiles]]; sb++ )
     433             :         {
     434   234654050 :             IF( LT_32( 1, pPowerSpectrum_fx[sb] ) )
     435             :             {
     436   234112967 :                 hPrivateData->logSpec[sb] = s_max( 0, extract_l( W_extract_l( W_shr( W_add( BASOP_Util_Log2( pPowerSpectrum_fx[sb] ), W_shl( e_ps[sb], Q25 ) ), Q25 ) ) ) );
     437   234112967 :                 move16();
     438             :             }
     439             :             ELSE
     440             :             {
     441      541083 :                 hPrivateData->logSpec[sb] = 0;
     442      541083 :                 move16();
     443             :             }
     444             :         }
     445             :     }
     446             : 
     447     2265519 :     FOR( tile_idx = 0; tile_idx < hGrid->nTiles; tile_idx++ )
     448             :     {
     449     1754766 :         strt_cpy = hGrid->sbWrap[tile_idx];
     450     1754766 :         move16();
     451             : 
     452     4441520 :         FOR( sfb = hGrid->sfbWrap[tile_idx]; sfb < hGrid->sfbWrap[tile_idx + 1]; sfb++ )
     453             :         {
     454     2686754 :             width = sub( swb_offset[sfb + 1], swb_offset[sfb] );
     455     2686754 :             sfbEnergyTileR = EPSILON_FIX;
     456     2686754 :             sfbEnergyTileC = EPSILON_FIX;
     457     2686754 :             sfbEnergyC = EPSILON_FX;
     458     2686754 :             sfbEnergyTileR_e = 0;
     459     2686754 :             sfbEnergyTileC_e = 0;
     460     2686754 :             sfbEnergyC_e = 0;
     461     2686754 :             move16();
     462     2686754 :             move16();
     463     2686754 :             move16();
     464     2686754 :             move16();
     465     2686754 :             move16();
     466     2686754 :             move16();
     467             : 
     468     2686754 :             IF( pPowerSpectrum_fx != NULL )
     469             :             {
     470     2399053 :                 tmp = strt_cpy;
     471     2399053 :                 move16();
     472   138962961 :                 FOR( sb = swb_offset[sfb]; sb < swb_offset[sfb + 1]; sb++ )
     473             :                 {
     474   136563908 :                     Word16 shift = norm_l( pPowerSpectrum_fx[sb] );
     475   136563908 :                     sfbEnergyC = BASOP_Util_Add_Mant32Exp( sfbEnergyC, sfbEnergyC_e, L_shl( pPowerSpectrum_fx[sb], shift ), sub( e_ps[sb], shift ), &sfbEnergyC_e );
     476             :                     // sfbEnergyTileR = BASOP_Util_Add_Mant32Exp( sfbEnergyTileR, sfbEnergyTileR_e, Mult_32_32( pMDCTSpectrum_fx[strt_cpy], pMDCTSpectrum_fx[strt_cpy] ), shl( e_mdct, 1 ), &sfbEnergyTileR_e );
     477   136563908 :                     Word64 tmp64 = W_mult_32_32( pMDCTSpectrum_fx[strt_cpy], pMDCTSpectrum_fx[strt_cpy] );
     478   136563908 :                     Word16 tmp64_e = W_norm( tmp64 );
     479   136563908 :                     tmp64 = W_shl( tmp64, tmp64_e );
     480             : 
     481   136563908 :                     sfbEnergyTileR = BASOP_Util_Add_Mant32Exp( sfbEnergyTileR, sfbEnergyTileR_e, W_extract_h( tmp64 ), shl( e_mdct, 1 ) - tmp64_e, &sfbEnergyTileR_e );
     482   136563908 :                     shift = norm_l( pPowerSpectrum_fx[strt_cpy] );
     483   136563908 :                     sfbEnergyTileC = BASOP_Util_Add_Mant32Exp( sfbEnergyTileC, sfbEnergyTileC_e, L_shl( pPowerSpectrum_fx[strt_cpy], shift ), sub( e_ps[strt_cpy], shift ), &sfbEnergyTileC_e );
     484             : 
     485   136563908 :                     strt_cpy = add( strt_cpy, 1 );
     486             :                 }
     487             : 
     488     2399053 :                 sfbEnergyTileR = L_deposit_h( BASOP_Util_Divide3232_Scale( sfbEnergyTileR, width, &tmp_e ) );
     489     2399053 :                 sfbEnergyTileR_e = add( sub( sfbEnergyTileR_e, Q31 ), tmp_e );
     490             : 
     491     2399053 :                 IF( sfbEnergyTileR == 0 )
     492             :                 {
     493         787 :                     sfbEnergyTileR = EPSILON_FX;
     494         787 :                     sfbEnergyTileR_e = 0;
     495         787 :                     move32();
     496         787 :                     move16();
     497             :                 }
     498     2399053 :                 IF( sfbEnergyC == 0 )
     499             :                 {
     500         787 :                     sfbEnergyC = EPSILON_FX;
     501         787 :                     sfbEnergyC_e = 0;
     502         787 :                     move32();
     503         787 :                     move16();
     504             :                 }
     505     2399053 :                 IF( sfbEnergyTileC == 0 )
     506             :                 {
     507         787 :                     sfbEnergyTileC = EPSILON_FX;
     508         787 :                     sfbEnergyTileC_e = 0;
     509         787 :                     temp = BASOP_Util_Divide3232_Scale( sfbEnergyC, sfbEnergyTileC, &tmp_e );
     510         787 :                     tmp_e = add( tmp_e, sub( sfbEnergyC_e, sfbEnergyTileC_e ) );
     511         787 :                     move32();
     512         787 :                     move16();
     513             :                 }
     514             :                 ELSE
     515             :                 {
     516             :                     /*gain = (float) ( sfbEnergyTileR * ( sfbEnergyC / sfbEnergyTileC ) );*/
     517     2398266 :                     temp = BASOP_Util_Divide3232_Scale( sfbEnergyC, sfbEnergyTileC, &tmp_e );
     518     2398266 :                     tmp_e = add( tmp_e, sub( sfbEnergyC_e, sfbEnergyTileC_e ) );
     519             :                 }
     520             : 
     521     2399053 :                 gain = Mult_32_16( sfbEnergyTileR, temp ); // gain_e
     522     2399053 :                 gain_e = add( tmp_e, sfbEnergyTileR_e );
     523             : 
     524     2399053 :                 IF( element_mode > EVS_MONO )
     525             :                 {
     526     2399053 :                     test();
     527     2399053 :                     IF( !isTransient )
     528             :                     {
     529             :                         Word16 diffSFM;
     530     2392368 :                         Word16 shiftedSFM = 0;
     531     2392368 :                         Word16 shiftedSFM_e = 0;
     532     2392368 :                         move16();
     533     2392368 :                         move16();
     534             : 
     535             :                         // tmp_tb = IGF_getSFM_new( pPowerSpectrum, hPrivateData->logSpec, swb_offset[sfb], swb_offset[sfb + 1] ) / IGF_getCrest_new( hPrivateData->logSpec, swb_offset[sfb], swb_offset[sfb + 1] );
     536     2392368 :                         sfm = IGF_getSFM_new_fx( pPowerSpectrum_fx, hPrivateData->logSpec, swb_offset[sfb], swb_offset[sfb + 1], e_ps );
     537     2392368 :                         sfm_exp = 0;
     538     2392368 :                         move16();
     539     2392368 :                         crest = IGF_getCrest_new_fx( hPrivateData->logSpec, swb_offset[sfb], swb_offset[sfb + 1], &crest_exp );
     540     2392368 :                         tmp_tb = BASOP_Util_Divide1616_Scale( sfm, crest, &tmp_e ); // tmp_tb_e
     541     2392368 :                         tmp_tb_e = add( tmp_e, sub( sfm_exp, crest_exp ) );
     542             : 
     543             :                         // tmp_sb = IGF_getSFM_new( pPowerSpectrum, hPrivateData->logSpec, tmp, strt_cpy ) / IGF_getCrest_new( hPrivateData->logSpec, tmp, strt_cpy );
     544     2392368 :                         sfm = IGF_getSFM_new_fx( pPowerSpectrum_fx, hPrivateData->logSpec, tmp, strt_cpy, e_ps );
     545     2392368 :                         crest = IGF_getCrest_new_fx( hPrivateData->logSpec, tmp, strt_cpy, &crest_exp );
     546     2392368 :                         tmp_sb = BASOP_Util_Divide1616_Scale( sfm, crest, &tmp_e ); // tmp_sb_e
     547     2392368 :                         tmp_sb_e = add( tmp_e, sub( sfm_exp, crest_exp ) );
     548             : 
     549     2392368 :                         test();
     550     2392368 :                         IF( last_core_acelp || hPrivateData->wasTransient )
     551             :                         {
     552       91115 :                             hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = tmp_tb; /*Exponent for hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] : hPrivateData->prevSFB_FIR_TB_e[sfb] and hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] : hPrivateData->prevSFB_IIR_TB_e[sfb] */
     553       91115 :                             hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = tmp_sb; /*Exponent for hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] : hPrivateData->prevSFB_FIR_SB_e[sfb] and hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] : hPrivateData->prevSFB_IIR_SB_e[sfb] */
     554       91115 :                             hPrivateData->prevSFB_FIR_TB_e[sfb] = hPrivateData->prevSFB_IIR_TB_e[sfb] = tmp_tb_e;
     555       91115 :                             hPrivateData->prevSFB_FIR_SB_e[sfb] = hPrivateData->prevSFB_IIR_SB_e[sfb] = tmp_sb_e;
     556       91115 :                             move16();
     557       91115 :                             move16();
     558       91115 :                             move16();
     559       91115 :                             move16();
     560       91115 :                             move16();
     561       91115 :                             move16();
     562       91115 :                             move16();
     563       91115 :                             move16();
     564             :                         }
     565             : 
     566     2392368 :                         tmp_tb = shr( tmp_tb, 2 ); /*taking 2 guard bits so it's exponent tmp_sb_e=+2*/
     567     2392368 :                         tmp_sb = shr( tmp_sb, 2 ); /*taking 2 guard bits so it's exponent tmp_tb_e=+2 */
     568     2392368 :                         tmp_sb_e = add( tmp_sb_e, 2 );
     569     2392368 :                         tmp_tb_e = add( tmp_tb_e, 2 );
     570             : 
     571             :                         Word16 tmp0, tmp2, tmp3, tmp4;
     572             :                         Word16 tmp0_e, tmp2_e, tmp3_e, tmp4_e;
     573     2392368 :                         tmp0 = shr( hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb], 2 );
     574     2392368 :                         tmp0_e = add( hPrivateData->prevSFB_FIR_TB_e[sfb], 2 );
     575     2392368 :                         move16();
     576     2392368 :                         tmp2 = shr( hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb], 2 );
     577     2392368 :                         tmp2_e = add( hPrivateData->prevSFB_IIR_TB_e[sfb], 2 );
     578     2392368 :                         move16();
     579     2392368 :                         tmp3 = shr( hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb], 2 );
     580     2392368 :                         tmp3_e = add( hPrivateData->prevSFB_FIR_SB_e[sfb], 2 );
     581     2392368 :                         move16();
     582     2392368 :                         tmp4 = shr( hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb], 2 );
     583     2392368 :                         tmp4_e = add( hPrivateData->prevSFB_IIR_SB_e[sfb], 2 );
     584     2392368 :                         move16();
     585             :                         Word16 x1, x2;
     586             : 
     587     2392368 :                         Word16 x1_e = BASOP_Util_Add_MantExp( tmp0, tmp0_e, tmp2, tmp2_e - 1, &x1 );
     588     2392368 :                         Word16 x2_e = BASOP_Util_Add_MantExp( tmp3, tmp3_e, tmp4, tmp4_e - 1, &x2 );
     589     2392368 :                         hPrivateData->sfb_tb_e[sfb] = BASOP_Util_Add_MantExp( tmp_tb, tmp_tb_e, x1, x1_e, &hPrivateData->SFM_tb_fx[sfb] ); // hPrivateData->sfb_sb_e[sfb]
     590     2392368 :                         move16();
     591     2392368 :                         BASOP_Util_Add_MantExp( 22118, 2, negate( hPrivateData->SFM_tb_fx[sfb] ), hPrivateData->sfb_tb_e[sfb], &tmp );
     592             : 
     593     2392368 :                         IF( tmp < 0 )
     594             :                         {
     595      275877 :                             hPrivateData->sfb_tb_e[sfb] = 2;
     596      275877 :                             hPrivateData->SFM_tb_fx[sfb] = 22118;
     597      275877 :                             move16();
     598      275877 :                             move16();
     599             :                         }
     600             : 
     601     2392368 :                         hPrivateData->sfb_sb_e[sfb] = BASOP_Util_Add_MantExp( tmp_sb, tmp_sb_e, x2, x2_e, &hPrivateData->SFM_sb_fx[sfb] ); // hPrivateData->sfb_tb_e[sfb]
     602     2392368 :                         move16();
     603     2392368 :                         BASOP_Util_Add_MantExp( 22118, 2, negate( hPrivateData->SFM_sb_fx[sfb] ), hPrivateData->sfb_sb_e[sfb], &tmp );
     604             : 
     605     2392368 :                         IF( tmp < 0 )
     606             :                         {
     607      160158 :                             hPrivateData->sfb_sb_e[sfb] = 2;
     608      160158 :                             hPrivateData->SFM_sb_fx[sfb] = 22118;
     609      160158 :                             move16();
     610      160158 :                             move16();
     611             :                         }
     612             : 
     613     2392368 :                         BASOP_Util_Add_MantExp( hPrivateData->SFM_sb_fx[sfb], hPrivateData->sfb_sb_e[sfb], negate( hPrivateData->SFM_tb_fx[sfb] ), hPrivateData->sfb_tb_e[sfb], &diffSFM );
     614             : 
     615     2392368 :                         test();
     616     2392368 :                         IF( diffSFM > 0 && LT_32( hPrivateData->SFM_tb_fx[sfb], L_shr( 3277 /*0.1 Q15*/, hPrivateData->sfb_tb_e[sfb] ) ) ) /* check whether target SFB is more tonal than source SFB */
     617       12175 :                         {
     618             :                             Word16 currDampingFactor, dampingFactor;
     619             :                             Word16 slope, threshold;
     620             :                             Word16 alpha;
     621             :                             Word16 slope_e, threshold_e, currDampingFactor_e, dampingFactor_e, alpha_e;
     622             : 
     623             :                             /* calculate spectral tilt to detect sudden drops (or increases) in energy in the current SFB */
     624             :                             // slope = IGF_getTilt_fx( pPowerSpectrum, swb_offset[sfb], swb_offset[sfb + 1], e_ps, &slope_e );
     625             : 
     626       12175 :                             x = 1;
     627       12175 :                             mean_x_fx = mean_y_fx = 0;
     628       12175 :                             Word32 mean_y_fx_tmp = 0;
     629       12175 :                             move32();
     630       12175 :                             mean_xy_fx = mean_x2_fx = 0;
     631       12175 :                             mean_x_e = 15;
     632       12175 :                             mean_xy_e = mean_y_e = mean_x2_e = 31;
     633       12175 :                             move16();
     634       12175 :                             move16();
     635       12175 :                             move16();
     636       12175 :                             move16();
     637       12175 :                             move16();
     638       12175 :                             move16();
     639       12175 :                             move16();
     640       12175 :                             move16();
     641       12175 :                             move16();
     642             : 
     643      754613 :                             FOR( sb = swb_offset[sfb]; sb < swb_offset[sfb + 1]; sb++ )
     644             :                             {
     645      742438 :                                 mean_x_fx = add( mean_x_fx, x );                   /*Q0*/
     646      742438 :                                 mean_x2_fx = L_add( mean_x2_fx, L_mult0( x, x ) ); /*Q0*/
     647             : 
     648             :                                 /*y = 20.f * log10f( max( 1.f, powerSpectrum[i] ) );*/
     649      742438 :                                 IF( LE_64( W_deposit32_l( pPowerSpectrum_fx[sb] ), W_shl( 1, ( sub( 31, e_ps[sb] ) ) ) ) )
     650             :                                 {
     651       95044 :                                     y = 0;
     652       95044 :                                     move16();
     653             :                                 }
     654             :                                 ELSE
     655             :                                 {
     656      647394 :                                     y = imult1616( 20, extract_l( L_shr( Mult_32_16( ( L_add( BASOP_Util_Log2( pPowerSpectrum_fx[sb] ), L_shl( e_ps[sb], Q25 ) ) ), INV_Log2_10_Q15 ), Q25 ) ) ); /*Q0*/
     657             :                                 }
     658      742438 :                                 mean_y_fx_tmp = L_mac0( mean_y_fx_tmp, y, 1 );     /*Q0*/
     659      742438 :                                 mean_xy_fx = L_add( mean_xy_fx, L_mult0( y, x ) ); /*Q0*/
     660             : 
     661      742438 :                                 x = add( x, 1 );
     662             :                             }
     663       12175 :                             mean_y_fx = BASOP_Util_Divide3216_Scale( mean_y_fx_tmp, width, &tmp_e ); /* mean_y_e*/
     664             : 
     665       12175 :                             mean_y_e = add( mean_y_e, sub( tmp_e, 15 ) );
     666       12175 :                             mean_x_fx = BASOP_Util_Divide1616_Scale( mean_x_fx, width, &tmp_e ); /* mean_x_e*/
     667       12175 :                             mean_x_e = add( mean_x_e, sub( tmp_e, 15 ) );
     668       12175 :                             mean_xy_fx = BASOP_Util_Divide3216_Scale( mean_xy_fx, width, &tmp_e ); /* mean_xy_e*/
     669       12175 :                             mean_xy_e = add( mean_xy_e, sub( tmp_e, 15 ) );
     670       12175 :                             mean_x2_fx = BASOP_Util_Divide3216_Scale( mean_x2_fx, width, &tmp_e ); /* mean_x2_e*/
     671       12175 :                             mean_x2_e = add( mean_x2_e, sub( tmp_e, 15 ) );
     672             : 
     673             :                             /*slope = ( mean_xy - mean_x * mean_y ) / ( mean_x2 - mean_x * mean_x );*/
     674       12175 :                             slope = BASOP_Util_Divide3232_Scale( ( L_sub( mean_xy_fx, L_shl( mult( mean_x_fx, mean_y_fx ), sub( add( mean_x_e, mean_y_e ), mean_xy_e ) ) ) ), ( L_sub( mean_x2_fx, L_shl( mult( mean_x_fx, mean_x_fx ), sub( add( mean_x_e, mean_x_e ), mean_x2_e ) ) ) ), &slope_e );
     675       12175 :                             slope_e = add( slope_e, sub( mean_xy_e, mean_x2_e ) );
     676             : 
     677             :                             /* determine whether strong tilt is due to a step in the spectrum (e.g. band limitation, no damping)
     678             :                                or a tonal component close the band border (apply damping) by calculating SFM for a shift of 1/2 SFB width*/
     679       12175 :                             threshold = BASOP_Util_Divide1616_Scale( 60, width, &threshold_e );
     680             : 
     681       12175 :                             test();
     682       12175 :                             IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( slope, add( slope_e, 16 ), negate( threshold ), add( threshold_e, 16 ) ), -1 ) )
     683             :                             {
     684         191 :                                 Word16 shift = shr( width, 1 );
     685         191 :                                 sfm = IGF_getSFM_new_fx( pPowerSpectrum_fx, hPrivateData->logSpec, swb_offset[sfb] - shift, swb_offset[sfb + 1] - shift, e_ps );
     686         191 :                                 crest = IGF_getCrest_new_fx( hPrivateData->logSpec, swb_offset[sfb] - shift, swb_offset[sfb + 1] - shift, &crest_exp );
     687         191 :                                 shiftedSFM = BASOP_Util_Divide1616_Scale( sfm, crest, &shiftedSFM_e );
     688             :                             }
     689       11984 :                             ELSE IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( slope, add( slope_e, 16 ), threshold, add( threshold_e, 16 ) ), 1 ) && ( NE_16( sfb, sub( hGrid->sfbWrap[hGrid->nTiles], 1 ) ) ) )
     690             :                             {
     691          54 :                                 Word16 shift = shr( width, 1 );
     692          54 :                                 shiftedSFM = BASOP_Util_Divide1616_Scale( IGF_getSFM_new_fx( pPowerSpectrum_fx, hPrivateData->logSpec, swb_offset[sfb] + shift, swb_offset[sfb + 1] + shift, e_ps ), IGF_getCrest_new_fx( hPrivateData->logSpec, swb_offset[sfb] + shift, swb_offset[sfb + 1] + shift, &crest_exp ), &tmp_e );
     693          54 :                                 sfm = IGF_getSFM_new_fx( pPowerSpectrum_fx, hPrivateData->logSpec, swb_offset[sfb] + shift, swb_offset[sfb + 1] + shift, e_ps );
     694          54 :                                 crest = IGF_getCrest_new_fx( hPrivateData->logSpec, swb_offset[sfb] + shift, swb_offset[sfb + 1] + shift, &crest_exp );
     695          54 :                                 shiftedSFM = BASOP_Util_Divide1616_Scale( sfm, crest, &shiftedSFM_e );
     696             :                             }
     697       12175 :                             shiftedSFM_e = add( shiftedSFM_e, sub( sfm_exp, crest_exp ) );
     698             : 
     699       12175 :                             IF( GT_32( shiftedSFM, L_shl( 1311 /*0.04f Q15*/, negate( shiftedSFM_e ) ) ) )
     700             :                             {
     701          21 :                                 currDampingFactor = 32767; /*1.f Q15*/
     702          21 :                                 currDampingFactor_e = 0;
     703          21 :                                 move16();
     704          21 :                                 move16();
     705             :                             }
     706             :                             ELSE
     707             :                             {
     708             :                                 // alpha = min( 320.f / (float) swb_offset[sfb + 1], 1.25f );
     709       12154 :                                 temp = BASOP_Util_Divide1616_Scale( 320, swb_offset[sfb + 1], &alpha_e );
     710       12154 :                                 alpha = extract_l( L_min( temp, L_shl( 20480 /*1.25 Q14*/, sub( 1, alpha_e ) ) ) ); // alpha_e
     711             : 
     712             :                                 // currDampingFactor = expf( 1.25f * alpha * logf( hPrivateData->SFM_tb[sfb] / hPrivateData->SFM_sb[sfb] ) );
     713       12154 :                                 temp = BASOP_Util_Divide1616_Scale( hPrivateData->SFM_tb_fx[sfb], hPrivateData->SFM_sb_fx[sfb], &tmp_e ); // tmp_e
     714       12154 :                                 tmp_e = add( tmp_e, sub( hPrivateData->sfb_tb_e[sfb], hPrivateData->sfb_sb_e[sfb] ) );
     715       12154 :                                 Word16 temp1 = mult( 20480 /* 1.25f in Q14 */, alpha );
     716       12154 :                                 Word16 tmp1_e = add( 1, alpha_e );
     717       12154 :                                 currDampingFactor = round_fx( BASOP_util_Pow2( Mpy_32_16_1( L_add( BASOP_Util_Log2( temp ), L_shl_sat( add( 16, tmp_e ), 25 ) ), temp1 ), add( tmp1_e, 6 ), &currDampingFactor_e ) ); // currDampingFactor_e
     718             : 
     719             :                                 /* calculate tonal-to-noise ratio and reduce damping for low values*/
     720             : 
     721             :                                 Word32 tonalToNoise;
     722             :                                 Word16 adap;
     723             :                                 Word16 adap_e;         /*stores exp for adap*/
     724             :                                 Word16 tonalToNoise_e; /*stores exponent for tonalToNoise*/
     725       12154 :                                 tonalToNoise_e = 9;
     726       12154 :                                 move16();
     727       12154 :                                 adap = BASOP_Util_Divide1616_Scale( width, 40, &adap_e );
     728       12154 :                                 tonalToNoise = IGF_getTNR_ivas_fx( pPowerSpectrum_fx, swb_offset[sfb], swb_offset[sfb + 1], adap, e_ps, adap_e ); /*Q22*/
     729             : 
     730       12154 :                                 IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( tonalToNoise, tonalToNoise_e, L_add( L_shl( 10, sub( 15, adap_e ) ), adap ), add( 16, adap_e ) ), -1 ) )
     731             :                                 {
     732             :                                     // currDampingFactor += 0.03f * ( ( 10 + adap ) - tonalToNoise );
     733        8722 :                                     Word32 temp2 = BASOP_Util_Add_Mant32Exp( L_add( L_shl( 10, sub( 15, adap_e ) ) /*exp:adap_e*/, adap ), add( adap_e, 16 ), L_negate( tonalToNoise ), tonalToNoise_e, &tmp_e ); // tmp_e
     734        8722 :                                     currDampingFactor_e = BASOP_Util_Add_MantExp( currDampingFactor, currDampingFactor_e, extract_l( Mult_32_32( 983 /*0.03f Q15*/, temp2 ) ), tmp_e, &currDampingFactor );       // currDampingFactor_e
     735             :                                 }
     736             :                             }
     737             : 
     738       12175 :                             Word32 L_tmp = hPrivateData->prevDampingFactor_IIR_fx[sfb];
     739       12175 :                             move32();
     740       12175 :                             L_tmp = L_shl( L_tmp, hPrivateData->prevDampingFactor_IIR_e[sfb] );
     741       12175 :                             test();
     742       12175 :                             test();
     743       12175 :                             IF( last_core_acelp || hPrivateData->wasTransient || EQ_32( L_tmp, MIN16B ) )
     744             :                             {
     745        4147 :                                 tmp = BASOP_Util_Cmp_Mant32Exp( currDampingFactor, currDampingFactor_e, 3277 /* 0.1f in Q15 */, 0 );
     746        4147 :                                 IF( tmp >= 0 )
     747             :                                 {
     748        4106 :                                     hPrivateData->prevDampingFactor_IIR_fx[sfb] = currDampingFactor;
     749        4106 :                                     hPrivateData->prevDampingFactor_IIR_e[sfb] = currDampingFactor_e;
     750        4106 :                                     move16();
     751        4106 :                                     move16();
     752             :                                 }
     753             :                                 ELSE
     754             :                                 {
     755          41 :                                     hPrivateData->prevDampingFactor_IIR_fx[sfb] = 3277; /* 0.1 in Q15 */
     756          41 :                                     hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
     757          41 :                                     move16();
     758          41 :                                     move16();
     759             :                                 }
     760             :                             }
     761       12175 :                             IF( last_core_acelp )
     762             :                             {
     763         560 :                                 hPrivateData->dampingFactorSmoothing[sfb] = 2;
     764         560 :                                 move16();
     765             :                             }
     766             : 
     767       12175 :                             dampingFactor_e = BASOP_Util_Add_MantExp( currDampingFactor, currDampingFactor_e, hPrivateData->prevDampingFactor_IIR_fx[sfb], hPrivateData->prevDampingFactor_IIR_e[sfb], &dampingFactor ); // dampingFactor_e
     768       12175 :                             dampingFactor = shr( dampingFactor, 1 );
     769             : 
     770       12175 :                             gain = Mult_32_16( gain, shl_sat( extract_l( L_min( L_add( dampingFactor, Mult_32_16( L_shl( hPrivateData->dampingFactorSmoothing[sfb], sub( 15, dampingFactor_e ) ) /*Q:15-dampingFactor_e*/, 3277 /*0.1f Q15*/ ) /*Q:15-dampingFactor_e*/ ), shl_sat( 1, sub( 15, dampingFactor_e ) ) ) ), dampingFactor_e ) /*Q15*/ );
     771             : 
     772       12175 :                             hPrivateData->prevDampingFactor_IIR_fx[sfb] = dampingFactor;
     773       12175 :                             move16();
     774       12175 :                             hPrivateData->prevDampingFactor_IIR_e[sfb] = dampingFactor_e;
     775       12175 :                             move16();
     776       12175 :                             if ( hPrivateData->dampingFactorSmoothing[sfb] > 0 )
     777             :                             {
     778        5286 :                                 hPrivateData->dampingFactorSmoothing[sfb] = sub( hPrivateData->dampingFactorSmoothing[sfb], 1 );
     779        5286 :                                 move16();
     780             :                             }
     781             :                         }
     782             :                         ELSE
     783             :                         {
     784     2380193 :                             hPrivateData->prevDampingFactor_IIR_fx[sfb] = MIN16B; // exponent : hPrivateData->prevDampingFactor_IIR_e[sfb]
     785     2380193 :                             hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
     786     2380193 :                             hPrivateData->dampingFactorSmoothing[sfb] = 1;
     787     2380193 :                             move16();
     788     2380193 :                             move16();
     789     2380193 :                             move16();
     790             :                         }
     791             : 
     792     2392368 :                         hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = tmp_tb; // hPrivateData->prevSFB_FIR_TB_e[sfb]
     793     2392368 :                         hPrivateData->prevSFB_FIR_TB_e[sfb] = tmp_tb_e;
     794     2392368 :                         hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = hPrivateData->SFM_tb_fx[sfb]; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     795     2392368 :                         hPrivateData->prevSFB_IIR_TB_e[sfb] = hPrivateData->sfb_tb_e[sfb];
     796     2392368 :                         hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = tmp_sb; // hPrivateData->prevSFB_FIR_SB_e[sfb]
     797     2392368 :                         hPrivateData->prevSFB_FIR_SB_e[sfb] = tmp_sb_e;
     798     2392368 :                         hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = hPrivateData->SFM_sb_fx[sfb]; // hPrivateData->prevSFB_IIR_SB_e[sfb]
     799     2392368 :                         hPrivateData->prevSFB_IIR_SB_e[sfb] = hPrivateData->sfb_sb_e[sfb];
     800     2392368 :                         move16();
     801     2392368 :                         move16();
     802     2392368 :                         move16();
     803     2392368 :                         move16();
     804     2392368 :                         move16();
     805     2392368 :                         move16();
     806     2392368 :                         move16();
     807     2392368 :                         move16();
     808             :                     }
     809             :                     ELSE
     810             :                     {
     811        6685 :                         hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = 0; // hPrivateData->prevSFB_FIR_TB_e[sfb]
     812        6685 :                         hPrivateData->prevSFB_FIR_TB_e[sfb] = 0;
     813        6685 :                         hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     814        6685 :                         hPrivateData->prevSFB_IIR_TB_e[sfb] = 0;
     815        6685 :                         hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     816        6685 :                         hPrivateData->prevSFB_FIR_SB_e[sfb] = 0;
     817        6685 :                         hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_SB_e[sfb]
     818        6685 :                         hPrivateData->prevSFB_IIR_SB_e[sfb] = 0;
     819        6685 :                         hPrivateData->prevDampingFactor_IIR_fx[sfb] = MIN16B; /* hPrivateData->prevDampingFactor_IIR_e[sfb]*/
     820        6685 :                         hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
     821        6685 :                         hPrivateData->dampingFactorSmoothing[sfb] = 2;
     822        6685 :                         move16();
     823        6685 :                         move16();
     824        6685 :                         move16();
     825        6685 :                         move16();
     826        6685 :                         move16();
     827        6685 :                         move16();
     828        6685 :                         move16();
     829        6685 :                         move16();
     830        6685 :                         move16();
     831        6685 :                         move16();
     832        6685 :                         move16();
     833             :                     }
     834             :                 }
     835             :             }
     836             :             ELSE
     837             :             {
     838      287701 :                 tmp_e = e_mdct;
     839      287701 :                 move16();
     840      287701 :                 sfbEnergyR = add_sat( EPSILON_FX, BASOP_Util_Divide3216_Scale( sum2_32_fx( pMDCTSpectrum_fx + swb_offset[sfb], width, &tmp_e ) /*exp: tmp_e*/, width, &sfbEnergyR_e ) ); // sfbEnergyR_e
     841      287701 :                 sfbEnergyR_e = add( sfbEnergyR_e, add( tmp_e, -15 ) );
     842      287701 :                 gain = L_shl( sfbEnergyR, 16 ); // gain_e
     843      287701 :                 move32();
     844      287701 :                 gain_e = sfbEnergyR_e;
     845             : 
     846      287701 :                 IF( element_mode > EVS_MONO )
     847             :                 {
     848      287701 :                     hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = 0; // hPrivateData->prevSFB_FIR_TB_e[sfb]
     849      287701 :                     hPrivateData->prevSFB_FIR_TB_e[sfb] = 0;
     850      287701 :                     hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     851      287701 :                     hPrivateData->prevSFB_IIR_TB_e[sfb] = 0;
     852      287701 :                     hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     853      287701 :                     hPrivateData->prevSFB_FIR_SB_e[sfb] = 0;
     854      287701 :                     hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_SB_e[sfb]
     855      287701 :                     hPrivateData->prevSFB_IIR_SB_e[sfb] = 0;
     856      287701 :                     hPrivateData->prevDampingFactor_IIR_fx[sfb] = MIN16B; /* hPrivateData->prevDampingFactor_IIR_e[sfb]*/
     857      287701 :                     hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
     858      287701 :                     hPrivateData->dampingFactorSmoothing[sfb] = 2;
     859      287701 :                     move16();
     860      287701 :                     move16();
     861      287701 :                     move16();
     862      287701 :                     move16();
     863      287701 :                     move16();
     864      287701 :                     move16();
     865      287701 :                     move16();
     866      287701 :                     move16();
     867      287701 :                     move16();
     868      287701 :                     move16();
     869      287701 :                     move16();
     870             :                 }
     871             :             }
     872             : 
     873     2686754 :             gain = Mult_32_16( gain, att ); // gain_e
     874     2686754 :             gain_e = add( gain_e, 0 );
     875             : 
     876             :             /*gain=0.5f+log2f(gain)*2+16 becuase 2.885390081777927f=2*1/loge(2) so 2*1/loge(2)*loge(x) can be written as 2*log2(x)*/
     877     2686754 :             gain = L_add( ONE_IN_Q22, L_add( L_add( L_shr( BASOP_Util_Log2( gain ), 1 ), L_shl( gain_e, Q24 ) ), L_shl( 16, Q23 ) ) ); /*Q23*/
     878     2686754 :             test();
     879     2686754 :             test();
     880     2686754 :             IF( !isTransient && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) ) )
     881             :             {
     882      385537 :                 gain = L_add( gain, ONE_IN_Q21 ); /* better preservation of original HF band energy */
     883             :             }
     884     2686754 :             test();
     885     2686754 :             test();
     886     2686754 :             IF( !isTransient && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_64000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_64000_CPE ) ) )
     887             :             {
     888      505737 :                 gain = L_add( gain, ONE_IN_Q20 );
     889             :             }
     890     2686754 :             gain = L_min( gain, 91 << Q23 ); /* 13+15+63, see arithcode encode residual */
     891     2686754 :             gain = L_max( gain, 0 );
     892     2686754 :             gain_e = 8; /* stores exponent for gain_fx*/
     893     2686754 :             move16();
     894     2686754 :             hPrivateData->igfScfQuantized[sfb] = extract_l( L_shr( gain, Q23 ) ); /*Q0*/
     895     2686754 :             move16();
     896             :         }
     897             :     }
     898             : 
     899      510753 :     return;
     900             : }
     901             : 
     902             : /*-------------------------------------------------------------------*
     903             :  * IGF_CalculateStereoEnvelope_fx()
     904             :  *
     905             :  * envelope estimation
     906             : 
     907             :  *-------------------------------------------------------------------*/
     908      102946 : static void IGF_CalculateStereoEnvelope_fx(
     909             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i  : instance handle of IGF Encoder              */
     910             :     const Word32 *pMDCTSpectrum_fx,        /* i  : MDCT spectrum                               */
     911             :     Word16 pMDCTSpectrum_e,                /* i  : exponent for pMDCTSpectrum_fx               */
     912             :     const Word32 *pMDCTSpectrumMsInv_fx,   /* i  : MDCT spectrum                               */
     913             :     Word16 pMDCTSpectrumMsInv_e,           /* i  : expontent for pMDCTSpectrumMsInv_fx         */
     914             :     const Word32 *pPowerSpectrum_fx,       /* i  : MDCT^2 + MDST^2 spectrum, or estimate       */
     915             :     Word16 *pPowerSpectrum_e,              /* i  : exponent for pPowerSpectrum_fx              */
     916             :     const Word32 *pPowerSpectrumMsInv_fx,  /* i  : inverse power spectrum                      */
     917             :     Word16 *q_pPowerSpectrumMsInv,         /* i  : Q for pPowerSpectrumMsInv_fx         */
     918             :     const Word16 igfGridIdx,               /* i  : IGF grid index                                     */
     919             :     const Word16 coreMsMask[N_MAX],        /* i  : line wise ms Mask                                  */
     920             :     const Word16 isTransient,              /* i  : flag indicating if transient is detected           */
     921             :     const Word16 last_core_acelp,          /* i  : indicator if last frame was ACELP core             */
     922             :     const Word16 mct_on )
     923             : {
     924             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
     925             :     H_IGF_GRID hGrid;
     926             :     Word16 *swb_offset;
     927             :     Word16 sfb;   /* this is the actual scalefactor band */
     928             :     Word16 width; /* this is width in subbands of the actual scalefactor band */
     929             :     Word16 tile_idx;
     930             :     Word16 strt_cpy;
     931             :     Word32 gain_fx; /* the gain which has to be applied to the source tile to get the destination energy */
     932             :     Word16 sb;
     933             :     Word16 sfbEnergyR_fx;
     934             :     Word32 sfbEnergyC_fx; /* the energy of the destination region of the tile */
     935             :     Word32 sfbEnergyTileR_fx;
     936             :     Word32 sfbEnergyTileC_fx; /* the energy of the destination region of the tile */
     937             :     Word16 tmp, x, y;
     938             :     Word16 mean_x_fx, mean_y_fx;
     939             :     Word32 mean_xy_fx, mean_x2_fx;
     940             :     Word16 slope_fx;
     941             :     Word16 tmp_tb_fx;
     942             :     Word16 tmp_sb_fx;
     943             :     Word16 sfbCnt;
     944             :     Word32 tileSrcSpec_fx[MAX_IGF_SFB_LEN];
     945             :     Word16 sfm;
     946             :     Word16 crest;
     947             :     Word16 temp;
     948             :     Word16 mean_x_e, mean_y_e;             /*Stores exponent for mean_x and mean_y respectively*/
     949             :     Word16 mean_xy_e, mean_x2_e;           /*stores exponent for mean_xy and mean_x2 respectively*/
     950             :     Word16 tileSrcSpec_e[MAX_IGF_SFB_LEN]; /*Exponent for tileSrcSpec_fx*/
     951             :     Word16 sfbEnergyTileR_e;               /*Exponent for sfbEnergyTileR_fx*/
     952             :     Word16 sfbEnergyTileC_e;               /*Exponent for sfbEnergyTileC_fx*/
     953             :     Word16 sfbEnergyC_e;                   /*Exponent for sfbEnergyC_fx*/
     954             :     Word16 gain_e;                         /*exponent for gain_fx*/
     955             :     Word16 crest_exp;                      /*stores exponent for output from crest*/
     956             :     Word16 sfm_exp;                        /*stores exponent for ouput from sfm*/
     957             :     Word16 tmp_tb_e;                       /*Stores exponent for tmp_tb_fx*/
     958             :     Word16 tmp_sb_e;                       /*stores exponent for tmp_sb_fx*/
     959             :     Word16 slope_e;                        /*stores exponent for slope_fx*/
     960             :     Word16 sfbEnergyR_e;                   /*stores exponent for sfbEnergyR*/
     961             :     Word16 tmp_e;
     962             :     Word32 temp_pPowerSpectrumMsInv[N_MAX], length;
     963      102946 :     Word16 q_temp_pPowerSpectrumMsInv = Q31, i;
     964      102946 :     move16();
     965             : 
     966      102946 :     IF( pPowerSpectrumMsInv_fx != NULL )
     967             :     {
     968       83836 :         length = N_MAX;
     969       83836 :         move16();
     970       83836 :         if ( mct_on )
     971             :         {
     972       53456 :             length = L_FRAME48k;
     973       53456 :             move16();
     974             :         }
     975    87857596 :         FOR( i = 0; i < length; i++ )
     976             :         {
     977    87773760 :             IF( pPowerSpectrumMsInv_fx[i] != 0 )
     978             :             {
     979    63814400 :                 q_temp_pPowerSpectrumMsInv = s_min( q_temp_pPowerSpectrumMsInv, add( q_pPowerSpectrumMsInv[i], norm_l( pPowerSpectrumMsInv_fx[i] ) ) );
     980             :             }
     981             :         }
     982    87857596 :         FOR( i = 0; i < length; i++ )
     983             :         {
     984    87773760 :             temp_pPowerSpectrumMsInv[i] = L_shl( pPowerSpectrumMsInv_fx[i], sub( q_temp_pPowerSpectrumMsInv, q_pPowerSpectrumMsInv[i] ) );
     985    87773760 :             move32();
     986             :         }
     987             :     }
     988             : 
     989      102946 :     hPrivateData = &hIGFEnc->igfData;
     990      102946 :     hGrid = &hPrivateData->igfInfo.grid[(Word16) igfGridIdx];
     991      102946 :     swb_offset = hGrid->swb_offset;
     992      102946 :     move16();
     993             : 
     994      102946 :     IF( igfGridIdx != IGF_GRID_LB_NORM )
     995             :     {
     996       24450 :         FOR( sfbCnt = 0; sfbCnt < sub( hGrid->sfbWrap[hGrid->nTiles], hGrid->sfbWrap[0] ); sfbCnt++ )
     997             :         {
     998             :             /* reset filter */
     999       20036 :             hPrivateData->prevSFM_FIR_SFB_TB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_FIR_TB_e[sfbCnt]
    1000       20036 :             hPrivateData->prevSFM_IIR_SFB_TB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_IIR_TB_e[sfbCnt]
    1001       20036 :             hPrivateData->prevSFM_FIR_SFB_SB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_FIR_SB_e[sfbCnt]
    1002       20036 :             hPrivateData->prevSFM_IIR_SFB_SB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_IIR_SB_e[sfbCnt]
    1003       20036 :             hPrivateData->prevSFB_FIR_TB_e[sfbCnt] = 0;
    1004       20036 :             hPrivateData->prevSFB_IIR_TB_e[sfbCnt] = 0;
    1005       20036 :             hPrivateData->prevSFB_FIR_SB_e[sfbCnt] = 0;
    1006       20036 :             hPrivateData->prevSFB_IIR_SB_e[sfbCnt] = 0;
    1007       20036 :             move16();
    1008       20036 :             move16();
    1009       20036 :             move16();
    1010       20036 :             move16();
    1011       20036 :             move16();
    1012       20036 :             move16();
    1013       20036 :             move16();
    1014       20036 :             move16();
    1015             :         }
    1016             :     }
    1017             : 
    1018      102946 :     IF( pPowerSpectrum_fx )
    1019             :     {
    1020    39289892 :         FOR( sb = hGrid->sbWrap[0]; sb < swb_offset[hGrid->sfbWrap[hGrid->nTiles]]; sb++ )
    1021             :         {
    1022             :             /*hPrivateData->logSpec[sb] = max( 0, (Word16) ( logf( max( FLT_MIN, pPowerSpectrum[sb] ) ) * INV_LOG_2 ) );*/
    1023    39206056 :             IF( LE_32( 1, pPowerSpectrum_fx[sb] ) )
    1024             :             {
    1025    39205735 :                 hPrivateData->logSpec[sb] = s_max( 0, (Word16) L_shr( L_add( BASOP_Util_Log2( pPowerSpectrum_fx[sb] ), L_shl( pPowerSpectrum_e[sb], Q25 ) ), 25 ) );
    1026             :             }
    1027             :             ELSE
    1028             :             {
    1029         321 :                 hPrivateData->logSpec[sb] = 0; /*max(0,-126) is always 0*/
    1030             :             }
    1031    39206056 :             move16();
    1032             :         }
    1033             :     }
    1034             : 
    1035      402258 :     FOR( tile_idx = 0; tile_idx < hGrid->nTiles; tile_idx++ )
    1036             :     {
    1037      299312 :         strt_cpy = hGrid->sbWrap[tile_idx];
    1038      299312 :         move16();
    1039             : 
    1040      802264 :         FOR( sfb = hGrid->sfbWrap[tile_idx]; sfb < hGrid->sfbWrap[tile_idx + 1]; sfb++ )
    1041             :         {
    1042      502952 :             width = sub( swb_offset[sfb + 1], swb_offset[sfb] );
    1043      502952 :             sfbEnergyTileR_fx = EPSILON_FX;
    1044      502952 :             sfbEnergyTileC_fx = EPSILON_FX;
    1045      502952 :             sfbEnergyC_fx = EPSILON_FX;
    1046      502952 :             sfbEnergyTileR_e = 0;
    1047      502952 :             sfbEnergyTileC_e = 0;
    1048      502952 :             sfbEnergyC_e = 0;
    1049      502952 :             move16();
    1050      502952 :             move16();
    1051      502952 :             move16();
    1052      502952 :             move32();
    1053      502952 :             move32();
    1054      502952 :             move32();
    1055      502952 :             IF( pPowerSpectrum_fx )
    1056             :             {
    1057             :                 Word16 final_exp;
    1058             :                 Word16 norm_exp;
    1059             :                 Word32 scaled_value;
    1060      409724 :                 tmp = strt_cpy;
    1061      409724 :                 move16();
    1062             : 
    1063    21883932 :                 FOR( sb = swb_offset[sfb]; sb < swb_offset[sfb + 1]; sb++ )
    1064             :                 {
    1065    21474208 :                     IF( NE_16( coreMsMask[sb], coreMsMask[strt_cpy] ) )
    1066             :                     {
    1067    12751260 :                         sfbEnergyC_fx = BASOP_Util_Add_Mant32Exp( sfbEnergyC_fx, sfbEnergyC_e, pPowerSpectrum_fx[sb], pPowerSpectrum_e[sb], &sfbEnergyC_e ); /*resultant exponent is stored in sfbEnergyC_e*/
    1068    12751260 :                         norm_exp = norm_l( pMDCTSpectrumMsInv_fx[strt_cpy] );
    1069    12751260 :                         final_exp = sub( pMDCTSpectrumMsInv_e, norm_exp );
    1070    12751260 :                         scaled_value = L_shl( pMDCTSpectrumMsInv_fx[strt_cpy], norm_exp );
    1071    12751260 :                         sfbEnergyTileR_fx = BASOP_Util_Add_Mant32Exp( sfbEnergyTileR_fx, sfbEnergyTileR_e, Mult_32_32( scaled_value, scaled_value ), shl( final_exp, 1 ), &sfbEnergyTileR_e );                /*resultant exponent is stored in sfbEnergyTileR_e*/
    1072    12751260 :                         sfbEnergyTileC_fx = BASOP_Util_Add_Mant32Exp( sfbEnergyTileC_fx, sfbEnergyTileC_e, pPowerSpectrumMsInv_fx[strt_cpy], sub( 31, q_pPowerSpectrumMsInv[strt_cpy] ), &sfbEnergyTileC_e ); /*resultant exponent is stored in sfbEnergyTileC_e*/
    1073    12751260 :                         tileSrcSpec_fx[strt_cpy - tmp] = temp_pPowerSpectrumMsInv[strt_cpy];                                                                                                                  /*resultant exponent is stored in tileSrcSpec_e*/
    1074    12751260 :                         tileSrcSpec_e[strt_cpy - tmp] = sub( 31, q_temp_pPowerSpectrumMsInv );
    1075             :                     }
    1076             :                     ELSE
    1077             :                     {
    1078     8722948 :                         sfbEnergyC_fx = BASOP_Util_Add_Mant32Exp( sfbEnergyC_fx, sfbEnergyC_e, pPowerSpectrum_fx[sb], pPowerSpectrum_e[sb], &sfbEnergyC_e ); /*resultant exponent is stored in sfbEnergyC_e*/
    1079     8722948 :                         norm_exp = norm_l( pMDCTSpectrum_fx[strt_cpy] );
    1080     8722948 :                         final_exp = sub( pMDCTSpectrum_e, norm_exp );
    1081     8722948 :                         scaled_value = L_shl( pMDCTSpectrum_fx[strt_cpy], norm_exp );
    1082     8722948 :                         sfbEnergyTileR_fx = BASOP_Util_Add_Mant32Exp( sfbEnergyTileR_fx, sfbEnergyTileR_e, Mult_32_32( scaled_value, scaled_value ), shl( final_exp, 1 ), &sfbEnergyTileR_e ); /*resultant exponent is stored in sfbEnergyTileR_e*/
    1083     8722948 :                         sfbEnergyTileC_fx = BASOP_Util_Add_Mant32Exp( sfbEnergyTileC_fx, sfbEnergyTileC_e, pPowerSpectrum_fx[strt_cpy], pPowerSpectrum_e[strt_cpy], &sfbEnergyTileC_e );       /*resultant exponent is stored in sfbEnergyTileC_e*/
    1084     8722948 :                         tileSrcSpec_fx[strt_cpy - tmp] = pPowerSpectrum_fx[strt_cpy];                                                                                                          /*resultant exponent is stored in tileSrcSpec_e*/
    1085     8722948 :                         tileSrcSpec_e[strt_cpy - tmp] = pPowerSpectrum_e[strt_cpy];
    1086             :                     }
    1087    21474208 :                     move32();
    1088    21474208 :                     move16();
    1089    21474208 :                     strt_cpy = add( strt_cpy, 1 );
    1090             :                 }
    1091             : 
    1092      409724 :                 sfbEnergyTileR_fx = BASOP_Util_Divide3216_Scale( sfbEnergyTileR_fx, width, &tmp_e );
    1093      409724 :                 sfbEnergyTileR_e = sub( add( sfbEnergyTileR_e, tmp_e ), 15 ); /*stores the resultant exponent for sfbEnergyTileR_fx*/
    1094             : 
    1095             :                 /*gain = (float) ( sfbEnergyTileR * ( sfbEnergyC / sfbEnergyTileC ) );*/
    1096      409724 :                 temp = BASOP_Util_Divide3232_Scale( sfbEnergyC_fx, L_add( sfbEnergyTileC_fx, EPSILON_FX ), &tmp_e );
    1097      409724 :                 gain_e = add( tmp_e, sub( sfbEnergyC_e, sfbEnergyTileC_e ) );
    1098      409724 :                 gain_fx = Mult_32_16( sfbEnergyTileR_fx, temp );
    1099      409724 :                 gain_e = add( 16, add( gain_e, sfbEnergyTileR_e ) ); /*stores the resultant exponent for gain_fx*/
    1100             : 
    1101      409724 :                 IF( !isTransient )
    1102             :                 {
    1103             :                     Word16 diffSFM_fx;
    1104             :                     Word16 shiftedSFM_fx;
    1105             :                     Word16 shiftedSFM_e; /*stores the resultant exponent for shiftedSFM_fx*/
    1106      409724 :                     shiftedSFM_fx = 0;
    1107      409724 :                     shiftedSFM_e = 0;
    1108      409724 :                     move16();
    1109      409724 :                     move16();
    1110             : 
    1111             :                     // tmp_tb = IGF_getSFM_ivas(pPowerSpectrum, swb_offset[sfb], swb_offset[sfb + 1]) / IGF_getCrest_ivas(pPowerSpectrum, swb_offset[sfb], swb_offset[sfb + 1]);
    1112      409724 :                     sfm = IGF_getSFM_ivas_fx( &sfm_exp, pPowerSpectrum_fx, pPowerSpectrum_e, swb_offset[sfb], swb_offset[sfb + 1] );
    1113      409724 :                     crest = IGF_getCrest_ivas( &crest_exp, pPowerSpectrum_fx, pPowerSpectrum_e, swb_offset[sfb], swb_offset[sfb + 1] );
    1114      409724 :                     tmp_tb_fx = BASOP_Util_Divide1616_Scale( sfm, crest, &tmp_e );
    1115      409724 :                     tmp_tb_e = add( tmp_e, sub( sfm_exp, crest_exp ) ); /*stores the resultant exponent for tmp_tb_fx*/
    1116             : 
    1117             :                     // tmp_sb = IGF_getSFM_ivas(tileSrcSpec, 0, strt_cpy - tmp) / IGF_getCrest_ivas(tileSrcSpec, 0, strt_cpy - tmp);
    1118      409724 :                     sfm = IGF_getSFM_ivas_fx( &sfm_exp, tileSrcSpec_fx, tileSrcSpec_e, 0, sub( strt_cpy, tmp ) );
    1119      409724 :                     crest = IGF_getCrest_ivas( &crest_exp, tileSrcSpec_fx, tileSrcSpec_e, 0, sub( strt_cpy, tmp ) );
    1120      409724 :                     tmp_sb_fx = BASOP_Util_Divide1616_Scale( sfm, crest, &tmp_e );
    1121      409724 :                     tmp_sb_e = add( tmp_e, sub( sfm_exp, crest_exp ) ); /*stores the resultant exponent for tmp_sb_fx*/
    1122             : 
    1123      409724 :                     IF( last_core_acelp || hPrivateData->wasTransient )
    1124             :                     {
    1125        7955 :                         hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = tmp_tb_fx; /*Exponent for hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] and hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] values stored in hPrivateData->sfb_tb_e[sfb] */
    1126        7955 :                         hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = tmp_sb_fx; /*Exponent for hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] and hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] values stored in hPrivateData->sfb_sb_e[sfb]*/
    1127        7955 :                         hPrivateData->prevSFB_FIR_TB_e[sfb] = tmp_sb_e;
    1128        7955 :                         hPrivateData->prevSFB_IIR_TB_e[sfb] = tmp_sb_e;
    1129        7955 :                         hPrivateData->prevSFB_FIR_SB_e[sfb] = tmp_sb_e;
    1130        7955 :                         hPrivateData->prevSFB_IIR_SB_e[sfb] = tmp_sb_e;
    1131        7955 :                         move16();
    1132        7955 :                         move16();
    1133        7955 :                         move16();
    1134        7955 :                         move16();
    1135        7955 :                         move16();
    1136        7955 :                         move16();
    1137        7955 :                         move16();
    1138        7955 :                         move16();
    1139             :                     }
    1140             :                     ELSE
    1141             :                     {
    1142             :                         /* Purpose of this block:
    1143             :                         -to make the exponent of hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] and hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] equal to that of tmp_tb_fx
    1144             :                         -to make the exponent of hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] and hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] equal to that of tmp_sb_fx
    1145             :                         */
    1146             :                         Word16 diff_sb_e; /*stores the difference of exponents for sb*/
    1147             :                         Word16 diff_tb_e; /*stores the difference of exponents for tb*/
    1148      401769 :                         diff_sb_e = sub( tmp_sb_e, hPrivateData->sfb_sb_e[sfb] );
    1149      401769 :                         diff_tb_e = sub( tmp_tb_e, hPrivateData->sfb_tb_e[sfb] );
    1150      401769 :                         IF( LE_16( tmp_tb_e, hPrivateData->sfb_tb_e[sfb] ) )
    1151             :                         {
    1152      393549 :                             tmp_tb_fx = shl( tmp_tb_fx, diff_tb_e );
    1153      393549 :                             tmp_tb_e = hPrivateData->sfb_tb_e[sfb];
    1154      393549 :                             move16();
    1155             :                         }
    1156             :                         ELSE
    1157             :                         {
    1158        8220 :                             hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = shr( hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb], diff_tb_e );
    1159        8220 :                             hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = shr( hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb], diff_tb_e );
    1160        8220 :                             move16();
    1161        8220 :                             move16();
    1162        8220 :                             hPrivateData->prevSFB_FIR_TB_e[sfb] = tmp_tb_e;
    1163        8220 :                             hPrivateData->prevSFB_IIR_TB_e[sfb] = tmp_tb_e;
    1164        8220 :                             move16();
    1165        8220 :                             move16();
    1166             :                         }
    1167      401769 :                         IF( LE_16( tmp_sb_e, hPrivateData->sfb_sb_e[sfb] ) )
    1168             :                         {
    1169      393441 :                             tmp_sb_fx = shl( tmp_sb_fx, diff_sb_e );
    1170      393441 :                             tmp_sb_e = hPrivateData->sfb_sb_e[sfb];
    1171      393441 :                             move16();
    1172             :                         }
    1173             :                         ELSE
    1174             :                         {
    1175        8328 :                             hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = shr( hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb], diff_sb_e );
    1176        8328 :                             hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = shr( hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb], diff_sb_e );
    1177        8328 :                             move16();
    1178        8328 :                             move16();
    1179        8328 :                             hPrivateData->prevSFB_FIR_SB_e[sfb] = tmp_sb_e;
    1180        8328 :                             hPrivateData->prevSFB_IIR_SB_e[sfb] = tmp_sb_e;
    1181        8328 :                             move16();
    1182        8328 :                             move16();
    1183             :                         }
    1184             :                     }
    1185      409724 :                     tmp_tb_fx = shr_sat( tmp_tb_fx, sub( 2, tmp_tb_e ) );                                                                                                                                                                                                           /* Since we're limiting max value to 2.7f we can saturate to Q13 */
    1186      409724 :                     tmp_sb_fx = shr_sat( tmp_sb_fx, sub( 2, tmp_sb_e ) );                                                                                                                                                                                                           /* Since we're limiting max value to 2.7f we can saturate to Q13 */
    1187      409724 :                     hPrivateData->SFM_tb_fx[sfb] = add_sat( tmp_tb_fx, add_sat( shr( hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb], sub( 2, hPrivateData->prevSFB_FIR_TB_e[sfb] ) ), shr( hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb], sub( 3, hPrivateData->prevSFB_IIR_TB_e[sfb] ) ) ) ); /* Since we're limiting max value to 2.7f we can saturate to Q13 */
    1188      409724 :                     hPrivateData->SFM_tb_fx[sfb] = s_min( 22118 /*2.7f Q13*/, hPrivateData->SFM_tb_fx[sfb] );                                                                                                                                                                       /* resultant exponent stored in hPrivateData->sfb_sb_e[sfb]*/
    1189      409724 :                     hPrivateData->SFM_sb_fx[sfb] = add_sat( tmp_sb_fx, add_sat( shr( hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb], sub( 2, hPrivateData->prevSFB_FIR_SB_e[sfb] ) ), shr( hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb], sub( 3, hPrivateData->prevSFB_IIR_SB_e[sfb] ) ) ) ); /* Since we're limiting max value to 2.7f we can saturate to Q13 */
    1190      409724 :                     hPrivateData->SFM_sb_fx[sfb] = s_min( 22118 /*2.7f Q13*/, hPrivateData->SFM_sb_fx[sfb] );                                                                                                                                                                       /*resultant exponent stores in hPrivateData->sfb_tb_e[sfb]*/
    1191      409724 :                     move16();
    1192      409724 :                     move16();
    1193      409724 :                     move16();
    1194      409724 :                     move16();
    1195      409724 :                     hPrivateData->sfb_sb_e[sfb] = 2;
    1196      409724 :                     hPrivateData->sfb_tb_e[sfb] = 2;
    1197      409724 :                     move16();
    1198      409724 :                     move16();
    1199             : 
    1200      409724 :                     diffSFM_fx = sub( hPrivateData->SFM_sb_fx[sfb], hPrivateData->SFM_tb_fx[sfb] ); /*Q13*/
    1201             : 
    1202      409724 :                     test();
    1203      409724 :                     IF( diffSFM_fx > 0 && LT_16( hPrivateData->SFM_tb_fx[sfb], 819 /*0.1 Q13*/ ) ) /* check whether target SFB is more tonal than source SFB */
    1204        1252 :                     {
    1205             :                         Word16 currDampingFactor_fx, dampingFactor_fx, alpha_fx;
    1206             :                         Word16 threshold_e, threshold_fx, alpha_e, currDampingFactor_e, dampingFactor_e;
    1207             :                         /* calculate spectral tilt to detect sudden drops (or increases) in energy in the current SFB */
    1208        1252 :                         x = 1;
    1209        1252 :                         mean_x_fx = mean_y_fx = 0;
    1210        1252 :                         mean_xy_fx = mean_x2_fx = 0;
    1211        1252 :                         mean_x_e = mean_y_e = 15;
    1212        1252 :                         mean_xy_e = mean_x2_e = 31;
    1213        1252 :                         move16();
    1214        1252 :                         move16();
    1215        1252 :                         move16();
    1216        1252 :                         move16();
    1217        1252 :                         move16();
    1218        1252 :                         move16();
    1219        1252 :                         move16();
    1220        1252 :                         move16();
    1221        1252 :                         move16();
    1222       69188 :                         FOR( sb = swb_offset[sfb]; sb < swb_offset[sfb + 1]; sb++ )
    1223             :                         {
    1224       67936 :                             mean_x_fx = add( mean_x_fx, x );                   /*Q0*/
    1225       67936 :                             mean_x2_fx = L_add( mean_x2_fx, L_mult0( x, x ) ); /*Q0*/
    1226             : 
    1227             :                             /*y = 20 * (Word16) log10f( max( 1e-018f, pPowerSpectrum[sb] ) );*/
    1228       67936 :                             IF( LT_32( pPowerSpectrum_fx[sb], 1 ) )
    1229             :                             {
    1230           0 :                                 y = 20 * ( -18 );
    1231           0 :                                 move16();
    1232             :                             }
    1233             :                             ELSE
    1234             :                             {
    1235       67936 :                                 y = imult1616( 20, extract_l( L_shr( Mult_32_16( ( L_add( BASOP_Util_Log2( pPowerSpectrum_fx[sb] ), L_shl( pPowerSpectrum_e[sb], Q25 ) ) ), INV_Log2_10_Q15 ), 25 ) ) ); /*Q0*/
    1236             :                             }
    1237       67936 :                             mean_y_fx = add( mean_y_fx, y );                   /*Q0*/
    1238       67936 :                             mean_xy_fx = L_add( mean_xy_fx, L_mult0( y, x ) ); /*Q0*/
    1239             : 
    1240       67936 :                             x = add( x, 1 );
    1241             :                         }
    1242        1252 :                         mean_y_fx = BASOP_Util_Divide1616_Scale( mean_y_fx, width, &tmp_e ); /* resultant exp stores in mean_y_e*/
    1243        1252 :                         mean_y_e = add( mean_y_e, sub( tmp_e, 15 ) );
    1244        1252 :                         mean_x_fx = BASOP_Util_Divide1616_Scale( mean_x_fx, width, &tmp_e ); /* resultant exp stores in mean_x_e*/
    1245        1252 :                         mean_x_e = add( mean_x_e, sub( tmp_e, 15 ) );
    1246        1252 :                         mean_xy_fx = BASOP_Util_Divide3216_Scale( mean_xy_fx, width, &tmp_e ); /* resultant exp stores in mean_xy_e*/
    1247        1252 :                         mean_xy_e = add( mean_xy_e, sub( tmp_e, 15 ) );
    1248        1252 :                         mean_x2_fx = BASOP_Util_Divide3216_Scale( mean_x2_fx, width, &tmp_e ); /* resultant exp stores in mean_x2_e*/
    1249        1252 :                         mean_x2_e = add( mean_x2_e, sub( tmp_e, 15 ) );
    1250             : 
    1251             :                         /*slope = ( mean_xy - mean_x * mean_y ) / ( mean_x2 - mean_x * mean_x );*/
    1252        1252 :                         slope_fx = BASOP_Util_Divide3232_Scale( ( L_sub( mean_xy_fx, L_shl( mult( mean_x_fx, mean_y_fx ), sub( add( mean_x_e, mean_y_e ), mean_xy_e ) ) ) ), ( L_sub( mean_x2_fx, L_shl( mult( mean_x_fx, mean_x_fx ), sub( add( mean_x_e, mean_x_e ), mean_x2_e ) ) ) ), &slope_e );
    1253        1252 :                         slope_e = add( slope_e, sub( mean_xy_e, mean_x2_e ) ); /*stores resultant exponent for slope_fx*/
    1254             : 
    1255             :                         /* determine whether strong tilt is due to a step in the spectrum (e.g. band limitation, no damping)
    1256             :                                    or a tonal component close the band border (apply damping) by calculating SFM for a shift of 1/2 SFB width*/
    1257        1252 :                         threshold_fx = BASOP_Util_Divide1616_Scale( 60, width, &threshold_e ); /*stores resultant exponent for threshold_fx*/
    1258        1252 :                         test();
    1259        1252 :                         IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( slope_fx, add( slope_e, 16 ), negate( threshold_fx ), add( threshold_e, 16 ) ), -1 ) )
    1260             :                         {
    1261          31 :                             Word16 shift = shr( width, 1 );
    1262             :                             // shiftedSFM = IGF_getSFM_ivas( pPowerSpectrum, swb_offset[sfb] - shift, swb_offset[sfb + 1] - shift ) / IGF_getCrest_ivas( pPowerSpectrum, swb_offset[sfb] - shift, swb_offset[sfb + 1] - shift );
    1263          31 :                             sfm = IGF_getSFM_ivas_fx( &sfm_exp, pPowerSpectrum_fx, pPowerSpectrum_e, sub( swb_offset[sfb], shift ), sub( swb_offset[sfb + 1], shift ) );
    1264          31 :                             crest = IGF_getCrest_ivas( &crest_exp, pPowerSpectrum_fx, pPowerSpectrum_e, sub( swb_offset[sfb], shift ), sub( swb_offset[sfb + 1], shift ) );
    1265          31 :                             shiftedSFM_fx = BASOP_Util_Divide1616_Scale( sfm, crest, &shiftedSFM_e );
    1266             :                         }
    1267        1221 :                         ELSE IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( slope_fx, add( slope_e, 16 ), threshold_fx, add( threshold_e, 16 ) ), 1 ) && ( NE_16( sfb, sub( hGrid->sfbWrap[hGrid->nTiles], 1 ) ) ) )
    1268             :                         {
    1269             :                             Word16 shift;
    1270           0 :                             shift = shr( width, 1 );
    1271             :                             // shiftedSFM = IGF_getSFM_ivas( pPowerSpectrum, swb_offset[sfb] + shift, swb_offset[sfb + 1] + shift ) / IGF_getCrest_ivas( pPowerSpectrum, swb_offset[sfb] + shift, swb_offset[sfb + 1] + shift );
    1272           0 :                             sfm = IGF_getSFM_ivas_fx( &sfm_exp, pPowerSpectrum_fx, pPowerSpectrum_e, add( swb_offset[sfb], shift ), add( swb_offset[sfb + 1], shift ) );
    1273           0 :                             crest = IGF_getCrest_ivas( &crest_exp, pPowerSpectrum_fx, pPowerSpectrum_e, add( swb_offset[sfb], shift ), add( swb_offset[sfb + 1], shift ) );
    1274           0 :                             shiftedSFM_fx = BASOP_Util_Divide1616_Scale( sfm, crest, &shiftedSFM_e );
    1275             :                         }
    1276        1252 :                         IF( shiftedSFM_fx )
    1277             :                         {
    1278          31 :                             shiftedSFM_e = add( shiftedSFM_e, sub( sfm_exp, crest_exp ) ); /* stores resultant exponent for shiftedSFM_fx*/
    1279             :                         }
    1280             : 
    1281             :                         // alpha = min( 320.f / (float) swb_offset[sfb + 1], 1.25f );
    1282        1252 :                         temp = BASOP_Util_Divide1616_Scale( 320, swb_offset[sfb + 1], &alpha_e );
    1283        1252 :                         alpha_fx = extract_l( L_min( temp, L_shl( 20480 /*1.25 Q14*/, sub( 1, alpha_e ) ) ) ); /* exponent is alpha_e*/
    1284        1252 :                         temp = BASOP_Util_Divide1616_Scale( hPrivateData->SFM_tb_fx[sfb], hPrivateData->SFM_sb_fx[sfb], &tmp_e );
    1285        1252 :                         tmp_e = add( tmp_e, sub( hPrivateData->sfb_tb_e[sfb], hPrivateData->sfb_sb_e[sfb] ) ); /* stores resultant exponent for temp */
    1286             : 
    1287             :                         // currDampingFactor = expf( alpha * logf( hPrivateData->SFM_tb[sfb] / hPrivateData->SFM_sb[sfb] ) );
    1288        1252 :                         currDampingFactor_fx = round_fx( BASOP_util_Pow2( Mpy_32_16_1( L_add( BASOP_Util_Log2( temp ), L_shl( add( 16, tmp_e ), 25 ) ), alpha_fx ), add( alpha_e, 6 ), &currDampingFactor_e ) ); /*exp is currDampingFactor_e*/
    1289             : 
    1290        1252 :                         IF( GT_32( shiftedSFM_fx, L_shl( 1311 /*0.04f Q15*/, negate( shiftedSFM_e ) ) ) )
    1291             :                         {
    1292           2 :                             currDampingFactor_fx = 32767; /*1.f Q15*/
    1293           2 :                             currDampingFactor_e = 0;
    1294           2 :                             move16();
    1295           2 :                             move16();
    1296             :                         }
    1297        1252 :                         test();
    1298        1252 :                         test();
    1299        1252 :                         IF( last_core_acelp || hPrivateData->wasTransient || EQ_32( hPrivateData->prevDampingFactor_IIR_fx[sfb], L_shl( -1, sub( 15, hPrivateData->prevDampingFactor_IIR_e[sfb] ) ) ) )
    1300             :                         {
    1301         576 :                             tmp = BASOP_Util_Cmp_Mant32Exp( currDampingFactor_fx, currDampingFactor_e, 3277, 0 );
    1302         576 :                             IF( tmp >= 0 )
    1303             :                             {
    1304         573 :                                 hPrivateData->prevDampingFactor_IIR_fx[sfb] = currDampingFactor_fx;
    1305         573 :                                 hPrivateData->prevDampingFactor_IIR_e[sfb] = currDampingFactor_e;
    1306             :                             }
    1307             :                             ELSE
    1308             :                             {
    1309           3 :                                 hPrivateData->prevDampingFactor_IIR_fx[sfb] = 3277; /* 0.1 in Q15 */
    1310           3 :                                 hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
    1311             :                             }
    1312             : 
    1313         576 :                             move16();
    1314         576 :                             move16();
    1315             :                         }
    1316             : 
    1317             :                         {
    1318             :                             Word32 tonalToNoise;
    1319             :                             Word16 adap;
    1320             :                             Word16 adap_e;         /*stores exp for adap*/
    1321             :                             Word16 tonalToNoise_e; /*stores exponent for tonalToNoise*/
    1322        1252 :                             tonalToNoise_e = 9;    /*stores exponent for tonalToNoise*/
    1323        1252 :                             move16();
    1324        1252 :                             adap = BASOP_Util_Divide1616_Scale( width, 30, &adap_e );
    1325        1252 :                             tonalToNoise = IGF_getTNR_ivas_fx( pPowerSpectrum_fx, swb_offset[sfb], swb_offset[sfb + 1], adap, pPowerSpectrum_e, adap_e ); /*Q22*/
    1326        1252 :                             IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( tonalToNoise, tonalToNoise_e, L_add( L_shl( 10, sub( 15, adap_e ) ), adap ), add( 16, adap_e ) ), -1 ) )
    1327             :                             {
    1328             :                                 // currDampingFactor += 0.1f * ( ( 10 + adap ) - tonalToNoise );
    1329        1124 :                                 Word32 temp2 = BASOP_Util_Add_Mant32Exp( L_add( L_shl( 10, sub( 15, adap_e ) ) /*exp:adap_e*/, adap ), add( adap_e, 16 ), L_negate( tonalToNoise ), tonalToNoise_e, &tmp_e ); /* resultant exp is tmp_e*/
    1330        1124 :                                 currDampingFactor_e = BASOP_Util_Add_MantExp( currDampingFactor_fx, currDampingFactor_e, extract_l( Mult_32_16( temp2, 3277 /*0.1f Q15*/ ) ), tmp_e, &currDampingFactor_fx ); /*stores resultant exp for currDampingFactor_fx*/
    1331             :                             }
    1332             :                         }
    1333             : 
    1334        1252 :                         dampingFactor_e = BASOP_Util_Add_MantExp( currDampingFactor_fx, currDampingFactor_e, hPrivateData->prevDampingFactor_IIR_fx[sfb], hPrivateData->prevDampingFactor_IIR_e[sfb], &dampingFactor_fx );
    1335        1252 :                         dampingFactor_fx = shr( dampingFactor_fx, 1 ); /* resultant exponent is dampingFactor_e*/
    1336        1252 :                         IF( NE_16( BASOP_Util_Cmp_Mant32Exp( dampingFactor_fx, add( dampingFactor_e, 16 ), shr( hPrivateData->prevDampingFactor_IIR_fx[sfb], 1 ), add( hPrivateData->prevDampingFactor_IIR_e[sfb], 16 ) ), -1 ) )
    1337             :                         {
    1338             :                             // do nothing
    1339             :                         }
    1340             :                         ELSE
    1341             :                         {
    1342         511 :                             dampingFactor_fx = shr( hPrivateData->prevDampingFactor_IIR_fx[sfb], 1 ); /* resultant exponent is hPrivateData->prevDampingFactor_IIR_e[sfb]*/
    1343         511 :                             dampingFactor_e = hPrivateData->prevDampingFactor_IIR_e[sfb];
    1344         511 :                             move16();
    1345             :                         }
    1346        1252 :                         IF( dampingFactor_e < 0 )
    1347             :                         {
    1348          57 :                             dampingFactor_fx = shl( dampingFactor_fx, dampingFactor_e );
    1349          57 :                             dampingFactor_e = 0;
    1350          57 :                             move16();
    1351             :                         }
    1352        1252 :                         gain_fx = Mult_32_16( gain_fx, shl_sat( extract_l( L_min( L_add( dampingFactor_fx, Mult_32_16( L_shl( hPrivateData->dampingFactorSmoothing[sfb], sub( 15, dampingFactor_e ) ) /*Q:15-dampingFactor_e*/, 3277 /*0.1f Q15*/ ) /*Q:15-dampingFactor_e*/ ), shl_sat( 1, sub( 15, dampingFactor_e ) ) ) ), dampingFactor_e ) /*Q15*/ );
    1353             : 
    1354        1252 :                         hPrivateData->prevDampingFactor_IIR_fx[sfb] = dampingFactor_fx;
    1355        1252 :                         hPrivateData->prevDampingFactor_IIR_e[sfb] = dampingFactor_e;
    1356        1252 :                         move16();
    1357        1252 :                         move16();
    1358        1252 :                         if ( hPrivateData->dampingFactorSmoothing[sfb] > 0 )
    1359             :                         {
    1360         693 :                             hPrivateData->dampingFactorSmoothing[sfb] = sub( hPrivateData->dampingFactorSmoothing[sfb], 1 );
    1361         693 :                             move16();
    1362             :                         }
    1363             :                     }
    1364             :                     ELSE
    1365             :                     {
    1366      408472 :                         hPrivateData->prevDampingFactor_IIR_fx[sfb] = -( 1 << 15 ); /* resultant exp which is 0 stores in hPrivateData->prevDampingFactor_IIR_e[sfb]*/
    1367      408472 :                         hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
    1368      408472 :                         hPrivateData->dampingFactorSmoothing[sfb] = 1;
    1369      408472 :                         move16();
    1370      408472 :                         move16();
    1371      408472 :                         move16();
    1372             :                     }
    1373             : 
    1374      409724 :                     hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = tmp_tb_fx;
    1375      409724 :                     hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = hPrivateData->SFM_tb_fx[sfb];
    1376      409724 :                     hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = tmp_sb_fx;
    1377      409724 :                     hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = hPrivateData->SFM_sb_fx[sfb];
    1378      409724 :                     hPrivateData->prevSFB_FIR_TB_e[sfb] = hPrivateData->sfb_tb_e[sfb];
    1379      409724 :                     hPrivateData->prevSFB_IIR_TB_e[sfb] = hPrivateData->sfb_tb_e[sfb];
    1380      409724 :                     hPrivateData->prevSFB_FIR_SB_e[sfb] = hPrivateData->sfb_sb_e[sfb];
    1381      409724 :                     hPrivateData->prevSFB_IIR_SB_e[sfb] = hPrivateData->sfb_sb_e[sfb];
    1382      409724 :                     move16();
    1383      409724 :                     move16();
    1384      409724 :                     move16();
    1385      409724 :                     move16();
    1386      409724 :                     move16();
    1387      409724 :                     move16();
    1388      409724 :                     move16();
    1389      409724 :                     move16();
    1390             :                 }
    1391             :                 ELSE
    1392             :                 {
    1393           0 :                     hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = 0;
    1394           0 :                     hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = 0;
    1395           0 :                     hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = 0;
    1396           0 :                     hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = 0;
    1397           0 :                     hPrivateData->prevSFB_FIR_TB_e[sfb] = 0;
    1398           0 :                     hPrivateData->prevSFB_IIR_TB_e[sfb] = 0;
    1399           0 :                     hPrivateData->prevSFB_FIR_SB_e[sfb] = 0;
    1400           0 :                     hPrivateData->prevSFB_IIR_SB_e[sfb] = 0;
    1401             : 
    1402           0 :                     hPrivateData->dampingFactorSmoothing[sfb] = 2;
    1403           0 :                     move16();
    1404           0 :                     move16();
    1405           0 :                     move16();
    1406           0 :                     move16();
    1407           0 :                     move16();
    1408           0 :                     move16();
    1409           0 :                     move16();
    1410           0 :                     move16();
    1411           0 :                     move16();
    1412             :                 }
    1413             :             }
    1414             :             ELSE
    1415             :             {
    1416       93228 :                 tmp_e = pMDCTSpectrum_e;
    1417       93228 :                 sfbEnergyR_fx = add_sat( EPSILON_FX, BASOP_Util_Divide3216_Scale( sum2_32_fx( pMDCTSpectrum_fx + swb_offset[sfb], width, &tmp_e ) /*exp: tmp_e*/, width, &sfbEnergyR_e ) );
    1418       93228 :                 sfbEnergyR_e = add( sfbEnergyR_e, add( tmp_e, -15 ) ); /* stores resultant exponent for sfbEnergyR_fx*/
    1419       93228 :                 gain_fx = sfbEnergyR_fx;                               /*resultant exponent stored in gain_e=sfbEnergyR_e*/
    1420       93228 :                 move32();
    1421       93228 :                 gain_e = add( sfbEnergyR_e, 16 ); /* because gain_fx is word32;only after adding 16 q of gain_fx is 15-sfbEnergyR_e*/
    1422             : 
    1423       93228 :                 hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = 0;
    1424       93228 :                 hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = 0;
    1425       93228 :                 hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = 0;
    1426       93228 :                 hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = 0;
    1427       93228 :                 hPrivateData->prevSFB_FIR_TB_e[sfb] = 0;
    1428       93228 :                 hPrivateData->prevSFB_IIR_TB_e[sfb] = 0;
    1429       93228 :                 hPrivateData->prevSFB_FIR_SB_e[sfb] = 0;
    1430       93228 :                 hPrivateData->prevSFB_IIR_SB_e[sfb] = 0;
    1431       93228 :                 move16();
    1432       93228 :                 move16();
    1433       93228 :                 move16();
    1434       93228 :                 move16();
    1435       93228 :                 move16();
    1436       93228 :                 move16();
    1437       93228 :                 move16();
    1438       93228 :                 move16();
    1439             :             }
    1440             :             /*gain=0.5f+log2f(gain)*2+16 becuase 2.885390081777927f=2*1/loge(2) so 2*1/loge(2)*loge(x) can be written as 2*log2(x)*/
    1441      502952 :             gain_fx = L_add( ONE_IN_Q22, L_add( L_add( L_shr( BASOP_Util_Log2( gain_fx ), 1 ), L_shl( gain_e, Q24 ) ), L_shl( 16, Q23 ) ) ); /*Q23*/
    1442      502952 :             test();
    1443      502952 :             test();
    1444      502952 :             if ( !isTransient && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) ) )
    1445             :             {
    1446      109015 :                 gain_fx = L_add( gain_fx, ONE_IN_Q21 ); /* better preservation of original HF band energy */
    1447             :             }
    1448      502952 :             test();
    1449      502952 :             test();
    1450      502952 :             if ( !isTransient && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_64000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_64000_CPE ) ) )
    1451             :             {
    1452      179030 :                 gain_fx = L_add( gain_fx, ONE_IN_Q20 );
    1453             :             }
    1454      502952 :             gain_fx = L_min( gain_fx, 91 << Q23 ); /* 13+15+63, see arithcode encode residual */
    1455      502952 :             gain_fx = L_max( gain_fx, 0 );
    1456      502952 :             gain_e = 8; /* stores exponent for gain_fx*/
    1457      502952 :             move16();
    1458      502952 :             hPrivateData->igfScfQuantized[sfb] = (Word16) ( L_shr( gain_fx, 23 ) ); /*Q0*/
    1459      502952 :             move16();
    1460             :         }
    1461             :     }
    1462             : 
    1463      102946 :     return;
    1464             : }
    1465             : 
    1466             : 
    1467             : /*-------------------------------------------------------------------*
    1468             :  * IGF_WriteEnvelope()
    1469             :  *
    1470             :  * writes IGF SCF values
    1471             :  *-------------------------------------------------------------------*/
    1472             : /*! r: number of bits writen */
    1473     1203870 : static Word16 IGF_WriteEnvelope(
    1474             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i  : instance handle of IGF Encoder                                               */
    1475             :     BSTR_ENC_HANDLE hBstr,                 /* i/o: encoder bitstream handle                                                     */
    1476             :     Word16 *pBitOffset,                    /* i  : ptr to bitOffset counter                                                     */
    1477             :     const Word16 igfGridIdx,               /* i  : igf grid index see declaration of IGF_GRID_IDX for details                   */
    1478             :     const Word16 isIndepFlag,              /* i  : if 1 frame is independent, 0 = frame is coded with data from previous frame  */
    1479             :     Word16 *igfAllZero                     /* i  : returns 1 if all IGF scfs are zero, else 0                                   */
    1480             : )
    1481             : {
    1482             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
    1483             :     H_IGF_GRID hGrid;
    1484             :     Word16 sfb;
    1485             :     Word16 totBitCount;
    1486             :     Word16 startBitCount;
    1487             : 
    1488     1203870 :     startBitCount = *pBitOffset;
    1489     1203870 :     totBitCount = 0;
    1490     1203870 :     *igfAllZero = 1;
    1491     1203870 :     move16();
    1492     1203870 :     move16();
    1493     1203870 :     move16();
    1494     1203870 :     hPrivateData = &hIGFEnc->igfData;
    1495     1203870 :     hGrid = &hPrivateData->igfInfo.grid[igfGridIdx];
    1496             : 
    1497     1217661 :     FOR( sfb = hGrid->startSfb; sfb < hGrid->stopSfb; sfb++ )
    1498             :     {
    1499     1215738 :         IF( hPrivateData->igfScfQuantized[sfb] != 0 )
    1500             :         {
    1501     1201947 :             *igfAllZero = 0;
    1502     1201947 :             move16();
    1503     1201947 :             BREAK;
    1504             :         }
    1505             :     }
    1506             : 
    1507     1203870 :     IF( *igfAllZero != 0 )
    1508             :     {
    1509        1923 :         IGF_write_bit_fx( hBstr, pBitOffset, 1 );
    1510             : 
    1511        1923 :         if ( NULL == hBstr )
    1512             :         {
    1513         959 :             IGFSCFEncoderSaveContextState_fx( &hPrivateData->hIGFSCFArithEnc, igfGridIdx );
    1514             :         }
    1515             : 
    1516        1923 :         IGFSCFEncoderReset_fx( &hPrivateData->hIGFSCFArithEnc );
    1517             : 
    1518        1923 :         if ( NULL == hBstr )
    1519             :         {
    1520         959 :             IGFSCFEncoderRestoreContextState_fx( &hPrivateData->hIGFSCFArithEnc, igfGridIdx );
    1521             :         }
    1522             :     }
    1523             :     ELSE
    1524             :     {
    1525     1201947 :         IGF_write_bit_fx( hBstr, pBitOffset, 0 );
    1526             : 
    1527     1201947 :         if ( NULL == hBstr )
    1528             :         {
    1529      589212 :             IGFSCFEncoderSaveContextState_fx( &hPrivateData->hIGFSCFArithEnc, igfGridIdx );
    1530             :         }
    1531             : 
    1532     1201947 :         *pBitOffset = IGFSCFEncoderEncode_ivas_fx( &hPrivateData->hIGFSCFArithEnc, hBstr, *pBitOffset, &hPrivateData->igfScfQuantized[hGrid->startSfb], igfGridIdx, isIndepFlag );
    1533     1201947 :         move16();
    1534             : 
    1535     1201947 :         if ( NULL == hBstr )
    1536             :         {
    1537      589212 :             IGFSCFEncoderRestoreContextState_fx( &hPrivateData->hIGFSCFArithEnc, igfGridIdx );
    1538             :         }
    1539             :     }
    1540     1203870 :     totBitCount = sub( *pBitOffset, startBitCount );
    1541             : 
    1542     1203870 :     return totBitCount;
    1543             : }
    1544             : 
    1545             : 
    1546             : /*-------------------------------------------------------------------*
    1547             :  * IGF_Whitening()
    1548             :  *
    1549             :  * calculates the IGF whitening levels by SFM and crest
    1550             :  *-------------------------------------------------------------------*/
    1551      613699 : static void IGF_Whitening_ivas_fx(
    1552             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i  :     | instance handle of IGF Encoder               */
    1553             :     Word32 *powerSpectrum,                 /* i  : Q31 | MDCT/MDST power spectrum                     */
    1554             :     Word16 *powerSpectrum_e,               /* i  : Q31 | MDCT/MDST power spectrum                     */
    1555             :     const Word16 igfGridIdx,               /* i  : Q0  | IGF grid index                               */
    1556             :     const Word16 isTransient,              /* i  : Q0  | flag indicating if transient is detected     */
    1557             :     const Word16 last_core_acelp,          /* i  : Q0  | indicator if last frame was ACELP core       */
    1558             :     const Word16 isTNSActive,              /* i  : Q0  | indicator if TNS is active                   */
    1559             :     const Word16 sp_aud_decision0,         /* i  : Q0  | first stage classifier decision              */
    1560             :     const Word32 brate,                    /* i  : Q0  | bitrate                                      */
    1561             :     const Word16 element_mode              /* i  : Q0  | element mode                                 */
    1562             : )
    1563             : {
    1564             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
    1565             :     H_IGF_GRID hGrid;
    1566             :     Word16 p; /*Q0*/
    1567             :     Word16 tmp;
    1568             :     Word16 tmp_e;
    1569             :     Word16 SFM_src;
    1570             :     Word16 SFM_tar;
    1571             :     Word16 SFM_src_e;
    1572             :     Word16 SFM_tar_e;
    1573             :     Word16 num_Tiles;
    1574             :     Word16 SFM;
    1575             :     Word16 crest_e;
    1576             : 
    1577      613699 :     SFM = -ONE_IN_Q13; /*1.0f Q13*/
    1578      613699 :     move16();
    1579             : 
    1580      613699 :     hPrivateData = &hIGFEnc->igfData;
    1581      613699 :     hGrid = &hPrivateData->igfInfo.grid[(Word16) igfGridIdx];
    1582             : 
    1583      613699 :     IF( NE_16( igfGridIdx, IGF_GRID_LB_NORM ) )
    1584             :     {
    1585      138255 :         FOR( p = 0; p < hGrid->nTiles; p++ )
    1586             :         {
    1587             :             /* reset filter */
    1588      106199 :             hPrivateData->prevSFM_FIR[p] = L_deposit_l( 0 );
    1589      106199 :             hPrivateData->prevSFM_IIR[p] = 0;
    1590      106199 :             move16();
    1591             : 
    1592             :             /* preset values: */
    1593      106199 :             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1594      106199 :             move16();
    1595             :         }
    1596             :     }
    1597             : 
    1598     6750689 :     FOR( p = 0; p < IGF_MAX_TILES; p++ )
    1599             :     {
    1600             :         /* update prev data: */
    1601     6136990 :         hPrivateData->igfPrevWhiteningLevel[p] = hPrivateData->igfCurrWhiteningLevel[p];
    1602             :         /* preset values: */
    1603     6136990 :         hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1604     6136990 :         move16();
    1605     6136990 :         move16();
    1606             :     }
    1607             : 
    1608      613699 :     test();
    1609      613699 :     IF( !( isTransient || hPrivateData->wasTransient ) )
    1610             :     {
    1611      578218 :         IF( powerSpectrum )
    1612             :         {
    1613     2530693 :             FOR( p = 0; p < hGrid->nTiles; p++ )
    1614             :             {
    1615             :                 Word16 sb;
    1616             : 
    1617     1953761 :                 IF( isTNSActive )
    1618             :                 {
    1619    14877511 :                     FOR( sb = hGrid->tile[p]; sb < hGrid->tile[p + 1]; sb++ )
    1620             :                     {
    1621    14728760 :                         IF( LT_32( powerSpectrum[sb], 1 ) )
    1622       13551 :                         hPrivateData->logSpec[sb] = 0; /* max(0,FLT_MIN_EXP )*/
    1623             :                         ELSE
    1624    14715209 :                             hPrivateData->logSpec[sb] = extract_l( L_max( 0, L_shr( L_add( BASOP_Util_Log2( powerSpectrum[sb] ), L_shl( powerSpectrum_e[sb], Q25 ) ), Q25 ) ) );
    1625    14728760 :                         move16();
    1626             :                     }
    1627             :                 }
    1628             : 
    1629             :                 /* if current tile contains only a single SFB, reuse already computed SFM values */
    1630     1953761 :                 test();
    1631     1953761 :                 IF( element_mode > EVS_MONO && EQ_16( sub( hGrid->sfbWrap[p + 1], hGrid->sfbWrap[p] ), 1 ) )
    1632             :                 {
    1633     1173390 :                     tmp = hPrivateData->SFM_tb_fx[p];
    1634     1173390 :                     tmp_e = hPrivateData->sfb_tb_e[p];
    1635     1173390 :                     move16();
    1636     1173390 :                     move16();
    1637             :                 }
    1638             :                 ELSE
    1639             :                 {
    1640      780371 :                     tmp = BASOP_Util_Divide1616_Scale( IGF_getSFM_new_fx( powerSpectrum, hPrivateData->logSpec, hGrid->tile[p], hGrid->tile[p + 1], powerSpectrum_e ), IGF_getCrest_new_fx( hPrivateData->logSpec, hGrid->tile[p], hGrid->tile[p + 1], &crest_e ), &tmp_e );
    1641      780371 :                     tmp_e = sub( tmp_e, crest_e );
    1642             :                 }
    1643             : 
    1644     1953761 :                 test();
    1645     1953761 :                 IF( last_core_acelp || hPrivateData->wasTransient )
    1646             :                 {
    1647       49204 :                     hPrivateData->prevSFM_FIR[p] = L_shl( tmp, add( 1, tmp_e ) ); /*16-(15-exp)=>15Q16*/
    1648       49204 :                     hPrivateData->prevSFM_IIR[p] = shl( tmp, sub( tmp_e, 2 ) );   /*13-(15-exp)=>2Q13*/
    1649       49204 :                     move32();
    1650       49204 :                     move16();
    1651             :                 }
    1652             : 
    1653     1953761 :                 test();
    1654     1953761 :                 IF( LE_32( brate, IVAS_48k ) && EQ_16( element_mode, IVAS_CPE_MDCT ) )
    1655      450747 :                 {
    1656             :                     Word16 temp;
    1657      450747 :                     num_Tiles = 0;
    1658      450747 :                     SFM_src = 0;
    1659      450747 :                     SFM_tar = 0;
    1660      450747 :                     SFM_src_e = 0;
    1661      450747 :                     SFM_tar_e = 0;
    1662      450747 :                     move16();
    1663      450747 :                     move16();
    1664      450747 :                     move16();
    1665      450747 :                     move16();
    1666      450747 :                     move16();
    1667             : 
    1668      928767 :                     FOR( sb = hGrid->sfbWrap[p]; sb < hGrid->sfbWrap[p + 1]; sb++ )
    1669             :                     {
    1670      478020 :                         num_Tiles = add( num_Tiles, 1 );
    1671      478020 :                         SFM_src_e = BASOP_Util_Add_MantExp( hPrivateData->SFM_sb_fx[sb], hPrivateData->sfb_sb_e[sb], SFM_src, SFM_src_e, &SFM_src );
    1672      478020 :                         SFM_tar_e = BASOP_Util_Add_MantExp( hPrivateData->SFM_tb_fx[sb], hPrivateData->sfb_tb_e[sb], SFM_tar, SFM_tar_e, &SFM_tar );
    1673             :                     }
    1674             : 
    1675             :                     /* compute the average */
    1676      450747 :                     SFM_src = shr( BASOP_Util_Divide1616_Scale( SFM_src, num_Tiles, &temp ), 2 );
    1677      450747 :                     SFM_src_e = add( SFM_src_e, sub( temp, 13 ) ); /*temp-15+2:because right shifted by 2 which are the guard bits*/
    1678      450747 :                     SFM_tar = shr( BASOP_Util_Divide1616_Scale( SFM_tar, num_Tiles, &temp ), 2 );
    1679      450747 :                     SFM_tar_e = add( SFM_tar_e, sub( temp, 13 ) ); /*temp-15+2:because right shifted by 2 which are the guard bits*/
    1680             : 
    1681      450747 :                     IF( LT_16( SFM_tar_e, SFM_src_e ) )
    1682             :                     {
    1683       33827 :                         SFM_tar = shl( SFM_tar, sub( SFM_tar_e, SFM_src_e ) ); /*making the q for SFM_tar and SFM_src equal with 2 as guard bits*/
    1684       33827 :                         SFM_tar_e = SFM_src_e;
    1685             :                     }
    1686             :                     ELSE
    1687             :                     {
    1688      416920 :                         SFM_src = shr( SFM_src, sub( SFM_tar_e, SFM_src_e ) ); /*making the q for SFM_tar and SFM_src equal with 2 as guard bits*/
    1689      416920 :                         SFM_src_e = SFM_tar_e;
    1690             :                     }
    1691      450747 :                     move16();
    1692             : 
    1693      450747 :                     test();
    1694      450747 :                     test();
    1695      450747 :                     IF( ( p > 0 ) && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) ) )
    1696             :                     {
    1697      380168 :                         test();
    1698      380168 :                         if ( EQ_16( p, 1 ) && EQ_16( abs_s( sub( hPrivateData->igfCurrWhiteningLevel[0], hPrivateData->igfCurrWhiteningLevel[1] ) ), 2 ) ) /* OFF vs. STRONG */
    1699             :                         {
    1700        6396 :                             hPrivateData->igfCurrWhiteningLevel[0] = IGF_WHITENING_MID;
    1701        6396 :                             move16();
    1702             :                         }
    1703      380168 :                         hPrivateData->igfCurrWhiteningLevel[p] = hPrivateData->igfCurrWhiteningLevel[p - 1];
    1704      380168 :                         move16();
    1705             :                     }
    1706       70579 :                     ELSE IF( sp_aud_decision0 )
    1707             :                     {
    1708             :                         /* Music */
    1709             :                         /* whitening Off: when tonality of target is more than source or tonality of target is close to that of source */
    1710       33171 :                         test();
    1711       33171 :                         if ( LE_16( SFM_tar, SFM_src ) || LE_32( SFM_tar, L_add( SFM_src, L_shl( 1, sub( 14, SFM_src_e /*0.5 with exponent SFM_src_e*/ ) ) ) ) )
    1712             :                         {
    1713       27578 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1714       27578 :                             move16();
    1715             :                         }
    1716             : 
    1717             :                         /* whitening mid:  */
    1718       33171 :                         test();
    1719       33171 :                         if ( GT_32( SFM_tar, L_add( SFM_src, L_shl( 1, sub( 14, SFM_src_e /*0.5 with exponent SFM_src_e*/ ) ) ) ) && LE_32( SFM_tar, L_add( SFM_src, L_shl( 5, sub( 13, SFM_src_e ) ) /*1.25 with exponent SFM_src_e*/ ) ) )
    1720             :                         {
    1721        5116 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_MID;
    1722        5116 :                             move16();
    1723             :                         }
    1724             : 
    1725             :                         /* whitening strong */
    1726       33171 :                         if ( GT_32( SFM_tar, L_add( SFM_src, L_shl( 5, sub( 13, SFM_src_e ) ) /*1.25 with exponent SFM_src_e*/ ) ) )
    1727             :                         {
    1728         477 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_STRONG;
    1729         477 :                             move16();
    1730             :                         }
    1731             :                     }
    1732             :                     ELSE
    1733             :                     {
    1734             :                         /* Speech */
    1735             :                         /* whitening Off: when tonality of target is more than source or tonality of target is close to that of source */
    1736       37408 :                         test();
    1737       37408 :                         if ( LE_16( SFM_tar, SFM_src ) || LE_32( SFM_tar, L_add( SFM_src, L_shr( 3277 /*0.1 Q15*/, SFM_src_e ) ) ) )
    1738             :                         {
    1739       19142 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1740       19142 :                             move16();
    1741             :                         }
    1742             : 
    1743             :                         /* whitening mid:  */
    1744       37408 :                         test();
    1745       37408 :                         if ( GT_32( SFM_tar, L_add( SFM_src, L_shr( 3277 /*0.1 Q15*/, SFM_src_e ) ) ) && LE_32( SFM_tar, L_add( SFM_src, L_shl( 1, sub( 14, SFM_src_e /*0.5 with exponent SFM_src_e*/ ) ) ) ) )
    1746             :                         {
    1747       11673 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_MID;
    1748       11673 :                             move16();
    1749             :                         }
    1750             : 
    1751             :                         /* whitening strong */
    1752       37408 :                         if ( GT_32( SFM_tar, L_add( SFM_src, L_shl( 1, sub( 14, SFM_src_e /*0.5 with exponent SFM_src_e*/ ) ) ) ) )
    1753             :                         {
    1754        6593 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_STRONG;
    1755        6593 :                             move16();
    1756             :                         }
    1757             :                     }
    1758             : 
    1759      450747 :                     SFM = shl( SFM_tar, sub( SFM_tar_e, 2 ) ); /*2Q13*/
    1760             :                 }
    1761             :                 ELSE
    1762             :                 {
    1763     1503014 :                     test();
    1764     1503014 :                     IF( element_mode > EVS_MONO && EQ_16( sub( hGrid->sfbWrap[p + 1], hGrid->sfbWrap[p] ), 1 ) )
    1765             :                     {
    1766      749916 :                         SFM = shl( tmp, sub( tmp_e, 2 ) ); /*2Q13*/
    1767             :                     }
    1768             :                     ELSE
    1769             :                     {
    1770             :                         Word32 temp;
    1771      753098 :                         temp = L_add( L_shl( tmp, sub( tmp_e, 2 ) ), L_add( L_shr( hPrivateData->prevSFM_FIR[p], 3 ), L_shr( hPrivateData->prevSFM_IIR[p], 1 ) ) );
    1772      753098 :                         SFM = extract_l( L_min( 22118 /*2.7*/, temp ) ); /*2Q13*/
    1773             :                     }
    1774     1503014 :                     hPrivateData->prevSFM_FIR[p] = L_shl( tmp, add( 1, tmp_e ) ); /*15Q16*/
    1775     1503014 :                     hPrivateData->prevSFM_IIR[p] = SFM;
    1776     1503014 :                     move32();
    1777     1503014 :                     move16();
    1778             : 
    1779     1503014 :                     IF( GT_16( SFM, hGrid->whiteningThreshold[1][p] ) )
    1780             :                     {
    1781      580115 :                         hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_STRONG;
    1782      580115 :                         move16();
    1783             :                     }
    1784      922899 :                     ELSE IF( GT_16( SFM, hGrid->whiteningThreshold[0][p] ) )
    1785             :                     {
    1786      589904 :                         hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_MID;
    1787      589904 :                         move16();
    1788             :                     }
    1789             :                     ELSE
    1790             :                     {
    1791      332995 :                         hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1792      332995 :                         move16();
    1793             :                     }
    1794             :                 }
    1795             : 
    1796     1953761 :                 IF( element_mode > EVS_MONO )
    1797             :                 {
    1798     1953761 :                     IF( last_core_acelp ) /* reset */
    1799             :                     {
    1800       49204 :                         set16_fx( hPrivateData->igfPastSFM_fx[p], -ONE_IN_Q13, IGF_PAST_SFM_LEN );
    1801       49204 :                         hPrivateData->igfWhiteningHangoverCnt[p] = 2;
    1802       49204 :                         move16();
    1803             :                     }
    1804             :                     ELSE
    1805             :                     {
    1806     1904557 :                         test();
    1807     1904557 :                         test();
    1808     1904557 :                         test();
    1809     1904557 :                         test();
    1810             :                         /* check whether change in whitening level should be allowed or not (if SFM is inside a certain margin around thresholds) */
    1811     1904557 :                         IF( NE_16( hPrivateData->igfCurrWhiteningLevel[p], hPrivateData->igfPrevWhiteningLevel[p] ) &&
    1812             :                             ( ( GT_32( SFM, L_sub( hGrid->whiteningThreshold[0][p], 1229 /*0.15f Q13*/ ) ) && LT_32( SFM, L_add( hGrid->whiteningThreshold[0][p], 1229 ) ) ) ||
    1813             :                               ( GT_32( SFM, L_sub( hGrid->whiteningThreshold[1][p], 1229 ) ) && LT_32( SFM, L_add( hGrid->whiteningThreshold[1][p], 1229 ) ) ) ) )
    1814             :                         {
    1815             :                             Word16 mean_past_SFM;
    1816             :                             Word16 mean_past_SFM_e;
    1817             :                             Word16 countable;
    1818             :                             Word16 i;
    1819      264973 :                             mean_past_SFM = 0;
    1820      264973 :                             mean_past_SFM_e = 0;
    1821      264973 :                             countable = 0;
    1822      264973 :                             move16();
    1823      264973 :                             move16();
    1824      264973 :                             move16();
    1825             : 
    1826             :                             /* compute mean of last (available) SFM values */
    1827     1589838 :                             FOR( i = 0; i < IGF_PAST_SFM_LEN; i++ )
    1828             :                             {
    1829     1324865 :                                 IF( hPrivateData->igfPastSFM_fx[p][i] >= 0 )
    1830             :                                 {
    1831     1130683 :                                     mean_past_SFM_e = BASOP_Util_Add_MantExp( mean_past_SFM, mean_past_SFM_e, hPrivateData->igfPastSFM_fx[p][i], 2, &mean_past_SFM );
    1832     1130683 :                                     countable = add( countable, 1 );
    1833             :                                 }
    1834             :                             }
    1835      264973 :                             IF( countable )
    1836             :                             {
    1837             :                                 Word16 temp;
    1838      259860 :                                 mean_past_SFM = BASOP_Util_Divide1616_Scale( mean_past_SFM, countable, &temp );
    1839      259860 :                                 mean_past_SFM_e = add( mean_past_SFM_e, sub( temp, 15 ) );
    1840      259860 :                                 mean_past_SFM = shl( mean_past_SFM, sub( mean_past_SFM_e, 2 ) ); /*mean_past_SFM_e=2*/
    1841             :                                 /* deny change in whitening level for small deviations from mean SFM */
    1842      259860 :                                 if ( LT_16( abs_s( sub( SFM, mean_past_SFM ) ), 1638 ) /*0.2 in Q13*/ )
    1843             :                                 {
    1844      125000 :                                     hPrivateData->igfCurrWhiteningLevel[p] = hPrivateData->igfPrevWhiteningLevel[p];
    1845      125000 :                                     move16();
    1846             :                                 }
    1847             :                             }
    1848             :                         }
    1849             :                     }
    1850             : 
    1851     1953761 :                     hPrivateData->igfPastSFM_fx[p][hPrivateData->igfPastSFM_pos] = SFM; /*2Q13*/
    1852     1953761 :                     move16();
    1853             :                 }
    1854             :             }
    1855             : 
    1856      576932 :             SWITCH( hPrivateData->igfInfo.bitRateIndex )
    1857             :             {
    1858       84254 :                 case IGF_BITRATE_WB_9600:
    1859             :                 case IGF_BITRATE_RF_WB_13200:
    1860             :                 case IGF_BITRATE_WB_13200_CPE:
    1861             :                 case IGF_BITRATE_WB_16400_CPE:
    1862             :                 case IGF_BITRATE_RF_SWB_13200:
    1863             :                 case IGF_BITRATE_SWB_9600:
    1864             :                 case IGF_BITRATE_SWB_13200_CPE:
    1865             :                 case IGF_BITRATE_SWB_16400:
    1866             :                 case IGF_BITRATE_SWB_24400:
    1867             :                 case IGF_BITRATE_SWB_24400_CPE:
    1868             :                 case IGF_BITRATE_SWB_32000_CPE:
    1869             :                 case IGF_BITRATE_SWB_32000:
    1870             :                 case IGF_BITRATE_FB_16400:
    1871             :                 case IGF_BITRATE_FB_24400:
    1872             :                 case IGF_BITRATE_FB_24400_CPE:
    1873             :                 case IGF_BITRATE_FB_32000_CPE:
    1874             :                 case IGF_BITRATE_FB_32000:
    1875       84254 :                     hPrivateData->igfCurrWhiteningLevel[hGrid->nTiles - 1] = hPrivateData->igfCurrWhiteningLevel[hGrid->nTiles - 2];
    1876       84254 :                     move16();
    1877       84254 :                     break;
    1878      492678 :                 default:
    1879      492678 :                     break;
    1880             :             }
    1881             :         }
    1882             :         ELSE
    1883             :         {
    1884        5617 :             FOR( p = 0; p < hGrid->nTiles; p++ )
    1885             :             {
    1886        4331 :                 hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_MID;
    1887        4331 :                 move16();
    1888             :             }
    1889             :         }
    1890             :     }
    1891             :     ELSE
    1892             :     {
    1893             :         /* reset filter */
    1894      390291 :         FOR( p = 0; p < IGF_MAX_TILES; p++ )
    1895             :         {
    1896      354810 :             hPrivateData->prevSFM_FIR[p] = L_deposit_l( 0 );
    1897      354810 :             hPrivateData->prevSFM_IIR[p] = 0;
    1898      354810 :             move32();
    1899      354810 :             move16();
    1900             :         }
    1901             :     }
    1902             : 
    1903      613699 :     IF( element_mode > EVS_MONO )
    1904             :     {
    1905      613699 :         IF( EQ_16( SFM, -ONE_IN_Q13 /*1.0f 2Q13*/ ) ) /* reset */
    1906             :         {
    1907      137084 :             FOR( p = 0; p < hGrid->nTiles; p++ )
    1908             :             {
    1909      100317 :                 set16_fx( hPrivateData->igfPastSFM_fx[p], -ONE_IN_Q13, IGF_PAST_SFM_LEN );
    1910      100317 :                 hPrivateData->igfWhiteningHangoverCnt[p] = 2;
    1911      100317 :                 move16();
    1912             :             }
    1913             :         }
    1914             : 
    1915             :         /* vibrato handling */
    1916     1845397 :         FOR( p = 0; p < hGrid->nTiles; p = p + 2 )
    1917             :         {
    1918     1231698 :             test();
    1919     1231698 :             test();
    1920     1231698 :             test();
    1921     1231698 :             IF( ( hPrivateData->igfPrevWhiteningLevel[p] == IGF_WHITENING_OFF && hPrivateData->igfCurrWhiteningLevel[p] != IGF_WHITENING_OFF ) ||
    1922             :                 ( hPrivateData->igfPrevWhiteningLevel[p + 1] == IGF_WHITENING_OFF && hPrivateData->igfCurrWhiteningLevel[p + 1] != IGF_WHITENING_OFF ) )
    1923             :             {
    1924             :                 Word16 i;
    1925             :                 Word16 pastSfm_a[4], pastSfm_b[4];
    1926             :                 Word16 pastSfmDiffSum_a, pastSfmDiffSum_b;
    1927             : 
    1928      928545 :                 FOR( i = 0; i < 4; i++ )
    1929             :                 {
    1930      742836 :                     pastSfm_a[i] = hPrivateData->igfPastSFM_fx[p][add( hPrivateData->igfPastSFM_pos, sub( 4, i ) ) % IGF_PAST_SFM_LEN];
    1931      742836 :                     pastSfm_b[i] = hPrivateData->igfPastSFM_fx[p + 1][add( hPrivateData->igfPastSFM_pos, sub( 4, i ) ) % IGF_PAST_SFM_LEN];
    1932      742836 :                     move16();
    1933      742836 :                     move16();
    1934             :                 }
    1935      185709 :                 pastSfmDiffSum_a = pastSfmDiffSum_b = 0;
    1936      185709 :                 move16();
    1937      185709 :                 move16();
    1938      524772 :                 FOR( i = 0; i < 3; i++ )
    1939             :                 {
    1940      433468 :                     IF( NE_16( pastSfm_a[i + 1], -ONE_IN_Q13 ) )
    1941             :                     {
    1942      339063 :                         pastSfmDiffSum_a = add( pastSfmDiffSum_a, sub( pastSfm_a[i], pastSfm_a[i + 1] ) );
    1943      339063 :                         pastSfmDiffSum_b = add( pastSfmDiffSum_b, sub( pastSfm_b[i], pastSfm_b[i + 1] ) );
    1944             :                     }
    1945             :                     ELSE
    1946             :                     {
    1947       94405 :                         break;
    1948             :                     }
    1949             :                 }
    1950      185709 :                 test();
    1951      185709 :                 test();
    1952      185709 :                 test();
    1953      185709 :                 test();
    1954             :                 /* if tonality oscillates between two tiles, turn whitening off in both */
    1955      185709 :                 IF( ( ( pastSfmDiffSum_a > 0 && pastSfmDiffSum_b < 0 ) ||
    1956             :                       ( pastSfmDiffSum_a < 0 && pastSfmDiffSum_b > 0 ) ) &&
    1957             :                     ( GT_32( L_abs( L_sub( pastSfmDiffSum_a, pastSfmDiffSum_b ) ), ONE_IN_Q12 /* 0.5 in Q13 */ ) ) )
    1958             :                 {
    1959        9101 :                     hPrivateData->igfCurrWhiteningLevel[p] = hPrivateData->igfCurrWhiteningLevel[p + 1] = IGF_WHITENING_OFF;
    1960        9101 :                     move16();
    1961        9101 :                     move16();
    1962             :                 }
    1963             :             }
    1964             :         }
    1965             : 
    1966             :         /* hangover */
    1967     2667777 :         FOR( p = 0; p < hGrid->nTiles; p++ )
    1968             :         {
    1969     2054078 :             IF( NE_16( hPrivateData->igfCurrWhiteningLevel[p], hPrivateData->igfPrevWhiteningLevel[p] ) )
    1970             :             {
    1971      417221 :                 hPrivateData->igfWhiteningHangoverCnt[p] = add( hPrivateData->igfWhiteningHangoverCnt[p], 1 );
    1972      417221 :                 IF( EQ_16( hPrivateData->igfWhiteningHangoverCnt[p], 3 ) )
    1973             :                 {
    1974      115786 :                     hPrivateData->igfWhiteningHangoverCnt[p] = 0;
    1975             :                 }
    1976             :                 ELSE
    1977             :                 {
    1978      301435 :                     hPrivateData->igfCurrWhiteningLevel[p] = hPrivateData->igfPrevWhiteningLevel[p];
    1979             :                 }
    1980      417221 :                 move16();
    1981      417221 :                 move16();
    1982             :             }
    1983             :             ELSE
    1984             :             {
    1985     1636857 :                 hPrivateData->igfWhiteningHangoverCnt[p] = 0;
    1986     1636857 :                 move16();
    1987             :             }
    1988             :         }
    1989             : 
    1990      613699 :         hPrivateData->igfPastSFM_pos = add( hPrivateData->igfPastSFM_pos, 1 ) % IGF_PAST_SFM_LEN;
    1991      613699 :         move16();
    1992             :     }
    1993             : 
    1994      613699 :     hPrivateData->wasTransient = isTransient;
    1995      613699 :     move16();
    1996             : 
    1997      613699 :     return;
    1998             : }
    1999             : 
    2000             : 
    2001             : /*-------------------------------------------------------------------*
    2002             :  * IGF_WriteWhiteningTile_fx()
    2003             :  *
    2004             :  * write whitening levels into bitstream
    2005             :  *-------------------------------------------------------------------*/
    2006             : 
    2007             : /*! r: number of bits written */
    2008     2503437 : static Word16 IGF_WriteWhiteningTile_fx(                        /**< out: Q0 | number of bits written     */
    2009             :                                          BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle       */
    2010             :                                          Word16 *pBitOffset,    /**< in:     | ptr to bitOffset counter   */
    2011             :                                          Word16 whiteningLevel  /**< in: Q0  | whitening levels to write  */
    2012             : )
    2013             : {
    2014             :     Word16 totBitCount;
    2015             :     Word16 startBitCount;
    2016             : 
    2017     2503437 :     totBitCount = 0;
    2018     2503437 :     startBitCount = *pBitOffset;
    2019     2503437 :     move16();
    2020     2503437 :     move16();
    2021             : 
    2022     2503437 :     IF( EQ_32( whiteningLevel, IGF_WHITENING_MID ) )
    2023             :     {
    2024      942644 :         IGF_write_bits( hBstr, pBitOffset, 0, 1 );
    2025             :     }
    2026             :     ELSE
    2027             :     {
    2028     1560793 :         IGF_write_bits( hBstr, pBitOffset, 1, 1 );
    2029     1560793 :         IF( whiteningLevel == IGF_WHITENING_OFF )
    2030             :         {
    2031      780185 :             IGF_write_bits( hBstr, pBitOffset, 0, 1 );
    2032             :         }
    2033             :         ELSE
    2034             :         {
    2035      780608 :             IGF_write_bits( hBstr, pBitOffset, 1, 1 );
    2036             :         }
    2037             :     }
    2038     2503437 :     totBitCount = sub( *pBitOffset, startBitCount );
    2039             : 
    2040     2503437 :     return totBitCount;
    2041             : }
    2042             : 
    2043             : 
    2044             : /*-------------------------------------------------------------------*
    2045             :  * IGF_WriteWhiteningLevels_fx()
    2046             :  *
    2047             :  * writes the whitening levels
    2048             :  *-------------------------------------------------------------------*/
    2049             : 
    2050     1203870 : static Word16 IGF_WriteWhiteningLevels_fx(                                          /**< out: Q0 | total number of bits written                                                 */
    2051             :                                            const IGF_ENC_INSTANCE_HANDLE hInstance, /**< in:     | instance handle of IGF encoder                                               */
    2052             :                                            BSTR_ENC_HANDLE hBstr,                   /* i/o: encoder bitstream handle       */
    2053             :                                            Word16 *pBitOffset,                      /**< in:     | ptr to bitOffset counter                                                     */
    2054             :                                            const Word16 igfGridIdx,                 /**< in: Q0  | igf grid index see declaration of IGF_GRID_IDX for details                   */
    2055             :                                            const Word16 isIndepFlag                 /**< in: Q0  | if 1 frame is independent, 0 = frame is coded with data from previous frame  */
    2056             : )
    2057             : {
    2058             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
    2059             :     H_IGF_GRID hGrid;
    2060             :     Word16 p;
    2061             :     Word16 nTiles;
    2062             :     Word16 isSame;
    2063             :     Word32 tmp32;
    2064             :     Word16 totBitCount;
    2065             :     Word16 startBitCount;
    2066             : 
    2067     1203870 :     totBitCount = 0;
    2068     1203870 :     move16();
    2069     1203870 :     isSame = 1;
    2070     1203870 :     move16();
    2071     1203870 :     startBitCount = *pBitOffset;
    2072     1203870 :     move16();
    2073     1203870 :     hPrivateData = &hInstance->igfData;
    2074     1203870 :     hGrid = &hPrivateData->igfInfo.grid[igfGridIdx];
    2075     1203870 :     nTiles = hGrid->nTiles;
    2076     1203870 :     move16();
    2077             : 
    2078     1203870 :     IF( isIndepFlag )
    2079             :     {
    2080     1192106 :         isSame = 0;
    2081     1192106 :         move16();
    2082             :     }
    2083             :     ELSE
    2084             :     {
    2085       11764 :         p = 0;
    2086       11764 :         move16();
    2087       11764 :         tmp32 = 0;
    2088       11764 :         move32();
    2089       11764 :         test();
    2090       38412 :         WHILE( ( LT_16( p, nTiles ) ) && ( tmp32 == 0 ) )
    2091             :         {
    2092       26648 :             test();
    2093       26648 :             tmp32 = L_sub( hPrivateData->igfCurrWhiteningLevel[p], hPrivateData->igfPrevWhiteningLevel[p] );
    2094       26648 :             if ( tmp32 != 0 )
    2095             :             {
    2096         659 :                 isSame = 0;
    2097         659 :                 move16();
    2098             :             }
    2099       26648 :             p++;
    2100             :         }
    2101             :     }
    2102     1203870 :     IF( isSame )
    2103             :     {
    2104       11105 :         IGF_write_bits( hBstr, pBitOffset, 1, 1 );
    2105             :     }
    2106             :     ELSE
    2107             :     {
    2108     1192765 :         IF( !isIndepFlag )
    2109             :         {
    2110         659 :             IGF_write_bits( hBstr, pBitOffset, 0, 1 );
    2111             :         }
    2112     1192765 :         IGF_WriteWhiteningTile_fx( hBstr, pBitOffset, hPrivateData->igfCurrWhiteningLevel[0] );
    2113     1192765 :         p = 1;
    2114     1192765 :         move16();
    2115     1192765 :         tmp32 = 0;
    2116     1192765 :         move32();
    2117     1192765 :         test();
    2118     1192765 :         IF( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) )
    2119             :         {
    2120      148009 :             isSame = 1;
    2121      148009 :             move16();
    2122             :         }
    2123             :         ELSE
    2124             :         {
    2125     1044756 :             if ( LT_16( p, nTiles ) )
    2126             :             {
    2127      538151 :                 isSame = 1;
    2128      538151 :                 move16();
    2129             :             }
    2130     1044756 :             test();
    2131     2383226 :             WHILE( ( LT_16( p, nTiles ) ) && ( tmp32 == 0 ) )
    2132             :             {
    2133     1338470 :                 test();
    2134     1338470 :                 tmp32 = L_sub( hPrivateData->igfCurrWhiteningLevel[p], hPrivateData->igfCurrWhiteningLevel[p - 1] );
    2135     1338470 :                 if ( tmp32 != 0 )
    2136             :                 {
    2137      296684 :                     isSame = 0;
    2138      296684 :                     move16();
    2139             :                 }
    2140     1338470 :                 p++;
    2141             :             }
    2142             :         }
    2143     1192765 :         test();
    2144     1192765 :         IF( !isSame )
    2145             :         {
    2146      803289 :             IGF_write_bits( hBstr, pBitOffset, 1, 1 );
    2147     2113961 :             FOR( p = 1; p < nTiles; p++ )
    2148             :             {
    2149     1310672 :                 IGF_WriteWhiteningTile_fx( hBstr, pBitOffset, hPrivateData->igfCurrWhiteningLevel[p] );
    2150             :             }
    2151             :         }
    2152      389476 :         ELSE IF( NE_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) && NE_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) )
    2153             :         {
    2154      241467 :             IGF_write_bits( hBstr, pBitOffset, 0, 1 );
    2155             :         }
    2156             :     }
    2157             : 
    2158     1203870 :     totBitCount = sub( *pBitOffset, startBitCount );
    2159             : 
    2160     1203870 :     return totBitCount;
    2161             : }
    2162             : 
    2163             : 
    2164             : /*-------------------------------------------------------------------*
    2165             :  * IGF_WriteFlatteningTrigger_fx()
    2166             :  *
    2167             :  * write flattening trigger
    2168             :  *-------------------------------------------------------------------*/
    2169             : 
    2170             : /*! r: number of bits written */
    2171     1203870 : static Word16 IGF_WriteFlatteningTrigger_fx(                                          /**< out:    | number of bits written         */
    2172             :                                              const IGF_ENC_INSTANCE_HANDLE hInstance, /**< in:     | instance handle of IGF Encoder */
    2173             :                                              BSTR_ENC_HANDLE hBstr,                   /* i/o: encoder bitstream handle       */
    2174             :                                              Word16 *pBitOffset                       /**< in:     | ptr to bitOffset counter       */
    2175             : )
    2176             : {
    2177             :     Word16 flatteningTrigger;
    2178             : 
    2179             :     Word16 totBitCount;
    2180             :     Word16 startBitCount;
    2181     1203870 :     totBitCount = 0;
    2182     1203870 :     startBitCount = *pBitOffset;
    2183     1203870 :     flatteningTrigger = hInstance->flatteningTrigger;
    2184     1203870 :     move16();
    2185     1203870 :     move16();
    2186     1203870 :     move16();
    2187             : 
    2188     1203870 :     IGF_write_bits( hBstr, pBitOffset, flatteningTrigger, 1 );
    2189     1203870 :     totBitCount = sub( *pBitOffset, startBitCount );
    2190             : 
    2191     1203870 :     return totBitCount;
    2192             : }
    2193             : 
    2194             : 
    2195             : /*-------------------------------------------------------------------*
    2196             :  * IGF_UpdateInfo()
    2197             :  *
    2198             :  * updates the start/stop frequency of IGF according to igfGridIdx
    2199             :  *-------------------------------------------------------------------*/
    2200             : 
    2201             : /*-------------------------------------------------------------------*
    2202             :  * IGFEncWriteBitstream()
    2203             :  *
    2204             :  * IGF bitstream writer
    2205             :  *-------------------------------------------------------------------*/
    2206             : 
    2207             : /*! r: number of bits written per frame */
    2208     1203870 : Word16 IGFEncWriteBitstream_ivas_fx(
    2209             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i  : instance handle of IGF Encoder                                              */
    2210             :     BSTR_ENC_HANDLE hBstr,                 /* i/o: encoder bitstream handle                                                    */
    2211             :     Word16 *pBitOffset,                    /* i  : ptr to bitOffset counter                                                    */
    2212             :     const Word16 igfGridIdx,               /* i  : igf grid index see declaration of IGF_GRID_IDX for details                  */
    2213             :     const Word16 isIndepFlag               /* i  : if 1 frame is independent, 0 = frame is coded with data from previous frame */
    2214             : )
    2215             : {
    2216             :     Word16 igfAllZero;
    2217             :     Word16 startBitCount;
    2218             : 
    2219     1203870 :     startBitCount = *pBitOffset;
    2220     1203870 :     move16();
    2221     1203870 :     hIGFEnc->infoTotalBitsPerFrameWritten = 0;
    2222     1203870 :     move16();
    2223             : 
    2224     1203870 :     if ( isIndepFlag )
    2225             :     {
    2226     1192106 :         hIGFEnc->infoTotalBitsWritten = 0;
    2227     1192106 :         move16();
    2228             :     }
    2229             : 
    2230     1203870 :     IGF_WriteEnvelope( hIGFEnc,       /* i: instance handle of IGF Encoder                                              */
    2231             :                        hBstr,         /* i: encoder state                                                               */
    2232             :                        pBitOffset,    /* i: ptr to bitOffset counter                                                    */
    2233             :                        igfGridIdx,    /* i: igf grid index see definition of IGF_GRID_IDX for details                   */
    2234             :                        isIndepFlag,   /* i: if 1 frame is independent, 0 = frame is coded with data from previous frame */
    2235             :                        &igfAllZero ); /* o: *igfAllZero                                                                 */
    2236             : 
    2237     1203870 :     IGF_WriteWhiteningLevels_fx( hIGFEnc,       /* i: instance handle of IGF Encoder                                              */
    2238             :                                  hBstr,         /* i: encoder state                                                               */
    2239             :                                  pBitOffset,    /* i: ptr to bitOffset counter                                                    */
    2240             :                                  igfGridIdx,    /* i: igf grid index see definition of IGF_GRID_IDX for details                   */
    2241             :                                  isIndepFlag ); /* i: if 1 frame is independent, 0 = frame is coded with data from previous frame */
    2242             : 
    2243     1203870 :     IGF_WriteFlatteningTrigger_fx( hIGFEnc,      /* i: instance handle of IGF Encoder                                              */
    2244             :                                    hBstr,        /* i: encoder state                                                               */
    2245             :                                    pBitOffset ); /* i: ptr to bitOffset counter                                                    */
    2246             : 
    2247     1203870 :     hIGFEnc->infoTotalBitsPerFrameWritten = sub( *pBitOffset, startBitCount );
    2248     1203870 :     hIGFEnc->infoTotalBitsWritten = add( hIGFEnc->infoTotalBitsWritten, hIGFEnc->infoTotalBitsPerFrameWritten );
    2249     1203870 :     move16();
    2250     1203870 :     move16();
    2251             : 
    2252     1203870 :     return hIGFEnc->infoTotalBitsPerFrameWritten;
    2253             : }
    2254             : 
    2255             : 
    2256             : /*-------------------------------------------------------------------*
    2257             :  * IGFEncSetMode()
    2258             :  *
    2259             :  * sets the IGF mode according to given bitrate
    2260             :  *-------------------------------------------------------------------*/
    2261       37162 : void IGFEncSetMode_ivas_fx(
    2262             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i/o: instance handle of IGF Encoder */
    2263             :     const Word32 total_brate,              /* i  : encoder total bitrate          */
    2264             :     const Word16 bwidth,                   /* i  : encoder audio bandwidth        */
    2265             :     const Word16 element_mode,             /* i  : IVAS element mode              */
    2266             :     const Word16 rf_mode                   /* i  : flag to signal the RF mode     */
    2267             : )
    2268             : {
    2269             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
    2270             :     Word16 i;
    2271             : 
    2272       37162 :     hPrivateData = &hIGFEnc->igfData;
    2273       37162 :     hPrivateData->igfBitstreamBits = 0;
    2274       37162 :     move16();
    2275       37162 :     set16_fx( hPrivateData->igfScfQuantized, 0, IGF_MAX_SFB );
    2276       37162 :     set16_fx( hPrivateData->igfCurrWhiteningLevel, 0, IGF_MAX_TILES );
    2277       37162 :     set16_fx( hPrivateData->igfPrevWhiteningLevel, 0, IGF_MAX_TILES );
    2278       37162 :     set16_fx( hPrivateData->igfWhiteningHangoverCnt, 0, IGF_MAX_TILES );
    2279      408782 :     FOR( i = 0; i < IGF_MAX_TILES; i++ )
    2280             :     {
    2281      371620 :         set16_fx( hPrivateData->igfPastSFM_fx[i], -( ONE_IN_Q13 ), IGF_PAST_SFM_LEN );
    2282             :     }
    2283             : 
    2284       37162 :     hPrivateData->igfPastSFM_pos = 0;
    2285       37162 :     move16();
    2286             : 
    2287    11929002 :     FOR( i = 0; i < IGF_BITBUFSIZE / 8; i++ )
    2288             :     {
    2289    11891840 :         hPrivateData->igfBitstream[i] = 0;
    2290    11891840 :         move16();
    2291             :     }
    2292       37162 :     hPrivateData->wasTransient = 0;
    2293       37162 :     move16();
    2294       37162 :     set32_fx( hPrivateData->prevSFM_FIR, 0, IGF_MAX_TILES );
    2295       37162 :     set16_fx( hPrivateData->prevSFM_IIR, 0, IGF_MAX_TILES );
    2296       37162 :     set16_fx( hPrivateData->dampingFactorSmoothing, 2, IGF_MAX_SFB );
    2297       37162 :     set16_fx( hPrivateData->prevSFM_FIR_SFB_SB_fx, 0, IGF_MAX_SFB );
    2298       37162 :     set16_fx( hPrivateData->prevSFB_FIR_TB_e, 15, IGF_MAX_SFB );
    2299       37162 :     set16_fx( hPrivateData->prevSFB_FIR_SB_e, 15, IGF_MAX_SFB );
    2300       37162 :     set16_fx( hPrivateData->prevSFM_IIR_SFB_SB_fx, 0, IGF_MAX_SFB );
    2301       37162 :     set16_fx( hPrivateData->prevSFB_IIR_SB_e, 15, IGF_MAX_SFB );
    2302       37162 :     set16_fx( hPrivateData->prevSFM_FIR_SFB_TB_fx, 0, IGF_MAX_SFB );
    2303       37162 :     set16_fx( hPrivateData->prevSFB_IIR_TB_e, 15, IGF_MAX_SFB );
    2304       37162 :     set16_fx( hPrivateData->sfb_tb_e, 15, IGF_MAX_SFB );
    2305       37162 :     set16_fx( hPrivateData->sfb_sb_e, 15, IGF_MAX_SFB );
    2306       37162 :     set16_fx( hPrivateData->prevSFM_IIR_SFB_TB_fx, 0, IGF_MAX_SFB );
    2307       37162 :     set16_fx( hPrivateData->prevDampingFactor_IIR_fx, -( ONE_IN_Q15 ), IGF_MAX_SFB );
    2308       37162 :     set16_fx( hPrivateData->prevDampingFactor_IIR_e, 0, IGF_MAX_SFB );
    2309       37162 :     set16_fx( hPrivateData->logSpec, 0, L_FRAME_PLUS );
    2310       37162 :     set16_fx( hPrivateData->SFM_sb_fx, 0, IGF_MAX_SFB );
    2311       37162 :     set16_fx( hPrivateData->SFM_tb_fx, 0, IGF_MAX_SFB );
    2312             : 
    2313       37162 :     IF( IGFCommonFuncsIGFConfiguration_ivas_fx( total_brate, bwidth, element_mode, &hPrivateData->igfInfo, rf_mode ) != 0 )
    2314             :     {
    2315       37143 :         IGFSCFEncoderOpen_fx( &hPrivateData->hIGFSCFArithEnc, &hPrivateData->igfInfo, total_brate, bwidth, element_mode, rf_mode );
    2316             : 
    2317       37143 :         hIGFEnc->infoSamplingRate = hPrivateData->igfInfo.sampleRate;
    2318       37143 :         move32();
    2319       37143 :         hIGFEnc->infoStartFrequency = hPrivateData->igfInfo.grid[0].startFrequency;
    2320       37143 :         move16();
    2321       37143 :         hIGFEnc->infoStopFrequency = hPrivateData->igfInfo.grid[0].stopFrequency;
    2322       37143 :         move16();
    2323       37143 :         hIGFEnc->infoStartLine = hPrivateData->igfInfo.grid[0].startLine;
    2324       37143 :         move16();
    2325       37143 :         hIGFEnc->infoStopLine = hPrivateData->igfInfo.grid[0].stopLine;
    2326       37143 :         move16();
    2327             :     }
    2328             :     ELSE
    2329             :     {
    2330             :         /* IGF configuration failed -> error! */
    2331          19 :         hIGFEnc->infoSamplingRate = 0;
    2332          19 :         move32();
    2333          19 :         hIGFEnc->infoStartFrequency = -1;
    2334          19 :         move16();
    2335          19 :         hIGFEnc->infoStopFrequency = -1;
    2336          19 :         move16();
    2337          19 :         hIGFEnc->infoStartLine = -1;
    2338          19 :         move16();
    2339          19 :         hIGFEnc->infoStopLine = -1;
    2340          19 :         move16();
    2341          19 :         fprintf( stderr, "IGFEncSetMode_fx: initialization error!\n" );
    2342             :     }
    2343             : 
    2344             :     /* reset remaining variables */
    2345       37162 :     hIGFEnc->infoTotalBitsWritten = 0;
    2346       37162 :     move16();
    2347       37162 :     hIGFEnc->infoTotalBitsPerFrameWritten = 0;
    2348       37162 :     move16();
    2349       37162 :     hIGFEnc->flatteningTrigger = 0;
    2350       37162 :     move16();
    2351       37162 :     hIGFEnc->spec_be_igf_e = 0;
    2352       37162 :     move16();
    2353       37162 :     hIGFEnc->tns_predictionGain = 0;
    2354       37162 :     move16();
    2355       37162 :     set32_fx( hIGFEnc->spec_be_igf, 0, N_MAX_TCX - IGF_START_MN );
    2356       37162 :     return;
    2357             : }
    2358             : 
    2359             : 
    2360             : /*-------------------------------------------------------------------*
    2361             :  * IGFEncConcatenateBitstream()
    2362             :  *
    2363             :  * IGF bitstream concatenation for TCX10 modes
    2364             :  *-------------------------------------------------------------------*/
    2365             : 
    2366             : /*-------------------------------------------------------------------*
    2367             :  * IGFEncResetTCX10BitCounter_ivas_fx()
    2368             :  *
    2369             :  * IGF reset bitstream bit counter for TCX10 modes
    2370             :  *-------------------------------------------------------------------*/
    2371             : 
    2372           0 : void IGFEncResetTCX10BitCounter_ivas_fx(
    2373             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc /* i  : instance handle of IGF Encoder */
    2374             : )
    2375             : {
    2376             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
    2377             : 
    2378           0 :     hPrivateData = &hIGFEnc->igfData;
    2379           0 :     hPrivateData->igfBitstreamBits = 0;
    2380           0 :     hIGFEnc->infoTotalBitsWritten = 0;
    2381           0 :     move16();
    2382           0 :     move16();
    2383           0 :     return;
    2384             : }
    2385             : 
    2386             : 
    2387             : /*-------------------------------------------------------------------*
    2388             :  * IGFEncApplyMono()
    2389             :  *
    2390             :  * apply the IGF encoder, main encoder interface
    2391             :  *-------------------------------------------------------------------*/
    2392             : 
    2393      510753 : void IGFEncApplyMono_ivas_fx(
    2394             :     Encoder_State *st,             /* i  : Encoder state                                          */
    2395             :     Word16 powerSpectrum_len,      /* i: length of pPowerSpectrum_fx buffer */
    2396             :     const Word16 igfGridIdx,       /* i  : IGF grid index                                         */
    2397             :     Word32 *pMDCTSpectrum_fx,      /* i/o: MDCT spectrum                                          */
    2398             :     Word16 e_mdct,                 /* i : exponent of pMDCTspectrum                                                        */
    2399             :     Word32 *pPowerSpectrum_fx,     /* i/o: MDCT^2 + MDST^2 spectrum, or estimate                  */
    2400             :     Word16 *e_ps,                  /* i : exponent of pPowerSpectrum                                                       */
    2401             :     const Word16 isTCX20,          /* i  : flag indicating if the input is TCX20 or TCX10/2xTCX5  */
    2402             :     const Word8 isTNSActive,       /* i  : flag indicating if the TNS is active                   */
    2403             :     const Word16 sp_aud_decision0, /* i  : first stage switching decision                         */
    2404             :     const Word16 vad_hover_flag    /* i  : VAD hangover flag                                      */
    2405             : )
    2406             : {
    2407             :     Word32 *pPowerSpectrumParameter_fx;
    2408             :     Word16 *pPowerSpectrumParameter_exp;
    2409      510753 :     Word16 att_fx = MAX16B;
    2410             :     Word16 last_core_acelp;
    2411      510753 :     move16();
    2412             : 
    2413             :     Word32 common_pPowerSpectrum_fx[N_MAX + L_MDCT_OVLP_MAX];
    2414             : 
    2415      510753 :     set32_fx( common_pPowerSpectrum_fx, 0, N_MAX );
    2416             : 
    2417      510753 :     Word16 common_pPowerSpectrum_exp = MIN16B;
    2418      510753 :     move16();
    2419      510753 :     IF( st->last_core == ACELP_CORE )
    2420             :     {
    2421        8528 :         last_core_acelp = 1;
    2422        8528 :         move16();
    2423             :     }
    2424             :     ELSE
    2425             :     {
    2426      502225 :         last_core_acelp = 0;
    2427      502225 :         move16();
    2428             :     }
    2429             : 
    2430      510753 :     test();
    2431      510753 :     IF( !isTNSActive && isTCX20 )
    2432             :     {
    2433      447512 :         pPowerSpectrumParameter_fx = pPowerSpectrum_fx;
    2434      447512 :         pPowerSpectrumParameter_exp = e_ps;
    2435             :     }
    2436             :     ELSE
    2437             :     {
    2438       63241 :         pPowerSpectrumParameter_fx = NULL;
    2439       63241 :         pPowerSpectrumParameter_exp = NULL;
    2440             :     }
    2441             : 
    2442      510753 :     IGF_UpdateInfo( st->hIGFEnc, igfGridIdx );
    2443             : 
    2444      510753 :     test();
    2445      510753 :     IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) || EQ_16( st->element_mode, IVAS_CPE_TD ) )
    2446             :     {
    2447       12683 :         calculate_hangover_attenuation_gain_ivas_fx( st, &att_fx, vad_hover_flag );
    2448             :     }
    2449             : 
    2450      510753 :     IGF_CalculateEnvelope_ivas_fx( st->hIGFEnc, pMDCTSpectrum_fx, e_mdct, pPowerSpectrumParameter_fx, pPowerSpectrumParameter_exp, igfGridIdx, st->hTranDet->transientDetector.bIsAttackPresent, last_core_acelp, st->element_mode, att_fx );
    2451             : 
    2452      510753 :     IF( isTCX20 )
    2453             :     {
    2454      491639 :         pPowerSpectrumParameter_fx = pPowerSpectrum_fx;
    2455      491639 :         pPowerSpectrumParameter_exp = e_ps;
    2456             :     }
    2457             :     ELSE
    2458             :     {
    2459       19114 :         pPowerSpectrumParameter_fx = NULL;
    2460       19114 :         pPowerSpectrumParameter_exp = NULL;
    2461             :     }
    2462             : 
    2463      510753 :     IF( EQ_16( st->element_mode, IVAS_CPE_MDCT ) )
    2464             :     {
    2465      283946 :         IGF_Whitening_ivas_fx( st->hIGFEnc, pPowerSpectrumParameter_fx, pPowerSpectrumParameter_exp, igfGridIdx, st->hTranDet->transientDetector.bIsAttackPresent, last_core_acelp, isTNSActive, sp_aud_decision0, st->element_brate, st->element_mode );
    2466             :     }
    2467             :     ELSE
    2468             :     {
    2469      226807 :         IGF_Whitening_ivas_fx( st->hIGFEnc, pPowerSpectrumParameter_fx, pPowerSpectrumParameter_exp, igfGridIdx, st->hTranDet->transientDetector.bIsAttackPresent, last_core_acelp, isTNSActive, sp_aud_decision0, st->total_brate, st->element_mode );
    2470             :     }
    2471             : 
    2472      510753 :     IF( pPowerSpectrumParameter_fx )
    2473             :     {
    2474   642250739 :         FOR( Word16 i = 0; i < powerSpectrum_len; i++ )
    2475             :         {
    2476   641759100 :             common_pPowerSpectrum_exp = s_max( common_pPowerSpectrum_exp, pPowerSpectrumParameter_exp[i] );
    2477             :         }
    2478             : 
    2479   642250739 :         FOR( Word16 i = 0; i < powerSpectrum_len; i++ )
    2480             :         {
    2481   641759100 :             common_pPowerSpectrum_fx[i] = L_shl( pPowerSpectrumParameter_fx[i], sub( pPowerSpectrumParameter_exp[i], common_pPowerSpectrum_exp ) );
    2482   641759100 :             move16();
    2483             :         }
    2484      491639 :         pPowerSpectrumParameter_fx = common_pPowerSpectrum_fx;
    2485             :     }
    2486      510753 :     IGF_ErodeSpectrum_ivas_fx( st->hIGFEnc, pMDCTSpectrum_fx, pPowerSpectrumParameter_fx, common_pPowerSpectrum_exp, igfGridIdx, 0 );
    2487      510753 : }
    2488             : 
    2489             : 
    2490             : /*-------------------------------------------------------------------*
    2491             :  * IGFEncApplyStereo()
    2492             :  *
    2493             :  * apply the IGF encoder, main encoder interface
    2494             :  *-------------------------------------------------------------------*/
    2495             : 
    2496       51473 : void IGFEncApplyStereo_fx(
    2497             :     STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct,                /* i/o: MDCT stereo encoder structure           */
    2498             :     Word16 ms_mask[2][MAX_SFB],                             /* i  : bandwise MS mask                        */
    2499             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc[CPE_CHANNELS],    /* i  : instance handle of IGF Encoder          */
    2500             :     const Word16 igfGridIdx,                                /* i  : IGF grid index                          */
    2501             :     Encoder_State *sts[CPE_CHANNELS],                       /* i  : Encoder state                           */
    2502             :     Word32 *pPowerSpectrum_fx[CPE_CHANNELS],                /* i/o: MDCT^2 + MDST^2 spectrum, or estimate   */
    2503             :     Word16 *exp_pPowerSpectrum_fx[CPE_CHANNELS],            /* i/o: exp of pPowerSpectrum_fx                */
    2504             :     Word32 *pPowerSpectrumMsInv_fx[CPE_CHANNELS][NB_DIV],   /* i/o: inverse power spectrum                  */
    2505             :     Word16 *q_pPowerSpectrumMsInv_fx[CPE_CHANNELS][NB_DIV], /* i/o: Q of pPowerSpectrumMsInv_fx           */
    2506             :     Word32 *inv_spectrum_fx[CPE_CHANNELS][NB_DIV],          /* i  : inverse spectrum                        */
    2507             :     Word16 exp_inv_spectrum_fx[CPE_CHANNELS],               /* i  : exp of inverse spectrum                 */
    2508             :     const Word16 frameno,                                   /* i  : flag indicating index of current subfr. */
    2509             :     const Word16 sp_aud_decision0,                          /* i  : sp_aud_decision0                        */
    2510             :     const Word32 element_brate,                             /* i  : element bitrate                         */
    2511             :     const Word16 mct_on )
    2512             : {
    2513             :     Word32 *pPowerSpectrumParameter_fx[NB_DIV];     /* If it is NULL it informs a function that specific handling is needed */
    2514             :     Word16 *exp_pPowerSpectrumParameter_fx[NB_DIV]; /* If it is NULL it informs a function that specific handling is needed */
    2515             :     Word32 *pPowerSpectrumParameterMsInv_fx[NB_DIV];
    2516             :     Word16 *q_pPowerSpectrumParameterMsInv_fx[NB_DIV];
    2517             :     Word16 coreMsMask[N_MAX];
    2518             :     Word16 sfb, ch, last_core_acelp;
    2519             :     STEREO_MDCT_BAND_PARAMETERS *sfbConf;
    2520             :     Word32 common_pPowerSpectrum_fx[N_MAX];
    2521             : 
    2522       51473 :     set32_fx( common_pPowerSpectrum_fx, 0, N_MAX );
    2523             : 
    2524       51473 :     Word16 common_pPowerSpectrum_exp = MIN16B;
    2525       51473 :     move16();
    2526             : 
    2527             :     /* assumptions: stereo filling was already done on the flattened spectra
    2528             :      *               IGF region is always coded M/S, never L/R (to be done in the encoder)
    2529             :      *               for residual bands with stereo filling infoTcxNoise is set to zero
    2530             :      *               both channels have the same IGF configuration
    2531             :      */
    2532             : 
    2533             :     /* sanity checks: check if both channels have the same configuration...*/
    2534       51473 :     assert( ( sts[0]->core == sts[1]->core ) );
    2535             : 
    2536             :     /* initialization */
    2537       51473 :     IF( EQ_16( sts[0]->core, TCX_20_CORE ) )
    2538             :     {
    2539       49266 :         sfbConf = &hStereoMdct->stbParamsTCX20;
    2540             :     }
    2541             :     ELSE
    2542             :     {
    2543        2207 :         sfbConf = &hStereoMdct->stbParamsTCX10;
    2544             :     }
    2545       51473 :     if ( sts[0]->last_core == ACELP_CORE )
    2546             :     {
    2547           0 :         sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
    2548             :     }
    2549             : 
    2550             :     /* create line wise ms mask for the core bands */
    2551       51473 :     set16_fx( coreMsMask, 0, N_MAX );
    2552     2195705 :     FOR( sfb = 0; sfb < sfbConf->sfbCnt; sfb++ )
    2553             :     {
    2554     2144232 :         set16_fx( &coreMsMask[sfbConf->sfbOffset[sfb]], ms_mask[frameno][sfb], sub( sfbConf->sfbOffset[sfb + 1], sfbConf->sfbOffset[sfb] ) );
    2555             :     }
    2556             : 
    2557       51473 :     test();
    2558       51473 :     test();
    2559       51473 :     IF( EQ_16( sts[0]->core, TCX_20_CORE ) && !sts[0]->hTcxEnc->fUseTns[frameno] && !sts[1]->hTcxEnc->fUseTns[frameno] )
    2560             :     {
    2561       41918 :         pPowerSpectrumParameter_fx[0] = &pPowerSpectrum_fx[0][0];
    2562       41918 :         exp_pPowerSpectrumParameter_fx[0] = &exp_pPowerSpectrum_fx[0][0];
    2563       41918 :         pPowerSpectrumParameter_fx[1] = &pPowerSpectrum_fx[1][0];
    2564       41918 :         exp_pPowerSpectrumParameter_fx[1] = &exp_pPowerSpectrum_fx[1][0];
    2565       41918 :         pPowerSpectrumParameterMsInv_fx[0] = pPowerSpectrumMsInv_fx[0][0];
    2566       41918 :         pPowerSpectrumParameterMsInv_fx[1] = pPowerSpectrumMsInv_fx[1][0];
    2567       41918 :         q_pPowerSpectrumParameterMsInv_fx[0] = q_pPowerSpectrumMsInv_fx[0][0];
    2568       41918 :         q_pPowerSpectrumParameterMsInv_fx[1] = q_pPowerSpectrumMsInv_fx[1][0];
    2569             :     }
    2570             :     ELSE
    2571             :     {
    2572        9555 :         pPowerSpectrumParameter_fx[0] = NULL;
    2573        9555 :         pPowerSpectrumParameter_fx[1] = NULL;
    2574        9555 :         pPowerSpectrumParameterMsInv_fx[0] = NULL;
    2575        9555 :         pPowerSpectrumParameterMsInv_fx[1] = NULL;
    2576        9555 :         q_pPowerSpectrumParameterMsInv_fx[0] = NULL;
    2577        9555 :         q_pPowerSpectrumParameterMsInv_fx[1] = NULL;
    2578             :     }
    2579      154419 :     FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
    2580             :     {
    2581      102946 :         last_core_acelp = extract_l( EQ_16( sts[ch]->last_core, ACELP_CORE ) );
    2582             : 
    2583      102946 :         IGF_UpdateInfo( hIGFEnc[ch], igfGridIdx );
    2584      102946 :         IGF_CalculateStereoEnvelope_fx( hIGFEnc[ch], sts[ch]->hTcxEnc->spectrum_fx[frameno], sts[ch]->hTcxEnc->spectrum_e[frameno], inv_spectrum_fx[ch][frameno],
    2585      102946 :                                         exp_inv_spectrum_fx[ch], pPowerSpectrumParameter_fx[ch], exp_pPowerSpectrum_fx[ch], pPowerSpectrumParameterMsInv_fx[ch],
    2586      102946 :                                         q_pPowerSpectrumParameterMsInv_fx[ch], igfGridIdx, coreMsMask, sts[ch]->hTranDet->transientDetector.bIsAttackPresent, last_core_acelp, mct_on );
    2587             : 
    2588      102946 :         IF( EQ_16( sts[ch]->core, TCX_20_CORE ) )
    2589             :         {
    2590       98532 :             pPowerSpectrumParameter_fx[ch] = pPowerSpectrum_fx[ch];
    2591       98532 :             exp_pPowerSpectrumParameter_fx[ch] = exp_pPowerSpectrum_fx[ch];
    2592             :         }
    2593             :         ELSE
    2594             :         {
    2595        4414 :             pPowerSpectrumParameter_fx[ch] = NULL;
    2596             :         }
    2597             : 
    2598      102946 :         IGF_Whitening_ivas_fx( hIGFEnc[ch], pPowerSpectrumParameter_fx[ch], exp_pPowerSpectrumParameter_fx[ch], igfGridIdx, sts[ch]->hTranDet->transientDetector.bIsAttackPresent, last_core_acelp, ( sts[0]->hTcxEnc->fUseTns[frameno] || sts[1]->hTcxEnc->fUseTns[frameno] ), sp_aud_decision0, element_brate, sts[ch]->element_mode );
    2599             : 
    2600      102946 :         IF( pPowerSpectrumParameter_fx[ch] )
    2601             :         {
    2602       98532 :             Word16 length = N_MAX;
    2603       98532 :             move16();
    2604       98532 :             if ( mct_on )
    2605             :             {
    2606       63416 :                 length = L_FRAME48k;
    2607       63416 :                 move16();
    2608             :             }
    2609   103117092 :             FOR( Word16 i = 0; i < length; i++ )
    2610             :             {
    2611   103018560 :                 common_pPowerSpectrum_exp = s_max( common_pPowerSpectrum_exp, exp_pPowerSpectrumParameter_fx[ch][i] );
    2612             :             }
    2613             : 
    2614   103117092 :             FOR( Word16 i = 0; i < length; i++ )
    2615             :             {
    2616   103018560 :                 common_pPowerSpectrum_fx[i] = L_shl( pPowerSpectrumParameter_fx[ch][i], sub( exp_pPowerSpectrumParameter_fx[ch][i], common_pPowerSpectrum_exp ) );
    2617   103018560 :                 move32();
    2618             :             }
    2619       98532 :             pPowerSpectrumParameter_fx[ch] = common_pPowerSpectrum_fx;
    2620             :         }
    2621      102946 :         IGF_ErodeSpectrum_ivas_fx( hIGFEnc[ch], sts[ch]->hTcxEnc->spectrum_fx[frameno], pPowerSpectrumParameter_fx[ch], common_pPowerSpectrum_exp, igfGridIdx, mct_on );
    2622             :     }
    2623       51473 :     return;
    2624             : }
    2625             : 
    2626             : 
    2627             : /*-------------------------------------------------------------------*
    2628             :  * IGFSaveSpectrumForITF()
    2629             :  *
    2630             :  *
    2631             :  *-------------------------------------------------------------------*/
    2632             : 
    2633      613699 : void IGFSaveSpectrumForITF_ivas_fx(
    2634             :     IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i/o: instance handle of IGF Encoder  */
    2635             :     const Word16 igfGridIdx,         /* i  : IGF grid index                  */
    2636             :     const Word32 *pITFSpectrum,      /* i  : MDCT spectrum                   */
    2637             :     Word16 exp_pITFSpectrum )
    2638             : {
    2639      613699 :     IGF_UpdateInfo( hIGFEnc, igfGridIdx );
    2640             : 
    2641      613699 :     Copy32( pITFSpectrum + IGF_START_MN, hIGFEnc->spec_be_igf, sub( hIGFEnc->infoStopLine, IGF_START_MN ) );
    2642             : 
    2643      613699 :     scale_sig32( hIGFEnc->spec_be_igf, sub( hIGFEnc->infoStopLine, IGF_START_MN ), sub( exp_pITFSpectrum, s_max( exp_pITFSpectrum, hIGFEnc->spec_be_igf_e ) ) );
    2644      613699 :     scale_sig32( hIGFEnc->spec_be_igf + sub( hIGFEnc->infoStopLine, IGF_START_MN ), sub( N_MAX_TCX - IGF_START_MN, sub( hIGFEnc->infoStopLine, IGF_START_MN ) ), sub( hIGFEnc->spec_be_igf_e, s_max( exp_pITFSpectrum, hIGFEnc->spec_be_igf_e ) ) );
    2645      613699 :     hIGFEnc->spec_be_igf_e = s_max( exp_pITFSpectrum, hIGFEnc->spec_be_igf_e );
    2646      613699 :     move16();
    2647             : 
    2648      613699 :     return;
    2649             : }
    2650             : 
    2651        3460 : ivas_error IGF_Reconfig(
    2652             :     IGF_ENC_INSTANCE_HANDLE *hIGFEnc, /* i/o: instance handle of IGF Encoder  */
    2653             :     const Word16 igf,                 /* i  : IGF on/off                      */
    2654             :     const Word16 reset,               /* i  : reset flag                      */
    2655             :     const Word32 brate,               /* i  : bitrate for configuration       */
    2656             :     const Word16 bwidth,              /* i  : signal bandwidth                */
    2657             :     const Word16 element_mode,        /* i  : IVAS element mode               */
    2658             :     const Word16 rf_mode              /* i  : flag to signal the RF mode      */
    2659             : )
    2660             : {
    2661             :     ivas_error error;
    2662             : 
    2663        3460 :     error = IVAS_ERR_OK;
    2664        3460 :     move32();
    2665             : 
    2666        3460 :     test();
    2667        3460 :     test();
    2668        3460 :     test();
    2669        3460 :     IF( igf && *hIGFEnc == NULL )
    2670             :     {
    2671         451 :         IF( ( *hIGFEnc = (IGF_ENC_INSTANCE_HANDLE) malloc( sizeof( IGF_ENC_INSTANCE ) ) ) == NULL )
    2672             :         {
    2673           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hIGFEnc\n" ) );
    2674             :         }
    2675         451 :         IGFEncSetMode_ivas_fx( *hIGFEnc, brate, bwidth, element_mode, rf_mode );
    2676             :     }
    2677        3009 :     ELSE IF( igf && reset )
    2678             :     {
    2679        1971 :         IGFEncSetMode_ivas_fx( *hIGFEnc, brate, bwidth, element_mode, rf_mode );
    2680             :     }
    2681        1038 :     ELSE IF( !igf && *hIGFEnc != NULL )
    2682             :     {
    2683         655 :         free( *hIGFEnc );
    2684         655 :         *hIGFEnc = NULL;
    2685             :     }
    2686             : 
    2687        3460 :     return error;
    2688             : }

Generated by: LCOV version 1.14