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

Generated by: LCOV version 1.14