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 8836 : 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 8836 : error = IVAS_ERR_OK;
72 :
73 : /*-----------------------------------------------------------------*
74 : * General parameters
75 : *-----------------------------------------------------------------*/
76 :
77 8836 : if ( st->Opt_AMR_WB )
78 : {
79 0 : st->last_core = AMR_WB_CORE;
80 : }
81 : else
82 : {
83 8836 : st->last_core = -1;
84 : }
85 :
86 8836 : st->L_frame = L_FRAME;
87 8836 : st->last_coder_type = GENERIC;
88 8836 : st->coder_type = GENERIC;
89 8836 : st->last_total_brate = st->total_brate;
90 8836 : st->last_bits_frame_nominal = -1;
91 8836 : st->last_total_brate_cng = -1;
92 8836 : st->last_core_brate = st->total_brate;
93 8836 : st->dtx_sce_sba = 0;
94 8836 : st->extl = -1;
95 8836 : st->last_extl = -1;
96 8836 : st->last_L_frame = L_FRAME;
97 8836 : st->rate_switching_reset = 0;
98 8836 : st->rate_switching_reset_16kHz = 0;
99 8836 : st->clas = UNVOICED_CLAS;
100 8836 : st->low_rate_mode = 0;
101 8836 : st->ini_frame = 0;
102 8836 : st->inactive_coder_type_flag = 0;
103 8836 : st->sba_br_sw_while_no_data = 0;
104 :
105 8836 : st->coder_type_raw = VOICED;
106 8836 : st->last_coder_type_raw = st->coder_type_raw;
107 :
108 8836 : st->flag_ACELP16k = set_ACELP_flag( st->element_mode, st->total_brate, st->total_brate, idchan, 0, -1, -1 );
109 :
110 8836 : st->is_ism_format = 0;
111 8836 : if ( ism_mode != ISM_MODE_NONE )
112 : {
113 2160 : st->is_ism_format = 1;
114 : }
115 :
116 : /*-----------------------------------------------------------------*
117 : * Bitstream
118 : *-----------------------------------------------------------------*/
119 :
120 8836 : if ( !vad_only_flag )
121 : {
122 8767 : 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 8767 : st->hBstr->ind_list = st_ivas->ind_list;
129 8767 : st->hBstr->ivas_ind_list_zero = &st_ivas->ind_list;
130 8767 : st->hBstr->ivas_max_num_indices = &st_ivas->ivas_max_num_indices;
131 8767 : st->hBstr->nb_ind_tot = 0;
132 8767 : st->hBstr->nb_bits_tot = 0;
133 8767 : st->hBstr->st_ivas = st_ivas;
134 : }
135 : else
136 : {
137 69 : st->hBstr = NULL;
138 : }
139 :
140 : /*-----------------------------------------------------------------*
141 : * Pre-processing and ACELP core parameters
142 : *-----------------------------------------------------------------*/
143 :
144 8836 : mvr2r( GEWB_Ave, st->mem_AR, M );
145 8836 : mvr2r( GEWB_Ave, st->lsfoldbfi0, M );
146 8836 : mvr2r( GEWB_Ave, st->lsfoldbfi1, M );
147 8836 : mvr2r( GEWB_Ave, st->lsf_adaptive_mean, M );
148 :
149 8836 : st->next_force_safety_net = 0;
150 :
151 8836 : st->pstreaklen = 0;
152 8836 : st->streaklimit = 1.0f;
153 8836 : set_f( st->mem_MA, 0, M );
154 :
155 8836 : init_gp_clip( st->clip_var );
156 8836 : pitch_ol_init( &st->old_thres, &st->old_pitch, &st->delta_pit, &st->old_corr );
157 8836 : set_f( st->old_wsp, 0, L_WSP_MEM );
158 8836 : set_f( st->old_wsp2, 0, ( L_WSP_MEM - L_INTERPOL ) / OPL_DECIM );
159 :
160 8836 : st->mem_preemph = 0.0f;
161 8836 : st->mem_preemph16k = 0.0f;
162 8836 : st->mem_preemph_enc = 0.0;
163 :
164 : /* AVQ pre-quantizer memory */
165 8836 : st->mem_preemp_preQ = 0.0f;
166 8836 : st->mem_deemp_preQ = 0.0f;
167 8836 : st->last_nq_preQ = 0;
168 8836 : st->last_code_preq = 0;
169 8836 : st->use_acelp_preq = 0;
170 8836 : st->last_harm_flag_acelp = 0;
171 :
172 : /* (Decimated) Weighted Speech Memory */
173 8836 : st->mem_wsp_enc = 0.0;
174 8836 : st->mem_wsp = 0.0f;
175 :
176 8836 : set_f( st->mem_decim2, 0, 3 );
177 8836 : set_f( st->Bin_E, 0, L_FFT );
178 :
179 8836 : st->ee_old = 10.0f;
180 8836 : st->Nb_ACELP_frames = 0;
181 8836 : 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 8836 : st->old_pitch_la = 0;
185 :
186 8836 : if ( st->input_Fs == 8000 )
187 : {
188 0 : st->min_band = 1;
189 0 : st->max_band = 16;
190 : }
191 : else
192 : {
193 8836 : st->min_band = 0;
194 8836 : st->max_band = 19;
195 : }
196 :
197 8836 : if ( st->Opt_AMR_WB )
198 : {
199 0 : mvr2r( mean_isf_amr_wb, st->lsf_old, M );
200 0 : isf2isp( st->lsf_old, st->lsp_old1, M, INT_FS_12k8 );
201 : }
202 : else
203 : {
204 8836 : mvr2r( GEWB_Ave, st->lsf_old, M );
205 8836 : lsf2lsp( st->lsf_old, st->lsp_old1, M, INT_FS_12k8 );
206 : }
207 :
208 8836 : mvr2r( st->lsf_old, st->lsf_old1, M );
209 8836 : mvr2r( st->lsp_old1, st->lsp_old, M );
210 8836 : mvr2r( st->lsp_old, st->lsp_old16k, M );
211 8836 : mvr2r( st->lsp_old, st->lspold_enc, M );
212 :
213 8836 : st->stab_fac = 0.0f;
214 :
215 : /* Bass post-filter memories - encoder side of MODE2 */
216 8836 : st->bpf_off = 0;
217 8836 : st->pst_mem_deemp_err = 0.0f;
218 8836 : st->pst_lp_ener = 0.0f;
219 :
220 : /* TC coder type */
221 8836 : st->tc_cnt = 0;
222 :
223 : /* find_uv() parameters */
224 8836 : st->old_dE1 = 0.0f;
225 8836 : st->old_ind_deltaMax = 0;
226 8836 : set_f( st->old_enr_ssf, 0.0f, 2 * NB_SSF );
227 8836 : st->spike_hyst = -1;
228 :
229 : /* stereo switching memories */
230 8836 : st->mem_preemph_DFT = 0.0f;
231 8836 : set_f( st->inp_12k8_mem_stereo_sw, 0, STEREO_DFT_OVL_12k8 - L_MEM_RECALC_12K8 - L_FILT );
232 8836 : st->mem_preemph16k_DFT = 0.0f;
233 8836 : set_f( st->inp_16k_mem_stereo_sw, 0, STEREO_DFT_OVL_16k - L_MEM_RECALC_16K - L_FILT16k );
234 :
235 8836 : st->sharpFlag = 0;
236 :
237 : /* Stationary noise UV modification */
238 8836 : st->ge_sm = 10;
239 8836 : st->uv_count = 0;
240 8836 : st->act_count = 3;
241 8836 : mvr2r( st->lsp_old, st->lspold_s, M );
242 8836 : st->noimix_seed = RANDOM_INITSEED;
243 8836 : st->min_alpha = 1;
244 8836 : st->exc_pe = 0;
245 :
246 : /* FEC */
247 8836 : st->last_clas = UNVOICED_CLAS;
248 8836 : st->prev_fmerit = 0.0f;
249 8836 : st->fmerit_dt = 0.0f;
250 8836 : st->Last_pulse_pos = 0;
251 :
252 97196 : for ( i = 0; i < 2 * NB_SUBFR16k; i++ )
253 : {
254 88360 : st->old_pitch_buf[i] = L_SUBFR;
255 : }
256 :
257 : /* mode1 core switching */
258 8836 : st->old_Es_pred = 0;
259 8836 : set_f( st->old_Aq_12_8 + 1, 0, M );
260 8836 : st->old_Aq_12_8[0] = 1;
261 :
262 : /* stable short pitch detection */
263 8836 : st->voicing0_sm = 0;
264 8836 : st->voicing_sm = 0;
265 8836 : st->LF_EnergyRatio_sm = 1;
266 8836 : st->predecision_flag = 0;
267 8836 : st->diff_sm = 0;
268 8836 : st->energy_sm = 0;
269 :
270 8836 : set_s( st->pitch, L_SUBFR, 3 );
271 8836 : set_f( st->voicing, 0.0f, 3 );
272 :
273 : /*-----------------------------------------------------------------*
274 : * General signal buffers
275 : *-----------------------------------------------------------------*/
276 :
277 8836 : if ( !vad_only_flag )
278 : {
279 8767 : 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 8767 : st->Bin_E_old = st->hSignalBuf->Bin_E_old;
285 8767 : st->mem_decim = st->hSignalBuf->mem_decim;
286 8767 : st->mem_decim16k = st->hSignalBuf->mem_decim16k;
287 8767 : st->old_inp_12k8 = st->hSignalBuf->old_inp_12k8;
288 8767 : st->old_inp_16k = st->hSignalBuf->old_inp_16k;
289 8767 : st->buf_speech_enc_pe = st->hSignalBuf->buf_speech_enc_pe;
290 8767 : st->buf_synth = st->hSignalBuf->buf_synth;
291 8767 : st->buf_speech_enc = st->hSignalBuf->buf_speech_enc;
292 8767 : st->buf_wspeech_enc = st->hSignalBuf->buf_wspeech_enc;
293 :
294 : /* initializations */
295 8767 : set_f( st->Bin_E_old, 0, L_FFT / 2 );
296 8767 : set_f( st->mem_decim, 0, 2 * L_FILT_MAX );
297 8767 : set_f( st->mem_decim16k, 0, 2 * L_FILT_MAX );
298 8767 : set_f( st->old_inp_12k8, 0, L_INP_MEM );
299 8767 : set_f( st->old_inp_16k, 0, L_INP_MEM );
300 :
301 8767 : st->input_buff = st->hSignalBuf->input_buff;
302 8767 : set_zero( st->input_buff, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ) );
303 8767 : st->old_input_signal = st->input_buff;
304 8767 : if ( st->element_mode == EVS_MONO )
305 : {
306 3 : st->input = st->input_buff + st->input_Fs / FRAMES_PER_SEC + NS2SA( st->input_Fs, DELAY_FIR_RESAMPL_NS );
307 : }
308 : else
309 : {
310 8764 : st->input = st->input_buff + st->input_Fs / FRAMES_PER_SEC;
311 : }
312 : }
313 : else
314 : {
315 69 : st->hSignalBuf = NULL;
316 69 : st->Bin_E_old = NULL;
317 69 : st->mem_decim = NULL;
318 69 : st->mem_decim16k = NULL;
319 69 : st->old_inp_12k8 = NULL;
320 69 : st->old_inp_16k = NULL;
321 69 : st->buf_speech_enc_pe = NULL;
322 69 : st->buf_synth = NULL;
323 69 : st->buf_speech_enc = NULL;
324 69 : st->buf_wspeech_enc = NULL;
325 69 : st->input_buff = NULL;
326 : }
327 :
328 : /*-----------------------------------------------------------------*
329 : * Noise estimator
330 : *-----------------------------------------------------------------*/
331 :
332 8836 : if ( idchan == 0 || st->element_mode == IVAS_CPE_TD || st->element_mode == IVAS_CPE_MDCT || st->element_mode == EVS_MONO )
333 : {
334 8737 : 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 8737 : noise_est_init( st->hNoiseEst );
340 : }
341 : else
342 : {
343 99 : st->hNoiseEst = NULL;
344 : }
345 :
346 : /*-----------------------------------------------------------------*
347 : * VAD
348 : *-----------------------------------------------------------------*/
349 :
350 8836 : st->vad_flag = 1;
351 8836 : st->localVAD = 0;
352 :
353 8836 : 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 8668 : 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 8668 : wb_vad_init( st->hVAD );
361 : }
362 : else
363 : {
364 168 : st->hVAD = NULL;
365 : }
366 8836 : st->Pos_relE_cnt = 20;
367 :
368 : /* CLDFB-based VAD */
369 8836 : if ( st->element_mode == EVS_MONO )
370 : {
371 3 : 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 3 : vad_init( st->hVAD_CLDFB );
377 : }
378 : else
379 : {
380 8833 : st->hVAD_CLDFB = NULL;
381 : }
382 :
383 : /*-----------------------------------------------------------------*
384 : * Speech/music classifier
385 : *-----------------------------------------------------------------*/
386 :
387 8836 : if ( idchan == 0 || st->element_mode == IVAS_CPE_TD || st->element_mode == IVAS_CPE_MDCT || st->element_mode == EVS_MONO )
388 : {
389 8737 : 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 8737 : speech_music_clas_init( st->hSpMusClas );
395 :
396 8737 : st->sp_aud_decision0 = 0;
397 8737 : st->sp_aud_decision1 = 0;
398 8737 : st->sp_aud_decision2 = 0;
399 : }
400 : else
401 : {
402 99 : st->hSpMusClas = NULL;
403 : }
404 :
405 :
406 : /*-----------------------------------------------------------------*
407 : * WB, SWB and FB bandwidth detector
408 : *-----------------------------------------------------------------*/
409 :
410 8836 : st->lt_mean_NB = 0;
411 8836 : st->lt_mean_WB = 0;
412 8836 : st->lt_mean_SWB = 0;
413 8836 : st->count_WB = BWD_COUNT_MAX;
414 8836 : st->count_SWB = BWD_COUNT_MAX;
415 8836 : st->count_FB = BWD_COUNT_MAX;
416 8836 : st->bwidth = st->max_bwidth;
417 8836 : st->last_input_bwidth = st->bwidth;
418 8836 : st->last_bwidth = st->bwidth;
419 8836 : st->last_bwidth_cng = st->bwidth;
420 8836 : st->bwidth_sw_cnt = 0;
421 :
422 :
423 : /*-----------------------------------------------------------------*
424 : * DTX
425 : *-----------------------------------------------------------------*/
426 :
427 8836 : st->lp_speech = 45.0f; /* Initialize the long-term active speech level in dB */
428 8836 : st->lp_noise = 0.0f;
429 8836 : st->flag_noisy_speech_snr = 0;
430 8836 : st->fd_cng_reset_flag = 0;
431 8836 : st->cng_type = -1;
432 8836 : st->bckr_tilt_lt = 0.f;
433 8836 : st->active_cnt = 0;
434 :
435 8836 : if ( ( ( idchan == 0 && st->Opt_DTX_ON ) || st->element_mode == EVS_MONO ) || ( st->element_mode == IVAS_CPE_MDCT && st->Opt_DTX_ON ) )
436 : {
437 488 : 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 488 : dtx_enc_init( st, var_SID_rate_flag, interval_SID );
442 : }
443 : else
444 : {
445 8348 : st->hDtxEnc = NULL;
446 : }
447 :
448 : /*-----------------------------------------------------------------*
449 : * No other handles needed to be allocated for front-VAD structure
450 : *-----------------------------------------------------------------*/
451 :
452 8836 : if ( vad_only_flag )
453 : {
454 69 : st->hTdCngEnc = NULL;
455 69 : st->cldfbAnaEnc = NULL;
456 69 : st->hFdCngEnc = NULL;
457 69 : st->hSC_VBR = NULL;
458 69 : st->hAmrwb_IO = NULL;
459 69 : st->hLPDmem = NULL;
460 69 : st->hGSCEnc = NULL;
461 69 : st->hBWE_TD = NULL;
462 69 : st->cldfbSynTd = NULL;
463 69 : st->hBWE_FD = NULL;
464 69 : st->hHQ_core = NULL;
465 69 : st->hRF = NULL;
466 69 : st->hTECEnc = NULL;
467 69 : st->hTcxEnc = NULL;
468 69 : st->hTcxCfg = NULL;
469 69 : st->hIGFEnc = NULL;
470 69 : st->hPlcExt = NULL;
471 69 : st->hTranDet = NULL;
472 :
473 69 : st->element_mode = IVAS_SCE;
474 69 : st->idchan = 100; /* indicates hCoreCoderVAD */
475 69 : st->core = -1;
476 69 : st->rf_mode = 0;
477 :
478 69 : return error;
479 : }
480 :
481 : /*-----------------------------------------------------------------*
482 : * LP-CNG
483 : *-----------------------------------------------------------------*/
484 :
485 8767 : 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 91 : 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 91 : td_cng_enc_init( st->hTdCngEnc, st->Opt_DTX_ON, st->max_bwidth );
493 : }
494 : else
495 : {
496 8676 : st->hTdCngEnc = NULL;
497 : }
498 :
499 : /*-----------------------------------------------------------------*
500 : * CLDFB & resampling tools parameters
501 : *-----------------------------------------------------------------*/
502 :
503 8767 : if ( ( st->element_mode != IVAS_CPE_MDCT && idchan == 0 ) || ( st->element_mode == IVAS_CPE_MDCT && st->Opt_DTX_ON ) )
504 : {
505 3220 : 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 5547 : st->cldfbAnaEnc = NULL;
513 : }
514 :
515 8767 : st->currEnergyLookAhead = 6.1e-5f;
516 :
517 : /*-----------------------------------------------------------------*
518 : * SC-VBR parameters
519 : *-----------------------------------------------------------------*/
520 :
521 8767 : if ( st->Opt_SC_VBR || st->element_mode == EVS_MONO )
522 : {
523 3 : 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 3 : sc_vbr_enc_init( st->hSC_VBR );
529 : }
530 : else
531 : {
532 8764 : st->hSC_VBR = NULL;
533 : }
534 :
535 8767 : st->last_Opt_SC_VBR = 0;
536 :
537 :
538 : /*-----------------------------------------------------------------*
539 : * AMR-WB IO initialization
540 : *-----------------------------------------------------------------*/
541 :
542 8767 : if ( st->Opt_AMR_WB || st->element_mode == EVS_MONO )
543 : {
544 3 : 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 3 : amr_wb_enc_init( st->hAmrwb_IO );
550 : }
551 : else
552 : {
553 8764 : st->hAmrwb_IO = NULL;
554 : }
555 :
556 : /*-----------------------------------------------------------------*
557 : * ACELP LPDmem
558 : *-----------------------------------------------------------------*/
559 :
560 8767 : if ( ( idchan == 0 && st->element_mode != IVAS_CPE_MDCT ) || st->element_mode == IVAS_CPE_TD )
561 : {
562 3106 : 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 3106 : LPDmem_enc_init( st->hLPDmem );
568 : }
569 : else
570 : {
571 5661 : st->hLPDmem = NULL;
572 : }
573 :
574 : /*-----------------------------------------------------------------*
575 : * parameters for AC coder type (GSC)
576 : *-----------------------------------------------------------------*/
577 :
578 8767 : st->GSC_noisy_speech = 0;
579 8767 : st->GSC_IVAS_mode = 0;
580 :
581 8767 : if ( ( idchan == 0 && st->element_mode != IVAS_CPE_MDCT ) || st->element_mode == IVAS_CPE_TD )
582 : {
583 3106 : 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 3106 : GSC_enc_init( st->hGSCEnc );
589 : }
590 : else
591 : {
592 5661 : st->hGSCEnc = NULL;
593 : }
594 :
595 : /*-----------------------------------------------------------------*
596 : * TBE parameters
597 : *-----------------------------------------------------------------*/
598 :
599 8767 : if ( idchan == 0 && st->element_mode != IVAS_CPE_MDCT )
600 : {
601 3106 : 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 3106 : if ( ( error = openCldfb( &st->cldfbSynTd, CLDFB_SYNTHESIS, 16000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
607 : {
608 0 : return error;
609 : }
610 :
611 3106 : InitSWBencBuffer( st->hBWE_TD );
612 3106 : ResetSHBbuffer_Enc( st->hBWE_TD );
613 : }
614 : else
615 : {
616 5661 : st->hBWE_TD = NULL;
617 5661 : st->cldfbSynTd = NULL;
618 : }
619 :
620 : /*-----------------------------------------------------------------*
621 : * SWB BWE parameters
622 : *-----------------------------------------------------------------*/
623 :
624 8767 : if ( idchan == 0 && st->element_mode != IVAS_CPE_MDCT )
625 : {
626 3106 : 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 3106 : fd_bwe_enc_init( st->hBWE_FD );
632 : }
633 : else
634 : {
635 5661 : st->hBWE_FD = NULL;
636 : }
637 :
638 : /*-----------------------------------------------------------------*
639 : * HQ core parameters
640 : *-----------------------------------------------------------------*/
641 :
642 8767 : if ( st->element_mode != IVAS_CPE_TD && st->element_mode != IVAS_CPE_MDCT && idchan == 0 )
643 : {
644 3106 : 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 3106 : HQ_core_enc_init( st->hHQ_core );
650 : }
651 : else
652 : {
653 5661 : st->hHQ_core = NULL;
654 : }
655 :
656 : /* init memory for detect_transient(), used by HQ core and swb_bwe_enc */
657 8767 : st->old_hpfilt_in = 0.0f;
658 8767 : st->old_hpfilt_out = 0.0f;
659 8767 : st->EnergyLT = 0.0f;
660 8767 : st->Energy_Old = 0;
661 8767 : st->TransientHangOver = 0;
662 :
663 : /*-----------------------------------------------------------------*
664 : * Channel-aware mode
665 : *-----------------------------------------------------------------*/
666 :
667 8767 : if ( !st->Opt_RF_ON || ( st->bwidth != WB && st->bwidth != SWB ) || st->total_brate != ACELP_13k20 )
668 : {
669 8767 : st->rf_mode = 0;
670 : }
671 : else
672 : {
673 0 : st->rf_mode = st->Opt_RF_ON;
674 : }
675 :
676 8767 : st->rf_mode_last = st->rf_mode;
677 :
678 8767 : if ( st->Opt_RF_ON || st->element_mode == EVS_MONO )
679 : {
680 3 : 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 3 : reset_rf_indices( st->hRF, st->L_frame, &st->rf_target_bits_write );
687 : }
688 : else
689 : {
690 8764 : st->hRF = NULL;
691 : }
692 :
693 : /*-----------------------------------------------------------------*
694 : * Temporal Envelope Coding
695 : *-----------------------------------------------------------------*/
696 :
697 8767 : if ( st->element_mode == EVS_MONO )
698 : {
699 3 : 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 8764 : 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 8767 : if ( idchan == 0 || st->element_mode == IVAS_CPE_MDCT )
716 : {
717 8668 : 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 8668 : st->hTcxEnc->spectrum[0] = st->hTcxEnc->spectrum_long;
724 8668 : st->hTcxEnc->spectrum[1] = st->hTcxEnc->spectrum_long + N_TCX10_MAX;
725 :
726 8668 : set_f( st->hTcxEnc->old_out, 0, L_FRAME32k );
727 :
728 : /* MDCT selector */
729 8668 : MDCT_selector_reset( st->hTcxEnc );
730 :
731 : /* MDCT classifier */
732 8668 : MDCT_classifier_reset( st->hTcxEnc );
733 :
734 8668 : 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 99 : st->hTcxEnc = NULL;
742 99 : st->hTcxCfg = NULL;
743 : }
744 :
745 : /*-----------------------------------------------------------------*
746 : * IGF
747 : *-----------------------------------------------------------------*/
748 :
749 8767 : igf_brate = st->total_brate;
750 8767 : if ( st->element_mode == IVAS_SCE && ( st_ivas->hEncoderConfig->ivas_format == ISM_FORMAT || st_ivas->hEncoderConfig->ivas_format == MASA_ISM_FORMAT ) )
751 : {
752 2160 : igf_brate = st->total_brate - ISM_NB_BITS_METADATA_NOMINAL * FRAMES_PER_SEC;
753 : }
754 6607 : else if ( st->element_mode == IVAS_CPE_DFT || ( st->element_mode == IVAS_SCE && ( st_ivas->hEncoderConfig->ivas_format == SBA_FORMAT ||
755 470 : st_ivas->hEncoderConfig->ivas_format == SBA_ISM_FORMAT ||
756 277 : st_ivas->hEncoderConfig->ivas_format == MC_FORMAT ||
757 31 : 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 1042 : if ( element_brate == IVAS_13k2 )
761 : {
762 266 : igf_brate = ACELP_9k60;
763 : }
764 776 : else if ( element_brate == IVAS_16k4 )
765 : {
766 272 : igf_brate = ACELP_13k20;
767 : }
768 504 : else if ( element_brate == IVAS_24k4 )
769 : {
770 197 : igf_brate = ACELP_16k40;
771 : }
772 307 : else if ( element_brate == IVAS_32k )
773 : {
774 251 : igf_brate = ACELP_24k40;
775 : }
776 : }
777 5565 : else if ( st->element_mode == IVAS_CPE_MDCT )
778 : {
779 5562 : igf_brate = element_brate;
780 : }
781 :
782 8767 : if ( st->codec_mode == MODE2 || st->element_mode > EVS_MONO )
783 : {
784 8765 : st->igf = getIgfPresent( st->element_mode, igf_brate, st->max_bwidth, st->rf_mode );
785 : }
786 : else
787 : {
788 2 : st->igf = 0;
789 : }
790 :
791 8767 : if ( ( idchan == 0 || st->element_mode == IVAS_CPE_MDCT ) && ( st->igf || st->element_mode == EVS_MONO ) )
792 : {
793 6880 : 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 1887 : st->hIGFEnc = NULL;
801 : }
802 :
803 : /*-----------------------------------------------------------------*
804 : * Mode 2 initialization
805 : *-----------------------------------------------------------------*/
806 :
807 8767 : st->last_sr_core = st->last_L_frame * FRAMES_PER_SEC;
808 :
809 :
810 : /* PLC encoder */
811 8767 : if ( st->element_mode == EVS_MONO )
812 : {
813 3 : 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 8764 : st->hPlcExt = NULL;
821 : }
822 :
823 : /* Init Mode 2 core coder */
824 8767 : st->last_totalNoise = 0.f;
825 8767 : set_f( st->totalNoise_increase_hist, 0.f, TOTALNOISE_HIST_SIZE );
826 8767 : st->totalNoise_increase_len = 0;
827 :
828 8767 : init_coder_ace_plus( st, st->last_total_brate, igf_brate, 0 /* initialization value */ );
829 :
830 : /*-----------------------------------------------------------------*
831 : * FD-CNG encoder
832 : *-----------------------------------------------------------------*/
833 :
834 8767 : if ( ( idchan == 0 && st->Opt_DTX_ON ) || st->element_mode == EVS_MONO || ( st->element_mode == IVAS_CPE_MDCT && st->Opt_DTX_ON ) )
835 : {
836 419 : if ( ( error = createFdCngEnc( &st->hFdCngEnc ) ) != IVAS_ERR_OK )
837 : {
838 0 : return error;
839 : }
840 :
841 419 : initFdCngEnc( st->hFdCngEnc, st->input_Fs, st->cldfbAnaEnc->scale );
842 :
843 : /* initialization for IVAS modes happens in first frame pre-processing */
844 419 : if ( st->element_mode == EVS_MONO )
845 : {
846 3 : configureFdCngEnc( st->hFdCngEnc, st->bwidth, st->rf_mode && st->total_brate == ACELP_13k20 ? ACELP_9k60 : st->total_brate );
847 : }
848 : }
849 : else
850 : {
851 8348 : st->hFdCngEnc = NULL;
852 : }
853 :
854 : /*-----------------------------------------------------------------*
855 : * Transient detector
856 : *-----------------------------------------------------------------*/
857 :
858 8767 : 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 8767 : if ( st->element_mode > EVS_MONO )
864 : {
865 8764 : InitTransientDetection( (int16_t) ( st->input_Fs / FRAMES_PER_SEC ), 0, st->hTranDet, 1 );
866 : }
867 : else
868 : {
869 3 : 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 8767 : st->tdm_LRTD_flag = 0;
877 8767 : st->cng_sba_flag = 0;
878 8767 : st->bits_frame_channel = 0;
879 8767 : st->side_bits_frame_channel = 0;
880 :
881 8767 : return error;
882 : }
883 :
884 :
885 : /*-----------------------------------------------------------------------*
886 : * LPDmem_enc_init()
887 : *
888 : * Initialization of ACELP LPDmem state variables
889 : *-----------------------------------------------------------------------*/
890 :
891 3897 : void LPDmem_enc_init(
892 : LPD_state_HANDLE hLPDmem /* i/o: LP memories */
893 : )
894 : {
895 3897 : set_zero( hLPDmem->syn, 1 + M );
896 :
897 3897 : set_f( hLPDmem->old_exc, 0, L_EXC_MEM );
898 3897 : hLPDmem->mem_w0 = 0.0f;
899 3897 : set_f( hLPDmem->mem_syn, 0, M );
900 3897 : set_f( hLPDmem->mem_syn1, 0, M );
901 3897 : set_f( hLPDmem->mem_syn2, 0, M );
902 3897 : set_zero( hLPDmem->mem_syn_r, L_SYN_MEM );
903 3897 : set_f( hLPDmem->mem_syn3, 0, M );
904 :
905 3897 : hLPDmem->tilt_code = 0.0f;
906 3897 : hLPDmem->gc_threshold = 0.0f;
907 3897 : set_f( hLPDmem->dispMem, 0, 8 );
908 :
909 3897 : return;
910 : }
911 :
912 :
913 : /*-----------------------------------------------------------------------*
914 : * destroy_cldfb_encoder()
915 : *
916 : * Free memory which was allocated in init_encoder()
917 : *-----------------------------------------------------------------------*/
918 :
919 8836 : void destroy_cldfb_encoder(
920 : Encoder_State *st /* i/o: Encoder static variables structure */
921 : )
922 : {
923 8836 : deleteCldfb( &st->cldfbSynTd );
924 8836 : deleteCldfb( &st->cldfbAnaEnc );
925 :
926 8836 : deleteFdCngEnc( &st->hFdCngEnc );
927 :
928 8836 : return;
929 : }
|