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

Generated by: LCOV version 1.14