LCOV - code coverage report
Current view: top level - lib_enc - nois_est_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 574a190e3c6896c6c4ed10d7f23649709a0c4347 Lines: 1635 1749 93.5 %
Date: 2025-06-27 02:59:36 Functions: 12 12 100.0 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : 
       5             : #include <stdint.h>
       6             : #include <assert.h>
       7             : #include "options.h"
       8             : #include "cnst.h"
       9             : #include "rom_com.h"
      10             : //#include "prot_fx.h"
      11             : #include "prot_fx.h"     /* Function prototypes                    */
      12             : #include "prot_fx_enc.h" /* Function prototypes                    */
      13             : #include "basop_util.h"
      14             : 
      15             : /*-----------------------------------------------------------------*
      16             :  * Local constants
      17             :  *-----------------------------------------------------------------*/
      18             : 
      19             : #define ALPHA_FX   3277  /*.1 Q15*/
      20             : #define ALPHAM1_FX 29491 /*.9 Q15*/
      21             : 
      22             : #define TH_PC_FX 12 /* max sum of pitch differencies */
      23             : 
      24             : #define BCKR_SLOW_UPDATE_SCALE_FX 3277  /*Q15 Step size for slow bckr update */
      25             : #define COR_MIN16_FX              17039 /*.52 Q15 */
      26             : #define COR_MAX16_FX              27853 /*.85 Q15 */
      27             : 
      28             : /* 10.0e5f causes problems with music - as the noise estimate starts to track the music */
      29             : #define TH_STA16_FX ( 350000 << 10 ) /* MODE1: 3.5e5f */
      30             : 
      31             : #define TH_EPS16_FX    3277  /* Q11 (1.6) tuned for speech only, not for music */
      32             : #define K_16_FX        25690 /* Q20 (0.0245f) */
      33             : #define C_16_FX        -1925 /* Q13 (-0.235f) */
      34             : #define ALPHA_MAX16_FX 32440 /* Q15 (0.99)    */
      35             : 
      36             : #define COR_MIN8_FX   21299 /*.65 Q15 */
      37             : #define COR_MAX8_FX   22938 /*.70 Q15 */
      38             : #define TH_EPS8_FX    21299 /* 10.4 in Q11 Thresholds for NB processing - ... */
      39             : #define TH_STA8_FX    ( 500000 << 10 )
      40             : #define K_8_FX        9452  /* Q20 (0.0091f) */
      41             : #define C_8_FX        2609  /* Q13 (0.0091f) */
      42             : #define ALPHA_MAX8_FX 32735 /* Q15 (0.999)   */
      43             : 
      44             : #define HC_CNT_SLOW_FX 80 /* limit for harm corr count slow */
      45             : 
      46             : #define HE_LT_CNT_PRE_FX       50
      47             : #define HE_LT_CNT_INIT_FX      150
      48             : #define HE_LT_CNT_PRE_SHORT_FX 250
      49             : #define HE_LT_CNT_FX           30
      50             : 
      51             : #define LN_E_MIN_PLUS_ONE_FX 1     /* max(1, ln(E_MIN+1.0))  = max(1,ln(0.0035f+1f)) in  Q8 */
      52             : #define COR_MAX_NNE_FX       27853 /*  0.85 Q15    */
      53             : 
      54             : #define HE_LT_THR1_FX  2560      /*10.0  Q8*/
      55             : #define HE_LT_THR2_FX  7680      /*30.0 Q8*/
      56             : #define HE_LT_THR1_Q24 167772160 /*10.0 Q24 */
      57             : #define HE_LT_THR2_Q24 503316480 /*30.0 Q24 */
      58             : 
      59             : /*-----------------------------------------------------------------*
      60             :  * noise_est_AR1_Qx()
      61             :  *
      62             :  * y(n)(Qx) = alpha(Q15) * x(Qx)  +  (1.0f-alpha)* y(n-1) (Qx)
      63             :  *-----------------------------------------------------------------*/
      64             : 
      65       35562 : Word16 noise_est_AR1_Qx(              /* o : Qx   y(n)  */
      66             :                          Word16 x,    /* i : Qx  x(n)   */
      67             :                          Word16 y,    /* i : Qx  y(n-1) */
      68             :                          Word16 alpha /*i : Q15 scaling of driving x(n)  */
      69             : )
      70             : {
      71             :     Word16 alpham1;
      72             :     /*alpham1 = negate(add((Word16)-32768, alpha)); */
      73       35562 :     alpham1 = sub( 32767, alpha ); /* one cycle less */
      74             : 
      75       35562 :     return mac_r( L_mult( y, alpham1 ), x, alpha );
      76             : }
      77             : 
      78     1714834 : Word32 noise_est_AR1_Qx_32(              /* o : Qx   y(n)  */
      79             :                             Word32 x,    /* i : Qx  x(n)   */
      80             :                             Word32 y,    /* i : Qx  y(n-1) */
      81             :                             Word32 alpha /*i : Q15 scaling of driving x(n)  */
      82             : )
      83             : {
      84             :     Word32 alpham1;
      85             :     /*alpham1 = negate(add((Word16)-32768, alpha)); */
      86     1714834 :     alpham1 = L_sub( MAX_32, alpha ); /* one cycle less */
      87     1714834 :     alpham1++;
      88             : 
      89     1714834 :     return Madd_32_32( Mpy_32_32( y, alpham1 ), x, alpha );
      90             : }
      91             : 
      92             : /*-----------------------------------------------------------------*
      93             :  * noise_est_ln_q8_fx()
      94             :  *
      95             :  * q8  = (float)log( L_enr[i] + add1po*1.0 );
      96             :  *-----------------------------------------------------------------*/
      97             : 
      98      139500 : static Word16 noise_est_ln_q8_fx(
      99             :     Word32 L_enr,
     100             :     Word16 flag_add1p0, /* flag to  add 1.0 */
     101             :     Word16 q_new_plus_q_scale )
     102             : {
     103             :     Word16 e_ener, f_ener;
     104             :     Word32 L_tmp;
     105             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     106      139500 :     Flag Overflow = 0;
     107      139500 :     move32();
     108             : #endif
     109             : 
     110      139500 :     L_tmp = L_add_o( L_enr, L_shl( (Word32) 1L, q_new_plus_q_scale ), &Overflow ); /*  +1.0f */
     111      139500 :     if ( flag_add1p0 == 0 )
     112             :     {
     113       46500 :         L_tmp = L_add( L_enr, 0 ); /* +0 , no offset */
     114             :     }
     115             : 
     116      139500 :     L_tmp = L_max( L_tmp, (Word32) 1L ); /* make sure log2_norm_lc does not cause table reading out of bounds */
     117             : 
     118      139500 :     e_ener = norm_l( L_tmp );
     119      139500 :     f_ener = Log2_norm_lc( L_shl( L_tmp, e_ener ) );
     120      139500 :     e_ener = sub( sub( 30, e_ener ), q_new_plus_q_scale );
     121      139500 :     L_tmp = Mpy_32_16( e_ener, f_ener, 22713 ); /* Q16 (22713 = Ln(2) in Q15)*/
     122      139500 :     return round_fx( L_shl( L_tmp, 8 ) );       /* Q8 */
     123             : }
     124             : 
     125             : /*-----------------------------------------------------------------*
     126             :  * eps_quota_fx()
     127             :  *
     128             :  *
     129             :  *-----------------------------------------------------------------*/
     130             : 
     131        9300 : static Word32 eps_quota_fx(                   /* o:  eps_num/eps_den  in  q_out */
     132             :                             Word16 eps_num_h, /* num high ,*/
     133             :                             Word16 eps_num_l, /* num low (signed) ,*/
     134             :                             Word16 eps_den_h, /* den low  */
     135             :                             Word16 eps_den_l, /* den low (signed),*/
     136             :                             Word16 q_out      /* range 15...0 , tested with 11 and 12 */
     137             : )
     138             : {
     139             :     Word32 L_tmp_num, L_tmp_den;
     140             :     Word16 exp_num, exp_den;
     141             :     Word16 m_num, m_den;
     142             :     Word16 num_shift;
     143             : 
     144             : 
     145        9300 :     L_tmp_num = L_Comp( eps_num_h, eps_num_l );
     146        9300 :     L_tmp_den = L_Comp( eps_den_h, eps_den_l );
     147             : 
     148        9300 :     exp_num = sub( norm_l( L_tmp_num ), 1 ); /*  make sure m_ num is lower than  m_den  */
     149        9300 :     m_num = extract_h( L_shl( L_tmp_num, exp_num ) );
     150        9300 :     exp_den = norm_l( L_tmp_den );
     151        9300 :     m_den = extract_h( L_shl( L_tmp_den, exp_den ) );
     152             : 
     153        9300 :     exp_num = sub( exp_num, exp_den );
     154        9300 :     if ( m_den != 0 )
     155             :     {
     156        9300 :         assert( m_den >= m_num );
     157        9300 :         m_num = div_s( m_num, m_den ); /* single basop */
     158             :     }
     159             : 
     160        9300 :     num_shift = add( 15 - q_out, exp_num );
     161        9300 :     if ( m_den == 0 )
     162             :     {
     163             :         /* no division made due to zero denominator  */
     164           0 :         m_num = 0;
     165           0 :         move16();
     166             :     }
     167             : 
     168        9300 :     return L_shr( m_num, num_shift );
     169             : }
     170             : 
     171             : /*-----------------------------------------------------------------*
     172             :  * noise_est_init_fx()
     173             :  *
     174             :  * Initialization of Noise estimator
     175             :  *-----------------------------------------------------------------*/
     176           3 : void noise_est_init_fx(
     177             :     NOISE_EST_HANDLE hNoiseEst /* i/o: Noise estimation handle */
     178             : )
     179             : {
     180             :     Word16 i;
     181             : 
     182          63 :     FOR( i = 0; i < NB_BANDS; i++ )
     183             :     {
     184          60 :         hNoiseEst->fr_bands1_fx[i] = 1;
     185          60 :         move32(); /*1e-5f; */
     186          60 :         hNoiseEst->fr_bands2_fx[i] = 1;
     187          60 :         move32(); /*1e-5f; */
     188          60 :         hNoiseEst->ave_enr2_fx[i] = E_MIN_FX;
     189          60 :         move32(); /*Q7//E_MIN; */
     190          60 :         hNoiseEst->enrO_fx[i] = E_MIN_FX;
     191          60 :         move32();
     192          60 :         hNoiseEst->bckr_fx[i] = E_MIN_FX;
     193          60 :         move32();
     194          60 :         hNoiseEst->ave_enr_fx[i] = E_MIN_FX;
     195          60 :         move32();
     196             :     }
     197           3 :     hNoiseEst->fr_bands_fx_q = 17;
     198           3 :     move16(); /*1e-5f; */
     199           3 :     hNoiseEst->q_enrO = Q7;
     200           3 :     move16();
     201           3 :     hNoiseEst->q_bckr = Q7;
     202           3 :     move16();
     203           3 :     hNoiseEst->ave_enr_q = Q7;
     204           3 :     move16();
     205             : 
     206           3 :     hNoiseEst->totalNoise_fx = 0;
     207           3 :     move16();
     208           3 :     hNoiseEst->first_noise_updt = 0;
     209           3 :     move16();
     210             : 
     211           3 :     hNoiseEst->aEn = 6;
     212           3 :     move16();
     213           3 :     hNoiseEst->aEn_inac_cnt = 0;
     214           3 :     move16();
     215             : 
     216           3 :     hNoiseEst->harm_cor_cnt = 0;
     217           3 :     move16();
     218           3 :     hNoiseEst->bg_cnt = 0;
     219           3 :     move16();
     220             : 
     221           3 :     hNoiseEst->lt_tn_track_fx = 6554; /*.20 in Q15*/
     222           3 :     move16();
     223           3 :     hNoiseEst->lt_tn_dist_fx = 0;
     224           3 :     move16();
     225           3 :     hNoiseEst->lt_Ellp_dist_fx = 0;
     226           3 :     move16();
     227           3 :     hNoiseEst->lt_haco_ev_fx = 13107; /*.40 in Q15*/
     228           3 :     move16();
     229           3 :     hNoiseEst->low_tn_track_cnt = 0;
     230           3 :     move16();
     231             : 
     232           3 :     hNoiseEst->Etot_st_est_fx = 5120;    /* 20.0f in Q8 */
     233           3 :     hNoiseEst->Etot_sq_st_est_fx = 1600; /* 400 in Q2 */
     234           3 :     move16();
     235           3 :     move16();
     236             : 
     237           3 :     hNoiseEst->epsP_0_2_lp_fx = 4096; /*1.0 Q12*/
     238           3 :     move16();
     239           3 :     hNoiseEst->epsP_0_2_ad_lp_fx = 0;
     240           3 :     move16();
     241           3 :     hNoiseEst->epsP_2_16_lp_fx = 4096;
     242           3 :     move16();
     243           3 :     hNoiseEst->epsP_2_16_lp2_fx = 4096;
     244           3 :     move16();
     245           3 :     hNoiseEst->epsP_2_16_dlp_lp2_fx = 0;
     246           3 :     move16();
     247           3 :     hNoiseEst->lt_aEn_zero_fx = 0;
     248           3 :     move16();
     249             : 
     250             :     /* Tonal detector */
     251         387 :     FOR( i = 0; i < L_FFT / 2; i++ )
     252             :     {
     253         384 :         hNoiseEst->old_S_fx[i] = 1;
     254         384 :         move16();
     255             :     }
     256           3 :     set16_fx( hNoiseEst->cor_map_fx, 0, L_FFT / 2 );
     257           3 :     hNoiseEst->act_pred_fx = 32767;
     258           3 :     move16();
     259           3 :     hNoiseEst->noise_char_fx = 0;
     260           3 :     move16();
     261           3 :     hNoiseEst->multi_harm_limit_fx = THR_CORR_INIT_FX;
     262           3 :     move16();
     263           3 :     hNoiseEst->Etot_lp_fx = 0;
     264           3 :     hNoiseEst->Etot_h_fx = 0;
     265           3 :     hNoiseEst->Etot_l_fx = 0;
     266           3 :     hNoiseEst->Etot_l_lp_fx = 0;
     267           3 :     hNoiseEst->Etot_last_fx = 0;
     268           3 :     hNoiseEst->Etot_v_h2_fx = 0;
     269           3 :     hNoiseEst->sign_dyn_lp_fx = 0;
     270           3 :     move16();
     271           3 :     move16();
     272           3 :     move16();
     273           3 :     move16();
     274           3 :     move16();
     275           3 :     move16();
     276           3 :     move16();
     277             : 
     278           3 :     return;
     279             : }
     280             : 
     281        9202 : void noise_est_init_ivas_fx(
     282             :     NOISE_EST_HANDLE hNoiseEst /* i/o: Noise estimation handle */
     283             : )
     284             : {
     285             :     Word16 i;
     286             : 
     287      193242 :     FOR( i = 0; i < NB_BANDS; i++ )
     288             :     {
     289      184040 :         hNoiseEst->fr_bands1_fx[i] = 1;
     290      184040 :         move32(); /*1e-5f; */
     291      184040 :         hNoiseEst->fr_bands2_fx[i] = 1;
     292      184040 :         move32(); /*1e-5f; */
     293      184040 :         hNoiseEst->ave_enr2_fx[i] = E_MIN_Q11_FX;
     294      184040 :         move32(); /*Q7//E_MIN; */
     295      184040 :         hNoiseEst->enrO_fx[i] = E_MIN_Q11_FX;
     296      184040 :         move32();
     297      184040 :         hNoiseEst->bckr_fx[i] = E_MIN_Q27_FX;
     298      184040 :         move32();
     299      184040 :         hNoiseEst->ave_enr_fx[i] = E_MIN_Q11_FX;
     300      184040 :         move32();
     301             :     }
     302        9202 :     hNoiseEst->fr_bands_fx_q = 17;
     303        9202 :     move16(); /*1e-5f; */
     304        9202 :     hNoiseEst->q_enrO = Q11;
     305        9202 :     move16();
     306        9202 :     hNoiseEst->q_bckr = Q27;
     307        9202 :     move16();
     308        9202 :     hNoiseEst->ave_enr_q = Q11;
     309        9202 :     move16();
     310             : 
     311        9202 :     move16();
     312        9202 :     hNoiseEst->totalNoise_32fx = 0;
     313        9202 :     move32();
     314        9202 :     hNoiseEst->first_noise_updt = 0;
     315        9202 :     move16();
     316        9202 :     hNoiseEst->first_noise_updt_cnt = 0;
     317        9202 :     move16();
     318             : 
     319        9202 :     hNoiseEst->aEn = 6;
     320        9202 :     move16();
     321        9202 :     hNoiseEst->aEn_inac_cnt = 0;
     322        9202 :     move16();
     323        9202 :     hNoiseEst->harm_cor_cnt = 0;
     324        9202 :     move16();
     325        9202 :     hNoiseEst->bg_cnt = 0;
     326        9202 :     move16();
     327             : 
     328        9202 :     hNoiseEst->lt_tn_track_fx = 6554; /*.20 in Q15*/
     329        9202 :     move16();
     330        9202 :     hNoiseEst->lt_tn_dist_fx = 0;
     331        9202 :     move16();
     332        9202 :     hNoiseEst->lt_Ellp_dist_fx = 0;
     333        9202 :     move16();
     334        9202 :     hNoiseEst->lt_haco_ev_fx = 13107; /*.40 in Q15*/
     335        9202 :     move16();
     336        9202 :     hNoiseEst->low_tn_track_cnt = 0;
     337        9202 :     move16();
     338             : 
     339        9202 :     hNoiseEst->Etot_st_est_fx = 5120;    /* 20.0f in Q8 */
     340        9202 :     hNoiseEst->Etot_sq_st_est_fx = 1600; /* 400 in Q2 */
     341        9202 :     move16();
     342        9202 :     move16();
     343        9202 :     hNoiseEst->L_Etot_st_est_fx = 167772160;   /* 20.0f in Q23 */
     344        9202 :     hNoiseEst->L_Etot_sq_st_est_fx = 26214400; /* 400 in Q16 */
     345        9202 :     move32();
     346        9202 :     move32();
     347        9202 :     hNoiseEst->epsP_0_2_lp_fx = 4096; /*1.0 Q12*/
     348        9202 :     move16();
     349        9202 :     hNoiseEst->epsP_0_2_ad_lp_fx = 0;
     350        9202 :     move16();
     351        9202 :     hNoiseEst->epsP_2_16_lp_fx = 4096;
     352        9202 :     move16();
     353        9202 :     hNoiseEst->epsP_2_16_lp2_fx = 4096;
     354        9202 :     move16();
     355        9202 :     hNoiseEst->epsP_2_16_dlp_lp2_fx = 0;
     356        9202 :     move16();
     357        9202 :     hNoiseEst->lt_aEn_zero_fx = 0;
     358        9202 :     move16();
     359             : 
     360             :     /* Tonal detector */
     361     1187058 :     FOR( i = 0; i < L_FFT / 2; i++ )
     362             :     {
     363     1177856 :         hNoiseEst->old_S_fx[i] = ONE_IN_Q7;
     364     1177856 :         move16();
     365             :     }
     366        9202 :     set16_fx( hNoiseEst->cor_map_fx, 0, L_FFT / 2 );
     367        9202 :     hNoiseEst->act_pred_fx = 32767;
     368        9202 :     move16();
     369        9202 :     hNoiseEst->noise_char_fx = 0;
     370        9202 :     move16();
     371        9202 :     hNoiseEst->multi_harm_limit_fx = THR_CORR_INIT_FX;
     372        9202 :     move16();
     373        9202 :     hNoiseEst->Etot_lp_fx = 0;
     374        9202 :     hNoiseEst->Etot_h_fx = 0;
     375        9202 :     hNoiseEst->Etot_l_32fx = 0;
     376        9202 :     hNoiseEst->Etot_l_lp_32fx = 0;
     377        9202 :     hNoiseEst->Etot_last_32fx = 0;
     378        9202 :     hNoiseEst->Etot_v_h2_32fx = 0;
     379        9202 :     hNoiseEst->sign_dyn_lp_fx = 0;
     380        9202 :     move16();
     381        9202 :     move16();
     382        9202 :     move32();
     383        9202 :     move16();
     384        9202 :     move32();
     385        9202 :     move32();
     386        9202 :     move32();
     387             : 
     388        9202 :     return;
     389             : }
     390             : /*-----------------------------------------------------------------*
     391             :  * noise_est_pre_fx()
     392             :  *
     393             :  * Track energy and signal dynamics
     394             :  *-----------------------------------------------------------------*/
     395             : 
     396        3100 : void noise_est_pre_fx(
     397             :     const Word16 Etot,             /* i  : Energy of current frame     */
     398             :     const Word16 ini_frame_fx,     /* i  : Frame number (init)         */
     399             :     NOISE_EST_HANDLE hNoiseEst,    /* i/o: Noise estimation handle     */
     400             :     const Word16 idchan,           /* i  : channel ID                  */
     401             :     const Word16 element_mode,     /* i  : element mode                */
     402             :     const Word16 last_element_mode /* i  : last element mode           */
     403             : )
     404             : {
     405             :     Word16 tmp;
     406             : 
     407        3100 :     IF( LE_16( ini_frame_fx, 1 ) || ( EQ_16( idchan, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) && EQ_16( last_element_mode, IVAS_CPE_DFT ) ) )
     408             :     {
     409           6 :         hNoiseEst->Etot_h_fx = Etot;
     410           6 :         move16();
     411           6 :         hNoiseEst->Etot_l_fx = Etot;
     412           6 :         move16();
     413           6 :         hNoiseEst->Etot_l_lp_fx = Etot;
     414           6 :         move16();
     415           6 :         hNoiseEst->Etot_last_fx = Etot;
     416           6 :         move16();
     417           6 :         hNoiseEst->Etot_v_h2_fx = 0;
     418           6 :         move16();
     419           6 :         hNoiseEst->Etot_lp_fx = Etot;
     420           6 :         move16();
     421           6 :         hNoiseEst->sign_dyn_lp_fx = 0;
     422           6 :         move16();
     423             :     }
     424             :     ELSE
     425             :     {
     426             :         /* *Etot_lp = 0.20f*Etot + 0.80f* *Etot_lp;  */
     427        3094 :         hNoiseEst->Etot_lp_fx = mac_r( L_mult( 6554, Etot ), 26214, hNoiseEst->Etot_lp_fx );
     428        3094 :         move16();
     429             : 
     430        3094 :         hNoiseEst->Etot_h_fx = sub( hNoiseEst->Etot_h_fx, 10 );
     431        3094 :         move16(); /* 10=0.04 in  Q8 */
     432        3094 :         hNoiseEst->Etot_h_fx = s_max( hNoiseEst->Etot_h_fx, Etot );
     433        3094 :         move16();
     434        3094 :         hNoiseEst->Etot_l_fx = add( hNoiseEst->Etot_l_fx, 20 );
     435        3094 :         move16(); /* 20 =  .08 in Q8 */
     436             : 
     437             : 
     438             :         /* Could even be higher but it also delays first entry to DTX */
     439        3094 :         IF( GT_16( hNoiseEst->harm_cor_cnt, HE_LT_CNT_PRE_FX ) )
     440             :         {
     441           0 :             test();
     442           0 :             IF( ( LT_16( ini_frame_fx, s_min( HE_LT_CNT_INIT_FX, MAX_FRAME_COUNTER - 1 ) ) ) && ( LT_16( sub( hNoiseEst->Etot_h_fx, hNoiseEst->Etot_lp_fx ), (Word16) 3 * 256 ) ) /* 3.0 Q8 */
     443             :             )
     444             :             {
     445             :                 /* *Etot_l += min(2,(*Etot_last-*Etot_l)*0.1f); */
     446           0 :                 tmp = mult_r( sub( hNoiseEst->Etot_last_fx, hNoiseEst->Etot_l_fx ), 3277 ); /* factor in Q15 3277  .1*32768 */
     447           0 :                 tmp = s_min( 512, tmp );                                                    /* 2.0 in Q8 is 512*/
     448           0 :                 hNoiseEst->Etot_l_fx = add( hNoiseEst->Etot_l_fx, tmp );
     449           0 :                 move16(); /* Q8 */
     450             :             }
     451             : 
     452             :             /* Avoids large steps in short active segments */
     453           0 :             test();
     454           0 :             IF( ( GT_16( sub( hNoiseEst->Etot_last_fx, hNoiseEst->Etot_l_fx ), HE_LT_THR2_FX ) ) /* 30.0f*Q8 */
     455             :                 && ( GT_16( hNoiseEst->harm_cor_cnt, HE_LT_CNT_PRE_SHORT_FX ) ) )
     456             :             {
     457             :                 /* *Etot_l += (*Etot_last-*Etot_l)*0.02f; */
     458           0 :                 hNoiseEst->Etot_l_fx = add( hNoiseEst->Etot_l_fx, mult_r( sub( hNoiseEst->Etot_last_fx, hNoiseEst->Etot_l_fx ), 655 ) );
     459           0 :                 move16(); /* 0.02 = 655  Q8*/
     460             :             }
     461           0 :             ELSE IF( GT_16( sub( hNoiseEst->Etot_last_fx, hNoiseEst->Etot_l_fx ), HE_LT_THR1_FX ) ) /* 10.0 in Q8*/
     462             :             {
     463           0 :                 hNoiseEst->Etot_l_fx = add( hNoiseEst->Etot_l_fx, 20 );
     464           0 :                 move16(); /* 0.08 is 20  in Q8*/
     465             :             }
     466             :         }
     467             : 
     468        3094 :         hNoiseEst->Etot_l_fx = s_min( hNoiseEst->Etot_l_fx, Etot );
     469        3094 :         move16();
     470        3094 :         test();
     471        3094 :         IF( LT_16( ini_frame_fx, 100 ) && LT_16( hNoiseEst->Etot_l_fx, hNoiseEst->Etot_l_lp_fx ) )
     472             :         {
     473             :             /**Etot_l_lp = 0.1f * *Etot_l + (1.0f - 0.1) * *Etot_l_lp; */
     474          11 :             hNoiseEst->Etot_l_lp_fx = mac_r( L_mult( 3277, hNoiseEst->Etot_l_fx ), 29491, hNoiseEst->Etot_l_lp_fx );
     475          11 :             move16();
     476             :         }
     477             :         ELSE
     478             :         {
     479        3083 :             test();
     480        3083 :             test();
     481        3083 :             test();
     482        3083 :             test();
     483        3083 :             IF( ( ( GT_16( hNoiseEst->harm_cor_cnt, HE_LT_CNT_FX ) ) && ( GT_16( sub_sat( hNoiseEst->Etot_last_fx, hNoiseEst->Etot_l_fx ), HE_LT_THR2_FX ) ) ) || ( ( sub( hNoiseEst->harm_cor_cnt, HE_LT_CNT_FX ) > 0 ) && ( LT_16( ini_frame_fx, HE_LT_CNT_INIT_FX ) ) ) || ( GT_16( sub_sat( hNoiseEst->Etot_l_lp_fx, hNoiseEst->Etot_l_fx ), HE_LT_THR2_FX ) ) )
     484             :             {
     485             :                 /**Etot_l_lp = 0.03f * *Etot_l + (1.0f - 0.03f) * *Etot_l_lp; */
     486           0 :                 hNoiseEst->Etot_l_lp_fx = mac_r( L_mult( 983, hNoiseEst->Etot_l_fx ), 31785, hNoiseEst->Etot_l_lp_fx );
     487           0 :                 move16();
     488             :             }
     489             :             ELSE
     490             :             {
     491             :                 /*  *Etot_l_lp = 0.02f * *Etot_l + (1.0f - 0.02f) * *Etot_l_lp; */
     492        3083 :                 hNoiseEst->Etot_l_lp_fx = round_fx( L_mac( L_mult( 655, hNoiseEst->Etot_l_fx ), 32113, hNoiseEst->Etot_l_lp_fx ) );
     493        3083 :                 move16();
     494             :             }
     495             :         }
     496             :         /**sign_dyn_lp = 0.1f * (*Etot_h - *Etot_l) + (1.0f - 0.1f) * *sign_dyn_lp;*/
     497        3094 :         hNoiseEst->sign_dyn_lp_fx = round_fx( L_mac( L_mult( 3277, sub_sat( hNoiseEst->Etot_h_fx, hNoiseEst->Etot_l_fx ) ), 29491, hNoiseEst->sign_dyn_lp_fx ) );
     498        3094 :         move16();
     499             :     }
     500             : 
     501        3100 :     return;
     502             : }
     503             : 
     504     1192920 : void noise_est_pre_32fx(
     505             :     const Word32 Etot,             /* i  : Energy of current frame Q24 */
     506             :     const Word16 ini_frame_fx,     /* i  : Frame number (init)         */
     507             :     NOISE_EST_HANDLE hNoiseEst,    /* i/o: Noise estimation handle     */
     508             :     const Word16 idchan,           /* i  : channel ID                  */
     509             :     const Word16 element_mode,     /* i  : element mode                */
     510             :     const Word16 last_element_mode /* i  : last element mode           */
     511             : )
     512             : {
     513             :     Word32 tmp;
     514             :     Word32 Etot_h_32fx;
     515             :     Word32 Etot_l_32fx;
     516             :     Word32 Etot_lp_32fx;
     517             : 
     518     1192920 :     test();
     519     1192920 :     test();
     520     1192920 :     test();
     521     1192920 :     IF( LE_16( ini_frame_fx, 1 ) || ( EQ_16( idchan, 1 ) && EQ_16( element_mode, IVAS_CPE_TD ) && EQ_16( last_element_mode, IVAS_CPE_DFT ) ) )
     522             :     {
     523       16806 :         Etot_h_32fx = Etot;
     524       16806 :         move32();
     525       16806 :         Etot_l_32fx = Etot;
     526       16806 :         move32();
     527       16806 :         hNoiseEst->Etot_l_lp_32fx = Etot; // Q24
     528       16806 :         move32();
     529       16806 :         hNoiseEst->Etot_last_32fx = Etot; // Q24
     530       16806 :         move32();
     531       16806 :         hNoiseEst->Etot_v_h2_32fx = 0;
     532       16806 :         move32();
     533       16806 :         Etot_lp_32fx = Etot;
     534       16806 :         move32();
     535       16806 :         hNoiseEst->sign_dyn_lp_fx = 0;
     536       16806 :         move16();
     537             :     }
     538             :     ELSE
     539             :     {
     540             :         /* *Etot_lp = 0.20f*Etot + 0.80f* *Etot_lp;  */
     541     1176114 :         Etot_lp_32fx = L_add( Mpy_32_32( 429496730 /* 0.2 in Q31 */, Etot ), Mpy_32_32( 1717986918 /* 0.8 in Q31 */, L_deposit_h( hNoiseEst->Etot_lp_fx ) ) );
     542             : 
     543     1176114 :         Etot_h_32fx = L_sub( L_deposit_h( hNoiseEst->Etot_h_fx ), 671089 ); /* 671089=0.04 in  Q24 */
     544     1176114 :         Etot_h_32fx = L_max( Etot_h_32fx, Etot );
     545     1176114 :         Etot_l_32fx = L_add( hNoiseEst->Etot_l_32fx, 1342177 ); /* 1342177 =  .08 in Q24 */
     546             : 
     547             :         /* Could even be higher but it also delays first entry to DTX */
     548     1176114 :         IF( GT_16( hNoiseEst->harm_cor_cnt, HE_LT_CNT_PRE_FX ) )
     549             :         {
     550       53210 :             test();
     551       53210 :             IF( ( LT_16( ini_frame_fx, s_min( HE_LT_CNT_INIT_FX, MAX_FRAME_COUNTER - 1 ) ) ) && ( LT_32( L_sub( Etot_h_32fx, Etot_lp_32fx ), (Word32) 3 * ONE_IN_Q24 ) ) /* 3.0 Q24 */
     552             :             )
     553             :             {
     554             :                 /* *Etot_l += min(2,(*Etot_last-*Etot_l)*0.1f); */
     555        9032 :                 tmp = Mpy_32_32( L_sub( ( hNoiseEst->Etot_last_32fx ), Etot_l_32fx ), 214748365 ); /* 0.1f factor in Q31 */
     556        9032 :                 tmp = L_min( 33554432, tmp );                                                      /* 2.0 in Q24 is 33554432 */
     557        9032 :                 Etot_l_32fx = L_add( Etot_l_32fx, tmp );                                           /* Q24 */
     558             :             }
     559             : 
     560             :             /* Avoids large steps in short active segments */
     561       53210 :             test();
     562       53210 :             IF( ( GT_32( L_sub( hNoiseEst->Etot_last_32fx, Etot_l_32fx ), 503316480 ) ) /* 30.0f*Q24 */
     563             :                 && ( GT_16( hNoiseEst->harm_cor_cnt, HE_LT_CNT_PRE_SHORT_FX ) ) )
     564             :             {
     565             :                 /* *Etot_l += (*Etot_last-*Etot_l)*0.02f; */
     566           0 :                 Etot_l_32fx = L_add( Etot_l_32fx, Mpy_32_32( L_sub( hNoiseEst->Etot_last_32fx, Etot_l_32fx ), 42949673 ) ); /* 0.02 in Q24*/
     567             :             }
     568       53210 :             ELSE IF( GT_32( L_sub( hNoiseEst->Etot_last_32fx, Etot_l_32fx ), 167772160 ) ) /* 10.0 in Q24*/
     569             :             {
     570        2265 :                 Etot_l_32fx = L_add( Etot_l_32fx, 1342177 ); /* 0.08 in Q24*/
     571             :             }
     572             :         }
     573             : 
     574     1176114 :         Etot_l_32fx = L_min( Etot_l_32fx, Etot ); // Q24
     575             : 
     576     1176114 :         test();
     577     1176114 :         IF( LT_16( ini_frame_fx, 100 ) && LT_32( Etot_l_32fx, ( hNoiseEst->Etot_l_lp_32fx ) ) )
     578             :         {
     579             :             /**Etot_l_lp = 0.1f * *Etot_l + (1.0f - 0.1) * *Etot_l_lp; */
     580       82132 :             hNoiseEst->Etot_l_lp_32fx = W_round64_L( W_add( W_mult_32_32( 214748364 /* 0.1f in  Q31*/, Etot_l_32fx ), W_mult_32_32( 1932735283 /* 0.9f in  Q31*/, hNoiseEst->Etot_l_lp_32fx ) ) ); // Q8
     581       82132 :             move16();
     582             :         }
     583             :         ELSE
     584             :         {
     585     1093982 :             test();
     586     1093982 :             test();
     587     1093982 :             test();
     588     1093982 :             test();
     589     1093982 :             IF( ( ( GT_16( hNoiseEst->harm_cor_cnt, HE_LT_CNT_FX ) ) && ( GT_32( L_sub_sat( hNoiseEst->Etot_last_32fx, Etot_l_32fx ), HE_LT_THR2_Q24 ) ) ) || ( ( sub( hNoiseEst->harm_cor_cnt, HE_LT_CNT_FX ) > 0 ) && ( LT_16( ini_frame_fx, HE_LT_CNT_INIT_FX ) ) ) || ( GT_32( L_sub_sat( hNoiseEst->Etot_l_lp_32fx, Etot_l_32fx ), HE_LT_THR2_Q24 ) ) )
     590             :             {
     591             :                 /**Etot_l_lp = 0.03f * *Etot_l + (1.0f - 0.03f) * *Etot_l_lp; */
     592       27571 :                 hNoiseEst->Etot_l_lp_32fx = W_extract_h( W_add( W_mult_32_32( 64424509 /* 0.03f in  Q31*/, Etot_l_32fx ), W_mult_32_32( 2083059139 /* 0.97f in  Q31*/, hNoiseEst->Etot_l_lp_32fx ) ) ); // Q24
     593       27571 :                 move16();
     594             :             }
     595             :             ELSE
     596             :             {
     597             :                 /*  *Etot_l_lp = 0.02f * *Etot_l + (1.0f - 0.02f) * *Etot_l_lp; */
     598     1066411 :                 hNoiseEst->Etot_l_lp_32fx = W_extract_h( W_add( W_mult_32_32( 42949673 /* 0.02f in  Q31*/, Etot_l_32fx ), W_mult_32_32( 2104533975 /* 0.98f in  Q31*/, hNoiseEst->Etot_l_lp_32fx ) ) ); // Q24
     599     1066411 :                 move16();
     600             :             }
     601             :         }
     602             :         /**sign_dyn_lp = 0.1f * (*Etot_h - *Etot_l) + (1.0f - 0.1f) * *sign_dyn_lp;*/
     603     1176114 :         hNoiseEst->sign_dyn_lp_fx = extract_h( L_add( Mpy_32_32( 214748365 /* 0.1f in  Q31*/, L_sub_sat( Etot_h_32fx, Etot_l_32fx ) ), Mpy_32_32( 1932735283 /*( 1.0f - 0.1f ) in  Q31*/, L_deposit_h( hNoiseEst->sign_dyn_lp_fx ) ) ) ); // Q8
     604     1176114 :         move16();
     605             :     }
     606             : 
     607     1192920 :     hNoiseEst->Etot_l_32fx = Etot_l_32fx;              // Q24
     608     1192920 :     hNoiseEst->Etot_h_fx = extract_h( Etot_h_32fx );   // Q8
     609     1192920 :     hNoiseEst->Etot_lp_fx = extract_h( Etot_lp_32fx ); // Q8
     610     1192920 :     move32();
     611     1192920 :     move16();
     612     1192920 :     move16();
     613             : 
     614     1192920 :     return;
     615             : }
     616             : /*==================================================================================*/
     617             : /* FUNCTION : noise_est_down_fx()                                                   */
     618             : /*----------------------------------------------------------------------------------*/
     619             : /* PURPOSE :   Down-ward noise udatation routine                                    */
     620             : /*----------------------------------------------------------------------------------*/
     621             : /*  INPUT ARGUMENTS :                                                               */
     622             : /* _ (Word32[]) fr_bands   : per band input energy                     Q_new+QSCALE */
     623             : /* _ (Word32[]) bckr       : per band background noise energy estimate Q_new+QSCALE */
     624             : /* _ (Word16  ) min_band   : minimum critical band                               Q0 */
     625             : /* _ (Word16  ) max_band   : maximum critical band                               Q0 */
     626             : /* _ (Word32[]) bckr_he    : per band background noise energy estimate Q_new+QSCALE */
     627             : /* _ (Word16  ) Etot       : Energy of current frame                             Q8 */
     628             : /* _ (Word16* ) Etot_last  : Energy of last frame                                Q8 */
     629             : /* _ (Word16* ) Etot_v_h2   : Energy variaions of noise frames                   Q8 */
     630             : /* _ (Word16  ) Q_new      : Qformat                                                */
     631             : /*----------------------------------------------------------------------------------*/
     632             : /* OUTPUT ARGUMENTS :                                                               */
     633             : /* _ (Word32[]) bckr       : per band background noise energy estimate Q_new+QSCALE */
     634             : /* _ (Word32[]) tmpN       : temporary noise update                    Q_new+QSCALE */
     635             : /* _ (Word32[]) enr        : averaged energy over both subframes       Q_new+QSCALE */
     636             : /* _ (Word16* ) totalNoise : noise estimate over all critical bands              Q8 */
     637             : /* _ (Word16  ) Etot       : Energy of current frame                             Q8 */
     638             : /* _ (Word16* ) Etot_last  : Energy of last frame                                Q8 */
     639             : /* _ (Word16* ) Etot_v_h2   : Energy variaions of noise frames                   Q8 */
     640             : /* _ (Word32[]) tmpN_he1   : temporary noise update 1                  Q_new+QSCALE */
     641             : /*----------------------------------------------------------------------------------*/
     642             : 
     643             : 
     644        3100 : void noise_est_down_fx(
     645             :     const Word32 fr_bands[], /* i  : per band input energy (contains 2 vectors) */
     646             :     Word32 bckr[],           /* i/o: per band background noise energy estimate  */
     647             :     Word32 tmpN[],           /* o  : temporary noise update                     */
     648             :     Word32 enr[],            /* o  : averaged energy over both subframes        */
     649             :     const Word16 min_band,   /* i  : minimum critical band                      */
     650             :     const Word16 max_band,   /* i  : maximum critical band                      */
     651             :     Word16 *totalNoise,      /* o  : noise estimate over all critical bands     */
     652             :     Word16 Etot,             /* i  : Energy of current frame                    */
     653             :     Word16 *Etot_last,       /* i/o: Energy of last frame            Q8         */
     654             :     Word16 *Etot_v_h2,       /* i/o: Energy variations of noise frames  Q8      */
     655             :     Word16 Q_new,
     656             :     const Word32 e_min /* i  : minimum energy scaled    Q_new + QSCALE    */
     657             : )
     658             : 
     659             : {
     660             :     Word32 Ltmp, L_tmp;
     661             :     const Word32 *pt1, *pt2;
     662             :     Word16 i;
     663             :     Word16 e_Noise, f_Noise;
     664             :     Word16 scale;
     665             :     Word32 totalNoise_temp;
     666             :     Word32 L_Etot, L_Etot_last, L_Etot_v_h2, L_Etot_v;
     667             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     668        3100 :     Flag Overflow = 0;
     669             : #endif
     670             : 
     671        3100 :     L_Etot = L_shl( Etot, 16 ); /*Q24 for later AR1 computations*/
     672        3100 :     L_Etot_last = L_shl( *Etot_last, 16 );
     673        3100 :     L_Etot_v_h2 = L_shl( *Etot_v_h2, 16 );
     674        3100 :     scale = add( Q_new, QSCALE );
     675        3100 :     move16();
     676             : 
     677             :     /*-----------------------------------------------------------------*
     678             :      * Estimate total noise energy
     679             :      *-----------------------------------------------------------------*/
     680             : 
     681        3100 :     totalNoise_temp = L_deposit_l( 0 );
     682       65100 :     FOR( i = min_band; i <= max_band; i++ )
     683             :     {
     684       62000 :         totalNoise_temp = L_add_o( totalNoise_temp, bckr[i], &Overflow ); /*Q_new+QSCALE*/
     685             :     }
     686        3100 :     totalNoise_temp = L_max( totalNoise_temp, L_shl( e_min, 4 ) );
     687             : 
     688             : 
     689        3100 :     totalNoise_temp = L_max( totalNoise_temp, (Word32) 1L ); /* make sure log2_norm_lc does not cause table reading out of bounds */
     690             : 
     691             :     /*totalNoise = 10.0f * (float)log10( *totalNoise );*/
     692        3100 :     e_Noise = norm_l( totalNoise_temp );
     693        3100 :     f_Noise = Log2_norm_lc( L_shl( totalNoise_temp, e_Noise ) );
     694        3100 :     e_Noise = sub( 30, e_Noise );
     695        3100 :     e_Noise = sub( e_Noise, scale );
     696        3100 :     Ltmp = Mpy_32_16( e_Noise, f_Noise, LG10 );
     697        3100 :     *totalNoise = round_fx( L_shl( Ltmp, 10 ) ); /*Q8*/
     698        3100 :     move16();
     699             :     /*-----------------------------------------------------------------*
     700             :      * Average energy per frame for each frequency band
     701             :      *-----------------------------------------------------------------*/
     702             : 
     703        3100 :     pt1 = fr_bands; /*Q_new+QSCALE*/
     704        3100 :     pt2 = fr_bands + NB_BANDS;
     705             : 
     706       65100 :     FOR( i = 0; i < NB_BANDS; i++ )
     707             :     {
     708       62000 :         Ltmp = L_add_o( L_shr_r( *pt1, 1 ), L_shr_r( *pt2, 1 ), &Overflow );
     709             :         /*Ltmp = L_shr_r(L_add(*pt1,*pt2),1);*/
     710       62000 :         enr[i] = Ltmp;
     711       62000 :         move32(); /*Q_new+QSCALE*/
     712       62000 :         pt1++;
     713       62000 :         pt2++;
     714             :     }
     715             : 
     716             :     /*-----------------------------------------------------------------*
     717             :      * Background noise energy update
     718             :      *-----------------------------------------------------------------*/
     719             : 
     720       65100 :     FOR( i = 0; i < NB_BANDS; i++ )
     721             :     {
     722             :         /* tmpN[i] = (1-ALPHA) * bckr[i] + ALPHA * enr[i]; */
     723       62000 :         L_tmp = Mult_32_16( enr[i], ALPHA_FX ); /*ALPHA * enr2*/
     724       62000 :         tmpN[i] = Madd_32_16( L_tmp, bckr[i], ALPHAM1_FX );
     725       62000 :         move32();                          /*Q_new+QSCALE*/
     726       62000 :         tmpN[i] = L_max( tmpN[i], e_min ); /* handle  div by zero in find_tilt_fx  */
     727       62000 :         move32();
     728             :         /*  if( tmpN[i] < bckr[i] ) { bckr[i] = tmpN[i];  }*/
     729             :         /* Defend to increase noise estimate: keep as it is or decrease  */
     730       62000 :         bckr[i] = L_max( L_min( bckr[i], tmpN[i] ), e_min );
     731       62000 :         move32(); /*Q_new+QSCALE*/
     732             :     }
     733             : 
     734             :     /*------------------------------------------------------------------*
     735             :      * Energy variation update
     736             :      *------------------------------------------------------------------*/
     737             :     /*Etot_v = (float) fabs(*Etot_last - Etot);*/
     738        3100 :     L_Etot_v = L_abs( L_sub( L_Etot_last, L_Etot ) ); /* Q24 */
     739             : 
     740             :     /* *Etot_v_h2 = (1.0f-0.02f) * *Etot_v_h2 + 0.02f * min(3.0f, Etot_v); */
     741        3100 :     L_Etot_v_h2 = Mult_32_16( L_Etot_v_h2, 32113 ); /*.98 in Q15 , Q24+Q15+1 -16 => Q24 */
     742        3100 :     L_Etot_v = L_min( (Word32) ( 3 * ( 1 << 24 ) ), L_Etot_v );
     743        3100 :     L_tmp = Mult_32_16( L_Etot_v, 655 ); /*.02 in Q15  , Q24+Q15+1 -16 ==> Q24 ) */
     744             : 
     745        3100 :     *Etot_v_h2 = round_fx( L_add( L_Etot_v_h2, L_tmp ) ); /*Q24->Q8*/
     746        3100 :     move16();
     747             :     /* if (*Etot_v_h2 < 0.1f) {  *Etot_v_h2 = 0.1f; } */
     748        3100 :     *Etot_v_h2 = s_max( *Etot_v_h2, 26 ); /* 0.1 in Q8*/
     749        3100 :     move16();
     750        3100 :     return;
     751             : }
     752             : 
     753     1192920 : void noise_est_down_ivas_fx(
     754             :     const Word32 fr_bands[], /* i  : per band input energy (contains 2 vectors) q_fr_bands */
     755             :     const Word16 q_fr_bands, /* i  : Q of fr_bands                                         */
     756             :     Word32 bckr[],           /* i/o: per band background noise energy estimate  q_fr_bands */
     757             :     Word16 *q_bckr,
     758             :     Word32 tmpN[], /* o  : temporary noise update                     q_fr_bands */
     759             :     Word16 *q_tmpN,
     760             :     Word32 enr[], /* o  : averaged energy over both subframes                   */
     761             :     Word16 *q_enr,
     762             :     const Word16 min_band, /* i  : minimum critical band                                 */
     763             :     const Word16 max_band, /* i  : maximum critical band                                 */
     764             :     Word32 *totalNoise,    /* o  : noise estimate over all critical bands                */
     765             :     Word32 Etot,           /* i  : Energy of current frame                            Q24*/
     766             :     Word32 *Etot_last,     /* i/o: Energy of last frame            Q24                   */
     767             :     Word32 *Etot_v_h2      /* i/o: Energy variations of noise frames  Q24                */
     768             : )
     769             : 
     770             : {
     771             :     Word32 Ltmp, L_tmp;
     772             :     const Word32 *pt1, *pt2;
     773             :     Word16 i;
     774             :     Word16 e_Noise, f_Noise;
     775             :     Word32 totalNoise_temp;
     776             :     Word32 L_Etot, L_Etot_last, L_Etot_v_h2, L_Etot_v;
     777             :     Word64 sum;
     778             :     Word16 q_sum;
     779             :     Word32 enr32[NB_BANDS], bckr32[NB_BANDS], tmpN32[NB_BANDS];
     780             :     Word16 enr_q[NB_BANDS], bckr_q[NB_BANDS], tmpN_q[NB_BANDS];
     781             :     Word16 shift, shift1, shift2, shift3;
     782             :     Word64 tmpN64, tmp, enr64;
     783             : 
     784     1192920 :     Copy32( bckr, bckr32, NB_BANDS );
     785     1192920 :     set16_fx( bckr_q, *q_bckr, NB_BANDS );
     786             : 
     787     1192920 :     L_Etot = Etot; /*Q24 for later AR1 computations*/
     788     1192920 :     move32();
     789     1192920 :     L_Etot_last = *Etot_last;
     790     1192920 :     L_Etot_v_h2 = *Etot_v_h2;
     791             : 
     792             :     /*-----------------------------------------------------------------*
     793             :      * Estimate total noise energy
     794             :      *-----------------------------------------------------------------*/
     795             : 
     796     1192920 :     totalNoise_temp = L_deposit_l( 0 );
     797     1192920 :     sum = 0;
     798     1192920 :     move64();
     799    25034264 :     FOR( i = min_band; i <= max_band; i++ )
     800             :     {
     801    23841344 :         sum = W_mac_32_16( sum, bckr[i], 1 ); // q_fr_bands+1
     802             :     }
     803     1192920 :     q_sum = add( *q_bckr, 1 );
     804     1192920 :     IF( sum == 0 )
     805             :     {
     806        5498 :         sum = W_mult0_32_32( E_MIN_FXQ31, add( sub( max_band, min_band ), 1 ) ); // Q31
     807        5498 :         q_sum = Q31;
     808        5498 :         move16();
     809             :     }
     810     1192920 :     e_Noise = W_norm( sum );
     811     1192920 :     totalNoise_temp = W_extract_h( W_shl( sum, e_Noise ) ); // q_sum+e_Noise-32
     812     1192920 :     e_Noise = sub( 63, add( e_Noise, q_sum ) );             // 31-(q_sum+e_Noise-32)
     813             : 
     814             :     /*totalNoise = 10.0f * (float)log10( *totalNoise );*/
     815     1192920 :     f_Noise = Log2_norm_lc( totalNoise_temp );          // exponent of log => 30-0 = 30
     816     1192920 :     e_Noise = sub( e_Noise, 1 );                        // 30-(31-e_Noise) = e_Noise-1
     817     1192920 :     Ltmp = L_mac( L_deposit_h( e_Noise ), f_Noise, 1 ); // Q16
     818     1192920 :     Ltmp = Mpy_32_16_1( Ltmp, LG10 );                   // Q14 (16+13-15)
     819     1192920 :     Ltmp = L_shl( Ltmp, 10 );                           // Q26
     820     1192920 :     *totalNoise = ( Ltmp );                             /*Q24*/
     821     1192920 :     move32();
     822             : 
     823             :     /*-----------------------------------------------------------------*
     824             :      * Average energy per frame for each frequency band
     825             :      *-----------------------------------------------------------------*/
     826             : 
     827     1192920 :     pt1 = fr_bands;
     828     1192920 :     pt2 = fr_bands + NB_BANDS;
     829             : 
     830    25051320 :     FOR( i = 0; i < NB_BANDS; i++ )
     831             :     {
     832             :         /* enr[i] = 0.5f * ( *pt1++ + *pt2++ ); */
     833    23858400 :         enr64 = W_mac_32_32( W_mult_32_32( *pt1, ONE_IN_Q29 ), *pt2, ONE_IN_Q29 ); // q_fr_bands + 1 + Q29 + 1 (0.5 handle here)
     834    23858400 :         shift = W_norm( enr64 );
     835    23858400 :         enr32[i] = W_extract_h( W_shl( enr64, shift ) );
     836    23858400 :         move32();
     837    23858400 :         enr_q[i] = sub( add( q_fr_bands, shift ), 1 );
     838    23858400 :         move16();
     839    23858400 :         pt1++;
     840    23858400 :         pt2++;
     841             :     }
     842             : 
     843             :     /*-----------------------------------------------------------------*
     844             :      * Background noise energy update
     845             :      *-----------------------------------------------------------------*/
     846    25051320 :     FOR( i = 0; i < NB_BANDS; i++ )
     847             :     {
     848             :         /* tmpN[i] = (1-ALPHA) * bckr[i] + ALPHA * enr[i]; */
     849             :         /* handle  div by zero in find_tilt_fx  */
     850             : 
     851    23858400 :         tmpN64 = W_mult_32_16( bckr32[i], ALPHAM1_FX );
     852    23858400 :         tmp = W_mult_32_16( enr32[i], ALPHA_FX );
     853             : 
     854    23858400 :         shift = s_min( bckr_q[i], enr_q[i] );
     855    23858400 :         tmpN64 = W_add( W_shl( tmpN64, sub( shift, bckr_q[i] ) ), W_shl( tmp, sub( shift, enr_q[i] ) ) ); // shift + q16
     856    23858400 :         shift1 = W_norm( tmpN64 );
     857    23858400 :         tmpN32[i] = W_extract_h( W_shl( tmpN64, shift1 ) ); // shift + q16 + shift1 - 32
     858    23858400 :         move32();
     859    23858400 :         tmpN_q[i] = sub( add( add( Q16, shift ), shift1 ), 32 );
     860    23858400 :         move16();
     861             : 
     862    23858400 :         IF( GT_32( E_MIN_FXQ31, L_shl_sat( tmpN32[i], sub( 31, tmpN_q[i] ) ) ) )
     863             :         {
     864     2160563 :             tmpN32[i] = E_MIN_FXQ31;
     865     2160563 :             tmpN_q[i] = 31;
     866     2160563 :             move32();
     867     2160563 :             move16();
     868             :         }
     869             : 
     870    23858400 :         IF( GT_32( bckr32[i], L_shl_sat( tmpN32[i], sub( bckr_q[i], tmpN_q[i] ) ) ) )
     871             :         {
     872     1543543 :             bckr32[i] = tmpN32[i]; /* Defend to increase noise estimate: keep as it is or decrease  */
     873     1543543 :             bckr_q[i] = tmpN_q[i];
     874     1543543 :             move32();
     875     1543543 :             move16();
     876             :         }
     877             :     }
     878             : 
     879             :     /* Scaling to common Q*/
     880     1192920 :     shift1 = bckr_q[0], shift2 = enr_q[0], shift3 = tmpN_q[0];
     881     1192920 :     move16();
     882     1192920 :     move16();
     883     1192920 :     move16();
     884             : 
     885    23858400 :     FOR( i = 1; i < NB_BANDS; i++ )
     886             :     {
     887    22665480 :         shift1 = s_min( shift1, bckr_q[i] );
     888    22665480 :         shift2 = s_min( shift2, enr_q[i] );
     889    22665480 :         shift3 = s_min( shift3, tmpN_q[i] );
     890             :     }
     891    25051320 :     FOR( i = 0; i < NB_BANDS; i++ )
     892             :     {
     893    23858400 :         bckr[i] = L_shl( bckr32[i], sub( shift1, bckr_q[i] ) );
     894    23858400 :         enr[i] = L_shl( enr32[i], sub( shift2, enr_q[i] ) );
     895    23858400 :         tmpN[i] = L_shl( tmpN32[i], sub( shift3, tmpN_q[i] ) );
     896    23858400 :         move32();
     897    23858400 :         move32();
     898    23858400 :         move32();
     899             :     }
     900     1192920 :     *q_bckr = shift1;
     901     1192920 :     *q_enr = shift2;
     902     1192920 :     *q_tmpN = shift3;
     903     1192920 :     move16();
     904     1192920 :     move16();
     905     1192920 :     move16();
     906             :     /*------------------------------------------------------------------*
     907             :      * Energy variation update
     908             :      *------------------------------------------------------------------*/
     909             :     /*Etot_v = (float) fabs(*Etot_last - Etot);*/
     910     1192920 :     L_Etot_v = L_abs( L_sub( L_Etot_last, L_Etot ) ); /* Q24 */
     911             : 
     912             :     /* *Etot_v_h2 = (1.0f-0.02f) * *Etot_v_h2 + 0.02f * min(3.0f, Etot_v); */
     913     1192920 :     L_tmp = L_min( 50331648 /* 3.0f in Q24 */, L_Etot_v );                   // Q24
     914     1192920 :     L_tmp = Mult_32_16( L_tmp, 655 /*.02 in Q15 */ );                        // Q24
     915     1192920 :     L_Etot_v_h2 = Madd_32_16( L_tmp, L_Etot_v_h2, 32113 /* 0.98 in Q15 */ ); // Q24
     916             : 
     917             :     /* if (*Etot_v_h2 < 0.1f) {  *Etot_v_h2 = 0.1f; } */
     918     1192920 :     *Etot_v_h2 = L_max( L_Etot_v_h2, 1677722 /* 0.1 in Q24*/ ); // Q24
     919     1192920 :     move16();
     920             : 
     921     1192920 :     return;
     922             : }
     923             : 
     924             : /*-----------------------------------------------------------------*
     925             :  * noise_est_fx()
     926             :  *
     927             :  * Noise energy estimation (noise energy is updated in case of noise-only frame)
     928             :  *-----------------------------------------------------------------*/
     929        3100 : void noise_est_fx(
     930             :     Encoder_State *st_fx,       /* i/o: state structure                                                     */
     931             :     const Word16 old_pitch1,    /* i  : previous frame OL pitch[1]                                          */
     932             :     const Word32 tmpN[],        /* i  : temporary noise update                            Q_new + QSCALE    */
     933             :     const Word16 epsP_h[],      /* i  : msb prediction error energies                        Q_r-1          */
     934             :     const Word16 epsP_l[],      /* i  : msb prediction error energies                        Q_r-1          */
     935             :     const Word16 Etot,          /* i  : total channel E (see find_enr_fx.c)                  Q8             */
     936             :     const Word16 relE,          /* i  : (VA_CHECK addition) relative frame energy            Q8?            */
     937             :     const Word16 corr_shift,    /* i  : normalized correlation correction                    Q15            */
     938             :     const Word32 enr[],         /* i  : averaged energy over both subframes               Q_new + Q_SCALE   */
     939             :     Word32 fr_bands[],          /* i  : spectrum per critical bands of the current frame  Q_new + Q_SCALE   */
     940             :     Word16 *cor_map_sum,        /* o  :                                                         Q8          */
     941             :     Word16 *ncharX,             /* o  : IVAS_CODE -> size of ncharX needs validation  noise character for sp/mus classifier               Qx?             */
     942             :     Word16 *sp_div,             /* o  :                                                      Q_sp_div       */
     943             :     Word16 *Q_sp_div,           /* o  :    Q factor for sp_div                                              */
     944             :     Word16 *non_staX,           /* o  : non-stationarity for sp/mus classifier                              */
     945             :     Word16 *loc_harm,           /* o  :   multi-harmonicity flag for UV classifier                          */
     946             :     const Word32 *lf_E,         /* i  : per bin energy  for low frequencies             Q_new + Q_SCALE -2  */
     947             :     Word16 *st_harm_cor_cnt,    /* i/o  : 1st harm correlation timer                         Q0             */
     948             :     const Word16 Etot_l_lp,     /* i    : Smoothed low energy                                Q8             */
     949             :     const Word16 Etot_v_h2,     /* i    : Energy variations                                  Q8             */
     950             :     Word16 *bg_cnt,             /* i    : Background burst length timer                      Q0             */
     951             :     Word16 EspecdB[],           /* i/o:  log E spectrum (with f=0) of the current frame Q7  for multi harm  */
     952             :     Word16 Q_new,               /* i  : SCaling of current frame                                            */
     953             :     const Word32 Le_min_scaled, /*i  : Minimum energy value     in  Q_new + Q_SCALE                         */
     954             :     Word16 *sp_floor,           /* o  : noise floor estimate                                 Q7             */
     955             :     Word16 S_map[],             /* o  : short-term correlation map                           Q7             */
     956             :     const Word16 ini_frame      /* i  : Frame number (init)                                                */
     957             : )
     958             : {
     959             :     Word16 alpha, alpha2, alpha2m1, alpham1;
     960             :     Word16 cor_min, cor_max, num, den, ExpNum, ExpDen, noise_chartmp;
     961             :     Word16 wtmp1, wtmp, ExpLmax, ExpLmax2, tmpExp, nchar_thr, cor_tmp;
     962             :     Word16 i, tmp_pc, pc, th_eps;
     963             :     Word32 th_sta, Lnum, Lden, non_sta, LepsP, Ltmpden;
     964             :     Word16 e_ener, f_ener;
     965             :     Word32 Ltmp, Ltmp1, Lsum_num, Lsum_den, *pt1, *pt2, Ltmp2, Lnon_sta2;
     966             :     Word16 spec_div, noise_char;
     967             :     Word16 log_enr16;
     968             :     Word16 updt_step; /* Q15 */
     969             :     Word16 aE_bgd, sd1_bgd, bg_bgd2;
     970             :     Word16 tn_ini;
     971             :     Word16 epsP_0_2, epsP_0_2_ad, epsP_0_2_ad_lp_max;
     972             :     Word16 epsP_2_16, epsP_2_16_dlp, epsP_2_16_dlp_max;
     973             :     Word16 PAU, BG_1, NEW_POS_BG;
     974             : 
     975             :     Word16 haco_ev_max;
     976             :     Word16 Etot_l_lp_thr;
     977             :     Word16 comb_ahc_epsP, comb_hcm_epsP;
     978             : 
     979             :     Word16 enr_bgd, cns_bgd, lp_bgd, ns_mask;
     980             :     Word16 lt_haco_mask, bg_haco_mask;
     981             :     Word16 SD_1, SD_1_inv, bg_bgd3, PD_1, PD_2, PD_3, PD_4, PD_5;
     982             : 
     983             :     Word16 non_staB; /* Q8 */
     984             :     Word32 L_tmp_enr, L_tmp_ave_enr, L_tmp_ave_enr2;
     985             :     Word16 tmp_Q;
     986             :     Word16 tmp, tmp2;          /* general temp registers */
     987             :     Word16 tmp_enr, tmp_floor; /* constants in Q8 */
     988             :     Word16 vad_bwidth_fx;      /* vad ns  control variabel for input bwidth from teh BWD  */
     989             :     /* for DTX operation */
     990             :     Word16 vad_2nd_stage_fx;
     991             : 
     992             :     Word16 lim_Etot_fx;     /* Q8 */
     993             :     Word16 lim_Etot_sq_fx;  /* Q2 */
     994             :     Word16 st_E_var_est_fx; /* Q2 */
     995             :     NOISE_EST_HANDLE hNoiseEst;
     996             :     SP_MUS_CLAS_HANDLE hSpMusClas;
     997        3100 :     hSpMusClas = st_fx->hSpMusClas;
     998             :     (void) ( ncharX );
     999             : 
    1000             :     /* Check if LR-VAD */
    1001             :     {
    1002        3100 :         hNoiseEst = st_fx->hNoiseEst;
    1003             :     }
    1004             : 
    1005        3100 :     GSC_ENC_HANDLE hGSCEnc = st_fx->hGSCEnc;
    1006             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    1007        3100 :     Flag Overflow = 0;
    1008        3100 :     move32();
    1009             : #endif
    1010             : 
    1011             :     /*-----------------------------------------------------------------*
    1012             :      * Initialization
    1013             :      *-----------------------------------------------------------------*/
    1014        3100 :     vad_bwidth_fx = st_fx->input_bwidth;
    1015        3100 :     move16();
    1016             : 
    1017             :     /*st_fx->ener_RAT = 10.0f * (float)log10( mean(lf_E, 8));*/
    1018             :     {
    1019        3100 :         IF( hSpMusClas != NULL )
    1020             :         {
    1021        3100 :             Ltmp = L_shr( lf_E[0], 3 );
    1022       24800 :             FOR( i = 1; i < 8; i++ )
    1023             :             {
    1024       21700 :                 Ltmp = L_add( Ltmp, L_shr( lf_E[i], 3 ) );
    1025             :             }
    1026        3100 :             IF( LT_32( Ltmp, L_shl( 1, add( Q_new, Q_SCALE - 2 ) ) ) )
    1027             :             {
    1028          78 :                 hSpMusClas->ener_RAT_fx = 0;
    1029          78 :                 move16();
    1030             :                 // PMT("hSpMusClas->ener_RAT_fx = 0, that should be validated")
    1031             :             }
    1032             :             ELSE
    1033             :             {
    1034        3022 :                 Ltmp = L_max( Ltmp, (Word32) 1L ); /* make sure log2_norm_lc does not cause table reading out of bounds */
    1035        3022 :                 e_ener = norm_l( Ltmp );
    1036        3022 :                 f_ener = Log2_norm_lc( L_shl( Ltmp, e_ener ) );
    1037        3022 :                 e_ener = sub( 30, e_ener );
    1038        3022 :                 e_ener = sub( e_ener, sub( add( Q_new, QSCALE ), 2 ) );
    1039        3022 :                 Ltmp = Mpy_32_16( e_ener, f_ener, LG10 );
    1040        3022 :                 wtmp = round_fx( L_shl( Ltmp, 10 ) ); /*Q8*/
    1041             : 
    1042             :                 /* st_fx->ener_RAT /= (Etot + 0.01f);   */
    1043        3022 :                 wtmp1 = add( Etot, 3 ); /*3 is 0.01 in Q8  */
    1044             :                 /*  st_fx->ener_RAT_fx = wtmp/wtmp1 */
    1045        3022 :                 hSpMusClas->ener_RAT_fx = 0;
    1046        3022 :                 move16();
    1047        3022 :                 IF( wtmp > 0 )
    1048             :                 {
    1049        3022 :                     hSpMusClas->ener_RAT_fx = 32767;
    1050        3022 :                     move16(); /*Q15*/
    1051        3022 :                     IF( GE_16( wtmp1, wtmp ) )
    1052             :                     {
    1053        3022 :                         hSpMusClas->ener_RAT_fx = div_s( wtmp, wtmp1 ); /*Q15*/ /* wtmp1 gte than wtmp */
    1054        3022 :                         move16();
    1055             :                     }
    1056             :                 }
    1057             :             }
    1058             :         }
    1059             :     }
    1060             :     /*-----------------------------------------------------------------*
    1061             :      * Set the threshold for eps & non_sta based on input sampling rate
    1062             :      * The reason is that in case of 8kHz sampling input, there is nothing
    1063             :      * between 4kHz-6.4kHz. In noisy conditions, this makes a fast
    1064             :      * transition even in noise-only parts, hence producing a "higher
    1065             :      * order" spectral envelope => the epsP ratio is much less effective.
    1066             :      *-----------------------------------------------------------------*/
    1067             : 
    1068        3100 :     IF( vad_bwidth_fx != NB ) /* WB input */
    1069             :     {
    1070        3100 :         th_eps = TH_EPS16_FX;
    1071        3100 :         move16(); /*Q11*/
    1072        3100 :         th_sta = TH_STA16_FX;
    1073        3100 :         move16(); /*Q10  */
    1074        3100 :         cor_min = COR_MIN16_FX;
    1075        3100 :         move16(); /*Q15*/
    1076        3100 :         cor_max = COR_MAX16_FX;
    1077        3100 :         move16(); /*Q15*/
    1078             :     }
    1079             :     ELSE /* NB input */
    1080             :     {
    1081           0 :         th_eps = TH_EPS8_FX;
    1082           0 :         move16(); /* Q11 */
    1083           0 :         th_sta = TH_STA8_FX;
    1084           0 :         move16(); /* Q10 */
    1085           0 :         cor_min = COR_MIN8_FX;
    1086           0 :         move16();
    1087           0 :         cor_max = COR_MAX8_FX;
    1088           0 :         move16();
    1089             :     }
    1090             : 
    1091             : 
    1092             :     /*-----------------------------------------------------------------*
    1093             :      * Estimation of pitch stationarity
    1094             :      *-----------------------------------------------------------------*/
    1095             : 
    1096             :     /* pc = abs(pit[0] - pitO) + abs(pit[1] - pit[0]) */ /* needed in signal_clas() */
    1097        3100 :     wtmp = abs_s( sub( st_fx->pitch[0], old_pitch1 ) );
    1098        3100 :     wtmp1 = abs_s( sub( st_fx->pitch[1], st_fx->pitch[0] ) );
    1099        3100 :     pc = add( wtmp, wtmp1 );
    1100             : 
    1101             : 
    1102        3100 :     Ltmp = L_deposit_h( corr_shift );
    1103        3100 :     Ltmp = L_mac( Ltmp, st_fx->voicing_fx[0], 10923 );
    1104        3100 :     Ltmp = L_mac_o( Ltmp, st_fx->voicing_fx[1], 10923, &Overflow );
    1105        3100 :     wtmp = mac_ro( Ltmp, st_fx->voicing_fx[2], 10923, &Overflow );
    1106             : 
    1107        3100 :     tmp_pc = pc;
    1108        3100 :     move16();
    1109        3100 :     if ( LT_16( wtmp, cor_min ) )
    1110             :     {
    1111         184 :         tmp_pc = TH_PC_FX;
    1112         184 :         move16(); /* low correlation -> probably inactive signal */
    1113             :     }
    1114             : 
    1115             :     /* Update */
    1116             : 
    1117             :     /*-----------------------------------------------------------------*
    1118             :      * Multi-harmonic analysis
    1119             :      *-----------------------------------------------------------------*/
    1120             : 
    1121             :     {
    1122        3100 :         IF( st_fx->hSpMusClas != NULL )
    1123             :         {
    1124        3100 :             i = 0;
    1125        3100 :             move16();
    1126        3100 :             *loc_harm = multi_harm_fx( EspecdB, hNoiseEst->old_S_fx, hNoiseEst->cor_map_fx, &hNoiseEst->multi_harm_limit_fx, st_fx->total_brate,
    1127        6200 :                                        st_fx->bwidth, ( st_fx->hGSCEnc != NULL ) ? &hGSCEnc->cor_strong_limit : &i, &hSpMusClas->mean_avr_dyn_fx, &hSpMusClas->last_sw_dyn_fx, cor_map_sum, sp_floor, S_map );
    1128        3100 :             move16();
    1129             :         }
    1130             :     }
    1131             :     /*-----------------------------------------------------------------*
    1132             :      * Detection of frames with non-stationary spectral content
    1133             :      *-----------------------------------------------------------------*/
    1134             : 
    1135             :     /* weighted sum of spectral changes per critical bands */
    1136        3100 :     Lsum_num = L_deposit_l( 0 );
    1137        3100 :     Lsum_den = L_deposit_l( 0 );
    1138             : 
    1139             :     /* Find a proper scaling to prevent overflow, but acheiving good computation on low level signals */
    1140        3100 :     tmpExp = 0;
    1141        3100 :     move16();
    1142        3100 :     ExpLmax = sub( 30, norm_l( fr_bands[10] ) );
    1143        3100 :     ExpLmax2 = sub( 30, norm_l( hNoiseEst->fr_bands2_fx[10] ) );
    1144        3100 :     tmpExp = s_max( tmpExp, sub( shl( s_max( ExpLmax, ExpLmax2 ), 1 ), s_min( ExpLmax, ExpLmax2 ) ) );
    1145       31000 :     FOR( i = 11; i <= st_fx->max_band; i++ )
    1146             :     {
    1147       27900 :         ExpLmax = sub( 30, norm_l( fr_bands[i] ) );
    1148       27900 :         ExpLmax2 = sub( 30, norm_l( hNoiseEst->fr_bands2_fx[i] ) );
    1149       27900 :         tmpExp = s_max( tmpExp, sub( shl( s_max( ExpLmax, ExpLmax2 ), 1 ), s_min( ExpLmax, ExpLmax2 ) ) );
    1150             :     }
    1151        3100 :     tmpExp = sub( tmpExp, 30 - 4 - 4 ); /* 4bits for internal summation and 4 bits for comparaison */
    1152             : 
    1153        3100 :     pt1 = fr_bands + 10;
    1154        3100 :     pt2 = hNoiseEst->fr_bands2_fx + 10;
    1155       34100 :     FOR( i = 10; i <= st_fx->max_band; i++ )
    1156             :     {
    1157       31000 :         Lnum = L_max( *pt1, *pt2 ); /* Don't need if anymore */
    1158       31000 :         Lsum_den = L_add_o( Lsum_den, Lnum, &Overflow );
    1159       31000 :         Ltmpden = L_min( *pt1, *pt2 );
    1160       31000 :         if ( Ltmpden == 0 )
    1161             :         {
    1162           0 :             Ltmpden = L_add( Ltmpden, 1 );
    1163             :         }
    1164       31000 :         ExpNum = sub( norm_l( Lnum ), 1 );
    1165       31000 :         num = extract_h( L_shl( Lnum, ExpNum ) );
    1166       31000 :         num = mult_r( num, num );
    1167       31000 :         ExpDen = norm_l( Ltmpden );
    1168       31000 :         den = extract_h( L_shl( Ltmpden, ExpDen ) );
    1169       31000 :         num = div_s( num, den );
    1170       31000 :         Ltmp = L_shr( num, add( sub( sub( shl( ExpNum, 1 ), ExpDen ), 15 + 1 ), tmpExp ) );
    1171       31000 :         Lsum_num = L_add( Lsum_num, Ltmp );
    1172             : 
    1173       31000 :         pt1++;
    1174       31000 :         pt2++;
    1175             :     }
    1176        3100 :     Lsum_den = L_shr( Lsum_den, tmpExp );
    1177             : 
    1178             : 
    1179             :     /* calculation of spectral diversity */
    1180             :     /* THR_SPDIV_FX = 5 , 1/5 Q15 =  6554 */
    1181        3100 :     spec_div = 0;
    1182        3100 :     move16();
    1183        3100 :     if ( GT_32( Mult_32_16( Lsum_num, 6554 ), Lsum_den ) ) /* Qx+Q15+1-16  ==> Qx */
    1184             :     {
    1185        1510 :         spec_div = 1;
    1186        1510 :         move16();
    1187             :     }
    1188             : 
    1189             :     /* *sp_div = Lsum_num / (Lsum_den + 1e-5f); */
    1190        3100 :     ExpNum = sub( norm_l( Lsum_num ), 1 );
    1191        3100 :     num = extract_h( L_shl( Lsum_num, ExpNum ) );
    1192             : 
    1193        3100 :     Lsum_den = L_add( Lsum_den, 1 );
    1194             : 
    1195        3100 :     ExpDen = norm_l( Lsum_den );
    1196        3100 :     den = extract_h( L_shl( Lsum_den, ExpDen ) );
    1197             : 
    1198        3100 :     *sp_div = div_s( num, den );
    1199        3100 :     move16();
    1200             : 
    1201        3100 :     *Q_sp_div = add( 15, sub( ExpNum, ExpDen ) );
    1202        3100 :     move16();
    1203             : 
    1204             :     /*-----------------------------------------------------------------*
    1205             :      * Detection of frames with high energy content in high frequencies
    1206             :      *-----------------------------------------------------------------*/
    1207             : 
    1208             :     /* calculation of energy in first 10 critical bands */
    1209        3100 :     Ltmp = sum32_fx( &fr_bands[st_fx->min_band], sub( 10, st_fx->min_band ) );
    1210             : 
    1211             :     /* calculation of energy in the rest of bands */
    1212        3100 :     Ltmp2 = sum32_fx( &fr_bands[10], sub( st_fx->max_band, 9 ) );
    1213             : 
    1214        3100 :     wtmp = shl_o( 1, sub( add( Q_new, QSCALE ), 1 ), &Overflow );
    1215             : 
    1216        3100 :     test();
    1217        3100 :     IF( L_msu( Ltmp, 100, wtmp ) < 0 || L_msu( Ltmp2, 100, wtmp ) < 0 )
    1218             :     {
    1219         858 :         noise_chartmp = 0;
    1220         858 :         move16();
    1221             :     }
    1222             :     ELSE
    1223             :     {
    1224             :         /* ftemp2 /= ftemp */
    1225        2242 :         ExpNum = sub( norm_l( Ltmp2 ), 1 );
    1226        2242 :         num = extract_h( L_shl( Ltmp2, ExpNum ) );
    1227             : 
    1228        2242 :         ExpDen = norm_l( Ltmp );
    1229        2242 :         den = extract_h( L_shl( Ltmp, ExpDen ) );
    1230        2242 :         num = div_s( num, den );
    1231        2242 :         noise_chartmp = extract_h( L_shr_o( num, add( sub( ExpNum, ExpDen ), 4 - 16 ), &Overflow ) ); /* Q11 */
    1232             :     }
    1233             : 
    1234        3100 :     noise_chartmp = s_min( noise_chartmp, (Word16) 10 << 11 ); /* Q11 */
    1235             : 
    1236             :     /* update LT value of the final parameter */
    1237             :     /* *st_noise_char = M_ALPHA * *st_noise_char + (1-M_ALPHA) * noise_chartmp */
    1238        3100 :     hNoiseEst->noise_char_fx = mac_r( L_mult( M_ALPHA_FX, hNoiseEst->noise_char_fx ), ONE_MINUS_M_ALPHA, noise_chartmp );
    1239             : 
    1240             : 
    1241        3100 :     nchar_thr = THR_NCHAR_WB_FX;
    1242        3100 :     move16(); /* 1.0 Q11 */
    1243        3100 :     if ( vad_bwidth_fx == NB )
    1244             :     {
    1245           0 :         nchar_thr = THR_NCHAR_NB_FX;
    1246           0 :         move16(); /* 1.0 Q11 */
    1247             :     }
    1248             : 
    1249        3100 :     noise_char = 0;
    1250        3100 :     move16();
    1251        3100 :     if ( GT_16( hNoiseEst->noise_char_fx, nchar_thr ) )
    1252             :     {
    1253         604 :         noise_char = 1;
    1254         604 :         move16();
    1255             :     }
    1256             : 
    1257             :     /* save the 2 last spectra per crit. bands for the future */
    1258        3100 :     Copy32( hNoiseEst->fr_bands1_fx, hNoiseEst->fr_bands2_fx, NB_BANDS );
    1259        3100 :     Copy32( fr_bands + NB_BANDS, hNoiseEst->fr_bands1_fx, NB_BANDS );
    1260             : 
    1261             :     /*-----------------------------------------------------------------*
    1262             :      * Non-stationarity estimation for each band
    1263             :      * Handicap high E frames in average computing
    1264             :      *-----------------------------------------------------------------*/
    1265             : 
    1266             :     /* set averaging factor */
    1267             :     /* ftemp = relE; */
    1268             :     /* if( ftemp < 0.0f ) { ftemp = 0.0f;     } */
    1269        3100 :     tmp = s_max( relE, 0 ); /* Q8  */
    1270             : 
    1271             :     /* alpha =  0.064f * ftemp  +  0.75f; */
    1272        3100 :     Ltmp = Mult_32_16( (Word32) 137438953L, tmp );                  /* Q31(.064)+Q8+1-16      -->  Q24 */
    1273        3100 :     Ltmp = L_mac( Ltmp, 256, 24576 );                               /* Q8+Q15(.75)+1  --> Q24    */
    1274        3100 :     alpha = round_fx_o( L_shl_o( Ltmp, 7, &Overflow ), &Overflow ); /*Q24 +7 --> Q31    Q15*/
    1275             : 
    1276             :     /*if( alpha > 0.999f  {     alpha = 0.999f;} */
    1277        3100 :     alpha = s_min( alpha, 32735 );            /*.999 in Q15*/
    1278        3100 :     alpham1 = negate( add( -32768, alpha ) ); /* 1.0 - alpha  */
    1279             :     /*--------------------------------------------------------------*
    1280             :      * during significant attacks, replace the LT energy by the
    1281             :      * current energy this will cause non_sta2 failures to occur in
    1282             :      * different frames than non_sta failures
    1283             :      *--------------------------------------------------------------*/
    1284             : 
    1285        3100 :     alpha2 = alpha;
    1286        3100 :     move16();
    1287        3100 :     alpha2m1 = alpham1;
    1288        3100 :     move16();
    1289        3100 :     IF( spec_div > 0 )
    1290             :     {
    1291        1510 :         alpha2 = 0;
    1292        1510 :         move16();
    1293        1510 :         alpha2m1 = 32767;
    1294        1510 :         move16();
    1295             :     }
    1296        3100 :     Lnon_sta2 = L_deposit_l( 1 << 10 );
    1297             : 
    1298        3100 :     non_sta = L_deposit_l( 1 << 10 );
    1299        3100 :     *non_staX = 0;
    1300        3100 :     move16();
    1301        3100 :     non_staB = 0;
    1302        3100 :     move16();
    1303             : 
    1304       65100 :     FOR( i = st_fx->min_band; i <= st_fx->max_band; i++ )
    1305             :     {
    1306             :         /*  + 1.0f added to reduce sensitivity to non stationarity in low energies  */
    1307             :         /* tmp_enr = enr[i] + 1.0f; */
    1308       62000 :         tmp_Q = add( Q_new, Q_SCALE );
    1309       62000 :         Ltmp = L_shl( (Word32) 1L, tmp_Q );                                   /* 1.0 added in the right dynamic domain */
    1310       62000 :         L_tmp_enr = L_add_o( enr[i], Ltmp, &Overflow );                       /* enr      scale  dynamic     */
    1311       62000 :         L_tmp_ave_enr = L_add_o( hNoiseEst->ave_enr_fx[i], Ltmp, &Overflow ); /* ave__enr  scale dynamic  */
    1312             : 
    1313       62000 :         IF( LE_32( non_sta, th_sta ) ) /* Just to limit the saturation */
    1314             :         {
    1315             :             /* if( enr[i] > st_ave_enr2[i] ) */
    1316             :             /* non_sta2 = non_sta2 * ((enr[i]+1) / (st_ave_enr2[i]+1)) */
    1317       33954 :             Lnum = L_max( L_tmp_enr, L_tmp_ave_enr );
    1318             : 
    1319             :             /* else */
    1320             :             /* non_sta2 = non_sta2 * ((st_ave_enr2[i]+1) / (enr[i]+1)) */
    1321       33954 :             Lden = L_min( L_tmp_enr, L_tmp_ave_enr );
    1322             : 
    1323       33954 :             ExpNum = sub( norm_l( Lnum ), 1 );
    1324       33954 :             num = extract_h( L_shl( Lnum, ExpNum ) );
    1325       33954 :             Lnum = L_shl( Lnum, ExpNum );
    1326       33954 :             ExpDen = norm_l( Lden );
    1327       33954 :             den = extract_h( L_shl( Lden, ExpDen ) );
    1328       33954 :             num = div_s( num, den );
    1329       33954 :             Ltmp = Mult_32_16( non_sta, num );
    1330       33954 :             non_sta = L_shr_o( Ltmp, sub( ExpNum, ExpDen ), &Overflow ); /* Q10 */
    1331             :         }
    1332             : 
    1333             :         /*    st->ave_enr[i] = alpha * st->ave_enr[i] + (1-alpha) * enr[i];*/ /* update long-term average */
    1334       62000 :         Ltmp = Mult_32_16( hNoiseEst->ave_enr_fx[i], alpha );
    1335       62000 :         Ltmp = L_add( Ltmp, Mult_32_16( enr[i], alpham1 ) );
    1336       62000 :         hNoiseEst->ave_enr_fx[i] = L_max( Le_min_scaled, Ltmp );
    1337       62000 :         move32();
    1338             : 
    1339             :         /* calculation of another non-stationarity measure (following attacks) */
    1340             :         /*if( non_sta2 <= th_sta ){
    1341             :              tmp_ave2 =  st->ave_enr2[i] + 1.0f;
    1342             :              if( tmp_enr > tmp_ave2 ){
    1343             :                  non_sta2 = non_sta2 * ( tmp_enr / tmp_ave2 );
    1344             :              } else {
    1345             :                  non_sta2 = non_sta2 * (tmp_ave2 / tmp_enr );
    1346             :              }
    1347             :          } */
    1348             : 
    1349             :         /* ave_enr2:: calculation of another non-stationarity measure (following attacks)  */
    1350       62000 :         Ltmp = L_shl( (Word32) 1L, tmp_Q );                                     /* 1.0 added in the right dynamic domain */
    1351             :         /*L_tmp_enr    =  L_add(enr[i]              , Ltmp );*/                 /* enr      scale  dynamic    , done above  */
    1352       62000 :         L_tmp_ave_enr2 = L_add_o( hNoiseEst->ave_enr2_fx[i], Ltmp, &Overflow ); /* ave__enr  scale dynamic  */
    1353       62000 :         IF( LE_32( Lnon_sta2, th_sta ) )                                        /* Just to limit the saturation */
    1354             :         {
    1355       46423 :             Lnum = L_max( L_tmp_enr, L_tmp_ave_enr2 );
    1356       46423 :             Lden = L_min( L_tmp_enr, L_tmp_ave_enr2 );
    1357             : 
    1358       46423 :             ExpNum = sub( norm_l( Lnum ), 1 );
    1359       46423 :             num = extract_h( L_shl( Lnum, ExpNum ) );
    1360       46423 :             Lnum = L_shl( Lnum, ExpNum );
    1361       46423 :             ExpDen = norm_l( Lden );
    1362       46423 :             den = extract_h( L_shl( Lden, ExpDen ) );
    1363       46423 :             num = div_s( num, den );
    1364       46423 :             Ltmp1 = Mult_32_16( Lnon_sta2, num );
    1365       46423 :             Lnon_sta2 = L_shr_o( Ltmp1, sub( ExpNum, ExpDen ), &Overflow ); /* Q10 */
    1366             :         }
    1367             : 
    1368             :         /* st_ave_enr2[i] = (float)alpha2 * st_ave_enr2[i]
    1369             :                                 + (1.0f - alpha2) * (enr[i]) */
    1370       62000 :         Ltmp1 = Mult_32_16( hNoiseEst->ave_enr2_fx[i], alpha2 );
    1371       62000 :         Ltmp1 = L_add( Ltmp1, Mult_32_16( enr[i], alpha2m1 ) );
    1372       62000 :         hNoiseEst->ave_enr2_fx[i] = L_max( Le_min_scaled, Ltmp1 );
    1373       62000 :         move32();
    1374             : 
    1375             :         /* calculation of non-stationarity measure for speech/music classification */
    1376             :         {
    1377       62000 :             test();
    1378       62000 :             test();
    1379       62000 :             IF( GE_16( i, START_BAND_SPMUS ) && LT_16( i, NB_BANDS_SPMUS + START_BAND_SPMUS ) && st_fx->hSpMusClas != NULL )
    1380             :             {
    1381             :                 /* log_enr = (float)ln_fx(enr[i]); */
    1382       46500 :                 log_enr16 = noise_est_ln_q8_fx( enr[i], 0, tmp_Q );
    1383       46500 :                 wtmp = abs_s( sub( log_enr16, hSpMusClas->past_log_enr_fx[i - START_BAND_SPMUS] ) );
    1384       46500 :                 *non_staX = add_o( *non_staX, wtmp, &Overflow );
    1385       46500 :                 move16(); /* Q8 */
    1386       46500 :                 hSpMusClas->past_log_enr_fx[i - START_BAND_SPMUS] = log_enr16;
    1387       46500 :                 move16();
    1388             :             }
    1389             :         }
    1390       62000 :         test();
    1391       62000 :         IF( GE_16( i, 2 ) && LE_16( i, 16 ) )
    1392             :         {
    1393       46500 :             IF( GE_16( ini_frame, 100 ) )
    1394             :             {
    1395             :                 /* calculate non-stationarity feature relative background */
    1396       42000 :                 tmp_enr = noise_est_ln_q8_fx( enr[i], 1, tmp_Q ); /* 1.0f  added */
    1397       42000 :                 tmp_floor = LN_E_MIN_PLUS_ONE_FX;
    1398       42000 :                 move16(); /* non dynamic init constant in Q8 */
    1399       42000 :                 tmp_floor = noise_est_ln_q8_fx( hNoiseEst->bckr_fx[i], 1, tmp_Q );
    1400       42000 :                 non_staB = add_o( non_staB, abs_s( sub( tmp_enr, tmp_floor ) ), &Overflow ); /*  Q8  */
    1401             :             }
    1402             :             ELSE /*ini_frame < 100*/
    1403             :             {
    1404             :                 /* calculate non-stationarity feature relative background */
    1405        4500 :                 tmp_enr = noise_est_ln_q8_fx( enr[i], 1, tmp_Q ); /* 1.0f  added */
    1406        4500 :                 tmp_floor = LN_E_MIN_PLUS_ONE_FX;
    1407        4500 :                 move16(); /* non dynamic init constant in Q8 */
    1408        4500 :                 tmp_floor = noise_est_ln_q8_fx( E_MIN_FX, 1, tmp_Q );
    1409        4500 :                 non_staB = add_o( non_staB, abs_s( sub( tmp_enr, tmp_floor ) ), &Overflow ); /*  Q8  */
    1410             :             }
    1411             :         }
    1412             : 
    1413             :     } /* end of band loop FOR( i = st_fx->min_band; i <= st_fx->max_band; i++ ) */
    1414             : 
    1415        3100 :     IF( LT_16( Etot, -1280 ) )
    1416             :     {
    1417           8 :         non_sta = L_deposit_l( 1024 );   /* 1.0 in  Q10  */
    1418           8 :         Lnon_sta2 = L_deposit_l( 1024 ); /* 1.0 in  Q10 */
    1419             :     }
    1420             : 
    1421        3100 :     lim_Etot_fx = s_max( 5120, Etot );                                              /* 20.0f Q8 */
    1422        3100 :     lim_Etot_sq_fx = extract_h( L_shl_r( L_mult( lim_Etot_fx, lim_Etot_fx ), 1 ) ); /* Q2 */
    1423             : 
    1424        3100 :     IF( LT_16( st_fx->ini_frame, 150 ) )
    1425             :     {
    1426             :         /* Allow use of quicker filter during init - if needed */
    1427             :         /* st->Etot_st_est = 0.25f * lim_Etot + (1.0f-0.25F) * st->Etot_st_est; */
    1428         450 :         hNoiseEst->Etot_st_est_fx = mac_r( L_mult( 8192, lim_Etot_fx ), 24576, hNoiseEst->Etot_st_est_fx );
    1429         450 :         move16();
    1430             :         /* st->Etot_sq_st_est = 0.25f * lim_Etot * lim_Etot + (1.0f-0.25f) * st->Etot_sq_st_est; */
    1431         450 :         hNoiseEst->Etot_sq_st_est_fx = mac_r( L_mult( 8192, lim_Etot_sq_fx ), 24576, hNoiseEst->Etot_sq_st_est_fx );
    1432         450 :         move16();
    1433             :     }
    1434             :     ELSE
    1435             :     {
    1436             :         /* st->Etot_st_est = 0.25f * lim_Etot + (1.0f-0.25F) * st->Etot_st_est; */
    1437        2650 :         hNoiseEst->Etot_st_est_fx = mac_r( L_mult( 8192, lim_Etot_fx ), 24576, hNoiseEst->Etot_st_est_fx );
    1438        2650 :         move16();
    1439             :         /* st->Etot_sq_st_est = 0.25f * lim_Etot * lim_Etot + (1.0f-0.25f) * st->Etot_sq_st_est; */
    1440        2650 :         hNoiseEst->Etot_sq_st_est_fx = mac_r( L_mult( 8192, lim_Etot_sq_fx ), 24576, hNoiseEst->Etot_sq_st_est_fx );
    1441        2650 :         move16();
    1442             :     }
    1443             : 
    1444        3100 :     st_E_var_est_fx = sub( hNoiseEst->Etot_sq_st_est_fx, extract_h( L_shl_r( L_mult( hNoiseEst->Etot_st_est_fx, hNoiseEst->Etot_st_est_fx ), 1 ) ) );
    1445             : 
    1446             : 
    1447             :     /*-----------------------------------------------------------------*
    1448             :      * Count frames since last correlation or harmonic event
    1449             :      *-----------------------------------------------------------------*/
    1450             : 
    1451        3100 :     Ltmp = L_mult( st_fx->voicing_fx[0], 16384 );
    1452        3100 :     Ltmp = L_mac( Ltmp, st_fx->voicing_fx[1], 16384 );
    1453             : 
    1454        3100 :     test();
    1455        3100 :     test();
    1456        3100 :     *st_harm_cor_cnt = add( *st_harm_cor_cnt, 1 );
    1457        3100 :     move16();
    1458        3100 :     if ( ( Etot > 0 ) && ( ( *loc_harm > 0 ) || ( GT_16( round_fx( Ltmp ), COR_MAX_NNE_FX ) ) ) )
    1459             :     {
    1460        2483 :         *st_harm_cor_cnt = 0;
    1461        2483 :         move16();
    1462             :     }
    1463        3100 :     test();
    1464        3100 :     test();
    1465        3100 :     test();
    1466        3539 :     if ( ( GT_16( *st_harm_cor_cnt, 1 ) ) && ( ( LT_16( Etot, 3840 ) ) || /* 15 in Q8 */
    1467         859 :                                                ( GT_16( st_fx->ini_frame, 10 ) &&
    1468         420 :                                                  GT_16( sub( Etot, hNoiseEst->Etot_lp_fx ), 1792 ) ) ) /* 7 in Q8 */
    1469             :     )
    1470             :     {
    1471         145 :         *st_harm_cor_cnt = 1;
    1472             :     }
    1473        3100 :     test();
    1474        3100 :     test();
    1475        3464 :     if ( GT_16( *st_harm_cor_cnt, 1 ) &&
    1476         615 :          GT_16( Etot, 7680 ) &&       /* 30.0f in Q8 */
    1477         251 :          GT_16( st_E_var_est_fx, 32 ) /* 8.0f in Q2 */
    1478             :     )
    1479             :     {
    1480             : 
    1481             :         /* st->harm_cor_cnt = max(1, (short) round_f( (float) st->harm_cor_cnt / 4.0f )) ; */
    1482         231 :         *st_harm_cor_cnt = s_max( 1, shr( add( *st_harm_cor_cnt, 2 ), 1 ) );
    1483         231 :         move16();
    1484             :     }
    1485             : 
    1486             : 
    1487             :     /*-----------------------------------------------------------------*
    1488             :      * Energy based pause length counter
    1489             :      *-----------------------------------------------------------------*/
    1490        3100 :     test();
    1491        3100 :     IF( ( *bg_cnt >= 0 ) && ( GT_16( sub( Etot, Etot_l_lp ), 1280 ) /*5.0 in Q8*/ ) )
    1492             :     {
    1493             :         /* Possible speech burst */
    1494          34 :         *bg_cnt = -1;
    1495          34 :         move16();
    1496             :     }
    1497             :     ELSE
    1498             :     {
    1499        3066 :         test();
    1500        3066 :         if ( EQ_16( *bg_cnt, -1 ) && ( LT_16( sub( Etot, Etot_l_lp ), 1280 ) ) /*5 in Q8*/ )
    1501             :         {
    1502             :             /* Possible start of speech pause */
    1503          34 :             *bg_cnt = 0;
    1504          34 :             move16();
    1505             :         }
    1506             :     }
    1507        3100 :     if ( *bg_cnt >= 0 )
    1508             :     {
    1509         220 :         *bg_cnt = add( *bg_cnt, 1 );
    1510         220 :         move16();
    1511             :     }
    1512             : 
    1513             :     /*-----------------------------------------------------------------*
    1514             :      * Linear predition efficiency 0 to 2 order
    1515             :      *-----------------------------------------------------------------*/
    1516             : 
    1517             :     /*epsP_0_2 = max(0 , min(8, epsP[0] / epsP[2])); */
    1518        3100 :     Ltmp = eps_quota_fx( epsP_h[0], epsP_l[0],
    1519        3100 :                          epsP_h[2], epsP_l[2], 12 );                        /* Word32  Q12 */
    1520             :     BASOP_SATURATE_WARNING_OFF_EVS                                          /* may saturate*/
    1521        3100 :         epsP_0_2 = round_fx_o( L_shl_o( Ltmp, 16, &Overflow ), &Overflow ); /* Q12+16  -16 ->  Q12 , NB saturation in Q12 sets max value to  7,999 */
    1522             :     BASOP_SATURATE_WARNING_ON_EVS
    1523             : 
    1524        3100 :     epsP_0_2 = s_max( 0, epsP_0_2 ); /* min value is 0 , Q12 */
    1525             : 
    1526             : 
    1527             :     /* st->epsP_0_2_lp = 0.15f * epsP_0_2 + (1.0f-0.15f) * st->epsP_0_2_lp; */
    1528        3100 :     alpha = 4915;
    1529        3100 :     move16(); /*0.15 in Q15 */
    1530        3100 :     hNoiseEst->epsP_0_2_lp_fx = noise_est_AR1_Qx( epsP_0_2, hNoiseEst->epsP_0_2_lp_fx, alpha );
    1531        3100 :     move16();
    1532             :     /* epsP_0_2_ad = (float) fabs(epsP_0_2 - st->epsP_0_2_lp );  */
    1533        3100 :     epsP_0_2_ad = abs_s( sub( epsP_0_2, hNoiseEst->epsP_0_2_lp_fx ) ); /* Q12 */
    1534             : 
    1535             :     /*if (epsP_0_2_ad < st->epsP_0_2_ad_lp)  {
    1536             :         st->epsP_0_2_ad_lp = 0.1f * epsP_0_2_ad + (1.0f - 0.1f) * st->epsP_0_2_ad_lp;
    1537             :     } else {
    1538             :         st->epsP_0_2_ad_lp = 0.2f * epsP_0_2_ad + (1.0f - 0.2f) * st->epsP_0_2_ad_lp;
    1539             :      } */
    1540        3100 :     alpha = 6554;
    1541        3100 :     move16(); /* 0.2 Q15 */
    1542        3100 :     if ( LT_16( epsP_0_2_ad, hNoiseEst->epsP_0_2_ad_lp_fx ) )
    1543             :     {
    1544        2269 :         alpha = shr( alpha, 1 ); /* 0.1 Q15 */
    1545             :     }
    1546        3100 :     hNoiseEst->epsP_0_2_ad_lp_fx = noise_est_AR1_Qx( epsP_0_2_ad, hNoiseEst->epsP_0_2_ad_lp_fx, alpha );
    1547        3100 :     move16();
    1548             :     /* epsP_0_2_ad_lp_max = max(epsP_0_2_ad,st->epsP_0_2_ad_lp);*/
    1549        3100 :     epsP_0_2_ad_lp_max = s_max( epsP_0_2_ad, hNoiseEst->epsP_0_2_ad_lp_fx ); /* Q12 */
    1550             : 
    1551             : 
    1552             :     /*-----------------------------------------------------------------*
    1553             :      * Linear predition efficiency 2 to 16 order
    1554             :      *-----------------------------------------------------------------*/
    1555             : 
    1556             :     /*  epsP_2_16 = max(0 , min(8, epsP[2] / epsP[16])); */
    1557        3100 :     Ltmp = eps_quota_fx( epsP_h[2], epsP_l[2],
    1558        3100 :                          epsP_h[16], epsP_l[16], 12 );                       /* Word32  Q12 */
    1559             :     BASOP_SATURATE_WARNING_OFF_EVS                                           /* may saturate*/
    1560        3100 :         epsP_2_16 = round_fx_o( L_shl_o( Ltmp, 16, &Overflow ), &Overflow ); /* Q12+16  -16 ->  Q12 ,
    1561             :                                           NB saturation in Q12 sets max value to  7,999 */
    1562             :     BASOP_SATURATE_WARNING_ON_EVS
    1563             : 
    1564        3100 :     epsP_2_16 = s_max( 0, epsP_2_16 ); /* min value is 0 , Q12 */
    1565             : 
    1566             : 
    1567             :     /* if (epsP_2_16 > st->epsP_2_16_lp){
    1568             :           st->epsP_2_16_lp = 0.2f * epsP_2_16 + (1.0f-0.2f) * st->epsP_2_16_lp;
    1569             :       } else {
    1570             :           st->epsP_2_16_lp = 0.03f * epsP_2_16 + (1.0f-0.03f) * st->epsP_2_16_lp;
    1571             :       }
    1572             : 
    1573             :       st->epsP_2_16_lp2 = 0.02f * epsP_2_16 + (1.0f-0.02f) * st->epsP_2_16_lp2; */
    1574             : 
    1575        3100 :     alpha = 983;
    1576        3100 :     move16(); /* 0.03  Q15 */
    1577        3100 :     if ( GT_16( epsP_2_16, hNoiseEst->epsP_2_16_lp_fx ) )
    1578             :     {
    1579         538 :         alpha = 6554;
    1580         538 :         move16(); /* 0.2  Q15 */
    1581             :     }
    1582        3100 :     hNoiseEst->epsP_2_16_lp_fx = noise_est_AR1_Qx( epsP_2_16, hNoiseEst->epsP_2_16_lp_fx, alpha );
    1583        3100 :     move16();
    1584        3100 :     hNoiseEst->epsP_2_16_lp2_fx = noise_est_AR1_Qx( epsP_2_16, hNoiseEst->epsP_2_16_lp2_fx, 655 ); /* 0.02 */
    1585        3100 :     move16();
    1586        3100 :     epsP_2_16_dlp = sub( hNoiseEst->epsP_2_16_lp_fx, hNoiseEst->epsP_2_16_lp2_fx );
    1587             : 
    1588             : 
    1589             :     /* if (epsP_2_16_dlp < st->epsP_2_16_dlp_lp2 )  {
    1590             :         st->epsP_2_16_dlp_lp2 = 0.02f * epsP_2_16_dlp + (1.0f-0.02f) * st->epsP_2_16_dlp_lp2;
    1591             :     } else {
    1592             :         st->epsP_2_16_dlp_lp2 = 0.05f * epsP_2_16_dlp + (1.0f-0.05f) * st->epsP_2_16_dlp_lp2;
    1593             :     }*/
    1594        3100 :     alpha = 1638;
    1595        3100 :     move16(); /* 0.05  Q15 */
    1596        3100 :     if ( LT_16( epsP_2_16_dlp, hNoiseEst->epsP_2_16_dlp_lp2_fx ) )
    1597             :     {
    1598        2218 :         alpha = 655;
    1599        2218 :         move16(); /* 0.02  Q15 */
    1600             :     }
    1601        3100 :     hNoiseEst->epsP_2_16_dlp_lp2_fx = noise_est_AR1_Qx( epsP_2_16_dlp, hNoiseEst->epsP_2_16_dlp_lp2_fx, alpha );
    1602        3100 :     move16();
    1603             :     /* epsP_2_16_dlp_max  = max(epsP_2_16_dlp,st->epsP_2_16_dlp_lp2); */
    1604        3100 :     epsP_2_16_dlp_max = s_max( epsP_2_16_dlp, hNoiseEst->epsP_2_16_dlp_lp2_fx );
    1605             : 
    1606             :     /*-----------------------------------------------------------------*
    1607             :      * long term extensions of frame features
    1608             :      *-----------------------------------------------------------------*/
    1609             : 
    1610        3100 :     tmp = sub( Etot, hNoiseEst->totalNoise_fx ); /* Q8 */
    1611             :     /* st->lt_tn_track = 0.03f* (Etot - st->totalNoise < 10) + 0.97f*st->lt_tn_track; */
    1612        3100 :     tmp2 = 0;
    1613        3100 :     move16();
    1614        3100 :     if ( LT_16( tmp, 2560 ) ) /*10 in Q8 */
    1615             :     {
    1616          13 :         tmp2 = 32767;
    1617          13 :         move16();
    1618             :     }
    1619        3100 :     hNoiseEst->lt_tn_track_fx = noise_est_AR1_Qx( tmp2, hNoiseEst->lt_tn_track_fx, 983 ); /*0.03 in Q15 ,Q15 state*/
    1620        3100 :     move16();
    1621             :     /* st->lt_tn_dist = 0.03f* (Etot - st->totalNoise) + 0.97f*st->lt_tn_dist; */
    1622        3100 :     hNoiseEst->lt_tn_dist_fx = noise_est_AR1_Qx( tmp, hNoiseEst->lt_tn_dist_fx, 983 ); /*0.03 in Q15 ,Q8 state*/
    1623        3100 :     move16();
    1624             :     /* st->lt_Ellp_dist = 0.03f* (Etot - st->Etot_l_lp) + 0.97f*st->lt_Ellp_dist;*/
    1625        3100 :     tmp = sub( Etot, hNoiseEst->Etot_l_lp_fx );                                            /* Q8 */
    1626        3100 :     hNoiseEst->lt_Ellp_dist_fx = noise_est_AR1_Qx( tmp, hNoiseEst->lt_Ellp_dist_fx, 983 ); /*0.03 in Q15 ,Q8 state*/
    1627        3100 :     move16();
    1628             : 
    1629             :     /* if (st->harm_cor_cnt == 0)  {
    1630             :         st->lt_haco_ev = 0.03f*1.0 + 0.97f*st->lt_haco_ev;
    1631             :     } else {
    1632             :         st->lt_haco_ev = 0.99f*st->lt_haco_ev;
    1633             :     } */
    1634        3100 :     IF( *st_harm_cor_cnt == 0 )
    1635             :     {
    1636        2483 :         hNoiseEst->lt_haco_ev_fx = noise_est_AR1_Qx( (Word16) 32767, hNoiseEst->lt_haco_ev_fx, 983 ); /*.03 in Q15 , Q15 state */
    1637             :     }
    1638             :     ELSE
    1639             :     {
    1640         617 :         hNoiseEst->lt_haco_ev_fx = mult_r( 32440, hNoiseEst->lt_haco_ev_fx ); /*.99 in Q15 , Q15 state */
    1641             :     }
    1642        3100 :     move16();
    1643             : 
    1644             :     /* if (st->lt_tn_track < 0.05f)  {
    1645             :         st->low_tn_track_cnt++;
    1646             :     } else {
    1647             :         st->low_tn_track_cnt=0;
    1648             :     }*/
    1649        3100 :     tmp = 0;
    1650        3100 :     move16();
    1651        3100 :     move16();
    1652        3100 :     if ( LT_16( hNoiseEst->lt_tn_track_fx, 1638 ) ) /* 0.05 in Q15*/
    1653             :     {
    1654        2944 :         tmp = add( hNoiseEst->low_tn_track_cnt, 1 );
    1655             :     }
    1656        3100 :     hNoiseEst->low_tn_track_cnt = tmp;
    1657        3100 :     move16();
    1658             : 
    1659             : 
    1660             :     /* update of the long-term non-stationarity measure (between 0 and 1) */
    1661             :     /* if ( (non_sta > th_sta) || (*loc_harm > 0) ) {
    1662             :         st->act_pred = M_GAMMA * st->act_pred + (1-M_GAMMA) * 1;
    1663             :     } else {
    1664             :         st->act_pred = M_GAMMA * st->act_pred + (1-M_GAMMA) * 0;
    1665             :     }*/
    1666        3100 :     Ltmp = L_mult( M_GAMMA_FX, hNoiseEst->act_pred_fx ); /*Q15*Q15+1 --> Q31 , 32440= .99 Q15 */
    1667        3100 :     tmp = round_fx( Ltmp );                              /* Q15 */
    1668        3100 :     test();
    1669        3100 :     if ( ( GT_32( non_sta, th_sta ) ) /* float th_sta  NB 5e10 ,  WB 3.5e10*/
    1670         281 :          || ( *loc_harm > 0 ) )
    1671             :     {
    1672        2913 :         tmp = mac_r( Ltmp, ( -32768 + M_GAMMA_FX ), -32768 ); /* (-0.01)*(-1.0) */
    1673             :     }
    1674        3100 :     hNoiseEst->act_pred_fx = tmp;
    1675        3100 :     move16();
    1676             : 
    1677             : 
    1678             :     /*-----------------------------------------------------------------*
    1679             :      * Background noise adaptation enable flag
    1680             :      *-----------------------------------------------------------------*/
    1681        3100 :     Ltmp = L_mult( st_fx->voicing_fx[0], 16384 );
    1682        3100 :     Ltmp = L_mac( Ltmp, st_fx->voicing_fx[1], 16384 );
    1683        3100 :     cor_tmp = mac_ro( Ltmp, corr_shift, MAX_16, &Overflow );
    1684             : 
    1685        3100 :     LepsP = eps_quota_fx( epsP_h[2], epsP_l[2],
    1686        3100 :                           epsP_h[16], epsP_l[16], 11 ); /* L_epsP in Q11 */
    1687             :     /* note  this  epsP2/eps16  is not limited to 8  as,  epsP_2_16  is  !!  */
    1688             : 
    1689        3100 :     vad_2nd_stage_fx = 0;
    1690        3100 :     move16(); /* background noise present - decrement counter */
    1691             :     /*
    1692             :      if( ( (*st_harm_cor_cnt < 3*HC_CNT_SLOW )
    1693             :           && ( ( non_sta > th_sta ) ||
    1694             :           ( tmp_pc < TH_PC ) ||
    1695             :           ( noise_char > 0) )
    1696             :           )
    1697             :           ||
    1698             :           ( (st->ini_frame > 150) && (Etot - Etot_l_lp) > 10 ) ||
    1699             :           ( 0.5f * (voicing[0]+voicing[1]) > cor_max ) ||
    1700             :           ( epsP[2] / epsP[16] > th_eps ) ||
    1701             :           ( *loc_harm > 0) ||
    1702             :           ((st->act_pred > 0.8f) && (non_sta2 > th_sta))
    1703             :           ) */
    1704             : 
    1705        3100 :     Ltmp = L_mult( st_fx->voicing_fx[0], 16384 );         /* Q15 + Q15(.5)) + 1    ->  Q31  */
    1706        3100 :     cor_tmp = mac_r( Ltmp, st_fx->voicing_fx[1], 16384 ); /* Q31 -16               ->  Q15  */
    1707        3100 :     if ( Etot < 0 )
    1708             :     {
    1709          13 :         cor_tmp = 0;
    1710          13 :         move16();
    1711             :     }
    1712             : 
    1713        3100 :     test();
    1714        3100 :     test();
    1715        3100 :     test();
    1716        3100 :     test();
    1717        3100 :     test();
    1718        3100 :     test();
    1719        3100 :     test();
    1720        3100 :     test();
    1721        3100 :     test();
    1722        3100 :     test();
    1723             : 
    1724        3179 :     if ( ( ( LT_16( *st_harm_cor_cnt, ( 3 * HC_CNT_SLOW_FX ) ) ) && ( ( GT_32( non_sta, th_sta ) ) || ( LT_16( tmp_pc, TH_PC_FX ) ) || ( GT_16( noise_char, 0 ) ) ) ) ||
    1725         187 :          ( ( GT_16( st_fx->ini_frame, HE_LT_CNT_INIT_FX ) ) && ( GT_16( sub( Etot, Etot_l_lp ), 2560 ) ) ) ||
    1726         107 :          ( GT_16( cor_tmp, cor_max ) ) || /* Q15 */
    1727          99 :          ( GT_32( LepsP, th_eps ) ) ||    /* Q11 */
    1728          87 :          ( GT_16( *loc_harm, 0 ) ) ||
    1729          82 :          ( ( GT_16( hNoiseEst->act_pred_fx, 26214 ) ) && ( GT_32( Lnon_sta2, th_sta ) ) ) /*act_pred in Q15 , th_sta in Q10 */
    1730             :     )
    1731             :     {
    1732        3059 :         vad_2nd_stage_fx = 1;
    1733        3059 :         move16(); /*  active signal present - increment counter  */
    1734             :     }
    1735             : 
    1736        3100 :     tmp = 2;
    1737        3100 :     move16(); /* Signal present  */
    1738        3100 :     if ( vad_2nd_stage_fx == 0 )
    1739             :     {
    1740          41 :         tmp = -1;
    1741          41 :         move16(); /* Background  present  */
    1742             :     }
    1743        3100 :     hNoiseEst->aEn = add( hNoiseEst->aEn, tmp );
    1744             : 
    1745             : 
    1746        3100 :     hNoiseEst->aEn = s_min( hNoiseEst->aEn, 6 );
    1747        3100 :     hNoiseEst->aEn = s_max( hNoiseEst->aEn, 0 );
    1748             : 
    1749             :     /* Additional NNE detectors */
    1750             : 
    1751             :     /* comb_ahc_epsP = max(max(st->act_pred, st->lt_haco_ev), epsP_2_16_dlp); */
    1752             :     /*                             Q15            Q15          Q12 */
    1753        3100 :     comb_ahc_epsP = s_max( s_max( shr( hNoiseEst->act_pred_fx, 15 - 12 ), shr( hNoiseEst->lt_haco_ev_fx, 15 - 12 ) ), epsP_2_16_dlp ); /* Q12 */
    1754             : 
    1755             : 
    1756             :     /* comb_hcm_epsP = max(max(st->lt_haco_ev,epsP_2_16_dlp_max),epsP_0_2_ad_lp_max); */
    1757             :     /*                            Q15         Q12                 Q12              */
    1758        3100 :     comb_hcm_epsP = s_max( s_max( shr( hNoiseEst->lt_haco_ev_fx, 15 - 12 ), epsP_2_16_dlp_max ), epsP_0_2_ad_lp_max ); /* Q12 */
    1759             : 
    1760             :     /*haco_ev_max = max(*st_harm_cor_cnt==0,st->lt_haco_ev); */
    1761        3100 :     tmp = 0;
    1762        3100 :     move16();
    1763        3100 :     if ( *st_harm_cor_cnt == 0 )
    1764             :     {
    1765        2483 :         tmp = (Word16) 32767;
    1766        2483 :         move16();
    1767             :     }
    1768        3100 :     haco_ev_max = s_max( tmp, hNoiseEst->lt_haco_ev_fx ); /* Q15 */
    1769             : 
    1770             :     /* Etot_l_lp_thr = st->Etot_l_lp + (1.5f + 1.5f * (st->Etot_lp<50.0f))*st->Etot_v_h2; */
    1771        3100 :     tmp = 12288;
    1772        3100 :     move16();                                    /* 1.5 Q13 */
    1773        3100 :     if ( LT_16( hNoiseEst->Etot_lp_fx, 12800 ) ) /* 50.0 in Q8 */
    1774             :     {
    1775        1677 :         tmp = shl( tmp, 1 ); /*1.5 + 1.5  Q13 */
    1776             :     }
    1777        3100 :     Ltmp = L_deposit_h( hNoiseEst->Etot_l_lp_fx );
    1778        3100 :     Etot_l_lp_thr = round_fx( L_add( Ltmp, L_shl( L_mult( tmp, Etot_v_h2 ), 2 ) ) ); /* Q13+Q8+1 +2 = Q24 -> Q8*/
    1779             : 
    1780             :     /* enr_bgd = Etot < Etot_l_lp_thr; */
    1781        3100 :     enr_bgd = 0;
    1782        3100 :     move16();
    1783        3100 :     if ( LT_16( Etot, Etot_l_lp_thr ) ) /* Q8 */
    1784             :     {
    1785         229 :         enr_bgd = 1;
    1786         229 :         move16(); /* Q0 */
    1787             :     }
    1788             : 
    1789             :     /* cns_bgd = (epsP_0_2 > 7.95f) && (non_sta< 1e3f); */
    1790        3100 :     cns_bgd = 0;
    1791        3100 :     move16();
    1792        3100 :     test();
    1793        3100 :     if ( ( GT_16( epsP_0_2, 32563 ) )        /* 7.95 in Q12 */
    1794        1337 :          && ( LT_32( non_sta, 1024000L ) ) ) /* 1e3f in  Q10 ? */
    1795             :     {
    1796          59 :         cns_bgd = 1;
    1797          59 :         move16(); /* Q0 */
    1798             :     }
    1799             : 
    1800             :     /*lp_bgd  = epsP_2_16_dlp_max < 0.10f; */
    1801        3100 :     lp_bgd = 0;
    1802        3100 :     move16();
    1803        3100 :     if ( LT_16( epsP_2_16_dlp_max, 410 ) ) /*0.10 Q12 */
    1804             :     {
    1805           3 :         lp_bgd = 1;
    1806           3 :         move16(); /* Q0 */
    1807             :     }
    1808             : 
    1809             : 
    1810             :     /* ns_mask = non_sta < 1e5f; */
    1811        3100 :     ns_mask = 0;
    1812        3100 :     move16();
    1813        3100 :     if ( LT_32( non_sta, (Word32) 102400000L ) ) /*  (1e5f in  Q10)*/
    1814             :     {
    1815         222 :         ns_mask = 1;
    1816         222 :         move16(); /* Q0 */
    1817             :     }
    1818             : 
    1819             : 
    1820             :     /* lt_haco_mask = st->lt_haco_ev < 0.5f; */
    1821        3100 :     lt_haco_mask = 0;
    1822        3100 :     move16();
    1823        3100 :     if ( LT_16( hNoiseEst->lt_haco_ev_fx, 16384 ) ) /*  ( .5 in  Q15)*/
    1824             :     {
    1825          75 :         lt_haco_mask = 1;
    1826          75 :         move16(); /* Q0 */
    1827             :     }
    1828             : 
    1829             :     /* bg_haco_mask = haco_ev_max < 0.4f;   */
    1830        3100 :     bg_haco_mask = 0;
    1831        3100 :     move16();
    1832        3100 :     if ( LT_16( haco_ev_max, 13107 ) ) /* ( 0.4 in  Q15)*/
    1833             :     {
    1834          42 :         bg_haco_mask = 1;
    1835          42 :         move16(); /* Q0 */
    1836             :     }
    1837             : 
    1838             : 
    1839             :     /* SD_1 = ( (epsP_0_2_ad > 0.5f) && (epsP_0_2 > 7.95f) ); */
    1840        3100 :     SD_1 = 0;
    1841        3100 :     move16();
    1842        3100 :     test();
    1843        3100 :     if ( ( GT_16( epsP_0_2_ad, 2048 ) )    /* 0.5 in Q12 */
    1844        2307 :          && ( GT_16( epsP_0_2, 32563 ) ) ) /* 7.95 in Q12 */
    1845             :     {
    1846         958 :         SD_1 = 1;
    1847         958 :         move16(); /* Q0 */
    1848             :     }
    1849        3100 :     SD_1_inv = sub( 1, SD_1 ); /* Q0 */
    1850             : 
    1851             :     /* NB "STL::test()";   has a cost of 2,  using  bitwise   "s_and" ,  "s_or"  at a cost of 1 */
    1852             :     /* NB only lowest bit position is used,  result is always  0 or 1  */
    1853             : 
    1854             :     /* bg_bgd3 = enr_bgd || ( ( cns_bgd || lp_bgd ) && ns_mask && lt_haco_mask && SD_1==0 ); */
    1855        3100 :     tmp = s_and( s_and( s_and( s_or( cns_bgd, lp_bgd ), ns_mask ), lt_haco_mask ), SD_1_inv );
    1856        3100 :     bg_bgd3 = s_or( enr_bgd, tmp );
    1857             : 
    1858             :     /*PD_1 = (epsP_2_16_dlp_max < 0.10f ) ; */
    1859        3100 :     PD_1 = 0;
    1860        3100 :     move16();
    1861        3100 :     if ( ( LT_16( epsP_2_16_dlp_max, 410 ) ) ) /* 0.10  in Q12 */
    1862             :     {
    1863           3 :         PD_1 = 1;
    1864           3 :         move16(); /* Q0 */
    1865             :     }
    1866             : 
    1867             :     /*PD_2 = (epsP_0_2_ad_lp_max < 0.10f ) ; */
    1868        3100 :     PD_2 = 0;
    1869        3100 :     move16();
    1870        3100 :     if ( ( LT_16( epsP_0_2_ad_lp_max, 410 ) ) ) /* 0.10  in Q12 */
    1871             :     {
    1872         101 :         PD_2 = 1;
    1873         101 :         move16(); /* Q0 */
    1874             :     }
    1875             : 
    1876             :     /*PD_3 = (comb_ahc_epsP < 0.85f ); */
    1877        3100 :     PD_3 = 0;
    1878        3100 :     move16();
    1879        3100 :     if ( ( LT_16( comb_ahc_epsP, 3482 ) ) ) /* 0.85  in Q12 */
    1880             :     {
    1881          92 :         PD_3 = 1;
    1882          92 :         move16(); /* Q0 */
    1883             :     }
    1884             : 
    1885             :     /* PD_4 = comb_ahc_epsP < 0.15f; */
    1886        3100 :     PD_4 = 0;
    1887        3100 :     move16();
    1888        3100 :     if ( ( LT_16( comb_ahc_epsP, 614 ) ) ) /* 0.15  in Q12 */
    1889             :     {
    1890           0 :         PD_4 = 1;
    1891           0 :         move16(); /* Q0 */
    1892             :     }
    1893             : 
    1894             :     /*PD_5 = comb_hcm_epsP < 0.30f;   */
    1895        3100 :     PD_5 = 0;
    1896        3100 :     move16();
    1897        3100 :     if ( ( LT_16( comb_hcm_epsP, 1229 ) ) ) /* 0.30  in Q12 */
    1898             :     {
    1899           0 :         PD_5 = 1;
    1900           0 :         move16(); /* Q0 */
    1901             :     }
    1902             : 
    1903             :     /* BG_1 = ( (SD_1==0) || (Etot < Etot_l_lp_thr) )
    1904             :                && bg_haco_mask && (st->act_pred < 0.85f) && (st->Etot_lp < 50.0f); */
    1905        3100 :     BG_1 = 0;
    1906        3100 :     move16();
    1907        3100 :     test();
    1908        3100 :     test();
    1909        3100 :     test();
    1910        3100 :     test();
    1911        3100 :     if ( ( ( SD_1 == 0 ) || ( LT_16( Etot, Etot_l_lp_thr ) ) ) && ( bg_haco_mask != 0 ) && ( LT_16( hNoiseEst->act_pred_fx, 27853 ) ) /* 0.85f in Q15 */
    1912           0 :          && ( LT_16( hNoiseEst->Etot_lp_fx, 50 * 256 ) ) )                                                                            /* 50.0 in Q8 */
    1913             :     {
    1914           0 :         BG_1 = 1;
    1915           0 :         move16();
    1916             :     }
    1917             : 
    1918             :     /*  PAU = (st->aEn==0)
    1919             :           || ( (Etot < 55.0f) && (SD_1==0)
    1920             :                 && ( ( PD_3 && (PD_1 || PD_2 ) ) ||   ( PD_4 || PD_5 )  ) ); */
    1921        3100 :     PAU = 0;
    1922        3100 :     move16(); /*Q0*/
    1923        3100 :     if ( hNoiseEst->aEn == 0 )
    1924             :     {
    1925           2 :         PAU = 1;
    1926           2 :         move16(); /*Q0*/
    1927             :     }
    1928        3100 :     tmp = 0;
    1929        3100 :     move16();                      /*Q0*/
    1930        3100 :     if ( LT_16( Etot, 55 * 256 ) ) /*55.0 in Q8 */
    1931             :     {
    1932        2191 :         tmp = 1;
    1933        2191 :         move16(); /*Q0*/
    1934             :     }
    1935        3100 :     tmp = s_and( tmp, SD_1_inv );
    1936        3100 :     PAU = s_or( PAU, s_and( tmp, s_or( s_and( PD_3, s_or( PD_1, PD_2 ) ), s_or( PD_4, PD_5 ) ) ) );
    1937             : 
    1938             : 
    1939             :     /* NEW_POS_BG = (PAU | BG_1) & bg_bgd3;   note bitwise logic in float */
    1940        3100 :     NEW_POS_BG = s_and( s_or( PAU, BG_1 ), bg_bgd3 );
    1941             : 
    1942             :     /* Original silence detector works in most cases */
    1943             :     /* aE_bgd = (st->aEn == 0);*/
    1944        3100 :     aE_bgd = 0;
    1945        3100 :     move16();
    1946        3100 :     if ( hNoiseEst->aEn == 0 )
    1947             :     {
    1948           2 :         aE_bgd = 1;
    1949           2 :         move16();
    1950             :     }
    1951             : 
    1952             : 
    1953             :     /* When the signal dynamics is high and the energy is close to the background estimate */
    1954             :     /* sd1_bgd =     (st->sign_dyn_lp > 15)
    1955             :                   && (Etot - st->Etot_l_lp ) < 2*st->Etot_v_h2
    1956             :                   && st->harm_cor_cnt > 20; */
    1957        3100 :     sd1_bgd = 0;
    1958        3100 :     move16();
    1959        3100 :     test();
    1960        3100 :     test();
    1961        3100 :     if ( ( GT_16( hNoiseEst->sign_dyn_lp_fx, 15 * 256 ) )                          /* 15 in Q8  */
    1962        3061 :          && ( LT_16( sub( Etot, hNoiseEst->Etot_l_lp_fx ), shl( Etot_v_h2, 1 ) ) ) /*  Q8 , Etot_v_h2 has limited dynmics can be upscaled*/
    1963         185 :          && ( GT_16( *st_harm_cor_cnt, 20 ) ) )
    1964             :     {
    1965           0 :         sd1_bgd = 1;
    1966           0 :         move16();
    1967             :     }
    1968             : 
    1969             :     /* tn_ini = st->ini_frame < 150 && st->harm_cor_cnt > 5 &&
    1970             :                                  ( (st->act_pred < 0.59f && st->lt_haco_ev <0.23f ) ||
    1971             :                                     st->act_pred < 0.38f ||
    1972             :                                     st->lt_haco_ev < 0.15f ||
    1973             :                                     non_staB < 50.0f ||
    1974             :                                     aE_bgd );*/
    1975             : 
    1976        3100 :     tmp = 0;
    1977        3100 :     move16();
    1978        3100 :     test();
    1979        3100 :     test();
    1980        3100 :     test();
    1981        3100 :     test();
    1982        3100 :     test();
    1983        3100 :     test();
    1984        3100 :     test();
    1985        3100 :     test();
    1986        3100 :     test();
    1987        3100 :     test();
    1988        3100 :     test();
    1989        3100 :     test();
    1990        3100 :     if ( ( ( LT_16( hNoiseEst->act_pred_fx, 19333 ) ) && ( LT_16( hNoiseEst->lt_haco_ev_fx, 7537 ) ) )                                                                          /* .59 in Q15 .23 in Q15 */
    1991        3100 :          || ( LT_16( hNoiseEst->act_pred_fx, 12452 ) )                                                                                                                          /* .38 in Q15 */
    1992        3100 :          || ( ( st_fx->element_mode == EVS_MONO && LT_16( hNoiseEst->lt_haco_ev_fx, 4915 ) ) || ( st_fx->element_mode > EVS_MONO && LT_16( hNoiseEst->lt_haco_ev_fx, 2621 ) ) ) /* .15 in Q15 || 0.08 */
    1993        3100 :          || ( LT_16( non_staB, 50 * 256 ) )                                                                                                                                     /* 50.0 in Q8  */
    1994        2273 :          || aE_bgd != 0 || ( ( LT_16( Etot, 10752 ) )                                                                                                                           /* 42 in Q8 */
    1995         388 :                              && ( GT_16( hNoiseEst->harm_cor_cnt, 10 ) ) && ( LT_16( hNoiseEst->lt_haco_ev_fx, 11469 ) )                                                        /* 0.35 in Q15 */
    1996           0 :                              && ( LT_16( hNoiseEst->act_pred_fx, 26214 ) )                                                                                                      /* 0.80 in Q15 */
    1997             :                              ) )
    1998             :     {
    1999         827 :         tmp = 1;
    2000         827 :         move16();
    2001             :     }
    2002             : 
    2003        3100 :     tn_ini = 0;
    2004        3100 :     move16();
    2005        3100 :     test();
    2006        3100 :     test();
    2007        3100 :     test();
    2008        3100 :     if ( ( LT_16( st_fx->ini_frame, HE_LT_CNT_INIT_FX ) ) && ( GT_16( hNoiseEst->harm_cor_cnt, 5 ) ) /* > 5  Q0 */
    2009           0 :          && ( LT_16( sub( Etot, hNoiseEst->Etot_lp_fx ), 1792 ) )                                    /* 7 in Q8 */
    2010           0 :          && ( NE_16( tmp, 0 ) ) )
    2011             :     {
    2012           0 :         tn_ini = 1;
    2013           0 :         move16();
    2014             :     }
    2015             : 
    2016             :     /* Energy close to the background estimate serves as a mask for other background detectors */
    2017             :     /* bg_bgd2 = Etot < Etot_l_lp_thr || tn_ini ; */
    2018        3100 :     bg_bgd2 = 0;
    2019        3100 :     move16();
    2020        3100 :     test();
    2021        3100 :     if ( ( LT_16( Etot, Etot_l_lp_thr ) ) || ( tn_ini != 0 ) )
    2022             :     {
    2023         229 :         bg_bgd2 = 1;
    2024         229 :         move16(); /* Q0 */
    2025             :     }
    2026             : 
    2027        3100 :     updt_step = 0;
    2028        3100 :     move16(); /* Q15 */
    2029             :     /*if (( bg_bgd2 && ( aE_bgd || sd1_bgd || st->lt_tn_track >0.90f || NEW_POS_BG ) )
    2030             :          ||  tn_ini ) */
    2031        3100 :     tmp = 0;
    2032        3100 :     move16();
    2033        3100 :     if ( GT_16( hNoiseEst->lt_tn_track_fx, 29491 ) ) /* .90 in Q15 */
    2034             :     {
    2035           0 :         tmp = 1;
    2036           0 :         move16();
    2037             :     }
    2038             : 
    2039        3100 :     IF( s_or( s_and( bg_bgd2, s_or( aE_bgd, s_or( sd1_bgd, s_or( tmp, NEW_POS_BG ) ) ) ), tn_ini ) )
    2040             :     {
    2041             :         /*if( (  ( st->act_pred < 0.85f )
    2042             :                && (aE_bgd !=0)
    2043             :                && ( st->lt_Ellp_dist < 10  || sd1_bgd )
    2044             :                && (st->lt_tn_dist<40)
    2045             :                && ( ( Etot - st->totalNoise ) < 10.0f )
    2046             :              )
    2047             :                || ( (st->first_noise_updt == 0)  && (st->harm_cor_cnt > 80)  && (aE_bgd!=0) && (st->lt_aEn_zero > 0.5f) )
    2048             :                || ( (tn_ini!=0) && ( aE_bgd != 0)  || (non_staB < 10.0) || (st->harm_cor_cnt > 80) )
    2049             :                )*/
    2050             : 
    2051           0 :         test();
    2052           0 :         test();
    2053           0 :         test();
    2054           0 :         test();
    2055           0 :         test();
    2056           0 :         test();
    2057           0 :         test(); /* for the ELSE IF below*/
    2058           0 :         test();
    2059           0 :         test();
    2060           0 :         test();
    2061           0 :         test();
    2062           0 :         test();
    2063           0 :         test(); /* for the ELSE IF below*/
    2064             : 
    2065           0 :         test();
    2066           0 :         test();
    2067           0 :         test();
    2068           0 :         test();
    2069           0 :         test();
    2070           0 :         test();
    2071           0 :         test();
    2072           0 :         test();
    2073           0 :         test();
    2074           0 :         test();
    2075           0 :         test();
    2076           0 :         test();
    2077           0 :         test();
    2078           0 :         test();
    2079           0 :         test();
    2080           0 :         IF( ( ( LT_16( hNoiseEst->act_pred_fx, 27853 ) )                                                                    /* 0.85 in Q15 */
    2081             :               && ( NE_16( aE_bgd, 0 ) ) && ( ( LT_16( hNoiseEst->lt_Ellp_dist_fx, 10 * 256 ) ) || ( NE_16( sd1_bgd, 0 ) ) ) /* 10.0 in Q8*/
    2082             :               && ( LT_16( hNoiseEst->lt_tn_dist_fx, 40 * 256 ) )                                                            /* 40.0 in Q8*/
    2083             :               && ( LT_16( sub( Etot, hNoiseEst->totalNoise_fx ), 10 * 256 ) )                                               /* 10.0 in Q8*/
    2084             :               ) ||
    2085             :             ( ( hNoiseEst->first_noise_updt == 0 ) && ( GT_16( hNoiseEst->harm_cor_cnt, 80 ) ) && ( aE_bgd != 0 ) && ( GT_16( hNoiseEst->lt_aEn_zero_fx, 16384 ) ) /*.5 in Q15*/
    2086             :               ) ||
    2087             :             ( ( tn_ini != 0 ) && ( ( aE_bgd != 0 ) || ( LT_16( non_staB, 10 * 256 ) ) || ( GT_16( hNoiseEst->harm_cor_cnt, 80 ) ) ) /* 10.0 in Q8*/
    2088             :               ) )
    2089             : 
    2090             :         {
    2091           0 :             updt_step = 32767;
    2092           0 :             move16();
    2093           0 :             hNoiseEst->first_noise_updt = 1;
    2094           0 :             move16();
    2095           0 :             FOR( i = 0; i < NB_BANDS; i++ )
    2096             :             {
    2097           0 :                 hNoiseEst->bckr_fx[i] = tmpN[i];
    2098           0 :                 move32();
    2099             :             }
    2100             :         }
    2101             :         /* else if ( ( ( st->act_pred < 0.80f ) && ( aE_bgd  || PAU )  &&  st->lt_haco_ev < 0.10f )
    2102             :                      || ( ( st->act_pred < 0.70f ) && ( aE_bgd || non_staB < 17.0f ) && PAU &&  st->lt_haco_ev < 0.15f )
    2103             :                      || ( st->harm_cor_cnt > 80 && st->totalNoise > 5.0f && Etot < max(1.0f,Etot_l_lp + 1.5f* st->Etot_v_h2) )
    2104             :                   ||
    2105             :                   ( st->harm_cor_cnt > 50 && st->first_noise_updt > 30 && aE_bgd  && st->lt_aEn_zero>0.5f )
    2106             :                   || tn_ini
    2107             :                 ) */
    2108           0 :         ELSE IF( ( ( LT_16( hNoiseEst->act_pred_fx, 26214 ) )                                              /* .8 in Q15*/
    2109             :                    && ( ( aE_bgd != 0 ) || ( PAU != 0 ) ) && ( LT_16( hNoiseEst->lt_haco_ev_fx, 3277 ) ) ) /* .10 in q15*/
    2110             :                  || ( ( LT_16( hNoiseEst->act_pred_fx, 22938 ) )                                           /* 0.70 in Q15 */
    2111             :                       && ( ( aE_bgd != 0 ) || ( LT_16( non_staB, 17 * 256 ) ) )                            /* 17.0 in Q8 */
    2112             :                       && ( PAU != 0 ) && ( LT_16( hNoiseEst->lt_haco_ev_fx, 4915 ) )                       /* 0.15 in Q15 */
    2113             :                       ) ||
    2114             :                  ( ( GT_16( hNoiseEst->harm_cor_cnt, 80 ) ) && ( GT_16( hNoiseEst->totalNoise_fx, 5 * 256 ) )                                           /* 5.0 in Q8 */
    2115             :                    && ( LT_16( Etot, s_max( (Word16) 1 * 256, add( Etot_l_lp, add( hNoiseEst->Etot_v_h2_fx, shr( hNoiseEst->Etot_v_h2_fx, 1 ) ) ) ) ) ) /* 1.5= 1.0+.5 */
    2116             :                    ) ||
    2117             :                  ( ( GT_16( hNoiseEst->harm_cor_cnt, 50 ) ) && ( GT_16( hNoiseEst->first_noise_updt, 30 ) ) && ( aE_bgd != 0 ) && ( GT_16( hNoiseEst->lt_aEn_zero_fx, 16384 ) ) ) /*.5 in Q15*/
    2118             :                  || ( tn_ini != 0 ) )
    2119             : 
    2120             :         {
    2121           0 :             updt_step = 3277;
    2122           0 :             move16(); /* 0.1 in Q15 */
    2123             :             /* if ( !aE_bgd &&  st->harm_cor_cnt < 50
    2124             :                    &&  ( (st->act_pred > 0.6f)
    2125             :                          || ( (tn_ini==0) && (Etot_l_lp - st->totalNoise < 10.0f)  && non_staB > 8.0f )
    2126             :                         )
    2127             :                  )
    2128             :              */
    2129           0 :             test();
    2130           0 :             test();
    2131           0 :             test();
    2132           0 :             test();
    2133           0 :             test();
    2134           0 :             IF( ( aE_bgd == 0 ) && ( LT_16( hNoiseEst->harm_cor_cnt, 50 ) ) && ( ( GT_16( hNoiseEst->act_pred_fx, 19661 ) )                                                /* 0.6 in Q15*/
    2135             :                                                                                  || ( ( tn_ini == 0 ) && ( LT_16( sub( Etot_l_lp, hNoiseEst->totalNoise_fx ), 10 * 256 ) ) /* 10.0 in Q8 */
    2136             :                                                                                       && ( GT_16( non_staB, 8 * 256 ) )                                                    /*  8.0 in in Q8*/
    2137             :                                                                                       ) ) )
    2138             :             {
    2139           0 :                 updt_step = 328;
    2140           0 :                 move16(); /* 0.01 Q15 */
    2141             :             }
    2142             :             /*
    2143             :               IF (updt_step > 0  )
    2144             :               {
    2145             :               */
    2146           0 :             hNoiseEst->first_noise_updt = 1;
    2147           0 :             move16();
    2148           0 :             FOR( i = 0; i < NB_BANDS; i++ )
    2149             :             {
    2150             :                 /* st->bckr[i] = st->bckr[i] + updt_step * (tmpN[i]-st->bckr[i]);*/
    2151             :                 /* 32 bit state update */
    2152           0 :                 Ltmp = L_sub( tmpN[i], hNoiseEst->bckr_fx[i] ); /*Q_new+Q_SCALE*/
    2153           0 :                 Ltmp = Mult_32_16( Ltmp, updt_step );           /* Q_new+Q_SCALE+15+1  -16*/
    2154           0 :                 hNoiseEst->bckr_fx[i] = L_add( Ltmp, hNoiseEst->bckr_fx[i] );
    2155           0 :                 move32();
    2156             :             }
    2157             :             /*
    2158             :              } */
    2159             :         }
    2160             :         /*else if (aE_bgd || st->harm_cor_cnt > 100 )*/
    2161           0 :         ELSE IF( ( aE_bgd != 0 ) || ( GT_16( hNoiseEst->harm_cor_cnt, 100 ) ) )
    2162             :         {
    2163           0 :             hNoiseEst->first_noise_updt = add( hNoiseEst->first_noise_updt, 1 );
    2164           0 :             move16();
    2165             :         }
    2166             :     }
    2167             :     ELSE
    2168             :     {
    2169             :         /* If in music lower bckr to drop further */
    2170        3100 :         test();
    2171        3100 :         test();
    2172        3100 :         IF( ( GT_16( hNoiseEst->low_tn_track_cnt, 300 ) ) && ( GT_16( hNoiseEst->lt_haco_ev_fx, 29491 ) ) /*.9 in Q15 */
    2173             :             && ( hNoiseEst->totalNoise_fx > 0 ) )
    2174             :         {
    2175           0 :             updt_step = -655;
    2176           0 :             move16(); /* for debug purposes */
    2177           0 :             FOR( i = 0; i < NB_BANDS; i++ )
    2178             :             {
    2179           0 :                 IF( GT_32( hNoiseEst->bckr_fx[i], L_shl( Le_min_scaled, 1L ) ) ) /* 2*E_MIN(float) in float,  here we use  2*Le_min_scaled  Q_new+Q_SCALE  */
    2180             :                 {
    2181             :                     /* st->bckr[i] = 0.98f*st->bckr[i];  */
    2182           0 :                     hNoiseEst->bckr_fx[i] = Mult_32_16( hNoiseEst->bckr_fx[i], 32113 ); /* .98 in Q15 */
    2183           0 :                     move32();                                                           /* move to array */
    2184             :                 }
    2185             :             }
    2186             :         }
    2187             :         /*st->lt_aEn_zero = 0.2f * (st->aEn==0) + (1-0.2f)  *st->lt_aEn_zero;*/
    2188             :         /*  y(n+1)=        alpha*tmp            + (1-alpha)*y(n)           */
    2189        3100 :         tmp = 0;
    2190        3100 :         move16();
    2191        3100 :         if ( hNoiseEst->aEn == 0 )
    2192             :         {
    2193           2 :             tmp = 32767;
    2194           2 :             move16();
    2195             :         }
    2196        3100 :         hNoiseEst->lt_aEn_zero_fx = noise_est_AR1_Qx( tmp, hNoiseEst->lt_aEn_zero_fx, 6554 ); /* alpha=0.2 , Q15 */
    2197        3100 :         move16();
    2198             :     }
    2199             : 
    2200        3100 :     return;
    2201             : }
    2202             : 
    2203             : /*-----------------------------------------------------------------*
    2204             :  * noise_est_fx()
    2205             :  *
    2206             :  * Noise energy estimation (noise energy is updated in case of noise-only frame)
    2207             :  *-----------------------------------------------------------------*/
    2208     1192920 : void noise_est_ivas_fx(
    2209             :     Encoder_State *st_fx,                 /* i/o: state structure                                                     */
    2210             :     const Word16 old_pitch1,              /* i  : previous frame OL pitch[1]                                          */
    2211             :     const Word32 tmpN[],                  /* i  : temporary noise update                            Q_new + QSCALE    */
    2212             :     const Word32 epsP[],                  /* i  : msb prediction error energies                        Qx             */
    2213             :     const Word16 Etot,                    /* i  : total channel E (see find_enr_fx.c)                  Q8             */
    2214             :     const Word16 relE,                    /* i  : (VA_CHECK addition) relative frame energy            Q8?            */
    2215             :     const Word16 corr_shift,              /* i  : normalized correlation correction                    Q15            */
    2216             :     const Word32 enr[],                   /* i  : averaged energy over both subframes               q_enr             */
    2217             :     const Word16 q_enr,                   /* i  : q_enr of enr                                      Q0                */
    2218             :     Word32 fr_bands[],                    /* i  : spectrum per critical bands of the current frame  q_fr_bands        */
    2219             :     Word16 q_fr_bands,                    /* i  : Q of q_fr_bands                                                     */
    2220             :     Word16 *cor_map_sum,                  /* o  :                                                         Q8          */
    2221             :     Word16 *ncharX,                       /* o  :                                                         Q11         */
    2222             :     Word16 *sp_div,                       /* o  :                                                      Q_sp_div       */
    2223             :     Word16 *Q_sp_div,                     /* o  :    Q factor for sp_div                                              */
    2224             :     Word32 *non_staX,                     /* o  : non-stationarity for sp/mus classifier               Q20             */
    2225             :     Word16 *loc_harm,                     /* o  :   multi-harmonicity flag for UV classifier                          */
    2226             :     const Word32 *lf_E,                   /* i  : per bin energy  for low frequencies                  q_lf_E         */
    2227             :     const Word16 q_lf_E,                  /* i  : Q of lf_E                                            Q0             */
    2228             :     Word16 *st_harm_cor_cnt,              /* i/o  : 1st harm correlation timer                         Q0             */
    2229             :     const Word16 Etot_l_lp,               /* i    : Smoothed low energy                                Q8             */
    2230             :     const Word32 Etot_v_h2,               /* i    : Energy variations                                  Q24            */
    2231             :     Word16 *bg_cnt,                       /* i    : Background burst length timer                      Q0             */
    2232             :     Word16 EspecdB[],                     /* i/o:  log E spectrum (with f=0) of the current frame Q7  for multi harm  */
    2233             :     Word16 *sp_floor,                     /* o  : noise floor estimate                                 Q7             */
    2234             :     Word16 S_map[],                       /* o  : short-term correlation map                           Q7             */
    2235             :     STEREO_CLASSIF_HANDLE hStereoClassif, /* i/o: stereo classifier structure                                       */
    2236             :     FRONT_VAD_ENC_HANDLE hFrontVad,       /* i/o: front-VAD handle                                                  */
    2237             :     const Word16 ini_frame                /* i  : Frame number (init)                                                */
    2238             : )
    2239             : {
    2240             :     Word16 alpha, alpha2, alpha2m1, alpham1;
    2241             :     Word16 cor_min, cor_max, num, den, ExpNum, ExpDen, noise_chartmp;
    2242             :     Word16 wtmp1, wtmp, nchar_thr, cor_tmp;
    2243             :     Word16 i, tmp_pc, pc, th_eps;
    2244             :     Word32 th_sta, Lnum, Lden, non_sta, LepsP;
    2245             :     Word16 e_ener, f_ener;
    2246             :     Word32 Ltmp, Ltmp1, Lsum_num, Lsum_den, *pt1, *pt2, Ltmp2, Lnon_sta2;
    2247             :     Word64 w_sum_num, w_tmp;
    2248             :     Word16 spec_div, noise_char;
    2249             :     Word16 log_enr16;
    2250             :     Word16 updt_step; /* Q15 */
    2251             :     Word16 aE_bgd, sd1_bgd, bg_bgd2;
    2252             :     Word16 tn_ini;
    2253             :     Word16 epsP_0_2, epsP_0_2_ad, epsP_0_2_ad_lp_max;
    2254             :     Word16 epsP_2_16, epsP_2_16_dlp, epsP_2_16_dlp_max;
    2255             :     Word16 PAU, BG_1, NEW_POS_BG;
    2256             : 
    2257             :     Word16 haco_ev_max;
    2258             :     Word16 Etot_l_lp_thr;
    2259             :     Word16 comb_ahc_epsP, comb_hcm_epsP;
    2260             : 
    2261             :     Word16 enr_bgd, cns_bgd, lp_bgd, ns_mask;
    2262             :     Word16 lt_haco_mask, bg_haco_mask;
    2263             :     Word16 SD_1, bg_bgd3, PD_1, PD_2, PD_3, PD_4, PD_5;
    2264             : 
    2265             :     Word16 non_staB; /* Q8 */
    2266             :     Word32 L_tmp_enr, L_tmp_ave, L_tmp_ave2;
    2267             :     Word16 tmp, tmp2;          /* general temp registers */
    2268             :     Word16 tmp_enr, tmp_floor; /* constants in Q8 */
    2269             :     Word16 vad_bwidth_fx;      /* vad ns  control variabel for input bwidth from teh BWD  */
    2270             :     /* for DTX operation */
    2271             :     Word32 L_tmp;
    2272             : 
    2273             :     Word16 lim_Etot_fx;    /* Q8 */
    2274             :     Word32 lim_Etot_sq_fx; /* Q16 */
    2275             :     Word32 st_E_var_est_fx;
    2276             :     NOISE_EST_HANDLE hNoiseEst;
    2277             :     SP_MUS_CLAS_HANDLE hSpMusClas;
    2278             :     Word32 Le_min_scaled;
    2279             :     Word64 temp;
    2280     1192920 :     hSpMusClas = st_fx->hSpMusClas;
    2281             : 
    2282     1192920 :     Le_min_scaled = L_shl( E_MIN_FXQ31, sub( q_fr_bands, Q31 ) ); // q_fr_bands
    2283             : 
    2284     1192920 :     GSC_ENC_HANDLE hGSCEnc = st_fx->hGSCEnc;
    2285             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    2286     1192920 :     Flag Overflow = 0;
    2287     1192920 :     move32();
    2288             : #endif
    2289             : 
    2290             :     /* Check if LR-VAD */
    2291     1192920 :     IF( hFrontVad != NULL )
    2292             :     {
    2293       81726 :         hNoiseEst = hFrontVad->hNoiseEst;
    2294             :     }
    2295             :     ELSE
    2296             :     {
    2297     1111194 :         hNoiseEst = st_fx->hNoiseEst;
    2298             :     }
    2299             : 
    2300             :     /*-----------------------------------------------------------------*
    2301             :      * Initialization
    2302             :      *-----------------------------------------------------------------*/
    2303     1192920 :     vad_bwidth_fx = st_fx->input_bwidth;
    2304     1192920 :     move16();
    2305             : 
    2306             : 
    2307             :     /*st_fx->ener_RAT = 10.0f * (float)log10( mean(lf_E, 8));*/
    2308     1192920 :     temp = 0;
    2309     1192920 :     move64();
    2310     1192920 :     IF( hFrontVad == NULL )
    2311             :     {
    2312     1111194 :         IF( hSpMusClas != NULL )
    2313             :         {
    2314             :             /* E = mean( lf_E, 8 ); */
    2315    10000746 :             FOR( i = 0; i < 8; i++ )
    2316             :             {
    2317     8889552 :                 temp = W_mac_32_16( temp, lf_E[i], 1 ); // q_lf_E+1
    2318             :             }
    2319             :             /* (temp / 8) */
    2320     1111194 :             Ltmp = W_extract_l( W_shr( temp, 4 ) ); // q_lf_E+1 -> q_lf_E
    2321             : 
    2322     1111194 :             IF( LT_32( Ltmp, L_shl_sat( 1, q_lf_E ) ) )
    2323             :             {
    2324      143918 :                 hSpMusClas->ener_RAT_fx = 0;
    2325             :             }
    2326             :             ELSE
    2327             :             {
    2328      967276 :                 e_ener = norm_l( Ltmp );
    2329      967276 :                 f_ener = Log2_norm_lc( L_shl( Ltmp, e_ener ) );
    2330      967276 :                 e_ener = sub( 30, e_ener );
    2331      967276 :                 e_ener = sub( e_ener, q_lf_E );
    2332      967276 :                 Ltmp = L_mac( L_deposit_h( e_ener ), f_ener, 1 ); // Q16
    2333      967276 :                 Ltmp = Mpy_32_16_1( Ltmp, LG10 );                 // Q14 (16+13-15)
    2334      967276 :                 Ltmp = L_shl( Ltmp, 10 );                         // Q24
    2335      967276 :                 wtmp = round_fx( Ltmp );                          /*Q8*/
    2336             : 
    2337             :                 /* st_fx->ener_RAT /= (Etot + 0.01f);
    2338             :                    if ( st->hSpMusClas->ener_RAT > 1.0 )
    2339             :                    {
    2340             :                      st->hSpMusClas->ener_RAT = 1.0f;
    2341             :                    }
    2342             :                 */
    2343             : 
    2344      967276 :                 wtmp1 = add( Etot, 3 ); /* 0.01f in Q8  */
    2345      967276 :                 hSpMusClas->ener_RAT_fx = 0;
    2346      967276 :                 move16();
    2347      967276 :                 IF( wtmp > 0 )
    2348             :                 {
    2349      967270 :                     hSpMusClas->ener_RAT_fx = 32767;
    2350      967270 :                     move16(); /*Q15*/
    2351      967270 :                     IF( GE_16( wtmp1, wtmp ) )
    2352             :                     {
    2353      967270 :                         hSpMusClas->ener_RAT_fx = div_s( wtmp, wtmp1 ); /*Q15*/ /* wtmp1 gte than wtmp */
    2354      967270 :                         move16();
    2355             :                     }
    2356             :                 }
    2357             :             }
    2358             :         }
    2359             :     }
    2360             :     /*-----------------------------------------------------------------*
    2361             :      * Set the threshold for eps & non_sta based on input sampling rate
    2362             :      * The reason is that in case of 8kHz sampling input, there is nothing
    2363             :      * between 4kHz-6.4kHz. In noisy conditions, this makes a fast
    2364             :      * transition even in noise-only parts, hence producing a "higher
    2365             :      * order" spectral envelope => the epsP ratio is much less effective.
    2366             :      *-----------------------------------------------------------------*/
    2367             : 
    2368     1192920 :     IF( vad_bwidth_fx != NB ) /* WB input */
    2369             :     {
    2370     1188630 :         th_eps = TH_EPS16_FX;
    2371     1188630 :         move16(); /*Q11*/
    2372     1188630 :         th_sta = TH_STA16_FX;
    2373     1188630 :         move16(); /*Q10  */
    2374     1188630 :         cor_min = COR_MIN16_FX;
    2375     1188630 :         move16(); /*Q15*/
    2376     1188630 :         cor_max = COR_MAX16_FX;
    2377     1188630 :         move16(); /*Q15*/
    2378             :     }
    2379             :     ELSE /* NB input */
    2380             :     {
    2381        4290 :         th_eps = TH_EPS8_FX;
    2382        4290 :         move16(); /* Q11 */
    2383        4290 :         th_sta = TH_STA8_FX;
    2384        4290 :         move16(); /* Q10 */
    2385        4290 :         cor_min = COR_MIN8_FX;
    2386        4290 :         move16();
    2387        4290 :         cor_max = COR_MAX8_FX;
    2388        4290 :         move16();
    2389             :     }
    2390             : 
    2391             : 
    2392             :     /*-----------------------------------------------------------------*
    2393             :      * Estimation of pitch stationarity
    2394             :      *-----------------------------------------------------------------*/
    2395             : 
    2396             :     /* pc = abs(pit[0] - pitO) + abs(pit[1] - pit[0]) */ /* needed in signal_clas() */
    2397     1192920 :     wtmp = abs_s( sub( st_fx->pitch[0], old_pitch1 ) );
    2398     1192920 :     wtmp1 = abs_s( sub( st_fx->pitch[1], st_fx->pitch[0] ) );
    2399     1192920 :     pc = add( wtmp, wtmp1 );
    2400             : 
    2401             : 
    2402     1192920 :     Ltmp = L_deposit_h( corr_shift );
    2403     1192920 :     Ltmp = L_mac( Ltmp, st_fx->voicing_fx[0], 10923 );
    2404     1192920 :     Ltmp = L_mac_o( Ltmp, st_fx->voicing_fx[1], 10923, &Overflow );
    2405     1192920 :     wtmp = mac_ro( Ltmp, st_fx->voicing_fx[2], 10923, &Overflow );
    2406             : 
    2407     1192920 :     tmp_pc = pc;
    2408     1192920 :     move16();
    2409     1192920 :     if ( LT_16( wtmp, cor_min ) )
    2410             :     {
    2411      266673 :         tmp_pc = TH_PC_FX;
    2412      266673 :         move16(); /* low correlation -> probably inactive signal */
    2413             :     }
    2414             : 
    2415             :     /* Update */
    2416             : 
    2417             :     /*-----------------------------------------------------------------*
    2418             :      * Multi-harmonic analysis
    2419             :      *-----------------------------------------------------------------*/
    2420     1192920 :     IF( hFrontVad == NULL )
    2421             :     {
    2422     1111194 :         IF( st_fx->hSpMusClas != NULL )
    2423             :         {
    2424     1111194 :             i = 0;
    2425     1111194 :             move16();
    2426     1111194 :             *loc_harm = multi_harm_ivas_fx( EspecdB, hNoiseEst->old_S_fx, hNoiseEst->cor_map_fx, &hNoiseEst->multi_harm_limit_fx, st_fx->total_brate,
    2427     2222388 :                                             st_fx->bwidth, ( st_fx->hGSCEnc != NULL ) ? &hGSCEnc->cor_strong_limit : &i, &hSpMusClas->mean_avr_dyn_fx, &hSpMusClas->last_sw_dyn_fx, cor_map_sum, sp_floor, S_map );
    2428     1111194 :             move16();
    2429             :         }
    2430             :     }
    2431             :     /*-----------------------------------------------------------------*
    2432             :      * Detection of frames with non-stationary spectral content
    2433             :      *-----------------------------------------------------------------*/
    2434             :     /* weighted sum of spectral changes per critical bands */
    2435     1192920 :     w_sum_num = 0;
    2436     1192920 :     move64();
    2437     1192920 :     Lsum_den = L_deposit_l( 0 );
    2438             : 
    2439     1192920 :     pt1 = fr_bands + 10;
    2440     1192920 :     pt2 = hNoiseEst->fr_bands2_fx + 10;
    2441             :     Word64 w_sum_den;
    2442             :     Word16 exp, exp2;
    2443     1192920 :     w_sum_den = 0;
    2444     1192920 :     move64();
    2445    13109328 :     FOR( i = 10; i <= st_fx->max_band; i++ )
    2446             :     {
    2447    11916408 :         Lnum = L_max( *pt1, *pt2 );
    2448    11916408 :         Lden = L_min( *pt1, *pt2 );
    2449             : 
    2450    11916408 :         w_sum_den = W_mac_32_16( w_sum_den, Lnum, 1 ); // q_fr_bands+1
    2451             : 
    2452    11916408 :         exp = sub( norm_l( Lnum ), 1 );
    2453    11916408 :         Lnum = L_shl( Lnum, exp );                  // q_fr_bands+exp
    2454    11916408 :         num = extract_h( Mpy_32_32( Lnum, Lnum ) ); // 2*(q_fr_bands+exp)-31-16
    2455    11916408 :         ExpNum = add( sub( shl( q_fr_bands, 1 ), 47 ), shl( exp, 1 ) );
    2456             : 
    2457             : 
    2458    11916408 :         den = E_MIN_FXQ31 >> 8; // 29360, 0.0035f in Q23
    2459    11916408 :         ExpDen = Q23;
    2460    11916408 :         move32();
    2461    11916408 :         move16();
    2462             : 
    2463    11916408 :         IF( Lden != 0 )
    2464             :         {
    2465    11846898 :             exp = norm_l( Lden );
    2466    11846898 :             den = extract_h( L_shl( Lden, exp ) ); // q_fr_bands+ExpDen-16
    2467    11846898 :             ExpDen = sub( add( q_fr_bands, exp ), Q16 );
    2468             :         }
    2469             : 
    2470    11916408 :         num = div_s( num, den );                                        // Q15+ExpNum-ExpDen
    2471    11916408 :         w_tmp = W_shl( num, sub( q_fr_bands, sub( ExpNum, ExpDen ) ) ); // q_fr_bands+15
    2472    11916408 :         w_sum_num = W_add( w_sum_num, w_tmp );
    2473             : 
    2474    11916408 :         pt1++;
    2475    11916408 :         pt2++;
    2476             :     }
    2477             : 
    2478     1192920 :     ExpNum = W_norm( w_sum_num );
    2479     1192920 :     Lsum_num = W_extract_h( W_shl( w_sum_num, ExpNum ) ); // q_fr_bands+15+ExpNum-32
    2480     1192920 :     ExpNum = add( q_fr_bands, sub( ExpNum, 17 ) );
    2481             : 
    2482     1192920 :     ExpDen = W_norm( w_sum_den );
    2483     1192920 :     Lsum_den = W_extract_h( W_shl( w_sum_den, ExpDen ) ); // q_fr_bands+1+ExpDen-32
    2484     1192920 :     ExpDen = add( add( q_fr_bands, 1 ), sub( ExpDen, 32 ) );
    2485             : 
    2486             :     /* calculation of spectral diversity */
    2487             :     /* THR_SPDIV_FX = 5 , 1/5 Q15 =  6554 */
    2488     1192920 :     spec_div = 0;
    2489     1192920 :     move16();
    2490     1192920 :     if ( GT_32( Mult_32_16( Lsum_num, 6554 ), L_shl_sat( Lsum_den, sub( ExpNum, ExpDen ) ) ) ) /* Qx+Q15+1-16  ==> Qx */
    2491             :     {
    2492      388650 :         spec_div = 1;
    2493      388650 :         move16();
    2494             :     }
    2495             : 
    2496             :     /* *sp_div = Lsum_num / (Lsum_den + 1e-5f); */
    2497     1192920 :     IF( Lsum_den == 0 )
    2498             :     {
    2499           0 :         Lsum_den = 1407374884; // 1e-5 in Q47
    2500           0 :         ExpDen = 47;
    2501           0 :         move32();
    2502           0 :         move16();
    2503             :     }
    2504             : 
    2505     1192920 :     *sp_div = BASOP_Util_Divide3232_Scale( Lsum_num, Lsum_den, &exp );
    2506     1192920 :     move16();
    2507     1192920 :     *Q_sp_div = add( sub( 15, exp ), sub( ExpNum, ExpDen ) );
    2508     1192920 :     move16();
    2509             : 
    2510             :     /*-----------------------------------------------------------------*
    2511             :      * Detection of frames with high energy content in high frequencies
    2512             :      *-----------------------------------------------------------------*/
    2513             : 
    2514     1192920 :     pt1 = &fr_bands[st_fx->min_band];
    2515     1192920 :     pt2 = &fr_bands[10];
    2516     1192920 :     w_sum_num = 0;
    2517     1192920 :     w_sum_den = 0;
    2518     1192920 :     move64();
    2519     1192920 :     move64();
    2520             : 
    2521             :     /* calculation of energy in first 10 critical bands */
    2522    13117856 :     FOR( i = 0; i < sub( 10, st_fx->min_band ); i++ )
    2523             :     {
    2524    11924936 :         w_sum_den = W_mac_32_16( w_sum_den, *pt1, 1 ); // q_fr_bands+1
    2525    11924936 :         pt1++;
    2526             :     }
    2527     1192920 :     exp = W_norm( w_sum_den );
    2528     1192920 :     Ltmp = W_extract_h( W_shl( w_sum_den, exp ) ); // q_fr_bands+1+exp-32
    2529     1192920 :     exp = sub( add( q_fr_bands, exp ), 31 );
    2530             : 
    2531             :     /* calculation of energy in the rest of bands */
    2532    13109328 :     FOR( i = 0; i < sub( st_fx->max_band, 9 ); i++ )
    2533             :     {
    2534    11916408 :         w_sum_num = W_mac_32_16( w_sum_num, *pt2, 1 );
    2535    11916408 :         pt2++;
    2536             :     }
    2537     1192920 :     exp2 = sub( W_norm( w_sum_num ), 1 );
    2538     1192920 :     Ltmp2 = W_extract_h( W_shl( w_sum_num, exp2 ) ); // q_fr_bands+1+exp2-32
    2539     1192920 :     exp2 = sub( add( q_fr_bands, exp2 ), 31 );
    2540             : 
    2541     1192920 :     test();
    2542     1192920 :     IF( LT_32( L_shr( Ltmp, exp ), 100 ) || LT_32( L_shr( Ltmp2, exp2 ), 100 ) )
    2543             :     {
    2544      472060 :         noise_chartmp = 0;
    2545      472060 :         move16();
    2546             :     }
    2547             :     ELSE
    2548             :     {
    2549             :         /* ftemp2 /= ftemp */
    2550      720860 :         num = div_s( extract_h( Ltmp2 ), extract_h( Ltmp ) );                // 15+exp2-exp
    2551      720860 :         noise_chartmp = shl_o( num, sub( sub( exp, exp2 ), 4 ), &Overflow ); // 15+exp2-exp1 -> Q11
    2552             :     }
    2553             : 
    2554     1192920 :     if ( ncharX != NULL )
    2555             :     {
    2556     1175130 :         *ncharX = noise_chartmp; /* Q11 */
    2557     1175130 :         move16();
    2558             :     }
    2559             : 
    2560     1192920 :     IF( hStereoClassif != NULL )
    2561             :     {
    2562      760851 :         IF( st_fx->idchan == 0 )
    2563             :         {
    2564      410255 :             hStereoClassif->nchar_ch1_fx = noise_chartmp; /* Q11 */
    2565      410255 :             move32();
    2566      410255 :             hStereoClassif->nchar_ch1_e = 31 - Q11;
    2567      410255 :             move16();
    2568             :         }
    2569             :         ELSE
    2570             :         {
    2571      350596 :             hStereoClassif->nchar_ch2_fx = noise_chartmp; /* Q11 */
    2572      350596 :             move32();
    2573      350596 :             hStereoClassif->nchar_ch2_e = 31 - Q11;
    2574      350596 :             move16();
    2575             :         }
    2576             :     }
    2577             : 
    2578     1192920 :     noise_chartmp = s_min( noise_chartmp, 10 << 11 ); /* Q11 */
    2579             : 
    2580             :     /* update LT value of the final parameter */
    2581             :     /* *st_noise_char = M_ALPHA * *st_noise_char + (1-M_ALPHA) * noise_chartmp */
    2582     1192920 :     hNoiseEst->noise_char_fx = mac_r( L_mult( M_ALPHA_FX, hNoiseEst->noise_char_fx ), ONE_MINUS_M_ALPHA, noise_chartmp );
    2583     1192920 :     move16();
    2584             : 
    2585     1192920 :     nchar_thr = THR_NCHAR_WB_FX;
    2586     1192920 :     move16(); /* 1.0 Q11 */
    2587     1192920 :     if ( vad_bwidth_fx == NB )
    2588             :     {
    2589        4290 :         nchar_thr = THR_NCHAR_NB_FX;
    2590        4290 :         move16(); /* 1.0 Q11 */
    2591             :     }
    2592             : 
    2593     1192920 :     noise_char = 0;
    2594     1192920 :     move16();
    2595     1192920 :     if ( GT_16( hNoiseEst->noise_char_fx, nchar_thr ) )
    2596             :     {
    2597      110062 :         noise_char = 1;
    2598      110062 :         move16();
    2599             :     }
    2600             : 
    2601             :     /* save the 2 last spectra per crit. bands for the future */
    2602     1192920 :     Copy32( hNoiseEst->fr_bands1_fx, hNoiseEst->fr_bands2_fx, NB_BANDS ); // q_fr_bands
    2603     1192920 :     Copy32( fr_bands + NB_BANDS, hNoiseEst->fr_bands1_fx, NB_BANDS );     // q_fr_bands
    2604             : 
    2605             :     /*-----------------------------------------------------------------*
    2606             :      * Non-stationarity estimation for each band
    2607             :      * Handicap high E frames in average computing
    2608             :      *-----------------------------------------------------------------*/
    2609             : 
    2610             :     /* set averaging factor */
    2611             :     /* ftemp = relE; */
    2612             :     /* if( ftemp < 0.0f ) { ftemp = 0.0f;     } */
    2613     1192920 :     tmp = s_max( relE, 0 ); /* Q8  */
    2614             : 
    2615             :     /* alpha =  0.064f * ftemp  +  0.75f; */
    2616     1192920 :     Ltmp = Madd_32_16( 12582912 /* 0.75 in Q24*/, 137438953, tmp ); // Q24
    2617     1192920 :     alpha = round_fx_o( L_shl_o( Ltmp, 7, &Overflow ), &Overflow ); /*Q24 +7 --> Q31    Q15*/
    2618             : 
    2619             :     /*if( alpha > 0.999f  {     alpha = 0.999f;} */
    2620     1192920 :     alpha = s_min( alpha, 32735 );            /*.999 in Q15*/
    2621     1192920 :     alpham1 = negate( add( -32768, alpha ) ); /* 1.0 - alpha  */
    2622             :     /*--------------------------------------------------------------*
    2623             :      * during significant attacks, replace the LT energy by the
    2624             :      * current energy this will cause non_sta2 failures to occur in
    2625             :      * different frames than non_sta failures
    2626             :      *--------------------------------------------------------------*/
    2627             : 
    2628     1192920 :     alpha2 = alpha;
    2629     1192920 :     move16();
    2630     1192920 :     alpha2m1 = alpham1;
    2631     1192920 :     move16();
    2632     1192920 :     IF( spec_div > 0 )
    2633             :     {
    2634      388650 :         alpha2 = 0;
    2635      388650 :         move16();
    2636      388650 :         alpha2m1 = 32767;
    2637      388650 :         move16();
    2638             :     }
    2639     1192920 :     Lnon_sta2 = L_deposit_l( 1 << 10 ); // Q10
    2640             : 
    2641     1192920 :     non_sta = L_deposit_l( 1 << 10 ); // Q10
    2642     1192920 :     *non_staX = 0;
    2643     1192920 :     move16();
    2644     1192920 :     non_staB = 0;
    2645     1192920 :     move16();
    2646             : 
    2647     1192920 :     Le_min_scaled = L_shl( E_MIN_FXQ31, sub( q_enr, Q31 ) ); // q_enr
    2648             : 
    2649    25034264 :     FOR( i = st_fx->min_band; i <= st_fx->max_band; i++ )
    2650             :     {
    2651    23841344 :         Ltmp = L_shl( 1, q_enr ); // q_enr
    2652             :         /*  + 1.0f added to reduce sensitivity to non stationarity in low energies  */
    2653             :         /* tmp_enr = enr[i] + 1.0f; */
    2654    23841344 :         L_tmp_enr = L_add( enr[i], Ltmp ); // q_enr
    2655             : 
    2656    23841344 :         IF( LE_32( non_sta, th_sta ) ) /* Just to limit the saturation */
    2657             :         {
    2658    16235513 :             L_tmp_ave = L_add( hNoiseEst->ave_enr_fx[i], Ltmp ); // q_enr
    2659             : 
    2660             :             /* if( enr[i] > st_ave_enr2[i] ) */
    2661             :             /* non_sta2 = non_sta2 * ((enr[i]+1) / (st_ave_enr2[i]+1)) */
    2662    16235513 :             Lnum = L_max( L_tmp_enr, L_tmp_ave ); // q_enr
    2663             : 
    2664             :             /* else */
    2665             :             /* non_sta2 = non_sta2 * ((st_ave_enr2[i]+1) / (enr[i]+1)) */
    2666    16235513 :             Lden = L_min( L_tmp_enr, L_tmp_ave ); // q_enr
    2667             : 
    2668    16235513 :             if ( Lden == 0 )
    2669             :             {
    2670           0 :                 Lden = L_max( Ltmp, EPSILON_FX ); // q_enr
    2671             :             }
    2672             : 
    2673    16235513 :             ExpNum = sub( norm_l( Lnum ), 1 );
    2674    16235513 :             num = extract_h( L_shl( Lnum, ExpNum ) ); // q_enr+ExpNum-16
    2675             : 
    2676    16235513 :             ExpDen = norm_l( Lden );
    2677    16235513 :             den = extract_h( L_shl( Lden, ExpDen ) ); // q_enr+ExpDen-16
    2678             : 
    2679    16235513 :             num = div_s( num, den );                                      // 15+ExpNum-ExpDen
    2680    16235513 :             Ltmp1 = Mult_32_16( non_sta, num );                           // 15+ExpNum-ExpDen+10-15
    2681    16235513 :             non_sta = L_shr_o( Ltmp1, sub( ExpNum, ExpDen ), &Overflow ); /* Q10 */
    2682             :         }
    2683             : 
    2684             :         /*    st->ave_enr[i] = alpha * st->ave_enr[i] + (1-alpha) * enr[i];*/ /* update long-term average */
    2685    23841344 :         Ltmp1 = Mult_32_16( hNoiseEst->ave_enr_fx[i], alpha );                // q_enr
    2686    23841344 :         Ltmp1 = Madd_32_16( Ltmp1, enr[i], alpham1 );                         // q_enr
    2687    23841344 :         hNoiseEst->ave_enr_fx[i] = L_max( Le_min_scaled, Ltmp1 );             // q_enr
    2688    23841344 :         move32();
    2689             : 
    2690             :         /* calculation of another non-stationarity measure (following attacks) */
    2691             :         /*if( non_sta2 <= th_sta ){
    2692             :              tmp_ave2 =  st->ave_enr2[i] + 1.0f;
    2693             :              if( tmp_enr > tmp_ave2 ){
    2694             :                  non_sta2 = non_sta2 * ( tmp_enr / tmp_ave2 );
    2695             :              } else {
    2696             :                  non_sta2 = non_sta2 * (tmp_ave2 / tmp_enr );
    2697             :              }
    2698             :          } */
    2699             : 
    2700             :         /* ave_enr2:: calculation of another non-stationarity measure (following attacks)  */
    2701    23841344 :         IF( LE_32( Lnon_sta2, th_sta ) ) /* Just to limit the saturation */
    2702             :         {
    2703    19716820 :             L_tmp_ave2 = L_add( hNoiseEst->ave_enr2_fx[i], Ltmp ); // q_enr
    2704    19716820 :             Lnum = L_max( L_tmp_enr, L_tmp_ave2 );                 // q_enr
    2705    19716820 :             Lden = L_min( L_tmp_enr, L_tmp_ave2 );                 // q_enr
    2706             : 
    2707    19716820 :             if ( Lden == 0 )
    2708             :             {
    2709           0 :                 Lden = L_max( Ltmp, EPSILON_FX ); // q_enr
    2710             :             }
    2711             : 
    2712    19716820 :             ExpNum = sub( norm_l( Lnum ), 1 );
    2713    19716820 :             num = extract_h( L_shl( Lnum, ExpNum ) ); // q_enr+ExpNum-16
    2714             : 
    2715    19716820 :             ExpDen = norm_l( Lden );
    2716    19716820 :             den = extract_h( L_shl( Lden, ExpDen ) ); // q_enr+ExpDen-16
    2717             : 
    2718    19716820 :             num = div_s( num, den );                                        // 15+ExpNum-ExpDen
    2719    19716820 :             Ltmp1 = Mult_32_16( Lnon_sta2, num );                           // 15+ExpNum-ExpDen+10-15
    2720    19716820 :             Lnon_sta2 = L_shr_o( Ltmp1, sub( ExpNum, ExpDen ), &Overflow ); /* Q10 */
    2721             :         }
    2722             : 
    2723             :         /* st_ave_enr2[i] = (float)alpha2 * st_ave_enr2[i] + (1.0f - alpha2) * (enr[i]) */
    2724    23841344 :         Ltmp1 = Mult_32_16( hNoiseEst->ave_enr2_fx[i], alpha2 );   // q_enr
    2725    23841344 :         Ltmp1 = Madd_32_16( Ltmp1, enr[i], alpha2m1 );             // q_enr
    2726    23841344 :         hNoiseEst->ave_enr2_fx[i] = L_max( Le_min_scaled, Ltmp1 ); // q_enr
    2727    23841344 :         move32();
    2728             : 
    2729             :         /* calculation of non-stationarity measure for speech/music classification */
    2730    23841344 :         IF( hFrontVad == NULL )
    2731             :         {
    2732    22206824 :             test();
    2733    22206824 :             test();
    2734    22206824 :             IF( GE_16( i, START_BAND_SPMUS ) && LT_16( i, NB_BANDS_SPMUS + START_BAND_SPMUS ) && st_fx->hSpMusClas != NULL )
    2735             :             {
    2736             :                 /* log_enr = (float)ln_fx(enr[i]); */
    2737    16667910 :                 Ltmp1 = L_max( enr[i], 1 );
    2738    16667910 :                 e_ener = norm_l( Ltmp1 );
    2739    16667910 :                 f_ener = Log2_norm_lc( L_shl( Ltmp1, e_ener ) );
    2740    16667910 :                 e_ener = sub( sub( 30, e_ener ), q_enr );
    2741    16667910 :                 Ltmp1 = L_mac( f_ener, e_ener, ONE_IN_Q14 ); // Q15
    2742    16667910 :                 Ltmp1 = Mpy_32_16_1( Ltmp1, 22713 );         // Q15
    2743    16667910 :                 log_enr16 = round_fx( L_shl( Ltmp1, 9 ) );   /* Q8 */
    2744    16667910 :                 wtmp = abs_s( sub( log_enr16, hSpMusClas->past_log_enr_fx[i - START_BAND_SPMUS] ) );
    2745    16667910 :                 *non_staX = L_add( *non_staX, wtmp );
    2746    16667910 :                 move16(); /* Q8 */
    2747    16667910 :                 hSpMusClas->past_log_enr_fx[i - START_BAND_SPMUS] = log_enr16;
    2748    16667910 :                 move16();
    2749             :             }
    2750             :         }
    2751             : 
    2752    23841344 :         test();
    2753    23841344 :         IF( GE_16( i, 2 ) && LE_16( i, 16 ) )
    2754             :         {
    2755    17893800 :             tmp_enr = LN_E_MIN_PLUS_ONE_FX; // Q8
    2756    17893800 :             move16();
    2757    17893800 :             IF( enr[i] != 0 )
    2758             :             {
    2759    17893783 :                 Ltmp1 = L_add( enr[i], Ltmp ); // q_enr
    2760    17893783 :                 e_ener = norm_l( Ltmp1 );
    2761    17893783 :                 f_ener = Log2_norm_lc( L_shl( Ltmp1, e_ener ) );
    2762    17893783 :                 e_ener = sub( sub( 30, e_ener ), q_enr );
    2763    17893783 :                 Ltmp1 = L_mac( f_ener, e_ener, ONE_IN_Q14 ); // Q15
    2764    17893783 :                 Ltmp1 = Mpy_32_16_1( Ltmp1, 22713 );         // Q15
    2765    17893783 :                 tmp_enr = round_fx( L_shl( Ltmp1, 9 ) );     /* Q8 */
    2766             :             }
    2767             : 
    2768    17893800 :             IF( LT_16( ini_frame, 100 ) )
    2769             :             {
    2770     4040355 :                 non_staB = add_o( non_staB, abs_s( sub( tmp_enr, LN_E_MIN_PLUS_ONE_FX ) ), &Overflow ); /*  Q8  */
    2771             :             }
    2772             :             ELSE /*ini_frame < 100*/
    2773             :             {
    2774    13853445 :                 tmp_floor = LN_E_MIN_PLUS_ONE_FX; // Q8
    2775    13853445 :                 move16();
    2776    13853445 :                 IF( hNoiseEst->bckr_fx[i] != 0 )
    2777             :                 {
    2778    13788195 :                     Ltmp1 = L_add( hNoiseEst->bckr_fx[i], L_shl( 1, hNoiseEst->q_bckr ) ); // hNoiseEst->q_bckr
    2779    13788195 :                     e_ener = norm_l( Ltmp1 );
    2780    13788195 :                     f_ener = Log2_norm_lc( L_shl( Ltmp1, e_ener ) );
    2781    13788195 :                     e_ener = sub( sub( 30, e_ener ), hNoiseEst->q_bckr );
    2782    13788195 :                     Ltmp1 = L_mac( f_ener, e_ener, ONE_IN_Q14 ); // Q15
    2783    13788195 :                     Ltmp1 = Mpy_32_16_1( Ltmp1, 22713 );         // Q15
    2784    13788195 :                     tmp_floor = round_fx( L_shl( Ltmp1, 9 ) );   /* Q8 */
    2785             :                 }
    2786    13853445 :                 non_staB = add_o( non_staB, abs_s( sub( tmp_enr, tmp_floor ) ), &Overflow ); /*  Q8  */
    2787             :             }
    2788             :         }
    2789             : 
    2790             :     }                                   /* end of band loop FOR( i = st_fx->min_band; i <= st_fx->max_band; i++ ) */
    2791     1192920 :     *non_staX = L_shl( *non_staX, 12 ); // Q20
    2792     1192920 :     move32();
    2793     1192920 :     IF( LT_16( Etot, -1280 /* -5.0f in Q8 */ ) )
    2794             :     {
    2795      125177 :         non_sta = L_deposit_l( 1024 );   /* 1.0 in  Q10  */
    2796      125177 :         Lnon_sta2 = L_deposit_l( 1024 ); /* 1.0 in  Q10 */
    2797             :     }
    2798             : 
    2799     1192920 :     lim_Etot_fx = s_max( 5120, Etot );                    /* 20.0f Q8 */
    2800     1192920 :     lim_Etot_sq_fx = L_mult0( lim_Etot_fx, lim_Etot_fx ); /* Q16 */
    2801             : 
    2802     1192920 :     IF( LT_16( ini_frame, 150 ) )
    2803             :     {
    2804             :         /* Allow use of quicker filter during init - if needed */
    2805             :         /* st->Etot_st_est = 0.25f * lim_Etot + (1.0f-0.25F) * st->Etot_st_est; */
    2806      354989 :         hNoiseEst->L_Etot_st_est_fx = Madd_32_16_r( L_mult0( 8192, lim_Etot_fx ), hNoiseEst->L_Etot_st_est_fx, 24576 ); // Q23
    2807      354989 :         move32();
    2808             :         /* st->Etot_sq_st_est = 0.25f * lim_Etot * lim_Etot + (1.0f-0.25f) * st->Etot_sq_st_est; */
    2809      354989 :         hNoiseEst->L_Etot_sq_st_est_fx = Madd_32_16_r( Mult_32_16( lim_Etot_sq_fx, 8192 ), hNoiseEst->L_Etot_sq_st_est_fx, 24576 ); // Q16
    2810      354989 :         move32();
    2811             :     }
    2812             :     ELSE
    2813             :     {
    2814             :         /* st->Etot_st_est = 0.25f * lim_Etot + (1.0f-0.25F) * st->Etot_st_est; */
    2815      837931 :         hNoiseEst->L_Etot_st_est_fx = Madd_32_16_r( L_mult0( 8192, lim_Etot_fx ), hNoiseEst->L_Etot_st_est_fx, 24576 ); // Q23
    2816      837931 :         move32();
    2817             :         /* st->Etot_sq_st_est = 0.25f * lim_Etot * lim_Etot + (1.0f-0.25f) * st->Etot_sq_st_est; */
    2818      837931 :         hNoiseEst->L_Etot_sq_st_est_fx = Madd_32_16_r( Mult_32_16( lim_Etot_sq_fx, 8192 ), hNoiseEst->L_Etot_sq_st_est_fx, 24576 ); // Q16
    2819      837931 :         move32();
    2820             :     }
    2821             : 
    2822             :     Word16 exp_tmp;
    2823     1192920 :     st_E_var_est_fx = BASOP_Util_Add_Mant32Exp( hNoiseEst->L_Etot_sq_st_est_fx, Q15, L_negate( Mpy_32_32( hNoiseEst->L_Etot_st_est_fx, hNoiseEst->L_Etot_st_est_fx ) ), Q16, &exp_tmp ); // exp(exp_tmp)
    2824             :     /*-----------------------------------------------------------------*
    2825             :      * Count frames since last correlation or harmonic event
    2826             :      *-----------------------------------------------------------------*/
    2827             : 
    2828     1192920 :     Ltmp = L_mult( st_fx->voicing_fx[0], 16384 /* 0.5 in Q15 */ );      // Q31
    2829     1192920 :     Ltmp = L_mac( Ltmp, st_fx->voicing_fx[1], 16384 /* 0.5 in Q15 */ ); // Q31
    2830             : 
    2831     1192920 :     *st_harm_cor_cnt = add( *st_harm_cor_cnt, 1 );
    2832     1192920 :     move16();
    2833     1192920 :     test();
    2834     1192920 :     test();
    2835     1192920 :     if ( ( Etot > 0 ) && ( ( *loc_harm > 0 ) || ( GT_32( Ltmp, 1825361101 /* 0.85 in Q31 */ ) ) ) )
    2836             :     {
    2837      616095 :         *st_harm_cor_cnt = 0;
    2838      616095 :         move16();
    2839             :     }
    2840             : 
    2841     1192920 :     test();
    2842     1192920 :     test();
    2843     1192920 :     test();
    2844     1581282 :     if ( ( GT_16( *st_harm_cor_cnt, 1 ) ) && ( ( LT_16( Etot, 3840 /* 15 in Q8 */ ) ) ||
    2845      737536 :                                                ( GT_16( ini_frame, 10 ) &&
    2846      349174 :                                                  GT_16( sub( Etot, hNoiseEst->Etot_lp_fx ), 1792 /* 7 in Q8 */ ) ) ) )
    2847             :     {
    2848      155917 :         *st_harm_cor_cnt = 1;
    2849      155917 :         move16();
    2850             :     }
    2851     1192920 :     test();
    2852     1192920 :     test();
    2853     1192920 :     IF( GT_16( *st_harm_cor_cnt, 1 ) && GT_16( Etot, 7680 /* 30.0f in Q8 */ ) && EQ_16( BASOP_Util_Cmp_Mant32Exp( st_E_var_est_fx, exp_tmp, 524288 /* 8.0f in Q16 */, Q15 ), 1 ) )
    2854             :     {
    2855             :         /* st->harm_cor_cnt = max(1, (short) round_f( (float) st->harm_cor_cnt / 4.0f )) ; */
    2856      148574 :         *st_harm_cor_cnt = s_max( 1, shr( add( *st_harm_cor_cnt, 2 ), 2 ) );
    2857      148574 :         move16();
    2858             :     }
    2859             : 
    2860             : 
    2861             :     /*-----------------------------------------------------------------*
    2862             :      * Energy based pause length counter
    2863             :      *-----------------------------------------------------------------*/
    2864     1192920 :     test();
    2865     1192920 :     IF( ( *bg_cnt >= 0 ) && ( GT_16( sub( Etot, Etot_l_lp ), 1280 ) /*5.0 in Q8*/ ) )
    2866             :     {
    2867             :         /* Possible speech burst */
    2868       37851 :         *bg_cnt = -1;
    2869       37851 :         move16();
    2870             :     }
    2871             :     ELSE
    2872             :     {
    2873     1155069 :         test();
    2874     1155069 :         if ( EQ_16( *bg_cnt, -1 ) && ( LT_16( sub( Etot, Etot_l_lp ), 1280 ) ) /*5 in Q8*/ )
    2875             :         {
    2876             :             /* Possible start of speech pause */
    2877       34460 :             *bg_cnt = 0;
    2878       34460 :             move16();
    2879             :         }
    2880             :     }
    2881     1192920 :     if ( *bg_cnt >= 0 )
    2882             :     {
    2883      370555 :         *bg_cnt = add( *bg_cnt, 1 );
    2884      370555 :         move16();
    2885             :     }
    2886             : 
    2887             :     /*-----------------------------------------------------------------*
    2888             :      * Linear predition efficiency 0 to 2 order
    2889             :      *-----------------------------------------------------------------*/
    2890             : 
    2891             :     /*epsP_0_2 = max(0 , min(8, epsP[0] / epsP[2])); */
    2892     1192920 :     exp = sub( norm_l( epsP[0] ), 1 );
    2893     1192920 :     num = extract_h( L_shl( epsP[0], exp ) ); // Qx+exp-16
    2894             : 
    2895     1192920 :     exp2 = norm_l( epsP[2] );
    2896     1192920 :     den = extract_h( L_shl( epsP[2], exp2 ) ); // Qx+exp2-16
    2897             : 
    2898             :     /* max(0, min(8, epsP[0] / epsP[2])) */
    2899     1192920 :     epsP_0_2 = div_s( num, den );                               // Q15+exp-exp2
    2900     1192920 :     epsP_0_2 = shr_sat( epsP_0_2, add( sub( exp, exp2 ), 3 ) ); // Q12
    2901     1192920 :     epsP_0_2 = s_max( 0, epsP_0_2 );                            /* min value is 0 , Q12 */
    2902             : 
    2903             :     /* st->epsP_0_2_lp = 0.15f * epsP_0_2 + (1.0f-0.15f) * st->epsP_0_2_lp; */
    2904     1192920 :     hNoiseEst->epsP_0_2_lp_fx = mac_r( L_mult( epsP_0_2, 4915 /* 0.15 in Q15*/ ), hNoiseEst->epsP_0_2_lp_fx, 27853 /* 0.85 in Q15*/ ); // Q12
    2905     1192920 :     move16();
    2906             : 
    2907             :     /* epsP_0_2_ad = (float) fabs(epsP_0_2 - st->epsP_0_2_lp );  */
    2908     1192920 :     epsP_0_2_ad = abs_s( sub( epsP_0_2, hNoiseEst->epsP_0_2_lp_fx ) ); /* Q12 */
    2909             : 
    2910             :     /*if (epsP_0_2_ad < st->epsP_0_2_ad_lp)  {
    2911             :         st->epsP_0_2_ad_lp = 0.1f * epsP_0_2_ad + (1.0f - 0.1f) * st->epsP_0_2_ad_lp;
    2912             :     } else {
    2913             :         st->epsP_0_2_ad_lp = 0.2f * epsP_0_2_ad + (1.0f - 0.2f) * st->epsP_0_2_ad_lp;
    2914             :      } */
    2915             : 
    2916     1192920 :     IF( LT_16( epsP_0_2_ad, hNoiseEst->epsP_0_2_ad_lp_fx ) )
    2917             :     {
    2918      837052 :         hNoiseEst->epsP_0_2_ad_lp_fx = mac_r( L_mult( epsP_0_2_ad, 3277 /* 0.1 in Q15*/ ), hNoiseEst->epsP_0_2_ad_lp_fx, 29491 /* 0.9 in Q15*/ ); // Q12
    2919      837052 :         move16();
    2920             :     }
    2921             :     ELSE
    2922             :     {
    2923      355868 :         hNoiseEst->epsP_0_2_ad_lp_fx = mac_r( L_mult( epsP_0_2_ad, 6554 /* 0.2 in Q15*/ ), hNoiseEst->epsP_0_2_ad_lp_fx, 26214 /* 0.8 in Q15*/ ); // Q12
    2924      355868 :         move16();
    2925             :     }
    2926             : 
    2927             :     /* epsP_0_2_ad_lp_max = max(epsP_0_2_ad,st->epsP_0_2_ad_lp);*/
    2928     1192920 :     epsP_0_2_ad_lp_max = s_max( epsP_0_2_ad, hNoiseEst->epsP_0_2_ad_lp_fx ); /* Q12 */
    2929             : 
    2930             : 
    2931             :     /*-----------------------------------------------------------------*
    2932             :      * Linear predition efficiency 2 to 16 order
    2933             :      *-----------------------------------------------------------------*/
    2934             : 
    2935             :     /*epsP_2_16 = max(0 , min(8, epsP[2] / epsP[16])); */
    2936     1192920 :     exp = sub( norm_l( epsP[2] ), 1 );
    2937     1192920 :     num = extract_h( L_shl( epsP[2], exp ) ); // Qx+exp-16
    2938             : 
    2939     1192920 :     exp2 = norm_l( epsP[16] );
    2940     1192920 :     den = extract_h( L_shl( epsP[16], exp2 ) ); // Qx+exp2-16
    2941             : 
    2942     1192920 :     epsP_2_16 = div_s( num, den );                                // Q15+exp-exp2
    2943     1192920 :     epsP_2_16 = shr_sat( epsP_2_16, add( sub( exp, exp2 ), 3 ) ); // Q12
    2944     1192920 :     epsP_2_16 = s_max( 0, epsP_2_16 );                            /* min value is 0 , Q12 */
    2945             : 
    2946             :     /* if (epsP_2_16 > st->epsP_2_16_lp){
    2947             :           st->epsP_2_16_lp = 0.2f * epsP_2_16 + (1.0f-0.2f) * st->epsP_2_16_lp;
    2948             :       } else {
    2949             :           st->epsP_2_16_lp = 0.03f * epsP_2_16 + (1.0f-0.03f) * st->epsP_2_16_lp;
    2950             :       } */
    2951             : 
    2952     1192920 :     IF( GT_16( epsP_2_16, hNoiseEst->epsP_2_16_lp_fx ) )
    2953             :     {
    2954      233392 :         hNoiseEst->epsP_2_16_lp_fx = mac_r( L_mult( epsP_2_16, 6554 /* 0.2 in Q15*/ ), hNoiseEst->epsP_2_16_lp_fx, 26214 /* 0.8 in Q15*/ ); // Q12
    2955      233392 :         move16();
    2956             :     }
    2957             :     ELSE
    2958             :     {
    2959      959528 :         hNoiseEst->epsP_2_16_lp_fx = mac_r( L_mult( epsP_2_16, 983 /* 0.03 in Q15*/ ), hNoiseEst->epsP_2_16_lp_fx, 31785 /* 0.97 in Q15*/ ); // Q12
    2960      959528 :         move16();
    2961             :     }
    2962             : 
    2963     1192920 :     hNoiseEst->epsP_2_16_lp2_fx = mac_r( L_mult( epsP_2_16, 655 /* 0.02 in Q15*/ ), hNoiseEst->epsP_2_16_lp2_fx, 32113 /* 0.98 in Q15*/ ); // Q12
    2964     1192920 :     move16();
    2965             : 
    2966     1192920 :     epsP_2_16_dlp = sub( hNoiseEst->epsP_2_16_lp_fx, hNoiseEst->epsP_2_16_lp2_fx );
    2967             : 
    2968             :     /* if (epsP_2_16_dlp < st->epsP_2_16_dlp_lp2 )  {
    2969             :         st->epsP_2_16_dlp_lp2 = 0.02f * epsP_2_16_dlp + (1.0f-0.02f) * st->epsP_2_16_dlp_lp2;
    2970             :     } else {
    2971             :         st->epsP_2_16_dlp_lp2 = 0.05f * epsP_2_16_dlp + (1.0f-0.05f) * st->epsP_2_16_dlp_lp2;
    2972             :     }*/
    2973             : 
    2974     1192920 :     IF( LT_16( epsP_2_16_dlp, hNoiseEst->epsP_2_16_dlp_lp2_fx ) )
    2975             :     {
    2976      710482 :         hNoiseEst->epsP_2_16_dlp_lp2_fx = mac_r( L_mult( epsP_2_16_dlp, 655 /* 0.02 in Q15*/ ), hNoiseEst->epsP_2_16_dlp_lp2_fx, 32113 /* 0.98 in Q15*/ ); // Q12
    2977      710482 :         move16();
    2978             :     }
    2979             :     ELSE
    2980             :     {
    2981      482438 :         hNoiseEst->epsP_2_16_dlp_lp2_fx = mac_r( L_mult( epsP_2_16_dlp, 1638 /* 0.05 in Q15*/ ), hNoiseEst->epsP_2_16_dlp_lp2_fx, 31130 /* 0.95 in Q15*/ ); // Q12
    2982      482438 :         move16();
    2983             :     }
    2984             : 
    2985             :     /* epsP_2_16_dlp_max  = max(epsP_2_16_dlp,st->epsP_2_16_dlp_lp2); */
    2986     1192920 :     epsP_2_16_dlp_max = s_max( epsP_2_16_dlp, hNoiseEst->epsP_2_16_dlp_lp2_fx );
    2987             : 
    2988             :     /*-----------------------------------------------------------------*
    2989             :      * long term extensions of frame features
    2990             :      *-----------------------------------------------------------------*/
    2991             : 
    2992     1192920 :     tmp = sub( Etot, extract_h( hNoiseEst->totalNoise_32fx ) ); /* Q8 */
    2993             :     /* st->lt_tn_track = 0.03f* (Etot - st->totalNoise < 10) + 0.97f*st->lt_tn_track; */
    2994     1192920 :     tmp2 = 0;
    2995     1192920 :     move16();
    2996     1192920 :     if ( LT_16( tmp, 2560 ) ) /*10 in Q8 */
    2997             :     {
    2998      252677 :         tmp2 = 32767;
    2999      252677 :         move16();
    3000             :     }
    3001     1192920 :     hNoiseEst->lt_tn_track_fx = mac_r( L_mult( tmp2, 983 /* 0.03 in Q15*/ ), hNoiseEst->lt_tn_track_fx, 31785 /* 0.97 in Q15*/ ); // Q12
    3002     1192920 :     move16();
    3003             : 
    3004             :     /* st->lt_tn_dist = 0.03f* (Etot - st->totalNoise) + 0.97f*st->lt_tn_dist; */
    3005     1192920 :     hNoiseEst->lt_tn_dist_fx = mac_r( L_mult( tmp, 983 /* 0.03 in Q15*/ ), hNoiseEst->lt_tn_dist_fx, 31785 /* 0.97 in Q15*/ ); // Q8
    3006     1192920 :     move16();
    3007             : 
    3008             :     /* st->lt_Ellp_dist = 0.03f* (Etot - st->Etot_l_lp) + 0.97f*st->lt_Ellp_dist;*/
    3009     1192920 :     tmp = sub( Etot, extract_h( hNoiseEst->Etot_l_lp_32fx ) );                                                                     /* Q8 */
    3010     1192920 :     hNoiseEst->lt_Ellp_dist_fx = mac_r( L_mult( tmp, 983 /* 0.03 in Q15*/ ), hNoiseEst->lt_Ellp_dist_fx, 31785 /* 0.97 in Q15*/ ); // Q8
    3011     1192920 :     move16();
    3012             : 
    3013             : 
    3014             :     /* if (st->harm_cor_cnt == 0)  {
    3015             :         st->lt_haco_ev = 0.03f*1.0 + 0.97f*st->lt_haco_ev;
    3016             :     } else {
    3017             :         st->lt_haco_ev = 0.99f*st->lt_haco_ev;
    3018             :     } */
    3019     1192920 :     IF( *st_harm_cor_cnt == 0 )
    3020             :     {
    3021      616095 :         hNoiseEst->lt_haco_ev_fx = mac_r( 64424509 /* 0.03 in Q31*/, hNoiseEst->lt_haco_ev_fx, 31785 /* 0.97 in Q15*/ ); // Q15
    3022      616095 :         move16();
    3023             :     }
    3024             :     ELSE
    3025             :     {
    3026      576825 :         hNoiseEst->lt_haco_ev_fx = mult_r( 32440, hNoiseEst->lt_haco_ev_fx ); /*.99 in Q15 , Q15 state */
    3027      576825 :         move16();
    3028             :     }
    3029             : 
    3030             :     /* if (st->lt_tn_track < 0.05f)  {
    3031             :         st->low_tn_track_cnt++;
    3032             :     } else {
    3033             :         st->low_tn_track_cnt=0;
    3034             :     }*/
    3035     1192920 :     IF( LT_16( hNoiseEst->lt_tn_track_fx, 1638 ) ) /* 0.05 in Q15*/
    3036             :     {
    3037      679226 :         hNoiseEst->low_tn_track_cnt = add( hNoiseEst->low_tn_track_cnt, 1 );
    3038      679226 :         move16();
    3039             :     }
    3040             :     ELSE
    3041             :     {
    3042      513694 :         hNoiseEst->low_tn_track_cnt = 0;
    3043      513694 :         move16();
    3044             :     }
    3045             : 
    3046             :     /* update of the long-term non-stationarity measure (between 0 and 1) */
    3047             :     /* if ( (non_sta > th_sta) || (*loc_harm > 0) ) {
    3048             :         st->act_pred = M_GAMMA * st->act_pred + (1-M_GAMMA) * 1;
    3049             :     } else {
    3050             :         st->act_pred = M_GAMMA * st->act_pred + (1-M_GAMMA) * 0;
    3051             :     }*/
    3052     1192920 :     test();
    3053     1192920 :     IF( ( GT_32( non_sta, th_sta ) ) || ( *loc_harm > 0 ) )
    3054             :     {
    3055      870388 :         hNoiseEst->act_pred_fx = mac_r( 21474836 /* 1 - M_GAMMA in Q31 */, M_GAMMA_FX, hNoiseEst->act_pred_fx ); // Q31
    3056      870388 :         move16();
    3057             :     }
    3058             :     ELSE
    3059             :     {
    3060      322532 :         hNoiseEst->act_pred_fx = mult_r( M_GAMMA_FX, hNoiseEst->act_pred_fx ); /*Q15*Q15+1 --> Q31 , 32440= .99 Q15 */
    3061      322532 :         move16();
    3062             :     }
    3063             : 
    3064             :     /*-----------------------------------------------------------------*
    3065             :      * Background noise adaptation enable flag
    3066             :      *-----------------------------------------------------------------*/
    3067             :     /*
    3068             :      if( ( (*st_harm_cor_cnt < 3*HC_CNT_SLOW )
    3069             :           && ( ( non_sta > th_sta ) ||
    3070             :           ( tmp_pc < TH_PC ) ||
    3071             :           ( noise_char > 0) )
    3072             :           )
    3073             :           ||
    3074             :           ( (st->ini_frame > 150) && (Etot - Etot_l_lp) > 10 ) ||
    3075             :           ( 0.5f * (voicing[0]+voicing[1]) > cor_max ) ||
    3076             :           ( epsP[2] / epsP[16] > th_eps ) ||
    3077             :           ( *loc_harm > 0) ||
    3078             :           ((st->act_pred > 0.8f) && (non_sta2 > th_sta))
    3079             :           ) */
    3080             : 
    3081     1192920 :     Ltmp = L_mult( st_fx->voicing_fx[0], 16384 );         /* Q15 + Q15(.5)) + 1    ->  Q31  */
    3082     1192920 :     cor_tmp = mac_r( Ltmp, st_fx->voicing_fx[1], 16384 ); /* Q31 -16               ->  Q15  */
    3083     1192920 :     if ( Etot < 0 )
    3084             :     {
    3085      131003 :         cor_tmp = 0;
    3086      131003 :         move16();
    3087             :     }
    3088             :     /* epsP[2] / epsP[16] */
    3089     1192920 :     exp = sub( norm_l( epsP[2] ), 1 );
    3090     1192920 :     num = extract_h( L_shl( epsP[2], exp ) ); // Qx+exp-16
    3091             : 
    3092     1192920 :     exp2 = norm_l( epsP[16] );
    3093     1192920 :     den = extract_h( L_shl( epsP[16], exp2 ) ); // Qx+exp2-16
    3094             : 
    3095     1192920 :     LepsP = extract_l( div_s( num, den ) );             // Q15+exp-exp2
    3096     1192920 :     LepsP = L_shr( LepsP, add( sub( exp, exp2 ), 4 ) ); // Q11
    3097             : 
    3098             : 
    3099     1192920 :     test();
    3100     1192920 :     test();
    3101     1192920 :     test();
    3102     1192920 :     test();
    3103     1192920 :     test();
    3104     1192920 :     test();
    3105     1192920 :     test();
    3106     1192920 :     test();
    3107     1192920 :     test();
    3108     1192920 :     test();
    3109     1192920 :     IF( ( ( LT_16( *st_harm_cor_cnt, ( 3 * HC_CNT_SLOW_FX ) ) ) && ( ( GT_32( non_sta, th_sta ) ) || ( LT_16( tmp_pc, TH_PC_FX ) ) || ( noise_char > 0 ) ) ) ||
    3110             :         ( ( GT_16( ini_frame, HE_LT_CNT_INIT_FX ) ) && ( GT_16( sub( Etot, Etot_l_lp ), 2560 ) ) ) ||
    3111             :         ( GT_16( cor_tmp, cor_max ) ) || /* Q15 */
    3112             :         ( GT_32( LepsP, th_eps ) ) ||    /* Q11 */
    3113             :         ( GT_16( *loc_harm, 0 ) ) ||
    3114             :         ( ( GT_16( hNoiseEst->act_pred_fx, 26214 ) ) && ( GT_32( Lnon_sta2, th_sta ) ) ) /*act_pred in Q15 , th_sta in Q10 */
    3115             :     )
    3116             :     {
    3117             :         /* hNoiseEst->aEn = hNoiseEst->aEn + 2; */
    3118      970905 :         hNoiseEst->aEn = add( hNoiseEst->aEn, 2 ); /*  active signal present - increment counter  */
    3119      970905 :         move16();
    3120             :     }
    3121             :     ELSE
    3122             :     {
    3123             :         /* hNoiseEst->aEn = hNoiseEst->aEn - 1; */
    3124      222015 :         hNoiseEst->aEn = add( hNoiseEst->aEn, -1 ); /* background noise present - decrement counter */
    3125      222015 :         move16();
    3126             :     }
    3127             : 
    3128     1192920 :     hNoiseEst->aEn = s_max( s_min( hNoiseEst->aEn, 6 ), 0 );
    3129     1192920 :     move16();
    3130             : 
    3131     1192920 :     IF( LE_16( hNoiseEst->aEn, 1 ) )
    3132             :     {
    3133      181888 :         hNoiseEst->aEn_inac_cnt = add( hNoiseEst->aEn_inac_cnt, 1 );
    3134      181888 :         hNoiseEst->aEn_inac_cnt = s_min( hNoiseEst->aEn_inac_cnt, 128 );
    3135      181888 :         move16();
    3136      181888 :         move16();
    3137             :     }
    3138             :     /*-----------------------------------------------------------------*
    3139             :      * Stereo classifier - save raw aEn
    3140             :      *-----------------------------------------------------------------*/
    3141     1192920 :     IF( hStereoClassif != NULL )
    3142             :     {
    3143             :         /*
    3144             :         if ( ( non_sta > th_sta ) ||
    3145             :              ( tmp_pc < TH_PC ) ||
    3146             :              ( 0.5f * ( st->voicing[0] + st->voicing[1] ) > cor_max ) ||
    3147             :              ( epsP[2] / epsP[16] > th_eps ) ||
    3148             :              ( ( hNoiseEst->act_pred > 0.8f ) && ( non_sta2 > th_sta ) ) )*/
    3149      760851 :         wtmp = sub( hStereoClassif->aEn_raw[st_fx->idchan], 1 );
    3150      760851 :         test();
    3151      760851 :         test();
    3152      760851 :         test();
    3153      760851 :         test();
    3154      760851 :         test();
    3155      933446 :         if ( ( GT_32( non_sta, th_sta ) ) || ( LT_16( tmp_pc, TH_PC_FX ) ) ||
    3156      343671 :              ( GT_16( cor_tmp, cor_max ) ) ||
    3157      325519 :              ( GT_32( LepsP, th_eps ) ) ||
    3158      198569 :              ( ( GT_16( hNoiseEst->act_pred_fx, 26214 ) ) && ( GT_32( Lnon_sta2, th_sta ) ) ) ) /*act_pred in Q15 , th_sta in Q10 */
    3159             :         {
    3160             :             /* active signal present - increment counter */
    3161      611117 :             wtmp = add( hStereoClassif->aEn_raw[st_fx->idchan], 2 );
    3162             :         }
    3163      760851 :         hStereoClassif->aEn_raw[st_fx->idchan] = wtmp;
    3164      760851 :         move16();
    3165             : 
    3166      760851 :         wtmp = s_min( hStereoClassif->aEn_raw[st_fx->idchan], 6 );
    3167      760851 :         hStereoClassif->aEn_raw[st_fx->idchan] = s_max( wtmp, 0 );
    3168      760851 :         move16();
    3169             :     }
    3170             : 
    3171             : 
    3172             :     /* Additional NNE detectors */
    3173             : 
    3174             :     /* comb_ahc_epsP = max(max(st->act_pred, st->lt_haco_ev), epsP_2_16_dlp); */
    3175             :     /*                             Q15            Q15          Q12 */
    3176     1192920 :     comb_ahc_epsP = s_max( shr( s_max( hNoiseEst->act_pred_fx, hNoiseEst->lt_haco_ev_fx ), 15 - 12 ), epsP_2_16_dlp ); /* Q12 */
    3177             : 
    3178             : 
    3179             :     /* comb_hcm_epsP = max(max(st->lt_haco_ev,epsP_2_16_dlp_max),epsP_0_2_ad_lp_max); */
    3180             :     /*                            Q15         Q12                 Q12              */
    3181     1192920 :     comb_hcm_epsP = s_max( s_max( shr( hNoiseEst->lt_haco_ev_fx, 15 - 12 ), epsP_2_16_dlp_max ), epsP_0_2_ad_lp_max ); /* Q12 */
    3182             : 
    3183             :     /*haco_ev_max = max(*st_harm_cor_cnt==0,st->lt_haco_ev); */
    3184     1192920 :     tmp = 0;
    3185     1192920 :     move16();
    3186     1192920 :     if ( *st_harm_cor_cnt == 0 )
    3187             :     {
    3188      616095 :         tmp = 32767;
    3189      616095 :         move16();
    3190             :     }
    3191     1192920 :     haco_ev_max = s_max( tmp, hNoiseEst->lt_haco_ev_fx ); /* Q15 */
    3192             : 
    3193             :     /* Etot_l_lp_thr = st->Etot_l_lp + (1.5f + 1.5f * (st->Etot_lp<50.0f))*st->Etot_v_h2; */
    3194     1192920 :     L_tmp = 49152; /* 1.5 Q15 */
    3195     1192920 :     move32();
    3196     1192920 :     if ( LT_16( hNoiseEst->Etot_lp_fx, 12800 ) ) /* 50.0 in Q8 */
    3197             :     {
    3198      940833 :         L_tmp = L_shl( L_tmp, 1 ); /*1.5 + 1.5  Q15 */
    3199             :     }
    3200     1192920 :     temp = W_mac_32_32( W_mult_32_32( hNoiseEst->Etot_l_lp_32fx, ONE_IN_Q15 ), Etot_v_h2, L_tmp ); // Q40(24+15+1)
    3201     1192920 :     Etot_l_lp_thr = W_round32_s( temp );                                                           // Q8
    3202             : 
    3203             :     /* enr_bgd = Etot < Etot_l_lp_thr; */
    3204     1192920 :     enr_bgd = 0;
    3205     1192920 :     move16();
    3206     1192920 :     if ( LT_16( Etot, Etot_l_lp_thr ) ) /* Q8 */
    3207             :     {
    3208      342603 :         enr_bgd = 1;
    3209      342603 :         move16(); /* Q0 */
    3210             :     }
    3211             : 
    3212             :     /* cns_bgd = (epsP_0_2 > 7.95f) && (non_sta< 1e3f); */
    3213     1192920 :     cns_bgd = 0;
    3214     1192920 :     move16();
    3215     1192920 :     test();
    3216     1192920 :     if ( ( GT_16( epsP_0_2, 32563 ) )        /* 7.95 in Q12 */
    3217      446047 :          && ( LT_32( non_sta, 1024000L ) ) ) /* 1e3f in  Q10 ? */
    3218             :     {
    3219       72921 :         cns_bgd = 1;
    3220       72921 :         move16(); /* Q0 */
    3221             :     }
    3222             : 
    3223             :     /*lp_bgd  = epsP_2_16_dlp_max < 0.10f; */
    3224     1192920 :     lp_bgd = 0;
    3225     1192920 :     move16();
    3226     1192920 :     if ( LT_16( epsP_2_16_dlp_max, 410 ) ) /*0.10 Q12 */
    3227             :     {
    3228      149537 :         lp_bgd = 1;
    3229      149537 :         move16(); /* Q0 */
    3230             :     }
    3231             : 
    3232             : 
    3233             :     /* ns_mask = non_sta < 1e5f; */
    3234     1192920 :     ns_mask = 0;
    3235     1192920 :     move16();
    3236     1192920 :     if ( LT_32( non_sta, 102400000 ) ) /*  (1e5f in  Q10)*/
    3237             :     {
    3238      374440 :         ns_mask = 1;
    3239      374440 :         move16(); /* Q0 */
    3240             :     }
    3241             : 
    3242             : 
    3243             :     /* lt_haco_mask = st->lt_haco_ev < 0.5f; */
    3244     1192920 :     lt_haco_mask = 0;
    3245     1192920 :     move16();
    3246     1192920 :     if ( LT_16( hNoiseEst->lt_haco_ev_fx, 16384 ) ) /*  ( .5 in  Q15)*/
    3247             :     {
    3248      458900 :         lt_haco_mask = 1;
    3249      458900 :         move16(); /* Q0 */
    3250             :     }
    3251             : 
    3252             :     /* bg_haco_mask = haco_ev_max < 0.4f;   */
    3253     1192920 :     bg_haco_mask = 0;
    3254     1192920 :     move16();
    3255     1192920 :     if ( LT_16( haco_ev_max, 13107 ) ) /* ( 0.4 in  Q15)*/
    3256             :     {
    3257      342817 :         bg_haco_mask = 1;
    3258      342817 :         move16(); /* Q0 */
    3259             :     }
    3260             : 
    3261             : 
    3262             :     /* SD_1 = ( (epsP_0_2_ad > 0.5f) && (epsP_0_2 > 7.95f) ); */
    3263     1192920 :     SD_1 = 0;
    3264     1192920 :     move16();
    3265     1192920 :     test();
    3266     1192920 :     if ( ( GT_16( epsP_0_2_ad, 2048 ) )    /* 0.5 in Q12 */
    3267      624129 :          && ( GT_16( epsP_0_2, 32563 ) ) ) /* 7.95 in Q12 */
    3268             :     {
    3269      243912 :         SD_1 = 1;
    3270      243912 :         move16(); /* Q0 */
    3271             :     }
    3272             : 
    3273             :     /* NB "STL::test()";   has a cost of 2,  using  bitwise   "s_and" ,  "s_or"  at a cost of 1 */
    3274             :     /* NB only lowest bit position is used,  result is always  0 or 1  */
    3275             : 
    3276             :     /* bg_bgd3 = enr_bgd || ( ( cns_bgd || lp_bgd ) && ns_mask && lt_haco_mask && SD_1==0 ); */
    3277     1192920 :     test();
    3278     1192920 :     test();
    3279     1192920 :     test();
    3280     1192920 :     test();
    3281     1192920 :     test();
    3282     1192920 :     bg_bgd3 = enr_bgd || ( ( cns_bgd || lp_bgd ) && ns_mask && lt_haco_mask && ( SD_1 == 0 ) );
    3283     1192920 :     move16();
    3284             : 
    3285             :     /*PD_1 = (epsP_2_16_dlp_max < 0.10f ) ; */
    3286     1192920 :     PD_1 = 0;
    3287     1192920 :     move16();
    3288     1192920 :     if ( ( LT_16( epsP_2_16_dlp_max, 410 ) ) ) /* 0.10  in Q12 */
    3289             :     {
    3290      149537 :         PD_1 = 1;
    3291      149537 :         move16(); /* Q0 */
    3292             :     }
    3293             : 
    3294             :     /*PD_2 = (epsP_0_2_ad_lp_max < 0.10f ) ; */
    3295     1192920 :     PD_2 = 0;
    3296     1192920 :     move16();
    3297     1192920 :     if ( ( LT_16( epsP_0_2_ad_lp_max, 410 ) ) ) /* 0.10  in Q12 */
    3298             :     {
    3299      183977 :         PD_2 = 1;
    3300      183977 :         move16(); /* Q0 */
    3301             :     }
    3302             : 
    3303             :     /*PD_3 = (comb_ahc_epsP < 0.85f ); */
    3304     1192920 :     PD_3 = 0;
    3305     1192920 :     move16();
    3306     1192920 :     if ( ( LT_16( comb_ahc_epsP, 3482 ) ) ) /* 0.85  in Q12 */
    3307             :     {
    3308      303000 :         PD_3 = 1;
    3309      303000 :         move16(); /* Q0 */
    3310             :     }
    3311             : 
    3312             :     /* PD_4 = comb_ahc_epsP < 0.15f; */
    3313     1192920 :     PD_4 = 0;
    3314     1192920 :     move16();
    3315     1192920 :     if ( ( LT_16( comb_ahc_epsP, 614 ) ) ) /* 0.15  in Q12 */
    3316             :     {
    3317       80201 :         PD_4 = 1;
    3318       80201 :         move16(); /* Q0 */
    3319             :     }
    3320             : 
    3321             :     /*PD_5 = comb_hcm_epsP < 0.30f;   */
    3322     1192920 :     PD_5 = 0;
    3323     1192920 :     move16();
    3324     1192920 :     if ( ( LT_16( comb_hcm_epsP, 1229 ) ) ) /* 0.30  in Q12 */
    3325             :     {
    3326      118904 :         PD_5 = 1;
    3327      118904 :         move16(); /* Q0 */
    3328             :     }
    3329             : 
    3330             :     /* BG_1 = ( (SD_1==0) || (Etot < Etot_l_lp_thr) )
    3331             :                && bg_haco_mask && (st->act_pred < 0.85f) && (st->Etot_lp < 50.0f); */
    3332     1192920 :     BG_1 = 0;
    3333     1192920 :     move16();
    3334     1192920 :     test();
    3335     1192920 :     test();
    3336     1192920 :     test();
    3337     1192920 :     test();
    3338     1192920 :     if ( ( ( SD_1 == 0 ) || ( LT_16( Etot, Etot_l_lp_thr ) ) ) && ( bg_haco_mask != 0 ) && ( LT_16( hNoiseEst->act_pred_fx, 27853 ) ) /* 0.85f in Q15 */
    3339      233731 :          && ( LT_16( hNoiseEst->Etot_lp_fx, 50 * 256 ) ) )                                                                            /* 50.0 in Q8 */
    3340             :     {
    3341      224248 :         BG_1 = 1;
    3342      224248 :         move16();
    3343             :     }
    3344             : 
    3345             :     /*  PAU = (st->aEn==0)
    3346             :           || ( (Etot < 55.0f) && (SD_1==0)
    3347             :                 && ( ( PD_3 && (PD_1 || PD_2 ) ) ||   ( PD_4 || PD_5 )  ) ); */
    3348     1192920 :     PAU = 0;
    3349     1192920 :     move16();
    3350     1192920 :     test();
    3351     1192920 :     test();
    3352     1192920 :     test();
    3353     1192920 :     test();
    3354     1192920 :     test();
    3355     1192920 :     test();
    3356     1192920 :     test();
    3357     1192920 :     if ( ( hNoiseEst->aEn == 0 ) || ( LT_16( Etot, 55 * 256 ) && ( SD_1 == 0 ) && ( ( PD_3 && ( PD_1 || PD_2 ) ) || ( PD_4 || PD_5 ) ) ) )
    3358             :     {
    3359      207679 :         PAU = 1;
    3360      207679 :         move16();
    3361             :     }
    3362             : 
    3363             : 
    3364             :     /* NEW_POS_BG = (PAU | BG_1) & bg_bgd3;   note bitwise logic in float */
    3365     1192920 :     NEW_POS_BG = s_and( s_or( PAU, BG_1 ), bg_bgd3 );
    3366             : 
    3367             :     /* Original silence detector works in most cases */
    3368             :     /* aE_bgd = (st->aEn == 0);*/
    3369     1192920 :     aE_bgd = 0;
    3370     1192920 :     move16();
    3371     1192920 :     if ( hNoiseEst->aEn == 0 )
    3372             :     {
    3373      171463 :         aE_bgd = 1;
    3374      171463 :         move16();
    3375             :     }
    3376             : 
    3377             :     /* When the signal dynamics is high and the energy is close to the background estimate */
    3378             :     /* sd1_bgd =     (st->sign_dyn_lp > 15)
    3379             :                   && (Etot - st->Etot_l_lp ) < 2*st->Etot_v_h2
    3380             :                   && st->harm_cor_cnt > 20; */
    3381     1192920 :     sd1_bgd = 0;
    3382     1192920 :     move16();
    3383     1192920 :     test();
    3384     1192920 :     test();
    3385     1192920 :     if ( ( GT_16( hNoiseEst->sign_dyn_lp_fx, 15 * 256 ) )                                               /* 15 in Q8  */
    3386      864276 :          && ( LT_32( L_sub( L_deposit_h( Etot ), hNoiseEst->Etot_l_lp_32fx ), L_shl( Etot_v_h2, 1 ) ) ) /*  Q24 , Etot_v_h2 has limited dynmics can be upscaled*/
    3387      131560 :          && ( GT_16( *st_harm_cor_cnt, 20 ) ) )
    3388             :     {
    3389       13516 :         sd1_bgd = 1;
    3390       13516 :         move16();
    3391             :     }
    3392             : 
    3393             :     /* tn_ini = st->ini_frame < 150 && st->harm_cor_cnt > 5 &&
    3394             :                                  ( (st->act_pred < 0.59f && st->lt_haco_ev <0.23f ) ||
    3395             :                                     st->act_pred < 0.38f ||
    3396             :                                     st->lt_haco_ev < 0.15f ||
    3397             :                                     non_staB < 50.0f ||
    3398             :                                     aE_bgd );*/
    3399     1192920 :     tn_ini = 0;
    3400     1192920 :     move16();
    3401     1192920 :     test();
    3402     1192920 :     test();
    3403     1192920 :     test();
    3404     1192920 :     test();
    3405     1192920 :     test();
    3406     1192920 :     test();
    3407     1192920 :     test();
    3408     1192920 :     test();
    3409     1192920 :     test();
    3410     1192920 :     test();
    3411     1192920 :     test();
    3412     1192920 :     test();
    3413     1192920 :     test();
    3414     1192920 :     test();
    3415     1192920 :     test();
    3416     1237282 :     if ( LT_16( ini_frame, HE_LT_CNT_INIT_FX ) && GT_16( hNoiseEst->harm_cor_cnt, 5 ) && LT_16( sub( Etot, hNoiseEst->Etot_lp_fx ), 1792 /* 7 in Q8 */ ) &&
    3417       88724 :          ( ( LT_16( hNoiseEst->act_pred_fx, 19333 /* 0.59 in Q15 */ ) && LT_16( hNoiseEst->lt_haco_ev_fx, 7537 /* 0.23 in Q15 */ ) ) || LT_16( hNoiseEst->act_pred_fx, 12452 /* 0.38 in Q15 */ ) ||
    3418       54088 :            ( ( ( st_fx->element_mode == EVS_MONO ) && LT_16( hNoiseEst->lt_haco_ev_fx, 4915 /* 0.15 in Q15 */ ) ) || ( ( st_fx->element_mode > EVS_MONO ) && LT_16( hNoiseEst->lt_haco_ev_fx, 2621 /* 0.08 in Q15 */ ) ) ) ||
    3419       41492 :            LT_16( non_staB, 50 * 256 /* 50 in Q8 */ ) || ( aE_bgd != 0 ) || ( LT_16( Etot, 10752 /* 42.0 in Q8 */ ) && GT_16( hNoiseEst->harm_cor_cnt, 10 ) && LT_16( hNoiseEst->lt_haco_ev_fx, 11469 /* 0.35 in Q8 */ ) && LT_16( hNoiseEst->act_pred_fx, 26214 /* 0.8 in Q8 */ ) ) ) )
    3420             :     {
    3421       31550 :         tn_ini = 1;
    3422       31550 :         move16();
    3423             :     }
    3424             : 
    3425             :     /* Energy close to the background estimate serves as a mask for other background detectors */
    3426             :     /* bg_bgd2 = Etot < Etot_l_lp_thr || tn_ini ; */
    3427     1192920 :     bg_bgd2 = 0;
    3428     1192920 :     move16();
    3429     1192920 :     test();
    3430     1192920 :     if ( ( LT_16( Etot, Etot_l_lp_thr ) ) || ( tn_ini != 0 ) )
    3431             :     {
    3432      353088 :         bg_bgd2 = 1;
    3433      353088 :         move16(); /* Q0 */
    3434             :     }
    3435             : 
    3436     1192920 :     updt_step = 0;
    3437     1192920 :     move16(); /* Q15 */
    3438             :     /*if (( bg_bgd2 && ( aE_bgd || sd1_bgd || st->lt_tn_track >0.90f || NEW_POS_BG ) )
    3439             :          ||  tn_ini ) */
    3440     1192920 :     test();
    3441     1192920 :     test();
    3442     1192920 :     test();
    3443     1192920 :     test();
    3444     1192920 :     test();
    3445     1192920 :     IF( ( bg_bgd2 && ( aE_bgd || sd1_bgd || GT_16( hNoiseEst->lt_tn_track_fx, 29491 /* 0.90 in Q15 */ ) || NEW_POS_BG ) ) || tn_ini )
    3446             :     {
    3447             :         /*if( (  ( st->act_pred < 0.85f )
    3448             :                && (aE_bgd !=0)
    3449             :                && ( st->lt_Ellp_dist < 10  || sd1_bgd )
    3450             :                && (st->lt_tn_dist<40)
    3451             :                && ( ( Etot - st->totalNoise ) < 10.0f )
    3452             :              )
    3453             :                || ( (st->first_noise_updt == 0)  && (st->harm_cor_cnt > 80)  && (aE_bgd!=0) && (st->lt_aEn_zero > 0.5f) )
    3454             :                || ( (tn_ini!=0) && ( aE_bgd != 0)  || (non_staB < 10.0) || (st->harm_cor_cnt > 80) )
    3455             :                )*/
    3456             : 
    3457      195354 :         test();
    3458      195354 :         test();
    3459      195354 :         test();
    3460      195354 :         test();
    3461      195354 :         test();
    3462      195354 :         test();
    3463      195354 :         test(); /* for the ELSE IF below*/
    3464      195354 :         test();
    3465      195354 :         test();
    3466      195354 :         test();
    3467      195354 :         test();
    3468      195354 :         test();
    3469      195354 :         test(); /* for the ELSE IF below*/
    3470             : 
    3471      195354 :         test();
    3472      195354 :         test();
    3473      195354 :         test();
    3474      195354 :         test();
    3475      195354 :         test();
    3476      195354 :         test();
    3477      195354 :         test();
    3478      195354 :         test();
    3479      195354 :         test();
    3480      195354 :         test();
    3481      195354 :         test();
    3482      195354 :         test();
    3483      195354 :         test();
    3484      195354 :         test();
    3485      195354 :         test();
    3486      195354 :         test();
    3487      195354 :         test();
    3488      195354 :         IF( ( LT_16( hNoiseEst->act_pred_fx, 27853 /* 0.85 in Q15 */ ) && ( aE_bgd != 0 ) && ( LT_16( hNoiseEst->lt_Ellp_dist_fx, 10 * 256 /* 10 in Q8*/ ) || ( sd1_bgd != 0 ) ) && ( LT_16( hNoiseEst->lt_tn_dist_fx, 40 * 256 ) ) /* 40.0 in Q8*/
    3489             :               && LT_16( sub( Etot, extract_h( hNoiseEst->totalNoise_32fx ) ), 10 * 256 /* 10 in Q8 */ ) /* 10.0 in Q8*/ ) ||
    3490             :             ( ( hNoiseEst->first_noise_updt == 0 ) && GT_16( hNoiseEst->harm_cor_cnt, 80 ) && ( aE_bgd != 0 ) && GT_16( hNoiseEst->lt_aEn_zero_fx, 16384 /* 0.5 in Q15 */ ) ) ||
    3491             :             ( ( tn_ini != 0 ) && ( ( aE_bgd != 0 ) || LT_16( non_staB, 10 * 256 /* 10 in Q8*/ ) || GT_16( hNoiseEst->harm_cor_cnt, 80 ) ) ) )
    3492             :         {
    3493      150596 :             updt_step = 32767;
    3494      150596 :             move16();
    3495      150596 :             hNoiseEst->first_noise_updt = 1;
    3496      150596 :             move16();
    3497     3162516 :             FOR( i = 0; i < NB_BANDS; i++ )
    3498             :             {
    3499     3011920 :                 hNoiseEst->bckr_fx[i] = tmpN[i];
    3500     3011920 :                 move32();
    3501             :             }
    3502             :         }
    3503             :         /* else if ( ( ( st->act_pred < 0.80f ) && ( aE_bgd  || PAU )  &&  st->lt_haco_ev < 0.10f )
    3504             :                      || ( ( st->act_pred < 0.70f ) && ( aE_bgd || non_staB < 17.0f ) && PAU &&  st->lt_haco_ev < 0.15f )
    3505             :                      || ( st->harm_cor_cnt > 80 && st->totalNoise > 5.0f && Etot < max(1.0f,Etot_l_lp + 1.5f* st->Etot_v_h2) )
    3506             :                   ||
    3507             :                   ( st->harm_cor_cnt > 50 && st->first_noise_updt > 30 && aE_bgd  && st->lt_aEn_zero>0.5f )
    3508             :                   || tn_ini
    3509             :                 ) */
    3510       44758 :         ELSE IF( ( LT_16( hNoiseEst->act_pred_fx, 26214 /* 0.8 in Q15*/ ) && ( ( aE_bgd != 0 ) || ( PAU != 0 ) ) && ( LT_16( hNoiseEst->lt_haco_ev_fx, 3277 /* 0.1 in q15*/ ) ) ) ||
    3511             :                  ( ( LT_16( hNoiseEst->act_pred_fx, 22938 /* 0.70 in Q15 */ ) ) && ( ( aE_bgd != 0 ) || ( LT_16( non_staB, 17 * 256 /* 17.0 in Q8 */ ) ) ) && ( PAU != 0 ) && ( LT_16( hNoiseEst->lt_haco_ev_fx, 4915 /* 0.15 in Q15 */ ) ) ) ||
    3512             :                  ( GT_16( hNoiseEst->harm_cor_cnt, 80 ) && GT_16( extract_h( hNoiseEst->totalNoise_32fx ), 5 * 256 /* 5.0 in Q8 */ ) && LT_16( Etot, s_max( 1 * 256, add( Etot_l_lp, extract_h( L_add( hNoiseEst->Etot_v_h2_32fx, L_shr( hNoiseEst->Etot_v_h2_32fx, 1 ) ) ) /* 1.5= 1.0+.5 */ ) ) ) ) ||
    3513             :                  ( GT_16( hNoiseEst->harm_cor_cnt, 50 ) && GT_16( hNoiseEst->first_noise_updt, 30 ) && ( aE_bgd != 0 ) && GT_16( hNoiseEst->lt_aEn_zero_fx, 16384 /*.5 in Q15*/ ) ) || ( tn_ini != 0 ) )
    3514             :         {
    3515       10470 :             updt_step = 3277;
    3516       10470 :             move16(); /* 0.1 in Q15 */
    3517             : 
    3518             :             /* if ( !aE_bgd &&  st->harm_cor_cnt < 50
    3519             :                    &&  ( (st->act_pred > 0.6f)
    3520             :                          || ( (tn_ini==0) && (Etot_l_lp - st->totalNoise < 10.0f)  && non_staB > 8.0f )
    3521             :                         )
    3522             :                  )
    3523             :              */
    3524       10470 :             test();
    3525       10470 :             test();
    3526       10470 :             test();
    3527       10470 :             test();
    3528       10470 :             test();
    3529       17385 :             if ( ( aE_bgd == 0 ) && LT_16( hNoiseEst->harm_cor_cnt, 50 ) &&
    3530       10080 :                  ( GT_16( hNoiseEst->act_pred_fx, 19661 /* 0.6 in Q15*/ ) ||
    3531        2439 :                    ( ( tn_ini == 0 ) && LT_16( sub( Etot_l_lp, extract_h( hNoiseEst->totalNoise_32fx ) ), 10 * 256 /* 10.0 in Q8 */ ) && GT_16( non_staB, 8 * 256 /*  8.0 in in Q8*/ ) ) ) )
    3532             :             {
    3533        4138 :                 updt_step = 328;
    3534        4138 :                 move16(); /* 0.01 Q15 */
    3535             :             }
    3536             : 
    3537       10470 :             hNoiseEst->first_noise_updt = 1;
    3538       10470 :             move16();
    3539      219870 :             FOR( i = 0; i < NB_BANDS; i++ )
    3540             :             {
    3541             :                 /* st->bckr[i] = st->bckr[i] + updt_step * (tmpN[i]-st->bckr[i]);*/
    3542             :                 /* 32 bit state update */
    3543      209400 :                 Ltmp = L_sub( tmpN[i], hNoiseEst->bckr_fx[i] ); // hNoiseEst->q_bckr
    3544      209400 :                 Ltmp = Mult_32_16( Ltmp, updt_step );
    3545      209400 :                 hNoiseEst->bckr_fx[i] = L_add( Ltmp, hNoiseEst->bckr_fx[i] ); // hNoiseEst->q_bckr
    3546      209400 :                 move32();
    3547             :             }
    3548             :         }
    3549             :         /*else if (aE_bgd || st->harm_cor_cnt > 100 )*/
    3550       34288 :         ELSE IF( ( aE_bgd != 0 ) || GT_16( hNoiseEst->harm_cor_cnt, 100 ) )
    3551             :         {
    3552        9672 :             hNoiseEst->first_noise_updt = add( hNoiseEst->first_noise_updt, 1 );
    3553        9672 :             move16();
    3554             :         }
    3555             :     }
    3556             :     ELSE
    3557             :     {
    3558             :         /* If in music lower bckr to drop further */
    3559      997566 :         test();
    3560      997566 :         test();
    3561      997566 :         IF( GT_16( hNoiseEst->low_tn_track_cnt, 300 ) && GT_16( hNoiseEst->lt_haco_ev_fx, 29491 /* 0.9 in Q15 */ ) && ( hNoiseEst->totalNoise_32fx > 0 ) )
    3562             :         {
    3563        3548 :             updt_step = -655;
    3564        3548 :             move16(); /* for debug purposes */
    3565       74508 :             FOR( i = 0; i < NB_BANDS; i++ )
    3566             :             {
    3567       70960 :                 IF( GT_32( hNoiseEst->bckr_fx[i], L_shl_sat( E_MIN_FXQ31, sub( hNoiseEst->q_bckr, Q30 ) ) /* 2*E_MIN */ ) )
    3568             :                 {
    3569             :                     /* st->bckr[i] = 0.98f*st->bckr[i];  */
    3570       58948 :                     hNoiseEst->bckr_fx[i] = Mult_32_16( hNoiseEst->bckr_fx[i], 32113 /* .98 in Q15 */ ); // hNoiseEst->q_bckr
    3571       58948 :                     move32();                                                                            /* move to array */
    3572             :                 }
    3573             :             }
    3574             :         }
    3575             :     }
    3576             :     /*st->lt_aEn_zero = 0.2f * (st->aEn==0) + (1-0.2f)  *st->lt_aEn_zero;*/
    3577             :     /*  y(n+1)=        alpha*tmp            + (1-alpha)*y(n)           */
    3578     1192920 :     L_tmp = 0;
    3579     1192920 :     move32();
    3580     1192920 :     if ( hNoiseEst->aEn == 0 )
    3581             :     {
    3582      171463 :         L_tmp = 429496730; // 0.2 in Q31
    3583      171463 :         move32();
    3584             :     }
    3585     1192920 :     hNoiseEst->lt_aEn_zero_fx = mac_r( L_tmp, hNoiseEst->lt_aEn_zero_fx, 26214 /* 0.8 in Q15*/ ); // Q15
    3586     1192920 :     move16();
    3587             : 
    3588     1192920 :     IF( st_fx->element_mode > EVS_MONO )
    3589             :     {
    3590     1192920 :         test();
    3591     1192920 :         IF( hNoiseEst->first_noise_updt > 0 && LT_16( hNoiseEst->first_noise_updt_cnt, 100 ) )
    3592             :         {
    3593       99808 :             hNoiseEst->first_noise_updt_cnt = add( hNoiseEst->first_noise_updt_cnt, 1 );
    3594       99808 :             move16();
    3595             :         }
    3596             :     }
    3597             : 
    3598     1192920 :     return;
    3599             : }

Generated by: LCOV version 1.14