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 @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 1287 1336 96.3 %
Date: 2025-05-03 01:55:50 Functions: 19 20 95.0 %

          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     7319266 : 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     7319266 :     IF( hBstr )
      63             :     {
      64     3702754 :         push_next_indice( hBstr, value, 1 );
      65             :     }
      66             : 
      67     7319266 :     ( *bitCount ) = add( ( *bitCount ), 1 );
      68     7319266 :     move16();
      69             : 
      70     7319266 :     return;
      71             : }
      72             : 
      73             : /*-------------------------------------------------------------------*
      74             :  * IGF_write_bits()
      75             :  *
      76             :  * write bits to stream
      77             :  *-------------------------------------------------------------------*/
      78     6157374 : 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    12314748 :     WHILE( bits-- )
      86             :     {
      87     6157374 :         IF( s_and( value, shl( 1, bits ) ) == 0 )
      88             :         {
      89     1946628 :             IGF_write_bit_fx( hBstr, bitCount, 0 );
      90             :         }
      91             :         ELSE
      92             :         {
      93     4210746 :             IGF_write_bit_fx( hBstr, bitCount, 1 );
      94             :         }
      95             :     }
      96             : 
      97     6157374 :     return;
      98             : }
      99             : 
     100             : 
     101             : /*-------------------------------------------------------------------*
     102             :  * IGF_getCrest_new()
     103             :  *
     104             :  * crest factor calculation
     105             :  *-------------------------------------------------------------------*/
     106             : 
     107             : /*! r: crest factor */
     108     5369392 : 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     5369392 :     x_eff = 0;
     124     5369392 :     x_max = 0;
     125     5369392 :     exp = 0;
     126     5369392 :     temp = 0;
     127     5369392 :     crest = 32767; /*1.0f in Q15*/
     128     5369392 :     move32();
     129     5369392 :     move16();
     130     5369392 :     move16();
     131     5369392 :     move16();
     132     5369392 :     move16();
     133             : 
     134   369390138 :     FOR( i = start; i < stop; i++ )
     135             :     {
     136   364020746 :         x = logSpec[i];
     137   364020746 :         move16();
     138   364020746 :         x_eff = L_add( x_eff, L_mult0( x, x ) );
     139             : 
     140   364020746 :         IF( GT_16( x, x_max ) )
     141             :         {
     142    16688687 :             x_max = x;
     143    16688687 :             move16();
     144             :         }
     145             :     }
     146             : 
     147     5369392 :     x_eff = BASOP_Util_Divide3216_Scale( x_eff, sub( stop, start ), &temp_e );
     148     5369392 :     temp_e = add( temp_e, 16 ); /*exp += 31 - 15 + 16(because x_eff is word32)*/
     149     5369392 :     x_eff = L_shr( x_eff, sub( 15, temp_e ) );
     150     5369392 :     temp_e = 15;
     151     5369392 :     move16();
     152     5369392 :     temp = Sqrt16( extract_l( x_eff ), &temp_e );
     153             : 
     154     5369392 :     test();
     155     5369392 :     IF( x_eff > 0 && x_max > 0 )
     156             :     {
     157     4771225 :         temp = BASOP_Util_Divide1616_Scale( x_max, temp, &exp );
     158     4771225 :         exp = add( exp, sub( 15, temp_e ) );
     159     4771225 :         IF( exp < 0 )
     160             :         {
     161           0 :             temp = shl( temp, exp );
     162           0 :             exp = 0;
     163           0 :             move16();
     164             :         }
     165     4771225 :         crest = s_max( shl_sat( 1, sub( 15, exp ) ), temp );
     166             :     }
     167     5369392 :     *crest_exp = exp;
     168     5369392 :     move16();
     169     5369392 :     return crest;
     170             : }
     171             : 
     172             : 
     173             : /*-------------------------------------------------------------------*
     174             :  * IGF_getSFM_new()
     175             :  *
     176             :  * calculates spectral flatness measurement
     177             :  *-------------------------------------------------------------------*/
     178             : 
     179             : /*! r: SFM value */
     180     5369392 : 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     5369392 :     num = 0;
     201     5369392 :     denom = ONE_IN_Q30;
     202     5369392 :     denom_e = 1;
     203     5369392 :     sfm = MAX16B; // Q15
     204     5369392 :     move16();
     205     5369392 :     move32();
     206     5369392 :     move16();
     207     5369392 :     move16();
     208             : 
     209   369390138 :     FOR( i = start; i < stop; i++ )
     210             :     {
     211   364020746 :         tmp = powerSpectrum[i];
     212   364020746 :         n = logSpec[i /*-start*/];
     213   364020746 :         move32();
     214   364020746 :         move16();
     215   364020746 :         num = add( num, n );
     216   364020746 :         denom = BASOP_Util_Add_Mant32Exp( tmp, e_ps[i], denom, denom_e, &denom_e );
     217             :     }
     218             : 
     219     5369392 :     numf = BASOP_Util_Divide1616_Scale( num, sub( stop, start ), &numf_e );
     220     5369392 :     denom = BASOP_Util_Divide3216_Scale( denom, sub( stop, start ), &tmp_e );
     221     5369392 :     denom_e = add( add( denom_e, tmp_e ), 1 ); /*denom_e+tmp_e-15 +16(because type of denom is word32)*/
     222             : 
     223     5369392 :     IF( denom != 0 )
     224             :     {
     225     5369392 :         tmp = BASOP_util_Pow2( L_add( numf, shl_sat( 1, sub( 14, numf_e ) ) ), add( 16, numf_e ), &tmp_e );
     226     5369392 :         sfm = BASOP_Util_Divide3232_Scale( tmp, denom, &sfm_e );
     227     5369392 :         sfm_e = add( sfm_e, sub( tmp_e, denom_e ) );
     228     5369392 :         sfm = shl_sat( extract_l( L_min( sfm, L_shl_sat( 1, sub( 15, sfm_e ) ) ) ), sfm_e );
     229             :     }
     230             : 
     231     5369392 :     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        1277 : static Word32 IGF_getTNR_fx(
     250             :     const Word32 *powerSpectrum, /* i  : energies               */
     251             :     const Word16 start,          /* i  : start subband index    */
     252             :     const Word16 stop,           /* i  : stop subband index     */
     253             :     const Word16 adap,           /* i  : SFB width adaptation   */
     254             :     Word16 e_ps,                 /*Stores exponent for powerSpectrum*/
     255             :     Word16 e_adap                /*Stores exponent for adap*/
     256             : )
     257             : {
     258             :     Word16 i;
     259             :     Word16 width;
     260             :     Word32 avg;
     261             :     Word32 tonal;
     262             :     Word16 tonal_e; /* holds exp for tonal*/
     263             :     Word32 noise;
     264             :     Word16 noise_e; /* holds exp for noise*/
     265             :     Word32 tonalToNoise;
     266             :     Word32 rootSpec[300];
     267             :     Word16 rootSpec_e[300]; /*rootSpec_e[i] holds exp for rootSpec[i]*/
     268             :     Word16 avg_e;           /* holds exp for avg*/
     269             :     Word16 tmp_e;
     270        1277 :     avg = 0;
     271        1277 :     tonal = 0;
     272        1277 :     noise = EPSILON_FX;
     273        1277 :     tonal_e = 0;
     274        1277 :     noise_e = 0;
     275        1277 :     avg_e = 0;
     276        1277 :     tmp_e = 0;
     277        1277 :     move32();
     278        1277 :     move32();
     279        1277 :     move32();
     280        1277 :     move16();
     281        1277 :     move16();
     282        1277 :     move16();
     283        1277 :     move16();
     284             : 
     285        1277 :     set32_fx( rootSpec, 0, 300 );
     286        1277 :     set16_fx( rootSpec_e, 0, 300 );
     287             : 
     288        1277 :     width = sub( stop, start );
     289       70677 :     FOR( i = start; i < stop; i++ )
     290             :     {
     291       69400 :         rootSpec_e[i - start] = e_ps;
     292       69400 :         move16();
     293       69400 :         rootSpec[i - start] = Sqrt32( powerSpectrum[i], &rootSpec_e[i - start] ); /*rootSpec[i - start] = sqrtf( powerSpectrum[i] );*/
     294       69400 :         move32();
     295       69400 :         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*/
     296             :     }
     297        1277 :     avg = BASOP_Util_Divide3216_Scale( avg, width, &tmp_e ); /*avg /= width;*/
     298        1277 :     avg_e = add( 16, sub( add( avg_e, tmp_e ), 15 ) );
     299             : 
     300       70677 :     FOR( i = start; i < stop; i++ )
     301             :     {
     302             :         Word16 normSpec_e;                                                                      /*stores resultant exponent for normSpec*/
     303       69400 :         Word16 normSpec = BASOP_Util_Divide3232_Scale( rootSpec[i - start], avg, &normSpec_e ); /*rootSpec[i - start] / avg;*/
     304       69400 :         normSpec_e = add( normSpec_e, sub( rootSpec_e[i - start], avg_e ) );
     305       69400 :         IF( GT_32( normSpec, L_add( L_shl( 1, sub( 15, normSpec_e ) ), L_shl( adap, sub( e_adap, normSpec_e ) ) ) ) )
     306             :         {
     307        4827 :             tonal = BASOP_Util_Add_Mant32Exp( tonal, tonal_e, rootSpec[i - start], rootSpec_e[i - start], &tonal_e ); /*tonal += rootSpec[i - start];*/
     308             :         }
     309       64573 :         ELSE IF( LT_32( normSpec, L_shl( 1, sub( 15, normSpec_e ) ) ) )
     310             :         {
     311       53862 :             noise = BASOP_Util_Add_Mant32Exp( noise, noise_e, rootSpec[i - start], rootSpec_e[i - start], &noise_e ); /*noise += rootSpec[i - start];*/
     312             :         }
     313             :     }
     314             : 
     315             :     /*tonalToNoise = 20.f * log10f( max( 1e-018f, tonal / noise ) )*/
     316        1277 :     IF( noise == 0 ) // To handle condition if denom = 0
     317             :     {
     318           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 );
     319             :     }
     320             :     ELSE
     321             :     {
     322        1277 :         Word16 temp = BASOP_Util_Divide3232_Scale( tonal, noise, &tmp_e ); /*tonal / noise*/
     323        1277 :         tmp_e = add( tmp_e, sub( tonal_e, noise_e ) );
     324        1277 :         IF( GE_16( temp, 1 ) )
     325             :         {
     326        1277 :             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*/
     327             :         }
     328             :         ELSE
     329             :         {
     330           0 :             tonalToNoise = -1509949440; /*-360.f Q22*/
     331           0 :             move32();
     332             :         }
     333             :     }
     334             : 
     335        1277 :     return tonalToNoise;
     336             : }
     337             : 
     338       11559 : static Word32 IGF_getTNR_ivas_fx(
     339             :     const Word32 *powerSpectrum, /* i  : energies               */
     340             :     const Word16 start,          /* i  : start subband index    */
     341             :     const Word16 stop,           /* i  : stop subband index     */
     342             :     const Word16 adap,           /* i  : SFB width adaptation   */
     343             :     Word16 *e_ps,                /*Stores exponent for powerSpectrum*/
     344             :     Word16 e_adap                /*Stores exponent for adap*/
     345             : )
     346             : {
     347             :     Word16 i;
     348             :     Word16 width;
     349             :     Word32 avg;
     350             :     Word32 tonal;
     351             :     Word16 tonal_e; /* holds exp for tonal*/
     352             :     Word32 noise;
     353             :     Word16 noise_e; /* holds exp for noise*/
     354             :     Word32 tonalToNoise;
     355             :     Word32 rootSpec[300];
     356             :     Word16 rootSpec_e[300]; /*rootSpec_e[i] holds exp for rootSpec[i]*/
     357             :     Word16 avg_e;           /* holds exp for avg*/
     358             :     Word16 tmp_e;
     359       11559 :     avg = 0;
     360       11559 :     tonal = 0;
     361       11559 :     noise = EPSILON_FX;
     362       11559 :     tonal_e = 0;
     363       11559 :     noise_e = 0;
     364       11559 :     avg_e = 0;
     365       11559 :     tmp_e = 0;
     366       11559 :     move32();
     367       11559 :     move32();
     368       11559 :     move32();
     369       11559 :     move16();
     370       11559 :     move16();
     371       11559 :     move16();
     372       11559 :     move16();
     373             : 
     374       11559 :     set32_fx( rootSpec, 0, 300 );
     375       11559 :     set16_fx( rootSpec_e, 0, 300 );
     376             : 
     377       11559 :     width = sub( stop, start );
     378      714849 :     FOR( i = start; i < stop; i++ )
     379             :     {
     380      703290 :         rootSpec_e[( i - start )] = e_ps[i];
     381      703290 :         move16();
     382      703290 :         rootSpec[( i - start )] = Sqrt32( powerSpectrum[i], &rootSpec_e[( i - start )] ); /*rootSpec[i - start] = sqrtf( powerSpectrum[i] );*/
     383      703290 :         move32();
     384      703290 :         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*/
     385             :     }
     386       11559 :     avg = BASOP_Util_Divide3216_Scale( avg, width, &tmp_e ); /*avg /= width;*/
     387       11559 :     avg_e = add( 16, sub( add( avg_e, tmp_e ), 15 ) );
     388             : 
     389      714849 :     FOR( i = start; i < stop; i++ )
     390             :     {
     391             :         Word16 normSpec_e;                                                                      /*stores resultant exponent for normSpec*/
     392      703290 :         Word16 normSpec = BASOP_Util_Divide3232_Scale( rootSpec[i - start], avg, &normSpec_e ); /*rootSpec[i - start] / avg;*/
     393      703290 :         normSpec_e = add( normSpec_e, sub( rootSpec_e[i - start], avg_e ) );
     394      703290 :         IF( GT_32( normSpec, L_add_sat( L_shl_sat( 1, sub( 15, normSpec_e ) ), L_shl_sat( adap, sub( e_adap, normSpec_e ) ) ) ) )
     395             :         {
     396       57120 :             tonal = BASOP_Util_Add_Mant32Exp( tonal, tonal_e, rootSpec[( i - start )], rootSpec_e[( i - start )], &tonal_e ); /*tonal += rootSpec[i - start];*/
     397             :         }
     398      646170 :         ELSE IF( LT_32( normSpec, L_shl_sat( 1, sub( 15, normSpec_e ) ) ) )
     399             :         {
     400      575538 :             noise = BASOP_Util_Add_Mant32Exp( noise, noise_e, rootSpec[( i - start )], rootSpec_e[( i - start )], &noise_e ); /*noise += rootSpec[i - start];*/
     401             :         }
     402             :     }
     403             : 
     404             :     /*tonalToNoise = 20.f * log10f( max( 1e-018f, tonal / noise ) )*/
     405       11559 :     IF( noise == 0 ) // To handle condition if denom = 0
     406             :     {
     407           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 );
     408             :     }
     409             :     ELSE
     410             :     {
     411       11559 :         Word16 temp = BASOP_Util_Divide3232_Scale( tonal, noise, &tmp_e ); /*tonal / noise*/
     412       11559 :         tmp_e = add( tmp_e, sub( tonal_e, noise_e ) );
     413       11559 :         IF( GE_16( temp, 1 ) )
     414             :         {
     415       11559 :             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*/
     416             :         }
     417             :         ELSE
     418             :         {
     419           0 :             tonalToNoise = -1509949440; /*-360.f Q22*/
     420           0 :             move32();
     421             :         }
     422             :     }
     423             : 
     424       11559 :     return tonalToNoise;
     425             : }
     426             : 
     427             : /*-------------------------------------------------------------------*
     428             :  * IGF_CalculateEnvelope()
     429             :  *
     430             :  * envelope estimation
     431             :  *-------------------------------------------------------------------*/
     432             : 
     433      491554 : static void IGF_CalculateEnvelope_ivas_fx(
     434             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i  : instance handle of IGF Encoder                  */
     435             :     Word32 *pMDCTSpectrum_fx,              /* i  : MDCT spectrum                                   */
     436             :     Word16 e_mdct,                         /* i  : exp of MDCT spectrum                  */
     437             :     Word32 *pPowerSpectrum_fx,             /* i  : MDCT^2 + MDST^2 spectrum, or estimate           */
     438             :     Word16 *e_ps,                          /* i  : exp of power spectrum                 */
     439             :     const Word16 igfGridIdx,               /* i  : IGF grid index                                  */
     440             :     const Word16 isTransient,              /* i  : flag indicating if transient is detected        */
     441             :     const Word16 last_core_acelp,          /* i  : indicator if last frame was ACELP core          */
     442             :     const Word16 element_mode,             /* i  : IVAS element_mode                               */
     443             :     const Word16 att                       /* i  : attenuation                                     */
     444             : )
     445             : {
     446             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
     447             :     H_IGF_GRID hGrid;
     448             :     Word16 *swb_offset;
     449             :     Word16 sfb;   /* this is the actual scalefactor band */
     450             :     Word16 width; /* this is width in subbands of the actual scalefactor band */
     451             :     Word16 tile_idx;
     452             :     Word16 strt_cpy;
     453             :     Word32 gain; /* the gain which has to be applied to the source tile to get the destination energy */
     454             :     Word16 sb;
     455             :     Word32 sfbEnergyR;
     456             :     Word32 sfbEnergyC; /* the energy of the destination region of the tile */
     457             :     Word32 sfbEnergyTileR;
     458             :     Word32 sfbEnergyTileC; /* the energy of the destination region of the tile */
     459             :     Word16 tmp, x, y;
     460             :     Word16 mean_x_fx, mean_y_fx;
     461             :     Word32 mean_xy_fx, mean_x2_fx;
     462             :     Word16 tmp_tb;
     463             :     Word16 tmp_sb;
     464             :     Word16 sfbCnt;
     465             :     Word16 sfm;
     466             :     Word16 crest;
     467             :     Word16 temp;
     468             :     Word16 mean_x_e, mean_y_e;   /*Stores exponent for mean_x and mean_y respectively*/
     469             :     Word16 mean_xy_e, mean_x2_e; /*stores exponent for mean_xy and mean_x2 respectively*/
     470             :     Word16 sfbEnergyTileR_e;     /*Exponent for sfbEnergyTileR*/
     471             :     Word16 sfbEnergyTileC_e;     /*Exponent for sfbEnergyTileC*/
     472             :     Word16 sfbEnergyC_e;         /*Exponent for sfbEnergyC*/
     473             :     Word16 sfbEnergyR_e;
     474             :     Word16 gain_e;    /*exponent for gain*/
     475             :     Word16 tmp_tb_e;  /*Stores exponent for tmp_tb*/
     476             :     Word16 tmp_sb_e;  /*stores exponent for tmp_sb*/
     477             :     Word16 crest_exp; /*Stores the exponent of the result(return value)*/
     478             :     Word16 sfm_exp;   /*stores exponent for ouput from sfm*/
     479             :     Word16 tmp_e;
     480             : 
     481      491554 :     hPrivateData = &hIGFEnc->igfData;
     482      491554 :     hGrid = &hPrivateData->igfInfo.grid[(Word16) igfGridIdx];
     483      491554 :     swb_offset = hGrid->swb_offset;
     484             : 
     485      491554 :     IF( element_mode > EVS_MONO )
     486             :     {
     487      491554 :         IF( igfGridIdx != IGF_GRID_LB_NORM )
     488             :         {
     489      160283 :             FOR( sfbCnt = 0; sfbCnt < sub( hGrid->sfbWrap[hGrid->nTiles], hGrid->sfbWrap[0] ); sfbCnt++ )
     490             :             {
     491             :                 /* reset filter */
     492      133905 :                 hPrivateData->prevSFM_FIR_SFB_TB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_FIR_TB_e[sfbCnt]
     493      133905 :                 hPrivateData->prevSFB_FIR_TB_e[sfbCnt] = 0;
     494      133905 :                 hPrivateData->prevSFM_IIR_SFB_TB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_IIR_TB_e[sfbCnt]
     495      133905 :                 hPrivateData->prevSFB_IIR_TB_e[sfbCnt] = 0;
     496      133905 :                 hPrivateData->prevSFM_FIR_SFB_SB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_FIR_SB_e[sfbCnt]
     497      133905 :                 hPrivateData->prevSFB_FIR_SB_e[sfbCnt] = 0;
     498      133905 :                 hPrivateData->prevSFM_IIR_SFB_SB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_IIR_SB_e[sfbCnt]
     499      133905 :                 hPrivateData->prevSFB_IIR_SB_e[sfbCnt] = 0;
     500      133905 :                 hPrivateData->prevDampingFactor_IIR_fx[sfbCnt] = MIN16B; /* -1.f in Q15*/
     501      133905 :                 hPrivateData->prevDampingFactor_IIR_e[sfbCnt] = 0;
     502      133905 :                 hPrivateData->dampingFactorSmoothing[sfbCnt] = 2;
     503      133905 :                 move16();
     504      133905 :                 move16();
     505      133905 :                 move16();
     506      133905 :                 move16();
     507      133905 :                 move16();
     508      133905 :                 move16();
     509      133905 :                 move16();
     510      133905 :                 move16();
     511      133905 :                 move16();
     512      133905 :                 move16();
     513      133905 :                 move16();
     514             :             }
     515             :         }
     516             :     }
     517             : 
     518      491554 :     IF( pPowerSpectrum_fx )
     519             :     {
     520   227013923 :         FOR( sb = hGrid->sbWrap[0]; sb < swb_offset[hGrid->sfbWrap[hGrid->nTiles]]; sb++ )
     521             :         {
     522   226582990 :             IF( LT_32( 1, pPowerSpectrum_fx[sb] ) )
     523             :             {
     524   226033185 :                 hPrivateData->logSpec[sb] = s_max( 0, (Word16) L_shr( L_add( BASOP_Util_Log2( pPowerSpectrum_fx[sb] ), L_shl( e_ps[sb], Q25 ) ), Q25 ) );
     525   226033185 :                 move16();
     526             :             }
     527             :             ELSE
     528             :             {
     529      549805 :                 hPrivateData->logSpec[sb] = 0;
     530      549805 :                 move16();
     531             :             }
     532             :         }
     533             :     }
     534             : 
     535     2204811 :     FOR( tile_idx = 0; tile_idx < hGrid->nTiles; tile_idx++ )
     536             :     {
     537     1713257 :         strt_cpy = hGrid->sbWrap[tile_idx];
     538     1713257 :         move16();
     539             : 
     540     4309396 :         FOR( sfb = hGrid->sfbWrap[tile_idx]; sfb < hGrid->sfbWrap[tile_idx + 1]; sfb++ )
     541             :         {
     542     2596139 :             width = sub( swb_offset[sfb + 1], swb_offset[sfb] );
     543     2596139 :             sfbEnergyTileR = EPSILON_FIX;
     544     2596139 :             sfbEnergyTileC = EPSILON_FIX;
     545     2596139 :             sfbEnergyC = EPSILON_FX;
     546     2596139 :             sfbEnergyTileR_e = 0;
     547     2596139 :             sfbEnergyTileC_e = 0;
     548     2596139 :             sfbEnergyC_e = 0;
     549     2596139 :             move16();
     550     2596139 :             move16();
     551     2596139 :             move16();
     552     2596139 :             move16();
     553     2596139 :             move16();
     554     2596139 :             move16();
     555             : 
     556     2596139 :             IF( pPowerSpectrum_fx != NULL )
     557             :             {
     558     2319785 :                 tmp = strt_cpy;
     559     2319785 :                 move16();
     560   134759629 :                 FOR( sb = swb_offset[sfb]; sb < swb_offset[sfb + 1]; sb++ )
     561             :                 {
     562   132439844 :                     Word16 shift = norm_l( pPowerSpectrum_fx[sb] );
     563   132439844 :                     sfbEnergyC = BASOP_Util_Add_Mant32Exp( sfbEnergyC, sfbEnergyC_e, L_shl( pPowerSpectrum_fx[sb], shift ), sub( e_ps[sb], shift ), &sfbEnergyC_e );
     564             :                     // 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 );
     565   132439844 :                     Word64 tmp64 = W_mult_32_32( pMDCTSpectrum_fx[strt_cpy], pMDCTSpectrum_fx[strt_cpy] );
     566   132439844 :                     Word16 tmp64_e = W_norm( tmp64 );
     567   132439844 :                     tmp64 = W_shl( tmp64, tmp64_e );
     568             : 
     569   132439844 :                     sfbEnergyTileR = BASOP_Util_Add_Mant32Exp( sfbEnergyTileR, sfbEnergyTileR_e, W_extract_h( tmp64 ), shl( e_mdct, 1 ) - tmp64_e, &sfbEnergyTileR_e );
     570   132439844 :                     shift = norm_l( pPowerSpectrum_fx[strt_cpy] );
     571   132439844 :                     sfbEnergyTileC = BASOP_Util_Add_Mant32Exp( sfbEnergyTileC, sfbEnergyTileC_e, L_shl( pPowerSpectrum_fx[strt_cpy], shift ), sub( e_ps[strt_cpy], shift ), &sfbEnergyTileC_e );
     572             : 
     573   132439844 :                     strt_cpy = add( strt_cpy, 1 );
     574             :                 }
     575             : 
     576     2319785 :                 sfbEnergyTileR = L_deposit_h( BASOP_Util_Divide3232_Scale( sfbEnergyTileR, width, &tmp_e ) );
     577     2319785 :                 sfbEnergyTileR_e = add( sub( sfbEnergyTileR_e, Q31 ), tmp_e );
     578             : 
     579     2319785 :                 IF( sfbEnergyTileR == 0 )
     580             :                 {
     581         901 :                     sfbEnergyTileR = EPSILON_FX;
     582         901 :                     sfbEnergyTileR_e = 0;
     583         901 :                     move32();
     584         901 :                     move16();
     585             :                 }
     586     2319785 :                 IF( sfbEnergyC == 0 )
     587             :                 {
     588         901 :                     sfbEnergyC = EPSILON_FX;
     589         901 :                     sfbEnergyC_e = 0;
     590         901 :                     move32();
     591         901 :                     move16();
     592             :                 }
     593     2319785 :                 IF( sfbEnergyTileC == 0 )
     594             :                 {
     595         901 :                     sfbEnergyTileC = EPSILON_FX;
     596         901 :                     sfbEnergyTileC_e = 0;
     597         901 :                     temp = BASOP_Util_Divide3232_Scale( sfbEnergyC, sfbEnergyTileC, &tmp_e );
     598         901 :                     tmp_e = add( tmp_e, sub( sfbEnergyC_e, sfbEnergyTileC_e ) );
     599         901 :                     move32();
     600         901 :                     move16();
     601             :                 }
     602             :                 ELSE
     603             :                 {
     604             :                     /*gain = (float) ( sfbEnergyTileR * ( sfbEnergyC / sfbEnergyTileC ) );*/
     605     2318884 :                     temp = BASOP_Util_Divide3232_Scale( sfbEnergyC, sfbEnergyTileC, &tmp_e );
     606     2318884 :                     tmp_e = add( tmp_e, sub( sfbEnergyC_e, sfbEnergyTileC_e ) );
     607             :                 }
     608             : 
     609     2319785 :                 gain = Mult_32_16( sfbEnergyTileR, temp ); // gain_e
     610     2319785 :                 gain_e = add( tmp_e, sfbEnergyTileR_e );
     611             : 
     612     2319785 :                 IF( element_mode > EVS_MONO )
     613             :                 {
     614     2319785 :                     test();
     615     2319785 :                     IF( !isTransient )
     616             :                     {
     617             :                         Word16 diffSFM;
     618     2313027 :                         Word16 shiftedSFM = 0;
     619     2313027 :                         Word16 shiftedSFM_e = 0;
     620     2313027 :                         move16();
     621     2313027 :                         move16();
     622             : 
     623             :                         // 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] );
     624     2313027 :                         sfm = IGF_getSFM_new_fx( pPowerSpectrum_fx, hPrivateData->logSpec, swb_offset[sfb], swb_offset[sfb + 1], e_ps );
     625     2313027 :                         sfm_exp = 0;
     626     2313027 :                         move16();
     627     2313027 :                         crest = IGF_getCrest_new_fx( hPrivateData->logSpec, swb_offset[sfb], swb_offset[sfb + 1], &crest_exp );
     628     2313027 :                         tmp_tb = BASOP_Util_Divide1616_Scale( sfm, crest, &tmp_e ); // tmp_tb_e
     629     2313027 :                         tmp_tb_e = add( tmp_e, sub( sfm_exp, crest_exp ) );
     630             : 
     631             :                         // tmp_sb = IGF_getSFM_new( pPowerSpectrum, hPrivateData->logSpec, tmp, strt_cpy ) / IGF_getCrest_new( hPrivateData->logSpec, tmp, strt_cpy );
     632     2313027 :                         sfm = IGF_getSFM_new_fx( pPowerSpectrum_fx, hPrivateData->logSpec, tmp, strt_cpy, e_ps );
     633     2313027 :                         crest = IGF_getCrest_new_fx( hPrivateData->logSpec, tmp, strt_cpy, &crest_exp );
     634     2313027 :                         tmp_sb = BASOP_Util_Divide1616_Scale( sfm, crest, &tmp_e ); // tmp_sb_e
     635     2313027 :                         tmp_sb_e = add( tmp_e, sub( sfm_exp, crest_exp ) );
     636             : 
     637     2313027 :                         test();
     638     2313027 :                         IF( last_core_acelp || hPrivateData->wasTransient )
     639             :                         {
     640       88413 :                             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] */
     641       88413 :                             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] */
     642       88413 :                             hPrivateData->prevSFB_FIR_TB_e[sfb] = hPrivateData->prevSFB_IIR_TB_e[sfb] = tmp_tb_e;
     643       88413 :                             hPrivateData->prevSFB_FIR_SB_e[sfb] = hPrivateData->prevSFB_IIR_SB_e[sfb] = tmp_sb_e;
     644       88413 :                             move16();
     645       88413 :                             move16();
     646       88413 :                             move16();
     647       88413 :                             move16();
     648       88413 :                             move16();
     649       88413 :                             move16();
     650       88413 :                             move16();
     651       88413 :                             move16();
     652             :                         }
     653             : 
     654     2313027 :                         tmp_tb = shr( tmp_tb, 2 ); /*taking 2 guard bits so it's exponent tmp_sb_e=+2*/
     655     2313027 :                         tmp_sb = shr( tmp_sb, 2 ); /*taking 2 guard bits so it's exponent tmp_tb_e=+2 */
     656     2313027 :                         tmp_sb_e = add( tmp_sb_e, 2 );
     657     2313027 :                         tmp_tb_e = add( tmp_tb_e, 2 );
     658             : 
     659             :                         Word16 tmp0, tmp2, tmp3, tmp4;
     660             :                         Word16 tmp0_e, tmp2_e, tmp3_e, tmp4_e;
     661     2313027 :                         tmp0 = shr( hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb], 2 );
     662     2313027 :                         tmp0_e = add( hPrivateData->prevSFB_FIR_TB_e[sfb], 2 );
     663     2313027 :                         move16();
     664     2313027 :                         tmp2 = shr( hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb], 2 );
     665     2313027 :                         tmp2_e = add( hPrivateData->prevSFB_IIR_TB_e[sfb], 2 );
     666     2313027 :                         move16();
     667     2313027 :                         tmp3 = shr( hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb], 2 );
     668     2313027 :                         tmp3_e = add( hPrivateData->prevSFB_FIR_SB_e[sfb], 2 );
     669     2313027 :                         move16();
     670     2313027 :                         tmp4 = shr( hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb], 2 );
     671     2313027 :                         tmp4_e = add( hPrivateData->prevSFB_IIR_SB_e[sfb], 2 );
     672     2313027 :                         move16();
     673             :                         Word16 x1, x2;
     674             : 
     675     2313027 :                         Word16 x1_e = BASOP_Util_Add_MantExp( tmp0, tmp0_e, tmp2, tmp2_e - 1, &x1 );
     676     2313027 :                         Word16 x2_e = BASOP_Util_Add_MantExp( tmp3, tmp3_e, tmp4, tmp4_e - 1, &x2 );
     677     2313027 :                         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]
     678     2313027 :                         move16();
     679     2313027 :                         BASOP_Util_Add_MantExp( 22118, 2, negate( hPrivateData->SFM_tb_fx[sfb] ), hPrivateData->sfb_tb_e[sfb], &tmp );
     680             : 
     681     2313027 :                         IF( tmp < 0 )
     682             :                         {
     683      266659 :                             hPrivateData->sfb_tb_e[sfb] = 2;
     684      266659 :                             hPrivateData->SFM_tb_fx[sfb] = 22118;
     685      266659 :                             move16();
     686      266659 :                             move16();
     687             :                         }
     688             : 
     689     2313027 :                         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]
     690     2313027 :                         move16();
     691     2313027 :                         BASOP_Util_Add_MantExp( 22118, 2, negate( hPrivateData->SFM_sb_fx[sfb] ), hPrivateData->sfb_sb_e[sfb], &tmp );
     692             : 
     693     2313027 :                         IF( tmp < 0 )
     694             :                         {
     695      145633 :                             hPrivateData->sfb_sb_e[sfb] = 2;
     696      145633 :                             hPrivateData->SFM_sb_fx[sfb] = 22118;
     697      145633 :                             move16();
     698      145633 :                             move16();
     699             :                         }
     700             : 
     701     2313027 :                         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 );
     702             : 
     703     2313027 :                         test();
     704     2313027 :                         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 */
     705       11577 :                         {
     706             :                             Word16 currDampingFactor, dampingFactor;
     707             :                             Word16 slope, threshold;
     708             :                             Word16 alpha;
     709             :                             Word16 slope_e, threshold_e, currDampingFactor_e, dampingFactor_e, alpha_e;
     710             : 
     711             :                             /* calculate spectral tilt to detect sudden drops (or increases) in energy in the current SFB */
     712             :                             // slope = IGF_getTilt_fx( pPowerSpectrum, swb_offset[sfb], swb_offset[sfb + 1], e_ps, &slope_e );
     713             : 
     714       11577 :                             x = 1;
     715       11577 :                             mean_x_fx = mean_y_fx = 0;
     716       11577 :                             Word32 mean_y_fx_tmp = 0;
     717       11577 :                             move32();
     718       11577 :                             mean_xy_fx = mean_x2_fx = 0;
     719       11577 :                             mean_x_e = 15;
     720       11577 :                             mean_xy_e = mean_y_e = mean_x2_e = 31;
     721       11577 :                             move16();
     722       11577 :                             move16();
     723       11577 :                             move16();
     724       11577 :                             move16();
     725       11577 :                             move16();
     726       11577 :                             move16();
     727       11577 :                             move16();
     728       11577 :                             move16();
     729       11577 :                             move16();
     730             : 
     731      715559 :                             FOR( sb = swb_offset[sfb]; sb < swb_offset[sfb + 1]; sb++ )
     732             :                             {
     733      703982 :                                 mean_x_fx = add( mean_x_fx, x );                   /*Q0*/
     734      703982 :                                 mean_x2_fx = L_add( mean_x2_fx, L_mult0( x, x ) ); /*Q0*/
     735             : 
     736             :                                 /*y = 20.f * log10f( max( 1.f, powerSpectrum[i] ) );*/
     737      703982 :                                 IF( LE_64( W_deposit32_l( pPowerSpectrum_fx[sb] ), W_shl( 1, ( sub( 31, e_ps[sb] ) ) ) ) )
     738             :                                 {
     739       86395 :                                     y = 0;
     740       86395 :                                     move16();
     741             :                                 }
     742             :                                 ELSE
     743             :                                 {
     744      617587 :                                     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*/
     745             :                                 }
     746      703982 :                                 mean_y_fx_tmp = L_mac0( mean_y_fx_tmp, y, 1 );     /*Q0*/
     747      703982 :                                 mean_xy_fx = L_add( mean_xy_fx, L_mult0( y, x ) ); /*Q0*/
     748             : 
     749      703982 :                                 x = add( x, 1 );
     750             :                             }
     751       11577 :                             mean_y_fx = BASOP_Util_Divide3216_Scale( mean_y_fx_tmp, width, &tmp_e ); /* mean_y_e*/
     752             : 
     753       11577 :                             mean_y_e = add( mean_y_e, sub( tmp_e, 15 ) );
     754       11577 :                             mean_x_fx = BASOP_Util_Divide1616_Scale( mean_x_fx, width, &tmp_e ); /* mean_x_e*/
     755       11577 :                             mean_x_e = add( mean_x_e, sub( tmp_e, 15 ) );
     756       11577 :                             mean_xy_fx = BASOP_Util_Divide3216_Scale( mean_xy_fx, width, &tmp_e ); /* mean_xy_e*/
     757       11577 :                             mean_xy_e = add( mean_xy_e, sub( tmp_e, 15 ) );
     758       11577 :                             mean_x2_fx = BASOP_Util_Divide3216_Scale( mean_x2_fx, width, &tmp_e ); /* mean_x2_e*/
     759       11577 :                             mean_x2_e = add( mean_x2_e, sub( tmp_e, 15 ) );
     760             : 
     761             :                             /*slope = ( mean_xy - mean_x * mean_y ) / ( mean_x2 - mean_x * mean_x );*/
     762       11577 :                             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 );
     763       11577 :                             slope_e = add( slope_e, sub( mean_xy_e, mean_x2_e ) );
     764             : 
     765             :                             /* determine whether strong tilt is due to a step in the spectrum (e.g. band limitation, no damping)
     766             :                                or a tonal component close the band border (apply damping) by calculating SFM for a shift of 1/2 SFB width*/
     767       11577 :                             threshold = BASOP_Util_Divide1616_Scale( 60, width, &threshold_e );
     768             : 
     769       11577 :                             test();
     770       11577 :                             IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( slope, add( slope_e, 16 ), negate( threshold ), add( threshold_e, 16 ) ), -1 ) )
     771             :                             {
     772         134 :                                 Word16 shift = shr( width, 1 );
     773         134 :                                 sfm = IGF_getSFM_new_fx( pPowerSpectrum_fx, hPrivateData->logSpec, swb_offset[sfb] - shift, swb_offset[sfb + 1] - shift, e_ps );
     774         134 :                                 crest = IGF_getCrest_new_fx( hPrivateData->logSpec, swb_offset[sfb] - shift, swb_offset[sfb + 1] - shift, &crest_exp );
     775         134 :                                 shiftedSFM = BASOP_Util_Divide1616_Scale( sfm, crest, &shiftedSFM_e );
     776             :                             }
     777       11443 :                             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 ) ) ) )
     778             :                             {
     779          54 :                                 Word16 shift = shr( width, 1 );
     780          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 );
     781          54 :                                 sfm = IGF_getSFM_new_fx( pPowerSpectrum_fx, hPrivateData->logSpec, swb_offset[sfb] + shift, swb_offset[sfb + 1] + shift, e_ps );
     782          54 :                                 crest = IGF_getCrest_new_fx( hPrivateData->logSpec, swb_offset[sfb] + shift, swb_offset[sfb + 1] + shift, &crest_exp );
     783          54 :                                 shiftedSFM = BASOP_Util_Divide1616_Scale( sfm, crest, &shiftedSFM_e );
     784             :                             }
     785       11577 :                             shiftedSFM_e = add( shiftedSFM_e, sub( sfm_exp, crest_exp ) );
     786             : 
     787       11577 :                             IF( GT_32( shiftedSFM, L_shl( 1311 /*0.04f Q15*/, negate( shiftedSFM_e ) ) ) )
     788             :                             {
     789          18 :                                 currDampingFactor = 32767; /*1.f Q15*/
     790          18 :                                 currDampingFactor_e = 0;
     791          18 :                                 move16();
     792          18 :                                 move16();
     793             :                             }
     794             :                             ELSE
     795             :                             {
     796             :                                 // alpha = min( 320.f / (float) swb_offset[sfb + 1], 1.25f );
     797       11559 :                                 temp = BASOP_Util_Divide1616_Scale( 320, swb_offset[sfb + 1], &alpha_e );
     798       11559 :                                 alpha = extract_l( L_min( temp, L_shl( 20480 /*1.25 Q14*/, sub( 1, alpha_e ) ) ) ); // alpha_e
     799             : 
     800             :                                 // currDampingFactor = expf( 1.25f * alpha * logf( hPrivateData->SFM_tb[sfb] / hPrivateData->SFM_sb[sfb] ) );
     801       11559 :                                 temp = BASOP_Util_Divide1616_Scale( hPrivateData->SFM_tb_fx[sfb], hPrivateData->SFM_sb_fx[sfb], &tmp_e ); // tmp_e
     802       11559 :                                 tmp_e = add( tmp_e, sub( hPrivateData->sfb_tb_e[sfb], hPrivateData->sfb_sb_e[sfb] ) );
     803       11559 :                                 Word16 temp1 = mult( 20480 /* 1.25f in Q14 */, alpha );
     804       11559 :                                 Word16 tmp1_e = add( 1, alpha_e );
     805       11559 :                                 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
     806             : 
     807             :                                 /* calculate tonal-to-noise ratio and reduce damping for low values*/
     808             : 
     809             :                                 Word32 tonalToNoise;
     810             :                                 Word16 adap;
     811             :                                 Word16 adap_e;         /*stores exp for adap*/
     812             :                                 Word16 tonalToNoise_e; /*stores exponent for tonalToNoise*/
     813       11559 :                                 tonalToNoise_e = 9;
     814       11559 :                                 move16();
     815       11559 :                                 adap = BASOP_Util_Divide1616_Scale( width, 40, &adap_e );
     816       11559 :                                 tonalToNoise = IGF_getTNR_ivas_fx( pPowerSpectrum_fx, swb_offset[sfb], swb_offset[sfb + 1], adap, e_ps, adap_e ); /*Q22*/
     817             : 
     818       11559 :                                 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 ) )
     819             :                                 {
     820             :                                     // currDampingFactor += 0.03f * ( ( 10 + adap ) - tonalToNoise );
     821        8317 :                                     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
     822        8317 :                                     currDampingFactor_e = BASOP_Util_Add_MantExp( currDampingFactor, currDampingFactor_e, extract_l( Mult_32_32( 983 /*0.03f Q15*/, temp2 ) ), tmp_e, &currDampingFactor );       // currDampingFactor_e
     823             :                                 }
     824             :                             }
     825             : 
     826       11577 :                             Word32 L_tmp = hPrivateData->prevDampingFactor_IIR_fx[sfb];
     827       11577 :                             move32();
     828       11577 :                             L_tmp = L_shl( L_tmp, hPrivateData->prevDampingFactor_IIR_e[sfb] );
     829       11577 :                             test();
     830       11577 :                             test();
     831       11577 :                             IF( last_core_acelp || hPrivateData->wasTransient || EQ_32( L_tmp, MIN16B ) )
     832             :                             {
     833        3906 :                                 tmp = BASOP_Util_Cmp_Mant32Exp( currDampingFactor, currDampingFactor_e, 3277 /* 0.1f in Q15 */, 0 );
     834        3906 :                                 IF( tmp >= 0 )
     835             :                                 {
     836        3865 :                                     hPrivateData->prevDampingFactor_IIR_fx[sfb] = currDampingFactor;
     837        3865 :                                     hPrivateData->prevDampingFactor_IIR_e[sfb] = currDampingFactor_e;
     838        3865 :                                     move16();
     839        3865 :                                     move16();
     840             :                                 }
     841             :                                 ELSE
     842             :                                 {
     843          41 :                                     hPrivateData->prevDampingFactor_IIR_fx[sfb] = 3277; /* 0.1 in Q15 */
     844          41 :                                     hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
     845          41 :                                     move16();
     846          41 :                                     move16();
     847             :                                 }
     848             :                             }
     849       11577 :                             IF( last_core_acelp )
     850             :                             {
     851         533 :                                 hPrivateData->dampingFactorSmoothing[sfb] = 2;
     852         533 :                                 move16();
     853             :                             }
     854             : 
     855       11577 :                             dampingFactor_e = BASOP_Util_Add_MantExp( currDampingFactor, currDampingFactor_e, hPrivateData->prevDampingFactor_IIR_fx[sfb], hPrivateData->prevDampingFactor_IIR_e[sfb], &dampingFactor ); // dampingFactor_e
     856       11577 :                             dampingFactor = shr( dampingFactor, 1 );
     857             : 
     858       11577 :                             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*/ );
     859             : 
     860       11577 :                             hPrivateData->prevDampingFactor_IIR_fx[sfb] = dampingFactor;
     861       11577 :                             move16();
     862       11577 :                             hPrivateData->prevDampingFactor_IIR_e[sfb] = dampingFactor_e;
     863       11577 :                             move16();
     864       11577 :                             if ( hPrivateData->dampingFactorSmoothing[sfb] > 0 )
     865             :                             {
     866        4972 :                                 hPrivateData->dampingFactorSmoothing[sfb] = sub( hPrivateData->dampingFactorSmoothing[sfb], 1 );
     867        4972 :                                 move16();
     868             :                             }
     869             :                         }
     870             :                         ELSE
     871             :                         {
     872     2301450 :                             hPrivateData->prevDampingFactor_IIR_fx[sfb] = MIN16B; // exponent : hPrivateData->prevDampingFactor_IIR_e[sfb]
     873     2301450 :                             hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
     874     2301450 :                             hPrivateData->dampingFactorSmoothing[sfb] = 1;
     875     2301450 :                             move16();
     876     2301450 :                             move16();
     877     2301450 :                             move16();
     878             :                         }
     879             : 
     880     2313027 :                         hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = tmp_tb; // hPrivateData->prevSFB_FIR_TB_e[sfb]
     881     2313027 :                         hPrivateData->prevSFB_FIR_TB_e[sfb] = tmp_tb_e;
     882     2313027 :                         hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = hPrivateData->SFM_tb_fx[sfb]; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     883     2313027 :                         hPrivateData->prevSFB_IIR_TB_e[sfb] = hPrivateData->sfb_tb_e[sfb];
     884     2313027 :                         hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = tmp_sb; // hPrivateData->prevSFB_FIR_SB_e[sfb]
     885     2313027 :                         hPrivateData->prevSFB_FIR_SB_e[sfb] = tmp_sb_e;
     886     2313027 :                         hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = hPrivateData->SFM_sb_fx[sfb]; // hPrivateData->prevSFB_IIR_SB_e[sfb]
     887     2313027 :                         hPrivateData->prevSFB_IIR_SB_e[sfb] = hPrivateData->sfb_sb_e[sfb];
     888     2313027 :                         move16();
     889     2313027 :                         move16();
     890     2313027 :                         move16();
     891     2313027 :                         move16();
     892     2313027 :                         move16();
     893     2313027 :                         move16();
     894     2313027 :                         move16();
     895     2313027 :                         move16();
     896             :                     }
     897             :                     ELSE
     898             :                     {
     899        6758 :                         hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = 0; // hPrivateData->prevSFB_FIR_TB_e[sfb]
     900        6758 :                         hPrivateData->prevSFB_FIR_TB_e[sfb] = 0;
     901        6758 :                         hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     902        6758 :                         hPrivateData->prevSFB_IIR_TB_e[sfb] = 0;
     903        6758 :                         hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     904        6758 :                         hPrivateData->prevSFB_FIR_SB_e[sfb] = 0;
     905        6758 :                         hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_SB_e[sfb]
     906        6758 :                         hPrivateData->prevSFB_IIR_SB_e[sfb] = 0;
     907        6758 :                         hPrivateData->prevDampingFactor_IIR_fx[sfb] = MIN16B; /* hPrivateData->prevDampingFactor_IIR_e[sfb]*/
     908        6758 :                         hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
     909        6758 :                         hPrivateData->dampingFactorSmoothing[sfb] = 2;
     910        6758 :                         move16();
     911        6758 :                         move16();
     912        6758 :                         move16();
     913        6758 :                         move16();
     914        6758 :                         move16();
     915        6758 :                         move16();
     916        6758 :                         move16();
     917        6758 :                         move16();
     918        6758 :                         move16();
     919        6758 :                         move16();
     920        6758 :                         move16();
     921             :                     }
     922             :                 }
     923             :             }
     924             :             ELSE
     925             :             {
     926      276354 :                 tmp_e = e_mdct;
     927      276354 :                 move16();
     928      276354 :                 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
     929      276354 :                 sfbEnergyR_e = add( sfbEnergyR_e, add( tmp_e, -15 ) );
     930      276354 :                 gain = L_shl( sfbEnergyR, 16 ); // gain_e
     931      276354 :                 move32();
     932      276354 :                 gain_e = sfbEnergyR_e;
     933             : 
     934      276354 :                 IF( element_mode > EVS_MONO )
     935             :                 {
     936      276354 :                     hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = 0; // hPrivateData->prevSFB_FIR_TB_e[sfb]
     937      276354 :                     hPrivateData->prevSFB_FIR_TB_e[sfb] = 0;
     938      276354 :                     hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     939      276354 :                     hPrivateData->prevSFB_IIR_TB_e[sfb] = 0;
     940      276354 :                     hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_TB_e[sfb]
     941      276354 :                     hPrivateData->prevSFB_FIR_SB_e[sfb] = 0;
     942      276354 :                     hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = 0; // hPrivateData->prevSFB_IIR_SB_e[sfb]
     943      276354 :                     hPrivateData->prevSFB_IIR_SB_e[sfb] = 0;
     944      276354 :                     hPrivateData->prevDampingFactor_IIR_fx[sfb] = MIN16B; /* hPrivateData->prevDampingFactor_IIR_e[sfb]*/
     945      276354 :                     hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
     946      276354 :                     hPrivateData->dampingFactorSmoothing[sfb] = 2;
     947      276354 :                     move16();
     948      276354 :                     move16();
     949      276354 :                     move16();
     950      276354 :                     move16();
     951      276354 :                     move16();
     952      276354 :                     move16();
     953      276354 :                     move16();
     954      276354 :                     move16();
     955      276354 :                     move16();
     956      276354 :                     move16();
     957      276354 :                     move16();
     958             :                 }
     959             :             }
     960             : 
     961     2596139 :             gain = Mult_32_16( gain, att ); // gain_e
     962     2596139 :             gain_e = add( gain_e, 0 );
     963             : 
     964             :             /*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)*/
     965     2596139 :             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*/
     966     2596139 :             test();
     967     2596139 :             test();
     968     2596139 :             IF( !isTransient && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) ) )
     969             :             {
     970      381100 :                 gain = L_add( gain, ONE_IN_Q21 ); /* better preservation of original HF band energy */
     971             :             }
     972     2596139 :             test();
     973     2596139 :             test();
     974     2596139 :             IF( !isTransient && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_64000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_64000_CPE ) ) )
     975             :             {
     976      487389 :                 gain = L_add( gain, ONE_IN_Q20 );
     977             :             }
     978     2596139 :             gain = L_min( gain, 91 << Q23 ); /* 13+15+63, see arithcode encode residual */
     979     2596139 :             gain = L_max( gain, 0 );
     980     2596139 :             gain_e = 8; /* stores exponent for gain_fx*/
     981     2596139 :             move16();
     982     2596139 :             hPrivateData->igfScfQuantized[sfb] = extract_l( L_shr( gain, Q23 ) ); /*Q0*/
     983     2596139 :             move16();
     984             :         }
     985             :     }
     986             : 
     987      491554 :     return;
     988             : }
     989             : 
     990             : /*-------------------------------------------------------------------*
     991             :  * IGF_CalculateStereoEnvelope_fx()
     992             :  *
     993             :  * envelope estimation
     994             : 
     995             :  *-------------------------------------------------------------------*/
     996      100434 : static void IGF_CalculateStereoEnvelope_fx(
     997             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i  : instance handle of IGF Encoder              */
     998             :     const Word32 *pMDCTSpectrum_fx,        /* i  : MDCT spectrum                               */
     999             :     Word16 pMDCTSpectrum_e,                /* i  : exponent for pMDCTSpectrum_fx               */
    1000             :     const Word32 *pMDCTSpectrumMsInv_fx,   /* i  : MDCT spectrum                               */
    1001             :     Word16 pMDCTSpectrumMsInv_e,           /* i  : expontent for pMDCTSpectrumMsInv_fx         */
    1002             :     const Word32 *pPowerSpectrum_fx,       /* i  : MDCT^2 + MDST^2 spectrum, or estimate       */
    1003             :     Word16 pPowerSpectrum_e,               /* i  : exponent for pPowerSpectrum_fx              */
    1004             :     const Word32 *pPowerSpectrumMsInv_fx,  /* i  : inverse power spectrum                      */
    1005             :     Word16 pPowerSpectrumMsInv_e,          /* i  : exponent for pPowerSpectrumMsInv_fx         */
    1006             :     const Word16 igfGridIdx,               /* i  : IGF grid index                                     */
    1007             :     const Word16 coreMsMask[N_MAX],        /* i  : line wise ms Mask                                  */
    1008             :     const Word16 isTransient,              /* i  : flag indicating if transient is detected           */
    1009             :     const Word16 last_core_acelp           /* i  : indicator if last frame was ACELP core             */
    1010             : )
    1011             : {
    1012             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
    1013             :     H_IGF_GRID hGrid;
    1014             :     Word16 *swb_offset;
    1015             :     Word16 sfb;   /* this is the actual scalefactor band */
    1016             :     Word16 width; /* this is width in subbands of the actual scalefactor band */
    1017             :     Word16 tile_idx;
    1018             :     Word16 strt_cpy;
    1019             :     Word32 gain_fx; /* the gain which has to be applied to the source tile to get the destination energy */
    1020             :     Word16 sb;
    1021             :     Word16 sfbEnergyR_fx;
    1022             :     Word32 sfbEnergyC_fx; /* the energy of the destination region of the tile */
    1023             :     Word32 sfbEnergyTileR_fx;
    1024             :     Word32 sfbEnergyTileC_fx; /* the energy of the destination region of the tile */
    1025             :     Word16 tmp, x, y;
    1026             :     Word16 mean_x_fx, mean_y_fx;
    1027             :     Word32 mean_xy_fx, mean_x2_fx;
    1028             :     Word16 slope_fx;
    1029             :     Word16 tmp_tb_fx;
    1030             :     Word16 tmp_sb_fx;
    1031             :     Word16 sfbCnt;
    1032             :     Word32 tileSrcSpec_fx[MAX_IGF_SFB_LEN];
    1033             :     Word16 sfm;
    1034             :     Word16 crest;
    1035             :     Word16 temp;
    1036             :     Word16 mean_x_e, mean_y_e;   /*Stores exponent for mean_x and mean_y respectively*/
    1037             :     Word16 mean_xy_e, mean_x2_e; /*stores exponent for mean_xy and mean_x2 respectively*/
    1038             :     Word16 tileSrcSpec_e;        /*Exponent for tileSrcSpec_fx*/
    1039             :     Word16 sfbEnergyTileR_e;     /*Exponent for sfbEnergyTileR_fx*/
    1040             :     Word16 sfbEnergyTileC_e;     /*Exponent for sfbEnergyTileC_fx*/
    1041             :     Word16 sfbEnergyC_e;         /*Exponent for sfbEnergyC_fx*/
    1042             :     Word16 gain_e;               /*exponent for gain_fx*/
    1043             :     Word16 crest_exp;            /*stores exponent for output from crest*/
    1044             :     Word16 sfm_exp;              /*stores exponent for ouput from sfm*/
    1045             :     Word16 tmp_tb_e;             /*Stores exponent for tmp_tb_fx*/
    1046             :     Word16 tmp_sb_e;             /*stores exponent for tmp_sb_fx*/
    1047             :     Word16 slope_e;              /*stores exponent for slope_fx*/
    1048             :     Word16 sfbEnergyR_e;         /*stores exponent for sfbEnergyR*/
    1049             :     Word16 tmp_e;
    1050             : 
    1051      100434 :     hPrivateData = &hIGFEnc->igfData;
    1052      100434 :     hGrid = &hPrivateData->igfInfo.grid[(Word16) igfGridIdx];
    1053      100434 :     swb_offset = hGrid->swb_offset;
    1054      100434 :     move16();
    1055             : 
    1056      100434 :     IF( igfGridIdx != IGF_GRID_LB_NORM )
    1057             :     {
    1058       23638 :         FOR( sfbCnt = 0; sfbCnt < sub( hGrid->sfbWrap[hGrid->nTiles], hGrid->sfbWrap[0] ); sfbCnt++ )
    1059             :         {
    1060             :             /* reset filter */
    1061       19420 :             hPrivateData->prevSFM_FIR_SFB_TB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_FIR_TB_e[sfbCnt]
    1062       19420 :             hPrivateData->prevSFM_IIR_SFB_TB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_IIR_TB_e[sfbCnt]
    1063       19420 :             hPrivateData->prevSFM_FIR_SFB_SB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_FIR_SB_e[sfbCnt]
    1064       19420 :             hPrivateData->prevSFM_IIR_SFB_SB_fx[sfbCnt] = 0; // exponent : hPrivateData->prevSFB_IIR_SB_e[sfbCnt]
    1065       19420 :             hPrivateData->prevSFB_FIR_TB_e[sfbCnt] = 0;
    1066       19420 :             hPrivateData->prevSFB_IIR_TB_e[sfbCnt] = 0;
    1067       19420 :             hPrivateData->prevSFB_FIR_SB_e[sfbCnt] = 0;
    1068       19420 :             hPrivateData->prevSFB_IIR_SB_e[sfbCnt] = 0;
    1069       19420 :             move16();
    1070       19420 :             move16();
    1071       19420 :             move16();
    1072       19420 :             move16();
    1073       19420 :             move16();
    1074       19420 :             move16();
    1075       19420 :             move16();
    1076       19420 :             move16();
    1077             :         }
    1078             :     }
    1079             : 
    1080      100434 :     IF( pPowerSpectrum_fx )
    1081             :     {
    1082    38228330 :         FOR( sb = hGrid->sbWrap[0]; sb < swb_offset[hGrid->sfbWrap[hGrid->nTiles]]; sb++ )
    1083             :         {
    1084             :             /*hPrivateData->logSpec[sb] = max( 0, (Word16) ( logf( max( FLT_MIN, pPowerSpectrum[sb] ) ) * INV_LOG_2 ) );*/
    1085    38146736 :             IF( LE_32( 1, pPowerSpectrum_fx[sb] ) )
    1086             :             {
    1087    38146049 :                 hPrivateData->logSpec[sb] = s_max( 0, (Word16) L_shr( L_add( BASOP_Util_Log2( pPowerSpectrum_fx[sb] ), L_shl( pPowerSpectrum_e, Q25 ) ), 25 ) );
    1088             :             }
    1089             :             ELSE
    1090             :             {
    1091         687 :                 hPrivateData->logSpec[sb] = 0; /*max(0,-126) is always 0*/
    1092             :             }
    1093    38146736 :             move16();
    1094             :         }
    1095             :     }
    1096             : 
    1097      395792 :     FOR( tile_idx = 0; tile_idx < hGrid->nTiles; tile_idx++ )
    1098             :     {
    1099      295358 :         strt_cpy = hGrid->sbWrap[tile_idx];
    1100      295358 :         move16();
    1101             : 
    1102      788394 :         FOR( sfb = hGrid->sfbWrap[tile_idx]; sfb < hGrid->sfbWrap[tile_idx + 1]; sfb++ )
    1103             :         {
    1104      493036 :             width = sub( swb_offset[sfb + 1], swb_offset[sfb] );
    1105      493036 :             sfbEnergyTileR_fx = EPSILON_FX;
    1106      493036 :             sfbEnergyTileC_fx = EPSILON_FX;
    1107      493036 :             sfbEnergyC_fx = EPSILON_FX;
    1108      493036 :             sfbEnergyTileR_e = 0;
    1109      493036 :             sfbEnergyTileC_e = 0;
    1110      493036 :             sfbEnergyC_e = 0;
    1111      493036 :             move16();
    1112      493036 :             move16();
    1113      493036 :             move16();
    1114      493036 :             move32();
    1115      493036 :             move32();
    1116      493036 :             move32();
    1117      493036 :             IF( pPowerSpectrum_fx )
    1118             :             {
    1119             :                 Word16 final_exp;
    1120             :                 Word16 norm_exp;
    1121             :                 Word32 scaled_value;
    1122      400264 :                 tmp = strt_cpy;
    1123      400264 :                 move16();
    1124             : 
    1125    21324536 :                 FOR( sb = swb_offset[sfb]; sb < swb_offset[sfb + 1]; sb++ )
    1126             :                 {
    1127    20924272 :                     IF( NE_16( coreMsMask[sb], coreMsMask[strt_cpy] ) )
    1128             :                     {
    1129    12408172 :                         sfbEnergyC_fx = BASOP_Util_Add_Mant32Exp( sfbEnergyC_fx, sfbEnergyC_e, pPowerSpectrum_fx[sb], pPowerSpectrum_e, &sfbEnergyC_e ); /*resultant exponent is stored in sfbEnergyC_e*/
    1130    12408172 :                         norm_exp = norm_l( pMDCTSpectrumMsInv_fx[strt_cpy] );
    1131    12408172 :                         final_exp = sub( pMDCTSpectrumMsInv_e, norm_exp );
    1132    12408172 :                         scaled_value = L_shl( pMDCTSpectrumMsInv_fx[strt_cpy], norm_exp );
    1133    12408172 :                         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*/
    1134    12408172 :                         sfbEnergyTileC_fx = BASOP_Util_Add_Mant32Exp( sfbEnergyTileC_fx, sfbEnergyTileC_e, pPowerSpectrumMsInv_fx[strt_cpy], pPowerSpectrumMsInv_e, &sfbEnergyTileC_e );       /*resultant exponent is stored in sfbEnergyTileC_e*/
    1135    12408172 :                         tileSrcSpec_fx[sub( strt_cpy, tmp )] = pPowerSpectrumMsInv_fx[strt_cpy];                                                                                               /*resultant exponent is stored in tileSrcSpec_e*/
    1136    12408172 :                         tileSrcSpec_e = pPowerSpectrumMsInv_e;
    1137             :                     }
    1138             :                     ELSE
    1139             :                     {
    1140     8516100 :                         sfbEnergyC_fx = BASOP_Util_Add_Mant32Exp( sfbEnergyC_fx, sfbEnergyC_e, pPowerSpectrum_fx[sb], pPowerSpectrum_e, &sfbEnergyC_e ); /*resultant exponent is stored in sfbEnergyC_e*/
    1141     8516100 :                         norm_exp = norm_l( pMDCTSpectrum_fx[strt_cpy] );
    1142     8516100 :                         final_exp = sub( pMDCTSpectrum_e, norm_exp );
    1143     8516100 :                         scaled_value = L_shl( pMDCTSpectrum_fx[strt_cpy], norm_exp );
    1144     8516100 :                         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*/
    1145     8516100 :                         sfbEnergyTileC_fx = BASOP_Util_Add_Mant32Exp( sfbEnergyTileC_fx, sfbEnergyTileC_e, pPowerSpectrum_fx[strt_cpy], pPowerSpectrum_e, &sfbEnergyTileC_e );                 /*resultant exponent is stored in sfbEnergyTileC_e*/
    1146     8516100 :                         tileSrcSpec_fx[sub( strt_cpy, tmp )] = pPowerSpectrum_fx[strt_cpy];                                                                                                    /*resultant exponent is stored in tileSrcSpec_e*/
    1147     8516100 :                         tileSrcSpec_e = pPowerSpectrum_e;
    1148             :                     }
    1149    20924272 :                     move32();
    1150    20924272 :                     move16();
    1151    20924272 :                     strt_cpy = add( strt_cpy, 1 );
    1152             :                 }
    1153             : 
    1154      400264 :                 sfbEnergyTileR_fx = BASOP_Util_Divide3216_Scale( sfbEnergyTileR_fx, width, &tmp_e );
    1155      400264 :                 sfbEnergyTileR_e = sub( add( sfbEnergyTileR_e, tmp_e ), 15 ); /*stores the resultant exponent for sfbEnergyTileR_fx*/
    1156             : 
    1157             :                 /*gain = (float) ( sfbEnergyTileR * ( sfbEnergyC / sfbEnergyTileC ) );*/
    1158      400264 :                 temp = BASOP_Util_Divide3232_Scale( sfbEnergyC_fx, L_add( sfbEnergyTileC_fx, EPSILON_FX ), &tmp_e );
    1159      400264 :                 gain_e = add( tmp_e, sub( sfbEnergyC_e, sfbEnergyTileC_e ) );
    1160      400264 :                 gain_fx = Mult_32_16( sfbEnergyTileR_fx, temp );
    1161      400264 :                 gain_e = add( 16, add( gain_e, sfbEnergyTileR_e ) ); /*stores the resultant exponent for gain_fx*/
    1162             : 
    1163      400264 :                 IF( !isTransient )
    1164             :                 {
    1165             :                     Word16 diffSFM_fx;
    1166             :                     Word16 shiftedSFM_fx;
    1167             :                     Word16 shiftedSFM_e; /*stores the resultant exponent for shiftedSFM_fx*/
    1168      400264 :                     shiftedSFM_fx = 0;
    1169      400264 :                     shiftedSFM_e = 0;
    1170      400264 :                     move16();
    1171      400264 :                     move16();
    1172             : 
    1173             :                     // tmp_tb = IGF_getSFM_ivas(pPowerSpectrum, swb_offset[sfb], swb_offset[sfb + 1]) / IGF_getCrest_ivas(pPowerSpectrum, swb_offset[sfb], swb_offset[sfb + 1]);
    1174      400264 :                     sfm = IGF_getSFM_ivas_fx( &sfm_exp, pPowerSpectrum_fx, &pPowerSpectrum_e, swb_offset[sfb], swb_offset[sfb + 1] );
    1175      400264 :                     crest = IGF_getCrest( &crest_exp, pPowerSpectrum_fx, pPowerSpectrum_e, swb_offset[sfb], swb_offset[sfb + 1] );
    1176      400264 :                     tmp_tb_fx = BASOP_Util_Divide1616_Scale( sfm, crest, &tmp_e );
    1177      400264 :                     tmp_tb_e = add( tmp_e, sub( sfm_exp, crest_exp ) ); /*stores the resultant exponent for tmp_tb_fx*/
    1178             : 
    1179             :                     // tmp_sb = IGF_getSFM_ivas(tileSrcSpec, 0, strt_cpy - tmp) / IGF_getCrest_ivas(tileSrcSpec, 0, strt_cpy - tmp);
    1180      400264 :                     sfm = IGF_getSFM_ivas_fx( &sfm_exp, tileSrcSpec_fx, &tileSrcSpec_e, 0, sub( strt_cpy, tmp ) );
    1181      400264 :                     crest = IGF_getCrest( &crest_exp, tileSrcSpec_fx, tileSrcSpec_e, 0, sub( strt_cpy, tmp ) );
    1182      400264 :                     tmp_sb_fx = BASOP_Util_Divide1616_Scale( sfm, crest, &tmp_e );
    1183      400264 :                     tmp_sb_e = add( tmp_e, sub( sfm_exp, crest_exp ) ); /*stores the resultant exponent for tmp_sb_fx*/
    1184             : 
    1185      400264 :                     IF( last_core_acelp || hPrivateData->wasTransient )
    1186             :                     {
    1187        7331 :                         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] */
    1188        7331 :                         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]*/
    1189        7331 :                         hPrivateData->prevSFB_FIR_TB_e[sfb] = tmp_sb_e;
    1190        7331 :                         hPrivateData->prevSFB_IIR_TB_e[sfb] = tmp_sb_e;
    1191        7331 :                         hPrivateData->prevSFB_FIR_SB_e[sfb] = tmp_sb_e;
    1192        7331 :                         hPrivateData->prevSFB_IIR_SB_e[sfb] = tmp_sb_e;
    1193        7331 :                         move16();
    1194        7331 :                         move16();
    1195        7331 :                         move16();
    1196        7331 :                         move16();
    1197        7331 :                         move16();
    1198        7331 :                         move16();
    1199        7331 :                         move16();
    1200        7331 :                         move16();
    1201             :                     }
    1202             :                     ELSE
    1203             :                     {
    1204             :                         /* Purpose of this block:
    1205             :                         -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
    1206             :                         -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
    1207             :                         */
    1208             :                         Word16 diff_sb_e; /*stores the difference of exponents for sb*/
    1209             :                         Word16 diff_tb_e; /*stores the difference of exponents for tb*/
    1210      392933 :                         diff_sb_e = sub( tmp_sb_e, hPrivateData->sfb_sb_e[sfb] );
    1211      392933 :                         diff_tb_e = sub( tmp_tb_e, hPrivateData->sfb_tb_e[sfb] );
    1212      392933 :                         IF( LE_16( tmp_tb_e, hPrivateData->sfb_tb_e[sfb] ) )
    1213             :                         {
    1214      384899 :                             tmp_tb_fx = shl( tmp_tb_fx, diff_tb_e );
    1215      384899 :                             tmp_tb_e = hPrivateData->sfb_tb_e[sfb];
    1216      384899 :                             move16();
    1217             :                         }
    1218             :                         ELSE
    1219             :                         {
    1220        8034 :                             hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = shr( hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb], diff_tb_e );
    1221        8034 :                             hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = shr( hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb], diff_tb_e );
    1222        8034 :                             move16();
    1223        8034 :                             move16();
    1224        8034 :                             hPrivateData->prevSFB_FIR_TB_e[sfb] = tmp_tb_e;
    1225        8034 :                             hPrivateData->prevSFB_IIR_TB_e[sfb] = tmp_tb_e;
    1226        8034 :                             move16();
    1227        8034 :                             move16();
    1228             :                         }
    1229      392933 :                         IF( LE_16( tmp_sb_e, hPrivateData->sfb_sb_e[sfb] ) )
    1230             :                         {
    1231      385146 :                             tmp_sb_fx = shl( tmp_sb_fx, diff_sb_e );
    1232      385146 :                             tmp_sb_e = hPrivateData->sfb_sb_e[sfb];
    1233      385146 :                             move16();
    1234             :                         }
    1235             :                         ELSE
    1236             :                         {
    1237        7787 :                             hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = shr( hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb], diff_sb_e );
    1238        7787 :                             hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = shr( hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb], diff_sb_e );
    1239        7787 :                             move16();
    1240        7787 :                             move16();
    1241        7787 :                             hPrivateData->prevSFB_FIR_SB_e[sfb] = tmp_sb_e;
    1242        7787 :                             hPrivateData->prevSFB_IIR_SB_e[sfb] = tmp_sb_e;
    1243        7787 :                             move16();
    1244        7787 :                             move16();
    1245             :                         }
    1246             :                     }
    1247      400264 :                     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 */
    1248      400264 :                     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 */
    1249      400264 :                     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 */
    1250      400264 :                     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]*/
    1251      400264 :                     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 */
    1252      400264 :                     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]*/
    1253      400264 :                     move16();
    1254      400264 :                     move16();
    1255      400264 :                     move16();
    1256      400264 :                     move16();
    1257      400264 :                     hPrivateData->sfb_sb_e[sfb] = 2;
    1258      400264 :                     hPrivateData->sfb_tb_e[sfb] = 2;
    1259      400264 :                     move16();
    1260      400264 :                     move16();
    1261             : 
    1262      400264 :                     diffSFM_fx = sub( hPrivateData->SFM_sb_fx[sfb], hPrivateData->SFM_tb_fx[sfb] ); /*Q13*/
    1263             : 
    1264      400264 :                     test();
    1265      400264 :                     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 */
    1266        1277 :                     {
    1267             :                         Word16 currDampingFactor_fx, dampingFactor_fx, alpha_fx;
    1268             :                         Word16 threshold_e, threshold_fx, alpha_e, currDampingFactor_e, dampingFactor_e;
    1269             :                         /* calculate spectral tilt to detect sudden drops (or increases) in energy in the current SFB */
    1270        1277 :                         x = 1;
    1271        1277 :                         mean_x_fx = mean_y_fx = 0;
    1272        1277 :                         mean_xy_fx = mean_x2_fx = 0;
    1273        1277 :                         mean_x_e = mean_y_e = 15;
    1274        1277 :                         mean_xy_e = mean_x2_e = 31;
    1275        1277 :                         move16();
    1276        1277 :                         move16();
    1277        1277 :                         move16();
    1278        1277 :                         move16();
    1279        1277 :                         move16();
    1280        1277 :                         move16();
    1281        1277 :                         move16();
    1282        1277 :                         move16();
    1283        1277 :                         move16();
    1284       70677 :                         FOR( sb = swb_offset[sfb]; sb < swb_offset[sfb + 1]; sb++ )
    1285             :                         {
    1286       69400 :                             mean_x_fx = add( mean_x_fx, x );                   /*Q0*/
    1287       69400 :                             mean_x2_fx = L_add( mean_x2_fx, L_mult0( x, x ) ); /*Q0*/
    1288             : 
    1289             :                             /*y = 20 * (Word16) log10f( max( 1e-018f, pPowerSpectrum[sb] ) );*/
    1290       69400 :                             IF( LT_32( pPowerSpectrum_fx[sb], 1 ) )
    1291             :                             {
    1292          20 :                                 y = 20 * ( -18 );
    1293          20 :                                 move16();
    1294             :                             }
    1295             :                             ELSE
    1296             :                             {
    1297       69380 :                                 y = imult1616( 20, extract_l( L_shr( Mult_32_16( ( L_add( BASOP_Util_Log2( pPowerSpectrum_fx[sb] ), L_shl( pPowerSpectrum_e, Q25 ) ) ), INV_Log2_10_Q15 ), 25 ) ) ); /*Q0*/
    1298             :                             }
    1299       69400 :                             mean_y_fx = add( mean_y_fx, y );                   /*Q0*/
    1300       69400 :                             mean_xy_fx = L_add( mean_xy_fx, L_mult0( y, x ) ); /*Q0*/
    1301             : 
    1302       69400 :                             x = add( x, 1 );
    1303             :                         }
    1304        1277 :                         mean_y_fx = BASOP_Util_Divide1616_Scale( mean_y_fx, width, &tmp_e ); /* resultant exp stores in mean_y_e*/
    1305        1277 :                         mean_y_e = add( mean_y_e, sub( tmp_e, 15 ) );
    1306        1277 :                         mean_x_fx = BASOP_Util_Divide1616_Scale( mean_x_fx, width, &tmp_e ); /* resultant exp stores in mean_x_e*/
    1307        1277 :                         mean_x_e = add( mean_x_e, sub( tmp_e, 15 ) );
    1308        1277 :                         mean_xy_fx = BASOP_Util_Divide3216_Scale( mean_xy_fx, width, &tmp_e ); /* resultant exp stores in mean_xy_e*/
    1309        1277 :                         mean_xy_e = add( mean_xy_e, sub( tmp_e, 15 ) );
    1310        1277 :                         mean_x2_fx = BASOP_Util_Divide3216_Scale( mean_x2_fx, width, &tmp_e ); /* resultant exp stores in mean_x2_e*/
    1311        1277 :                         mean_x2_e = add( mean_x2_e, sub( tmp_e, 15 ) );
    1312             : 
    1313             :                         /*slope = ( mean_xy - mean_x * mean_y ) / ( mean_x2 - mean_x * mean_x );*/
    1314        1277 :                         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 );
    1315        1277 :                         slope_e = add( slope_e, sub( mean_xy_e, mean_x2_e ) ); /*stores resultant exponent for slope_fx*/
    1316             : 
    1317             :                         /* determine whether strong tilt is due to a step in the spectrum (e.g. band limitation, no damping)
    1318             :                                    or a tonal component close the band border (apply damping) by calculating SFM for a shift of 1/2 SFB width*/
    1319        1277 :                         threshold_fx = BASOP_Util_Divide1616_Scale( 60, width, &threshold_e ); /*stores resultant exponent for threshold_fx*/
    1320        1277 :                         test();
    1321        1277 :                         IF( EQ_16( BASOP_Util_Cmp_Mant32Exp( slope_fx, add( slope_e, 16 ), negate( threshold_fx ), add( threshold_e, 16 ) ), -1 ) )
    1322             :                         {
    1323          29 :                             Word16 shift = shr( width, 1 );
    1324             :                             // 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 );
    1325          29 :                             sfm = IGF_getSFM_ivas_fx( &sfm_exp, pPowerSpectrum_fx, &pPowerSpectrum_e, sub( swb_offset[sfb], shift ), sub( swb_offset[sfb + 1], shift ) );
    1326          29 :                             crest = IGF_getCrest( &crest_exp, pPowerSpectrum_fx, pPowerSpectrum_e, sub( swb_offset[sfb], shift ), sub( swb_offset[sfb + 1], shift ) );
    1327          29 :                             shiftedSFM_fx = BASOP_Util_Divide1616_Scale( sfm, crest, &shiftedSFM_e );
    1328             :                         }
    1329        1248 :                         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 ) ) ) )
    1330             :                         {
    1331             :                             Word16 shift;
    1332           2 :                             shift = shr( width, 1 );
    1333             :                             // 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 );
    1334           2 :                             sfm = IGF_getSFM_ivas_fx( &sfm_exp, pPowerSpectrum_fx, &pPowerSpectrum_e, add( swb_offset[sfb], shift ), add( swb_offset[sfb + 1], shift ) );
    1335           2 :                             crest = IGF_getCrest( &crest_exp, pPowerSpectrum_fx, pPowerSpectrum_e, add( swb_offset[sfb], shift ), add( swb_offset[sfb + 1], shift ) );
    1336           2 :                             shiftedSFM_fx = BASOP_Util_Divide1616_Scale( sfm, crest, &shiftedSFM_e );
    1337             :                         }
    1338        1277 :                         IF( shiftedSFM_fx )
    1339             :                         {
    1340          31 :                             shiftedSFM_e = add( shiftedSFM_e, sub( sfm_exp, crest_exp ) ); /* stores resultant exponent for shiftedSFM_fx*/
    1341             :                         }
    1342             : 
    1343             :                         // alpha = min( 320.f / (float) swb_offset[sfb + 1], 1.25f );
    1344        1277 :                         temp = BASOP_Util_Divide1616_Scale( 320, swb_offset[sfb + 1], &alpha_e );
    1345        1277 :                         alpha_fx = extract_l( L_min( temp, L_shl( 20480 /*1.25 Q14*/, sub( 1, alpha_e ) ) ) ); /* exponent is alpha_e*/
    1346        1277 :                         temp = BASOP_Util_Divide1616_Scale( hPrivateData->SFM_tb_fx[sfb], hPrivateData->SFM_sb_fx[sfb], &tmp_e );
    1347        1277 :                         tmp_e = add( tmp_e, sub( hPrivateData->sfb_tb_e[sfb], hPrivateData->sfb_sb_e[sfb] ) ); /* stores resultant exponent for temp */
    1348             : 
    1349             :                         // currDampingFactor = expf( alpha * logf( hPrivateData->SFM_tb[sfb] / hPrivateData->SFM_sb[sfb] ) );
    1350        1277 :                         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*/
    1351             : 
    1352        1277 :                         IF( GT_32( shiftedSFM_fx, L_shl( 1311 /*0.04f Q15*/, negate( shiftedSFM_e ) ) ) )
    1353             :                         {
    1354           4 :                             currDampingFactor_fx = 32767; /*1.f Q15*/
    1355           4 :                             currDampingFactor_e = 0;
    1356           4 :                             move16();
    1357           4 :                             move16();
    1358             :                         }
    1359        1277 :                         test();
    1360        1277 :                         test();
    1361        1277 :                         IF( last_core_acelp || hPrivateData->wasTransient || EQ_32( hPrivateData->prevDampingFactor_IIR_fx[sfb], L_shl( -1, sub( 15, hPrivateData->prevDampingFactor_IIR_e[sfb] ) ) ) )
    1362             :                         {
    1363         606 :                             tmp = BASOP_Util_Cmp_Mant32Exp( currDampingFactor_fx, currDampingFactor_e, 3277, 0 );
    1364         606 :                             IF( tmp >= 0 )
    1365             :                             {
    1366         606 :                                 hPrivateData->prevDampingFactor_IIR_fx[sfb] = currDampingFactor_fx;
    1367         606 :                                 hPrivateData->prevDampingFactor_IIR_e[sfb] = currDampingFactor_e;
    1368             :                             }
    1369             :                             ELSE
    1370             :                             {
    1371           0 :                                 hPrivateData->prevDampingFactor_IIR_fx[sfb] = 3277; /* 0.1 in Q15 */
    1372           0 :                                 hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
    1373             :                             }
    1374             : 
    1375         606 :                             move16();
    1376         606 :                             move16();
    1377             :                         }
    1378             : 
    1379             :                         {
    1380             :                             Word32 tonalToNoise;
    1381             :                             Word16 adap;
    1382             :                             Word16 adap_e;         /*stores exp for adap*/
    1383             :                             Word16 tonalToNoise_e; /*stores exponent for tonalToNoise*/
    1384        1277 :                             tonalToNoise_e = 9;    /*stores exponent for tonalToNoise*/
    1385        1277 :                             move16();
    1386        1277 :                             adap = BASOP_Util_Divide1616_Scale( width, 30, &adap_e );
    1387        1277 :                             tonalToNoise = IGF_getTNR_fx( pPowerSpectrum_fx, swb_offset[sfb], swb_offset[sfb + 1], adap, pPowerSpectrum_e, adap_e ); /*Q22*/
    1388        1277 :                             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 ) )
    1389             :                             {
    1390             :                                 // currDampingFactor += 0.1f * ( ( 10 + adap ) - tonalToNoise );
    1391        1154 :                                 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*/
    1392        1154 :                                 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*/
    1393             :                             }
    1394             :                         }
    1395             : 
    1396        1277 :                         dampingFactor_e = BASOP_Util_Add_MantExp( currDampingFactor_fx, currDampingFactor_e, hPrivateData->prevDampingFactor_IIR_fx[sfb], hPrivateData->prevDampingFactor_IIR_e[sfb], &dampingFactor_fx );
    1397        1277 :                         dampingFactor_fx = shr( dampingFactor_fx, 1 ); /* resultant exponent is dampingFactor_e*/
    1398        1277 :                         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 ) )
    1399             :                         {
    1400             :                             // do nothing
    1401             :                         }
    1402             :                         ELSE
    1403             :                         {
    1404         513 :                             dampingFactor_fx = shr( hPrivateData->prevDampingFactor_IIR_fx[sfb], 1 ); /* resultant exponent is hPrivateData->prevDampingFactor_IIR_e[sfb]*/
    1405         513 :                             dampingFactor_e = hPrivateData->prevDampingFactor_IIR_e[sfb];
    1406         513 :                             move16();
    1407             :                         }
    1408        1277 :                         IF( dampingFactor_e < 0 )
    1409             :                         {
    1410          50 :                             dampingFactor_fx = shl( dampingFactor_fx, dampingFactor_e );
    1411          50 :                             dampingFactor_e = 0;
    1412          50 :                             move16();
    1413             :                         }
    1414        1277 :                         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*/ );
    1415             : 
    1416        1277 :                         hPrivateData->prevDampingFactor_IIR_fx[sfb] = dampingFactor_fx;
    1417        1277 :                         hPrivateData->prevDampingFactor_IIR_e[sfb] = dampingFactor_e;
    1418        1277 :                         move16();
    1419        1277 :                         move16();
    1420        1277 :                         if ( hPrivateData->dampingFactorSmoothing[sfb] > 0 )
    1421             :                         {
    1422         714 :                             hPrivateData->dampingFactorSmoothing[sfb] = sub( hPrivateData->dampingFactorSmoothing[sfb], 1 );
    1423         714 :                             move16();
    1424             :                         }
    1425             :                     }
    1426             :                     ELSE
    1427             :                     {
    1428      398987 :                         hPrivateData->prevDampingFactor_IIR_fx[sfb] = -( 1 << 15 ); /* resultant exp which is 0 stores in hPrivateData->prevDampingFactor_IIR_e[sfb]*/
    1429      398987 :                         hPrivateData->prevDampingFactor_IIR_e[sfb] = 0;
    1430      398987 :                         hPrivateData->dampingFactorSmoothing[sfb] = 1;
    1431      398987 :                         move16();
    1432      398987 :                         move16();
    1433      398987 :                         move16();
    1434             :                     }
    1435             : 
    1436      400264 :                     hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = tmp_tb_fx;
    1437      400264 :                     hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = hPrivateData->SFM_tb_fx[sfb];
    1438      400264 :                     hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = tmp_sb_fx;
    1439      400264 :                     hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = hPrivateData->SFM_sb_fx[sfb];
    1440      400264 :                     hPrivateData->prevSFB_FIR_TB_e[sfb] = hPrivateData->sfb_tb_e[sfb];
    1441      400264 :                     hPrivateData->prevSFB_IIR_TB_e[sfb] = hPrivateData->sfb_tb_e[sfb];
    1442      400264 :                     hPrivateData->prevSFB_FIR_SB_e[sfb] = hPrivateData->sfb_sb_e[sfb];
    1443      400264 :                     hPrivateData->prevSFB_IIR_SB_e[sfb] = hPrivateData->sfb_sb_e[sfb];
    1444      400264 :                     move16();
    1445      400264 :                     move16();
    1446      400264 :                     move16();
    1447      400264 :                     move16();
    1448      400264 :                     move16();
    1449      400264 :                     move16();
    1450      400264 :                     move16();
    1451      400264 :                     move16();
    1452             :                 }
    1453             :                 ELSE
    1454             :                 {
    1455           0 :                     hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = 0;
    1456           0 :                     hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = 0;
    1457           0 :                     hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = 0;
    1458           0 :                     hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = 0;
    1459           0 :                     hPrivateData->prevSFB_FIR_TB_e[sfb] = 0;
    1460           0 :                     hPrivateData->prevSFB_IIR_TB_e[sfb] = 0;
    1461           0 :                     hPrivateData->prevSFB_FIR_SB_e[sfb] = 0;
    1462           0 :                     hPrivateData->prevSFB_IIR_SB_e[sfb] = 0;
    1463             : 
    1464           0 :                     hPrivateData->dampingFactorSmoothing[sfb] = 2;
    1465           0 :                     move16();
    1466           0 :                     move16();
    1467           0 :                     move16();
    1468           0 :                     move16();
    1469           0 :                     move16();
    1470           0 :                     move16();
    1471           0 :                     move16();
    1472           0 :                     move16();
    1473           0 :                     move16();
    1474             :                 }
    1475             :             }
    1476             :             ELSE
    1477             :             {
    1478       92772 :                 tmp_e = pMDCTSpectrum_e;
    1479       92772 :                 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 ) );
    1480       92772 :                 sfbEnergyR_e = add( sfbEnergyR_e, add( tmp_e, -15 ) ); /* stores resultant exponent for sfbEnergyR_fx*/
    1481       92772 :                 gain_fx = sfbEnergyR_fx;                               /*resultant exponent stored in gain_e=sfbEnergyR_e*/
    1482       92772 :                 move32();
    1483       92772 :                 gain_e = add( sfbEnergyR_e, 16 ); /* because gain_fx is word32;only after adding 16 q of gain_fx is 15-sfbEnergyR_e*/
    1484             : 
    1485       92772 :                 hPrivateData->prevSFM_FIR_SFB_TB_fx[sfb] = 0;
    1486       92772 :                 hPrivateData->prevSFM_IIR_SFB_TB_fx[sfb] = 0;
    1487       92772 :                 hPrivateData->prevSFM_FIR_SFB_SB_fx[sfb] = 0;
    1488       92772 :                 hPrivateData->prevSFM_IIR_SFB_SB_fx[sfb] = 0;
    1489       92772 :                 hPrivateData->prevSFB_FIR_TB_e[sfb] = 0;
    1490       92772 :                 hPrivateData->prevSFB_IIR_TB_e[sfb] = 0;
    1491       92772 :                 hPrivateData->prevSFB_FIR_SB_e[sfb] = 0;
    1492       92772 :                 hPrivateData->prevSFB_IIR_SB_e[sfb] = 0;
    1493       92772 :                 move16();
    1494       92772 :                 move16();
    1495       92772 :                 move16();
    1496       92772 :                 move16();
    1497       92772 :                 move16();
    1498       92772 :                 move16();
    1499       92772 :                 move16();
    1500       92772 :                 move16();
    1501             :             }
    1502             :             /*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)*/
    1503      493036 :             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*/
    1504      493036 :             test();
    1505      493036 :             test();
    1506      493036 :             if ( !isTransient && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) ) )
    1507             :             {
    1508      109358 :                 gain_fx = L_add( gain_fx, ONE_IN_Q21 ); /* better preservation of original HF band energy */
    1509             :             }
    1510      493036 :             test();
    1511      493036 :             test();
    1512      493036 :             if ( !isTransient && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_64000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_64000_CPE ) ) )
    1513             :             {
    1514      179475 :                 gain_fx = L_add( gain_fx, ONE_IN_Q20 );
    1515             :             }
    1516      493036 :             gain_fx = L_min( gain_fx, 91 << Q23 ); /* 13+15+63, see arithcode encode residual */
    1517      493036 :             gain_fx = L_max( gain_fx, 0 );
    1518      493036 :             gain_e = 8; /* stores exponent for gain_fx*/
    1519      493036 :             move16();
    1520      493036 :             hPrivateData->igfScfQuantized[sfb] = (Word16) ( L_shr( gain_fx, 23 ) ); /*Q0*/
    1521      493036 :             move16();
    1522             :         }
    1523             :     }
    1524             : 
    1525      100434 :     return;
    1526             : }
    1527             : 
    1528             : 
    1529             : /*-------------------------------------------------------------------*
    1530             :  * IGF_WriteEnvelope()
    1531             :  *
    1532             :  * writes IGF SCF values
    1533             :  *-------------------------------------------------------------------*/
    1534             : /*! r: number of bits writen */
    1535     1161892 : static Word16 IGF_WriteEnvelope(
    1536             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i  : instance handle of IGF Encoder                                               */
    1537             :     BSTR_ENC_HANDLE hBstr,                 /* i/o: encoder bitstream handle                                                     */
    1538             :     Word16 *pBitOffset,                    /* i  : ptr to bitOffset counter                                                     */
    1539             :     const Word16 igfGridIdx,               /* i  : igf grid index see declaration of IGF_GRID_IDX for details                   */
    1540             :     const Word16 isIndepFlag,              /* i  : if 1 frame is independent, 0 = frame is coded with data from previous frame  */
    1541             :     Word16 *igfAllZero                     /* i  : returns 1 if all IGF scfs are zero, else 0                                   */
    1542             : )
    1543             : {
    1544             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
    1545             :     H_IGF_GRID hGrid;
    1546             :     Word16 sfb;
    1547             :     Word16 totBitCount;
    1548             :     Word16 startBitCount;
    1549             : 
    1550     1161892 :     startBitCount = *pBitOffset;
    1551     1161892 :     totBitCount = 0;
    1552     1161892 :     *igfAllZero = 1;
    1553     1161892 :     move16();
    1554     1161892 :     move16();
    1555     1161892 :     move16();
    1556     1161892 :     hPrivateData = &hIGFEnc->igfData;
    1557     1161892 :     hGrid = &hPrivateData->igfInfo.grid[igfGridIdx];
    1558             : 
    1559     1167916 :     FOR( sfb = hGrid->startSfb; sfb < hGrid->stopSfb; sfb++ )
    1560             :     {
    1561     1167065 :         IF( hPrivateData->igfScfQuantized[sfb] != 0 )
    1562             :         {
    1563     1161041 :             *igfAllZero = 0;
    1564     1161041 :             move16();
    1565     1161041 :             BREAK;
    1566             :         }
    1567             :     }
    1568             : 
    1569     1161892 :     IF( *igfAllZero != 0 )
    1570             :     {
    1571         851 :         IGF_write_bit_fx( hBstr, pBitOffset, 1 );
    1572             : 
    1573         851 :         if ( NULL == hBstr )
    1574             :         {
    1575         424 :             IGFSCFEncoderSaveContextState_fx( &hPrivateData->hIGFSCFArithEnc, igfGridIdx );
    1576             :         }
    1577             : 
    1578         851 :         IGFSCFEncoderReset_fx( &hPrivateData->hIGFSCFArithEnc );
    1579             : 
    1580         851 :         if ( NULL == hBstr )
    1581             :         {
    1582         424 :             IGFSCFEncoderRestoreContextState_fx( &hPrivateData->hIGFSCFArithEnc, igfGridIdx );
    1583             :         }
    1584             :     }
    1585             :     ELSE
    1586             :     {
    1587     1161041 :         IGF_write_bit_fx( hBstr, pBitOffset, 0 );
    1588             : 
    1589     1161041 :         if ( NULL == hBstr )
    1590             :         {
    1591      569480 :             IGFSCFEncoderSaveContextState_fx( &hPrivateData->hIGFSCFArithEnc, igfGridIdx );
    1592             :         }
    1593             : 
    1594     1161041 :         *pBitOffset = IGFSCFEncoderEncode_ivas_fx( &hPrivateData->hIGFSCFArithEnc, hBstr, *pBitOffset, &hPrivateData->igfScfQuantized[hGrid->startSfb], igfGridIdx, isIndepFlag );
    1595     1161041 :         move16();
    1596             : 
    1597     1161041 :         if ( NULL == hBstr )
    1598             :         {
    1599      569480 :             IGFSCFEncoderRestoreContextState_fx( &hPrivateData->hIGFSCFArithEnc, igfGridIdx );
    1600             :         }
    1601             :     }
    1602     1161892 :     totBitCount = sub( *pBitOffset, startBitCount );
    1603             : 
    1604     1161892 :     return totBitCount;
    1605             : }
    1606             : 
    1607             : 
    1608             : /*-------------------------------------------------------------------*
    1609             :  * IGF_Whitening()
    1610             :  *
    1611             :  * calculates the IGF whitening levels by SFM and crest
    1612             :  *-------------------------------------------------------------------*/
    1613      591988 : static void IGF_Whitening_ivas_fx(
    1614             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i  :     | instance handle of IGF Encoder               */
    1615             :     Word32 *powerSpectrum,                 /* i  : Q31 | MDCT/MDST power spectrum                     */
    1616             :     Word16 *powerSpectrum_e,               /* i  : Q31 | MDCT/MDST power spectrum                     */
    1617             :     const Word16 igfGridIdx,               /* i  : Q0  | IGF grid index                               */
    1618             :     const Word16 isTransient,              /* i  : Q0  | flag indicating if transient is detected     */
    1619             :     const Word16 last_core_acelp,          /* i  : Q0  | indicator if last frame was ACELP core       */
    1620             :     const Word16 isTNSActive,              /* i  : Q0  | indicator if TNS is active                   */
    1621             :     const Word16 sp_aud_decision0,         /* i  : Q0  | first stage classifier decision              */
    1622             :     const Word32 brate,                    /* i  : Q0  | bitrate                                      */
    1623             :     const Word16 element_mode              /* i  : Q0  | element mode                                 */
    1624             : )
    1625             : {
    1626             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
    1627             :     H_IGF_GRID hGrid;
    1628             :     Word16 p; /*Q0*/
    1629             :     Word16 tmp;
    1630             :     Word16 tmp_e;
    1631             :     Word16 SFM_src;
    1632             :     Word16 SFM_tar;
    1633             :     Word16 SFM_src_e;
    1634             :     Word16 SFM_tar_e;
    1635             :     Word16 num_Tiles;
    1636             :     Word16 SFM;
    1637             :     Word16 crest_e;
    1638             : 
    1639      591988 :     SFM = -ONE_IN_Q13; /*1.0f Q13*/
    1640      591988 :     move16();
    1641             : 
    1642      591988 :     hPrivateData = &hIGFEnc->igfData;
    1643      591988 :     hGrid = &hPrivateData->igfInfo.grid[(Word16) igfGridIdx];
    1644             : 
    1645      591988 :     IF( NE_16( igfGridIdx, IGF_GRID_LB_NORM ) )
    1646             :     {
    1647      133951 :         FOR( p = 0; p < hGrid->nTiles; p++ )
    1648             :         {
    1649             :             /* reset filter */
    1650      103355 :             hPrivateData->prevSFM_FIR[p] = L_deposit_l( 0 );
    1651      103355 :             hPrivateData->prevSFM_IIR[p] = 0;
    1652      103355 :             move16();
    1653             : 
    1654             :             /* preset values: */
    1655      103355 :             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1656      103355 :             move16();
    1657             :         }
    1658             :     }
    1659             : 
    1660     6511868 :     FOR( p = 0; p < IGF_MAX_TILES; p++ )
    1661             :     {
    1662             :         /* update prev data: */
    1663     5919880 :         hPrivateData->igfPrevWhiteningLevel[p] = hPrivateData->igfCurrWhiteningLevel[p];
    1664             :         /* preset values: */
    1665     5919880 :         hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1666     5919880 :         move16();
    1667     5919880 :         move16();
    1668             :     }
    1669             : 
    1670      591988 :     test();
    1671      591988 :     IF( !( isTransient || hPrivateData->wasTransient ) )
    1672             :     {
    1673      558622 :         IF( powerSpectrum )
    1674             :         {
    1675     2469628 :             FOR( p = 0; p < hGrid->nTiles; p++ )
    1676             :             {
    1677             :                 Word16 sb;
    1678             : 
    1679     1912300 :                 IF( isTNSActive )
    1680             :                 {
    1681    14588677 :                     FOR( sb = hGrid->tile[p]; sb < hGrid->tile[p + 1]; sb++ )
    1682             :                     {
    1683    14442106 :                         IF( LT_32( powerSpectrum[sb], 1 ) )
    1684       13392 :                         hPrivateData->logSpec[sb] = 0; /* max(0,FLT_MIN_EXP )*/
    1685             :                         ELSE
    1686    14428714 :                             hPrivateData->logSpec[sb] = extract_l( L_max( 0, L_shr( L_add( BASOP_Util_Log2( powerSpectrum[sb] ), L_shl( powerSpectrum_e[sb], Q25 ) ), Q25 ) ) );
    1687    14442106 :                         move16();
    1688             :                     }
    1689             :                 }
    1690             : 
    1691             :                 /* if current tile contains only a single SFB, reuse already computed SFM values */
    1692     1912300 :                 test();
    1693     1912300 :                 IF( element_mode > EVS_MONO && EQ_16( sub( hGrid->sfbWrap[p + 1], hGrid->sfbWrap[p] ), 1 ) )
    1694             :                 {
    1695     1169204 :                     tmp = hPrivateData->SFM_tb_fx[p];
    1696     1169204 :                     tmp_e = hPrivateData->sfb_tb_e[p];
    1697     1169204 :                     move16();
    1698     1169204 :                     move16();
    1699             :                 }
    1700             :                 ELSE
    1701             :                 {
    1702      743096 :                     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 );
    1703      743096 :                     tmp_e = sub( tmp_e, crest_e );
    1704             :                 }
    1705             : 
    1706     1912300 :                 test();
    1707     1912300 :                 IF( last_core_acelp || hPrivateData->wasTransient )
    1708             :                 {
    1709       49119 :                     hPrivateData->prevSFM_FIR[p] = L_shl( tmp, add( 1, tmp_e ) ); /*16-(15-exp)=>15Q16*/
    1710       49119 :                     hPrivateData->prevSFM_IIR[p] = shl( tmp, sub( tmp_e, 2 ) );   /*13-(15-exp)=>2Q13*/
    1711       49119 :                     move32();
    1712       49119 :                     move16();
    1713             :                 }
    1714             : 
    1715     1912300 :                 test();
    1716     1912300 :                 IF( LE_32( brate, IVAS_48k ) && EQ_16( element_mode, IVAS_CPE_MDCT ) )
    1717      446804 :                 {
    1718             :                     Word16 temp;
    1719      446804 :                     num_Tiles = 0;
    1720      446804 :                     SFM_src = 0;
    1721      446804 :                     SFM_tar = 0;
    1722      446804 :                     SFM_src_e = 0;
    1723      446804 :                     SFM_tar_e = 0;
    1724      446804 :                     move16();
    1725      446804 :                     move16();
    1726      446804 :                     move16();
    1727      446804 :                     move16();
    1728      446804 :                     move16();
    1729             : 
    1730      920808 :                     FOR( sb = hGrid->sfbWrap[p]; sb < hGrid->sfbWrap[p + 1]; sb++ )
    1731             :                     {
    1732      474004 :                         num_Tiles = add( num_Tiles, 1 );
    1733      474004 :                         SFM_src_e = BASOP_Util_Add_MantExp( hPrivateData->SFM_sb_fx[sb], hPrivateData->sfb_sb_e[sb], SFM_src, SFM_src_e, &SFM_src );
    1734      474004 :                         SFM_tar_e = BASOP_Util_Add_MantExp( hPrivateData->SFM_tb_fx[sb], hPrivateData->sfb_tb_e[sb], SFM_tar, SFM_tar_e, &SFM_tar );
    1735             :                     }
    1736             : 
    1737             :                     /* compute the average */
    1738      446804 :                     SFM_src = shr( BASOP_Util_Divide1616_Scale( SFM_src, num_Tiles, &temp ), 2 );
    1739      446804 :                     SFM_src_e = add( SFM_src_e, sub( temp, 13 ) ); /*temp-15+2:because right shifted by 2 which are the guard bits*/
    1740      446804 :                     SFM_tar = shr( BASOP_Util_Divide1616_Scale( SFM_tar, num_Tiles, &temp ), 2 );
    1741      446804 :                     SFM_tar_e = add( SFM_tar_e, sub( temp, 13 ) ); /*temp-15+2:because right shifted by 2 which are the guard bits*/
    1742             : 
    1743      446804 :                     IF( LT_16( SFM_tar_e, SFM_src_e ) )
    1744             :                     {
    1745       32803 :                         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*/
    1746       32803 :                         SFM_tar_e = SFM_src_e;
    1747             :                     }
    1748             :                     ELSE
    1749             :                     {
    1750      414001 :                         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*/
    1751      414001 :                         SFM_src_e = SFM_tar_e;
    1752             :                     }
    1753      446804 :                     move16();
    1754             : 
    1755      446804 :                     test();
    1756      446804 :                     test();
    1757      446804 :                     IF( ( p > 0 ) && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) ) )
    1758             :                     {
    1759      376870 :                         test();
    1760      376870 :                         if ( EQ_16( p, 1 ) && EQ_16( abs_s( sub( hPrivateData->igfCurrWhiteningLevel[0], hPrivateData->igfCurrWhiteningLevel[1] ) ), 2 ) ) /* OFF vs. STRONG */
    1761             :                         {
    1762        6710 :                             hPrivateData->igfCurrWhiteningLevel[0] = IGF_WHITENING_MID;
    1763        6710 :                             move16();
    1764             :                         }
    1765      376870 :                         hPrivateData->igfCurrWhiteningLevel[p] = hPrivateData->igfCurrWhiteningLevel[p - 1];
    1766      376870 :                         move16();
    1767             :                     }
    1768       69934 :                     ELSE IF( sp_aud_decision0 )
    1769             :                     {
    1770             :                         /* Music */
    1771             :                         /* whitening Off: when tonality of target is more than source or tonality of target is close to that of source */
    1772       33072 :                         test();
    1773       33072 :                         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*/ ) ) ) ) )
    1774             :                         {
    1775       27465 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1776       27465 :                             move16();
    1777             :                         }
    1778             : 
    1779             :                         /* whitening mid:  */
    1780       33072 :                         test();
    1781       33072 :                         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*/ ) ) )
    1782             :                         {
    1783        5045 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_MID;
    1784        5045 :                             move16();
    1785             :                         }
    1786             : 
    1787             :                         /* whitening strong */
    1788       33072 :                         if ( GT_32( SFM_tar, L_add( SFM_src, L_shl( 5, sub( 13, SFM_src_e ) ) /*1.25 with exponent SFM_src_e*/ ) ) )
    1789             :                         {
    1790         562 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_STRONG;
    1791         562 :                             move16();
    1792             :                         }
    1793             :                     }
    1794             :                     ELSE
    1795             :                     {
    1796             :                         /* Speech */
    1797             :                         /* whitening Off: when tonality of target is more than source or tonality of target is close to that of source */
    1798       36862 :                         test();
    1799       36862 :                         if ( LE_16( SFM_tar, SFM_src ) || LE_32( SFM_tar, L_add( SFM_src, L_shr( 3277 /*0.1 Q15*/, SFM_src_e ) ) ) )
    1800             :                         {
    1801       18431 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1802       18431 :                             move16();
    1803             :                         }
    1804             : 
    1805             :                         /* whitening mid:  */
    1806       36862 :                         test();
    1807       36862 :                         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*/ ) ) ) ) )
    1808             :                         {
    1809       11579 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_MID;
    1810       11579 :                             move16();
    1811             :                         }
    1812             : 
    1813             :                         /* whitening strong */
    1814       36862 :                         if ( GT_32( SFM_tar, L_add( SFM_src, L_shl( 1, sub( 14, SFM_src_e /*0.5 with exponent SFM_src_e*/ ) ) ) ) )
    1815             :                         {
    1816        6852 :                             hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_STRONG;
    1817        6852 :                             move16();
    1818             :                         }
    1819             :                     }
    1820             : 
    1821      446804 :                     SFM = shl( SFM_tar, sub( SFM_tar_e, 2 ) ); /*2Q13*/
    1822             :                 }
    1823             :                 ELSE
    1824             :                 {
    1825     1465496 :                     test();
    1826     1465496 :                     IF( element_mode > EVS_MONO && EQ_16( sub( hGrid->sfbWrap[p + 1], hGrid->sfbWrap[p] ), 1 ) )
    1827             :                     {
    1828      749600 :                         SFM = shl( tmp, sub( tmp_e, 2 ) ); /*2Q13*/
    1829             :                     }
    1830             :                     ELSE
    1831             :                     {
    1832             :                         Word32 temp;
    1833      715896 :                         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 ) ) );
    1834      715896 :                         SFM = extract_l( L_min( 22118 /*2.7*/, temp ) ); /*2Q13*/
    1835             :                     }
    1836     1465496 :                     hPrivateData->prevSFM_FIR[p] = L_shl( tmp, add( 1, tmp_e ) ); /*15Q16*/
    1837     1465496 :                     hPrivateData->prevSFM_IIR[p] = SFM;
    1838     1465496 :                     move32();
    1839     1465496 :                     move16();
    1840             : 
    1841     1465496 :                     IF( GT_16( SFM, hGrid->whiteningThreshold[1][p] ) )
    1842             :                     {
    1843      565281 :                         hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_STRONG;
    1844      565281 :                         move16();
    1845             :                     }
    1846      900215 :                     ELSE IF( GT_16( SFM, hGrid->whiteningThreshold[0][p] ) )
    1847             :                     {
    1848      577043 :                         hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_MID;
    1849      577043 :                         move16();
    1850             :                     }
    1851             :                     ELSE
    1852             :                     {
    1853      323172 :                         hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_OFF;
    1854      323172 :                         move16();
    1855             :                     }
    1856             :                 }
    1857             : 
    1858     1912300 :                 IF( element_mode > EVS_MONO )
    1859             :                 {
    1860     1912300 :                     IF( last_core_acelp ) /* reset */
    1861             :                     {
    1862       49119 :                         set16_fx( hPrivateData->igfPastSFM_fx[p], -ONE_IN_Q13, IGF_PAST_SFM_LEN );
    1863       49119 :                         hPrivateData->igfWhiteningHangoverCnt[p] = 2;
    1864       49119 :                         move16();
    1865             :                     }
    1866             :                     ELSE
    1867             :                     {
    1868     1863181 :                         test();
    1869     1863181 :                         test();
    1870     1863181 :                         test();
    1871     1863181 :                         test();
    1872             :                         /* check whether change in whitening level should be allowed or not (if SFM is inside a certain margin around thresholds) */
    1873     1863181 :                         IF( NE_16( hPrivateData->igfCurrWhiteningLevel[p], hPrivateData->igfPrevWhiteningLevel[p] ) &&
    1874             :                             ( ( GT_32( SFM, L_sub( hGrid->whiteningThreshold[0][p], 1229 /*0.15f Q13*/ ) ) && LT_32( SFM, L_add( hGrid->whiteningThreshold[0][p], 1229 ) ) ) ||
    1875             :                               ( GT_32( SFM, L_sub( hGrid->whiteningThreshold[1][p], 1229 ) ) && LT_32( SFM, L_add( hGrid->whiteningThreshold[1][p], 1229 ) ) ) ) )
    1876             :                         {
    1877             :                             Word16 mean_past_SFM;
    1878             :                             Word16 mean_past_SFM_e;
    1879             :                             Word16 countable;
    1880             :                             Word16 i;
    1881      258984 :                             mean_past_SFM = 0;
    1882      258984 :                             mean_past_SFM_e = 0;
    1883      258984 :                             countable = 0;
    1884      258984 :                             move16();
    1885      258984 :                             move16();
    1886      258984 :                             move16();
    1887             : 
    1888             :                             /* compute mean of last (available) SFM values */
    1889     1553904 :                             FOR( i = 0; i < IGF_PAST_SFM_LEN; i++ )
    1890             :                             {
    1891     1294920 :                                 IF( hPrivateData->igfPastSFM_fx[p][i] >= 0 )
    1892             :                                 {
    1893     1105462 :                                     mean_past_SFM_e = BASOP_Util_Add_MantExp( mean_past_SFM, mean_past_SFM_e, hPrivateData->igfPastSFM_fx[p][i], 2, &mean_past_SFM );
    1894     1105462 :                                     countable = add( countable, 1 );
    1895             :                                 }
    1896             :                             }
    1897      258984 :                             IF( countable )
    1898             :                             {
    1899             :                                 Word16 temp;
    1900      254056 :                                 mean_past_SFM = BASOP_Util_Divide1616_Scale( mean_past_SFM, countable, &temp );
    1901      254056 :                                 mean_past_SFM_e = add( mean_past_SFM_e, sub( temp, 15 ) );
    1902      254056 :                                 mean_past_SFM = shl( mean_past_SFM, sub( mean_past_SFM_e, 2 ) ); /*mean_past_SFM_e=2*/
    1903             :                                 /* deny change in whitening level for small deviations from mean SFM */
    1904      254056 :                                 if ( LT_16( abs_s( sub( SFM, mean_past_SFM ) ), 1638 ) /*0.2 in Q13*/ )
    1905             :                                 {
    1906      122719 :                                     hPrivateData->igfCurrWhiteningLevel[p] = hPrivateData->igfPrevWhiteningLevel[p];
    1907      122719 :                                     move16();
    1908             :                                 }
    1909             :                             }
    1910             :                         }
    1911             :                     }
    1912             : 
    1913     1912300 :                     hPrivateData->igfPastSFM_fx[p][hPrivateData->igfPastSFM_pos] = SFM; /*2Q13*/
    1914     1912300 :                     move16();
    1915             :                 }
    1916             :             }
    1917             : 
    1918      557328 :             SWITCH( hPrivateData->igfInfo.bitRateIndex )
    1919             :             {
    1920       84554 :                 case IGF_BITRATE_WB_9600:
    1921             :                 case IGF_BITRATE_RF_WB_13200:
    1922             :                 case IGF_BITRATE_WB_13200_CPE:
    1923             :                 case IGF_BITRATE_WB_16400_CPE:
    1924             :                 case IGF_BITRATE_RF_SWB_13200:
    1925             :                 case IGF_BITRATE_SWB_9600:
    1926             :                 case IGF_BITRATE_SWB_13200_CPE:
    1927             :                 case IGF_BITRATE_SWB_16400:
    1928             :                 case IGF_BITRATE_SWB_24400:
    1929             :                 case IGF_BITRATE_SWB_24400_CPE:
    1930             :                 case IGF_BITRATE_SWB_32000_CPE:
    1931             :                 case IGF_BITRATE_SWB_32000:
    1932             :                 case IGF_BITRATE_FB_16400:
    1933             :                 case IGF_BITRATE_FB_24400:
    1934             :                 case IGF_BITRATE_FB_24400_CPE:
    1935             :                 case IGF_BITRATE_FB_32000_CPE:
    1936             :                 case IGF_BITRATE_FB_32000:
    1937       84554 :                     hPrivateData->igfCurrWhiteningLevel[hGrid->nTiles - 1] = hPrivateData->igfCurrWhiteningLevel[hGrid->nTiles - 2];
    1938       84554 :                     move16();
    1939       84554 :                     break;
    1940      472774 :                 default:
    1941      472774 :                     break;
    1942             :             }
    1943             :         }
    1944             :         ELSE
    1945             :         {
    1946        5655 :             FOR( p = 0; p < hGrid->nTiles; p++ )
    1947             :             {
    1948        4361 :                 hPrivateData->igfCurrWhiteningLevel[p] = IGF_WHITENING_MID;
    1949        4361 :                 move16();
    1950             :             }
    1951             :         }
    1952             :     }
    1953             :     ELSE
    1954             :     {
    1955             :         /* reset filter */
    1956      367026 :         FOR( p = 0; p < IGF_MAX_TILES; p++ )
    1957             :         {
    1958      333660 :             hPrivateData->prevSFM_FIR[p] = L_deposit_l( 0 );
    1959      333660 :             hPrivateData->prevSFM_IIR[p] = 0;
    1960      333660 :             move32();
    1961      333660 :             move16();
    1962             :         }
    1963             :     }
    1964             : 
    1965      591988 :     IF( element_mode > EVS_MONO )
    1966             :     {
    1967      591988 :         IF( EQ_16( SFM, -ONE_IN_Q13 /*1.0f 2Q13*/ ) ) /* reset */
    1968             :         {
    1969      130975 :             FOR( p = 0; p < hGrid->nTiles; p++ )
    1970             :             {
    1971       96315 :                 set16_fx( hPrivateData->igfPastSFM_fx[p], -ONE_IN_Q13, IGF_PAST_SFM_LEN );
    1972       96315 :                 hPrivateData->igfWhiteningHangoverCnt[p] = 2;
    1973       96315 :                 move16();
    1974             :             }
    1975             :         }
    1976             : 
    1977             :         /* vibrato handling */
    1978     1799104 :         FOR( p = 0; p < hGrid->nTiles; p = p + 2 )
    1979             :         {
    1980     1207116 :             test();
    1981     1207116 :             test();
    1982     1207116 :             test();
    1983     1207116 :             IF( ( hPrivateData->igfPrevWhiteningLevel[p] == IGF_WHITENING_OFF && hPrivateData->igfCurrWhiteningLevel[p] != IGF_WHITENING_OFF ) ||
    1984             :                 ( hPrivateData->igfPrevWhiteningLevel[p + 1] == IGF_WHITENING_OFF && hPrivateData->igfCurrWhiteningLevel[p + 1] != IGF_WHITENING_OFF ) )
    1985             :             {
    1986             :                 Word16 i;
    1987             :                 Word16 pastSfm_a[4], pastSfm_b[4];
    1988             :                 Word16 pastSfmDiffSum_a, pastSfmDiffSum_b;
    1989             : 
    1990      909665 :                 FOR( i = 0; i < 4; i++ )
    1991             :                 {
    1992      727732 :                     pastSfm_a[i] = hPrivateData->igfPastSFM_fx[p][add( hPrivateData->igfPastSFM_pos, sub( 4, i ) ) % IGF_PAST_SFM_LEN];
    1993      727732 :                     pastSfm_b[i] = hPrivateData->igfPastSFM_fx[p + 1][add( hPrivateData->igfPastSFM_pos, sub( 4, i ) ) % IGF_PAST_SFM_LEN];
    1994      727732 :                     move16();
    1995      727732 :                     move16();
    1996             :                 }
    1997      181933 :                 pastSfmDiffSum_a = pastSfmDiffSum_b = 0;
    1998      181933 :                 move16();
    1999      181933 :                 move16();
    2000      516209 :                 FOR( i = 0; i < 3; i++ )
    2001             :                 {
    2002      425717 :                     IF( NE_16( pastSfm_a[i + 1], -ONE_IN_Q13 ) )
    2003             :                     {
    2004      334276 :                         pastSfmDiffSum_a = add( pastSfmDiffSum_a, sub( pastSfm_a[i], pastSfm_a[i + 1] ) );
    2005      334276 :                         pastSfmDiffSum_b = add( pastSfmDiffSum_b, sub( pastSfm_b[i], pastSfm_b[i + 1] ) );
    2006             :                     }
    2007             :                     ELSE
    2008             :                     {
    2009       91441 :                         break;
    2010             :                     }
    2011             :                 }
    2012      181933 :                 test();
    2013      181933 :                 test();
    2014      181933 :                 test();
    2015      181933 :                 test();
    2016             :                 /* if tonality oscillates between two tiles, turn whitening off in both */
    2017      181933 :                 IF( ( ( pastSfmDiffSum_a > 0 && pastSfmDiffSum_b < 0 ) ||
    2018             :                       ( pastSfmDiffSum_a < 0 && pastSfmDiffSum_b > 0 ) ) &&
    2019             :                     ( GT_32( L_abs( L_sub( pastSfmDiffSum_a, pastSfmDiffSum_b ) ), ONE_IN_Q12 /* 0.5 in Q13 */ ) ) )
    2020             :                 {
    2021        9051 :                     hPrivateData->igfCurrWhiteningLevel[p] = hPrivateData->igfCurrWhiteningLevel[p + 1] = IGF_WHITENING_OFF;
    2022        9051 :                     move16();
    2023        9051 :                     move16();
    2024             :                 }
    2025             :             }
    2026             :         }
    2027             : 
    2028             :         /* hangover */
    2029     2600603 :         FOR( p = 0; p < hGrid->nTiles; p++ )
    2030             :         {
    2031     2008615 :             IF( NE_16( hPrivateData->igfCurrWhiteningLevel[p], hPrivateData->igfPrevWhiteningLevel[p] ) )
    2032             :             {
    2033      410732 :                 hPrivateData->igfWhiteningHangoverCnt[p] = add( hPrivateData->igfWhiteningHangoverCnt[p], 1 );
    2034      410732 :                 IF( EQ_16( hPrivateData->igfWhiteningHangoverCnt[p], 3 ) )
    2035             :                 {
    2036      114409 :                     hPrivateData->igfWhiteningHangoverCnt[p] = 0;
    2037             :                 }
    2038             :                 ELSE
    2039             :                 {
    2040      296323 :                     hPrivateData->igfCurrWhiteningLevel[p] = hPrivateData->igfPrevWhiteningLevel[p];
    2041             :                 }
    2042      410732 :                 move16();
    2043      410732 :                 move16();
    2044             :             }
    2045             :             ELSE
    2046             :             {
    2047     1597883 :                 hPrivateData->igfWhiteningHangoverCnt[p] = 0;
    2048     1597883 :                 move16();
    2049             :             }
    2050             :         }
    2051             : 
    2052      591988 :         hPrivateData->igfPastSFM_pos = add( hPrivateData->igfPastSFM_pos, 1 ) % IGF_PAST_SFM_LEN;
    2053      591988 :         move16();
    2054             :     }
    2055             : 
    2056      591988 :     hPrivateData->wasTransient = isTransient;
    2057      591988 :     move16();
    2058             : 
    2059      591988 :     return;
    2060             : }
    2061             : 
    2062             : 
    2063             : /*-------------------------------------------------------------------*
    2064             :  * IGF_WriteWhiteningTile_fx()
    2065             :  *
    2066             :  * write whitening levels into bitstream
    2067             :  *-------------------------------------------------------------------*/
    2068             : 
    2069             : /*! r: number of bits written */
    2070     2452304 : static Word16 IGF_WriteWhiteningTile_fx(                        /**< out: Q0 | number of bits written     */
    2071             :                                          BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle       */
    2072             :                                          Word16 *pBitOffset,    /**< in:     | ptr to bitOffset counter   */
    2073             :                                          Word16 whiteningLevel  /**< in: Q0  | whitening levels to write  */
    2074             : )
    2075             : {
    2076             :     Word16 totBitCount;
    2077             :     Word16 startBitCount;
    2078             : 
    2079     2452304 :     totBitCount = 0;
    2080     2452304 :     startBitCount = *pBitOffset;
    2081     2452304 :     move16();
    2082     2452304 :     move16();
    2083             : 
    2084     2452304 :     IF( EQ_32( whiteningLevel, IGF_WHITENING_MID ) )
    2085             :     {
    2086      925002 :         IGF_write_bits( hBstr, pBitOffset, 0, 1 );
    2087             :     }
    2088             :     ELSE
    2089             :     {
    2090     1527302 :         IGF_write_bits( hBstr, pBitOffset, 1, 1 );
    2091     1527302 :         IF( whiteningLevel == IGF_WHITENING_OFF )
    2092             :         {
    2093      763890 :             IGF_write_bits( hBstr, pBitOffset, 0, 1 );
    2094             :         }
    2095             :         ELSE
    2096             :         {
    2097      763412 :             IGF_write_bits( hBstr, pBitOffset, 1, 1 );
    2098             :         }
    2099             :     }
    2100     2452304 :     totBitCount = sub( *pBitOffset, startBitCount );
    2101             : 
    2102     2452304 :     return totBitCount;
    2103             : }
    2104             : 
    2105             : 
    2106             : /*-------------------------------------------------------------------*
    2107             :  * IGF_WriteWhiteningLevels_fx()
    2108             :  *
    2109             :  * writes the whitening levels
    2110             :  *-------------------------------------------------------------------*/
    2111             : 
    2112     1161892 : static Word16 IGF_WriteWhiteningLevels_fx(                                          /**< out: Q0 | total number of bits written                                                 */
    2113             :                                            const IGF_ENC_INSTANCE_HANDLE hInstance, /**< in:     | instance handle of IGF encoder                                               */
    2114             :                                            BSTR_ENC_HANDLE hBstr,                   /* i/o: encoder bitstream handle       */
    2115             :                                            Word16 *pBitOffset,                      /**< in:     | ptr to bitOffset counter                                                     */
    2116             :                                            const Word16 igfGridIdx,                 /**< in: Q0  | igf grid index see declaration of IGF_GRID_IDX for details                   */
    2117             :                                            const Word16 isIndepFlag                 /**< in: Q0  | if 1 frame is independent, 0 = frame is coded with data from previous frame  */
    2118             : )
    2119             : {
    2120             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
    2121             :     H_IGF_GRID hGrid;
    2122             :     Word16 p;
    2123             :     Word16 nTiles;
    2124             :     Word16 isSame;
    2125             :     Word32 tmp32;
    2126             :     Word16 totBitCount;
    2127             :     Word16 startBitCount;
    2128             : 
    2129     1161892 :     totBitCount = 0;
    2130     1161892 :     move16();
    2131     1161892 :     isSame = 1;
    2132     1161892 :     move16();
    2133     1161892 :     startBitCount = *pBitOffset;
    2134     1161892 :     move16();
    2135     1161892 :     hPrivateData = &hInstance->igfData;
    2136     1161892 :     hGrid = &hPrivateData->igfInfo.grid[igfGridIdx];
    2137     1161892 :     nTiles = hGrid->nTiles;
    2138     1161892 :     move16();
    2139             : 
    2140     1161892 :     IF( isIndepFlag )
    2141             :     {
    2142     1150850 :         isSame = 0;
    2143     1150850 :         move16();
    2144             :     }
    2145             :     ELSE
    2146             :     {
    2147       11042 :         p = 0;
    2148       11042 :         move16();
    2149       11042 :         tmp32 = 0;
    2150       11042 :         move32();
    2151       11042 :         test();
    2152       36294 :         WHILE( ( LT_16( p, nTiles ) ) && ( tmp32 == 0 ) )
    2153             :         {
    2154       25252 :             test();
    2155       25252 :             tmp32 = L_sub( hPrivateData->igfCurrWhiteningLevel[p], hPrivateData->igfPrevWhiteningLevel[p] );
    2156       25252 :             if ( tmp32 != 0 )
    2157             :             {
    2158         662 :                 isSame = 0;
    2159         662 :                 move16();
    2160             :             }
    2161       25252 :             p++;
    2162             :         }
    2163             :     }
    2164     1161892 :     IF( isSame )
    2165             :     {
    2166       10380 :         IGF_write_bits( hBstr, pBitOffset, 1, 1 );
    2167             :     }
    2168             :     ELSE
    2169             :     {
    2170     1151512 :         IF( !isIndepFlag )
    2171             :         {
    2172         662 :             IGF_write_bits( hBstr, pBitOffset, 0, 1 );
    2173             :         }
    2174     1151512 :         IGF_WriteWhiteningTile_fx( hBstr, pBitOffset, hPrivateData->igfCurrWhiteningLevel[0] );
    2175     1151512 :         p = 1;
    2176     1151512 :         move16();
    2177     1151512 :         tmp32 = 0;
    2178     1151512 :         move32();
    2179     1151512 :         test();
    2180     1151512 :         IF( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) )
    2181             :         {
    2182      146678 :             isSame = 1;
    2183      146678 :             move16();
    2184             :         }
    2185             :         ELSE
    2186             :         {
    2187     1004834 :             if ( LT_16( p, nTiles ) )
    2188             :             {
    2189      505899 :                 isSame = 1;
    2190      505899 :                 move16();
    2191             :             }
    2192     1004834 :             test();
    2193     2303314 :             WHILE( ( LT_16( p, nTiles ) ) && ( tmp32 == 0 ) )
    2194             :             {
    2195     1298480 :                 test();
    2196     1298480 :                 tmp32 = L_sub( hPrivateData->igfCurrWhiteningLevel[p], hPrivateData->igfCurrWhiteningLevel[p - 1] );
    2197     1298480 :                 if ( tmp32 != 0 )
    2198             :                 {
    2199      288938 :                     isSame = 0;
    2200      288938 :                     move16();
    2201             :                 }
    2202     1298480 :                 p++;
    2203             :             }
    2204             :         }
    2205     1151512 :         test();
    2206     1151512 :         IF( !isSame )
    2207             :         {
    2208      787873 :             IGF_write_bits( hBstr, pBitOffset, 1, 1 );
    2209     2088665 :             FOR( p = 1; p < nTiles; p++ )
    2210             :             {
    2211     1300792 :                 IGF_WriteWhiteningTile_fx( hBstr, pBitOffset, hPrivateData->igfCurrWhiteningLevel[p] );
    2212             :             }
    2213             :         }
    2214      363639 :         ELSE IF( NE_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) && NE_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_FB_48000_CPE ) )
    2215             :         {
    2216      216961 :             IGF_write_bits( hBstr, pBitOffset, 0, 1 );
    2217             :         }
    2218             :     }
    2219             : 
    2220     1161892 :     totBitCount = sub( *pBitOffset, startBitCount );
    2221             : 
    2222     1161892 :     return totBitCount;
    2223             : }
    2224             : 
    2225             : 
    2226             : /*-------------------------------------------------------------------*
    2227             :  * IGF_WriteFlatteningTrigger_fx()
    2228             :  *
    2229             :  * write flattening trigger
    2230             :  *-------------------------------------------------------------------*/
    2231             : 
    2232             : /*! r: number of bits written */
    2233     1161892 : static Word16 IGF_WriteFlatteningTrigger_fx(                                          /**< out:    | number of bits written         */
    2234             :                                              const IGF_ENC_INSTANCE_HANDLE hInstance, /**< in:     | instance handle of IGF Encoder */
    2235             :                                              BSTR_ENC_HANDLE hBstr,                   /* i/o: encoder bitstream handle       */
    2236             :                                              Word16 *pBitOffset                       /**< in:     | ptr to bitOffset counter       */
    2237             : )
    2238             : {
    2239             :     Word16 flatteningTrigger;
    2240             : 
    2241             :     Word16 totBitCount;
    2242             :     Word16 startBitCount;
    2243     1161892 :     totBitCount = 0;
    2244     1161892 :     startBitCount = *pBitOffset;
    2245     1161892 :     flatteningTrigger = hInstance->flatteningTrigger;
    2246     1161892 :     move16();
    2247     1161892 :     move16();
    2248     1161892 :     move16();
    2249             : 
    2250     1161892 :     IGF_write_bits( hBstr, pBitOffset, flatteningTrigger, 1 );
    2251     1161892 :     totBitCount = sub( *pBitOffset, startBitCount );
    2252             : 
    2253     1161892 :     return totBitCount;
    2254             : }
    2255             : 
    2256             : 
    2257             : /*-------------------------------------------------------------------*
    2258             :  * IGF_UpdateInfo()
    2259             :  *
    2260             :  * updates the start/stop frequency of IGF according to igfGridIdx
    2261             :  *-------------------------------------------------------------------*/
    2262             : 
    2263             : /*-------------------------------------------------------------------*
    2264             :  * IGFEncWriteBitstream()
    2265             :  *
    2266             :  * IGF bitstream writer
    2267             :  *-------------------------------------------------------------------*/
    2268             : 
    2269             : /*! r: number of bits written per frame */
    2270     1161892 : Word16 IGFEncWriteBitstream_ivas_fx(
    2271             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i  : instance handle of IGF Encoder                                              */
    2272             :     BSTR_ENC_HANDLE hBstr,                 /* i/o: encoder bitstream handle                                                    */
    2273             :     Word16 *pBitOffset,                    /* i  : ptr to bitOffset counter                                                    */
    2274             :     const Word16 igfGridIdx,               /* i  : igf grid index see declaration of IGF_GRID_IDX for details                  */
    2275             :     const Word16 isIndepFlag               /* i  : if 1 frame is independent, 0 = frame is coded with data from previous frame */
    2276             : )
    2277             : {
    2278             :     Word16 igfAllZero;
    2279             :     Word16 startBitCount;
    2280             : 
    2281     1161892 :     startBitCount = *pBitOffset;
    2282     1161892 :     move16();
    2283     1161892 :     hIGFEnc->infoTotalBitsPerFrameWritten = 0;
    2284     1161892 :     move16();
    2285             : 
    2286     1161892 :     if ( isIndepFlag )
    2287             :     {
    2288     1150850 :         hIGFEnc->infoTotalBitsWritten = 0;
    2289     1150850 :         move16();
    2290             :     }
    2291             : 
    2292     1161892 :     IGF_WriteEnvelope( hIGFEnc,       /* i: instance handle of IGF Encoder                                              */
    2293             :                        hBstr,         /* i: encoder state                                                               */
    2294             :                        pBitOffset,    /* i: ptr to bitOffset counter                                                    */
    2295             :                        igfGridIdx,    /* i: igf grid index see definition of IGF_GRID_IDX for details                   */
    2296             :                        isIndepFlag,   /* i: if 1 frame is independent, 0 = frame is coded with data from previous frame */
    2297             :                        &igfAllZero ); /* o: *igfAllZero                                                                 */
    2298             : 
    2299     1161892 :     IGF_WriteWhiteningLevels_fx( hIGFEnc,       /* i: instance handle of IGF Encoder                                              */
    2300             :                                  hBstr,         /* i: encoder state                                                               */
    2301             :                                  pBitOffset,    /* i: ptr to bitOffset counter                                                    */
    2302             :                                  igfGridIdx,    /* i: igf grid index see definition of IGF_GRID_IDX for details                   */
    2303             :                                  isIndepFlag ); /* i: if 1 frame is independent, 0 = frame is coded with data from previous frame */
    2304             : 
    2305     1161892 :     IGF_WriteFlatteningTrigger_fx( hIGFEnc,      /* i: instance handle of IGF Encoder                                              */
    2306             :                                    hBstr,        /* i: encoder state                                                               */
    2307             :                                    pBitOffset ); /* i: ptr to bitOffset counter                                                    */
    2308             : 
    2309     1161892 :     hIGFEnc->infoTotalBitsPerFrameWritten = sub( *pBitOffset, startBitCount );
    2310     1161892 :     hIGFEnc->infoTotalBitsWritten = add( hIGFEnc->infoTotalBitsWritten, hIGFEnc->infoTotalBitsPerFrameWritten );
    2311     1161892 :     move16();
    2312     1161892 :     move16();
    2313             : 
    2314     1161892 :     return hIGFEnc->infoTotalBitsPerFrameWritten;
    2315             : }
    2316             : 
    2317             : 
    2318             : /*-------------------------------------------------------------------*
    2319             :  * IGFEncSetMode()
    2320             :  *
    2321             :  * sets the IGF mode according to given bitrate
    2322             :  *-------------------------------------------------------------------*/
    2323       35756 : void IGFEncSetMode_ivas_fx(
    2324             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i/o: instance handle of IGF Encoder */
    2325             :     const Word32 total_brate,              /* i  : encoder total bitrate          */
    2326             :     const Word16 bwidth,                   /* i  : encoder audio bandwidth        */
    2327             :     const Word16 element_mode,             /* i  : IVAS element mode              */
    2328             :     const Word16 rf_mode                   /* i  : flag to signal the RF mode     */
    2329             : )
    2330             : {
    2331             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
    2332             :     Word16 i;
    2333             : 
    2334       35756 :     hPrivateData = &hIGFEnc->igfData;
    2335       35756 :     hPrivateData->igfBitstreamBits = 0;
    2336       35756 :     move16();
    2337       35756 :     set16_fx( hPrivateData->igfScfQuantized, 0, IGF_MAX_SFB );
    2338       35756 :     set16_fx( hPrivateData->igfCurrWhiteningLevel, 0, IGF_MAX_TILES );
    2339       35756 :     set16_fx( hPrivateData->igfPrevWhiteningLevel, 0, IGF_MAX_TILES );
    2340       35756 :     set16_fx( hPrivateData->igfWhiteningHangoverCnt, 0, IGF_MAX_TILES );
    2341      393316 :     FOR( i = 0; i < IGF_MAX_TILES; i++ )
    2342             :     {
    2343      357560 :         set16_fx( hPrivateData->igfPastSFM_fx[i], -( ONE_IN_Q13 ), IGF_PAST_SFM_LEN );
    2344             :     }
    2345             : 
    2346       35756 :     hPrivateData->igfPastSFM_pos = 0;
    2347       35756 :     move16();
    2348             : 
    2349    11477676 :     FOR( i = 0; i < IGF_BITBUFSIZE / 8; i++ )
    2350             :     {
    2351    11441920 :         hPrivateData->igfBitstream[i] = 0;
    2352    11441920 :         move16();
    2353             :     }
    2354       35756 :     hPrivateData->wasTransient = 0;
    2355       35756 :     move16();
    2356       35756 :     set32_fx( hPrivateData->prevSFM_FIR, 0, IGF_MAX_TILES );
    2357       35756 :     set16_fx( hPrivateData->prevSFM_IIR, 0, IGF_MAX_TILES );
    2358       35756 :     set16_fx( hPrivateData->dampingFactorSmoothing, 2, IGF_MAX_SFB );
    2359       35756 :     set16_fx( hPrivateData->prevSFM_FIR_SFB_SB_fx, 0, IGF_MAX_SFB );
    2360       35756 :     set16_fx( hPrivateData->prevSFB_FIR_TB_e, 15, IGF_MAX_SFB );
    2361       35756 :     set16_fx( hPrivateData->prevSFB_FIR_SB_e, 15, IGF_MAX_SFB );
    2362       35756 :     set16_fx( hPrivateData->prevSFM_IIR_SFB_SB_fx, 0, IGF_MAX_SFB );
    2363       35756 :     set16_fx( hPrivateData->prevSFB_IIR_SB_e, 15, IGF_MAX_SFB );
    2364       35756 :     set16_fx( hPrivateData->prevSFM_FIR_SFB_TB_fx, 0, IGF_MAX_SFB );
    2365       35756 :     set16_fx( hPrivateData->prevSFB_IIR_TB_e, 15, IGF_MAX_SFB );
    2366       35756 :     set16_fx( hPrivateData->sfb_tb_e, 15, IGF_MAX_SFB );
    2367       35756 :     set16_fx( hPrivateData->sfb_sb_e, 15, IGF_MAX_SFB );
    2368       35756 :     set16_fx( hPrivateData->prevSFM_IIR_SFB_TB_fx, 0, IGF_MAX_SFB );
    2369       35756 :     set16_fx( hPrivateData->prevDampingFactor_IIR_fx, -( ONE_IN_Q15 ), IGF_MAX_SFB );
    2370       35756 :     set16_fx( hPrivateData->prevDampingFactor_IIR_e, 0, IGF_MAX_SFB );
    2371       35756 :     set16_fx( hPrivateData->logSpec, 0, L_FRAME_PLUS );
    2372       35756 :     set16_fx( hPrivateData->SFM_sb_fx, 0, IGF_MAX_SFB );
    2373       35756 :     set16_fx( hPrivateData->SFM_tb_fx, 0, IGF_MAX_SFB );
    2374             : 
    2375       35756 :     IF( IGFCommonFuncsIGFConfiguration_ivas_fx( total_brate, bwidth, element_mode, &hPrivateData->igfInfo, rf_mode ) != 0 )
    2376             :     {
    2377       35756 :         IGFSCFEncoderOpen_fx( &hPrivateData->hIGFSCFArithEnc, &hPrivateData->igfInfo, total_brate, bwidth, element_mode, rf_mode );
    2378             : 
    2379       35756 :         hIGFEnc->infoSamplingRate = hPrivateData->igfInfo.sampleRate;
    2380       35756 :         move32();
    2381       35756 :         hIGFEnc->infoStartFrequency = hPrivateData->igfInfo.grid[0].startFrequency;
    2382       35756 :         move16();
    2383       35756 :         hIGFEnc->infoStopFrequency = hPrivateData->igfInfo.grid[0].stopFrequency;
    2384       35756 :         move16();
    2385       35756 :         hIGFEnc->infoStartLine = hPrivateData->igfInfo.grid[0].startLine;
    2386       35756 :         move16();
    2387       35756 :         hIGFEnc->infoStopLine = hPrivateData->igfInfo.grid[0].stopLine;
    2388       35756 :         move16();
    2389             :     }
    2390             :     ELSE
    2391             :     {
    2392             :         /* IGF configuration failed -> error! */
    2393           0 :         hIGFEnc->infoSamplingRate = 0;
    2394           0 :         move32();
    2395           0 :         hIGFEnc->infoStartFrequency = -1;
    2396           0 :         move16();
    2397           0 :         hIGFEnc->infoStopFrequency = -1;
    2398           0 :         move16();
    2399           0 :         hIGFEnc->infoStartLine = -1;
    2400           0 :         move16();
    2401           0 :         hIGFEnc->infoStopLine = -1;
    2402           0 :         move16();
    2403           0 :         fprintf( stderr, "IGFEncSetMode_fx: initialization error!\n" );
    2404             :     }
    2405             : 
    2406             :     /* reset remaining variables */
    2407       35756 :     hIGFEnc->infoTotalBitsWritten = 0;
    2408       35756 :     move16();
    2409       35756 :     hIGFEnc->infoTotalBitsPerFrameWritten = 0;
    2410       35756 :     move16();
    2411       35756 :     hIGFEnc->flatteningTrigger = 0;
    2412       35756 :     move16();
    2413       35756 :     hIGFEnc->spec_be_igf_e = 0;
    2414       35756 :     move16();
    2415       35756 :     hIGFEnc->tns_predictionGain = 0;
    2416       35756 :     move16();
    2417       35756 :     set32_fx( hIGFEnc->spec_be_igf, 0, N_MAX_TCX - IGF_START_MN );
    2418       35756 :     return;
    2419             : }
    2420             : 
    2421             : /*-------------------------------------------------------------------*
    2422             :  * pack_bit_ivas()
    2423             :  *
    2424             :  * insert a bit into packed octet
    2425             :  *-------------------------------------------------------------------*/
    2426             : 
    2427             : /*-------------------------------------------------------------------*
    2428             :  * IGFEncConcatenateBitstream()
    2429             :  *
    2430             :  * IGF bitstream concatenation for TCX10 modes
    2431             :  *-------------------------------------------------------------------*/
    2432             : #ifndef HARM_PUSH_BIT /* Float code */
    2433             : void IGFEncConcatenateBitstream(
    2434             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i  : instance handle of IGF Encoder                  */
    2435             :     const Word16 bsBits,                   /* i  : number of IGF bits written to list of indices   */
    2436             :     BSTR_ENC_HANDLE hBstr                  /* i/o: bitstream handle                                */
    2437             : )
    2438             : {
    2439             :     Word16 i;
    2440             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
    2441             :     Indice *ind_list;
    2442             :     UWord8 *pFrame;      /* byte array with bit packet and byte aligned coded speech data */
    2443             :     Word16 *pFrame_size; /* number of bits in the binary encoded access unit [bits]       */
    2444             :     Word16 k, nb_bits_written;
    2445             :     Word32 imask;
    2446             :     UWord8 omask;
    2447             : 
    2448             :     hPrivateData = &hIGFEnc->igfData;
    2449             : 
    2450             :     ind_list = &hBstr->ind_list[sub( hBstr->nb_ind_tot, bsBits )]; /* here, we assume that each bit has been written as a single indice */
    2451             :     pFrame = hPrivateData->igfBitstream;
    2452             :     pFrame_size = &hPrivateData->igfBitstreamBits;
    2453             :     nb_bits_written = 0;
    2454             :     move16();
    2455             : 
    2456             :     omask = (UWord8) shr( 0x80, s_and( *pFrame_size, 0x7 ) );
    2457             :     pFrame += shr( *pFrame_size, 3 );
    2458             : 
    2459             :     /* bitstream packing (conversion of individual indices into a serial stream) */
    2460             :     FOR( i = 0; i < bsBits; i++ ){
    2461             :         IF( ind_list[i].nb_bits > 0 ){
    2462             :             /* mask from MSB to LSB */
    2463             :             imask = L_shl( 1, ( sub( ind_list[i].nb_bits, 1 ) ) );
    2464             : 
    2465             :     /* write bit by bit */
    2466             :     FOR( k = 0; k < ind_list[i].nb_bits; k++ )
    2467             :     {
    2468             :         pack_bit( ind_list[i].value & imask, &pFrame, &omask );
    2469             :         imask = L_shr( imask, 1 );
    2470             :     }
    2471             :     nb_bits_written = add( nb_bits_written, ind_list[i].nb_bits );
    2472             : 
    2473             :     /* delete the indice */
    2474             :     ind_list[i].nb_bits = -1;
    2475             :     move16();
    2476             : }
    2477             : }
    2478             : 
    2479             : *pFrame_size = add( *pFrame_size, nb_bits_written );
    2480             : move16();
    2481             : 
    2482             : /* update list of indices */
    2483             : hBstr->nb_ind_tot = sub( hBstr->nb_ind_tot, bsBits );
    2484             : hBstr->nb_bits_tot = sub( hBstr->nb_bits_tot, nb_bits_written );
    2485             : move16();
    2486             : move16();
    2487             : 
    2488             : return;
    2489             : }
    2490             : #endif
    2491             : 
    2492             : /*-------------------------------------------------------------------*
    2493             :  * IGFEncResetTCX10BitCounter_ivas_fx()
    2494             :  *
    2495             :  * IGF reset bitstream bit counter for TCX10 modes
    2496             :  *-------------------------------------------------------------------*/
    2497             : 
    2498           0 : void IGFEncResetTCX10BitCounter_ivas_fx(
    2499             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc /* i  : instance handle of IGF Encoder */
    2500             : )
    2501             : {
    2502             :     IGF_ENC_PRIVATE_DATA_HANDLE hPrivateData;
    2503             : 
    2504           0 :     hPrivateData = &hIGFEnc->igfData;
    2505           0 :     hPrivateData->igfBitstreamBits = 0;
    2506           0 :     hIGFEnc->infoTotalBitsWritten = 0;
    2507           0 :     move16();
    2508           0 :     move16();
    2509           0 :     return;
    2510             : }
    2511             : 
    2512             : 
    2513             : /*-------------------------------------------------------------------*
    2514             :  * IGFEncApplyMono()
    2515             :  *
    2516             :  * apply the IGF encoder, main encoder interface
    2517             :  *-------------------------------------------------------------------*/
    2518             : 
    2519      491554 : void IGFEncApplyMono_ivas_fx(
    2520             :     Encoder_State *st,             /* i  : Encoder state                                          */
    2521             :     Word16 powerSpectrum_len,      /* i: length of pPowerSpectrum_fx buffer */
    2522             :     const Word16 igfGridIdx,       /* i  : IGF grid index                                         */
    2523             :     Word32 *pMDCTSpectrum_fx,      /* i/o: MDCT spectrum                                          */
    2524             :     Word16 e_mdct,                 /* i : exponent of pMDCTspectrum                                                        */
    2525             :     Word32 *pPowerSpectrum_fx,     /* i/o: MDCT^2 + MDST^2 spectrum, or estimate                  */
    2526             :     Word16 *e_ps,                  /* i : exponent of pPowerSpectrum                                                       */
    2527             :     const Word16 isTCX20,          /* i  : flag indicating if the input is TCX20 or TCX10/2xTCX5  */
    2528             :     const Word8 isTNSActive,       /* i  : flag indicating if the TNS is active                   */
    2529             :     const Word16 sp_aud_decision0, /* i  : first stage switching decision                         */
    2530             :     const Word16 vad_hover_flag    /* i  : VAD hangover flag                                      */
    2531             : )
    2532             : {
    2533             :     Word32 *pPowerSpectrumParameter_fx;
    2534             :     Word16 *pPowerSpectrumParameter_exp;
    2535      491554 :     Word16 att_fx = MAX16B;
    2536             :     Word16 last_core_acelp;
    2537      491554 :     move16();
    2538             : 
    2539             :     Word32 common_pPowerSpectrum_fx[N_MAX + L_MDCT_OVLP_MAX];
    2540             : 
    2541      491554 :     set32_fx( common_pPowerSpectrum_fx, 0, N_MAX + L_MDCT_OVLP_MAX );
    2542             : 
    2543      491554 :     Word16 common_pPowerSpectrum_exp = MIN16B;
    2544      491554 :     move16();
    2545      491554 :     IF( st->last_core == ACELP_CORE )
    2546             :     {
    2547        8512 :         last_core_acelp = 1;
    2548        8512 :         move16();
    2549             :     }
    2550             :     ELSE
    2551             :     {
    2552      483042 :         last_core_acelp = 0;
    2553      483042 :         move16();
    2554             :     }
    2555             : 
    2556      491554 :     test();
    2557      491554 :     IF( !isTNSActive && isTCX20 )
    2558             :     {
    2559      430933 :         pPowerSpectrumParameter_fx = pPowerSpectrum_fx;
    2560      430933 :         pPowerSpectrumParameter_exp = e_ps;
    2561             :     }
    2562             :     ELSE
    2563             :     {
    2564       60621 :         pPowerSpectrumParameter_fx = NULL;
    2565       60621 :         pPowerSpectrumParameter_exp = NULL;
    2566             :     }
    2567             : 
    2568      491554 :     IGF_UpdateInfo( st->hIGFEnc, igfGridIdx );
    2569             : 
    2570      491554 :     test();
    2571      491554 :     IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) || EQ_16( st->element_mode, IVAS_CPE_TD ) )
    2572             :     {
    2573       12905 :         calculate_hangover_attenuation_gain_ivas_fx( st, &att_fx, vad_hover_flag );
    2574             :     }
    2575             : 
    2576      491554 :     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 );
    2577             : 
    2578      491554 :     IF( isTCX20 )
    2579             :     {
    2580      473688 :         pPowerSpectrumParameter_fx = pPowerSpectrum_fx;
    2581      473688 :         pPowerSpectrumParameter_exp = e_ps;
    2582             :     }
    2583             :     ELSE
    2584             :     {
    2585       17866 :         pPowerSpectrumParameter_fx = NULL;
    2586       17866 :         pPowerSpectrumParameter_exp = NULL;
    2587             :     }
    2588             : 
    2589      491554 :     IF( EQ_16( st->element_mode, IVAS_CPE_MDCT ) )
    2590             :     {
    2591      264457 :         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 );
    2592             :     }
    2593             :     ELSE
    2594             :     {
    2595      227097 :         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 );
    2596             :     }
    2597             : 
    2598      491554 :     IF( pPowerSpectrumParameter_fx )
    2599             :     {
    2600   624461148 :         FOR( Word16 i = 0; i < powerSpectrum_len; i++ )
    2601             :         {
    2602   623987460 :             common_pPowerSpectrum_exp = s_max( common_pPowerSpectrum_exp, pPowerSpectrumParameter_exp[i] );
    2603             :         }
    2604             : 
    2605   624461148 :         FOR( Word16 i = 0; i < powerSpectrum_len; i++ )
    2606             :         {
    2607   623987460 :             common_pPowerSpectrum_fx[i] = L_shl( pPowerSpectrumParameter_fx[i], sub( pPowerSpectrumParameter_exp[i], common_pPowerSpectrum_exp ) );
    2608   623987460 :             move16();
    2609             :         }
    2610      473688 :         pPowerSpectrumParameter_fx = common_pPowerSpectrum_fx;
    2611             :     }
    2612      491554 :     IGF_ErodeSpectrum_ivas_fx( st->hIGFEnc, pMDCTSpectrum_fx, pPowerSpectrumParameter_fx, common_pPowerSpectrum_exp, igfGridIdx, 0 );
    2613      491554 : }
    2614             : 
    2615             : 
    2616             : /*-------------------------------------------------------------------*
    2617             :  * IGFEncApplyStereo()
    2618             :  *
    2619             :  * apply the IGF encoder, main encoder interface
    2620             :  *-------------------------------------------------------------------*/
    2621             : 
    2622       50217 : void IGFEncApplyStereo_fx(
    2623             :     STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct,              /* i/o: MDCT stereo encoder structure           */
    2624             :     Word16 ms_mask[2][MAX_SFB],                           /* i  : bandwise MS mask                        */
    2625             :     const IGF_ENC_INSTANCE_HANDLE hIGFEnc[CPE_CHANNELS],  /* i  : instance handle of IGF Encoder          */
    2626             :     const Word16 igfGridIdx,                              /* i  : IGF grid index                          */
    2627             :     Encoder_State *sts[CPE_CHANNELS],                     /* i  : Encoder state                           */
    2628             :     Word32 *pPowerSpectrum_fx[CPE_CHANNELS],              /* i/o: MDCT^2 + MDST^2 spectrum, or estimate   */
    2629             :     Word16 exp_pPowerSpectrum_fx[CPE_CHANNELS],           /* i/o: exp of pPowerSpectrum_fx                */
    2630             :     Word32 *pPowerSpectrumMsInv_fx[CPE_CHANNELS][NB_DIV], /* i/o: inverse power spectrum                  */
    2631             :     Word16 exp_pPowerSpectrumMsInv_fx[CPE_CHANNELS],      /* i/o: exp of pPowerSpectrumMsInv_fx           */
    2632             :     Word32 *inv_spectrum_fx[CPE_CHANNELS][NB_DIV],        /* i  : inverse spectrum                        */
    2633             :     Word16 exp_inv_spectrum_fx[CPE_CHANNELS],             /* i  : exp of inverse spectrum                 */
    2634             :     const Word16 frameno,                                 /* i  : flag indicating index of current subfr. */
    2635             :     const Word16 sp_aud_decision0,                        /* i  : sp_aud_decision0                        */
    2636             :     const Word32 element_brate,                           /* i  : element bitrate                         */
    2637             :     const Word16 mct_on )
    2638             : {
    2639             :     Word32 *pPowerSpectrumParameter_fx[NB_DIV]; /* If it is NULL it informs a function that specific handling is needed */
    2640             :     Word32 *pPowerSpectrumParameterMsInv_fx[NB_DIV];
    2641             :     Word16 coreMsMask[N_MAX];
    2642             :     Word16 sfb, ch, last_core_acelp;
    2643             :     STEREO_MDCT_BAND_PARAMETERS *sfbConf;
    2644             :     Word16 exp_pPowerSpectrum[L_FRAME48k];
    2645             : 
    2646             :     /* assumptions: stereo filling was already done on the flattened spectra
    2647             :      *               IGF region is always coded M/S, never L/R (to be done in the encoder)
    2648             :      *               for residual bands with stereo filling infoTcxNoise is set to zero
    2649             :      *               both channels have the same IGF configuration
    2650             :      */
    2651             : 
    2652             :     /* sanity checks: check if both channels have the same configuration...*/
    2653       50217 :     assert( ( sts[0]->core == sts[1]->core ) );
    2654             : 
    2655             :     /* initialization */
    2656       50217 :     IF( EQ_16( sts[0]->core, TCX_20_CORE ) )
    2657             :     {
    2658       48108 :         sfbConf = &hStereoMdct->stbParamsTCX20;
    2659             :     }
    2660             :     ELSE
    2661             :     {
    2662        2109 :         sfbConf = &hStereoMdct->stbParamsTCX10;
    2663             :     }
    2664       50217 :     if ( sts[0]->last_core == ACELP_CORE )
    2665             :     {
    2666           0 :         sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
    2667             :     }
    2668             : 
    2669             :     /* create line wise ms mask for the core bands */
    2670       50217 :     set16_fx( coreMsMask, 0, N_MAX );
    2671     2142032 :     FOR( sfb = 0; sfb < sfbConf->sfbCnt; sfb++ )
    2672             :     {
    2673     2091815 :         set16_fx( &coreMsMask[sfbConf->sfbOffset[sfb]], ms_mask[frameno][sfb], sub( sfbConf->sfbOffset[sfb + 1], sfbConf->sfbOffset[sfb] ) );
    2674             :     }
    2675             : 
    2676       50217 :     test();
    2677       50217 :     test();
    2678       50217 :     IF( EQ_16( sts[0]->core, TCX_20_CORE ) && !sts[0]->hTcxEnc->fUseTns[frameno] && !sts[1]->hTcxEnc->fUseTns[frameno] )
    2679             :     {
    2680       40797 :         pPowerSpectrumParameter_fx[0] = &pPowerSpectrum_fx[0][0];
    2681       40797 :         pPowerSpectrumParameter_fx[1] = &pPowerSpectrum_fx[1][0];
    2682       40797 :         pPowerSpectrumParameterMsInv_fx[0] = pPowerSpectrumMsInv_fx[0][0];
    2683       40797 :         pPowerSpectrumParameterMsInv_fx[1] = pPowerSpectrumMsInv_fx[1][0];
    2684             :     }
    2685             :     ELSE
    2686             :     {
    2687        9420 :         pPowerSpectrumParameter_fx[0] = NULL;
    2688        9420 :         pPowerSpectrumParameter_fx[1] = NULL;
    2689        9420 :         pPowerSpectrumParameterMsInv_fx[0] = NULL;
    2690        9420 :         pPowerSpectrumParameterMsInv_fx[1] = NULL;
    2691             :     }
    2692      150651 :     FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
    2693             :     {
    2694      100434 :         last_core_acelp = extract_l( EQ_16( sts[ch]->last_core, ACELP_CORE ) );
    2695             : 
    2696      100434 :         IGF_UpdateInfo( hIGFEnc[ch], igfGridIdx );
    2697      100434 :         IGF_CalculateStereoEnvelope_fx( hIGFEnc[ch], sts[ch]->hTcxEnc->spectrum_fx[frameno], sts[ch]->hTcxEnc->spectrum_e[frameno], inv_spectrum_fx[ch][frameno],
    2698      100434 :                                         exp_inv_spectrum_fx[ch], pPowerSpectrumParameter_fx[ch], exp_pPowerSpectrum_fx[ch], pPowerSpectrumParameterMsInv_fx[ch],
    2699      100434 :                                         exp_pPowerSpectrumMsInv_fx[ch], igfGridIdx, coreMsMask, sts[ch]->hTranDet->transientDetector.bIsAttackPresent, last_core_acelp );
    2700             : 
    2701      100434 :         IF( EQ_16( sts[ch]->core, TCX_20_CORE ) )
    2702             :         {
    2703       96216 :             pPowerSpectrumParameter_fx[ch] = pPowerSpectrum_fx[ch];
    2704             :         }
    2705             :         ELSE
    2706             :         {
    2707        4218 :             pPowerSpectrumParameter_fx[ch] = NULL;
    2708             :         }
    2709             : 
    2710      100434 :         set16_fx( exp_pPowerSpectrum, exp_pPowerSpectrum_fx[ch], L_FRAME48k );
    2711             : 
    2712      100434 :         IGF_Whitening_ivas_fx( hIGFEnc[ch], pPowerSpectrumParameter_fx[ch], &exp_pPowerSpectrum[0], 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 );
    2713             : 
    2714      100434 :         IGF_ErodeSpectrum_ivas_fx( hIGFEnc[ch], sts[ch]->hTcxEnc->spectrum_fx[frameno], pPowerSpectrumParameter_fx[ch], exp_pPowerSpectrum_fx[ch], igfGridIdx, mct_on );
    2715             :     }
    2716       50217 :     return;
    2717             : }
    2718             : 
    2719             : 
    2720             : /*-------------------------------------------------------------------*
    2721             :  * IGFSaveSpectrumForITF()
    2722             :  *
    2723             :  *
    2724             :  *-------------------------------------------------------------------*/
    2725             : 
    2726      591988 : void IGFSaveSpectrumForITF_ivas_fx(
    2727             :     IGF_ENC_INSTANCE_HANDLE hIGFEnc, /* i/o: instance handle of IGF Encoder  */
    2728             :     const Word16 igfGridIdx,         /* i  : IGF grid index                  */
    2729             :     const Word32 *pITFSpectrum,      /* i  : MDCT spectrum                   */
    2730             :     Word16 exp_pITFSpectrum )
    2731             : {
    2732      591988 :     IGF_UpdateInfo( hIGFEnc, igfGridIdx );
    2733             : 
    2734      591988 :     Copy32( pITFSpectrum + IGF_START_MN, hIGFEnc->spec_be_igf, sub( hIGFEnc->infoStopLine, IGF_START_MN ) );
    2735             : 
    2736      591988 :     scale_sig32( hIGFEnc->spec_be_igf, sub( hIGFEnc->infoStopLine, IGF_START_MN ), sub( exp_pITFSpectrum, s_max( exp_pITFSpectrum, hIGFEnc->spec_be_igf_e ) ) );
    2737      591988 :     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 ) ) );
    2738      591988 :     hIGFEnc->spec_be_igf_e = s_max( exp_pITFSpectrum, hIGFEnc->spec_be_igf_e );
    2739      591988 :     move16();
    2740             : 
    2741      591988 :     return;
    2742             : }
    2743             : 
    2744        3060 : ivas_error IGF_Reconfig(
    2745             :     IGF_ENC_INSTANCE_HANDLE *hIGFEnc, /* i/o: instance handle of IGF Encoder  */
    2746             :     const Word16 igf,                 /* i  : IGF on/off                      */
    2747             :     const Word16 reset,               /* i  : reset flag                      */
    2748             :     const Word32 brate,               /* i  : bitrate for configuration       */
    2749             :     const Word16 bwidth,              /* i  : signal bandwidth                */
    2750             :     const Word16 element_mode,        /* i  : IVAS element mode               */
    2751             :     const Word16 rf_mode              /* i  : flag to signal the RF mode      */
    2752             : )
    2753             : {
    2754             :     ivas_error error;
    2755             : 
    2756        3060 :     error = IVAS_ERR_OK;
    2757        3060 :     move32();
    2758             : 
    2759        3060 :     test();
    2760        3060 :     test();
    2761        3060 :     test();
    2762        3060 :     IF( igf && *hIGFEnc == NULL )
    2763             :     {
    2764         394 :         IF( ( *hIGFEnc = (IGF_ENC_INSTANCE_HANDLE) malloc( sizeof( IGF_ENC_INSTANCE ) ) ) == NULL )
    2765             :         {
    2766           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hIGFEnc\n" ) );
    2767             :         }
    2768         394 :         IGFEncSetMode_ivas_fx( *hIGFEnc, brate, bwidth, element_mode, rf_mode );
    2769             :     }
    2770        2666 :     ELSE IF( igf && reset )
    2771             :     {
    2772        1819 :         IGFEncSetMode_ivas_fx( *hIGFEnc, brate, bwidth, element_mode, rf_mode );
    2773             :     }
    2774         847 :     ELSE IF( !igf && *hIGFEnc != NULL )
    2775             :     {
    2776         567 :         free( *hIGFEnc );
    2777         567 :         *hIGFEnc = NULL;
    2778             :     }
    2779             : 
    2780        3060 :     return error;
    2781             : }

Generated by: LCOV version 1.14