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

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include "cnst.h"
      43             : #include "prot.h"
      44             : #include "ivas_prot.h"
      45             : #include "rom_com.h"
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*-----------------------------------------------------------------------*
      49             :  * Local function prototypes
      50             :  *-----------------------------------------------------------------------*/
      51             : 
      52             : static void init_tcx( Encoder_State *st, const int16_t L_frame_old, const int32_t total_brate, const int32_t last_total_brate, const int16_t MCT_flag );
      53             : static void init_sig_buffers( Encoder_State *st, const int16_t L_frame_old, const int16_t L_subfr, const int32_t total_brate, const int32_t last_total_brate );
      54             : static void init_core_sig_ana( Encoder_State *st );
      55             : static void init_acelp( Encoder_State *st, const int16_t L_frame_old, const int32_t total_brate, const int32_t last_total_brate );
      56             : static void init_modes( Encoder_State *st, const int32_t last_total_brate );
      57             : 
      58             : /*-----------------------------------------------------------------------*
      59             :  * init_coder_ace_plus()
      60             :  *
      61             :  * Initialization of state variables
      62             :  *-----------------------------------------------------------------------*/
      63             : 
      64       41416 : void init_coder_ace_plus(
      65             :     Encoder_State *st,              /* i  : Encoder state                          */
      66             :     const int32_t last_total_brate, /* i  : last total bitrate                     */
      67             :     const int32_t igf_brate,        /* i  : IGF configuration bitrate              */
      68             :     const int16_t MCT_flag          /* i  : hMCT handle allocated (1) or not (0)   */
      69             : )
      70             : {
      71       41416 :     TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
      72             :     int16_t L_frame_old; /*keep old frame size for switching */
      73             :     int16_t L_subfr;
      74             : 
      75             :     /* Bitrate */
      76       41416 :     st->tcxonly = getTcxonly( st->element_mode, st->total_brate, MCT_flag, st->is_ism_format );
      77             : 
      78             :     /* Core Sampling Rate */
      79       41416 :     st->sr_core = getCoreSamplerateMode2( st->element_mode, st->total_brate, st->bwidth, st->flag_ACELP16k, st->rf_mode, st->is_ism_format );
      80       41416 :     st->fscale = sr2fscale( st->sr_core );
      81             : 
      82             :     /* Narrowband? */
      83       41416 :     st->narrowBand = ( st->bwidth == NB ) ? 1 : 0;
      84             : 
      85             :     /* Core Framing */
      86       41416 :     L_frame_old = st->last_L_frame;
      87       41416 :     st->L_frame = (int16_t) ( st->sr_core / FRAMES_PER_SEC );
      88       41416 :     st->L_frame_past = -1;
      89             : 
      90       41416 :     if ( hTcxEnc != NULL )
      91             :     {
      92       41317 :         hTcxEnc->L_frameTCX = (int16_t) ( st->input_Fs / FRAMES_PER_SEC );
      93             : 
      94       41317 :         if ( st->ini_frame == 0 )
      95             :         {
      96       15589 :             set_zero( hTcxEnc->Txnq, L_FRAME32k / 2 + 64 );
      97       15589 :             hTcxEnc->acelp_zir = hTcxEnc->Txnq + L_FRAME / 2;
      98       15589 :             hTcxEnc->tcx_target_bits_fac = 1.0f;
      99             :         }
     100             :     }
     101             : 
     102       41416 :     if ( ( st->element_mode == EVS_MONO && st->L_frame == L_FRAME16k && st->total_brate <= ACELP_32k ) || ( st->element_mode > EVS_MONO && st->L_frame == L_FRAME16k && st->total_brate <= MAX_ACELP_BRATE ) )
     103             :     {
     104       15103 :         st->nb_subfr = NB_SUBFR16k;
     105             :     }
     106             :     else
     107             :     {
     108       26313 :         st->nb_subfr = NB_SUBFR;
     109             :     }
     110       41416 :     L_subfr = st->L_frame / st->nb_subfr;
     111             : 
     112             :     /* Core Lookahead */
     113       41416 :     st->encoderLookahead_enc = NS2SA( st->sr_core, ACELP_LOOK_NS );
     114       41416 :     st->encoderLookahead_FB = NS2SA( st->input_Fs, ACELP_LOOK_NS );
     115             : 
     116       41416 :     if ( st->ini_frame == 0 )
     117             :     {
     118       15688 :         st->acelpFramesCount = 0;
     119       15688 :         st->prevTempFlatness = 1.0f;
     120             :     }
     121             : 
     122             :     /* Initialize TBE */
     123       41416 :     if ( st->hBWE_TD != NULL )
     124             :     {
     125       22255 :         st->hBWE_TD->prev_coder_type = GENERIC;
     126       22255 :         set_f( st->hBWE_TD->prev_lsf_diff, 0.5f, LPC_SHB_ORDER - 2 );
     127       22255 :         st->hBWE_TD->prev_tilt_para = 0.0f;
     128       22255 :         set_zero( st->hBWE_TD->cur_sub_Aq, M + 1 );
     129             :     }
     130             : 
     131       41416 :     st->currEnergyHF = 0;
     132             : 
     133             :     /* Initialize LPC analysis/quantization */
     134       41416 :     if ( st->sr_core <= INT_FS_16k && st->tcxonly == 0 )
     135             :     {
     136       20931 :         st->lpcQuantization = 1;
     137             :     }
     138             :     else
     139             :     {
     140       20485 :         st->lpcQuantization = 0;
     141             :     }
     142             : 
     143       41416 :     st->next_force_safety_net = 0;
     144       41416 :     if ( ( st->last_L_frame != st->L_frame ) || ( st->last_core == AMR_WB_CORE ) || ( st->last_core == HQ_CORE ) )
     145             :     {
     146       24844 :         set_f( st->mem_MA, 0.0f, M );
     147       24844 :         mvr2r( GEWB_Ave, st->mem_AR, M );
     148             :     }
     149             : 
     150             :     /* Initialize IGF */
     151       41416 :     if ( st->hIGFEnc != NULL )
     152             :     {
     153       34060 :         st->hIGFEnc->infoStopFrequency = -1;
     154             :     }
     155             : 
     156       41416 :     if ( st->igf && st->hIGFEnc != NULL )
     157             :     {
     158       33999 :         IGFEncSetMode( st->hIGFEnc, igf_brate, st->bwidth, st->element_mode, st->rf_mode );
     159             :     }
     160        7417 :     else if ( st->hIGFEnc != NULL )
     161             :     {
     162          61 :         st->hIGFEnc->infoTotalBitsWritten = 0;
     163          61 :         st->hIGFEnc->infoTotalBitsPerFrameWritten = 0;
     164             :     }
     165             : 
     166             :     /* Initialize Core Signal Analysis Module */
     167       41416 :     init_core_sig_ana( st );
     168             : 
     169             :     /* Initialize TCX */
     170       41416 :     if ( hTcxEnc != NULL )
     171             :     {
     172       41317 :         init_tcx( st, L_frame_old, st->total_brate, last_total_brate, MCT_flag );
     173             :     }
     174             : 
     175             :     /* Initialize Signal Buffers */
     176       41416 :     init_sig_buffers( st, L_frame_old, L_subfr, st->total_brate, last_total_brate );
     177             : 
     178             :     /* Initialize ACELP */
     179       41416 :     init_acelp( st, L_frame_old, st->total_brate, last_total_brate );
     180             : 
     181       41416 :     if ( st->ini_frame == 0 )
     182             :     {
     183       15688 :         st->tec_tfa = 0;
     184             :     }
     185             : 
     186       41416 :     if ( st->hTECEnc != NULL )
     187             :     {
     188         101 :         resetTecEnc( st->hTECEnc, st->tec_tfa );
     189             :     }
     190             : 
     191       41416 :     if ( st->bwidth == SWB && ( st->total_brate == ACELP_16k40 || st->total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO )
     192             :     {
     193          75 :         st->tec_tfa = 1;
     194             :     }
     195             :     else
     196             :     {
     197       41341 :         st->tec_tfa = 0;
     198             :     }
     199             : 
     200       41416 :     st->tec_flag = 0;
     201       41416 :     st->tfa_flag = 0;
     202             : 
     203             : 
     204       41416 :     if ( ( st->total_brate == ACELP_9k60 || st->total_brate == ACELP_16k40 || st->total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO )
     205             :     {
     206          75 :         st->glr = 1;
     207             :     }
     208             :     else
     209             :     {
     210       41341 :         st->glr = 0;
     211             :     }
     212             : 
     213       41416 :     st->glr_reset = 0;
     214             : 
     215             :     /* Initialize ACELP/TCX Modes */
     216       41416 :     init_modes( st, last_total_brate );
     217             : 
     218             :     /* Adaptive BPF */
     219       41416 :     set_zero( st->mem_bpf, 2 * L_FILT16k );
     220       41416 :     set_zero( st->mem_error_bpf, 2 * L_FILT16k );
     221             : 
     222       41416 :     if ( st->total_brate >= HQ_48k )
     223             :     {
     224       18257 :         st->enablePlcWaveadjust = 1;
     225             :     }
     226             :     else
     227             :     {
     228       23159 :         st->enablePlcWaveadjust = 0;
     229             :     }
     230             : 
     231       41416 :     if ( st->hPlcExt )
     232             :     {
     233         101 :         init_PLC_enc( st->hPlcExt, st->sr_core );
     234             :     }
     235             : 
     236       41416 :     st->glr_idx[0] = 0;
     237       41416 :     st->glr_idx[1] = 0;
     238       41416 :     st->mean_gc[0] = 0.0f;
     239       41416 :     st->mean_gc[1] = 0.0f;
     240       41416 :     st->prev_lsf4_mean = 0.0f;
     241       41416 :     st->last_stab_fac = 0.0f;
     242             : 
     243       41416 :     return;
     244             : }
     245             : 
     246             : /*-----------------------------------------------------------------------*
     247             :  * init_tcx()
     248             :  *
     249             :  * Initialization of TCX
     250             :  *-----------------------------------------------------------------------*/
     251             : 
     252       41317 : static void init_tcx(
     253             :     Encoder_State *st,
     254             :     const int16_t L_frame_old,
     255             :     const int32_t total_brate,
     256             :     const int32_t last_total_brate,
     257             :     const int16_t MCT_flag )
     258             : {
     259       41317 :     TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
     260             : 
     261             :     /* Share the memories for 2xTCX10/4xTCX5 and for TCX20 */
     262       41317 :     hTcxEnc->spectrum[0] = hTcxEnc->spectrum_long;
     263       41317 :     hTcxEnc->spectrum[1] = hTcxEnc->spectrum_long + N_TCX10_MAX;
     264             : 
     265       41317 :     init_tcx_cfg( st->hTcxCfg, total_brate, st->sr_core, st->input_Fs, st->L_frame, st->bwidth, hTcxEnc->L_frameTCX, st->fscale, st->encoderLookahead_enc, st->encoderLookahead_FB, st->preemph_fac, st->tcxonly, st->rf_mode, st->igf, st->hIGFEnc != NULL ? st->hIGFEnc->infoStopFrequency : 0, st->element_mode, st->ini_frame, MCT_flag );
     266             : 
     267             :     /* Init TCX target bits correction factor */
     268       41317 :     hTcxEnc->tcx_target_bits_fac = 1.0f;
     269       41317 :     hTcxEnc->measuredBwRatio = 1.0f;
     270       41317 :     hTcxEnc->noiseTiltFactor = 0.5625f;
     271       41317 :     hTcxEnc->noiseLevelMemory_cnt = 0;
     272             : 
     273       41317 :     set_f( hTcxEnc->ltpGainMemory, 0.0f, N_LTP_GAIN_MEMS );
     274       41317 :     set_s( hTcxEnc->memQuantZeros, 0, L_FRAME_PLUS );
     275             : 
     276             :     /* TCX-LTP */
     277       41317 :     hTcxEnc->tcxltp = getTcxLtp( st->sr_core );
     278             : 
     279       41317 :     if ( st->ini_frame == 0 )
     280             :     {
     281       15589 :         hTcxEnc->tcxltp_pitch_int_past = st->L_frame;
     282       15589 :         hTcxEnc->tcxltp_pitch_fr_past = 0;
     283       15589 :         hTcxEnc->tcxltp_gain_past = 0.f;
     284       15589 :         hTcxEnc->tcxltp_norm_corr_past = 0.f;
     285       15589 :         hTcxEnc->tcxltp_norm_corr_mem = 0.f;
     286       15589 :         hTcxEnc->kernel_switch_corr_past = 0.f;
     287       15589 :         hTcxEnc->kernel_symmetry_past = 0; /* MDCT_IV & 1 */
     288       15589 :         hTcxEnc->enc_ste_pre_corr_past = 0;
     289       15589 :         hTcxEnc->tfm_mem = 0.75f;
     290       15589 :         hTcxEnc->tcxltp_on_mem = 0;
     291             :     }
     292       25728 :     else if ( st->L_frame != L_frame_old && !( ( total_brate >= ACELP_16k40 && total_brate <= ACELP_24k40 ) &&
     293          20 :                                                ( total_brate == last_total_brate ) && ( st->last_bwidth == st->bwidth ) ) )
     294             :     {
     295             :         int16_t pitres, pitres_old;
     296             :         float pit, pit_old;
     297             : 
     298        5893 :         if ( L_frame_old % 160 == 0 )
     299             :         {
     300        3005 :             pitres_old = 6;
     301             :         }
     302             :         else
     303             :         {
     304        2888 :             pitres_old = 4;
     305             :         }
     306        5893 :         pit_old = (float) hTcxEnc->tcxltp_pitch_int_past + (float) hTcxEnc->tcxltp_pitch_fr_past / (float) pitres_old;
     307        5893 :         if ( st->L_frame % 160 == 0 )
     308             :         {
     309        3384 :             pitres = 6;
     310             :         }
     311             :         else
     312             :         {
     313        2509 :             pitres = 4;
     314             :         }
     315        5893 :         pit = pit_old * (float) st->L_frame / (float) L_frame_old;
     316        5893 :         hTcxEnc->tcxltp_pitch_int_past = (int16_t) pit;
     317        5893 :         hTcxEnc->tcxltp_pitch_fr_past = (int16_t) ( ( pit - (float) hTcxEnc->tcxltp_pitch_int_past ) * (float) pitres );
     318             :     }
     319             : 
     320       41317 :     hTcxEnc->tcx_lpc_shaped_ari = getTcxLpcShapedAri( total_brate, st->rf_mode, st->element_mode );
     321             : 
     322       41317 :     return;
     323             : }
     324             : 
     325             : /*-----------------------------------------------------------------------*
     326             :  * init_sig_buffers()
     327             :  *
     328             :  * Initialization of signal buffers
     329             :  *-----------------------------------------------------------------------*/
     330             : 
     331       41416 : static void init_sig_buffers(
     332             :     Encoder_State *st,
     333             :     const int16_t L_frame_old,
     334             :     const int16_t L_subfr,
     335             :     const int32_t total_brate,
     336             :     const int32_t last_total_brate )
     337             : {
     338       41416 :     TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
     339             : 
     340             :     /* Encoder Past Samples at encoder-sampling-rate */
     341       41416 :     st->encoderPastSamples_enc = ( st->L_frame * 9 ) / 16;
     342             : 
     343             :     /* Initialize Signal Buffers and Pointers at encoder-sampling-rate */
     344       41416 :     if ( st->ini_frame == 0 )
     345             :     {
     346       15688 :         set_zero( st->buf_speech_enc, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
     347       15688 :         set_zero( st->buf_speech_enc_pe, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
     348       15688 :         set_zero( st->buf_wspeech_enc, L_FRAME16k + L_SUBFR + L_FRAME16k + L_NEXT_MAX_16k + 320 );
     349       15688 :         if ( hTcxEnc != NULL )
     350             :         {
     351       15589 :             set_zero( hTcxEnc->buf_speech_ltp, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k );
     352             :         }
     353             :     }
     354       25728 :     else if ( st->L_frame != L_frame_old && !( ( total_brate >= ACELP_16k40 && total_brate <= ACELP_24k40 ) &&
     355          20 :                                                ( total_brate == last_total_brate ) && ( st->last_bwidth == st->bwidth ) ) )
     356             :     {
     357        5893 :         lerp( st->buf_speech_enc, st->buf_speech_enc, st->L_frame, L_frame_old );
     358             : 
     359        5893 :         if ( ( st->last_core != TCX_20_CORE ) && ( st->last_core != TCX_10_CORE ) )
     360             :         {
     361        1337 :             mvr2r( st->buf_speech_enc, hTcxEnc->buf_speech_ltp, st->L_frame );
     362             :         }
     363             : 
     364        5893 :         mvr2r( st->old_wsp, st->buf_wspeech_enc + st->L_frame + L_SUBFR - L_WSP_MEM, L_WSP_MEM );
     365             : 
     366             :         /*Resamp buffers needed only for ACELP*/
     367        5893 :         if ( st->L_frame == L_FRAME && !st->tcxonly )
     368             :         {
     369         341 :             mvr2r( st->old_inp_12k8, st->buf_speech_enc_pe + st->L_frame - L_INP_MEM, L_INP_MEM );
     370             :         }
     371        5552 :         else if ( st->L_frame == L_FRAME16k && !st->tcxonly )
     372             :         {
     373         768 :             lerp( st->buf_wspeech_enc + st->L_frame + L_SUBFR - L_WSP_MEM, st->buf_wspeech_enc + st->L_frame + L_SUBFR - 310, 310, L_WSP_MEM );
     374         768 :             mvr2r( st->old_inp_16k, st->buf_speech_enc_pe + st->L_frame - L_INP_MEM, L_INP_MEM );
     375             :         }
     376             : 
     377        5893 :         st->mem_preemph_enc = st->buf_speech_enc[st->L_frame - 1];
     378        5893 :         st->mem_wsp_enc = st->buf_wspeech_enc[st->L_frame + L_SUBFR - 1];
     379             :     }
     380       19835 :     else if ( !st->tcxonly && last_total_brate > ACELP_32k ) /* coming from TCXonly modes */
     381             :     {
     382        2070 :         mvr2r( st->old_wsp, st->buf_wspeech_enc + st->L_frame + L_SUBFR - L_WSP_MEM, L_WSP_MEM );
     383             : 
     384             :         /*Resamp buffers needed only for ACELP*/
     385        2070 :         if ( st->L_frame == L_FRAME16k )
     386             :         {
     387        2069 :             lerp( st->buf_wspeech_enc + st->L_frame + L_SUBFR - L_WSP_MEM, st->buf_wspeech_enc + st->L_frame + L_SUBFR - 310, 310, L_WSP_MEM );
     388             :         }
     389        2070 :         st->hLPDmem->mem_w0 = 0;
     390        2070 :         st->mem_wsp_enc = st->buf_wspeech_enc[st->L_frame + L_SUBFR - 1];
     391             :     }
     392             : 
     393       41416 :     st->new_speech_enc = st->buf_speech_enc + st->encoderPastSamples_enc + st->encoderLookahead_enc;
     394       41416 :     st->new_speech_enc_pe = st->buf_speech_enc_pe + st->encoderPastSamples_enc + st->encoderLookahead_enc;
     395             : 
     396       41416 :     if ( hTcxEnc != NULL )
     397             :     {
     398       41317 :         hTcxEnc->new_speech_ltp = hTcxEnc->buf_speech_ltp + st->encoderPastSamples_enc + st->encoderLookahead_enc;
     399             :     }
     400             : 
     401       41416 :     if ( st->hTcxEnc != NULL )
     402             :     {
     403       41317 :         st->hTcxEnc->new_speech_TCX = st->input_buff + st->input_Fs / FRAMES_PER_SEC; /* note: in EVS st->new_speech_TCX == st->input - 0.9375ms; in IVAS st->new_speech_TCX == st->input */
     404       41317 :         st->hTcxEnc->speech_TCX = st->hTcxEnc->new_speech_TCX - st->encoderLookahead_FB;
     405             :     }
     406             : 
     407       41416 :     st->speech_enc = st->buf_speech_enc + st->encoderPastSamples_enc;
     408       41416 :     st->speech_enc_pe = st->buf_speech_enc_pe + st->encoderPastSamples_enc;
     409             : 
     410       41416 :     if ( hTcxEnc != NULL )
     411             :     {
     412       41317 :         hTcxEnc->speech_ltp = hTcxEnc->buf_speech_ltp + st->encoderPastSamples_enc;
     413             :     }
     414             : 
     415       41416 :     if ( st->element_mode > EVS_MONO )
     416             :     {
     417       41315 :         st->wspeech_enc = st->buf_wspeech_enc + st->L_frame + L_SUBFR;
     418             :     }
     419             :     else
     420             :     {
     421         101 :         st->wspeech_enc = st->buf_wspeech_enc + st->L_frame + L_subfr;
     422             :     }
     423             : 
     424       41416 :     if ( st->ini_frame == 0 || st->L_frame != L_frame_old || st->last_codec_mode == MODE1 )
     425             :     {
     426       41416 :         set_zero( st->buf_synth, OLD_SYNTH_SIZE_ENC + L_FRAME32k );
     427             :     }
     428       41416 :     st->synth = st->buf_synth + st->L_frame + L_subfr;
     429             : 
     430       41416 :     return;
     431             : }
     432             : 
     433             : 
     434             : /*-----------------------------------------------------------------------*
     435             :  * init_core_sig_ana()
     436             :  *
     437             :  *
     438             :  *-----------------------------------------------------------------------*/
     439             : 
     440       41416 : static void init_core_sig_ana(
     441             :     Encoder_State *st )
     442             : {
     443             :     /* Pre-emphasis factor and memory */
     444       41416 :     if ( st->fscale < ( 16000 * FSCALE_DENOM ) / 12800 )
     445             :     {
     446        6096 :         st->preemph_fac = PREEMPH_FAC; /*WB*/
     447             :     }
     448       35320 :     else if ( st->fscale < ( 24000 * FSCALE_DENOM ) / 12800 )
     449             :     {
     450       15966 :         st->preemph_fac = PREEMPH_FAC_16k; /*WB*/
     451             :     }
     452             :     else
     453             :     {
     454       19354 :         st->preemph_fac = PREEMPH_FAC_SWB; /*SWB*/
     455             :     }
     456             : 
     457       41416 :     if ( st->sr_core == INT_FS_16k )
     458             :     {
     459       15966 :         st->gamma = GAMMA16k;
     460             :     }
     461             :     else
     462             :     {
     463       25450 :         st->gamma = GAMMA1;
     464             :     }
     465             : 
     466       41416 :     if ( st->narrowBand )
     467             :     {
     468           0 :         st->min_band = 1;
     469           0 :         st->max_band = 16;
     470             :     }
     471             :     else
     472             :     {
     473       41416 :         st->min_band = 0;
     474       41416 :         st->max_band = 19;
     475             :     }
     476             : 
     477       41416 :     return;
     478             : }
     479             : 
     480             : 
     481             : /*-----------------------------------------------------------------------*
     482             :  * init_acelp()
     483             :  *
     484             :  *
     485             :  *-----------------------------------------------------------------------*/
     486             : 
     487       41416 : static void init_acelp(
     488             :     Encoder_State *st,
     489             :     const int16_t L_frame_old,
     490             :     const int32_t total_brate,
     491             :     const int32_t last_total_brate )
     492             : {
     493             :     int16_t mem_syn_r_size_old;
     494             :     int16_t mem_syn_r_size_new;
     495             : 
     496       41416 :     LPD_state_HANDLE hLPDmem = st->hLPDmem;
     497             : 
     498             :     /* Init pitch lag */
     499       41416 :     if ( st->element_mode == IVAS_CPE_MDCT )
     500             :     {
     501             :         /* TCX LTP ana always runs @12.8kHz */
     502       19062 :         st->pit_res_max = initPitchLagParameters( 12800, &st->pit_min, &st->pit_fr1, &st->pit_fr1b, &st->pit_fr2, &st->pit_max );
     503             :     }
     504             :     else
     505             :     {
     506       22354 :         st->pit_res_max = initPitchLagParameters( st->sr_core, &st->pit_min, &st->pit_fr1, &st->pit_fr1b, &st->pit_fr2, &st->pit_max );
     507             :     }
     508             : 
     509             :     /* Init LPDmem */
     510       41416 :     if ( st->ini_frame == 0 )
     511             :     {
     512       15688 :         if ( hLPDmem != NULL )
     513             :         {
     514        3716 :             set_zero( hLPDmem->syn, 1 + M );
     515        3716 :             set_zero( hLPDmem->mem_syn_r, L_SYN_MEM );
     516             :         }
     517             : 
     518       15688 :         if ( st->hTcxEnc != NULL )
     519             :         {
     520       15589 :             set_zero( st->hTcxEnc->Txnq, L_FRAME32k / 2 + 64 );
     521       15589 :             st->hTcxEnc->acelp_zir = st->hTcxEnc->Txnq + ( st->L_frame / 2 );
     522             :         }
     523             :     }
     524             :     else /*Rate switching*/
     525             :     {
     526       25728 :         if ( st->hTcxEnc != NULL )
     527             :         {
     528       25728 :             if ( st->last_core == ACELP_CORE )
     529             :             {
     530        9044 :                 lerp( st->hTcxEnc->Txnq, st->hTcxEnc->Txnq, st->L_frame / 2, L_frame_old / 2 );
     531             :             }
     532             :             else
     533             :             {
     534       16684 :                 lerp( st->hTcxEnc->Txnq, st->hTcxEnc->Txnq, st->hTcxCfg->tcx_mdct_window_length, st->hTcxCfg->tcx_mdct_window_length_old );
     535             :             }
     536       25728 :             st->hTcxEnc->acelp_zir = st->hTcxEnc->Txnq + ( st->L_frame / 2 );
     537             :         }
     538             : 
     539             :         /* Rate switching */
     540       25728 :         if ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO )
     541             :         {
     542          98 :             if ( hLPDmem != NULL )
     543             :             {
     544          98 :                 mvr2r( hLPDmem->mem_syn1, hLPDmem->mem_syn2, M );
     545          98 :                 set_zero( hLPDmem->syn, M );
     546             :             }
     547             : 
     548          98 :             if ( st->hTcxEnc != NULL )
     549             :             {
     550          98 :                 set_zero( st->hTcxEnc->Txnq, L_FRAME32k / 2 + 64 );
     551             :             }
     552             :         }
     553             : 
     554       25728 :         if ( st->last_core == AMR_WB_CORE )
     555             :         {
     556           0 :             st->next_force_safety_net = 1;
     557           0 :             st->last_core = ACELP_CORE;
     558             :         }
     559             : 
     560       25728 :         if ( st->last_codec_mode == MODE1 && st->last_core == HQ_CORE )
     561             :         {
     562             :             /*Reset of ACELP memories*/
     563        7858 :             st->next_force_safety_net = 1;
     564        7858 :             st->rate_switching_reset = 1;
     565        7858 :             if ( hLPDmem != NULL )
     566             :             {
     567        7858 :                 hLPDmem->tilt_code = TILT_CODE;
     568        7858 :                 set_zero( hLPDmem->old_exc, L_EXC_MEM );
     569        7858 :                 set_zero( hLPDmem->syn, 1 + M );
     570        7858 :                 hLPDmem->mem_w0 = 0.0f;
     571        7858 :                 set_zero( hLPDmem->mem_syn, M );
     572        7858 :                 set_zero( hLPDmem->mem_syn2, M );
     573             :             }
     574             : 
     575             :             /* unquantized LPC*/
     576        7858 :             if ( !( ( total_brate >= ACELP_16k40 && total_brate <= ACELP_24k40 ) && total_brate == last_total_brate && st->last_bwidth == st->bwidth ) )
     577             :             {
     578        4361 :                 mvr2r( st->lsp_old1, st->lspold_enc, M ); /*lsp old @12.8kHz*/
     579        4361 :                 if ( st->L_frame == L_FRAME16k )
     580             :                 {
     581        4334 :                     lsp_convert_poly( st->lspold_enc, st->L_frame, 0 );
     582             :                 }
     583             :             }
     584        7858 :             mvr2r( st->lspold_enc, st->lsp_old, M ); /*used unquantized values for mid-LSF Q*/
     585        7858 :             lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core );
     586             : 
     587        7858 :             st->last_core = TCX_20_CORE;
     588             : 
     589        7858 :             st->hTcxCfg->last_aldo = 1; /*It was previously ALDO*/
     590        7858 :             st->hTcxCfg->tcx_curr_overlap_mode = ALDO_WINDOW;
     591             : 
     592             :             /*ALDO overlap windowed past: also used in MODE1 but for other MDCT-FB*/
     593        7858 :             set_f( st->hTcxEnc->old_out, 0, st->L_frame );
     594             :         }
     595             :         else
     596             :         {
     597       17870 :             if ( st->L_frame != L_frame_old && st->L_frame <= L_FRAME16k && L_frame_old <= L_FRAME16k ) /* Rate switching between 12.8 and 16 kHz*/
     598         537 :             {
     599             :                 float tmp, A[M + 1], Ap[M + 1], tmp_buf[M + 1];
     600             : 
     601             :                 /* convert quantized LSP vector */
     602         537 :                 st->rate_switching_reset = lsp_convert_poly( st->lsp_old, st->L_frame, 0 );
     603         537 :                 lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core );
     604             : 
     605         537 :                 if ( st->L_frame == L_FRAME16k )
     606             :                 {
     607         358 :                     mvr2r( st->lsp_old, st->lspold_enc, M );
     608             :                 }
     609             :                 else
     610             :                 {
     611         179 :                     mvr2r( st->lsp_old1, st->lspold_enc, M );
     612             :                 }
     613             : 
     614             :                 /* Update wsyn */
     615         537 :                 if ( hLPDmem != NULL )
     616             :                 {
     617         537 :                     synth_mem_updt2( st->L_frame, st->last_L_frame, hLPDmem->old_exc, hLPDmem->mem_syn_r, hLPDmem->mem_syn2, hLPDmem->mem_syn, ENC );
     618         537 :                     lsp2a_stab( st->lsp_old, A, M );
     619         537 :                     weight_a( A, Ap, GAMMA1, M );
     620         537 :                     tmp = 0.f;
     621         537 :                     tmp_buf[0] = 0.f;
     622         537 :                     mvr2r( hLPDmem->mem_syn2, tmp_buf + 1, M );
     623         537 :                     deemph( tmp_buf + 1, st->preemph_fac, M, &tmp );
     624         537 :                     residu( Ap, M, tmp_buf + M, &tmp, 1 );
     625         537 :                     hLPDmem->mem_w0 = st->wspeech_enc[-1] - tmp;
     626             :                 }
     627             :             }
     628       17333 :             else if ( st->L_frame != L_frame_old ) /* Rate switching involving TCX only modes */
     629             :             {
     630             :                 /*Partial reset of ACELP memories*/
     631        5341 :                 st->next_force_safety_net = 1;
     632        5341 :                 st->rate_switching_reset = 1;
     633             : 
     634             :                 /*reset partly some memories*/
     635        5341 :                 if ( hLPDmem != NULL )
     636             :                 {
     637        1172 :                     hLPDmem->tilt_code = TILT_CODE;
     638        1172 :                     set_zero( hLPDmem->old_exc, L_EXC_MEM );
     639             : 
     640             :                     /*Resamp others memories*/
     641             :                     /*Size of LPC syn memory*/
     642        1172 :                     mem_syn_r_size_old = (int16_t) ( 1.25 * L_frame_old / 20.f );
     643        1172 :                     mem_syn_r_size_new = (int16_t) ( 1.25 * st->L_frame / 20.f );
     644        1172 :                     lerp( hLPDmem->mem_syn_r + L_SYN_MEM - mem_syn_r_size_old, hLPDmem->mem_syn_r + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
     645        1172 :                     mvr2r( hLPDmem->mem_syn_r + L_SYN_MEM - M, hLPDmem->mem_syn, M );
     646        1172 :                     mvr2r( hLPDmem->mem_syn, hLPDmem->mem_syn2, M );
     647             : 
     648             :                     /*Untouched memories : LPDmem->syn & LPDmem->mem_w0*/
     649        1172 :                     hLPDmem->mem_w0 = 0;
     650             :                 }
     651             : 
     652             :                 /* unquantized LPC*/
     653        5341 :                 mvr2r( st->lsp_old1, st->lspold_enc, M ); /*lsp old @12.8kHz*/
     654        5341 :                 if ( st->L_frame == L_FRAME16k )
     655             :                 {
     656         980 :                     lsp_convert_poly( st->lspold_enc, st->L_frame, 0 );
     657             :                 }
     658        5341 :                 mvr2r( st->lspold_enc, st->lsp_old, M ); /*used unquantized values for mid-LSF Q*/
     659        5341 :                 lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core );
     660             :             }
     661             :             /* necessary in BASOP only, due to different representations of st->lsf_old */
     662             :             /* else if ( !st->tcxonly && (st->L_frame == L_FRAME16k) && (st->last_total_brate > ACELP_32k) ) */
     663             :             /* { */
     664             :             /*  lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core ); */
     665             :             /* } */
     666             :         }
     667             :     }
     668             : 
     669       41416 :     if ( st->last_bwidth == NB && st->bwidth != NB && st->ini_frame != 0 )
     670             :     {
     671           0 :         st->rate_switching_reset = 1;
     672             :     }
     673             : 
     674             :     /* Post-processing */
     675       41416 :     if ( hLPDmem != NULL )
     676             :     {
     677       22255 :         set_zero( hLPDmem->dispMem, 8 );
     678       22255 :         hLPDmem->gc_threshold = 0.0f;
     679             :     }
     680             : 
     681             :     /* Pulse Search configuration */
     682       41416 :     st->acelp_autocorr = 1;
     683             : 
     684             :     /*Use for 12.8 kHz sampling rate and low bitrates, the conventional pulse search->better SNR*/
     685       41416 :     if ( ( total_brate <= ACELP_9k60 || st->rf_mode == 1 ) && st->sr_core == INT_FS_12k8 )
     686             :     {
     687        1892 :         st->acelp_autocorr = 0;
     688             :     }
     689             : 
     690             :     /*BPF parameters for adjusting gain in function of background noise*/
     691       41416 :     if ( st->codec_mode == MODE2 )
     692             :     {
     693          99 :         st->pst_lp_ener = 0.0f;
     694          99 :         if ( st->last_codec_mode == MODE1 )
     695             :         {
     696          98 :             st->pst_mem_deemp_err = 0.0f;
     697             :         }
     698             :     }
     699             : 
     700             : 
     701       41416 :     return;
     702             : }
     703             : 
     704             : /*-----------------------------------------------------------------------*
     705             :  * init_modes()
     706             :  *
     707             :  *
     708             :  *-----------------------------------------------------------------------*/
     709             : 
     710       41416 : static void init_modes(
     711             :     Encoder_State *st,
     712             :     const int32_t last_total_brate )
     713             : {
     714             :     int16_t n;
     715             : 
     716             :     /* Restrict ACE/TCX20/TCX10 mode */
     717       41416 :     st->restrictedMode = getRestrictedMode( st->element_mode, st->total_brate, st->Opt_AMR_WB );
     718             : 
     719       41416 :     st->acelpEnabled = ( st->restrictedMode & 1 ) == 1;
     720       41416 :     st->tcx20Enabled = ( st->restrictedMode & 2 ) == 2;
     721       41416 :     st->tcx10Enabled = ( st->restrictedMode & 4 ) == 4;
     722             : 
     723             :     /* TCX mode (TCX20 TCX10_10 or NO_TCX) */
     724       41416 :     if ( st->hTcxEnc != NULL )
     725             :     {
     726       41317 :         st->hTcxEnc->tcxMode = NO_TCX;
     727             :     }
     728             : 
     729             :     /*st->bits_frame_nominal = (int16_t)( (float)st->L_frame * (float)FSCALE_DENOM * (float)st->total_brate / ( (float)st->fscale * 12800.0f ) );*/
     730       41416 :     st->bits_frame_nominal = (int16_t) ( (float) st->L_frame / (float) st->fscale * (float) FSCALE_DENOM / 128.0f * (float) st->total_brate / 100.0f + 0.49f );
     731             : 
     732       41416 :     if ( st->Opt_AMR_WB )
     733             :     {
     734           0 :         st->bits_frame = st->bits_frame_nominal;
     735           0 :         st->bits_frame_core = st->bits_frame_nominal;
     736           0 :         st->frame_size_index = 0;
     737             :     }
     738             :     else
     739             :     {
     740      379435 :         for ( n = 0; n < FRAME_SIZE_NB; n++ )
     741             :         {
     742             : 
     743      379435 :             if ( n < FRAME_SIZE_NB - 1 )
     744             :             {
     745      372739 :                 if ( FrameSizeConfig[n].frame_bits <= st->bits_frame_nominal && FrameSizeConfig[n + 1].frame_bits > st->bits_frame_nominal )
     746             :                 {
     747       34720 :                     st->frame_size_index = n;
     748       34720 :                     st->bits_frame = st->bits_frame_nominal;
     749       34720 :                     st->bits_frame_core = st->bits_frame_nominal - FrameSizeConfig[n].transmission_bits - FrameSizeConfig[n].bandwidth_bits - FrameSizeConfig[n].reserved_bits;
     750       34720 :                     break;
     751             :                 }
     752             :             }
     753             :             else
     754             :             {
     755        6696 :                 if ( FrameSizeConfig[n].frame_bits <= st->bits_frame_nominal )
     756             :                 {
     757        6696 :                     st->frame_size_index = n;
     758        6696 :                     st->bits_frame = st->bits_frame_nominal;
     759        6696 :                     st->bits_frame_core = st->bits_frame_nominal - FrameSizeConfig[n].transmission_bits - FrameSizeConfig[n].bandwidth_bits - FrameSizeConfig[n].reserved_bits;
     760        6696 :                     break;
     761             :                 }
     762             :             }
     763             :         }
     764             :     }
     765             : 
     766             :     /* Reconfigure core */
     767       41416 :     core_coder_reconfig( st, last_total_brate );
     768             : 
     769       41416 :     return;
     770             : }

Generated by: LCOV version 1.14