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 1158656 : 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 1158656 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
70 1158656 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
71 :
72 1158656 : if ( st->codec_mode != MODE1 ) /*already updated in MODE1*/
73 : {
74 1097 : st->fscale_old = st->fscale;
75 : }
76 :
77 1158656 : st->sr_core = getCoreSamplerateMode2( st->element_mode, total_brate, bwidth, st->flag_ACELP16k, st->rf_flag, st->is_ism_format );
78 1158656 : st->fscale = sr2fscale( st->sr_core );
79 1158656 : fscaleFB = sr2fscale( st->output_Fs );
80 :
81 : /* initializing variables for frame lengths etc. right in the beginning */
82 1158656 : st->L_frame = (int16_t) ( st->sr_core / FRAMES_PER_SEC );
83 1158656 : if ( st->ini_frame == 0 )
84 : {
85 740997 : st->last_L_frame = st->L_frame_past = st->L_frame;
86 : }
87 1158656 : if ( st->hTcxDec != NULL )
88 : {
89 1152129 : st->hTcxDec->L_frameTCX = (int16_t) ( st->output_Fs / FRAMES_PER_SEC );
90 1152129 : if ( st->ini_frame == 0 )
91 : {
92 734470 : st->L_frameTCX_past = st->hTcxDec->L_frameTCX;
93 : }
94 : }
95 :
96 1158656 : 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 1158656 : 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 440355 : st->nb_subfr = NB_SUBFR16k;
102 : }
103 : else
104 : {
105 718301 : st->nb_subfr = NB_SUBFR;
106 : }
107 1158656 : 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 1158656 : st->TcxBandwidth = getTcxBandwidth( bwidth );
110 1158656 : st->narrowBand = ( bwidth == NB ) ? 1 : 0;
111 1158656 : encoderLookahead = ( L_LOOK_12k8 * st->fscale ) / FSCALE_DENOM;
112 1158656 : encoderLookaheadFB = ( L_LOOK_12k8 * fscaleFB ) / FSCALE_DENOM;
113 :
114 1158656 : if ( st->element_mode == IVAS_CPE_MDCT )
115 : {
116 709884 : st->pit_res_max = initPitchLagParameters( 12800, &st->pit_min, &st->pit_fr1, &st->pit_fr1b, &st->pit_fr2, &st->pit_max );
117 709884 : hTcxDec->pit_max_TCX = (int16_t) ( st->pit_max * st->output_Fs / 12800 );
118 709884 : hTcxDec->pit_min_TCX = (int16_t) ( st->pit_min * st->output_Fs / 12800 );
119 : }
120 : else
121 : {
122 448772 : st->pit_res_max = initPitchLagParameters( st->sr_core, &st->pit_min, &st->pit_fr1, &st->pit_fr1b, &st->pit_fr2, &st->pit_max );
123 448772 : if ( hTcxDec != NULL )
124 : {
125 442245 : hTcxDec->pit_max_TCX = (int16_t) ( st->pit_max * st->output_Fs / st->sr_core );
126 442245 : hTcxDec->pit_min_TCX = (int16_t) ( st->pit_min * st->output_Fs / st->sr_core );
127 : }
128 : }
129 :
130 1158656 : if ( st->ini_frame == 0 )
131 : {
132 740997 : st->pit_res_max_past = st->pit_res_max;
133 : }
134 :
135 : /*Preemphasis param*/
136 1158656 : if ( st->fscale < ( 16000 * FSCALE_DENOM ) / 12800 )
137 : {
138 505177 : st->preemph_fac = PREEMPH_FAC; /*NB*/
139 : }
140 653479 : else if ( st->fscale < ( 24000 * FSCALE_DENOM ) / 12800 )
141 : {
142 247189 : st->preemph_fac = PREEMPH_FAC_16k; /*WB*/
143 : }
144 : else
145 : {
146 406290 : st->preemph_fac = PREEMPH_FAC_SWB; /*SWB*/
147 : }
148 :
149 1158656 : if ( st->sr_core == INT_FS_16k )
150 : {
151 247189 : st->gamma = GAMMA16k;
152 : }
153 911467 : else if ( st->element_mode > EVS_MONO && st->sr_core > INT_FS_16k )
154 : {
155 406109 : st->gamma = GAMMA16k;
156 : }
157 : else
158 : {
159 505358 : st->gamma = GAMMA1;
160 : }
161 :
162 : /* LPC quantization */
163 1158656 : if ( st->sr_core <= INT_FS_16k && st->tcxonly == 0 )
164 : {
165 658833 : st->lpcQuantization = 1;
166 : }
167 : else
168 : {
169 499823 : st->lpcQuantization = 0;
170 : }
171 :
172 1158656 : if ( st->tcxonly == 0 )
173 : {
174 658833 : st->numlpc = 1;
175 : }
176 : else
177 : {
178 499823 : st->numlpc = 2;
179 : }
180 :
181 : /* Initialize TBE */
182 1158656 : st->prev_coder_type = GENERIC;
183 1158656 : if ( st->hBWE_TD != NULL )
184 : {
185 495531 : set_f( st->hBWE_TD->prev_lsf_diff, 0.5f, LPC_SHB_ORDER - 2 );
186 495531 : st->hBWE_TD->prev_tilt_para = 0.0f;
187 495531 : set_zero( st->hBWE_TD->cur_sub_Aq, M + 1 );
188 : }
189 :
190 1158656 : if ( st->hIGFDec != NULL )
191 : {
192 1152129 : if ( !is_init || st->element_mode != IVAS_CPE_MDCT )
193 : {
194 885113 : 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 267016 : st->hTcxCfg->tcx_curr_overlap_mode = st->hTcxCfg->tcx_last_overlap_mode = ALDO_WINDOW;
199 267016 : st->hTcxCfg->last_aldo = 1;
200 : }
201 : }
202 :
203 1158656 : if ( st->hTECDec != NULL )
204 : {
205 1644 : resetTecDec( st->hTECDec );
206 : }
207 :
208 1158656 : if ( st->element_mode != IVAS_SCE )
209 : {
210 857406 : st->flag_cna = 0;
211 : }
212 1158656 : if ( st->ini_frame == 0 )
213 : {
214 740997 : st->flag_cna = 0;
215 740997 : st->last_flag_cna = 0;
216 : }
217 :
218 : /* Static vectors to zero */
219 1158656 : if ( st->ini_frame == 0 )
220 : {
221 740997 : st->last_is_cng = 0;
222 :
223 740997 : st->rate_switching_reset = 0;
224 :
225 740997 : if ( st->hTcxDec != NULL )
226 : {
227 734470 : reset_tcx_overl_buf( st->hTcxDec );
228 :
229 734470 : set_zero( st->hTcxDec->syn_OverlFB, L_FRAME_MAX / 2 );
230 734470 : set_zero( st->hTcxDec->old_synth, OLD_SYNTH_INTERNAL_DEC );
231 :
232 734470 : set_zero( st->hTcxDec->synth_history, L_PROT48k + L_FRAME_MAX );
233 : }
234 :
235 740997 : set_zero( st->syn, M + 1 );
236 :
237 740997 : set_zero( st->mem_syn_r, L_SYN_MEM );
238 :
239 740997 : mem_syn_r_size_old = 0; /* just to avoid MSVC warnings */
240 740997 : mem_syn_r_size_new = 0; /* just to avoid MSVC warnings */
241 :
242 740997 : 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 417659 : if ( st->hTcxDec != NULL && ( ( st->L_frame != st->last_L_frame ) || ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO ) ) )
248 : {
249 130892 : set_zero( st->hTcxDec->old_synth, OLD_SYNTH_INTERNAL_DEC );
250 : }
251 :
252 : /*Compute size of old and new memories*/
253 417659 : mem_syn_r_size_old = (int16_t) ( 1.25 * st->last_L_frame / 20.f );
254 417659 : mem_syn_r_size_new = (int16_t) ( 1.25 * st->L_frame / 20.f );
255 :
256 : /*Reset LPC mem*/
257 417659 : if ( ( st->L_frame != st->last_L_frame ) || ( st->last_core == AMR_WB_CORE ) || ( st->last_core == HQ_CORE ) )
258 : {
259 135615 : set_zero( st->mem_MA, M );
260 135615 : if ( st->sr_core == INT_FS_16k )
261 : {
262 30951 : mvr2r( GEWB2_Ave, st->mem_AR, M );
263 : }
264 : else
265 : {
266 104664 : mvr2r( GEWB_Ave, st->mem_AR, M );
267 : }
268 : }
269 :
270 : /*Mode 1/2 switching*/
271 417659 : if ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO )
272 : {
273 753 : mvr2r( st->lsp_old, st->lspold_uw, M );
274 753 : mvr2r( st->lsf_old, st->lsfold_uw, M );
275 753 : set_zero( st->syn, M );
276 : }
277 417659 : if ( st->last_core == AMR_WB_CORE )
278 : {
279 18 : st->last_core = ACELP_CORE;
280 18 : st->last_core_bfi = ACELP_CORE;
281 : }
282 :
283 417659 : 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 198096 : st->last_core_bfi = ACELP_CORE;
287 198096 : acelp_plc_mdct_transition( st );
288 : }
289 :
290 417659 : if ( st->last_codec_mode == MODE2 &&
291 124 : st->L_frame != st->last_L_frame &&
292 111 : ( ( st->m_frame_type == SID_FRAME && st->last_core > ACELP_CORE ) ||
293 111 : ( st->last_core > ACELP_CORE && st->core > ACELP_CORE ) || st->prev_bfi ) )
294 : {
295 58 : lerp( st->hHQ_core->old_outLB, st->hHQ_core->old_outLB, st->L_frame, st->last_L_frame );
296 : }
297 :
298 : /* Rate switching */
299 417659 : if ( st->last_codec_mode == MODE1 && st->last_core == HQ_CORE )
300 : {
301 : /* Switching from MDCT */
302 :
303 : /*Reset of ACELP memories*/
304 6007 : st->rate_switching_reset = 1;
305 6007 : st->tilt_code = TILT_CODE;
306 6007 : set_zero( st->old_exc, L_EXC_MEM_DEC );
307 6007 : set_zero( st->syn, 1 + M );
308 6007 : set_zero( st->mem_syn2, M );
309 :
310 : /*OLA -> zero */
311 6007 : if ( st->hTcxDec != NULL )
312 : {
313 6007 : reset_tcx_overl_buf( st->hTcxDec );
314 : }
315 :
316 6007 : if ( st->hTcxCfg != NULL )
317 : {
318 6007 : 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 6007 : st->hTcxCfg->last_aldo = 1; /*It was previously ALDO*/
320 6007 : st->hTcxCfg->tcx_curr_overlap_mode = ALDO_WINDOW;
321 : }
322 :
323 : /*OLA for Mode 2 TCX always reset in Mode switching cases*/
324 6007 : if ( st->hHQ_core != NULL )
325 : {
326 6007 : set_f( st->hHQ_core->old_outLB, 0, st->L_frame );
327 : }
328 :
329 6007 : st->last_core_bfi = TCX_20_CORE;
330 :
331 6007 : if ( st->hPFstat != NULL )
332 : {
333 5715 : st->hPFstat->on = 0;
334 : }
335 :
336 : /* reset CLDFB memories */
337 6007 : cldfb_reset_memory( st->cldfbAna );
338 6007 : cldfb_reset_memory( st->cldfbBPF );
339 6007 : cldfb_reset_memory( st->cldfbSyn );
340 6007 : if ( st->cldfbSynHB != NULL )
341 : {
342 27 : cldfb_reset_memory( st->cldfbSynHB );
343 : }
344 : }
345 411652 : 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 12675 : st->rate_switching_reset = lsp_convert_poly( st->lsp_old, st->L_frame, 0 );
351 12675 : lsp2a_stab( st->lsp_old, st->old_Aq_12_8, M );
352 :
353 12675 : lsp2lsf( st->lsp_old, st->lsf_old, M, st->sr_core );
354 12675 : mvr2r( st->lsp_old, st->lspold_uw, M );
355 12675 : mvr2r( st->lsf_old, st->lsfold_uw, M );
356 :
357 12675 : if ( !st->last_con_tcx )
358 : {
359 12611 : 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 398977 : else if ( st->L_frame != st->last_L_frame ) /* Rate switching involving TCX only modes */
365 : {
366 : /*Partial reset of ACELP memories*/
367 116932 : st->rate_switching_reset = 1;
368 :
369 : /*reset partly some memories*/
370 116932 : st->tilt_code = TILT_CODE;
371 116932 : if ( !st->last_con_tcx )
372 : {
373 116356 : set_zero( st->old_exc, L_EXC_MEM_DEC );
374 : }
375 116932 : set_zero( st->old_Aq_12_8, M + 1 );
376 :
377 : /*Resamp others memories*/
378 : /*Size of LPC syn memory*/
379 116932 : 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 116932 : 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 1158656 : if ( st->last_bwidth == NB && st->bwidth != NB && st->ini_frame != 0 )
390 : {
391 8 : st->rate_switching_reset = 1;
392 : }
393 :
394 1158656 : if ( st->hTcxDec != NULL )
395 : {
396 1152129 : st->hTcxDec->old_synth_len = 2 * st->L_frame;
397 1152129 : st->hTcxDec->old_synth_lenFB = 2 * st->hTcxDec->L_frameTCX;
398 : }
399 :
400 : /* bass pf reset */
401 1158656 : st->bpf_gain_param = 0;
402 1158656 : if ( st->hBPF != NULL )
403 : {
404 442245 : set_f( st->hBPF->pst_old_syn, 0, NBPSF_PIT_MAX );
405 : }
406 :
407 : /* Formant postfilter */
408 1158656 : if ( st->ini_frame == 0 )
409 : {
410 : /* do nothing */
411 : }
412 417659 : else if ( st->last_codec_mode == MODE2 )
413 : {
414 124 : if ( !st->tcxonly )
415 : {
416 58 : if ( st->hPFstat->on )
417 : {
418 20 : 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 20 : 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 38 : set_zero( st->hPFstat->mem_stp, L_SYN_MEM );
424 38 : set_zero( st->hPFstat->mem_pf_in, L_SYN_MEM );
425 38 : st->hPFstat->reset = 1;
426 38 : st->hPFstat->gain_prec = 1.f;
427 : }
428 : }
429 66 : else if ( st->hPFstat->on )
430 : {
431 39 : 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 39 : 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 417535 : if ( ( (int16_t) ( st->output_Fs / FRAMES_PER_SEC ) ) != L_FRAME8k )
441 : {
442 417486 : if ( st->hPFstat != NULL )
443 : {
444 242701 : st->hPFstat->reset = 1;
445 242701 : if ( st->hPFstat->on != 0 )
446 : {
447 148602 : st->hPFstat->reset = 0;
448 148602 : 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 148602 : 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 49 : if ( st->hTcxDec != NULL )
457 : {
458 49 : st->hTcxDec->old_synth[st->hTcxDec->old_synth_len - 1] = st->syn[M];
459 : }
460 49 : if ( st->hBPF != NULL )
461 : {
462 49 : st->hBPF->pst_old_syn[NBPSF_PIT_MAX - 1] = st->syn[M];
463 : }
464 : }
465 : }
466 :
467 : /* lsf and lsp initialization */
468 1158656 : if ( st->ini_frame == 0 )
469 : {
470 740997 : mvr2r( st->lsp_old, st->lspold_uw, M );
471 740997 : mvr2r( st->lsf_old, st->lsfold_uw, M );
472 :
473 740997 : set_zero( st->lsf_cng, M );
474 : }
475 :
476 1158656 : st->seed_tcx_plc = RANDOM_INITSEED;
477 1158656 : st->past_gpit = 0.0f;
478 1158656 : st->past_gcode = 0.0f;
479 1158656 : st->gc_threshold = 0.0f;
480 :
481 1158656 : lsf2lsp( st->lsf_cng, st->lspold_cng, M, INT_FS_12k8 );
482 1158656 : lsp2a_stab( st->lspold_cng, st->Aq_cng, M );
483 1158656 : st->plcBackgroundNoiseUpdated = 0;
484 1158656 : mvr2r( st->lsf_old, st->lsf_q_cng, M );
485 1158656 : mvr2r( st->lsf_old, st->old_lsf_q_cng, M );
486 1158656 : mvr2r( st->lsp_old, st->lsp_q_cng, M );
487 1158656 : mvr2r( st->lsp_old, st->old_lsp_q_cng, M );
488 1158656 : set_zero( st->mem_syn_unv_back, M );
489 1158656 : st->last_gain_syn_deemph = 1.f;
490 :
491 1158656 : 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 1158532 : st->last_concealed_gain_syn_deemph = 1.f;
496 1158532 : if ( hTcxDec != NULL )
497 : {
498 1152005 : hTcxDec->conceal_eof_gain = 1.0f;
499 : }
500 : }
501 : /* Post processing */
502 1158656 : set_zero( st->mem_Aq, NB_SUBFR16k * ( M + 1 ) );
503 :
504 1158656 : st->lp_ener_bfi = 60.0f;
505 1158656 : if ( st->ini_frame == 0 )
506 : {
507 740997 : st->prev_bfi = 0;
508 740997 : st->last_core_bfi = -1;
509 740997 : if ( st->hTcxDec != NULL )
510 : {
511 734470 : hTcxDec->tcxConceal_recalc_exc = 0;
512 : }
513 : }
514 1158656 : st->prev_old_bfi = 0;
515 :
516 1158656 : if ( st->hTcxDec != NULL )
517 : {
518 1152129 : st->hTcxDec->noise_filling_index[0] = st->hTcxDec->noise_filling_index[1] = 0;
519 1152129 : st->hTcxDec->tnsActive[0] = st->hTcxDec->tnsActive[1] = 0;
520 1152129 : set_f( st->hTcxDec->ltpGainMemory, 0.0f, N_LTP_GAIN_MEMS );
521 : }
522 :
523 1158656 : mvr2r( st->lsf_old, st->lsf_adaptive_mean, M );
524 1158656 : mvr2r( st->lsf_old, st->lsfoldbfi0, M );
525 1158656 : mvr2r( st->lsf_old, st->lsfoldbfi1, M );
526 :
527 1158656 : st->clas_dec = UNVOICED_CLAS;
528 :
529 1158656 : if ( !st->last_con_tcx )
530 : {
531 1157119 : 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 1158656 : if ( st->prev_bfi )
535 : {
536 : /* calculate energy at the end of the previous frame */
537 20332 : if ( st->core == ACELP_CORE && st->last_core == HQ_CORE )
538 : {
539 10 : fer_energy( st->hTcxDec->L_frameTCX, UNVOICED_CLAS, st->previoussynth, -1, &st->enr_old, 1 );
540 : }
541 : }
542 : else
543 : {
544 1138324 : st->last_good = UNVOICED_CLAS; /* last good received frame for concealment */
545 1138324 : st->enr_old = 0.0f; /* energy at the end of the previous frame */
546 : }
547 1158656 : st->lp_gainc = 0.0f;
548 1158656 : st->lp_gainp = 0.0f;
549 :
550 1158656 : if ( st->hTcxDec != NULL )
551 : {
552 1152129 : st->hTcxDec->prev_widow_left_rect = 0;
553 :
554 1152129 : if ( is_init || MCT_flag || !( st->element_mode == IVAS_CPE_MDCT && st->element_mode == last_element_mode ) )
555 : {
556 1056989 : st->hTcxDec->CngLevelBackgroundTrace_bfi = PLC_MIN_CNG_LEV;
557 1056989 : st->hTcxDec->NoiseLevelIndex_bfi = PLC_MIN_STAT_BUFF_SIZE - 1;
558 1056989 : st->hTcxDec->CurrLevelIndex_bfi = 0;
559 1056989 : st->hTcxDec->LastFrameLevel_bfi = PLC_MIN_CNG_LEV;
560 1056989 : set_f( st->hTcxDec->NoiseLevelMemory_bfi, PLC_MIN_CNG_LEV, PLC_MIN_STAT_BUFF_SIZE );
561 :
562 1056989 : st->hTcxDec->cummulative_damping_tcx = 1.0f;
563 : }
564 : }
565 :
566 1158656 : st->cummulative_damping = 1.0f;
567 :
568 15062528 : for ( i = 0; i < 2 * NB_SUBFR16k + 2; i++ )
569 : {
570 13903872 : st->old_pitch_buf[i] = (float) st->pit_min;
571 : }
572 :
573 15062528 : for ( i = 0; i < 2 * NB_SUBFR16k + 2; i++ )
574 : {
575 13903872 : st->mem_pitch_gain[i] = 1.f;
576 : }
577 :
578 1158656 : st->old_fpitch = (float) st->pit_min;
579 :
580 1158656 : st->rate_switching_init = 1;
581 :
582 1158656 : st->reset_mem_AR = 0;
583 :
584 : /* For phase dispersion */
585 1158656 : set_zero( st->dispMem, 8 );
586 :
587 1158656 : st->voice_fac = -1; /* purely unvoiced */
588 :
589 : /* TCX-LTP */
590 1158656 : if ( hTcxLtpDec != NULL )
591 : {
592 1152129 : 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 1158656 : if ( hTcxDec != NULL )
597 : {
598 1152129 : st->old_fpitchFB = (float) hTcxDec->pit_min_TCX;
599 :
600 1152129 : if ( st->ini_frame == 0 || ( st->last_codec_mode == MODE1 && st->element_mode == EVS_MONO ) )
601 : {
602 735223 : hTcxDec->tcxltp_last_gain_unmodified = 0.f;
603 : }
604 :
605 : /* TCX */
606 1152129 : hTcxDec->tcx_lpc_shaped_ari = getTcxLpcShapedAri( total_brate, st->rf_flag, st->element_mode );
607 :
608 1152129 : hTcxDec->envWeighted = 0;
609 : }
610 :
611 1158656 : if ( st->hBPF != NULL )
612 : {
613 442245 : st->hBPF->pst_mem_deemp_err = 0.0f;
614 : }
615 :
616 1158656 : if ( st->tcxonly )
617 : {
618 499823 : st->p_bpf_noise_buf = NULL;
619 : }
620 : else
621 : {
622 658833 : st->p_bpf_noise_buf = st->bpf_noise_buf;
623 : }
624 :
625 1158656 : if ( bwidth == SWB && ( total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO )
626 : {
627 235 : st->tec_tfa = 1;
628 : }
629 : else
630 : {
631 1158421 : st->tec_tfa = 0;
632 : }
633 :
634 1158656 : st->tec_flag = 0;
635 1158656 : st->tfa_flag = 0;
636 :
637 : /* needed in decoder to read the bitstream */
638 1158656 : st->enableGplc = 0;
639 :
640 1158656 : st->flagGuidedAcelp = 0;
641 1158656 : st->tonal_mdct_plc_active = 0;
642 1158656 : st->T0_4th = L_SUBFR;
643 1158656 : st->guidedT0 = st->T0_4th;
644 :
645 1158656 : if ( st->hPlcInfo != NULL && total_brate >= HQ_48k && st->element_mode == EVS_MONO )
646 : {
647 248 : st->enablePlcWaveadjust = 1;
648 :
649 248 : if ( st->hTcxDec != NULL && ( st->ini_frame == 0 || last_total_brate < HQ_48k || st->last_codec_mode == MODE1 || st->force_lpd_reset ) )
650 : {
651 221 : concealment_init( st->hTcxDec->L_frameTCX, st->hPlcInfo );
652 : }
653 : }
654 : else
655 : {
656 1158408 : st->enablePlcWaveadjust = 0;
657 : }
658 :
659 : /* PLC: [TCX: Tonal Concealment] */
660 1158656 : if ( st->hTonalMDCTConc != NULL && !( st->element_mode > EVS_MONO && st->ini_frame != 0 && st->hTonalMDCTConc->nSamples == st->hTcxDec->L_frameTCX ) )
661 : {
662 754173 : st->hTonalMDCTConc->nScaleFactors = 0;
663 754173 : st->hTonalMDCTConc->nSamples = 0;
664 754173 : st->hTonalMDCTConc->lastPcmOut = 0x0;
665 754173 : st->hTonalMDCTConc->lastBlockData.tonalConcealmentActive = 0;
666 754173 : st->hTonalMDCTConc->lastBlockData.nSamples = 0;
667 :
668 754173 : TonalMDCTConceal_Init( st->hTonalMDCTConc, st->hTcxDec->L_frameTCX, st->L_frame, FDNS_NPTS, st->hTcxCfg );
669 : }
670 :
671 1158656 : st->last_tns_active = 0;
672 1158656 : st->second_last_tns_active = 0;
673 1158656 : st->second_last_core = -1;
674 :
675 1158656 : if ( st->hTcxCfg != NULL &&
676 1152129 : st->element_mode != EVS_MONO )
677 : {
678 1150485 : st->hTcxCfg->fIsTNSAllowed = getTnsAllowed( is_init ? total_brate : st->bits_frame_nominal * FRAMES_PER_SEC, st->igf, st->element_mode );
679 : }
680 1158656 : if ( hTcxDec != NULL )
681 : {
682 1152129 : hTcxDec->tcxltp_second_last_pitch = st->old_fpitch;
683 1152129 : hTcxDec->tcxltp_third_last_pitch = st->old_fpitch;
684 : }
685 :
686 1158656 : if ( ( total_brate == ACELP_9k60 || total_brate == ACELP_16k40 || total_brate == ACELP_24k40 ) && st->element_mode == EVS_MONO )
687 : {
688 438 : st->dec_glr = 1;
689 : }
690 : else
691 : {
692 1158218 : st->dec_glr = 0;
693 : }
694 :
695 1158656 : st->dec_glr_idx = 0;
696 :
697 1158656 : st->VAD = 0;
698 1158656 : if ( hTcxDec != NULL )
699 : {
700 1152129 : hTcxDec->enableTcxLpc = 1;
701 :
702 1152129 : hTcxDec->old_gaintcx_bfi = 0.0f;
703 :
704 1152129 : hTcxDec->tcx_hm_LtpPitchLag = -1;
705 : }
706 :
707 1158656 : if ( st->hTcxCfg != NULL )
708 : {
709 1152129 : st->hTcxCfg->na_scale = 1.f;
710 : }
711 :
712 1158656 : return;
713 : }
714 :
715 : /*-----------------------------------------------------------------------*
716 : * tcxltp_dec_init()
717 : *
718 : * Initialization TCX-LTP handle
719 : *-----------------------------------------------------------------------*/
720 :
721 1203114 : 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 1203114 : hTcxLtpDec->tcxltp_gain = 0.0f;
730 :
731 1203114 : hTcxLtpDec->tcxltp = getTcxLtp( sr_core );
732 :
733 1203114 : if ( ini_frame == 0 || ( last_codec_mode == MODE1 && element_mode == EVS_MONO ) )
734 : {
735 :
736 786208 : hTcxLtpDec->tcxltp_pitch_int = pit_max;
737 786208 : hTcxLtpDec->tcxltp_pitch_fr = 0;
738 :
739 786208 : if ( ini_frame == 0 )
740 : {
741 785455 : set_f( hTcxLtpDec->tcxltp_mem_in, 0.0f, TCXLTP_MAX_DELAY );
742 785455 : set_f( hTcxLtpDec->tcxltp_mem_out, 0.0f, L_FRAME48k );
743 785455 : hTcxLtpDec->tcxltp_pitch_int_post_prev = 0;
744 785455 : hTcxLtpDec->tcxltp_pitch_fr_post_prev = 0;
745 785455 : hTcxLtpDec->tcxltp_gain_post_prev = 0.f;
746 785455 : hTcxLtpDec->tcxltp_filt_idx_prev = -1;
747 : }
748 : }
749 :
750 1203114 : return;
751 : }
752 :
753 : /*-----------------------------------------------------------------------*
754 : * reset_tcx_overl_buf()
755 : *
756 : * Reset TCX core overlap buffers
757 : *-----------------------------------------------------------------------*/
758 :
759 760370 : void reset_tcx_overl_buf(
760 : TCX_DEC_HANDLE hTcxDec /* i/o: TCX decoder handle */
761 : )
762 : {
763 760370 : set_zero( hTcxDec->old_syn_Overl, L_FRAME32k / 2 );
764 760370 : set_zero( hTcxDec->syn_Overl_TDAC, L_FRAME32k / 2 );
765 760370 : set_zero( hTcxDec->syn_Overl, L_FRAME32k / 2 );
766 760370 : set_zero( hTcxDec->syn_Overl_TDACFB, L_FRAME_MAX / 2 );
767 :
768 760370 : 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 198512 : 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 198512 : if ( st->prev_bfi != 0 && st->hTcxCfg != NULL )
788 : {
789 12268 : W1 = st->hTcxCfg->tcx_mdct_window_lengthFB;
790 12268 : w = st->hTcxCfg->tcx_mdct_windowFB;
791 :
792 12268 : nz = NS2SA( st->output_Fs, N_ZERO_MDCT_NS );
793 12268 : delay_comp = NS2SA( st->output_Fs, DELAY_CLDFB_NS ); /*CLDFB delay*/
794 :
795 12268 : mvr2r( st->hHQ_core->fer_samples + delay_comp, st->hTcxDec->syn_OverlFB, st->hTcxDec->L_frameTCX / 2 );
796 12268 : 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 12268 : set_f( st->hHQ_core->old_out, 0, nz );
799 12268 : mvr2r( st->hHQ_core->fer_samples + delay_comp, st->hHQ_core->old_out + nz, W1 );
800 :
801 3619298 : for ( i = 0; i < W1; i++ )
802 : {
803 3607030 : st->hHQ_core->old_out[i + nz] *= w[W1 - 1 - i] * w[W1 - 1 - i];
804 : }
805 12268 : set_f( &st->hHQ_core->old_out[W1 + nz], 0, nz );
806 :
807 12268 : lerp( st->hHQ_core->old_out, st->hHQ_core->old_outLB, st->L_frame, st->hTcxDec->L_frameTCX );
808 :
809 12268 : mvr2r( st->hHQ_core->old_out + nz, st->hTcxDec->syn_Overl_TDACFB, st->hTcxDec->L_frameTCX / 2 );
810 12268 : nz = NS2SA( st->sr_core, N_ZERO_MDCT_NS );
811 12268 : mvr2r( st->hHQ_core->old_outLB + nz, st->hTcxDec->syn_Overl_TDAC, st->L_frame / 2 );
812 : }
813 :
814 198512 : return;
815 : }
|