LCOV - code coverage report
Current view: top level - lib_dec - core_switching_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- long test vectors @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 489 518 94.4 %
Date: 2025-11-01 09:50:07 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    41463559 : 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    41463559 :     error = IVAS_ERR_OK;
      76             : 
      77             :     /* Codec mode switching */
      78    41463559 :     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    36768700 :         mvr2r( st->mem_syn2, st->mem_syn1, M );
      81    36768700 :         set_f( st->agc_mem2, 0, 2 );
      82    36768700 :         st->mem_deemph = st->syn[M];
      83    36768700 :         st->bpf_off = 1;
      84    36768700 :         if ( st->hBPF != NULL )
      85             :         {
      86     6950623 :             set_f( st->hBPF->pst_old_syn, 0, NBPSF_PIT_MAX );
      87     6950623 :             st->hBPF->pst_mem_deemp_err = 0;
      88             :         }
      89    36768700 :         st->psf_lp_noise = st->lp_noise;
      90             : 
      91             :         /* reset old HB synthesis buffer */
      92    36768700 :         if ( st->last_L_frame == L_FRAME )
      93             :         {
      94     1563431 :             st->old_bwe_delay = NS2SA( st->output_Fs, MAX_DELAY_TBE_NS - DELAY_SWB_TBE_12k8_NS );
      95             :         }
      96             :         else
      97             :         {
      98    35205269 :             st->old_bwe_delay = NS2SA( st->output_Fs, MAX_DELAY_TBE_NS - DELAY_SWB_TBE_16k_NS );
      99             :         }
     100    36768700 :         set_f( st->hb_prev_synth_buffer, 0, NS2SA( 48000, DELAY_BWE_TOTAL_NS ) );
     101             : 
     102    36768700 :         if ( st->hBWE_TD != NULL && st->last_core != ACELP_CORE )
     103             :         {
     104             :             /* reset BWE memories */
     105     7505067 :             set_f( st->hBWE_TD->old_bwe_exc, 0, PIT16k_MAX * 2 );
     106     7505067 :             st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
     107             :         }
     108             : 
     109             :         /* reset upd_cnt */
     110    36768700 :         st->upd_cnt = MAX_UPD_CNT;
     111             : 
     112    36768700 :         st->igf = 0;
     113             : 
     114    36768700 :         if ( st->output_Fs >= 16000 && st->hBWE_zero != NULL )
     115             :         {
     116     6950583 :             hf_synth_reset( st->hBWE_zero );
     117             :         }
     118             : 
     119    36768700 :         if ( st->hBWE_FD != NULL )
     120             :         {
     121     7505212 :             set_f( st->hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
     122             :         }
     123             : 
     124    36768700 :         if ( st->hHQ_core != NULL )
     125             :         {
     126    36767860 :             set_f( st->hHQ_core->prev_env, 0, SFM_N_WB );
     127    36767860 :             set_f( st->hHQ_core->prev_normq, 0, SFM_N_WB );
     128             : 
     129    36767860 :             set_f( st->hHQ_core->last_ni_gain, 0, BANDS_MAX );
     130    36767860 :             set_f( st->hHQ_core->last_env, 0, BANDS_MAX );
     131    36767860 :             st->hHQ_core->last_max_pos_pulse = 0;
     132             : 
     133    36767860 :             if ( st->output_Fs > 16000 )
     134             :             {
     135    32701816 :                 set_f( st->hHQ_core->prev_coeff_out, 0, L_HQ_WB_BWE );
     136             :             }
     137             : 
     138             :             /* pre-echo */
     139    36767860 :             st->hHQ_core->pastpre = 0;
     140             :         }
     141             : 
     142             :         /* reset the GSC pre echo energy threshold in case of switching */
     143    36768700 :         if ( st->hGSCDec != NULL )
     144             :         {
     145     6950623 :             st->hGSCDec->Last_frame_ener = (float) MAX_32;
     146             :         }
     147             : 
     148    36768700 :         if ( st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE )
     149             :         {
     150    36768555 :             if ( st->element_mode == EVS_MONO )
     151             :             {
     152         672 :                 st->last_core = HQ_CORE;
     153         672 :                 mvr2r( st->hTcxDec->FBTCXdelayBuf, st->prev_synth_buffer, NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS - DELAY_CLDFB_NS ) );
     154             :             }
     155             : 
     156    36768555 :             if ( st->hHQ_core != NULL )
     157             :             {
     158    36767715 :                 set_f( st->hHQ_core->last_ni_gain, 0, BANDS_MAX );
     159    36767715 :                 set_f( st->hHQ_core->last_env, 0, BANDS_MAX );
     160    36767715 :                 st->hHQ_core->last_max_pos_pulse = 0;
     161             : 
     162    36767715 :                 set_s( st->hHQ_core->prev_SWB_peak_pos, 0, SPT_SHORTEN_SBNUM );
     163    36767715 :                 st->hHQ_core->prev_frm_hfe2 = 0;
     164    36767715 :                 st->hHQ_core->prev_stab_hfe2 = 0;
     165             :             }
     166             :         }
     167             : 
     168    36768700 :         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     1309411 :             if ( st->hHQ_core != NULL )
     174             :             {
     175     1309384 :                 st->hHQ_core->oldHqVoicing = 0;
     176     1309384 :                 st->hHQ_core->HqVoicing = 0;
     177             :             }
     178             : 
     179     1309411 :             delay_comp = NS2SA( st->output_Fs, DELAY_CLDFB_NS );
     180             : 
     181     1309411 :             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     1309411 :             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    36768700 :         st->use_acelp_preq = 0;
     222    36768700 :         st->reset_mem_AR = 0;
     223             :     }
     224             : 
     225             :     /*FEC*/
     226    41463559 :     if ( st->L_frame <= L_FRAME16k )
     227             :     {
     228    40693336 :         if ( st->last_L_frame <= L_FRAME16k && st->core != HQ_CORE )
     229             :         {
     230    13610706 :             if ( st->L_frame != st->last_L_frame )
     231             :             {
     232      143927 :                 if ( st->L_frame > st->last_L_frame )
     233             :                 {
     234      106290 :                     oldLenClasBuff = L_SYN_MEM_CLAS_ESTIM * st->last_L_frame / st->L_frame;
     235      106290 :                     newLenClasBuff = L_SYN_MEM_CLAS_ESTIM;
     236             :                 }
     237             :                 else
     238             :                 {
     239       37637 :                     oldLenClasBuff = L_SYN_MEM_CLAS_ESTIM;
     240       37637 :                     newLenClasBuff = L_SYN_MEM_CLAS_ESTIM * st->L_frame / st->last_L_frame;
     241             :                 }
     242      143927 :                 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    27082630 :             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    41463559 :     if ( st->core == ACELP_CORE && ( st->last_core != ACELP_CORE || st->last_codec_mode == MODE2 ) )
     254             :     {
     255      218515 :         st->last_ppp_mode_dec = 0;
     256      218515 :         st->last_nelp_mode_dec = 0;
     257             :     }
     258             : 
     259             :     /* Handle state reset of stat_noise_uv_mod memory */
     260    41463559 :     if ( st->core == ACELP_CORE && ( st->last_core != ACELP_CORE || st->last_codec_mode == MODE2 || st->last_total_brate <= PPP_NELP_2k80 ) )
     261             :     {
     262     1206193 :         st->act_count = 3;
     263     1206193 :         st->uv_count = 0;
     264             :     }
     265             : 
     266    41463559 :     if ( ( ( st->core == ACELP_CORE || st->core == AMR_WB_CORE ) && st->last_core == HQ_CORE ) /* EVS and HQ -> ACELP */ ||
     267    41458571 :          ( ( 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    41456555 :          ( st->core == ACELP_CORE && st->last_L_frame > L_FRAME16k ) /* TCX @ 25.6/32 kHz -> ACELP */ )
     269             :     {
     270       46538 :         if ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD )
     271             :         {
     272       33129 :             st->hPFstat->reset = 1;
     273             :         }
     274             : 
     275       46538 :         if ( st->L_frame == L_FRAME16k )
     276             :         {
     277       14882 :             mvr2r( TRWB2_Ave, st->lsf_old, M ); /* init of LSP */
     278       14882 :             mvr2r( TRWB2_Ave, st->lsfoldbfi1, M );
     279       14882 :             mvr2r( TRWB2_Ave, st->lsfoldbfi0, M );
     280       14882 :             mvr2r( TRWB2_Ave, st->lsf_adaptive_mean, M );
     281       14882 :             lsf2lsp( st->lsf_old, st->lsp_old, M, INT_FS_16k );
     282             :         }
     283             :         else
     284             :         {
     285       31656 :             mvr2r( TRWB_Ave, st->lsf_old, M ); /* init of LSP */
     286       31656 :             mvr2r( TRWB_Ave, st->lsfoldbfi1, M );
     287       31656 :             mvr2r( TRWB_Ave, st->lsfoldbfi0, M );
     288       31656 :             mvr2r( TRWB_Ave, st->lsf_adaptive_mean, M );
     289       31656 :             lsf2lsp( st->lsf_old, st->lsp_old, M, INT_FS_12k8 );
     290             :         }
     291             : 
     292       46538 :         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        1923 :             set_f( st->hTcxDec->old_syn_Overl, 0.0f, L_FRAME32k / 2 );
     296        1923 :             set_f( st->hFdCngDec->hFdCngCom->olapBufferAna, 0.0f, FFTLEN );
     297             :         }
     298             : 
     299       46538 :         set_f( st->agc_mem2, 0, 2 );
     300       46538 :         st->mem_deemph = 0;
     301       46538 :         if ( !st->last_con_tcx )
     302             :         {
     303       46447 :             set_f( st->mem_syn2, 0.0f, M );
     304             :         }
     305       46538 :         set_f( st->mem_syn1, 0.0f, M );
     306       46538 :         if ( st->hBWE_TD != NULL )
     307             :         {
     308       44786 :             st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
     309             :         }
     310             : 
     311             :         /* Reset ACELP parameters */
     312       46538 :         set_zero( st->mem_MA, M );
     313       46538 :         if ( st->sr_core == INT_FS_16k )
     314             :         {
     315       14882 :             mvr2r( GEWB2_Ave, st->mem_AR, M );
     316             :         }
     317             :         else
     318             :         {
     319       31656 :             mvr2r( GEWB_Ave, st->mem_AR, M );
     320             :         }
     321       46538 :         st->tilt_code = 0.0f;
     322       46538 :         st->gc_threshold = 0.0f;
     323       46538 :         set_f( st->dispMem, 0, 8 );
     324             : 
     325       46538 :         st->last_coder_type = GENERIC;
     326             : 
     327       46538 :         fer_energy( output_frame, UNVOICED_CLAS, st->previoussynth, -1, &st->enr_old, 1 );
     328       46538 :         st->lp_gainp = 0.0f;
     329       46538 :         st->lp_gainc = (float) sqrt( st->lp_ener );
     330             : 
     331       46538 :         st->last_voice_factor = 0;
     332       46538 :         st->Last_GSC_noisy_speech_flag = 0;
     333             : 
     334             :         /* reset CLDFB memories */
     335       46538 :         cldfb_reset_memory( st->cldfbAna );
     336       46538 :         cldfb_reset_memory( st->cldfbBPF );
     337       46538 :         cldfb_reset_memory( st->cldfbSyn );
     338             : 
     339             :         /* reset TBE memories */
     340       46538 :         if ( !st->last_con_tcx && !( ( st->last_core == HQ_CORE ) && st->element_mode > EVS_MONO ) )
     341             :         {
     342       42202 :             set_f( st->old_exc, 0, L_EXC_MEM_DEC );
     343             :         }
     344        4336 :         else if ( st->L_frame < L_FRAME16k )
     345             :         {
     346             :             /* resample from 16kHz to 12.8kHZ */
     347         181 :             synth_mem_updt2( st->L_frame, L_FRAME16k, st->old_exc, st->mem_syn_r, st->mem_syn2, NULL, DEC );
     348             :         }
     349             : 
     350       46538 :         if ( st->hBWE_TD != NULL )
     351             :         {
     352       44786 :             set_f( st->hBWE_TD->old_bwe_exc, 0, PIT16k_MAX * 2 );
     353             :         }
     354             : 
     355       46538 :         if ( st->output_Fs >= 16000 && st->hBWE_zero != NULL )
     356             :         {
     357       44550 :             hf_synth_reset( st->hBWE_zero );
     358             :         }
     359             : 
     360       46538 :         if ( st->hBWE_FD != NULL )
     361             :         {
     362       44786 :             set_f( st->hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
     363             :         }
     364             :     }
     365             : 
     366    41463559 :     if ( ( st->core == ACELP_CORE || st->core == AMR_WB_CORE ) && ( st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE ) )
     367             :     {
     368      213468 :         if ( st->hBWE_TD != NULL )
     369             :         {
     370      210888 :             st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
     371      210888 :             set_f( st->hBWE_TD->old_bwe_exc, 0, PIT16k_MAX * 2 );
     372             :         }
     373             : 
     374      213468 :         st->tilt_code = 0.0f;
     375      213468 :         st->gc_threshold = 0.0f;
     376      213468 :         set_f( st->dispMem, 0, 8 );
     377             : 
     378      213468 :         st->last_coder_type = GENERIC;
     379             : 
     380      213468 :         fer_energy( output_frame, UNVOICED_CLAS, st->previoussynth, -1, &st->enr_old, 1 );
     381      213468 :         st->lp_gainp = 0.0f;
     382      213468 :         st->lp_gainc = (float) sqrt( st->lp_ener );
     383             : 
     384      213468 :         st->last_voice_factor = 0;
     385      213468 :         st->Last_GSC_noisy_speech_flag = 0;
     386             : 
     387      213468 :         if ( st->output_Fs >= 16000 && st->hBWE_zero != NULL )
     388             :         {
     389      210762 :             hf_synth_reset( st->hBWE_zero );
     390             :         }
     391             : 
     392      213468 :         if ( st->hBWE_FD != NULL )
     393             :         {
     394      210888 :             set_f( st->hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
     395             :         }
     396             : 
     397      213468 :         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       27312 :             offset = st->cldfbAna->p_filter_length - st->cldfbAna->no_channels;
     402       27312 :             mvr2r( st->hTcxDec->old_synthFB + st->hTcxDec->old_synth_lenFB - offset, st->cldfbAna->cldfb_state, offset );
     403             :         }
     404             :     }
     405             : 
     406    41463559 :     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       13269 :         set_f( st->hHQ_core->prev_env, 0, SFM_N_WB );
     409       13269 :         set_f( st->hHQ_core->prev_normq, 0, SFM_N_WB );
     410             : 
     411       13269 :         set_f( st->hHQ_core->last_ni_gain, 0, BANDS_MAX );
     412       13269 :         set_f( st->hHQ_core->last_env, 0, BANDS_MAX );
     413       13269 :         st->hHQ_core->last_max_pos_pulse = 0;
     414             : 
     415       13269 :         set_s( st->hHQ_core->prev_SWB_peak_pos, 0, SPT_SHORTEN_SBNUM );
     416       13269 :         st->hHQ_core->prev_frm_hfe2 = 0;
     417       13269 :         st->hHQ_core->prev_stab_hfe2 = 0;
     418       13269 :         if ( st->output_Fs > 16000 )
     419             :         {
     420       11267 :             set_f( st->hHQ_core->prev_coeff_out, 0, L_HQ_WB_BWE );
     421             :         }
     422             : 
     423       13269 :         if ( st->element_mode != EVS_MONO )
     424             :         {
     425             :             /* Estimate mem_env_delta to reinit env_stab */
     426       12702 :             tmp = max( 0, ENV_STAB_EST1 + ( ENV_STAB_EST2 * st->stab_fac_smooth_lt ) + ( ENV_STAB_EST3 * st->log_energy_diff_lt ) );
     427       12702 :             st->hHQ_core->mem_env_delta = (int16_t) min( MAX16B, (int32_t) ( tmp * ( 1 << 12 ) ) ); /* Convert to Q12 and handle saturation */
     428             : 
     429       12702 :             if ( st->last_core != TCX_20_CORE && st->last_core != TCX_10_CORE )
     430             :             {
     431        4041 :                 set_f( st->hHQ_core->old_out, 0, output_frame );
     432        4041 :                 set_f( st->hHQ_core->old_outLB, 0, L_FRAME16k );
     433             :             }
     434             : 
     435       12702 :             st->hHQ_core->no_att_hangover = 0;
     436       12702 :             st->hHQ_core->energy_lt = 300.0f;
     437             : 
     438       12702 :             set_s( st->hHQ_core->old_is_transient, 0, 3 );
     439       12702 :             set_f( st->hHQ_core->prev_noise_level, 0.0f, 2 );
     440       12702 :             st->hHQ_core->prev_R = 0;
     441       12702 :             set_s( st->hHQ_core->mem_norm + 1, 39, SFM_N_ENV_STAB - 1 );
     442       12702 :             st->hHQ_core->prev_hqswb_clas = HQ_NORMAL;
     443       12702 :             st->hHQ_core->prev_ni_ratio = 0.5f;
     444       12702 :             set_f( st->hHQ_core->prev_En_sb, 0.0f, NB_SWB_SUBBANDS );
     445             :         }
     446             :         else
     447             :         {
     448         567 :             set_f( st->hHQ_core->old_out, 0, output_frame );
     449         567 :             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    41463559 :     if ( st->hHQ_core != NULL )
     455             :     {
     456    41389939 :         st->hHQ_core->pastpre--;
     457    41389939 :         if ( st->hHQ_core->pastpre <= 0 )
     458             :         {
     459    41256424 :             reset_preecho_dec( st->hHQ_core );
     460             :         }
     461             :     }
     462             : 
     463    41463559 :     if ( st->core_brate == FRAME_NO_DATA )
     464             :     {
     465      187302 :         st->VAD = 0;
     466      187302 :         st->m_frame_type = ZERO_FRAME;
     467             :     }
     468    41276257 :     else if ( st->core_brate == SID_2k40 || st->core_brate == SID_1k75 )
     469             :     {
     470       28933 :         st->VAD = 0;
     471       28933 :         st->m_frame_type = SID_FRAME;
     472             :     }
     473             :     else
     474             :     {
     475    41247324 :         st->VAD = 1;
     476    41247324 :         st->m_frame_type = ACTIVE_FRAME;
     477             :     }
     478             : 
     479             :     /*switch on CNA on active frames*/
     480    41463559 :     if ( st->element_mode == EVS_MONO ) /* for IVAS modes, st->flag_cna is set earlier */
     481             :     {
     482       86251 :         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       39442 :             st->flag_cna = 1;
     485             :         }
     486       46809 :         else if ( st->VAD || ( ( st->cng_type == FD_CNG ) && ( st->L_frame == L_FRAME16k ) ) )
     487             :         {
     488       43889 :             st->flag_cna = 0;
     489             :         }
     490             :     }
     491             : 
     492    41463559 :     if ( st->core == AMR_WB_CORE )
     493             :     {
     494        3255 :         st->cng_type = LP_CNG;
     495             :     }
     496             : 
     497             :     /* Reconfigure CNG */
     498    41463559 :     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    19151149 :         if ( st->core != AMR_WB_CORE )
     502             :         {
     503    19151130 :             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          19 :             configureFdCngDec( st->hFdCngDec, WB, ACELP_8k00, st->L_frame, st->last_L_frame, st->element_mode );
     508             : 
     509          19 :             if ( st->VAD )
     510             :             {
     511          19 :                 st->hFdCngDec->hFdCngCom->CngBitrate = st->total_brate;
     512             :             }
     513             :         }
     514    19151149 :         if ( st->last_L_frame != st->L_frame && st->L_frame <= L_FRAME16k && st->last_L_frame <= L_FRAME16k )
     515             :         {
     516      143384 :             if ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD )
     517             :             {
     518       21996 :                 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      143384 :             lerp( st->hFdCngDec->hFdCngCom->olapBufferSynth2, st->hFdCngDec->hFdCngCom->olapBufferSynth2, st->L_frame * 2, st->last_L_frame * 2 );
     522             : 
     523      143384 :             if ( st->total_brate <= SID_2k40 && st->last_total_brate <= SID_2k40 )
     524             :             {
     525         363 :                 lerp( st->hFdCngDec->hFdCngCom->olapBufferSynth, st->hFdCngDec->hFdCngCom->olapBufferSynth, st->L_frame * 2, st->last_L_frame * 2 );
     526             : 
     527         363 :                 if ( st->L_frame == L_FRAME )
     528             :                 {
     529      103113 :                     for ( i = 0; i < st->L_frame * 2; i++ )
     530             :                     {
     531      102912 :                         st->hFdCngDec->hFdCngCom->olapBufferSynth[i] = st->hFdCngDec->hFdCngCom->olapBufferSynth[i] * 0.6250f;
     532             :                     }
     533             :                 }
     534             :                 else
     535             :                 {
     536      103842 :                     for ( i = 0; i < st->L_frame * 2; i++ )
     537             :                     {
     538      103680 :                         st->hFdCngDec->hFdCngCom->olapBufferSynth[i] = st->hFdCngDec->hFdCngCom->olapBufferSynth[i] * 1.6f;
     539             :                     }
     540             :                 }
     541             :             }
     542             :         }
     543             :     }
     544             : 
     545    41463559 :     return error;
     546             : }
     547             : 
     548             : 
     549             : /*---------------------------------------------------------------------*
     550             :  * core_switching_post_dec()
     551             :  *
     552             :  * Postprocessing for ACELP/HQ core switching
     553             :  *---------------------------------------------------------------------*/
     554             : 
     555    41460304 : 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    41460304 :     error = IVAS_ERR_OK;
     578             : 
     579    41460304 :     if ( st->core == ACELP_CORE && st->bfi && st->hHQ_core != NULL && !st->con_tcx )
     580             :     {
     581      173500 :         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    41460304 :     delta = 1;
     589    41460304 :     if ( output_frame == L_FRAME16k )
     590             :     {
     591     4653352 :         delta = 2;
     592             :     }
     593    36806952 :     else if ( output_frame == L_FRAME32k )
     594             :     {
     595     8916240 :         delta = 4;
     596             :     }
     597    27890712 :     else if ( output_frame == L_FRAME48k )
     598             :     {
     599    27884878 :         delta = 6;
     600             :     }
     601             : 
     602             :     /* set delay compensation between HQ synthesis and ACELP synthesis */
     603    41460304 :     delay_comp = delta * HQ_DELAY_COMP;
     604             : 
     605             :     /* Core switching done in DFT domain afterward*/
     606    41460304 :     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    39233692 :         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    35621911 :             st->use_acelp_preq = 0;
     611    35621911 :             if ( st->hBWE_FD != NULL )
     612             :             {
     613     6907678 :                 st->hBWE_FD->mem_deemph_old_syn = 0.0f;
     614             :             }
     615             : 
     616    35621911 :             if ( st->element_mode == EVS_MONO && st->core == HQ_CORE ) /* ACELP->HQ-CORE */
     617             :             {
     618       20194 :                 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         564 :                     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       20194 :                 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    35621911 :             delay_signal( synth, output_frame, st->delay_buf_out, delay_comp );
     634             : 
     635    35621911 :             if ( st->element_mode == EVS_MONO && st->core == HQ_CORE ) /* ACELP->HQ-CORE */
     636             :             {
     637       20194 :                 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         564 :                     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       19630 :                 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       19630 :                 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    35601717 :             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      245799 :                 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      218607 :                     core_switch_lb_upsamp( st, output );
     670             :                 }
     671             : 
     672      245799 :                 mvr2r( st->previoussynth, synth, delay_comp );
     673             : 
     674             :                 /* Overlap between TCX-LB and TCX-FB*/
     675      245799 :                 tmpDelta = NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS );
     676    24071394 :                 for ( i = 0; i < tmpDelta; i++ )
     677             :                 {
     678    23825595 :                     synth[i + delay_comp] = ( synth[i + delay_comp] * i + ( tmpDelta - i ) * st->previoussynth[i + delay_comp] ) / tmpDelta;
     679             :                 }
     680             : 
     681      245799 :                 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        7545 :                     smoothTransitionDtxToTcx( synth, output_frame, delay_comp );
     685             :                 }
     686             : 
     687             :                 /* Reset memories of CLDFBs */
     688      245799 :                 if ( st->cldfbAna != NULL )
     689             :                 {
     690      245799 :                     if ( st->cldfbAna->no_channels * st->cldfbAna->no_col != st->L_frame )
     691             :                     {
     692       27087 :                         configureCldfb( st->cldfbAna, st->L_frame * FRAMES_PER_SEC );
     693       27087 :                         configureCldfb( st->cldfbBPF, min( 16000, st->L_frame * FRAMES_PER_SEC ) );
     694             :                     }
     695             : 
     696      245799 :                     cldfb_reset_memory( st->cldfbAna );
     697      245799 :                     cldfb_reset_memory( st->cldfbBPF );
     698             :                 }
     699      245799 :                 cldfb_reset_memory( st->cldfbSyn );
     700             : 
     701             :                 /* Update memories for CLDFB ana for eventual next ACELP frame */
     702      245799 :                 if ( st->cldfbAna != NULL )
     703             :                 {
     704      245799 :                     delta = st->cldfbAna->no_channels;
     705      245799 :                     offset = st->cldfbAna->p_filter_length - st->cldfbAna->no_channels;
     706     5885343 :                     for ( i = 0; i < delta; i++ )
     707             :                     {
     708     5639544 :                         st->cldfbAna->cldfb_state[offset - delta + i] =
     709     5639544 :                             output[st->L_frame - delta + i] * ( (float) ( i + 1 ) ) / ( (float) delta );
     710             :                     }
     711             :                 }
     712             :             }
     713    35355918 :             else if ( st->element_mode != EVS_MONO )
     714             :             {
     715             :                 /* Reset memories of CLDFBs */
     716    35355918 :                 if ( st->cldfbAna != NULL )
     717             :                 {
     718     7693692 :                     if ( st->cldfbAna->no_channels * st->cldfbAna->no_col != st->L_frame )
     719             :                     {
     720      174714 :                         configureCldfb( st->cldfbAna, st->L_frame * FRAMES_PER_SEC );
     721      174714 :                         configureCldfb( st->cldfbBPF, min( 16000, st->L_frame * FRAMES_PER_SEC ) );
     722             :                     }
     723             : 
     724     7693692 :                     cldfb_reset_memory( st->cldfbAna );
     725     7693692 :                     cldfb_reset_memory( st->cldfbBPF );
     726             :                 }
     727             : 
     728    35355918 :                 if ( st->cldfbSyn != NULL )
     729             :                 {
     730    35355918 :                     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    35355918 :                 if ( st->cldfbAna != NULL )
     736             :                 {
     737     7693692 :                     delta = st->cldfbAna->no_channels;
     738     7693692 :                     offset = st->cldfbAna->p_filter_length - st->cldfbAna->no_channels;
     739   214762788 :                     for ( i = 0; i < delta; i++ )
     740             :                     {
     741   207069096 :                         st->cldfbAna->cldfb_state[offset - delta + i] =
     742   207069096 :                             output[st->L_frame - delta + i] * ( (float) ( i + 1 ) ) / ( (float) delta );
     743             :                     }
     744             :                 }
     745             :             }
     746             : 
     747    35621911 :             if ( st->hBWE_TD != NULL )
     748             :             {
     749     6907678 :                 st->hBWE_TD->bwe_non_lin_prev_scale = 0.0;
     750             :             }
     751             : 
     752    35621911 :             if ( st->hHQ_core != NULL && !( inner_frame_tbl[st->bwidth] == L_FRAME16k && st->core_brate <= HQ_32k ) )
     753             :             {
     754    35114450 :                 set_f( st->hHQ_core->prev_env, 0, SFM_N_WB );
     755    35114450 :                 set_f( st->hHQ_core->prev_normq, 0, SFM_N_WB );
     756             :             }
     757             : 
     758    35621911 :             mvr2r( synth, st->previoussynth, output_frame );
     759             : 
     760             :             /*Set post-filtering flag to zero*/
     761    35621911 :             if ( st->hBPF != NULL )
     762             :             {
     763     6304147 :                 st->hPFstat->on = 0;
     764             :             }
     765             :         }
     766             :         else
     767             :         {
     768             :             /* MDCT to ACELP transition */
     769     3611781 :             if ( st->last_core == HQ_CORE || st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE )
     770             :             {
     771      187839 :                 nZeros = (int16_t) ( NS2SA( st->output_Fs, N_ZERO_MDCT_NS ) );
     772      187839 :                 mvr2r( st->delay_buf_out, synth, delay_comp ); /* copy the HQ/ACELP delay synchronization buffer at the beginning of ACELP frame */
     773             : 
     774      187839 :                 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      187839 :                 tmpF = 1.0f / (float) NS2SA( st->output_Fs, 3000000 );
     780             : 
     781      187839 :                 if ( st->element_mode == IVAS_CPE_TD && st->hHQ_core == NULL )
     782             :                 {
     783      107832 :                     for ( i = 0; i < NS2SA( st->output_Fs, 3000000 ); i++ )
     784             :                     {
     785      106992 :                         synth[i + delay_comp] = ( 1 - tmpF * (float) i ) * output_mem[i] + tmpF * (float) i * synth[i + delay_comp];
     786             :                     }
     787             :                 }
     788      186999 :                 else if ( st->element_mode == IVAS_CPE_MDCT && st->core_brate <= SID_2k40 && st->prev_bfi )
     789             :                 {
     790       12216 :                     for ( i = 0; i < NS2SA( st->output_Fs, 3000000 ); i++ )
     791             :                     {
     792       12096 :                         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    23495583 :                     for ( i = 0; i < NS2SA( st->output_Fs, 3000000 ); i++ )
     798             :                     {
     799    23308704 :                         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     3611781 :             set_f( st->delay_buf_out, 0, HQ_DELTA_MAX * HQ_DELAY_COMP );
     805     3611781 :             if ( st->hHQ_core != NULL )
     806             :             {
     807     3538161 :                 st->hHQ_core->oldHqVoicing = 0;
     808             :             }
     809             :         }
     810             :     }
     811             :     else
     812             :     {
     813             :         /* memory update needed for DFT stereo -> TD stereo switching */
     814     2226612 :         mvr2r( synth + output_frame - delay_comp, st->delay_buf_out, delay_comp );
     815             :     }
     816             : 
     817             :     /* reset SWB BWE buffers */
     818    41460304 :     if ( st->bws_cnt == 0 || ( st->bws_cnt > 0 && st->coder_type != INACTIVE && st->coder_type != AUDIO ) )
     819             :     {
     820    41245097 :         st->attenu1 = 0.1f;
     821             :     }
     822             : 
     823    41460304 :     if ( st->hBWE_FD != NULL &&
     824    12130189 :          ( ( st->last_extl != SWB_BWE && st->extl == SWB_BWE ) || ( st->last_extl != FB_BWE && st->extl == FB_BWE ) ||
     825    12088423 :            ( ( 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       69390 :         set_f( st->hBWE_FD->old_wtda_swb, 0, output_frame );
     828             : 
     829       69390 :         if ( st->last_extl != WB_BWE )
     830             :         {
     831       67793 :             st->hBWE_FD->prev_mode = NORMAL;
     832             :         }
     833             : 
     834       69390 :         st->hBWE_FD->prev_Energy = 0.0f;
     835       69390 :         st->hBWE_FD->prev_L_swb_norm = 8;
     836       69390 :         st->hBWE_FD->prev_frica_flag = 0;
     837       69390 :         set_f( st->hBWE_FD->mem_imdct, 0, L_FRAME48k );
     838       69390 :         st->hBWE_FD->prev_td_energy = 0.0f;
     839       69390 :         st->hBWE_FD->prev_weight = 0.2f;
     840       69390 :         st->hBWE_FD->prev_fb_ener_adjust = 0.0f;
     841             :     }
     842             : 
     843             :     /* reset WB BWE buffers */
     844    41460304 :     if ( st->last_extl != WB_BWE && st->extl == WB_BWE && st->hBWE_FD != NULL )
     845             :     {
     846       19537 :         set_f( st->hBWE_FD->old_wtda_swb, 0, output_frame );
     847             : 
     848       19537 :         if ( st->last_extl != SWB_BWE && st->last_extl != FB_BWE )
     849             :         {
     850       18355 :             st->hBWE_FD->prev_mode = NORMAL;
     851             :         }
     852             : 
     853       19537 :         st->hBWE_FD->prev_Energy_wb = 0.0f;
     854       19537 :         st->hBWE_FD->prev_L_swb_norm = 8;
     855       19537 :         set_f( st->hBWE_FD->mem_imdct, 0, L_FRAME48k );
     856       19537 :         st->hBWE_FD->prev_flag = 0;
     857             :     }
     858             : 
     859             :     /* reset TBE buffers */
     860    41460304 :     if ( st->hBWE_TD != NULL )
     861             :     {
     862             :         /* reset SWB TBE buffers */
     863    12130189 :         if ( ( ( st->extl == SWB_TBE || st->extl == FB_TBE || st->extl == SWB_CNG ) &&
     864     2675549 :                ( 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    11926479 :              ( 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      454863 :             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      454863 :             set_f( st->hBWE_TD->GainShape_Delay, 0, NUM_SHB_SUBFR / 2 );
     871             : 
     872      454863 :             swb_tbe_reset_synth( st->hBWE_TD->genSHBsynth_Hilbert_Mem, st->hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local );
     873             : 
     874      454863 :             if ( output_frame == L_FRAME16k )
     875             :             {
     876       16112 :                 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      454863 :             set_f( st->hBWE_TD->int_3_over_2_tbemem_dec, 0.0f, INTERP_3_2_MEM_LEN );
     880      454863 :             st->hBWE_TD->prev_pow_exc16kWhtnd = 1.0f;
     881      454863 :             st->hBWE_TD->prev_mix_factor = 1.0f;
     882             :         }
     883    11675326 :         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     1740707 :             set_f( st->hBWE_TD->state_lpc_syn, 0.0f, LPC_SHB_ORDER );
     886     1740707 :             set_f( st->hBWE_TD->state_syn_shbexc, 0.0f, L_SHB_LAHEAD );
     887     1740707 :             set_f( st->hBWE_TD->mem_stp_swb, 0.0f, LPC_SHB_ORDER );
     888     1740707 :             set_f( st->hBWE_TD->mem_zero_swb, 0, LPC_SHB_ORDER );
     889     1740707 :             st->hBWE_TD->gain_prec_swb = 1.0f;
     890             :         }
     891     9934619 :         else if ( st->hBWE_TD != NULL && ( st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE ) )
     892             :         {
     893     7350282 :             TBEreset_dec( st );
     894             :         }
     895             : 
     896             :         /* reset FB TBE buffers */
     897    12130189 :         if ( ( st->L_frame != st->last_L_frame || st->last_extl != FB_TBE ) && st->extl == FB_TBE )
     898             :         {
     899       72106 :             set_f( st->hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
     900       72106 :             st->hBWE_TD->fb_tbe_demph = 0;
     901       72106 :             fb_tbe_reset_synth( st->hBWE_TD->fbbwe_hpf_mem, &st->hBWE_TD->prev_fbbwe_ratio );
     902             :         }
     903             : 
     904             :         /* reset WB TBE buffers */
     905    12130189 :         if ( st->last_extl != WB_TBE && st->extl == WB_TBE && st->last_core != TCX_20_CORE && st->last_core != TCX_10_CORE )
     906             :         {
     907        3445 :             wb_tbe_extras_reset( st->hBWE_TD->mem_genSHBexc_filt_down_wb2, st->hBWE_TD->mem_genSHBexc_filt_down_wb3 );
     908        3445 :             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        3445 :             set_f( st->hBWE_TD->state_syn_shbexc, 0, L_SHB_LAHEAD / 4 );
     911        3445 :             set_f( st->hBWE_TD->syn_overlap, 0, L_SHB_LAHEAD );
     912        3445 :             set_f( st->hBWE_TD->mem_csfilt, 0, 2 );
     913             :         }
     914             :     }
     915             : 
     916             :     /* Interp_3_2 CNG buffers reset */
     917    41460304 :     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        4050 :         set_f( st->hTdCngDec->interpol_3_2_cng_dec, 0.0f, INTERP_3_2_MEM_LEN );
     920             :     }
     921             : 
     922    41460304 :     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         564 : 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         564 :     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         564 :     if ( st->last_L_frame == L_FRAME )
     948             :     {
     949         127 :         if ( st->core_brate > ACELP_24k40 )
     950             :         {
     951           1 :             cbrate = ACELP_24k40;
     952             :         }
     953             :         else
     954             :         {
     955         126 :             cbrate = st->core_brate;
     956             :         }
     957             : 
     958             :         /* subtract ACELP switching frame bits */
     959         127 :         if ( st->core_brate >= ACELP_11k60 )
     960             :         {
     961         118 :             ( *num_bits )--; /* LP_FLAG bit */
     962             :         }
     963             : 
     964         127 :         *num_bits -= ACB_bits_tbl[BIT_ALLOC_IDX( cbrate, GENERIC, 0, 0 )];     /* pitch bits*/
     965         127 :         *num_bits -= gain_bits_tbl[BIT_ALLOC_IDX( cbrate, TRANSITION, 0, 0 )]; /* gain bits */
     966         127 :         *num_bits -= FCB_bits_tbl[BIT_ALLOC_IDX( cbrate, GENERIC, 0, 0 )];     /* FCB bits  */
     967             :     }
     968             :     else /* L_frame == L_FRAME16k */
     969             :     {
     970         437 :         if ( st->core_brate <= ACELP_8k00 )
     971             :         {
     972           0 :             cbrate = ACELP_8k00;
     973             :         }
     974         437 :         else if ( st->core_brate <= ACELP_14k80 )
     975             :         {
     976           0 :             cbrate = ACELP_14k80;
     977             :         }
     978             :         else
     979             :         {
     980         437 :             cbrate = min( st->core_brate, ACELP_22k60 );
     981             :         }
     982             : 
     983             :         /* subtract ACELP switching frame bits */
     984         437 :         if ( st->core_brate >= ACELP_11k60 )
     985             :         {
     986             :             /* subtract one bit for LP filtering flag */
     987         437 :             ( *num_bits )--;
     988             :         }
     989         437 :         *num_bits -= ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( cbrate, GENERIC, 0, 0 )];  /* pitch bits*/
     990         437 :         *num_bits -= gain_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( cbrate, GENERIC, 0, 0 )]; /* gain bits */
     991         437 :         *num_bits -= FCB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( cbrate, GENERIC, 0, 0 )];  /* FCB bits  */
     992             :     }
     993             : 
     994             :     /* subtract BWE bits */
     995         564 :     if ( !( ( inner_frame_tbl[st->bwidth] == L_FRAME16k && st->last_L_frame == L_FRAME16k ) || inner_frame_tbl[st->bwidth] == L_FRAME8k ) )
     996             :     {
     997         496 :         *num_bits -= ( NOOFGAINBITS1 + AUDIODELAYBITS );
     998             :     }
     999             : 
    1000             :     /* reset state of old_out if switching */
    1001         564 :     set_f( st->hHQ_core->old_out, 0.0f, output_frame );
    1002             : 
    1003         564 :     return;
    1004             : }
    1005             : 
    1006             : 
    1007             : /*---------------------------------------------------------------------*
    1008             :  * bandwidth_switching_detect()
    1009             :  *
    1010             :  * Classification for band-width switching
    1011             :  *---------------------------------------------------------------------*/
    1012             : 
    1013    41460304 : void bandwidth_switching_detect(
    1014             :     Decoder_State *st /* i/o: decoder state structure */
    1015             : )
    1016             : {
    1017    41460304 :     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    30002424 :         st->prev_bws_cnt = 0;
    1021    30002424 :         st->bws_cnt = 0;
    1022    30002424 :         st->bws_cnt1 = 0;
    1023             : 
    1024    30002424 :         return;
    1025             :     }
    1026             : 
    1027             :     /* update band-width switching counter */
    1028    11457880 :     if ( st->bws_cnt1 >= N_NS2W_FRAMES )
    1029             :     {
    1030      154601 :         st->bws_cnt1 = 0;
    1031             :     }
    1032    11303279 :     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         226 :         st->bws_cnt1++;
    1035             :     }
    1036    11303053 :     else if ( st->bws_cnt1 > 0 )
    1037             :     {
    1038       25651 :         st->bws_cnt = st->bwidth < st->last_bwidth ? 2 * ( N_NS2W_FRAMES - st->bws_cnt1 ) - 1 : 0;
    1039       25651 :         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    11457880 :     if ( st->bws_cnt >= N_WS2N_FRAMES )
    1044             :     {
    1045      153078 :         st->bws_cnt = 0;
    1046             :     }
    1047    11304802 :     else if ( st->total_brate < ACELP_9k60 && st->last_core_brate > ACELP_9k60 && st->bwidth < st->last_bwidth && st->bwidth == WB )
    1048             :     {
    1049       14514 :         st->bws_cnt++;
    1050             :     }
    1051    11290288 :     else if ( st->bws_cnt > 0 )
    1052             :     {
    1053      211307 :         st->bws_cnt1 = st->bwidth > st->last_bwidth ? ( ( N_WS2N_FRAMES - st->bws_cnt ) >> 1 ) : 0;
    1054      211307 :         st->bws_cnt = st->bwidth > st->last_bwidth ? 0 : ( ( st->bwidth == WB ) ? st->bws_cnt + 1 : 0 );
    1055             :     }
    1056             : 
    1057    11457880 :     return;
    1058             : }
    1059             : 
    1060             : 
    1061             : /*---------------------------------------------------------------------*
    1062             :  * bw_switching_pre_proc()
    1063             :  *
    1064             :  * Band-width switching pre-processing
    1065             :  *---------------------------------------------------------------------*/
    1066             : 
    1067    19289692 : 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    19289692 :     if ( st->element_mode > EVS_MONO )
    1078             :     {
    1079    19206696 :         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     4253472 :             calc_tilt_bwe( old_syn_12k8_16k, &st->tilt_wb, st->L_frame );
    1083             :         }
    1084             : 
    1085    19206696 :         return;
    1086             :     }
    1087             : 
    1088       82996 :     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       62802 :         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       62802 :         edct( old_syn_12k8_16k, syn_dct, L_FRAME, st->element_mode );
    1101             : 
    1102       62802 :         st->enerLL = EPSILON;
    1103     8101458 :         for ( i = 0; i < L_FRAME / 2; i++ )
    1104             :         {
    1105     8038656 :             st->enerLL += syn_dct[i] * syn_dct[i];
    1106             :         }
    1107       62802 :         st->enerLL = (float) sqrt( st->enerLL / 128 );
    1108             : 
    1109       62802 :         st->enerLH = EPSILON;
    1110     8101458 :         for ( ; i < L_FRAME; i++ )
    1111             :         {
    1112     8038656 :             st->enerLH += syn_dct[i] * syn_dct[i];
    1113             :         }
    1114       62802 :         st->enerLH = (float) sqrt( st->enerLH / 128 );
    1115             :     }
    1116             :     else
    1117             :     {
    1118       20194 :         if ( st->hHQ_core->old_is_transient[0] )
    1119             :         {
    1120         924 :             st->enerLL = EPSILON;
    1121       30492 :             for ( i = 0; i < 32; i++ )
    1122             :             {
    1123       29568 :                 st->enerLL += st->t_audio_q[i] * st->t_audio_q[i];
    1124             :             }
    1125         924 :             st->enerLL = (float) sqrt( st->enerLL / 32 );
    1126             : 
    1127         924 :             st->enerLH = EPSILON;
    1128       30492 :             for ( ; i < 64; i++ )
    1129             :             {
    1130       29568 :                 st->enerLH += st->t_audio_q[i] * st->t_audio_q[i];
    1131             :             }
    1132         924 :             st->enerLH = (float) sqrt( st->enerLH / 32 );
    1133             :         }
    1134             :         else
    1135             :         {
    1136       19270 :             st->enerLL = EPSILON;
    1137     2485830 :             for ( i = 0; i < L_FRAME / 2; i++ )
    1138             :             {
    1139     2466560 :                 st->enerLL += st->t_audio_q[i] * st->t_audio_q[i];
    1140             :             }
    1141       19270 :             st->enerLL = (float) sqrt( st->enerLL / 128 );
    1142             : 
    1143       19270 :             st->enerLH = EPSILON;
    1144     2485830 :             for ( ; i < L_FRAME; i++ )
    1145             :             {
    1146     2466560 :                 st->enerLH += st->t_audio_q[i] * st->t_audio_q[i];
    1147             :             }
    1148       19270 :             st->enerLH = (float) sqrt( st->enerLH / 128 );
    1149             :         }
    1150             :     }
    1151             : 
    1152       82996 :     if ( st->last_bwidth == 0 && st->extl <= SWB_CNG )
    1153             :     {
    1154        5157 :         st->prev_ener_shb = 0.0f;
    1155        5157 :         if ( st->hBWE_FD != NULL )
    1156             :         {
    1157        5157 :             set_f( st->hBWE_FD->prev_SWB_fenv, 0, SWB_FENV );
    1158             :         }
    1159             :     }
    1160       77839 :     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        1213 :         st->attenu1 = 0.1f;
    1163             :     }
    1164             : 
    1165       82996 :     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       50725 :         st->prev_fractive = 0;
    1168             :     }
    1169             : 
    1170       82996 :     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      218607 : 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      874428 :     for ( i = 0; i < CLDFB_OVRLP_MIN_SLOTS; i++ )
    1192             :     {
    1193      655821 :         set_f( realBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
    1194      655821 :         set_f( imagBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
    1195      655821 :         realBuffer[i] = realBufferTmp[i];
    1196      655821 :         imagBuffer[i] = imagBufferTmp[i];
    1197             :     }
    1198             : 
    1199             :     /* check if the CLDFB works on the right sample rate */
    1200      218607 :     if ( ( st->cldfbAna->no_channels * st->cldfbAna->no_col ) != st->L_frame )
    1201             :     {
    1202       48408 :         resampleCldfb( st->cldfbAna, st->L_frame * FRAMES_PER_SEC );
    1203             : 
    1204       48408 :         if ( st->cldfbBPF != NULL && st->L_frame <= L_FRAME16k )
    1205             :         {
    1206        8775 :             resampleCldfb( st->cldfbBPF, st->L_frame * FRAMES_PER_SEC );
    1207             :         }
    1208             : 
    1209       48408 :         if ( st->ini_frame > 0 )
    1210             :         {
    1211       48129 :             st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
    1212             :         }
    1213             :     }
    1214             : 
    1215             :     /* analysis of the synthesis at internal sampling rate */
    1216      218607 :     cldfbAnalysis( output, realBuffer, imagBuffer, CLDFB_OVRLP_MIN_SLOTS * st->cldfbAna->no_channels, st->cldfbAna );
    1217             : 
    1218             :     /* analysis and add the BPF error signal */
    1219      218607 :     if ( st->p_bpf_noise_buf )
    1220             :     {
    1221      178206 :         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      218607 :     if ( st->bwidth == NB )
    1226             :     {
    1227             :         /* set NB mask for upsampling */
    1228           0 :         st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - 10;
    1229             :     }
    1230      218607 :     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        3939 :         st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
    1234             :     }
    1235             : 
    1236             :     /* synthesis of the combined signal */
    1237      218607 :     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      218607 :     if ( st->hTcxDec != NULL )
    1241             :     {
    1242      218607 :         mvr2r( output, st->previoussynth, st->hTcxDec->L_frameTCX );
    1243             :     }
    1244             : 
    1245      218607 :     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        7545 : 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        7545 :     filter_len = TRANSITION_SMOOTHING_LEN_16k;
    1270        7545 :     if ( output_frame == L_FRAME32k )
    1271             :     {
    1272         990 :         filter_len = TRANSITION_SMOOTHING_LEN_32k;
    1273             :     }
    1274        6555 :     else if ( output_frame == L_FRAME48k )
    1275             :     {
    1276        5859 :         filter_len = TRANSITION_SMOOTHING_LEN_48k;
    1277             :     }
    1278             : 
    1279             :     /* prepare buffer */
    1280      162024 :     for ( i = 0; i < filter_len / 2; i++ )
    1281             :     {
    1282      154479 :         smoothing_input_buffer[i] = synth[0];
    1283             :     }
    1284        7545 :     mvr2r( synth, smoothing_input_buffer + filter_len / 2, 2 * delay_comp + filter_len / 2 );
    1285             : 
    1286             :     /* apply Mean filter */
    1287        7545 :     w = 1.f / filter_len;
    1288        7545 :     mem = sum_f( smoothing_input_buffer, filter_len );
    1289      817665 :     for ( i = 0; i < 2 * delay_comp; i++ )
    1290             :     {
    1291      810120 :         smoothing_out_buffer[i] = w * mem;
    1292      810120 :         mem = mem - smoothing_input_buffer[i] + smoothing_input_buffer[i + filter_len];
    1293             :     }
    1294             : 
    1295             :     /* apply fades around transition */
    1296        7545 :     step = 1.f / delay_comp;
    1297        7545 :     fade_in = 0.f;
    1298      412605 :     for ( i = 0; i < delay_comp; i++ )
    1299             :     {
    1300      405060 :         synth[i] = smoothing_out_buffer[i] * fade_in + synth[i] * ( 1 - fade_in );
    1301      405060 :         fade_in += step;
    1302             :     }
    1303             : 
    1304        7545 :     fade_in = 0.f;
    1305      412605 :     for ( ; i < 2 * delay_comp; i++ )
    1306             :     {
    1307      405060 :         synth[i] = synth[i] * fade_in + smoothing_out_buffer[i] * ( 1 - fade_in );
    1308      405060 :         fade_in += step;
    1309             :     }
    1310             : 
    1311        7545 :     return;
    1312             : }

Generated by: LCOV version 1.14