LCOV - code coverage report
Current view: top level - lib_enc - acelp_core_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 197 216 91.2 %
Date: 2025-05-23 08:37:30 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #include "string.h"
      42             : #endif
      43             : #include <math.h>
      44             : #include "cnst.h"
      45             : #include "rom_enc.h"
      46             : #include "rom_com.h"
      47             : #include "prot.h"
      48             : #include "ivas_cnst.h"
      49             : #include "ivas_prot.h"
      50             : #include "ivas_rom_com.h"
      51             : #include "wmc_auto.h"
      52             : 
      53             : /*-------------------------------------------------------------------*
      54             :  * acelp_core_enc()
      55             :  *
      56             :  * ACELP core encoder
      57             :  *--------------------------------------------------------------------*/
      58             : 
      59      182097 : ivas_error acelp_core_enc(
      60             :     Encoder_State *st,                   /* i/o: encoder state structure             */
      61             :     const float inp[],                   /* i  : input signal of the current frame   */
      62             :     const float ener,                    /* i  : residual energy from Levinson-Durbin*/
      63             :     float A[NB_SUBFR16k * ( M + 1 )],    /* i  : A(z) unquantized for the 4 subframes*/
      64             :     float Aw[NB_SUBFR16k * ( M + 1 )],   /* i  : weighted A(z) unquant. for subframes*/
      65             :     const float epsP[M + 1],             /* i  : LP prediction errors                */
      66             :     float lsp_new[M],                    /* i  : LSPs at the end of the frame        */
      67             :     float lsp_mid[M],                    /* i  : LSPs in the middle of the frame     */
      68             :     const int16_t vad_hover_flag,        /* i  : VAD hangover flag                   */
      69             :     const int16_t attack_flag,           /* i  : attack flag (GSC or TC)             */
      70             :     float bwe_exc_extended[],            /* i/o: bandwidth extended excitation       */
      71             :     float *voice_factors,                /* o  : voicing factors                     */
      72             :     float old_syn_12k8_16k[],            /* o  : intermediate ACELP synthesis at 12.8kHz or 16kHz to be used by SWB BWE */
      73             :     float pitch_buf[NB_SUBFR16k],        /* o  : floating pitch for each subframe    */
      74             :     int16_t *unbits,                     /* o  : number of unused bits               */
      75             :     STEREO_TD_ENC_DATA_HANDLE hStereoTD, /* i/o: TD stereo encoder handle            */
      76             :     const float tdm_lsfQ_PCh[M]          /* i  : Q LSFs for primary channel          */
      77             : )
      78             : {
      79             :     int16_t i, nBits;                                                        /* reserved bits                        */
      80             :     LPD_state_HANDLE hLPDmem;                                                /* i/o: acelp memories                  */
      81             :     float old_exc[L_EXC], *exc;                                              /* excitation signal buffer             */
      82             :     float lsf_new[M];                                                        /* ISFs at the end of the frame         */
      83             :     float Aq[NB_SUBFR16k * ( M + 1 )];                                       /* A(z)   quantized for the 4 subframes */
      84             :     float syn[L_FRAME16k];                                                   /* synthesis signal buffer              */
      85             :     float res[L_FRAME16k];                                                   /* Residual signal for FER protection   */
      86             :     float exc2[L_FRAME16k];                                                  /* enhanced excitation                  */
      87             :     float Es_pred;                                                           /* predicited scaled innovation energy  */
      88             :     float tmp_noise;                                                         /* NB post-filter long-term noise energy*/
      89             :     int16_t tc_subfr;                                                        /* TC sub-frame indication              */
      90             :     float old_bwe_exc[( PIT16k_MAX + ( L_FRAME16k + 1 ) + L_SUBFR16k ) * 2]; /* excitation buffer         */
      91             :     float *bwe_exc;                                                          /* excitation for SWB TBE               */
      92             :     int16_t allow_cn_step;
      93             :     int32_t int_fs;
      94             :     float att;
      95             :     int16_t nb_bits; /* parameters handling */
      96             : 
      97             :     /* SC-VBR - back-up memories for LSF quantizer and synthesis filter */
      98             :     int16_t pstreaklen;
      99             :     float mem_MA[M], mem_AR[M], Bin_E[L_FFT], Bin_E_old[L_FFT / 2], lsp_new_bck[M], lsp_mid_bck[M], mem_syn_bck[M];
     100             :     float clip_var, mem_w0_bck, streaklimit;
     101             : 
     102             :     float q_env[NUM_ENV_CNG];
     103      182097 :     int16_t sid_bw = -1;
     104             :     float exc3[L_FRAME16k];
     105             :     float syn1[L_FRAME16k];
     106             : 
     107             :     float tilt_code_bck;
     108             :     float gc_threshold_bck;
     109             :     float clip_var_bck[6];
     110             :     int16_t next_force_sf_bck;
     111             :     int16_t uc_two_stage_flag;
     112             :     int16_t position;
     113             :     float tmpF;
     114             :     int16_t ppp_mode, nelp_mode;
     115             :     int16_t tdm_lp_reuse_flag, tdm_low_rate_mode, tdm_Pitch_reuse_flag;
     116             :     float *tdm_Pri_pitch_buf;
     117             : 
     118             :     /* bitstream */
     119      182097 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     120             : 
     121             :     ivas_error error;
     122             : 
     123      182097 :     error = IVAS_ERR_OK;
     124             : 
     125      182097 :     if ( st->element_mode == IVAS_CPE_MDCT && st->core_brate <= SID_2k40 )
     126             :     {
     127             :         /* Core was ACELP because of DTX in MDCT-Stereo, but SID encoding for that is done in separate function */
     128        8872 :         return error;
     129             :     }
     130             : 
     131      173225 :     push_wmops( "acelp_core_enc" );
     132             :     /*------------------------------------------------------------------*
     133             :      * Initialization
     134             :      *------------------------------------------------------------------*/
     135             : 
     136      173225 :     hLPDmem = st->hLPDmem;
     137             : 
     138      173225 :     Es_pred = 0;
     139             : 
     140      173225 :     exc = old_exc + L_EXC_MEM; /* pointer to excitation signal in the current frame */
     141      173225 :     mvr2r( hLPDmem->old_exc, old_exc, L_EXC_MEM );
     142             : 
     143      173225 :     if ( st->hBWE_TD != NULL )
     144             :     {
     145      173099 :         bwe_exc = old_bwe_exc + PIT16k_MAX * 2; /* pointer to BWE excitation signal in the current frame */
     146      173099 :         mvr2r( st->hBWE_TD->old_bwe_exc, old_bwe_exc, PIT16k_MAX * 2 );
     147             :     }
     148             :     else
     149             :     {
     150         126 :         bwe_exc = NULL;
     151             :     }
     152             : 
     153      173225 :     st->bpf_off = 0;
     154      173225 :     if ( st->last_core == HQ_CORE || st->last_codec_mode == MODE2 || st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE )
     155             :     {
     156             :         /* in case of HQ->ACELP switching, do not apply BPF */
     157        7298 :         st->bpf_off = 1;
     158             :         /* reset the GSC pre echo energy threshold in case of switching */
     159        7298 :         if ( st->hGSCEnc != NULL )
     160             :         {
     161        7298 :             st->hGSCEnc->Last_frame_ener = (float) MAX_32;
     162             :         }
     163             :     }
     164             : 
     165             :     /* force safety-net LSFQ in the first frames after CNG segment */
     166      173225 :     if ( st->last_core_brate <= SID_2k40 )
     167             :     {
     168       15850 :         st->Nb_ACELP_frames = 0;
     169             :     }
     170      173225 :     if ( st->Nb_ACELP_frames < MAX16B )
     171             :     {
     172      173225 :         st->Nb_ACELP_frames++;
     173             :     }
     174             : 
     175      173225 :     if ( st->L_frame == L_FRAME )
     176             :     {
     177       91279 :         int_fs = INT_FS_12k8;
     178             :     }
     179             :     else
     180             :     {
     181       81946 :         int_fs = INT_FS_16k;
     182             :     }
     183             : 
     184      173225 :     tmp_noise = 0;
     185      173225 :     tc_subfr = -1;
     186      173225 :     position = -1;
     187             : 
     188             :     /* SC-VBR temporary variables */
     189      173225 :     pstreaklen = 0;
     190      173225 :     clip_var = 0;
     191      173225 :     mem_w0_bck = 0;
     192      173225 :     streaklimit = 0;
     193             : 
     194             :     /* channel-aware mode */
     195      173225 :     reset_rf_indices( st->hRF, st->L_frame, &st->rf_target_bits_write );
     196             : 
     197             :     /* VBR modes */
     198      173225 :     if ( st->Opt_SC_VBR )
     199             :     {
     200           0 :         ppp_mode = st->hSC_VBR->ppp_mode;
     201           0 :         nelp_mode = st->hSC_VBR->nelp_mode;
     202             :     }
     203             :     else
     204             :     {
     205      173225 :         ppp_mode = 0;
     206      173225 :         nelp_mode = 0;
     207             :     }
     208             : 
     209             :     /* TD stereo */
     210      173225 :     if ( st->element_mode == IVAS_CPE_TD && st->idchan == 1 )
     211             :     {
     212        3791 :         tdm_lp_reuse_flag = hStereoTD->tdm_lp_reuse_flag;
     213        3791 :         tdm_low_rate_mode = hStereoTD->tdm_low_rate_mode;
     214        3791 :         tdm_Pitch_reuse_flag = hStereoTD->tdm_Pitch_reuse_flag;
     215        3791 :         tdm_Pri_pitch_buf = hStereoTD->tdm_Pri_pitch_buf;
     216             :     }
     217             :     else
     218             :     {
     219      169434 :         tdm_lp_reuse_flag = 0;
     220      169434 :         tdm_low_rate_mode = 0;
     221      169434 :         if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
     222             :         {
     223        7534 :             tdm_low_rate_mode = 1;
     224             :         }
     225      169434 :         tdm_Pitch_reuse_flag = 0;
     226      169434 :         tdm_Pri_pitch_buf = NULL;
     227             :     }
     228             : 
     229             :     /*-----------------------------------------------------------------*
     230             :      * ACELP@12k8 / ACELP@16k switching
     231             :      *-----------------------------------------------------------------*/
     232             : 
     233      173225 :     if ( st->last_L_frame != st->L_frame && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE ) )
     234             :     {
     235             :         /* in case of switching, do not apply BPF (flag employed also in updt_enc()) */
     236         760 :         st->bpf_off = 1;
     237             : 
     238             :         /* force safety-net LSFQ in the first frames after ACELP@12k8/ACELP@16k switching */
     239         760 :         st->Nb_ACELP_frames = 1;
     240             : 
     241             :         /* convert old quantized LSP vector */
     242         760 :         if ( st->L_frame == L_FRAME )
     243             :         {
     244         302 :             st->rate_switching_reset = lsp_convert_poly( st->lsp_old, st->L_frame, 0 );
     245             :         }
     246             :         else
     247             :         {
     248         458 :             st->rate_switching_reset = st->rate_switching_reset_16kHz;
     249         458 :             mvr2r( st->lsp_old16k, st->lsp_old, M );
     250             :         }
     251             : 
     252             :         /* convert old quantized LSF vector */
     253         760 :         lsp2lsf( st->lsp_old, st->lsf_old, M, int_fs );
     254             : 
     255             :         /* interpolation of unquantized ISPs */
     256         760 :         if ( st->rate_switching_reset )
     257             :         {
     258             :             /*extrapolation in case of unstable LSP*/
     259           0 :             int_lsp4( st->L_frame, lsp_mid, lsp_mid, lsp_new, A, M, 0 );
     260             :         }
     261             :         else
     262             :         {
     263         760 :             int_lsp4( st->L_frame, st->lsp_old, lsp_mid, lsp_new, A, M, 0 );
     264             :         }
     265             : 
     266             :         /* Reset LPC mem */
     267         760 :         mvr2r( GEWB_Ave, st->mem_AR, M );
     268         760 :         set_zero( st->mem_MA, M );
     269             : 
     270             :         /* update synthesis filter memories */
     271         760 :         synth_mem_updt2( st->L_frame, st->last_L_frame, hLPDmem->old_exc, hLPDmem->mem_syn_r, hLPDmem->mem_syn2, hLPDmem->mem_syn, ENC );
     272         760 :         mvr2r( hLPDmem->old_exc, old_exc, L_EXC_MEM );
     273         760 :         mvr2r( hLPDmem->mem_syn2, hLPDmem->mem_syn1, M );
     274         760 :         mvr2r( hLPDmem->mem_syn2, hLPDmem->mem_syn3, M );
     275             : 
     276             :         /* update Aw[] coefficients */
     277         760 :         weight_a_subfr( st->L_frame / L_SUBFR, A, Aw, st->gamma, M );
     278             :     }
     279             : 
     280      173225 :     if ( st->last_bwidth == NB && st->bwidth != NB && st->ini_frame != 0 )
     281             :     {
     282           0 :         st->rate_switching_reset = 1;
     283             :     }
     284             : 
     285             :     /*----------------------------------------------------------------*
     286             :      * Encoding of CNG frames
     287             :      *----------------------------------------------------------------*/
     288             : 
     289      173225 :     if ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA )
     290             :     {
     291       15427 :         if ( st->cng_type == LP_CNG )
     292             :         {
     293             :             /* Run CNG post parameter update */
     294        2395 :             cng_params_postupd( st->hTdCngEnc->ho_circ_ptr, &st->hTdCngEnc->cng_buf_cnt, st->hTdCngEnc->cng_exc2_buf, st->hTdCngEnc->cng_brate_buf, st->hTdCngEnc->ho_env_circ, st->element_mode, st->hFdCngEnc->hFdCngCom->CngBandwidth );
     295             : 
     296             :             /* encode CNG parameters */
     297        2395 :             CNG_enc( st, Aq, inp, ener, lsp_mid, lsp_new, lsf_new, &allow_cn_step, q_env, &sid_bw );
     298             : 
     299             :             /* comfort noise generation */
     300        2395 :             CNG_exc( st->core_brate, st->L_frame, &st->hTdCngEnc->Enew, &st->hTdCngEnc->cng_seed, exc, exc2, &st->hTdCngEnc->lp_ener, st->last_core_brate, &st->hDtxEnc->first_CNG, &st->hTdCngEnc->cng_ener_seed, bwe_exc, allow_cn_step, &st->hTdCngEnc->last_allow_cn_step, st->hTdCngEnc->num_ho, q_env, st->hTdCngEnc->lp_env, st->hTdCngEnc->old_env, st->hTdCngEnc->exc_mem, st->hTdCngEnc->exc_mem1, &sid_bw, &st->hTdCngEnc->cng_ener_seed1, exc3, st->Opt_AMR_WB, EVS_MONO );
     301             :         }
     302             :         else
     303             :         {
     304       13032 :             if ( st->core_brate == SID_2k40 && st->element_mode != IVAS_CPE_MDCT )
     305             :             {
     306        2049 :                 FdCng_encodeSID( st );
     307        2049 :                 st->hDtxEnc->last_CNG_L_frame = st->L_frame;
     308             :             }
     309             : 
     310       13032 :             generate_comfort_noise_enc( st );
     311             : 
     312       13032 :             FdCng_exc( st->hFdCngEnc->hFdCngCom, &st->hDtxEnc->CNG_mode, st->L_frame, st->lsp_old, st->hDtxEnc->first_CNG, st->hDtxEnc->lspCNG, Aq, lsp_new, lsf_new, exc, exc2, bwe_exc );
     313       13032 :             mvr2r( exc2, exc3, st->L_frame );
     314             : 
     315       13032 :             if ( st->core_brate == SID_2k40 )
     316             :             {
     317        2049 :                 if ( st->hTdCngEnc != NULL )
     318             :                 {
     319        1494 :                     tmpF = cng_energy( st->element_mode, st->bwidth, st->hDtxEnc->CNG_mode, st->hTdCngEnc->CNG_att, exc, st->L_frame );
     320        1494 :                     i = (int16_t) ( ( tmpF + 2.0f ) * STEP_SID );
     321        1494 :                     i = min( max( i, 0 ), 127 );
     322        1494 :                     st->hTdCngEnc->old_enr_index = i;
     323             :                 }
     324             :             }
     325             :         }
     326             : 
     327             : #ifdef DEBUG_MODE_ACELP
     328             :         dbgwrite( exc, sizeof( float ), st->L_frame, 1, "res/exc.enc" );
     329             : #endif
     330             :         /* Reset HO counter in the first SID frame */
     331       15427 :         if ( st->hTdCngEnc != NULL )
     332             :         {
     333       13274 :             st->hTdCngEnc->burst_ho_cnt = 0;
     334             :         }
     335             : 
     336             :         /* synthesis at 12.8kHz sampling rate */
     337       15427 :         syn_12k8( st->L_frame, Aq, exc3, syn1, hLPDmem->mem_syn3, 1 );
     338             : 
     339             :         /* reset the encoder */
     340       15427 :         CNG_reset_enc( st, pitch_buf, voice_factors, 0 );
     341             : 
     342             :         /* update st->mem_syn1 for ACELP core switching */
     343       15427 :         mvr2r( hLPDmem->mem_syn3, hLPDmem->mem_syn1, M );
     344             : 
     345             :         /* update ACELP core synthesis filter memory */
     346       15427 :         mvr2r( hLPDmem->mem_syn3, hLPDmem->mem_syn, M );
     347             : 
     348             :         /* update old synthesis buffer - needed for ACELP internal sampling rate switching */
     349       15427 :         mvr2r( syn1 + st->L_frame - L_SYN_MEM, hLPDmem->mem_syn_r, L_SYN_MEM );
     350             : 
     351             :         /* save and delay synthesis to be used by SWB BWE */
     352       15427 :         if ( st->hBWE_FD != NULL )
     353             :         {
     354       15427 :             save_old_syn( st->L_frame, syn1, old_syn_12k8_16k, st->hBWE_FD->old_syn_12k8_16k, st->preemph_fac, &st->hBWE_FD->mem_deemph_old_syn );
     355             :         }
     356             : 
     357             :         /*Update MODE2 core switching memory*/
     358       15427 :         deemph( syn1, st->preemph_fac, st->L_frame, &( hLPDmem->syn[M] ) );
     359       15427 :         mvr2r( syn1 + st->L_frame - M - 1, hLPDmem->syn, M + 1 );
     360             :     }
     361             : 
     362             :     /*----------------------------------------------------------------*
     363             :      * Encoding of all other frames
     364             :      *----------------------------------------------------------------*/
     365             : 
     366             :     else
     367             :     {
     368             :         /*-----------------------------------------------------------------*
     369             :          * Configure ACELP bit allocation
     370             :          *-----------------------------------------------------------------*/
     371             : 
     372      157798 :         nb_bits = 0;
     373      157798 :         st->acelp_cfg.FEC_mode = 0;
     374      157798 :         uc_two_stage_flag = 0;
     375             : 
     376      157798 :         if ( !nelp_mode && !ppp_mode )
     377             :         {
     378      157798 :             config_acelp1( ENC, st->total_brate, st->core_brate, st->core, st->extl, st->extl_brate, st->L_frame, st->GSC_noisy_speech, &( st->acelp_cfg ), hBstr->nb_bits_tot, st->coder_type, st->inactive_coder_type_flag, tc_subfr, 0, &nb_bits, unbits, st->element_mode, &uc_two_stage_flag, tdm_lp_reuse_flag, tdm_low_rate_mode, st->idchan, st->active_cnt, tdm_Pitch_reuse_flag, st->tdm_LRTD_flag, st->GSC_IVAS_mode );
     379             :         }
     380             : 
     381             :         /*-----------------------------------------------------------------*
     382             :          * After inactive period, use the most up-to-date ISPs
     383             :          *-----------------------------------------------------------------*/
     384             : 
     385      157798 :         if ( st->hDtxEnc != NULL && ( st->last_core_brate == FRAME_NO_DATA || st->last_core_brate == SID_2k40 ) )
     386             :         {
     387        1312 :             mvr2r( st->hDtxEnc->lspCNG, st->lsp_old, M );
     388        1312 :             lsp2lsf( st->hDtxEnc->lspCNG, st->lsf_old, M, int_fs );
     389             :         }
     390             : 
     391             :         /*-----------------------------------------------------------------*
     392             :          * Reset higher ACELP pre-quantizer in case of switching
     393             :          *-----------------------------------------------------------------*/
     394             : 
     395      157798 :         if ( !st->use_acelp_preq )
     396             :         {
     397      144126 :             st->mem_deemp_preQ = 0.0f;
     398      144126 :             st->mem_preemp_preQ = 0.0f;
     399      144126 :             st->last_code_preq = 0;
     400      144126 :             st->last_nq_preQ = 0;
     401             :         }
     402      157798 :         st->use_acelp_preq = 0;
     403             : 
     404             :         /*-----------------------------------------------------------------*
     405             :          * LSF Quantization
     406             :          * A[z] calculation
     407             :          *-----------------------------------------------------------------*/
     408             : 
     409             :         /* SC-VBR & channel-aware mode - back-up memories for LSF quantizer and synthesis filter */
     410      157798 :         lsf_syn_mem_backup( st, &tilt_code_bck, &gc_threshold_bck, clip_var_bck, &next_force_sf_bck, lsp_new, lsp_mid, &clip_var, mem_AR, mem_MA, lsp_new_bck, lsp_mid_bck, Bin_E, Bin_E_old, mem_syn_bck, &mem_w0_bck, &streaklimit, &pstreaklen );
     411             : 
     412      157798 :         if ( !tdm_lp_reuse_flag )
     413             :         {
     414      157656 :             lsf_enc( st, lsf_new, lsp_new, lsp_mid, Aq, tdm_low_rate_mode, st->GSC_IVAS_mode, tdm_lsfQ_PCh );
     415             :         }
     416             :         else
     417             :         {
     418             :             const float *pt_interp_2;
     419             : 
     420         142 :             if ( st->active_cnt != 1 )
     421             :             {
     422             :                 int16_t beta_index;
     423             :                 float lsf_wgts[M];
     424             : 
     425             :                 /* intra_frame prediction for the LSFs */
     426         142 :                 lsp2lsf( lsp_new, lsf_new, M, 12800 );
     427             : 
     428         142 :                 Unified_weighting( &st->Bin_E[L_FFT / 2], lsf_new, lsf_wgts, st->bwidth == NB, st->coder_type == UNVOICED, st->sr_core, M );
     429             : 
     430         142 :                 tdm_SCh_lsf_reuse( ENC, st->element_brate, lsf_new, lsp_new, tdm_lsfQ_PCh, lsf_wgts, &beta_index );
     431             : 
     432         142 :                 push_indice( hBstr, IND_IC_LSF_PRED, beta_index, TDM_IC_LSF_PRED_BITS );
     433             :             }
     434             : 
     435         142 :             pt_interp_2 = interpol_frac_12k8;
     436         142 :             if ( tdm_low_rate_mode == 1 && st->coder_type > UNVOICED )
     437             :             {
     438           0 :                 pt_interp_2 = interpol_frac2;
     439             :             }
     440             : 
     441         142 :             if ( st->active_cnt == 1 )
     442             :             {
     443           0 :                 mvr2r( lsp_new, st->lsp_old, M );
     444           0 :                 lsp2lsf( lsp_new, st->lsf_old, M, st->sr_core );
     445           0 :                 lsp2lsf( lsp_new, lsf_new, M, st->sr_core );
     446             :             }
     447             : 
     448             :             /* LSP interpolation and conversion of LSPs to A(z) */
     449         142 :             int_lsp( st->L_frame, st->lsp_old, lsp_new, Aq, M, pt_interp_2, 0 );
     450             : 
     451             :             /* Check LSF stability (distance between old LSFs and current LSFs) */
     452         142 :             st->stab_fac = lsf_stab( lsf_new, st->lsf_old, 0, st->L_frame );
     453             :         }
     454             : 
     455      157798 :         if ( st->last_core == HQ_CORE && st->element_mode > EVS_MONO )
     456             :         {
     457             :             /* Prepare ACB memory from last HQ frame */
     458         226 :             tmpF = hLPDmem->old_exc[0];
     459         226 :             preemph( hLPDmem->old_exc, st->preemph_fac, st->L_frame, &tmpF );
     460         226 :             mvr2r( hLPDmem->old_exc + st->L_frame - M, hLPDmem->mem_syn, M );
     461         226 :             residu( Aq, M, hLPDmem->old_exc, old_exc, st->L_frame );
     462             :         }
     463             : 
     464      157798 :         if ( st->last_core != ACELP_CORE && st->element_mode > EVS_MONO )
     465             :         {
     466             :             /* Prepare ACB memory of old_bwe_exc */
     467        6920 :             if ( st->L_frame == L_FRAME )
     468             :             {
     469        3697 :                 lerp( old_exc, old_bwe_exc, L_EXC_MEM_DEC * HIBND_ACB_L_FAC, L_EXC_MEM_DEC );
     470             :             }
     471             :             else
     472             :             {
     473        3223 :                 lerp( old_exc, old_bwe_exc, L_EXC_MEM_DEC * 2, L_EXC_MEM_DEC );
     474             :             }
     475             :         }
     476             : 
     477             : 
     478             :         /*---------------------------------------------------------------*
     479             :          * Calculation of LP residual (filtering through A[z] filter)
     480             :          *---------------------------------------------------------------*/
     481             : 
     482      157798 :         calc_residu( inp, res, Aq, st->L_frame );
     483             : 
     484      157798 :         calculate_hangover_attenuation_gain( st, &att, vad_hover_flag );
     485      157798 :         if ( att != 1.0f )
     486             :         {
     487        1410 :             v_multc( res, att, res, st->L_frame );
     488             :         }
     489             : 
     490             :         /*-----------------------------------------------------------------*
     491             :          * Determine TC subframe classification
     492             :          *-----------------------------------------------------------------*/
     493             : 
     494      157798 :         if ( st->coder_type == TRANSITION )
     495             :         {
     496       14366 :             tc_classif_enc( st->L_frame, &tc_subfr, &position, attack_flag, st->pitch[0], res );
     497             : 
     498       14366 :             config_acelp1( ENC, st->total_brate, st->core_brate, st->core, st->extl, st->extl_brate, st->L_frame, -1, &( st->acelp_cfg ), hBstr->nb_bits_tot, st->coder_type, st->inactive_coder_type_flag, tc_subfr, 1, NULL, unbits, st->element_mode, &uc_two_stage_flag, tdm_lp_reuse_flag, tdm_low_rate_mode, st->idchan, st->active_cnt, tdm_Pitch_reuse_flag, st->tdm_LRTD_flag, st->GSC_IVAS_mode );
     499             :         }
     500             : 
     501             :         /*---------------------------------------------------------------*
     502             :          * Calculation of prediction for scaled innovation energy
     503             :          * (for memory-less gain quantizer)
     504             :          *---------------------------------------------------------------*/
     505             : 
     506      157798 :         if ( nb_bits > 0 )
     507             :         {
     508      131648 :             Es_pred_enc( &Es_pred, &i, st->L_frame, L_SUBFR, res, st->voicing, nb_bits, uc_two_stage_flag );
     509      131648 :             push_indice( hBstr, IND_ES_PRED, i, nb_bits );
     510             :         }
     511             : 
     512             :         /*------------------------------------------------------------*
     513             :          * Encode excitation according to coding type
     514             :          *------------------------------------------------------------*/
     515             : 
     516      157798 :         if ( tdm_low_rate_mode ) /* tdm stereo low rate mode */
     517             :         {
     518        7561 :             if ( st->coder_type <= UNVOICED )
     519             :             {
     520        7561 :                 tdm_low_rate_enc( st, Aq, res, syn, exc, pitch_buf, voice_factors, bwe_exc, 0 /*attack_flag*/, lsf_new, &tmp_noise );
     521             :             }
     522             :             else /* GENERIC */
     523             :             {
     524           0 :                 encod_gen_2sbfr( st, inp, Aw, Aq, res, syn, exc, exc2, pitch_buf, voice_factors, bwe_exc, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
     525             :             }
     526             :         }
     527      150237 :         else if ( nelp_mode )
     528             :         {
     529             :             /* SC-VBR - NELP frames */
     530           0 :             encod_nelp( st, inp, Aw, Aq, res, syn, &tmp_noise, exc, exc2, pitch_buf, voice_factors, bwe_exc );
     531             :         }
     532      150237 :         else if ( st->coder_type == UNVOICED )
     533             :         {
     534             :             /* UNVOICED frames (Gauss. excitation) */
     535        2037 :             encod_unvoiced( st, inp, Aw, Aq, Es_pred, uc_two_stage_flag, res, syn, &tmp_noise, exc, pitch_buf, voice_factors, bwe_exc );
     536             :         }
     537      148200 :         else if ( st->coder_type == TRANSITION )
     538             :         {
     539       14366 :             encod_tran( st, inp, Aw, Aq, Es_pred, res, syn, exc, exc2, pitch_buf, voice_factors, bwe_exc, tc_subfr, position, unbits );
     540             :         }
     541      133834 :         else if ( ppp_mode )
     542             :         {
     543             :             /* SC-VBR - PPP frames */
     544           0 :             if ( ( error = encod_ppp( st, inp, Aw, Aq, res, syn, exc, exc2, pitch_buf, voice_factors, bwe_exc ) ) != IVAS_ERR_OK )
     545             :             {
     546           0 :                 return error;
     547             :             }
     548             : 
     549             : 
     550           0 :             if ( st->hSC_VBR->bump_up ) /* PPP failed, bump up */
     551             :             {
     552             :                 /* restore memories of LSF quantizer and synthesis filter */
     553           0 :                 lsf_syn_mem_restore( st, tilt_code_bck, gc_threshold_bck, clip_var_bck, next_force_sf_bck, lsp_new, lsp_mid, clip_var, mem_AR, mem_MA, lsp_new_bck, lsp_mid_bck, Bin_E, Bin_E_old, mem_syn_bck, mem_w0_bck, streaklimit, pstreaklen );
     554             : 
     555             :                 /* Configure ACELP bit allocation */
     556           0 :                 config_acelp1( ENC, st->total_brate, st->core_brate, st->core, st->extl, st->extl_brate, st->L_frame, -1, &( st->acelp_cfg ), hBstr->nb_bits_tot, st->coder_type, st->inactive_coder_type_flag, tc_subfr, 0, &nb_bits, unbits, 0, &uc_two_stage_flag, 0, 0, st->idchan, st->active_cnt, tdm_Pitch_reuse_flag, st->tdm_LRTD_flag, st->GSC_IVAS_mode );
     557             : 
     558             :                 /* redo LSF quantization */
     559           0 :                 lsf_enc( st, lsf_new, lsp_new, lsp_mid, Aq, tdm_low_rate_mode, 0, NULL );
     560             : 
     561             :                 /* recalculation of LP residual (filtering through A[z] filter) */
     562           0 :                 calc_residu( inp, res, Aq, st->L_frame );
     563           0 :                 st->hTdCngEnc->burst_ho_cnt = 0;
     564             : 
     565             :                 /* VOICED frames in SC-VBR */
     566           0 :                 encod_gen_voic( st, inp, Aw, Aq, Es_pred, res, syn, exc, exc2, pitch_buf, voice_factors, bwe_exc, unbits, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
     567             :             }
     568             :         }
     569      133834 :         else if ( st->coder_type == AUDIO || ( st->coder_type == INACTIVE && st->inactive_coder_type_flag ) )
     570             :         {
     571             :             /* AUDIO and INACTIVE frames (coded by GSC technology) */
     572       15245 :             encod_audio( st, inp, Aw, Aq, res, syn, exc, pitch_buf, voice_factors, bwe_exc, attack_flag, lsf_new, &tmp_noise, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
     573             :         }
     574             :         else
     575             :         {
     576             :             /* GENERIC, VOICED and INACTIVE frames (coded by AVQ technology) */
     577      118589 :             encod_gen_voic( st, inp, Aw, Aq, Es_pred, res, syn, exc, exc2, pitch_buf, voice_factors, bwe_exc, unbits, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
     578             :         }
     579             : 
     580             : #ifdef DEBUG_MODE_ACELP
     581             :         dbgwrite( exc, sizeof( float ), st->L_frame, 1, fname( debug_dir, "exc.enc", st->idchan, st->id_element, ENC ) );
     582             :         dbgwrite( res, sizeof( float ), st->L_frame, 1, fname( debug_dir, "resid", st->idchan, st->id_element, ENC ) );
     583             : #endif
     584             : 
     585             : #ifdef DEBUGGING
     586             :         /* SNR measuremenet of CELP output */
     587             :         if ( ppp_mode == 0 )
     588             :         {
     589             :             char name[50] = "CELP_output_ch ";
     590             : 
     591             :             if ( st->id_element == 0 )
     592             :             {
     593             :                 name[14] = (char) ( st->idchan + '0' );
     594             :             }
     595             :             else
     596             :             {
     597             :                 char name2[50] = "CELP_output.idX_chX  ";
     598             :                 name2[14] = (char) ( st->id_element + '0' );
     599             :                 name2[18] = (char) ( st->idchan + '0' );
     600             :                 strcpy( name, name2 );
     601             :             }
     602             : 
     603             :             if ( st->idchan == 0 )
     604             :             {
     605             :                 snr_celp( st->L_frame, L_SUBFR, st->gamma, st->preemph_fac, st->vad_flag, st->coder_type, inp, syn, A, 0, name );
     606             :             }
     607             :             else if ( st->idchan == 1 )
     608             :             {
     609             :                 snr_celp( st->L_frame, L_SUBFR, st->gamma, st->preemph_fac, st->vad_flag, st->coder_type, inp, syn, A, 1, name );
     610             :             }
     611             :         }
     612             : #endif
     613             :         /* update mem_syn1 for ACELP core switching */
     614      157798 :         mvr2r( hLPDmem->mem_syn, hLPDmem->mem_syn1, M );
     615             : 
     616             :         /* update old synthesis buffer - needed for ACELP internal sampling rate switching */
     617      157798 :         mvr2r( syn + st->L_frame - L_SYN_MEM, hLPDmem->mem_syn_r, L_SYN_MEM );
     618             : 
     619             :         /* save and delay synthesis to be used by SWB BWE */
     620      157798 :         if ( st->hBWE_FD != NULL )
     621             :         {
     622      157672 :             save_old_syn( st->L_frame, syn, old_syn_12k8_16k, st->hBWE_FD->old_syn_12k8_16k, st->preemph_fac, &st->hBWE_FD->mem_deemph_old_syn );
     623             :         }
     624             : 
     625             :         /*Update MODE2 core switching memory*/
     626      157798 :         mvr2r( syn, syn1, st->L_frame );
     627      157798 :         deemph( syn1, st->preemph_fac, st->L_frame, &( hLPDmem->syn[M] ) );
     628      157798 :         mvr2r( syn1 + st->L_frame - M - 1, hLPDmem->syn, M + 1 );
     629             : 
     630      157798 :         if ( st->element_mode > EVS_MONO && st->hTcxEnc != NULL )
     631             :         {
     632      152597 :             mvr2r( syn1 + st->L_frame / 2, st->hTcxEnc->Txnq, st->L_frame / 2 );
     633             :         }
     634             : 
     635             :         /*--------------------------------------------------------------------------------------*
     636             :          * Modify the excitation signal when the noise is stationary
     637             :          *--------------------------------------------------------------------------------------*/
     638             : 
     639      157798 :         if ( !( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) && nelp_mode != 1 && !( st->element_mode == IVAS_SCE && tdm_low_rate_mode ) )
     640             :         {
     641             :             /* exc2 buffer is needed only for updating of Aq[] which is needed for core switching */
     642      146473 :             mvr2r( exc, exc2, st->L_frame );
     643      146473 :             stat_noise_uv_enc( st, epsP, lsp_new, lsp_mid, Aq, exc2, uc_two_stage_flag );
     644             :         }
     645             : 
     646             :         /*-----------------------------------------------------------------*
     647             :          * Encode supplementary information for Frame Error Concealment
     648             :          *-----------------------------------------------------------------*/
     649             : 
     650      157798 :         FEC_encode( hBstr, st->acelp_cfg, syn, st->coder_type, st->clas, pitch_buf, res, &st->Last_pulse_pos, st->L_frame, st->total_brate );
     651             : 
     652      157798 :         if ( st->hBWE_TD != NULL )
     653             :         {
     654      157672 :             if ( st->L_frame == L_FRAME )
     655             :             {
     656       83478 :                 mvr2r( Aq + 2 * ( M + 1 ), st->hBWE_TD->cur_sub_Aq, ( M + 1 ) );
     657             :             }
     658             :             else
     659             :             {
     660       74194 :                 mvr2r( Aq + 3 * ( M + 1 ), st->hBWE_TD->cur_sub_Aq, ( M + 1 ) );
     661             :             }
     662             :         }
     663             : 
     664             : 
     665             :     } /* end of active inp coding */
     666             : 
     667             : 
     668             :     /*-----------------------------------------------------------------*
     669             :      * Write ACELP unused bits
     670             :      *-----------------------------------------------------------------*/
     671             : 
     672      173225 :     if ( st->core_brate != SID_2k40 && st->core_brate != FRAME_NO_DATA && st->core_brate != PPP_NELP_2k80 )
     673             :     {
     674      157796 :         nBits = st->acelp_cfg.ubits;
     675             : 
     676      177838 :         while ( nBits > 0 )
     677             :         {
     678       20042 :             i = min( nBits, 16 );
     679       20042 :             push_indice( hBstr, IND_UNUSED, 0, i );
     680       20042 :             nBits -= i;
     681             : #ifdef DEBUGGING
     682             :             if ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD )
     683             :             {
     684             :                 printf( "Issue with unused bits at Ln 653 in acelp_core_enc channel 1  at frame %d\n", frame );
     685             :             }
     686             : #endif
     687             :         }
     688             :     }
     689             : 
     690             :     /*-----------------------------------------------------------------*
     691             :      * Apply non linearity in case of SWB TBE
     692             :      *-----------------------------------------------------------------*/
     693             : 
     694      173225 :     if ( st->hBWE_TD != NULL )
     695             :     {
     696      173099 :         if ( ( st->last_Opt_SC_VBR == 1 && st->Opt_SC_VBR == 0 ) || ( ( st->extl == SWB_TBE || st->extl == WB_TBE || st->extl == FB_TBE ) && st->last_extl != SWB_TBE && st->last_extl != WB_TBE && st->last_extl != FB_TBE ) || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD && !st->tdm_LRTD_flag ) )
     697             :         {
     698        7145 :             st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
     699        7145 :             set_f( st->hBWE_TD->old_bwe_exc_extended, 0.0f, NL_BUFF_OFFSET );
     700             :         }
     701             : 
     702      173099 :         if ( !st->Opt_SC_VBR && ( st->idchan == 0 || st->element_mode != IVAS_CPE_TD || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD && st->tdm_LRTD_flag ) ) )
     703             :         {
     704             :             /* Apply a non linearity to the SHB excitation */
     705      173099 :             non_linearity( bwe_exc, bwe_exc_extended, st->hBWE_TD->old_bwe_exc_extended, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale, st->coder_type, voice_factors, st->L_frame );
     706             :         }
     707             : 
     708      173099 :         if ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA )
     709             :         {
     710       15427 :             st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
     711             :         }
     712             :     }
     713             : 
     714             :     /*-----------------------------------------------------------------*
     715             :      * Updates
     716             :      *-----------------------------------------------------------------*/
     717             : 
     718      173225 :     updt_enc( st, old_exc, pitch_buf, Es_pred, Aq, lsf_new, lsp_new, old_bwe_exc );
     719             : 
     720      173225 :     if ( st->hTdCngEnc != NULL && st->Opt_DTX_ON && st->core_brate > SID_2k40 )
     721             :     {
     722             :         /* update CNG parameters in active frames */
     723       21910 :         cng_params_upd( lsp_new, exc, st->L_frame, &st->hTdCngEnc->ho_circ_ptr, st->hTdCngEnc->ho_ener_circ, &st->hTdCngEnc->ho_circ_size, st->hTdCngEnc->ho_lsp_circ, ENC, st->hTdCngEnc->ho_env_circ, &st->hTdCngEnc->cng_buf_cnt, st->hTdCngEnc->cng_exc2_buf, st->hTdCngEnc->cng_brate_buf, st->hDtxEnc->last_active_brate, st->element_mode, st->hFdCngEnc->hFdCngCom->CngBandwidth );
     724             : 
     725       21910 :         if ( st->L_frame == L_FRAME )
     726             :         {
     727             :             /* store LSPs@16k, potentially to be used in CNG@16k */
     728       10010 :             mvr2r( st->lsp_old16k, &( st->hTdCngEnc->ho_lsp_circ2[( st->hTdCngEnc->ho_circ_ptr ) * M] ), M );
     729             :         }
     730             : 
     731             :         /* set LSP@16k flag for the first buffer */
     732       21910 :         st->hTdCngEnc->ho_16k_lsp[st->hTdCngEnc->ho_circ_ptr] = ( st->L_frame == L_FRAME ? 0 : 1 );
     733             : 
     734             :         /* efficient DTX hangover control */
     735       21910 :         if ( st->hTdCngEnc->burst_ho_cnt > 1 )
     736             :         {
     737         867 :             dtx_hangover_control( st, lsp_new );
     738             :         }
     739             :     }
     740             : 
     741             :     /* SC-VBR update of average data rate */
     742      173225 :     if ( st->vad_flag == 1 )
     743             :     {
     744             :         /* reset in case of bitrate switching in EVS */
     745      142962 :         if ( st->hSC_VBR != NULL )
     746             :         {
     747        1403 :             update_average_rate( st->hSC_VBR, st->core_brate );
     748             :         }
     749             :     }
     750             : 
     751      173225 :     pop_wmops();
     752             : 
     753      173225 :     return error;
     754             : }

Generated by: LCOV version 1.14