LCOV - code coverage report
Current view: top level - lib_enc - find_uv_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ bf9ba511f19a231c475b8e110fdaeacf18aa32c4 Lines: 444 537 82.7 %
Date: 2025-07-11 02:46:47 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : #include <stdint.h>
       5             : #include "options.h" /* Compilation switches                   */
       6             : #include "cnst.h"    /* Common constants                       */
       7             : //#include "prot_fx.h"        /* Function prototypes                    */
       8             : #include "rom_com.h"     /* Function prototypes                    */
       9             : #include "prot_fx.h"     /* Function prototypes                    */
      10             : #include "prot_fx_enc.h" /* Function prototypes                    */
      11             : 
      12             : /*-------------------------------------------------------------------*
      13             :  * Local constants
      14             :  *-------------------------------------------------------------------*/
      15             : 
      16             : #define L_ENR ( NB_SSF + 2 )
      17             : 
      18             : 
      19             : /*-------------------------------------------------------------------*
      20             :  * find_ener_decrease_fx()
      21             :  *
      22             :  * Find maximum energy ration between short subblocks in case
      23             :  * energy is trailing off after a spike
      24             :  *-------------------------------------------------------------------*/
      25             : 
      26       19853 : static Word16 find_ener_decrease_fx(                            /* o  : maximum energy ratio                                                            Q10*/
      27             :                                      const Word16 ind_deltaMax, /* i  : index of the beginning of maximum energy search         Q0*/
      28             :                                      const Word32 *pt_enr_ssf   /* i  : Pointer to the energy buffer                                            Qx*/
      29             : )
      30             : {
      31             :     Word16 i, j, end, flag;
      32             :     Word16 wtmp0, wtmp1;
      33             :     Word32 maxEnr, minEnr;
      34             :     Word16 dE2, exp0, exp1;
      35             : #ifndef ISSUE_1770_replace_shr_ro
      36             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
      37             :     Flag Overflow = 0;
      38             :     move32();
      39             : #endif
      40             : #endif
      41             : 
      42       19853 :     dE2 = 0;
      43       19853 :     move16();
      44             : 
      45       19853 :     j = ind_deltaMax + 2;
      46       19853 :     move16();
      47       19853 :     end = j + L_ENR;
      48       19853 :     move16();
      49       19853 :     maxEnr = L_add( pt_enr_ssf[j], 0 );
      50       19853 :     j = add( j, 1 );
      51       19853 :     flag = 0;
      52       19853 :     move16();
      53      198530 :     FOR( i = j; i < end; i++ )
      54             :     {
      55      178677 :         test();
      56      178677 :         IF( ( GT_32( pt_enr_ssf[i], maxEnr ) ) && ( flag == 0 ) )
      57             :         {
      58        7838 :             maxEnr = L_add( pt_enr_ssf[i], 0 ); /*Qx*/
      59        7838 :             j = add( j, 1 );
      60             :         }
      61             :         ELSE
      62             :         {
      63      170839 :             flag = 1;
      64      170839 :             move16();
      65             :         }
      66             :     }
      67             : 
      68       19853 :     minEnr = L_add( maxEnr, 0 );
      69      190692 :     FOR( i = j; i < end; i++ )
      70             :     {
      71      170839 :         minEnr = L_min( minEnr, pt_enr_ssf[i] ); /* Qx */
      72             :     }
      73             : 
      74             : 
      75       19853 :     minEnr = L_add_sat( minEnr, 100000 );
      76       19853 :     exp0 = norm_l( minEnr );
      77       19853 :     wtmp0 = extract_h( L_shl( minEnr, exp0 ) );
      78       19853 :     exp1 = sub( norm_l( maxEnr ), 1 );
      79       19853 :     wtmp1 = extract_h( L_shl( maxEnr, exp1 ) );
      80       19853 :     wtmp1 = div_s( wtmp1, wtmp0 );
      81             : #ifdef ISSUE_1770_replace_shr_ro
      82       19853 :     dE2 = shr_r_sat( wtmp1, add( sub( exp1, exp0 ), 15 - 10 ) ); /*Q10*/
      83             : #else
      84             :     dE2 = shr_ro( wtmp1, add( sub( exp1, exp0 ), 15 - 10 ), &Overflow ); /*Q10*/
      85             : #endif
      86             : 
      87       19853 :     return dE2;
      88             : }
      89             : 
      90             : /*-------------------------------------------------------------------*
      91             :  * find_uv_fx()
      92             :  *
      93             :  * Decision about coder type
      94             :  *-------------------------------------------------------------------*/
      95             : 
      96        3100 : Word16 find_uv_fx(                           /* o  : coding type                                                                                                        */
      97             :                    Encoder_State *st_fx,     /* i/o: encoder state structure                                                                            */
      98             :                    const Word16 *T_op_fr,    /* i  : pointer to adjusted fractional pitch (4 val.)                                      Q6*/
      99             :                    const Word16 *voicing_fr, /* i  : refined correlation for each subframes                                                     Q15*/
     100             :                    const Word16 *speech,     /* i  : pointer to speech signal for E computation                                         Q_new*/
     101             :                    const Word32 *ee,         /* i  : lf/hf Energy ratio for present frame                                                       Q6*/
     102             :                    const Word16 corr_shift,  /* i  : normalized correlation correction in noise                                         Q15*/
     103             :                    const Word16 relE,        /* i  : relative frame energy                                                                                      Q8*/
     104             :                    const Word16 Etot,        /* i  : total energy                                                                                                       Q8*/
     105             :                    const Word32 hp_E[],      /* i  : energy in HF                                                                                                       Q_new + Q_SCALE*/
     106             :                    const Word16 Q_new,
     107             :                    Word16 *flag_spitch, /* i/o: flag to indicate very short stable pitch and high correlation           Q0*/
     108             :                    const Word16 shift,
     109             :                    const Word16 last_core_orig /* i  : original last core                                                                                       Q0*/
     110             : )
     111             : {
     112             :     Word16 coder_type, i;
     113             :     Word32 mean_ee, dE1, fac_32;
     114             :     const Word16 *pt_speech;
     115             :     Word32 L_tmp, enr_ssf[2 * NB_SSF + 2 * NB_SSF + 2], E_min_th;
     116             :     Word16 dE2;
     117             :     Word16 ind_deltaMax, tmp_offset_flag;
     118             :     Word32 Ltmp0, *pt_enr_ssf, *pt_enr_ssf1, dE2_th;
     119             :     Word16 exp0, exp1, Q_in;
     120             :     Word16 wtmp0, wtmp1;
     121             :     Word16 fac, mean_voi3, dE3;
     122             :     Word16 relE_thres;
     123             :     Word16 mean_voi3_offset;
     124             :     Word16 voicing_m, dpit1, dpit2, dpit3;
     125             :     Word16 ee0_th, ee1_th, voi_th, nb_cond, flag_low_relE;
     126        3100 :     NOISE_EST_HANDLE hNoiseEst = st_fx->hNoiseEst;
     127        3100 :     SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
     128             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     129        3100 :     Flag Overflow = 0;
     130        3100 :     move32();
     131             : #endif
     132             : 
     133             :     Word16 Last_Resort;
     134             :     Word16 vadnoise;
     135             : 
     136        3100 :     IF( hSC_VBR != NULL )
     137             :     {
     138        3100 :         Last_Resort = hSC_VBR->Last_Resort; /* Q0 */
     139        3100 :         vadnoise = hSC_VBR->vadnoise_fx;
     140        3100 :         move16();
     141        3100 :         move16();
     142             :     }
     143             :     ELSE
     144             :     {
     145           0 :         Last_Resort = 0;
     146           0 :         vadnoise = 0;
     147           0 :         move16();
     148           0 :         move16();
     149             :     }
     150             : 
     151        3100 :     Q_in = sub( Q_new, 1 );
     152             : 
     153             :     /*-----------------------------------------------------------------*
     154             :      * Detect sudden energy increases to catch voice and music
     155             :      * temporal events (dE1)
     156             :      *
     157             :      * - Find maximum energy per short subblocks.
     158             :      *   Two subblock sets are used shifted by half the subblock length
     159             :      * - Find maximum energy ratio between adjacent subblocks
     160             :      *-----------------------------------------------------------------*/
     161             : 
     162             :     /* Find maximum energy per short subblocks  */
     163        3100 :     pt_speech = speech - SSF; /* Q_new */
     164        3100 :     pt_enr_ssf = enr_ssf + 2 * NB_SSF;
     165       58900 :     FOR( i = 0; i < 2 * ( NB_SSF + 1 ); i++ )
     166             :     {
     167       55800 :         emaximum_fx( Q_in, pt_speech, SSF, pt_enr_ssf );
     168       55800 :         pt_speech += ( SSF / 2 );
     169       55800 :         pt_enr_ssf++;
     170             :     }
     171             : 
     172        3100 :     dE1 = 0;
     173        3100 :     move16();
     174        3100 :     ind_deltaMax = 0;
     175        3100 :     move16();
     176        3100 :     pt_enr_ssf = enr_ssf + 2 * NB_SSF;
     177        3100 :     pt_enr_ssf1 = pt_enr_ssf + 2;
     178             : 
     179             :     /* Test on energy increase between adjacent sub-subframes */
     180        3100 :     exp1 = 0;
     181        3100 :     move16();
     182       52700 :     FOR( i = 0; i < 2 * NB_SSF; i++ )
     183             :     {
     184             :         /*fac = *pt_enr_ssf1 / (*pt_enr_ssf + 1);*/
     185       49600 :         Ltmp0 = L_max( *pt_enr_ssf, 1 );
     186       49600 :         exp0 = norm_l( Ltmp0 );
     187       49600 :         wtmp0 = extract_h( L_shl( Ltmp0, exp0 ) );
     188       49600 :         exp1 = sub( norm_l( *pt_enr_ssf1 ), 1 );
     189       49600 :         wtmp1 = extract_h( L_shl( *pt_enr_ssf1, exp1 ) );
     190       49600 :         fac = div_s( wtmp1, wtmp0 );
     191       49600 :         fac_32 = L_shr_o( L_deposit_l( fac ), add( sub( exp1, exp0 ), 15 - 13 ), &Overflow ); /* fac32 in Q13*/
     192             : 
     193       49600 :         if ( GT_32( fac_32, dE1 ) )
     194             :         {
     195       10463 :             ind_deltaMax = i;
     196       10463 :             move16();
     197             :         }
     198             : 
     199       49600 :         dE1 = L_max( dE1, fac_32 ); /* Q13 */
     200             : 
     201       49600 :         pt_enr_ssf++;
     202       49600 :         pt_enr_ssf1++;
     203             :     }
     204             : 
     205             :     /*-----------------------------------------------------------------*
     206             :      * Average spectral tilt
     207             :      * Average voicing (normalized correlation)
     208             :      *-----------------------------------------------------------------*/
     209             : 
     210             :     /*mean_ee = 1.0f/3.0f * (st->ee_old + ee[0] + ee[1]); */                              /* coefficients take into account the position of the window */
     211        3100 :     mean_ee = L_add_o( L_add_o( st_fx->ee_old_fx, ee[0], &Overflow ), ee[1], &Overflow ); /* Q6 */
     212        3100 :     mean_ee = Mult_32_16( mean_ee, 10923 /* 1/3 in Q15 */ );                              /*Q6*/
     213             : 
     214             :     /* mean_voi3 = 1.0f/3.0f * (voicing[0] + voicing[1] + voicing[2]);*/
     215        3100 :     Ltmp0 = L_mult( st_fx->voicing_fx[0], 10923 /* 1/3 in Q15 */ );               /* Q31 */
     216        3100 :     Ltmp0 = L_mac( Ltmp0, st_fx->voicing_fx[1], 10923 /* 1/3 in Q15 */ );         /* Q31 */
     217        3100 :     mean_voi3 = mac_r_sat( Ltmp0, st_fx->voicing_fx[2], 10923 /* 1/3 in Q15 */ ); /*Q15*/
     218             :     /*-----------------------------------------------------------------*
     219             :      * Total frame energy difference (dE3)
     220             :      *-----------------------------------------------------------------*/
     221             : 
     222        3100 :     dE3 = sub( Etot, hNoiseEst->Etot_last_fx ); /*Q8*/
     223             : 
     224             :     /*-----------------------------------------------------------------*
     225             :      * Energy decrease after spike (dE2)
     226             :      *-----------------------------------------------------------------*/
     227             : 
     228             :     /* set different thresholds and conditions for NB and WB input */
     229        3100 :     dE2_th = 30 << 10;
     230        3100 :     move32();
     231        3100 :     nb_cond = 1;
     232        3100 :     move16(); /* no additional condition for WB input */
     233        3100 :     IF( EQ_16( st_fx->input_bwidth, NB ) )
     234             :     {
     235           0 :         dE2_th = 21 << 10;
     236           0 :         move32();
     237           0 :         if ( GE_16( add_o( mean_voi3, corr_shift, &Overflow ), 22282 /* 0.68 in Q15 */ ) ) /*( mean_voi3 + corr_shift ) >= 0.68f*/
     238             :         {
     239           0 :             nb_cond = 0;
     240           0 :             move16();
     241             :         }
     242             :     }
     243             : 
     244             :     /* calcualte maximum energy decrease */
     245        3100 :     dE2 = 0;
     246        3100 :     move16(); /* Test on energy decrease after an energy spike  */
     247        3100 :     pt_enr_ssf = enr_ssf + 2 * NB_SSF;
     248             : 
     249        3100 :     test();
     250        3100 :     IF( GT_32( dE1, 30 << 13 ) && nb_cond ) /*>30 Q13*/
     251             :     {
     252          84 :         IF( LT_16( sub( shl( NB_SSF, 1 ), ind_deltaMax ), L_ENR ) )
     253             :         {
     254          61 :             st_fx->old_ind_deltaMax = ind_deltaMax; /* Q0 */
     255          61 :             move16();
     256          61 :             Copy32( pt_enr_ssf, st_fx->old_enr_ssf_fx, 2 * NB_SSF ); /* Qx */
     257             :         }
     258             :         ELSE
     259             :         {
     260          23 :             st_fx->old_ind_deltaMax = -1;
     261          23 :             move16();
     262          23 :             dE2 = find_ener_decrease_fx( ind_deltaMax, pt_enr_ssf ); /*Q10*/
     263             : 
     264          23 :             if ( GT_32( dE2, dE2_th ) )
     265             :             {
     266           2 :                 st_fx->spike_hyst = 0;
     267           2 :                 move16();
     268             :             }
     269             :         }
     270             :     }
     271             :     ELSE
     272             :     {
     273        3016 :         IF( st_fx->old_ind_deltaMax >= 0 )
     274             :         {
     275          59 :             Copy32( st_fx->old_enr_ssf_fx, enr_ssf, 2 * NB_SSF );            /* Qx */
     276          59 :             dE2 = find_ener_decrease_fx( st_fx->old_ind_deltaMax, enr_ssf ); /* Q10 */
     277             : 
     278          59 :             if ( GT_32( dE2, dE2_th ) )
     279             :             {
     280           3 :                 st_fx->spike_hyst = 1;
     281           3 :                 move16();
     282             :             }
     283             :         }
     284             : 
     285        3016 :         st_fx->old_ind_deltaMax = -1;
     286        3016 :         move16();
     287             :     }
     288             : 
     289             :     /*-----------------------------------------------------------------*
     290             :      * Detection of voiced offsets (tmp_offset_flag)
     291             :      *-----------------------------------------------------------------*/
     292             : 
     293        3100 :     tmp_offset_flag = 1;
     294        3100 :     move16();
     295             : 
     296        3100 :     IF( NE_16( st_fx->input_bwidth, NB ) )
     297             :     {
     298        3100 :         ee0_th = 154; /*2.4 in Q6 */
     299        3100 :         move16();
     300        3100 :         voi_th = 24248; /*0.74f Q15 */
     301        3100 :         move16();
     302             :     }
     303             :     ELSE
     304             :     {
     305           0 :         ee0_th = 627; /*9.8f Q6 */
     306           0 :         move16();
     307           0 :         voi_th = 24904; /*0.76f Q15*/
     308           0 :         move16();
     309             :     }
     310             : 
     311        3100 :     test();
     312        3100 :     test();
     313        3100 :     test();
     314        6069 :     if ( ( EQ_16( st_fx->last_coder_type_raw, UNVOICED ) ) ||                              /* previous frame was unvoiced  */
     315        3989 :          ( ( LT_32( ee[0], ee0_th ) ) && ( GT_32( hp_E[0], L_shl( E_MIN_FX, Q_new ) ) ) && /* energy is concentrated in high frequencies provided that some energy is present in HF */
     316         485 :            ( LT_16( add_o( st_fx->voicing_fx[0], corr_shift, &Overflow ), voi_th ) ) ) )   /* normalized correlation is low */
     317             :     {
     318         312 :         tmp_offset_flag = 0;
     319         312 :         move16();
     320             :     }
     321             : 
     322             :     /*-----------------------------------------------------------------*
     323             :      * Decision about UC
     324             :      *-----------------------------------------------------------------*/
     325             : 
     326             :     /* SC-VBR - set additional parameters and thresholds for SC-VBR */
     327        3100 :     mean_voi3_offset = 0;
     328        3100 :     move16();
     329        3100 :     flag_low_relE = 0;
     330        3100 :     move16();
     331        3100 :     ee1_th = 608; /*9.5 Q6*/
     332        3100 :     move16();
     333        3100 :     IF( st_fx->Opt_SC_VBR || ( EQ_16( st_fx->idchan, 1 ) && EQ_16( st_fx->element_mode, IVAS_CPE_TD ) ) ) /* Allow the low energy flag for the secondary channel */
     334             :     {
     335           0 :         ee1_th = 544; /*8.5f Q6*/
     336           0 :         move16();
     337             : 
     338             :         /* SC-VBR - determine the threshold on relative energy as a function of lp_noise */
     339           0 :         IF( NE_16( st_fx->input_bwidth, NB ) )
     340             :         {
     341             :             /*relE_thres = 0.700f * st->lp_noise - 33.5f; (lp_noise in Q8, constant Q8<<16) */
     342           0 :             L_tmp = L_mac( -562036736 /* 33.5f in Q24 */, 22938 /* 0.7 in Q15 */, st_fx->lp_noise_fx ); // Q24
     343           0 :             if ( Last_Resort == 0 )
     344             :             {
     345             :                 /*relE_thres = 0.650f * st->lp_noise - 33.5f; (lp_noise in Q8, constant Q8<<16)*/
     346           0 :                 L_tmp = L_mac( -562036736 /* 33.5f in Q24 */, 21299 /* 0.650f in Q15 */, st_fx->lp_noise_fx ); // Q24
     347             :             }
     348           0 :             relE_thres = round_fx( L_tmp );
     349             :         }
     350             :         ELSE
     351             :         {
     352             : 
     353             :             /*relE_thres = 0.60f * st->lp_noise - 28.2f; (lp_noise in Q8, constant Q8<<16)*/
     354           0 :             L_tmp = L_mac( -473117491 /* 28.2f in Q24 */, 19661 /* 0.6f in Q15 */, st_fx->lp_noise_fx ); // Q24
     355           0 :             relE_thres = round_fx( L_tmp );
     356             :         }
     357           0 :         relE_thres = s_max( relE_thres, -6400 /* -25.0f in Q8 */ ); /* Q8 */
     358             : 
     359             :         /* SC-VBR = set flag on low relative energy */
     360           0 :         if ( LT_16( relE, relE_thres ) )
     361             :         {
     362           0 :             flag_low_relE = 1;
     363           0 :             move16();
     364             :         }
     365             : 
     366             :         /* SC-VBR - correction of voicing threshold for NB inputs (important only in noisy conditions) */
     367           0 :         test();
     368           0 :         if ( EQ_16( st_fx->input_bwidth, NB ) && LT_16( vadnoise, 20 << 8 ) ) /* vadnoise in Q8, constant Q0<<8 */
     369             :         {
     370           0 :             mean_voi3_offset = 1638; /*0.05f Q15*/
     371           0 :             move16();
     372             :         }
     373             :     }
     374             : 
     375             :     /* make decision whether frame is unvoiced */
     376        3100 :     E_min_th = L_shl( E_MIN_FX, Q_new );
     377        3100 :     coder_type = GENERIC;
     378        3100 :     move16();
     379        3100 :     IF( EQ_16( st_fx->input_bwidth, NB ) )
     380             :     {
     381           0 :         test();
     382           0 :         test();
     383           0 :         test();
     384           0 :         test();
     385           0 :         test();
     386           0 :         test();
     387           0 :         test();
     388           0 :         test();
     389           0 :         test();
     390           0 :         test();
     391           0 :         test();
     392           0 :         if ( ( ( LT_16( add_o( mean_voi3, corr_shift, &Overflow ), add( 22282 /* 0.68 in Q15 */, mean_voi3_offset ) ) ) && /* normalized correlation low  */
     393           0 :                ( LT_16( add_o( st_fx->voicing_fx[2], corr_shift, &Overflow ), 25887 /* 0.79 in Q15 */ ) ) &&               /* normalized correlation low on look-ahead - onset detection */
     394           0 :                ( LT_32( ee[0], 640 /* 10.0f in Q6 */ ) ) && ( GT_32( hp_E[0], E_min_th ) ) &&                              /* energy concentrated in high frequencies provided that some energy is present in HF...  */
     395           0 :                ( LT_32( ee[1], ee1_th ) ) && ( GT_32( hp_E[1], E_min_th ) ) &&                                             /* ... biased towards look-ahead to detect onsets  */
     396           0 :                ( tmp_offset_flag == 0 ) &&                                                                                 /* Take care of voiced offsets */
     397             :                /*( st_fx->music_hysteresis_fx == 0 ) &&*/                                                                  /*  ... and in segment after AUDIO frames   */
     398           0 :                ( LE_32( dE1, 237568 /* 29.0f in Q13 */ ) ) &&                                                              /* Avoid on sharp energy spikes  */
     399           0 :                ( LE_32( st_fx->old_dE1_fx, 237568 /* 29.0f in Q13 */ ) ) &&                                                /*   + one frame hysteresis   */
     400           0 :                ( st_fx->spike_hyst < 0 ) ) ||                                                                              /* Avoid after sharp energy spikes followed by decay (e.g. castanets) */
     401             :              flag_low_relE )                                                                                               /* low relative frame energy (only for SC-VBR) */
     402             :         {
     403           0 :             coder_type = UNVOICED;
     404           0 :             move16();
     405             :         }
     406             :     }
     407             :     ELSE
     408             :     {
     409        3100 :         test();
     410        3100 :         test();
     411        3100 :         test();
     412        3100 :         test();
     413        3100 :         test();
     414        3100 :         test();
     415        3100 :         test();
     416        3100 :         test();
     417        3100 :         test();
     418        3100 :         test();
     419        3100 :         test();
     420        3100 :         test();
     421        4035 :         if ( ( ( LT_16( add_o( mean_voi3, corr_shift, &Overflow ), add( 22774 /* 0.695f in Q15 */, mean_voi3_offset ) ) ) && /* normalized correlation low  */
     422        1768 :                ( LT_16( add_sat( st_fx->voicing_fx[2], corr_shift ), 25887 /* 0.8 in Q15 */ ) ) &&                           /* normalized correlation low on look-ahead - onset detection */
     423        1353 :                ( LT_32( ee[0], 397 /* 6.2f in Q6 */ ) ) && ( GT_32( hp_E[0], E_min_th ) ) &&                                 /* energy concentrated in high frequencies provided that some energy is present in HF...  */
     424         467 :                ( LT_32( ee[1], 397 /* 6.2f in Q6 */ ) ) && ( GT_32( hp_E[1], E_min_th ) ) &&                                 /* ... biased towards look-ahead to detect onsets  */
     425         151 :                ( tmp_offset_flag == 0 ) &&                                                                                   /* Take care of voiced offsets */
     426             :                /*( st_fx->music_hysteresis_fx == 0 ) &&  */                                                                  /*  ... and in segment after AUDIO frames   */
     427         292 :                ( LE_32( dE1, 245760 /* 30.0f in Q13 */ ) ) &&                                                                /* Avoid on sharp energy spikes  */
     428         141 :                ( LE_32( st_fx->old_dE1_fx, 245760 /* 30.0f in Q13 */ ) ) &&                                                  /*   + one frame hysteresis   */
     429         132 :                ( st_fx->spike_hyst < 0 ) )                                                                                   /* Avoid after sharp energy spikes followed by decay (e.g. castanets) */
     430        2969 :              || ( flag_low_relE && ( LE_32( st_fx->old_dE1_fx, 245760 /* 30.0f in Q13 */ ) ) ) )                             /* low relative frame energy (only for SC-VBR) */
     431             :         {
     432         131 :             coder_type = UNVOICED;
     433         131 :             move16();
     434             :         }
     435             :     }
     436             : 
     437             :     /*-----------------------------------------------------------------*
     438             :      * Decision about VC
     439             :      *-----------------------------------------------------------------*/
     440        3100 :     if ( st_fx->Opt_SC_VBR )
     441             :     {
     442           0 :         hSC_VBR->set_ppp_generic = 0;
     443             :     }
     444        3100 :     move16();
     445             : 
     446        3100 :     test();
     447        3100 :     test();
     448        3100 :     IF( EQ_16( st_fx->localVAD, 1 ) && EQ_16( coder_type, GENERIC ) && NE_16( last_core_orig, AMR_WB_CORE ) )
     449             :     {
     450        2864 :         dpit1 = abs_s( sub( T_op_fr[1], T_op_fr[0] ) ); // Q6
     451        2864 :         dpit2 = abs_s( sub( T_op_fr[2], T_op_fr[1] ) ); // Q6
     452        2864 :         dpit3 = abs_s( sub( T_op_fr[3], T_op_fr[2] ) ); // Q6
     453             : 
     454        2864 :         test();
     455        2864 :         test();
     456        2864 :         test();
     457        2864 :         test();
     458        2864 :         test();
     459        2864 :         test();
     460        2864 :         test();
     461        2864 :         test();
     462        2864 :         test();
     463        2864 :         IF( ( GT_16( voicing_fr[0], 19825 /* 0.605 in Q15 */ ) ) && /* normalized correlation high in 1st sf.  */
     464             :             ( GT_16( voicing_fr[1], 19825 /* 0.605 in Q15 */ ) ) && /* normalized correlation high in 2st sf.  */
     465             :             ( GT_16( voicing_fr[2], 19825 /* 0.605 in Q15 */ ) ) && /* normalized correlation high in 3st sf.  */
     466             :             ( GT_16( voicing_fr[3], 19825 /* 0.605 in Q15 */ ) ) && /* normalized correlation high in 4st sf.  */
     467             :             ( GT_32( mean_ee, 256 /* 4.0f in Q6 */ ) ) &&           /* energy concentrated in low frequencies  */
     468             :             ( LT_16( dpit1, 3 << 6 ) ) &&
     469             :             ( LT_16( dpit2, 3 << 6 ) ) &&
     470             :             ( LT_16( dpit3, 3 << 6 ) ) )
     471             :         {
     472        1383 :             coder_type = VOICED;
     473        1383 :             move16();
     474             :         }
     475        1481 :         ELSE IF( st_fx->Opt_SC_VBR && EQ_16( st_fx->input_bwidth, NB ) && LT_16( vadnoise, 20 << 8 ) )
     476             :         {
     477           0 :             test();
     478           0 :             test();
     479           0 :             test();
     480           0 :             test();
     481           0 :             test();
     482           0 :             test();
     483           0 :             test();
     484           0 :             IF( GT_16( voicing_fr[0], 8192 /* 0.25 in Q15 */ ) &&     /* normalized correlation high in 1st sf.  */
     485             :                 ( GT_16( voicing_fr[1], 8192 /* 0.25 in Q15 */ ) ) && /* normalized correlation high in 2st sf.  */
     486             :                 ( GT_16( voicing_fr[2], 8192 /* 0.25 in Q15 */ ) ) && /* normalized correlation high in 3st sf.  */
     487             :                 ( GT_16( voicing_fr[3], 8192 /* 0.25 in Q15 */ ) ) && /* normalized correlation high in 4st sf.  */
     488             :                 ( GT_32( mean_ee, 64 ) ) &&                           /* energy concentrated in low frequencies  */
     489             :                 ( LT_16( dpit1, 5 << 6 ) ) &&
     490             :                 ( LT_16( dpit2, 5 << 6 ) ) &&
     491             :                 ( LT_16( dpit3, 5 << 6 ) ) )
     492             :             {
     493           0 :                 hSC_VBR->set_ppp_generic = 1;
     494           0 :                 move16();
     495           0 :                 coder_type = VOICED;
     496           0 :                 move16();
     497             :             }
     498             :         }
     499             : 
     500             :         /* set VOICED mode for frames with very stable pitch and high correlation
     501             :            and avoid to switch to AUDIO/MUSIC later                              */
     502        2864 :         voicing_m = mac_r( L_mac( L_mac( L_mult( voicing_fr[3], 8192 /* 0.25 in Q15 */ ), voicing_fr[2], 8192 /* 0.25 in Q15 */ ), voicing_fr[1], 8192 /* 0.25 in Q15 */ ), voicing_fr[0], 8192 /* 0.25 in Q15 */ );
     503        2864 :         test();
     504        2864 :         test();
     505        2864 :         test();
     506        2864 :         test();
     507        2864 :         test();
     508        2864 :         IF( *flag_spitch || ( LE_16( dpit1, 3 << 6 ) && LE_16( dpit2, 3 << 6 ) && LE_16( dpit3, 3 << 6 ) &&
     509             :                               GT_16( voicing_m, 31130 /* 0.95f in Q15 */ ) && GT_16( st_fx->voicing_sm_fx, 31785 /* 0.97f in Q15 */ ) ) )
     510             :         {
     511          48 :             coder_type = VOICED;
     512          48 :             move16();
     513          48 :             *flag_spitch = 1;
     514          48 :             move16(); /*to avoid switch to AUDIO/MUSIC later*/
     515             :         }
     516             :     }
     517             : 
     518             :     /*-----------------------------------------------------------------*
     519             :      * Channel-aware mode - set RF mode and total bitrate
     520             :      *-----------------------------------------------------------------*/
     521             : 
     522        3100 :     st_fx->rf_mode = st_fx->Opt_RF_ON; /* Q0 */
     523        3100 :     move16();
     524             : 
     525        3100 :     IF( EQ_16( coder_type, GENERIC ) )
     526             :     {
     527        1586 :         test();
     528        1586 :         test();
     529        1586 :         test();
     530        1586 :         test();
     531        1586 :         IF( ( LT_16( voicing_fr[0], 6554 /* 0.2f in Q15 */ ) ) && /* normalized correlation high in 2st sf.  */
     532             :             ( LT_16( voicing_fr[1], 6554 /* 0.2f in Q15 */ ) ) && /* normalized correlation high in 2st sf.  */
     533             :             ( LT_16( voicing_fr[2], 6554 /* 0.2f in Q15 */ ) ) && /* normalized correlation high in 3rd sf.  */
     534             :             ( LT_16( voicing_fr[3], 6554 /* 0.2f in Q15 */ ) ) && /* normalized correlation high in 4th sf.  */
     535             :             ( GT_16( vadnoise, 25 << 8 ) ) )                      /* when speech is clean */
     536             : 
     537             :         {
     538           0 :             st_fx->rf_mode = 0;
     539           0 :             move16();
     540             :             /* Current frame cannot be compressed to pack the partial redundancy;*/
     541             : 
     542           0 :             if ( st_fx->rf_mode != st_fx->Opt_RF_ON )
     543             :             {
     544           0 :                 core_coder_mode_switch_fx( st_fx, st_fx->last_total_brate, 0, shift );
     545             :             }
     546             :         }
     547             :     }
     548             : 
     549             :     /*-----------------------------------------------------------------*
     550             :      * Updates
     551             :      *-----------------------------------------------------------------*/
     552             : 
     553             :     /* update spike hysteresis parameters */
     554        3100 :     test();
     555        3100 :     if ( st_fx->spike_hyst >= 0 && LT_16( st_fx->spike_hyst, 2 ) )
     556             :     {
     557           7 :         st_fx->spike_hyst = add( st_fx->spike_hyst, 1 ); /* Q0 */
     558             :     }
     559             : 
     560             :     /* reset spike hysteresis */
     561        3100 :     test();
     562        3100 :     test();
     563        3100 :     test();
     564        3146 :     if ( ( GT_16( st_fx->spike_hyst, 1 ) ) &&
     565          89 :          ( GT_16( dE3, 5 << 8 ) ||                                                              /* energy increases               */
     566          61 :            ( GT_16( relE, -3328 ) && ( GT_16( add_sat( mean_voi3, corr_shift ), 22774 ) ) ) ) ) /* normalized correlation is high */
     567             :     {
     568           5 :         st_fx->spike_hyst = -1;
     569           5 :         move16();
     570             :     }
     571             : 
     572             :     /* update tilt parameters */
     573        3100 :     st_fx->ee_old_fx = ee[1];
     574        3100 :     move32(); /*Q6*/
     575        3100 :     st_fx->old_dE1_fx = dE1;
     576        3100 :     move32(); /*Q13*/
     577             : 
     578             :     /* save the raw coder_type for various modules later in the codec (the reason is that e.g. UNVOICED is lost at higher rates) */
     579        3100 :     st_fx->coder_type_raw = coder_type;
     580        3100 :     move16();
     581             : 
     582        3100 :     return coder_type;
     583             : }
     584             : 
     585             : /*-------------------------------------------------------------------*
     586             :  * find_uv()
     587             :  *
     588             :  * Decision about coder type
     589             :  *-------------------------------------------------------------------*/
     590     1111194 : Word16 find_uv_ivas_fx(                                       /* o  : coding type                                                   */
     591             :                         Encoder_State *st_fx,                 /* i/o: encoder state structure                                       */
     592             :                         const Word16 *T_op_fr,                /* i  : pointer to adjusted fractional pitch (4 val.)                                     Q6*/
     593             :                         const Word16 *voicing_fr,             /* i  : refined correlation for each subframes                                            Q15*/
     594             :                         const Word16 *speech,                 /* i  : pointer to speech signal for E computation                                        Q_new*/
     595             :                         const Word32 *ee,                     /* i  : lf/hf Energy ratio for present frame                                                      Q6*/
     596             :                         Word32 *dE1X,                         /* o  : sudden energy increase for S/M classifier                     Q13*/
     597             :                         const Word16 corr_shift,              /* i  : normalized correlation correction in noise                                        Q15*/
     598             :                         const Word16 relE,                    /* i  : relative frame energy                                                                                     Q8*/
     599             :                         const Word16 Etot,                    /* i  : total energy                                                                                                      Q8*/
     600             :                         const Word32 hp_E[],                  /* i  : energy in HF                                                                                                      q_hp_E*/
     601             :                         Word16 *flag_spitch,                  /* i/o: flag to indicate very short stable pitch and high correlation Q0*/
     602             :                         const Word16 last_core_orig,          /* i  : original last core                                            Q0*/
     603             :                         STEREO_CLASSIF_HANDLE hStereoClassif, /* i/o: stereo classifier structure                                   */
     604             :                         const Word16 Q_new,
     605             :                         const Word16 q_hp_E )
     606             : {
     607             :     Word16 coder_type, i;
     608             :     Word32 mean_ee, dE1, fac_32;
     609             :     const Word16 *pt_speech;
     610             :     Word32 L_tmp, enr_ssf[2 * NB_SSF + 2 * NB_SSF + 2], E_min_th;
     611             :     Word16 dE2;
     612             :     Word16 ind_deltaMax, tmp_offset_flag;
     613             :     Word32 Ltmp0, *pt_enr_ssf, *pt_enr_ssf1, dE2_th;
     614             :     Word16 exp0, exp1;
     615             :     Word16 wtmp0, wtmp1;
     616             :     Word16 fac, mean_voi3, dE3;
     617             :     Word16 relE_thres;
     618             :     Word16 mean_voi3_offset;
     619             :     Word16 voicing_m, dpit1, dpit2, dpit3;
     620             :     Word16 ee0_th, ee1_th, voi_th, nb_cond, flag_low_relE;
     621     1111194 :     NOISE_EST_HANDLE hNoiseEst = st_fx->hNoiseEst;
     622     1111194 :     SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
     623             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     624     1111194 :     Flag Overflow = 0;
     625     1111194 :     move32();
     626             : #endif
     627             :     Word16 Last_Resort;
     628             :     Word16 vadnoise;
     629             : 
     630     1111194 :     IF( hSC_VBR != NULL )
     631             :     {
     632           0 :         Last_Resort = hSC_VBR->Last_Resort; /* Q0 */
     633           0 :         move16();
     634           0 :         vadnoise = hSC_VBR->vadnoise_fx; /* Q8 */
     635           0 :         move16();
     636             :     }
     637             :     ELSE
     638             :     {
     639     1111194 :         Last_Resort = 0;
     640     1111194 :         move16();
     641     1111194 :         vadnoise = 0;
     642     1111194 :         move16();
     643             :     }
     644             : 
     645             :     /*-----------------------------------------------------------------*
     646             :      * Detect sudden energy increases to catch voice and music
     647             :      * temporal events (dE1)
     648             :      *
     649             :      * - Find maximum energy per short subblocks.
     650             :      *   Two subblock sets are used shifted by half the subblock length
     651             :      * - Find maximum energy ratio between adjacent subblocks
     652             :      *-----------------------------------------------------------------*/
     653             : 
     654             :     /* Find maximum energy per short subblocks  */
     655     1111194 :     pt_speech = speech - SSF;
     656     1111194 :     pt_enr_ssf = enr_ssf + 2 * NB_SSF;
     657    21112686 :     FOR( i = 0; i < 2 * ( NB_SSF + 1 ); i++ )
     658             :     {
     659    20001492 :         emaximum_fx( Q_new, pt_speech, SSF, pt_enr_ssf );
     660    20001492 :         pt_speech += ( SSF / 2 );
     661    20001492 :         pt_enr_ssf++;
     662             :     }
     663             : 
     664     1111194 :     dE1 = 0;
     665     1111194 :     move16();
     666     1111194 :     ind_deltaMax = 0;
     667     1111194 :     move16();
     668     1111194 :     pt_enr_ssf = enr_ssf + 2 * NB_SSF;
     669     1111194 :     pt_enr_ssf1 = pt_enr_ssf + 2;
     670             : 
     671             :     /* Test on energy increase between adjacent sub-subframes */
     672     1111194 :     exp1 = 0;
     673     1111194 :     move16();
     674    18890298 :     FOR( i = 0; i < 2 * NB_SSF; i++ )
     675             :     {
     676             :         /*fac = *pt_enr_ssf1 / (*pt_enr_ssf + 1);*/
     677    17779104 :         Ltmp0 = L_max( *pt_enr_ssf, 1 );
     678    17779104 :         exp0 = norm_l( Ltmp0 );
     679    17779104 :         wtmp0 = extract_h( L_shl( Ltmp0, exp0 ) );
     680    17779104 :         exp1 = sub( norm_l( *pt_enr_ssf1 ), 1 );
     681    17779104 :         wtmp1 = extract_h( L_shl( *pt_enr_ssf1, exp1 ) );
     682    17779104 :         fac = div_s( wtmp1, wtmp0 );
     683    17779104 :         fac_32 = L_shr_o( L_deposit_l( fac ), add( sub( exp1, exp0 ), 15 - 13 ), &Overflow ); /* fac32 in Q13*/
     684             : 
     685    17779104 :         if ( GT_32( fac_32, dE1 ) )
     686             :         {
     687     3433463 :             ind_deltaMax = i;
     688     3433463 :             move16();
     689             :         }
     690             : 
     691    17779104 :         dE1 = L_max( dE1, fac_32 ); /* Q13 */
     692             : 
     693    17779104 :         pt_enr_ssf++;
     694    17779104 :         pt_enr_ssf1++;
     695             :     }
     696             : 
     697     1111194 :     IF( hStereoClassif != NULL )
     698             :     {
     699      760851 :         IF( st_fx->idchan == 0 )
     700             :         {
     701      410255 :             hStereoClassif->dE1_ch1_fx = dE1; /* Q13 */
     702      410255 :             move32();
     703      410255 :             hStereoClassif->dE1_ch1_e = 31 - Q13;
     704      410255 :             move16();
     705             :         }
     706             :         ELSE
     707             :         {
     708      350596 :             hStereoClassif->dE1_ch2_fx = dE1; /* Q13 */
     709      350596 :             move32();
     710      350596 :             hStereoClassif->dE1_ch2_e = 31 - Q13;
     711      350596 :             move16();
     712             :         }
     713             :     }
     714             : 
     715     1111194 :     if ( dE1X != NULL )
     716             :     {
     717     1111194 :         *dE1X = dE1; /* Q13 */
     718     1111194 :         move32();
     719             :     }
     720             : 
     721             :     /*-----------------------------------------------------------------*
     722             :      * Average spectral tilt
     723             :      * Average voicing (normalized correlation)
     724             :      *-----------------------------------------------------------------*/
     725             : 
     726             :     /*mean_ee = 1.0f/3.0f * (st->ee_old + ee[0] + ee[1]); */                              /* coefficients take into account the position of the window */
     727     1111194 :     mean_ee = L_add_o( L_add_o( st_fx->ee_old_fx, ee[0], &Overflow ), ee[1], &Overflow ); /* Q6 */
     728     1111194 :     mean_ee = Mult_32_16( mean_ee, 10923 );                                               /*Q6*/
     729             : 
     730             :     /* mean_voi3 = 1.0f/3.0f * (voicing[0] + voicing[1] + voicing[2]);*/
     731     1111194 :     Ltmp0 = L_mult( st_fx->voicing_fx[0], 10923 /* 1/3 in Q15 */ );               /* Q31 */
     732     1111194 :     Ltmp0 = L_mac( Ltmp0, st_fx->voicing_fx[1], 10923 /* 1/3 in Q15 */ );         /* Q31 */
     733     1111194 :     mean_voi3 = mac_r_sat( Ltmp0, st_fx->voicing_fx[2], 10923 /* 1/3 in Q15 */ ); /*Q15*/
     734             :     /*-----------------------------------------------------------------*
     735             :      * Total frame energy difference (dE3)
     736             :      *-----------------------------------------------------------------*/
     737             : 
     738     1111194 :     dE3 = sub( Etot, extract_h( hNoiseEst->Etot_last_32fx ) ); /*Q8*/
     739             : 
     740             :     /*-----------------------------------------------------------------*
     741             :      * Energy decrease after spike (dE2)
     742             :      *-----------------------------------------------------------------*/
     743             : 
     744             :     /* set different thresholds and conditions for NB and WB input */
     745     1111194 :     dE2_th = 30 << 10;
     746     1111194 :     move32();
     747     1111194 :     nb_cond = 1;
     748     1111194 :     move16(); /* no additional condition for WB input */
     749     1111194 :     IF( st_fx->input_bwidth == NB )
     750             :     {
     751        4290 :         dE2_th = 21 << 10;
     752        4290 :         move32();
     753        4290 :         if ( GE_16( add_o( mean_voi3, corr_shift, &Overflow ), 22282 ) ) /*( mean_voi3 + corr_shift ) >= 0.68f*/
     754             :         {
     755        2880 :             nb_cond = 0;
     756        2880 :             move16();
     757             :         }
     758             :     }
     759             : 
     760             :     /* calcualte maximum energy decrease */
     761     1111194 :     dE2 = 0;
     762     1111194 :     move16(); /* Test on energy decrease after an energy spike  */
     763     1111194 :     pt_enr_ssf = enr_ssf + 2 * NB_SSF;
     764             : 
     765     1111194 :     test();
     766     1111194 :     IF( GT_32( dE1, 30 << 13 ) && nb_cond ) /*>30 Q13*/
     767             :     {
     768       18754 :         IF( LT_16( sub( shl( NB_SSF, 1 ), ind_deltaMax ), L_ENR ) )
     769             :         {
     770        7918 :             st_fx->old_ind_deltaMax = ind_deltaMax; /* Q0 */
     771        7918 :             move16();
     772        7918 :             Copy32( pt_enr_ssf, st_fx->old_enr_ssf_fx, 2 * NB_SSF ); /* Qx */
     773             :         }
     774             :         ELSE
     775             :         {
     776       10836 :             st_fx->old_ind_deltaMax = -1;
     777       10836 :             move16();
     778       10836 :             dE2 = find_ener_decrease_fx( ind_deltaMax, pt_enr_ssf ); /*Q10*/
     779             : 
     780       10836 :             if ( GT_32( dE2, dE2_th ) )
     781             :             {
     782         138 :                 st_fx->spike_hyst = 0;
     783         138 :                 move16();
     784             :             }
     785             :         }
     786             :     }
     787             :     ELSE
     788             :     {
     789     1092440 :         IF( st_fx->old_ind_deltaMax >= 0 )
     790             :         {
     791        8935 :             Copy32( st_fx->old_enr_ssf_fx, enr_ssf, 2 * NB_SSF );            /* Qx */
     792        8935 :             dE2 = find_ener_decrease_fx( st_fx->old_ind_deltaMax, enr_ssf ); /* Q10 */
     793             : 
     794        8935 :             if ( GT_32( dE2, dE2_th ) )
     795             :             {
     796         107 :                 st_fx->spike_hyst = 1;
     797         107 :                 move16();
     798             :             }
     799             :         }
     800             : 
     801     1092440 :         st_fx->old_ind_deltaMax = -1;
     802     1092440 :         move16();
     803             :     }
     804             : 
     805             :     /*-----------------------------------------------------------------*
     806             :      * Detection of voiced offsets (tmp_offset_flag)
     807             :      *-----------------------------------------------------------------*/
     808             : 
     809     1111194 :     tmp_offset_flag = 1;
     810     1111194 :     move16();
     811             : 
     812     1111194 :     IF( st_fx->input_bwidth != NB )
     813             :     {
     814     1106904 :         ee0_th = 154; /*2.4 in Q6 */
     815     1106904 :         move16();
     816     1106904 :         voi_th = 24248; /*0.74f Q15 */
     817     1106904 :         move16();
     818             :     }
     819             :     ELSE
     820             :     {
     821        4290 :         ee0_th = 627; /*9.8f Q6 */
     822        4290 :         move16();
     823        4290 :         voi_th = 24904; /*0.76f Q15*/
     824        4290 :         move16();
     825             :     }
     826             : 
     827     1111194 :     E_min_th = L_shl( E_MIN_IVAS_FX_Q31, sub( q_hp_E, Q31 ) );
     828             : 
     829     1111194 :     test();
     830     1111194 :     test();
     831     1111194 :     test();
     832     1111194 :     IF( ( EQ_16( st_fx->last_coder_type_raw, UNVOICED ) ) ||                            /* previous frame was unvoiced  */
     833             :         ( ( LT_32( ee[0], ee0_th ) ) && ( GT_32( hp_E[0], E_min_th ) ) &&               /* energy is concentrated in high frequencies provided that some energy is present in HF */
     834             :           ( LT_16( add_o( st_fx->voicing_fx[0], corr_shift, &Overflow ), voi_th ) ) ) ) /* normalized correlation is low */
     835             :     {
     836      127561 :         tmp_offset_flag = 0;
     837      127561 :         move16();
     838             :     }
     839             : 
     840             :     /*-----------------------------------------------------------------*
     841             :      * Decision about UC
     842             :      *-----------------------------------------------------------------*/
     843             : 
     844             :     /* SC-VBR - set additional parameters and thresholds for SC-VBR */
     845     1111194 :     mean_voi3_offset = 0;
     846     1111194 :     move16();
     847     1111194 :     flag_low_relE = 0;
     848     1111194 :     move16();
     849     1111194 :     ee1_th = 608; /*9.5 Q6*/
     850     1111194 :     move16();
     851     1111194 :     test();
     852     1111194 :     test();
     853     1111194 :     IF( st_fx->Opt_SC_VBR || ( EQ_16( st_fx->idchan, 1 ) && EQ_16( st_fx->element_mode, IVAS_CPE_TD ) ) ) /* Allow the low energy flag for the secondary channel */
     854             :     {
     855        3811 :         ee1_th = 544; /*8.5f Q6*/
     856        3811 :         move16();
     857             : 
     858             :         /* SC-VBR - determine the threshold on relative energy as a function of lp_noise */
     859        3811 :         IF( st_fx->input_bwidth != NB )
     860             :         {
     861             :             /*relE_thres = 0.700f * st->lp_noise - 33.5f; (lp_noise in Q8, constant Q8<<16) */
     862        3811 :             L_tmp = L_mac( -562036736 /* 33.5f in Q24 */, 22938 /* 0.7 in Q15 */, extract_h( st_fx->lp_noise_32fx ) ); // Q24
     863        3811 :             IF( Last_Resort == 0 )
     864             :             {
     865             :                 /*relE_thres = 0.650f * st->lp_noise - 33.5f; (lp_noise in Q8, constant Q8<<16)*/
     866        3811 :                 L_tmp = L_mac( -562036736 /* 33.5f in Q24 */, 21299 /* 0.650f in Q15 */, extract_h( st_fx->lp_noise_32fx ) ); // Q24
     867             :             }
     868        3811 :             relE_thres = round_fx( L_tmp );
     869             :         }
     870             :         ELSE
     871             :         {
     872             : 
     873             :             /*relE_thres = 0.60f * st->lp_noise - 28.2f; (lp_noise in Q8, constant Q8<<16)*/
     874           0 :             L_tmp = L_mac( -473117491 /* 28.2f in Q24 */, 19661 /* 0.6f in Q15 */, extract_h( st_fx->lp_noise_32fx ) ); // Q24
     875           0 :             relE_thres = round_fx( L_tmp );
     876             :         }
     877        3811 :         relE_thres = s_max( relE_thres, -6400 /* -25.0f in Q8 */ ); /* Q8 */
     878             : 
     879             :         /* SC-VBR = set flag on low relative energy */
     880        3811 :         if ( LT_16( relE, relE_thres ) )
     881             :         {
     882         332 :             flag_low_relE = 1;
     883         332 :             move16();
     884             :         }
     885             : 
     886             :         /* SC-VBR - correction of voicing threshold for NB inputs (important only in noisy conditions) */
     887        3811 :         test();
     888        3811 :         if ( st_fx->input_bwidth == NB && LT_16( vadnoise, 20 << 8 ) ) /* vadnoise in Q8, constant Q0<<8 */
     889             :         {
     890           0 :             mean_voi3_offset = 1638; /*0.05f Q15*/
     891           0 :             move16();
     892             :         }
     893             :     }
     894             : 
     895             :     /* make decision whether frame is unvoiced */
     896     1111194 :     coder_type = GENERIC;
     897     1111194 :     move16();
     898     1111194 :     IF( st_fx->input_bwidth == NB )
     899             :     {
     900        4290 :         test();
     901        4290 :         test();
     902        4290 :         test();
     903        4290 :         test();
     904        4290 :         test();
     905        4290 :         test();
     906        4290 :         test();
     907        4290 :         test();
     908        4290 :         test();
     909        4290 :         test();
     910        4290 :         IF( ( ( LT_16( add_o( mean_voi3, corr_shift, &Overflow ), add( 22282 /* 0.68 in Q15 */, mean_voi3_offset ) ) ) && /* normalized correlation low  */
     911             :               ( LT_16( add_o( st_fx->voicing_fx[2], corr_shift, &Overflow ), 25887 /* 0.79 in Q15 */ ) ) &&               /* normalized correlation low on look-ahead - onset detection */
     912             :               ( LT_32( ee[0], 640 /* 10.0f in Q6 */ ) ) && ( GT_32( hp_E[0], E_min_th ) ) &&                              /* energy concentrated in high frequencies provided that some energy is present in HF...  */
     913             :               ( LT_32( ee[1], ee1_th ) ) && ( GT_32( hp_E[1], E_min_th ) ) &&                                             /* ... biased towards look-ahead to detect onsets  */
     914             :               ( tmp_offset_flag == 0 ) &&                                                                                 /* Take care of voiced offsets */
     915             :               /*( st_fx->music_hysteresis_fx == 0 ) &&*/                                                                  /*  ... and in segment after AUDIO frames   */
     916             :               ( LE_32( dE1, 237568 /* 29.0f in Q13 */ ) ) &&                                                              /* Avoid on sharp energy spikes  */
     917             :               ( LE_32( st_fx->old_dE1_fx, 237568 /* 29.0f in Q13 */ ) ) &&                                                /*   + one frame hysteresis   */
     918             :               ( st_fx->spike_hyst < 0 ) ) ||                                                                              /* Avoid after sharp energy spikes followed by decay (e.g. castanets) */
     919             :             flag_low_relE )                                                                                               /* low relative frame energy (only for SC-VBR) */
     920             :         {
     921         121 :             coder_type = UNVOICED;
     922         121 :             move16();
     923             :         }
     924             :     }
     925             :     ELSE
     926             :     {
     927     1106904 :         test();
     928     1106904 :         test();
     929     1106904 :         test();
     930     1106904 :         test();
     931     1106904 :         test();
     932     1106904 :         test();
     933     1106904 :         test();
     934     1106904 :         test();
     935     1106904 :         test();
     936     1693368 :         if ( ( ( LT_16( add_o( mean_voi3, corr_shift, &Overflow ), add( 22774 /* 0.695f in Q15 */, mean_voi3_offset ) ) ) && /* normalized correlation low  */
     937             :                /*( LT_16( add_o( st_fx->voicing_fx[2], corr_shift, &Overflow ), 25887 ) ) && */                              /* normalized correlation low on look-ahead - onset detection */
     938      960653 :                ( LT_32( ee[0], 397 /* 6.2f in Q6 */ ) ) && ( GT_32( hp_E[0], E_min_th ) ) &&                                 /* energy concentrated in high frequencies provided that some energy is present in HF...  */
     939      230620 :                ( LT_32( ee[1], 397 /* 6.2f in Q16 */ ) ) && ( GT_32( hp_E[1], E_min_th ) ) &&                                /* ... biased towards look-ahead to detect onsets  */
     940       82717 :                ( tmp_offset_flag == 0 ) &&                                                                                   /* Take care of voiced offsets */
     941             :                /*( st_fx->music_hysteresis_fx == 0 ) &&  */                                                                  /*  ... and in segment after AUDIO frames   */
     942      162186 :                ( LE_32( dE1, 245760 /* 30.0f in Q13 */ ) ) &&                                                                /* Avoid on sharp energy spikes  */
     943       79469 :                ( LE_32( st_fx->old_dE1_fx, 245760 /* 30.0f in Q13 */ ) ) &&                                                  /*   + one frame hysteresis   */
     944       77100 :                ( st_fx->spike_hyst < 0 ) )                                                                                   /* Avoid after sharp energy spikes followed by decay (e.g. castanets) */
     945     1030078 :              || ( flag_low_relE && ( LE_32( st_fx->old_dE1_fx, 245760 /* 30.0f in Q13 */ ) ) ) )                             /* low relative frame energy (only for SC-VBR) */
     946             :         {
     947       77106 :             coder_type = UNVOICED;
     948       77106 :             move16();
     949             :         }
     950             :     }
     951             : 
     952             :     /*-----------------------------------------------------------------*
     953             :      * Decision about VC
     954             :      *-----------------------------------------------------------------*/
     955     1111194 :     if ( st_fx->Opt_SC_VBR )
     956             :     {
     957           0 :         hSC_VBR->set_ppp_generic = 0;
     958             :     }
     959     1111194 :     move16();
     960             : 
     961     1111194 :     test();
     962     1111194 :     test();
     963     1111194 :     IF( EQ_16( st_fx->localVAD, 1 ) && EQ_16( coder_type, GENERIC ) && NE_16( last_core_orig, AMR_WB_CORE ) )
     964             :     {
     965      873679 :         dpit1 = abs_s( sub( T_op_fr[1], T_op_fr[0] ) ); /* Q6 */
     966      873679 :         dpit2 = abs_s( sub( T_op_fr[2], T_op_fr[1] ) ); /* Q6 */
     967      873679 :         dpit3 = abs_s( sub( T_op_fr[3], T_op_fr[2] ) ); /* Q6 */
     968             : 
     969      873679 :         test();
     970      873679 :         test();
     971      873679 :         test();
     972      873679 :         test();
     973      873679 :         test();
     974      873679 :         test();
     975      873679 :         test();
     976      873679 :         test();
     977      873679 :         test();
     978      873679 :         IF( ( GT_16( voicing_fr[0], 19825 /* 0.605 in Q15 */ ) ) && /* normalized correlation high in 1st sf.  */
     979             :             ( GT_16( voicing_fr[1], 19825 /* 0.605 in Q15 */ ) ) && /* normalized correlation high in 2st sf.  */
     980             :             ( GT_16( voicing_fr[2], 19825 /* 0.605 in Q15 */ ) ) && /* normalized correlation high in 3st sf.  */
     981             :             ( GT_16( voicing_fr[3], 19825 /* 0.605 in Q15 */ ) ) && /* normalized correlation high in 4st sf.  */
     982             :             ( GT_32( mean_ee, 256 /* 4.0f in Q6 */ ) ) &&           /* energy concentrated in low frequencies  */
     983             :             ( LT_16( dpit1, 3 << 6 ) ) &&
     984             :             ( LT_16( dpit2, 3 << 6 ) ) &&
     985             :             ( LT_16( dpit3, 3 << 6 ) ) )
     986             :         {
     987      362854 :             coder_type = VOICED;
     988      362854 :             move16();
     989             :         }
     990      510825 :         ELSE IF( st_fx->Opt_SC_VBR && st_fx->input_bwidth == NB && LT_16( vadnoise, 20 << 8 ) )
     991             :         {
     992           0 :             test();
     993           0 :             test();
     994           0 :             test();
     995           0 :             test();
     996           0 :             test();
     997           0 :             test();
     998           0 :             test();
     999           0 :             IF( GT_16( voicing_fr[0], 8192 /* 0.25 in Q15 */ ) &&     /* normalized correlation high in 1st sf.  */
    1000             :                 ( GT_16( voicing_fr[1], 8192 /* 0.25 in Q15 */ ) ) && /* normalized correlation high in 2st sf.  */
    1001             :                 ( GT_16( voicing_fr[2], 8192 /* 0.25 in Q15 */ ) ) && /* normalized correlation high in 3st sf.  */
    1002             :                 ( GT_16( voicing_fr[3], 8192 /* 0.25 in Q15 */ ) ) && /* normalized correlation high in 4st sf.  */
    1003             :                 ( GT_32( mean_ee, 64 /* 1.0f in Q6 */ ) ) &&          /* energy concentrated in low frequencies  */
    1004             :                 ( LT_16( dpit1, 5 << 6 ) ) &&
    1005             :                 ( LT_16( dpit2, 5 << 6 ) ) &&
    1006             :                 ( LT_16( dpit3, 5 << 6 ) ) )
    1007             :             {
    1008           0 :                 hSC_VBR->set_ppp_generic = 1;
    1009           0 :                 move16();
    1010           0 :                 coder_type = VOICED;
    1011           0 :                 move16();
    1012             :             }
    1013             :         }
    1014             : 
    1015             :         /* set VOICED mode for frames with very stable pitch and high correlation
    1016             :            and avoid to switch to AUDIO/MUSIC later                              */
    1017      873679 :         voicing_m = mac_r( L_mac( L_mac( L_mult( voicing_fr[3], 8192 /* 0.25 in Q15 */ ), voicing_fr[2], 8192 /* 0.25 in Q15 */ ), voicing_fr[1], 8192 /* 0.25 in Q15 */ ), voicing_fr[0], 8192 /* 0.25 in Q15 */ ); /* Q15 */
    1018      873679 :         test();
    1019      873679 :         test();
    1020      873679 :         test();
    1021      873679 :         test();
    1022      873679 :         test();
    1023      873679 :         IF( *flag_spitch || ( LE_16( dpit1, 3 << 6 ) && LE_16( dpit2, 3 << 6 ) && LE_16( dpit3, 3 << 6 ) &&
    1024             :                               GT_16( voicing_m, 31130 /* 0.95f in Q15 */ ) && GT_16( st_fx->voicing_sm_fx, 31785 /* 0.97f in Q15 */ ) ) )
    1025             :         {
    1026       37531 :             coder_type = VOICED;
    1027       37531 :             move16();
    1028       37531 :             *flag_spitch = 1;
    1029       37531 :             move16(); /*to avoid switch to AUDIO/MUSIC later*/
    1030             :         }
    1031             :     }
    1032             : 
    1033             :     /*-----------------------------------------------------------------*
    1034             :      * Channel-aware mode - set RF mode and total bitrate
    1035             :      *-----------------------------------------------------------------*/
    1036             : 
    1037     1111194 :     st_fx->rf_mode = st_fx->Opt_RF_ON; /* Q0 */
    1038     1111194 :     move16();
    1039             : 
    1040     1111194 :     IF( EQ_16( coder_type, GENERIC ) )
    1041             :     {
    1042      665994 :         test();
    1043      665994 :         test();
    1044      665994 :         test();
    1045      665994 :         test();
    1046      665994 :         IF( ( LT_16( voicing_fr[0], 6554 /* 0.2f in Q15 */ ) ) && /* normalized correlation high in 2st sf.  */
    1047             :             ( LT_16( voicing_fr[1], 6554 /* 0.2f in Q15 */ ) ) && /* normalized correlation high in 2st sf.  */
    1048             :             ( LT_16( voicing_fr[2], 6554 /* 0.2f in Q15 */ ) ) && /* normalized correlation high in 3rd sf.  */
    1049             :             ( LT_16( voicing_fr[3], 6554 /* 0.2f in Q15 */ ) ) && /* normalized correlation high in 4th sf.  */
    1050             :             ( GT_16( vadnoise, 25 << 8 ) ) )                      /* when speech is clean */
    1051             : 
    1052             :         {
    1053           0 :             st_fx->rf_mode = 0;
    1054           0 :             move16();
    1055             :             /* Current frame cannot be compressed to pack the partial redundancy;*/
    1056             : 
    1057           0 :             IF( NE_16( st_fx->rf_mode, st_fx->Opt_RF_ON ) )
    1058             :             {
    1059           0 :                 core_coder_mode_switch_ivas_fx( st_fx, st_fx->last_total_brate, 0 );
    1060             :             }
    1061             :         }
    1062             :     }
    1063             : 
    1064             :     /*-----------------------------------------------------------------*
    1065             :      * UNCLR classifier
    1066             :      *-----------------------------------------------------------------*/
    1067             : 
    1068     1111194 :     IF( hStereoClassif != NULL )
    1069             :     {
    1070      760851 :         test();
    1071      760851 :         test();
    1072      760851 :         test();
    1073      760851 :         test();
    1074      760851 :         test();
    1075      760851 :         IF( st_fx->element_mode > EVS_MONO && ( EQ_16( coder_type, GENERIC ) || EQ_16( coder_type, UNVOICED ) || coder_type == INACTIVE || st_fx->localVAD == 0 ) && LT_16( hStereoClassif->unclr_sw_enable_cnt[st_fx->idchan], MAX_UV_CNT ) )
    1076             :         {
    1077      483973 :             hStereoClassif->unclr_sw_enable_cnt[st_fx->idchan] = add( hStereoClassif->unclr_sw_enable_cnt[st_fx->idchan], 1 );
    1078      483973 :             move16();
    1079             :         }
    1080             :         ELSE
    1081             :         {
    1082      276878 :             hStereoClassif->unclr_sw_enable_cnt[st_fx->idchan] = 0;
    1083      276878 :             move16();
    1084             :         }
    1085             :     }
    1086             : 
    1087             :     /*-----------------------------------------------------------------*
    1088             :      * Updates
    1089             :      *-----------------------------------------------------------------*/
    1090             : 
    1091             :     /* update spike hysteresis parameters */
    1092     1111194 :     test();
    1093     1111194 :     if ( st_fx->spike_hyst >= 0 && LT_16( st_fx->spike_hyst, 2 ) )
    1094             :     {
    1095         383 :         st_fx->spike_hyst = add( st_fx->spike_hyst, 1 ); /* Q0 */
    1096         383 :         move16();
    1097             :     }
    1098             : 
    1099             :     /* reset spike hysteresis */
    1100     1111194 :     test();
    1101     1111194 :     test();
    1102     1111194 :     test();
    1103     1114409 :     if ( ( GT_16( st_fx->spike_hyst, 1 ) ) &&
    1104        6294 :          ( GT_16( dE3, 5 << 8 ) ||                                                                                                /* energy increases               */
    1105        5195 :            ( GT_16( relE, -3328 /* 13 in Q8 */ ) && ( GT_16( add_sat( mean_voi3, corr_shift ), 22774 /* 0.695 in Q15 */ ) ) ) ) ) /* normalized correlation is high */
    1106             :     {
    1107         234 :         st_fx->spike_hyst = -1;
    1108         234 :         move16();
    1109             :     }
    1110             : 
    1111             :     /* update tilt parameters */
    1112     1111194 :     st_fx->ee_old_fx = ee[1];
    1113     1111194 :     move32(); /*Q6*/
    1114     1111194 :     st_fx->old_dE1_fx = dE1;
    1115     1111194 :     move32(); /*Q13*/
    1116             : 
    1117             :     /* save the raw coder_type for various modules later in the codec (the reason is that e.g. UNVOICED is lost at higher rates) */
    1118     1111194 :     st_fx->coder_type_raw = coder_type; /* Q0 */
    1119     1111194 :     move16();
    1120             : 
    1121     1111194 :     return coder_type;
    1122             : }

Generated by: LCOV version 1.14