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 "stat_com.h"
43 : #include "cnst.h"
44 : #include "prot.h"
45 : #include "rom_com.h"
46 : #include "wmc_auto.h"
47 :
48 : /*-----------------------------------------------------------------------*
49 : * open_decoder_LPD()
50 : *
51 : * Initialization of state variables
52 : *-----------------------------------------------------------------------*/
53 :
54 100866 : void open_decoder_LPD(
55 : Decoder_State *st, /* i/o: decoder state structure */
56 : const int32_t total_brate, /* i : total bitrate */
57 : const int32_t last_total_brate, /* i : last total bitrate */
58 : const int16_t bwidth, /* i : audio bandwidth */
59 : const int16_t MCT_flag, /* i : hMCT handle allocated (1) or not (0) */
60 : const int16_t last_element_mode, /* i : last element mode */
61 : const int16_t is_init /* i : indicate call during initialization */
62 : )
63 : {
64 : int16_t i;
65 : int16_t mem_syn_r_size_old;
66 : int16_t mem_syn_r_size_new;
67 : int16_t fscaleFB;
68 : int16_t encoderLookahead, encoderLookaheadFB;
69 100866 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
70 100866 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
71 :
72 100866 : if ( st->codec_mode != MODE1 ) /*already updated in MODE1*/
73 : {
74 114 : st->fscale_old = st->fscale;
75 : }
76 :
77 100866 : st->sr_core = getCoreSamplerateMode2( st->element_mode, total_brate, bwidth, st->flag_ACELP16k, st->rf_flag, st->is_ism_format );
78 100866 : st->fscale = sr2fscale( st->sr_core );
79 100866 : fscaleFB = sr2fscale( st->output_Fs );
80 :
81 : /* initializing variables for frame lengths etc. right in the beginning */
82 100866 : st->L_frame = (int16_t) ( st->sr_core / FRAMES_PER_SEC );
83 100866 : if ( st->ini_frame == 0 )
84 : {
85 45567 : st->last_L_frame = st->L_frame_past = st->L_frame;
86 : }
87 100866 : if ( st->hTcxDec != NULL )
88 : {
89 100569 : st->hTcxDec->L_frameTCX = (int16_t) ( st->output_Fs / FRAMES_PER_SEC );
90 100569 : if ( st->ini_frame == 0 )
91 : {
92 45270 : st->L_frameTCX_past = st->hTcxDec->L_frameTCX;
93 : }
94 : }
95 :
96 100866 : st->tcxonly = getTcxonly( st->element_mode, total_brate, MCT_flag, st->is_ism_format );
97 :
98 : /* the TD TCX PLC in MODE1 still runs with 80ms subframes */
99 100866 : if ( ( st->element_mode == EVS_MONO && st->L_frame == L_FRAME16k && total_brate <= ACELP_32k ) || ( st->element_mode > EVS_MONO && st->L_frame == L_FRAME16k && total_brate <= MAX_ACELP_BRATE ) || ( st->tcxonly && ( st->sr_core == 32000 || st->sr_core == 16000 ) ) )
100 : {
101 37314 : st->nb_subfr = NB_SUBFR16k;
102 : }
103 : else
104 : {
105 63552 : st->nb_subfr = NB_SUBFR;
106 : }
107 100866 : st->bits_frame = (int16_t) ( ( (float) st->L_frame / (float) st->fscale ) * (float) FSCALE_DENOM / 128.0f * (float) total_brate / 100.0f + 0.49f );
108 :
109 100866 : st->TcxBandwidth = getTcxBandwidth( bwidth );
110 100866 : st->narrowBand = ( bwidth == NB ) ? 1 : 0;
111 100866 : encoderLookahead = ( L_LOOK_12k8 * st->fscale ) / FSCALE_DENOM;
112 100866 : encoderLookaheadFB = ( L_LOOK_12k8 * fscaleFB ) / FSCALE_DENOM;
113 :
114 100866 : if ( st->element_mode == IVAS_CPE_MDCT )
115 : {
116 57192 : st->pit_res_max = initPitchLagParameters( 12800, &st->pit_min, &st->pit_fr1, &st->pit_fr1b, &st->pit_fr2, &st->pit_max );
117 57192 : hTcxDec->pit_max_TCX = (int16_t) ( st->pit_max * st->output_Fs / 12800 );
118 57192 : hTcxDec->pit_min_TCX = (int16_t) ( st->pit_min * st->output_Fs / 12800 );
119 : }
120 : else
121 : {
122 43674 : st->pit_res_max = initPitchLagParameters( st->sr_core, &st->pit_min, &st->pit_fr1, &st->pit_fr1b, &st->pit_fr2, &st->pit_max );
123 43674 : if ( hTcxDec != NULL )
124 : {
125 43377 : hTcxDec->pit_max_TCX = (int16_t) ( st->pit_max * st->output_Fs / st->sr_core );
126 43377 : hTcxDec->pit_min_TCX = (int16_t) ( st->pit_min * st->output_Fs / st->sr_core );
127 : }
128 : }
129 :
130 100866 : if ( st->ini_frame == 0 )
131 : {
132 45567 : st->pit_res_max_past = st->pit_res_max;
133 : }
134 :
135 : /*Preemphasis param*/
136 100866 : if ( st->fscale < ( 16000 * FSCALE_DENOM ) / 12800 )
137 : {
138 40371 : st->preemph_fac = PREEMPH_FAC; /*NB*/
139 : }
140 60495 : else if ( st->fscale < ( 24000 * FSCALE_DENOM ) / 12800 )
141 : {
142 18915 : st->preemph_fac = PREEMPH_FAC_16k; /*WB*/
143 : }
144 : else
145 : {
146 41580 : st->preemph_fac = PREEMPH_FAC_SWB; /*SWB*/
147 : }
148 :
149 100866 : if ( st->sr_core == INT_FS_16k )
150 : {
151 18915 : st->gamma = GAMMA16k;
152 : }
153 81951 : else if ( st->element_mode > EVS_MONO && st->sr_core > INT_FS_16k )
154 : {
155 41580 : st->gamma = GAMMA16k;
156 : }
157 : else
158 : {
159 40371 : st->gamma = GAMMA1;
160 : }
161 :
162 : /* LPC quantization */
163 100866 : if ( st->sr_core <= INT_FS_16k && st->tcxonly == 0 )
164 : {
165 56517 : st->lpcQuantization = 1;
166 : }
167 : else
168 : {
169 44349 : st->lpcQuantization = 0;
170 : }
171 :
172 100866 : if ( st->tcxonly == 0 )
173 : {
174 56517 : st->numlpc = 1;
175 : }
176 : else
177 : {
178 44349 : st->numlpc = 2;
179 : }
180 :
181 : /* Initialize TBE */
182 100866 : st->prev_coder_type = GENERIC;
183 100866 : if ( st->hBWE_TD != NULL )
184 : {
185 47790 : set_f( st->hBWE_TD->prev_lsf_diff, 0.5f, LPC_SHB_ORDER - 2 );
186 47790 : st->hBWE_TD->prev_tilt_para = 0.0f;
187 47790 : set_zero( st->hBWE_TD->cur_sub_Aq, M + 1 );
188 : }
189 :
190 100866 : if ( st->hIGFDec != NULL )
191 : {
192 100569 : if ( !is_init || st->element_mode != IVAS_CPE_MDCT )
193 : {
194 83619 : init_tcx_cfg( st->hTcxCfg, total_brate, st->sr_core, st->output_Fs, st->L_frame, st->bwidth, st->hTcxDec->L_frameTCX, st->fscale, encoderLookahead, encoderLookaheadFB, st->preemph_fac, st->tcxonly, st->rf_flag, st->igf, st->hIGFDec->infoIGFStopFreq, st->element_mode, st->ini_frame, MCT_flag );
195 : }
196 : else
197 : {
198 16950 : st->hTcxCfg->tcx_curr_overlap_mode = st->hTcxCfg->tcx_last_overlap_mode = ALDO_WINDOW;
199 16950 : st->hTcxCfg->last_aldo = 1;
200 : }
201 : }
202 :
203 100866 : if ( st->hTECDec != NULL )
204 : {
205 123 : resetTecDec( st->hTECDec );
206 : }
207 :
208 100866 : if ( st->element_mode != IVAS_SCE )
209 : {
210 67263 : st->flag_cna = 0;
211 : }
212 100866 : if ( st->ini_frame == 0 )
213 : {
214 45567 : st->flag_cna = 0;
215 45567 : st->last_flag_cna = 0;
216 : }
217 :
218 : /* Static vectors to zero */
219 100866 : if ( st->ini_frame == 0 )
220 : {
221 45567 : st->last_is_cng = 0;
222 :
223 45567 : st->rate_switching_reset = 0;
224 :
225 45567 : if ( st->hTcxDec != NULL )
226 : {
227 45270 : reset_tcx_overl_buf( st->hTcxDec );
228 :
229 45270 : set_zero( st->hTcxDec->syn_OverlFB, L_FRAME_MAX / 2 );
230 45270 : set_zero( st->hTcxDec->old_synth, OLD_SYNTH_INTERNAL_DEC );
231 :
232 45270 : set_zero( st->hTcxDec->synth_history, L_PROT48k + L_FRAME_MAX );
233 : }
234 :
235 45567 : set_zero( st->syn, M + 1 );
236 :
237 45567 : set_zero( st->mem_syn_r, L_SYN_MEM );
238 :
239 45567 : mem_syn_r_size_old = 0; /* just to avoid MSVC warnings */
240 45567 : mem_syn_r_size_new = 0; /* just to avoid MSVC warnings */
241 :
242 45567 : st->con_tcx = 0;
243 : }
244 : else
245 : {
246 : /* Reset old_synth in case of core sampling rate switching and Mode 1/2 switching*/
247 55299 : if ( st->hTcxDec != NULL && ( ( st->L_frame != st->last_L_frame ) || ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO ) ) )
248 : {
249 19668 : set_zero( st->hTcxDec->old_synth, OLD_SYNTH_INTERNAL_DEC );
250 : }
251 :
252 : /*Compute size of old and new memories*/
253 55299 : mem_syn_r_size_old = (int16_t) ( 1.25 * st->last_L_frame / 20.f );
254 55299 : mem_syn_r_size_new = (int16_t) ( 1.25 * st->L_frame / 20.f );
255 :
256 : /*Reset LPC mem*/
257 55299 : if ( ( st->L_frame != st->last_L_frame ) || ( st->last_core == AMR_WB_CORE ) || ( st->last_core == HQ_CORE ) )
258 : {
259 20727 : set_zero( st->mem_MA, M );
260 20727 : if ( st->sr_core == INT_FS_16k )
261 : {
262 5121 : mvr2r( GEWB2_Ave, st->mem_AR, M );
263 : }
264 : else
265 : {
266 15606 : mvr2r( GEWB_Ave, st->mem_AR, M );
267 : }
268 : }
269 :
270 : /*Mode 1/2 switching*/
271 55299 : if ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO )
272 : {
273 111 : mvr2r( st->lsp_old, st->lspold_uw, M );
274 111 : mvr2r( st->lsf_old, st->lsfold_uw, M );
275 111 : set_zero( st->syn, M );
276 : }
277 55299 : if ( st->last_core == AMR_WB_CORE )
278 : {
279 0 : st->last_core = ACELP_CORE;
280 0 : st->last_core_bfi = ACELP_CORE;
281 : }
282 :
283 55299 : if ( ( ( st->element_mode != IVAS_CPE_DFT ) || ( st->element_mode == IVAS_CPE_DFT && st->prev_bfi ) ) && st->last_codec_mode == MODE1 && st->last_core == ACELP_CORE )
284 : {
285 : /* Switching from Mode 1 ACELP */
286 24501 : st->last_core_bfi = ACELP_CORE;
287 24501 : acelp_plc_mdct_transition( st );
288 : }
289 :
290 55299 : if ( st->last_codec_mode == MODE2 &&
291 0 : st->L_frame != st->last_L_frame &&
292 0 : ( ( st->m_frame_type == SID_FRAME && st->last_core > ACELP_CORE ) ||
293 0 : ( st->last_core > ACELP_CORE && st->core > ACELP_CORE ) || st->prev_bfi ) )
294 : {
295 0 : lerp( st->hHQ_core->old_outLB, st->hHQ_core->old_outLB, st->L_frame, st->last_L_frame );
296 : }
297 :
298 : /* Rate switching */
299 55299 : if ( st->last_codec_mode == MODE1 && st->last_core == HQ_CORE )
300 : {
301 : /* Switching from MDCT */
302 :
303 : /*Reset of ACELP memories*/
304 1221 : st->rate_switching_reset = 1;
305 1221 : st->tilt_code = TILT_CODE;
306 1221 : set_zero( st->old_exc, L_EXC_MEM_DEC );
307 1221 : set_zero( st->syn, 1 + M );
308 1221 : set_zero( st->mem_syn2, M );
309 :
310 : /*OLA -> zero */
311 1221 : if ( st->hTcxDec != NULL )
312 : {
313 1221 : reset_tcx_overl_buf( st->hTcxDec );
314 : }
315 :
316 1221 : if ( st->hTcxCfg != NULL )
317 : {
318 1221 : mvr2r( st->hHQ_core->old_out + NS2SA( st->output_Fs, N_ZERO_MDCT_NS ), st->hTcxDec->syn_OverlFB, st->hTcxCfg->tcx_mdct_window_lengthFB );
319 1221 : st->hTcxCfg->last_aldo = 1; /*It was previously ALDO*/
320 1221 : st->hTcxCfg->tcx_curr_overlap_mode = ALDO_WINDOW;
321 : }
322 :
323 : /*OLA for Mode 2 TCX always reset in Mode switching cases*/
324 1221 : if ( st->hHQ_core != NULL )
325 : {
326 1221 : set_f( st->hHQ_core->old_outLB, 0, st->L_frame );
327 : }
328 :
329 1221 : st->last_core_bfi = TCX_20_CORE;
330 :
331 1221 : if ( st->hPFstat != NULL )
332 : {
333 1221 : st->hPFstat->on = 0;
334 : }
335 :
336 : /* reset CLDFB memories */
337 1221 : cldfb_reset_memory( st->cldfbAna );
338 1221 : cldfb_reset_memory( st->cldfbBPF );
339 1221 : cldfb_reset_memory( st->cldfbSyn );
340 1221 : if ( st->cldfbSynHB != NULL )
341 : {
342 6 : cldfb_reset_memory( st->cldfbSynHB );
343 : }
344 : }
345 54078 : else if ( ( st->L_frame != st->last_L_frame ) && ( st->L_frame <= L_FRAME16k ) && ( st->last_L_frame <= L_FRAME16k ) ) /* Rate switching between 12.8 and 16 kHz*/
346 : {
347 : /*Interpolation of ACELP memories*/
348 :
349 : /* convert quantized LSP vector */
350 1554 : st->rate_switching_reset = lsp_convert_poly( st->lsp_old, st->L_frame, 0 );
351 1554 : lsp2a_stab( st->lsp_old, st->old_Aq_12_8, M );
352 :
353 1554 : lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core );
354 1554 : mvr2r( st->lsp_old, st->lspold_uw, M );
355 1554 : mvr2r( st->lsf_old, st->lsfold_uw, M );
356 :
357 1554 : if ( !st->last_con_tcx )
358 : {
359 1539 : synth_mem_updt2( st->L_frame, st->last_L_frame, st->old_exc, st->mem_syn_r, st->mem_syn2, NULL, DEC );
360 : }
361 :
362 : /*mem of deemphasis stayed unchanged.*/
363 : }
364 52524 : else if ( st->L_frame != st->last_L_frame ) /* Rate switching involving TCX only modes */
365 : {
366 : /*Partial reset of ACELP memories*/
367 17952 : st->rate_switching_reset = 1;
368 :
369 : /*reset partly some memories*/
370 17952 : st->tilt_code = TILT_CODE;
371 17952 : if ( !st->last_con_tcx )
372 : {
373 17880 : set_zero( st->old_exc, L_EXC_MEM_DEC );
374 : }
375 17952 : set_zero( st->old_Aq_12_8, M + 1 );
376 :
377 : /*Resamp others memories*/
378 : /*Size of LPC syn memory*/
379 17952 : lerp( st->mem_syn_r + L_SYN_MEM - mem_syn_r_size_old, st->mem_syn_r + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
380 17952 : mvr2r( st->mem_syn_r + L_SYN_MEM - M, st->mem_syn2, M );
381 : }
382 : /* update of lsf_old only needed in BASOP */
383 : /* else if( !st->tcxonly && (st->L_frame == L_FRAME16k) && (st->last_total_brate > ACELP_32k) ) */
384 : /* { */
385 : /* lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core ); */
386 : /* } */
387 : }
388 :
389 100866 : if ( st->last_bwidth == NB && st->bwidth != NB && st->ini_frame != 0 )
390 : {
391 0 : st->rate_switching_reset = 1;
392 : }
393 :
394 100866 : if ( st->hTcxDec != NULL )
395 : {
396 100569 : st->hTcxDec->old_synth_len = 2 * st->L_frame;
397 100569 : st->hTcxDec->old_synth_lenFB = 2 * st->hTcxDec->L_frameTCX;
398 : }
399 :
400 : /* bass pf reset */
401 100866 : st->bpf_gain_param = 0;
402 100866 : if ( st->hBPF != NULL )
403 : {
404 43377 : set_f( st->hBPF->pst_old_syn, 0, NBPSF_PIT_MAX );
405 : }
406 :
407 : /* Formant postfilter */
408 100866 : if ( st->ini_frame == 0 )
409 : {
410 : /* do nothing */
411 : }
412 55299 : else if ( st->last_codec_mode == MODE2 )
413 : {
414 0 : if ( !st->tcxonly )
415 : {
416 0 : if ( st->hPFstat->on )
417 : {
418 0 : lerp( st->hPFstat->mem_stp + L_SYN_MEM - mem_syn_r_size_old, st->hPFstat->mem_stp + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
419 0 : lerp( st->hPFstat->mem_pf_in + L_SYN_MEM - mem_syn_r_size_old, st->hPFstat->mem_pf_in + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
420 : }
421 : else
422 : {
423 0 : set_zero( st->hPFstat->mem_stp, L_SYN_MEM );
424 0 : set_zero( st->hPFstat->mem_pf_in, L_SYN_MEM );
425 0 : st->hPFstat->reset = 1;
426 0 : st->hPFstat->gain_prec = 1.f;
427 : }
428 : }
429 0 : else if ( st->hPFstat->on )
430 : {
431 0 : lerp( st->hPFstat->mem_stp + L_SYN_MEM - mem_syn_r_size_old, st->hPFstat->mem_stp + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
432 0 : lerp( st->hPFstat->mem_pf_in + L_SYN_MEM - mem_syn_r_size_old, st->hPFstat->mem_pf_in + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
433 : }
434 : }
435 : else
436 : {
437 : /*codec mode switching*/
438 :
439 : /*reset post-filter except for Narrowband*/
440 55299 : if ( ( (int16_t) ( st->output_Fs / FRAMES_PER_SEC ) ) != L_FRAME8k )
441 : {
442 55299 : if ( st->hPFstat != NULL )
443 : {
444 32085 : st->hPFstat->reset = 1;
445 32085 : if ( st->hPFstat->on != 0 )
446 : {
447 19287 : st->hPFstat->reset = 0;
448 19287 : lerp( st->hPFstat->mem_stp + L_SYN_MEM - mem_syn_r_size_old, st->hPFstat->mem_stp + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
449 19287 : lerp( st->hPFstat->mem_pf_in + L_SYN_MEM - mem_syn_r_size_old, st->hPFstat->mem_pf_in + L_SYN_MEM - mem_syn_r_size_new, mem_syn_r_size_new, mem_syn_r_size_old );
450 : }
451 : }
452 : }
453 : else
454 : {
455 : /*feed last value old_synth as it is used for pre-emphasis mem*/
456 0 : if ( st->hTcxDec != NULL )
457 : {
458 0 : st->hTcxDec->old_synth[st->hTcxDec->old_synth_len - 1] = st->syn[M];
459 : }
460 0 : if ( st->hBPF != NULL )
461 : {
462 0 : st->hBPF->pst_old_syn[NBPSF_PIT_MAX - 1] = st->syn[M];
463 : }
464 : }
465 : }
466 :
467 : /* lsf and lsp initialization */
468 100866 : if ( st->ini_frame == 0 )
469 : {
470 45567 : mvr2r( st->lsp_old, st->lspold_uw, M );
471 45567 : mvr2r( st->lsf_old, st->lsfold_uw, M );
472 :
473 45567 : set_zero( st->lsf_cng, M );
474 : }
475 :
476 100866 : st->seed_tcx_plc = RANDOM_INITSEED;
477 100866 : st->past_gpit = 0.0f;
478 100866 : st->past_gcode = 0.0f;
479 100866 : st->gc_threshold = 0.0f;
480 :
481 100866 : lsf2lsp( st->lsf_cng, st->lspold_cng, M, INT_FS_12k8 );
482 100866 : lsp2a_stab( st->lspold_cng, st->Aq_cng, M );
483 100866 : st->plcBackgroundNoiseUpdated = 0;
484 100866 : mvr2r( st->lsf_old, st->lsf_q_cng, M );
485 100866 : mvr2r( st->lsf_old, st->old_lsf_q_cng, M );
486 100866 : mvr2r( st->lsp_old, st->lsp_q_cng, M );
487 100866 : mvr2r( st->lsp_old, st->old_lsp_q_cng, M );
488 100866 : set_zero( st->mem_syn_unv_back, M );
489 100866 : st->last_gain_syn_deemph = 1.f;
490 :
491 100866 : if ( st->last_codec_mode == MODE1 || st->ini_frame == 0 )
492 : {
493 : /* this assumes that MODE1 fades out in the frequency domain -
494 : otherwise some data from MODE1 would be needed here */
495 100866 : st->last_concealed_gain_syn_deemph = 1.f;
496 100866 : if ( hTcxDec != NULL )
497 : {
498 100569 : hTcxDec->conceal_eof_gain = 1.0f;
499 : }
500 : }
501 : /* Post processing */
502 100866 : set_zero( st->mem_Aq, NB_SUBFR16k * ( M + 1 ) );
503 :
504 100866 : st->lp_ener_bfi = 60.0f;
505 100866 : if ( st->ini_frame == 0 )
506 : {
507 45567 : st->prev_bfi = 0;
508 45567 : st->last_core_bfi = -1;
509 45567 : if ( st->hTcxDec != NULL )
510 : {
511 45270 : hTcxDec->tcxConceal_recalc_exc = 0;
512 : }
513 : }
514 100866 : st->prev_old_bfi = 0;
515 :
516 100866 : if ( st->hTcxDec != NULL )
517 : {
518 100569 : st->hTcxDec->noise_filling_index[0] = st->hTcxDec->noise_filling_index[1] = 0;
519 100569 : st->hTcxDec->tnsActive[0] = st->hTcxDec->tnsActive[1] = 0;
520 100569 : set_f( st->hTcxDec->ltpGainMemory, 0.0f, N_LTP_GAIN_MEMS );
521 : }
522 :
523 100866 : mvr2r( st->lsf_old, st->lsf_adaptive_mean, M );
524 100866 : mvr2r( st->lsf_old, st->lsfoldbfi0, M );
525 100866 : mvr2r( st->lsf_old, st->lsfoldbfi1, M );
526 :
527 100866 : st->clas_dec = UNVOICED_CLAS;
528 :
529 100866 : if ( !st->last_con_tcx )
530 : {
531 100698 : st->old_enr_LP = 0.0f; /* LP filter E of last good voiced frame or local LP filter E in TD TCX PLC */
532 : }
533 :
534 100866 : if ( st->prev_bfi )
535 : {
536 : /* calculate energy at the end of the previous frame */
537 1512 : if ( st->core == ACELP_CORE && st->last_core == HQ_CORE )
538 : {
539 0 : fer_energy( st->hTcxDec->L_frameTCX, UNVOICED_CLAS, st->previoussynth, -1, &st->enr_old, 1 );
540 : }
541 : }
542 : else
543 : {
544 99354 : st->last_good = UNVOICED_CLAS; /* last good received frame for concealment */
545 99354 : st->enr_old = 0.0f; /* energy at the end of the previous frame */
546 : }
547 100866 : st->lp_gainc = 0.0f;
548 100866 : st->lp_gainp = 0.0f;
549 :
550 100866 : if ( st->hTcxDec != NULL )
551 : {
552 100569 : st->hTcxDec->prev_widow_left_rect = 0;
553 :
554 100569 : if ( is_init || MCT_flag || !( st->element_mode == IVAS_CPE_MDCT && st->element_mode == last_element_mode ) )
555 : {
556 90873 : st->hTcxDec->CngLevelBackgroundTrace_bfi = PLC_MIN_CNG_LEV;
557 90873 : st->hTcxDec->NoiseLevelIndex_bfi = PLC_MIN_STAT_BUFF_SIZE - 1;
558 90873 : st->hTcxDec->CurrLevelIndex_bfi = 0;
559 90873 : st->hTcxDec->LastFrameLevel_bfi = PLC_MIN_CNG_LEV;
560 90873 : set_f( st->hTcxDec->NoiseLevelMemory_bfi, PLC_MIN_CNG_LEV, PLC_MIN_STAT_BUFF_SIZE );
561 :
562 90873 : st->hTcxDec->cummulative_damping_tcx = 1.0f;
563 : }
564 : }
565 :
566 100866 : st->cummulative_damping = 1.0f;
567 :
568 1311258 : for ( i = 0; i < 2 * NB_SUBFR16k + 2; i++ )
569 : {
570 1210392 : st->old_pitch_buf[i] = (float) st->pit_min;
571 : }
572 :
573 1311258 : for ( i = 0; i < 2 * NB_SUBFR16k + 2; i++ )
574 : {
575 1210392 : st->mem_pitch_gain[i] = 1.f;
576 : }
577 :
578 100866 : st->old_fpitch = (float) st->pit_min;
579 :
580 100866 : st->rate_switching_init = 1;
581 :
582 100866 : st->reset_mem_AR = 0;
583 :
584 : /* For phase dispersion */
585 100866 : set_zero( st->dispMem, 8 );
586 :
587 100866 : st->voice_fac = -1; /* purely unvoiced */
588 :
589 : /* TCX-LTP */
590 100866 : if ( hTcxLtpDec != NULL )
591 : {
592 100569 : tcxltp_dec_init( hTcxLtpDec, st->ini_frame, st->last_codec_mode, st->element_mode, st->pit_max, st->sr_core );
593 : }
594 :
595 : /* TCX */
596 100866 : if ( hTcxDec != NULL )
597 : {
598 100569 : st->old_fpitchFB = (float) hTcxDec->pit_min_TCX;
599 :
600 100569 : if ( st->ini_frame == 0 || ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO ) )
601 : {
602 45381 : hTcxDec->tcxltp_last_gain_unmodified = 0.f;
603 : }
604 :
605 : /* TCX */
606 100569 : hTcxDec->tcx_lpc_shaped_ari = getTcxLpcShapedAri( total_brate, st->rf_flag, st->element_mode );
607 :
608 100569 : hTcxDec->envWeighted = 0;
609 : }
610 :
611 100866 : if ( st->hBPF != NULL )
612 : {
613 43377 : st->hBPF->pst_mem_deemp_err = 0.0f;
614 : }
615 :
616 100866 : if ( st->tcxonly )
617 : {
618 44349 : st->p_bpf_noise_buf = NULL;
619 : }
620 : else
621 : {
622 56517 : st->p_bpf_noise_buf = st->bpf_noise_buf;
623 : }
624 :
625 100866 : if ( bwidth == SWB && ( total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO )
626 : {
627 42 : st->tec_tfa = 1;
628 : }
629 : else
630 : {
631 100824 : st->tec_tfa = 0;
632 : }
633 :
634 100866 : st->tec_flag = 0;
635 100866 : st->tfa_flag = 0;
636 :
637 : /* needed in decoder to read the bitstream */
638 100866 : st->enableGplc = 0;
639 :
640 100866 : st->flagGuidedAcelp = 0;
641 100866 : st->tonal_mdct_plc_active = 0;
642 100866 : st->T0_4th = L_SUBFR;
643 100866 : st->guidedT0 = st->T0_4th;
644 :
645 100866 : if ( st->hPlcInfo != NULL && total_brate >= HQ_48k && st->element_mode == EVS_MONO )
646 : {
647 0 : st->enablePlcWaveadjust = 1;
648 :
649 0 : if ( st->hTcxDec != NULL && ( st->ini_frame == 0 || last_total_brate < HQ_48k || st->last_codec_mode == MODE1 || st->force_lpd_reset ) )
650 : {
651 0 : concealment_init( st->hTcxDec->L_frameTCX, st->hPlcInfo );
652 : }
653 : }
654 : else
655 : {
656 100866 : st->enablePlcWaveadjust = 0;
657 : }
658 :
659 : /* PLC: [TCX: Tonal Concealment] */
660 100866 : if ( st->hTonalMDCTConc != NULL && !( st->element_mode > EVS_MONO && st->ini_frame != 0 && st->hTonalMDCTConc->nSamples == st->hTcxDec->L_frameTCX ) )
661 : {
662 47526 : st->hTonalMDCTConc->nScaleFactors = 0;
663 47526 : st->hTonalMDCTConc->nSamples = 0;
664 47526 : st->hTonalMDCTConc->lastPcmOut = 0x0;
665 47526 : st->hTonalMDCTConc->lastBlockData.tonalConcealmentActive = 0;
666 47526 : st->hTonalMDCTConc->lastBlockData.nSamples = 0;
667 :
668 47526 : TonalMDCTConceal_Init( st->hTonalMDCTConc, st->hTcxDec->L_frameTCX, st->L_frame, FDNS_NPTS, st->hTcxCfg );
669 : }
670 :
671 100866 : st->last_tns_active = 0;
672 100866 : st->second_last_tns_active = 0;
673 100866 : st->second_last_core = -1;
674 :
675 100866 : if ( st->hTcxCfg != NULL &&
676 100569 : st->element_mode != EVS_MONO )
677 : {
678 100446 : st->hTcxCfg->fIsTNSAllowed = getTnsAllowed( is_init ? total_brate : st->bits_frame_nominal * FRAMES_PER_SEC, st->igf, st->element_mode );
679 : }
680 100866 : if ( hTcxDec != NULL )
681 : {
682 100569 : hTcxDec->tcxltp_second_last_pitch = st->old_fpitch;
683 100569 : hTcxDec->tcxltp_third_last_pitch = st->old_fpitch;
684 : }
685 :
686 100866 : if ( ( total_brate == ACELP_9k60 || total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO )
687 : {
688 42 : st->dec_glr = 1;
689 : }
690 : else
691 : {
692 100824 : st->dec_glr = 0;
693 : }
694 :
695 100866 : st->dec_glr_idx = 0;
696 :
697 100866 : st->VAD = 0;
698 100866 : if ( hTcxDec != NULL )
699 : {
700 100569 : hTcxDec->enableTcxLpc = 1;
701 :
702 100569 : hTcxDec->old_gaintcx_bfi = 0.0f;
703 :
704 100569 : hTcxDec->tcx_hm_LtpPitchLag = -1;
705 : }
706 :
707 100866 : if ( st->hTcxCfg != NULL )
708 : {
709 100569 : st->hTcxCfg->na_scale = 1.f;
710 : }
711 :
712 100866 : return;
713 : }
714 :
715 : /*-----------------------------------------------------------------------*
716 : * tcxltp_dec_init()
717 : *
718 : * Initialization TCX-LTP handle
719 : *-----------------------------------------------------------------------*/
720 :
721 105690 : void tcxltp_dec_init(
722 : TCX_LTP_DEC_HANDLE hTcxLtpDec,
723 : const int16_t ini_frame,
724 : const int16_t last_codec_mode,
725 : const int16_t element_mode,
726 : const int16_t pit_max,
727 : const int32_t sr_core )
728 : {
729 105690 : hTcxLtpDec->tcxltp_gain = 0.0f;
730 :
731 105690 : hTcxLtpDec->tcxltp = getTcxLtp( sr_core );
732 :
733 105690 : if ( ini_frame == 0 || ( last_codec_mode == MODE1 && element_mode == EVS_MONO ) )
734 : {
735 :
736 50502 : hTcxLtpDec->tcxltp_pitch_int = pit_max;
737 50502 : hTcxLtpDec->tcxltp_pitch_fr = 0;
738 :
739 50502 : if ( ini_frame == 0 )
740 : {
741 50391 : set_f( hTcxLtpDec->tcxltp_mem_in, 0.0f, TCXLTP_MAX_DELAY );
742 50391 : set_f( hTcxLtpDec->tcxltp_mem_out, 0.0f, L_FRAME48k );
743 50391 : hTcxLtpDec->tcxltp_pitch_int_post_prev = 0;
744 50391 : hTcxLtpDec->tcxltp_pitch_fr_post_prev = 0;
745 50391 : hTcxLtpDec->tcxltp_gain_post_prev = 0.f;
746 50391 : hTcxLtpDec->tcxltp_filt_idx_prev = -1;
747 : }
748 : }
749 :
750 105690 : return;
751 : }
752 :
753 : /*-----------------------------------------------------------------------*
754 : * reset_tcx_overl_buf()
755 : *
756 : * Reset TCX core overlap buffers
757 : *-----------------------------------------------------------------------*/
758 :
759 48714 : void reset_tcx_overl_buf(
760 : TCX_DEC_HANDLE hTcxDec /* i/o: TCX decoder handle */
761 : )
762 : {
763 48714 : set_zero( hTcxDec->old_syn_Overl, L_FRAME32k / 2 );
764 48714 : set_zero( hTcxDec->syn_Overl_TDAC, L_FRAME32k / 2 );
765 48714 : set_zero( hTcxDec->syn_Overl, L_FRAME32k / 2 );
766 48714 : set_zero( hTcxDec->syn_Overl_TDACFB, L_FRAME_MAX / 2 );
767 :
768 48714 : return;
769 : }
770 :
771 :
772 : /*-----------------------------------------------------------------------*
773 : * acelp_plc_mdct_transition()
774 : *
775 : * Prepare MDCT OLA memories in TCX/HQ after ACELP PLC
776 : *-----------------------------------------------------------------------*/
777 :
778 24510 : void acelp_plc_mdct_transition(
779 : Decoder_State *st /* i/o: Decoder state */
780 : )
781 : {
782 : float *w;
783 : int16_t W1, nz, delay_comp;
784 : int16_t i;
785 :
786 : /*PLC*/
787 24510 : if ( st->prev_bfi != 0 && st->hTcxCfg != NULL )
788 : {
789 636 : W1 = st->hTcxCfg->tcx_mdct_window_lengthFB;
790 636 : w = st->hTcxCfg->tcx_mdct_windowFB;
791 :
792 636 : nz = NS2SA( st->output_Fs, N_ZERO_MDCT_NS );
793 636 : delay_comp = NS2SA( st->output_Fs, DELAY_CLDFB_NS ); /*CLDFB delay*/
794 :
795 636 : mvr2r( st->hHQ_core->fer_samples + delay_comp, st->hTcxDec->syn_OverlFB, st->hTcxDec->L_frameTCX / 2 );
796 636 : lerp( st->hHQ_core->fer_samples + delay_comp, st->hTcxDec->syn_Overl, st->L_frame / 2, st->hTcxDec->L_frameTCX / 2 ); /*ACELP(bfi)->TCX(rect)*/
797 : /*old_out needed for MODE1 routine and syn_Overl_TDAC for MODE2 routine*/
798 636 : set_f( st->hHQ_core->old_out, 0, nz );
799 636 : mvr2r( st->hHQ_core->fer_samples + delay_comp, st->hHQ_core->old_out + nz, W1 );
800 :
801 216936 : for ( i = 0; i < W1; i++ )
802 : {
803 216300 : st->hHQ_core->old_out[i + nz] *= w[W1 - 1 - i] * w[W1 - 1 - i];
804 : }
805 636 : set_f( &st->hHQ_core->old_out[W1 + nz], 0, nz );
806 :
807 636 : lerp( st->hHQ_core->old_out, st->hHQ_core->old_outLB, st->L_frame, st->hTcxDec->L_frameTCX );
808 :
809 636 : mvr2r( st->hHQ_core->old_out + nz, st->hTcxDec->syn_Overl_TDACFB, st->hTcxDec->L_frameTCX / 2 );
810 636 : nz = NS2SA( st->sr_core, N_ZERO_MDCT_NS );
811 636 : mvr2r( st->hHQ_core->old_outLB + nz, st->hTcxDec->syn_Overl_TDAC, st->L_frame / 2 );
812 : }
813 :
814 24510 : return;
815 : }
|