LCOV - code coverage report
Current view: top level - lib_enc - ivas_core_pre_proc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ efe53129c9ed87a5067dd0a8fb9dca41db9c4add Lines: 266 267 99.6 %
Date: 2026-02-12 08:06:51 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    17835655 : 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    17835655 :     push_wmops( "pre_proc" );
      88             : 
      89             :     /*----------------------------------------------------------------*
      90             :      * Initialization
      91             :      *----------------------------------------------------------------*/
      92             : 
      93    17835655 :     element_mode = st->element_mode;
      94             : 
      95    17835655 :     new_inp_12k8 = old_inp_12k8 + L_INP_MEM; /* pointer to new samples of the input signal in 12.8kHz core */
      96    17835655 :     inp_12k8 = new_inp_12k8 - L_LOOK_12k8;
      97             : 
      98    17835655 :     if ( element_mode != IVAS_CPE_DFT )
      99             :     {
     100    16922866 :         new_inp_12k8 -= L_FILT;
     101             :     }
     102             : 
     103    17835655 :     wsp = old_wsp + L_WSP_MEM; /* pointer to the current frame of weighted signal in 12.8kHz core */
     104             : 
     105    17835655 :     lMemRecalc_12k8 = 0;
     106    17835655 :     if ( element_mode == IVAS_CPE_TD )
     107             :     {
     108       62354 :         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    17835655 :     if ( st->core_brate == FRAME_NO_DATA )
     116             :     {
     117             :         /* prevent "L_frame" changes in CNG segments */
     118       84741 :         st->L_frame = st->last_L_frame;
     119             :     }
     120    17750914 :     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        5820 :         st->L_frame = st->hDtxEnc->last_CNG_L_frame;
     124             :     }
     125    17745094 :     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    10157409 :         st->L_frame = L_FRAME32k;
     128             :     }
     129     7587685 :     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       56430 :         st->L_frame = L_FRAME25_6k;
     132             :     }
     133     7531255 :     else if ( st->flag_ACELP16k )
     134             :     {
     135     5768418 :         st->L_frame = L_FRAME16k;
     136             :     }
     137             :     else
     138             :     {
     139     1762837 :         st->L_frame = L_FRAME;
     140             :     }
     141             : 
     142    17835655 :     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      413322 :         configureFdCngEnc( st->hFdCngEnc, max( st->input_bwidth, WB ), st->L_frame == L_FRAME16k ? ACELP_16k40 : ACELP_9k60 );
     145             :     }
     146             : 
     147    17835655 :     if ( st->ini_frame == 0 )
     148             :     {
     149             :         /* avoid switching of internal ACELP Fs in the very first frame */
     150      154587 :         st->last_L_frame = st->L_frame;
     151             :     }
     152             : 
     153    17835655 :     if ( st->L_frame == L_FRAME )
     154             :     {
     155     1792495 :         st->gamma = GAMMA1;
     156     1792495 :         st->preemph_fac = PREEMPH_FAC;
     157             :     }
     158    16043160 :     else if ( st->L_frame == L_FRAME32k )
     159             :     {
     160    10157409 :         st->gamma = GAMMA16k;
     161    10157409 :         st->preemph_fac = PREEMPH_FAC_SWB;
     162             :     }
     163             :     else
     164             :     {
     165     5885751 :         st->gamma = GAMMA16k;
     166     5885751 :         st->preemph_fac = PREEMPH_FAC_16k;
     167             :     }
     168             : 
     169    17835655 :     st->sr_core = st->L_frame * FRAMES_PER_SEC;
     170    17835655 :     st->encoderLookahead_enc = NS2SA( st->sr_core, ACELP_LOOK_NS );
     171    17835655 :     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    17835655 :     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     4319618 :         st->coder_type = TRANSITION;
     185             :     }
     186    13516037 :     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       59282 :         st->coder_type = TRANSITION;
     190             :     }
     191    13456755 :     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       93110 :         st->coder_type = TRANSITION;
     195             :     }
     196             :     /* select INACTIVE frames */
     197    13363645 :     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      531767 :         if ( !( st->Opt_SC_VBR && vad_flag_dtx ) && ( st->idchan == 0 || element_mode != IVAS_CPE_TD ) )
     202             :         {
     203      525657 :             st->coder_type = INACTIVE;
     204      525657 :             st->hGSCEnc->noise_lev = NOISE_LEVEL_SP3;
     205             :         }
     206             :     }
     207    12831878 :     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     1631678 :         st->coder_type = INACTIVE;
     211             :     }
     212             : 
     213             :     /*---------------------------------------------------------------------*
     214             :      * Decision matrix (selection of technologies)
     215             :      *---------------------------------------------------------------------*/
     216             : 
     217    17835655 :     st->mdct_sw = MODE1;
     218    17835655 :     st->mdct_sw_enable = MODE1;
     219    17835655 :     if ( ( st->total_brate <= MIN_BRATE_GSC_NOISY_FLAG || st->bwidth < SWB || st->flag_ACELP16k ) && st->GSC_IVAS_mode == 0 )
     220             :     {
     221    16909528 :         st->GSC_noisy_speech = 0;
     222             :     }
     223             : 
     224             :     /* core selection */
     225    17835655 :     ivas_decision_matrix_enc( st, element_brate, fft_buff, enerBuffer, last_element_mode );
     226             : 
     227    17835655 :     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    17835655 :     if ( st->core == TCX_20_CORE || st->core == HQ_CORE )
     233             :     {
     234    15880524 :         st->Nb_ACELP_frames = 0;
     235             :         /* Configure TCX with the same bitrate as given when (re-)initializing TCX */
     236    15880524 :         total_brate_tmp = st->total_brate;
     237    15880524 :         st->total_brate = st->bits_frame_nominal * FRAMES_PER_SEC;
     238    15880524 :         SetModeIndex( st, st->last_bits_frame_nominal * FRAMES_PER_SEC, last_element_mode, MCT_flag );
     239             : 
     240    15880524 :         st->sr_core = getCoreSamplerateMode2( element_mode, st->total_brate, st->bwidth, st->flag_ACELP16k, st->rf_mode, st->is_ism_format );
     241    15880524 :         st->total_brate = total_brate_tmp;
     242             : 
     243    15880524 :         st->L_frame = (int16_t) ( st->sr_core / FRAMES_PER_SEC );
     244    15880524 :         st->encoderLookahead_enc = NS2SA( st->sr_core, ACELP_LOOK_NS );
     245    15880524 :         st->encoderPastSamples_enc = ( st->L_frame * 9 ) >> 4;
     246             : 
     247    15880524 :         if ( st->sr_core == INT_FS_12k8 )
     248             :         {
     249      706857 :             st->preemph_fac = PREEMPH_FAC;
     250      706857 :             st->gamma = GAMMA1;
     251             :         }
     252    15173667 :         else if ( st->sr_core == INT_FS_16k )
     253             :         {
     254     3035853 :             st->preemph_fac = PREEMPH_FAC_16k;
     255     3035853 :             st->gamma = GAMMA16k;
     256             :         }
     257             :         else /* st->sr_core >=25600 */
     258             :         {
     259    12137814 :             st->preemph_fac = PREEMPH_FAC_SWB;
     260    12137814 :             st->gamma = GAMMA16k;
     261             :         }
     262             : 
     263    15880524 :         if ( st->vad_flag == 0 )
     264             :         {
     265     2688773 :             st->coder_type = INACTIVE;
     266             :         }
     267    13191751 :         else if ( st->coder_type > GENERIC )
     268             :         {
     269     3859268 :             st->coder_type = GENERIC;
     270             :         }
     271    15880524 :         if ( st->element_mode != IVAS_CPE_MDCT )
     272             :         {
     273     3142934 :             SetTCXModeInfo( st, st->hTranDet, &st->hTcxCfg->tcx_curr_overlap_mode );
     274             :         }
     275             :     }
     276     1955131 :     else if ( st->element_mode == IVAS_CPE_MDCT )
     277             :     {
     278       31156 :         st->hTcxEnc->tfm_mem = 0.75f;
     279             :     }
     280     1923975 :     else if ( element_brate != last_element_brate )
     281             :     {
     282         626 :         if ( st->core_brate != FRAME_NO_DATA )
     283             :         {
     284         619 :             SetModeIndex( st, st->bits_frame_nominal * FRAMES_PER_SEC, element_mode, MCT_flag );
     285             :         }
     286             : 
     287         626 :         if ( st->extl != -1 && st->extl != IGF_BWE && st->igf == 1 )
     288             :         {
     289         619 :             st->igf = 0;
     290             :         }
     291             :     }
     292             : 
     293             :     /*-----------------------------------------------------------------*
     294             :      * Update of ACELP harmonicity counter (used in ACELP transform codebook @32kbps)
     295             :      *-----------------------------------------------------------------*/
     296             : 
     297    17835655 :     if ( loc_harm == 1 && cor_map_sum > 50 && st->clas == VOICED_CLAS && st->coder_type == GENERIC )
     298             :     {
     299     2843007 :         st->last_harm_flag_acelp++;
     300             : 
     301     2843007 :         if ( st->last_harm_flag_acelp > 10 )
     302             :         {
     303      753791 :             st->last_harm_flag_acelp = 10;
     304             :         }
     305             :     }
     306             :     else
     307             :     {
     308    14992648 :         st->last_harm_flag_acelp = 0;
     309             :     }
     310             : 
     311             :     /*-----------------------------------------------------------------*
     312             :      * Update audio frames counter (used for UV decision)
     313             :      *-----------------------------------------------------------------*/
     314             : 
     315    17835655 :     if ( st->coder_type == AUDIO )
     316             :     {
     317       91470 :         st->audio_frame_cnt += AUDIO_COUNTER_STEP;
     318             :     }
     319    17744185 :     else if ( st->coder_type != INACTIVE )
     320             :     {
     321    14681670 :         st->audio_frame_cnt--;
     322             :     }
     323             : 
     324    17835655 :     if ( st->audio_frame_cnt > AUDIO_COUNTER_MAX )
     325             :     {
     326       23411 :         st->audio_frame_cnt = AUDIO_COUNTER_MAX;
     327             :     }
     328             : 
     329    17835655 :     if ( st->audio_frame_cnt < 0 )
     330             :     {
     331    10735724 :         st->audio_frame_cnt = 0;
     332             :     }
     333             : 
     334             :     /*-----------------------------------------------------------------*
     335             :      * Set formant sharpening flag
     336             :      *-----------------------------------------------------------------*/
     337             : 
     338    17835655 :     st->sharpFlag = 0;
     339             : 
     340    17835655 :     if ( st->coder_type == GENERIC || st->coder_type == VOICED || st->coder_type == TRANSITION )
     341             :     {
     342    14634408 :         if ( element_brate >= FRMT_SHP_MIN_BRATE_IVAS && st->lp_noise > FORMANT_SHARPENING_NOISE_THRESHOLD )
     343             :         {
     344     1079048 :             st->sharpFlag = 0;
     345             :         }
     346             :         else
     347             :         {
     348    13555360 :             st->sharpFlag = 1;
     349             :         }
     350             :     }
     351             : 
     352             :     /* TD stereo, secondary channel - due to lack of signaling bits, sharpFlag is always 1 */
     353    17835655 :     if ( element_mode == IVAS_CPE_TD && st->idchan == 1 )
     354             :     {
     355       31177 :         st->sharpFlag = 0;
     356       31177 :         if ( st->coder_type == GENERIC || st->coder_type == VOICED )
     357             :         {
     358       23825 :             st->sharpFlag = 1;
     359             :         }
     360             :     }
     361             : 
     362             :     /*-----------------------------------------------------------------*
     363             :      * Set voicing flag for HQ FEC
     364             :      *-----------------------------------------------------------------*/
     365             : 
     366    17835655 :     if ( st->sp_aud_decision1 == 0 && ( st->coder_type == VOICED || st->coder_type == GENERIC ) )
     367             :     {
     368     7233190 :         *Voicing_flag = 1;
     369             :     }
     370             :     else
     371             :     {
     372    10602465 :         *Voicing_flag = 0;
     373             :     }
     374             : 
     375             :     /*-----------------------------------------------------------------*
     376             :      * Compute core-coder buffers at internal sampling rate
     377             :      *-----------------------------------------------------------------*/
     378             : 
     379    17835655 :     sr_core_tmp = ( st->tcxonly == 0 ) ? INT_FS_16k : max( INT_FS_16k, st->sr_core ); /* indicates the ACELP sampling rate */
     380             : 
     381    17835655 :     L_look = NS2SA( sr_core_tmp, ACELP_LOOK_NS ); /* lookahead at other sampling rate (16kHz, 25.6kHz, 32kHz) */
     382             : 
     383    17835655 :     inp_16k = old_inp_16k + L_INP_MEM - L_look;
     384             : 
     385    17835655 :     if ( !flag_16k_smc )
     386             :     {
     387    17212482 :         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    17835655 :     if ( !( st->L_frame == L_FRAME16k && element_mode != IVAS_CPE_MDCT ) )
     391             :     {
     392             :         /* update signal buffers */
     393    15544430 :         mvr2r( new_inp_12k8, st->buf_speech_enc_pe + st->L_frame, L_FRAME );
     394    15544430 :         mvr2r( st->buf_speech_enc + L_FRAME32k, st->buf_speech_enc + st->L_frame, L_FRAME );
     395             : 
     396    15544430 :         if ( element_mode == IVAS_CPE_DFT )
     397             :         {
     398      421782 :             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    15122648 :         else if ( element_mode == IVAS_CPE_TD || element_mode == IVAS_CPE_MDCT )
     401             :         {
     402    12808118 :             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     2314530 :         else if ( element_mode == IVAS_SCE )
     405             :         {
     406     2314530 :             mvr2r( st->buf_speech_enc + L_FRAME32k - L_FILT, st->buf_speech_enc + st->L_frame - L_FILT, L_FILT );
     407             :         }
     408             : 
     409    15544430 :         if ( st->tcxonly == 0 )
     410             :         {
     411     1722161 :             mvr2r( wsp, st->wspeech_enc, L_FRAME + L_LOOK_12k8 );
     412             :         }
     413             :     }
     414             : 
     415    17835655 :     if ( flag_16k_smc )
     416             :     {
     417      623173 :         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    17835655 :     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    17835655 :     if ( st->L_frame == L_FRAME )
     429             :     {
     430     1792495 :         *inp = inp_12k8;
     431             :     }
     432             :     else
     433             :     {
     434    16043160 :         *inp = inp_16k;
     435             :     }
     436             : 
     437             :     /* Update VAD hangover frame counter in active frames */
     438    17835655 :     if ( !( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA ) && st->tcxonly == 0 )
     439             :     {
     440     3905603 :         if ( st->hTdCngEnc != NULL && st->Opt_DTX_ON && vad_hover_flag )
     441             :         {
     442       14011 :             st->hTdCngEnc->burst_ho_cnt++;
     443       14011 :             if ( st->hTdCngEnc->burst_ho_cnt > HO_HIST_SIZE )
     444             :             {
     445        1278 :                 st->hTdCngEnc->burst_ho_cnt = HO_HIST_SIZE;
     446             :             }
     447             :         }
     448     3891592 :         else if ( st->hTdCngEnc != NULL && vad_flag_dtx )
     449             :         {
     450      670705 :             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    17835655 :     pop_wmops();
     460    17835655 :     return;
     461             : }
     462             : 
     463             : 
     464             : /*-------------------------------------------------------------------*
     465             :  * ivas_compute_core_buffers()
     466             :  *
     467             :  * Compute core-coder buffers at internal sampling rate
     468             :  *--------------------------------------------------------------------*/
     469             : 
     470    17835655 : 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    17835655 :     signal_in = st->input;
     496             : 
     497    17835655 :     input_Fs = st->input_Fs;
     498    17835655 :     element_mode = st->element_mode;
     499             : 
     500    17835655 :     lMemRecalc_16k = 0;
     501    17835655 :     lMemRecalc = 0;
     502    17835655 :     if ( element_mode == IVAS_CPE_TD )
     503             :     {
     504       62354 :         lMemRecalc_16k = NS2SA( INT_FS_16k, L_MEM_RECALC_NS );
     505       62354 :         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    17835655 :     L_frame_tmp = ( st->tcxonly == 0 ) ? L_FRAME16k : max( L_FRAME16k, st->L_frame );
     513             : 
     514    17835655 :     L_look = NS2SA( sr_core, ACELP_LOOK_NS ); /* lookahead at other sampling rate (16kHz, 25.6kHz, 32kHz) */
     515             : 
     516    17835655 :     new_inp_16k = old_inp_16k + L_INP_MEM; /* pointer to new samples of the input signal in 16kHz core */
     517    17835655 :     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    17835655 :     if ( element_mode == IVAS_CPE_TD || element_mode == IVAS_SCE )
     521             :     {
     522     4154120 :         new_inp_16k -= NS2SA( sr_core, DELAY_FIR_RESAMPL_NS );
     523             :     }
     524             : 
     525    17835655 :     if ( element_mode == IVAS_CPE_DFT )
     526             :     {
     527      912789 :         mvr2r( st->old_inp_16k, old_inp_16k, L_INP_MEM - STEREO_DFT_OVL_16k );
     528             :     }
     529    16922866 :     else if ( element_mode == IVAS_CPE_TD )
     530             :     {
     531       62354 :         mvr2r( st->old_inp_16k, old_inp_16k, L_INP_MEM - L_MEM_RECALC_16K - L_FILT16k );
     532             :     }
     533             :     else
     534             :     {
     535    16860512 :         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    17835655 :     if ( element_mode == IVAS_SCE )
     543             :     {
     544     4091766 :         if ( input_Fs == sr_core )
     545             :         {
     546             :             /* no resampling needed, only delay adjustment to account for the FIR resampling delay */
     547      230120 :             delay = NS2SA( input_Fs, DELAY_FIR_RESAMPL_NS );
     548      230120 :             mvr2r( st->mem_decim16k + delay, new_inp_16k, delay );
     549      230120 :             mvr2r( signal_in, new_inp_16k + delay, input_frame );
     550      230120 :             mvr2r( signal_in + input_frame - 2 * delay, st->mem_decim16k, 2 * delay );
     551             :         }
     552     3861646 :         else if ( input_Fs == 32000 || input_Fs == 48000 )
     553             :         {
     554     3861646 :             modify_Fs( signal_in, input_frame, input_Fs, new_inp_16k, sr_core, st->mem_decim16k, 0 );
     555             : 
     556     3861646 :             mvr2r( st->mem_decim16k, mem_decim16k_dummy, 2 * L_FILT_MAX );
     557     3861646 :             set_f( temp1F_icatdmResampBuf, 0, L_FILT_MAX );
     558     3861646 :             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    13743889 :     else if ( element_mode == IVAS_CPE_TD )
     562             :     {
     563       62354 :         if ( input_Fs == sr_core )
     564             :         {
     565             :             /* no resampling needed, only delay adjustment to account for the FIR resampling delay */
     566        5294 :             delay = NS2SA( input_Fs, DELAY_FIR_RESAMPL_NS );
     567        5294 :             mvr2r( st->mem_decim16k + delay, new_inp_16k - lMemRecalc - delay + L_FILT16k, delay );
     568        5294 :             mvr2r( signal_in - lMemRecalc, new_inp_16k - lMemRecalc + L_FILT16k, input_frame + lMemRecalc );
     569        5294 :             mvr2r( signal_in + input_frame - lMemRecalc - 2 * delay, st->mem_decim16k, 2 * delay );
     570             :         }
     571       57060 :         else if ( input_Fs == 32000 || input_Fs == 48000 )
     572             :         {
     573             :             /* reconstruct past segment of input signal when switching from MDCT stereo */
     574       57060 :             if ( last_element_mode == IVAS_CPE_MDCT /*|| st->idchan == 1*/ )
     575             :             {
     576         484 :                 int16_t length_inp = NS2SA( input_Fs, L_MEM_RECALC_SCH_NS - DELAY_FIR_RESAMPL_NS );
     577         484 :                 int16_t length_16k = NS2SA( INT_FS_16k, L_MEM_RECALC_SCH_NS - DELAY_FIR_RESAMPL_NS );
     578             : 
     579         484 :                 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         484 :                 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       57060 :             modify_Fs( signal_in - lMemRecalc, input_frame, input_Fs, new_inp_16k - lMemRecalc_16k, sr_core, st->mem_decim16k, 0 );
     584       57060 :             mvr2r( st->mem_decim16k, mem_decim16k_dummy, 2 * L_FILT_MAX );
     585             : 
     586       57060 :             if ( lMemRecalc > 0 )
     587             :             {
     588       57060 :                 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       57060 :             set_f( temp1F_icatdmResampBuf, 0, L_FILT_MAX );
     592       57060 :             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    13681535 :     else if ( st->idchan == 0 )
     596             :     {
     597             :         /* update the FIR resampling filter memory, needed for switching to time-domain (FIR) resampling */
     598     7297162 :         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    17835655 :     if ( sr_core == INT_FS_16k && element_mode != IVAS_CPE_MDCT )
     606             :     {
     607     4083697 :         delay = NS2SA( INT_FS_16k, DELAY_FD_BWE_ENC_12k8_NS );
     608             : 
     609     4083697 :         if ( element_mode == IVAS_CPE_DFT )
     610             :         {
     611             :             /* save input resampled at 16kHz, non-preemphasised.*/
     612      912789 :             mvr2r( new_inp_16k, new_inp_resamp16k, L_FRAME16k );
     613             : 
     614      912789 :             if ( st->bwidth == WB )
     615             :             {
     616      189063 :                 mvr2r( new_inp_16k - delay, st->hBWE_FD->old_input_wb, delay );
     617      189063 :                 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     3170908 :         else if ( element_mode == IVAS_CPE_TD )
     621             :         {
     622             :             /* save input resampled at 16kHz, non-preemphasised */
     623       62354 :             mvr2r( new_inp_16k + L_FILT16k, new_inp_resamp16k, L_FRAME16k );
     624             : 
     625       62354 :             if ( st->bwidth == WB && st->hBWE_FD != NULL )
     626             :             {
     627        5819 :                 mvr2r( new_inp_16k + L_FILT16k - delay, st->hBWE_FD->old_input_wb, delay );
     628        5819 :                 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     3108554 :         else if ( element_mode == IVAS_SCE )
     632             :         {
     633             :             /* save input resampled at 16kHz, non-preemphasised */
     634     3108554 :             mvr2r( new_inp_16k + L_FILT16k, new_inp_resamp16k, L_FRAME16k );
     635             : 
     636     3108554 :             if ( st->bwidth == WB )
     637             :             {
     638      630566 :                 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    13751958 :     else if ( sr_core > INT_FS_16k )
     644             :     {
     645             :         /* reset the buffer, the signal is needed for WB BWEs */
     646    12137814 :         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    17835655 :     if ( st->tcxonly == 0 && !( ( element_mode == IVAS_CPE_TD && st->idchan == 1 ) || element_mode == IVAS_CPE_MDCT ) )
     654             :     {
     655     3942909 :         st->mem_preemph_enc = new_inp_16k[L_frame_tmp - 1];
     656             :     }
     657             : 
     658    17835655 :     if ( sr_core == INT_FS_16k && element_mode != IVAS_CPE_MDCT )
     659             :     {
     660     4083697 :         if ( element_mode == IVAS_CPE_DFT )
     661             :         {
     662      912789 :             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      912789 :             st->mem_preemph16k = st->mem_preemph16k_DFT;
     665      912789 :             st->mem_preemph16k_DFT = old_inp_16k[L_INP_MEM - STEREO_DFT_OVL_16k + L_FRAME16k - 1];
     666             : 
     667      912789 :             if ( st->L_frame == L_FRAME16k )
     668             :             {
     669      491007 :                 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      912789 :             preemph( new_inp_16k - STEREO_DFT_OVL_16k, PREEMPH_FAC_16k, L_FRAME16k, &( st->mem_preemph16k ) );
     672      912789 :             tmp = st->mem_preemph16k;
     673      912789 :             preemph( new_inp_16k - STEREO_DFT_OVL_16k + L_FRAME16k, PREEMPH_FAC_16k, STEREO_DFT_OVL_16k, &tmp );
     674             :         }
     675     3170908 :         else if ( st->element_mode == IVAS_CPE_TD )
     676             :         {
     677       62354 :             if ( last_element_mode == IVAS_CPE_DFT )
     678             :             {
     679         902 :                 st->mem_preemph16k = st->mem_preemph16k_DFT;
     680         902 :                 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         902 :                 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       62354 :             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       62354 :             if ( last_element_mode == IVAS_CPE_MDCT )
     688             :             {
     689         484 :                 int16_t length_16k = NS2SA( INT_FS_16k, L_MEM_RECALC_SCH_NS - DELAY_FIR_RESAMPL_NS );
     690         484 :                 preemph( new_inp_16k - lMemRecalc_16k - length_16k, PREEMPH_FAC, length_16k, &st->mem_preemph16k );
     691             :             }
     692             : 
     693       62354 :             if ( st->L_frame == L_FRAME16k )
     694             :             {
     695       22982 :                 mvr2r( new_inp_16k - lMemRecalc_16k, st->buf_speech_enc + L_FRAME16k - lMemRecalc_16k - L_FILT16k, L_FRAME16k + lMemRecalc_16k + L_FILT16k );
     696             :             }
     697       62354 :             preemph( new_inp_16k - lMemRecalc_16k, PREEMPH_FAC_16k, L_FRAME16k, &( st->mem_preemph16k ) );
     698       62354 :             tmp = st->mem_preemph16k;
     699       62354 :             preemph( new_inp_16k - lMemRecalc_16k + L_FRAME16k, PREEMPH_FAC_16k, lMemRecalc_16k + L_FILT16k, &tmp );
     700             :         }
     701     3108554 :         else if ( element_mode == IVAS_SCE )
     702             :         {
     703     3108554 :             preemph( new_inp_16k, PREEMPH_FAC_16k, L_FRAME16k, &( st->mem_preemph16k ) );
     704     3108554 :             tmp = st->mem_preemph16k;
     705     3108554 :             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    13751958 :         if ( element_mode == IVAS_CPE_MDCT )
     711             :         {
     712    12768746 :             st->mem_preemph16k = 0;
     713             :         }
     714             :         else /* SCE */
     715             :         {
     716      983212 :             st->mem_preemph16k = new_inp_16k[L_frame_tmp - 1];
     717             :         }
     718    13751958 :         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    17835655 :     if ( st->L_frame == L_FRAME16k && element_mode != IVAS_CPE_MDCT )
     727             :     {
     728             :         /* update signal buffers */
     729     2291225 :         if ( element_mode == IVAS_CPE_DFT )
     730             :         {
     731      491007 :             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     1800218 :         else if ( element_mode == IVAS_CPE_TD )
     734             :         {
     735       22982 :             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     1777236 :             mvr2r( new_inp_resamp16k, st->buf_speech_enc + L_FRAME16k, L_FRAME16k );
     740     1777236 :             mvr2r( new_inp_16k, st->buf_speech_enc_pe + L_FRAME16k, L_FRAME16k );
     741             :         }
     742             : 
     743             :         /*--------------------------------------------------------------*
     744             :          * LPC analysis
     745             :          *---------------------------------------------------------------*/
     746             : 
     747     2291225 :         if ( st->last_L_frame == L_FRAME )
     748             :         {
     749             :             /* this is just an approximation, but it is sufficient */
     750       18366 :             mvr2r( st->lsp_old1, st->lspold_enc, M );
     751             :         }
     752             : 
     753     2291225 :         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     2291225 :         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    17835655 :     if ( ( element_mode == IVAS_CPE_TD && st->idchan == 1 ) || element_mode == IVAS_CPE_MDCT )
     768             :     {
     769    12799923 :         set_f( st->old_inp_16k, 0, L_INP_MEM );
     770             :     }
     771     5035732 :     else if ( sr_core == INT_FS_16k )
     772             :     {
     773     4052520 :         mvr2r( &old_inp_16k[L_frame_tmp], st->old_inp_16k, L_INP_MEM );
     774             :     }
     775             :     else
     776             :     {
     777      983212 :         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    17835655 :     if ( inp16k_out != NULL )
     781             :     {
     782    17212482 :         *inp16k_out = inp_16k;
     783             :     }
     784             : 
     785    17835655 :     if ( new_inp_resamp16k_out != NULL )
     786             :     {
     787    17212482 :         mvr2r( new_inp_resamp16k, new_inp_resamp16k_out, L_FRAME16k );
     788             :     }
     789             : 
     790    17835655 :     return;
     791             : }

Generated by: LCOV version 1.14