LCOV - code coverage report
Current view: top level - lib_dec - core_switching_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main @ fec202a8f89be4a2f278a9fc377bfb58b58fab11 Lines: 489 518 94.4 %
Date: 2025-09-14 08:49:17 Functions: 7 7 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 <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include <math.h>
      43             : #include "cnst.h"
      44             : #include "rom_com.h"
      45             : #include "prot.h"
      46             : #include "ivas_cnst.h"
      47             : #include "wmc_auto.h"
      48             : 
      49             : /*---------------------------------------------------------------------*
      50             :  * Local prototypes
      51             :  *---------------------------------------------------------------------*/
      52             : 
      53             : static void core_switch_lb_upsamp( Decoder_State *st, float *output );
      54             : static void smoothTransitionDtxToTcx( float synth[], const int16_t output_frame, const int16_t delay_comp );
      55             : 
      56             : /*---------------------------------------------------------------------*
      57             :  * core_switching_pre_dec()
      58             :  *
      59             :  * Preprocessing/preparation for ACELP/HQ core switching
      60             :  *---------------------------------------------------------------------*/
      61             : 
      62    35675004 : ivas_error core_switching_pre_dec(
      63             :     Decoder_State *st,                 /* i/o: decoder state structure      */
      64             :     const int16_t output_frame,        /* i  : frame length                 */
      65             :     const int32_t last_core_brate_st0, /* i  : channel 0 last core bitrate  */
      66             :     const int16_t nchan_out,           /* i  : number of output channels    */
      67             :     const int16_t last_element_mode,   /* i  : last_element_mode            */
      68             :     const int32_t last_element_brate   /* i  : last element bitrate         */
      69             : )
      70             : {
      71             :     int16_t i, oldLenClasBuff, newLenClasBuff;
      72             :     ivas_error error;
      73             :     float tmp;
      74             : 
      75    35675004 :     error = IVAS_ERR_OK;
      76             : 
      77             :     /* Codec mode switching */
      78    35675004 :     if ( st->last_codec_mode == MODE2 || ( ( st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE ) && st->element_mode > EVS_MONO ) )
      79             :     {
      80    32034177 :         mvr2r( st->mem_syn2, st->mem_syn1, M );
      81    32034177 :         set_f( st->agc_mem2, 0, 2 );
      82    32034177 :         st->mem_deemph = st->syn[M];
      83    32034177 :         st->bpf_off = 1;
      84    32034177 :         if ( st->hBPF != NULL )
      85             :         {
      86     6449790 :             set_f( st->hBPF->pst_old_syn, 0, NBPSF_PIT_MAX );
      87     6449790 :             st->hBPF->pst_mem_deemp_err = 0;
      88             :         }
      89    32034177 :         st->psf_lp_noise = st->lp_noise;
      90             : 
      91             :         /* reset old HB synthesis buffer */
      92    32034177 :         if ( st->last_L_frame == L_FRAME )
      93             :         {
      94     1226306 :             st->old_bwe_delay = NS2SA( st->output_Fs, MAX_DELAY_TBE_NS - DELAY_SWB_TBE_12k8_NS );
      95             :         }
      96             :         else
      97             :         {
      98    30807871 :             st->old_bwe_delay = NS2SA( st->output_Fs, MAX_DELAY_TBE_NS - DELAY_SWB_TBE_16k_NS );
      99             :         }
     100    32034177 :         set_f( st->hb_prev_synth_buffer, 0, NS2SA( 48000, DELAY_BWE_TOTAL_NS ) );
     101             : 
     102    32034177 :         if ( st->hBWE_TD != NULL && st->last_core != ACELP_CORE )
     103             :         {
     104             :             /* reset BWE memories */
     105     6689599 :             set_f( st->hBWE_TD->old_bwe_exc, 0, PIT16k_MAX * 2 );
     106     6689599 :             st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
     107             :         }
     108             : 
     109             :         /* reset upd_cnt */
     110    32034177 :         st->upd_cnt = MAX_UPD_CNT;
     111             : 
     112    32034177 :         st->igf = 0;
     113             : 
     114    32034177 :         if ( st->output_Fs >= 16000 && st->hBWE_zero != NULL )
     115             :         {
     116     6449750 :             hf_synth_reset( st->hBWE_zero );
     117             :         }
     118             : 
     119    32034177 :         if ( st->hBWE_FD != NULL )
     120             :         {
     121     6689770 :             set_f( st->hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
     122             :         }
     123             : 
     124    32034177 :         if ( st->hHQ_core != NULL )
     125             :         {
     126    32033873 :             set_f( st->hHQ_core->prev_env, 0, SFM_N_WB );
     127    32033873 :             set_f( st->hHQ_core->prev_normq, 0, SFM_N_WB );
     128             : 
     129    32033873 :             set_f( st->hHQ_core->last_ni_gain, 0, BANDS_MAX );
     130    32033873 :             set_f( st->hHQ_core->last_env, 0, BANDS_MAX );
     131    32033873 :             st->hHQ_core->last_max_pos_pulse = 0;
     132             : 
     133    32033873 :             if ( st->output_Fs > 16000 )
     134             :             {
     135    25452615 :                 set_f( st->hHQ_core->prev_coeff_out, 0, L_HQ_WB_BWE );
     136             :             }
     137             : 
     138             :             /* pre-echo */
     139    32033873 :             st->hHQ_core->pastpre = 0;
     140             :         }
     141             : 
     142             :         /* reset the GSC pre echo energy threshold in case of switching */
     143    32034177 :         if ( st->hGSCDec != NULL )
     144             :         {
     145     6449790 :             st->hGSCDec->Last_frame_ener = (float) MAX_32;
     146             :         }
     147             : 
     148    32034177 :         if ( st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE )
     149             :         {
     150    32034006 :             if ( st->element_mode == EVS_MONO )
     151             :             {
     152         570 :                 st->last_core = HQ_CORE;
     153         570 :                 mvr2r( st->hTcxDec->FBTCXdelayBuf, st->prev_synth_buffer, NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS - DELAY_CLDFB_NS ) );
     154             :             }
     155             : 
     156    32034006 :             if ( st->hHQ_core != NULL )
     157             :             {
     158    32033702 :                 set_f( st->hHQ_core->last_ni_gain, 0, BANDS_MAX );
     159    32033702 :                 set_f( st->hHQ_core->last_env, 0, BANDS_MAX );
     160    32033702 :                 st->hHQ_core->last_max_pos_pulse = 0;
     161             : 
     162    32033702 :                 set_s( st->hHQ_core->prev_SWB_peak_pos, 0, SPT_SHORTEN_SBNUM );
     163    32033702 :                 st->hHQ_core->prev_frm_hfe2 = 0;
     164    32033702 :                 st->hHQ_core->prev_stab_hfe2 = 0;
     165             :             }
     166             :         }
     167             : 
     168    32034177 :         if ( st->prev_bfi != 0 )
     169             :         {
     170             :             int16_t delay_comp;
     171             : 
     172             :             /*switch off Hq Voicing as it was not updated in MODE2*/
     173     2368316 :             if ( st->hHQ_core != NULL )
     174             :             {
     175     2368307 :                 st->hHQ_core->oldHqVoicing = 0;
     176     2368307 :                 st->hHQ_core->HqVoicing = 0;
     177             :             }
     178             : 
     179     2368316 :             delay_comp = NS2SA( st->output_Fs, DELAY_CLDFB_NS );
     180             : 
     181     2368316 :             if ( !st->last_con_tcx && st->last_core_bfi == ACELP_CORE && st->core == HQ_CORE )
     182             :             {
     183             :                 float *realBuffer[CLDFB_NO_COL_MAX_SWITCH], *imagBuffer[CLDFB_NO_COL_MAX_SWITCH];
     184             :                 float realBufferTmp[CLDFB_NO_COL_MAX_SWITCH][CLDFB_NO_CHANNELS_MAX], imagBufferTmp[CLDFB_NO_COL_MAX_SWITCH][CLDFB_NO_CHANNELS_MAX];
     185             : 
     186           0 :                 for ( i = 0; i < CLDFB_NO_COL_MAX_SWITCH; i++ )
     187             :                 {
     188           0 :                     set_f( realBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
     189           0 :                     set_f( imagBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
     190           0 :                     realBuffer[i] = realBufferTmp[i];
     191           0 :                     imagBuffer[i] = imagBufferTmp[i];
     192             :                 }
     193             : 
     194             :                 /* CLDFB analysis of the synthesis at internal sampling rate */
     195           0 :                 if ( ( error = cldfb_save_memory( st->cldfbAna ) ) != IVAS_ERR_OK )
     196             :                 {
     197           0 :                     return error;
     198             :                 }
     199             : 
     200           0 :                 cldfbAnalysis( st->hTcxDec->syn_Overl, realBuffer, imagBuffer, delay_comp, st->cldfbAna );
     201           0 :                 cldfb_restore_memory( st->cldfbAna );
     202             : 
     203             :                 /* CLDFB synthesis of the combined signal */
     204           0 :                 if ( ( error = cldfb_save_memory( st->cldfbSyn ) ) != IVAS_ERR_OK )
     205             :                 {
     206           0 :                     return error;
     207             :                 }
     208             : 
     209           0 :                 cldfbSynthesis( realBuffer, imagBuffer, st->hHQ_core->fer_samples, delay_comp, st->cldfbSyn );
     210           0 :                 cldfb_restore_memory( st->cldfbSyn );
     211             :             }
     212             : 
     213     2368316 :             if ( !st->last_con_tcx && st->last_core_bfi == ACELP_CORE && st->core == HQ_CORE )
     214             :             {
     215           0 :                 lerp( st->hTcxDec->syn_Overl, st->hHQ_core->fer_samples + delay_comp, output_frame / 2, st->last_L_frame / 2 );
     216             :                 /*Set to zero the remaining part*/
     217           0 :                 set_f( st->hHQ_core->fer_samples + delay_comp + output_frame / 2, 0, ( output_frame / 2 ) - delay_comp );
     218             :             }
     219             :         }
     220             : 
     221    32034177 :         st->use_acelp_preq = 0;
     222    32034177 :         st->reset_mem_AR = 0;
     223             :     }
     224             : 
     225             :     /*FEC*/
     226    35675004 :     if ( st->L_frame <= L_FRAME16k )
     227             :     {
     228    34309691 :         if ( st->last_L_frame <= L_FRAME16k && st->core != HQ_CORE )
     229             :         {
     230    14616819 :             if ( st->L_frame != st->last_L_frame )
     231             :             {
     232       59846 :                 if ( st->L_frame > st->last_L_frame )
     233             :                 {
     234       43850 :                     oldLenClasBuff = L_SYN_MEM_CLAS_ESTIM * st->last_L_frame / st->L_frame;
     235       43850 :                     newLenClasBuff = L_SYN_MEM_CLAS_ESTIM;
     236             :                 }
     237             :                 else
     238             :                 {
     239       15996 :                     oldLenClasBuff = L_SYN_MEM_CLAS_ESTIM;
     240       15996 :                     newLenClasBuff = L_SYN_MEM_CLAS_ESTIM * st->L_frame / st->last_L_frame;
     241             :                 }
     242       59846 :                 lerp( &st->mem_syn_clas_estim[L_SYN_MEM_CLAS_ESTIM - oldLenClasBuff], &st->mem_syn_clas_estim[L_SYN_MEM_CLAS_ESTIM - newLenClasBuff], newLenClasBuff, oldLenClasBuff );
     243             :             }
     244             :         }
     245             :         else
     246             :         {
     247    19692872 :             set_zero( st->mem_syn_clas_estim, L_SYN_MEM_CLAS_ESTIM );
     248             :         }
     249             :     }
     250             : 
     251             :     /* Here we only handle cases where last_ppp and last_nelp not updated when coming from CodecB or other cores
     252             :        within ACELP_CORE if switching from another bitarate to vbr, last_ppp and last_nelp is always updated in the previous frame */
     253    35675004 :     if ( st->core == ACELP_CORE && ( st->last_core != ACELP_CORE || st->last_codec_mode == MODE2 ) )
     254             :     {
     255      175744 :         st->last_ppp_mode_dec = 0;
     256      175744 :         st->last_nelp_mode_dec = 0;
     257             :     }
     258             : 
     259             :     /* Handle state reset of stat_noise_uv_mod memory */
     260    35675004 :     if ( st->core == ACELP_CORE && ( st->last_core != ACELP_CORE || st->last_codec_mode == MODE2 || st->last_total_brate <= PPP_NELP_2k80 ) )
     261             :     {
     262      681042 :         st->act_count = 3;
     263      681042 :         st->uv_count = 0;
     264             :     }
     265             : 
     266    35675004 :     if ( ( ( st->core == ACELP_CORE || st->core == AMR_WB_CORE ) && st->last_core == HQ_CORE ) /* EVS and HQ -> ACELP */ ||
     267    35670715 :          ( ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD || ( st->element_mode == IVAS_CPE_MDCT && last_element_mode == IVAS_CPE_DFT ) ) && nchan_out == 2 && st->core_brate != SID_2k40 && st->core_brate != FRAME_NO_DATA && ( last_core_brate_st0 == FRAME_NO_DATA || last_core_brate_st0 == SID_2k40 ) ) ||
     268    35669371 :          ( st->core == ACELP_CORE && st->last_L_frame > L_FRAME16k ) /* TCX @ 25.6/32 kHz -> ACELP */ )
     269             :     {
     270       23353 :         if ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD )
     271             :         {
     272       13824 :             st->hPFstat->reset = 1;
     273             :         }
     274             : 
     275       23353 :         if ( st->L_frame == L_FRAME16k )
     276             :         {
     277        9360 :             mvr2r( TRWB2_Ave, st->lsf_old, M ); /* init of LSP */
     278        9360 :             mvr2r( TRWB2_Ave, st->lsfoldbfi1, M );
     279        9360 :             mvr2r( TRWB2_Ave, st->lsfoldbfi0, M );
     280        9360 :             mvr2r( TRWB2_Ave, st->lsf_adaptive_mean, M );
     281        9360 :             lsf2lsp( st->lsf_old, st->lsp_old, M, INT_FS_16k );
     282             :         }
     283             :         else
     284             :         {
     285       13993 :             mvr2r( TRWB_Ave, st->lsf_old, M ); /* init of LSP */
     286       13993 :             mvr2r( TRWB_Ave, st->lsfoldbfi1, M );
     287       13993 :             mvr2r( TRWB_Ave, st->lsfoldbfi0, M );
     288       13993 :             mvr2r( TRWB_Ave, st->lsf_adaptive_mean, M );
     289       13993 :             lsf2lsp( st->lsf_old, st->lsp_old, M, INT_FS_12k8 );
     290             :         }
     291             : 
     292       23353 :         if ( ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD ) && nchan_out == 2 && st->core_brate > SID_2k40 && ( last_core_brate_st0 == FRAME_NO_DATA || last_core_brate_st0 == SID_2k40 ) && st->hTcxDec != NULL )
     293             :         {
     294             :             /* Last frame was Stereo CNG and the synthesis memory is outdated -- reset */
     295        1288 :             set_f( st->hTcxDec->old_syn_Overl, 0.0f, L_FRAME32k / 2 );
     296        1288 :             set_f( st->hFdCngDec->hFdCngCom->olapBufferAna, 0.0f, FFTLEN );
     297             :         }
     298             : 
     299       23353 :         set_f( st->agc_mem2, 0, 2 );
     300       23353 :         st->mem_deemph = 0;
     301       23353 :         if ( !st->last_con_tcx )
     302             :         {
     303       23239 :             set_f( st->mem_syn2, 0.0f, M );
     304             :         }
     305       23353 :         set_f( st->mem_syn1, 0.0f, M );
     306       23353 :         if ( st->hBWE_TD != NULL )
     307             :         {
     308       21763 :             st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
     309             :         }
     310             : 
     311             :         /* Reset ACELP parameters */
     312       23353 :         set_zero( st->mem_MA, M );
     313       23353 :         if ( st->sr_core == INT_FS_16k )
     314             :         {
     315        9360 :             mvr2r( GEWB2_Ave, st->mem_AR, M );
     316             :         }
     317             :         else
     318             :         {
     319       13993 :             mvr2r( GEWB_Ave, st->mem_AR, M );
     320             :         }
     321       23353 :         st->tilt_code = 0.0f;
     322       23353 :         st->gc_threshold = 0.0f;
     323       23353 :         set_f( st->dispMem, 0, 8 );
     324             : 
     325       23353 :         st->last_coder_type = GENERIC;
     326             : 
     327       23353 :         fer_energy( output_frame, UNVOICED_CLAS, st->previoussynth, -1, &st->enr_old, 1 );
     328       23353 :         st->lp_gainp = 0.0f;
     329       23353 :         st->lp_gainc = (float) sqrt( st->lp_ener );
     330             : 
     331       23353 :         st->last_voice_factor = 0;
     332       23353 :         st->Last_GSC_noisy_speech_flag = 0;
     333             : 
     334             :         /* reset CLDFB memories */
     335       23353 :         cldfb_reset_memory( st->cldfbAna );
     336       23353 :         cldfb_reset_memory( st->cldfbBPF );
     337       23353 :         cldfb_reset_memory( st->cldfbSyn );
     338             : 
     339             :         /* reset TBE memories */
     340       23353 :         if ( !st->last_con_tcx && !( ( st->last_core == HQ_CORE ) && st->element_mode > EVS_MONO ) )
     341             :         {
     342       19553 :             set_f( st->old_exc, 0, L_EXC_MEM_DEC );
     343             :         }
     344        3800 :         else if ( st->L_frame < L_FRAME16k )
     345             :         {
     346             :             /* resample from 16kHz to 12.8kHZ */
     347         135 :             synth_mem_updt2( st->L_frame, L_FRAME16k, st->old_exc, st->mem_syn_r, st->mem_syn2, NULL, DEC );
     348             :         }
     349             : 
     350       23353 :         if ( st->hBWE_TD != NULL )
     351             :         {
     352       21763 :             set_f( st->hBWE_TD->old_bwe_exc, 0, PIT16k_MAX * 2 );
     353             :         }
     354             : 
     355       23353 :         if ( st->output_Fs >= 16000 && st->hBWE_zero != NULL )
     356             :         {
     357       21625 :             hf_synth_reset( st->hBWE_zero );
     358             :         }
     359             : 
     360       23353 :         if ( st->hBWE_FD != NULL )
     361             :         {
     362       21763 :             set_f( st->hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
     363             :         }
     364             :     }
     365             : 
     366    35675004 :     if ( ( st->core == ACELP_CORE || st->core == AMR_WB_CORE ) && ( st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE ) )
     367             :     {
     368      171364 :         if ( st->hBWE_TD != NULL )
     369             :         {
     370      169214 :             st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
     371      169214 :             set_f( st->hBWE_TD->old_bwe_exc, 0, PIT16k_MAX * 2 );
     372             :         }
     373             : 
     374      171364 :         st->tilt_code = 0.0f;
     375      171364 :         st->gc_threshold = 0.0f;
     376      171364 :         set_f( st->dispMem, 0, 8 );
     377             : 
     378      171364 :         st->last_coder_type = GENERIC;
     379             : 
     380      171364 :         fer_energy( output_frame, UNVOICED_CLAS, st->previoussynth, -1, &st->enr_old, 1 );
     381      171364 :         st->lp_gainp = 0.0f;
     382      171364 :         st->lp_gainc = (float) sqrt( st->lp_ener );
     383             : 
     384      171364 :         st->last_voice_factor = 0;
     385      171364 :         st->Last_GSC_noisy_speech_flag = 0;
     386             : 
     387      171364 :         if ( st->output_Fs >= 16000 && st->hBWE_zero != NULL )
     388             :         {
     389      169154 :             hf_synth_reset( st->hBWE_zero );
     390             :         }
     391             : 
     392      171364 :         if ( st->hBWE_FD != NULL )
     393             :         {
     394      169214 :             set_f( st->hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
     395             :         }
     396             : 
     397      171364 :         if ( nchan_out == 1 && st->element_mode == IVAS_CPE_DFT && st->element_brate <= IVAS_24k4 && last_element_brate > IVAS_24k4 )
     398             :         {
     399             :             /* update cldbf state with previous frame TCX synthesis when going from a bitrate with residual coding to a bitrate without it */
     400             :             int16_t offset;
     401       11376 :             offset = st->cldfbAna->p_filter_length - st->cldfbAna->no_channels;
     402       11376 :             mvr2r( st->hTcxDec->old_synthFB + st->hTcxDec->old_synth_lenFB - offset, st->cldfbAna->cldfb_state, offset );
     403             :         }
     404             :     }
     405             : 
     406    35675004 :     if ( st->core == HQ_CORE && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE || ( ( st->element_mode != EVS_MONO ) && ( st->last_core != HQ_CORE ) ) ) )
     407             :     {
     408        9772 :         set_f( st->hHQ_core->prev_env, 0, SFM_N_WB );
     409        9772 :         set_f( st->hHQ_core->prev_normq, 0, SFM_N_WB );
     410             : 
     411        9772 :         set_f( st->hHQ_core->last_ni_gain, 0, BANDS_MAX );
     412        9772 :         set_f( st->hHQ_core->last_env, 0, BANDS_MAX );
     413        9772 :         st->hHQ_core->last_max_pos_pulse = 0;
     414             : 
     415        9772 :         set_s( st->hHQ_core->prev_SWB_peak_pos, 0, SPT_SHORTEN_SBNUM );
     416        9772 :         st->hHQ_core->prev_frm_hfe2 = 0;
     417        9772 :         st->hHQ_core->prev_stab_hfe2 = 0;
     418        9772 :         if ( st->output_Fs > 16000 )
     419             :         {
     420        8041 :             set_f( st->hHQ_core->prev_coeff_out, 0, L_HQ_WB_BWE );
     421             :         }
     422             : 
     423        9772 :         if ( st->element_mode != EVS_MONO )
     424             :         {
     425             :             /* Estimate mem_env_delta to reinit env_stab */
     426        9384 :             tmp = max( 0, ENV_STAB_EST1 + ( ENV_STAB_EST2 * st->stab_fac_smooth_lt ) + ( ENV_STAB_EST3 * st->log_energy_diff_lt ) );
     427        9384 :             st->hHQ_core->mem_env_delta = (int16_t) min( MAX16B, (int32_t) ( tmp * ( 1 << 12 ) ) ); /* Convert to Q12 and handle saturation */
     428             : 
     429        9384 :             if ( st->last_core != TCX_20_CORE && st->last_core != TCX_10_CORE )
     430             :             {
     431        3073 :                 set_f( st->hHQ_core->old_out, 0, output_frame );
     432        3073 :                 set_f( st->hHQ_core->old_outLB, 0, L_FRAME16k );
     433             :             }
     434             : 
     435        9384 :             st->hHQ_core->no_att_hangover = 0;
     436        9384 :             st->hHQ_core->energy_lt = 300.0f;
     437             : 
     438        9384 :             set_s( st->hHQ_core->old_is_transient, 0, 3 );
     439        9384 :             set_f( st->hHQ_core->prev_noise_level, 0.0f, 2 );
     440        9384 :             st->hHQ_core->prev_R = 0;
     441        9384 :             set_s( st->hHQ_core->mem_norm + 1, 39, SFM_N_ENV_STAB - 1 );
     442        9384 :             st->hHQ_core->prev_hqswb_clas = HQ_NORMAL;
     443        9384 :             st->hHQ_core->prev_ni_ratio = 0.5f;
     444        9384 :             set_f( st->hHQ_core->prev_En_sb, 0.0f, NB_SWB_SUBBANDS );
     445             :         }
     446             :         else
     447             :         {
     448         388 :             set_f( st->hHQ_core->old_out, 0, output_frame );
     449         388 :             set_f( st->hHQ_core->old_outLB, 0, L_FRAME16k );
     450             :         }
     451             :     }
     452             : 
     453             :     /* handle switching cases where preecho_sb was not called in the last frame (memory not up to date) */
     454    35675004 :     if ( st->hHQ_core != NULL )
     455             :     {
     456    35635870 :         st->hHQ_core->pastpre--;
     457    35635870 :         if ( st->hHQ_core->pastpre <= 0 )
     458             :         {
     459    35546715 :             reset_preecho_dec( st->hHQ_core );
     460             :         }
     461             :     }
     462             : 
     463    35675004 :     if ( st->core_brate == FRAME_NO_DATA )
     464             :     {
     465      141941 :         st->VAD = 0;
     466      141941 :         st->m_frame_type = ZERO_FRAME;
     467             :     }
     468    35533063 :     else if ( st->core_brate == SID_2k40 || st->core_brate == SID_1k75 )
     469             :     {
     470       22158 :         st->VAD = 0;
     471       22158 :         st->m_frame_type = SID_FRAME;
     472             :     }
     473             :     else
     474             :     {
     475    35510905 :         st->VAD = 1;
     476    35510905 :         st->m_frame_type = ACTIVE_FRAME;
     477             :     }
     478             : 
     479             :     /*switch on CNA on active frames*/
     480    35675004 :     if ( st->element_mode == EVS_MONO ) /* for IVAS modes, st->flag_cna is set earlier */
     481             :     {
     482       65864 :         if ( st->VAD && ( ( st->core != AMR_WB_CORE && st->total_brate <= CNA_MAX_BRATE ) || ( st->core == AMR_WB_CORE && st->total_brate <= ACELP_8k85 ) ) )
     483             :         {
     484       30384 :             st->flag_cna = 1;
     485             :         }
     486       35480 :         else if ( st->VAD || ( ( st->cng_type == FD_CNG ) && ( st->L_frame == L_FRAME16k ) ) )
     487             :         {
     488       32560 :             st->flag_cna = 0;
     489             :         }
     490             :     }
     491             : 
     492    35675004 :     if ( st->core == AMR_WB_CORE )
     493             :     {
     494        7295 :         st->cng_type = LP_CNG;
     495             :     }
     496             : 
     497             :     /* Reconfigure CNG */
     498    35675004 :     if ( st->hFdCngDec && ( ( st->last_L_frame != st->L_frame ) || ( st->hFdCngDec->hFdCngCom->frameSize != st->L_frame ) || st->ini_frame == 0 || st->bwidth != st->last_bwidth ) )
     499             :     {
     500             :         /* || st->last_core == AMR_WB_CORE || st->last_codec_mode == MODE2)){*/
     501    17037357 :         if ( st->core != AMR_WB_CORE )
     502             :         {
     503    17037250 :             configureFdCngDec( st->hFdCngDec, st->bwidth, st->rf_flag == 1 && st->total_brate == ACELP_13k20 ? ACELP_9k60 : st->total_brate, st->L_frame, st->last_L_frame, st->element_mode );
     504             :         }
     505             :         else
     506             :         {
     507         107 :             configureFdCngDec( st->hFdCngDec, WB, ACELP_8k00, st->L_frame, st->last_L_frame, st->element_mode );
     508             : 
     509         107 :             if ( st->VAD )
     510             :             {
     511         107 :                 st->hFdCngDec->hFdCngCom->CngBitrate = st->total_brate;
     512             :             }
     513             :         }
     514    17037357 :         if ( st->last_L_frame != st->L_frame && st->L_frame <= L_FRAME16k && st->last_L_frame <= L_FRAME16k )
     515             :         {
     516       59643 :             if ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD )
     517             :             {
     518        9192 :                 lerp( st->hFdCngDec->hFdCngCom->olapBufferAna + st->last_L_frame, st->hFdCngDec->hFdCngCom->olapBufferAna + st->L_frame, st->L_frame, st->last_L_frame );
     519             :             }
     520             : 
     521       59643 :             lerp( st->hFdCngDec->hFdCngCom->olapBufferSynth2, st->hFdCngDec->hFdCngCom->olapBufferSynth2, st->L_frame * 2, st->last_L_frame * 2 );
     522             : 
     523       59643 :             if ( st->total_brate <= SID_2k40 && st->last_total_brate <= SID_2k40 )
     524             :             {
     525         157 :                 lerp( st->hFdCngDec->hFdCngCom->olapBufferSynth, st->hFdCngDec->hFdCngCom->olapBufferSynth, st->L_frame * 2, st->last_L_frame * 2 );
     526             : 
     527         157 :                 if ( st->L_frame == L_FRAME )
     528             :                 {
     529       45144 :                     for ( i = 0; i < st->L_frame * 2; i++ )
     530             :                     {
     531       45056 :                         st->hFdCngDec->hFdCngCom->olapBufferSynth[i] = st->hFdCngDec->hFdCngCom->olapBufferSynth[i] * 0.6250f;
     532             :                     }
     533             :                 }
     534             :                 else
     535             :                 {
     536       44229 :                     for ( i = 0; i < st->L_frame * 2; i++ )
     537             :                     {
     538       44160 :                         st->hFdCngDec->hFdCngCom->olapBufferSynth[i] = st->hFdCngDec->hFdCngCom->olapBufferSynth[i] * 1.6f;
     539             :                     }
     540             :                 }
     541             :             }
     542             :         }
     543             :     }
     544             : 
     545    35675004 :     return error;
     546             : }
     547             : 
     548             : 
     549             : /*---------------------------------------------------------------------*
     550             :  * core_switching_post_dec()
     551             :  *
     552             :  * Postprocessing for ACELP/HQ core switching
     553             :  *---------------------------------------------------------------------*/
     554             : 
     555    35667709 : ivas_error core_switching_post_dec(
     556             :     Decoder_State *st,                   /* i/o: decoder state structure             */
     557             :     float *synth,                        /* i/o: output synthesis                    */
     558             :     float *output,                       /* i/o: LB synth/upsampled LB synth         */
     559             :     float output_mem[],                  /* i  : OLA memory from last TCX/HQ frame   */
     560             :     const int16_t use_cldfb_for_dft,     /* i  : flag to use of CLDFB for DFT Stereo */
     561             :     const int16_t output_frame,          /* i  : frame length                        */
     562             :     const int16_t core_switching_flag,   /* i  : ACELP->HQ switching flag            */
     563             :     const int16_t sba_dirac_stereo_flag, /* i  : signal stereo output for SBA DirAC  */
     564             :     const int16_t nchan_out,             /* i  : number of output channels           */
     565             :     const int16_t last_element_mode      /* i  : element mode of previous frame      */
     566             : )
     567             : {
     568             :     int16_t i, delay_comp, delta;
     569             :     float tmpF;
     570             :     float tmpDelta;
     571             :     float synth_subfr_out[SWITCH_MAX_GAP], synth_subfr_bwe[SWITCH_MAX_GAP];
     572             :     float mem_synth[NS2SA( 16000, DELAY_CLDFB_NS ) + 2];
     573             :     int16_t nZeros;
     574             :     int16_t offset;
     575             :     ivas_error error;
     576             : 
     577    35667709 :     error = IVAS_ERR_OK;
     578             : 
     579    35667709 :     if ( st->core == ACELP_CORE && st->bfi && st->hHQ_core != NULL && !st->con_tcx )
     580             :     {
     581      231457 :         if ( ( error = acelp_core_switch_dec_bfi( st ) ) != IVAS_ERR_OK )
     582             :         {
     583           0 :             return error;
     584             :         }
     585             :     }
     586             : 
     587             :     /* set multiplication factor according to the sampling rate */
     588    35667709 :     delta = 1;
     589    35667709 :     if ( output_frame == L_FRAME16k )
     590             :     {
     591     7419624 :         delta = 2;
     592             :     }
     593    28248085 :     else if ( output_frame == L_FRAME32k )
     594             :     {
     595     9714870 :         delta = 4;
     596             :     }
     597    18533215 :     else if ( output_frame == L_FRAME48k )
     598             :     {
     599    18526508 :         delta = 6;
     600             :     }
     601             : 
     602             :     /* set delay compensation between HQ synthesis and ACELP synthesis */
     603    35667709 :     delay_comp = delta * HQ_DELAY_COMP;
     604             : 
     605             :     /* Core switching done in DFT domain afterward*/
     606    35667709 :     if ( ( st->element_mode != IVAS_CPE_DFT || use_cldfb_for_dft ) && ( !sba_dirac_stereo_flag || ( sba_dirac_stereo_flag && st->core_brate == SID_2k40 && st->cng_type == FD_CNG ) ) )
     607             :     {
     608    34218154 :         if ( st->core == HQ_CORE || st->core == TCX_20_CORE || st->core == TCX_10_CORE || ( st->core == ACELP_CORE && st->bfi == 1 && st->con_tcx == 1 ) )
     609             :         {
     610    31209595 :             st->use_acelp_preq = 0;
     611    31209595 :             if ( st->hBWE_FD != NULL )
     612             :             {
     613     6421361 :                 st->hBWE_FD->mem_deemph_old_syn = 0.0f;
     614             :             }
     615             : 
     616    31209595 :             if ( st->element_mode == EVS_MONO && st->core == HQ_CORE ) /* ACELP->HQ-CORE */
     617             :             {
     618       10966 :                 if ( core_switching_flag && st->last_L_frame == st->last_L_frame_ori && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE ) )
     619             :                 {
     620         385 :                     if ( ( error = acelp_core_switch_dec( st, synth_subfr_out, synth_subfr_bwe, output_frame, core_switching_flag, mem_synth, nchan_out ) ) != IVAS_ERR_OK )
     621             :                     {
     622           0 :                         return error;
     623             :                     }
     624             :                 }
     625             : 
     626       10966 :                 if ( core_switching_flag && st->last_core == HQ_CORE && st->prev_bfi )
     627             :                 {
     628           0 :                     mvr2r( st->delay_buf_out, synth_subfr_out, delay_comp );
     629             :                 }
     630             :             }
     631             : 
     632             :             /* delay HQ synthesis to synchronize with ACELP synthesis */
     633    31209595 :             delay_signal( synth, output_frame, st->delay_buf_out, delay_comp );
     634             : 
     635    31209595 :             if ( st->element_mode == EVS_MONO && st->core == HQ_CORE ) /* ACELP->HQ-CORE */
     636             :             {
     637       10966 :                 if ( core_switching_flag && st->last_L_frame == st->last_L_frame_ori && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE ) )
     638             :                 {
     639         385 :                     core_switching_OLA( mem_synth, st->last_L_frame, st->output_Fs, synth, synth_subfr_out, synth_subfr_bwe, output_frame, st->bwidth );
     640             :                 }
     641       10581 :                 else if ( core_switching_flag && st->last_core == HQ_CORE && st->prev_bfi ) /* HQ | ACELP | TRANSITION  with ACELP frame lost */
     642             :                 {
     643             :                     /* Overlap between old->out[] (stocked in st->fer_samples[]) and good HQ frame on L/2 */
     644           0 :                     nZeros = (int16_t) ( NS2SA( st->output_Fs, N_ZERO_MDCT_NS ) );
     645           0 :                     tmpDelta = 1.0f / (float) ( output_frame >> 1 );
     646           0 :                     for ( i = 0; i < ( output_frame >> 1 ); i++ )
     647             :                     {
     648           0 :                         tmpF = (float) i * tmpDelta;
     649           0 :                         synth[i + delay_comp] = ( 1 - tmpF ) * st->hHQ_core->fer_samples[i + nZeros] + synth[i + delay_comp] * tmpF;
     650             :                     }
     651             :                 }
     652       10581 :                 else if ( ( !core_switching_flag && st->core == HQ_CORE && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE ) ) || /* ACELP | TRANSITION | HQ with TRANSITION lost */
     653           0 :                           ( core_switching_flag && st->prev_bfi && st->last_L_frame != st->last_L_frame_ori ) )                                 /* ACELP@12k8 | ACELP@16k | TRANSITION with ACELP@16k lost */
     654             :                 {
     655             :                     /* Overlap between CELP estimation (BFI) and good HQ frame on L/2 */
     656           3 :                     tmpDelta = 1.0f / (float) ( output_frame >> 1 );
     657         563 :                     for ( i = 0; i < ( output_frame >> 1 ); i++ )
     658             :                     {
     659         560 :                         tmpF = (float) i * tmpDelta;
     660         560 :                         synth[i] = synth[i] * tmpF + ( 1 - tmpF ) * st->hHQ_core->fer_samples[i];
     661             :                     }
     662             :                 }
     663             :             }
     664    31198629 :             else if ( ( ( st->last_core == ACELP_CORE || st->last_core_bfi == ACELP_CORE ) && !( st->prev_bfi == 1 && st->last_con_tcx == 1 ) ) || st->last_core == AMR_WB_CORE ) /*ACELP->TCX/HQ*/
     665             :             {
     666             :                 /* if this is first active MDCT-Stereo frame after a CNG frame and output format is mono DMX, this should only be done for the zero-th channel, the other one will simply be copied over after this function */
     667      202458 :                 if ( ( ( st->last_core_brate != SID_2k40 && st->last_core_brate != FRAME_NO_DATA ) || ( st->element_mode != IVAS_CPE_DFT && st->element_mode != IVAS_CPE_TD ) || nchan_out == 1 ) && !( st->element_mode == IVAS_CPE_MDCT && st->idchan == 1 && ( nchan_out == 1 || last_element_mode == IVAS_CPE_DFT ) ) )
     668             :                 {
     669      191581 :                     core_switch_lb_upsamp( st, output );
     670             :                 }
     671             : 
     672      202458 :                 mvr2r( st->previoussynth, synth, delay_comp );
     673             : 
     674             :                 /* Overlap between TCX-LB and TCX-FB*/
     675      202458 :                 tmpDelta = NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS );
     676    17806762 :                 for ( i = 0; i < tmpDelta; i++ )
     677             :                 {
     678    17604304 :                     synth[i + delay_comp] = ( synth[i + delay_comp] * i + ( tmpDelta - i ) * st->previoussynth[i + delay_comp] ) / tmpDelta;
     679             :                 }
     680             : 
     681      202458 :                 if ( ( st->element_mode == IVAS_CPE_MDCT || ( st->is_ism_format && st->core == TCX_20_CORE /* <- means TCX in general, TCX10 is forbidden after ACELP */ ) ) && st->last_core_brate <= SID_2k40 && st->core_brate > SID_2k40 )
     682             :                 {
     683             :                     /* smooth transitions to avoid pops in car noise items */
     684        4932 :                     smoothTransitionDtxToTcx( synth, output_frame, delay_comp );
     685             :                 }
     686             : 
     687             :                 /* Reset memories of CLDFBs */
     688      202458 :                 if ( st->cldfbAna != NULL )
     689             :                 {
     690      202458 :                     if ( st->cldfbAna->no_channels * st->cldfbAna->no_col != st->L_frame )
     691             :                     {
     692       10818 :                         configureCldfb( st->cldfbAna, st->L_frame * FRAMES_PER_SEC );
     693       10818 :                         configureCldfb( st->cldfbBPF, min( 16000, st->L_frame * FRAMES_PER_SEC ) );
     694             :                     }
     695             : 
     696      202458 :                     cldfb_reset_memory( st->cldfbAna );
     697      202458 :                     cldfb_reset_memory( st->cldfbBPF );
     698             :                 }
     699      202458 :                 cldfb_reset_memory( st->cldfbSyn );
     700             : 
     701             :                 /* Update memories for CLDFB ana for eventual next ACELP frame */
     702      202458 :                 if ( st->cldfbAna != NULL )
     703             :                 {
     704      202458 :                     delta = st->cldfbAna->no_channels;
     705      202458 :                     offset = st->cldfbAna->p_filter_length - st->cldfbAna->no_channels;
     706     4422870 :                     for ( i = 0; i < delta; i++ )
     707             :                     {
     708     4220412 :                         st->cldfbAna->cldfb_state[offset - delta + i] =
     709     4220412 :                             output[st->L_frame - delta + i] * ( (float) ( i + 1 ) ) / ( (float) delta );
     710             :                     }
     711             :                 }
     712             :             }
     713    30996171 :             else if ( st->element_mode != EVS_MONO )
     714             :             {
     715             :                 /* Reset memories of CLDFBs */
     716    30996171 :                 if ( st->cldfbAna != NULL )
     717             :                 {
     718     6868949 :                     if ( st->cldfbAna->no_channels * st->cldfbAna->no_col != st->L_frame )
     719             :                     {
     720       95418 :                         configureCldfb( st->cldfbAna, st->L_frame * FRAMES_PER_SEC );
     721       95418 :                         configureCldfb( st->cldfbBPF, min( 16000, st->L_frame * FRAMES_PER_SEC ) );
     722             :                     }
     723             : 
     724     6868949 :                     cldfb_reset_memory( st->cldfbAna );
     725     6868949 :                     cldfb_reset_memory( st->cldfbBPF );
     726             :                 }
     727             : 
     728    30996171 :                 if ( st->cldfbSyn != NULL )
     729             :                 {
     730    30996171 :                     cldfb_reset_memory( st->cldfbSyn );
     731             :                 }
     732             : 
     733             :                 /* Update memories for CLDFB ana for eventual next ACELP frame */
     734             :                 /* Analysis CLDF memory is fed with ramped signal for last slot */
     735    30996171 :                 if ( st->cldfbAna != NULL )
     736             :                 {
     737     6868949 :                     delta = st->cldfbAna->no_channels;
     738     6868949 :                     offset = st->cldfbAna->p_filter_length - st->cldfbAna->no_channels;
     739   184651153 :                     for ( i = 0; i < delta; i++ )
     740             :                     {
     741   177782204 :                         st->cldfbAna->cldfb_state[offset - delta + i] =
     742   177782204 :                             output[st->L_frame - delta + i] * ( (float) ( i + 1 ) ) / ( (float) delta );
     743             :                     }
     744             :                 }
     745             :             }
     746             : 
     747    31209595 :             if ( st->hBWE_TD != NULL )
     748             :             {
     749     6421361 :                 st->hBWE_TD->bwe_non_lin_prev_scale = 0.0;
     750             :             }
     751             : 
     752    31209595 :             if ( st->hHQ_core != NULL && !( inner_frame_tbl[st->bwidth] == L_FRAME16k && st->core_brate <= HQ_32k ) )
     753             :             {
     754    30441016 :                 set_f( st->hHQ_core->prev_env, 0, SFM_N_WB );
     755    30441016 :                 set_f( st->hHQ_core->prev_normq, 0, SFM_N_WB );
     756             :             }
     757             : 
     758    31209595 :             mvr2r( synth, st->previoussynth, output_frame );
     759             : 
     760             :             /*Set post-filtering flag to zero*/
     761    31209595 :             if ( st->hBPF != NULL )
     762             :             {
     763     6162103 :                 st->hPFstat->on = 0;
     764             :             }
     765             :         }
     766             :         else
     767             :         {
     768             :             /* MDCT to ACELP transition */
     769     3008559 :             if ( st->last_core == HQ_CORE || st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE )
     770             :             {
     771      157179 :                 nZeros = (int16_t) ( NS2SA( st->output_Fs, N_ZERO_MDCT_NS ) );
     772      157179 :                 mvr2r( st->delay_buf_out, synth, delay_comp ); /* copy the HQ/ACELP delay synchronization buffer at the beginning of ACELP frame */
     773             : 
     774      157179 :                 if ( st->prev_bfi && st->hHQ_core != NULL && st->hHQ_core->HqVoicing && st->last_core == HQ_CORE )
     775             :                 {
     776           0 :                     mvr2r( st->hHQ_core->fer_samples, st->hHQ_core->old_out + nZeros, NS2SA( st->output_Fs, 3000000 ) );
     777             :                 }
     778             : 
     779      157179 :                 tmpF = 1.0f / (float) NS2SA( st->output_Fs, 3000000 );
     780             : 
     781      157179 :                 if ( st->element_mode == IVAS_CPE_TD && st->hHQ_core == NULL )
     782             :                 {
     783       38800 :                     for ( i = 0; i < NS2SA( st->output_Fs, 3000000 ); i++ )
     784             :                     {
     785       38496 :                         synth[i + delay_comp] = ( 1 - tmpF * (float) i ) * output_mem[i] + tmpF * (float) i * synth[i + delay_comp];
     786             :                     }
     787             :                 }
     788      156875 :                 else if ( st->element_mode == IVAS_CPE_MDCT && st->core_brate <= SID_2k40 && st->prev_bfi )
     789             :                 {
     790       14240 :                     for ( i = 0; i < NS2SA( st->output_Fs, 3000000 ); i++ )
     791             :                     {
     792       14112 :                         synth[i + delay_comp] = ( 1 - tmpF * (float) i ) * st->hHQ_core->old_out[i + nZeros] * st->hTcxDec->conceal_eof_gain + tmpF * (float) i * synth[i + delay_comp];
     793             :                     }
     794             :                 }
     795             :                 else
     796             :                 {
     797    17765739 :                     for ( i = 0; i < NS2SA( st->output_Fs, 3000000 ); i++ )
     798             :                     {
     799    17608992 :                         synth[i + delay_comp] = ( 1 - tmpF * (float) i ) * st->hHQ_core->old_out[i + nZeros] + tmpF * (float) i * synth[i + delay_comp];
     800             :                     }
     801             :                 }
     802             :             }
     803             : 
     804     3008559 :             set_f( st->delay_buf_out, 0, HQ_DELTA_MAX * HQ_DELAY_COMP );
     805     3008559 :             if ( st->hHQ_core != NULL )
     806             :             {
     807     2969425 :                 st->hHQ_core->oldHqVoicing = 0;
     808             :             }
     809             :         }
     810             :     }
     811             :     else
     812             :     {
     813             :         /* memory update needed for DFT stereo -> TD stereo switching */
     814     1449555 :         mvr2r( synth + output_frame - delay_comp, st->delay_buf_out, delay_comp );
     815             :     }
     816             : 
     817             :     /* reset SWB BWE buffers */
     818    35667709 :     if ( st->bws_cnt == 0 || ( st->bws_cnt > 0 && st->coder_type != INACTIVE && st->coder_type != AUDIO ) )
     819             :     {
     820    35585316 :         st->attenu1 = 0.1f;
     821             :     }
     822             : 
     823    35667709 :     if ( st->hBWE_FD != NULL &&
     824    10269159 :          ( ( st->last_extl != SWB_BWE && st->extl == SWB_BWE ) || ( st->last_extl != FB_BWE && st->extl == FB_BWE ) ||
     825    10238345 :            ( ( st->last_core == HQ_CORE || st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE || st->last_extl == SWB_TBE ) && st->extl < 0 && st->core == ACELP_CORE ) || ( st->last_core == ACELP_CORE && st->core == ACELP_CORE && ( ( st->prev_coder_type != INACTIVE && st->coder_type != INACTIVE ) || ( st->prev_coder_type != AUDIO && st->coder_type == AUDIO ) ) && st->bws_cnt > 0 ) ) )
     826             :     {
     827       61816 :         set_f( st->hBWE_FD->old_wtda_swb, 0, output_frame );
     828             : 
     829       61816 :         if ( st->last_extl != WB_BWE )
     830             :         {
     831       60678 :             st->hBWE_FD->prev_mode = NORMAL;
     832             :         }
     833             : 
     834       61816 :         st->hBWE_FD->prev_Energy = 0.0f;
     835       61816 :         st->hBWE_FD->prev_L_swb_norm = 8;
     836       61816 :         st->hBWE_FD->prev_frica_flag = 0;
     837       61816 :         set_f( st->hBWE_FD->mem_imdct, 0, L_FRAME48k );
     838       61816 :         st->hBWE_FD->prev_td_energy = 0.0f;
     839       61816 :         st->hBWE_FD->prev_weight = 0.2f;
     840       61816 :         st->hBWE_FD->prev_fb_ener_adjust = 0.0f;
     841             :     }
     842             : 
     843             :     /* reset WB BWE buffers */
     844    35667709 :     if ( st->last_extl != WB_BWE && st->extl == WB_BWE && st->hBWE_FD != NULL )
     845             :     {
     846       13435 :         set_f( st->hBWE_FD->old_wtda_swb, 0, output_frame );
     847             : 
     848       13435 :         if ( st->last_extl != SWB_BWE && st->last_extl != FB_BWE )
     849             :         {
     850       13038 :             st->hBWE_FD->prev_mode = NORMAL;
     851             :         }
     852             : 
     853       13435 :         st->hBWE_FD->prev_Energy_wb = 0.0f;
     854       13435 :         st->hBWE_FD->prev_L_swb_norm = 8;
     855       13435 :         set_f( st->hBWE_FD->mem_imdct, 0, L_FRAME48k );
     856       13435 :         st->hBWE_FD->prev_flag = 0;
     857             :     }
     858             : 
     859             :     /* reset TBE buffers */
     860    35667709 :     if ( st->hBWE_TD != NULL )
     861             :     {
     862             :         /* reset SWB TBE buffers */
     863    10269159 :         if ( ( ( st->extl == SWB_TBE || st->extl == FB_TBE || st->extl == SWB_CNG ) &&
     864     2045458 :                ( st->L_frame != st->last_L_frame || ( st->last_extl != SWB_TBE && st->last_extl != FB_TBE && st->last_core != TCX_20_CORE && st->last_core != TCX_10_CORE ) || st->last_core == HQ_CORE ) ) ||
     865    10141074 :              ( st->bwidth < st->last_bwidth && st->last_extl != SWB_TBE ) || st->old_ppp_mode || ( ( st->prev_coder_type == AUDIO || st->prev_coder_type == INACTIVE ) && st->bws_cnt > 0 ) || ( st->bws_cnt == 0 && st->prev_bws_cnt == N_WS2N_FRAMES ) )
     866             :         {
     867      225666 :             swb_tbe_reset( st->hBWE_TD->mem_csfilt, st->hBWE_TD->mem_genSHBexc_filt_down_shb, st->hBWE_TD->state_lpc_syn, st->hBWE_TD->syn_overlap, st->hBWE_TD->state_syn_shbexc, &( st->hBWE_TD->tbe_demph ), &( st->hBWE_TD->tbe_premph ), st->hBWE_TD->mem_stp_swb, &( st->hBWE_TD->gain_prec_swb ) );
     868             : 
     869             :             /* reset GainShape delay for SWB TBE FEC */
     870      225666 :             set_f( st->hBWE_TD->GainShape_Delay, 0, NUM_SHB_SUBFR / 2 );
     871             : 
     872      225666 :             swb_tbe_reset_synth( st->hBWE_TD->genSHBsynth_Hilbert_Mem, st->hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local );
     873             : 
     874      225666 :             if ( output_frame == L_FRAME16k )
     875             :             {
     876        9296 :                 set_f( st->hBWE_TD->mem_resamp_HB_32k, 0, 2 * ALLPASSSECTIONS_STEEP + 1 ); /* reset in case that SWB TBE layer is transmitted, but the output is 16kHz sampled */
     877             :             }
     878             : 
     879      225666 :             set_f( st->hBWE_TD->int_3_over_2_tbemem_dec, 0.0f, INTERP_3_2_MEM_LEN );
     880      225666 :             st->hBWE_TD->prev_pow_exc16kWhtnd = 1.0f;
     881      225666 :             st->hBWE_TD->prev_mix_factor = 1.0f;
     882             :         }
     883    10043493 :         else if ( ( st->extl == SWB_TBE || st->extl == FB_TBE ) && ( ( st->element_mode == IVAS_CPE_TD && st->last_extl != SWB_TBE && st->last_extl != FB_TBE ) || ( st->element_mode != IVAS_CPE_TD && st->last_total_brate != st->total_brate ) || ( st->last_bwidth != st->bwidth ) || ( st->last_codec_mode != MODE1 ) || ( st->rf_flag != st->rf_flag_last ) ) )
     884             :         {
     885     1399549 :             set_f( st->hBWE_TD->state_lpc_syn, 0.0f, LPC_SHB_ORDER );
     886     1399549 :             set_f( st->hBWE_TD->state_syn_shbexc, 0.0f, L_SHB_LAHEAD );
     887     1399549 :             set_f( st->hBWE_TD->mem_stp_swb, 0.0f, LPC_SHB_ORDER );
     888     1399549 :             set_f( st->hBWE_TD->mem_zero_swb, 0, LPC_SHB_ORDER );
     889     1399549 :             st->hBWE_TD->gain_prec_swb = 1.0f;
     890             :         }
     891     8643944 :         else if ( st->hBWE_TD != NULL && ( st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE ) )
     892             :         {
     893     6587911 :             TBEreset_dec( st );
     894             :         }
     895             : 
     896             :         /* reset FB TBE buffers */
     897    10269159 :         if ( ( st->L_frame != st->last_L_frame || st->last_extl != FB_TBE ) && st->extl == FB_TBE )
     898             :         {
     899       49789 :             set_f( st->hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
     900       49789 :             st->hBWE_TD->fb_tbe_demph = 0;
     901       49789 :             fb_tbe_reset_synth( st->hBWE_TD->fbbwe_hpf_mem, &st->hBWE_TD->prev_fbbwe_ratio );
     902             :         }
     903             : 
     904             :         /* reset WB TBE buffers */
     905    10269159 :         if ( st->last_extl != WB_TBE && st->extl == WB_TBE && st->last_core != TCX_20_CORE && st->last_core != TCX_10_CORE )
     906             :         {
     907        4773 :             wb_tbe_extras_reset( st->hBWE_TD->mem_genSHBexc_filt_down_wb2, st->hBWE_TD->mem_genSHBexc_filt_down_wb3 );
     908        4773 :             wb_tbe_extras_reset_synth( st->hBWE_TD->state_lsyn_filt_shb, st->hBWE_TD->state_lsyn_filt_dwn_shb, st->hBWE_TD->mem_resamp_HB );
     909             : 
     910        4773 :             set_f( st->hBWE_TD->state_syn_shbexc, 0, L_SHB_LAHEAD / 4 );
     911        4773 :             set_f( st->hBWE_TD->syn_overlap, 0, L_SHB_LAHEAD );
     912        4773 :             set_f( st->hBWE_TD->mem_csfilt, 0, 2 );
     913             :         }
     914             :     }
     915             : 
     916             :     /* Interp_3_2 CNG buffers reset */
     917    35667709 :     if ( st->hTdCngDec != NULL && st->output_Fs == 48000 && ( st->last_core_brate > SID_2k40 ) && ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 ) && st->hTdCngDec != NULL )
     918             :     {
     919        3618 :         set_f( st->hTdCngDec->interpol_3_2_cng_dec, 0.0f, INTERP_3_2_MEM_LEN );
     920             :     }
     921             : 
     922    35667709 :     return error;
     923             : }
     924             : 
     925             : 
     926             : /*---------------------------------------------------------------------*
     927             :  * core_switching_hq_prepare_dec()
     928             :  *
     929             :  * Preprocessing in the first HQ frame after ACELP frame
     930             :  * Modify bit allocation for HQ core by removing ACELP subframe budget
     931             :  *---------------------------------------------------------------------*/
     932             : 
     933         385 : void core_switching_hq_prepare_dec(
     934             :     Decoder_State *st,         /* i/o: decoder state structure */
     935             :     int16_t *num_bits,         /* i/o: bit budget update       */
     936             :     const int16_t output_frame /* i  : output frame length     */
     937             : )
     938             : {
     939             :     int32_t cbrate;
     940             : 
     941         385 :     if ( st->last_core == HQ_CORE && st->prev_bfi )
     942             :     {
     943           0 :         mvr2r( st->hHQ_core->old_out, st->hHQ_core->fer_samples, output_frame );
     944             :     }
     945             : 
     946             :     /* set switching frame bitrate */
     947         385 :     if ( st->last_L_frame == L_FRAME )
     948             :     {
     949          78 :         if ( st->core_brate > ACELP_24k40 )
     950             :         {
     951           1 :             cbrate = ACELP_24k40;
     952             :         }
     953             :         else
     954             :         {
     955          77 :             cbrate = st->core_brate;
     956             :         }
     957             : 
     958             :         /* subtract ACELP switching frame bits */
     959          78 :         if ( st->core_brate >= ACELP_11k60 )
     960             :         {
     961          69 :             ( *num_bits )--; /* LP_FLAG bit */
     962             :         }
     963             : 
     964          78 :         *num_bits -= ACB_bits_tbl[BIT_ALLOC_IDX( cbrate, GENERIC, 0, 0 )];     /* pitch bits*/
     965          78 :         *num_bits -= gain_bits_tbl[BIT_ALLOC_IDX( cbrate, TRANSITION, 0, 0 )]; /* gain bits */
     966          78 :         *num_bits -= FCB_bits_tbl[BIT_ALLOC_IDX( cbrate, GENERIC, 0, 0 )];     /* FCB bits  */
     967             :     }
     968             :     else /* L_frame == L_FRAME16k */
     969             :     {
     970         307 :         if ( st->core_brate <= ACELP_8k00 )
     971             :         {
     972           0 :             cbrate = ACELP_8k00;
     973             :         }
     974         307 :         else if ( st->core_brate <= ACELP_14k80 )
     975             :         {
     976           0 :             cbrate = ACELP_14k80;
     977             :         }
     978             :         else
     979             :         {
     980         307 :             cbrate = min( st->core_brate, ACELP_22k60 );
     981             :         }
     982             : 
     983             :         /* subtract ACELP switching frame bits */
     984         307 :         if ( st->core_brate >= ACELP_11k60 )
     985             :         {
     986             :             /* subtract one bit for LP filtering flag */
     987         307 :             ( *num_bits )--;
     988             :         }
     989         307 :         *num_bits -= ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( cbrate, GENERIC, 0, 0 )];  /* pitch bits*/
     990         307 :         *num_bits -= gain_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( cbrate, GENERIC, 0, 0 )]; /* gain bits */
     991         307 :         *num_bits -= FCB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( cbrate, GENERIC, 0, 0 )];  /* FCB bits  */
     992             :     }
     993             : 
     994             :     /* subtract BWE bits */
     995         385 :     if ( !( ( inner_frame_tbl[st->bwidth] == L_FRAME16k && st->last_L_frame == L_FRAME16k ) || inner_frame_tbl[st->bwidth] == L_FRAME8k ) )
     996             :     {
     997         327 :         *num_bits -= ( NOOFGAINBITS1 + AUDIODELAYBITS );
     998             :     }
     999             : 
    1000             :     /* reset state of old_out if switching */
    1001         385 :     set_f( st->hHQ_core->old_out, 0.0f, output_frame );
    1002             : 
    1003         385 :     return;
    1004             : }
    1005             : 
    1006             : 
    1007             : /*---------------------------------------------------------------------*
    1008             :  * bandwidth_switching_detect()
    1009             :  *
    1010             :  * Classification for band-width switching
    1011             :  *---------------------------------------------------------------------*/
    1012             : 
    1013    35667709 : void bandwidth_switching_detect(
    1014             :     Decoder_State *st /* i/o: decoder state structure */
    1015             : )
    1016             : {
    1017    35667709 :     if ( ( st->element_mode == IVAS_CPE_TD && st->idchan == 1 ) || st->element_mode == IVAS_CPE_MDCT )
    1018             :     {
    1019             :         /* there is no BWE in TD stereo secondary channel and in MDCT stereo, IGF is part of the core decoding -> no BW switching -> reset BWS counters */
    1020    25694482 :         st->prev_bws_cnt = 0;
    1021    25694482 :         st->bws_cnt = 0;
    1022    25694482 :         st->bws_cnt1 = 0;
    1023             : 
    1024    25694482 :         return;
    1025             :     }
    1026             : 
    1027             :     /* update band-width switching counter */
    1028     9973227 :     if ( st->bws_cnt1 >= N_NS2W_FRAMES )
    1029             :     {
    1030      146521 :         st->bws_cnt1 = 0;
    1031             :     }
    1032     9826706 :     else if ( st->total_brate > ACELP_9k60 && st->last_core_brate < ACELP_9k60 && st->bwidth == SWB && st->last_bwidth == WB && st->last_low_rate_mode == 0 )
    1033             :     {
    1034         122 :         st->bws_cnt1++;
    1035             :     }
    1036     9826584 :     else if ( st->bws_cnt1 > 0 )
    1037             :     {
    1038       10231 :         st->bws_cnt = st->bwidth < st->last_bwidth ? 2 * ( N_NS2W_FRAMES - st->bws_cnt1 ) - 1 : 0;
    1039       10231 :         st->bws_cnt1 = st->bwidth < st->last_bwidth ? 0 : ( ( st->bwidth == SWB ) ? st->bws_cnt1 + 1 : 0 );
    1040             :     }
    1041             : 
    1042             :     /* update band-width switching counter */
    1043     9973227 :     if ( st->bws_cnt >= N_WS2N_FRAMES )
    1044             :     {
    1045      146015 :         st->bws_cnt = 0;
    1046             :     }
    1047     9827212 :     else if ( st->total_brate < ACELP_9k60 && st->last_core_brate > ACELP_9k60 && st->bwidth < st->last_bwidth && st->bwidth == WB )
    1048             :     {
    1049        6017 :         st->bws_cnt++;
    1050             :     }
    1051     9821195 :     else if ( st->bws_cnt > 0 )
    1052             :     {
    1053       81550 :         st->bws_cnt1 = st->bwidth > st->last_bwidth ? ( ( N_WS2N_FRAMES - st->bws_cnt ) >> 1 ) : 0;
    1054       81550 :         st->bws_cnt = st->bwidth > st->last_bwidth ? 0 : ( ( st->bwidth == WB ) ? st->bws_cnt + 1 : 0 );
    1055             :     }
    1056             : 
    1057     9973227 :     return;
    1058             : }
    1059             : 
    1060             : 
    1061             : /*---------------------------------------------------------------------*
    1062             :  * bw_switching_pre_proc()
    1063             :  *
    1064             :  * Band-width switching pre-processing
    1065             :  *---------------------------------------------------------------------*/
    1066             : 
    1067    16116617 : void bw_switching_pre_proc(
    1068             :     Decoder_State *st,                /* i/o: decoder state structure                  */
    1069             :     const float *old_syn_12k8_16k,    /* i  : ACELP core synthesis at 12.8kHz or 16kHz */
    1070             :     const int32_t last_element_brate, /* i  : last element bitrate                     */
    1071             :     const int16_t nchan_out           /* i  : number of output channels                */
    1072             : )
    1073             : {
    1074             :     int16_t i;
    1075             :     float syn_dct[L_FRAME];
    1076             : 
    1077    16116617 :     if ( st->element_mode > EVS_MONO )
    1078             :     {
    1079    16058048 :         if ( st->core == ACELP_CORE && !( st->bfi == 1 && st->con_tcx == 1 ) && st->hBWE_FD != NULL && !( st->core_brate <= SID_2k40 && st->element_mode == IVAS_CPE_DFT && nchan_out == 2 ) && !( st->element_mode == IVAS_CPE_MDCT && nchan_out == 1 && st->idchan == 1 && last_element_brate <= IVAS_SID_5k2 ) )
    1080             :         {
    1081             :             /* Calculate tilt of the ACELP core synthesis - needed in SWB BWE decoding */
    1082     3320458 :             calc_tilt_bwe( old_syn_12k8_16k, &st->tilt_wb, st->L_frame );
    1083             :         }
    1084             : 
    1085    16058048 :         return;
    1086             :     }
    1087             : 
    1088       58569 :     if ( st->core == ACELP_CORE && !( st->bfi == 1 && st->con_tcx == 1 ) )
    1089             :     {
    1090             :         /*----------------------------------------------------------------------*
    1091             :          * Calculate tilt of the ACELP core synthesis
    1092             :          *----------------------------------------------------------------------*/
    1093             : 
    1094       47603 :         calc_tilt_bwe( old_syn_12k8_16k, &st->tilt_wb, st->L_frame );
    1095             : 
    1096             :         /*-------------------------------------------------------------------------------*
    1097             :          * Calculate frequency energy of 0~3.2kHz and 3.2~6.4kHz the ACELP core synthesis
    1098             :          *-------------------------------------------------------------------------------*/
    1099             : 
    1100       47603 :         edct( old_syn_12k8_16k, syn_dct, L_FRAME, st->element_mode );
    1101             : 
    1102       47603 :         st->enerLL = EPSILON;
    1103     6140787 :         for ( i = 0; i < L_FRAME / 2; i++ )
    1104             :         {
    1105     6093184 :             st->enerLL += syn_dct[i] * syn_dct[i];
    1106             :         }
    1107       47603 :         st->enerLL = (float) sqrt( st->enerLL / 128 );
    1108             : 
    1109       47603 :         st->enerLH = EPSILON;
    1110     6140787 :         for ( ; i < L_FRAME; i++ )
    1111             :         {
    1112     6093184 :             st->enerLH += syn_dct[i] * syn_dct[i];
    1113             :         }
    1114       47603 :         st->enerLH = (float) sqrt( st->enerLH / 128 );
    1115             :     }
    1116             :     else
    1117             :     {
    1118       10966 :         if ( st->hHQ_core->old_is_transient[0] )
    1119             :         {
    1120         688 :             st->enerLL = EPSILON;
    1121       22704 :             for ( i = 0; i < 32; i++ )
    1122             :             {
    1123       22016 :                 st->enerLL += st->t_audio_q[i] * st->t_audio_q[i];
    1124             :             }
    1125         688 :             st->enerLL = (float) sqrt( st->enerLL / 32 );
    1126             : 
    1127         688 :             st->enerLH = EPSILON;
    1128       22704 :             for ( ; i < 64; i++ )
    1129             :             {
    1130       22016 :                 st->enerLH += st->t_audio_q[i] * st->t_audio_q[i];
    1131             :             }
    1132         688 :             st->enerLH = (float) sqrt( st->enerLH / 32 );
    1133             :         }
    1134             :         else
    1135             :         {
    1136       10278 :             st->enerLL = EPSILON;
    1137     1325862 :             for ( i = 0; i < L_FRAME / 2; i++ )
    1138             :             {
    1139     1315584 :                 st->enerLL += st->t_audio_q[i] * st->t_audio_q[i];
    1140             :             }
    1141       10278 :             st->enerLL = (float) sqrt( st->enerLL / 128 );
    1142             : 
    1143       10278 :             st->enerLH = EPSILON;
    1144     1325862 :             for ( ; i < L_FRAME; i++ )
    1145             :             {
    1146     1315584 :                 st->enerLH += st->t_audio_q[i] * st->t_audio_q[i];
    1147             :             }
    1148       10278 :             st->enerLH = (float) sqrt( st->enerLH / 128 );
    1149             :         }
    1150             :     }
    1151             : 
    1152       58569 :     if ( st->last_bwidth == 0 && st->extl <= SWB_CNG )
    1153             :     {
    1154        5862 :         st->prev_ener_shb = 0.0f;
    1155        5862 :         if ( st->hBWE_FD != NULL )
    1156             :         {
    1157        5862 :             set_f( st->hBWE_FD->prev_SWB_fenv, 0, SWB_FENV );
    1158             :         }
    1159             :     }
    1160       52707 :     else if ( ( ( st->core == ACELP_CORE && ( st->last_core == HQ_CORE || st->last_core == TCX_10_CORE || st->last_core == TCX_20_CORE ) ) || ( st->core == st->last_core && st->extl != st->last_extl ) ) && st->last_bwidth >= SWB )
    1161             :     {
    1162         799 :         st->attenu1 = 0.1f;
    1163             :     }
    1164             : 
    1165       58569 :     if ( st->last_core == HQ_CORE || ( st->last_core == ACELP_CORE && !( st->last_extl == WB_TBE || st->last_extl == SWB_TBE || st->last_extl == FB_TBE ) && st->core_brate > ACELP_8k00 ) )
    1166             :     {
    1167       30149 :         st->prev_fractive = 0;
    1168             :     }
    1169             : 
    1170       58569 :     return;
    1171             : }
    1172             : 
    1173             : 
    1174             : /*---------------------------------------------------------------------*
    1175             :  * core_switch_lb_upsamp()
    1176             :  *
    1177             :  * Resample HQ/TCX-LB to the output sampling rate (8/16/32/48 kHz)
    1178             :  *---------------------------------------------------------------------*/
    1179             : 
    1180      191581 : static void core_switch_lb_upsamp(
    1181             :     Decoder_State *st, /* i/o: Decoder state               */
    1182             :     float *output      /* i/o: LB synth/upsampled LB synth */
    1183             : )
    1184             : {
    1185             :     int16_t i;
    1186             :     float *realBuffer[CLDFB_OVRLP_MIN_SLOTS], *imagBuffer[CLDFB_OVRLP_MIN_SLOTS];
    1187             :     float realBufferTmp[CLDFB_OVRLP_MIN_SLOTS][CLDFB_NO_CHANNELS_MAX];
    1188             :     float imagBufferTmp[CLDFB_OVRLP_MIN_SLOTS][CLDFB_NO_CHANNELS_MAX];
    1189             : 
    1190             :     /* open CLDFB buffer up to CLDFB_NO_CHANNELS_MAX bands for 48kHz */
    1191      766324 :     for ( i = 0; i < CLDFB_OVRLP_MIN_SLOTS; i++ )
    1192             :     {
    1193      574743 :         set_f( realBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
    1194      574743 :         set_f( imagBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
    1195      574743 :         realBuffer[i] = realBufferTmp[i];
    1196      574743 :         imagBuffer[i] = imagBufferTmp[i];
    1197             :     }
    1198             : 
    1199             :     /* check if the CLDFB works on the right sample rate */
    1200      191581 :     if ( ( st->cldfbAna->no_channels * st->cldfbAna->no_col ) != st->L_frame )
    1201             :     {
    1202       21946 :         resampleCldfb( st->cldfbAna, st->L_frame * FRAMES_PER_SEC );
    1203             : 
    1204       21946 :         if ( st->cldfbBPF != NULL && st->L_frame <= L_FRAME16k )
    1205             :         {
    1206        3885 :             resampleCldfb( st->cldfbBPF, st->L_frame * FRAMES_PER_SEC );
    1207             :         }
    1208             : 
    1209       21946 :         if ( st->ini_frame > 0 )
    1210             :         {
    1211       21811 :             st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
    1212             :         }
    1213             :     }
    1214             : 
    1215             :     /* analysis of the synthesis at internal sampling rate */
    1216      191581 :     cldfbAnalysis( output, realBuffer, imagBuffer, CLDFB_OVRLP_MIN_SLOTS * st->cldfbAna->no_channels, st->cldfbAna );
    1217             : 
    1218             :     /* analysis and add the BPF error signal */
    1219      191581 :     if ( st->p_bpf_noise_buf )
    1220             :     {
    1221      172590 :         addBassPostFilter( st->p_bpf_noise_buf, st->bpf_off ? 0 : CLDFB_OVRLP_MIN_SLOTS * st->cldfbBPF->no_channels, realBuffer, imagBuffer, st->cldfbBPF );
    1222             :     }
    1223             : 
    1224             :     /* set output mask for upsampling */
    1225      191581 :     if ( st->bwidth == NB )
    1226             :     {
    1227             :         /* set NB mask for upsampling */
    1228           0 :         st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - 10;
    1229             :     }
    1230      191581 :     else if ( st->cldfbSyn->bandsToZero != st->cldfbSyn->no_channels - st->cldfbAna->no_channels )
    1231             :     {
    1232             :         /* in case of BW switching, re-init to default */
    1233        3548 :         st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
    1234             :     }
    1235             : 
    1236             :     /* synthesis of the combined signal */
    1237      191581 :     cldfbSynthesis( realBuffer, imagBuffer, output, CLDFB_OVRLP_MIN_SLOTS * st->cldfbSyn->no_channels, st->cldfbSyn );
    1238             : 
    1239             :     /* save synthesis - needed in case of core switching */
    1240      191581 :     if ( st->hTcxDec != NULL )
    1241             :     {
    1242      191581 :         mvr2r( output, st->previoussynth, st->hTcxDec->L_frameTCX );
    1243             :     }
    1244             : 
    1245      191581 :     return;
    1246             : }
    1247             : 
    1248             : /*---------------------------------------------------------------------*
    1249             :  * smoothTransitionDtxToTcx()
    1250             :  *
    1251             :  * apply smoothing to the transition part for inactive to active transitions in DTX
    1252             :  *---------------------------------------------------------------------*/
    1253             : 
    1254             : #define TRANSITION_SMOOTHING_LEN_16k 15
    1255             : #define TRANSITION_SMOOTHING_LEN_32k 31
    1256             : #define TRANSITION_SMOOTHING_LEN_48k 47
    1257             : 
    1258        4932 : static void smoothTransitionDtxToTcx(
    1259             :     float synth[],              /* i/o: synthesis                       */
    1260             :     const int16_t output_frame, /* i  : output frame length             */
    1261             :     const int16_t delay_comp    /* i  : delay compensation in samples   */
    1262             : )
    1263             : {
    1264             :     int16_t i, filter_len;
    1265             :     float w, mem, step, fade_in;
    1266             :     float smoothing_input_buffer[2 * NS2SA( 48000, DELAY_CLDFB_NS ) + TRANSITION_SMOOTHING_LEN_48k];
    1267             :     float smoothing_out_buffer[2 * NS2SA( 48000, DELAY_CLDFB_NS ) + TRANSITION_SMOOTHING_LEN_48k];
    1268             : 
    1269        4932 :     filter_len = TRANSITION_SMOOTHING_LEN_16k;
    1270        4932 :     if ( output_frame == L_FRAME32k )
    1271             :     {
    1272         728 :         filter_len = TRANSITION_SMOOTHING_LEN_32k;
    1273             :     }
    1274        4204 :     else if ( output_frame == L_FRAME48k )
    1275             :     {
    1276        3772 :         filter_len = TRANSITION_SMOOTHING_LEN_48k;
    1277             :     }
    1278             : 
    1279             :     /* prepare buffer */
    1280      105632 :     for ( i = 0; i < filter_len / 2; i++ )
    1281             :     {
    1282      100700 :         smoothing_input_buffer[i] = synth[0];
    1283             :     }
    1284        4932 :     mvr2r( synth, smoothing_input_buffer + filter_len / 2, 2 * delay_comp + filter_len / 2 );
    1285             : 
    1286             :     /* apply Mean filter */
    1287        4932 :     w = 1.f / filter_len;
    1288        4932 :     mem = sum_f( smoothing_input_buffer, filter_len );
    1289      533092 :     for ( i = 0; i < 2 * delay_comp; i++ )
    1290             :     {
    1291      528160 :         smoothing_out_buffer[i] = w * mem;
    1292      528160 :         mem = mem - smoothing_input_buffer[i] + smoothing_input_buffer[i + filter_len];
    1293             :     }
    1294             : 
    1295             :     /* apply fades around transition */
    1296        4932 :     step = 1.f / delay_comp;
    1297        4932 :     fade_in = 0.f;
    1298      269012 :     for ( i = 0; i < delay_comp; i++ )
    1299             :     {
    1300      264080 :         synth[i] = smoothing_out_buffer[i] * fade_in + synth[i] * ( 1 - fade_in );
    1301      264080 :         fade_in += step;
    1302             :     }
    1303             : 
    1304        4932 :     fade_in = 0.f;
    1305      269012 :     for ( ; i < 2 * delay_comp; i++ )
    1306             :     {
    1307      264080 :         synth[i] = synth[i] * fade_in + smoothing_out_buffer[i] * ( 1 - fade_in );
    1308      264080 :         fade_in += step;
    1309             :     }
    1310             : 
    1311        4932 :     return;
    1312             : }

Generated by: LCOV version 1.14