LCOV - code coverage report
Current view: top level - lib_enc - dtx_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ fa5c1457a864f30b71f903a41b38fb85641dc2f6 Lines: 693 963 72.0 %
Date: 2025-11-19 04:05:36 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Nov 04, 2021. Version 16.4.0
       3             :   ====================================================================================*/
       4             : 
       5             : #include <stdint.h>
       6             : #include "options.h" /* Compilation switches                   */
       7             : #include "cnst.h"    /* Common constants                       */
       8             : #include "rom_com.h"
       9             : #include <assert.h>
      10             : #include "prot_fx.h"     /* Function prototypes                    */
      11             : #include "prot_fx_enc.h" /* Function prototypes                    */
      12             : 
      13             : 
      14             : /*-------------------------------------------------------------------*
      15             :  * Local constants
      16             :  *-------------------------------------------------------------------*/
      17             : 
      18             : #define ALPHA_ENER_FAST_FX 29491 /* Fast adaptation (noise down, speech up) */
      19             : #define ALPHA_ENER_SLOW_FX 32440 /* Fast adaptation (noise down, speech up) */
      20             : 
      21             : #define MIN_CNT 50 /* Minimum frame number before SID interval adaptation */
      22             : 
      23             : #define SNR_H_FX 13056 /* Estimated SNR and corresponding SID interval        */
      24             : #define SNR_L_FX 9216  /* 51dB corresponds to 25dB SNR before noise supressor */
      25             : #define INT_H    50
      26             : #define INT_L    8
      27             : #define RATIO    ( INT_H - INT_L ) / ( ( SNR_H_FX - SNR_L_FX ) / 256 )
      28             : 
      29             : #define LTE_VAR_FX -1024 /* Q8, -4.0f */
      30             : 
      31             : #define CNG_TYPE_HO 20 /* hangover for switching between CNG types */
      32             : 
      33             : 
      34             : #define MAX_BRATE_DTX_EVS  ACELP_24k40 /* maximum bitrate to which the default DTX is applied in EVS; otherwise DTX is applied only in silence */
      35             : #define MAX_BRATE_DTX_IVAS IVAS_80k    /* maximum bitrate to which the default DTX is applied in IVAS; otherwise DTX is applied only in silence */
      36             : 
      37             : 
      38             : /*-------------------------------------------------------------------*
      39             :  * Local function prototypes
      40             :  *-------------------------------------------------------------------*/
      41             : 
      42             : static void update_SID_cnt_fx( DTX_ENC_HANDLE hDtxEnc, const Word32 core_brate, const Word16 Opt_AMR_WB );
      43             : 
      44             : 
      45             : /*==================================================================================*/
      46             : /* FUNCTION : dtx_ivas_fx()                                                              */
      47             : /*----------------------------------------------------------------------------------*/
      48             : /* PURPOSE :  Discontinuous transmission operation                                  */
      49             : /*----------------------------------------------------------------------------------*/
      50             : /*  INPUT ARGUMENTS :                                                               */
      51             : /* _ (Encoder_State_Fx) st_fx : encoder state structure                             */
      52             : /* _ (Word16)   vad           : vad flag                                       Q0   */
      53             : /* _ (Word16[]) speech_fx     : Pointer to the speech frame               qSpeech   */
      54             : /* _ (Word16)   qSpeech       : speech buffer qformat value                         */
      55             : /* _ (Word16*)  qener         : frame_ener/lt_ener_voiced/lt_ener_noise buf qformat */
      56             : /*----------------------------------------------------------------------------------*/
      57             : /* OUTPUT ARGUMENTS :                                                               */
      58             : /* _ (Word16*)  qener         : frame_ener/lt_ener_voiced/lt_ener_noise buf qformat */
      59             : /* _ (Encoder_State_Fx) st_fx : encoder state structure                             */
      60             : /*----------------------------------------------------------------------------------*/
      61             : 
      62             : /*----------------------------------------------------------------------------------*/
      63             : /* RETURN ARGUMENTS :                                                               */
      64             : /* _ None                                                                           */
      65             : /*==================================================================================*/
      66     1323786 : void dtx_ivas_fx(
      67             :     Encoder_State *st_fx,               /* i/o: encoder state structure                  */
      68             :     const Word32 last_ivas_total_brate, /* i  : last IVAS total bitrate                  Q0*/
      69             :     const Word32 ivas_total_brate,      /* i  : IVAS total bitrate                       Q0*/
      70             :     const Word16 vad,                   /* i  : vad flag for DTX                         Q0*/
      71             :     const Word16 speech[],              /* i  : Pointer to the speech frame              Q_speech*/
      72             :     Word16 Q_speech                     /* i  : Q factor for speech                      */
      73             : )
      74             : {
      75             :     Word16 alpha, i, j, Q_speech2;
      76             :     Word32 L_tmp;
      77     1323786 :     DTX_ENC_HANDLE hDtxEnc = st_fx->hDtxEnc;
      78     1323786 :     TD_CNG_ENC_HANDLE hTdCngEnc = st_fx->hTdCngEnc;
      79             : 
      80             :     Word16 last_br_cng_flag, last_br_flag, br_dtx_flag;
      81             :     Word32 total_brate_ref;
      82             : 
      83     1323786 :     total_brate_ref = st_fx->total_brate;
      84     1323786 :     move32();
      85             : 
      86     1323786 :     IF( st_fx->dtx_sce_sba != 0 )
      87             :     {
      88       11680 :         last_br_cng_flag = 1;
      89       11680 :         last_br_flag = 1;
      90       11680 :         move16();
      91       11680 :         move16();
      92             :     }
      93             :     ELSE
      94             :     {
      95     1312106 :         test();
      96     1312106 :         test();
      97     1312106 :         test();
      98     1312106 :         last_br_cng_flag = LE_32( st_fx->last_total_brate_cng, MAX_BRATE_DTX_EVS ) || LT_32( ( st_fx->lp_noise_32fx ), DTX_THR * 16777216 ) || ( EQ_16( st_fx->element_mode, IVAS_SCE ) && LE_32( st_fx->last_total_brate_cng, MAX_BRATE_DTX_IVAS ) );
      99             : 
     100     1312106 :         test();
     101     1312106 :         test();
     102     1312106 :         test();
     103             : 
     104     1312106 :         last_br_flag = ( st_fx->element_mode == EVS_MONO && LE_32( st_fx->last_total_brate, MAX_BRATE_DTX_EVS ) ) ||
     105     2125967 :                        ( st_fx->element_mode != EVS_MONO && LE_32( last_ivas_total_brate, MAX_BRATE_DTX_IVAS ) ) ||
     106      813861 :                        LT_32( ( st_fx->lp_noise_32fx ), DTX_THR * 16777216 );
     107             :     }
     108             : 
     109             :     /* Initialization */
     110     1323786 :     IF( st_fx->ini_frame == 0 )
     111             :     {
     112       14686 :         st_fx->active_cnt = CNG_TYPE_HO;
     113       14686 :         move16();
     114             : 
     115       14686 :         st_fx->cng_type = FD_CNG;
     116       14686 :         move16();
     117       14686 :         test();
     118       14686 :         test();
     119       14686 :         test();
     120       14686 :         if ( ( EQ_16( st_fx->codec_mode, MODE1 ) || st_fx->Opt_AMR_WB ) && NE_16( st_fx->element_mode, IVAS_SCE ) && NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) )
     121             :         {
     122         144 :             st_fx->cng_type = LP_CNG;
     123         144 :             move16();
     124             :         }
     125             :     }
     126     1323786 :     test();
     127     1323786 :     test();
     128     1323786 :     test();
     129     1323786 :     test();
     130     1323786 :     test();
     131     1323786 :     IF( st_fx->Opt_DTX_ON && vad == 0 &&
     132             :         GT_16( st_fx->ini_frame, 2 ) && /* CNG coding starts after 2 frames */
     133             :         st_fx->fd_cng_reset_flag == 0 &&
     134             :         NE_16( st_fx->last_core, AMR_WB_CORE ) &&
     135             :         st_fx->Opt_AMR_WB == 0 )
     136             :     {
     137       38924 :         test();
     138       38924 :         test();
     139       38924 :         test();
     140       38924 :         IF( GT_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate_cng, -1 ) &&
     141             :             NE_32( st_fx->last_total_brate_cng, st_fx->total_brate ) && last_br_cng_flag )
     142             :         {
     143        4432 :             st_fx->total_brate = st_fx->last_total_brate_cng;
     144        4432 :             move32();
     145        4432 :             test();
     146        4432 :             if ( !( EQ_32( st_fx->total_brate, ACELP_7k20 ) && st_fx->Opt_SC_VBR ) )
     147             :             {
     148        4432 :                 st_fx->Opt_SC_VBR = 0;
     149        4432 :                 move16();
     150             :             }
     151        4432 :             st_fx->rf_mode = st_fx->last_rf_mode_cng;
     152        4432 :             move16();
     153        4432 :             st_fx->bwidth = st_fx->last_bwidth_cng;
     154        4432 :             move16();
     155        4432 :             st_fx->codec_mode = st_fx->last_codec_mode_cng;
     156        4432 :             move16();
     157             :         }
     158       38924 :         test();
     159       38924 :         test();
     160       38924 :         IF( LE_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate, st_fx->total_brate ) && last_br_flag )
     161             : 
     162             :         {
     163        3985 :             st_fx->total_brate = st_fx->last_total_brate;
     164        3985 :             move32();
     165        3985 :             test();
     166        3985 :             if ( !( EQ_32( st_fx->total_brate, ACELP_7k20 ) && st_fx->Opt_SC_VBR ) )
     167             :             {
     168        3985 :                 st_fx->Opt_SC_VBR = 0;
     169        3985 :                 move16();
     170             :             }
     171             : 
     172        3985 :             st_fx->Opt_RF_ON = 0;
     173        3985 :             move16();
     174        3985 :             test();
     175        3985 :             test();
     176        3985 :             test();
     177        3985 :             if ( st_fx->rf_mode && st_fx->rf_fec_offset > 0 && EQ_32( st_fx->total_brate, ACELP_13k20 ) && ( st_fx->bwidth != NB ) )
     178             :             {
     179           0 :                 st_fx->Opt_RF_ON = 1;
     180           0 :                 move16();
     181             :             }
     182        3985 :             st_fx->rf_mode = st_fx->Opt_RF_ON;
     183        3985 :             move16();
     184        3985 :             st_fx->bwidth = st_fx->last_bwidth;
     185        3985 :             move32();
     186        3985 :             IF( st_fx->element_mode > EVS_MONO )
     187             :             {
     188        3985 :                 st_fx->codec_mode = MODE1;
     189        3985 :                 move16();
     190             :             }
     191             :             ELSE
     192             :             {
     193           0 :                 st_fx->codec_mode = get_codec_mode( st_fx->total_brate );
     194           0 :                 move16();
     195             :             }
     196             :         }
     197             :     }
     198             : 
     199             :     /*------------------------------------------------------------------------*
     200             :      * Select SID or FRAME_NO_DATA frame if DTX is enabled
     201             :      *------------------------------------------------------------------------*/
     202             : 
     203     1323786 :     br_dtx_flag = 1;
     204     1323786 :     move16();
     205             : 
     206     1323786 :     IF( st_fx->dtx_sce_sba == 0 )
     207             :     {
     208     1312106 :         test();
     209     1312106 :         test();
     210     1312106 :         test();
     211     1312106 :         test();
     212     1312106 :         br_dtx_flag = ( ( st_fx->element_mode == EVS_MONO ) && LE_32( st_fx->total_brate, MAX_BRATE_DTX_EVS ) ) ||
     213     2136418 :                       ( ( st_fx->element_mode != EVS_MONO ) && LE_32( ivas_total_brate, MAX_BRATE_DTX_IVAS ) ) ||
     214      824312 :                       LT_16( extract_h( st_fx->lp_noise_32fx ), DTX_THR * 256 );
     215             :     }
     216     1323786 :     test();
     217     1323786 :     test();
     218     1323786 :     test();
     219     1323786 :     test();
     220     1323786 :     IF( st_fx->Opt_DTX_ON && vad == 0 &&
     221             :         GT_16( st_fx->ini_frame, 2 ) && /* CNG coding starts after 2 frames */
     222             :         br_dtx_flag &&
     223             :         st_fx->fd_cng_reset_flag == 0 )
     224             :     {
     225             :         /* reset counter */
     226       35169 :         st_fx->active_cnt = 0;
     227       35169 :         move16();
     228             : 
     229       35169 :         IF( st_fx->Opt_AMR_WB )
     230             :         {
     231           0 :             st_fx->last_total_brate_cng = -1;
     232           0 :             move16();
     233             :         }
     234             :         ELSE
     235             :         {
     236       35169 :             st_fx->last_total_brate_cng = st_fx->total_brate;
     237       35169 :             move16();
     238       35169 :             st_fx->last_bwidth_cng = st_fx->bwidth;
     239       35169 :             move16();
     240       35169 :             st_fx->last_codec_mode_cng = st_fx->codec_mode;
     241       35169 :             move16();
     242       35169 :             st_fx->last_rf_mode_cng = st_fx->rf_mode;
     243       35169 :             move16();
     244             :         }
     245             : 
     246       35169 :         IF( hDtxEnc->cnt_SID == 0 )
     247             :         {
     248             :             /* this will be a SID frame */
     249        6278 :             IF( st_fx->Opt_AMR_WB )
     250             :             {
     251           0 :                 st_fx->core_brate = SID_1k75;
     252           0 :                 move32();
     253             :             }
     254             :             ELSE
     255             :             {
     256        6278 :                 st_fx->core_brate = SID_2k40;
     257        6278 :                 move32();
     258             :             }
     259             :         }
     260             :         ELSE
     261             :         {
     262             :             /* this will be a no data frame */
     263       28891 :             st_fx->core_brate = FRAME_NO_DATA;
     264       28891 :             move32();
     265             :         }
     266             : 
     267       35169 :         test();
     268       35169 :         test();
     269       35169 :         IF( ( st_fx->core_brate == FRAME_NO_DATA ) && ( st_fx->last_core != ACELP_CORE ) && !st_fx->Opt_AMR_WB )
     270             :         {
     271             :             /* force SID frame when switching from HQ core or AMR-WB IO mode into inactive frame in ACELP core when DTX is on */
     272        2468 :             st_fx->core_brate = SID_2k40;
     273        2468 :             move32();
     274             :         }
     275       35169 :         test();
     276       35169 :         test();
     277       35169 :         IF( ( ( st_fx->last_core != ACELP_CORE ) || EQ_16( st_fx->cng_type, FD_CNG ) ) && EQ_16( st_fx->dtx_sce_sba, 1 ) )
     278             :         {
     279        1820 :             st_fx->cng_type = FD_CNG;
     280        1820 :             move16();
     281        1820 :             test();
     282        1820 :             test();
     283        1820 :             test();
     284        1820 :             test();
     285        1820 :             test();
     286        1820 :             test();
     287        1820 :             IF( ( st_fx->element_mode == EVS_MONO ) && ( EQ_32( st_fx->total_brate, ACELP_9k60 ) || EQ_32( st_fx->total_brate, ACELP_16k40 ) || EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_48k ) || EQ_32( st_fx->total_brate, HQ_96k ) || EQ_32( st_fx->total_brate, HQ_128k ) ) )
     288             :             {
     289           0 :                 st_fx->codec_mode = MODE2;
     290           0 :                 move16();
     291             :             }
     292             :         }
     293             :         ELSE
     294             :         {
     295       33349 :             test();
     296       33349 :             test();
     297       33349 :             test();
     298       33349 :             test();
     299       33349 :             IF( ( EQ_16( st_fx->cng_type, FD_CNG ) && ( LE_32( st_fx->total_brate, MAX_BRATE_DTX_EVS ) || ( ( st_fx->element_mode != EVS_MONO ) && LE_32( ivas_total_brate, MAX_BRATE_DTX_IVAS ) ) ) ) || ( EQ_16( st_fx->element_mode, IVAS_CPE_MDCT ) ) ) /* at highest bitrates, use exclusively LP_CNG */
     300             :             {
     301       30341 :                 test();
     302       30341 :                 test();
     303       30341 :                 test();
     304       30341 :                 IF( ( st_fx->element_mode == EVS_MONO ) && ( EQ_32( st_fx->total_brate, ACELP_9k60 ) || EQ_32( st_fx->total_brate, ACELP_16k40 ) || EQ_32( st_fx->total_brate, ACELP_24k40 ) ) )
     305             :                 {
     306           0 :                     st_fx->codec_mode = MODE2;
     307           0 :                     move16();
     308             :                 }
     309             :             }
     310             :             ELSE
     311             :             {
     312        3008 :                 st_fx->cng_type = LP_CNG;
     313        3008 :                 move16();
     314        3008 :                 if ( EQ_16( st_fx->codec_mode, MODE2 ) )
     315             :                 {
     316           0 :                     hTdCngEnc->lp_cng_mode2 = 1;
     317           0 :                     move16();
     318             :                 }
     319        3008 :                 st_fx->codec_mode = MODE1;
     320        3008 :                 move16();
     321             :             }
     322             :         }
     323             :         /* reset the bitstream (IVAS format signalling was already written) */
     324       35169 :         test();
     325       35169 :         IF( NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) && st_fx->hBstr != NULL )
     326             :         {
     327             : 
     328       22886 :             reset_indices_enc_fx( st_fx->hBstr, st_fx->hBstr->nb_ind_tot );
     329             :         }
     330             :     }
     331     1288617 :     ELSE IF( st_fx->element_mode != EVS_MONO )
     332             :     {
     333     1288617 :         st_fx->total_brate = total_brate_ref;
     334     1288617 :         move32();
     335             :     }
     336             : 
     337             :     /*------------------------------------------------------------------------*
     338             :      * Reset counters when in active frame (not in SID or FRAME_NO_DATA frame)
     339             :      *------------------------------------------------------------------------*/
     340             :     /* NB core bit rate can be "-1"  at startup , so one can not use   core_brate_fx <=2400 */
     341     1323786 :     test();
     342     1323786 :     test();
     343     1323786 :     IF( ( NE_32( st_fx->core_brate, SID_2k40 ) ) && ( NE_32( st_fx->core_brate, SID_1k75 ) ) && ( st_fx->core_brate != FRAME_NO_DATA ) )
     344             :     {
     345     1288617 :         IF( hDtxEnc != NULL )
     346             :         {
     347      116223 :             hDtxEnc->cnt_SID = 0;
     348      116223 :             move16();
     349             : 
     350             :             /* change SID update rate */
     351             :             /* first SID update is only 8 (3) frames after the active speech end */
     352      116223 :             IF( !st_fx->Opt_AMR_WB )
     353             :             {
     354      116223 :                 hDtxEnc->max_SID = FIXED_SID_RATE;
     355      116223 :                 move16();
     356             :             }
     357             :             ELSE
     358             :             {
     359           0 :                 hDtxEnc->max_SID = 3;
     360           0 :                 move16(); /* first SID update is only 3 frames after the active speech end */
     361             :             }
     362             : 
     363      116223 :             if ( LT_16( hDtxEnc->interval_SID, hDtxEnc->max_SID ) )
     364             :             {
     365           0 :                 hDtxEnc->max_SID = hDtxEnc->interval_SID;
     366           0 :                 move16(); /* change SID update rate */
     367             :             }
     368             : 
     369      116223 :             hDtxEnc->cng_cnt = 0;
     370      116223 :             move16(); /* reset the counter of CNG frames for averaging */
     371             :         }
     372     1288617 :         test();
     373     1288617 :         test();
     374     1288617 :         IF( GE_16( st_fx->active_cnt, CNG_TYPE_HO ) && st_fx->Opt_AMR_WB == 0 && NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) )
     375             :         {
     376      495745 :             IF( EQ_16( st_fx->element_mode, IVAS_SCE ) )
     377             :             {
     378             :                 Word32 lp_thresh, fd_thresh;
     379      416557 :                 test();
     380      416557 :                 IF( st_fx->Opt_DTX_ON && EQ_16( st_fx->dtx_sce_sba, 1 ) )
     381             :                 {
     382        9660 :                     lp_thresh = 327680; // 5 in Q16
     383        9660 :                     move32();
     384        9660 :                     fd_thresh = 131072; // 2 in Q16
     385        9660 :                     move32();
     386             :                 }
     387             :                 ELSE
     388             :                 {
     389      406897 :                     lp_thresh = 655360; // 10 in Q16
     390      406897 :                     move32();
     391      406897 :                     fd_thresh = 327680; // 5 in Q16
     392      406897 :                     move32();
     393             :                 }
     394             : 
     395             :                 /*More conservative selection of LP-CNG for SCE*/
     396      416557 :                 test();
     397      416557 :                 test();
     398      416557 :                 test();
     399      416557 :                 IF( st_fx->cng_type == LP_CNG && ( GT_32( st_fx->bckr_tilt_lt, lp_thresh ) ) )
     400             :                 {
     401           0 :                     st_fx->cng_type = FD_CNG;
     402           0 :                     move16();
     403             :                 }
     404      416557 :                 ELSE IF( EQ_16( st_fx->cng_type, FD_CNG ) && ( LT_32( st_fx->bckr_tilt_lt, fd_thresh ) ) && ( GT_32( st_fx->lp_noise_32fx, 67108864 /* 2 in Q24 */ ) ) )
     405             :                 {
     406          70 :                     st_fx->cng_type = LP_CNG;
     407          70 :                     move16();
     408             :                 }
     409             :             }
     410             :             ELSE
     411             :             {
     412       79188 :                 test();
     413       79188 :                 test();
     414       79188 :                 test();
     415       79188 :                 test();
     416       79188 :                 test();
     417       79188 :                 test();
     418       79188 :                 test();
     419       79188 :                 test();
     420       79188 :                 IF( ( st_fx->cng_type == LP_CNG ) && ( ( ( st_fx->input_bwidth == NB ) && GT_32( st_fx->bckr_tilt_lt, 589824 /*9.f Q16*/ ) ) || ( ( st_fx->input_bwidth > NB ) && GT_32( st_fx->bckr_tilt_lt, 2949120l /*45.f Q16*/ ) ) ) )
     421             :                 {
     422          53 :                     st_fx->cng_type = FD_CNG;
     423          53 :                     move16();
     424             :                 }
     425       79135 :                 ELSE IF( EQ_16( st_fx->cng_type, FD_CNG ) && ( ( ( st_fx->input_bwidth == NB ) && LT_32( st_fx->bckr_tilt_lt, 131072l /*2.f Q16*/ ) ) || ( ( st_fx->input_bwidth > NB ) && LT_32( st_fx->bckr_tilt_lt, 655360l /*10.f Q16*/ ) ) ) )
     426             :                 {
     427          59 :                     st_fx->cng_type = LP_CNG;
     428          59 :                     move16();
     429             :                 }
     430             :             }
     431      495745 :             st_fx->last_total_brate_cng = -1;
     432      495745 :             move16();
     433             :         }
     434      792872 :         ELSE IF( st_fx->Opt_AMR_WB )
     435             :         {
     436           0 :             st_fx->cng_type = LP_CNG;
     437           0 :             move16();
     438             :         }
     439     1288617 :         st_fx->active_cnt = add( st_fx->active_cnt, 1 );
     440     1288617 :         move16();
     441     1288617 :         st_fx->active_cnt = s_min( st_fx->active_cnt, 200 );
     442     1288617 :         move16();
     443             :     }
     444             : 
     445             :     /*------------------------------------------------------------------------*
     446             :      * Update speech and background noise long-term energy
     447             :      *------------------------------------------------------------------------*/
     448     1323786 :     IF( hDtxEnc != NULL )
     449             :     {
     450      151392 :         hDtxEnc->frame_ener_fx = L_deposit_l( 0 );
     451      151392 :         move32();
     452             : 
     453      151392 :         IF( st_fx->Opt_DTX_ON )
     454             :         {
     455      151392 :             Q_speech2 = add( shl( Q_speech, 1 ), 7 );
     456     2573664 :             FOR( j = 0; j < 16; j++ )
     457             :             {
     458     2422272 :                 L_tmp = L_mult0( *speech, *speech ); /*2*Q_speech*/
     459     2422272 :                 speech++;
     460    38756352 :                 FOR( i = 1; i < L_FRAME / 16; i++ )
     461             :                 {
     462    36334080 :                     L_tmp = L_mac0_sat( L_tmp, *speech, *speech ); /*2*Q_speech*/
     463    36334080 :                     speech++;
     464             :                 }
     465     2422272 :                 hDtxEnc->frame_ener_fx = L_add( hDtxEnc->frame_ener_fx, L_shr( L_tmp, Q_speech2 ) ); /* Q(-7) */
     466     2422272 :                 move32();
     467             :             }
     468             : 
     469             :             /* Active speech (voiced) */
     470             : 
     471      151392 :             IF( EQ_16( st_fx->clas, VOICED_CLAS ) )
     472             :             {
     473       25154 :                 alpha = ALPHA_ENER_SLOW_FX; /*Q15 */
     474       25154 :                 move16();
     475       25154 :                 if ( GT_32( hDtxEnc->frame_ener_fx, hDtxEnc->lt_ener_voiced_fx ) )
     476             :                 {
     477        8390 :                     alpha = ALPHA_ENER_FAST_FX; /*Q15 */
     478        8390 :                     move16();
     479             :                 }
     480             : 
     481             :                 /*st_fx->lt_ener_voiced_fx = alpha * st_fx->lt_ener_voiced_fx + (1.0f-alpha) * st_fx->frame_ener_fx;*/
     482       25154 :                 L_tmp = L_sub( hDtxEnc->lt_ener_voiced_fx, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
     483       25154 :                 L_tmp = Mult_32_16( L_tmp, alpha );                                  /*Q(-7)*/
     484       25154 :                 hDtxEnc->lt_ener_voiced_fx = L_add( L_tmp, hDtxEnc->frame_ener_fx ); /* Q(-7) */
     485       25154 :                 move32();
     486             : 
     487       25154 :                 hDtxEnc->VarDTX_cnt_voiced = add( hDtxEnc->VarDTX_cnt_voiced, 1 );
     488       25154 :                 move16();
     489             : 
     490       25154 :                 hDtxEnc->VarDTX_cnt_voiced = s_min( hDtxEnc->VarDTX_cnt_voiced, MIN_CNT );
     491       25154 :                 move16();
     492             :             }
     493             :             /* Background noise */
     494      126238 :             ELSE IF( !st_fx->Opt_AMR_WB )
     495             :             {
     496      126238 :                 alpha = ALPHA_ENER_SLOW_FX; /*Q15*/
     497      126238 :                 move16();
     498      126238 :                 if ( LT_32( hDtxEnc->frame_ener_fx, hDtxEnc->lt_ener_noise_fx ) )
     499             :                 {
     500       34506 :                     alpha = ALPHA_ENER_FAST_FX; /*Q15*/
     501       34506 :                     move16();
     502             :                 }
     503             : 
     504             :                 /*st_fx->lt_ener_noise_fx = alpha * st_fx->lt_ener_noise_fx + (1.0f-alpha) * st_fx->frame_ener_fx;*/
     505      126238 :                 L_tmp = L_sub( hDtxEnc->lt_ener_noise_fx, hDtxEnc->frame_ener_fx ); /* Q(-7) */
     506      126238 :                 L_tmp = Mult_32_16( L_tmp, alpha );                                 /* Q(-7) */
     507      126238 :                 hDtxEnc->lt_ener_noise_fx = L_add( L_tmp, hDtxEnc->frame_ener_fx ); /* Q(-7) */
     508      126238 :                 move32();
     509             : 
     510      126238 :                 hDtxEnc->VarDTX_cnt_noise = add( hDtxEnc->VarDTX_cnt_noise, 1 );
     511      126238 :                 move16();
     512             : 
     513      126238 :                 hDtxEnc->VarDTX_cnt_noise = s_min( hDtxEnc->VarDTX_cnt_noise, MIN_CNT );
     514      126238 :                 move16();
     515             :             }
     516             :         }
     517             :     }
     518             : 
     519             :     /* Update of the SID counter */
     520     1323786 :     update_SID_cnt_fx( hDtxEnc, st_fx->core_brate, st_fx->Opt_AMR_WB );
     521             : 
     522             :     /* Update encoded bandwidth */
     523     1323786 :     test();
     524     1323786 :     test();
     525     1323786 :     IF( st_fx->Opt_DTX_ON && ( EQ_32( st_fx->core_brate, SID_2k40 ) || ( st_fx->core_brate == FRAME_NO_DATA ) ) )
     526             :     {
     527             : 
     528       35169 :         st_fx->bwidth = st_fx->last_bwidth;
     529       35169 :         move16();
     530       35169 :         test();
     531       35169 :         IF( GT_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate_cng, -1 ) )
     532             :         {
     533       10170 :             st_fx->bwidth = st_fx->last_bwidth_cng;
     534       10170 :             move16();
     535             :         }
     536             : 
     537       35169 :         test();
     538       35169 :         test();
     539       35169 :         IF( st_fx->Opt_RF_ON && ( EQ_32( st_fx->total_brate, ACELP_13k20 ) ) && ( ( st_fx->bwidth == NB ) ) )
     540             :         {
     541           0 :             st_fx->codec_mode = MODE1;
     542           0 :             move16();
     543           0 :             reset_rf_indices_fx( st_fx );
     544           0 :             st_fx->Opt_RF_ON = 0;
     545           0 :             move16();
     546           0 :             st_fx->rf_mode = 0;
     547           0 :             move16();
     548             :         }
     549             : 
     550       35169 :         test();
     551       35169 :         IF( st_fx->Opt_RF_ON && NE_32( st_fx->total_brate, ACELP_13k20 ) )
     552             :         {
     553           0 :             reset_rf_indices_fx( st_fx );
     554           0 :             move16();
     555           0 :             st_fx->Opt_RF_ON = 0;
     556           0 :             move16();
     557           0 :             st_fx->rf_mode = 0;
     558             :         }
     559             : 
     560             :         /* Set and limit the encoded bandwidth */
     561       35169 :         IF( EQ_16( st_fx->codec_mode, MODE2 ) )
     562             :         {
     563             :             Word16 n, bits_frame_nominal;
     564             : 
     565             :             UWord16 lsb;
     566             :             Word16 tmpbandwidthMin;
     567             : 
     568           0 :             Mpy_32_16_ss( st_fx->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
     569           0 :             bits_frame_nominal = extract_l( L_shr( L_tmp, 3 ) );    /* Q0 */
     570             : 
     571           0 :             FOR( n = 0; n < FRAME_SIZE_NB; n++ )
     572             :             {
     573           0 :                 IF( EQ_16( FrameSizeConfig[n].frame_bits, bits_frame_nominal ) )
     574             :                 {
     575           0 :                     BREAK;
     576             :                 }
     577             :             }
     578           0 :             IF( EQ_16( n, FRAME_SIZE_NB ) )
     579             :             {
     580           0 :                 assert( !"Bitrate not supported: not part of EVS" );
     581             :             }
     582           0 :             tmpbandwidthMin = FrameSizeConfig[n].bandwidth_min;
     583           0 :             move16();
     584           0 :             if ( EQ_16( st_fx->rf_mode, 1 ) )
     585             :             {
     586           0 :                 tmpbandwidthMin = WB;
     587           0 :                 move16();
     588             :             }
     589           0 :             st_fx->bwidth = s_max( s_min( st_fx->bwidth, FrameSizeConfig[n].bandwidth_max ), tmpbandwidthMin );
     590           0 :             move16();
     591             :         }
     592             :     }
     593             : 
     594     1323786 :     return;
     595             : }
     596             : 
     597             : /*==================================================================================*/
     598             : /* FUNCTION : dtx_fx()                                                              */
     599             : /*----------------------------------------------------------------------------------*/
     600             : /* PURPOSE :  Discontinuous transmission operation                                  */
     601             : /*----------------------------------------------------------------------------------*/
     602             : /*  INPUT ARGUMENTS :                                                               */
     603             : /* _ (Encoder_State_Fx) st_fx : encoder state structure                             */
     604             : /* _ (Word16)   vad           : vad flag                                       Q0   */
     605             : /* _ (Word16[]) speech_fx     : Pointer to the speech frame               qSpeech   */
     606             : /* _ (Word16)   qSpeech       : speech buffer qformat value                         */
     607             : /* _ (Word16*)  qener         : frame_ener/lt_ener_voiced/lt_ener_noise buf qformat */
     608             : /*----------------------------------------------------------------------------------*/
     609             : /* OUTPUT ARGUMENTS :                                                               */
     610             : /* _ (Word16*)  qener         : frame_ener/lt_ener_voiced/lt_ener_noise buf qformat */
     611             : /* _ (Encoder_State_Fx) st_fx : encoder state structure                             */
     612             : /*----------------------------------------------------------------------------------*/
     613             : 
     614             : /*----------------------------------------------------------------------------------*/
     615             : /* RETURN ARGUMENTS :                                                               */
     616             : /* _ None                                                                           */
     617             : /*==================================================================================*/
     618        5150 : void dtx_fx(
     619             :     Encoder_State *st_fx,  /* i/o: encoder state structure                  */
     620             :     const Word16 vad,      /* i  : vad flag for DTX                         Q0*/
     621             :     const Word16 speech[], /* i  : Pointer to the speech frame              Q_speech*/
     622             :     Word16 Q_speech        /* i  : Q factor for speech                      */
     623             : 
     624             : )
     625             : {
     626             :     Word16 alpha, j, i, Q_speech2;
     627             :     Word32 L_tmp;
     628        5150 :     DTX_ENC_HANDLE hDtxEnc = st_fx->hDtxEnc;
     629        5150 :     TD_CNG_ENC_HANDLE hTdCngEnc = st_fx->hTdCngEnc;
     630             : 
     631             :     Word16 last_br_cng_flag, last_br_flag, br_dtx_flag;
     632             : 
     633        5150 :     IF( st_fx->dtx_sce_sba != 0 )
     634             :     {
     635           0 :         last_br_cng_flag = 1;
     636           0 :         last_br_flag = 1;
     637           0 :         br_dtx_flag = 1;
     638           0 :         move16();
     639           0 :         move16();
     640           0 :         move16();
     641             :     }
     642             :     ELSE
     643             :     {
     644        5150 :         last_br_cng_flag = LE_32( st_fx->last_total_brate_cng, ACELP_24k40 ) || LT_16( st_fx->lp_noise_fx, DTX_THR * 256 ) || ( EQ_16( st_fx->element_mode, IVAS_SCE ) && LE_32( st_fx->last_total_brate_cng, ACELP_32k ) );
     645             : 
     646        5150 :         last_br_flag = LE_32( st_fx->last_total_brate, ACELP_24k40 ) || LT_16( st_fx->lp_noise_fx, DTX_THR * 256 ) || ( EQ_16( st_fx->element_mode, IVAS_SCE ) && LE_32( st_fx->last_total_brate, ACELP_32k ) );
     647        5150 :         br_dtx_flag = 0;
     648        5150 :         move16();
     649             :     }
     650             :     /* Initialization */
     651        5150 :     IF( st_fx->ini_frame == 0 )
     652             :     {
     653           5 :         st_fx->active_cnt = CNG_TYPE_HO;
     654           5 :         move16();
     655             : 
     656           5 :         st_fx->cng_type = FD_CNG;
     657           5 :         move16();
     658           5 :         test();
     659           5 :         IF( ( EQ_16( st_fx->codec_mode, MODE1 ) || st_fx->Opt_AMR_WB ) && EQ_16( st_fx->element_mode, IVAS_SCE ) && EQ_16( st_fx->element_mode, IVAS_CPE_MDCT ) )
     660             :         {
     661           0 :             st_fx->cng_type = LP_CNG;
     662           0 :             move16();
     663             :         }
     664             :     }
     665        5150 :     test();
     666        5150 :     test();
     667        5150 :     IF( st_fx->Opt_DTX_ON && vad == 0 &&
     668             :         GT_16( st_fx->ini_frame, 2 ) && /* CNG coding starts after 2 frames */
     669             :         st_fx->fd_cng_reset_flag == 0 &&
     670             :         NE_16( st_fx->last_core, AMR_WB_CORE ) &&
     671             :         st_fx->Opt_AMR_WB == 0 )
     672             :     {
     673           0 :         test();
     674           0 :         test();
     675           0 :         test();
     676           0 :         IF( GT_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate_cng, -1 ) &&
     677             :             NE_32( st_fx->last_total_brate_cng, st_fx->total_brate ) && last_br_cng_flag )
     678             :         {
     679           0 :             st_fx->total_brate = st_fx->last_total_brate_cng;
     680           0 :             move32();
     681           0 :             test();
     682           0 :             IF( !( EQ_32( st_fx->total_brate, ACELP_7k20 ) && st_fx->Opt_SC_VBR ) )
     683             :             {
     684           0 :                 st_fx->Opt_SC_VBR = 0;
     685           0 :                 move16();
     686             :             }
     687           0 :             st_fx->rf_mode = st_fx->last_rf_mode_cng;
     688           0 :             move16();
     689           0 :             st_fx->bwidth = st_fx->last_bwidth_cng;
     690           0 :             move16();
     691           0 :             st_fx->codec_mode = st_fx->last_codec_mode_cng;
     692           0 :             move16();
     693             :         }
     694           0 :         test();
     695           0 :         test();
     696           0 :         IF( LE_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate, st_fx->total_brate ) && last_br_flag )
     697             : 
     698             :         {
     699           0 :             st_fx->total_brate = st_fx->last_total_brate;
     700           0 :             move32();
     701           0 :             test();
     702           0 :             IF( !( EQ_32( st_fx->total_brate, ACELP_7k20 ) && st_fx->Opt_SC_VBR ) )
     703             :             {
     704           0 :                 st_fx->Opt_SC_VBR = 0;
     705           0 :                 move16();
     706             :             }
     707             : 
     708           0 :             st_fx->Opt_RF_ON = 0;
     709           0 :             move16();
     710           0 :             test();
     711           0 :             test();
     712           0 :             test();
     713           0 :             IF( st_fx->rf_mode && st_fx->rf_fec_offset > 0 && EQ_32( st_fx->total_brate, ACELP_13k20 ) && NE_16( st_fx->bwidth, NB ) )
     714             :             {
     715           0 :                 st_fx->Opt_RF_ON = 1;
     716           0 :                 move16();
     717             :             }
     718           0 :             st_fx->rf_mode = st_fx->Opt_RF_ON;
     719           0 :             move16();
     720           0 :             st_fx->bwidth = st_fx->last_bwidth;
     721           0 :             move32();
     722           0 :             IF( ( st_fx->element_mode > EVS_MONO ) )
     723             :             {
     724           0 :                 st_fx->codec_mode = MODE1;
     725           0 :                 move16();
     726             :             }
     727             :             ELSE
     728             :             {
     729           0 :                 st_fx->codec_mode = get_codec_mode( st_fx->total_brate );
     730             :             }
     731             :         }
     732             :     }
     733             : 
     734             :     /*------------------------------------------------------------------------*
     735             :      * Select SID or FRAME_NO_DATA frame if DTX is enabled
     736             :      *------------------------------------------------------------------------*/
     737             : 
     738        5150 :     if ( st_fx->dtx_sce_sba == 0 )
     739             :     {
     740       11300 :         br_dtx_flag = LE_32( st_fx->total_brate, ACELP_24k40 ) || LT_16( st_fx->lp_noise_fx, DTX_THR * 256 ) || ( EQ_16( st_fx->element_mode, IVAS_SCE ) && LE_32( st_fx->total_brate, ACELP_32k ) ) ||
     741        6150 :                       EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) || ( EQ_16( st_fx->element_mode, IVAS_CPE_MDCT ) && ( LE_32( st_fx->element_brate, IVAS_64k ) || LT_16( st_fx->lp_noise_fx, DTX_THR * 256 ) ) );
     742             :     }
     743        5150 :     test();
     744        5150 :     test();
     745        5150 :     test();
     746        5150 :     test();
     747        5150 :     IF( st_fx->Opt_DTX_ON && vad == 0 &&
     748             :         GT_16( st_fx->ini_frame, 2 ) && /* CNG coding starts after 2 frames */
     749             :         br_dtx_flag &&
     750             :         st_fx->fd_cng_reset_flag == 0 )
     751             :     {
     752             :         /* reset counter */
     753           0 :         st_fx->active_cnt = 0;
     754           0 :         move16();
     755             : 
     756           0 :         IF( st_fx->Opt_AMR_WB )
     757             :         {
     758           0 :             st_fx->last_total_brate_cng = -1;
     759           0 :             st_fx->last_rf_mode_cng = st_fx->rf_mode;
     760           0 :             move32();
     761           0 :             move16();
     762             :         }
     763             :         ELSE
     764             :         {
     765           0 :             st_fx->last_total_brate_cng = st_fx->total_brate;
     766           0 :             st_fx->last_bwidth_cng = st_fx->bwidth;
     767           0 :             st_fx->last_codec_mode_cng = st_fx->codec_mode;
     768           0 :             move32();
     769           0 :             move16();
     770           0 :             move16();
     771             :         }
     772             : 
     773           0 :         IF( hDtxEnc->cnt_SID == 0 )
     774             :         {
     775             :             /* this will be a SID frame */
     776           0 :             IF( st_fx->Opt_AMR_WB )
     777             :             {
     778           0 :                 st_fx->core_brate = SID_1k75;
     779           0 :                 move32();
     780             :             }
     781             :             ELSE
     782             :             {
     783           0 :                 st_fx->core_brate = SID_2k40;
     784           0 :                 move32();
     785             :             }
     786             :         }
     787             :         ELSE
     788             :         {
     789             :             /* this will be a no data frame */
     790           0 :             st_fx->core_brate = FRAME_NO_DATA;
     791           0 :             move32();
     792             :         }
     793             : 
     794           0 :         test();
     795           0 :         test();
     796           0 :         IF( ( st_fx->core_brate == FRAME_NO_DATA ) && ( st_fx->last_core != ACELP_CORE ) && !st_fx->Opt_AMR_WB )
     797             :         {
     798             :             /* force SID frame when switching from HQ core or AMR-WB IO mode into inactive frame in ACELP core when DTX is on */
     799           0 :             st_fx->core_brate = SID_2k40;
     800           0 :             move32();
     801             :         }
     802             : 
     803             :         {
     804           0 :             test();
     805           0 :             test();
     806           0 :             test();
     807           0 :             test();
     808           0 :             IF( ( EQ_16( st_fx->cng_type, FD_CNG ) && ( LE_32( st_fx->total_brate, ACELP_24k40 ) || ( ( st_fx->element_mode != EVS_MONO ) && LE_32( st_fx->total_brate, ACELP_32k ) ) ) ) || ( EQ_16( st_fx->element_mode, IVAS_CPE_MDCT ) ) ) /* at highest bitrates, use exclusively LP_CNG */
     809             :             {
     810           0 :                 test();
     811           0 :                 test();
     812           0 :                 IF( EQ_16( st_fx->element_mode, EVS_MONO ) && ( EQ_32( st_fx->total_brate, ACELP_9k60 ) || EQ_32( st_fx->total_brate, ACELP_16k40 ) || EQ_32( st_fx->total_brate, ACELP_24k40 ) ) )
     813             :                 {
     814           0 :                     st_fx->codec_mode = MODE2;
     815           0 :                     move16();
     816             :                 }
     817             :             }
     818             :             ELSE
     819             :             {
     820           0 :                 st_fx->cng_type = LP_CNG;
     821           0 :                 move16();
     822           0 :                 IF( st_fx->codec_mode == MODE2 )
     823             :                 {
     824           0 :                     hTdCngEnc->lp_cng_mode2 = 1;
     825           0 :                     move16();
     826             :                 }
     827           0 :                 st_fx->codec_mode = MODE1;
     828           0 :                 move16();
     829             :             }
     830             :         }
     831             : 
     832             :         /* reset the bitstream (IVAS format signalling was already written) */
     833           0 :         IF( NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) && st_fx->hBstr != NULL )
     834             :         {
     835           0 :             reset_indices_enc_fx( st_fx->hBstr, st_fx->hBstr->nb_ind_tot );
     836             :         }
     837             :     }
     838             : 
     839             :     /*------------------------------------------------------------------------*
     840             :      * Reset counters when in active frame (not in SID or FRAME_NO_DATA frame)
     841             :      *------------------------------------------------------------------------*/
     842             :     /* NB core bit rate can be "-1"  at startup , so one can not use   core_brate_fx <=2400 */
     843        5150 :     test();
     844        5150 :     test();
     845        5150 :     IF( ( NE_32( st_fx->core_brate, SID_2k40 ) ) && ( NE_32( st_fx->core_brate, SID_1k75 ) ) && ( st_fx->core_brate != FRAME_NO_DATA ) )
     846             :     {
     847        5150 :         IF( hDtxEnc != NULL )
     848             :         {
     849        5150 :             hDtxEnc->cnt_SID = 0;
     850        5150 :             move16();
     851             : 
     852             :             /* change SID update rate */
     853             :             /* first SID update is only 8 (3) frames after the active speech end */
     854        5150 :             IF( !st_fx->Opt_AMR_WB )
     855             :             {
     856        5150 :                 hDtxEnc->max_SID = FIXED_SID_RATE;
     857        5150 :                 move16();
     858             :             }
     859             :             ELSE
     860             :             {
     861           0 :                 hDtxEnc->max_SID = 3;
     862           0 :                 move16(); /* first SID update is only 3 frames after the active speech end */
     863             :             }
     864             : 
     865        5150 :             IF( LT_16( hDtxEnc->interval_SID, hDtxEnc->max_SID ) )
     866             :             {
     867           0 :                 hDtxEnc->max_SID = hDtxEnc->interval_SID;
     868           0 :                 move16(); /* change SID update rate */
     869             :             }
     870             : 
     871        5150 :             hDtxEnc->cng_cnt = 0;
     872        5150 :             move16(); /* reset the counter of CNG frames for averaging */
     873             :         }
     874        5150 :         test();
     875        5150 :         IF( GE_16( st_fx->active_cnt, CNG_TYPE_HO ) && st_fx->Opt_AMR_WB == 0 && NE_16( st_fx->element_mode, IVAS_CPE_MDCT ) )
     876             :         {
     877        5150 :             IF( EQ_16( st_fx->element_mode, IVAS_SCE ) )
     878             :             {
     879             :             }
     880             :             ELSE
     881             :             {
     882        5150 :                 test();
     883        5150 :                 test();
     884        5150 :                 test();
     885        5150 :                 test();
     886        5150 :                 test();
     887        5150 :                 test();
     888        5150 :                 test();
     889        5150 :                 test();
     890        5150 :                 IF( EQ_16( st_fx->cng_type, LP_CNG ) && ( ( EQ_16( st_fx->input_bwidth, NB ) && GT_32( st_fx->bckr_tilt_lt, 589824l /*9.f Q16*/ ) ) || ( GT_16( st_fx->input_bwidth, NB ) && GT_32( st_fx->bckr_tilt_lt, 2949120l /*45.f Q16*/ ) ) ) )
     891             :                 {
     892           0 :                     st_fx->cng_type = FD_CNG;
     893           0 :                     move16();
     894             :                 }
     895        5150 :                 ELSE IF( EQ_16( st_fx->cng_type, FD_CNG ) && ( ( EQ_16( st_fx->input_bwidth, NB ) && LT_32( st_fx->bckr_tilt_lt, 131072l /*2.f Q16*/ ) ) || ( GT_16( st_fx->input_bwidth, NB ) && LT_32( st_fx->bckr_tilt_lt, 655360l /*10.f Q16*/ ) ) ) )
     896             :                 {
     897           5 :                     st_fx->cng_type = LP_CNG;
     898           5 :                     move16();
     899             :                 }
     900             :             }
     901        5150 :             st_fx->last_total_brate_cng = -1;
     902             :         }
     903           0 :         ELSE IF( st_fx->Opt_AMR_WB )
     904             :         {
     905           0 :             st_fx->cng_type = LP_CNG;
     906           0 :             move16();
     907             :         }
     908        5150 :         st_fx->active_cnt = add( st_fx->active_cnt, 1 );
     909        5150 :         st_fx->active_cnt = s_min( st_fx->active_cnt, 200 );
     910             :     }
     911             : 
     912             :     /*------------------------------------------------------------------------*
     913             :      * Update speech and background noise long-term energy
     914             :      *------------------------------------------------------------------------*/
     915        5150 :     IF( hDtxEnc != NULL )
     916             :     {
     917        5150 :         hDtxEnc->frame_ener_fx = L_deposit_l( 0 );
     918             : 
     919        5150 :         IF( st_fx->Opt_DTX_ON )
     920             :         {
     921           0 :             Q_speech2 = add( shl( Q_speech, 1 ), 7 );
     922           0 :             FOR( j = 0; j < 16; j++ )
     923             :             {
     924           0 :                 L_tmp = L_mult0( *speech, *speech ); /*2*Q_speech*/
     925           0 :                 speech++;
     926           0 :                 FOR( i = 1; i < L_FRAME / 16; i++ )
     927             :                 {
     928           0 :                     L_tmp = L_mac0_sat( L_tmp, *speech, *speech ); /*2*Q_speech*/
     929           0 :                     speech++;
     930             :                 }
     931           0 :                 hDtxEnc->frame_ener_fx = L_add( hDtxEnc->frame_ener_fx, L_shr( L_tmp, Q_speech2 ) ); /*Q(-7) */
     932             :             }
     933             : 
     934             :             /* Active speech (voiced) */
     935             : 
     936           0 :             IF( EQ_16( st_fx->clas, VOICED_CLAS ) )
     937             :             {
     938           0 :                 alpha = ALPHA_ENER_SLOW_FX;
     939           0 :                 move16();
     940           0 :                 if ( GT_32( hDtxEnc->frame_ener_fx, hDtxEnc->lt_ener_voiced_fx ) )
     941             :                 {
     942           0 :                     alpha = ALPHA_ENER_FAST_FX; /*Q15 */
     943           0 :                     move16();
     944             :                 }
     945             : 
     946             :                 /*st_fx->lt_ener_voiced_fx = alpha * st_fx->lt_ener_voiced_fx + (1.0f-alpha) * st_fx->frame_ener_fx;*/
     947           0 :                 L_tmp = L_sub( hDtxEnc->lt_ener_voiced_fx, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
     948           0 :                 L_tmp = Mult_32_16( L_tmp, alpha );                                  /*Q(-7)*/
     949           0 :                 hDtxEnc->lt_ener_voiced_fx = L_add( L_tmp, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
     950             : 
     951           0 :                 hDtxEnc->VarDTX_cnt_voiced = add( hDtxEnc->VarDTX_cnt_voiced, 1 );
     952             : 
     953           0 :                 hDtxEnc->VarDTX_cnt_voiced = s_min( hDtxEnc->VarDTX_cnt_voiced, MIN_CNT );
     954             :             }
     955             :             /* Background noise */
     956           0 :             ELSE IF( !st_fx->Opt_AMR_WB )
     957             :             {
     958           0 :                 alpha = ALPHA_ENER_SLOW_FX; /*Q15 */
     959           0 :                 move16();
     960           0 :                 if ( LT_32( hDtxEnc->frame_ener_fx, hDtxEnc->lt_ener_noise_fx ) )
     961             :                 {
     962           0 :                     alpha = ALPHA_ENER_FAST_FX; /*Q15 */
     963           0 :                     move16();
     964             :                 }
     965             : 
     966             :                 /*st_fx->lt_ener_noise_fx = alpha * st_fx->lt_ener_noise_fx + (1.0f-alpha) * st_fx->frame_ener_fx;*/
     967           0 :                 L_tmp = L_sub( hDtxEnc->lt_ener_noise_fx, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
     968           0 :                 L_tmp = Mult_32_16( L_tmp, alpha );                                 /*Q(-7)*/
     969           0 :                 hDtxEnc->lt_ener_noise_fx = L_add( L_tmp, hDtxEnc->frame_ener_fx ); /*Q(-7)*/
     970           0 :                 move32();
     971             : 
     972           0 :                 hDtxEnc->VarDTX_cnt_noise = add( hDtxEnc->VarDTX_cnt_noise, 1 );
     973             : 
     974           0 :                 hDtxEnc->VarDTX_cnt_noise = s_min( hDtxEnc->VarDTX_cnt_noise, MIN_CNT );
     975             :             }
     976             :         }
     977             :     }
     978             : 
     979             :     /* Update of the SID counter */
     980        5150 :     update_SID_cnt_fx( hDtxEnc, st_fx->core_brate, st_fx->Opt_AMR_WB );
     981             : 
     982             :     /* Update encoded bandwidth */
     983        5150 :     test();
     984        5150 :     test();
     985        5150 :     IF( st_fx->Opt_DTX_ON && ( st_fx->core_brate == SID_2k40 || st_fx->core_brate == FRAME_NO_DATA ) )
     986             :     {
     987             : 
     988           0 :         st_fx->bwidth = st_fx->last_bwidth;
     989           0 :         move16();
     990           0 :         test();
     991           0 :         IF( GT_32( st_fx->last_core_brate, SID_2k40 ) && NE_32( st_fx->last_total_brate_cng, -1 ) )
     992             :         {
     993           0 :             st_fx->bwidth = st_fx->last_bwidth_cng;
     994           0 :             move16();
     995             :         }
     996             : 
     997           0 :         test();
     998           0 :         test();
     999           0 :         IF( st_fx->Opt_RF_ON && ( EQ_32( st_fx->total_brate, ACELP_13k20 ) ) && ( EQ_16( st_fx->bwidth, NB ) ) )
    1000             :         {
    1001           0 :             st_fx->codec_mode = MODE1;
    1002           0 :             move16();
    1003           0 :             reset_rf_indices_fx( st_fx );
    1004           0 :             st_fx->Opt_RF_ON = 0;
    1005           0 :             move16();
    1006           0 :             st_fx->rf_mode = 0;
    1007           0 :             move16();
    1008             :         }
    1009             : 
    1010           0 :         test();
    1011           0 :         test();
    1012           0 :         IF( st_fx->Opt_RF_ON && NE_32( st_fx->total_brate, ACELP_13k20 ) )
    1013             :         {
    1014           0 :             reset_rf_indices_fx( st_fx );
    1015           0 :             move16();
    1016           0 :             st_fx->Opt_RF_ON = 0;
    1017           0 :             move16();
    1018           0 :             st_fx->rf_mode = 0;
    1019           0 :             move16();
    1020             :         }
    1021             : 
    1022             :         /* Set and limit the encoded bandwidth */
    1023           0 :         IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    1024             :         {
    1025             :             Word16 n, bits_frame_nominal;
    1026             : 
    1027             :             UWord16 lsb;
    1028             :             Word16 tmpbandwidthMin;
    1029             : 
    1030           0 :             Mpy_32_16_ss( st_fx->total_brate, 5243, &L_tmp, &lsb ); /* 5243 is 1/50 in Q18. (0+18-15=3) */
    1031           0 :             bits_frame_nominal = extract_l( L_shr( L_tmp, 3 ) );    /* Q0 */
    1032             : 
    1033           0 :             FOR( n = 0; n < FRAME_SIZE_NB; n++ )
    1034             :             {
    1035           0 :                 IF( EQ_16( FrameSizeConfig[n].frame_bits, bits_frame_nominal ) )
    1036             :                 {
    1037           0 :                     BREAK;
    1038             :                 }
    1039             :             }
    1040           0 :             if ( n == FRAME_SIZE_NB )
    1041             :             {
    1042           0 :                 assert( !"Bitrate not supported: not part of EVS" );
    1043             :             }
    1044           0 :             tmpbandwidthMin = FrameSizeConfig[n].bandwidth_min;
    1045           0 :             if ( EQ_16( st_fx->rf_mode, 1 ) )
    1046             :             {
    1047           0 :                 tmpbandwidthMin = WB;
    1048             :             }
    1049           0 :             st_fx->bwidth = s_max( s_min( st_fx->bwidth, FrameSizeConfig[n].bandwidth_max ), tmpbandwidthMin );
    1050             :         }
    1051             :     }
    1052             : 
    1053        5150 :     return;
    1054             : }
    1055             : 
    1056             : /*---------------------------------------------------------------------*
    1057             :  * update_SID_cnt()
    1058             :  *
    1059             :  * Update of the SID counter
    1060             :  *---------------------------------------------------------------------*/
    1061             : 
    1062     1328936 : static void update_SID_cnt_fx(
    1063             :     DTX_ENC_HANDLE hDtxEnc,  /* i/o: common DTX handle       */
    1064             :     const Word32 core_brate, /* i  : core coder core bitrate Q0*/
    1065             :     const Word16 Opt_AMR_WB  /* i  : AMR BW IO mode?         Q0*/
    1066             : )
    1067             : {
    1068             :     Word16 EstimatedSNR, delta, frac, exp;
    1069             :     Word32 L_tmp1, L_tmp2;
    1070             : 
    1071     1328936 :     test();
    1072     1328936 :     test();
    1073     1328936 :     IF( EQ_32( core_brate, SID_2k40 ) || EQ_32( core_brate, SID_1k75 ) || core_brate == FRAME_NO_DATA )
    1074             :     {
    1075             :         /* Adapt the SID interval */
    1076       35169 :         test();
    1077       35169 :         test();
    1078       35169 :         IF( hDtxEnc->var_SID_rate_flag != 0 && EQ_16( hDtxEnc->VarDTX_cnt_voiced, MIN_CNT ) && EQ_16( hDtxEnc->VarDTX_cnt_noise, MIN_CNT ) )
    1079             :         {
    1080             :             /* EstimatedSNR = 10.0f * (float)log10( (0.01f + st_fx->lt_ener_voiced) / (0.01f + st_fx->lt_ener_noise) ); */
    1081             : 
    1082           0 :             L_tmp1 = L_max( hDtxEnc->lt_ener_voiced_fx, 1 );
    1083           0 :             exp = norm_l( L_tmp1 );
    1084           0 :             frac = Log2_norm_lc( L_shl( L_tmp1, exp ) );
    1085           0 :             exp = sub( 30, exp );
    1086           0 :             L_tmp1 = L_Comp( exp, frac ); /*Q16*/
    1087           0 :             L_tmp2 = L_max( hDtxEnc->lt_ener_noise_fx, 1 );
    1088           0 :             exp = norm_l( L_tmp2 );
    1089           0 :             frac = Log2_norm_lc( L_shl( L_tmp2, exp ) );
    1090           0 :             exp = sub( 30, exp );
    1091           0 :             L_tmp1 = L_sub( L_tmp1, L_Comp( exp, frac ) ); /*Q16*/
    1092             :             /* 10 x Log10(a/b) = 10 x Log10(2) x [Log2(a) - Log2(b)] */
    1093             :             /* 10 x Log10(2) = ~3.0103 */
    1094           0 :             L_tmp1 = Mpy_32_16_1( L_tmp1, 24660 ); /* mult by 3.0103 / 4 in Q15 */
    1095           0 :             L_tmp1 = L_shl( L_tmp1, 2 + 8 );       /* mult by 4 and shift left 8 to go in Q24 */
    1096           0 :             EstimatedSNR = round_fx( L_tmp1 );     /* now in Q8 */
    1097           0 :             IF( GT_16( EstimatedSNR, SNR_H_FX ) )
    1098             :             {
    1099           0 :                 hDtxEnc->interval_SID = INT_H;
    1100           0 :                 move16();
    1101             :             }
    1102           0 :             ELSE IF( LT_16( EstimatedSNR, SNR_L_FX ) )
    1103             :             {
    1104           0 :                 hDtxEnc->interval_SID = INT_L;
    1105           0 :                 move16();
    1106             :             }
    1107             :             ELSE
    1108             :             {
    1109           0 :                 hDtxEnc->interval_SID = extract_h( L_mac( INT_L * 65536L - SNR_L_FX / 256 * 65536L * RATIO, ( 32768 / 256 ) * RATIO, EstimatedSNR ) );
    1110             :             }
    1111           0 :             hDtxEnc->interval_SID = s_min( s_max( hDtxEnc->interval_SID, INT_L ), INT_H );
    1112             : 
    1113           0 :             test();
    1114             : 
    1115           0 :             IF( Opt_AMR_WB == 0 || NE_16( hDtxEnc->max_SID, 3 ) )
    1116             :             {
    1117           0 :                 hDtxEnc->max_SID = hDtxEnc->interval_SID;
    1118           0 :                 move16(); /* change SID update rate */
    1119             :             }
    1120             :         }
    1121       35169 :         test();
    1122       35169 :         IF( hDtxEnc->cnt_SID != 0 )
    1123             :         {
    1124       28891 :             L_tmp1 = L_max( hDtxEnc->lt_ener_noise_fx, 1 );
    1125       28891 :             exp = norm_l( L_tmp1 );
    1126       28891 :             frac = Log2_norm_lc( L_shl( L_tmp1, exp ) );
    1127       28891 :             exp = sub( 30, exp );
    1128       28891 :             L_tmp1 = L_Comp( exp, frac ); /*Q16*/
    1129       28891 :             L_tmp2 = L_max( hDtxEnc->lt_ener_last_SID_fx, 1 );
    1130       28891 :             exp = norm_l( L_tmp2 );
    1131       28891 :             frac = Log2_norm_lc( L_shl( L_tmp2, exp ) );
    1132       28891 :             exp = sub( 30, exp );
    1133       28891 :             L_tmp1 = L_sub( L_tmp1, L_Comp( exp, frac ) ); /*Q16*/
    1134             :             /* 10 x Log10(a/b) = 10 x Log10(2) x [Log2(a) - Log2(b)] */
    1135             :             /* 10 x Log10(2) = ~3.0103 */
    1136       28891 :             L_tmp1 = Mpy_32_16_1( L_tmp1, 24660 ); /* mult by 3.0103 / 4 in Q15 */
    1137       28891 :             L_tmp1 = L_shl( L_tmp1, 2 + 8 );       /* mult by 4 and shift left 8 to go in Q24 */
    1138       28891 :             delta = round_fx( L_tmp1 );            /* now in Q8 */
    1139       28891 :             test();
    1140       28891 :             test();
    1141       28891 :             IF( LT_16( delta, LTE_VAR_FX ) && EQ_16( hDtxEnc->VarDTX_cnt_voiced, MIN_CNT ) && EQ_16( hDtxEnc->VarDTX_cnt_noise, MIN_CNT ) )
    1142             :             {
    1143             :                 /* Send SID frame, and reset lt_ener_noise */
    1144         135 :                 hDtxEnc->lt_ener_noise_fx = hDtxEnc->frame_ener_fx; /* Q(-7) */
    1145         135 :                 move32();
    1146             :             }
    1147             :         }
    1148             :         ELSE
    1149             :         {
    1150             :             /* If SID frame was sent, update long-term energy */
    1151        6278 :             hDtxEnc->lt_ener_last_SID_fx = hDtxEnc->lt_ener_noise_fx; /* Q(-7) */
    1152        6278 :             move32();
    1153             :         }
    1154       35169 :         hDtxEnc->cnt_SID = add( hDtxEnc->cnt_SID, 1 );
    1155             : 
    1156       35169 :         IF( hDtxEnc->var_SID_rate_flag )
    1157             :         {
    1158           0 :             test();
    1159           0 :             test();
    1160           0 :             test();
    1161             : 
    1162           0 :             IF( Opt_AMR_WB != 0 && EQ_16( hDtxEnc->max_SID, 3 ) && EQ_16( hDtxEnc->cnt_SID, 3 ) )
    1163             :             {
    1164             :                 /* set the size of CNG history buffer for averaging to DTX_HIST_SIZE frames */
    1165             :                 /* be sure that DTX_HIST_SIZE >= INT_L */
    1166           0 :                 hDtxEnc->cng_hist_size = 3;
    1167           0 :                 move16();
    1168             :             }
    1169           0 :             test();
    1170             :             /*else if ( st_fx->max_SID != 3 && st_fx->cnt_SID == DTX_HIST_SIZE )//compile error */
    1171           0 :             if ( NE_16( hDtxEnc->max_SID, 3 ) && EQ_16( hDtxEnc->cnt_SID, DTX_HIST_SIZE ) )
    1172             :             {
    1173             :                 /* set the size of CNG history buffer for averaging to 3 frames */
    1174           0 :                 hDtxEnc->cng_hist_size = DTX_HIST_SIZE;
    1175           0 :                 move16();
    1176             :             }
    1177             :         }
    1178       35169 :         test();
    1179       35169 :         IF( hDtxEnc->var_SID_rate_flag == 0 && GT_16( hDtxEnc->interval_SID, 1 ) )
    1180             :         {
    1181             :             /* set the size of CNG history buffer for averaging to interval_SID frames */
    1182       35169 :             hDtxEnc->cng_hist_size = hDtxEnc->interval_SID;
    1183       35169 :             move16();
    1184       35169 :             if ( GT_16( hDtxEnc->cng_hist_size, DTX_HIST_SIZE ) )
    1185             :             {
    1186           0 :                 hDtxEnc->cng_hist_size = DTX_HIST_SIZE;
    1187           0 :                 move16();
    1188             :             }
    1189             :         }
    1190       35169 :         IF( GE_16( hDtxEnc->cnt_SID, hDtxEnc->max_SID ) )
    1191             :         {
    1192             :             /* adaptive SID update interval */
    1193        3721 :             hDtxEnc->max_SID = hDtxEnc->interval_SID;
    1194        3721 :             move16();
    1195        3721 :             hDtxEnc->cnt_SID = 0;
    1196        3721 :             move16();
    1197             :         }
    1198             :     }
    1199             : 
    1200     1328936 :     return;
    1201             : }
    1202             : 
    1203             : 
    1204        1573 : void dtx_hangover_control_fx(
    1205             :     Encoder_State *st_fx,      /* i/o: encoder state structure                  */
    1206             :     const Word16 lsp_new_fx[M] /* i  : current frame LSPs                       Q15*/
    1207             : )
    1208             : {
    1209             :     Word16 ptr;
    1210             :     Word16 i, j, m;
    1211             :     Word16 tmp_lsp[/*max(DTX_HIST_SIZE,*/ HO_HIST_SIZE /*)*/ * M];
    1212             :     Word32 tmp_enr[/*max(DTX_HIST_SIZE,*/ HO_HIST_SIZE /*)*/];
    1213             :     Word16 tmp[/*max(DTX_HIST_SIZE,*/ HO_HIST_SIZE /*)*/ * M];
    1214             :     Word16 enr_new;
    1215             :     Word16 weights;
    1216             :     Word32 enr_est;
    1217             :     Word16 enr_est_log;
    1218             :     Word16 lsp_est[M];
    1219             :     Word16 Dlsp, Denr;
    1220             :     Word16 lsf_tmp[M];
    1221             :     Word32 C[M];
    1222             :     Word32 max_val[2];
    1223             :     Word16 max_idx[2];
    1224             :     Word16 ftmp_fx;
    1225             :     Word16 Dlsp_n2e, Denr_n2e;
    1226             :     Word16 exp, fra, exp2, fra2;
    1227             :     Word16 S_max;
    1228             :     Word16 S_tmp;
    1229             :     Word32 L_tmp;
    1230        1573 :     VAD_HANDLE hVAD = st_fx->hVAD;
    1231        1573 :     DTX_ENC_HANDLE hDtxEnc = st_fx->hDtxEnc;
    1232        1573 :     TD_CNG_ENC_HANDLE hTdCngEnc = st_fx->hTdCngEnc;
    1233             : 
    1234             :     /* get current frame exc energy in log2 */
    1235        1573 :     exp = norm_l( hTdCngEnc->ho_ener_circ_fx[hTdCngEnc->ho_circ_ptr] );
    1236        1573 :     fra = Log2_norm_lc( L_shl( hTdCngEnc->ho_ener_circ_fx[hTdCngEnc->ho_circ_ptr], exp ) );
    1237        1573 :     exp = sub( sub( 30, exp ), 6 );
    1238        1573 :     L_tmp = L_Comp( exp, fra );              /*Q16*/
    1239        1573 :     enr_new = round_fx( L_shl( L_tmp, 8 ) ); /*Q8 */
    1240             : 
    1241        1573 :     if ( enr_new < 0 )
    1242             :     {
    1243         278 :         enr_new = 0;
    1244         278 :         move16(); /*Q8 */
    1245             :     }
    1246             : 
    1247             :     /* get energies and lsps of hangover frames  */
    1248        1573 :     ptr = sub( hTdCngEnc->ho_circ_ptr, sub( hTdCngEnc->burst_ho_cnt, 1 ) ); /*Q0*/
    1249        1573 :     IF( ptr < 0 )
    1250             :     {
    1251         493 :         ptr = add( hTdCngEnc->ho_circ_size, ptr ); /*Q0*/
    1252             :     }
    1253             : 
    1254        6146 :     FOR( i = 0; i < hTdCngEnc->burst_ho_cnt - 1; i++ )
    1255             :     {
    1256        4573 :         Copy( &( hTdCngEnc->ho_lsp_circ_fx[ptr * M] ), &( tmp_lsp[i * M] ), M ); /*Q15*/
    1257        4573 :         tmp_enr[i] = hTdCngEnc->ho_ener_circ_fx[ptr];                            /*Q6 */
    1258        4573 :         move32();
    1259             : 
    1260        4573 :         ptr = add( ptr, 1 );
    1261        4573 :         if ( EQ_16( ptr, hTdCngEnc->ho_circ_size ) )
    1262             :         {
    1263         493 :             ptr = 0;
    1264         493 :             move16();
    1265             :         }
    1266             :     }
    1267             : 
    1268             :     /* get estimated CNG energy and lsps assuming terminate hangover at current frame */
    1269        1573 :     ptr = sub( hTdCngEnc->burst_ho_cnt, 2 );
    1270        1573 :     enr_est = Mpy_32_16_1( tmp_enr[ptr], W_DTX_HO_FX[0] ); /*Q6 */
    1271        1573 :     weights = W_DTX_HO_FX[0];                              /*Q15 */
    1272        1573 :     move16();
    1273        1573 :     Copy( &( tmp_lsp[ptr * M] ), tmp, M );
    1274        1573 :     m = 1;
    1275        1573 :     move16();
    1276             : 
    1277        3470 :     FOR( i = 1; i < hTdCngEnc->burst_ho_cnt - 2; i++ )
    1278             :     {
    1279        1897 :         test();
    1280        1897 :         IF( LT_32( Mpy_32_16_1( tmp_enr[ptr - i], ONE_OVER_BUF_H_NRG_FX ), tmp_enr[ptr] ) &&
    1281             :             GT_32( tmp_enr[ptr - i], Mpy_32_16_1( tmp_enr[ptr], BUF_L_NRG_FX ) ) )
    1282             :         {
    1283         679 :             enr_est = L_add( enr_est, Mpy_32_16_1( tmp_enr[ptr - i], W_DTX_HO_FX[i] ) ); /*Q6 */
    1284         679 :             weights = add( weights, W_DTX_HO_FX[i] );                                    /*Q15 */
    1285         679 :             Copy( &tmp_lsp[( ptr - i ) * M], &tmp[m * M], M );                           /*Q15*/
    1286         679 :             m = add( m, 1 );
    1287             :         }
    1288             :     }
    1289             : 
    1290        1573 :     exp = norm_l( enr_est );
    1291        1573 :     fra = round_fx_sat( L_shl_sat( enr_est, exp ) ); /*Q6+exp-16*/
    1292        1573 :     exp2 = norm_s( weights );
    1293        1573 :     fra2 = shl( weights, exp2 ); /*Q15+exp2*/
    1294        1573 :     exp = sub( sub( exp, 16 ), exp2 );
    1295        1573 :     IF( GT_16( fra, fra2 ) )
    1296             :     {
    1297         521 :         fra = shr( fra, 1 );
    1298         521 :         exp = sub( exp, 1 );
    1299             :     }
    1300        1573 :     L_tmp = L_deposit_l( div_s( fra, fra2 ) ); /*Q6+exp*/
    1301        1573 :     enr_est = L_shr( L_tmp, exp );             /*Q6 */
    1302             : 
    1303        1573 :     if ( LT_32( enr_est, 64 ) )
    1304             :     {
    1305         294 :         enr_est = 64; /*Q6 */
    1306         294 :         move16();
    1307             :     }
    1308             : 
    1309        1573 :     exp = norm_l( enr_est );
    1310        1573 :     fra = Log2_norm_lc( L_shl( enr_est, exp ) );
    1311        1573 :     exp = sub( sub( 30, exp ), 6 );
    1312        1573 :     L_tmp = L_Comp( exp, fra );                      /*Q16*/
    1313        1573 :     enr_est_log = round_fx( L_shl( L_tmp, 8 ) );     /*Q8 */
    1314        1573 :     Denr_n2e = abs_s( sub( enr_new, enr_est_log ) ); /*Q8 */
    1315             : 
    1316        1573 :     IF( LT_16( m, 3 ) )
    1317             :     {
    1318        1399 :         enr_est = L_add( Mpy_32_16_1( enr_est, 26214 ), Mpy_32_16_1( hTdCngEnc->ho_ener_circ_fx[hTdCngEnc->ho_circ_ptr], 6554 ) ); /*Q6 */
    1319             :     }
    1320             :     ELSE
    1321             :     {
    1322         174 :         enr_est = L_add( Mpy_32_16_1( enr_est, 31130 ), Mpy_32_16_1( hTdCngEnc->ho_ener_circ_fx[hTdCngEnc->ho_circ_ptr], 1638 ) ); /*Q6 */
    1323             :     }
    1324             : 
    1325        1573 :     exp = norm_l( enr_est );
    1326        1573 :     fra = Log2_norm_lc( L_shl( enr_est, exp ) );
    1327        1573 :     exp = sub( sub( 30, exp ), 6 );
    1328        1573 :     L_tmp = L_Comp( exp, fra );                  /*Q16*/
    1329        1573 :     enr_est_log = round_fx( L_shl( L_tmp, 8 ) ); /*Q8 */
    1330             : 
    1331        1573 :     if ( enr_est_log < 0 )
    1332             :     {
    1333         299 :         enr_est_log = 0;
    1334         299 :         move16();
    1335             :     }
    1336             : 
    1337        1573 :     set32_fx( max_val, 0, 2 );
    1338        1573 :     set16_fx( max_idx, 0, 2 );
    1339             : 
    1340        3825 :     FOR( i = 0; i < m; i++ )
    1341             :     {
    1342        2252 :         IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    1343             :         {
    1344        1103 :             lsp2lsf_fx( &tmp[i * M], lsf_tmp, M, INT_FS_FX );
    1345        1103 :             ftmp_fx = 964;                                        /*6400.0f / ( M + 1 ) * 2.56*/
    1346        1103 :             move16();                                             /*QX2.56  */
    1347        1103 :             S_tmp = sub( 16384, add( lsf_tmp[M - 1], ftmp_fx ) ); /*QX2.56 */
    1348        1103 :             C[i] = L_mult0( S_tmp, S_tmp );                       /*QX6.5536 */
    1349             :         }
    1350             :         ELSE
    1351             :         {
    1352        1149 :             lsp2lsf_fx( &tmp[i * M], lsf_tmp, M, INT_FS_16k );
    1353        1149 :             ftmp_fx = 1205;                                       /*8000.0f/ ( M + 1 ) * 2.56*/
    1354        1149 :             move16();                                             /*QX2.56 */
    1355        1149 :             S_tmp = sub( 20480, add( lsf_tmp[M - 1], ftmp_fx ) ); /*QX2.56 */
    1356        1149 :             C[i] = L_mult0( S_tmp, S_tmp );                       /*QX6.5536 */
    1357             :         }
    1358             : 
    1359        2252 :         S_tmp = sub( lsf_tmp[0], ftmp_fx );  /*QX2.56 */
    1360        2252 :         C[i] = L_mac0( C[i], S_tmp, S_tmp ); /*QX6.5536       */
    1361       36032 :         FOR( j = 0; j < M - 1; j++ )
    1362             :         {
    1363       33780 :             S_tmp = sub( sub( lsf_tmp[j + 1], lsf_tmp[j] ), ftmp_fx ); /*QX2.56 */
    1364       33780 :             C[i] = L_mac0( C[i], S_tmp, S_tmp );                       /*QX6.5536 */
    1365             :         }
    1366             : 
    1367        2252 :         C[i] = Mpy_32_16_1( C[i], 1928 /*Q15*/ ); /*QX6.5536 */
    1368             : 
    1369        2252 :         IF( GT_32( C[i], max_val[0] ) )
    1370             :         {
    1371        1830 :             max_val[1] = max_val[0]; /*x6.5536*/
    1372        1830 :             move16();
    1373        1830 :             max_idx[1] = max_idx[0]; /*Q0*/
    1374        1830 :             move16();
    1375        1830 :             max_val[0] = C[i]; /*x6.5536*/
    1376        1830 :             move16();
    1377        1830 :             max_idx[0] = i;
    1378        1830 :             move16();
    1379             :         }
    1380         422 :         ELSE IF( GT_32( C[i], max_val[1] ) )
    1381             :         {
    1382         304 :             max_val[1] = C[i]; /*x6.5536*/
    1383         304 :             move16();
    1384         304 :             max_idx[1] = i;
    1385         304 :             move16();
    1386             :         }
    1387             :     }
    1388             : 
    1389        1573 :     IF( EQ_16( m, 1 ) )
    1390             :     {
    1391        1173 :         Copy( tmp, lsp_est, M ); /*Q15*/
    1392             :     }
    1393         400 :     ELSE IF( LT_16( m, 4 ) )
    1394             :     {
    1395        5610 :         FOR( i = 0; i < M; i++ )
    1396             :         {
    1397        5280 :             lsp_est[i] = 0; /*Q15 */
    1398        5280 :             move16();
    1399       17504 :             FOR( j = 0; j < m; j++ )
    1400             :             {
    1401       12224 :                 lsp_est[i] = add_sat( lsp_est[i], tmp[j * M + i] ); /*Q15 */
    1402             :             }
    1403             : 
    1404        5280 :             lsp_est[i] = sub( lsp_est[i], tmp[max_idx[0] * M + i] ); /*Q15 */
    1405        5280 :             S_tmp = div_s( 1, sub( m, 1 ) );                         /*Q15 */
    1406        5280 :             lsp_est[i] = mult_r( lsp_est[i], S_tmp );                /*Q15 */
    1407             :         }
    1408             :     }
    1409             :     ELSE
    1410             :     {
    1411        1190 :         FOR( i = 0; i < M; i++ )
    1412             :         {
    1413        1120 :             lsp_est[i] = 0; /*Q15 */
    1414        1120 :             move16();
    1415        6160 :             FOR( j = 0; j < m; j++ )
    1416             :             {
    1417        5040 :                 lsp_est[i] = add_sat( lsp_est[i], tmp[j * M + i] ); /*Q15 */
    1418             :             }
    1419             : 
    1420        1120 :             lsp_est[i] = sub_sat( lsp_est[i], add_sat( tmp[max_idx[0] * M + i], tmp[max_idx[1] * M + i] ) ); /*Q15 */
    1421        1120 :             S_tmp = div_s( 1, sub( m, 2 ) );                                                                 /*Q15 */
    1422        1120 :             lsp_est[i] = mult_r( lsp_est[i], S_tmp );                                                        /*Q15 */
    1423             :         }
    1424             :     }
    1425             : 
    1426        1573 :     Dlsp_n2e = 0; /*Q15 */
    1427        1573 :     move16();
    1428       26741 :     FOR( i = 0; i < M; i++ )
    1429             :     {
    1430       25168 :         Dlsp_n2e = add_sat( Dlsp_n2e, abs_s( sub( lsp_new_fx[i], lsp_est[i] ) ) );      /*Q15 */
    1431       25168 :         lsp_est[i] = add( mult_r( 26214, lsp_est[i] ), mult_r( 6554, lsp_new_fx[i] ) ); /*Q15 */
    1432             :     }
    1433             : 
    1434             :     /* get deviation of CNG parameters between newly estimated and current state memory */
    1435        1573 :     Dlsp = 0;
    1436        1573 :     move16();
    1437        1573 :     S_max = 0;
    1438        1573 :     move16();
    1439             : 
    1440       26741 :     FOR( i = 0; i < M; i++ )
    1441             :     {
    1442       25168 :         S_tmp = abs_s( sub( hDtxEnc->lspCNG_fx[i], lsp_est[i] ) ); /*Q15 */
    1443       25168 :         Dlsp = add_sat( Dlsp, S_tmp );                             /*Q15 */
    1444       25168 :         IF( GT_16( S_tmp, S_max ) )
    1445             :         {
    1446        7219 :             S_max = S_tmp; /*Q15 */
    1447             :         }
    1448             :     }
    1449             : 
    1450        1573 :     exp = norm_l( hTdCngEnc->lp_ener_fx );
    1451        1573 :     fra = Log2_norm_lc( L_shl( hTdCngEnc->lp_ener_fx, exp ) );
    1452        1573 :     exp = sub( sub( 30, exp ), 6 );
    1453        1573 :     L_tmp = L_Comp( exp, fra );                /*Q16*/
    1454        1573 :     S_tmp = round_fx( L_shl( L_tmp, 8 ) );     /*Q8 */
    1455        1573 :     Denr = abs_s( sub( S_tmp, enr_est_log ) ); /*Q8 */
    1456             : 
    1457             :     /* make decision if DTX hangover can be terminated */
    1458        1573 :     hVAD->hangover_terminate_flag = 0; /*Q0 */
    1459        1573 :     move16();
    1460             : 
    1461        1573 :     test();
    1462        1573 :     test();
    1463        1573 :     test();
    1464        1573 :     test();
    1465        1573 :     test();
    1466        1573 :     test();
    1467        1573 :     test();
    1468        1573 :     test();
    1469        1573 :     test();
    1470        1573 :     test();
    1471        1573 :     test();
    1472        1573 :     IF( ( LT_16( Dlsp, 13107 ) && LT_16( Denr, 359 ) && LT_16( S_max, 3277 ) && LT_16( Dlsp_n2e, 13107 ) && LT_16( Denr_n2e, 308 ) && st_fx->Opt_SC_VBR == 1 ) ||
    1473             :         ( LT_16( Dlsp, 13107 ) && LT_16( Denr, 205 ) && LT_16( S_max, 3277 ) && LT_16( Dlsp_n2e, 13107 ) && LT_16( Denr_n2e, 205 ) && st_fx->Opt_SC_VBR == 0 ) )
    1474             : 
    1475             :     {
    1476          68 :         hVAD->hangover_terminate_flag = 1; /*Q0   */
    1477          68 :         move16();
    1478             :     }
    1479             : 
    1480        1573 :     return;
    1481             : }
    1482             : 
    1483             : /*-------------------------------------------------------------------*
    1484             :  * td_cng_enc_init_fx()
    1485             :  *
    1486             :  *
    1487             :  *-------------------------------------------------------------------*/
    1488             : 
    1489           5 : void td_cng_enc_init_fx(
    1490             :     TD_CNG_ENC_HANDLE hTdCngEnc, /* i/o: DTX/TD CNG data handle              */
    1491             :     const Word16 Opt_DTX_ON,     /* i  : flag indicating DTX operation       Q0*/
    1492             :     const Word16 max_bwidth      /* i  : maximum encoded bandwidth           Q0*/
    1493             : )
    1494             : {
    1495             : 
    1496           5 :     hTdCngEnc->cng_seed = RANDOM_INITSEED;
    1497           5 :     hTdCngEnc->cng_ener_seed = RANDOM_INITSEED;
    1498           5 :     hTdCngEnc->cng_ener_seed1 = RANDOM_INITSEED;
    1499           5 :     hTdCngEnc->lp_ener_fx = 0;
    1500           5 :     hTdCngEnc->old_enr_index = -1;
    1501           5 :     hTdCngEnc->Enew_fx = 0;
    1502             : 
    1503           5 :     hTdCngEnc->lp_sp_enr_fx = 0;
    1504           5 :     hTdCngEnc->last_allow_cn_step = 0;
    1505           5 :     move16();
    1506           5 :     move32();
    1507           5 :     move16();
    1508           5 :     move16();
    1509           5 :     move32();
    1510           5 :     move16();
    1511           5 :     move16();
    1512             : 
    1513           5 :     IF( Opt_DTX_ON )
    1514             :     {
    1515           0 :         hTdCngEnc->cng_hist_ptr = -1;
    1516           0 :         move16();
    1517           0 :         set16_fx( hTdCngEnc->cng_lsp_hist_fx, 0, DTX_HIST_SIZE * M );
    1518           0 :         set16_fx( hTdCngEnc->cng_ener_hist_fx, 0, DTX_HIST_SIZE );
    1519             : 
    1520           0 :         hTdCngEnc->ho_hist_ptr = -1;
    1521           0 :         move16();
    1522           0 :         hTdCngEnc->ho_sid_bw = L_deposit_l( 0 );
    1523           0 :         set16_fx( hTdCngEnc->ho_lsp_hist_fx, 0, HO_HIST_SIZE * M );
    1524           0 :         set32_fx( hTdCngEnc->ho_ener_hist_fx, 0, HO_HIST_SIZE );
    1525           0 :         set32_fx( hTdCngEnc->ho_env_hist_fx, 0, HO_HIST_SIZE * NUM_ENV_CNG );
    1526           0 :         hTdCngEnc->ho_hist_size = 0;
    1527           0 :         move16();
    1528           0 :         hTdCngEnc->act_cnt = 0;
    1529           0 :         move16();
    1530             :     }
    1531             : 
    1532           5 :     set16_fx( hTdCngEnc->ho_16k_lsp, 0, HO_HIST_SIZE );
    1533           5 :     hTdCngEnc->act_cnt2 = 0;
    1534           5 :     hTdCngEnc->num_ho = 0;
    1535           5 :     move16();
    1536           5 :     move16();
    1537             : 
    1538           5 :     hTdCngEnc->ho_circ_ptr = -1;
    1539           5 :     move16();
    1540           5 :     set16_fx( hTdCngEnc->ho_lsp_circ_fx, 0, HO_HIST_SIZE * M );
    1541           5 :     set32_fx( hTdCngEnc->ho_ener_circ_fx, 0, HO_HIST_SIZE );
    1542           5 :     set32_fx( hTdCngEnc->ho_env_circ_fx, 0, HO_HIST_SIZE * NUM_ENV_CNG );
    1543           5 :     hTdCngEnc->ho_circ_size = 0;
    1544           5 :     hTdCngEnc->burst_ho_cnt = 0;
    1545           5 :     move16();
    1546           5 :     move16();
    1547             : 
    1548           5 :     hTdCngEnc->cng_buf_cnt = 0;
    1549             : 
    1550             : 
    1551           5 :     set32_fx( hTdCngEnc->lp_env_fx, 0, 20 );
    1552           5 :     set32_fx( hTdCngEnc->cng_res_env_fx, 0, 20 * 8 );
    1553           5 :     set16_fx( hTdCngEnc->exc_mem_fx, 0, 24 );
    1554           5 :     set16_fx( hTdCngEnc->exc_mem1_fx, 0, 30 );
    1555           5 :     set16_fx( hTdCngEnc->exc_mem2_fx, 0, 30 );
    1556           5 :     set32_fx( hTdCngEnc->old_env_fx, 0, NUM_ENV_CNG );
    1557             : 
    1558             :     /* SWB CNG/DTX */
    1559           5 :     hTdCngEnc->last_wb_cng_ener_fx = -1541; /* Q8 */
    1560           5 :     move16();
    1561           5 :     hTdCngEnc->last_shb_cng_ener_fx = -1541; /* Q8 */
    1562           5 :     move16();
    1563           5 :     hTdCngEnc->mov_wb_cng_ener_fx = -1541; /* Q8 */
    1564           5 :     move16();
    1565           5 :     hTdCngEnc->mov_shb_cng_ener_fx = -1541; /* Q8 */
    1566           5 :     move16();
    1567           5 :     hTdCngEnc->shb_cng_ini_cnt = 1;
    1568           5 :     move16();
    1569           5 :     hTdCngEnc->shb_NO_DATA_cnt = 0;
    1570           5 :     move16();
    1571           5 :     hTdCngEnc->last_SID_bwidth = s_min( max_bwidth, SWB );
    1572           5 :     hTdCngEnc->last_vad = 0;
    1573           5 :     move16();
    1574             : 
    1575           5 :     return;
    1576             : }
    1577             : 
    1578         200 : void td_cng_enc_init_ivas_fx(
    1579             :     TD_CNG_ENC_HANDLE hTdCngEnc, /* i/o: DTX/TD CNG data handle              */
    1580             :     const Word16 Opt_DTX_ON,     /* i  : flag indicating DTX operation       Q0*/
    1581             :     const Word16 max_bwidth      /* i  : maximum encoded bandwidth           Q0*/
    1582             : )
    1583             : {
    1584             : 
    1585         200 :     hTdCngEnc->cng_seed = RANDOM_INITSEED;
    1586         200 :     move16();
    1587         200 :     hTdCngEnc->cng_ener_seed = RANDOM_INITSEED;
    1588         200 :     move16();
    1589         200 :     hTdCngEnc->cng_ener_seed1 = RANDOM_INITSEED;
    1590         200 :     move16();
    1591         200 :     hTdCngEnc->lp_ener_fx = 0;
    1592         200 :     move16();
    1593         200 :     hTdCngEnc->old_enr_index = -1;
    1594         200 :     move16();
    1595         200 :     hTdCngEnc->Enew_fx = 0;
    1596         200 :     move16();
    1597             : 
    1598         200 :     hTdCngEnc->lp_sp_enr_fx = 0;
    1599         200 :     move16();
    1600         200 :     hTdCngEnc->last_allow_cn_step = 0;
    1601         200 :     move16();
    1602             : 
    1603         200 :     hTdCngEnc->CNG_att_fx = 0;
    1604         200 :     move16();
    1605             : 
    1606         200 :     IF( Opt_DTX_ON )
    1607             :     {
    1608         200 :         hTdCngEnc->cng_hist_ptr = -1;
    1609         200 :         move16();
    1610         200 :         set16_fx( hTdCngEnc->cng_lsp_hist_fx, 0, DTX_HIST_SIZE * M );
    1611         200 :         set16_fx( hTdCngEnc->cng_ener_hist_fx, 0, DTX_HIST_SIZE );
    1612         200 :         hTdCngEnc->ho_hist_ptr = -1;
    1613         200 :         move16();
    1614         200 :         hTdCngEnc->ho_sid_bw = 0;
    1615         200 :         move32();
    1616         200 :         set16_fx( hTdCngEnc->ho_lsp_hist_fx, 0, HO_HIST_SIZE * M );
    1617         200 :         set32_fx( hTdCngEnc->ho_ener_hist_fx, 0, HO_HIST_SIZE );
    1618         200 :         set32_fx( hTdCngEnc->ho_env_hist_fx, 0, HO_HIST_SIZE * NUM_ENV_CNG );
    1619         200 :         hTdCngEnc->ho_hist_size = 0;
    1620         200 :         move16();
    1621         200 :         hTdCngEnc->act_cnt = 0;
    1622         200 :         move16();
    1623             :     }
    1624             : 
    1625         200 :     set16_fx( hTdCngEnc->ho_16k_lsp, 0, HO_HIST_SIZE );
    1626         200 :     hTdCngEnc->act_cnt2 = 0;
    1627         200 :     move16();
    1628         200 :     hTdCngEnc->num_ho = 0;
    1629         200 :     move16();
    1630             : 
    1631         200 :     hTdCngEnc->ho_circ_ptr = -1;
    1632         200 :     move16();
    1633         200 :     set16_fx( hTdCngEnc->ho_lsp_circ_fx, 0, HO_HIST_SIZE * M );
    1634         200 :     set32_fx( hTdCngEnc->ho_ener_circ_fx, 0, HO_HIST_SIZE );
    1635         200 :     set32_fx( hTdCngEnc->ho_env_circ_fx, 0, HO_HIST_SIZE * NUM_ENV_CNG );
    1636         200 :     hTdCngEnc->ho_circ_size = 0;
    1637         200 :     move16();
    1638         200 :     hTdCngEnc->burst_ho_cnt = 0;
    1639         200 :     move16();
    1640         200 :     hTdCngEnc->cng_buf_cnt = 0;
    1641         200 :     move16();
    1642             : 
    1643         200 :     set32_fx( hTdCngEnc->lp_env_fx, 0, 20 );
    1644         200 :     set32_fx( hTdCngEnc->cng_res_env_fx, 0, 20 * 8 );
    1645         200 :     set16_fx( hTdCngEnc->exc_mem_fx, 0, 24 );
    1646         200 :     set16_fx( hTdCngEnc->exc_mem1_fx, 0, 30 );
    1647         200 :     set16_fx( hTdCngEnc->exc_mem2_fx, 0, 30 );
    1648         200 :     set32_fx( hTdCngEnc->old_env_fx, 0, NUM_ENV_CNG );
    1649         200 :     set16_fx( hTdCngEnc->cng_exc2_buf, 0, HO_HIST_SIZE * L_FFT );
    1650         200 :     set16_fx( hTdCngEnc->cng_Qexc_buf, Q15, HO_HIST_SIZE );
    1651             : 
    1652             :     /* SWB CNG/DTX */
    1653         200 :     hTdCngEnc->last_wb_cng_ener_fx = -1541; /* Q8 */
    1654         200 :     move16();
    1655         200 :     hTdCngEnc->last_shb_cng_ener_fx = -1541; /* Q8 */
    1656         200 :     move16();
    1657         200 :     hTdCngEnc->mov_wb_cng_ener_fx = -1541; /* Q8 */
    1658         200 :     move16();
    1659         200 :     hTdCngEnc->mov_shb_cng_ener_fx = -1541; /* Q8 */
    1660         200 :     move16();
    1661         200 :     hTdCngEnc->shb_cng_ini_cnt = 1;
    1662         200 :     move16();
    1663         200 :     hTdCngEnc->shb_NO_DATA_cnt = 0;
    1664         200 :     move16();
    1665         200 :     hTdCngEnc->last_SID_bwidth = s_min( max_bwidth, SWB );
    1666         200 :     move16();
    1667         200 :     hTdCngEnc->last_vad = 0;
    1668         200 :     move16();
    1669         200 :     hTdCngEnc->last_idx_ener = 0;
    1670         200 :     move16();
    1671             : 
    1672         200 :     return;
    1673             : }
    1674             : 
    1675             : /*-------------------------------------------------------------------*
    1676             :  * dtx_enc_init_fx()
    1677             :  *
    1678             :  * Initialize DTX parameters
    1679             :  *-------------------------------------------------------------------*/
    1680             : 
    1681         863 : void dtx_enc_init_fx(
    1682             :     Encoder_State *st,              /* i  : Encoder state handle              */
    1683             :     const Word16 var_SID_rate_flag, /* i  : flag for variable SID update rate Q0*/
    1684             :     const Word16 interval_SID       /* i  : interval for SID update           Q0*/
    1685             : )
    1686             : {
    1687             :     DTX_ENC_HANDLE hDtxEnc;
    1688         863 :     hDtxEnc = st->hDtxEnc;
    1689             : 
    1690         863 :     hDtxEnc->first_CNG = 0;
    1691         863 :     move16();
    1692         863 :     hDtxEnc->cnt_SID = 0;
    1693         863 :     move16();
    1694         863 :     hDtxEnc->max_SID = 2;
    1695         863 :     move16();
    1696         863 :     hDtxEnc->CNG_mode = -1;
    1697         863 :     move16();
    1698         863 :     Copy( st->lsp_old1_fx, hDtxEnc->lspCNG_fx, M );
    1699         863 :     hDtxEnc->VarDTX_cnt_voiced = 0;
    1700         863 :     move16();
    1701         863 :     hDtxEnc->VarDTX_cnt_noise = 0;
    1702         863 :     move16();
    1703         863 :     hDtxEnc->lt_ener_voiced_fx = 0;
    1704         863 :     move32();
    1705         863 :     hDtxEnc->lt_ener_noise_fx = 0;
    1706         863 :     move32();
    1707         863 :     hDtxEnc->frame_ener_fx = 0;
    1708         863 :     move32();
    1709         863 :     hDtxEnc->lt_ener_last_SID_fx = 0;
    1710         863 :     move32();
    1711         863 :     hDtxEnc->last_CNG_L_frame = L_FRAME;
    1712         863 :     move16();
    1713         863 :     hDtxEnc->var_SID_rate_flag = var_SID_rate_flag; /*Q0*/
    1714         863 :     move16();
    1715         863 :     hDtxEnc->last_active_brate = ACELP_7k20;
    1716         863 :     move32();
    1717         863 :     hDtxEnc->cng_cnt = 0;
    1718         863 :     move16();
    1719             : 
    1720         863 :     IF( hDtxEnc->var_SID_rate_flag )
    1721             :     {
    1722           5 :         hDtxEnc->interval_SID = 12;
    1723           5 :         move16();
    1724           5 :         hDtxEnc->cng_hist_size = DTX_HIST_SIZE;
    1725           5 :         move16();
    1726             :     }
    1727             :     ELSE
    1728             :     {
    1729         858 :         hDtxEnc->interval_SID = interval_SID;
    1730         858 :         move16();
    1731         858 :         if ( GE_16( hDtxEnc->interval_SID, DTX_HIST_SIZE ) )
    1732             :         {
    1733         858 :             hDtxEnc->cng_hist_size = hDtxEnc->interval_SID;
    1734         858 :             move16();
    1735             :         }
    1736             :     }
    1737         863 :     return;
    1738             : }

Generated by: LCOV version 1.14