LCOV - code coverage report
Current view: top level - lib_dec - core_dec_init.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ b5bd5e0684ad2d9250297e1e7a0ddc986cb7b37e Lines: 330 333 99.1 %
Date: 2025-10-27 07:01:45 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      732462 : 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      732462 :     TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
      70      732462 :     TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
      71             : 
      72      732462 :     if ( st->codec_mode != MODE1 ) /*already updated in MODE1*/
      73             :     {
      74         571 :         st->fscale_old = st->fscale;
      75             :     }
      76             : 
      77      732462 :     st->sr_core = getCoreSamplerateMode2( st->element_mode, total_brate, bwidth, st->flag_ACELP16k, st->rf_flag, st->is_ism_format );
      78      732462 :     st->fscale = sr2fscale( st->sr_core );
      79      732462 :     fscaleFB = sr2fscale( st->output_Fs );
      80             : 
      81             :     /* initializing variables for frame lengths etc. right in the beginning */
      82      732462 :     st->L_frame = (int16_t) ( st->sr_core / FRAMES_PER_SEC );
      83      732462 :     if ( st->ini_frame == 0 )
      84             :     {
      85      558318 :         st->last_L_frame = st->L_frame_past = st->L_frame;
      86             :     }
      87      732462 :     if ( st->hTcxDec != NULL )
      88             :     {
      89      725675 :         st->hTcxDec->L_frameTCX = (int16_t) ( st->output_Fs / FRAMES_PER_SEC );
      90      725675 :         if ( st->ini_frame == 0 )
      91             :         {
      92      551531 :             st->L_frameTCX_past = st->hTcxDec->L_frameTCX;
      93             :         }
      94             :     }
      95             : 
      96      732462 :     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      732462 :     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      277765 :         st->nb_subfr = NB_SUBFR16k;
     102             :     }
     103             :     else
     104             :     {
     105      454697 :         st->nb_subfr = NB_SUBFR;
     106             :     }
     107      732462 :     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      732462 :     st->TcxBandwidth = getTcxBandwidth( bwidth );
     110      732462 :     st->narrowBand = ( bwidth == NB ) ? 1 : 0;
     111      732462 :     encoderLookahead = ( L_LOOK_12k8 * st->fscale ) / FSCALE_DENOM;
     112      732462 :     encoderLookaheadFB = ( L_LOOK_12k8 * fscaleFB ) / FSCALE_DENOM;
     113             : 
     114      732462 :     if ( st->element_mode == IVAS_CPE_MDCT )
     115             :     {
     116      473506 :         st->pit_res_max = initPitchLagParameters( 12800, &st->pit_min, &st->pit_fr1, &st->pit_fr1b, &st->pit_fr2, &st->pit_max );
     117      473506 :         hTcxDec->pit_max_TCX = (int16_t) ( st->pit_max * st->output_Fs / 12800 );
     118      473506 :         hTcxDec->pit_min_TCX = (int16_t) ( st->pit_min * st->output_Fs / 12800 );
     119             :     }
     120             :     else
     121             :     {
     122      258956 :         st->pit_res_max = initPitchLagParameters( st->sr_core, &st->pit_min, &st->pit_fr1, &st->pit_fr1b, &st->pit_fr2, &st->pit_max );
     123      258956 :         if ( hTcxDec != NULL )
     124             :         {
     125      252169 :             hTcxDec->pit_max_TCX = (int16_t) ( st->pit_max * st->output_Fs / st->sr_core );
     126      252169 :             hTcxDec->pit_min_TCX = (int16_t) ( st->pit_min * st->output_Fs / st->sr_core );
     127             :         }
     128             :     }
     129             : 
     130      732462 :     if ( st->ini_frame == 0 )
     131             :     {
     132      558318 :         st->pit_res_max_past = st->pit_res_max;
     133             :     }
     134             : 
     135             :     /*Preemphasis param*/
     136      732462 :     if ( st->fscale < ( 16000 * FSCALE_DENOM ) / 12800 )
     137             :     {
     138      366557 :         st->preemph_fac = PREEMPH_FAC; /*NB*/
     139             :     }
     140      365905 :     else if ( st->fscale < ( 24000 * FSCALE_DENOM ) / 12800 )
     141             :     {
     142      148479 :         st->preemph_fac = PREEMPH_FAC_16k; /*WB*/
     143             :     }
     144             :     else
     145             :     {
     146      217426 :         st->preemph_fac = PREEMPH_FAC_SWB; /*SWB*/
     147             :     }
     148             : 
     149      732462 :     if ( st->sr_core == INT_FS_16k )
     150             :     {
     151      148479 :         st->gamma = GAMMA16k;
     152             :     }
     153      583983 :     else if ( st->element_mode > EVS_MONO && st->sr_core > INT_FS_16k )
     154             :     {
     155      217310 :         st->gamma = GAMMA16k;
     156             :     }
     157             :     else
     158             :     {
     159      366673 :         st->gamma = GAMMA1;
     160             :     }
     161             : 
     162             :     /* LPC quantization */
     163      732462 :     if ( st->sr_core <= INT_FS_16k && st->tcxonly == 0 )
     164             :     {
     165      431785 :         st->lpcQuantization = 1;
     166             :     }
     167             :     else
     168             :     {
     169      300677 :         st->lpcQuantization = 0;
     170             :     }
     171             : 
     172      732462 :     if ( st->tcxonly == 0 )
     173             :     {
     174      431785 :         st->numlpc = 1;
     175             :     }
     176             :     else
     177             :     {
     178      300677 :         st->numlpc = 2;
     179             :     }
     180             : 
     181             :     /* Initialize TBE */
     182      732462 :     st->prev_coder_type = GENERIC;
     183      732462 :     if ( st->hBWE_TD != NULL )
     184             :     {
     185      258810 :         set_f( st->hBWE_TD->prev_lsf_diff, 0.5f, LPC_SHB_ORDER - 2 );
     186      258810 :         st->hBWE_TD->prev_tilt_para = 0.0f;
     187      258810 :         set_zero( st->hBWE_TD->cur_sub_Aq, M + 1 );
     188             :     }
     189             : 
     190      732462 :     if ( st->hIGFDec != NULL )
     191             :     {
     192      725675 :         if ( !is_init || st->element_mode != IVAS_CPE_MDCT )
     193             :         {
     194      514793 :             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      210882 :             st->hTcxCfg->tcx_curr_overlap_mode = st->hTcxCfg->tcx_last_overlap_mode = ALDO_WINDOW;
     199      210882 :             st->hTcxCfg->last_aldo = 1;
     200             :         }
     201             :     }
     202             : 
     203      732462 :     if ( st->hTECDec != NULL )
     204             :     {
     205        1066 :         resetTecDec( st->hTECDec );
     206             :     }
     207             : 
     208      732462 :     if ( st->element_mode != IVAS_SCE )
     209             :     {
     210      561615 :         st->flag_cna = 0;
     211             :     }
     212      732462 :     if ( st->ini_frame == 0 )
     213             :     {
     214      558318 :         st->flag_cna = 0;
     215      558318 :         st->last_flag_cna = 0;
     216             :     }
     217             : 
     218             :     /* Static vectors to zero */
     219      732462 :     if ( st->ini_frame == 0 )
     220             :     {
     221      558318 :         st->last_is_cng = 0;
     222             : 
     223      558318 :         st->rate_switching_reset = 0;
     224             : 
     225      558318 :         if ( st->hTcxDec != NULL )
     226             :         {
     227      551531 :             reset_tcx_overl_buf( st->hTcxDec );
     228             : 
     229      551531 :             set_zero( st->hTcxDec->syn_OverlFB, L_FRAME_MAX / 2 );
     230      551531 :             set_zero( st->hTcxDec->old_synth, OLD_SYNTH_INTERNAL_DEC );
     231             : 
     232      551531 :             set_zero( st->hTcxDec->synth_history, L_PROT48k + L_FRAME_MAX );
     233             :         }
     234             : 
     235      558318 :         set_zero( st->syn, M + 1 );
     236             : 
     237      558318 :         set_zero( st->mem_syn_r, L_SYN_MEM );
     238             : 
     239      558318 :         mem_syn_r_size_old = 0; /* just to avoid MSVC warnings */
     240      558318 :         mem_syn_r_size_new = 0; /* just to avoid MSVC warnings */
     241             : 
     242      558318 :         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      174144 :         if ( st->hTcxDec != NULL && ( ( st->L_frame != st->last_L_frame ) || ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO ) ) )
     248             :         {
     249       27603 :             set_zero( st->hTcxDec->old_synth, OLD_SYNTH_INTERNAL_DEC );
     250             :         }
     251             : 
     252             :         /*Compute size of old and new memories*/
     253      174144 :         mem_syn_r_size_old = (int16_t) ( 1.25 * st->last_L_frame / 20.f );
     254      174144 :         mem_syn_r_size_new = (int16_t) ( 1.25 * st->L_frame / 20.f );
     255             : 
     256             :         /*Reset LPC mem*/
     257      174144 :         if ( ( st->L_frame != st->last_L_frame ) || ( st->last_core == AMR_WB_CORE ) || ( st->last_core == HQ_CORE ) )
     258             :         {
     259       29204 :             set_zero( st->mem_MA, M );
     260       29204 :             if ( st->sr_core == INT_FS_16k )
     261             :             {
     262        7204 :                 mvr2r( GEWB2_Ave, st->mem_AR, M );
     263             :             }
     264             :             else
     265             :             {
     266       22000 :                 mvr2r( GEWB_Ave, st->mem_AR, M );
     267             :             }
     268             :         }
     269             : 
     270             :         /*Mode 1/2 switching*/
     271      174144 :         if ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO )
     272             :         {
     273         325 :             mvr2r( st->lsp_old, st->lspold_uw, M );
     274         325 :             mvr2r( st->lsf_old, st->lsfold_uw, M );
     275         325 :             set_zero( st->syn, M );
     276             :         }
     277      174144 :         if ( st->last_core == AMR_WB_CORE )
     278             :         {
     279           8 :             st->last_core = ACELP_CORE;
     280           8 :             st->last_core_bfi = ACELP_CORE;
     281             :         }
     282             : 
     283      174144 :         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      102218 :             st->last_core_bfi = ACELP_CORE;
     287      102218 :             acelp_plc_mdct_transition( st );
     288             :         }
     289             : 
     290      174144 :         if ( st->last_codec_mode == MODE2 &&
     291          36 :              st->L_frame != st->last_L_frame &&
     292          36 :              ( ( st->m_frame_type == SID_FRAME && st->last_core > ACELP_CORE ) ||
     293          36 :                ( st->last_core > ACELP_CORE && st->core > ACELP_CORE ) || st->prev_bfi ) )
     294             :         {
     295          22 :             lerp( st->hHQ_core->old_outLB, st->hHQ_core->old_outLB, st->L_frame, st->last_L_frame );
     296             :         }
     297             : 
     298             :         /* Rate switching */
     299      174144 :         if ( st->last_codec_mode == MODE1 && st->last_core == HQ_CORE )
     300             :         {
     301             :             /* Switching from MDCT */
     302             : 
     303             :             /*Reset of ACELP memories*/
     304        2197 :             st->rate_switching_reset = 1;
     305        2197 :             st->tilt_code = TILT_CODE;
     306        2197 :             set_zero( st->old_exc, L_EXC_MEM_DEC );
     307        2197 :             set_zero( st->syn, 1 + M );
     308        2197 :             set_zero( st->mem_syn2, M );
     309             : 
     310             :             /*OLA -> zero */
     311        2197 :             if ( st->hTcxDec != NULL )
     312             :             {
     313        2197 :                 reset_tcx_overl_buf( st->hTcxDec );
     314             :             }
     315             : 
     316        2197 :             if ( st->hTcxCfg != NULL )
     317             :             {
     318        2197 :                 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        2197 :                 st->hTcxCfg->last_aldo = 1; /*It was previously ALDO*/
     320        2197 :                 st->hTcxCfg->tcx_curr_overlap_mode = ALDO_WINDOW;
     321             :             }
     322             : 
     323             :             /*OLA for Mode 2 TCX always reset in Mode switching cases*/
     324        2197 :             if ( st->hHQ_core != NULL )
     325             :             {
     326        2197 :                 set_f( st->hHQ_core->old_outLB, 0, st->L_frame );
     327             :             }
     328             : 
     329        2197 :             st->last_core_bfi = TCX_20_CORE;
     330             : 
     331        2197 :             if ( st->hPFstat != NULL )
     332             :             {
     333        2197 :                 st->hPFstat->on = 0;
     334             :             }
     335             : 
     336             :             /* reset CLDFB memories */
     337        2197 :             cldfb_reset_memory( st->cldfbAna );
     338        2197 :             cldfb_reset_memory( st->cldfbBPF );
     339        2197 :             cldfb_reset_memory( st->cldfbSyn );
     340        2197 :             if ( st->cldfbSynHB != NULL )
     341             :             {
     342           6 :                 cldfb_reset_memory( st->cldfbSynHB );
     343             :             }
     344             :         }
     345      171947 :         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        2686 :             st->rate_switching_reset = lsp_convert_poly( st->lsp_old, st->L_frame, 0 );
     351        2686 :             lsp2a_stab( st->lsp_old, st->old_Aq_12_8, M );
     352             : 
     353        2686 :             lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core );
     354        2686 :             mvr2r( st->lsp_old, st->lspold_uw, M );
     355        2686 :             mvr2r( st->lsf_old, st->lsfold_uw, M );
     356             : 
     357        2686 :             if ( !st->last_con_tcx )
     358             :             {
     359        2643 :                 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      169261 :         else if ( st->L_frame != st->last_L_frame ) /* Rate switching involving TCX only modes */
     365             :         {
     366             :             /*Partial reset of ACELP memories*/
     367       24321 :             st->rate_switching_reset = 1;
     368             : 
     369             :             /*reset partly some memories*/
     370       24321 :             st->tilt_code = TILT_CODE;
     371       24321 :             if ( !st->last_con_tcx )
     372             :             {
     373       24007 :                 set_zero( st->old_exc, L_EXC_MEM_DEC );
     374             :             }
     375       24321 :             set_zero( st->old_Aq_12_8, M + 1 );
     376             : 
     377             :             /*Resamp others memories*/
     378             :             /*Size of LPC syn memory*/
     379       24321 :             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       24321 :             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      732462 :     if ( st->last_bwidth == NB && st->bwidth != NB && st->ini_frame != 0 )
     390             :     {
     391           0 :         st->rate_switching_reset = 1;
     392             :     }
     393             : 
     394      732462 :     if ( st->hTcxDec != NULL )
     395             :     {
     396      725675 :         st->hTcxDec->old_synth_len = 2 * st->L_frame;
     397      725675 :         st->hTcxDec->old_synth_lenFB = 2 * st->hTcxDec->L_frameTCX;
     398             :     }
     399             : 
     400             :     /* bass pf reset */
     401      732462 :     st->bpf_gain_param = 0;
     402      732462 :     if ( st->hBPF != NULL )
     403             :     {
     404      252169 :         set_f( st->hBPF->pst_old_syn, 0, NBPSF_PIT_MAX );
     405             :     }
     406             : 
     407             :     /* Formant postfilter */
     408      732462 :     if ( st->ini_frame == 0 )
     409             :     {
     410             :         /* do nothing */
     411             :     }
     412      174144 :     else if ( st->last_codec_mode == MODE2 )
     413             :     {
     414          36 :         if ( !st->tcxonly )
     415             :         {
     416          12 :             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          12 :                 set_zero( st->hPFstat->mem_stp, L_SYN_MEM );
     424          12 :                 set_zero( st->hPFstat->mem_pf_in, L_SYN_MEM );
     425          12 :                 st->hPFstat->reset = 1;
     426          12 :                 st->hPFstat->gain_prec = 1.f;
     427             :             }
     428             :         }
     429          24 :         else if ( st->hPFstat->on )
     430             :         {
     431          12 :             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          12 :             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      174108 :         if ( ( (int16_t) ( st->output_Fs / FRAMES_PER_SEC ) ) != L_FRAME8k )
     441             :         {
     442      174102 :             if ( st->hPFstat != NULL )
     443             :             {
     444      123412 :                 st->hPFstat->reset = 1;
     445      123412 :                 if ( st->hPFstat->on != 0 )
     446             :                 {
     447       69749 :                     st->hPFstat->reset = 0;
     448       69749 :                     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       69749 :                     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           6 :             if ( st->hTcxDec != NULL )
     457             :             {
     458           6 :                 st->hTcxDec->old_synth[st->hTcxDec->old_synth_len - 1] = st->syn[M];
     459             :             }
     460           6 :             if ( st->hBPF != NULL )
     461             :             {
     462           6 :                 st->hBPF->pst_old_syn[NBPSF_PIT_MAX - 1] = st->syn[M];
     463             :             }
     464             :         }
     465             :     }
     466             : 
     467             :     /* lsf and lsp initialization */
     468      732462 :     if ( st->ini_frame == 0 )
     469             :     {
     470      558318 :         mvr2r( st->lsp_old, st->lspold_uw, M );
     471      558318 :         mvr2r( st->lsf_old, st->lsfold_uw, M );
     472             : 
     473      558318 :         set_zero( st->lsf_cng, M );
     474             :     }
     475             : 
     476      732462 :     st->seed_tcx_plc = RANDOM_INITSEED;
     477      732462 :     st->past_gpit = 0.0f;
     478      732462 :     st->past_gcode = 0.0f;
     479      732462 :     st->gc_threshold = 0.0f;
     480             : 
     481      732462 :     lsf2lsp( st->lsf_cng, st->lspold_cng, M, INT_FS_12k8 );
     482      732462 :     lsp2a_stab( st->lspold_cng, st->Aq_cng, M );
     483      732462 :     st->plcBackgroundNoiseUpdated = 0;
     484      732462 :     mvr2r( st->lsf_old, st->lsf_q_cng, M );
     485      732462 :     mvr2r( st->lsf_old, st->old_lsf_q_cng, M );
     486      732462 :     mvr2r( st->lsp_old, st->lsp_q_cng, M );
     487      732462 :     mvr2r( st->lsp_old, st->old_lsp_q_cng, M );
     488      732462 :     set_zero( st->mem_syn_unv_back, M );
     489      732462 :     st->last_gain_syn_deemph = 1.f;
     490             : 
     491      732462 :     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      732426 :         st->last_concealed_gain_syn_deemph = 1.f;
     496      732426 :         if ( hTcxDec != NULL )
     497             :         {
     498      725639 :             hTcxDec->conceal_eof_gain = 1.0f;
     499             :         }
     500             :     }
     501             :     /* Post processing */
     502      732462 :     set_zero( st->mem_Aq, NB_SUBFR16k * ( M + 1 ) );
     503             : 
     504      732462 :     st->lp_ener_bfi = 60.0f;
     505      732462 :     if ( st->ini_frame == 0 )
     506             :     {
     507      558318 :         st->prev_bfi = 0;
     508      558318 :         st->last_core_bfi = -1;
     509      558318 :         if ( st->hTcxDec != NULL )
     510             :         {
     511      551531 :             hTcxDec->tcxConceal_recalc_exc = 0;
     512             :         }
     513             :     }
     514      732462 :     st->prev_old_bfi = 0;
     515             : 
     516      732462 :     if ( st->hTcxDec != NULL )
     517             :     {
     518      725675 :         st->hTcxDec->noise_filling_index[0] = st->hTcxDec->noise_filling_index[1] = 0;
     519      725675 :         st->hTcxDec->tnsActive[0] = st->hTcxDec->tnsActive[1] = 0;
     520      725675 :         set_f( st->hTcxDec->ltpGainMemory, 0.0f, N_LTP_GAIN_MEMS );
     521             :     }
     522             : 
     523      732462 :     mvr2r( st->lsf_old, st->lsf_adaptive_mean, M );
     524      732462 :     mvr2r( st->lsf_old, st->lsfoldbfi0, M );
     525      732462 :     mvr2r( st->lsf_old, st->lsfoldbfi1, M );
     526             : 
     527      732462 :     st->clas_dec = UNVOICED_CLAS;
     528             : 
     529      732462 :     if ( !st->last_con_tcx )
     530             :     {
     531      731576 :         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      732462 :     if ( st->prev_bfi )
     535             :     {
     536             :         /* calculate energy at the end of the previous frame */
     537        5771 :         if ( st->core == ACELP_CORE && st->last_core == HQ_CORE )
     538             :         {
     539           8 :             fer_energy( st->hTcxDec->L_frameTCX, UNVOICED_CLAS, st->previoussynth, -1, &st->enr_old, 1 );
     540             :         }
     541             :     }
     542             :     else
     543             :     {
     544      726691 :         st->last_good = UNVOICED_CLAS; /* last good received frame for concealment */
     545      726691 :         st->enr_old = 0.0f;            /* energy at the end of the previous frame */
     546             :     }
     547      732462 :     st->lp_gainc = 0.0f;
     548      732462 :     st->lp_gainp = 0.0f;
     549             : 
     550      732462 :     if ( st->hTcxDec != NULL )
     551             :     {
     552      725675 :         st->hTcxDec->prev_widow_left_rect = 0;
     553             : 
     554      725675 :         if ( is_init || MCT_flag || !( st->element_mode == IVAS_CPE_MDCT && st->element_mode == last_element_mode ) )
     555             :         {
     556      672805 :             st->hTcxDec->CngLevelBackgroundTrace_bfi = PLC_MIN_CNG_LEV;
     557      672805 :             st->hTcxDec->NoiseLevelIndex_bfi = PLC_MIN_STAT_BUFF_SIZE - 1;
     558      672805 :             st->hTcxDec->CurrLevelIndex_bfi = 0;
     559      672805 :             st->hTcxDec->LastFrameLevel_bfi = PLC_MIN_CNG_LEV;
     560      672805 :             set_f( st->hTcxDec->NoiseLevelMemory_bfi, PLC_MIN_CNG_LEV, PLC_MIN_STAT_BUFF_SIZE );
     561             : 
     562      672805 :             st->hTcxDec->cummulative_damping_tcx = 1.0f;
     563             :         }
     564             :     }
     565             : 
     566      732462 :     st->cummulative_damping = 1.0f;
     567             : 
     568     9522006 :     for ( i = 0; i < 2 * NB_SUBFR16k + 2; i++ )
     569             :     {
     570     8789544 :         st->old_pitch_buf[i] = (float) st->pit_min;
     571             :     }
     572             : 
     573     9522006 :     for ( i = 0; i < 2 * NB_SUBFR16k + 2; i++ )
     574             :     {
     575     8789544 :         st->mem_pitch_gain[i] = 1.f;
     576             :     }
     577             : 
     578      732462 :     st->old_fpitch = (float) st->pit_min;
     579             : 
     580      732462 :     st->rate_switching_init = 1;
     581             : 
     582      732462 :     st->reset_mem_AR = 0;
     583             : 
     584             :     /* For phase dispersion */
     585      732462 :     set_zero( st->dispMem, 8 );
     586             : 
     587      732462 :     st->voice_fac = -1; /* purely unvoiced  */
     588             : 
     589             :     /* TCX-LTP */
     590      732462 :     if ( hTcxLtpDec != NULL )
     591             :     {
     592      725675 :         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      732462 :     if ( hTcxDec != NULL )
     597             :     {
     598      725675 :         st->old_fpitchFB = (float) hTcxDec->pit_min_TCX;
     599             : 
     600      725675 :         if ( st->ini_frame == 0 || ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO ) )
     601             :         {
     602      551856 :             hTcxDec->tcxltp_last_gain_unmodified = 0.f;
     603             :         }
     604             : 
     605             :         /* TCX */
     606      725675 :         hTcxDec->tcx_lpc_shaped_ari = getTcxLpcShapedAri( total_brate, st->rf_flag, st->element_mode );
     607             : 
     608      725675 :         hTcxDec->envWeighted = 0;
     609             :     }
     610             : 
     611      732462 :     if ( st->hBPF != NULL )
     612             :     {
     613      252169 :         st->hBPF->pst_mem_deemp_err = 0.0f;
     614             :     }
     615             : 
     616      732462 :     if ( st->tcxonly )
     617             :     {
     618      300677 :         st->p_bpf_noise_buf = NULL;
     619             :     }
     620             :     else
     621             :     {
     622      431785 :         st->p_bpf_noise_buf = st->bpf_noise_buf;
     623             :     }
     624             : 
     625      732462 :     if ( bwidth == SWB && ( total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO )
     626             :     {
     627         148 :         st->tec_tfa = 1;
     628             :     }
     629             :     else
     630             :     {
     631      732314 :         st->tec_tfa = 0;
     632             :     }
     633             : 
     634      732462 :     st->tec_flag = 0;
     635      732462 :     st->tfa_flag = 0;
     636             : 
     637             :     /* needed in decoder to read the bitstream */
     638      732462 :     st->enableGplc = 0;
     639             : 
     640      732462 :     st->flagGuidedAcelp = 0;
     641      732462 :     st->tonal_mdct_plc_active = 0;
     642      732462 :     st->T0_4th = L_SUBFR;
     643      732462 :     st->guidedT0 = st->T0_4th;
     644             : 
     645      732462 :     if ( st->hPlcInfo != NULL && total_brate >= HQ_48k && st->element_mode == EVS_MONO )
     646             :     {
     647         156 :         st->enablePlcWaveadjust = 1;
     648             : 
     649         156 :         if ( st->hTcxDec != NULL && ( st->ini_frame == 0 || last_total_brate < HQ_48k || st->last_codec_mode == MODE1 || st->force_lpd_reset ) )
     650             :         {
     651         144 :             concealment_init( st->hTcxDec->L_frameTCX, st->hPlcInfo );
     652             :         }
     653             :     }
     654             :     else
     655             :     {
     656      732306 :         st->enablePlcWaveadjust = 0;
     657             :     }
     658             : 
     659             :     /* PLC: [TCX: Tonal Concealment] */
     660      732462 :     if ( st->hTonalMDCTConc != NULL && !( st->element_mode > EVS_MONO && st->ini_frame != 0 && st->hTonalMDCTConc->nSamples == st->hTcxDec->L_frameTCX ) )
     661             :     {
     662      554233 :         st->hTonalMDCTConc->nScaleFactors = 0;
     663      554233 :         st->hTonalMDCTConc->nSamples = 0;
     664      554233 :         st->hTonalMDCTConc->lastPcmOut = 0x0;
     665      554233 :         st->hTonalMDCTConc->lastBlockData.tonalConcealmentActive = 0;
     666      554233 :         st->hTonalMDCTConc->lastBlockData.nSamples = 0;
     667             : 
     668      554233 :         TonalMDCTConceal_Init( st->hTonalMDCTConc, st->hTcxDec->L_frameTCX, st->L_frame, FDNS_NPTS, st->hTcxCfg );
     669             :     }
     670             : 
     671      732462 :     st->last_tns_active = 0;
     672      732462 :     st->second_last_tns_active = 0;
     673      732462 :     st->second_last_core = -1;
     674             : 
     675      732462 :     if ( st->hTcxCfg != NULL &&
     676      725675 :          st->element_mode != EVS_MONO )
     677             :     {
     678      724609 :         st->hTcxCfg->fIsTNSAllowed = getTnsAllowed( is_init ? total_brate : st->bits_frame_nominal * FRAMES_PER_SEC, st->igf, st->element_mode );
     679             :     }
     680      732462 :     if ( hTcxDec != NULL )
     681             :     {
     682      725675 :         hTcxDec->tcxltp_second_last_pitch = st->old_fpitch;
     683      725675 :         hTcxDec->tcxltp_third_last_pitch = st->old_fpitch;
     684             :     }
     685             : 
     686      732462 :     if ( ( total_brate == ACELP_9k60 || total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO )
     687             :     {
     688         234 :         st->dec_glr = 1;
     689             :     }
     690             :     else
     691             :     {
     692      732228 :         st->dec_glr = 0;
     693             :     }
     694             : 
     695      732462 :     st->dec_glr_idx = 0;
     696             : 
     697      732462 :     st->VAD = 0;
     698      732462 :     if ( hTcxDec != NULL )
     699             :     {
     700      725675 :         hTcxDec->enableTcxLpc = 1;
     701             : 
     702      725675 :         hTcxDec->old_gaintcx_bfi = 0.0f;
     703             : 
     704      725675 :         hTcxDec->tcx_hm_LtpPitchLag = -1;
     705             :     }
     706             : 
     707      732462 :     if ( st->hTcxCfg != NULL )
     708             :     {
     709      725675 :         st->hTcxCfg->na_scale = 1.f;
     710             :     }
     711             : 
     712      732462 :     return;
     713             : }
     714             : 
     715             : /*-----------------------------------------------------------------------*
     716             :  * tcxltp_dec_init()
     717             :  *
     718             :  * Initialization TCX-LTP handle
     719             :  *-----------------------------------------------------------------------*/
     720             : 
     721      743165 : 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      743165 :     hTcxLtpDec->tcxltp_gain = 0.0f;
     730             : 
     731      743165 :     hTcxLtpDec->tcxltp = getTcxLtp( sr_core );
     732             : 
     733      743165 :     if ( ini_frame == 0 || ( last_codec_mode == MODE1 && element_mode == EVS_MONO ) )
     734             :     {
     735             : 
     736      569346 :         hTcxLtpDec->tcxltp_pitch_int = pit_max;
     737      569346 :         hTcxLtpDec->tcxltp_pitch_fr = 0;
     738             : 
     739      569346 :         if ( ini_frame == 0 )
     740             :         {
     741      569021 :             set_f( hTcxLtpDec->tcxltp_mem_in, 0.0f, TCXLTP_MAX_DELAY );
     742      569021 :             set_f( hTcxLtpDec->tcxltp_mem_out, 0.0f, L_FRAME48k );
     743      569021 :             hTcxLtpDec->tcxltp_pitch_int_post_prev = 0;
     744      569021 :             hTcxLtpDec->tcxltp_pitch_fr_post_prev = 0;
     745      569021 :             hTcxLtpDec->tcxltp_gain_post_prev = 0.f;
     746      569021 :             hTcxLtpDec->tcxltp_filt_idx_prev = -1;
     747             :         }
     748             :     }
     749             : 
     750      743165 :     return;
     751             : }
     752             : 
     753             : /*-----------------------------------------------------------------------*
     754             :  * reset_tcx_overl_buf()
     755             :  *
     756             :  * Reset TCX core overlap buffers
     757             :  *-----------------------------------------------------------------------*/
     758             : 
     759      557121 : void reset_tcx_overl_buf(
     760             :     TCX_DEC_HANDLE hTcxDec /* i/o: TCX decoder handle */
     761             : )
     762             : {
     763      557121 :     set_zero( hTcxDec->old_syn_Overl, L_FRAME32k / 2 );
     764      557121 :     set_zero( hTcxDec->syn_Overl_TDAC, L_FRAME32k / 2 );
     765      557121 :     set_zero( hTcxDec->syn_Overl, L_FRAME32k / 2 );
     766      557121 :     set_zero( hTcxDec->syn_Overl_TDACFB, L_FRAME_MAX / 2 );
     767             : 
     768      557121 :     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      102339 : 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      102339 :     if ( st->prev_bfi != 0 && st->hTcxCfg != NULL )
     788             :     {
     789        3513 :         W1 = st->hTcxCfg->tcx_mdct_window_lengthFB;
     790        3513 :         w = st->hTcxCfg->tcx_mdct_windowFB;
     791             : 
     792        3513 :         nz = NS2SA( st->output_Fs, N_ZERO_MDCT_NS );
     793        3513 :         delay_comp = NS2SA( st->output_Fs, DELAY_CLDFB_NS ); /*CLDFB delay*/
     794             : 
     795        3513 :         mvr2r( st->hHQ_core->fer_samples + delay_comp, st->hTcxDec->syn_OverlFB, st->hTcxDec->L_frameTCX / 2 );
     796        3513 :         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        3513 :         set_f( st->hHQ_core->old_out, 0, nz );
     799        3513 :         mvr2r( st->hHQ_core->fer_samples + delay_comp, st->hHQ_core->old_out + nz, W1 );
     800             : 
     801     1017673 :         for ( i = 0; i < W1; i++ )
     802             :         {
     803     1014160 :             st->hHQ_core->old_out[i + nz] *= w[W1 - 1 - i] * w[W1 - 1 - i];
     804             :         }
     805        3513 :         set_f( &st->hHQ_core->old_out[W1 + nz], 0, nz );
     806             : 
     807        3513 :         lerp( st->hHQ_core->old_out, st->hHQ_core->old_outLB, st->L_frame, st->hTcxDec->L_frameTCX );
     808             : 
     809        3513 :         mvr2r( st->hHQ_core->old_out + nz, st->hTcxDec->syn_Overl_TDACFB, st->hTcxDec->L_frameTCX / 2 );
     810        3513 :         nz = NS2SA( st->sr_core, N_ZERO_MDCT_NS );
     811        3513 :         mvr2r( st->hHQ_core->old_outLB + nz, st->hTcxDec->syn_Overl_TDAC, st->L_frame / 2 );
     812             :     }
     813             : 
     814      102339 :     return;
     815             : }

Generated by: LCOV version 1.14