LCOV - code coverage report
Current view: top level - lib_enc - init_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 352 377 93.4 %
Date: 2025-05-28 04:28:20 Functions: 3 3 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 "rom_com.h"
      44             : #include "rom_enc.h"
      45             : #include "prot.h"
      46             : #include "ivas_prot.h"
      47             : #include "ivas_cnst.h"
      48             : #include "wmc_auto.h"
      49             : 
      50             : /*-----------------------------------------------------------------------*
      51             :  * init_encoder()
      52             :  *
      53             :  * Initialization of state variables
      54             :  *-----------------------------------------------------------------------*/
      55             : 
      56      130289 : ivas_error init_encoder(
      57             :     Encoder_State *st,               /* i/o: state structure                      */
      58             :     Encoder_Struct *st_ivas,         /* i/o: encoder state structure              */
      59             :     const int16_t idchan,            /* i  : channel ID                           */
      60             :     const int16_t var_SID_rate_flag, /* i  : flag for variable SID update rate    */
      61             :     const int16_t interval_SID,      /* i  : interval for SID update              */
      62             :     const int16_t vad_only_flag,     /* i  : flag to indicate front-VAD structure */
      63             :     const ISM_MODE ism_mode,         /* i  : ISM mode                             */
      64             :     const int32_t element_brate      /* i  : element bitrate                      */
      65             : )
      66             : {
      67             :     int16_t i;
      68             :     ivas_error error;
      69             :     int32_t igf_brate;
      70             : 
      71      130289 :     error = IVAS_ERR_OK;
      72             : 
      73             :     /*-----------------------------------------------------------------*
      74             :      * General parameters
      75             :      *-----------------------------------------------------------------*/
      76             : 
      77      130289 :     if ( st->Opt_AMR_WB )
      78             :     {
      79          48 :         st->last_core = AMR_WB_CORE;
      80             :     }
      81             :     else
      82             :     {
      83      130241 :         st->last_core = -1;
      84             :     }
      85             : 
      86      130289 :     st->L_frame = L_FRAME;
      87      130289 :     st->last_coder_type = GENERIC;
      88      130289 :     st->coder_type = GENERIC;
      89      130289 :     st->last_total_brate = st->total_brate;
      90      130289 :     st->last_bits_frame_nominal = -1;
      91      130289 :     st->last_total_brate_cng = -1;
      92      130289 :     st->last_core_brate = st->total_brate;
      93      130289 :     st->dtx_sce_sba = 0;
      94      130289 :     st->extl = -1;
      95      130289 :     st->last_extl = -1;
      96      130289 :     st->last_L_frame = L_FRAME;
      97      130289 :     st->rate_switching_reset = 0;
      98      130289 :     st->rate_switching_reset_16kHz = 0;
      99      130289 :     st->clas = UNVOICED_CLAS;
     100      130289 :     st->low_rate_mode = 0;
     101      130289 :     st->ini_frame = 0;
     102      130289 :     st->inactive_coder_type_flag = 0;
     103      130289 :     st->sba_br_sw_while_no_data = 0;
     104             : 
     105      130289 :     st->coder_type_raw = VOICED;
     106      130289 :     st->last_coder_type_raw = st->coder_type_raw;
     107             : 
     108      130289 :     st->flag_ACELP16k = set_ACELP_flag( st->element_mode, st->total_brate, st->total_brate, idchan, 0, -1, -1 );
     109             : 
     110      130289 :     st->is_ism_format = 0;
     111      130289 :     if ( ism_mode != ISM_MODE_NONE )
     112             :     {
     113       47513 :         st->is_ism_format = 1;
     114             :     }
     115             : 
     116             :     /*-----------------------------------------------------------------*
     117             :      * Bitstream
     118             :      *-----------------------------------------------------------------*/
     119             : 
     120      130289 :     if ( !vad_only_flag )
     121             :     {
     122      130040 :         if ( ( st->hBstr = (BSTR_ENC_HANDLE) malloc( sizeof( BSTR_ENC_DATA ) ) ) == NULL )
     123             :         {
     124           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Bitstream structure\n" ) );
     125             :         }
     126             : 
     127             :         /* set pointer to the buffer of indices */
     128      130040 :         st->hBstr->ind_list = st_ivas->ind_list;
     129      130040 :         st->hBstr->ivas_ind_list_zero = &st_ivas->ind_list;
     130      130040 :         st->hBstr->ivas_max_num_indices = &st_ivas->ivas_max_num_indices;
     131      130040 :         st->hBstr->nb_ind_tot = 0;
     132      130040 :         st->hBstr->nb_bits_tot = 0;
     133      130040 :         st->hBstr->st_ivas = st_ivas;
     134             :     }
     135             :     else
     136             :     {
     137         249 :         st->hBstr = NULL;
     138             :     }
     139             : 
     140             :     /*-----------------------------------------------------------------*
     141             :      * Pre-processing and ACELP core parameters
     142             :      *-----------------------------------------------------------------*/
     143             : 
     144      130289 :     mvr2r( GEWB_Ave, st->mem_AR, M );
     145      130289 :     mvr2r( GEWB_Ave, st->lsfoldbfi0, M );
     146      130289 :     mvr2r( GEWB_Ave, st->lsfoldbfi1, M );
     147      130289 :     mvr2r( GEWB_Ave, st->lsf_adaptive_mean, M );
     148             : 
     149      130289 :     st->next_force_safety_net = 0;
     150             : 
     151      130289 :     st->pstreaklen = 0;
     152      130289 :     st->streaklimit = 1.0f;
     153      130289 :     set_f( st->mem_MA, 0, M );
     154             : 
     155      130289 :     init_gp_clip( st->clip_var );
     156      130289 :     pitch_ol_init( &st->old_thres, &st->old_pitch, &st->delta_pit, &st->old_corr );
     157      130289 :     set_f( st->old_wsp, 0, L_WSP_MEM );
     158      130289 :     set_f( st->old_wsp2, 0, ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM );
     159             : 
     160      130289 :     st->mem_preemph = 0.0f;
     161      130289 :     st->mem_preemph16k = 0.0f;
     162      130289 :     st->mem_preemph_enc = 0.0;
     163             : 
     164             :     /* AVQ pre-quantizer memory */
     165      130289 :     st->mem_preemp_preQ = 0.0f;
     166      130289 :     st->mem_deemp_preQ = 0.0f;
     167      130289 :     st->last_nq_preQ = 0;
     168      130289 :     st->last_code_preq = 0;
     169      130289 :     st->use_acelp_preq = 0;
     170      130289 :     st->last_harm_flag_acelp = 0;
     171             : 
     172             :     /* (Decimated) Weighted Speech Memory */
     173      130289 :     st->mem_wsp_enc = 0.0;
     174      130289 :     st->mem_wsp = 0.0f;
     175             : 
     176      130289 :     set_f( st->mem_decim2, 0, 3 );
     177      130289 :     set_f( st->Bin_E, 0, L_FFT );
     178             : 
     179      130289 :     st->ee_old = 10.0f;
     180      130289 :     st->Nb_ACELP_frames = 0;
     181      130289 :     st->audio_frame_cnt = AUDIO_COUNTER_INI; /* Initialization of the audio frame counter mildly into the audio mode      */
     182             : 
     183             :     /* adaptive lag window memory */
     184      130289 :     st->old_pitch_la = 0;
     185             : 
     186      130289 :     if ( st->input_Fs == 8000 )
     187             :     {
     188          23 :         st->min_band = 1;
     189          23 :         st->max_band = 16;
     190             :     }
     191             :     else
     192             :     {
     193      130266 :         st->min_band = 0;
     194      130266 :         st->max_band = 19;
     195             :     }
     196             : 
     197      130289 :     if ( st->Opt_AMR_WB )
     198             :     {
     199          48 :         mvr2r( mean_isf_amr_wb, st->lsf_old, M );
     200          48 :         isf2isp( st->lsf_old, st->lsp_old1, M, INT_FS_12k8 );
     201             :     }
     202             :     else
     203             :     {
     204      130241 :         mvr2r( GEWB_Ave, st->lsf_old, M );
     205      130241 :         lsf2lsp( st->lsf_old, st->lsp_old1, M, INT_FS_12k8 );
     206             :     }
     207             : 
     208      130289 :     mvr2r( st->lsf_old, st->lsf_old1, M );
     209      130289 :     mvr2r( st->lsp_old1, st->lsp_old, M );
     210      130289 :     mvr2r( st->lsp_old, st->lsp_old16k, M );
     211      130289 :     mvr2r( st->lsp_old, st->lspold_enc, M );
     212             : 
     213      130289 :     st->stab_fac = 0.0f;
     214             : 
     215             :     /* Bass post-filter memories - encoder side of MODE2 */
     216      130289 :     st->bpf_off = 0;
     217      130289 :     st->pst_mem_deemp_err = 0.0f;
     218      130289 :     st->pst_lp_ener = 0.0f;
     219             : 
     220             :     /* TC coder type */
     221      130289 :     st->tc_cnt = 0;
     222             : 
     223             :     /* find_uv() parameters */
     224      130289 :     st->old_dE1 = 0.0f;
     225      130289 :     st->old_ind_deltaMax = 0;
     226      130289 :     set_f( st->old_enr_ssf, 0.0f, 2 * NB_SSF );
     227      130289 :     st->spike_hyst = -1;
     228             : 
     229             :     /* stereo switching memories */
     230      130289 :     st->mem_preemph_DFT = 0.0f;
     231      130289 :     set_f( st->inp_12k8_mem_stereo_sw, 0, STEREO_DFT_OVL_12k8 - L_MEM_RECALC_12K8 - L_FILT );
     232      130289 :     st->mem_preemph16k_DFT = 0.0f;
     233      130289 :     set_f( st->inp_16k_mem_stereo_sw, 0, STEREO_DFT_OVL_16k - L_MEM_RECALC_16K - L_FILT16k );
     234             : 
     235      130289 :     st->sharpFlag = 0;
     236             : 
     237             :     /* Stationary noise UV modification  */
     238      130289 :     st->ge_sm = 10;
     239      130289 :     st->uv_count = 0;
     240      130289 :     st->act_count = 3;
     241      130289 :     mvr2r( st->lsp_old, st->lspold_s, M );
     242      130289 :     st->noimix_seed = RANDOM_INITSEED;
     243      130289 :     st->min_alpha = 1;
     244      130289 :     st->exc_pe = 0;
     245             : 
     246             :     /* FEC */
     247      130289 :     st->last_clas = UNVOICED_CLAS;
     248      130289 :     st->prev_fmerit = 0.0f;
     249      130289 :     st->fmerit_dt = 0.0f;
     250      130289 :     st->Last_pulse_pos = 0;
     251             : 
     252     1433179 :     for ( i = 0; i < 2 * NB_SUBFR16k; i++ )
     253             :     {
     254     1302890 :         st->old_pitch_buf[i] = L_SUBFR;
     255             :     }
     256             : 
     257             :     /* mode1 core switching */
     258      130289 :     st->old_Es_pred = 0;
     259      130289 :     set_f( st->old_Aq_12_8 + 1, 0, M );
     260      130289 :     st->old_Aq_12_8[0] = 1;
     261             : 
     262             :     /* stable short pitch detection */
     263      130289 :     st->voicing0_sm = 0;
     264      130289 :     st->voicing_sm = 0;
     265      130289 :     st->LF_EnergyRatio_sm = 1;
     266      130289 :     st->predecision_flag = 0;
     267      130289 :     st->diff_sm = 0;
     268      130289 :     st->energy_sm = 0;
     269             : 
     270      130289 :     set_s( st->pitch, L_SUBFR, 3 );
     271      130289 :     set_f( st->voicing, 0.0f, 3 );
     272             : 
     273             :     /*-----------------------------------------------------------------*
     274             :      * General signal buffers
     275             :      *-----------------------------------------------------------------*/
     276             : 
     277      130289 :     if ( !vad_only_flag )
     278             :     {
     279      130040 :         if ( ( st->hSignalBuf = (SIGNAL_BUFFERS_ENC_HANDLE) malloc( sizeof( SIGNAL_BUFFERS_ENC_DATA ) ) ) == NULL )
     280             :         {
     281           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Signal buffers\n" ) );
     282             :         }
     283             : 
     284      130040 :         st->Bin_E_old = st->hSignalBuf->Bin_E_old;
     285      130040 :         st->mem_decim = st->hSignalBuf->mem_decim;
     286      130040 :         st->mem_decim16k = st->hSignalBuf->mem_decim16k;
     287      130040 :         st->old_inp_12k8 = st->hSignalBuf->old_inp_12k8;
     288      130040 :         st->old_inp_16k = st->hSignalBuf->old_inp_16k;
     289      130040 :         st->buf_speech_enc_pe = st->hSignalBuf->buf_speech_enc_pe;
     290      130040 :         st->buf_synth = st->hSignalBuf->buf_synth;
     291      130040 :         st->buf_speech_enc = st->hSignalBuf->buf_speech_enc;
     292      130040 :         st->buf_wspeech_enc = st->hSignalBuf->buf_wspeech_enc;
     293             : 
     294             :         /* initializations */
     295      130040 :         set_f( st->Bin_E_old, 0, L_FFT / 2 );
     296      130040 :         set_f( st->mem_decim, 0, 2 * L_FILT_MAX );
     297      130040 :         set_f( st->mem_decim16k, 0, 2 * L_FILT_MAX );
     298      130040 :         set_f( st->old_inp_12k8, 0, L_INP_MEM );
     299      130040 :         set_f( st->old_inp_16k, 0, L_INP_MEM );
     300             : 
     301      130040 :         st->input_buff = st->hSignalBuf->input_buff;
     302      130040 :         set_zero( st->input_buff, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ) );
     303      130040 :         st->old_input_signal = st->input_buff;
     304      130040 :         if ( st->element_mode == EVS_MONO )
     305             :         {
     306         301 :             st->input = st->input_buff + st->input_Fs / FRAMES_PER_SEC + NS2SA( st->input_Fs, DELAY_FIR_RESAMPL_NS );
     307             :         }
     308             :         else
     309             :         {
     310      129739 :             st->input = st->input_buff + st->input_Fs / FRAMES_PER_SEC;
     311             :         }
     312             :     }
     313             :     else
     314             :     {
     315         249 :         st->hSignalBuf = NULL;
     316         249 :         st->Bin_E_old = NULL;
     317         249 :         st->mem_decim = NULL;
     318         249 :         st->mem_decim16k = NULL;
     319         249 :         st->old_inp_12k8 = NULL;
     320         249 :         st->old_inp_16k = NULL;
     321         249 :         st->buf_speech_enc_pe = NULL;
     322         249 :         st->buf_synth = NULL;
     323         249 :         st->buf_speech_enc = NULL;
     324         249 :         st->buf_wspeech_enc = NULL;
     325         249 :         st->input_buff = NULL;
     326             :     }
     327             : 
     328             :     /*-----------------------------------------------------------------*
     329             :      * Noise estimator
     330             :      *-----------------------------------------------------------------*/
     331             : 
     332      130289 :     if ( idchan == 0 || st->element_mode == IVAS_CPE_TD || st->element_mode == IVAS_CPE_MDCT || st->element_mode == EVS_MONO )
     333             :     {
     334      129876 :         if ( ( st->hNoiseEst = (NOISE_EST_HANDLE) malloc( sizeof( NOISE_EST_DATA ) ) ) == NULL )
     335             :         {
     336           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Noise estimation\n" );
     337             :         }
     338             : 
     339      129876 :         noise_est_init( st->hNoiseEst );
     340             :     }
     341             :     else
     342             :     {
     343         413 :         st->hNoiseEst = NULL;
     344             :     }
     345             : 
     346             :     /*-----------------------------------------------------------------*
     347             :      * VAD
     348             :      *-----------------------------------------------------------------*/
     349             : 
     350      130289 :     st->vad_flag = 1;
     351      130289 :     st->localVAD = 0;
     352             : 
     353      130289 :     if ( ( idchan == 0 || st->element_mode == IVAS_CPE_TD || st->element_mode == IVAS_CPE_MDCT || st->element_mode == EVS_MONO ) && ( !vad_only_flag ) )
     354             :     {
     355      129627 :         if ( ( st->hVAD = (VAD_HANDLE) malloc( sizeof( VAD_DATA ) ) ) == NULL )
     356             :         {
     357           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for VAD\n" ) );
     358             :         }
     359             : 
     360      129627 :         wb_vad_init( st->hVAD );
     361             :     }
     362             :     else
     363             :     {
     364         662 :         st->hVAD = NULL;
     365             :     }
     366      130289 :     st->Pos_relE_cnt = 20;
     367             : 
     368             :     /* CLDFB-based VAD */
     369      130289 :     if ( st->element_mode == EVS_MONO )
     370             :     {
     371         301 :         if ( ( st->hVAD_CLDFB = (VAD_CLDFB_HANDLE) malloc( sizeof( T_CldfbVadState ) ) ) == NULL )
     372             :         {
     373           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CLDFB VAD\n" ) );
     374             :         }
     375             : 
     376         301 :         vad_init( st->hVAD_CLDFB );
     377             :     }
     378             :     else
     379             :     {
     380      129988 :         st->hVAD_CLDFB = NULL;
     381             :     }
     382             : 
     383             :     /*-----------------------------------------------------------------*
     384             :      * Speech/music classifier
     385             :      *-----------------------------------------------------------------*/
     386             : 
     387      130289 :     if ( idchan == 0 || st->element_mode == IVAS_CPE_TD || st->element_mode == IVAS_CPE_MDCT || st->element_mode == EVS_MONO )
     388             :     {
     389      129876 :         if ( ( st->hSpMusClas = (SP_MUS_CLAS_HANDLE) malloc( sizeof( SP_MUS_CLAS_DATA ) ) ) == NULL )
     390             :         {
     391           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Speech/music classifier\n" ) );
     392             :         }
     393             : 
     394      129876 :         speech_music_clas_init( st->hSpMusClas );
     395             : 
     396      129876 :         st->sp_aud_decision0 = 0;
     397      129876 :         st->sp_aud_decision1 = 0;
     398      129876 :         st->sp_aud_decision2 = 0;
     399             :     }
     400             :     else
     401             :     {
     402         413 :         st->hSpMusClas = NULL;
     403             :     }
     404             : 
     405             : 
     406             :     /*-----------------------------------------------------------------*
     407             :      * WB, SWB and FB bandwidth detector
     408             :      *-----------------------------------------------------------------*/
     409             : 
     410      130289 :     st->lt_mean_NB = 0;
     411      130289 :     st->lt_mean_WB = 0;
     412      130289 :     st->lt_mean_SWB = 0;
     413      130289 :     st->count_WB = BWD_COUNT_MAX;
     414      130289 :     st->count_SWB = BWD_COUNT_MAX;
     415      130289 :     st->count_FB = BWD_COUNT_MAX;
     416      130289 :     st->bwidth = st->max_bwidth;
     417      130289 :     st->last_input_bwidth = st->bwidth;
     418      130289 :     st->last_bwidth = st->bwidth;
     419      130289 :     st->last_bwidth_cng = st->bwidth;
     420      130289 :     st->bwidth_sw_cnt = 0;
     421             : 
     422             : 
     423             :     /*-----------------------------------------------------------------*
     424             :      * DTX
     425             :      *-----------------------------------------------------------------*/
     426             : 
     427      130289 :     st->lp_speech = 45.0f; /* Initialize the long-term active speech level in dB */
     428      130289 :     st->lp_noise = 0.0f;
     429      130289 :     st->flag_noisy_speech_snr = 0;
     430      130289 :     st->fd_cng_reset_flag = 0;
     431      130289 :     st->cng_type = -1;
     432      130289 :     st->bckr_tilt_lt = 0.f;
     433      130289 :     st->active_cnt = 0;
     434             : 
     435      130289 :     if ( ( ( idchan == 0 && st->Opt_DTX_ON ) || st->element_mode == EVS_MONO ) || ( st->element_mode == IVAS_CPE_MDCT && st->Opt_DTX_ON ) )
     436             :     {
     437        5825 :         if ( ( st->hDtxEnc = (DTX_ENC_HANDLE) malloc( sizeof( DTX_ENC_DATA ) ) ) == NULL )
     438             :         {
     439           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DTX variables\n" ) );
     440             :         }
     441        5825 :         dtx_enc_init( st, var_SID_rate_flag, interval_SID );
     442             :     }
     443             :     else
     444             :     {
     445      124464 :         st->hDtxEnc = NULL;
     446             :     }
     447             : 
     448             :     /*-----------------------------------------------------------------*
     449             :      * No other handles needed to be allocated for front-VAD structure
     450             :      *-----------------------------------------------------------------*/
     451             : 
     452      130289 :     if ( vad_only_flag )
     453             :     {
     454         249 :         st->hTdCngEnc = NULL;
     455         249 :         st->cldfbAnaEnc = NULL;
     456         249 :         st->hFdCngEnc = NULL;
     457         249 :         st->hSC_VBR = NULL;
     458         249 :         st->hAmrwb_IO = NULL;
     459         249 :         st->hLPDmem = NULL;
     460         249 :         st->hGSCEnc = NULL;
     461         249 :         st->hBWE_TD = NULL;
     462         249 :         st->cldfbSynTd = NULL;
     463         249 :         st->hBWE_FD = NULL;
     464         249 :         st->hHQ_core = NULL;
     465         249 :         st->hRF = NULL;
     466         249 :         st->hTECEnc = NULL;
     467         249 :         st->hTcxEnc = NULL;
     468         249 :         st->hTcxCfg = NULL;
     469         249 :         st->hIGFEnc = NULL;
     470         249 :         st->hPlcExt = NULL;
     471         249 :         st->hTranDet = NULL;
     472             : 
     473         249 :         st->element_mode = IVAS_SCE;
     474         249 :         st->idchan = 100; /* indicates hCoreCoderVAD */
     475         249 :         st->core = -1;
     476         249 :         st->rf_mode = 0;
     477             : 
     478         249 :         return error;
     479             :     }
     480             : 
     481             :     /*-----------------------------------------------------------------*
     482             :      * LP-CNG
     483             :      *-----------------------------------------------------------------*/
     484             : 
     485      130040 :     if ( ( ( idchan == 0 && st->Opt_DTX_ON && st->element_mode != IVAS_CPE_MDCT ) || st->element_mode == EVS_MONO ) && !( ism_mode == ISM_MODE_PARAM || ism_mode == ISM_MODE_DISC ) )
     486             :     {
     487        1104 :         if ( ( st->hTdCngEnc = (TD_CNG_ENC_HANDLE) malloc( sizeof( TD_CNG_ENC_DATA ) ) ) == NULL )
     488             :         {
     489           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DTX/TD CNG\n" ) );
     490             :         }
     491             : 
     492        1104 :         td_cng_enc_init( st->hTdCngEnc, st->Opt_DTX_ON, st->max_bwidth );
     493             :     }
     494             :     else
     495             :     {
     496      128936 :         st->hTdCngEnc = NULL;
     497             :     }
     498             : 
     499             :     /*-----------------------------------------------------------------*
     500             :      * CLDFB & resampling tools parameters
     501             :      *-----------------------------------------------------------------*/
     502             : 
     503      130040 :     if ( ( st->element_mode != IVAS_CPE_MDCT && idchan == 0 ) || ( st->element_mode == IVAS_CPE_MDCT && st->Opt_DTX_ON ) )
     504             :     {
     505       59977 :         if ( ( error = openCldfb( &st->cldfbAnaEnc, CLDFB_ANALYSIS, st->input_Fs, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
     506             :         {
     507           0 :             return error;
     508             :         }
     509             :     }
     510             :     else
     511             :     {
     512       70063 :         st->cldfbAnaEnc = NULL;
     513             :     }
     514             : 
     515      130040 :     st->currEnergyLookAhead = 6.1e-5f;
     516             : 
     517             :     /*-----------------------------------------------------------------*
     518             :      * SC-VBR parameters
     519             :      *-----------------------------------------------------------------*/
     520             : 
     521      130040 :     if ( st->Opt_SC_VBR || st->element_mode == EVS_MONO )
     522             :     {
     523         301 :         if ( ( st->hSC_VBR = (SC_VBR_ENC_HANDLE) malloc( sizeof( SC_VBR_ENC_DATA ) ) ) == NULL )
     524             :         {
     525           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SC-VBR\n" ) );
     526             :         }
     527             : 
     528         301 :         sc_vbr_enc_init( st->hSC_VBR );
     529             :     }
     530             :     else
     531             :     {
     532      129739 :         st->hSC_VBR = NULL;
     533             :     }
     534             : 
     535      130040 :     st->last_Opt_SC_VBR = 0;
     536             : 
     537             : 
     538             :     /*-----------------------------------------------------------------*
     539             :      * AMR-WB IO initialization
     540             :      *-----------------------------------------------------------------*/
     541             : 
     542      130040 :     if ( st->Opt_AMR_WB || st->element_mode == EVS_MONO )
     543             :     {
     544         301 :         if ( ( st->hAmrwb_IO = (AMRWB_IO_ENC_HANDLE) malloc( sizeof( AMRWB_IO_ENC_DATA ) ) ) == NULL )
     545             :         {
     546           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for AMR-WB IO\n" ) );
     547             :         }
     548             : 
     549         301 :         amr_wb_enc_init( st->hAmrwb_IO );
     550             :     }
     551             :     else
     552             :     {
     553      129739 :         st->hAmrwb_IO = NULL;
     554             :     }
     555             : 
     556             :     /*-----------------------------------------------------------------*
     557             :      * ACELP LPDmem
     558             :      *-----------------------------------------------------------------*/
     559             : 
     560      130040 :     if ( ( idchan == 0 && st->element_mode != IVAS_CPE_MDCT ) || st->element_mode == IVAS_CPE_TD )
     561             :     {
     562       57645 :         if ( ( st->hLPDmem = (LPD_state_HANDLE) malloc( sizeof( LPD_state ) ) ) == NULL )
     563             :         {
     564           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LPDmem\n" ) );
     565             :         }
     566             : 
     567       57645 :         LPDmem_enc_init( st->hLPDmem );
     568             :     }
     569             :     else
     570             :     {
     571       72395 :         st->hLPDmem = NULL;
     572             :     }
     573             : 
     574             :     /*-----------------------------------------------------------------*
     575             :      * parameters for AC coder type (GSC)
     576             :      *-----------------------------------------------------------------*/
     577             : 
     578      130040 :     st->GSC_noisy_speech = 0;
     579      130040 :     st->GSC_IVAS_mode = 0;
     580             : 
     581      130040 :     if ( ( idchan == 0 && st->element_mode != IVAS_CPE_MDCT ) || st->element_mode == IVAS_CPE_TD )
     582             :     {
     583       57645 :         if ( ( st->hGSCEnc = (GSC_ENC_HANDLE) malloc( sizeof( GSC_ENC_DATA ) ) ) == NULL )
     584             :         {
     585           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for GSC\n" ) );
     586             :         }
     587             : 
     588       57645 :         GSC_enc_init( st->hGSCEnc );
     589             :     }
     590             :     else
     591             :     {
     592       72395 :         st->hGSCEnc = NULL;
     593             :     }
     594             : 
     595             :     /*-----------------------------------------------------------------*
     596             :      * TBE parameters
     597             :      *-----------------------------------------------------------------*/
     598             : 
     599      130040 :     if ( idchan == 0 && st->element_mode != IVAS_CPE_MDCT )
     600             :     {
     601       57645 :         if ( ( st->hBWE_TD = (TD_BWE_ENC_HANDLE) malloc( sizeof( TD_BWE_ENC_DATA ) ) ) == NULL )
     602             :         {
     603           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD BWE\n" ) );
     604             :         }
     605             : 
     606       57645 :         if ( ( error = openCldfb( &st->cldfbSynTd, CLDFB_SYNTHESIS, 16000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
     607             :         {
     608           0 :             return error;
     609             :         }
     610             : 
     611       57645 :         InitSWBencBuffer( st->hBWE_TD );
     612       57645 :         ResetSHBbuffer_Enc( st->hBWE_TD );
     613             :     }
     614             :     else
     615             :     {
     616       72395 :         st->hBWE_TD = NULL;
     617       72395 :         st->cldfbSynTd = NULL;
     618             :     }
     619             : 
     620             :     /*-----------------------------------------------------------------*
     621             :      * SWB BWE parameters
     622             :      *-----------------------------------------------------------------*/
     623             : 
     624      130040 :     if ( idchan == 0 && st->element_mode != IVAS_CPE_MDCT )
     625             :     {
     626       57645 :         if ( ( st->hBWE_FD = (FD_BWE_ENC_HANDLE) malloc( sizeof( FD_BWE_ENC_DATA ) ) ) == NULL )
     627             :         {
     628           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FD BWE\n" ) );
     629             :         }
     630             : 
     631       57645 :         fd_bwe_enc_init( st->hBWE_FD );
     632             :     }
     633             :     else
     634             :     {
     635       72395 :         st->hBWE_FD = NULL;
     636             :     }
     637             : 
     638             :     /*-----------------------------------------------------------------*
     639             :      * HQ core parameters
     640             :      *-----------------------------------------------------------------*/
     641             : 
     642      130040 :     if ( st->element_mode != IVAS_CPE_TD && st->element_mode != IVAS_CPE_MDCT && idchan == 0 )
     643             :     {
     644       57645 :         if ( ( st->hHQ_core = (HQ_ENC_HANDLE) malloc( sizeof( HQ_ENC_DATA ) ) ) == NULL )
     645             :         {
     646           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HQ core\n" ) );
     647             :         }
     648             : 
     649       57645 :         HQ_core_enc_init( st->hHQ_core );
     650             :     }
     651             :     else
     652             :     {
     653       72395 :         st->hHQ_core = NULL;
     654             :     }
     655             : 
     656             :     /* init memory for detect_transient(), used by HQ core and swb_bwe_enc */
     657      130040 :     st->old_hpfilt_in = 0.0f;
     658      130040 :     st->old_hpfilt_out = 0.0f;
     659      130040 :     st->EnergyLT = 0.0f;
     660      130040 :     st->Energy_Old = 0;
     661      130040 :     st->TransientHangOver = 0;
     662             : 
     663             :     /*-----------------------------------------------------------------*
     664             :      * Channel-aware mode
     665             :      *-----------------------------------------------------------------*/
     666             : 
     667      130040 :     if ( !st->Opt_RF_ON || ( st->bwidth != WB && st->bwidth != SWB ) || st->total_brate != ACELP_13k20 )
     668             :     {
     669      130007 :         st->rf_mode = 0;
     670             :     }
     671             :     else
     672             :     {
     673          33 :         st->rf_mode = st->Opt_RF_ON;
     674             :     }
     675             : 
     676      130040 :     st->rf_mode_last = st->rf_mode;
     677             : 
     678      130040 :     if ( st->Opt_RF_ON || st->element_mode == EVS_MONO )
     679             :     {
     680         301 :         if ( ( st->hRF = (RF_ENC_HANDLE) malloc( sizeof( RF_ENC_DATA ) ) ) == NULL )
     681             :         {
     682           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for RF\n" ) );
     683             :         }
     684             : 
     685             :         /* initialize RF indice buffers */
     686         301 :         reset_rf_indices( st->hRF, st->L_frame, &st->rf_target_bits_write );
     687             :     }
     688             :     else
     689             :     {
     690      129739 :         st->hRF = NULL;
     691             :     }
     692             : 
     693             :     /*-----------------------------------------------------------------*
     694             :      * Temporal Envelope Coding
     695             :      *-----------------------------------------------------------------*/
     696             : 
     697      130040 :     if ( st->element_mode == EVS_MONO )
     698             :     {
     699         301 :         if ( ( st->hTECEnc = (TEC_ENC_HANDLE) malloc( sizeof( TEC_ENC_DATA ) ) ) == NULL )
     700             :         {
     701           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TEC\n" ) );
     702             :         }
     703             :     }
     704             :     else
     705             :     {
     706      129739 :         st->hTECEnc = NULL;
     707             :     }
     708             :     /* note: initialization done later in init_coder_ace_plus() */
     709             : 
     710             :     /*-----------------------------------------------------------------*
     711             :      * TCX core
     712             :      *-----------------------------------------------------------------*/
     713             : 
     714             :     // ToDo: reduction possible for MCT_CHAN_MODE_LFE channel
     715      130040 :     if ( idchan == 0 || st->element_mode == IVAS_CPE_MDCT )
     716             :     {
     717      129627 :         if ( ( st->hTcxEnc = (TCX_ENC_HANDLE) malloc( sizeof( TCX_ENC_DATA ) ) ) == NULL )
     718             :         {
     719           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hTcxEnc\n" ) );
     720             :         }
     721             : 
     722             :         /* Share the memories for 2xTCX10/4xTCX5 and for TCX20 */
     723      129627 :         st->hTcxEnc->spectrum[0] = st->hTcxEnc->spectrum_long;
     724      129627 :         st->hTcxEnc->spectrum[1] = st->hTcxEnc->spectrum_long + N_TCX10_MAX;
     725             : 
     726      129627 :         set_f( st->hTcxEnc->old_out, 0, L_FRAME32k );
     727             : 
     728             :         /* MDCT selector */
     729      129627 :         MDCT_selector_reset( st->hTcxEnc );
     730             : 
     731             :         /* MDCT classifier */
     732      129627 :         MDCT_classifier_reset( st->hTcxEnc );
     733             : 
     734      129627 :         if ( ( st->hTcxCfg = (TCX_CONFIG_HANDLE) malloc( sizeof( TCX_config ) ) ) == NULL )
     735             :         {
     736           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hTcxCfg\n" ) );
     737             :         }
     738             :     }
     739             :     else
     740             :     {
     741         413 :         st->hTcxEnc = NULL;
     742         413 :         st->hTcxCfg = NULL;
     743             :     }
     744             : 
     745             :     /*-----------------------------------------------------------------*
     746             :      * IGF
     747             :      *-----------------------------------------------------------------*/
     748             : 
     749      130040 :     igf_brate = st->total_brate;
     750      130040 :     if ( st->element_mode == IVAS_SCE && ( st_ivas->hEncoderConfig->ivas_format == ISM_FORMAT || st_ivas->hEncoderConfig->ivas_format == MASA_ISM_FORMAT ) )
     751             :     {
     752       47513 :         igf_brate = st->total_brate - ISM_NB_BITS_METADATA_NOMINAL * FRAMES_PER_SEC;
     753             :     }
     754       82527 :     else if ( st->element_mode == IVAS_CPE_DFT || ( st->element_mode == IVAS_SCE && ( st_ivas->hEncoderConfig->ivas_format == SBA_FORMAT ||
     755        6552 :                                                                                       st_ivas->hEncoderConfig->ivas_format == SBA_ISM_FORMAT ||
     756        4740 :                                                                                       st_ivas->hEncoderConfig->ivas_format == MC_FORMAT ||
     757         145 :                                                                                       st_ivas->hEncoderConfig->ivas_format == MASA_FORMAT ) ) )
     758             :     {
     759             :         /* use nominal bitrates for DFT Stereo and (O)SBA, same as in stereo_dft_config()/ivas_spar_config() */
     760       10244 :         if ( element_brate == IVAS_13k2 )
     761             :         {
     762        2375 :             igf_brate = ACELP_9k60;
     763             :         }
     764        7869 :         else if ( element_brate == IVAS_16k4 )
     765             :         {
     766        2141 :             igf_brate = ACELP_13k20;
     767             :         }
     768        5728 :         else if ( element_brate == IVAS_24k4 )
     769             :         {
     770        2075 :             igf_brate = ACELP_16k40;
     771             :         }
     772        3653 :         else if ( element_brate == IVAS_32k )
     773             :         {
     774        2353 :             igf_brate = ACELP_24k40;
     775             :         }
     776             :     }
     777       72283 :     else if ( st->element_mode == IVAS_CPE_MDCT )
     778             :     {
     779       71982 :         igf_brate = element_brate;
     780             :     }
     781             : 
     782      130040 :     if ( st->codec_mode == MODE2 || st->element_mode > EVS_MONO )
     783             :     {
     784      129892 :         st->igf = getIgfPresent( st->element_mode, igf_brate, st->max_bwidth, st->rf_mode );
     785             :     }
     786             :     else
     787             :     {
     788         148 :         st->igf = 0;
     789             :     }
     790             : 
     791      130040 :     if ( ( idchan == 0 || st->element_mode == IVAS_CPE_MDCT ) && ( st->igf || st->element_mode == EVS_MONO ) )
     792             :     {
     793      111365 :         if ( ( st->hIGFEnc = (IGF_ENC_INSTANCE_HANDLE) malloc( sizeof( IGF_ENC_INSTANCE ) ) ) == NULL )
     794             :         {
     795           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hIGFEnc\n" ) );
     796             :         }
     797             :     }
     798             :     else
     799             :     {
     800       18675 :         st->hIGFEnc = NULL;
     801             :     }
     802             : 
     803             :     /*-----------------------------------------------------------------*
     804             :      * Mode 2 initialization
     805             :      *-----------------------------------------------------------------*/
     806             : 
     807      130040 :     st->last_sr_core = st->last_L_frame * FRAMES_PER_SEC;
     808             : 
     809             : 
     810             :     /* PLC encoder */
     811      130040 :     if ( st->element_mode == EVS_MONO )
     812             :     {
     813         301 :         if ( ( st->hPlcExt = (PLC_ENC_EVS_HANDLE) malloc( sizeof( PLC_ENC_EVS ) ) ) == NULL )
     814             :         {
     815           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for hPlcExt\n" ) );
     816             :         }
     817             :     }
     818             :     else
     819             :     {
     820      129739 :         st->hPlcExt = NULL;
     821             :     }
     822             : 
     823             :     /* Init Mode 2 core coder */
     824      130040 :     st->last_totalNoise = 0.f;
     825      130040 :     set_f( st->totalNoise_increase_hist, 0.f, TOTALNOISE_HIST_SIZE );
     826      130040 :     st->totalNoise_increase_len = 0;
     827             : 
     828      130040 :     init_coder_ace_plus( st, st->last_total_brate, igf_brate, 0 /* initialization value */ );
     829             : 
     830             :     /*-----------------------------------------------------------------*
     831             :      * FD-CNG encoder
     832             :      *-----------------------------------------------------------------*/
     833             : 
     834      130040 :     if ( ( idchan == 0 && st->Opt_DTX_ON ) || st->element_mode == EVS_MONO || ( st->element_mode == IVAS_CPE_MDCT && st->Opt_DTX_ON ) )
     835             :     {
     836        5576 :         if ( ( error = createFdCngEnc( &st->hFdCngEnc ) ) != IVAS_ERR_OK )
     837             :         {
     838           0 :             return error;
     839             :         }
     840             : 
     841        5576 :         initFdCngEnc( st->hFdCngEnc, st->input_Fs, st->cldfbAnaEnc->scale );
     842             : 
     843             :         /* initialization for IVAS modes happens in first frame pre-processing */
     844        5576 :         if ( st->element_mode == EVS_MONO )
     845             :         {
     846         301 :             configureFdCngEnc( st->hFdCngEnc, st->bwidth, st->rf_mode && st->total_brate == ACELP_13k20 ? ACELP_9k60 : st->total_brate );
     847             :         }
     848             :     }
     849             :     else
     850             :     {
     851      124464 :         st->hFdCngEnc = NULL;
     852             :     }
     853             : 
     854             :     /*-----------------------------------------------------------------*
     855             :      * Transient detector
     856             :      *-----------------------------------------------------------------*/
     857             : 
     858      130040 :     if ( ( st->hTranDet = (TRAN_DET_HANDLE) malloc( sizeof( TRAN_DET_DATA ) ) ) == NULL )
     859             :     {
     860           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Transient Detection\n" ) );
     861             :     }
     862             : 
     863      130040 :     if ( st->element_mode > EVS_MONO )
     864             :     {
     865      129739 :         InitTransientDetection( (int16_t) ( st->input_Fs / FRAMES_PER_SEC ), 0, st->hTranDet, 1 );
     866             :     }
     867             :     else
     868             :     {
     869         301 :         InitTransientDetection( (int16_t) ( st->input_Fs / FRAMES_PER_SEC ), NS2SA( st->input_Fs, DELAY_FIR_RESAMPL_NS ), st->hTranDet, 0 );
     870             :     }
     871             : 
     872             :     /*-----------------------------------------------------------------*
     873             :      * IVAS parameters
     874             :      *-----------------------------------------------------------------*/
     875             : 
     876      130040 :     st->tdm_LRTD_flag = 0;
     877      130040 :     st->cng_sba_flag = 0;
     878      130040 :     st->bits_frame_channel = 0;
     879      130040 :     st->side_bits_frame_channel = 0;
     880             : 
     881      130040 :     return error;
     882             : }
     883             : 
     884             : 
     885             : /*-----------------------------------------------------------------------*
     886             :  * LPDmem_enc_init()
     887             :  *
     888             :  * Initialization of ACELP LPDmem state variables
     889             :  *-----------------------------------------------------------------------*/
     890             : 
     891       76194 : void LPDmem_enc_init(
     892             :     LPD_state_HANDLE hLPDmem /* i/o: LP memories     */
     893             : )
     894             : {
     895       76194 :     set_zero( hLPDmem->syn, 1 + M );
     896             : 
     897       76194 :     set_f( hLPDmem->old_exc, 0, L_EXC_MEM );
     898       76194 :     hLPDmem->mem_w0 = 0.0f;
     899       76194 :     set_f( hLPDmem->mem_syn, 0, M );
     900       76194 :     set_f( hLPDmem->mem_syn1, 0, M );
     901       76194 :     set_f( hLPDmem->mem_syn2, 0, M );
     902       76194 :     set_zero( hLPDmem->mem_syn_r, L_SYN_MEM );
     903       76194 :     set_f( hLPDmem->mem_syn3, 0, M );
     904             : 
     905       76194 :     hLPDmem->tilt_code = 0.0f;
     906       76194 :     hLPDmem->gc_threshold = 0.0f;
     907       76194 :     set_f( hLPDmem->dispMem, 0, 8 );
     908             : 
     909       76194 :     return;
     910             : }
     911             : 
     912             : 
     913             : /*-----------------------------------------------------------------------*
     914             :  * destroy_cldfb_encoder()
     915             :  *
     916             :  * Free memory which was allocated in init_encoder()
     917             :  *-----------------------------------------------------------------------*/
     918             : 
     919      130289 : void destroy_cldfb_encoder(
     920             :     Encoder_State *st /* i/o: Encoder static variables structure   */
     921             : )
     922             : {
     923      130289 :     deleteCldfb( &st->cldfbSynTd );
     924      130289 :     deleteCldfb( &st->cldfbAnaEnc );
     925             : 
     926      130289 :     deleteFdCngEnc( &st->hFdCngEnc );
     927             : 
     928      130289 :     return;
     929             : }

Generated by: LCOV version 1.14