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

Generated by: LCOV version 1.14