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