LCOV - code coverage report
Current view: top level - lib_dec - dec_LPD.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 8834b716eb6d7dfb881d5c69dd21cb18e1692722 Lines: 145 288 50.3 %
Date: 2025-07-09 08:36:12 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #ifdef DEBUGGING
      41             : #include "debug.h"
      42             : #endif
      43             : #include <math.h>
      44             : #include "prot.h"
      45             : #include "rom_com.h"
      46             : #include "cnst.h"
      47             : #include "basop_proto_func.h"
      48             : #include "stat_com.h"
      49             : #include "wmc_auto.h"
      50             : 
      51             : 
      52             : /*-------------------------------------------------------------------*
      53             :  * decoder_LPD()
      54             :  *
      55             :  * Core decoder MODE2
      56             :  *--------------------------------------------------------------------*/
      57             : 
      58        3750 : void decoder_LPD(
      59             :     Decoder_State *st,     /* i/o: decoder memory state pointer        */
      60             :     float signal_out[],    /* o  : signal with LPD delay (7 subfrs)    */
      61             :     float signal_outFB[],  /* o  : synthesis @output_FS                */
      62             :     int16_t *total_nbbits, /* i/o:    number of bits / decoded bits    */
      63             :     float *bpf_noise_buf,  /* i/o: BPF noise buffer                    */
      64             :     int16_t bfi,           /* i  : BFI flag                            */
      65             :     int16_t *bitsRead,     /* o  : number of read bits                 */
      66             :     int16_t param[],       /* o  : buffer of parameters                */
      67             :     float *pitch_buf,      /* i/o: floating pitch values for each subfr*/
      68             :     float *voice_factors,  /* o  : voicing factors                     */
      69             :     float *ptr_bwe_exc     /* o  : excitation for SWB TBE              */
      70             : )
      71             : {
      72             :     int16_t *prm;
      73             :     int16_t param_lpc[NPRM_LPC_NEW];
      74             :     float synth_buf[OLD_SYNTH_INTERNAL_DEC + L_FRAME_PLUS_INTERNAL + M];
      75             :     float *synth;
      76             :     float synth_bufFB[OLD_SYNTH_SIZE_DEC + L_FRAME_PLUS + M];
      77             :     float *synthFB;
      78             :     float lsf[( NB_DIV + 1 ) * M], lsp[( NB_DIV + 1 ) * M], lspmid[M], lsfmid[M];
      79             :     float Aq[( NB_SUBFR16k + 1 ) * ( M + 1 )];
      80             :     int16_t pitch[NB_SUBFR16k];
      81             :     float pit_gain[NB_SUBFR16k];
      82             :     int16_t i, k;
      83             :     int16_t past_core_mode, offset;
      84             :     int16_t L_frame, nb_subfr, L_frameTCX;
      85             :     Word16 Aind[M + 1], lspind[M];
      86             :     float tmp_old[M + 1], tmp_new[M + 1], enr_old, enr_new;
      87             :     float lspnew_uw[NB_DIV * M], lsfnew_uw[NB_DIV * M];
      88             :     float lsf_q_1st_rf[M], lsf_q_rf[M], lsp_q_rf[M];
      89             :     float lsp_diff;
      90             :     int16_t LSF_Q_prediction; /* o  : LSF prediction mode                 */
      91             :     int16_t tcx_last_overlap_mode, tcx_current_overlap_mode;
      92        3750 :     TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
      93        3750 :     TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
      94             : 
      95             :     /*--------------------------------------------------------------------------------*
      96             :      * Initializations
      97             :      *--------------------------------------------------------------------------------*/
      98             : 
      99        3750 :     prm = param;           /* to avoid compilation warnings */
     100        3750 :     LSF_Q_prediction = -1; /* to avoid compilation warnings */
     101        3750 :     enr_old = 0.0f;
     102        3750 :     enr_new = 0.0f;
     103             : 
     104        3750 :     if ( st->use_partial_copy && st->rf_frame_type >= RF_TCXFD && st->rf_frame_type <= RF_TCXTD2 )
     105             :     {
     106           0 :         bfi = st->bfi;
     107             :     }
     108             : 
     109        3750 :     past_core_mode = st->last_core_bfi;
     110             : 
     111             :     /*Adjust bit per frame*/
     112        3750 :     if ( !bfi )
     113             :     {
     114        3750 :         st->bits_frame_core = st->bits_frame - ( *bitsRead );
     115             :     }
     116             : 
     117             :     /* Framing parameters */
     118        3750 :     L_frame = st->L_frame;
     119        3750 :     L_frameTCX = st->hTcxDec->L_frameTCX;
     120        3750 :     nb_subfr = st->nb_subfr;
     121             : 
     122             :     /* Initialize pointers */
     123        3750 :     synth = synth_buf + st->hTcxDec->old_synth_len;
     124        3750 :     synthFB = synth_bufFB + st->hTcxDec->old_synth_lenFB;
     125        3750 :     mvr2r( st->hTcxDec->old_synth, synth_buf, st->hTcxDec->old_synth_len );
     126        3750 :     mvr2r( st->hTcxDec->old_synthFB, synth_bufFB, st->hTcxDec->old_synth_lenFB );
     127        3750 :     set_zero( synth, L_FRAME_PLUS_INTERNAL + M );
     128        3750 :     set_zero( synthFB, L_FRAME_PLUS + M );
     129             : 
     130             :     /*For post-processing (post-filtering+blind BWE)*/
     131        3750 :     if ( st->tcxonly == 0 )
     132             :     {
     133             :         /* for bass postfilter */
     134        3750 :         set_s( pitch, L_SUBFR, nb_subfr );
     135        3750 :         set_zero( pit_gain, nb_subfr );
     136             :     }
     137             : 
     138             :     /* PLC: [Common: Memory update]
     139             :      * PLC: Update the number of lost frames */
     140        3750 :     if ( bfi )
     141             :     {
     142           0 :         st->nbLostCmpt++;
     143             :     }
     144             : 
     145             :     /*--------------------------------------------------------------------------------*
     146             :      * BITSTREAM DECODING
     147             :      *--------------------------------------------------------------------------------*/
     148             : 
     149        3750 :     if ( !bfi )
     150             :     {
     151        3750 :         st->second_last_core = st->last_core;
     152        3750 :         tcx_last_overlap_mode = st->hTcxCfg->tcx_last_overlap_mode;
     153        3750 :         tcx_current_overlap_mode = st->hTcxCfg->tcx_curr_overlap_mode;
     154             : 
     155        3750 :         dec_prm( st, param, param_lpc, total_nbbits, bitsRead );
     156             : 
     157        3750 :         if ( !st->rate_switching_init && ( st->last_codec_mode ) == MODE2 && st->BER_detect )
     158             :         {
     159           0 :             st->coder_type = st->last_coder_type;
     160           0 :             st->last_core = st->second_last_core;
     161           0 :             st->hTcxCfg->tcx_last_overlap_mode = tcx_last_overlap_mode;
     162           0 :             st->hTcxCfg->tcx_curr_overlap_mode = tcx_current_overlap_mode;
     163           0 :             st->bfi = 1;
     164           0 :             bfi = 1;
     165           0 :             st->flagGuidedAcelp = 0;
     166           0 :             st->nbLostCmpt++;
     167           0 :             st->core_brate = st->last_core_brate;
     168           0 :             st->core = GetPLCModeDecision( st );
     169             :         }
     170             :     }
     171             :     else
     172             :     {
     173           0 :         if ( st->use_partial_copy && st->rf_frame_type >= RF_TCXFD && st->rf_frame_type <= RF_TCXTD2 )
     174             :         {
     175           0 :             dec_prm( st, param, param_lpc, total_nbbits, bitsRead );
     176             :         }
     177             : 
     178           0 :         if ( st->nbLostCmpt > 1 )
     179             :         {
     180           0 :             st->flagGuidedAcelp = 0;
     181             :         }
     182             :         /* PLC: [Common: mode decision]
     183             :          * PLC: Decide which Concealment to use. Update pitch lags if needed */
     184           0 :         st->core = GetPLCModeDecision( st );
     185             :     }
     186             : 
     187             :     /* PLC: [Common: Memory update]
     188             :      * PLC: Update the number of lost frames */
     189        3750 :     if ( !bfi )
     190             :     {
     191        3750 :         if ( st->prev_bfi == 1 )
     192             :         {
     193           0 :             st->prev_nbLostCmpt = st->nbLostCmpt;
     194             :         }
     195             :         else
     196             :         {
     197        3750 :             st->prev_nbLostCmpt = 0;
     198             :         }
     199             : 
     200        3750 :         st->nbLostCmpt = 0;
     201             :     }
     202             : 
     203             :     /*--------------------------------------------------------------------------------*
     204             :      * LPC PARAMETERS
     205             :      *--------------------------------------------------------------------------------*/
     206             : 
     207        3750 :     if ( ( bfi == 0 ) || ( bfi == 1 && st->use_partial_copy && st->rf_frame_type == RF_TCXFD ) )
     208             :     {
     209        3750 :         if ( st->use_partial_copy && ( st->rf_frame_type < RF_TCXFD || st->rf_frame_type > RF_TCXTD2 ) )
     210             :         {
     211           0 :             if ( hTcxDec->envWeighted )
     212             :             {
     213           0 :                 mvr2r( st->lspold_uw, st->lsp_old, M );
     214           0 :                 mvr2r( st->lsfold_uw, st->lsf_old, M );
     215           0 :                 hTcxDec->envWeighted = 0;
     216             :             }
     217             : 
     218             :             /* first stage VQ, 8 bits; reuse TCX high rate codebook */
     219           0 :             set_f( lsf_q_1st_rf, 0.0f, M );
     220           0 :             vlpc_1st_dec( param_lpc[0], lsf_q_1st_rf, st->sr_core );
     221             : 
     222             :             /* second stage vq */
     223             :             /* quantized lsf from two stages */
     224           0 :             v_add( lsf_q_1st_rf, lsf_q_diff_cb_8b_rf + M * param_lpc[1], lsf_q_rf, M );
     225             : 
     226           0 :             v_sort( lsf_q_rf, 0, M - 1 );
     227           0 :             reorder_lsf( lsf_q_rf, LSF_GAP, M, st->sr_core );
     228             : 
     229             :             /* current n-th ACELP frame and its corresponding partial copy */
     230           0 :             lsf2lsp( lsf_q_rf, lsp_q_rf, M, st->sr_core );
     231             : 
     232             :             /* copy the old and current lsfs and lsps into the lsf[] and lsp[] buffer for interpolation */
     233           0 :             mvr2r( st->lsf_old, &lsf[0], M );
     234           0 :             mvr2r( st->lsp_old, &lsp[0], M );
     235           0 :             mvr2r( lsf_q_rf, &lsf[M], M );
     236           0 :             mvr2r( lsp_q_rf, &lsp[M], M );
     237           0 :             lsp_diff = 0.0f;
     238             : 
     239           0 :             for ( i = 0; i < M; i++ )
     240             :             {
     241           0 :                 lsp_diff += (float) fabs( lsp[i + M] - lsp[i] );
     242             :             }
     243             : 
     244           0 :             if ( st->core == ACELP_CORE && st->last_core == ACELP_CORE && lsp_diff < 1.6f && lsp_diff > 0.12f && st->next_coder_type == GENERIC && !st->prev_use_partial_copy && st->last_coder_type == UNVOICED && st->rf_frame_type >= RF_GENPRED )
     245             :             {
     246           0 :                 mvr2r( &lsp[0], &lsp[M], M );
     247             :             }
     248             : 
     249             :             /* update mem_MA and mem_AR memories */
     250           0 :             lsf_update_memory( st->narrowBand, &lsf[M], st->mem_MA, st->mem_MA );
     251           0 :             mvr2r( &lsf[M], st->mem_AR, M );
     252             : 
     253           0 :             for ( k = 0; k < st->numlpc; ++k )
     254             :             {
     255           0 :                 mvr2r( &lsp[( k + 1 ) * M], &lspnew_uw[k * M], M );
     256           0 :                 mvr2r( &lsf[( k + 1 ) * M], &lsfnew_uw[k * M], M );
     257             :             }
     258             :         }
     259        3750 :         else if ( ( hTcxDec->enableTcxLpc && st->core != ACELP_CORE ) || ( bfi && st->use_partial_copy && st->rf_frame_type == RF_TCXFD ) )
     260           0 :         {
     261             :             int16_t tcx_lpc_cdk;
     262             : 
     263           0 :             if ( bfi && st->use_partial_copy && st->rf_frame_type == RF_TCXFD )
     264             :             {
     265           0 :                 tcx_lpc_cdk = tcxlpc_get_cdk( GENERIC );
     266             :             }
     267             :             else
     268             :             {
     269           0 :                 tcx_lpc_cdk = tcxlpc_get_cdk( st->coder_type );
     270             :             }
     271             : 
     272           0 :             mvr2r( st->lsf_old, &lsf[0], M );
     273           0 :             mvr2r( st->lsp_old, &lsp[0], M );
     274             : 
     275           0 :             D_lsf_tcxlpc( param_lpc, &lsf[M], lspind, st->narrowBand, tcx_lpc_cdk, st->mem_MA );
     276             : 
     277           0 :             lsf2lsp( &lsf[M], &lsp[M], M, st->sr_core );
     278             : 
     279           0 :             lsf_update_memory( st->narrowBand, &lsf[M], st->mem_MA, st->mem_MA );
     280           0 :             mvr2r( &lsf[M], st->mem_AR, M );
     281           0 :             hTcxDec->envWeighted = 1;
     282             : 
     283           0 :             E_LPC_lsp_unweight( &lsp[M], lspnew_uw, lsfnew_uw, 1.0f / st->gamma );
     284             :         }
     285             :         else
     286             :         {
     287        3750 :             if ( hTcxDec->envWeighted )
     288             :             {
     289           0 :                 mvr2r( st->lspold_uw, st->lsp_old, M );
     290           0 :                 mvr2r( st->lsfold_uw, st->lsf_old, M );
     291           0 :                 hTcxDec->envWeighted = 0;
     292             :             }
     293             : 
     294             :             /* Unquantize LPC */
     295        3750 :             if ( st->core == TCX_20_CORE )
     296             :             {
     297        1938 :                 lpc_unquantize( st, lsf, lsp, param_lpc, lspmid, lsfmid, AUDIO, &LSF_Q_prediction );
     298             :             }
     299             :             else
     300             :             {
     301        1812 :                 lpc_unquantize( st, lsf, lsp, param_lpc, lspmid, lsfmid, st->coder_type, &LSF_Q_prediction );
     302             : 
     303        1812 :                 if ( st->prev_use_partial_copy && st->last_core == ACELP_CORE && st->core == ACELP_CORE && st->prev_rf_frame_type >= RF_GENPRED && st->coder_type == UNVOICED )
     304             :                 {
     305           0 :                     if ( st->lpcQuantization && st->acelp_cfg.midLpc )
     306             :                     {
     307           0 :                         mvr2r( lspmid, &lsp[0], M );
     308           0 :                         mvr2r( &lsp[M], lspmid, M );
     309             :                     }
     310             :                 }
     311             :             }
     312             : 
     313        7500 :             for ( k = 0; k < st->numlpc; ++k )
     314             :             {
     315        3750 :                 mvr2r( &lsp[( k + 1 ) * M], &lspnew_uw[k * M], M );
     316        3750 :                 mvr2r( &lsf[( k + 1 ) * M], &lsfnew_uw[k * M], M );
     317             :             }
     318             :         }
     319             : 
     320             :         /* PLC: [LPD: LPC concealment] built the moving average for the LPC concealment */
     321        7500 :         for ( k = 0; k < st->numlpc; k++ )
     322             :         {
     323       63750 :             for ( i = 0; i < M; i++ )
     324             :             {
     325       60000 :                 st->lsf_adaptive_mean[i] = ( st->lsfoldbfi1[i] + st->lsfoldbfi0[i] + lsfnew_uw[k * M + i] ) / 3;
     326       60000 :                 st->lsfoldbfi1[i] = st->lsfoldbfi0[i];
     327       60000 :                 st->lsfoldbfi0[i] = lsfnew_uw[k * M + i];
     328             :             }
     329             :         }
     330             :     }
     331             :     else
     332             :     {
     333             :         /* PLC: [LPD: LPC concealment] Conceal the LPC from the lost frame */
     334             :         const float *lsfBase; /* base for differential lsf coding */
     335             : 
     336           0 :         if ( st->tcxonly == 0 || st->core < TCX_10_CORE )
     337             :         {
     338           0 :             st->numlpc = 1;
     339             :         }
     340             :         else
     341             :         {
     342           0 :             st->numlpc = 2;
     343             :         }
     344             : 
     345           0 :         if ( st->nbLostCmpt == 1 )
     346             :         {
     347           0 :             mvr2r( st->lsf_old, st->old_lsf_q_cng, M );
     348           0 :             mvr2r( st->lsp_old, st->old_lsp_q_cng, M );
     349             :         }
     350             : 
     351             : 
     352           0 :         lsfBase = PlcGetlsfBase( st->lpcQuantization, st->narrowBand, st->sr_core );
     353             : 
     354           0 :         dlpc_bfi( st->L_frame, lsfnew_uw, st->lsfold_uw, st->last_good, st->nbLostCmpt, st->mem_MA, st->mem_AR, &( st->stab_fac ), st->lsf_adaptive_mean, st->numlpc, st->lsf_cng, st->plcBackgroundNoiseUpdated, st->lsf_q_cng, st->old_lsf_q_cng, lsfBase );
     355             : 
     356           0 :         hTcxDec->envWeighted = 0;
     357             : 
     358           0 :         mvr2r( st->lspold_uw, lsp, M );
     359           0 :         mvr2r( st->lsfold_uw, lsf, M );
     360             : 
     361           0 :         for ( k = 0; k < st->numlpc; k++ )
     362             :         {
     363           0 :             mvr2r( &lsfnew_uw[k * M], &lsf[( k + 1 ) * M], M );
     364             : 
     365           0 :             lsf2lsp( &lsf[( k + 1 ) * M], &lsp[( k + 1 ) * M], M, st->sr_core );
     366           0 :             lsf2lsp( st->lsf_q_cng, st->lsp_q_cng, M, st->sr_core );
     367             : 
     368           0 :             mvr2r( &lsp[( k + 1 ) * M], &lspnew_uw[k * M], M );
     369             :         }
     370             :     }
     371             : 
     372             :     /*--------------------------------------------------------------*
     373             :      * Rate switching
     374             :      *---------------------------------------------------------------*/
     375             : 
     376        3750 :     if ( st->rate_switching_reset )
     377             :     {
     378          63 :         mvr2r( &( lsf[M] ), &( lsf[0] ), M );
     379          63 :         mvr2r( &( lsp[M] ), &( lsp[0] ), M );
     380          63 :         mvr2r( &( lsf[M] ), st->lsf_old, M );
     381          63 :         mvr2r( &( lsp[M] ), st->lsp_old, M );
     382          63 :         mvr2r( &( lsf[M] ), lsfmid, M );
     383          63 :         mvr2r( &( lsp[M] ), lspmid, M );
     384          63 :         lsp2a_stab( st->lsp_old, st->old_Aq_12_8, M );
     385             :     }
     386             : 
     387        3750 :     if ( st->enablePlcWaveadjust )
     388             :     {
     389           0 :         if ( st->core == ACELP_CORE )
     390             :         {
     391           0 :             st->tonality_flag = 0;
     392             :         }
     393             : 
     394           0 :         if ( bfi )
     395             :         {
     396           0 :             st->hPlcInfo->nbLostCmpt++;
     397             :         }
     398             :     }
     399             : 
     400             :     /*--------------------------------------------------------------------------------*
     401             :      * ACELP
     402             :      *--------------------------------------------------------------------------------*/
     403             : 
     404        3750 :     if ( st->prev_bfi != 0 && bfi == 0 && st->coder_type == VOICED && st->prev_nbLostCmpt > 4 )
     405             :     {
     406           0 :         st->dec_glr_idx = 1;
     407           0 :         st->reset_mem_AR = 1;
     408             :     }
     409             : 
     410        3750 :     if ( st->core == ACELP_CORE )
     411             :     {
     412        1812 :         if ( !st->tcxonly )
     413             :         {
     414             :             /* Set pointer to parameters */
     415        1812 :             prm = param;
     416             : 
     417             :             /* Stability Factor */
     418        1812 :             if ( !bfi )
     419             :             {
     420        1812 :                 st->stab_fac = lsf_stab( &lsf[M], &lsf[0], 0, st->L_frame );
     421             :             }
     422             : 
     423        1812 :             if ( !bfi && st->prev_bfi )
     424             :             {
     425             :                 /* check if LSP interpolation can be relaxed or if LPC power can be diffused*/
     426           0 :                 lsp2a_stab( &lsp[0], tmp_old, M );
     427           0 :                 enr_old = enr_1_Az( tmp_old, 2 * L_SUBFR );
     428             : 
     429           0 :                 lsp2a_stab( &lsp[M], tmp_new, M );
     430           0 :                 enr_new = enr_1_Az( tmp_new, 2 * L_SUBFR );
     431             :             }
     432             : 
     433        1812 :             if ( !bfi && ( st->dec_glr_idx == 1 || ( !( st->safety_net ) && enr_new >= 256.f && enr_new > 2 * enr_old ) ) && st->prev_bfi )
     434             :             {
     435           0 :                 RecLpcSpecPowDiffuseLc( &lsp[M], &lsp[0], &lsf[M], st, st->dec_glr_idx == 1 ? 1 : 0 );
     436           0 :                 int_lsp( L_frame, &lsp[0], &lsp[M], Aq, M, interpol_frac_12k8, 0 );
     437           0 :                 mvr2r( &lsf[M], lsfnew_uw, M );
     438             :             }
     439             :             else
     440             :             {
     441             :                 /* LPC Interpolation for ACELP */
     442        1812 :                 if ( !bfi && st->acelp_cfg.midLpc )
     443             :                 {
     444        1008 :                     st->relax_prev_lsf_interp = 0;
     445             : 
     446        1008 :                     if ( st->prev_bfi )
     447             :                     {
     448             :                         /* check if LSP interpolation can be relaxed */
     449           0 :                         if ( enr_new < ( 0.25f * enr_old ) )
     450             :                         {
     451             :                             /* don't use safety_net as this is getting impacted with lsf_restruct */
     452           0 :                             if ( st->clas_dec == UNVOICED_CLAS || st->clas_dec == SIN_ONSET || st->clas_dec == INACTIVE_CLAS || st->coder_type == GENERIC || st->coder_type == TRANSITION )
     453             :                             {
     454           0 :                                 st->relax_prev_lsf_interp = 1;
     455             :                             }
     456             :                             else
     457             :                             {
     458           0 :                                 st->relax_prev_lsf_interp = -1;
     459             :                             }
     460             :                         }
     461             :                     }
     462             : 
     463        1008 :                     if ( st->stab_fac == 0 && st->old_bfi_cnt > 0 && st->clas_dec != VOICED_CLAS && st->clas_dec != ONSET && st->relax_prev_lsf_interp == 0 )
     464             :                     {
     465           0 :                         st->relax_prev_lsf_interp = 2;
     466             :                     }
     467        1008 :                     int_lsp4( L_frame, &lsp[0], lspmid, &lsp[M], Aq, M, st->relax_prev_lsf_interp );
     468             :                 }
     469             :                 else
     470             :                 {
     471         804 :                     int_lsp( L_frame, &lsp[0], &lsp[M], Aq, M, interpol_frac_12k8, 0 );
     472         804 :                     int_lsp( L_frame, st->old_lsp_q_cng, st->lsp_q_cng, st->Aq_cng, M, interpol_frac_12k8, 0 );
     473             :                 }
     474             :             }
     475             :         }
     476             : 
     477        1812 :         if ( bfi && st->last_core != ACELP_CORE )
     478             :         {
     479             :             /* PLC: [TCX: TD PLC] */
     480           0 :             con_tcx( st, &synthFB[0], -1.f, NULL, 0, NULL );
     481           0 :             lerp( synthFB, synth, st->L_frame, st->hTcxDec->L_frameTCX );
     482           0 :             st->con_tcx = 1;
     483           0 :             set_f( &st->mem_pitch_gain[2], st->lp_gainp, st->nb_subfr );
     484             :         }
     485             :         else
     486             :         {
     487             :             /* ACELP decoder */
     488        1812 :             if ( st->hBWE_TD != NULL )
     489             :             {
     490        1812 :                 if ( st->L_frame == L_FRAME )
     491             :                 {
     492           0 :                     mvr2r( Aq + 2 * ( M + 1 ), st->hBWE_TD->cur_sub_Aq, ( M + 1 ) );
     493             :                 }
     494             :                 else
     495             :                 {
     496        1812 :                     mvr2r( Aq + 3 * ( M + 1 ), st->hBWE_TD->cur_sub_Aq, ( M + 1 ) );
     497             :                 }
     498             :             }
     499             : 
     500        1812 :             if ( bfi )
     501             :             {
     502             :                 /* PLC: [ACELP: general]
     503             :                  * PLC: Use the ACELP like concealment */
     504           0 :                 con_acelp( Aq, st->core_ext_mode, &synth[0], pitch, pit_gain, st->stab_fac, st, pitch_buf, voice_factors, ptr_bwe_exc );
     505           0 :                 mvr2r( &st->mem_pitch_gain[2], &st->mem_pitch_gain[st->nb_subfr + 2], st->nb_subfr );
     506           0 :                 set_zero( &st->mem_pitch_gain[2], st->nb_subfr );
     507             :             }
     508             :             else
     509             :             {
     510        1812 :                 decoder_acelp( st, prm, Aq, st->acelp_cfg, &synth[0], pitch, pit_gain, st->stab_fac, pitch_buf, voice_factors, LSF_Q_prediction, ptr_bwe_exc );
     511             : 
     512        1812 :                 if ( st->flagGuidedAcelp > 0 )
     513             :                 {
     514         915 :                     st->guidedT0 = max( min( st->T0_4th + st->guidedT0, NBPSF_PIT_MAX ), PIT_MIN_16k );
     515             :                 }
     516             : 
     517       10872 :                 for ( i = 0; i < st->nb_subfr; i++ )
     518             :                 {
     519        9060 :                     st->mem_pitch_gain[2 + ( 2 * st->nb_subfr - 1 ) - i] = st->mem_pitch_gain[2 + ( st->nb_subfr - 1 ) - i];
     520        9060 :                     st->mem_pitch_gain[2 + ( st->nb_subfr - 1 ) - i] = pit_gain[i];
     521             :                 }
     522             :             }
     523             :         }
     524             : 
     525             :         /* LPC for ACELP/BWE */
     526        1812 :         if ( st->narrowBand || st->sr_core == INT_FS_12k8 || st->sr_core == INT_FS_16k )
     527             :         {
     528        1812 :             mvr2r( Aq, st->mem_Aq, nb_subfr * ( M + 1 ) );
     529             :         }
     530             : 
     531             :         /* PLC: [TCX: Tonal Concealment] */
     532             :         /* Signal that this frame is not TCX */
     533        1812 :         TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, 0, 0, 0, 0 );
     534             : 
     535        1812 :         if ( !bfi )
     536             :         {
     537        1812 :             st->second_last_tns_active = st->last_tns_active;
     538        1812 :             st->last_tns_active = 0;
     539        1812 :             hTcxDec->tcxltp_last_gain_unmodified = 0.0f;
     540             :         }
     541             :     }
     542             : 
     543             : 
     544             :     /*--------------------------------------------------------------------------------*
     545             :      * TCX20
     546             :      *--------------------------------------------------------------------------------*/
     547             : 
     548        3750 :     if ( st->core == TCX_20_CORE )
     549             :     {
     550             :         /* Set pointer to parameters */
     551        1938 :         prm = param;
     552             : 
     553             :         /* Stability Factor */
     554        1938 :         if ( !bfi )
     555             :         {
     556        1938 :             st->stab_fac = lsf_stab( &lsf[M], &lsf[0], 0, st->L_frame );
     557             :         }
     558             : 
     559        1938 :         if ( hTcxDec->enableTcxLpc )
     560             :         {
     561             :             /* Convert quantized lsp to A */
     562           0 :             lsp2a_stab( &lsp[M], Aq, M );
     563             :         }
     564             :         else
     565             :         {
     566        1938 :             if ( !st->tcxonly )
     567             :             {
     568        1938 :                 if ( !bfi && st->prev_bfi && !( st->safety_net ) && st->rate_switching_reset )
     569             :                 {
     570             :                     /* diffuse LPC power on rate switching*/
     571           0 :                     RecLpcSpecPowDiffuseLc( &lsp[M], &lsp[0], &lsf[M], st, 0 );
     572           0 :                     int_lsp( L_frame, &lsp[0], &lsp[M], Aq, M, interpol_frac_12k8, 0 );
     573           0 :                     mvr2r( &lsf[M], lsfnew_uw, M );
     574             :                 }
     575             :                 else
     576             :                 {
     577             :                     /* LPC Interpolation for TCX */
     578        1938 :                     E_LPC_int_lpc_tcx( &lsp[0], &lsp[M], Aq );
     579             :                 }
     580             :             }
     581             :             else
     582             :             {
     583           0 :                 lsp2a_stab( &lsp[M], Aq, M );
     584             :             }
     585             :         }
     586             : 
     587        1938 :         if ( !bfi && hTcxDec->tcx_lpc_shaped_ari )
     588             :         {
     589           0 :             basop_E_LPC_f_lsp_a_conversion( lspind, Aind, M );
     590             :         }
     591             : 
     592             :         /* TCX decoder */
     593        1938 :         decoder_tcx( st, prm, Aq, Aind, &synth[0], &synthFB[0], bfi, 0, 0 );
     594             :     }
     595             : 
     596             :     /*--------------------------------------------------------------------------------*
     597             :      * TCX10
     598             :      *--------------------------------------------------------------------------------*/
     599             : 
     600        3750 :     if ( st->core == TCX_10_CORE )
     601             :     {
     602           0 :         prm = NULL; /* just to avoid MSVC warnings */
     603             : 
     604           0 :         for ( k = 0; k < 2; k++ )
     605             :         {
     606           0 :             if ( !bfi )
     607             :             {
     608             :                 /* Set pointer to parameters */
     609           0 :                 prm = param + ( k * DEC_NPRM_DIV );
     610             : 
     611             :                 /* Stability Factor */
     612           0 :                 st->stab_fac = lsf_stab( &lsf[( k + 1 ) * M], &lsf[k * M], 0, st->L_frame );
     613             :             }
     614             : 
     615           0 :             lsp2a_stab( &lsp[( k + 1 ) * M], Aq, M );
     616           0 :             IGFDecRestoreTCX10SubFrameData( st->hIGFDec, k );
     617             : 
     618             :             /* TCX decoder */
     619           0 :             decoder_tcx( st, prm, Aq, Aind, &synth[k * L_frame / 2], &synthFB[k * L_frameTCX / 2], bfi, k, 0 );
     620             :         }
     621             :     }
     622             : 
     623        3750 :     if ( st->core == TCX_10_CORE || st->core == TCX_20_CORE )
     624             :     {
     625        1938 :         if ( st->enablePlcWaveadjust ||          /* bfi      */
     626        1938 :              ( st->last_total_brate >= HQ_48k && /* recovery */
     627           0 :                st->last_codec_mode == MODE2 ) )
     628             :         {
     629             :             /* waveform adjustment */
     630           0 :             concealment_signal_tuning( st, bfi, synthFB, past_core_mode );
     631             : 
     632           0 :             if ( ( bfi || st->prev_bfi ) && st->hPlcInfo->Pitch && st->hPlcInfo->concealment_method == TCX_NONTONAL )
     633             :             {
     634           0 :                 lerp( synthFB, synth, L_frame, L_frameTCX );
     635             : 
     636           0 :                 if ( !bfi && st->prev_bfi )
     637             :                 {
     638           0 :                     st->hPlcInfo->Pitch = 0;
     639             :                 }
     640             :             }
     641             :         }
     642             : 
     643        1938 :         if ( !bfi )
     644             :         {
     645        1938 :             TonalMDCTConceal_SaveTimeSignal( st->hTonalMDCTConc, synthFB, L_frameTCX );
     646             :         }
     647             : 
     648        1938 :         decoder_tcx_post( st, synth, synthFB, Aq, bfi, 0 );
     649             : 
     650        1938 :         if ( st->core == TCX_20_CORE )
     651             :         {
     652             :             /* LPC Interpolation for BWE/post-processing */
     653        1938 :             if ( st->narrowBand || st->sr_core == INT_FS_12k8 || st->sr_core == INT_FS_16k )
     654             :             {
     655        1938 :                 int_lsp( L_frame, st->lspold_uw, lspnew_uw, Aq, M, interpol_frac_12k8, 0 );
     656        1938 :                 mvr2r( Aq, st->mem_Aq, nb_subfr * ( M + 1 ) );
     657             :             }
     658             :         }
     659             :     }
     660             : 
     661             : 
     662             :     /* PLC: [Common: Classification] */
     663             :     /* the classifier buffer is always updated if the sr is at
     664             :        16000 or below - the classification itself is just performed if(!st->tcxonly ) */
     665        3750 :     if ( st->sr_core <= INT_FS_16k )
     666             :     {
     667        3750 :         if ( st->core == TCX_20_CORE || st->core == TCX_10_CORE || ( st->tcxonly && st->bfi ) )
     668             :         {
     669             :             float pitch_C[4];
     670             : 
     671             :             /* note: the classifier needs the pitch only for tcx_only == 0, i.e. not for TCX10 */
     672        1938 :             pitch_C[0] = pitch_C[1] = pitch_C[2] = pitch_C[3] = (float) floor( st->old_fpitch + 0.5f );
     673             : 
     674        1938 :             FEC_clas_estim( synth, pitch_C, st->L_frame, st->tcxonly ? GENERIC : st->core_ext_mode, st->codec_mode, st->mem_syn_clas_estim, &st->clas_dec, &st->lp_ener_bfi, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, hTcxLtpDec->tcxltp ? hTcxDec->tcxltp_last_gain_unmodified : -1.0f, st->narrowBand, CLASSIFIER_TCX, bfi, st->preemph_fac, st->tcxonly, st->last_core_brate, -1 );
     675             :         }
     676             :     }
     677             : 
     678             :     /*--------------------------------------------------------------------------------*
     679             :      * Updates
     680             :      *--------------------------------------------------------------------------------*/
     681             : 
     682        3750 :     if ( bfi && st->last_core != ACELP_CORE )
     683             :     {
     684             :         /* Update FEC_scale_syn parameters */
     685           0 :         if ( hTcxLtpDec->tcxltp_gain == 0 )
     686             :         {
     687           0 :             fer_energy( L_frame, UNVOICED, synth, (float) ( L_frame / 2 ), &st->enr_old, L_frame );
     688             :         }
     689             :         else
     690             :         {
     691           0 :             fer_energy( L_frame, st->clas_dec, synth, st->old_fpitch, &st->enr_old, L_frame );
     692             :         }
     693             :     }
     694             : 
     695        3750 :     if ( !bfi && st->clas_dec >= VOICED_TRANSITION && st->clas_dec < INACTIVE_CLAS )
     696             :     {
     697        2907 :         if ( st->core == ACELP_CORE )
     698             :         {
     699        1458 :             offset = ( st->nb_subfr - 1 ) * ( M + 1 );
     700             :         }
     701             :         else
     702             :         {
     703             :             /* TCX */
     704        1449 :             offset = 0;
     705             :         }
     706             : 
     707             :         /* use latest LPC set */
     708        2907 :         st->old_enr_LP = enr_1_Az( Aq + offset, L_SUBFR );
     709             :     }
     710             : 
     711             : 
     712             :     /* Update */
     713        3750 :     mvr2r( synth_buf + L_frame, st->hTcxDec->old_synth, st->hTcxDec->old_synth_len );
     714        3750 :     mvr2r( st->hTcxDec->old_synthFB + L_frameTCX - NS2SA( st->output_Fs, PH_ECU_MEM_NS ), st->hTcxDec->synth_history, NS2SA( st->output_Fs, PH_ECU_MEM_NS ) );
     715        3750 :     mvr2r( synth_bufFB + L_frameTCX, st->hTcxDec->old_synthFB, st->hTcxDec->old_synth_lenFB );
     716        3750 :     mvr2r( st->hHQ_core->old_out + NS2SA( st->output_Fs, N_ZERO_MDCT_NS ), st->hTcxDec->old_synthFB + st->hTcxDec->old_synth_lenFB, NS2SA( st->output_Fs, PH_ECU_LOOKAHEAD_NS ) );
     717             : 
     718        3750 :     mvr2r( &lspnew_uw[( st->numlpc - 1 ) * M], st->lspold_uw, M );
     719        3750 :     mvr2r( &lsfnew_uw[( st->numlpc - 1 ) * M], st->lsfold_uw, M );
     720             : 
     721        3750 :     if ( bfi == 1 )
     722             :     {
     723           0 :         mvr2r( st->lspold_uw, st->lsp_old, M ); /* for recovery */
     724           0 :         mvr2r( st->lsfold_uw, st->lsf_old, M ); /* for recovery */
     725             :     }
     726             :     else
     727             :     {
     728        3750 :         mvr2r( &lsp[st->numlpc * M], st->lsp_old, M );
     729        3750 :         mvr2r( &lsf[st->numlpc * M], st->lsf_old, M );
     730             :     }
     731        3750 :     mvr2r( st->lsp_q_cng, st->old_lsp_q_cng, M );
     732        3750 :     mvr2r( st->lsf_q_cng, st->old_lsf_q_cng, M );
     733             : 
     734             :     /* Update MODE1 CNG parameters */
     735        3750 :     if ( !st->tcxonly && st->hTdCngDec != NULL )
     736             :     {
     737             :         /* update CNG parameters in active frames */
     738        3750 :         if ( st->bwidth == NB && hTcxDec->enableTcxLpc && st->core != ACELP_CORE )
     739           0 :         {
     740             :             float buf[L_LP], res[L_FRAME], A[M + 1], r[M + 1], tmp, lsptmp[M];
     741           0 :             assert( st->L_frame == L_FRAME );
     742           0 :             mvr2r( synth + L_FRAME - L_LP, buf, L_LP );
     743           0 :             tmp = synth[L_FRAME - L_LP - 1];
     744           0 :             preemph( buf, st->preemph_fac, L_LP, &tmp );
     745           0 :             autocorr( buf, r, M, L_LP, LP_assym_window, 0, 0, 0 );
     746           0 :             lag_wind( r, M, INT_FS_12k8, LAGW_WEAK );
     747           0 :             lev_dur( A, r, M, NULL );
     748           0 :             a2lsp_stab( A, lsptmp, &lspnew_uw[0] );
     749           0 :             residu( A, M, buf + L_LP - L_FRAME, res, L_FRAME );
     750             : 
     751           0 :             if ( st->hTdCngDec != NULL )
     752             :             {
     753           0 :                 cng_params_upd( lsptmp, res, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ, DEC, st->hTdCngDec->ho_env_circ, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth );
     754             :             }
     755             :         }
     756        3750 :         else if ( st->hTdCngDec != NULL )
     757             :         {
     758        3750 :             cng_params_upd( &lsp[M], st->old_exc + L_EXC_MEM_DEC - st->L_frame, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ, DEC, st->hTdCngDec->ho_env_circ, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth );
     759             :         }
     760             : 
     761             :         /* Set 16k LSP flag for CNG buffer */
     762        3750 :         st->hTdCngDec->ho_16k_lsp[st->hTdCngDec->ho_circ_ptr] = ( st->L_frame == L_FRAME ? 0 : 1 );
     763             :     }
     764             : 
     765        3750 :     st->last_is_cng = 0;
     766             : 
     767             :     /* Postfiltering */
     768        3750 :     post_decoder( st, synth_buf, pit_gain, pitch, signal_out, bpf_noise_buf );
     769             : 
     770        3750 :     if ( signal_outFB )
     771             :     {
     772        3750 :         mvr2r( synthFB, signal_outFB, L_frameTCX );
     773             :     }
     774             : 
     775        3750 :     if ( st->enablePlcWaveadjust )
     776             :     {
     777           0 :         if ( !bfi )
     778             :         {
     779           0 :             st->hPlcInfo->nbLostCmpt = 0;
     780             :         }
     781             : 
     782           0 :         if ( st->core == ACELP_CORE ) /* may happen only if bfi==1 */
     783             :         {
     784           0 :             set_state( st->hPlcInfo->Transient, st->core, MAX_POST_LEN );
     785             :         }
     786             :     }
     787             : 
     788             : 
     789        3750 :     return;
     790             : }

Generated by: LCOV version 1.14