LCOV - code coverage report
Current view: top level - lib_enc - dtx.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 305 348 87.6 %
Date: 2025-05-23 08:37:30 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #ifdef DEBUGGING
      41             : #include "debug.h"
      42             : #endif
      43             : #include <math.h>
      44             : #include "cnst.h"
      45             : #include "prot.h"
      46             : #include "ivas_prot.h"
      47             : #include "rom_com.h"
      48             : #include "wmc_auto.h"
      49             : 
      50             : /*-------------------------------------------------------------------*
      51             :  * Local constants
      52             :  *-------------------------------------------------------------------*/
      53             : 
      54             : #define ALPHA_ENER_SLOW 0.99f /* Slow adaptation (noise up, speech down) */
      55             : #define ALPHA_ENER_FAST 0.90f /* Fast adaptation (noise down, speech up) */
      56             : #define MIN_CNT         50    /* Minimum frame number before SID interval adaptation */
      57             : 
      58             : #define SNR_H 51.0f /* Estimated SNR and corresponding SID interval        */
      59             :                     /* 51dB corresponds to 25dB SNR before noise supressor */
      60             : #define SNR_L 36.0f
      61             : #define INT_H 50
      62             : #define INT_L 8
      63             : 
      64             : #define LTE_VAR -4.0f
      65             : 
      66             : #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 */
      67             : #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 */
      68             : 
      69             : #define DTX_THR_EVS 15 /* lp_noise threshold for DTX at higher bitrates in EVS */ // ToDo: to be removed once EVS is CR fixed
      70             : 
      71             : /*-------------------------------------------------------------------*
      72             :  * Local function prototypes
      73             :  *-------------------------------------------------------------------*/
      74             : 
      75             : static void update_SID_cnt( DTX_ENC_HANDLE hDtxEnc, const int32_t core_brate, const int16_t Opt_AMR_WB );
      76             : 
      77             : /*-------------------------------------------------------------------*
      78             :  * dtx()
      79             :  *
      80             :  * Discontinuous transmission operation
      81             :  *-------------------------------------------------------------------*/
      82             : 
      83     1153834 : void dtx(
      84             :     Encoder_State *st,                   /* i/o: encoder state structure            */
      85             :     const int32_t last_ivas_total_brate, /* i  : last IVAS total bitrate            */
      86             :     const int32_t ivas_total_brate,      /* i  : IVAS total bitrate                 */
      87             :     const int16_t vad,                   /* i  : VAD flag for DTX                   */
      88             :     const float speech[]                 /* i  : Pointer to the speech frame        */
      89             : )
      90             : {
      91             :     float alpha;
      92     1153834 :     DTX_ENC_HANDLE hDtxEnc = st->hDtxEnc;
      93             :     int16_t last_br_cng_flag, last_br_flag, br_dtx_flag;
      94             :     int32_t total_brate_ref;
      95             :     int16_t dtx_thr;
      96             : 
      97     1153834 :     dtx_thr = DTX_THR;
      98     1153834 :     if ( st->element_mode == EVS_MONO )
      99             :     {
     100        3100 :         dtx_thr = DTX_THR_EVS; // ToDo: to be removed once EVS is CR fixed
     101             :     }
     102             : 
     103     1153834 :     total_brate_ref = st->total_brate;
     104             : 
     105     1153834 :     if ( st->dtx_sce_sba != 0 )
     106             :     {
     107       11370 :         last_br_cng_flag = 1;
     108       11370 :         last_br_flag = 1;
     109             :     }
     110             :     else
     111             :     {
     112     1142464 :         last_br_cng_flag = st->last_total_brate_cng <= MAX_BRATE_DTX_EVS || st->lp_noise < dtx_thr || ( st->element_mode == IVAS_SCE && st->last_total_brate_cng <= MAX_BRATE_DTX_IVAS );
     113     1145564 :         last_br_flag = ( st->element_mode == EVS_MONO && st->last_total_brate <= MAX_BRATE_DTX_EVS ) ||
     114     1868826 :                        ( st->element_mode != EVS_MONO && last_ivas_total_brate <= MAX_BRATE_DTX_IVAS ) ||
     115      723262 :                        st->lp_noise < dtx_thr;
     116             :     }
     117             : 
     118             :     /* Initialization */
     119     1153834 :     if ( st->ini_frame == 0 )
     120             :     {
     121        8796 :         st->active_cnt = CNG_TYPE_HO;
     122             : 
     123        8796 :         if ( ( st->codec_mode == MODE1 || st->Opt_AMR_WB ) && st->element_mode != IVAS_SCE && st->element_mode != IVAS_CPE_MDCT )
     124             :         {
     125         134 :             st->cng_type = LP_CNG;
     126             :         }
     127             :         else
     128             :         {
     129        8662 :             st->cng_type = FD_CNG;
     130             :         }
     131             :     }
     132             : 
     133     1153834 :     if ( st->Opt_DTX_ON && vad == 0 &&
     134       39898 :          st->ini_frame > 2 && /* CNG coding starts after 3 frames */
     135       39898 :          st->fd_cng_reset_flag == 0 &&
     136       39722 :          st->last_core != AMR_WB_CORE &&
     137       39722 :          st->Opt_AMR_WB == 0 )
     138             :     {
     139       39722 :         if ( st->last_core_brate > SID_2k40 &&
     140       13186 :              st->last_total_brate_cng != -1 &&
     141        9183 :              st->last_total_brate_cng != st->total_brate &&
     142             :              last_br_cng_flag )
     143             :         {
     144        4178 :             st->total_brate = st->last_total_brate_cng;
     145        4178 :             if ( !( st->total_brate == ACELP_7k20 && st->Opt_SC_VBR ) )
     146             :             {
     147        4178 :                 st->Opt_SC_VBR = 0;
     148             :             }
     149             : 
     150        4178 :             st->rf_mode = st->last_rf_mode_cng;
     151        4178 :             st->bwidth = st->last_bwidth_cng;
     152        4178 :             st->codec_mode = st->last_codec_mode_cng;
     153             :         }
     154             : 
     155       39722 :         if ( st->last_core_brate <= SID_2k40 &&
     156       26536 :              st->last_total_brate != st->total_brate &&
     157             :              last_br_flag )
     158             :         {
     159        4197 :             st->total_brate = st->last_total_brate;
     160             : 
     161        4197 :             if ( !( st->total_brate == ACELP_7k20 && st->Opt_SC_VBR ) )
     162             :             {
     163        4197 :                 st->Opt_SC_VBR = 0;
     164             :             }
     165             : 
     166        4197 :             st->Opt_RF_ON = 0;
     167        4197 :             if ( st->rf_mode && st->rf_fec_offset > 0 && st->total_brate == ACELP_13k20 && st->bwidth != NB )
     168             :             {
     169           0 :                 st->Opt_RF_ON = 1;
     170             :             }
     171        4197 :             st->rf_mode = st->Opt_RF_ON;
     172        4197 :             st->bwidth = st->last_bwidth;
     173             : 
     174        4197 :             if ( st->element_mode > EVS_MONO )
     175             :             {
     176        4197 :                 st->codec_mode = MODE1;
     177             :             }
     178             :             else
     179             :             {
     180           0 :                 st->codec_mode = get_codec_mode( st->total_brate );
     181             :             }
     182             :         }
     183             :     }
     184             : 
     185             :     /*------------------------------------------------------------------------*
     186             :      * Select SID or FRAME_NO_DATA frame if DTX is enabled
     187             :      *------------------------------------------------------------------------*/
     188             : 
     189     1153834 :     br_dtx_flag = 1;
     190     1153834 :     if ( st->dtx_sce_sba == 0 )
     191             :     {
     192     1145564 :         br_dtx_flag = ( st->element_mode == EVS_MONO && st->total_brate <= MAX_BRATE_DTX_EVS ) ||
     193     1873806 :                       ( st->element_mode != EVS_MONO && ivas_total_brate <= MAX_BRATE_DTX_IVAS ) ||
     194      728242 :                       st->lp_noise < dtx_thr;
     195             :     }
     196             : 
     197     1153834 :     if ( st->Opt_DTX_ON && vad == 0 &&
     198       39898 :          st->ini_frame > 2 && /* CNG coding starts after 3 frames */
     199             : 
     200       36069 :          br_dtx_flag &&
     201       36069 :          st->fd_cng_reset_flag == 0 )
     202             :     {
     203             :         /* reset counter */
     204       36016 :         st->active_cnt = 0;
     205             : 
     206       36016 :         if ( st->Opt_AMR_WB )
     207             :         {
     208           0 :             st->last_total_brate_cng = -1;
     209             :         }
     210             :         else
     211             :         {
     212       36016 :             st->last_total_brate_cng = st->total_brate;
     213       36016 :             st->last_bwidth_cng = st->bwidth;
     214       36016 :             st->last_codec_mode_cng = st->codec_mode;
     215       36016 :             st->last_rf_mode_cng = st->rf_mode;
     216             :         }
     217             : 
     218       36016 :         if ( hDtxEnc->cnt_SID == 0 )
     219             :         {
     220             :             /* this will be a SID frame */
     221        5817 :             if ( st->Opt_AMR_WB )
     222             :             {
     223           0 :                 st->core_brate = SID_1k75;
     224             :             }
     225             :             else
     226             :             {
     227        5817 :                 st->core_brate = SID_2k40;
     228             :             }
     229             :         }
     230             :         else
     231             :         {
     232             :             /* this will be a no data frame */
     233       30199 :             st->core_brate = FRAME_NO_DATA;
     234             :         }
     235             : 
     236       36016 :         if ( st->core_brate == FRAME_NO_DATA && st->last_core != ACELP_CORE && !st->Opt_AMR_WB )
     237             :         {
     238             :             /* force SID frame when switching from HQ core or AMR-WB IO mode into inactive frame in ACELP core when DTX is on */
     239             :             {
     240        2393 :                 st->core_brate = SID_2k40;
     241             :             }
     242             :         }
     243             : 
     244       36016 :         if ( ( st->last_core != ACELP_CORE || st->cng_type == FD_CNG ) && st->dtx_sce_sba == 1 )
     245             :         {
     246        1976 :             st->cng_type = FD_CNG;
     247        1976 :             if ( st->element_mode == EVS_MONO && ( st->total_brate == ACELP_9k60 || st->total_brate == ACELP_16k40 || st->total_brate == ACELP_24k40 || st->total_brate == ACELP_48k || st->total_brate == HQ_96k || st->total_brate == HQ_128k ) )
     248             :             {
     249           0 :                 st->codec_mode = MODE2;
     250             :             }
     251             :         }
     252             :         else
     253             :         {
     254       34040 :             if ( ( st->cng_type == FD_CNG && ( st->total_brate <= MAX_BRATE_DTX_EVS || ( st->element_mode != EVS_MONO && ivas_total_brate <= MAX_BRATE_DTX_IVAS ) ) ) || ( st->element_mode == IVAS_CPE_MDCT ) ) /* at highest bitrates, use exclusively LP_CNG */
     255             :             {
     256       31437 :                 if ( st->element_mode == EVS_MONO && ( st->total_brate == ACELP_9k60 || st->total_brate == ACELP_16k40 || st->total_brate == ACELP_24k40 ) )
     257             :                 {
     258           0 :                     st->codec_mode = MODE2;
     259             :                 }
     260             :             }
     261             :             else
     262             :             {
     263        2603 :                 st->cng_type = LP_CNG;
     264        2603 :                 if ( st->codec_mode == MODE2 )
     265             :                 {
     266           0 :                     st->hTdCngEnc->lp_cng_mode2 = 1;
     267             :                 }
     268        2603 :                 st->codec_mode = MODE1;
     269             :             }
     270             :         }
     271             : 
     272             :         /* reset the bitstream (IVAS format signaling was already written) */
     273       36016 :         if ( st->element_mode != IVAS_CPE_MDCT && st->hBstr != NULL )
     274             :         {
     275       22753 :             reset_indices_enc( st->hBstr, st->hBstr->nb_ind_tot );
     276             :         }
     277             :     }
     278     1117818 :     else if ( st->element_mode != EVS_MONO )
     279             :     {
     280     1114718 :         st->total_brate = total_brate_ref;
     281             :     }
     282             : 
     283             :     /*------------------------------------------------------------------------*
     284             :      * Reset counters when in active frame (neither SID nor FRAME_NO_DATA frame)
     285             :      *------------------------------------------------------------------------*/
     286             : 
     287     1153834 :     if ( st->core_brate != SID_2k40 && st->core_brate != SID_1k75 && st->core_brate != FRAME_NO_DATA )
     288             :     {
     289     1117818 :         if ( hDtxEnc != NULL )
     290             :         {
     291      106036 :             hDtxEnc->cnt_SID = 0;
     292             : 
     293             :             /* change SID update rate */
     294             :             /* first SID update is only 8 (3 in AMR-WB IO mode) frames after the active speech end */
     295      106036 :             if ( !st->Opt_AMR_WB )
     296             :             {
     297      106036 :                 hDtxEnc->max_SID = FIXED_SID_RATE;
     298             :             }
     299             :             else
     300             :             {
     301           0 :                 hDtxEnc->max_SID = 3;
     302             :             }
     303             : 
     304      106036 :             if ( hDtxEnc->max_SID > hDtxEnc->interval_SID )
     305             :             {
     306           0 :                 hDtxEnc->max_SID = hDtxEnc->interval_SID;
     307             :             }
     308             : 
     309             :             /* reset the counter of CNG frames for averaging */
     310      106036 :             hDtxEnc->cng_cnt = 0;
     311             :         }
     312             : 
     313     1117818 :         if ( st->active_cnt >= CNG_TYPE_HO && !st->Opt_AMR_WB && st->element_mode != IVAS_CPE_MDCT )
     314             :         {
     315      404425 :             if ( st->element_mode == IVAS_SCE )
     316             :             {
     317             :                 float lp_thresh, fd_thresh;
     318      348144 :                 if ( st->Opt_DTX_ON && st->dtx_sce_sba == 1 )
     319             :                 {
     320        9206 :                     lp_thresh = 5.f;
     321        9206 :                     fd_thresh = 2.f;
     322             :                 }
     323             :                 else
     324             : 
     325             :                 {
     326      338938 :                     lp_thresh = 10.f;
     327      338938 :                     fd_thresh = 5.f;
     328             :                 }
     329             : 
     330             :                 /*More conservative selection of LP-CNG for SCE*/
     331      348144 :                 if ( st->cng_type == LP_CNG && ( st->bckr_tilt_lt > lp_thresh ) )
     332             :                 {
     333           0 :                     st->cng_type = FD_CNG;
     334             :                 }
     335      348144 :                 else if ( st->cng_type == FD_CNG && ( st->bckr_tilt_lt < fd_thresh ) && ( st->lp_noise > 2.f ) )
     336             :                 {
     337          64 :                     st->cng_type = LP_CNG;
     338             :                 }
     339             :             }
     340             :             else
     341             :             {
     342       56281 :                 if ( st->cng_type == LP_CNG && ( ( st->input_bwidth == NB && st->bckr_tilt_lt > 9.f ) || ( st->input_bwidth > NB && st->bckr_tilt_lt > 45.f ) ) )
     343             :                 {
     344          66 :                     st->cng_type = FD_CNG;
     345             :                 }
     346       56215 :                 else if ( st->cng_type == FD_CNG && ( ( st->input_bwidth == NB && st->bckr_tilt_lt < 2.f ) || ( st->input_bwidth > NB && st->bckr_tilt_lt < 10.f ) ) )
     347             :                 {
     348          40 :                     st->cng_type = LP_CNG;
     349             :                 }
     350             :             }
     351      404425 :             st->last_total_brate_cng = -1;
     352             :         }
     353      713393 :         else if ( st->Opt_AMR_WB )
     354             :         {
     355           0 :             st->cng_type = LP_CNG;
     356             :         }
     357             : 
     358     1117818 :         st->active_cnt++;
     359             : 
     360     1117818 :         st->active_cnt = min( st->active_cnt, 200 );
     361             :     }
     362             : 
     363             :     /*------------------------------------------------------------------------*
     364             :      * Update speech and background noise long-term energy
     365             :      *------------------------------------------------------------------------*/
     366             : 
     367     1153834 :     if ( hDtxEnc != NULL )
     368             :     {
     369      142052 :         hDtxEnc->frame_ener = 0.0f;
     370             : 
     371      142052 :         if ( st->Opt_DTX_ON )
     372             :         {
     373      138952 :             hDtxEnc->frame_ener = sum2_f( speech, L_FRAME );
     374             : 
     375             :             /* Active speech (voiced) */
     376      138952 :             if ( st->clas == VOICED_CLAS )
     377             :             {
     378       20790 :                 alpha = ALPHA_ENER_SLOW;
     379       20790 :                 if ( hDtxEnc->frame_ener > hDtxEnc->lt_ener_voiced )
     380             :                 {
     381        6523 :                     alpha = ALPHA_ENER_FAST;
     382             :                 }
     383             : 
     384       20790 :                 hDtxEnc->lt_ener_voiced = alpha * hDtxEnc->lt_ener_voiced + ( 1.0f - alpha ) * hDtxEnc->frame_ener;
     385             : 
     386       20790 :                 hDtxEnc->VarDTX_cnt_voiced++;
     387       20790 :                 if ( hDtxEnc->VarDTX_cnt_voiced > MIN_CNT )
     388             :                 {
     389       13684 :                     hDtxEnc->VarDTX_cnt_voiced = MIN_CNT;
     390             :                 }
     391             :             }
     392             : 
     393             :             /* Background noise */
     394      118162 :             else if ( !st->Opt_AMR_WB )
     395             :             {
     396      118162 :                 alpha = ALPHA_ENER_SLOW;
     397      118162 :                 if ( hDtxEnc->frame_ener < hDtxEnc->lt_ener_noise )
     398             :                 {
     399       40233 :                     alpha = ALPHA_ENER_FAST;
     400             :                 }
     401             : 
     402      118162 :                 hDtxEnc->lt_ener_noise = alpha * hDtxEnc->lt_ener_noise + ( 1.0f - alpha ) * hDtxEnc->frame_ener;
     403             : 
     404      118162 :                 hDtxEnc->VarDTX_cnt_noise++;
     405             : 
     406      118162 :                 if ( hDtxEnc->VarDTX_cnt_noise > MIN_CNT )
     407             :                 {
     408       97691 :                     hDtxEnc->VarDTX_cnt_noise = MIN_CNT;
     409             :                 }
     410             :             }
     411             :         }
     412             :     }
     413             : 
     414             :     /* Update of the SID counter */
     415     1153834 :     update_SID_cnt( hDtxEnc, st->core_brate, st->Opt_AMR_WB );
     416             : 
     417             :     /* Update encoded bandwidth */
     418     1153834 :     if ( st->Opt_DTX_ON && ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA ) )
     419             :     {
     420       36016 :         st->bwidth = st->last_bwidth;
     421       36016 :         if ( st->last_core_brate > SID_2k40 && st->last_total_brate_cng != -1 )
     422             :         {
     423        9552 :             st->bwidth = st->last_bwidth_cng;
     424             :         }
     425             : 
     426       36016 :         if ( st->Opt_RF_ON && st->total_brate == ACELP_13k20 && st->bwidth == NB )
     427             :         {
     428           0 :             st->codec_mode = MODE1;
     429           0 :             reset_rf_indices( st->hRF, st->L_frame, &st->rf_target_bits_write );
     430           0 :             st->Opt_RF_ON = 0;
     431           0 :             st->rf_mode = 0;
     432             :         }
     433             : 
     434       36016 :         if ( st->Opt_RF_ON && st->total_brate != ACELP_13k20 && st->hRF != NULL )
     435             :         {
     436           0 :             reset_rf_indices( st->hRF, st->L_frame, &st->rf_target_bits_write );
     437           0 :             st->Opt_RF_ON = 0;
     438           0 :             st->rf_mode = 0;
     439             :         }
     440             : 
     441       36016 :         if ( st->codec_mode == MODE2 )
     442             :         {
     443             :             int16_t n, bits_frame_nominal, tmpBandwidthMin;
     444             : 
     445           0 :             bits_frame_nominal = (int16_t) ( st->total_brate / FRAMES_PER_SEC );
     446           0 :             for ( n = 0; n < FRAME_SIZE_NB; n++ )
     447             :             {
     448           0 :                 if ( FrameSizeConfig[n].frame_bits == bits_frame_nominal )
     449             :                 {
     450           0 :                     break;
     451             :                 }
     452             :             }
     453           0 :             if ( n == FRAME_SIZE_NB )
     454             :             {
     455           0 :                 assert( !"Bitrate not supported: not part of EVS" );
     456             :             }
     457             : 
     458           0 :             tmpBandwidthMin = FrameSizeConfig[n].bandwidth_min;
     459             : 
     460           0 :             if ( st->rf_mode )
     461             :             {
     462           0 :                 tmpBandwidthMin = WB;
     463             :             }
     464             : 
     465           0 :             st->bwidth = check_bounds_s( st->bwidth, tmpBandwidthMin, FrameSizeConfig[n].bandwidth_max );
     466             :         }
     467             :     }
     468             : 
     469             : #ifdef DEBUG_MODE_ACELP
     470             :     {
     471             :         int16_t tmp_s;
     472             : 
     473             :         tmp_s = (int16_t) st->bckr_tilt_lt;
     474             :         dbgwrite( &( tmp_s ), sizeof( int16_t ), 1, st->L_frame, "./res/bckr_tilt.pcm" );
     475             :         tmp_s = (int16_t) st->lp_noise;
     476             :         dbgwrite( &( tmp_s ), sizeof( int16_t ), 1, st->L_frame, "./res/lp_noise.pcm" );
     477             :     }
     478             : #endif
     479             : 
     480     1153834 :     return;
     481             : }
     482             : 
     483             : 
     484             : /*---------------------------------------------------------------------*
     485             :  * update_SID_cnt()
     486             :  *
     487             :  * Update of the SID counter
     488             :  *---------------------------------------------------------------------*/
     489             : 
     490     1153834 : static void update_SID_cnt(
     491             :     DTX_ENC_HANDLE hDtxEnc,   /* i/o: common DTX handle       */
     492             :     const int32_t core_brate, /* i  : core coder core bitrate */
     493             :     const int16_t Opt_AMR_WB  /* i  : AMR BW IO mode?         */
     494             : )
     495             : {
     496             :     float EstimatedSNR, delta;
     497             : 
     498     1153834 :     if ( core_brate == SID_2k40 || core_brate == SID_1k75 || core_brate == FRAME_NO_DATA )
     499             :     {
     500             :         /* Adapt the SID interval */
     501       36016 :         if ( hDtxEnc->var_SID_rate_flag && hDtxEnc->VarDTX_cnt_voiced == MIN_CNT && hDtxEnc->VarDTX_cnt_noise == MIN_CNT )
     502             :         {
     503           0 :             EstimatedSNR = 10.0f * (float) log10( ( 0.01f + hDtxEnc->lt_ener_voiced ) / ( 0.01f + hDtxEnc->lt_ener_noise ) );
     504           0 :             if ( EstimatedSNR > SNR_H )
     505             :             {
     506           0 :                 hDtxEnc->interval_SID = INT_H;
     507             :             }
     508           0 :             else if ( EstimatedSNR < SNR_L )
     509             :             {
     510           0 :                 hDtxEnc->interval_SID = INT_L;
     511             :             }
     512             :             else
     513             :             {
     514           0 :                 hDtxEnc->interval_SID = INT_L + (int16_t) ( ( INT_H - INT_L ) * ( EstimatedSNR - SNR_L ) / ( SNR_H - SNR_L ) );
     515             :             }
     516           0 :             hDtxEnc->interval_SID = check_bounds_s( hDtxEnc->interval_SID, INT_L, INT_H );
     517             : 
     518           0 :             if ( !Opt_AMR_WB || hDtxEnc->max_SID != 3 )
     519             :             {
     520           0 :                 hDtxEnc->max_SID = hDtxEnc->interval_SID; /* change SID update rate */
     521             :             }
     522             :         }
     523             : 
     524       36016 :         if ( hDtxEnc->cnt_SID != 0 )
     525             :         {
     526             :             /* Send SID frame only if long-term energy variation is above threshold */
     527       30199 :             delta = 10.0f * (float) log10( ( 0.01f + hDtxEnc->lt_ener_noise ) / ( 0.01f + hDtxEnc->lt_ener_last_SID ) );
     528       30199 :             if ( delta < LTE_VAR && hDtxEnc->VarDTX_cnt_voiced == MIN_CNT && hDtxEnc->VarDTX_cnt_noise == MIN_CNT )
     529             :             {
     530             :                 /* Send SID frame, and reset hDtxEnc->lt_ener_noise */
     531           0 :                 hDtxEnc->lt_ener_noise = hDtxEnc->frame_ener;
     532             :             }
     533             :         }
     534             :         else
     535             :         {
     536             :             /* If SID frame was sent, update long-term energy */
     537        5817 :             hDtxEnc->lt_ener_last_SID = hDtxEnc->lt_ener_noise;
     538             :         }
     539             : 
     540       36016 :         hDtxEnc->cnt_SID++;
     541             : 
     542       36016 :         if ( hDtxEnc->var_SID_rate_flag )
     543             :         {
     544           0 :             if ( Opt_AMR_WB && hDtxEnc->max_SID == 3 && hDtxEnc->cnt_SID == 3 )
     545             :             {
     546             :                 /* set the size of CNG history buffer for averaging to 3 frames */
     547           0 :                 hDtxEnc->cng_hist_size = 3;
     548             :             }
     549           0 :             else if ( hDtxEnc->max_SID != 3 && hDtxEnc->cnt_SID == DTX_HIST_SIZE )
     550             :             {
     551             :                 /* set the size of CNG history buffer for averaging to DTX_HIST_SIZE frames */
     552             :                 /* be sure that DTX_HIST_SIZE >= INT_L */
     553           0 :                 hDtxEnc->cng_hist_size = DTX_HIST_SIZE;
     554             :             }
     555             :         }
     556             : 
     557       36016 :         if ( !hDtxEnc->var_SID_rate_flag && hDtxEnc->interval_SID > 1 )
     558             :         {
     559             :             /* set the size of CNG history buffer for averaging to interval_SID frames */
     560       36016 :             hDtxEnc->cng_hist_size = hDtxEnc->interval_SID;
     561       36016 :             if ( hDtxEnc->cng_hist_size > DTX_HIST_SIZE )
     562             :             {
     563           0 :                 hDtxEnc->cng_hist_size = DTX_HIST_SIZE;
     564             :             }
     565             :         }
     566             : 
     567       36016 :         if ( hDtxEnc->cnt_SID >= hDtxEnc->max_SID )
     568             :         {
     569             :             /* adaptive SID update interval */
     570        3915 :             hDtxEnc->max_SID = hDtxEnc->interval_SID;
     571        3915 :             hDtxEnc->cnt_SID = 0;
     572             :         }
     573             :     }
     574             : 
     575     1153834 :     return;
     576             : }
     577             : 
     578             : 
     579             : /*-------------------------------------------------------------------*
     580             :  * dtx_hangover_control()
     581             :  *
     582             :  *
     583             :  *-------------------------------------------------------------------*/
     584             : 
     585        1552 : void dtx_hangover_control(
     586             :     Encoder_State *st,     /* i/o: encoder state structure  */
     587             :     const float lsp_new[M] /* i  : current frame LSPs       */
     588             : )
     589             : {
     590             :     int16_t ptr;
     591             :     int16_t i, j, m;
     592             :     float tmp_lsp[max( DTX_HIST_SIZE, HO_HIST_SIZE ) * M];
     593             :     float tmp_enr[max( DTX_HIST_SIZE, HO_HIST_SIZE )];
     594             :     float tmp[max( DTX_HIST_SIZE, HO_HIST_SIZE ) * M];
     595             :     float enr_new;
     596             :     float weights;
     597             :     float enr_est, lsp_est[M];
     598             :     float Dlsp, Denr;
     599             :     float lsf_tmp[M];
     600             :     float C[M];
     601             :     float max_val[2];
     602             :     int16_t max_idx[2];
     603             :     float ftmp;
     604             :     float Dlsp_n2e, Denr_n2e;
     605             : 
     606        1552 :     TD_CNG_ENC_HANDLE hTdCngEnc = st->hTdCngEnc;
     607             : 
     608             :     /* get current frame exc energy in log2 */
     609        1552 :     enr_new = (float) ( log10( hTdCngEnc->ho_ener_circ[hTdCngEnc->ho_circ_ptr] ) / log10( 2.0f ) );
     610             : 
     611        1552 :     if ( enr_new < 0.0f )
     612             :     {
     613         362 :         enr_new = 0.0f;
     614             :     }
     615             : 
     616             :     /* get energies and lsps of hangover frames  */
     617        1552 :     ptr = hTdCngEnc->ho_circ_ptr - ( hTdCngEnc->burst_ho_cnt - 1 );
     618        1552 :     if ( ptr < 0 )
     619             :     {
     620         517 :         ptr += hTdCngEnc->ho_circ_size;
     621             :     }
     622             : 
     623        5883 :     for ( i = 0; i < hTdCngEnc->burst_ho_cnt - 1; i++ )
     624             :     {
     625        4331 :         mvr2r( &( hTdCngEnc->ho_lsp_circ[ptr * M] ), &( tmp_lsp[i * M] ), M );
     626        4331 :         tmp_enr[i] = hTdCngEnc->ho_ener_circ[ptr];
     627             : 
     628        4331 :         ptr++;
     629        4331 :         if ( ptr == hTdCngEnc->ho_circ_size )
     630             :         {
     631         517 :             ptr = 0;
     632             :         }
     633             :     }
     634             : 
     635             :     /* get estimated CNG energy and lsps assuming terminate hangover at current frame */
     636        1552 :     ptr = hTdCngEnc->burst_ho_cnt - 2;
     637        1552 :     enr_est = W_DTX_HO[0] * tmp_enr[ptr];
     638        1552 :     weights = W_DTX_HO[0];
     639        1552 :     mvr2r( &( tmp_lsp[ptr * M] ), tmp, M );
     640        1552 :     m = 1;
     641             : 
     642        3293 :     for ( i = 1; i < hTdCngEnc->burst_ho_cnt - 2; i++ )
     643             :     {
     644        1741 :         if ( tmp_enr[ptr - i] < tmp_enr[ptr] * BUF_H_NRG && tmp_enr[ptr - i] > tmp_enr[ptr] * BUF_L_NRG )
     645             :         {
     646         618 :             enr_est += W_DTX_HO[i] * tmp_enr[ptr - i];
     647         618 :             weights += W_DTX_HO[i];
     648         618 :             mvr2r( &tmp_lsp[( ptr - i ) * M], &tmp[m * M], M );
     649         618 :             m++;
     650             :         }
     651             :     }
     652             : 
     653        1552 :     enr_est /= weights;
     654             : 
     655        1552 :     if ( enr_est < 1.0f )
     656             :     {
     657         362 :         enr_est = 1.0f;
     658             :     }
     659             : 
     660        1552 :     Denr_n2e = (float) fabs( enr_new - log10( enr_est ) / log10( 2.0f ) );
     661             : 
     662        1552 :     if ( m < 3 )
     663             :     {
     664        1401 :         enr_est = 0.8f * enr_est + ( 1 - 0.8f ) * hTdCngEnc->ho_ener_circ[hTdCngEnc->ho_circ_ptr];
     665             :     }
     666             :     else
     667             :     {
     668         151 :         enr_est = 0.95f * enr_est + ( 1 - 0.95f ) * hTdCngEnc->ho_ener_circ[hTdCngEnc->ho_circ_ptr];
     669             :     }
     670             : 
     671        1552 :     enr_est = (float) ( log10( enr_est ) / log10( 2.0f ) );
     672             : 
     673        1552 :     if ( enr_est < 0.0f )
     674             :     {
     675         362 :         enr_est = 0.0f;
     676             :     }
     677             : 
     678        1552 :     set_f( max_val, 0.0f, 2 );
     679        1552 :     set_s( max_idx, 0, 2 );
     680             : 
     681        3722 :     for ( i = 0; i < m; i++ )
     682             :     {
     683        2170 :         if ( st->L_frame == L_FRAME )
     684             :         {
     685        1039 :             lsp2lsf( &tmp[i * M], lsf_tmp, M, INT_FS_12k8 );
     686        1039 :             ftmp = 6400.0f / ( M + 1 );
     687        1039 :             C[i] = ( 6400.0f - lsf_tmp[M - 1] - ftmp ) * ( 6400.0f - lsf_tmp[M - 1] - ftmp );
     688             :         }
     689             :         else
     690             :         {
     691        1131 :             lsp2lsf( &tmp[i * M], lsf_tmp, M, INT_FS_16k );
     692        1131 :             ftmp = 8000.0f / ( M + 1 );
     693        1131 :             C[i] = ( 8000.0f - lsf_tmp[M - 1] - ftmp ) * ( 8000.0f - lsf_tmp[M - 1] - ftmp );
     694             :         }
     695             : 
     696        2170 :         C[i] += ( lsf_tmp[0] - ftmp ) * ( lsf_tmp[0] - ftmp );
     697       34720 :         for ( j = 0; j < M - 1; j++ )
     698             :         {
     699       32550 :             C[i] += ( lsf_tmp[j + 1] - lsf_tmp[j] - ftmp ) * ( lsf_tmp[j + 1] - lsf_tmp[j] - ftmp );
     700             :         }
     701             : 
     702        2170 :         C[i] *= 0.0588235f; /* 0.0588235f = 1/(M+1) */
     703             : 
     704        2170 :         if ( C[i] > max_val[0] )
     705             :         {
     706        1806 :             max_val[1] = max_val[0];
     707        1806 :             max_idx[1] = max_idx[0];
     708        1806 :             max_val[0] = C[i];
     709        1806 :             max_idx[0] = i;
     710             :         }
     711         364 :         else if ( C[i] > max_val[1] )
     712             :         {
     713         262 :             max_val[1] = C[i];
     714         262 :             max_idx[1] = i;
     715             :         }
     716             :     }
     717             : 
     718        1552 :     if ( m == 1 )
     719             :     {
     720        1171 :         mvr2r( tmp, lsp_est, M );
     721             :     }
     722         381 :     else if ( m < 4 )
     723             :     {
     724        5491 :         for ( i = 0; i < M; i++ )
     725             :         {
     726        5168 :             lsp_est[i] = 0.0f;
     727       16992 :             for ( j = 0; j < m; j++ )
     728             :             {
     729       11824 :                 lsp_est[i] += tmp[j * M + i];
     730             :             }
     731             : 
     732        5168 :             lsp_est[i] -= tmp[max_idx[0] * M + i];
     733        5168 :             lsp_est[i] /= (float) ( m - 1 );
     734             :         }
     735             :     }
     736             :     else
     737             :     {
     738         986 :         for ( i = 0; i < M; i++ )
     739             :         {
     740         928 :             lsp_est[i] = 0.0f;
     741        5088 :             for ( j = 0; j < m; j++ )
     742             :             {
     743        4160 :                 lsp_est[i] += tmp[j * M + i];
     744             :             }
     745             : 
     746         928 :             lsp_est[i] -= ( tmp[max_idx[0] * M + i] + tmp[max_idx[1] * M + i] );
     747         928 :             lsp_est[i] /= (float) ( m - 2 );
     748             :         }
     749             :     }
     750             : 
     751        1552 :     Dlsp_n2e = 0.0f;
     752       26384 :     for ( i = 0; i < M; i++ )
     753             :     {
     754       24832 :         Dlsp_n2e += (float) fabs( lsp_new[i] - lsp_est[i] );
     755       24832 :         lsp_est[i] = 0.8f * lsp_est[i] + ( 1 - 0.8f ) * lsp_new[i];
     756             :     }
     757             : 
     758             :     /* get deviation of CNG parameters between newly estimated and current state memory */
     759        1552 :     Dlsp = 0.0f;
     760        1552 :     max_val[0] = 0.0f;
     761             : 
     762       26384 :     for ( i = 0; i < M; i++ )
     763             :     {
     764       24832 :         Dlsp += (float) fabs( st->hDtxEnc->lspCNG[i] - lsp_est[i] );
     765       24832 :         if ( fabs( st->hDtxEnc->lspCNG[i] - lsp_est[i] ) > max_val[0] )
     766             :         {
     767        9107 :             max_val[0] = (float) fabs( st->hDtxEnc->lspCNG[i] - lsp_est[i] );
     768             :         }
     769             :     }
     770        1552 :     Denr = (float) fabs( ( log10( hTdCngEnc->lp_ener + 0.1f ) / log10( 2.0f ) ) - enr_est );
     771             : 
     772             :     /* make decision if DTX hangover can be terminated */
     773        1552 :     st->hVAD->hangover_terminate_flag = 0;
     774             : 
     775        1552 :     if ( ( Dlsp < 0.4f && Denr < 1.4f && max_val[0] < 0.1f && Dlsp_n2e < 0.4f && Denr_n2e < 1.2f && st->Opt_SC_VBR ) ||
     776         581 :          ( Dlsp < 0.4f && Denr < 0.8f && max_val[0] < 0.1f && Dlsp_n2e < 0.4f && Denr_n2e < 0.8f && !st->Opt_SC_VBR ) )
     777             :     {
     778         248 :         st->hVAD->hangover_terminate_flag = 1;
     779             :     }
     780             : 
     781        1552 :     return;
     782             : }
     783             : 
     784             : 
     785             : /*-------------------------------------------------------------------*
     786             :  * td_cng_enc_init()
     787             :  *
     788             :  *
     789             :  *-------------------------------------------------------------------*/
     790             : 
     791         151 : void td_cng_enc_init(
     792             :     TD_CNG_ENC_HANDLE hTdCngEnc, /* i/o: DTX/TD CNG data handle              */
     793             :     const int16_t Opt_DTX_ON,    /* i  : flag indicating DTX operation       */
     794             :     const int16_t max_bwidth     /* i  : maximum encoded bandwidth           */
     795             : )
     796             : {
     797             : 
     798         151 :     hTdCngEnc->cng_seed = RANDOM_INITSEED;
     799         151 :     hTdCngEnc->cng_ener_seed = RANDOM_INITSEED;
     800         151 :     hTdCngEnc->cng_ener_seed1 = RANDOM_INITSEED;
     801         151 :     hTdCngEnc->lp_ener = 0.0f;
     802         151 :     hTdCngEnc->old_enr_index = -1;
     803         151 :     hTdCngEnc->Enew = 0.0f;
     804             : 
     805         151 :     hTdCngEnc->lp_sp_enr = 0.0f;
     806         151 :     hTdCngEnc->last_allow_cn_step = 0;
     807             : 
     808         151 :     hTdCngEnc->CNG_att = 0.0f;
     809             : 
     810         151 :     if ( Opt_DTX_ON )
     811             :     {
     812         148 :         hTdCngEnc->cng_hist_ptr = -1;
     813         148 :         set_f( hTdCngEnc->cng_lsp_hist, 0, DTX_HIST_SIZE * M );
     814         148 :         set_f( hTdCngEnc->cng_ener_hist, 0, DTX_HIST_SIZE );
     815         148 :         hTdCngEnc->ho_hist_ptr = -1;
     816         148 :         hTdCngEnc->ho_sid_bw = 0;
     817         148 :         set_f( hTdCngEnc->ho_lsp_hist, 0, HO_HIST_SIZE * M );
     818         148 :         set_f( hTdCngEnc->ho_ener_hist, 0, HO_HIST_SIZE );
     819         148 :         set_f( hTdCngEnc->ho_env_hist, 0, HO_HIST_SIZE * NUM_ENV_CNG );
     820         148 :         hTdCngEnc->ho_hist_size = 0;
     821         148 :         hTdCngEnc->act_cnt = 0;
     822             :     }
     823             : 
     824         151 :     set_s( hTdCngEnc->ho_16k_lsp, 0, HO_HIST_SIZE );
     825         151 :     hTdCngEnc->act_cnt2 = 0;
     826         151 :     hTdCngEnc->num_ho = 0;
     827             : 
     828         151 :     hTdCngEnc->ho_circ_ptr = -1;
     829         151 :     set_f( hTdCngEnc->ho_lsp_circ, 0, HO_HIST_SIZE * M );
     830         151 :     set_f( hTdCngEnc->ho_ener_circ, 0, HO_HIST_SIZE );
     831         151 :     set_f( hTdCngEnc->ho_env_circ, 0, HO_HIST_SIZE * NUM_ENV_CNG );
     832         151 :     hTdCngEnc->ho_circ_size = 0;
     833         151 :     hTdCngEnc->burst_ho_cnt = 0;
     834         151 :     hTdCngEnc->cng_buf_cnt = 0;
     835             : 
     836             : 
     837         151 :     set_f( hTdCngEnc->lp_env, 0.0f, 20 );
     838         151 :     set_f( hTdCngEnc->cng_res_env, 0.0f, 20 * 8 );
     839         151 :     set_f( hTdCngEnc->exc_mem, 0.0f, 24 );
     840         151 :     set_f( hTdCngEnc->exc_mem1, 0.0f, 30 );
     841         151 :     set_f( hTdCngEnc->exc_mem2, 0.0f, 30 );
     842         151 :     set_f( hTdCngEnc->old_env, 0.0f, NUM_ENV_CNG );
     843             : 
     844             :     /* SWB CNG/DTX */
     845         151 :     hTdCngEnc->last_wb_cng_ener = -6.02f;
     846         151 :     hTdCngEnc->last_shb_cng_ener = -6.02f;
     847         151 :     hTdCngEnc->mov_wb_cng_ener = -6.02f;
     848         151 :     hTdCngEnc->mov_shb_cng_ener = -6.02f;
     849         151 :     hTdCngEnc->shb_cng_ini_cnt = 1;
     850         151 :     hTdCngEnc->shb_NO_DATA_cnt = 0;
     851         151 :     hTdCngEnc->last_SID_bwidth = min( max_bwidth, SWB );
     852         151 :     hTdCngEnc->last_vad = 0;
     853         151 :     hTdCngEnc->last_idx_ener = 0;
     854             : 
     855         151 :     return;
     856             : }
     857             : 
     858             : 
     859             : /*-------------------------------------------------------------------*
     860             :  * dtx_enc_init()
     861             :  *
     862             :  * Initialize DTX parameters
     863             :  *-------------------------------------------------------------------*/
     864             : 
     865         614 : void dtx_enc_init(
     866             :     Encoder_State *st,               /* i  : Encoder state handle              */
     867             :     const int16_t var_SID_rate_flag, /* i  : flag for variable SID update rate */
     868             :     const int16_t interval_SID       /* i  : interval for SID update           */
     869             : )
     870             : {
     871             :     DTX_ENC_HANDLE hDtxEnc;
     872         614 :     hDtxEnc = st->hDtxEnc;
     873             : 
     874         614 :     hDtxEnc->first_CNG = 0;
     875         614 :     hDtxEnc->cnt_SID = 0;
     876         614 :     hDtxEnc->max_SID = 2;
     877         614 :     hDtxEnc->CNG_mode = -1;
     878         614 :     mvr2r( st->lsp_old1, hDtxEnc->lspCNG, M );
     879         614 :     hDtxEnc->VarDTX_cnt_voiced = 0;
     880         614 :     hDtxEnc->VarDTX_cnt_noise = 0;
     881         614 :     hDtxEnc->lt_ener_voiced = 0.0f;
     882         614 :     hDtxEnc->lt_ener_noise = 0.0f;
     883         614 :     hDtxEnc->frame_ener = 0.0f;
     884         614 :     hDtxEnc->lt_ener_last_SID = 0.0f;
     885         614 :     hDtxEnc->last_CNG_L_frame = L_FRAME;
     886         614 :     hDtxEnc->var_SID_rate_flag = var_SID_rate_flag;
     887         614 :     hDtxEnc->last_active_brate = ACELP_7k20;
     888         614 :     hDtxEnc->cng_cnt = 0;
     889             : 
     890         614 :     if ( hDtxEnc->var_SID_rate_flag )
     891             :     {
     892           3 :         hDtxEnc->interval_SID = 12;
     893           3 :         hDtxEnc->cng_hist_size = DTX_HIST_SIZE;
     894             :     }
     895             :     else
     896             :     {
     897         611 :         hDtxEnc->interval_SID = interval_SID;
     898         611 :         if ( hDtxEnc->interval_SID >= DTX_HIST_SIZE )
     899             :         {
     900         611 :             hDtxEnc->cng_hist_size = hDtxEnc->interval_SID;
     901             :         }
     902             :     }
     903             : 
     904         614 :     return;
     905             : }

Generated by: LCOV version 1.14