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

Generated by: LCOV version 1.14