LCOV - code coverage report
Current view: top level - lib_dec - acelp_core_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main @ fec202a8f89be4a2f278a9fc377bfb58b58fab11 Lines: 477 487 97.9 %
Date: 2025-09-14 08:49:17 Functions: 1 1 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 <assert.h>
      39             : #include "options.h"
      40             : #ifdef DEBUGGING
      41             : #include "debug.h"
      42             : #endif
      43             : #include "cnst.h"
      44             : #include "rom_com.h"
      45             : #include "prot.h"
      46             : #include "ivas_cnst.h"
      47             : #include "ivas_prot.h"
      48             : #include "ivas_rom_com.h"
      49             : #include "wmc_auto.h"
      50             : 
      51             : /*-------------------------------------------------------------------*
      52             :  * acelp_core_dec()
      53             :  *
      54             :  * ACELP core decoder
      55             :  *-------------------------------------------------------------------*/
      56             : 
      57     3450752 : ivas_error acelp_core_dec(
      58             :     Decoder_State *st,                   /* i/o: decoder state structure                                                 */
      59             :     float output[],                      /* o  : synthesis @internal Fs                                                  */
      60             :     float synth[],                       /* o  : synthesis                                                               */
      61             :     float save_hb_synth[],               /* o  : HB synthesis                                                            */
      62             :     float bwe_exc_extended[],            /* i/o: bandwidth extended excitation                                           */
      63             :     float *voice_factors,                /* o  : voicing factors                                                         */
      64             :     float old_syn_12k8_16k[],            /* o  : intermediate ACELP synthesis at 12.8kHz or 16kHz to be used by SWB BWE  */
      65             :     const int16_t sharpFlag,             /* i  : formant sharpening flag                                                 */
      66             :     float pitch_buf[NB_SUBFR16k],        /* o  : floating pitch for each subframe                                        */
      67             :     int16_t *unbits,                     /* o  : number of unused bits                                                   */
      68             :     int16_t *sid_bw,                     /* o  : 0-NB/WB, 1-SWB SID                                                      */
      69             :     STEREO_TD_DEC_DATA_HANDLE hStereoTD, /* i/o: TD stereo decoder handle                                                */
      70             :     const float tdm_lsfQ_PCh[M],         /* i  : Q LSFs for primary channel                                              */
      71             :     const int16_t use_cldfb_for_dft,     /* i  : flag to use of CLDFB for DFT Stereo                                     */
      72             :     const int16_t last_element_mode,     /* i  : last element mode                                                       */
      73             :     const int32_t last_element_brate,    /* i  : last element bitrate                                                    */
      74             :     const int16_t flag_sec_CNA,          /* i  : CNA flag for secondary channel                                          */
      75             :     const int16_t nchan_out,             /* i  : number of output channels                                               */
      76             :     STEREO_CNG_DEC_HANDLE hStereoCng,    /* i  : stereo CNG handle                                                       */
      77             :     const int16_t read_sid_info          /* i  : read SID info flag                                                      */
      78             : )
      79             : {
      80             :     float old_exc[L_EXC_DEC], *exc;                                              /* excitation signal buffer              */
      81             :     float syn_tmp[L_FRAME16k + L_SUBFR], *syn;                                   /* synthesis signal buffer               */
      82             :     int16_t output_frame;                                                        /* frame length at output sampling freq. */
      83             :     float lsf_new[M];                                                            /* LSFs at the end of the frame          */
      84             :     float lsp_new[M];                                                            /* LSPs at the end of the frame          */
      85             :     float lsp_mid[M];                                                            /* LSPs in the middle of the frame       */
      86             :     float Aq[NB_SUBFR16k * ( M + 1 )];                                           /* A(q) quantized for the 4 subframes    */
      87             :     float old_exc2[L_FRAME16k + L_EXC_MEM], *exc2;                               /* total excitation buffer               */
      88             :     float mem_tmp[M];                                                            /* temporary synthesis filter memory     */
      89             :     float enr_q;                                                                 /* E information for FER protection      */
      90             :     float tmp_noise;                                                             /* Long term temporary noise energy      */
      91             :     float Es_pred;                                                               /* predicted scaled innov. energy        */
      92             :     float FEC_pitch;                                                             /* FEC pitch                             */
      93             :     float old_bwe_exc[( ( PIT16k_MAX + ( L_FRAME16k + 1 ) + L_SUBFR16k ) * 2 )]; /* excitation buffer                     */
      94             :     float *bwe_exc;                                                              /* Excitation for SWB TBE                */
      95             :     int16_t i, int_fs;
      96             :     int16_t tc_subfr;
      97             :     int16_t allow_cn_step;
      98             :     float temp_buf[L_FRAME16k + L_SYN_MEM];
      99             :     int16_t last_pulse_pos;
     100             :     int16_t T0_tmp;
     101             :     int16_t do_WI;
     102             :     float dct_buffer[DCT_L_POST];
     103             :     float exc_buffer[DCT_L_POST];
     104             :     float dct_exc_tmp[L_FRAME16k];
     105             :     float bpf_error_signal[L_FRAME16k];
     106             :     int16_t nb_bits; /* number of bits                       */
     107             :     int16_t indice;  /* parameter indices to write           */
     108             :     float gain_buf[NB_SUBFR16k];
     109             :     float q_env[20];
     110             :     float exc3[L_FRAME16k];
     111             :     float syn1_tmp[L_FRAME16k + 2], *syn1;
     112             :     float *realBuffer[CLDFB_NO_COL_MAX], *imagBuffer[CLDFB_NO_COL_MAX];
     113             :     float realBufferTmp[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
     114             :     float imagBufferTmp[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
     115             :     int16_t LSF_Q_prediction; /* LSF prediction mode                  */
     116             :     float tmpF;
     117             :     int16_t uc_two_stage_flag;
     118             :     int16_t tdm_lp_reuse_flag, tdm_low_rate_mode, tdm_Pitch_reuse_flag;
     119             :     float *old_exc_s; /* Start of last excitation frame       */
     120             :     float *p_tdm_Pri_pitch_buf;
     121             :     int16_t local_element_mode;
     122             :     ivas_error error;
     123             : 
     124     3450752 :     error = IVAS_ERR_OK;
     125             : 
     126     3450752 :     if ( st->element_mode == IVAS_CPE_MDCT && nchan_out == 1 && st->idchan == 1 && last_element_brate <= IVAS_SID_5k2 )
     127             :     {
     128             :         /* In MDCT-Stereo DTX with mono output, we can skip CNG for the second channel, except for the first inactive frame following an active period */
     129        6711 :         return error;
     130             :     }
     131             : 
     132     3444041 :     push_wmops( "acelp_core_dec" );
     133             : 
     134     3444041 :     output_frame = (int16_t) ( st->output_Fs / FRAMES_PER_SEC );
     135             : 
     136             :     /*----------------------------------------------------------------*
     137             :      * stereo SID and CNG frames processing
     138             :      *----------------------------------------------------------------*/
     139             : 
     140     3444041 :     if ( st->core_brate <= SID_2k40 && st->element_mode == IVAS_CPE_DFT && nchan_out == 2 )
     141             :     {
     142       31996 :         if ( st->cng_type == FD_CNG )
     143             :         {
     144       25068 :             configureFdCngDec( st->hFdCngDec, st->bwidth, ACELP_14k25, st->L_frame, st->last_L_frame, st->element_mode );
     145             : 
     146             :             /* Only run parameter decoding in SID frames */
     147       25068 :             if ( st->core_brate == SID_2k40 )
     148             :             {
     149        3314 :                 FdCng_decodeSID( st );
     150             :             }
     151             : 
     152      626700 :             for ( i = 0; i < NPART; i++ )
     153             :             {
     154      601632 :                 st->hFdCngDec->hFdCngCom->sidNoiseEstLp[i] = STEREO_DFT_FD_FILT * st->hFdCngDec->hFdCngCom->sidNoiseEstLp[i] + ( 1 - STEREO_DFT_FD_FILT ) * st->hFdCngDec->hFdCngCom->sidNoiseEst[i];
     155             :             }
     156             : 
     157       25068 :             ApplyFdCng( NULL, NULL, NULL, NULL, st, 0, 0 );
     158             :         }
     159             :         else
     160             :         {
     161        6928 :             configureFdCngDec( st->hFdCngDec, st->bwidth, ACELP_14k25, st->L_frame, st->last_L_frame, st->element_mode );
     162             : 
     163             :             /* decode CNG parameters */
     164        6928 :             CNG_dec( st, last_element_mode, Aq, lsp_new, lsf_new, &allow_cn_step, sid_bw, q_env );
     165             : 
     166             :             /* comfort noise generation */
     167        6928 :             CNG_exc( st->core_brate, st->L_frame, &st->hTdCngDec->Enew, &st->hTdCngDec->cng_seed, NULL, NULL, &st->lp_ener, st->last_core_brate, &st->first_CNG, &( st->hTdCngDec->cng_ener_seed ), NULL, allow_cn_step, &st->hTdCngDec->last_allow_cn_step, st->hTdCngDec->num_ho, q_env, st->hTdCngDec->lp_env, st->hTdCngDec->old_env, st->hTdCngDec->exc_mem, st->hTdCngDec->exc_mem1, sid_bw, &st->hTdCngDec->cng_ener_seed1, NULL, st->Opt_AMR_WB, st->element_mode );
     168             : 
     169        6928 :             mvr2r( Aq, st->Aq_cng, M + 1 );
     170             : 
     171             :             /* update old LSP and LSF vector */
     172        6928 :             mvr2r( lsf_new, st->lsf_old, M );
     173        6928 :             mvr2r( lsp_new, st->lsp_old, M );
     174             :         }
     175             : 
     176       31996 :         set_f( output, 0, output_frame ); /* output and synth are not used in DFT domain CNG generation and the decoder output is unaffected if they are left uninitalized */
     177       31996 :         set_f( synth, 0, output_frame );  /* They are however read in a few places which causes errors in the valgrind tests. Simplest solution from a code perspective was to set them to zero. */
     178             : 
     179             :         /* CN generation done in DFT domain */
     180       31996 :         pop_wmops();
     181       31996 :         return error;
     182             :     }
     183             : 
     184             :     /*----------------------------------------------------------------*
     185             :      * Active frames processing
     186             :      *----------------------------------------------------------------*/
     187             : 
     188             :     /* open CLDFB buffer up to CLDFB_NO_CHANNELS_MAX bands for 48kHz */
     189    58004765 :     for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
     190             :     {
     191    54592720 :         set_f( realBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
     192    54592720 :         set_f( imagBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
     193    54592720 :         realBuffer[i] = realBufferTmp[i];
     194    54592720 :         imagBuffer[i] = imagBufferTmp[i];
     195             :     }
     196             : 
     197             :     /*----------------------------------------------------------------*
     198             :      * Initialization
     199             :      *----------------------------------------------------------------*/
     200             : 
     201     3412045 :     LSF_Q_prediction = -1;
     202     3412045 :     set_f( syn_tmp, 0, L_SUBFR );
     203     3412045 :     syn = syn_tmp + L_SUBFR;
     204     3412045 :     syn1_tmp[0] = 0;
     205     3412045 :     syn1_tmp[1] = 0;
     206     3412045 :     syn1 = syn1_tmp + 2;
     207     3412045 :     st->bpf_off = 0;
     208             : 
     209     3412045 :     if ( st->last_core == HQ_CORE || st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE || ( st->element_mode == IVAS_CPE_DFT && st->last_core_brate <= SID_2k40 ) || ( st->element_mode == IVAS_CPE_MDCT && st->last_core_brate <= SID_2k40 ) )
     210             :     {
     211             :         /* in case of HQ->ACELP switching, do not apply BPF */
     212      242340 :         st->bpf_off = 1;
     213             : 
     214      242340 :         if ( st->hPFstat != NULL )
     215             :         {
     216             :             /* in case of core switching, reset post-filter memories */
     217      200849 :             st->hPFstat->on = 0;
     218             :         }
     219             : 
     220      242340 :         if ( st->hGSCDec != NULL )
     221             :         {
     222             :             /* reset the GSC pre echo energy threshold in case of switching */
     223      200849 :             st->hGSCDec->Last_frame_ener = (float) MAX_32;
     224             :         }
     225             :     }
     226             : 
     227     3412045 :     if ( st->hGSCDec != NULL && st->prev_bfi > 0 )
     228             :     {
     229             :         /* reset the GSC pre echo energy threshold in case of FEC */
     230      234771 :         st->hGSCDec->Last_frame_ener = (float) MAX_32;
     231             :     }
     232             : 
     233     3412045 :     if ( st->hFdCngDec != NULL && ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD ) && ( st->last_core_brate == SID_2k40 || st->last_core_brate == FRAME_NO_DATA ) )
     234             :     {
     235       27966 :         set_zero( st->hFdCngDec->hFdCngCom->olapBufferSynth2, FFTLEN );
     236       27966 :         set_zero( hStereoCng->olapBufferSynth22, FFTLEN );
     237             :     }
     238             : 
     239     3412045 :     st->clas_dec = st->last_good;
     240     3412045 :     enr_q = 0.0f;
     241     3412045 :     Es_pred = 0.0f;
     242     3412045 :     tmp_noise = 0.0f;
     243             : 
     244     3412045 :     mvr2r( st->old_exc, old_exc, L_EXC_MEM_DEC );
     245     3412045 :     exc = old_exc + L_EXC_MEM_DEC;
     246     3412045 :     if ( st->hWIDec != NULL )
     247             :     {
     248       11837 :         mvr2r( st->hWIDec->old_exc2, old_exc2, L_EXC_MEM );
     249             :     }
     250             :     else
     251             :     {
     252     3400208 :         set_f( old_exc2, 0, L_EXC_MEM );
     253             :     }
     254     3412045 :     exc2 = old_exc2 + L_EXC_MEM;
     255     3412045 :     if ( st->hBWE_TD != NULL )
     256             :     {
     257     3368061 :         mvr2r( st->hBWE_TD->old_bwe_exc, old_bwe_exc, PIT16k_MAX * 2 );
     258     3368061 :         bwe_exc = old_bwe_exc + PIT16k_MAX * 2;
     259             :     }
     260             :     else
     261             :     {
     262       43984 :         bwe_exc = NULL;
     263             :     }
     264             : 
     265     3412045 :     last_pulse_pos = 0;
     266     3412045 :     do_WI = 0;
     267     3412045 :     st->GSC_noisy_speech = 0;
     268     3412045 :     st->relax_prev_lsf_interp = 0;
     269     3412045 :     set_zero( gain_buf, NB_SUBFR16k );
     270             : 
     271     3412045 :     if ( st->L_frame == L_FRAME )
     272             :     {
     273     1789235 :         st->gamma = GAMMA1;
     274     1789235 :         st->preemph_fac = PREEMPH_FAC;
     275     1789235 :         int_fs = INT_FS_12k8;
     276             :     }
     277             :     else
     278             :     {
     279     1622810 :         st->gamma = GAMMA16k;
     280     1622810 :         st->preemph_fac = PREEMPH_FAC_16k;
     281     1622810 :         int_fs = INT_FS_16k;
     282             :     }
     283             : 
     284             :     /* reset post-filter in case of switching */
     285     3412045 :     if ( st->hPFstat != NULL && st->hPFstat->on == 0 )
     286             :     {
     287      527739 :         st->hPFstat->reset = 1;
     288             :     }
     289             : 
     290             :     /* TD stereo parameters */
     291     3412045 :     if ( st->element_mode == IVAS_CPE_TD && st->idchan == 1 )
     292             :     {
     293       39134 :         tdm_lp_reuse_flag = hStereoTD->tdm_lp_reuse_flag;
     294       39134 :         tdm_low_rate_mode = hStereoTD->tdm_low_rate_mode;
     295       39134 :         tdm_Pitch_reuse_flag = hStereoTD->tdm_Pitch_reuse_flag;
     296       39134 :         p_tdm_Pri_pitch_buf = hStereoTD->tdm_Pri_pitch_buf;
     297             :     }
     298             :     else
     299             :     {
     300     3372911 :         tdm_lp_reuse_flag = 0;
     301     3372911 :         tdm_low_rate_mode = 0;
     302     3372911 :         if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
     303             :         {
     304      287166 :             tdm_low_rate_mode = 1;
     305             :         }
     306     3372911 :         tdm_Pitch_reuse_flag = 0;
     307     3372911 :         p_tdm_Pri_pitch_buf = NULL;
     308             :     }
     309             : 
     310             :     /*----------------------------------------------------------------*
     311             :      * Updates in case of internal sampling rate switching
     312             :      *----------------------------------------------------------------*/
     313             : 
     314     3412045 :     if ( st->last_L_frame != st->L_frame && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE ) )
     315             :     {
     316             :         int16_t dec;
     317             : 
     318       11655 :         if ( st->hPFstat->on != 0 )
     319             :         {
     320             :             int16_t mem_syn_r_size_old, mem_syn_r_size_new;
     321             : 
     322       10557 :             mem_syn_r_size_old = (int16_t) ( 1.25 * st->last_L_frame / 20.f );
     323       10557 :             mem_syn_r_size_new = (int16_t) ( 1.25 * st->L_frame / 20.f );
     324       10557 :             lerp( st->hPFstat->mem_stp + L_SYN_MEM - mem_syn_r_size_old, st->hPFstat->mem_stp + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
     325       10557 :             lerp( st->hPFstat->mem_pf_in + L_SYN_MEM - mem_syn_r_size_old, st->hPFstat->mem_pf_in + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
     326             :         }
     327             : 
     328             :         /* convert quantized LSP vector */
     329       11655 :         st->rate_switching_reset = lsp_convert_poly( st->lsp_old, st->L_frame, 0 );
     330             : 
     331             :         /* convert old quantized LSF vector */
     332       11655 :         lsp2lsf( st->lsp_old, st->lsf_old, M, int_fs );
     333             : 
     334             :         /* FEC - update adaptive LSF mean vector */
     335       11655 :         mvr2r( st->lsf_old, st->lsfoldbfi1, M );
     336       11655 :         mvr2r( st->lsf_old, st->lsfoldbfi0, M );
     337       11655 :         mvr2r( st->lsf_old, st->lsf_adaptive_mean, M );
     338             : 
     339             :         /* Reset LPC mem */
     340       11655 :         if ( st->sr_core == INT_FS_16k )
     341             :         {
     342        7104 :             mvr2r( GEWB2_Ave, st->mem_AR, M );
     343             :         }
     344             :         else
     345             :         {
     346        4551 :             mvr2r( GEWB_Ave, st->mem_AR, M );
     347             :         }
     348       11655 :         set_zero( st->mem_MA, M );
     349             : 
     350             :         /* update synthesis filter memories */
     351       11655 :         dec = DEC;
     352       11655 :         if ( st->element_mode != EVS_MONO )
     353             :         {
     354       11519 :             dec = DEC_IVAS;
     355             :         }
     356       11655 :         synth_mem_updt2( st->L_frame, st->last_L_frame, st->old_exc, st->mem_syn_r, st->mem_syn2, NULL, dec );
     357       11655 :         mvr2r( st->old_exc, old_exc, L_EXC_MEM_DEC );
     358       11655 :         mvr2r( st->mem_syn2, st->mem_syn1, M );
     359       11655 :         mvr2r( st->mem_syn2, st->mem_syn3, M );
     360             :     }
     361             : 
     362             :     /* update buffer of old subframe pitch values */
     363     3412045 :     if ( st->last_L_frame != st->L_frame )
     364             :     {
     365       37603 :         if ( st->L_frame == L_FRAME )
     366             :         {
     367       24530 :             if ( st->last_L_frame == L_FRAME32k )
     368             :             {
     369        5881 :                 tmpF = (float) 12800 / (float) 32000;
     370             :             }
     371       18649 :             else if ( st->last_L_frame == 512 )
     372             :             {
     373        7117 :                 tmpF = (float) 12800 / (float) 25600;
     374             :             }
     375             :             else /* st->last_L_frame == L_FRAME16k */
     376             :             {
     377       11532 :                 tmpF = (float) 12800 / (float) 16000;
     378             :             }
     379             : 
     380      122650 :             for ( i = NB_SUBFR16k - NB_SUBFR; i < NB_SUBFR16k; i++ )
     381             :             {
     382       98120 :                 st->old_pitch_buf[i - 1] = tmpF * st->old_pitch_buf[i];
     383             :             }
     384             : 
     385      122650 :             for ( i = 2 * NB_SUBFR16k - NB_SUBFR; i < 2 * NB_SUBFR16k; i++ )
     386             :             {
     387       98120 :                 st->old_pitch_buf[i - 2] = tmpF * st->old_pitch_buf[i];
     388             :             }
     389             :         }
     390             :         else
     391             :         {
     392       13073 :             if ( st->last_L_frame == L_FRAME32k )
     393             :             {
     394        1473 :                 tmpF = (float) 16000 / (float) 32000;
     395             :             }
     396       11600 :             else if ( st->last_L_frame == 512 )
     397             :             {
     398        3298 :                 tmpF = (float) 16000 / (float) 25600;
     399             :             }
     400             :             else /* st->last_L_frame == L_FRAME12k8 */
     401             :             {
     402        8302 :                 tmpF = (float) 16000 / (float) 12800;
     403             :             }
     404             : 
     405       65365 :             for ( i = 2 * NB_SUBFR - 1; i >= NB_SUBFR; i-- )
     406             :             {
     407       52292 :                 st->old_pitch_buf[i + 2] = tmpF * st->old_pitch_buf[i];
     408             :             }
     409       13073 :             st->old_pitch_buf[NB_SUBFR + 1] = st->old_pitch_buf[NB_SUBFR + 2];
     410             : 
     411       65365 :             for ( i = NB_SUBFR - 1; i >= 0; i-- )
     412             :             {
     413       52292 :                 st->old_pitch_buf[i + 1] = tmpF * st->old_pitch_buf[i];
     414             :             }
     415       13073 :             st->old_pitch_buf[0] = st->old_pitch_buf[1];
     416             :         }
     417             :     }
     418             : 
     419     3412045 :     if ( st->bfi_pitch_frame != st->L_frame )
     420             :     {
     421       85012 :         if ( st->L_frame == L_FRAME )
     422             :         {
     423       24249 :             if ( st->bfi_pitch_frame == L_FRAME32k )
     424             :             {
     425        5915 :                 tmpF = (float) 12800 / (float) 32000;
     426             :             }
     427       18334 :             else if ( st->bfi_pitch_frame == 512 )
     428             :             {
     429        7183 :                 tmpF = (float) 12800 / (float) 25600;
     430             :             }
     431             :             else /* st->bfi_pitch_frame == L_FRAME16k */
     432             :             {
     433       11151 :                 tmpF = (float) 12800 / (float) 16000;
     434             :             }
     435       24249 :             st->bfi_pitch *= tmpF;
     436       24249 :             st->bfi_pitch_frame = L_FRAME;
     437             :         }
     438             :         else
     439             :         {
     440       60763 :             if ( st->bfi_pitch_frame == L_FRAME32k )
     441             :             {
     442        1475 :                 tmpF = (float) 16000 / (float) 32000;
     443             :             }
     444       59288 :             else if ( st->bfi_pitch_frame == 512 )
     445             :             {
     446        3304 :                 tmpF = (float) 16000 / (float) 25600;
     447             :             }
     448             :             else /* st->bfi_pitch_frame == L_FRAME12k8 */
     449             :             {
     450       55984 :                 tmpF = (float) 16000 / (float) 12800;
     451             :             }
     452       60763 :             st->bfi_pitch *= tmpF;
     453       60763 :             st->bfi_pitch_frame = L_FRAME16k;
     454             :         }
     455             :     }
     456             : 
     457     3412045 :     if ( st->last_bwidth == NB && st->bwidth != NB && st->ini_frame != 0 )
     458             :     {
     459           5 :         st->rate_switching_reset = 1;
     460             :     }
     461             : 
     462             :     /*----------------------------------------------------------------------*
     463             :      * GOOD frame
     464             :      *----------------------------------------------------------------------*/
     465             : 
     466     3412045 :     if ( !st->bfi )
     467             :     {
     468             : 
     469             :         /*----------------------------------------------------------------*
     470             :          * Decoding of TC subframe classification
     471             :          *----------------------------------------------------------------*/
     472             : 
     473     3179351 :         tc_subfr = -1;
     474     3179351 :         if ( st->coder_type == TRANSITION )
     475             :         {
     476      289393 :             tc_subfr = tc_classif( st );
     477             :         }
     478             : 
     479             :         /*----------------------------------------------------------------*
     480             :          * Decoding of GSC IVAS mode
     481             :          *----------------------------------------------------------------*/
     482             : 
     483     3179351 :         if ( st->element_mode > EVS_MONO && st->idchan == 0 && !( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 ) && !tdm_low_rate_mode )
     484             :         {
     485     2692712 :             if ( st->coder_type == AUDIO || ( st->coder_type == INACTIVE && st->inactive_coder_type_flag ) )
     486             :             {
     487      207573 :                 st->GSC_IVAS_mode = get_next_indice( st, 2 );
     488             :             }
     489             :         }
     490             : 
     491             :         /*----------------------------------------------------------------*
     492             :          * Decoding of inactive CNG frames
     493             :          *----------------------------------------------------------------*/
     494             : 
     495     3179351 :         if ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 )
     496             :         {
     497             :             /* decode CNG parameters */
     498      124501 :             if ( st->cng_type == LP_CNG )
     499             :             {
     500        8106 :                 CNG_dec( st, last_element_mode, Aq, lsp_new, lsf_new, &allow_cn_step, sid_bw, q_env );
     501             : 
     502             :                 /* comfort noise generation */
     503        8106 :                 local_element_mode = st->element_mode;
     504        8106 :                 if ( ( nchan_out == 1 && st->element_mode == IVAS_CPE_DFT ) || ( st->masa_sid_format == 1 ) )
     505             :                 {
     506        6170 :                     local_element_mode = IVAS_SCE; /* For DFT Stereo mono decoding, run CNG_exc as in SCE */
     507             :                 }
     508        8106 :                 CNG_exc( st->core_brate, st->L_frame, &st->hTdCngDec->Enew, &st->hTdCngDec->cng_seed, exc, exc2, &st->lp_ener, st->last_core_brate, &st->first_CNG, &( st->hTdCngDec->cng_ener_seed ), bwe_exc, allow_cn_step, &st->hTdCngDec->last_allow_cn_step, st->hTdCngDec->num_ho, q_env, st->hTdCngDec->lp_env, st->hTdCngDec->old_env, st->hTdCngDec->exc_mem, st->hTdCngDec->exc_mem1, sid_bw, &st->hTdCngDec->cng_ener_seed1, exc3, st->Opt_AMR_WB, local_element_mode );
     509        8106 :                 mvr2r( Aq, st->Aq_cng, M + 1 );
     510             :             }
     511             :             else
     512             :             {
     513      116395 :                 if ( st->core_brate == SID_2k40 && st->element_mode != IVAS_CPE_MDCT )
     514             :                 {
     515        9161 :                     FdCng_decodeSID( st );
     516        9161 :                     *sid_bw = 0;
     517             :                 }
     518             : 
     519      116395 :                 if ( st->element_mode == IVAS_CPE_DFT )
     520             :                 {
     521       20897 :                     assert( nchan_out == 1 );
     522             : 
     523      522425 :                     for ( i = 0; i < NPART; i++ )
     524             :                     {
     525      501528 :                         st->hFdCngDec->hFdCngCom->sidNoiseEstLp[i] = STEREO_DFT_FD_FILT * st->hFdCngDec->hFdCngCom->sidNoiseEstLp[i] + ( 1 - STEREO_DFT_FD_FILT ) * st->hFdCngDec->hFdCngCom->sidNoiseEst[i];
     526             :                     }
     527       20897 :                     ApplyFdCng( syn, NULL, realBuffer, imagBuffer, st, 0, ( st->coder_type == AUDIO && !st->GSC_noisy_speech ) );
     528             :                 }
     529             : 
     530      116395 :                 if ( !read_sid_info )
     531             :                 {
     532             :                     float noise_lvl_highest;
     533             : 
     534       18900 :                     noise_lvl_highest = st->hFdCngDec->hFdCngCom->cngNoiseLevel[st->hFdCngDec->hFdCngCom->stopFFTbin - st->hFdCngDec->hFdCngCom->startBand - 1];
     535      367940 :                     for ( int16_t b = st->hFdCngDec->hFdCngCom->stopFFTbin - st->hFdCngDec->hFdCngCom->startBand; b < st->hFdCngDec->hFdCngCom->stopBand; b++ )
     536             :                     {
     537      349040 :                         st->hFdCngDec->hFdCngCom->cngNoiseLevel[b] = noise_lvl_highest;
     538             :                     }
     539             :                 }
     540             : 
     541      116395 :                 generate_comfort_noise_dec( NULL, NULL, st, nchan_out );
     542             : 
     543      116395 :                 FdCng_exc( st->hFdCngDec->hFdCngCom, &st->CNG_mode, st->L_frame, st->lsp_old, st->first_CNG, st->lspCNG, Aq, lsp_new, lsf_new, exc, exc2, bwe_exc );
     544             : 
     545      116395 :                 mvr2r( exc2, exc3, st->L_frame );
     546             :             }
     547             : 
     548             :             /* update past excitation signals for LD music post-filter */
     549      124501 :             if ( st->hMusicPF != NULL )
     550             :             {
     551       83010 :                 mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
     552       83010 :                 mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
     553             : 
     554             :                 /* Update music post processing values */
     555             :                 /* Filter energies update */
     556    53209410 :                 for ( i = 0; i < DCT_L_POST; i++ )
     557             :                 {
     558    53126400 :                     st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
     559             :                 }
     560             :             }
     561             : 
     562             : #ifdef DEBUG_MODE_ACELP
     563             :             dbgwrite( exc, sizeof( float ), st->L_frame, 1, fname( debug_dir, "exc", st->idchan, st->id_element, DEC ) );
     564             : #endif
     565             : 
     566             :             /* synthesis at 12.8kHz sampling rate */
     567      124501 :             syn_12k8( st->L_frame, Aq, exc2, syn, st->mem_syn2, 1 );
     568      124501 :             syn_12k8( st->L_frame, Aq, exc3, syn1, st->mem_syn3, 1 );
     569             : 
     570             :             /* reset the decoder */
     571      124501 :             CNG_reset_dec( st, pitch_buf, voice_factors );
     572             : 
     573             :             /* update st->mem_syn1 for ACELP core switching */
     574      124501 :             mvr2r( st->mem_syn3, st->mem_syn1, M );
     575             : 
     576             :             /* update old synthesis for classification */
     577      124501 :             mvr2r( syn1 + st->L_frame - L_SYN_MEM_CLAS_ESTIM, st->mem_syn_clas_estim, L_SYN_MEM_CLAS_ESTIM );
     578             : 
     579             :             /* save and delay synthesis to be used by SWB BWE */
     580      124501 :             if ( st->hBWE_FD != NULL )
     581             :             {
     582       83470 :                 save_old_syn( st->L_frame, syn1, old_syn_12k8_16k, st->hBWE_FD->old_syn_12k8_16k, st->preemph_fac, &st->hBWE_FD->mem_deemph_old_syn );
     583             :             }
     584             :         }
     585             : 
     586             :         /*----------------------------------------------------------------*
     587             :          * Decoding of all other frames
     588             :          *----------------------------------------------------------------*/
     589             : 
     590             :         else
     591             :         {
     592             :             /*-----------------------------------------------------------------*
     593             :              * Configure ACELP bit allocation
     594             :              *-----------------------------------------------------------------*/
     595             : 
     596     3054850 :             nb_bits = 0;
     597     3054850 :             st->acelp_cfg.FEC_mode = 0;
     598     3054850 :             uc_two_stage_flag = 0;
     599             : 
     600     3054850 :             if ( !st->nelp_mode_dec && !st->ppp_mode_dec )
     601             :             {
     602             :                 int16_t tc_subfr_tmp;
     603             : 
     604     3054417 :                 tc_subfr_tmp = tc_subfr;
     605     3054417 :                 if ( tc_subfr_tmp < L_SUBFR )
     606             :                 {
     607     2904714 :                     tc_subfr_tmp = 0;
     608             :                 }
     609             : 
     610     3054417 :                 if ( tc_subfr == TC_0_192 )
     611             :                 {
     612        2509 :                     nb_bits = -1;
     613             :                 }
     614             : 
     615     3054417 :                 config_acelp1( DEC, st->total_brate, st->core_brate, st->core, st->extl_orig, st->extl_brate_orig, st->L_frame, st->GSC_noisy_speech, &( st->acelp_cfg ), st->next_bit_pos, st->coder_type, st->inactive_coder_type_flag, tc_subfr_tmp, 1, &nb_bits, unbits, st->element_mode, &uc_two_stage_flag, tdm_lp_reuse_flag, tdm_low_rate_mode, st->idchan, tdm_Pitch_reuse_flag, st->tdm_LRTD_flag, st->GSC_IVAS_mode );
     616             : 
     617     3054417 :                 if ( st->coder_type == TRANSITION && tc_subfr < L_SUBFR && st->L_frame == L_FRAME )
     618             :                 {
     619       73655 :                     config_acelp1( DEC, st->total_brate, st->core_brate, st->core, st->extl_orig, st->extl_brate_orig, st->L_frame, -1, &( st->acelp_cfg ), st->next_bit_pos, TRANSITION, -1, tc_subfr, 2, &nb_bits, unbits, st->element_mode, &uc_two_stage_flag, tdm_lp_reuse_flag, tdm_low_rate_mode, st->idchan, tdm_Pitch_reuse_flag, st->tdm_LRTD_flag, st->GSC_IVAS_mode );
     620             :                 }
     621             :             }
     622             : 
     623             :             /*-----------------------------------------------------------------*
     624             :              * After CNG period, use the most up-to-date LSPs
     625             :              *-----------------------------------------------------------------*/
     626             : 
     627     3054850 :             if ( st->hTdCngDec != NULL && ( st->last_core_brate == FRAME_NO_DATA || st->last_core_brate == SID_2k40 ) )
     628             :             {
     629        4102 :                 mvr2r( st->lspCNG, st->lsp_old, M );
     630        4102 :                 lsp2lsf( st->lspCNG, st->lsf_old, M, int_fs );
     631             :             }
     632             : 
     633             :             /*-----------------------------------------------------------------*
     634             :              * Reset higher ACELP pre-quantizer in case of switching
     635             :              *-----------------------------------------------------------------*/
     636             : 
     637     3054850 :             if ( !st->use_acelp_preq )
     638             :             {
     639     2850901 :                 st->mem_preemp_preQ = 0.0f;
     640     2850901 :                 st->last_nq_preQ = 0;
     641     2850901 :                 st->last_code_preq = 0;
     642             :             }
     643     3054850 :             st->use_acelp_preq = 0;
     644             : 
     645             :             /*-----------------------------------------------------------------*
     646             :              * LSF de-quantization and interpolation
     647             :              *-----------------------------------------------------------------*/
     648             : 
     649     3054850 :             if ( !tdm_lp_reuse_flag )
     650             :             {
     651     3051644 :                 lsf_dec( st, tc_subfr, Aq, &LSF_Q_prediction, lsf_new, lsp_new, lsp_mid, tdm_low_rate_mode, tdm_lsfQ_PCh );
     652             :             }
     653             :             else
     654             :             {
     655             :                 const float *pt_interp_2;
     656             :                 int16_t beta_index;
     657             : 
     658        3206 :                 beta_index = get_next_indice( st, TDM_IC_LSF_PRED_BITS );
     659        3206 :                 tdm_SCh_lsf_reuse( DEC, st->element_brate, lsf_new, lsp_new, tdm_lsfQ_PCh, NULL, &beta_index );
     660             : 
     661        3206 :                 if ( st->rate_switching_reset )
     662             :                 {
     663             :                     /* extrapolation in case of unstable LSF convert */
     664           0 :                     mvr2r( lsp_new, st->lsp_old, M );
     665           0 :                     mvr2r( lsf_new, st->lsf_old, M );
     666             :                 }
     667             : 
     668        3206 :                 pt_interp_2 = interpol_frac_12k8;
     669        3206 :                 if ( tdm_low_rate_mode == 1 && st->coder_type > UNVOICED )
     670             :                 {
     671           0 :                     pt_interp_2 = interpol_frac2;
     672             :                 }
     673             : 
     674        3206 :                 if ( st->active_cnt == 1 )
     675             :                 {
     676           5 :                     mvr2r( lsp_new, st->lsp_old, M );
     677           5 :                     lsp2lsf( lsp_new, st->lsf_old, M, st->sr_core );
     678             :                 }
     679             : 
     680             :                 /* LSP interpolation and conversion of LSPs to A(z) */
     681        3206 :                 int_lsp( st->L_frame, st->lsp_old, lsp_new, Aq, M, pt_interp_2, 0 );
     682             :                 /* Check LSF stability (distance between old LSFs and current LSFs) */
     683        3206 :                 st->stab_fac = lsf_stab( lsf_new, st->lsf_old, 0, st->L_frame );
     684             :             }
     685             : 
     686     3054850 :             if ( st->last_core == HQ_CORE && st->element_mode > EVS_MONO )
     687             :             {
     688             :                 /* Prepare ACB memory from last HQ frame */
     689        3686 :                 old_exc_s = st->old_exc + L_EXC_MEM_DEC - st->L_frame;
     690        3686 :                 tmpF = *old_exc_s;
     691        3686 :                 st->mem_deemph = old_exc_s[st->L_frame - 1];
     692        3686 :                 preemph( old_exc_s, st->preemph_fac, st->L_frame, &tmpF );
     693        3686 :                 mvr2r( old_exc_s + st->L_frame - M, st->mem_syn2, M );
     694        3686 :                 residu( Aq, M, old_exc_s, old_exc + L_EXC_MEM_DEC - st->L_frame, st->L_frame );
     695             :             }
     696             : 
     697     3054850 :             if ( st->last_core != ACELP_CORE && st->element_mode > EVS_MONO )
     698             :             {
     699             :                 /* Prepare ACB memory of old_bwe_exc */
     700      167749 :                 if ( st->L_frame == L_FRAME )
     701             :                 {
     702       70611 :                     lerp( old_exc, old_bwe_exc, L_EXC_MEM_DEC * HIBND_ACB_L_FAC, L_EXC_MEM_DEC );
     703             :                 }
     704             :                 else
     705             :                 {
     706       97138 :                     lerp( old_exc, old_bwe_exc, L_EXC_MEM_DEC * 2, L_EXC_MEM_DEC );
     707             :                 }
     708             :             }
     709             : 
     710             :             /*-----------------------------------------------------------------*
     711             :              * FEC - first good frame after lost frame(s) (possibility to correct the ACB)
     712             :              *-----------------------------------------------------------------*/
     713             : 
     714     3054850 :             if ( st->acelp_cfg.FEC_mode > 0 )
     715             :             {
     716     2245393 :                 last_pulse_pos = 0;
     717             : 
     718             :                 /* decode the last glottal pulse position */
     719     2245393 :                 T0_tmp = FEC_pos_dec( st, &last_pulse_pos, &enr_q, nb_bits );
     720             : 
     721     2245393 :                 if ( st->last_core != HQ_CORE || ( st->last_core == HQ_CORE && st->last_con_tcx ) )
     722             :                 {
     723     2241206 :                     if ( st->clas_dec == SIN_ONSET && last_pulse_pos != 0 && st->prev_bfi == 1 )
     724             :                     {
     725          23 :                         FEC_SinOnset( old_exc + L_EXC_MEM_DEC - L_EXC_MEM, last_pulse_pos, T0_tmp, enr_q, Aq, st->L_frame );
     726             :                     }
     727     2241183 :                     else if ( ( st->coder_type == GENERIC || st->coder_type == VOICED ) && last_pulse_pos != 0 && st->old_bfi_cnt == 1 && st->hWIDec != NULL )
     728             :                     {
     729          37 :                         do_WI = FEC_enhACB( st->L_frame, st->last_L_frame, old_exc + L_EXC_MEM_DEC - L_EXC_MEM, T0_tmp, last_pulse_pos, st->bfi_pitch );
     730             :                     }
     731             :                 }
     732             :             }
     733             : 
     734             :             /*------------------------------------------------------------*
     735             :              * In case of first frame after an erasure and transition from voiced to unvoiced or inactive
     736             :              * redo the LPC interpolation
     737             :              *------------------------------------------------------------*/
     738             : 
     739     3054850 :             if ( st->stab_fac == 0 && st->old_bfi_cnt > 0 && st->clas_dec != VOICED_CLAS && st->clas_dec != ONSET && st->relax_prev_lsf_interp == 0 && !( st->element_mode == IVAS_CPE_TD && st->idchan == 1 ) )
     740             :             {
     741        6648 :                 int_lsp4( st->L_frame, st->lsp_old, lsp_mid, lsp_new, Aq, M, 2 );
     742             :             }
     743             : 
     744             :             /*---------------------------------------------------------------*
     745             :              * Decoding of the scaled predicted innovation energy
     746             :              *---------------------------------------------------------------*/
     747             : 
     748     3054850 :             if ( nb_bits > 0 )
     749             :             {
     750     2511826 :                 indice = get_next_indice( st, nb_bits );
     751     2511826 :                 Es_pred_dec( &Es_pred, indice, nb_bits, uc_two_stage_flag );
     752             :             }
     753             : 
     754             :             /*------------------------------------------------------------*
     755             :              * Decode excitation according to coding type
     756             :              *------------------------------------------------------------*/
     757             : 
     758     3054850 :             if ( tdm_low_rate_mode ) /* tdm stereo low rate mode */
     759             :             {
     760      286514 :                 if ( st->coder_type <= UNVOICED )
     761             :                 {
     762      286006 :                     tdm_low_rate_dec( st, dct_exc_tmp, &tmp_noise, pitch_buf, voice_factors, exc, exc2, bwe_exc, lsf_new );
     763             :                 }
     764             :                 else /* GENERIC */
     765             :                 {
     766         508 :                     decod_gen_2sbfr( st, sharpFlag, Aq, pitch_buf, voice_factors, exc, exc2, bwe_exc, gain_buf, tdm_Pitch_reuse_flag, p_tdm_Pri_pitch_buf );
     767             : 
     768         508 :                     if ( st->element_mode == IVAS_CPE_TD )
     769             :                     {
     770         508 :                         tmp_noise = st->lp_gainc;
     771             :                     }
     772             :                 }
     773             :             }
     774     2768336 :             else if ( st->nelp_mode_dec )
     775             :             {
     776             :                 /* SC-VBR - NELP frames */
     777         308 :                 decod_nelp( st, &tmp_noise, pitch_buf, exc, exc2, voice_factors, bwe_exc, st->bfi, gain_buf );
     778             :             }
     779     2768028 :             else if ( st->coder_type == UNVOICED )
     780             :             {
     781             :                 /* UNVOICED frames */
     782       33660 :                 decod_unvoiced( st, Aq, Es_pred, uc_two_stage_flag, &tmp_noise, pitch_buf, voice_factors, exc, exc2, bwe_exc, gain_buf );
     783             :             }
     784     2734368 :             else if ( st->ppp_mode_dec )
     785             :             {
     786             :                 /* SC-VBR - PPP frames */
     787         125 :                 if ( ( error = decod_ppp( st, Aq, pitch_buf, exc, exc2, voice_factors, bwe_exc, gain_buf, st->bfi ) ) != IVAS_ERR_OK )
     788             :                 {
     789           0 :                     return error;
     790             :                 }
     791             :             }
     792     2734243 :             else if ( st->coder_type == TRANSITION )
     793             :             {
     794      289393 :                 decod_tran( st, st->L_frame, tc_subfr, Aq, Es_pred, pitch_buf, voice_factors, exc, exc2, bwe_exc, unbits, sharpFlag, gain_buf );
     795             :             }
     796     2444850 :             else if ( st->coder_type == AUDIO || ( st->coder_type == INACTIVE && st->inactive_coder_type_flag ) )
     797             :             {
     798             :                 /* AUDIO and INACTIVE frames (coded by GSC technology) */
     799      211433 :                 decod_audio( st, dct_exc_tmp, Aq, &tmp_noise, pitch_buf, voice_factors, exc, exc2, bwe_exc, lsf_new, gain_buf, tdm_lp_reuse_flag, tdm_low_rate_mode, tdm_Pitch_reuse_flag, p_tdm_Pri_pitch_buf );
     800             :             }
     801             :             else
     802             :             {
     803             :                 /* GENERIC, VOICED and INACTIVE frames (coded by AVQ technology) */
     804     2233417 :                 if ( ( error = decod_gen_voic( st, st->L_frame, sharpFlag, Aq, Es_pred, do_WI, pitch_buf, voice_factors, exc, exc2, bwe_exc, unbits, gain_buf, tdm_Pitch_reuse_flag, p_tdm_Pri_pitch_buf ) ) != IVAS_ERR_OK )
     805             :                 {
     806           0 :                     return error;
     807             :                 }
     808             : 
     809     2233417 :                 if ( st->element_mode == IVAS_CPE_TD )
     810             :                 {
     811       61051 :                     tmp_noise = st->lp_gainc;
     812             :                 }
     813             :             }
     814             : 
     815             : #ifdef DEBUG_MODE_ACELP
     816             :             dbgwrite( exc, sizeof( float ), st->L_frame, 1, fname( debug_dir, "exc", st->idchan, st->id_element, DEC ) );
     817             : #endif
     818             : 
     819             :             /* synthesis for ACELP core switching and SWB BWE */
     820     3054850 :             syn_12k8( st->L_frame, Aq, exc, temp_buf, st->mem_syn1, 1 );
     821             : 
     822             :             /* save and delay synthesis to be used by SWB BWE */
     823     3054850 :             if ( st->hBWE_FD != NULL )
     824             :             {
     825     3051918 :                 save_old_syn( st->L_frame, temp_buf, old_syn_12k8_16k, st->hBWE_FD->old_syn_12k8_16k, st->preemph_fac, &st->hBWE_FD->mem_deemph_old_syn );
     826             :             }
     827             : 
     828             :             /*-----------------------------------------------------------------*
     829             :              * Apply energy matching when switching to inactive frames
     830             :              *-----------------------------------------------------------------*/
     831             : 
     832     3054850 :             inact_switch_ematch( exc2, dct_exc_tmp, st->hGSCDec->lt_ener_per_band, st->coder_type, st->inactive_coder_type_flag, st->L_frame, st->bfi, st->last_core, st->last_codec_mode, tdm_low_rate_mode, st->element_mode );
     833             : 
     834             :             /*------------------------------------------------------------*
     835             :              * Decode information and modify the excitation signal of stationary unvoiced frames
     836             :              *------------------------------------------------------------*/
     837             : 
     838     3054850 :             if ( !( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) && st->nelp_mode_dec != 1 && !( st->element_mode == IVAS_SCE && tdm_low_rate_mode ) )
     839             :             {
     840     2736841 :                 stat_noise_uv_dec( st, lsp_new, lsp_mid, Aq, exc2, uc_two_stage_flag );
     841             :             }
     842             : 
     843             :             /*------------------------------------------------------------*
     844             :              * Save filter memory in case the synthesis is redone after scaling
     845             :              * Synthesis at 12k8 Hz sampling rate
     846             :              *------------------------------------------------------------*/
     847             : 
     848             :             /* update past excitation signals for LD music post-filter */
     849     3054850 :             if ( st->hMusicPF != NULL )
     850             :             {
     851     3054850 :                 mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
     852     3054850 :                 mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
     853     3054850 :                 mvr2r( st->hMusicPF->dct_post_old_exc, exc_buffer, DCT_L_POST - OFFSET2 );
     854             :             }
     855             : 
     856     3054850 :             if ( ( st->coder_type == AUDIO && !st->GSC_noisy_speech ) || ( st->GSC_IVAS_mode >= 1 && st->L_frame == L_FRAME ) )
     857      159840 :             {
     858      159840 :                 int16_t last_coder_type = st->last_coder_type;
     859             : 
     860      159840 :                 if ( ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) || ( st->GSC_IVAS_mode >= 1 && st->GSC_noisy_speech == 0 ) )
     861             :                 {
     862      105533 :                     last_coder_type = AUDIO;
     863             :                 }
     864             : 
     865             :                 /* Extrapolation of the last future part, windowing and high resolution DCT transform */
     866      159840 :                 Prep_music_postP( exc_buffer, dct_buffer, st->hMusicPF->filt_lfE, st->last_core, pitch_buf, st->hMusicPF->LDm_enh_lp_gbin );
     867             : 
     868             :                 /* LD music post-filter */
     869      159840 :                 LD_music_post_filter( st->hMusicPF, dct_buffer, dct_buffer, st->core_brate, AUDIO, last_coder_type );
     870             : 
     871             :                 /* Inverse DCT transform, retrieval of the aligned excitation, re-synthesis */
     872      159840 :                 mvr2r( st->mem_syn2, mem_tmp, M );
     873      159840 :                 Post_music_postP( dct_buffer, exc_buffer, exc2, st->mem_syn2, st->mem_syn2, Aq, syn );
     874             :             }
     875             :             else
     876             :             {
     877             :                 /* Core synthesis at 12.8kHz or 16kHz */
     878     2895010 :                 mvr2r( st->mem_syn2, mem_tmp, M );
     879     2895010 :                 syn_12k8( st->L_frame, Aq, exc2, syn, st->mem_syn2, 1 );
     880             : 
     881     2895010 :                 if ( st->hMusicPF != NULL )
     882             :                 {
     883  1855701410 :                     for ( i = 0; i < DCT_L_POST; i++ )
     884             :                     {
     885  1852806400 :                         st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
     886             :                     }
     887             :                 }
     888             :             }
     889             : 
     890             :             /*------------------------------------------------------------*
     891             :              * FEC - Estimate the classification information
     892             :              *------------------------------------------------------------*/
     893             : 
     894     3054850 :             FEC_clas_estim( syn, pitch_buf, st->L_frame, st->coder_type, st->codec_mode, st->mem_syn_clas_estim, &st->clas_dec, &st->lp_ener_bfi, st->Opt_AMR_WB, &st->decision_hyst, NULL, NULL, NULL, NULL, NULL, NULL, temp_buf, 0, 0, 0, 0, 0, 0, st->last_core_brate, st->acelp_cfg.FEC_mode );
     895             : 
     896             :             /*------------------------------------------------------------*
     897             :              * FEC - Estimate pitch
     898             :              *------------------------------------------------------------*/
     899             : 
     900     3054850 :             FEC_pitch_estim( st->Opt_AMR_WB, st->last_core, st->L_frame, st->clas_dec, st->last_good, pitch_buf, st->old_pitch_buf, &st->bfi_pitch, &st->bfi_pitch_frame, &st->upd_cnt, st->coder_type );
     901             : 
     902             :             /*------------------------------------------------------------*
     903             :              * FEC - Smooth the speech energy evolution when recovering after a BAD frame
     904             :              * (smoothing is performed in the excitation domain and signal is resynthesized after)
     905             :              *------------------------------------------------------------*/
     906             : 
     907     3054850 :             FEC_scale_syn( st->L_frame, st->clas_dec, st->last_good, syn, pitch_buf, st->enr_old, enr_q, st->coder_type, LSF_Q_prediction, &st->scaling_flag, &st->lp_ener_FEC_av, &st->lp_ener_FEC_max, st->bfi, st->total_brate, st->prev_bfi, st->last_core_brate, exc, exc2, Aq, &st->old_enr_LP, mem_tmp, st->mem_syn2, st->last_con_tcx && ( st->L_frameTCX_past != st->L_frame ) && ( st->last_core != ACELP_CORE ), 0 );
     908             : 
     909             :             /* estimate the pitch-synchronous speech energy per sample to be used when normal operation recovers */
     910     3054850 :             if ( ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD && st->total_brate <= ACELP_7k20 ) || ( st->total_brate == ACELP_7k20 ) || ( st->total_brate == ACELP_8k00 ) )
     911             :             {
     912       15163 :                 fer_energy( st->L_frame, st->clas_dec, syn, pitch_buf[( ( st->L_frame ) >> 6 ) - 1], &st->enr_old, st->L_frame );
     913             :             }
     914             :         }
     915             : 
     916             :     } /* End of GOOD FRAME */
     917             : 
     918             :     /*----------------------------------------------------------------*
     919             :      * BAD frame
     920             :      *----------------------------------------------------------------*/
     921             : 
     922             :     else
     923             :     {
     924             :         /* SC-VBR */
     925      232694 :         if ( st->last_nelp_mode_dec == 1 )
     926             :         {
     927           0 :             st->nelp_mode_dec = 1;
     928             :         }
     929             : 
     930             :         /* long burst frame erasures */
     931      232694 :         if ( st->nbLostCmpt > 5 && st->clas_dec >= VOICED_CLAS && st->clas_dec < INACTIVE_CLAS )
     932             :         {
     933        5869 :             st->last_good = VOICED_TRANSITION;
     934             :         }
     935             : 
     936             :         /* LSF estimation and A(z) calculation */
     937      232694 :         lsf_dec_bfi( MODE1, lsf_new, st->lsf_old, st->lsf_adaptive_mean, NULL, st->mem_MA, st->mem_AR, st->stab_fac, st->last_coder_type, st->L_frame, st->last_good, st->nbLostCmpt, 0, NULL, NULL, NULL, st->hGSCDec->Last_GSC_pit_band_idx, st->Opt_AMR_WB, st->bwidth );
     938             : 
     939      232694 :         FEC_lsf2lsp_interp( st, st->L_frame, Aq, lsf_new, lsp_new );
     940             : 
     941      232694 :         if ( st->nelp_mode_dec == 1 )
     942             :         {
     943             :             /* SC-VBR */
     944           0 :             decod_nelp( st, &tmp_noise, pitch_buf, exc, exc2, voice_factors, bwe_exc, st->bfi, gain_buf );
     945           0 :             FEC_pitch = pitch_buf[3];
     946             :         }
     947             :         else
     948             :         {
     949             :             /* calculation of excitation signal */
     950      232694 :             FEC_exc_estim( st, st->L_frame, exc, exc2, dct_exc_tmp, pitch_buf, voice_factors, &FEC_pitch, bwe_exc, lsf_new, &tmp_noise );
     951             : 
     952      232694 :             tmp_noise = st->lp_gainc;
     953             : 
     954             :             /* SC-VBR */
     955      232694 :             st->prev_gain_pit_dec = st->lp_gainp;
     956             :         }
     957             : 
     958             :         /* synthesis for ACELP core switching and SWB BWE */
     959      232694 :         syn_12k8( st->L_frame, Aq, exc, temp_buf, st->mem_syn1, 1 );
     960             : 
     961             :         /* save and delay synthesis to be used by SWB BWE */
     962      232694 :         if ( st->hBWE_FD != NULL )
     963             :         {
     964      232673 :             save_old_syn( st->L_frame, temp_buf, old_syn_12k8_16k, st->hBWE_FD->old_syn_12k8_16k, st->preemph_fac, &st->hBWE_FD->mem_deemph_old_syn );
     965             :         }
     966             : 
     967             :         /* Apply energy matching when switching to inactive frames */
     968      232694 :         inact_switch_ematch( exc2, dct_exc_tmp, st->hGSCDec->lt_ener_per_band, st->coder_type, st->inactive_coder_type_flag, st->L_frame, st->bfi, st->last_core, st->last_codec_mode, tdm_low_rate_mode, st->element_mode );
     969             :         /* update past excitation signals for LD music post-filter */
     970      232694 :         if ( st->hMusicPF != NULL )
     971             :         {
     972      232694 :             mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
     973      232694 :             mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
     974             : 
     975             :             /* Update music post processing values */
     976             :             /* Filter energies update */
     977   149156854 :             for ( i = 0; i < DCT_L_POST; i++ )
     978             :             {
     979   148924160 :                 st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
     980             :             }
     981             :             /* Update circular buffer, keep last energy difference unchanged */
     982     9307760 :             for ( i = 1; i < MAX_LT; i++ )
     983             :             {
     984     9075066 :                 st->hMusicPF->LDm_lt_diff_etot[i - 1] = st->hMusicPF->LDm_lt_diff_etot[i];
     985             :             }
     986             :         }
     987             : 
     988             :         /* synthesis at 12k8 Hz sampling rate */
     989      232694 :         if ( ( st->total_brate == ACELP_7k20 ) || ( st->total_brate == ACELP_8k00 ) )
     990             :         {
     991         204 :             mvr2r( st->mem_syn2, mem_tmp, M );
     992             :         }
     993      232694 :         syn_12k8( st->L_frame, Aq, exc2, syn, st->mem_syn2, 1 );
     994             : 
     995             :         /* update buffer for classifier */
     996      232694 :         if ( st->hWIDec != NULL )
     997             :         {
     998         484 :             mvr2r( exc2 + st->L_frame - L_EXC_MEM, st->hWIDec->old_exc2, L_EXC_MEM );
     999         484 :             mvr2r( syn + st->L_frame - L_EXC_MEM, st->hWIDec->old_syn2, L_EXC_MEM );
    1000             :         }
    1001      232694 :         mvr2r( syn + st->L_frame - L_SYN_MEM_CLAS_ESTIM, st->mem_syn_clas_estim, L_SYN_MEM_CLAS_ESTIM );
    1002             : 
    1003             :         /*------------------------------------------------------------*
    1004             :          * FEC - Smooth the speech energy evolution when recovering after a BAD frame
    1005             :          * (smoothing is performed in the excitation domain and signal is resynthesized after)
    1006             :          *------------------------------------------------------------*/
    1007             : 
    1008      232694 :         if ( ( st->total_brate == ACELP_7k20 ) || ( st->total_brate == ACELP_8k00 ) )
    1009             :         {
    1010         204 :             FEC_scale_syn( st->L_frame, st->clas_dec, st->last_good, syn, pitch_buf, st->enr_old, enr_q, st->coder_type, LSF_Q_prediction, &st->scaling_flag, &st->lp_ener_FEC_av, &st->lp_ener_FEC_max, st->bfi, st->total_brate, st->prev_bfi, st->last_core_brate, exc, exc2, Aq, &st->old_enr_LP, mem_tmp, st->mem_syn2, st->last_con_tcx && ( st->L_frameTCX_past != st->L_frame ) && ( st->last_core != ACELP_CORE ), 0 );
    1011             :         }
    1012             : 
    1013             :         /* estimate the pitch-synchronous speech energy per sample to be used when normal operation recovers */
    1014      232694 :         fer_energy( st->L_frame, st->last_good, syn, FEC_pitch, &st->enr_old, st->L_frame );
    1015             : 
    1016      232694 :         if ( st->nelp_mode_dec != 1 )
    1017             :         {
    1018             :             /* modify the excitation signal of stationary unvoiced frames */
    1019      232694 :             stat_noise_uv_mod( st->coder_type, 0, st->lsp_old, lsp_new, lsp_new, Aq, exc2, 1, &st->ge_sm, &st->uv_count, &st->act_count, st->lspold_s, &st->noimix_seed, &st->min_alpha, &st->exc_pe, st->core_brate, st->bwidth );
    1020             :         }
    1021             :     }
    1022             : 
    1023     3412045 :     if ( st->hBWE_TD != NULL )
    1024             :     {
    1025     3368061 :         if ( st->L_frame == L_FRAME )
    1026             :         {
    1027     1786282 :             mvr2r( Aq + 2 * ( M + 1 ), st->hBWE_TD->cur_sub_Aq, ( M + 1 ) );
    1028             :         }
    1029             :         else
    1030             :         {
    1031     1581779 :             mvr2r( Aq + 3 * ( M + 1 ), st->hBWE_TD->cur_sub_Aq, ( M + 1 ) );
    1032             :         }
    1033             :     }
    1034             : 
    1035             :     /*--------------------------------------------------------*
    1036             :      * Apply NB postfilter in case of 8kHz output
    1037             :      *--------------------------------------------------------*/
    1038             : 
    1039     3412045 :     if ( st->last_bwidth == NB && st->hPFstat != NULL )
    1040             :     {
    1041       98030 :         if ( st->bwidth == NB )
    1042             :         {
    1043        5401 :             st->hPFstat->on = 1;
    1044        5401 :             nb_post_filt( st->L_frame, L_SUBFR, st->hPFstat, &st->psf_lp_noise, tmp_noise, syn, Aq, pitch_buf, st->coder_type, st->BER_detect, 0 );
    1045             :         }
    1046             :         else
    1047             :         {
    1048       92629 :             st->hPFstat->on = 0;
    1049       92629 :             nb_post_filt( st->L_frame, L_SUBFR, st->hPFstat, &st->psf_lp_noise, tmp_noise, syn, Aq, pitch_buf, AUDIO, st->BER_detect, 0 );
    1050             :         }
    1051             :     }
    1052             :     else
    1053             :     {
    1054     3314015 :         st->psf_lp_noise = st->lp_noise;
    1055             :     }
    1056             : 
    1057             :     /*------------------------------------------------------------------*
    1058             :      * Perform fixed deemphasis through 1/(1 - g*z^-1)
    1059             :      *-----------------------------------------------------------------*/
    1060             : 
    1061             :     /* update old synthesis buffer - needed for ACELP internal sampling rate switching */
    1062     3412045 :     mvr2r( syn + st->L_frame - L_SYN_MEM, st->mem_syn_r, L_SYN_MEM );
    1063     3412045 :     deemph( syn, st->preemph_fac, st->L_frame, &( st->mem_deemph ) );
    1064             : 
    1065     3412045 :     AGC_dec( syn, st->agc_mem2, st->L_frame );
    1066             : 
    1067     3412045 :     if ( st->hTcxDec != NULL )
    1068             :     {
    1069     3372911 :         mvr2r( syn + st->L_frame / 2, st->hTcxDec->old_syn_Overl, st->L_frame / 2 );
    1070             :     }
    1071     3412045 :     mvr2r( syn + st->L_frame - M - 1, st->syn, M + 1 );
    1072             : 
    1073             :     /*------------------------------------------------------------------*
    1074             :      * Formant post-filter
    1075             :      *-----------------------------------------------------------------*/
    1076             : 
    1077     3412045 :     if ( st->hPFstat != NULL && st->last_bwidth >= WB && ( st->core_brate > ACELP_24k40 || st->element_mode > EVS_MONO ) && st->core_brate <= ACELP_32k )
    1078             :     {
    1079     3079243 :         mvr2r( syn, temp_buf + L_SYN_MEM, L_FRAME16k );
    1080             : 
    1081     3079243 :         st->hPFstat->on = 1;
    1082     3079243 :         formant_post_filt( st->hPFstat, temp_buf + L_SYN_MEM, Aq, syn, st->L_frame, L_SUBFR, st->lp_noise, st->total_brate, 0 );
    1083             :     }
    1084      332802 :     else if ( st->hPFstat != NULL && st->last_bwidth >= WB )
    1085             :     {
    1086      193281 :         if ( st->hPFstat->on )
    1087             :         {
    1088       10724 :             mvr2r( st->hPFstat->mem_pf_in + L_SYN_MEM - M, temp_buf, M );
    1089       10724 :             mvr2r( syn, temp_buf + M, L_SUBFR );
    1090       10724 :             residu( Aq, M, temp_buf + M, temp_buf + M + L_SUBFR, L_SUBFR );
    1091       10724 :             syn_filt( Aq, M, temp_buf + M + L_SUBFR, temp_buf, L_SUBFR, st->hPFstat->mem_stp + L_SYN_MEM - M, 0 );
    1092       10724 :             scale_st( syn, temp_buf, &st->hPFstat->gain_prec, L_SUBFR, -1 );
    1093       10724 :             mvr2r( temp_buf, syn, L_SUBFR / 2 );
    1094       10724 :             blend_subfr2( temp_buf + L_SUBFR / 2, syn + L_SUBFR / 2, syn + L_SUBFR / 2 );
    1095             :         }
    1096      193281 :         st->hPFstat->on = 0;
    1097             :     }
    1098             : 
    1099             :     /*----------------------------------------------------------------*
    1100             :      * Comfort noise addition
    1101             :      *----------------------------------------------------------------*/
    1102             : 
    1103     3412045 :     if ( ( st->hFdCngDec != NULL || st->idchan == 1 ) && st->element_mode != IVAS_CPE_MDCT )
    1104             :     {
    1105     3370554 :         if ( st->element_mode == IVAS_CPE_TD || st->flag_cna || ( st->cng_type == FD_CNG && st->total_brate <= ACELP_32k ) || ( st->cng_type == LP_CNG && st->core_brate <= SID_2k40 ) )
    1106             :         {
    1107             :             /*VAD only for non inactive frame*/
    1108     2920254 :             st->VAD = st->VAD && ( st->coder_type != INACTIVE );
    1109             : 
    1110     2920254 :             if ( st->idchan == 0 && ( st->flag_cna || ( st->cng_type == FD_CNG && st->total_brate <= ACELP_32k ) || ( st->cng_type == LP_CNG && st->core_brate <= SID_2k40 ) ) )
    1111             :             {
    1112             :                 /*Noisy speech detector*/
    1113     2851775 :                 noisy_speech_detection( st->hFdCngDec, st->VAD, syn );
    1114             : 
    1115     2851775 :                 st->hFdCngDec->hFdCngCom->likelihood_noisy_speech = 0.99f * st->hFdCngDec->hFdCngCom->likelihood_noisy_speech + 0.01f * (float) st->hFdCngDec->hFdCngCom->flag_noisy_speech;
    1116             :             }
    1117             : 
    1118     2920254 :             if ( st->idchan == 0 )
    1119             :             {
    1120     2881120 :                 st->lp_noise = st->hFdCngDec->lp_noise;
    1121             :             }
    1122             : 
    1123     2920254 :             if ( st->element_mode != IVAS_CPE_TD && !st->cng_ism_flag )
    1124             :             {
    1125             :                 /*Noise estimate*/
    1126     2805424 :                 ApplyFdCng( syn, NULL, realBuffer, imagBuffer, st, 0, ( st->coder_type == AUDIO && !st->GSC_noisy_speech ) );
    1127             :             }
    1128             : 
    1129     2920254 :             if ( !st->cna_dirac_flag )
    1130             :             {
    1131             :                 /* CNA: Generate additional comfort noise to mask potential coding artefacts */
    1132     2786169 :                 if ( st->flag_cna && !( st->coder_type == AUDIO && !( st->element_mode > EVS_MONO && st->GSC_noisy_speech ) ) )
    1133             :                 {
    1134     1903240 :                     if ( st->element_mode == IVAS_CPE_TD && nchan_out == 2 )
    1135             :                     {
    1136        3852 :                         if ( hStereoCng->flag_cna_fade )
    1137             :                         {
    1138          36 :                             generate_stereo_masking_noise( syn, st, hStereoTD, flag_sec_CNA, 1, hStereoCng, nchan_out );
    1139          36 :                             hStereoCng->flag_cna_fade = 0;
    1140             :                         }
    1141             :                         else
    1142             :                         {
    1143        3816 :                             if ( st->element_mode != last_element_mode && st->idchan == 0 )
    1144             :                             {
    1145             :                                 /* Clear memory for secondary channel CNA */
    1146          17 :                                 set_f( hStereoCng->olapBufferSynth22, 0.0f, st->hFdCngDec->hFdCngCom->frameSize / 2 );
    1147             :                             }
    1148             : 
    1149        3816 :                             generate_stereo_masking_noise( syn, st, hStereoTD, flag_sec_CNA, 0, hStereoCng, nchan_out );
    1150             :                         }
    1151             :                     }
    1152     1899388 :                     else if ( st->element_mode != IVAS_CPE_DFT )
    1153             :                     {
    1154     1299884 :                         if ( st->idchan == 0 )
    1155             :                         {
    1156     1299383 :                             if ( st->element_mode != last_element_mode )
    1157             :                             {
    1158           3 :                                 set_f( st->hFdCngDec->hFdCngCom->olapBufferSynth2, 0.0f, st->hFdCngDec->hFdCngCom->fftlen );
    1159             :                             }
    1160     1299383 :                             generate_masking_noise( syn, st->hFdCngDec->hFdCngCom, st->hFdCngDec->hFdCngCom->frameSize, 0, 0, 0, st->element_mode, hStereoCng, nchan_out );
    1161             :                         }
    1162             :                     }
    1163             :                 }
    1164      882929 :                 else if ( st->flag_cna && st->coder_type == AUDIO && ( ( st->last_core == ACELP_CORE && !( st->last_coder_type == AUDIO && !( st->element_mode > EVS_MONO && st->Last_GSC_noisy_speech_flag ) ) ) || st->last_core == TCX_20_CORE ) )
    1165             :                 {
    1166       29276 :                     if ( st->element_mode == IVAS_CPE_TD && nchan_out == 2 )
    1167             :                     {
    1168           0 :                         generate_stereo_masking_noise( syn, st, hStereoTD, flag_sec_CNA, 1, hStereoCng, nchan_out );
    1169           0 :                         hStereoCng->flag_cna_fade = 1;
    1170             :                     }
    1171             :                     else
    1172             :                     {
    1173       29276 :                         v_multc( st->hFdCngDec->hFdCngCom->olapBufferSynth2 + 5 * st->hFdCngDec->hFdCngCom->frameSize / 4, (float) ( st->hFdCngDec->hFdCngCom->fftlen / 2 ), temp_buf, st->hFdCngDec->hFdCngCom->frameSize / 2 );
    1174       29276 :                         v_add( temp_buf, syn, syn, st->hFdCngDec->hFdCngCom->frameSize / 2 );
    1175             :                     }
    1176             :                 }
    1177             :                 else
    1178             :                 {
    1179      853653 :                     if ( hStereoCng != NULL )
    1180             :                     {
    1181      129160 :                         hStereoCng->flag_cna_fade = 1;
    1182      129160 :                         hStereoCng->enableSecCNA = 0;
    1183             :                     }
    1184             :                 }
    1185             : 
    1186     2786169 :                 if ( st->element_mode == IVAS_CPE_TD )
    1187             :                 {
    1188             :                     /*Noise estimate*/
    1189       76691 :                     if ( st->idchan == 0 && ( nchan_out == 2 || ( st->core_brate != FRAME_NO_DATA && st->core_brate != SID_2k40 ) ) )
    1190             :                     {
    1191       37557 :                         ApplyFdCng( syn, NULL, realBuffer, imagBuffer, st, 0, ( st->coder_type == AUDIO && !st->GSC_noisy_speech ) );
    1192             :                     }
    1193             :                 }
    1194             :             }
    1195             :         }
    1196             : 
    1197     3370554 :         if ( !st->cna_dirac_flag )
    1198             :         {
    1199     3187644 :             if ( st->flag_cna == 0 && st->L_frame == L_FRAME16k && st->last_flag_cna == 1 && ( ( st->last_core == ACELP_CORE && !( st->last_coder_type == AUDIO && !( st->element_mode > EVS_MONO && st->Last_GSC_noisy_speech_flag ) ) ) || st->last_core == AMR_WB_CORE ) )
    1200             :             {
    1201        1885 :                 v_multc( st->hFdCngDec->hFdCngCom->olapBufferSynth2 + 5 * st->L_frame / 4, 256.f, temp_buf, st->L_frame / 2 );
    1202        1885 :                 v_add( temp_buf, syn, syn, st->L_frame / 2 );
    1203             :             }
    1204             : 
    1205     3187644 :             if ( st->flag_cna == 0 || ( st->coder_type == AUDIO && !( st->element_mode > EVS_MONO && st->GSC_noisy_speech ) ) )
    1206             :             {
    1207     1284404 :                 if ( st->idchan == 0 )
    1208             :                 {
    1209     1247715 :                     set_f( st->hFdCngDec->hFdCngCom->olapBufferSynth2, 0.f, st->hFdCngDec->hFdCngCom->fftlen );
    1210             :                 }
    1211     1284404 :                 if ( hStereoCng != NULL && st->idchan == 0 )
    1212             :                 {
    1213      110615 :                     set_f( hStereoCng->olapBufferSynth22, 0.f, st->hFdCngDec->hFdCngCom->fftlen );
    1214             :                 }
    1215             :             }
    1216             :         }
    1217             :     }
    1218             : 
    1219             : #ifdef DEBUG_MODE_ACELP
    1220             :     dbgwrite( syn, sizeof( float ), st->L_frame, 1, fname( debug_dir, "syn.intFs", st->idchan, st->id_element, DEC ) );
    1221             : #endif
    1222             : 
    1223             :     /*----------------------------------------------------------------*
    1224             :      * Resample to the output sampling rate (8/16/32/48 kHz)
    1225             :      * Bass post-filter
    1226             :      *----------------------------------------------------------------*/
    1227             : 
    1228             :     /* check if the CLDFB works on the right sample rate */
    1229     3412045 :     if ( ( st->cldfbAna->no_channels * st->cldfbAna->no_col ) != st->L_frame )
    1230             :     {
    1231       86479 :         resampleCldfb( st->cldfbAna, st->L_frame * FRAMES_PER_SEC );
    1232       86479 :         resampleCldfb( st->cldfbBPF, st->L_frame * FRAMES_PER_SEC );
    1233             : 
    1234       86479 :         if ( st->ini_frame > 0 )
    1235             :         {
    1236       38504 :             st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
    1237             :         }
    1238             :     }
    1239             : 
    1240             :     /* analyze pitch coherence for bass post-filter */
    1241     3412045 :     bpf_pitch_coherence( st, pitch_buf );
    1242             : 
    1243     3412045 :     if ( !( st->element_mode == IVAS_CPE_MDCT && st->bpf_off ) )
    1244             :     {
    1245     3370554 :         if ( st->L_frame != st->last_L_frame && st->last_codec_mode != MODE2 )
    1246             :         {
    1247       35834 :             if ( st->L_frame == L_FRAME )
    1248             :             {
    1249       24446 :                 retro_interp5_4( st->hBPF->pst_old_syn );
    1250             :             }
    1251       11388 :             else if ( st->L_frame == L_FRAME16k )
    1252             :             {
    1253       11388 :                 retro_interp4_5( syn, st->hBPF->pst_old_syn );
    1254             :             }
    1255             :         }
    1256             : 
    1257             :         /* bass post-filter */
    1258     3370554 :         bass_psfilter( st->hBPF, st->Opt_AMR_WB, syn, st->L_frame, pitch_buf, st->bpf_off, st->stab_fac, &st->stab_fac_smooth, st->coder_type, bpf_error_signal );
    1259             :     }
    1260             : 
    1261     3412045 :     if ( st->element_mode != IVAS_CPE_DFT || use_cldfb_for_dft )
    1262     3056401 :     {
    1263             :         float realBufferSave[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    1264             :         float imagBufferSave[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    1265             :         float *pRealSave[CLDFB_NO_COL_MAX], *pImagSave[CLDFB_NO_COL_MAX];
    1266    51958817 :         for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
    1267             :         {
    1268    48902416 :             pRealSave[i] = realBufferSave[i];
    1269    48902416 :             pImagSave[i] = imagBufferSave[i];
    1270             :         }
    1271     3056401 :         if ( st->p_bpf_noise_buf )
    1272             :         {
    1273     2922073 :             mvr2r( bpf_error_signal, st->p_bpf_noise_buf, st->L_frame );
    1274             :         }
    1275             : 
    1276             :         /* analysis of the synthesis at internal sampling rate */
    1277     3056401 :         cldfbAnalysis( syn, realBuffer, imagBuffer, -1, st->cldfbAna );
    1278             : 
    1279             :         /* analysis and add the BPF error signal */
    1280     3056401 :         addBassPostFilter( bpf_error_signal, st->bpf_off ? 0 : -1, realBuffer, imagBuffer, st->cldfbBPF );
    1281             : 
    1282             :         /* set output mask for upsampling */
    1283     3056401 :         if ( st->bwidth == NB )
    1284             :         {
    1285             :             /* set NB mask for upsampling */
    1286        5402 :             st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - 10;
    1287             :         }
    1288     3050999 :         else if ( st->cldfbSyn->bandsToZero != st->cldfbSyn->no_channels - st->cldfbAna->no_channels )
    1289             :         {
    1290             :             /* in case of BW switching, re-init to default */
    1291      165486 :             st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
    1292             :         }
    1293             : 
    1294     3056401 :         if ( !st->cng_sba_flag || st->element_mode == IVAS_CPE_MDCT )
    1295             :         {
    1296             :             /*WB/SWB-FD_CNG*/
    1297     2657155 :             if ( ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 ) && ( st->cng_type == FD_CNG ) && ( st->hFdCngDec->hFdCngCom->numCoreBands < st->cldfbSyn->no_channels ) )
    1298             :             {
    1299       94957 :                 generate_comfort_noise_dec_hf( realBuffer, imagBuffer, st->hFdCngDec->hFdCngCom, st->cng_ism_flag );
    1300             : 
    1301       94957 :                 if ( st->hFdCngDec->hFdCngCom->regularStopBand < st->cldfbSyn->no_channels )
    1302             :                 {
    1303       69429 :                     st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->hFdCngDec->hFdCngCom->regularStopBand;
    1304             :                 }
    1305             :                 else
    1306             :                 {
    1307       25528 :                     st->cldfbSyn->bandsToZero = 0;
    1308             :                 }
    1309             :             }
    1310             :         }
    1311             : 
    1312     3056401 :         if ( save_hb_synth != NULL )
    1313             :         {
    1314             :             /* save and then zero-out lowband */
    1315       31964 :             for ( int16_t j = 0; j < CLDFB_NO_CHANNELS_MAX; j++ )
    1316             :             {
    1317      534480 :                 for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
    1318             :                 {
    1319      503040 :                     realBufferSave[i][j] = realBuffer[i][j];
    1320      503040 :                     imagBufferSave[i][j] = imagBuffer[i][j];
    1321      503040 :                     if ( j < st->hFdCngDec->hFdCngCom->numCoreBands && i < st->hFdCngDec->hFdCngCom->numSlots )
    1322             :                     {
    1323      156160 :                         realBuffer[i][j] = 0.0f;
    1324      156160 :                         imagBuffer[i][j] = 0.0f;
    1325             :                     }
    1326             :                 }
    1327             :             }
    1328             : 
    1329         524 :             cldfbSynthesis( realBuffer, imagBuffer, save_hb_synth, -1, st->cldfbSynHB );
    1330             : 
    1331             :             /* restore lowband */
    1332       31964 :             for ( int16_t j = 0; j < CLDFB_NO_CHANNELS_MAX; j++ )
    1333             :             {
    1334      534480 :                 for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
    1335             :                 {
    1336      503040 :                     realBuffer[i][j] = realBufferSave[i][j];
    1337      503040 :                     imagBuffer[i][j] = imagBufferSave[i][j];
    1338             :                 }
    1339             :             }
    1340             : 
    1341         524 :             cldfbSynthesis( pRealSave, pImagSave, synth, -1, st->cldfbSyn );
    1342             :         }
    1343             :         else
    1344             :         {
    1345             :             /* synthesis of the combined signal */
    1346     3055877 :             cldfbSynthesis( realBuffer, imagBuffer, synth, -1, st->cldfbSyn );
    1347             :         }
    1348             : 
    1349             :         /* save synthesis - needed in case of core switching */
    1350     3056401 :         mvr2r( synth, st->previoussynth, output_frame );
    1351             : 
    1352             : #ifdef DEBUG_MODE_ACELP
    1353             :         dbgwrite( synth, sizeof( float ), output_frame, 1, fname( debug_dir, "output.Fs", st->idchan, st->id_element, DEC ) );
    1354             : #endif
    1355             :     }
    1356             :     else
    1357             :     {
    1358      355644 :         int16_t nSamples = NS2SA( st->L_frame * FRAMES_PER_SEC, FRAME_SIZE_NS /*DELAY_CLDFB_NS*/ ); /* IVAS-64: optimization is likely possible here (don't resample the whole frame) */
    1359             : 
    1360             :         /* analysis of the synthesis at internal sampling rate - needed for DFT stereo -> TD stereo switching */
    1361      355644 :         cldfbAnalysis( syn + st->L_frame - nSamples, realBuffer, imagBuffer, nSamples, st->cldfbAna );
    1362             : 
    1363             :         /* analysis and add the BPF error signal - needed for DFT stereo -> TD stereo switching */
    1364      355644 :         addBassPostFilter( bpf_error_signal + st->L_frame - nSamples, st->bpf_off ? 0 : nSamples, realBuffer, imagBuffer, st->cldfbBPF );
    1365             : 
    1366             :         /* synthesis of the combined signal - needed for DFT stereo -> TD stereo switching */
    1367      355644 :         cldfbSynthesis( realBuffer, imagBuffer, synth /*dummy*/, NS2SA( st->output_Fs, FRAME_SIZE_NS /*DELAY_CLDFB_NS*/ ), st->cldfbSyn );
    1368             : 
    1369      355644 :         if ( st->p_bpf_noise_buf )
    1370             :         {
    1371      343469 :             mvr2r( bpf_error_signal, st->p_bpf_noise_buf, st->L_frame );
    1372             :         }
    1373             : 
    1374             : #ifdef DEBUG_MODE_ACELP
    1375             :         dbgwrite( synth, sizeof( float ), output_frame, 1, fname( debug_dir, "output.Fs", st->idchan, st->id_element, DEC ) );
    1376             : #endif
    1377      355644 :         set_f( synth, 0.0f, output_frame );
    1378             :     }
    1379             : 
    1380             :     /* Copy output signal */
    1381     3412045 :     if ( st->element_mode > EVS_MONO )
    1382             :     {
    1383     3364442 :         mvr2r( syn, output, st->L_frame );
    1384             :     }
    1385             : 
    1386             :     /*-----------------------------------------------------------------*
    1387             :      * Bandwidth extension 6kHz-7kHz
    1388             :      *-----------------------------------------------------------------*/
    1389             : 
    1390     3412045 :     if ( st->hBWE_zero != NULL )
    1391             :     {
    1392     3370554 :         if ( ( st->L_frame == L_FRAME && st->bwidth != NB && output_frame >= L_FRAME16k &&
    1393     1783783 :                ( st->extl == -1 || st->extl == SWB_CNG || ( st->extl == WB_BWE && st->extl_brate == 0 && st->coder_type != AUDIO ) ) ) )
    1394             :         {
    1395      337280 :             hf_synth( st->hBWE_zero, st->core_brate, output_frame, Aq, exc2, syn, synth );
    1396             :         }
    1397             :         else
    1398             :         {
    1399     3033274 :             hf_synth_reset( st->hBWE_zero );
    1400             :         }
    1401             :     }
    1402             : 
    1403             :     /*-----------------------------------------------------------------*
    1404             :      * Populate parameters for SWB TBE
    1405             :      *-----------------------------------------------------------------*/
    1406             : 
    1407     3412045 :     if ( st->hBWE_TD != NULL )
    1408             :     {
    1409     3368061 :         if ( ( !st->bfi && st->prev_bfi ) || ( st->last_vbr_hw_BWE_disable_dec == 1 && st->vbr_hw_BWE_disable_dec == 0 ) || ( ( st->extl == SWB_TBE || st->extl == WB_TBE || st->extl == FB_TBE ) && st->last_extl != SWB_TBE && st->last_extl != WB_TBE && st->last_extl != FB_TBE ) || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD && !st->tdm_LRTD_flag ) )
    1410             :         {
    1411      235908 :             st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
    1412      235908 :             set_f( st->hBWE_TD->old_bwe_exc_extended, 0.0f, NL_BUFF_OFFSET );
    1413             :         }
    1414             : 
    1415     3368061 :         if ( !st->ppp_mode_dec && ( st->idchan == 0 || st->element_mode != IVAS_CPE_TD || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD && st->tdm_LRTD_flag ) ) )
    1416             :         {
    1417     3367936 :             non_linearity( bwe_exc, bwe_exc_extended, st->hBWE_TD->old_bwe_exc_extended, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale, st->coder_type, voice_factors, st->L_frame
    1418             : #ifdef NONBE_1328_FIX_NON_LINEARITY
    1419             :                            ,
    1420     3367936 :                            st->element_mode
    1421             : #endif
    1422             :             );
    1423             :         }
    1424             : 
    1425     3368061 :         if ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 )
    1426             :         {
    1427       83470 :             st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
    1428             :         }
    1429             :     }
    1430             : 
    1431             :     /*----------------------------------------------------------------------*
    1432             :      * Updates
    1433             :      *----------------------------------------------------------------------*/
    1434             : 
    1435     3412045 :     updt_dec( st, old_exc, pitch_buf, Es_pred, Aq, lsf_new, lsp_new, voice_factors, old_bwe_exc, gain_buf );
    1436             : 
    1437     3412045 :     if ( st->core_brate > SID_2k40 && st->hTdCngDec != NULL && st->hFdCngDec != NULL )
    1438             :     {
    1439             :         /* update CNG parameters in active frames */
    1440     2593938 :         cng_params_upd( lsp_new, exc, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ, DEC, st->hTdCngDec->ho_env_circ, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth );
    1441             : 
    1442             :         /* Set 16k LSP flag for CNG buffer */
    1443     2593938 :         st->hTdCngDec->ho_16k_lsp[st->hTdCngDec->ho_circ_ptr] = ( st->L_frame == L_FRAME ? 0 : 1 );
    1444             :     }
    1445             : 
    1446     3412045 :     pop_wmops();
    1447     3412045 :     return error;
    1448             : }

Generated by: LCOV version 1.14