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