LCOV - code coverage report
Current view: top level - lib_enc - ivas_core_pre_proc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- conformance test test_26252.py @ efe53129c9ed87a5067dd0a8fb9dca41db9c4add Lines: 266 267 99.6 %
Date: 2026-02-12 06:30:15 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2026 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             : #include <stdint.h>
      34             : #include "options.h"
      35             : #ifdef DEBUGGING
      36             : #include "debug.h"
      37             : #endif
      38             : #include "cnst.h"
      39             : #include "ivas_cnst.h"
      40             : #include "ivas_prot.h"
      41             : #include "rom_enc.h"
      42             : #include "rom_com.h"
      43             : #include "prot.h"
      44             : #include "wmc_auto.h"
      45             : 
      46             : 
      47             : /*-------------------------------------------------------------------*
      48             :  * pre_proc_ivas()
      49             :  *
      50             :  * Pre-processing (Selection of internal Fs, classification, SC VBR decision,
      51             :  * Decision matrix, Preprocessing at other Fs, core switching decision, ...)
      52             :  *--------------------------------------------------------------------*/
      53             : 
      54      834877 : void pre_proc_ivas(
      55             :     Encoder_State *st,                             /* i/o: encoder state structure                 */
      56             :     const int16_t last_element_mode,               /* i  : last element mode                       */
      57             :     const int32_t element_brate,                   /* i  : element bitrate                         */
      58             :     const int32_t last_element_brate,              /* i  : last element bitrate                    */
      59             :     const int16_t input_frame,                     /* i  : frame length                            */
      60             :     float old_inp_12k8[],                          /* i/o: buffer of old input signal              */
      61             :     float old_inp_16k[],                           /* i/o: buffer of old input signal @ 16kHz      */
      62             :     float **inp,                                   /* o  : ptr. to inp. signal in the current frame*/
      63             :     float *ener,                                   /* o  : residual energy from Levinson-Durbin    */
      64             :     float A[NB_SUBFR16k * ( M + 1 )],              /* i/o: A(z) unquantized for the 4 subframes    */
      65             :     float Aw[NB_SUBFR16k * ( M + 1 )],             /* i/o: weighted A(z) unquantized for subframes */
      66             :     float epsP[M + 1],                             /* i/o: LP prediction errors                    */
      67             :     float lsp_new[M],                              /* i/o: LSPs at the end of the frame            */
      68             :     float lsp_mid[M],                              /* i/o: LSPs in the middle of the frame         */
      69             :     float *new_inp_resamp16k,                      /* o  : new input signal @16kHz, non pre-emphasised, used by the WB TBE/BWE */
      70             :     int16_t *Voicing_flag,                         /* o  : voicing flag for HQ FEC                 */
      71             :     const float old_wsp[],                         /* i  : weighted input signal buffer            */
      72             :     const int16_t loc_harm,                        /* i  : harmonicity flag                        */
      73             :     const float cor_map_sum,                       /* i  : speech/music clasif. parameter          */
      74             :     const int16_t vad_flag_dtx,                    /* i  : HE-SAD flag with additional DTX HO      */
      75             :     const float enerBuffer[CLDFB_NO_CHANNELS_MAX], /* i  : energy buffer                           */
      76             :     const float fft_buff[2 * L_FFT],               /* i  : FFT buffer                              */
      77             :     const int16_t MCT_flag,                        /* i  : hMCT handle allocated (1) or not (0)    */
      78             :     const int16_t vad_hover_flag,                  /* i  : VAD hangover flag                       */
      79             :     const int16_t flag_16k_smc                     /* i  : flag to indicate if the OL SMC is run at 16 kHz */
      80             : )
      81             : {
      82             :     int16_t L_look, element_mode, lMemRecalc_12k8;
      83             :     float *inp_12k8, *new_inp_12k8, *inp_16k; /* pointers to current frame and new data */
      84             :     const float *wsp;                         /* weighted input signal buffer           */
      85             :     int32_t sr_core_tmp, total_brate_tmp;
      86             : 
      87      834877 :     push_wmops( "pre_proc" );
      88             : 
      89             :     /*----------------------------------------------------------------*
      90             :      * Initialization
      91             :      *----------------------------------------------------------------*/
      92             : 
      93      834877 :     element_mode = st->element_mode;
      94             : 
      95      834877 :     new_inp_12k8 = old_inp_12k8 + L_INP_MEM; /* pointer to new samples of the input signal in 12.8kHz core */
      96      834877 :     inp_12k8 = new_inp_12k8 - L_LOOK_12k8;
      97             : 
      98      834877 :     if ( element_mode != IVAS_CPE_DFT )
      99             :     {
     100      784525 :         new_inp_12k8 -= L_FILT;
     101             :     }
     102             : 
     103      834877 :     wsp = old_wsp + L_WSP_MEM; /* pointer to the current frame of weighted signal in 12.8kHz core */
     104             : 
     105      834877 :     lMemRecalc_12k8 = 0;
     106      834877 :     if ( element_mode == IVAS_CPE_TD )
     107             :     {
     108        4124 :         lMemRecalc_12k8 = NS2SA( INT_FS_12k8, L_MEM_RECALC_NS );
     109             :     }
     110             : 
     111             :     /*----------------------------------------------------------------*
     112             :      * Selection of internal ACELP Fs (12.8 kHz or 16 kHz)
     113             :      *----------------------------------------------------------------*/
     114             : 
     115      834877 :     if ( st->core_brate == FRAME_NO_DATA )
     116             :     {
     117             :         /* prevent "L_frame" changes in CNG segments */
     118        9464 :         st->L_frame = st->last_L_frame;
     119             :     }
     120      825413 :     else if ( st->core_brate == SID_2k40 && st->bwidth >= WB && st->hDtxEnc->first_CNG && ( st->hTdCngEnc != NULL && st->hTdCngEnc->act_cnt2 < MIN_ACT_CNG_UPD ) )
     121             :     {
     122             :         /* prevent "L_frame" changes in SID frame after short segment of active frames */
     123         640 :         st->L_frame = st->hDtxEnc->last_CNG_L_frame;
     124             :     }
     125      824773 :     else if ( ( ( st->element_mode == IVAS_CPE_MDCT && st->element_brate >= IVAS_64k && st->bwidth >= SWB ) || ( element_mode == IVAS_SCE && st->total_brate > MAX_ACELP_BRATE && st->bwidth >= SWB ) ) && st->core_brate != SID_2k40 )
     126             :     {
     127      451669 :         st->L_frame = L_FRAME32k;
     128             :     }
     129      373104 :     else if ( st->bwidth >= SWB && st->total_brate > MAX_ACELP_BRATE_ISM && st->total_brate <= MAX_ACELP_BRATE && element_mode == IVAS_SCE && st->is_ism_format && st->tcxonly && st->core_brate != SID_2k40 )
     130             :     {
     131        4461 :         st->L_frame = L_FRAME25_6k;
     132             :     }
     133      368643 :     else if ( st->flag_ACELP16k )
     134             :     {
     135      255527 :         st->L_frame = L_FRAME16k;
     136             :     }
     137             :     else
     138             :     {
     139      113116 :         st->L_frame = L_FRAME;
     140             :     }
     141             : 
     142      834877 :     if ( st->hFdCngEnc != NULL && st->element_mode != IVAS_CPE_MDCT && ( ( st->hFdCngEnc->hFdCngCom->frameSize != st->L_frame ) || ( st->hFdCngEnc->hFdCngCom->CngBandwidth != st->input_bwidth ) ) )
     143             :     {
     144       25062 :         configureFdCngEnc( st->hFdCngEnc, max( st->input_bwidth, WB ), st->L_frame == L_FRAME16k ? ACELP_16k40 : ACELP_9k60 );
     145             :     }
     146             : 
     147      834877 :     if ( st->ini_frame == 0 )
     148             :     {
     149             :         /* avoid switching of internal ACELP Fs in the very first frame */
     150        9896 :         st->last_L_frame = st->L_frame;
     151             :     }
     152             : 
     153      834877 :     if ( st->L_frame == L_FRAME )
     154             :     {
     155      116793 :         st->gamma = GAMMA1;
     156      116793 :         st->preemph_fac = PREEMPH_FAC;
     157             :     }
     158      718084 :     else if ( st->L_frame == L_FRAME32k )
     159             :     {
     160      451669 :         st->gamma = GAMMA16k;
     161      451669 :         st->preemph_fac = PREEMPH_FAC_SWB;
     162             :     }
     163             :     else
     164             :     {
     165      266415 :         st->gamma = GAMMA16k;
     166      266415 :         st->preemph_fac = PREEMPH_FAC_16k;
     167             :     }
     168             : 
     169      834877 :     st->sr_core = st->L_frame * FRAMES_PER_SEC;
     170      834877 :     st->encoderLookahead_enc = NS2SA( st->sr_core, ACELP_LOOK_NS );
     171      834877 :     st->encoderPastSamples_enc = ( st->L_frame * 9 ) >> 4;
     172             : 
     173             : 
     174             :     /*-----------------------------------------------------------------*
     175             :      * coder_type rewriting in case of switching
     176             :      * IC frames selection
     177             :      * enforce TC frames in case of switching
     178             :      *-----------------------------------------------------------------*/
     179             : 
     180             :     /* enforce TRANSITION frames */
     181      834877 :     if ( !( st->element_mode == IVAS_CPE_TD && st->idchan == 1 ) && st->last_L_frame != st->L_frame && st->core_brate != FRAME_NO_DATA && st->core_brate != SID_2k40 && st->last_core_brate != FRAME_NO_DATA && st->last_core_brate != SID_2k40 && st->coder_type_raw != VOICED )
     182             :     {
     183             :         /* enforce TC frame in case of ACELP@12k8 <-> ACELP@16k core switching */
     184      134372 :         st->coder_type = TRANSITION;
     185             :     }
     186      700505 :     else if ( st->last_core == HQ_CORE && st->coder_type_raw != VOICED )
     187             :     {
     188             :         /* enforce TC frame in case of HQ -> ACELP core switching */
     189        4592 :         st->coder_type = TRANSITION;
     190             :     }
     191      695913 :     else if ( st->last_core_brate <= SID_2k40 && st->cng_type == FD_CNG && !( element_mode == IVAS_CPE_TD ) )
     192             :     {
     193             :         /* enforce TC frame in case of FD_CNG -> ACELP switching (past excitation not available) */
     194       10831 :         st->coder_type = TRANSITION;
     195             :     }
     196             :     /* select INACTIVE frames */
     197      685082 :     else if ( st->total_brate <= MAX_GSC_INACTIVE_BRATE && st->vad_flag == 0 && st->element_mode != IVAS_CPE_MDCT )
     198             :     {
     199             :         /* inactive frames will be coded by GSC technology */
     200             :         /* except for the VBR mode. VBR mode uses NELP for that */
     201       18722 :         if ( !( st->Opt_SC_VBR && vad_flag_dtx ) && ( st->idchan == 0 || element_mode != IVAS_CPE_TD ) )
     202             :         {
     203       18722 :             st->coder_type = INACTIVE;
     204       18722 :             st->hGSCEnc->noise_lev = NOISE_LEVEL_SP3;
     205             :         }
     206             :     }
     207      666360 :     else if ( st->total_brate > MAX_GSC_INACTIVE_BRATE && ( ( st->vad_flag == 0 && st->bwidth >= SWB && st->max_bwidth >= SWB ) || ( st->localVAD == 0 && ( st->bwidth <= WB || st->max_bwidth <= WB ) ) ) )
     208             :     {
     209             :         /* inactive frames will be coded by AVQ technology (exceptionally it can be later rewritten to GSC technology in ivas_combined_format_brate_sanity()) */
     210       48284 :         st->coder_type = INACTIVE;
     211             :     }
     212             : 
     213             :     /*---------------------------------------------------------------------*
     214             :      * Decision matrix (selection of technologies)
     215             :      *---------------------------------------------------------------------*/
     216             : 
     217      834877 :     st->mdct_sw = MODE1;
     218      834877 :     st->mdct_sw_enable = MODE1;
     219      834877 :     if ( ( st->total_brate <= MIN_BRATE_GSC_NOISY_FLAG || st->bwidth < SWB || st->flag_ACELP16k ) && st->GSC_IVAS_mode == 0 )
     220             :     {
     221      767268 :         st->GSC_noisy_speech = 0;
     222             :     }
     223             : 
     224             :     /* core selection */
     225      834877 :     ivas_decision_matrix_enc( st, element_brate, fft_buff, enerBuffer, last_element_mode );
     226             : 
     227      834877 :     if ( st->L_frame == L_FRAME16k && ( st->coder_type == VOICED || st->coder_type == UNVOICED ) ) /* VOICED and UNVOICED are not supported in ACELP@16k */
     228             :     {
     229           0 :         st->coder_type = GENERIC;
     230             :     }
     231             : 
     232      834877 :     if ( st->core == TCX_20_CORE || st->core == HQ_CORE )
     233             :     {
     234      695897 :         st->Nb_ACELP_frames = 0;
     235             :         /* Configure TCX with the same bitrate as given when (re-)initializing TCX */
     236      695897 :         total_brate_tmp = st->total_brate;
     237      695897 :         st->total_brate = st->bits_frame_nominal * FRAMES_PER_SEC;
     238      695897 :         SetModeIndex( st, st->last_bits_frame_nominal * FRAMES_PER_SEC, last_element_mode, MCT_flag );
     239             : 
     240      695897 :         st->sr_core = getCoreSamplerateMode2( element_mode, st->total_brate, st->bwidth, st->flag_ACELP16k, st->rf_mode, st->is_ism_format );
     241      695897 :         st->total_brate = total_brate_tmp;
     242             : 
     243      695897 :         st->L_frame = (int16_t) ( st->sr_core / FRAMES_PER_SEC );
     244      695897 :         st->encoderLookahead_enc = NS2SA( st->sr_core, ACELP_LOOK_NS );
     245      695897 :         st->encoderPastSamples_enc = ( st->L_frame * 9 ) >> 4;
     246             : 
     247      695897 :         if ( st->sr_core == INT_FS_12k8 )
     248             :         {
     249       43190 :             st->preemph_fac = PREEMPH_FAC;
     250       43190 :             st->gamma = GAMMA1;
     251             :         }
     252      652707 :         else if ( st->sr_core == INT_FS_16k )
     253             :         {
     254      133122 :             st->preemph_fac = PREEMPH_FAC_16k;
     255      133122 :             st->gamma = GAMMA16k;
     256             :         }
     257             :         else /* st->sr_core >=25600 */
     258             :         {
     259      519585 :             st->preemph_fac = PREEMPH_FAC_SWB;
     260      519585 :             st->gamma = GAMMA16k;
     261             :         }
     262             : 
     263      695897 :         if ( st->vad_flag == 0 )
     264             :         {
     265       75856 :             st->coder_type = INACTIVE;
     266             :         }
     267      620041 :         else if ( st->coder_type > GENERIC )
     268             :         {
     269      139881 :             st->coder_type = GENERIC;
     270             :         }
     271      695897 :         if ( st->element_mode != IVAS_CPE_MDCT )
     272             :         {
     273      186101 :             SetTCXModeInfo( st, st->hTranDet, &st->hTcxCfg->tcx_curr_overlap_mode );
     274             :         }
     275             :     }
     276      138980 :     else if ( st->element_mode == IVAS_CPE_MDCT )
     277             :     {
     278        3406 :         st->hTcxEnc->tfm_mem = 0.75f;
     279             :     }
     280      135574 :     else if ( element_brate != last_element_brate )
     281             :     {
     282          36 :         if ( st->core_brate != FRAME_NO_DATA )
     283             :         {
     284          36 :             SetModeIndex( st, st->bits_frame_nominal * FRAMES_PER_SEC, element_mode, MCT_flag );
     285             :         }
     286             : 
     287          36 :         if ( st->extl != -1 && st->extl != IGF_BWE && st->igf == 1 )
     288             :         {
     289          36 :             st->igf = 0;
     290             :         }
     291             :     }
     292             : 
     293             :     /*-----------------------------------------------------------------*
     294             :      * Update of ACELP harmonicity counter (used in ACELP transform codebook @32kbps)
     295             :      *-----------------------------------------------------------------*/
     296             : 
     297      834877 :     if ( loc_harm == 1 && cor_map_sum > 50 && st->clas == VOICED_CLAS && st->coder_type == GENERIC )
     298             :     {
     299      151210 :         st->last_harm_flag_acelp++;
     300             : 
     301      151210 :         if ( st->last_harm_flag_acelp > 10 )
     302             :         {
     303       25094 :             st->last_harm_flag_acelp = 10;
     304             :         }
     305             :     }
     306             :     else
     307             :     {
     308      683667 :         st->last_harm_flag_acelp = 0;
     309             :     }
     310             : 
     311             :     /*-----------------------------------------------------------------*
     312             :      * Update audio frames counter (used for UV decision)
     313             :      *-----------------------------------------------------------------*/
     314             : 
     315      834877 :     if ( st->coder_type == AUDIO )
     316             :     {
     317        8569 :         st->audio_frame_cnt += AUDIO_COUNTER_STEP;
     318             :     }
     319      826308 :     else if ( st->coder_type != INACTIVE )
     320             :     {
     321      735599 :         st->audio_frame_cnt--;
     322             :     }
     323             : 
     324      834877 :     if ( st->audio_frame_cnt > AUDIO_COUNTER_MAX )
     325             :     {
     326        2376 :         st->audio_frame_cnt = AUDIO_COUNTER_MAX;
     327             :     }
     328             : 
     329      834877 :     if ( st->audio_frame_cnt < 0 )
     330             :     {
     331      438665 :         st->audio_frame_cnt = 0;
     332             :     }
     333             : 
     334             :     /*-----------------------------------------------------------------*
     335             :      * Set formant sharpening flag
     336             :      *-----------------------------------------------------------------*/
     337             : 
     338      834877 :     st->sharpFlag = 0;
     339             : 
     340      834877 :     if ( st->coder_type == GENERIC || st->coder_type == VOICED || st->coder_type == TRANSITION )
     341             :     {
     342      732343 :         if ( element_brate >= FRMT_SHP_MIN_BRATE_IVAS && st->lp_noise > FORMANT_SHARPENING_NOISE_THRESHOLD )
     343             :         {
     344       44004 :             st->sharpFlag = 0;
     345             :         }
     346             :         else
     347             :         {
     348      688339 :             st->sharpFlag = 1;
     349             :         }
     350             :     }
     351             : 
     352             :     /* TD stereo, secondary channel - due to lack of signaling bits, sharpFlag is always 1 */
     353      834877 :     if ( element_mode == IVAS_CPE_TD && st->idchan == 1 )
     354             :     {
     355        2062 :         st->sharpFlag = 0;
     356        2062 :         if ( st->coder_type == GENERIC || st->coder_type == VOICED )
     357             :         {
     358        2022 :             st->sharpFlag = 1;
     359             :         }
     360             :     }
     361             : 
     362             :     /*-----------------------------------------------------------------*
     363             :      * Set voicing flag for HQ FEC
     364             :      *-----------------------------------------------------------------*/
     365             : 
     366      834877 :     if ( st->sp_aud_decision1 == 0 && ( st->coder_type == VOICED || st->coder_type == GENERIC ) )
     367             :     {
     368      333086 :         *Voicing_flag = 1;
     369             :     }
     370             :     else
     371             :     {
     372      501791 :         *Voicing_flag = 0;
     373             :     }
     374             : 
     375             :     /*-----------------------------------------------------------------*
     376             :      * Compute core-coder buffers at internal sampling rate
     377             :      *-----------------------------------------------------------------*/
     378             : 
     379      834877 :     sr_core_tmp = ( st->tcxonly == 0 ) ? INT_FS_16k : max( INT_FS_16k, st->sr_core ); /* indicates the ACELP sampling rate */
     380             : 
     381      834877 :     L_look = NS2SA( sr_core_tmp, ACELP_LOOK_NS ); /* lookahead at other sampling rate (16kHz, 25.6kHz, 32kHz) */
     382             : 
     383      834877 :     inp_16k = old_inp_16k + L_INP_MEM - L_look;
     384             : 
     385      834877 :     if ( !flag_16k_smc )
     386             :     {
     387      825069 :         ivas_compute_core_buffers( st, &inp_16k, old_inp_16k, new_inp_resamp16k, input_frame, last_element_mode, sr_core_tmp, ener, A, Aw, epsP, lsp_new, lsp_mid );
     388             :     }
     389             : 
     390      834877 :     if ( !( st->L_frame == L_FRAME16k && element_mode != IVAS_CPE_MDCT ) )
     391             :     {
     392             :         /* update signal buffers */
     393      695355 :         mvr2r( new_inp_12k8, st->buf_speech_enc_pe + st->L_frame, L_FRAME );
     394      695355 :         mvr2r( st->buf_speech_enc + L_FRAME32k, st->buf_speech_enc + st->L_frame, L_FRAME );
     395             : 
     396      695355 :         if ( element_mode == IVAS_CPE_DFT )
     397             :         {
     398       25177 :             mvr2r( st->buf_speech_enc + L_FRAME32k - STEREO_DFT_OVL_12k8, st->buf_speech_enc + st->L_frame - STEREO_DFT_OVL_12k8, STEREO_DFT_OVL_12k8 );
     399             :         }
     400      670178 :         else if ( element_mode == IVAS_CPE_TD || element_mode == IVAS_CPE_MDCT )
     401             :         {
     402      516088 :             mvr2r( st->buf_speech_enc + L_FRAME32k - lMemRecalc_12k8 - L_FILT, st->buf_speech_enc + st->L_frame - lMemRecalc_12k8 - L_FILT, lMemRecalc_12k8 + L_FILT );
     403             :         }
     404      154090 :         else if ( element_mode == IVAS_SCE )
     405             :         {
     406      154090 :             mvr2r( st->buf_speech_enc + L_FRAME32k - L_FILT, st->buf_speech_enc + st->L_frame - L_FILT, L_FILT );
     407             :         }
     408             : 
     409      695355 :         if ( st->tcxonly == 0 )
     410             :         {
     411      110897 :             mvr2r( wsp, st->wspeech_enc, L_FRAME + L_LOOK_12k8 );
     412             :         }
     413             :     }
     414             : 
     415      834877 :     if ( flag_16k_smc )
     416             :     {
     417        9808 :         mvr2r( st->buf_speech_enc + L_FRAME16k, new_inp_resamp16k, L_FRAME16k );
     418             :     }
     419             : 
     420             :     /*-----------------------------------------------------------------*
     421             :      * Updates
     422             :      *-----------------------------------------------------------------*/
     423             : 
     424             :     /* update old weighted speech buffer - for OL pitch analysis */
     425      834877 :     mvr2r( &old_wsp[L_FRAME], st->old_wsp, L_WSP_MEM );
     426             : 
     427             :     /* set the pointer of the current frame for the ACELP core */
     428      834877 :     if ( st->L_frame == L_FRAME )
     429             :     {
     430      116793 :         *inp = inp_12k8;
     431             :     }
     432             :     else
     433             :     {
     434      718084 :         *inp = inp_16k;
     435             :     }
     436             : 
     437             :     /* Update VAD hangover frame counter in active frames */
     438      834877 :     if ( !( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA ) && st->tcxonly == 0 )
     439             :     {
     440      240827 :         if ( st->hTdCngEnc != NULL && st->Opt_DTX_ON && vad_hover_flag )
     441             :         {
     442        1283 :             st->hTdCngEnc->burst_ho_cnt++;
     443        1283 :             if ( st->hTdCngEnc->burst_ho_cnt > HO_HIST_SIZE )
     444             :             {
     445          21 :                 st->hTdCngEnc->burst_ho_cnt = HO_HIST_SIZE;
     446             :             }
     447             :         }
     448      239544 :         else if ( st->hTdCngEnc != NULL && vad_flag_dtx )
     449             :         {
     450       16780 :             st->hTdCngEnc->burst_ho_cnt = 0;
     451             :         }
     452             :     }
     453             : 
     454             : #ifdef DEBUG_MODE_ACELP
     455             :     dbgwrite( inp_12k8, sizeof( float ), L_FRAME, 1, fname( debug_dir, "inp_12k8", st->idchan, st->id_element, ENC ) );
     456             :     dbgwrite( inp_16k, sizeof( float ), L_FRAME, 1, fname( debug_dir, "inp_16k", st->idchan, st->id_element, ENC ) );
     457             : #endif
     458             : 
     459      834877 :     pop_wmops();
     460      834877 :     return;
     461             : }
     462             : 
     463             : 
     464             : /*-------------------------------------------------------------------*
     465             :  * ivas_compute_core_buffers()
     466             :  *
     467             :  * Compute core-coder buffers at internal sampling rate
     468             :  *--------------------------------------------------------------------*/
     469             : 
     470      834877 : void ivas_compute_core_buffers(
     471             :     Encoder_State *st,                 /* i/o: encoder state structure                  */
     472             :     float **inp16k_out,                /* o  : ptr. to inp. signal in the current frame */
     473             :     float *old_inp_16k,                /* i/o: buffer of old input signal @ 16kHz       */
     474             :     float new_inp_resamp16k_out[],     /* o  : new input signal @16kHz, non pre-emphasised, used by the WB TBE/BWE */
     475             :     const int16_t input_frame,         /* i  : frame length                             */
     476             :     const int16_t last_element_mode,   /* i  : last element mode                        */
     477             :     const int32_t sr_core,             /* i  : core-coder sampling rate                 */
     478             :     float *ener,                       /* o  : residual energy from Levinson-Durbin     */
     479             :     float A[NB_SUBFR16k * ( M + 1 )],  /* i/o: A(z) unquantized for the 4 subframes     */
     480             :     float Aw[NB_SUBFR16k * ( M + 1 )], /* i/o: weighted A(z) unquantized for subframes  */
     481             :     float epsP[M + 1],                 /* i/o: LP prediction errors                     */
     482             :     float lsp_new[M],                  /* i/o: LSPs at the end of the frame             */
     483             :     float lsp_mid[M]                   /* i/o: LSPs in the middle of the frame          */
     484             : )
     485             : {
     486             :     float *inp_16k, *new_inp_16k;
     487             :     float tmp, mem_decim16k_dummy[2 * L_FILT_MAX];
     488             :     const float *signal_in;
     489             :     int16_t delay, element_mode;
     490             :     float temp1F_icatdmResampBuf[L_FILT_MAX]; /* temp buffers for ICA TDM resamplers */
     491             :     float new_inp_resamp16k[L_FRAME16k];
     492             :     int16_t lMemRecalc, lMemRecalc_16k, L_frame_tmp, L_look;
     493             :     int32_t input_Fs;
     494             : 
     495      834877 :     signal_in = st->input;
     496             : 
     497      834877 :     input_Fs = st->input_Fs;
     498      834877 :     element_mode = st->element_mode;
     499             : 
     500      834877 :     lMemRecalc_16k = 0;
     501      834877 :     lMemRecalc = 0;
     502      834877 :     if ( element_mode == IVAS_CPE_TD )
     503             :     {
     504        4124 :         lMemRecalc_16k = NS2SA( INT_FS_16k, L_MEM_RECALC_NS );
     505        4124 :         lMemRecalc = NS2SA( input_Fs, L_MEM_RECALC_NS );
     506             :     }
     507             : 
     508             :     /*---------------------------------------------------------------*
     509             :      * Preprocessing at other sampling frequency rate (16/25.6/32kHz)
     510             :      *----------------------------------------------------------------*/
     511             : 
     512      834877 :     L_frame_tmp = ( st->tcxonly == 0 ) ? L_FRAME16k : max( L_FRAME16k, st->L_frame );
     513             : 
     514      834877 :     L_look = NS2SA( sr_core, ACELP_LOOK_NS ); /* lookahead at other sampling rate (16kHz, 25.6kHz, 32kHz) */
     515             : 
     516      834877 :     new_inp_16k = old_inp_16k + L_INP_MEM; /* pointer to new samples of the input signal in 16kHz core */
     517      834877 :     inp_16k = new_inp_16k - L_look;        /* pointer to the current frame of input signal in 16kHz core */
     518             : 
     519             :     /* shift the pointer back to take care of resampler memory update */
     520      834877 :     if ( element_mode == IVAS_CPE_TD || element_mode == IVAS_SCE )
     521             :     {
     522      271323 :         new_inp_16k -= NS2SA( sr_core, DELAY_FIR_RESAMPL_NS );
     523             :     }
     524             : 
     525      834877 :     if ( element_mode == IVAS_CPE_DFT )
     526             :     {
     527       50352 :         mvr2r( st->old_inp_16k, old_inp_16k, L_INP_MEM - STEREO_DFT_OVL_16k );
     528             :     }
     529      784525 :     else if ( element_mode == IVAS_CPE_TD )
     530             :     {
     531        4124 :         mvr2r( st->old_inp_16k, old_inp_16k, L_INP_MEM - L_MEM_RECALC_16K - L_FILT16k );
     532             :     }
     533             :     else
     534             :     {
     535      780401 :         mvr2r( st->old_inp_16k, old_inp_16k, L_INP_MEM - L_FILT16k );
     536             :     }
     537             : 
     538             :     /*---------------------------------------------------------------*
     539             :      * Change the sampling frequency to 16/25.6/32 kHz
     540             :      *----------------------------------------------------------------*/
     541             : 
     542      834877 :     if ( element_mode == IVAS_SCE )
     543             :     {
     544      267199 :         if ( input_Fs == sr_core )
     545             :         {
     546             :             /* no resampling needed, only delay adjustment to account for the FIR resampling delay */
     547        3734 :             delay = NS2SA( input_Fs, DELAY_FIR_RESAMPL_NS );
     548        3734 :             mvr2r( st->mem_decim16k + delay, new_inp_16k, delay );
     549        3734 :             mvr2r( signal_in, new_inp_16k + delay, input_frame );
     550        3734 :             mvr2r( signal_in + input_frame - 2 * delay, st->mem_decim16k, 2 * delay );
     551             :         }
     552      263465 :         else if ( input_Fs == 32000 || input_Fs == 48000 )
     553             :         {
     554      263465 :             modify_Fs( signal_in, input_frame, input_Fs, new_inp_16k, sr_core, st->mem_decim16k, 0 );
     555             : 
     556      263465 :             mvr2r( st->mem_decim16k, mem_decim16k_dummy, 2 * L_FILT_MAX );
     557      263465 :             set_f( temp1F_icatdmResampBuf, 0, L_FILT_MAX );
     558      263465 :             modify_Fs( temp1F_icatdmResampBuf, NS2SA( input_Fs, DELAY_FIR_RESAMPL_NS ), input_Fs, new_inp_16k + NS2SA( sr_core, FRAME_SIZE_NS ), sr_core, mem_decim16k_dummy, 0 );
     559             :         }
     560             :     }
     561      567678 :     else if ( element_mode == IVAS_CPE_TD )
     562             :     {
     563        4124 :         if ( input_Fs == sr_core )
     564             :         {
     565             :             /* no resampling needed, only delay adjustment to account for the FIR resampling delay */
     566        1062 :             delay = NS2SA( input_Fs, DELAY_FIR_RESAMPL_NS );
     567        1062 :             mvr2r( st->mem_decim16k + delay, new_inp_16k - lMemRecalc - delay + L_FILT16k, delay );
     568        1062 :             mvr2r( signal_in - lMemRecalc, new_inp_16k - lMemRecalc + L_FILT16k, input_frame + lMemRecalc );
     569        1062 :             mvr2r( signal_in + input_frame - lMemRecalc - 2 * delay, st->mem_decim16k, 2 * delay );
     570             :         }
     571        3062 :         else if ( input_Fs == 32000 || input_Fs == 48000 )
     572             :         {
     573             :             /* reconstruct past segment of input signal when switching from MDCT stereo */
     574        3062 :             if ( last_element_mode == IVAS_CPE_MDCT /*|| st->idchan == 1*/ )
     575             :             {
     576          34 :                 int16_t length_inp = NS2SA( input_Fs, L_MEM_RECALC_SCH_NS - DELAY_FIR_RESAMPL_NS );
     577          34 :                 int16_t length_16k = NS2SA( INT_FS_16k, L_MEM_RECALC_SCH_NS - DELAY_FIR_RESAMPL_NS );
     578             : 
     579          34 :                 mvr2r( signal_in - lMemRecalc - length_inp - 2 * NS2SA( input_Fs, DELAY_FIR_RESAMPL_NS ), st->mem_decim16k, 2 * NS2SA( input_Fs, DELAY_FIR_RESAMPL_NS ) );
     580          34 :                 modify_Fs( signal_in - lMemRecalc - length_inp, length_inp, input_Fs, new_inp_16k - lMemRecalc_16k - length_16k, sr_core, st->mem_decim16k, 0 );
     581             :             }
     582             : 
     583        3062 :             modify_Fs( signal_in - lMemRecalc, input_frame, input_Fs, new_inp_16k - lMemRecalc_16k, sr_core, st->mem_decim16k, 0 );
     584        3062 :             mvr2r( st->mem_decim16k, mem_decim16k_dummy, 2 * L_FILT_MAX );
     585             : 
     586        3062 :             if ( lMemRecalc > 0 )
     587             :             {
     588        3062 :                 modify_Fs( signal_in - lMemRecalc + input_frame, lMemRecalc, input_Fs, new_inp_16k + NS2SA( sr_core, FRAME_SIZE_NS ) - lMemRecalc_16k, sr_core, mem_decim16k_dummy, 0 );
     589             :             }
     590             : 
     591        3062 :             set_f( temp1F_icatdmResampBuf, 0, L_FILT_MAX );
     592        3062 :             modify_Fs( temp1F_icatdmResampBuf, NS2SA( input_Fs, DELAY_FIR_RESAMPL_NS ), input_Fs, new_inp_16k + NS2SA( sr_core, FRAME_SIZE_NS ), sr_core, mem_decim16k_dummy, 0 );
     593             :         }
     594             :     }
     595      563554 :     else if ( st->idchan == 0 )
     596             :     {
     597             :         /* update the FIR resampling filter memory, needed for switching to time-domain (FIR) resampling */
     598      306953 :         mvr2r( signal_in + input_frame - NS2SA( input_Fs, L_MEM_RECALC_NS ) - 2 * NS2SA( input_Fs, DELAY_FIR_RESAMPL_NS ), st->mem_decim16k, 2 * NS2SA( input_Fs, DELAY_FIR_RESAMPL_NS ) );
     599             :     }
     600             : 
     601             :     /*------------------------------------------------*
     602             :      * Update BWE memories                            *
     603             :      *------------------------------------------------*/
     604             : 
     605      834877 :     if ( sr_core == INT_FS_16k && element_mode != IVAS_CPE_MDCT )
     606             :     {
     607      256314 :         delay = NS2SA( INT_FS_16k, DELAY_FD_BWE_ENC_12k8_NS );
     608             : 
     609      256314 :         if ( element_mode == IVAS_CPE_DFT )
     610             :         {
     611             :             /* save input resampled at 16kHz, non-preemphasised.*/
     612       50352 :             mvr2r( new_inp_16k, new_inp_resamp16k, L_FRAME16k );
     613             : 
     614       50352 :             if ( st->bwidth == WB )
     615             :             {
     616       10099 :                 mvr2r( new_inp_16k - delay, st->hBWE_FD->old_input_wb, delay );
     617       10099 :                 mvr2r( new_inp_16k - STEREO_DFT_OVL_16k, st->hBWE_FD->old_wtda_swb + L_FRAME16k - STEREO_DFT_OVL_16k + delay, STEREO_DFT_OVL_16k - delay );
     618             :             }
     619             :         }
     620      205962 :         else if ( element_mode == IVAS_CPE_TD )
     621             :         {
     622             :             /* save input resampled at 16kHz, non-preemphasised */
     623        4124 :             mvr2r( new_inp_16k + L_FILT16k, new_inp_resamp16k, L_FRAME16k );
     624             : 
     625        4124 :             if ( st->bwidth == WB && st->hBWE_FD != NULL )
     626             :             {
     627        1166 :                 mvr2r( new_inp_16k + L_FILT16k - delay, st->hBWE_FD->old_input_wb, delay );
     628        1166 :                 mvr2r( new_inp_16k - L_MEM_RECALC_16K, st->hBWE_FD->old_wtda_swb + L_FRAME16k - L_MEM_RECALC_16K - L_FILT16k + delay, L_MEM_RECALC_16K + L_FILT16k - delay );
     629             :             }
     630             :         }
     631      201838 :         else if ( element_mode == IVAS_SCE )
     632             :         {
     633             :             /* save input resampled at 16kHz, non-preemphasised */
     634      201838 :             mvr2r( new_inp_16k + L_FILT16k, new_inp_resamp16k, L_FRAME16k );
     635             : 
     636      201838 :             if ( st->bwidth == WB )
     637             :             {
     638       21041 :                 mvr2r( new_inp_16k, st->hBWE_FD->old_input_wb + delay - L_FILT16k, L_FILT16k );
     639             :                 /* all buffer st->hBWE_FD->old_wtda_swb is correct and does not need to be updated */
     640             :             }
     641             :         }
     642             :     }
     643      578563 :     else if ( sr_core > INT_FS_16k )
     644             :     {
     645             :         /* reset the buffer, the signal is needed for WB BWEs */
     646      519585 :         set_f( new_inp_resamp16k, 0.0f, L_FRAME16k );
     647             :     }
     648             : 
     649             :     /*------------------------------------------------------------------*
     650             :      * Perform fixed preemphasis (16kHz signal) through 1 - g*z^-1
     651             :      *-----------------------------------------------------------------*/
     652             : 
     653      834877 :     if ( st->tcxonly == 0 && !( ( element_mode == IVAS_CPE_TD && st->idchan == 1 ) || element_mode == IVAS_CPE_MDCT ) )
     654             :     {
     655      246774 :         st->mem_preemph_enc = new_inp_16k[L_frame_tmp - 1];
     656             :     }
     657             : 
     658      834877 :     if ( sr_core == INT_FS_16k && element_mode != IVAS_CPE_MDCT )
     659             :     {
     660      256314 :         if ( element_mode == IVAS_CPE_DFT )
     661             :         {
     662       50352 :             mvr2r( new_inp_16k - STEREO_DFT_OVL_16k + L_FRAME16k, st->inp_16k_mem_stereo_sw, STEREO_DFT_OVL_16k - L_MEM_RECALC_16K - L_FILT16k ); /* update for TD/DFT stereo switching */
     663             : 
     664       50352 :             st->mem_preemph16k = st->mem_preemph16k_DFT;
     665       50352 :             st->mem_preemph16k_DFT = old_inp_16k[L_INP_MEM - STEREO_DFT_OVL_16k + L_FRAME16k - 1];
     666             : 
     667       50352 :             if ( st->L_frame == L_FRAME16k )
     668             :             {
     669       25175 :                 mvr2r( new_inp_16k - STEREO_DFT_OVL_16k, st->buf_speech_enc + L_FRAME16k - STEREO_DFT_OVL_16k, L_FRAME16k + STEREO_DFT_OVL_16k );
     670             :             }
     671       50352 :             preemph( new_inp_16k - STEREO_DFT_OVL_16k, PREEMPH_FAC_16k, L_FRAME16k, &( st->mem_preemph16k ) );
     672       50352 :             tmp = st->mem_preemph16k;
     673       50352 :             preemph( new_inp_16k - STEREO_DFT_OVL_16k + L_FRAME16k, PREEMPH_FAC_16k, STEREO_DFT_OVL_16k, &tmp );
     674             :         }
     675      205962 :         else if ( st->element_mode == IVAS_CPE_TD )
     676             :         {
     677        4124 :             if ( last_element_mode == IVAS_CPE_DFT )
     678             :             {
     679          70 :                 st->mem_preemph16k = st->mem_preemph16k_DFT;
     680          70 :                 mvr2r( st->inp_16k_mem_stereo_sw, new_inp_16k - L_MEM_RECALC_16K - ( STEREO_DFT_OVL_16k - L_MEM_RECALC_16K - L_FILT16k ), STEREO_DFT_OVL_16k - L_MEM_RECALC_16K - L_FILT16k );
     681          70 :                 preemph( new_inp_16k - L_MEM_RECALC_16K - ( STEREO_DFT_OVL_16k - L_MEM_RECALC_16K - L_FILT16k ), PREEMPH_FAC_16k, STEREO_DFT_OVL_16k - L_MEM_RECALC_16K - L_FILT16k, &st->mem_preemph16k );
     682             :             }
     683             : 
     684        4124 :             st->mem_preemph16k_DFT = old_inp_16k[L_INP_MEM - STEREO_DFT_OVL_16k + L_FRAME16k - 1];
     685             : 
     686             :             /* preemphasise past segment of input signal when switching from MDCT stereo */
     687        4124 :             if ( last_element_mode == IVAS_CPE_MDCT )
     688             :             {
     689          34 :                 int16_t length_16k = NS2SA( INT_FS_16k, L_MEM_RECALC_SCH_NS - DELAY_FIR_RESAMPL_NS );
     690          34 :                 preemph( new_inp_16k - lMemRecalc_16k - length_16k, PREEMPH_FAC, length_16k, &st->mem_preemph16k );
     691             :             }
     692             : 
     693        4124 :             if ( st->L_frame == L_FRAME16k )
     694             :             {
     695        1238 :                 mvr2r( new_inp_16k - lMemRecalc_16k, st->buf_speech_enc + L_FRAME16k - lMemRecalc_16k - L_FILT16k, L_FRAME16k + lMemRecalc_16k + L_FILT16k );
     696             :             }
     697        4124 :             preemph( new_inp_16k - lMemRecalc_16k, PREEMPH_FAC_16k, L_FRAME16k, &( st->mem_preemph16k ) );
     698        4124 :             tmp = st->mem_preemph16k;
     699        4124 :             preemph( new_inp_16k - lMemRecalc_16k + L_FRAME16k, PREEMPH_FAC_16k, lMemRecalc_16k + L_FILT16k, &tmp );
     700             :         }
     701      201838 :         else if ( element_mode == IVAS_SCE )
     702             :         {
     703      201838 :             preemph( new_inp_16k, PREEMPH_FAC_16k, L_FRAME16k, &( st->mem_preemph16k ) );
     704      201838 :             tmp = st->mem_preemph16k;
     705      201838 :             preemph( new_inp_16k + L_FRAME16k, PREEMPH_FAC_16k, L_FILT16k, &tmp );
     706             :         }
     707             :     }
     708             :     else /* keep memory up-to-date in case of bitrate switching */
     709             :     {
     710      578563 :         if ( element_mode == IVAS_CPE_MDCT )
     711             :         {
     712      513202 :             st->mem_preemph16k = 0;
     713             :         }
     714             :         else /* SCE */
     715             :         {
     716       65361 :             st->mem_preemph16k = new_inp_16k[L_frame_tmp - 1];
     717             :         }
     718      578563 :         st->mem_preemph16k_DFT = st->mem_preemph16k;
     719             :     }
     720             : 
     721             :     /*-----------------------------------------------------------------*
     722             :      * LP analysis at 16kHz if ACELP@16k core was selected
     723             :      * update buffers
     724             :      *-----------------------------------------------------------------*/
     725             : 
     726      834877 :     if ( st->L_frame == L_FRAME16k && element_mode != IVAS_CPE_MDCT )
     727             :     {
     728             :         /* update signal buffers */
     729      139522 :         if ( element_mode == IVAS_CPE_DFT )
     730             :         {
     731       25175 :             mvr2r( new_inp_16k - STEREO_DFT_OVL_16k, st->buf_speech_enc_pe + L_FRAME16k - STEREO_DFT_OVL_16k, L_FRAME16k + STEREO_DFT_OVL_16k );
     732             :         }
     733      114347 :         else if ( element_mode == IVAS_CPE_TD )
     734             :         {
     735        1238 :             mvr2r( new_inp_16k - lMemRecalc_16k, st->buf_speech_enc_pe + L_FRAME16k - lMemRecalc_16k - L_FILT16k, L_FRAME16k + lMemRecalc_16k + L_FILT16k );
     736             :         }
     737             :         else
     738             :         {
     739      113109 :             mvr2r( new_inp_resamp16k, st->buf_speech_enc + L_FRAME16k, L_FRAME16k );
     740      113109 :             mvr2r( new_inp_16k, st->buf_speech_enc_pe + L_FRAME16k, L_FRAME16k );
     741             :         }
     742             : 
     743             :         /*--------------------------------------------------------------*
     744             :          * LPC analysis
     745             :          *---------------------------------------------------------------*/
     746             : 
     747      139522 :         if ( st->last_L_frame == L_FRAME )
     748             :         {
     749             :             /* this is just an approximation, but it is sufficient */
     750        1431 :             mvr2r( st->lsp_old1, st->lspold_enc, M );
     751             :         }
     752             : 
     753      139522 :         analy_lp( inp_16k, L_FRAME16k, L_look, ener, A, epsP, lsp_new, lsp_mid, st->lspold_enc, st->pitch, st->voicing, INT_FS_16k, 0 );
     754             : 
     755             :         /*--------------------------------------------------------------*
     756             :          * Compute Weighted Input
     757             :          *---------------------------------------------------------------*/
     758             : 
     759      139522 :         find_wsp( L_FRAME16k, L_SUBFR, NB_SUBFR16k, A, Aw, st->speech_enc_pe, PREEMPH_FAC_16k, st->wspeech_enc, &st->mem_wsp_enc, st->gamma, L_LOOK_16k );
     760             :     }
     761             : 
     762             :     /*-----------------------------------------------------------------*
     763             :      * Updates
     764             :      *-----------------------------------------------------------------*/
     765             : 
     766             :     /* update old input signal @16kHz buffer */
     767      834877 :     if ( ( element_mode == IVAS_CPE_TD && st->idchan == 1 ) || element_mode == IVAS_CPE_MDCT )
     768             :     {
     769      515264 :         set_f( st->old_inp_16k, 0, L_INP_MEM );
     770             :     }
     771      319613 :     else if ( sr_core == INT_FS_16k )
     772             :     {
     773      254252 :         mvr2r( &old_inp_16k[L_frame_tmp], st->old_inp_16k, L_INP_MEM );
     774             :     }
     775             :     else
     776             :     {
     777       65361 :         lerp( st->old_inp_12k8 + L_INP_MEM - L_INP_MEM * 4 / 5, st->old_inp_16k, L_INP_MEM, L_INP_MEM * 4 / 5 );
     778             :     }
     779             : 
     780      834877 :     if ( inp16k_out != NULL )
     781             :     {
     782      825069 :         *inp16k_out = inp_16k;
     783             :     }
     784             : 
     785      834877 :     if ( new_inp_resamp16k_out != NULL )
     786             :     {
     787      825069 :         mvr2r( new_inp_resamp16k, new_inp_resamp16k_out, L_FRAME16k );
     788             :     }
     789             : 
     790      834877 :     return;
     791             : }

Generated by: LCOV version 1.14