LCOV - code coverage report
Current view: top level - lib_enc - igf_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 574a190e3c6896c6c4ed10d7f23649709a0c4347 Lines: 1277 1329 96.1 %
Date: 2025-06-27 02:59:36 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.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 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     7312392 : 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     7312392 :     IF( hBstr )
      63             :     {
      64     3699340 :         push_next_indice( hBstr, value, 1 );
      65             :     }
      66             : 
      67     7312392 :     ( *bitCount ) = add( ( *bitCount ), 1 );
      68     7312392 :     move16();
      69             : 
      70     7312392 :     return;
      71             : }
      72             : 
      73             : /*-------------------------------------------------------------------*
      74             :  * IGF_write_bits()
      75             :  *
      76             :  * write bits to stream
      77             :  *-------------------------------------------------------------------*/
      78     6149426 : 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    12298852 :     WHILE( bits-- )
      86             :     {
      87     6149426 :         IF( s_and( value, shl( 1, bits ) ) == 0 )
      88             :         {
      89     1945243 :             IGF_write_bit_fx( hBstr, bitCount, 0 );
      90             :         }
      91             :         ELSE
      92             :         {
      93     4204183 :             IGF_write_bit_fx( hBstr, bitCount, 1 );
      94             :         }
      95             :     }
      96             : 
      97     6149426 :     return;
      98             : }
      99             : 
     100             : 
     101             : /*-------------------------------------------------------------------*
     102             :  * IGF_getCrest_new()
     103             :  *
     104             :  * crest factor calculation
     105             :  *-------------------------------------------------------------------*/
     106             : 
     107             : /*! r: crest factor */
     108     5374680 : 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     5374680 :     x_eff = 0;
     124     5374680 :     x_max = 0;
     125     5374680 :     exp = 0;
     126     5374680 :     temp = 0;
     127     5374680 :     crest = 32767; /*1.0f in Q15*/
     128     5374680 :     move32();
     129     5374680 :     move16();
     130     5374680 :     move16();
     131     5374680 :     move16();
     132     5374680 :     move16();
     133             : 
     134   369020772 :     FOR( i = start; i < stop; i++ )
     135             :     {
     136   363646092 :         x = logSpec[i];
     137   363646092 :         move16();
     138   363646092 :         x_eff = L_add( x_eff, L_mult0( x, x ) );
     139             : 
     140   363646092 :         IF( GT_16( x, x_max ) )
     141             :         {
     142    16644639 :             x_max = x;
     143    16644639 :             move16();
     144             :         }
     145             :     }
     146             : 
     147     5374680 :     x_eff = BASOP_Util_Divide3216_Scale( x_eff, sub( stop, start ), &temp_e );
     148     5374680 :     temp_e = add( temp_e, 16 ); /*exp += 31 - 15 + 16(because x_eff is word32)*/
     149     5374680 :     x_eff = L_shr( x_eff, sub( 15, temp_e ) );
     150     5374680 :     temp_e = 15;
     151     5374680 :     move16();
     152     5374680 :     temp = Sqrt16( extract_l( x_eff ), &temp_e );
     153             : 
     154     5374680 :     test();
     155     5374680 :     IF( x_eff > 0 && x_max > 0 )
     156             :     {
     157     4763418 :         temp = BASOP_Util_Divide1616_Scale( x_max, temp, &exp );
     158     4763418 :         exp = add( exp, sub( 15, temp_e ) );
     159     4763418 :         IF( exp < 0 )
     160             :         {
     161           0 :             temp = shl( temp, exp );
     162           0 :             exp = 0;
     163           0 :             move16();
     164             :         }
     165     4763418 :         crest = s_max( shl_sat( 1, sub( 15, exp ) ), temp );
     166             :     }
     167     5374680 :     *crest_exp = exp;
     168     5374680 :     move16();
     169     5374680 :     return crest;
     170             : }
     171             : 
     172             : 
     173             : /*-------------------------------------------------------------------*
     174             :  * IGF_getSFM_new()
     175             :  *
     176             :  * calculates spectral flatness measurement
     177             :  *-------------------------------------------------------------------*/
     178             : 
     179             : /*! r: SFM value */
     180     5374680 : 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     5374680 :     num = 0;
     201     5374680 :     denom = ONE_IN_Q30;
     202     5374680 :     denom_e = 1;
     203     5374680 :     sfm = MAX16B; // Q15
     204     5374680 :     move16();
     205     5374680 :     move32();
     206     5374680 :     move16();
     207     5374680 :     move16();
     208             : 
     209   369020772 :     FOR( i = start; i < stop; i++ )
     210             :     {
     211   363646092 :         tmp = powerSpectrum[i];
     212   363646092 :         n = logSpec[i /*-start*/];
     213   363646092 :         move32();
     214   363646092 :         move16();
     215   363646092 :         num = add( num, n );
     216   363646092 :         denom = BASOP_Util_Add_Mant32Exp( tmp, e_ps[i], denom, denom_e, &denom_e );
     217             :     }
     218             : 
     219     5374680 :     numf = BASOP_Util_Divide1616_Scale( num, sub( stop, start ), &numf_e );
     220     5374680 :     denom = BASOP_Util_Divide3216_Scale( denom, sub( stop, start ), &tmp_e );
     221     5374680 :     denom_e = add( add( denom_e, tmp_e ), 1 ); /*denom_e+tmp_e-15 +16(because type of denom is word32)*/
     222             : 
     223     5374680 :     IF( denom != 0 )
     224             :     {
     225     5374680 :         tmp = BASOP_util_Pow2( L_add( numf, shl_sat( 1, sub( 14, numf_e ) ) ), add( 16, numf_e ), &tmp_e );
     226     5374680 :         sfm = BASOP_Util_Divide3232_Scale( tmp, denom, &sfm_e );
     227     5374680 :         sfm_e = add( sfm_e, sub( tmp_e, denom_e ) );
     228     5374680 :         sfm = shl_sat( extract_l( L_min( sfm, L_shl_sat( 1, sub( 15, sfm_e ) ) ) ), sfm_e );
     229             :     }
     230             : 
     231     5374680 :     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       12860 : 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       12860 :     avg = 0;
     272       12860 :     tonal = 0;
     273       12860 :     noise = EPSILON_FX;
     274       12860 :     tonal_e = 0;
     275       12860 :     noise_e = 0;
     276       12860 :     avg_e = 0;
     277       12860 :     tmp_e = 0;
     278       12860 :     move32();
     279       12860 :     move32();
     280       12860 :     move32();
     281       12860 :     move16();
     282       12860 :     move16();
     283       12860 :     move16();
     284       12860 :     move16();
     285             : 
     286       12860 :     set32_fx( rootSpec, 0, 300 );
     287       12860 :     set16_fx( rootSpec_e, 0, 300 );
     288             : 
     289       12860 :     width = sub( stop, start );
     290      789022 :     FOR( i = start; i < stop; i++ )
     291             :     {
     292      776162 :         rootSpec_e[( i - start )] = e_ps[i];
     293      776162 :         move16();
     294      776162 :         rootSpec[( i - start )] = Sqrt32( powerSpectrum[i], &rootSpec_e[( i - start )] ); /*rootSpec[i - start] = sqrtf( powerSpectrum[i] );*/
     295      776162 :         move32();
     296      776162 :         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       12860 :     avg = BASOP_Util_Divide3216_Scale( avg, width, &tmp_e ); /*avg /= width;*/
     299       12860 :     avg_e = add( 16, sub( add( avg_e, tmp_e ), 15 ) );
     300             : 
     301      789022 :     FOR( i = start; i < stop; i++ )
     302             :     {
     303             :         Word16 normSpec_e;                                                                      /*stores resultant exponent for normSpec*/
     304      776162 :         Word16 normSpec = BASOP_Util_Divide3232_Scale( rootSpec[i - start], avg, &normSpec_e ); /*rootSpec[i - start] / avg;*/
     305      776162 :         normSpec_e = add( normSpec_e, sub( rootSpec_e[i - start], avg_e ) );
     306      776162 :         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       62124 :             tonal = BASOP_Util_Add_Mant32Exp( tonal, tonal_e, rootSpec[( i - start )], rootSpec_e[( i - start )], &tonal_e ); /*tonal += rootSpec[i - start];*/
     309             :         }
     310      714038 :         ELSE IF( LT_32( normSpec, L_shl_sat( 1, sub( 15, normSpec_e ) ) ) )
     311             :         {
     312      632112 :             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       12860 :     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       12860 :         Word16 temp = BASOP_Util_Divide3232_Scale( tonal, noise, &tmp_e ); /*tonal / noise*/
     324       12860 :         tmp_e = add( tmp_e, sub( tonal_e, noise_e ) );
     325       12860 :         IF( GE_16( temp, 1 ) )
     326             :         {
     327       12860 :             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       12860 :     return tonalToNoise;
     337             : }
     338             : 
     339             : /*-------------------------------------------------------------------*
     340             :  * IGF_CalculateEnvelope()
     341             :  *
     342             :  * envelope estimation
     343             :  *-------------------------------------------------------------------*/
     344             : 
     345      492473 : 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      492473 :     hPrivateData = &hIGFEnc->igfData;
     394      492473 :     hGrid = &hPrivateData->igfInfo.grid[(Word16) igfGridIdx];
     395      492473 :     swb_offset = hGrid->swb_offset;
     396             : 
     397      492473 :     IF( element_mode > EVS_MONO )
     398             :     {
     399      492473 :         IF( igfGridIdx != IGF_GRID_LB_NORM )
     400             :         {
     401      160229 :             FOR( sfbCnt = 0; sfbCnt < sub( hGrid->sfbWrap[hGrid->nTiles], hGrid->sfbWrap[0] ); sfbCnt++ )
     402             :             {
     403             :                 /* reset filter */
     404      133878 :                 hPrivateData->prevSFM_FIR_SFB_TB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_FIR_TB_e[sfbCnt]
     405      133878 :                 hPrivateData->prevSFB_FIR_TB_e[sfbCnt] = 0;
     406      133878 :                 hPrivateData->prevSFM_IIR_SFB_TB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_IIR_TB_e[sfbCnt]
     407      133878 :                 hPrivateData->prevSFB_IIR_TB_e[sfbCnt] = 0;
     408      133878 :                 hPrivateData->prevSFM_FIR_SFB_SB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_FIR_SB_e[sfbCnt]
     409      133878 :                 hPrivateData->prevSFB_FIR_SB_e[sfbCnt] = 0;
     410      133878 :                 hPrivateData->prevSFM_IIR_SFB_SB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_IIR_SB_e[sfbCnt]
     411      133878 :                 hPrivateData->prevSFB_IIR_SB_e[sfbCnt] = 0;
     412      133878 :                 hPrivateData->prevDampingFactor_IIR_fx[sfbCnt] = MIN16B; /* -1.f in Q15*/
     413      133878 :                 hPrivateData->prevDampingFactor_IIR_e[sfbCnt] = 0;
     414      133878 :                 hPrivateData->dampingFactorSmoothing[sfbCnt] = 2;
     415      133878 :                 move16();
     416      133878 :                 move16();
     417      133878 :                 move16();
     418      133878 :                 move16();
     419      133878 :                 move16();
     420      133878 :                 move16();
     421      133878 :                 move16();
     422      133878 :                 move16();
     423      133878 :                 move16();
     424      133878 :                 move16();
     425      133878 :                 move16();
     426             :             }
     427             :         }
     428             :     }
     429             : 
     430      492473 :     IF( pPowerSpectrum_fx )
     431             :     {
     432   227204362 :         FOR( sb = hGrid->sbWrap[0]; sb < swb_offset[hGrid->sfbWrap[hGrid->nTiles]]; sb++ )
     433             :         {
     434   226772428 :             IF( LT_32( 1, pPowerSpectrum_fx[sb] ) )
     435             :             {
     436   226232162 :                 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   226232162 :                 move16();
     438             :             }
     439             :             ELSE
     440             :             {
     441      540266 :                 hPrivateData->logSpec[sb] = 0;
     442      540266 :                 move16();
     443             :             }
     444             :         }
     445             :     }
     446             : 
     447     2206807 :     FOR( tile_idx = 0; tile_idx < hGrid->nTiles; tile_idx++ )
     448             :     {
     449     1714334 :         strt_cpy = hGrid->sbWrap[tile_idx];
     450     1714334 :         move16();
     451             : 
     452     4312418 :         FOR( sfb = hGrid->sfbWrap[tile_idx]; sfb < hGrid->sfbWrap[tile_idx + 1]; sfb++ )
     453             :         {
     454     2598084 :             width = sub( swb_offset[sfb + 1], swb_offset[sfb] );
     455     2598084 :             sfbEnergyTileR = EPSILON_FIX;
     456     2598084 :             sfbEnergyTileC = EPSILON_FIX;
     457     2598084 :             sfbEnergyC = EPSILON_FX;
     458     2598084 :             sfbEnergyTileR_e = 0;
     459     2598084 :             sfbEnergyTileC_e = 0;
     460     2598084 :             sfbEnergyC_e = 0;
     461     2598084 :             move16();
     462     2598084 :             move16();
     463     2598084 :             move16();
     464     2598084 :             move16();
     465     2598084 :             move16();
     466     2598084 :             move16();
     467             : 
     468     2598084 :             IF( pPowerSpectrum_fx != NULL )
     469             :             {
     470     2322435 :                 tmp = strt_cpy;
     471     2322435 :                 move16();
     472   134681981 :                 FOR( sb = swb_offset[sfb]; sb < swb_offset[sfb + 1]; sb++ )
     473             :                 {
     474   132359546 :                     Word16 shift = norm_l( pPowerSpectrum_fx[sb] );
     475   132359546 :                     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   132359546 :                     Word64 tmp64 = W_mult_32_32( pMDCTSpectrum_fx[strt_cpy], pMDCTSpectrum_fx[strt_cpy] );
     478   132359546 :                     Word16 tmp64_e = W_norm( tmp64 );
     479   132359546 :                     tmp64 = W_shl( tmp64, tmp64_e );
     480             : 
     481   132359546 :                     sfbEnergyTileR = BASOP_Util_Add_Mant32Exp( sfbEnergyTileR, sfbEnergyTileR_e, W_extract_h( tmp64 ), shl( e_mdct, 1 ) - tmp64_e, &sfbEnergyTileR_e );
     482   132359546 :                     shift = norm_l( pPowerSpectrum_fx[strt_cpy] );
     483   132359546 :                     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   132359546 :                     strt_cpy = add( strt_cpy, 1 );
     486             :                 }
     487             : 
     488     2322435 :                 sfbEnergyTileR = L_deposit_h( BASOP_Util_Divide3232_Scale( sfbEnergyTileR, width, &tmp_e ) );
     489     2322435 :                 sfbEnergyTileR_e = add( sub( sfbEnergyTileR_e, Q31 ), tmp_e );
     490             : 
     491     2322435 :                 IF( sfbEnergyTileR == 0 )
     492             :                 {
     493         787 :                     sfbEnergyTileR = EPSILON_FX;
     494         787 :                     sfbEnergyTileR_e = 0;
     495         787 :                     move32();
     496         787 :                     move16();
     497             :                 }
     498     2322435 :                 IF( sfbEnergyC == 0 )
     499             :                 {
     500         787 :                     sfbEnergyC = EPSILON_FX;
     501         787 :                     sfbEnergyC_e = 0;
     502         787 :                     move32();
     503         787 :                     move16();
     504             :                 }
     505     2322435 :                 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     2321648 :                     temp = BASOP_Util_Divide3232_Scale( sfbEnergyC, sfbEnergyTileC, &tmp_e );
     518     2321648 :                     tmp_e = add( tmp_e, sub( sfbEnergyC_e, sfbEnergyTileC_e ) );
     519             :                 }
     520             : 
     521     2322435 :                 gain = Mult_32_16( sfbEnergyTileR, temp ); // gain_e
     522     2322435 :                 gain_e = add( tmp_e, sfbEnergyTileR_e );
     523             : 
     524     2322435 :                 IF( element_mode > EVS_MONO )
     525             :                 {
     526     2322435 :                     test();
     527     2322435 :                     IF( !isTransient )
     528             :                     {
     529             :                         Word16 diffSFM;
     530     2315703 :                         Word16 shiftedSFM = 0;
     531     2315703 :                         Word16 shiftedSFM_e = 0;
     532     2315703 :                         move16();
     533     2315703 :                         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     2315703 :                         sfm = IGF_getSFM_new_fx( pPowerSpectrum_fx, hPrivateData->logSpec, swb_offset[sfb], swb_offset[sfb + 1], e_ps );
     537     2315703 :                         sfm_exp = 0;
     538     2315703 :                         move16();
     539     2315703 :                         crest = IGF_getCrest_new_fx( hPrivateData->logSpec, swb_offset[sfb], swb_offset[sfb + 1], &crest_exp );
     540     2315703 :                         tmp_tb = BASOP_Util_Divide1616_Scale( sfm, crest, &tmp_e ); // tmp_tb_e
     541     2315703 :                         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     2315703 :                         sfm = IGF_getSFM_new_fx( pPowerSpectrum_fx, hPrivateData->logSpec, tmp, strt_cpy, e_ps );
     545     2315703 :                         crest = IGF_getCrest_new_fx( hPrivateData->logSpec, tmp, strt_cpy, &crest_exp );
     546     2315703 :                         tmp_sb = BASOP_Util_Divide1616_Scale( sfm, crest, &tmp_e ); // tmp_sb_e
     547     2315703 :                         tmp_sb_e = add( tmp_e, sub( sfm_exp, crest_exp ) );
     548             : 
     549     2315703 :                         test();
     550     2315703 :                         IF( last_core_acelp || hPrivateData->wasTransient )
     551             :                         {
     552       88985 :                             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       88985 :                             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       88985 :                             hPrivateData->prevSFB_FIR_TB_e[sfb] = hPrivateData->prevSFB_IIR_TB_e[sfb] = tmp_tb_e;
     555       88985 :                             hPrivateData->prevSFB_FIR_SB_e[sfb] = hPrivateData->prevSFB_IIR_SB_e[sfb] = tmp_sb_e;
     556       88985 :                             move16();
     557       88985 :                             move16();
     558       88985 :                             move16();
     559       88985 :                             move16();
     560       88985 :                             move16();
     561       88985 :                             move16();
     562       88985 :                             move16();
     563       88985 :                             move16();
     564             :                         }
     565             : 
     566     2315703 :                         tmp_tb = shr( tmp_tb, 2 ); /*taking 2 guard bits so it's exponent tmp_sb_e=+2*/
     567     2315703 :                         tmp_sb = shr( tmp_sb, 2 ); /*taking 2 guard bits so it's exponent tmp_tb_e=+2 */
     568     2315703 :                         tmp_sb_e = add( tmp_sb_e, 2 );
     569     2315703 :                         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     2315703 :                         tmp0 = shr( hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb], 2 );
     574     2315703 :                         tmp0_e = add( hPrivateData->prevSFB_FIR_TB_e[sfb], 2 );
     575     2315703 :                         move16();
     576     2315703 :                         tmp2 = shr( hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb], 2 );
     577     2315703 :                         tmp2_e = add( hPrivateData->prevSFB_IIR_TB_e[sfb], 2 );
     578     2315703 :                         move16();
     579     2315703 :                         tmp3 = shr( hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb], 2 );
     580     2315703 :                         tmp3_e = add( hPrivateData->prevSFB_FIR_SB_e[sfb], 2 );
     581     2315703 :                         move16();
     582     2315703 :                         tmp4 = shr( hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb], 2 );
     583     2315703 :                         tmp4_e = add( hPrivateData->prevSFB_IIR_SB_e[sfb], 2 );
     584     2315703 :                         move16();
     585             :                         Word16 x1, x2;
     586             : 
     587     2315703 :                         Word16 x1_e = BASOP_Util_Add_MantExp( tmp0, tmp0_e, tmp2, tmp2_e - 1, &x1 );
     588     2315703 :                         Word16 x2_e = BASOP_Util_Add_MantExp( tmp3, tmp3_e, tmp4, tmp4_e - 1, &x2 );
     589     2315703 :                         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     2315703 :                         move16();
     591     2315703 :                         BASOP_Util_Add_MantExp( 22118, 2, negate( hPrivateData->SFM_tb_fx[sfb] ), hPrivateData->sfb_tb_e[sfb], &tmp );
     592             : 
     593     2315703 :                         IF( tmp < 0 )
     594             :                         {
     595      269703 :                             hPrivateData->sfb_tb_e[sfb] = 2;
     596      269703 :                             hPrivateData->SFM_tb_fx[sfb] = 22118;
     597      269703 :                             move16();
     598      269703 :                             move16();
     599             :                         }
     600             : 
     601     2315703 :                         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     2315703 :                         move16();
     603     2315703 :                         BASOP_Util_Add_MantExp( 22118, 2, negate( hPrivateData->SFM_sb_fx[sfb] ), hPrivateData->sfb_sb_e[sfb], &tmp );
     604             : 
     605     2315703 :                         IF( tmp < 0 )
     606             :                         {
     607      154244 :                             hPrivateData->sfb_sb_e[sfb] = 2;
     608      154244 :                             hPrivateData->SFM_sb_fx[sfb] = 22118;
     609      154244 :                             move16();
     610      154244 :                             move16();
     611             :                         }
     612             : 
     613     2315703 :                         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     2315703 :                         test();
     616     2315703 :                         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       11647 :                         {
     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       11647 :                             x = 1;
     627       11647 :                             mean_x_fx = mean_y_fx = 0;
     628       11647 :                             Word32 mean_y_fx_tmp = 0;
     629       11647 :                             move32();
     630       11647 :                             mean_xy_fx = mean_x2_fx = 0;
     631       11647 :                             mean_x_e = 15;
     632       11647 :                             mean_xy_e = mean_y_e = mean_x2_e = 31;
     633       11647 :                             move16();
     634       11647 :                             move16();
     635       11647 :                             move16();
     636       11647 :                             move16();
     637       11647 :                             move16();
     638       11647 :                             move16();
     639       11647 :                             move16();
     640       11647 :                             move16();
     641       11647 :                             move16();
     642             : 
     643      721493 :                             FOR( sb = swb_offset[sfb]; sb < swb_offset[sfb + 1]; sb++ )
     644             :                             {
     645      709846 :                                 mean_x_fx = add( mean_x_fx, x );                   /*Q0*/
     646      709846 :                                 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      709846 :                                 IF( LE_64( W_deposit32_l( pPowerSpectrum_fx[sb] ), W_shl( 1, ( sub( 31, e_ps[sb] ) ) ) ) )
     650             :                                 {
     651       88649 :                                     y = 0;
     652       88649 :                                     move16();
     653             :                                 }
     654             :                                 ELSE
     655             :                                 {
     656      621197 :                                     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      709846 :                                 mean_y_fx_tmp = L_mac0( mean_y_fx_tmp, y, 1 );     /*Q0*/
     659      709846 :                                 mean_xy_fx = L_add( mean_xy_fx, L_mult0( y, x ) ); /*Q0*/
     660             : 
     661      709846 :                                 x = add( x, 1 );
     662             :                             }
     663       11647 :                             mean_y_fx = BASOP_Util_Divide3216_Scale( mean_y_fx_tmp, width, &tmp_e ); /* mean_y_e*/
     664             : 
     665       11647 :                             mean_y_e = add( mean_y_e, sub( tmp_e, 15 ) );
     666       11647 :                             mean_x_fx = BASOP_Util_Divide1616_Scale( mean_x_fx, width, &tmp_e ); /* mean_x_e*/
     667       11647 :                             mean_x_e = add( mean_x_e, sub( tmp_e, 15 ) );
     668       11647 :                             mean_xy_fx = BASOP_Util_Divide3216_Scale( mean_xy_fx, width, &tmp_e ); /* mean_xy_e*/
     669       11647 :                             mean_xy_e = add( mean_xy_e, sub( tmp_e, 15 ) );
     670       11647 :                             mean_x2_fx = BASOP_Util_Divide3216_Scale( mean_x2_fx, width, &tmp_e ); /* mean_x2_e*/
     671       11647 :                             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       11647 :                             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       11647 :                             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       11647 :                             threshold = BASOP_Util_Divide1616_Scale( 60, width, &threshold_e );
     680             : 
     681       11647 :                             test();
     682       11647 :                             IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( slope, add( slope_e, 16 ), negate( threshold ), add( threshold_e, 16 ) ), -1 ) )
     683             :                             {
     684         137 :                                 Word16 shift = shr( width, 1 );
     685         137 :                                 sfm = IGF_getSFM_new_fx( pPowerSpectrum_fx, hPrivateData->logSpec, swb_offset[sfb] - shift, swb_offset[sfb + 1] - shift, e_ps );
     686         137 :                                 crest = IGF_getCrest_new_fx( hPrivateData->logSpec, swb_offset[sfb] - shift, swb_offset[sfb + 1] - shift, &crest_exp );
     687         137 :                                 shiftedSFM = BASOP_Util_Divide1616_Scale( sfm, crest, &shiftedSFM_e );
     688             :                             }
     689       11510 :                             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       11647 :                             shiftedSFM_e = add( shiftedSFM_e, sub( sfm_exp, crest_exp ) );
     698             : 
     699       11647 :                             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       11626 :                                 temp = BASOP_Util_Divide1616_Scale( 320, swb_offset[sfb + 1], &alpha_e );
     710       11626 :                                 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       11626 :                                 temp = BASOP_Util_Divide1616_Scale( hPrivateData->SFM_tb_fx[sfb], hPrivateData->SFM_sb_fx[sfb], &tmp_e ); // tmp_e
     714       11626 :                                 tmp_e = add( tmp_e, sub( hPrivateData->sfb_tb_e[sfb], hPrivateData->sfb_sb_e[sfb] ) );
     715       11626 :                                 Word16 temp1 = mult( 20480 /* 1.25f in Q14 */, alpha );
     716       11626 :                                 Word16 tmp1_e = add( 1, alpha_e );
     717       11626 :                                 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       11626 :                                 tonalToNoise_e = 9;
     726       11626 :                                 move16();
     727       11626 :                                 adap = BASOP_Util_Divide1616_Scale( width, 40, &adap_e );
     728       11626 :                                 tonalToNoise = IGF_getTNR_ivas_fx( pPowerSpectrum_fx, swb_offset[sfb], swb_offset[sfb + 1], adap, e_ps, adap_e ); /*Q22*/
     729             : 
     730       11626 :                                 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        8381 :                                     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        8381 :                                     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       11647 :                             Word32 L_tmp = hPrivateData->prevDampingFactor_IIR_fx[sfb];
     739       11647 :                             move32();
     740       11647 :                             L_tmp = L_shl( L_tmp, hPrivateData->prevDampingFactor_IIR_e[sfb] );
     741       11647 :                             test();
     742       11647 :                             test();
     743       11647 :                             IF( last_core_acelp || hPrivateData->wasTransient || EQ_32( L_tmp, MIN16B ) )
     744             :                             {
     745        3971 :                                 tmp = BASOP_Util_Cmp_Mant32Exp( currDampingFactor, currDampingFactor_e, 3277 /* 0.1f in Q15 */, 0 );
     746        3971 :                                 IF( tmp >= 0 )
     747             :                                 {
     748        3930 :                                     hPrivateData->prevDampingFactor_IIR_fx[sfb] = currDampingFactor;
     749        3930 :                                     hPrivateData->prevDampingFactor_IIR_e[sfb] = currDampingFactor_e;
     750        3930 :                                     move16();
     751        3930 :                                     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       11647 :                             IF( last_core_acelp )
     762             :                             {
     763         558 :                                 hPrivateData->dampingFactorSmoothing[sfb] = 2;
     764         558 :                                 move16();
     765             :                             }
     766             : 
     767       11647 :                             dampingFactor_e = BASOP_Util_Add_MantExp( currDampingFactor, currDampingFactor_e, hPrivateData->prevDampingFactor_IIR_fx[sfb], hPrivateData->prevDampingFactor_IIR_e[sfb], &dampingFactor ); // dampingFactor_e
     768       11647 :                             dampingFactor = shr( dampingFactor, 1 );
     769             : 
     770       11647 :                             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       11647 :                             hPrivateData->prevDampingFactor_IIR_fx[sfb] = dampingFactor;
     773       11647 :                             move16();
     774       11647 :                             hPrivateData->prevDampingFactor_IIR_e[sfb] = dampingFactor_e;
     775       11647 :                             move16();
     776       11647 :                             if ( hPrivateData->dampingFactorSmoothing[sfb] > 0 )
     777             :                             {
     778        5046 :                                 hPrivateData->dampingFactorSmoothing[sfb] = sub( hPrivateData->dampingFactorSmoothing[sfb], 1 );
     779        5046 :                                 move16();
     780             :                             }
     781             :                         }
     782             :                         ELSE
     783             :                         {
     784     2304056 :                             hPrivateData->prevDampingFactor_IIR_fx[sfb] = MIN16B; // exponent : hPrivateData->prevDampingFactor_IIR_e[sfb]
     785     2304056 :                             hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
     786     2304056 :                             hPrivateData->dampingFactorSmoothing[sfb] = 1;
     787     2304056 :                             move16();
     788     2304056 :                             move16();
     789     2304056 :                             move16();
     790             :                         }
     791             : 
     792     2315703 :                         hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = tmp_tb; // hPrivateData->prevSFB_FIR_TB_e[sfb]
     793     2315703 :                         hPrivateData->prevSFB_FIR_TB_e[sfb] = tmp_tb_e;
     794     2315703 :                         hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = hPrivateData->SFM_tb_fx[sfb]; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     795     2315703 :                         hPrivateData->prevSFB_IIR_TB_e[sfb] = hPrivateData->sfb_tb_e[sfb];
     796     2315703 :                         hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = tmp_sb; // hPrivateData->prevSFB_FIR_SB_e[sfb]
     797     2315703 :                         hPrivateData->prevSFB_FIR_SB_e[sfb] = tmp_sb_e;
     798     2315703 :                         hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = hPrivateData->SFM_sb_fx[sfb]; // hPrivateData->prevSFB_IIR_SB_e[sfb]
     799     2315703 :                         hPrivateData->prevSFB_IIR_SB_e[sfb] = hPrivateData->sfb_sb_e[sfb];
     800     2315703 :                         move16();
     801     2315703 :                         move16();
     802     2315703 :                         move16();
     803     2315703 :                         move16();
     804     2315703 :                         move16();
     805     2315703 :                         move16();
     806     2315703 :                         move16();
     807     2315703 :                         move16();
     808             :                     }
     809             :                     ELSE
     810             :                     {
     811        6732 :                         hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = 0; // hPrivateData->prevSFB_FIR_TB_e[sfb]
     812        6732 :                         hPrivateData->prevSFB_FIR_TB_e[sfb] = 0;
     813        6732 :                         hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     814        6732 :                         hPrivateData->prevSFB_IIR_TB_e[sfb] = 0;
     815        6732 :                         hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     816        6732 :                         hPrivateData->prevSFB_FIR_SB_e[sfb] = 0;
     817        6732 :                         hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_SB_e[sfb]
     818        6732 :                         hPrivateData->prevSFB_IIR_SB_e[sfb] = 0;
     819        6732 :                         hPrivateData->prevDampingFactor_IIR_fx[sfb] = MIN16B; /* hPrivateData->prevDampingFactor_IIR_e[sfb]*/
     820        6732 :                         hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
     821        6732 :                         hPrivateData->dampingFactorSmoothing[sfb] = 2;
     822        6732 :                         move16();
     823        6732 :                         move16();
     824        6732 :                         move16();
     825        6732 :                         move16();
     826        6732 :                         move16();
     827        6732 :                         move16();
     828        6732 :                         move16();
     829        6732 :                         move16();
     830        6732 :                         move16();
     831        6732 :                         move16();
     832        6732 :                         move16();
     833             :                     }
     834             :                 }
     835             :             }
     836             :             ELSE
     837             :             {
     838      275649 :                 tmp_e = e_mdct;
     839      275649 :                 move16();
     840      275649 :                 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      275649 :                 sfbEnergyR_e = add( sfbEnergyR_e, add( tmp_e, -15 ) );
     842      275649 :                 gain = L_shl( sfbEnergyR, 16 ); // gain_e
     843      275649 :                 move32();
     844      275649 :                 gain_e = sfbEnergyR_e;
     845             : 
     846      275649 :                 IF( element_mode > EVS_MONO )
     847             :                 {
     848      275649 :                     hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = 0; // hPrivateData->prevSFB_FIR_TB_e[sfb]
     849      275649 :                     hPrivateData->prevSFB_FIR_TB_e[sfb] = 0;
     850      275649 :                     hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     851      275649 :                     hPrivateData->prevSFB_IIR_TB_e[sfb] = 0;
     852      275649 :                     hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     853      275649 :                     hPrivateData->prevSFB_FIR_SB_e[sfb] = 0;
     854      275649 :                     hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_SB_e[sfb]
     855      275649 :                     hPrivateData->prevSFB_IIR_SB_e[sfb] = 0;
     856      275649 :                     hPrivateData->prevDampingFactor_IIR_fx[sfb] = MIN16B; /* hPrivateData->prevDampingFactor_IIR_e[sfb]*/
     857      275649 :                     hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
     858      275649 :                     hPrivateData->dampingFactorSmoothing[sfb] = 2;
     859      275649 :                     move16();
     860      275649 :                     move16();
     861      275649 :                     move16();
     862      275649 :                     move16();
     863      275649 :                     move16();
     864      275649 :                     move16();
     865      275649 :                     move16();
     866      275649 :                     move16();
     867      275649 :                     move16();
     868      275649 :                     move16();
     869      275649 :                     move16();
     870             :                 }
     871             :             }
     872             : 
     873     2598084 :             gain = Mult_32_16( gain, att ); // gain_e
     874     2598084 :             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     2598084 :             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     2598084 :             test();
     879     2598084 :             test();
     880     2598084 :             IF( !isTransient && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) ) )
     881             :             {
     882      382453 :                 gain = L_add( gain, ONE_IN_Q21 ); /* better preservation of original HF band energy */
     883             :             }
     884     2598084 :             test();
     885     2598084 :             test();
     886     2598084 :             IF( !isTransient && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_64000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_64000_CPE ) ) )
     887             :             {
     888      490401 :                 gain = L_add( gain, ONE_IN_Q20 );
     889             :             }
     890     2598084 :             gain = L_min( gain, 91 << Q23 ); /* 13+15+63, see arithcode encode residual */
     891     2598084 :             gain = L_max( gain, 0 );
     892     2598084 :             gain_e = 8; /* stores exponent for gain_fx*/
     893     2598084 :             move16();
     894     2598084 :             hPrivateData->igfScfQuantized[sfb] = extract_l( L_shr( gain, Q23 ) ); /*Q0*/
     895     2598084 :             move16();
     896             :         }
     897             :     }
     898             : 
     899      492473 :     return;
     900             : }
     901             : 
     902             : /*-------------------------------------------------------------------*
     903             :  * IGF_CalculateStereoEnvelope_fx()
     904             :  *
     905             :  * envelope estimation
     906             : 
     907             :  *-------------------------------------------------------------------*/
     908      100058 : 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      100058 :     Word16 q_temp_pPowerSpectrumMsInv = Q31, i;
     964      100058 :     move16();
     965             : 
     966      100058 :     IF( pPowerSpectrumMsInv_fx != NULL )
     967             :     {
     968       81370 :         length = N_MAX;
     969       81370 :         move16();
     970       81370 :         if ( mct_on )
     971             :         {
     972       51090 :             length = L_FRAME48k;
     973       51090 :             move16();
     974             :         }
     975    85463770 :         FOR( i = 0; i < length; i++ )
     976             :         {
     977    85382400 :             IF( pPowerSpectrumMsInv_fx[i] != 0 )
     978             :             {
     979    61890560 :                 q_temp_pPowerSpectrumMsInv = s_min( q_temp_pPowerSpectrumMsInv, add( q_pPowerSpectrumMsInv[i], norm_l( pPowerSpectrumMsInv_fx[i] ) ) );
     980             :             }
     981             :         }
     982    85463770 :         FOR( i = 0; i < length; i++ )
     983             :         {
     984    85382400 :             temp_pPowerSpectrumMsInv[i] = L_shl( pPowerSpectrumMsInv_fx[i], sub( q_temp_pPowerSpectrumMsInv, q_pPowerSpectrumMsInv[i] ) );
     985    85382400 :             move32();
     986             :         }
     987             :     }
     988             : 
     989      100058 :     hPrivateData = &hIGFEnc->igfData;
     990      100058 :     hGrid = &hPrivateData->igfInfo.grid[(Word16) igfGridIdx];
     991      100058 :     swb_offset = hGrid->swb_offset;
     992      100058 :     move16();
     993             : 
     994      100058 :     IF( igfGridIdx != IGF_GRID_LB_NORM )
     995             :     {
     996       23442 :         FOR( sfbCnt = 0; sfbCnt < sub( hGrid->sfbWrap[hGrid->nTiles], hGrid->sfbWrap[0] ); sfbCnt++ )
     997             :         {
     998             :             /* reset filter */
     999       19222 :             hPrivateData->prevSFM_FIR_SFB_TB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_FIR_TB_e[sfbCnt]
    1000       19222 :             hPrivateData->prevSFM_IIR_SFB_TB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_IIR_TB_e[sfbCnt]
    1001       19222 :             hPrivateData->prevSFM_FIR_SFB_SB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_FIR_SB_e[sfbCnt]
    1002       19222 :             hPrivateData->prevSFM_IIR_SFB_SB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_IIR_SB_e[sfbCnt]
    1003       19222 :             hPrivateData->prevSFB_FIR_TB_e[sfbCnt] = 0;
    1004       19222 :             hPrivateData->prevSFB_IIR_TB_e[sfbCnt] = 0;
    1005       19222 :             hPrivateData->prevSFB_FIR_SB_e[sfbCnt] = 0;
    1006       19222 :             hPrivateData->prevSFB_IIR_SB_e[sfbCnt] = 0;
    1007       19222 :             move16();
    1008       19222 :             move16();
    1009       19222 :             move16();
    1010       19222 :             move16();
    1011       19222 :             move16();
    1012       19222 :             move16();
    1013       19222 :             move16();
    1014       19222 :             move16();
    1015             :         }
    1016             :     }
    1017             : 
    1018      100058 :     IF( pPowerSpectrum_fx )
    1019             :     {
    1020    38084570 :         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    38003200 :             IF( LE_32( 1, pPowerSpectrum_fx[sb] ) )
    1024             :             {
    1025    38002910 :                 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         290 :                 hPrivateData->logSpec[sb] = 0; /*max(0,-126) is always 0*/
    1030             :             }
    1031    38003200 :             move16();
    1032             :         }
    1033             :     }
    1034             : 
    1035      393300 :     FOR( tile_idx = 0; tile_idx < hGrid->nTiles; tile_idx++ )
    1036             :     {
    1037      293242 :         strt_cpy = hGrid->sbWrap[tile_idx];
    1038      293242 :         move16();
    1039             : 
    1040      783240 :         FOR( sfb = hGrid->sfbWrap[tile_idx]; sfb < hGrid->sfbWrap[tile_idx + 1]; sfb++ )
    1041             :         {
    1042      489998 :             width = sub( swb_offset[sfb + 1], swb_offset[sfb] );
    1043      489998 :             sfbEnergyTileR_fx = EPSILON_FX;
    1044      489998 :             sfbEnergyTileC_fx = EPSILON_FX;
    1045      489998 :             sfbEnergyC_fx = EPSILON_FX;
    1046      489998 :             sfbEnergyTileR_e = 0;
    1047      489998 :             sfbEnergyTileC_e = 0;
    1048      489998 :             sfbEnergyC_e = 0;
    1049      489998 :             move16();
    1050      489998 :             move16();
    1051      489998 :             move16();
    1052      489998 :             move32();
    1053      489998 :             move32();
    1054      489998 :             move32();
    1055      489998 :             IF( pPowerSpectrum_fx )
    1056             :             {
    1057             :                 Word16 final_exp;
    1058             :                 Word16 norm_exp;
    1059             :                 Word32 scaled_value;
    1060      398268 :                 tmp = strt_cpy;
    1061      398268 :                 move16();
    1062             : 
    1063    21232412 :                 FOR( sb = swb_offset[sfb]; sb < swb_offset[sfb + 1]; sb++ )
    1064             :                 {
    1065    20834144 :                     IF( NE_16( coreMsMask[sb], coreMsMask[strt_cpy] ) )
    1066             :                     {
    1067    12329744 :                         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    12329744 :                         norm_exp = norm_l( pMDCTSpectrumMsInv_fx[strt_cpy] );
    1069    12329744 :                         final_exp = sub( pMDCTSpectrumMsInv_e, norm_exp );
    1070    12329744 :                         scaled_value = L_shl( pMDCTSpectrumMsInv_fx[strt_cpy], norm_exp );
    1071    12329744 :                         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    12329744 :                         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    12329744 :                         tileSrcSpec_fx[strt_cpy - tmp] = temp_pPowerSpectrumMsInv[strt_cpy];                                                                                                                  /*resultant exponent is stored in tileSrcSpec_e*/
    1074    12329744 :                         tileSrcSpec_e[strt_cpy - tmp] = sub( 31, q_temp_pPowerSpectrumMsInv );
    1075             :                     }
    1076             :                     ELSE
    1077             :                     {
    1078     8504400 :                         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     8504400 :                         norm_exp = norm_l( pMDCTSpectrum_fx[strt_cpy] );
    1080     8504400 :                         final_exp = sub( pMDCTSpectrum_e, norm_exp );
    1081     8504400 :                         scaled_value = L_shl( pMDCTSpectrum_fx[strt_cpy], norm_exp );
    1082     8504400 :                         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     8504400 :                         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     8504400 :                         tileSrcSpec_fx[strt_cpy - tmp] = pPowerSpectrum_fx[strt_cpy];                                                                                                          /*resultant exponent is stored in tileSrcSpec_e*/
    1085     8504400 :                         tileSrcSpec_e[strt_cpy - tmp] = pPowerSpectrum_e[strt_cpy];
    1086             :                     }
    1087    20834144 :                     move32();
    1088    20834144 :                     move16();
    1089    20834144 :                     strt_cpy = add( strt_cpy, 1 );
    1090             :                 }
    1091             : 
    1092      398268 :                 sfbEnergyTileR_fx = BASOP_Util_Divide3216_Scale( sfbEnergyTileR_fx, width, &tmp_e );
    1093      398268 :                 sfbEnergyTileR_e = sub( add( sfbEnergyTileR_e, tmp_e ), 15 ); /*stores the resultant exponent for sfbEnergyTileR_fx*/
    1094             : 
    1095             :                 /*gain = (float) ( sfbEnergyTileR * ( sfbEnergyC / sfbEnergyTileC ) );*/
    1096      398268 :                 temp = BASOP_Util_Divide3232_Scale( sfbEnergyC_fx, L_add( sfbEnergyTileC_fx, EPSILON_FX ), &tmp_e );
    1097      398268 :                 gain_e = add( tmp_e, sub( sfbEnergyC_e, sfbEnergyTileC_e ) );
    1098      398268 :                 gain_fx = Mult_32_16( sfbEnergyTileR_fx, temp );
    1099      398268 :                 gain_e = add( 16, add( gain_e, sfbEnergyTileR_e ) ); /*stores the resultant exponent for gain_fx*/
    1100             : 
    1101      398268 :                 IF( !isTransient )
    1102             :                 {
    1103             :                     Word16 diffSFM_fx;
    1104             :                     Word16 shiftedSFM_fx;
    1105             :                     Word16 shiftedSFM_e; /*stores the resultant exponent for shiftedSFM_fx*/
    1106      398268 :                     shiftedSFM_fx = 0;
    1107      398268 :                     shiftedSFM_e = 0;
    1108      398268 :                     move16();
    1109      398268 :                     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      398268 :                     sfm = IGF_getSFM_ivas_fx( &sfm_exp, pPowerSpectrum_fx, pPowerSpectrum_e, swb_offset[sfb], swb_offset[sfb + 1] );
    1113      398268 :                     crest = IGF_getCrest_ivas( &crest_exp, pPowerSpectrum_fx, pPowerSpectrum_e, swb_offset[sfb], swb_offset[sfb + 1] );
    1114      398268 :                     tmp_tb_fx = BASOP_Util_Divide1616_Scale( sfm, crest, &tmp_e );
    1115      398268 :                     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      398268 :                     sfm = IGF_getSFM_ivas_fx( &sfm_exp, tileSrcSpec_fx, tileSrcSpec_e, 0, sub( strt_cpy, tmp ) );
    1119      398268 :                     crest = IGF_getCrest_ivas( &crest_exp, tileSrcSpec_fx, tileSrcSpec_e, 0, sub( strt_cpy, tmp ) );
    1120      398268 :                     tmp_sb_fx = BASOP_Util_Divide1616_Scale( sfm, crest, &tmp_e );
    1121      398268 :                     tmp_sb_e = add( tmp_e, sub( sfm_exp, crest_exp ) ); /*stores the resultant exponent for tmp_sb_fx*/
    1122             : 
    1123      398268 :                     IF( last_core_acelp || hPrivateData->wasTransient )
    1124             :                     {
    1125        7534 :                         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        7534 :                         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        7534 :                         hPrivateData->prevSFB_FIR_TB_e[sfb] = tmp_sb_e;
    1128        7534 :                         hPrivateData->prevSFB_IIR_TB_e[sfb] = tmp_sb_e;
    1129        7534 :                         hPrivateData->prevSFB_FIR_SB_e[sfb] = tmp_sb_e;
    1130        7534 :                         hPrivateData->prevSFB_IIR_SB_e[sfb] = tmp_sb_e;
    1131        7534 :                         move16();
    1132        7534 :                         move16();
    1133        7534 :                         move16();
    1134        7534 :                         move16();
    1135        7534 :                         move16();
    1136        7534 :                         move16();
    1137        7534 :                         move16();
    1138        7534 :                         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      390734 :                         diff_sb_e = sub( tmp_sb_e, hPrivateData->sfb_sb_e[sfb] );
    1149      390734 :                         diff_tb_e = sub( tmp_tb_e, hPrivateData->sfb_tb_e[sfb] );
    1150      390734 :                         IF( LE_16( tmp_tb_e, hPrivateData->sfb_tb_e[sfb] ) )
    1151             :                         {
    1152      382764 :                             tmp_tb_fx = shl( tmp_tb_fx, diff_tb_e );
    1153      382764 :                             tmp_tb_e = hPrivateData->sfb_tb_e[sfb];
    1154      382764 :                             move16();
    1155             :                         }
    1156             :                         ELSE
    1157             :                         {
    1158        7970 :                             hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = shr( hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb], diff_tb_e );
    1159        7970 :                             hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = shr( hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb], diff_tb_e );
    1160        7970 :                             move16();
    1161        7970 :                             move16();
    1162        7970 :                             hPrivateData->prevSFB_FIR_TB_e[sfb] = tmp_tb_e;
    1163        7970 :                             hPrivateData->prevSFB_IIR_TB_e[sfb] = tmp_tb_e;
    1164        7970 :                             move16();
    1165        7970 :                             move16();
    1166             :                         }
    1167      390734 :                         IF( LE_16( tmp_sb_e, hPrivateData->sfb_sb_e[sfb] ) )
    1168             :                         {
    1169      382596 :                             tmp_sb_fx = shl( tmp_sb_fx, diff_sb_e );
    1170      382596 :                             tmp_sb_e = hPrivateData->sfb_sb_e[sfb];
    1171      382596 :                             move16();
    1172             :                         }
    1173             :                         ELSE
    1174             :                         {
    1175        8138 :                             hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = shr( hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb], diff_sb_e );
    1176        8138 :                             hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = shr( hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb], diff_sb_e );
    1177        8138 :                             move16();
    1178        8138 :                             move16();
    1179        8138 :                             hPrivateData->prevSFB_FIR_SB_e[sfb] = tmp_sb_e;
    1180        8138 :                             hPrivateData->prevSFB_IIR_SB_e[sfb] = tmp_sb_e;
    1181        8138 :                             move16();
    1182        8138 :                             move16();
    1183             :                         }
    1184             :                     }
    1185      398268 :                     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      398268 :                     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      398268 :                     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      398268 :                     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      398268 :                     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      398268 :                     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      398268 :                     move16();
    1192      398268 :                     move16();
    1193      398268 :                     move16();
    1194      398268 :                     move16();
    1195      398268 :                     hPrivateData->sfb_sb_e[sfb] = 2;
    1196      398268 :                     hPrivateData->sfb_tb_e[sfb] = 2;
    1197      398268 :                     move16();
    1198      398268 :                     move16();
    1199             : 
    1200      398268 :                     diffSFM_fx = sub( hPrivateData->SFM_sb_fx[sfb], hPrivateData->SFM_tb_fx[sfb] ); /*Q13*/
    1201             : 
    1202      398268 :                     test();
    1203      398268 :                     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        1234 :                     {
    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        1234 :                         x = 1;
    1209        1234 :                         mean_x_fx = mean_y_fx = 0;
    1210        1234 :                         mean_xy_fx = mean_x2_fx = 0;
    1211        1234 :                         mean_x_e = mean_y_e = 15;
    1212        1234 :                         mean_xy_e = mean_x2_e = 31;
    1213        1234 :                         move16();
    1214        1234 :                         move16();
    1215        1234 :                         move16();
    1216        1234 :                         move16();
    1217        1234 :                         move16();
    1218        1234 :                         move16();
    1219        1234 :                         move16();
    1220        1234 :                         move16();
    1221        1234 :                         move16();
    1222       68306 :                         FOR( sb = swb_offset[sfb]; sb < swb_offset[sfb + 1]; sb++ )
    1223             :                         {
    1224       67072 :                             mean_x_fx = add( mean_x_fx, x );                   /*Q0*/
    1225       67072 :                             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       67072 :                             IF( LT_32( pPowerSpectrum_fx[sb], 1 ) )
    1229             :                             {
    1230           0 :                                 y = 20 * ( -18 );
    1231           0 :                                 move16();
    1232             :                             }
    1233             :                             ELSE
    1234             :                             {
    1235       67072 :                                 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       67072 :                             mean_y_fx = add( mean_y_fx, y );                   /*Q0*/
    1238       67072 :                             mean_xy_fx = L_add( mean_xy_fx, L_mult0( y, x ) ); /*Q0*/
    1239             : 
    1240       67072 :                             x = add( x, 1 );
    1241             :                         }
    1242        1234 :                         mean_y_fx = BASOP_Util_Divide1616_Scale( mean_y_fx, width, &tmp_e ); /* resultant exp stores in mean_y_e*/
    1243        1234 :                         mean_y_e = add( mean_y_e, sub( tmp_e, 15 ) );
    1244        1234 :                         mean_x_fx = BASOP_Util_Divide1616_Scale( mean_x_fx, width, &tmp_e ); /* resultant exp stores in mean_x_e*/
    1245        1234 :                         mean_x_e = add( mean_x_e, sub( tmp_e, 15 ) );
    1246        1234 :                         mean_xy_fx = BASOP_Util_Divide3216_Scale( mean_xy_fx, width, &tmp_e ); /* resultant exp stores in mean_xy_e*/
    1247        1234 :                         mean_xy_e = add( mean_xy_e, sub( tmp_e, 15 ) );
    1248        1234 :                         mean_x2_fx = BASOP_Util_Divide3216_Scale( mean_x2_fx, width, &tmp_e ); /* resultant exp stores in mean_x2_e*/
    1249        1234 :                         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        1234 :                         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        1234 :                         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        1234 :                         threshold_fx = BASOP_Util_Divide1616_Scale( 60, width, &threshold_e ); /*stores resultant exponent for threshold_fx*/
    1258        1234 :                         test();
    1259        1234 :                         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        1203 :                         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        1234 :                         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        1234 :                         temp = BASOP_Util_Divide1616_Scale( 320, swb_offset[sfb + 1], &alpha_e );
    1283        1234 :                         alpha_fx = extract_l( L_min( temp, L_shl( 20480 /*1.25 Q14*/, sub( 1, alpha_e ) ) ) ); /* exponent is alpha_e*/
    1284        1234 :                         temp = BASOP_Util_Divide1616_Scale( hPrivateData->SFM_tb_fx[sfb], hPrivateData->SFM_sb_fx[sfb], &tmp_e );
    1285        1234 :                         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        1234 :                         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        1234 :                         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        1234 :                         test();
    1298        1234 :                         test();
    1299        1234 :                         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         582 :                             tmp = BASOP_Util_Cmp_Mant32Exp( currDampingFactor_fx, currDampingFactor_e, 3277, 0 );
    1302         582 :                             IF( tmp >= 0 )
    1303             :                             {
    1304         582 :                                 hPrivateData->prevDampingFactor_IIR_fx[sfb] = currDampingFactor_fx;
    1305         582 :                                 hPrivateData->prevDampingFactor_IIR_e[sfb] = currDampingFactor_e;
    1306             :                             }
    1307             :                             ELSE
    1308             :                             {
    1309           0 :                                 hPrivateData->prevDampingFactor_IIR_fx[sfb] = 3277; /* 0.1 in Q15 */
    1310           0 :                                 hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
    1311             :                             }
    1312             : 
    1313         582 :                             move16();
    1314         582 :                             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        1234 :                             tonalToNoise_e = 9;    /*stores exponent for tonalToNoise*/
    1323        1234 :                             move16();
    1324        1234 :                             adap = BASOP_Util_Divide1616_Scale( width, 30, &adap_e );
    1325        1234 :                             tonalToNoise = IGF_getTNR_ivas_fx( pPowerSpectrum_fx, swb_offset[sfb], swb_offset[sfb + 1], adap, pPowerSpectrum_e, adap_e ); /*Q22*/
    1326        1234 :                             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        1106 :                                 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        1106 :                                 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        1234 :                         dampingFactor_e = BASOP_Util_Add_MantExp( currDampingFactor_fx, currDampingFactor_e, hPrivateData->prevDampingFactor_IIR_fx[sfb], hPrivateData->prevDampingFactor_IIR_e[sfb], &dampingFactor_fx );
    1335        1234 :                         dampingFactor_fx = shr( dampingFactor_fx, 1 ); /* resultant exponent is dampingFactor_e*/
    1336        1234 :                         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         500 :                             dampingFactor_fx = shr( hPrivateData->prevDampingFactor_IIR_fx[sfb], 1 ); /* resultant exponent is hPrivateData->prevDampingFactor_IIR_e[sfb]*/
    1343         500 :                             dampingFactor_e = hPrivateData->prevDampingFactor_IIR_e[sfb];
    1344         500 :                             move16();
    1345             :                         }
    1346        1234 :                         IF( dampingFactor_e < 0 )
    1347             :                         {
    1348          57 :                             dampingFactor_fx = shl( dampingFactor_fx, dampingFactor_e );
    1349          57 :                             dampingFactor_e = 0;
    1350          57 :                             move16();
    1351             :                         }
    1352        1234 :                         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        1234 :                         hPrivateData->prevDampingFactor_IIR_fx[sfb] = dampingFactor_fx;
    1355        1234 :                         hPrivateData->prevDampingFactor_IIR_e[sfb] = dampingFactor_e;
    1356        1234 :                         move16();
    1357        1234 :                         move16();
    1358        1234 :                         if ( hPrivateData->dampingFactorSmoothing[sfb] > 0 )
    1359             :                         {
    1360         690 :                             hPrivateData->dampingFactorSmoothing[sfb] = sub( hPrivateData->dampingFactorSmoothing[sfb], 1 );
    1361         690 :                             move16();
    1362             :                         }
    1363             :                     }
    1364             :                     ELSE
    1365             :                     {
    1366      397034 :                         hPrivateData->prevDampingFactor_IIR_fx[sfb] = -( 1 << 15 ); /* resultant exp which is 0 stores in hPrivateData->prevDampingFactor_IIR_e[sfb]*/
    1367      397034 :                         hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
    1368      397034 :                         hPrivateData->dampingFactorSmoothing[sfb] = 1;
    1369      397034 :                         move16();
    1370      397034 :                         move16();
    1371      397034 :                         move16();
    1372             :                     }
    1373             : 
    1374      398268 :                     hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = tmp_tb_fx;
    1375      398268 :                     hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = hPrivateData->SFM_tb_fx[sfb];
    1376      398268 :                     hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = tmp_sb_fx;
    1377      398268 :                     hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = hPrivateData->SFM_sb_fx[sfb];
    1378      398268 :                     hPrivateData->prevSFB_FIR_TB_e[sfb] = hPrivateData->sfb_tb_e[sfb];
    1379      398268 :                     hPrivateData->prevSFB_IIR_TB_e[sfb] = hPrivateData->sfb_tb_e[sfb];
    1380      398268 :                     hPrivateData->prevSFB_FIR_SB_e[sfb] = hPrivateData->sfb_sb_e[sfb];
    1381      398268 :                     hPrivateData->prevSFB_IIR_SB_e[sfb] = hPrivateData->sfb_sb_e[sfb];
    1382      398268 :                     move16();
    1383      398268 :                     move16();
    1384      398268 :                     move16();
    1385      398268 :                     move16();
    1386      398268 :                     move16();
    1387      398268 :                     move16();
    1388      398268 :                     move16();
    1389      398268 :                     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       91730 :                 tmp_e = pMDCTSpectrum_e;
    1417       91730 :                 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       91730 :                 sfbEnergyR_e = add( sfbEnergyR_e, add( tmp_e, -15 ) ); /* stores resultant exponent for sfbEnergyR_fx*/
    1419       91730 :                 gain_fx = sfbEnergyR_fx;                               /*resultant exponent stored in gain_e=sfbEnergyR_e*/
    1420       91730 :                 move32();
    1421       91730 :                 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       91730 :                 hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = 0;
    1424       91730 :                 hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = 0;
    1425       91730 :                 hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = 0;
    1426       91730 :                 hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = 0;
    1427       91730 :                 hPrivateData->prevSFB_FIR_TB_e[sfb] = 0;
    1428       91730 :                 hPrivateData->prevSFB_IIR_TB_e[sfb] = 0;
    1429       91730 :                 hPrivateData->prevSFB_FIR_SB_e[sfb] = 0;
    1430       91730 :                 hPrivateData->prevSFB_IIR_SB_e[sfb] = 0;
    1431       91730 :                 move16();
    1432       91730 :                 move16();
    1433       91730 :                 move16();
    1434       91730 :                 move16();
    1435       91730 :                 move16();
    1436       91730 :                 move16();
    1437       91730 :                 move16();
    1438       91730 :                 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      489998 :             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      489998 :             test();
    1443      489998 :             test();
    1444      489998 :             if ( !isTransient && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) ) )
    1445             :             {
    1446      108253 :                 gain_fx = L_add( gain_fx, ONE_IN_Q21 ); /* better preservation of original HF band energy */
    1447             :             }
    1448      489998 :             test();
    1449      489998 :             test();
    1450      489998 :             if ( !isTransient && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_64000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_64000_CPE ) ) )
    1451             :             {
    1452      177535 :                 gain_fx = L_add( gain_fx, ONE_IN_Q20 );
    1453             :             }
    1454      489998 :             gain_fx = L_min( gain_fx, 91 << Q23 ); /* 13+15+63, see arithcode encode residual */
    1455      489998 :             gain_fx = L_max( gain_fx, 0 );
    1456      489998 :             gain_e = 8; /* stores exponent for gain_fx*/
    1457      489998 :             move16();
    1458      489998 :             hPrivateData->igfScfQuantized[sfb] = (Word16) ( L_shr( gain_fx, 23 ) ); /*Q0*/
    1459      489998 :             move16();
    1460             :         }
    1461             :     }
    1462             : 
    1463      100058 :     return;
    1464             : }
    1465             : 
    1466             : 
    1467             : /*-------------------------------------------------------------------*
    1468             :  * IGF_WriteEnvelope()
    1469             :  *
    1470             :  * writes IGF SCF values
    1471             :  *-------------------------------------------------------------------*/
    1472             : /*! r: number of bits writen */
    1473     1162966 : 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     1162966 :     startBitCount = *pBitOffset;
    1489     1162966 :     totBitCount = 0;
    1490     1162966 :     *igfAllZero = 1;
    1491     1162966 :     move16();
    1492     1162966 :     move16();
    1493     1162966 :     move16();
    1494     1162966 :     hPrivateData = &hIGFEnc->igfData;
    1495     1162966 :     hGrid = &hPrivateData->igfInfo.grid[igfGridIdx];
    1496             : 
    1497     1176563 :     FOR( sfb = hGrid->startSfb; sfb < hGrid->stopSfb; sfb++ )
    1498             :     {
    1499     1174672 :         IF( hPrivateData->igfScfQuantized[sfb] != 0 )
    1500             :         {
    1501     1161075 :             *igfAllZero = 0;
    1502     1161075 :             move16();
    1503     1161075 :             BREAK;
    1504             :         }
    1505             :     }
    1506             : 
    1507     1162966 :     IF( *igfAllZero != 0 )
    1508             :     {
    1509        1891 :         IGF_write_bit_fx( hBstr, pBitOffset, 1 );
    1510             : 
    1511        1891 :         if ( NULL == hBstr )
    1512             :         {
    1513         944 :             IGFSCFEncoderSaveContextState_fx( &hPrivateData->hIGFSCFArithEnc, igfGridIdx );
    1514             :         }
    1515             : 
    1516        1891 :         IGFSCFEncoderReset_fx( &hPrivateData->hIGFSCFArithEnc );
    1517             : 
    1518        1891 :         if ( NULL == hBstr )
    1519             :         {
    1520         944 :             IGFSCFEncoderRestoreContextState_fx( &hPrivateData->hIGFSCFArithEnc, igfGridIdx );
    1521             :         }
    1522             :     }
    1523             :     ELSE
    1524             :     {
    1525     1161075 :         IGF_write_bit_fx( hBstr, pBitOffset, 0 );
    1526             : 
    1527     1161075 :         if ( NULL == hBstr )
    1528             :         {
    1529      569491 :             IGFSCFEncoderSaveContextState_fx( &hPrivateData->hIGFSCFArithEnc, igfGridIdx );
    1530             :         }
    1531             : 
    1532     1161075 :         *pBitOffset = IGFSCFEncoderEncode_ivas_fx( &hPrivateData->hIGFSCFArithEnc, hBstr, *pBitOffset, &hPrivateData->igfScfQuantized[hGrid->startSfb], igfGridIdx, isIndepFlag );
    1533     1161075 :         move16();
    1534             : 
    1535     1161075 :         if ( NULL == hBstr )
    1536             :         {
    1537      569491 :             IGFSCFEncoderRestoreContextState_fx( &hPrivateData->hIGFSCFArithEnc, igfGridIdx );
    1538             :         }
    1539             :     }
    1540     1162966 :     totBitCount = sub( *pBitOffset, startBitCount );
    1541             : 
    1542     1162966 :     return totBitCount;
    1543             : }
    1544             : 
    1545             : 
    1546             : /*-------------------------------------------------------------------*
    1547             :  * IGF_Whitening()
    1548             :  *
    1549             :  * calculates the IGF whitening levels by SFM and crest
    1550             :  *-------------------------------------------------------------------*/
    1551      592531 : 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      592531 :     SFM = -ONE_IN_Q13; /*1.0f Q13*/
    1578      592531 :     move16();
    1579             : 
    1580      592531 :     hPrivateData = &hIGFEnc->igfData;
    1581      592531 :     hGrid = &hPrivateData->igfInfo.grid[(Word16) igfGridIdx];
    1582             : 
    1583      592531 :     IF( NE_16( igfGridIdx, IGF_GRID_LB_NORM ) )
    1584             :     {
    1585      133747 :         FOR( p = 0; p < hGrid->nTiles; p++ )
    1586             :         {
    1587             :             /* reset filter */
    1588      103176 :             hPrivateData->prevSFM_FIR[p] = L_deposit_l( 0 );
    1589      103176 :             hPrivateData->prevSFM_IIR[p] = 0;
    1590      103176 :             move16();
    1591             : 
    1592             :             /* preset values: */
    1593      103176 :             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1594      103176 :             move16();
    1595             :         }
    1596             :     }
    1597             : 
    1598     6517841 :     FOR( p = 0; p < IGF_MAX_TILES; p++ )
    1599             :     {
    1600             :         /* update prev data: */
    1601     5925310 :         hPrivateData->igfPrevWhiteningLevel[p] = hPrivateData->igfCurrWhiteningLevel[p];
    1602             :         /* preset values: */
    1603     5925310 :         hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1604     5925310 :         move16();
    1605     5925310 :         move16();
    1606             :     }
    1607             : 
    1608      592531 :     test();
    1609      592531 :     IF( !( isTransient || hPrivateData->wasTransient ) )
    1610             :     {
    1611      559150 :         IF( powerSpectrum )
    1612             :         {
    1613     2469103 :             FOR( p = 0; p < hGrid->nTiles; p++ )
    1614             :             {
    1615             :                 Word16 sb;
    1616             : 
    1617     1911243 :                 IF( isTNSActive )
    1618             :                 {
    1619    14483359 :                     FOR( sb = hGrid->tile[p]; sb < hGrid->tile[p + 1]; sb++ )
    1620             :                     {
    1621    14338008 :                         IF( LT_32( powerSpectrum[sb], 1 ) )
    1622       13402 :                         hPrivateData->logSpec[sb] = 0; /* max(0,FLT_MIN_EXP )*/
    1623             :                         ELSE
    1624    14324606 :                             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    14338008 :                         move16();
    1626             :                     }
    1627             :                 }
    1628             : 
    1629             :                 /* if current tile contains only a single SFB, reuse already computed SFM values */
    1630     1911243 :                 test();
    1631     1911243 :                 IF( element_mode > EVS_MONO && EQ_16( sub( hGrid->sfbWrap[p + 1], hGrid->sfbWrap[p] ), 1 ) )
    1632             :                 {
    1633     1168214 :                     tmp = hPrivateData->SFM_tb_fx[p];
    1634     1168214 :                     tmp_e = hPrivateData->sfb_tb_e[p];
    1635     1168214 :                     move16();
    1636     1168214 :                     move16();
    1637             :                 }
    1638             :                 ELSE
    1639             :                 {
    1640      743029 :                     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      743029 :                     tmp_e = sub( tmp_e, crest_e );
    1642             :                 }
    1643             : 
    1644     1911243 :                 test();
    1645     1911243 :                 IF( last_core_acelp || hPrivateData->wasTransient )
    1646             :                 {
    1647       48914 :                     hPrivateData->prevSFM_FIR[p] = L_shl( tmp, add( 1, tmp_e ) ); /*16-(15-exp)=>15Q16*/
    1648       48914 :                     hPrivateData->prevSFM_IIR[p] = shl( tmp, sub( tmp_e, 2 ) );   /*13-(15-exp)=>2Q13*/
    1649       48914 :                     move32();
    1650       48914 :                     move16();
    1651             :                 }
    1652             : 
    1653     1911243 :                 test();
    1654     1911243 :                 IF( LE_32( brate, IVAS_48k ) && EQ_16( element_mode, IVAS_CPE_MDCT ) )
    1655      447312 :                 {
    1656             :                     Word16 temp;
    1657      447312 :                     num_Tiles = 0;
    1658      447312 :                     SFM_src = 0;
    1659      447312 :                     SFM_tar = 0;
    1660      447312 :                     SFM_src_e = 0;
    1661      447312 :                     SFM_tar_e = 0;
    1662      447312 :                     move16();
    1663      447312 :                     move16();
    1664      447312 :                     move16();
    1665      447312 :                     move16();
    1666      447312 :                     move16();
    1667             : 
    1668      921564 :                     FOR( sb = hGrid->sfbWrap[p]; sb < hGrid->sfbWrap[p + 1]; sb++ )
    1669             :                     {
    1670      474252 :                         num_Tiles = add( num_Tiles, 1 );
    1671      474252 :                         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      474252 :                         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      447312 :                     SFM_src = shr( BASOP_Util_Divide1616_Scale( SFM_src, num_Tiles, &temp ), 2 );
    1677      447312 :                     SFM_src_e = add( SFM_src_e, sub( temp, 13 ) ); /*temp-15+2:because right shifted by 2 which are the guard bits*/
    1678      447312 :                     SFM_tar = shr( BASOP_Util_Divide1616_Scale( SFM_tar, num_Tiles, &temp ), 2 );
    1679      447312 :                     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      447312 :                     IF( LT_16( SFM_tar_e, SFM_src_e ) )
    1682             :                     {
    1683       33580 :                         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       33580 :                         SFM_tar_e = SFM_src_e;
    1685             :                     }
    1686             :                     ELSE
    1687             :                     {
    1688      413732 :                         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      413732 :                         SFM_src_e = SFM_tar_e;
    1690             :                     }
    1691      447312 :                     move16();
    1692             : 
    1693      447312 :                     test();
    1694      447312 :                     test();
    1695      447312 :                     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      377250 :                         test();
    1698      377250 :                         if ( EQ_16( p, 1 ) && EQ_16( abs_s( sub( hPrivateData->igfCurrWhiteningLevel[0], hPrivateData->igfCurrWhiteningLevel[1] ) ), 2 ) ) /* OFF vs. STRONG */
    1699             :                         {
    1700        6344 :                             hPrivateData->igfCurrWhiteningLevel[0] = IGF_WHITENING_MID;
    1701        6344 :                             move16();
    1702             :                         }
    1703      377250 :                         hPrivateData->igfCurrWhiteningLevel[p] = hPrivateData->igfCurrWhiteningLevel[p - 1];
    1704      377250 :                         move16();
    1705             :                     }
    1706       70062 :                     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       32987 :                         test();
    1711       32987 :                         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       27427 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1714       27427 :                             move16();
    1715             :                         }
    1716             : 
    1717             :                         /* whitening mid:  */
    1718       32987 :                         test();
    1719       32987 :                         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        5083 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_MID;
    1722        5083 :                             move16();
    1723             :                         }
    1724             : 
    1725             :                         /* whitening strong */
    1726       32987 :                         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       37075 :                         test();
    1737       37075 :                         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       18961 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1740       18961 :                             move16();
    1741             :                         }
    1742             : 
    1743             :                         /* whitening mid:  */
    1744       37075 :                         test();
    1745       37075 :                         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       11583 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_MID;
    1748       11583 :                             move16();
    1749             :                         }
    1750             : 
    1751             :                         /* whitening strong */
    1752       37075 :                         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        6531 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_STRONG;
    1755        6531 :                             move16();
    1756             :                         }
    1757             :                     }
    1758             : 
    1759      447312 :                     SFM = shl( SFM_tar, sub( SFM_tar_e, 2 ) ); /*2Q13*/
    1760             :                 }
    1761             :                 ELSE
    1762             :                 {
    1763     1463931 :                     test();
    1764     1463931 :                     IF( element_mode > EVS_MONO && EQ_16( sub( hGrid->sfbWrap[p + 1], hGrid->sfbWrap[p] ), 1 ) )
    1765             :                     {
    1766      747842 :                         SFM = shl( tmp, sub( tmp_e, 2 ) ); /*2Q13*/
    1767             :                     }
    1768             :                     ELSE
    1769             :                     {
    1770             :                         Word32 temp;
    1771      716089 :                         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      716089 :                         SFM = extract_l( L_min( 22118 /*2.7*/, temp ) ); /*2Q13*/
    1773             :                     }
    1774     1463931 :                     hPrivateData->prevSFM_FIR[p] = L_shl( tmp, add( 1, tmp_e ) ); /*15Q16*/
    1775     1463931 :                     hPrivateData->prevSFM_IIR[p] = SFM;
    1776     1463931 :                     move32();
    1777     1463931 :                     move16();
    1778             : 
    1779     1463931 :                     IF( GT_16( SFM, hGrid->whiteningThreshold[1][p] ) )
    1780             :                     {
    1781      564811 :                         hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_STRONG;
    1782      564811 :                         move16();
    1783             :                     }
    1784      899120 :                     ELSE IF( GT_16( SFM, hGrid->whiteningThreshold[0][p] ) )
    1785             :                     {
    1786      575696 :                         hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_MID;
    1787      575696 :                         move16();
    1788             :                     }
    1789             :                     ELSE
    1790             :                     {
    1791      323424 :                         hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1792      323424 :                         move16();
    1793             :                     }
    1794             :                 }
    1795             : 
    1796     1911243 :                 IF( element_mode > EVS_MONO )
    1797             :                 {
    1798     1911243 :                     IF( last_core_acelp ) /* reset */
    1799             :                     {
    1800       48914 :                         set16_fx( hPrivateData->igfPastSFM_fx[p], -ONE_IN_Q13, IGF_PAST_SFM_LEN );
    1801       48914 :                         hPrivateData->igfWhiteningHangoverCnt[p] = 2;
    1802       48914 :                         move16();
    1803             :                     }
    1804             :                     ELSE
    1805             :                     {
    1806     1862329 :                         test();
    1807     1862329 :                         test();
    1808     1862329 :                         test();
    1809     1862329 :                         test();
    1810             :                         /* check whether change in whitening level should be allowed or not (if SFM is inside a certain margin around thresholds) */
    1811     1862329 :                         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      258145 :                             mean_past_SFM = 0;
    1820      258145 :                             mean_past_SFM_e = 0;
    1821      258145 :                             countable = 0;
    1822      258145 :                             move16();
    1823      258145 :                             move16();
    1824      258145 :                             move16();
    1825             : 
    1826             :                             /* compute mean of last (available) SFM values */
    1827     1548870 :                             FOR( i = 0; i < IGF_PAST_SFM_LEN; i++ )
    1828             :                             {
    1829     1290725 :                                 IF( hPrivateData->igfPastSFM_fx[p][i] >= 0 )
    1830             :                                 {
    1831     1101324 :                                     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     1101324 :                                     countable = add( countable, 1 );
    1833             :                                 }
    1834             :                             }
    1835      258145 :                             IF( countable )
    1836             :                             {
    1837             :                                 Word16 temp;
    1838      253099 :                                 mean_past_SFM = BASOP_Util_Divide1616_Scale( mean_past_SFM, countable, &temp );
    1839      253099 :                                 mean_past_SFM_e = add( mean_past_SFM_e, sub( temp, 15 ) );
    1840      253099 :                                 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      253099 :                                 if ( LT_16( abs_s( sub( SFM, mean_past_SFM ) ), 1638 ) /*0.2 in Q13*/ )
    1843             :                                 {
    1844      122013 :                                     hPrivateData->igfCurrWhiteningLevel[p] = hPrivateData->igfPrevWhiteningLevel[p];
    1845      122013 :                                     move16();
    1846             :                                 }
    1847             :                             }
    1848             :                         }
    1849             :                     }
    1850             : 
    1851     1911243 :                     hPrivateData->igfPastSFM_fx[p][hPrivateData->igfPastSFM_pos] = SFM; /*2Q13*/
    1852     1911243 :                     move16();
    1853             :                 }
    1854             :             }
    1855             : 
    1856      557860 :             SWITCH( hPrivateData->igfInfo.bitRateIndex )
    1857             :             {
    1858       84262 :                 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       84262 :                     hPrivateData->igfCurrWhiteningLevel[hGrid->nTiles - 1] = hPrivateData->igfCurrWhiteningLevel[hGrid->nTiles - 2];
    1876       84262 :                     move16();
    1877       84262 :                     break;
    1878      473598 :                 default:
    1879      473598 :                     break;
    1880             :             }
    1881             :         }
    1882             :         ELSE
    1883             :         {
    1884        5631 :             FOR( p = 0; p < hGrid->nTiles; p++ )
    1885             :             {
    1886        4341 :                 hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_MID;
    1887        4341 :                 move16();
    1888             :             }
    1889             :         }
    1890             :     }
    1891             :     ELSE
    1892             :     {
    1893             :         /* reset filter */
    1894      367191 :         FOR( p = 0; p < IGF_MAX_TILES; p++ )
    1895             :         {
    1896      333810 :             hPrivateData->prevSFM_FIR[p] = L_deposit_l( 0 );
    1897      333810 :             hPrivateData->prevSFM_IIR[p] = 0;
    1898      333810 :             move32();
    1899      333810 :             move16();
    1900             :         }
    1901             :     }
    1902             : 
    1903      592531 :     IF( element_mode > EVS_MONO )
    1904             :     {
    1905      592531 :         IF( EQ_16( SFM, -ONE_IN_Q13 /*1.0f 2Q13*/ ) ) /* reset */
    1906             :         {
    1907      131004 :             FOR( p = 0; p < hGrid->nTiles; p++ )
    1908             :             {
    1909       96333 :                 set16_fx( hPrivateData->igfPastSFM_fx[p], -ONE_IN_Q13, IGF_PAST_SFM_LEN );
    1910       96333 :                 hPrivateData->igfWhiteningHangoverCnt[p] = 2;
    1911       96333 :                 move16();
    1912             :             }
    1913             :         }
    1914             : 
    1915             :         /* vibrato handling */
    1916     1798079 :         FOR( p = 0; p < hGrid->nTiles; p = p + 2 )
    1917             :         {
    1918     1205548 :             test();
    1919     1205548 :             test();
    1920     1205548 :             test();
    1921     1205548 :             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      905150 :                 FOR( i = 0; i < 4; i++ )
    1929             :                 {
    1930      724120 :                     pastSfm_a[i] = hPrivateData->igfPastSFM_fx[p][add( hPrivateData->igfPastSFM_pos, sub( 4, i ) ) % IGF_PAST_SFM_LEN];
    1931      724120 :                     pastSfm_b[i] = hPrivateData->igfPastSFM_fx[p + 1][add( hPrivateData->igfPastSFM_pos, sub( 4, i ) ) % IGF_PAST_SFM_LEN];
    1932      724120 :                     move16();
    1933      724120 :                     move16();
    1934             :                 }
    1935      181030 :                 pastSfmDiffSum_a = pastSfmDiffSum_b = 0;
    1936      181030 :                 move16();
    1937      181030 :                 move16();
    1938      511811 :                 FOR( i = 0; i < 3; i++ )
    1939             :                 {
    1940      422520 :                     IF( NE_16( pastSfm_a[i + 1], -ONE_IN_Q13 ) )
    1941             :                     {
    1942      330781 :                         pastSfmDiffSum_a = add( pastSfmDiffSum_a, sub( pastSfm_a[i], pastSfm_a[i + 1] ) );
    1943      330781 :                         pastSfmDiffSum_b = add( pastSfmDiffSum_b, sub( pastSfm_b[i], pastSfm_b[i + 1] ) );
    1944             :                     }
    1945             :                     ELSE
    1946             :                     {
    1947       91739 :                         break;
    1948             :                     }
    1949             :                 }
    1950      181030 :                 test();
    1951      181030 :                 test();
    1952      181030 :                 test();
    1953      181030 :                 test();
    1954             :                 /* if tonality oscillates between two tiles, turn whitening off in both */
    1955      181030 :                 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        8925 :                     hPrivateData->igfCurrWhiteningLevel[p] = hPrivateData->igfCurrWhiteningLevel[p + 1] = IGF_WHITENING_OFF;
    1960        8925 :                     move16();
    1961        8925 :                     move16();
    1962             :                 }
    1963             :             }
    1964             :         }
    1965             : 
    1966             :         /* hangover */
    1967     2600107 :         FOR( p = 0; p < hGrid->nTiles; p++ )
    1968             :         {
    1969     2007576 :             IF( NE_16( hPrivateData->igfCurrWhiteningLevel[p], hPrivateData->igfPrevWhiteningLevel[p] ) )
    1970             :             {
    1971      408718 :                 hPrivateData->igfWhiteningHangoverCnt[p] = add( hPrivateData->igfWhiteningHangoverCnt[p], 1 );
    1972      408718 :                 IF( EQ_16( hPrivateData->igfWhiteningHangoverCnt[p], 3 ) )
    1973             :                 {
    1974      113539 :                     hPrivateData->igfWhiteningHangoverCnt[p] = 0;
    1975             :                 }
    1976             :                 ELSE
    1977             :                 {
    1978      295179 :                     hPrivateData->igfCurrWhiteningLevel[p] = hPrivateData->igfPrevWhiteningLevel[p];
    1979             :                 }
    1980      408718 :                 move16();
    1981      408718 :                 move16();
    1982             :             }
    1983             :             ELSE
    1984             :             {
    1985     1598858 :                 hPrivateData->igfWhiteningHangoverCnt[p] = 0;
    1986     1598858 :                 move16();
    1987             :             }
    1988             :         }
    1989             : 
    1990      592531 :         hPrivateData->igfPastSFM_pos = add( hPrivateData->igfPastSFM_pos, 1 ) % IGF_PAST_SFM_LEN;
    1991      592531 :         move16();
    1992             :     }
    1993             : 
    1994      592531 :     hPrivateData->wasTransient = isTransient;
    1995      592531 :     move16();
    1996             : 
    1997      592531 :     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     2446586 : 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     2446586 :     totBitCount = 0;
    2018     2446586 :     startBitCount = *pBitOffset;
    2019     2446586 :     move16();
    2020     2446586 :     move16();
    2021             : 
    2022     2446586 :     IF( EQ_32( whiteningLevel, IGF_WHITENING_MID ) )
    2023             :     {
    2024      923404 :         IGF_write_bits( hBstr, pBitOffset, 0, 1 );
    2025             :     }
    2026             :     ELSE
    2027             :     {
    2028     1523182 :         IGF_write_bits( hBstr, pBitOffset, 1, 1 );
    2029     1523182 :         IF( whiteningLevel == IGF_WHITENING_OFF )
    2030             :         {
    2031      761780 :             IGF_write_bits( hBstr, pBitOffset, 0, 1 );
    2032             :         }
    2033             :         ELSE
    2034             :         {
    2035      761402 :             IGF_write_bits( hBstr, pBitOffset, 1, 1 );
    2036             :         }
    2037             :     }
    2038     2446586 :     totBitCount = sub( *pBitOffset, startBitCount );
    2039             : 
    2040     2446586 :     return totBitCount;
    2041             : }
    2042             : 
    2043             : 
    2044             : /*-------------------------------------------------------------------*
    2045             :  * IGF_WriteWhiteningLevels_fx()
    2046             :  *
    2047             :  * writes the whitening levels
    2048             :  *-------------------------------------------------------------------*/
    2049             : 
    2050     1162966 : 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     1162966 :     totBitCount = 0;
    2068     1162966 :     move16();
    2069     1162966 :     isSame = 1;
    2070     1162966 :     move16();
    2071     1162966 :     startBitCount = *pBitOffset;
    2072     1162966 :     move16();
    2073     1162966 :     hPrivateData = &hInstance->igfData;
    2074     1162966 :     hGrid = &hPrivateData->igfInfo.grid[igfGridIdx];
    2075     1162966 :     nTiles = hGrid->nTiles;
    2076     1162966 :     move16();
    2077             : 
    2078     1162966 :     IF( isIndepFlag )
    2079             :     {
    2080     1151918 :         isSame = 0;
    2081     1151918 :         move16();
    2082             :     }
    2083             :     ELSE
    2084             :     {
    2085       11048 :         p = 0;
    2086       11048 :         move16();
    2087       11048 :         tmp32 = 0;
    2088       11048 :         move32();
    2089       11048 :         test();
    2090       36323 :         WHILE( ( LT_16( p, nTiles ) ) && ( tmp32 == 0 ) )
    2091             :         {
    2092       25275 :             test();
    2093       25275 :             tmp32 = L_sub( hPrivateData->igfCurrWhiteningLevel[p], hPrivateData->igfPrevWhiteningLevel[p] );
    2094       25275 :             if ( tmp32 != 0 )
    2095             :             {
    2096         660 :                 isSame = 0;
    2097         660 :                 move16();
    2098             :             }
    2099       25275 :             p++;
    2100             :         }
    2101             :     }
    2102     1162966 :     IF( isSame )
    2103             :     {
    2104       10388 :         IGF_write_bits( hBstr, pBitOffset, 1, 1 );
    2105             :     }
    2106             :     ELSE
    2107             :     {
    2108     1152578 :         IF( !isIndepFlag )
    2109             :         {
    2110         660 :             IGF_write_bits( hBstr, pBitOffset, 0, 1 );
    2111             :         }
    2112     1152578 :         IGF_WriteWhiteningTile_fx( hBstr, pBitOffset, hPrivateData->igfCurrWhiteningLevel[0] );
    2113     1152578 :         p = 1;
    2114     1152578 :         move16();
    2115     1152578 :         tmp32 = 0;
    2116     1152578 :         move32();
    2117     1152578 :         test();
    2118     1152578 :         IF( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) )
    2119             :         {
    2120      146934 :             isSame = 1;
    2121      146934 :             move16();
    2122             :         }
    2123             :         ELSE
    2124             :         {
    2125     1005644 :             if ( LT_16( p, nTiles ) )
    2126             :             {
    2127      506648 :                 isSame = 1;
    2128      506648 :                 move16();
    2129             :             }
    2130     1005644 :             test();
    2131     2306755 :             WHILE( ( LT_16( p, nTiles ) ) && ( tmp32 == 0 ) )
    2132             :             {
    2133     1301111 :                 test();
    2134     1301111 :                 tmp32 = L_sub( hPrivateData->igfCurrWhiteningLevel[p], hPrivateData->igfCurrWhiteningLevel[p - 1] );
    2135     1301111 :                 if ( tmp32 != 0 )
    2136             :                 {
    2137      287320 :                     isSame = 0;
    2138      287320 :                     move16();
    2139             :                 }
    2140     1301111 :                 p++;
    2141             :             }
    2142             :         }
    2143     1152578 :         test();
    2144     1152578 :         IF( !isSame )
    2145             :         {
    2146      786316 :             IGF_write_bits( hBstr, pBitOffset, 1, 1 );
    2147     2080324 :             FOR( p = 1; p < nTiles; p++ )
    2148             :             {
    2149     1294008 :                 IGF_WriteWhiteningTile_fx( hBstr, pBitOffset, hPrivateData->igfCurrWhiteningLevel[p] );
    2150             :             }
    2151             :         }
    2152      366262 :         ELSE IF( NE_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) && NE_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) )
    2153             :         {
    2154      219328 :             IGF_write_bits( hBstr, pBitOffset, 0, 1 );
    2155             :         }
    2156             :     }
    2157             : 
    2158     1162966 :     totBitCount = sub( *pBitOffset, startBitCount );
    2159             : 
    2160     1162966 :     return totBitCount;
    2161             : }
    2162             : 
    2163             : 
    2164             : /*-------------------------------------------------------------------*
    2165             :  * IGF_WriteFlatteningTrigger_fx()
    2166             :  *
    2167             :  * write flattening trigger
    2168             :  *-------------------------------------------------------------------*/
    2169             : 
    2170             : /*! r: number of bits written */
    2171     1162966 : 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     1162966 :     totBitCount = 0;
    2182     1162966 :     startBitCount = *pBitOffset;
    2183     1162966 :     flatteningTrigger = hInstance->flatteningTrigger;
    2184     1162966 :     move16();
    2185     1162966 :     move16();
    2186     1162966 :     move16();
    2187             : 
    2188     1162966 :     IGF_write_bits( hBstr, pBitOffset, flatteningTrigger, 1 );
    2189     1162966 :     totBitCount = sub( *pBitOffset, startBitCount );
    2190             : 
    2191     1162966 :     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     1162966 : 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     1162966 :     startBitCount = *pBitOffset;
    2220     1162966 :     move16();
    2221     1162966 :     hIGFEnc->infoTotalBitsPerFrameWritten = 0;
    2222     1162966 :     move16();
    2223             : 
    2224     1162966 :     if ( isIndepFlag )
    2225             :     {
    2226     1151918 :         hIGFEnc->infoTotalBitsWritten = 0;
    2227     1151918 :         move16();
    2228             :     }
    2229             : 
    2230     1162966 :     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     1162966 :     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     1162966 :     IGF_WriteFlatteningTrigger_fx( hIGFEnc,      /* i: instance handle of IGF Encoder                                              */
    2244             :                                    hBstr,        /* i: encoder state                                                               */
    2245             :                                    pBitOffset ); /* i: ptr to bitOffset counter                                                    */
    2246             : 
    2247     1162966 :     hIGFEnc->infoTotalBitsPerFrameWritten = sub( *pBitOffset, startBitCount );
    2248     1162966 :     hIGFEnc->infoTotalBitsWritten = add( hIGFEnc->infoTotalBitsWritten, hIGFEnc->infoTotalBitsPerFrameWritten );
    2249     1162966 :     move16();
    2250     1162966 :     move16();
    2251             : 
    2252     1162966 :     return hIGFEnc->infoTotalBitsPerFrameWritten;
    2253             : }
    2254             : 
    2255             : 
    2256             : /*-------------------------------------------------------------------*
    2257             :  * IGFEncSetMode()
    2258             :  *
    2259             :  * sets the IGF mode according to given bitrate
    2260             :  *-------------------------------------------------------------------*/
    2261       35731 : 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       35731 :     hPrivateData = &hIGFEnc->igfData;
    2273       35731 :     hPrivateData->igfBitstreamBits = 0;
    2274       35731 :     move16();
    2275       35731 :     set16_fx( hPrivateData->igfScfQuantized, 0, IGF_MAX_SFB );
    2276       35731 :     set16_fx( hPrivateData->igfCurrWhiteningLevel, 0, IGF_MAX_TILES );
    2277       35731 :     set16_fx( hPrivateData->igfPrevWhiteningLevel, 0, IGF_MAX_TILES );
    2278       35731 :     set16_fx( hPrivateData->igfWhiteningHangoverCnt, 0, IGF_MAX_TILES );
    2279      393041 :     FOR( i = 0; i < IGF_MAX_TILES; i++ )
    2280             :     {
    2281      357310 :         set16_fx( hPrivateData->igfPastSFM_fx[i], -( ONE_IN_Q13 ), IGF_PAST_SFM_LEN );
    2282             :     }
    2283             : 
    2284       35731 :     hPrivateData->igfPastSFM_pos = 0;
    2285       35731 :     move16();
    2286             : 
    2287    11469651 :     FOR( i = 0; i < IGF_BITBUFSIZE / 8; i++ )
    2288             :     {
    2289    11433920 :         hPrivateData->igfBitstream[i] = 0;
    2290    11433920 :         move16();
    2291             :     }
    2292       35731 :     hPrivateData->wasTransient = 0;
    2293       35731 :     move16();
    2294       35731 :     set32_fx( hPrivateData->prevSFM_FIR, 0, IGF_MAX_TILES );
    2295       35731 :     set16_fx( hPrivateData->prevSFM_IIR, 0, IGF_MAX_TILES );
    2296       35731 :     set16_fx( hPrivateData->dampingFactorSmoothing, 2, IGF_MAX_SFB );
    2297       35731 :     set16_fx( hPrivateData->prevSFM_FIR_SFB_SB_fx, 0, IGF_MAX_SFB );
    2298       35731 :     set16_fx( hPrivateData->prevSFB_FIR_TB_e, 15, IGF_MAX_SFB );
    2299       35731 :     set16_fx( hPrivateData->prevSFB_FIR_SB_e, 15, IGF_MAX_SFB );
    2300       35731 :     set16_fx( hPrivateData->prevSFM_IIR_SFB_SB_fx, 0, IGF_MAX_SFB );
    2301       35731 :     set16_fx( hPrivateData->prevSFB_IIR_SB_e, 15, IGF_MAX_SFB );
    2302       35731 :     set16_fx( hPrivateData->prevSFM_FIR_SFB_TB_fx, 0, IGF_MAX_SFB );
    2303       35731 :     set16_fx( hPrivateData->prevSFB_IIR_TB_e, 15, IGF_MAX_SFB );
    2304       35731 :     set16_fx( hPrivateData->sfb_tb_e, 15, IGF_MAX_SFB );
    2305       35731 :     set16_fx( hPrivateData->sfb_sb_e, 15, IGF_MAX_SFB );
    2306       35731 :     set16_fx( hPrivateData->prevSFM_IIR_SFB_TB_fx, 0, IGF_MAX_SFB );
    2307       35731 :     set16_fx( hPrivateData->prevDampingFactor_IIR_fx, -( ONE_IN_Q15 ), IGF_MAX_SFB );
    2308       35731 :     set16_fx( hPrivateData->prevDampingFactor_IIR_e, 0, IGF_MAX_SFB );
    2309       35731 :     set16_fx( hPrivateData->logSpec, 0, L_FRAME_PLUS );
    2310       35731 :     set16_fx( hPrivateData->SFM_sb_fx, 0, IGF_MAX_SFB );
    2311       35731 :     set16_fx( hPrivateData->SFM_tb_fx, 0, IGF_MAX_SFB );
    2312             : 
    2313       35731 :     IF( IGFCommonFuncsIGFConfiguration_ivas_fx( total_brate, bwidth, element_mode, &hPrivateData->igfInfo, rf_mode ) != 0 )
    2314             :     {
    2315       35731 :         IGFSCFEncoderOpen_fx( &hPrivateData->hIGFSCFArithEnc, &hPrivateData->igfInfo, total_brate, bwidth, element_mode, rf_mode );
    2316             : 
    2317       35731 :         hIGFEnc->infoSamplingRate = hPrivateData->igfInfo.sampleRate;
    2318       35731 :         move32();
    2319       35731 :         hIGFEnc->infoStartFrequency = hPrivateData->igfInfo.grid[0].startFrequency;
    2320       35731 :         move16();
    2321       35731 :         hIGFEnc->infoStopFrequency = hPrivateData->igfInfo.grid[0].stopFrequency;
    2322       35731 :         move16();
    2323       35731 :         hIGFEnc->infoStartLine = hPrivateData->igfInfo.grid[0].startLine;
    2324       35731 :         move16();
    2325       35731 :         hIGFEnc->infoStopLine = hPrivateData->igfInfo.grid[0].stopLine;
    2326       35731 :         move16();
    2327             :     }
    2328             :     ELSE
    2329             :     {
    2330             :         /* IGF configuration failed -> error! */
    2331           0 :         hIGFEnc->infoSamplingRate = 0;
    2332           0 :         move32();
    2333           0 :         hIGFEnc->infoStartFrequency = -1;
    2334           0 :         move16();
    2335           0 :         hIGFEnc->infoStopFrequency = -1;
    2336           0 :         move16();
    2337           0 :         hIGFEnc->infoStartLine = -1;
    2338           0 :         move16();
    2339           0 :         hIGFEnc->infoStopLine = -1;
    2340           0 :         move16();
    2341           0 :         fprintf( stderr, "IGFEncSetMode_fx: initialization error!\n" );
    2342             :     }
    2343             : 
    2344             :     /* reset remaining variables */
    2345       35731 :     hIGFEnc->infoTotalBitsWritten = 0;
    2346       35731 :     move16();
    2347       35731 :     hIGFEnc->infoTotalBitsPerFrameWritten = 0;
    2348       35731 :     move16();
    2349       35731 :     hIGFEnc->flatteningTrigger = 0;
    2350       35731 :     move16();
    2351       35731 :     hIGFEnc->spec_be_igf_e = 0;
    2352       35731 :     move16();
    2353       35731 :     hIGFEnc->tns_predictionGain = 0;
    2354       35731 :     move16();
    2355       35731 :     set32_fx( hIGFEnc->spec_be_igf, 0, N_MAX_TCX - IGF_START_MN );
    2356       35731 :     return;
    2357             : }
    2358             : 
    2359             : /*-------------------------------------------------------------------*
    2360             :  * pack_bit_ivas()
    2361             :  *
    2362             :  * insert a bit into packed octet
    2363             :  *-------------------------------------------------------------------*/
    2364             : 
    2365             : /*-------------------------------------------------------------------*
    2366             :  * IGFEncConcatenateBitstream()
    2367             :  *
    2368             :  * IGF bitstream concatenation for TCX10 modes
    2369             :  *-------------------------------------------------------------------*/
    2370             : 
    2371             : /*-------------------------------------------------------------------*
    2372             :  * IGFEncResetTCX10BitCounter_ivas_fx()
    2373             :  *
    2374             :  * IGF reset bitstream bit counter for TCX10 modes
    2375             :  *-------------------------------------------------------------------*/
    2376             : 
    2377           0 : void IGFEncResetTCX10BitCounter_ivas_fx(
    2378             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc /* i  : instance handle of IGF Encoder */
    2379             : )
    2380             : {
    2381             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
    2382             : 
    2383           0 :     hPrivateData = &hIGFEnc->igfData;
    2384           0 :     hPrivateData->igfBitstreamBits = 0;
    2385           0 :     hIGFEnc->infoTotalBitsWritten = 0;
    2386           0 :     move16();
    2387           0 :     move16();
    2388           0 :     return;
    2389             : }
    2390             : 
    2391             : 
    2392             : /*-------------------------------------------------------------------*
    2393             :  * IGFEncApplyMono()
    2394             :  *
    2395             :  * apply the IGF encoder, main encoder interface
    2396             :  *-------------------------------------------------------------------*/
    2397             : 
    2398      492473 : void IGFEncApplyMono_ivas_fx(
    2399             :     Encoder_State *st,             /* i  : Encoder state                                          */
    2400             :     Word16 powerSpectrum_len,      /* i: length of pPowerSpectrum_fx buffer */
    2401             :     const Word16 igfGridIdx,       /* i  : IGF grid index                                         */
    2402             :     Word32 *pMDCTSpectrum_fx,      /* i/o: MDCT spectrum                                          */
    2403             :     Word16 e_mdct,                 /* i : exponent of pMDCTspectrum                                                        */
    2404             :     Word32 *pPowerSpectrum_fx,     /* i/o: MDCT^2 + MDST^2 spectrum, or estimate                  */
    2405             :     Word16 *e_ps,                  /* i : exponent of pPowerSpectrum                                                       */
    2406             :     const Word16 isTCX20,          /* i  : flag indicating if the input is TCX20 or TCX10/2xTCX5  */
    2407             :     const Word8 isTNSActive,       /* i  : flag indicating if the TNS is active                   */
    2408             :     const Word16 sp_aud_decision0, /* i  : first stage switching decision                         */
    2409             :     const Word16 vad_hover_flag    /* i  : VAD hangover flag                                      */
    2410             : )
    2411             : {
    2412             :     Word32 *pPowerSpectrumParameter_fx;
    2413             :     Word16 *pPowerSpectrumParameter_exp;
    2414      492473 :     Word16 att_fx = MAX16B;
    2415             :     Word16 last_core_acelp;
    2416      492473 :     move16();
    2417             : 
    2418             :     Word32 common_pPowerSpectrum_fx[N_MAX + L_MDCT_OVLP_MAX];
    2419             : 
    2420      492473 :     set32_fx( common_pPowerSpectrum_fx, 0, N_MAX );
    2421             : 
    2422      492473 :     Word16 common_pPowerSpectrum_exp = MIN16B;
    2423      492473 :     move16();
    2424      492473 :     IF( st->last_core == ACELP_CORE )
    2425             :     {
    2426        8475 :         last_core_acelp = 1;
    2427        8475 :         move16();
    2428             :     }
    2429             :     ELSE
    2430             :     {
    2431      483998 :         last_core_acelp = 0;
    2432      483998 :         move16();
    2433             :     }
    2434             : 
    2435      492473 :     test();
    2436      492473 :     IF( !isTNSActive && isTCX20 )
    2437             :     {
    2438      431934 :         pPowerSpectrumParameter_fx = pPowerSpectrum_fx;
    2439      431934 :         pPowerSpectrumParameter_exp = e_ps;
    2440             :     }
    2441             :     ELSE
    2442             :     {
    2443       60539 :         pPowerSpectrumParameter_fx = NULL;
    2444       60539 :         pPowerSpectrumParameter_exp = NULL;
    2445             :     }
    2446             : 
    2447      492473 :     IGF_UpdateInfo( st->hIGFEnc, igfGridIdx );
    2448             : 
    2449      492473 :     test();
    2450      492473 :     IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) || EQ_16( st->element_mode, IVAS_CPE_TD ) )
    2451             :     {
    2452       12683 :         calculate_hangover_attenuation_gain_ivas_fx( st, &att_fx, vad_hover_flag );
    2453             :     }
    2454             : 
    2455      492473 :     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 );
    2456             : 
    2457      492473 :     IF( isTCX20 )
    2458             :     {
    2459      474597 :         pPowerSpectrumParameter_fx = pPowerSpectrum_fx;
    2460      474597 :         pPowerSpectrumParameter_exp = e_ps;
    2461             :     }
    2462             :     ELSE
    2463             :     {
    2464       17876 :         pPowerSpectrumParameter_fx = NULL;
    2465       17876 :         pPowerSpectrumParameter_exp = NULL;
    2466             :     }
    2467             : 
    2468      492473 :     IF( EQ_16( st->element_mode, IVAS_CPE_MDCT ) )
    2469             :     {
    2470      265674 :         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 );
    2471             :     }
    2472             :     ELSE
    2473             :     {
    2474      226799 :         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 );
    2475             :     }
    2476             : 
    2477      492473 :     IF( pPowerSpectrumParameter_fx )
    2478             :     {
    2479   625437537 :         FOR( Word16 i = 0; i < powerSpectrum_len; i++ )
    2480             :         {
    2481   624962940 :             common_pPowerSpectrum_exp = s_max( common_pPowerSpectrum_exp, pPowerSpectrumParameter_exp[i] );
    2482             :         }
    2483             : 
    2484   625437537 :         FOR( Word16 i = 0; i < powerSpectrum_len; i++ )
    2485             :         {
    2486   624962940 :             common_pPowerSpectrum_fx[i] = L_shl( pPowerSpectrumParameter_fx[i], sub( pPowerSpectrumParameter_exp[i], common_pPowerSpectrum_exp ) );
    2487   624962940 :             move16();
    2488             :         }
    2489      474597 :         pPowerSpectrumParameter_fx = common_pPowerSpectrum_fx;
    2490             :     }
    2491      492473 :     IGF_ErodeSpectrum_ivas_fx( st->hIGFEnc, pMDCTSpectrum_fx, pPowerSpectrumParameter_fx, common_pPowerSpectrum_exp, igfGridIdx, 0 );
    2492      492473 : }
    2493             : 
    2494             : 
    2495             : /*-------------------------------------------------------------------*
    2496             :  * IGFEncApplyStereo()
    2497             :  *
    2498             :  * apply the IGF encoder, main encoder interface
    2499             :  *-------------------------------------------------------------------*/
    2500             : 
    2501       50029 : void IGFEncApplyStereo_fx(
    2502             :     STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct,                /* i/o: MDCT stereo encoder structure           */
    2503             :     Word16 ms_mask[2][MAX_SFB],                             /* i  : bandwise MS mask                        */
    2504             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc[CPE_CHANNELS],    /* i  : instance handle of IGF Encoder          */
    2505             :     const Word16 igfGridIdx,                                /* i  : IGF grid index                          */
    2506             :     Encoder_State *sts[CPE_CHANNELS],                       /* i  : Encoder state                           */
    2507             :     Word32 *pPowerSpectrum_fx[CPE_CHANNELS],                /* i/o: MDCT^2 + MDST^2 spectrum, or estimate   */
    2508             :     Word16 *exp_pPowerSpectrum_fx[CPE_CHANNELS],            /* i/o: exp of pPowerSpectrum_fx                */
    2509             :     Word32 *pPowerSpectrumMsInv_fx[CPE_CHANNELS][NB_DIV],   /* i/o: inverse power spectrum                  */
    2510             :     Word16 *q_pPowerSpectrumMsInv_fx[CPE_CHANNELS][NB_DIV], /* i/o: Q of pPowerSpectrumMsInv_fx           */
    2511             :     Word32 *inv_spectrum_fx[CPE_CHANNELS][NB_DIV],          /* i  : inverse spectrum                        */
    2512             :     Word16 exp_inv_spectrum_fx[CPE_CHANNELS],               /* i  : exp of inverse spectrum                 */
    2513             :     const Word16 frameno,                                   /* i  : flag indicating index of current subfr. */
    2514             :     const Word16 sp_aud_decision0,                          /* i  : sp_aud_decision0                        */
    2515             :     const Word32 element_brate,                             /* i  : element bitrate                         */
    2516             :     const Word16 mct_on )
    2517             : {
    2518             :     Word32 *pPowerSpectrumParameter_fx[NB_DIV];     /* If it is NULL it informs a function that specific handling is needed */
    2519             :     Word16 *exp_pPowerSpectrumParameter_fx[NB_DIV]; /* If it is NULL it informs a function that specific handling is needed */
    2520             :     Word32 *pPowerSpectrumParameterMsInv_fx[NB_DIV];
    2521             :     Word16 *q_pPowerSpectrumParameterMsInv_fx[NB_DIV];
    2522             :     Word16 coreMsMask[N_MAX];
    2523             :     Word16 sfb, ch, last_core_acelp;
    2524             :     STEREO_MDCT_BAND_PARAMETERS *sfbConf;
    2525             :     Word32 common_pPowerSpectrum_fx[N_MAX];
    2526             : 
    2527       50029 :     set32_fx( common_pPowerSpectrum_fx, 0, N_MAX );
    2528             : 
    2529       50029 :     Word16 common_pPowerSpectrum_exp = MIN16B;
    2530       50029 :     move16();
    2531             : 
    2532             :     /* assumptions: stereo filling was already done on the flattened spectra
    2533             :      *               IGF region is always coded M/S, never L/R (to be done in the encoder)
    2534             :      *               for residual bands with stereo filling infoTcxNoise is set to zero
    2535             :      *               both channels have the same IGF configuration
    2536             :      */
    2537             : 
    2538             :     /* sanity checks: check if both channels have the same configuration...*/
    2539       50029 :     assert( ( sts[0]->core == sts[1]->core ) );
    2540             : 
    2541             :     /* initialization */
    2542       50029 :     IF( EQ_16( sts[0]->core, TCX_20_CORE ) )
    2543             :     {
    2544       47919 :         sfbConf = &hStereoMdct->stbParamsTCX20;
    2545             :     }
    2546             :     ELSE
    2547             :     {
    2548        2110 :         sfbConf = &hStereoMdct->stbParamsTCX10;
    2549             :     }
    2550       50029 :     if ( sts[0]->last_core == ACELP_CORE )
    2551             :     {
    2552           0 :         sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
    2553             :     }
    2554             : 
    2555             :     /* create line wise ms mask for the core bands */
    2556       50029 :     set16_fx( coreMsMask, 0, N_MAX );
    2557     2134083 :     FOR( sfb = 0; sfb < sfbConf->sfbCnt; sfb++ )
    2558             :     {
    2559     2084054 :         set16_fx( &coreMsMask[sfbConf->sfbOffset[sfb]], ms_mask[frameno][sfb], sub( sfbConf->sfbOffset[sfb + 1], sfbConf->sfbOffset[sfb] ) );
    2560             :     }
    2561             : 
    2562       50029 :     test();
    2563       50029 :     test();
    2564       50029 :     IF( EQ_16( sts[0]->core, TCX_20_CORE ) && !sts[0]->hTcxEnc->fUseTns[frameno] && !sts[1]->hTcxEnc->fUseTns[frameno] )
    2565             :     {
    2566       40685 :         pPowerSpectrumParameter_fx[0] = &pPowerSpectrum_fx[0][0];
    2567       40685 :         exp_pPowerSpectrumParameter_fx[0] = &exp_pPowerSpectrum_fx[0][0];
    2568       40685 :         pPowerSpectrumParameter_fx[1] = &pPowerSpectrum_fx[1][0];
    2569       40685 :         exp_pPowerSpectrumParameter_fx[1] = &exp_pPowerSpectrum_fx[1][0];
    2570       40685 :         pPowerSpectrumParameterMsInv_fx[0] = pPowerSpectrumMsInv_fx[0][0];
    2571       40685 :         pPowerSpectrumParameterMsInv_fx[1] = pPowerSpectrumMsInv_fx[1][0];
    2572       40685 :         q_pPowerSpectrumParameterMsInv_fx[0] = q_pPowerSpectrumMsInv_fx[0][0];
    2573       40685 :         q_pPowerSpectrumParameterMsInv_fx[1] = q_pPowerSpectrumMsInv_fx[1][0];
    2574             :     }
    2575             :     ELSE
    2576             :     {
    2577        9344 :         pPowerSpectrumParameter_fx[0] = NULL;
    2578        9344 :         pPowerSpectrumParameter_fx[1] = NULL;
    2579        9344 :         pPowerSpectrumParameterMsInv_fx[0] = NULL;
    2580        9344 :         pPowerSpectrumParameterMsInv_fx[1] = NULL;
    2581        9344 :         q_pPowerSpectrumParameterMsInv_fx[0] = NULL;
    2582        9344 :         q_pPowerSpectrumParameterMsInv_fx[1] = NULL;
    2583             :     }
    2584      150087 :     FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
    2585             :     {
    2586      100058 :         last_core_acelp = extract_l( EQ_16( sts[ch]->last_core, ACELP_CORE ) );
    2587             : 
    2588      100058 :         IGF_UpdateInfo( hIGFEnc[ch], igfGridIdx );
    2589      100058 :         IGF_CalculateStereoEnvelope_fx( hIGFEnc[ch], sts[ch]->hTcxEnc->spectrum_fx[frameno], sts[ch]->hTcxEnc->spectrum_e[frameno], inv_spectrum_fx[ch][frameno],
    2590      100058 :                                         exp_inv_spectrum_fx[ch], pPowerSpectrumParameter_fx[ch], exp_pPowerSpectrum_fx[ch], pPowerSpectrumParameterMsInv_fx[ch],
    2591      100058 :                                         q_pPowerSpectrumParameterMsInv_fx[ch], igfGridIdx, coreMsMask, sts[ch]->hTranDet->transientDetector.bIsAttackPresent, last_core_acelp, mct_on );
    2592             : 
    2593      100058 :         IF( EQ_16( sts[ch]->core, TCX_20_CORE ) )
    2594             :         {
    2595       95838 :             pPowerSpectrumParameter_fx[ch] = pPowerSpectrum_fx[ch];
    2596       95838 :             exp_pPowerSpectrumParameter_fx[ch] = exp_pPowerSpectrum_fx[ch];
    2597             :         }
    2598             :         ELSE
    2599             :         {
    2600        4220 :             pPowerSpectrumParameter_fx[ch] = NULL;
    2601             :         }
    2602             : 
    2603      100058 :         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 );
    2604             : 
    2605      100058 :         IF( pPowerSpectrumParameter_fx[ch] )
    2606             :         {
    2607       95838 :             Word16 length = N_MAX;
    2608       95838 :             move16();
    2609       95838 :             if ( mct_on )
    2610             :             {
    2611       60830 :                 length = L_FRAME48k;
    2612       60830 :                 move16();
    2613             :             }
    2614   100502238 :             FOR( Word16 i = 0; i < length; i++ )
    2615             :             {
    2616   100406400 :                 common_pPowerSpectrum_exp = s_max( common_pPowerSpectrum_exp, exp_pPowerSpectrumParameter_fx[ch][i] );
    2617             :             }
    2618             : 
    2619   100502238 :             FOR( Word16 i = 0; i < length; i++ )
    2620             :             {
    2621   100406400 :                 common_pPowerSpectrum_fx[i] = L_shl( pPowerSpectrumParameter_fx[ch][i], sub( exp_pPowerSpectrumParameter_fx[ch][i], common_pPowerSpectrum_exp ) );
    2622   100406400 :                 move32();
    2623             :             }
    2624       95838 :             pPowerSpectrumParameter_fx[ch] = common_pPowerSpectrum_fx;
    2625             :         }
    2626      100058 :         IGF_ErodeSpectrum_ivas_fx( hIGFEnc[ch], sts[ch]->hTcxEnc->spectrum_fx[frameno], pPowerSpectrumParameter_fx[ch], common_pPowerSpectrum_exp, igfGridIdx, mct_on );
    2627             :     }
    2628       50029 :     return;
    2629             : }
    2630             : 
    2631             : 
    2632             : /*-------------------------------------------------------------------*
    2633             :  * IGFSaveSpectrumForITF()
    2634             :  *
    2635             :  *
    2636             :  *-------------------------------------------------------------------*/
    2637             : 
    2638      592531 : void IGFSaveSpectrumForITF_ivas_fx(
    2639             :     IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i/o: instance handle of IGF Encoder  */
    2640             :     const Word16 igfGridIdx,         /* i  : IGF grid index                  */
    2641             :     const Word32 *pITFSpectrum,      /* i  : MDCT spectrum                   */
    2642             :     Word16 exp_pITFSpectrum )
    2643             : {
    2644      592531 :     IGF_UpdateInfo( hIGFEnc, igfGridIdx );
    2645             : 
    2646      592531 :     Copy32( pITFSpectrum + IGF_START_MN, hIGFEnc->spec_be_igf, sub( hIGFEnc->infoStopLine, IGF_START_MN ) );
    2647             : 
    2648      592531 :     scale_sig32( hIGFEnc->spec_be_igf, sub( hIGFEnc->infoStopLine, IGF_START_MN ), sub( exp_pITFSpectrum, s_max( exp_pITFSpectrum, hIGFEnc->spec_be_igf_e ) ) );
    2649      592531 :     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 ) ) );
    2650      592531 :     hIGFEnc->spec_be_igf_e = s_max( exp_pITFSpectrum, hIGFEnc->spec_be_igf_e );
    2651      592531 :     move16();
    2652             : 
    2653      592531 :     return;
    2654             : }
    2655             : 
    2656        3060 : ivas_error IGF_Reconfig(
    2657             :     IGF_ENC_INSTANCE_HANDLE *hIGFEnc, /* i/o: instance handle of IGF Encoder  */
    2658             :     const Word16 igf,                 /* i  : IGF on/off                      */
    2659             :     const Word16 reset,               /* i  : reset flag                      */
    2660             :     const Word32 brate,               /* i  : bitrate for configuration       */
    2661             :     const Word16 bwidth,              /* i  : signal bandwidth                */
    2662             :     const Word16 element_mode,        /* i  : IVAS element mode               */
    2663             :     const Word16 rf_mode              /* i  : flag to signal the RF mode      */
    2664             : )
    2665             : {
    2666             :     ivas_error error;
    2667             : 
    2668        3060 :     error = IVAS_ERR_OK;
    2669        3060 :     move32();
    2670             : 
    2671        3060 :     test();
    2672        3060 :     test();
    2673        3060 :     test();
    2674        3060 :     IF( igf && *hIGFEnc == NULL )
    2675             :     {
    2676         394 :         IF( ( *hIGFEnc = (IGF_ENC_INSTANCE_HANDLE) malloc( sizeof( IGF_ENC_INSTANCE ) ) ) == NULL )
    2677             :         {
    2678           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hIGFEnc\n" ) );
    2679             :         }
    2680         394 :         IGFEncSetMode_ivas_fx( *hIGFEnc, brate, bwidth, element_mode, rf_mode );
    2681             :     }
    2682        2666 :     ELSE IF( igf && reset )
    2683             :     {
    2684        1819 :         IGFEncSetMode_ivas_fx( *hIGFEnc, brate, bwidth, element_mode, rf_mode );
    2685             :     }
    2686         847 :     ELSE IF( !igf && *hIGFEnc != NULL )
    2687             :     {
    2688         567 :         free( *hIGFEnc );
    2689         567 :         *hIGFEnc = NULL;
    2690             :     }
    2691             : 
    2692        3060 :     return error;
    2693             : }

Generated by: LCOV version 1.14