LCOV - code coverage report
Current view: top level - lib_dec - core_dec_init.c (source / functions) Hit Total Coverage
Test: Coverage on main -- conformance test test_26252.py @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 307 333 92.2 %
Date: 2025-11-01 05:07:43 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include "stat_com.h"
      43             : #include "cnst.h"
      44             : #include "prot.h"
      45             : #include "rom_com.h"
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*-----------------------------------------------------------------------*
      49             :  * open_decoder_LPD()
      50             :  *
      51             :  * Initialization of state variables
      52             :  *-----------------------------------------------------------------------*/
      53             : 
      54       46288 : void open_decoder_LPD(
      55             :     Decoder_State *st,               /* i/o: decoder state structure                */
      56             :     const int32_t total_brate,       /* i  : total bitrate                          */
      57             :     const int32_t last_total_brate,  /* i  : last total bitrate                     */
      58             :     const int16_t bwidth,            /* i  : audio bandwidth                        */
      59             :     const int16_t MCT_flag,          /* i  : hMCT handle allocated (1) or not (0)   */
      60             :     const int16_t last_element_mode, /* i  : last element mode                      */
      61             :     const int16_t is_init            /* i  : indicate call during initialization    */
      62             : )
      63             : {
      64             :     int16_t i;
      65             :     int16_t mem_syn_r_size_old;
      66             :     int16_t mem_syn_r_size_new;
      67             :     int16_t fscaleFB;
      68             :     int16_t encoderLookahead, encoderLookaheadFB;
      69       46288 :     TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
      70       46288 :     TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
      71             : 
      72       46288 :     if ( st->codec_mode != MODE1 ) /*already updated in MODE1*/
      73             :     {
      74          67 :         st->fscale_old = st->fscale;
      75             :     }
      76             : 
      77       46288 :     st->sr_core = getCoreSamplerateMode2( st->element_mode, total_brate, bwidth, st->flag_ACELP16k, st->rf_flag, st->is_ism_format );
      78       46288 :     st->fscale = sr2fscale( st->sr_core );
      79       46288 :     fscaleFB = sr2fscale( st->output_Fs );
      80             : 
      81             :     /* initializing variables for frame lengths etc. right in the beginning */
      82       46288 :     st->L_frame = (int16_t) ( st->sr_core / FRAMES_PER_SEC );
      83       46288 :     if ( st->ini_frame == 0 )
      84             :     {
      85       25098 :         st->last_L_frame = st->L_frame_past = st->L_frame;
      86             :     }
      87       46288 :     if ( st->hTcxDec != NULL )
      88             :     {
      89       46016 :         st->hTcxDec->L_frameTCX = (int16_t) ( st->output_Fs / FRAMES_PER_SEC );
      90       46016 :         if ( st->ini_frame == 0 )
      91             :         {
      92       24826 :             st->L_frameTCX_past = st->hTcxDec->L_frameTCX;
      93             :         }
      94             :     }
      95             : 
      96       46288 :     st->tcxonly = getTcxonly( st->element_mode, total_brate, MCT_flag, st->is_ism_format );
      97             : 
      98             :     /* the TD TCX PLC in MODE1 still runs with 80ms subframes */
      99       46288 :     if ( ( st->element_mode == EVS_MONO && st->L_frame == L_FRAME16k && total_brate <= ACELP_32k ) || ( st->element_mode > EVS_MONO && st->L_frame == L_FRAME16k && total_brate <= MAX_ACELP_BRATE ) || ( st->tcxonly && ( st->sr_core == 32000 || st->sr_core == 16000 ) ) )
     100             :     {
     101       16898 :         st->nb_subfr = NB_SUBFR16k;
     102             :     }
     103             :     else
     104             :     {
     105       29390 :         st->nb_subfr = NB_SUBFR;
     106             :     }
     107       46288 :     st->bits_frame = (int16_t) ( ( (float) st->L_frame / (float) st->fscale ) * (float) FSCALE_DENOM / 128.0f * (float) total_brate / 100.0f + 0.49f );
     108             : 
     109       46288 :     st->TcxBandwidth = getTcxBandwidth( bwidth );
     110       46288 :     st->narrowBand = ( bwidth == NB ) ? 1 : 0;
     111       46288 :     encoderLookahead = ( L_LOOK_12k8 * st->fscale ) / FSCALE_DENOM;
     112       46288 :     encoderLookaheadFB = ( L_LOOK_12k8 * fscaleFB ) / FSCALE_DENOM;
     113             : 
     114       46288 :     if ( st->element_mode == IVAS_CPE_MDCT )
     115             :     {
     116       28378 :         st->pit_res_max = initPitchLagParameters( 12800, &st->pit_min, &st->pit_fr1, &st->pit_fr1b, &st->pit_fr2, &st->pit_max );
     117       28378 :         hTcxDec->pit_max_TCX = (int16_t) ( st->pit_max * st->output_Fs / 12800 );
     118       28378 :         hTcxDec->pit_min_TCX = (int16_t) ( st->pit_min * st->output_Fs / 12800 );
     119             :     }
     120             :     else
     121             :     {
     122       17910 :         st->pit_res_max = initPitchLagParameters( st->sr_core, &st->pit_min, &st->pit_fr1, &st->pit_fr1b, &st->pit_fr2, &st->pit_max );
     123       17910 :         if ( hTcxDec != NULL )
     124             :         {
     125       17638 :             hTcxDec->pit_max_TCX = (int16_t) ( st->pit_max * st->output_Fs / st->sr_core );
     126       17638 :             hTcxDec->pit_min_TCX = (int16_t) ( st->pit_min * st->output_Fs / st->sr_core );
     127             :         }
     128             :     }
     129             : 
     130       46288 :     if ( st->ini_frame == 0 )
     131             :     {
     132       25098 :         st->pit_res_max_past = st->pit_res_max;
     133             :     }
     134             : 
     135             :     /*Preemphasis param*/
     136       46288 :     if ( st->fscale < ( 16000 * FSCALE_DENOM ) / 12800 )
     137             :     {
     138       19138 :         st->preemph_fac = PREEMPH_FAC; /*NB*/
     139             :     }
     140       27150 :     else if ( st->fscale < ( 24000 * FSCALE_DENOM ) / 12800 )
     141             :     {
     142        7846 :         st->preemph_fac = PREEMPH_FAC_16k; /*WB*/
     143             :     }
     144             :     else
     145             :     {
     146       19304 :         st->preemph_fac = PREEMPH_FAC_SWB; /*SWB*/
     147             :     }
     148             : 
     149       46288 :     if ( st->sr_core == INT_FS_16k )
     150             :     {
     151        7846 :         st->gamma = GAMMA16k;
     152             :     }
     153       38442 :     else if ( st->element_mode > EVS_MONO && st->sr_core > INT_FS_16k )
     154             :     {
     155       19304 :         st->gamma = GAMMA16k;
     156             :     }
     157             :     else
     158             :     {
     159       19138 :         st->gamma = GAMMA1;
     160             :     }
     161             : 
     162             :     /* LPC quantization */
     163       46288 :     if ( st->sr_core <= INT_FS_16k && st->tcxonly == 0 )
     164             :     {
     165       26061 :         st->lpcQuantization = 1;
     166             :     }
     167             :     else
     168             :     {
     169       20227 :         st->lpcQuantization = 0;
     170             :     }
     171             : 
     172       46288 :     if ( st->tcxonly == 0 )
     173             :     {
     174       26061 :         st->numlpc = 1;
     175             :     }
     176             :     else
     177             :     {
     178       20227 :         st->numlpc = 2;
     179             :     }
     180             : 
     181             :     /* Initialize TBE */
     182       46288 :     st->prev_coder_type = GENERIC;
     183       46288 :     if ( st->hBWE_TD != NULL )
     184             :     {
     185       19219 :         set_f( st->hBWE_TD->prev_lsf_diff, 0.5f, LPC_SHB_ORDER - 2 );
     186       19219 :         st->hBWE_TD->prev_tilt_para = 0.0f;
     187       19219 :         set_zero( st->hBWE_TD->cur_sub_Aq, M + 1 );
     188             :     }
     189             : 
     190       46288 :     if ( st->hIGFDec != NULL )
     191             :     {
     192       46016 :         if ( !is_init || st->element_mode != IVAS_CPE_MDCT )
     193             :         {
     194       36086 :             init_tcx_cfg( st->hTcxCfg, total_brate, st->sr_core, st->output_Fs, st->L_frame, st->bwidth, st->hTcxDec->L_frameTCX, st->fscale, encoderLookahead, encoderLookaheadFB, st->preemph_fac, st->tcxonly, st->rf_flag, st->igf, st->hIGFDec->infoIGFStopFreq, st->element_mode, st->ini_frame, MCT_flag );
     195             :         }
     196             :         else
     197             :         {
     198        9930 :             st->hTcxCfg->tcx_curr_overlap_mode = st->hTcxCfg->tcx_last_overlap_mode = ALDO_WINDOW;
     199        9930 :             st->hTcxCfg->last_aldo = 1;
     200             :         }
     201             :     }
     202             : 
     203       46288 :     if ( st->hTECDec != NULL )
     204             :     {
     205          72 :         resetTecDec( st->hTECDec );
     206             :     }
     207             : 
     208       46288 :     if ( st->element_mode != IVAS_SCE )
     209             :     {
     210       32912 :         st->flag_cna = 0;
     211             :     }
     212       46288 :     if ( st->ini_frame == 0 )
     213             :     {
     214       25098 :         st->flag_cna = 0;
     215       25098 :         st->last_flag_cna = 0;
     216             :     }
     217             : 
     218             :     /* Static vectors to zero */
     219       46288 :     if ( st->ini_frame == 0 )
     220             :     {
     221       25098 :         st->last_is_cng = 0;
     222             : 
     223       25098 :         st->rate_switching_reset = 0;
     224             : 
     225       25098 :         if ( st->hTcxDec != NULL )
     226             :         {
     227       24826 :             reset_tcx_overl_buf( st->hTcxDec );
     228             : 
     229       24826 :             set_zero( st->hTcxDec->syn_OverlFB, L_FRAME_MAX / 2 );
     230       24826 :             set_zero( st->hTcxDec->old_synth, OLD_SYNTH_INTERNAL_DEC );
     231             : 
     232       24826 :             set_zero( st->hTcxDec->synth_history, L_PROT48k + L_FRAME_MAX );
     233             :         }
     234             : 
     235       25098 :         set_zero( st->syn, M + 1 );
     236             : 
     237       25098 :         set_zero( st->mem_syn_r, L_SYN_MEM );
     238             : 
     239       25098 :         mem_syn_r_size_old = 0; /* just to avoid MSVC warnings */
     240       25098 :         mem_syn_r_size_new = 0; /* just to avoid MSVC warnings */
     241             : 
     242       25098 :         st->con_tcx = 0;
     243             :     }
     244             :     else
     245             :     {
     246             :         /* Reset old_synth in case of core sampling rate switching and Mode 1/2 switching*/
     247       21190 :         if ( st->hTcxDec != NULL && ( ( st->L_frame != st->last_L_frame ) || ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO ) ) )
     248             :         {
     249        7243 :             set_zero( st->hTcxDec->old_synth, OLD_SYNTH_INTERNAL_DEC );
     250             :         }
     251             : 
     252             :         /*Compute size of old and new memories*/
     253       21190 :         mem_syn_r_size_old = (int16_t) ( 1.25 * st->last_L_frame / 20.f );
     254       21190 :         mem_syn_r_size_new = (int16_t) ( 1.25 * st->L_frame / 20.f );
     255             : 
     256             :         /*Reset LPC mem*/
     257       21190 :         if ( ( st->L_frame != st->last_L_frame ) || ( st->last_core == AMR_WB_CORE ) || ( st->last_core == HQ_CORE ) )
     258             :         {
     259        7728 :             set_zero( st->mem_MA, M );
     260        7728 :             if ( st->sr_core == INT_FS_16k )
     261             :             {
     262        1948 :                 mvr2r( GEWB2_Ave, st->mem_AR, M );
     263             :             }
     264             :             else
     265             :             {
     266        5780 :                 mvr2r( GEWB_Ave, st->mem_AR, M );
     267             :             }
     268             :         }
     269             : 
     270             :         /*Mode 1/2 switching*/
     271       21190 :         if ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO )
     272             :         {
     273          65 :             mvr2r( st->lsp_old, st->lspold_uw, M );
     274          65 :             mvr2r( st->lsf_old, st->lsfold_uw, M );
     275          65 :             set_zero( st->syn, M );
     276             :         }
     277       21190 :         if ( st->last_core == AMR_WB_CORE )
     278             :         {
     279           0 :             st->last_core = ACELP_CORE;
     280           0 :             st->last_core_bfi = ACELP_CORE;
     281             :         }
     282             : 
     283       21190 :         if ( ( ( st->element_mode != IVAS_CPE_DFT ) || ( st->element_mode == IVAS_CPE_DFT && st->prev_bfi ) ) && st->last_codec_mode == MODE1 && st->last_core == ACELP_CORE )
     284             :         {
     285             :             /* Switching from Mode 1 ACELP */
     286        9561 :             st->last_core_bfi = ACELP_CORE;
     287        9561 :             acelp_plc_mdct_transition( st );
     288             :         }
     289             : 
     290       21190 :         if ( st->last_codec_mode == MODE2 &&
     291           0 :              st->L_frame != st->last_L_frame &&
     292           0 :              ( ( st->m_frame_type == SID_FRAME && st->last_core > ACELP_CORE ) ||
     293           0 :                ( st->last_core > ACELP_CORE && st->core > ACELP_CORE ) || st->prev_bfi ) )
     294             :         {
     295           0 :             lerp( st->hHQ_core->old_outLB, st->hHQ_core->old_outLB, st->L_frame, st->last_L_frame );
     296             :         }
     297             : 
     298             :         /* Rate switching */
     299       21190 :         if ( st->last_codec_mode == MODE1 && st->last_core == HQ_CORE )
     300             :         {
     301             :             /* Switching from MDCT */
     302             : 
     303             :             /*Reset of ACELP memories*/
     304         571 :             st->rate_switching_reset = 1;
     305         571 :             st->tilt_code = TILT_CODE;
     306         571 :             set_zero( st->old_exc, L_EXC_MEM_DEC );
     307         571 :             set_zero( st->syn, 1 + M );
     308         571 :             set_zero( st->mem_syn2, M );
     309             : 
     310             :             /*OLA -> zero */
     311         571 :             if ( st->hTcxDec != NULL )
     312             :             {
     313         571 :                 reset_tcx_overl_buf( st->hTcxDec );
     314             :             }
     315             : 
     316         571 :             if ( st->hTcxCfg != NULL )
     317             :             {
     318         571 :                 mvr2r( st->hHQ_core->old_out + NS2SA( st->output_Fs, N_ZERO_MDCT_NS ), st->hTcxDec->syn_OverlFB, st->hTcxCfg->tcx_mdct_window_lengthFB );
     319         571 :                 st->hTcxCfg->last_aldo = 1; /*It was previously ALDO*/
     320         571 :                 st->hTcxCfg->tcx_curr_overlap_mode = ALDO_WINDOW;
     321             :             }
     322             : 
     323             :             /*OLA for Mode 2 TCX always reset in Mode switching cases*/
     324         571 :             if ( st->hHQ_core != NULL )
     325             :             {
     326         571 :                 set_f( st->hHQ_core->old_outLB, 0, st->L_frame );
     327             :             }
     328             : 
     329         571 :             st->last_core_bfi = TCX_20_CORE;
     330             : 
     331         571 :             if ( st->hPFstat != NULL )
     332             :             {
     333         571 :                 st->hPFstat->on = 0;
     334             :             }
     335             : 
     336             :             /* reset CLDFB memories */
     337         571 :             cldfb_reset_memory( st->cldfbAna );
     338         571 :             cldfb_reset_memory( st->cldfbBPF );
     339         571 :             cldfb_reset_memory( st->cldfbSyn );
     340         571 :             if ( st->cldfbSynHB != NULL )
     341             :             {
     342           2 :                 cldfb_reset_memory( st->cldfbSynHB );
     343             :             }
     344             :         }
     345       20619 :         else if ( ( st->L_frame != st->last_L_frame ) && ( st->L_frame <= L_FRAME16k ) && ( st->last_L_frame <= L_FRAME16k ) ) /* Rate switching between 12.8 and 16 kHz*/
     346             :         {
     347             :             /*Interpolation of ACELP memories*/
     348             : 
     349             :             /* convert quantized LSP vector */
     350         579 :             st->rate_switching_reset = lsp_convert_poly( st->lsp_old, st->L_frame, 0 );
     351         579 :             lsp2a_stab( st->lsp_old, st->old_Aq_12_8, M );
     352             : 
     353         579 :             lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core );
     354         579 :             mvr2r( st->lsp_old, st->lspold_uw, M );
     355         579 :             mvr2r( st->lsf_old, st->lsfold_uw, M );
     356             : 
     357         579 :             if ( !st->last_con_tcx )
     358             :             {
     359         574 :                 synth_mem_updt2( st->L_frame, st->last_L_frame, st->old_exc, st->mem_syn_r, st->mem_syn2, NULL, DEC );
     360             :             }
     361             : 
     362             :             /*mem of deemphasis stayed unchanged.*/
     363             :         }
     364       20040 :         else if ( st->L_frame != st->last_L_frame ) /* Rate switching involving TCX only modes */
     365             :         {
     366             :             /*Partial reset of ACELP memories*/
     367        6578 :             st->rate_switching_reset = 1;
     368             : 
     369             :             /*reset partly some memories*/
     370        6578 :             st->tilt_code = TILT_CODE;
     371        6578 :             if ( !st->last_con_tcx )
     372             :             {
     373        6554 :                 set_zero( st->old_exc, L_EXC_MEM_DEC );
     374             :             }
     375        6578 :             set_zero( st->old_Aq_12_8, M + 1 );
     376             : 
     377             :             /*Resamp others memories*/
     378             :             /*Size of LPC syn memory*/
     379        6578 :             lerp( st->mem_syn_r + L_SYN_MEM - mem_syn_r_size_old, st->mem_syn_r + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
     380        6578 :             mvr2r( st->mem_syn_r + L_SYN_MEM - M, st->mem_syn2, M );
     381             :         }
     382             :         /* update of lsf_old only needed in BASOP */
     383             :         /* else if( !st->tcxonly && (st->L_frame == L_FRAME16k) && (st->last_total_brate > ACELP_32k) ) */
     384             :         /* { */
     385             :         /*     lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core ); */
     386             :         /* } */
     387             :     }
     388             : 
     389       46288 :     if ( st->last_bwidth == NB && st->bwidth != NB && st->ini_frame != 0 )
     390             :     {
     391           0 :         st->rate_switching_reset = 1;
     392             :     }
     393             : 
     394       46288 :     if ( st->hTcxDec != NULL )
     395             :     {
     396       46016 :         st->hTcxDec->old_synth_len = 2 * st->L_frame;
     397       46016 :         st->hTcxDec->old_synth_lenFB = 2 * st->hTcxDec->L_frameTCX;
     398             :     }
     399             : 
     400             :     /* bass pf reset */
     401       46288 :     st->bpf_gain_param = 0;
     402       46288 :     if ( st->hBPF != NULL )
     403             :     {
     404       17638 :         set_f( st->hBPF->pst_old_syn, 0, NBPSF_PIT_MAX );
     405             :     }
     406             : 
     407             :     /* Formant postfilter */
     408       46288 :     if ( st->ini_frame == 0 )
     409             :     {
     410             :         /* do nothing */
     411             :     }
     412       21190 :     else if ( st->last_codec_mode == MODE2 )
     413             :     {
     414           0 :         if ( !st->tcxonly )
     415             :         {
     416           0 :             if ( st->hPFstat->on )
     417             :             {
     418           0 :                 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 );
     419           0 :                 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 );
     420             :             }
     421             :             else
     422             :             {
     423           0 :                 set_zero( st->hPFstat->mem_stp, L_SYN_MEM );
     424           0 :                 set_zero( st->hPFstat->mem_pf_in, L_SYN_MEM );
     425           0 :                 st->hPFstat->reset = 1;
     426           0 :                 st->hPFstat->gain_prec = 1.f;
     427             :             }
     428             :         }
     429           0 :         else if ( st->hPFstat->on )
     430             :         {
     431           0 :             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 );
     432           0 :             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 );
     433             :         }
     434             :     }
     435             :     else
     436             :     {
     437             :         /*codec mode switching*/
     438             : 
     439             :         /*reset post-filter except for Narrowband*/
     440       21190 :         if ( ( (int16_t) ( st->output_Fs / FRAMES_PER_SEC ) ) != L_FRAME8k )
     441             :         {
     442       21190 :             if ( st->hPFstat != NULL )
     443             :             {
     444       12701 :                 st->hPFstat->reset = 1;
     445       12701 :                 if ( st->hPFstat->on != 0 )
     446             :                 {
     447        7261 :                     st->hPFstat->reset = 0;
     448        7261 :                     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 );
     449        7261 :                     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 );
     450             :                 }
     451             :             }
     452             :         }
     453             :         else
     454             :         {
     455             :             /*feed last value old_synth as it is used for pre-emphasis mem*/
     456           0 :             if ( st->hTcxDec != NULL )
     457             :             {
     458           0 :                 st->hTcxDec->old_synth[st->hTcxDec->old_synth_len - 1] = st->syn[M];
     459             :             }
     460           0 :             if ( st->hBPF != NULL )
     461             :             {
     462           0 :                 st->hBPF->pst_old_syn[NBPSF_PIT_MAX - 1] = st->syn[M];
     463             :             }
     464             :         }
     465             :     }
     466             : 
     467             :     /* lsf and lsp initialization */
     468       46288 :     if ( st->ini_frame == 0 )
     469             :     {
     470       25098 :         mvr2r( st->lsp_old, st->lspold_uw, M );
     471       25098 :         mvr2r( st->lsf_old, st->lsfold_uw, M );
     472             : 
     473       25098 :         set_zero( st->lsf_cng, M );
     474             :     }
     475             : 
     476       46288 :     st->seed_tcx_plc = RANDOM_INITSEED;
     477       46288 :     st->past_gpit = 0.0f;
     478       46288 :     st->past_gcode = 0.0f;
     479       46288 :     st->gc_threshold = 0.0f;
     480             : 
     481       46288 :     lsf2lsp( st->lsf_cng, st->lspold_cng, M, INT_FS_12k8 );
     482       46288 :     lsp2a_stab( st->lspold_cng, st->Aq_cng, M );
     483       46288 :     st->plcBackgroundNoiseUpdated = 0;
     484       46288 :     mvr2r( st->lsf_old, st->lsf_q_cng, M );
     485       46288 :     mvr2r( st->lsf_old, st->old_lsf_q_cng, M );
     486       46288 :     mvr2r( st->lsp_old, st->lsp_q_cng, M );
     487       46288 :     mvr2r( st->lsp_old, st->old_lsp_q_cng, M );
     488       46288 :     set_zero( st->mem_syn_unv_back, M );
     489       46288 :     st->last_gain_syn_deemph = 1.f;
     490             : 
     491       46288 :     if ( st->last_codec_mode == MODE1 || st->ini_frame == 0 )
     492             :     {
     493             :         /* this assumes that MODE1 fades out in the frequency domain -
     494             :            otherwise some data from MODE1 would be needed here */
     495       46288 :         st->last_concealed_gain_syn_deemph = 1.f;
     496       46288 :         if ( hTcxDec != NULL )
     497             :         {
     498       46016 :             hTcxDec->conceal_eof_gain = 1.0f;
     499             :         }
     500             :     }
     501             :     /* Post processing */
     502       46288 :     set_zero( st->mem_Aq, NB_SUBFR16k * ( M + 1 ) );
     503             : 
     504       46288 :     st->lp_ener_bfi = 60.0f;
     505       46288 :     if ( st->ini_frame == 0 )
     506             :     {
     507       25098 :         st->prev_bfi = 0;
     508       25098 :         st->last_core_bfi = -1;
     509       25098 :         if ( st->hTcxDec != NULL )
     510             :         {
     511       24826 :             hTcxDec->tcxConceal_recalc_exc = 0;
     512             :         }
     513             :     }
     514       46288 :     st->prev_old_bfi = 0;
     515             : 
     516       46288 :     if ( st->hTcxDec != NULL )
     517             :     {
     518       46016 :         st->hTcxDec->noise_filling_index[0] = st->hTcxDec->noise_filling_index[1] = 0;
     519       46016 :         st->hTcxDec->tnsActive[0] = st->hTcxDec->tnsActive[1] = 0;
     520       46016 :         set_f( st->hTcxDec->ltpGainMemory, 0.0f, N_LTP_GAIN_MEMS );
     521             :     }
     522             : 
     523       46288 :     mvr2r( st->lsf_old, st->lsf_adaptive_mean, M );
     524       46288 :     mvr2r( st->lsf_old, st->lsfoldbfi0, M );
     525       46288 :     mvr2r( st->lsf_old, st->lsfoldbfi1, M );
     526             : 
     527       46288 :     st->clas_dec = UNVOICED_CLAS;
     528             : 
     529       46288 :     if ( !st->last_con_tcx )
     530             :     {
     531       46232 :         st->old_enr_LP = 0.0f; /* LP filter E of last good voiced frame or local LP filter E in TD TCX PLC */
     532             :     }
     533             : 
     534       46288 :     if ( st->prev_bfi )
     535             :     {
     536             :         /* calculate energy at the end of the previous frame */
     537         589 :         if ( st->core == ACELP_CORE && st->last_core == HQ_CORE )
     538             :         {
     539           0 :             fer_energy( st->hTcxDec->L_frameTCX, UNVOICED_CLAS, st->previoussynth, -1, &st->enr_old, 1 );
     540             :         }
     541             :     }
     542             :     else
     543             :     {
     544       45699 :         st->last_good = UNVOICED_CLAS; /* last good received frame for concealment */
     545       45699 :         st->enr_old = 0.0f;            /* energy at the end of the previous frame */
     546             :     }
     547       46288 :     st->lp_gainc = 0.0f;
     548       46288 :     st->lp_gainp = 0.0f;
     549             : 
     550       46288 :     if ( st->hTcxDec != NULL )
     551             :     {
     552       46016 :         st->hTcxDec->prev_widow_left_rect = 0;
     553             : 
     554       46016 :         if ( is_init || MCT_flag || !( st->element_mode == IVAS_CPE_MDCT && st->element_mode == last_element_mode ) )
     555             :         {
     556       42008 :             st->hTcxDec->CngLevelBackgroundTrace_bfi = PLC_MIN_CNG_LEV;
     557       42008 :             st->hTcxDec->NoiseLevelIndex_bfi = PLC_MIN_STAT_BUFF_SIZE - 1;
     558       42008 :             st->hTcxDec->CurrLevelIndex_bfi = 0;
     559       42008 :             st->hTcxDec->LastFrameLevel_bfi = PLC_MIN_CNG_LEV;
     560       42008 :             set_f( st->hTcxDec->NoiseLevelMemory_bfi, PLC_MIN_CNG_LEV, PLC_MIN_STAT_BUFF_SIZE );
     561             : 
     562       42008 :             st->hTcxDec->cummulative_damping_tcx = 1.0f;
     563             :         }
     564             :     }
     565             : 
     566       46288 :     st->cummulative_damping = 1.0f;
     567             : 
     568      601744 :     for ( i = 0; i < 2 * NB_SUBFR16k + 2; i++ )
     569             :     {
     570      555456 :         st->old_pitch_buf[i] = (float) st->pit_min;
     571             :     }
     572             : 
     573      601744 :     for ( i = 0; i < 2 * NB_SUBFR16k + 2; i++ )
     574             :     {
     575      555456 :         st->mem_pitch_gain[i] = 1.f;
     576             :     }
     577             : 
     578       46288 :     st->old_fpitch = (float) st->pit_min;
     579             : 
     580       46288 :     st->rate_switching_init = 1;
     581             : 
     582       46288 :     st->reset_mem_AR = 0;
     583             : 
     584             :     /* For phase dispersion */
     585       46288 :     set_zero( st->dispMem, 8 );
     586             : 
     587       46288 :     st->voice_fac = -1; /* purely unvoiced  */
     588             : 
     589             :     /* TCX-LTP */
     590       46288 :     if ( hTcxLtpDec != NULL )
     591             :     {
     592       46016 :         tcxltp_dec_init( hTcxLtpDec, st->ini_frame, st->last_codec_mode, st->element_mode, st->pit_max, st->sr_core );
     593             :     }
     594             : 
     595             :     /* TCX */
     596       46288 :     if ( hTcxDec != NULL )
     597             :     {
     598       46016 :         st->old_fpitchFB = (float) hTcxDec->pit_min_TCX;
     599             : 
     600       46016 :         if ( st->ini_frame == 0 || ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO ) )
     601             :         {
     602       24891 :             hTcxDec->tcxltp_last_gain_unmodified = 0.f;
     603             :         }
     604             : 
     605             :         /* TCX */
     606       46016 :         hTcxDec->tcx_lpc_shaped_ari = getTcxLpcShapedAri( total_brate, st->rf_flag, st->element_mode );
     607             : 
     608       46016 :         hTcxDec->envWeighted = 0;
     609             :     }
     610             : 
     611       46288 :     if ( st->hBPF != NULL )
     612             :     {
     613       17638 :         st->hBPF->pst_mem_deemp_err = 0.0f;
     614             :     }
     615             : 
     616       46288 :     if ( st->tcxonly )
     617             :     {
     618       20227 :         st->p_bpf_noise_buf = NULL;
     619             :     }
     620             :     else
     621             :     {
     622       26061 :         st->p_bpf_noise_buf = st->bpf_noise_buf;
     623             :     }
     624             : 
     625       46288 :     if ( bwidth == SWB && ( total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO )
     626             :     {
     627          28 :         st->tec_tfa = 1;
     628             :     }
     629             :     else
     630             :     {
     631       46260 :         st->tec_tfa = 0;
     632             :     }
     633             : 
     634       46288 :     st->tec_flag = 0;
     635       46288 :     st->tfa_flag = 0;
     636             : 
     637             :     /* needed in decoder to read the bitstream */
     638       46288 :     st->enableGplc = 0;
     639             : 
     640       46288 :     st->flagGuidedAcelp = 0;
     641       46288 :     st->tonal_mdct_plc_active = 0;
     642       46288 :     st->T0_4th = L_SUBFR;
     643       46288 :     st->guidedT0 = st->T0_4th;
     644             : 
     645       46288 :     if ( st->hPlcInfo != NULL && total_brate >= HQ_48k && st->element_mode == EVS_MONO )
     646             :     {
     647           0 :         st->enablePlcWaveadjust = 1;
     648             : 
     649           0 :         if ( st->hTcxDec != NULL && ( st->ini_frame == 0 || last_total_brate < HQ_48k || st->last_codec_mode == MODE1 || st->force_lpd_reset ) )
     650             :         {
     651           0 :             concealment_init( st->hTcxDec->L_frameTCX, st->hPlcInfo );
     652             :         }
     653             :     }
     654             :     else
     655             :     {
     656       46288 :         st->enablePlcWaveadjust = 0;
     657             :     }
     658             : 
     659             :     /* PLC: [TCX: Tonal Concealment] */
     660       46288 :     if ( st->hTonalMDCTConc != NULL && !( st->element_mode > EVS_MONO && st->ini_frame != 0 && st->hTonalMDCTConc->nSamples == st->hTcxDec->L_frameTCX ) )
     661             :     {
     662       25650 :         st->hTonalMDCTConc->nScaleFactors = 0;
     663       25650 :         st->hTonalMDCTConc->nSamples = 0;
     664       25650 :         st->hTonalMDCTConc->lastPcmOut = 0x0;
     665       25650 :         st->hTonalMDCTConc->lastBlockData.tonalConcealmentActive = 0;
     666       25650 :         st->hTonalMDCTConc->lastBlockData.nSamples = 0;
     667             : 
     668       25650 :         TonalMDCTConceal_Init( st->hTonalMDCTConc, st->hTcxDec->L_frameTCX, st->L_frame, FDNS_NPTS, st->hTcxCfg );
     669             :     }
     670             : 
     671       46288 :     st->last_tns_active = 0;
     672       46288 :     st->second_last_tns_active = 0;
     673       46288 :     st->second_last_core = -1;
     674             : 
     675       46288 :     if ( st->hTcxCfg != NULL &&
     676       46016 :          st->element_mode != EVS_MONO )
     677             :     {
     678       45944 :         st->hTcxCfg->fIsTNSAllowed = getTnsAllowed( is_init ? total_brate : st->bits_frame_nominal * FRAMES_PER_SEC, st->igf, st->element_mode );
     679             :     }
     680       46288 :     if ( hTcxDec != NULL )
     681             :     {
     682       46016 :         hTcxDec->tcxltp_second_last_pitch = st->old_fpitch;
     683       46016 :         hTcxDec->tcxltp_third_last_pitch = st->old_fpitch;
     684             :     }
     685             : 
     686       46288 :     if ( ( total_brate == ACELP_9k60 || total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO )
     687             :     {
     688          28 :         st->dec_glr = 1;
     689             :     }
     690             :     else
     691             :     {
     692       46260 :         st->dec_glr = 0;
     693             :     }
     694             : 
     695       46288 :     st->dec_glr_idx = 0;
     696             : 
     697       46288 :     st->VAD = 0;
     698       46288 :     if ( hTcxDec != NULL )
     699             :     {
     700       46016 :         hTcxDec->enableTcxLpc = 1;
     701             : 
     702       46016 :         hTcxDec->old_gaintcx_bfi = 0.0f;
     703             : 
     704       46016 :         hTcxDec->tcx_hm_LtpPitchLag = -1;
     705             :     }
     706             : 
     707       46288 :     if ( st->hTcxCfg != NULL )
     708             :     {
     709       46016 :         st->hTcxCfg->na_scale = 1.f;
     710             :     }
     711             : 
     712       46288 :     return;
     713             : }
     714             : 
     715             : /*-----------------------------------------------------------------------*
     716             :  * tcxltp_dec_init()
     717             :  *
     718             :  * Initialization TCX-LTP handle
     719             :  *-----------------------------------------------------------------------*/
     720             : 
     721       47986 : void tcxltp_dec_init(
     722             :     TCX_LTP_DEC_HANDLE hTcxLtpDec,
     723             :     const int16_t ini_frame,
     724             :     const int16_t last_codec_mode,
     725             :     const int16_t element_mode,
     726             :     const int16_t pit_max,
     727             :     const int32_t sr_core )
     728             : {
     729       47986 :     hTcxLtpDec->tcxltp_gain = 0.0f;
     730             : 
     731       47986 :     hTcxLtpDec->tcxltp = getTcxLtp( sr_core );
     732             : 
     733       47986 :     if ( ini_frame == 0 || ( last_codec_mode == MODE1 && element_mode == EVS_MONO ) )
     734             :     {
     735             : 
     736       26861 :         hTcxLtpDec->tcxltp_pitch_int = pit_max;
     737       26861 :         hTcxLtpDec->tcxltp_pitch_fr = 0;
     738             : 
     739       26861 :         if ( ini_frame == 0 )
     740             :         {
     741       26796 :             set_f( hTcxLtpDec->tcxltp_mem_in, 0.0f, TCXLTP_MAX_DELAY );
     742       26796 :             set_f( hTcxLtpDec->tcxltp_mem_out, 0.0f, L_FRAME48k );
     743       26796 :             hTcxLtpDec->tcxltp_pitch_int_post_prev = 0;
     744       26796 :             hTcxLtpDec->tcxltp_pitch_fr_post_prev = 0;
     745       26796 :             hTcxLtpDec->tcxltp_gain_post_prev = 0.f;
     746       26796 :             hTcxLtpDec->tcxltp_filt_idx_prev = -1;
     747             :         }
     748             :     }
     749             : 
     750       47986 :     return;
     751             : }
     752             : 
     753             : /*-----------------------------------------------------------------------*
     754             :  * reset_tcx_overl_buf()
     755             :  *
     756             :  * Reset TCX core overlap buffers
     757             :  *-----------------------------------------------------------------------*/
     758             : 
     759       26185 : void reset_tcx_overl_buf(
     760             :     TCX_DEC_HANDLE hTcxDec /* i/o: TCX decoder handle */
     761             : )
     762             : {
     763       26185 :     set_zero( hTcxDec->old_syn_Overl, L_FRAME32k / 2 );
     764       26185 :     set_zero( hTcxDec->syn_Overl_TDAC, L_FRAME32k / 2 );
     765       26185 :     set_zero( hTcxDec->syn_Overl, L_FRAME32k / 2 );
     766       26185 :     set_zero( hTcxDec->syn_Overl_TDACFB, L_FRAME_MAX / 2 );
     767             : 
     768       26185 :     return;
     769             : }
     770             : 
     771             : 
     772             : /*-----------------------------------------------------------------------*
     773             :  * acelp_plc_mdct_transition()
     774             :  *
     775             :  * Prepare MDCT OLA memories in TCX/HQ after ACELP PLC
     776             :  *-----------------------------------------------------------------------*/
     777             : 
     778        9564 : void acelp_plc_mdct_transition(
     779             :     Decoder_State *st /* i/o: Decoder state */
     780             : )
     781             : {
     782             :     float *w;
     783             :     int16_t W1, nz, delay_comp;
     784             :     int16_t i;
     785             : 
     786             :     /*PLC*/
     787        9564 :     if ( st->prev_bfi != 0 && st->hTcxCfg != NULL )
     788             :     {
     789         280 :         W1 = st->hTcxCfg->tcx_mdct_window_lengthFB;
     790         280 :         w = st->hTcxCfg->tcx_mdct_windowFB;
     791             : 
     792         280 :         nz = NS2SA( st->output_Fs, N_ZERO_MDCT_NS );
     793         280 :         delay_comp = NS2SA( st->output_Fs, DELAY_CLDFB_NS ); /*CLDFB delay*/
     794             : 
     795         280 :         mvr2r( st->hHQ_core->fer_samples + delay_comp, st->hTcxDec->syn_OverlFB, st->hTcxDec->L_frameTCX / 2 );
     796         280 :         lerp( st->hHQ_core->fer_samples + delay_comp, st->hTcxDec->syn_Overl, st->L_frame / 2, st->hTcxDec->L_frameTCX / 2 ); /*ACELP(bfi)->TCX(rect)*/
     797             :         /*old_out needed for MODE1 routine and syn_Overl_TDAC for MODE2 routine*/
     798         280 :         set_f( st->hHQ_core->old_out, 0, nz );
     799         280 :         mvr2r( st->hHQ_core->fer_samples + delay_comp, st->hHQ_core->old_out + nz, W1 );
     800             : 
     801      100940 :         for ( i = 0; i < W1; i++ )
     802             :         {
     803      100660 :             st->hHQ_core->old_out[i + nz] *= w[W1 - 1 - i] * w[W1 - 1 - i];
     804             :         }
     805         280 :         set_f( &st->hHQ_core->old_out[W1 + nz], 0, nz );
     806             : 
     807         280 :         lerp( st->hHQ_core->old_out, st->hHQ_core->old_outLB, st->L_frame, st->hTcxDec->L_frameTCX );
     808             : 
     809         280 :         mvr2r( st->hHQ_core->old_out + nz, st->hTcxDec->syn_Overl_TDACFB, st->hTcxDec->L_frameTCX / 2 );
     810         280 :         nz = NS2SA( st->sr_core, N_ZERO_MDCT_NS );
     811         280 :         mvr2r( st->hHQ_core->old_outLB + nz, st->hTcxDec->syn_Overl_TDAC, st->L_frame / 2 );
     812             :     }
     813             : 
     814        9564 :     return;
     815             : }

Generated by: LCOV version 1.14