LCOV - code coverage report
Current view: top level - lib_enc - dtx_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ a17e51f9ff5a27406f0609d97377f4ae607c6718 Lines: 693 963 72.0 %
Date: 2025-11-06 02:08:20 Functions: 7 7 100.0 %

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

Generated by: LCOV version 1.14