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 <assert.h>
39 : #include "options.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include "cnst.h"
44 : #include "rom_com.h"
45 : #include "prot.h"
46 : #include "ivas_cnst.h"
47 : #include "ivas_prot.h"
48 : #include "ivas_rom_com.h"
49 : #include "wmc_auto.h"
50 :
51 : /*-------------------------------------------------------------------*
52 : * acelp_core_dec()
53 : *
54 : * ACELP core decoder
55 : *-------------------------------------------------------------------*/
56 :
57 242773 : ivas_error acelp_core_dec(
58 : Decoder_State *st, /* i/o: decoder state structure */
59 : float output[], /* o : synthesis @internal Fs */
60 : float synth[], /* o : synthesis */
61 : float save_hb_synth[], /* o : HB synthesis */
62 : float bwe_exc_extended[], /* i/o: bandwidth extended excitation */
63 : float *voice_factors, /* o : voicing factors */
64 : float old_syn_12k8_16k[], /* o : intermediate ACELP synthesis at 12.8kHz or 16kHz to be used by SWB BWE */
65 : const int16_t sharpFlag, /* i : formant sharpening flag */
66 : float pitch_buf[NB_SUBFR16k], /* o : floating pitch for each subframe */
67 : int16_t *unbits, /* o : number of unused bits */
68 : int16_t *sid_bw, /* o : 0-NB/WB, 1-SWB SID */
69 : STEREO_TD_DEC_DATA_HANDLE hStereoTD, /* i/o: TD stereo decoder handle */
70 : const float tdm_lsfQ_PCh[M], /* i : Q LSFs for primary channel */
71 : const int16_t use_cldfb_for_dft, /* i : flag to use of CLDFB for DFT Stereo */
72 : const int16_t last_element_mode, /* i : last element mode */
73 : const int32_t last_element_brate, /* i : last element bitrate */
74 : const int16_t flag_sec_CNA, /* i : CNA flag for secondary channel */
75 : const int16_t nchan_out, /* i : number of output channels */
76 : STEREO_CNG_DEC_HANDLE hStereoCng, /* i : stereo CNG handle */
77 : const int16_t read_sid_info /* i : read SID info flag */
78 : )
79 : {
80 : float old_exc[L_EXC_DEC], *exc; /* excitation signal buffer */
81 : float syn_tmp[L_FRAME16k + L_SUBFR], *syn; /* synthesis signal buffer */
82 : int16_t output_frame; /* frame length at output sampling freq. */
83 : float lsf_new[M]; /* LSFs at the end of the frame */
84 : float lsp_new[M]; /* LSPs at the end of the frame */
85 : float lsp_mid[M]; /* LSPs in the middle of the frame */
86 : float Aq[NB_SUBFR16k * ( M + 1 )]; /* A(q) quantized for the 4 subframes */
87 : float old_exc2[L_FRAME16k + L_EXC_MEM], *exc2; /* total excitation buffer */
88 : float mem_tmp[M]; /* temporary synthesis filter memory */
89 : float enr_q; /* E information for FER protection */
90 : float tmp_noise; /* Long term temporary noise energy */
91 : float Es_pred; /* predicted scaled innov. energy */
92 : float FEC_pitch; /* FEC pitch */
93 : float old_bwe_exc[( ( PIT16k_MAX + ( L_FRAME16k + 1 ) + L_SUBFR16k ) * 2 )]; /* excitation buffer */
94 : float *bwe_exc; /* Excitation for SWB TBE */
95 : int16_t i, int_fs;
96 : int16_t tc_subfr;
97 : int16_t allow_cn_step;
98 : float temp_buf[L_FRAME16k + L_SYN_MEM];
99 : int16_t last_pulse_pos;
100 : int16_t T0_tmp;
101 : int16_t do_WI;
102 : float dct_buffer[DCT_L_POST];
103 : float exc_buffer[DCT_L_POST];
104 : float dct_exc_tmp[L_FRAME16k];
105 : float bpf_error_signal[L_FRAME16k];
106 : int16_t nb_bits; /* number of bits */
107 : int16_t indice; /* parameter indices to write */
108 : float gain_buf[NB_SUBFR16k];
109 : float q_env[20];
110 : float exc3[L_FRAME16k];
111 : float syn1_tmp[L_FRAME16k + 2], *syn1;
112 : float *realBuffer[CLDFB_NO_COL_MAX], *imagBuffer[CLDFB_NO_COL_MAX];
113 : float realBufferTmp[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
114 : float imagBufferTmp[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
115 : int16_t LSF_Q_prediction; /* LSF prediction mode */
116 : float tmpF;
117 : int16_t uc_two_stage_flag;
118 : int16_t tdm_lp_reuse_flag, tdm_low_rate_mode, tdm_Pitch_reuse_flag;
119 : float *old_exc_s; /* Start of last excitation frame */
120 : float *p_tdm_Pri_pitch_buf;
121 : int16_t local_element_mode;
122 : ivas_error error;
123 :
124 242773 : error = IVAS_ERR_OK;
125 :
126 242773 : if ( st->element_mode == IVAS_CPE_MDCT && nchan_out == 1 && st->idchan == 1 && last_element_brate <= IVAS_SID_5k2 )
127 : {
128 : /* In MDCT-Stereo DTX with mono output, we can skip CNG for the second channel, except for the first inactive frame following an active period */
129 1080 : return error;
130 : }
131 :
132 241693 : push_wmops( "acelp_core_dec" );
133 :
134 241693 : output_frame = (int16_t) ( st->output_Fs / FRAMES_PER_SEC );
135 :
136 : /*----------------------------------------------------------------*
137 : * stereo SID and CNG frames processing
138 : *----------------------------------------------------------------*/
139 :
140 241693 : if ( st->core_brate <= SID_2k40 && st->element_mode == IVAS_CPE_DFT && nchan_out == 2 )
141 : {
142 5435 : if ( st->cng_type == FD_CNG )
143 : {
144 4195 : configureFdCngDec( st->hFdCngDec, st->bwidth, ACELP_14k25, st->L_frame, st->last_L_frame, st->element_mode );
145 :
146 : /* Only run parameter decoding in SID frames */
147 4195 : if ( st->core_brate == SID_2k40 )
148 : {
149 541 : FdCng_decodeSID( st );
150 : }
151 :
152 104875 : for ( i = 0; i < NPART; i++ )
153 : {
154 100680 : st->hFdCngDec->hFdCngCom->sidNoiseEstLp[i] = STEREO_DFT_FD_FILT * st->hFdCngDec->hFdCngCom->sidNoiseEstLp[i] + ( 1 - STEREO_DFT_FD_FILT ) * st->hFdCngDec->hFdCngCom->sidNoiseEst[i];
155 : }
156 :
157 4195 : ApplyFdCng( NULL, NULL, NULL, NULL, st, 0, 0 );
158 : }
159 : else
160 : {
161 1240 : configureFdCngDec( st->hFdCngDec, st->bwidth, ACELP_14k25, st->L_frame, st->last_L_frame, st->element_mode );
162 :
163 : /* decode CNG parameters */
164 1240 : CNG_dec( st, last_element_mode, Aq, lsp_new, lsf_new, &allow_cn_step, sid_bw, q_env );
165 :
166 : /* comfort noise generation */
167 1240 : CNG_exc( st->core_brate, st->L_frame, &st->hTdCngDec->Enew, &st->hTdCngDec->cng_seed, NULL, NULL, &st->lp_ener, st->last_core_brate, &st->first_CNG, &( st->hTdCngDec->cng_ener_seed ), NULL, allow_cn_step, &st->hTdCngDec->last_allow_cn_step, st->hTdCngDec->num_ho, q_env, st->hTdCngDec->lp_env, st->hTdCngDec->old_env, st->hTdCngDec->exc_mem, st->hTdCngDec->exc_mem1, sid_bw, &st->hTdCngDec->cng_ener_seed1, NULL, st->Opt_AMR_WB, st->element_mode );
168 :
169 1240 : mvr2r( Aq, st->Aq_cng, M + 1 );
170 :
171 : /* update old LSP and LSF vector */
172 1240 : mvr2r( lsf_new, st->lsf_old, M );
173 1240 : mvr2r( lsp_new, st->lsp_old, M );
174 : }
175 :
176 5435 : set_f( output, 0, output_frame ); /* output and synth are not used in DFT domain CNG generation and the decoder output is unaffected if they are left uninitalized */
177 5435 : set_f( synth, 0, output_frame ); /* They are however read in a few places which causes errors in the valgrind tests. Simplest solution from a code perspective was to set them to zero. */
178 :
179 : /* CN generation done in DFT domain */
180 5435 : pop_wmops();
181 5435 : return error;
182 : }
183 :
184 : /*----------------------------------------------------------------*
185 : * Active frames processing
186 : *----------------------------------------------------------------*/
187 :
188 : /* open CLDFB buffer up to CLDFB_NO_CHANNELS_MAX bands for 48kHz */
189 4016386 : for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
190 : {
191 3780128 : set_f( realBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
192 3780128 : set_f( imagBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
193 3780128 : realBuffer[i] = realBufferTmp[i];
194 3780128 : imagBuffer[i] = imagBufferTmp[i];
195 : }
196 :
197 : /*----------------------------------------------------------------*
198 : * Initialization
199 : *----------------------------------------------------------------*/
200 :
201 236258 : LSF_Q_prediction = -1;
202 236258 : set_f( syn_tmp, 0, L_SUBFR );
203 236258 : syn = syn_tmp + L_SUBFR;
204 236258 : syn1_tmp[0] = 0;
205 236258 : syn1_tmp[1] = 0;
206 236258 : syn1 = syn1_tmp + 2;
207 236258 : st->bpf_off = 0;
208 :
209 236258 : if ( st->last_core == HQ_CORE || st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE || ( st->element_mode == IVAS_CPE_DFT && st->last_core_brate <= SID_2k40 ) || ( st->element_mode == IVAS_CPE_MDCT && st->last_core_brate <= SID_2k40 ) )
210 : {
211 : /* in case of HQ->ACELP switching, do not apply BPF */
212 21060 : st->bpf_off = 1;
213 :
214 21060 : if ( st->hPFstat != NULL )
215 : {
216 : /* in case of core switching, reset post-filter memories */
217 13814 : st->hPFstat->on = 0;
218 : }
219 :
220 21060 : if ( st->hGSCDec != NULL )
221 : {
222 : /* reset the GSC pre echo energy threshold in case of switching */
223 13814 : st->hGSCDec->Last_frame_ener = (float) MAX_32;
224 : }
225 : }
226 :
227 236258 : if ( st->hGSCDec != NULL && st->prev_bfi > 0 )
228 : {
229 : /* reset the GSC pre echo energy threshold in case of FEC */
230 5965 : st->hGSCDec->Last_frame_ener = (float) MAX_32;
231 : }
232 :
233 236258 : if ( st->hFdCngDec != NULL && ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD ) && ( st->last_core_brate == SID_2k40 || st->last_core_brate == FRAME_NO_DATA ) )
234 : {
235 4957 : set_zero( st->hFdCngDec->hFdCngCom->olapBufferSynth2, FFTLEN );
236 4957 : set_zero( hStereoCng->olapBufferSynth22, FFTLEN );
237 : }
238 :
239 236258 : st->clas_dec = st->last_good;
240 236258 : enr_q = 0.0f;
241 236258 : Es_pred = 0.0f;
242 236258 : tmp_noise = 0.0f;
243 :
244 236258 : mvr2r( st->old_exc, old_exc, L_EXC_MEM_DEC );
245 236258 : exc = old_exc + L_EXC_MEM_DEC;
246 236258 : if ( st->hWIDec != NULL )
247 : {
248 0 : mvr2r( st->hWIDec->old_exc2, old_exc2, L_EXC_MEM );
249 : }
250 : else
251 : {
252 236258 : set_f( old_exc2, 0, L_EXC_MEM );
253 : }
254 236258 : exc2 = old_exc2 + L_EXC_MEM;
255 236258 : if ( st->hBWE_TD != NULL )
256 : {
257 228918 : mvr2r( st->hBWE_TD->old_bwe_exc, old_bwe_exc, PIT16k_MAX * 2 );
258 228918 : bwe_exc = old_bwe_exc + PIT16k_MAX * 2;
259 : }
260 : else
261 : {
262 7340 : bwe_exc = NULL;
263 : }
264 :
265 236258 : last_pulse_pos = 0;
266 236258 : do_WI = 0;
267 236258 : st->GSC_noisy_speech = 0;
268 236258 : st->relax_prev_lsf_interp = 0;
269 236258 : set_zero( gain_buf, NB_SUBFR16k );
270 :
271 236258 : if ( st->L_frame == L_FRAME )
272 : {
273 123251 : st->gamma = GAMMA1;
274 123251 : st->preemph_fac = PREEMPH_FAC;
275 123251 : int_fs = INT_FS_12k8;
276 : }
277 : else
278 : {
279 113007 : st->gamma = GAMMA16k;
280 113007 : st->preemph_fac = PREEMPH_FAC_16k;
281 113007 : int_fs = INT_FS_16k;
282 : }
283 :
284 : /* reset post-filter in case of switching */
285 236258 : if ( st->hPFstat != NULL && st->hPFstat->on == 0 )
286 : {
287 34411 : st->hPFstat->reset = 1;
288 : }
289 :
290 : /* TD stereo parameters */
291 236258 : if ( st->element_mode == IVAS_CPE_TD && st->idchan == 1 )
292 : {
293 3800 : tdm_lp_reuse_flag = hStereoTD->tdm_lp_reuse_flag;
294 3800 : tdm_low_rate_mode = hStereoTD->tdm_low_rate_mode;
295 3800 : tdm_Pitch_reuse_flag = hStereoTD->tdm_Pitch_reuse_flag;
296 3800 : p_tdm_Pri_pitch_buf = hStereoTD->tdm_Pri_pitch_buf;
297 : }
298 : else
299 : {
300 232458 : tdm_lp_reuse_flag = 0;
301 232458 : tdm_low_rate_mode = 0;
302 232458 : if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
303 : {
304 8202 : tdm_low_rate_mode = 1;
305 : }
306 232458 : tdm_Pitch_reuse_flag = 0;
307 232458 : p_tdm_Pri_pitch_buf = NULL;
308 : }
309 :
310 : /*----------------------------------------------------------------*
311 : * Updates in case of internal sampling rate switching
312 : *----------------------------------------------------------------*/
313 :
314 236258 : if ( st->last_L_frame != st->L_frame && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE ) )
315 : {
316 : int16_t dec;
317 :
318 832 : if ( st->hPFstat->on != 0 )
319 : {
320 : int16_t mem_syn_r_size_old, mem_syn_r_size_new;
321 :
322 759 : mem_syn_r_size_old = (int16_t) ( 1.25 * st->last_L_frame / 20.f );
323 759 : mem_syn_r_size_new = (int16_t) ( 1.25 * st->L_frame / 20.f );
324 759 : 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 );
325 759 : 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 );
326 : }
327 :
328 : /* convert quantized LSP vector */
329 832 : st->rate_switching_reset = lsp_convert_poly( st->lsp_old, st->L_frame, 0 );
330 :
331 : /* convert old quantized LSF vector */
332 832 : lsp2lsf( st->lsp_old, st->lsf_old, M, int_fs );
333 :
334 : /* FEC - update adaptive LSF mean vector */
335 832 : mvr2r( st->lsf_old, st->lsfoldbfi1, M );
336 832 : mvr2r( st->lsf_old, st->lsfoldbfi0, M );
337 832 : mvr2r( st->lsf_old, st->lsf_adaptive_mean, M );
338 :
339 : /* Reset LPC mem */
340 832 : if ( st->sr_core == INT_FS_16k )
341 : {
342 525 : mvr2r( GEWB2_Ave, st->mem_AR, M );
343 : }
344 : else
345 : {
346 307 : mvr2r( GEWB_Ave, st->mem_AR, M );
347 : }
348 832 : set_zero( st->mem_MA, M );
349 :
350 : /* update synthesis filter memories */
351 832 : dec = DEC;
352 832 : if ( st->element_mode != EVS_MONO )
353 : {
354 832 : dec = DEC_IVAS;
355 : }
356 832 : synth_mem_updt2( st->L_frame, st->last_L_frame, st->old_exc, st->mem_syn_r, st->mem_syn2, NULL, dec );
357 832 : mvr2r( st->old_exc, old_exc, L_EXC_MEM_DEC );
358 832 : mvr2r( st->mem_syn2, st->mem_syn1, M );
359 832 : mvr2r( st->mem_syn2, st->mem_syn3, M );
360 : }
361 :
362 : /* update buffer of old subframe pitch values */
363 236258 : if ( st->last_L_frame != st->L_frame )
364 : {
365 2267 : if ( st->L_frame == L_FRAME )
366 : {
367 1384 : if ( st->last_L_frame == L_FRAME32k )
368 : {
369 347 : tmpF = (float) 12800 / (float) 32000;
370 : }
371 1037 : else if ( st->last_L_frame == 512 )
372 : {
373 359 : tmpF = (float) 12800 / (float) 25600;
374 : }
375 : else /* st->last_L_frame == L_FRAME16k */
376 : {
377 678 : tmpF = (float) 12800 / (float) 16000;
378 : }
379 :
380 6920 : for ( i = NB_SUBFR16k - NB_SUBFR; i < NB_SUBFR16k; i++ )
381 : {
382 5536 : st->old_pitch_buf[i - 1] = tmpF * st->old_pitch_buf[i];
383 : }
384 :
385 6920 : for ( i = 2 * NB_SUBFR16k - NB_SUBFR; i < 2 * NB_SUBFR16k; i++ )
386 : {
387 5536 : st->old_pitch_buf[i - 2] = tmpF * st->old_pitch_buf[i];
388 : }
389 : }
390 : else
391 : {
392 883 : if ( st->last_L_frame == L_FRAME32k )
393 : {
394 42 : tmpF = (float) 16000 / (float) 32000;
395 : }
396 841 : else if ( st->last_L_frame == 512 )
397 : {
398 271 : tmpF = (float) 16000 / (float) 25600;
399 : }
400 : else /* st->last_L_frame == L_FRAME12k8 */
401 : {
402 570 : tmpF = (float) 16000 / (float) 12800;
403 : }
404 :
405 4415 : for ( i = 2 * NB_SUBFR - 1; i >= NB_SUBFR; i-- )
406 : {
407 3532 : st->old_pitch_buf[i + 2] = tmpF * st->old_pitch_buf[i];
408 : }
409 883 : st->old_pitch_buf[NB_SUBFR + 1] = st->old_pitch_buf[NB_SUBFR + 2];
410 :
411 4415 : for ( i = NB_SUBFR - 1; i >= 0; i-- )
412 : {
413 3532 : st->old_pitch_buf[i + 1] = tmpF * st->old_pitch_buf[i];
414 : }
415 883 : st->old_pitch_buf[0] = st->old_pitch_buf[1];
416 : }
417 : }
418 :
419 236258 : if ( st->bfi_pitch_frame != st->L_frame )
420 : {
421 3862 : if ( st->L_frame == L_FRAME )
422 : {
423 1364 : if ( st->bfi_pitch_frame == L_FRAME32k )
424 : {
425 347 : tmpF = (float) 12800 / (float) 32000;
426 : }
427 1017 : else if ( st->bfi_pitch_frame == 512 )
428 : {
429 361 : tmpF = (float) 12800 / (float) 25600;
430 : }
431 : else /* st->bfi_pitch_frame == L_FRAME16k */
432 : {
433 656 : tmpF = (float) 12800 / (float) 16000;
434 : }
435 1364 : st->bfi_pitch *= tmpF;
436 1364 : st->bfi_pitch_frame = L_FRAME;
437 : }
438 : else
439 : {
440 2498 : if ( st->bfi_pitch_frame == L_FRAME32k )
441 : {
442 42 : tmpF = (float) 16000 / (float) 32000;
443 : }
444 2456 : else if ( st->bfi_pitch_frame == 512 )
445 : {
446 273 : tmpF = (float) 16000 / (float) 25600;
447 : }
448 : else /* st->bfi_pitch_frame == L_FRAME12k8 */
449 : {
450 2183 : tmpF = (float) 16000 / (float) 12800;
451 : }
452 2498 : st->bfi_pitch *= tmpF;
453 2498 : st->bfi_pitch_frame = L_FRAME16k;
454 : }
455 : }
456 :
457 236258 : if ( st->last_bwidth == NB && st->bwidth != NB && st->ini_frame != 0 )
458 : {
459 0 : st->rate_switching_reset = 1;
460 : }
461 :
462 : /*----------------------------------------------------------------------*
463 : * GOOD frame
464 : *----------------------------------------------------------------------*/
465 :
466 236258 : if ( !st->bfi )
467 : {
468 :
469 : /*----------------------------------------------------------------*
470 : * Decoding of TC subframe classification
471 : *----------------------------------------------------------------*/
472 :
473 230327 : tc_subfr = -1;
474 230327 : if ( st->coder_type == TRANSITION )
475 : {
476 18176 : tc_subfr = tc_classif( st );
477 : }
478 :
479 : /*----------------------------------------------------------------*
480 : * Decoding of GSC IVAS mode
481 : *----------------------------------------------------------------*/
482 :
483 230327 : if ( st->element_mode > EVS_MONO && st->idchan == 0 && !( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 ) && !tdm_low_rate_mode )
484 : {
485 197398 : if ( st->coder_type == AUDIO || ( st->coder_type == INACTIVE && st->inactive_coder_type_flag ) )
486 : {
487 21926 : st->GSC_IVAS_mode = get_next_indice( st, 2 );
488 : }
489 : }
490 :
491 : /*----------------------------------------------------------------*
492 : * Decoding of inactive CNG frames
493 : *----------------------------------------------------------------*/
494 :
495 230327 : if ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 )
496 : {
497 : /* decode CNG parameters */
498 19421 : if ( st->cng_type == LP_CNG )
499 : {
500 1150 : CNG_dec( st, last_element_mode, Aq, lsp_new, lsf_new, &allow_cn_step, sid_bw, q_env );
501 :
502 : /* comfort noise generation */
503 1150 : local_element_mode = st->element_mode;
504 1150 : if ( ( nchan_out == 1 && st->element_mode == IVAS_CPE_DFT ) || ( st->masa_sid_format == 1 ) )
505 : {
506 1150 : local_element_mode = IVAS_SCE; /* For DFT Stereo mono decoding, run CNG_exc as in SCE */
507 : }
508 1150 : CNG_exc( st->core_brate, st->L_frame, &st->hTdCngDec->Enew, &st->hTdCngDec->cng_seed, exc, exc2, &st->lp_ener, st->last_core_brate, &st->first_CNG, &( st->hTdCngDec->cng_ener_seed ), bwe_exc, allow_cn_step, &st->hTdCngDec->last_allow_cn_step, st->hTdCngDec->num_ho, q_env, st->hTdCngDec->lp_env, st->hTdCngDec->old_env, st->hTdCngDec->exc_mem, st->hTdCngDec->exc_mem1, sid_bw, &st->hTdCngDec->cng_ener_seed1, exc3, st->Opt_AMR_WB, local_element_mode );
509 1150 : mvr2r( Aq, st->Aq_cng, M + 1 );
510 : }
511 : else
512 : {
513 18271 : if ( st->core_brate == SID_2k40 && st->element_mode != IVAS_CPE_MDCT )
514 : {
515 1285 : FdCng_decodeSID( st );
516 1285 : *sid_bw = 0;
517 : }
518 :
519 18271 : if ( st->element_mode == IVAS_CPE_DFT )
520 : {
521 3683 : assert( nchan_out == 1 );
522 :
523 92075 : for ( i = 0; i < NPART; i++ )
524 : {
525 88392 : st->hFdCngDec->hFdCngCom->sidNoiseEstLp[i] = STEREO_DFT_FD_FILT * st->hFdCngDec->hFdCngCom->sidNoiseEstLp[i] + ( 1 - STEREO_DFT_FD_FILT ) * st->hFdCngDec->hFdCngCom->sidNoiseEst[i];
526 : }
527 3683 : ApplyFdCng( syn, NULL, realBuffer, imagBuffer, st, 0, ( st->coder_type == AUDIO && !st->GSC_noisy_speech ) );
528 : }
529 :
530 18271 : if ( !read_sid_info )
531 : {
532 : float noise_lvl_highest;
533 :
534 3129 : noise_lvl_highest = st->hFdCngDec->hFdCngCom->cngNoiseLevel[st->hFdCngDec->hFdCngCom->stopFFTbin - st->hFdCngDec->hFdCngCom->startBand - 1];
535 65271 : for ( int16_t b = st->hFdCngDec->hFdCngCom->stopFFTbin - st->hFdCngDec->hFdCngCom->startBand; b < st->hFdCngDec->hFdCngCom->stopBand; b++ )
536 : {
537 62142 : st->hFdCngDec->hFdCngCom->cngNoiseLevel[b] = noise_lvl_highest;
538 : }
539 : }
540 :
541 18271 : generate_comfort_noise_dec( NULL, NULL, st, nchan_out );
542 :
543 18271 : FdCng_exc( st->hFdCngDec->hFdCngCom, &st->CNG_mode, st->L_frame, st->lsp_old, st->first_CNG, st->lspCNG, Aq, lsp_new, lsf_new, exc, exc2, bwe_exc );
544 :
545 18271 : mvr2r( exc2, exc3, st->L_frame );
546 : }
547 :
548 : /* update past excitation signals for LD music post-filter */
549 19421 : if ( st->hMusicPF != NULL )
550 : {
551 12175 : mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
552 12175 : mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
553 :
554 : /* Update music post processing values */
555 : /* Filter energies update */
556 7804175 : for ( i = 0; i < DCT_L_POST; i++ )
557 : {
558 7792000 : st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
559 : }
560 : }
561 :
562 : #ifdef DEBUG_MODE_ACELP
563 : dbgwrite( exc, sizeof( float ), st->L_frame, 1, fname( debug_dir, "exc", st->idchan, st->id_element, DEC ) );
564 : #endif
565 :
566 : /* synthesis at 12.8kHz sampling rate */
567 19421 : syn_12k8( st->L_frame, Aq, exc2, syn, st->mem_syn2, 1 );
568 19421 : syn_12k8( st->L_frame, Aq, exc3, syn1, st->mem_syn3, 1 );
569 :
570 : /* reset the decoder */
571 19421 : CNG_reset_dec( st, pitch_buf, voice_factors );
572 :
573 : /* update st->mem_syn1 for ACELP core switching */
574 19421 : mvr2r( st->mem_syn3, st->mem_syn1, M );
575 :
576 : /* update old synthesis for classification */
577 19421 : mvr2r( syn1 + st->L_frame - L_SYN_MEM_CLAS_ESTIM, st->mem_syn_clas_estim, L_SYN_MEM_CLAS_ESTIM );
578 :
579 : /* save and delay synthesis to be used by SWB BWE */
580 19421 : if ( st->hBWE_FD != NULL )
581 : {
582 12206 : save_old_syn( st->L_frame, syn1, old_syn_12k8_16k, st->hBWE_FD->old_syn_12k8_16k, st->preemph_fac, &st->hBWE_FD->mem_deemph_old_syn );
583 : }
584 : }
585 :
586 : /*----------------------------------------------------------------*
587 : * Decoding of all other frames
588 : *----------------------------------------------------------------*/
589 :
590 : else
591 : {
592 : /*-----------------------------------------------------------------*
593 : * Configure ACELP bit allocation
594 : *-----------------------------------------------------------------*/
595 :
596 210906 : nb_bits = 0;
597 210906 : st->acelp_cfg.FEC_mode = 0;
598 210906 : uc_two_stage_flag = 0;
599 :
600 210906 : if ( !st->nelp_mode_dec && !st->ppp_mode_dec )
601 : {
602 : int16_t tc_subfr_tmp;
603 :
604 210906 : tc_subfr_tmp = tc_subfr;
605 210906 : if ( tc_subfr_tmp < L_SUBFR )
606 : {
607 201375 : tc_subfr_tmp = 0;
608 : }
609 :
610 210906 : if ( tc_subfr == TC_0_192 )
611 : {
612 223 : nb_bits = -1;
613 : }
614 :
615 210906 : config_acelp1( DEC, st->total_brate, st->core_brate, st->core, st->extl_orig, st->extl_brate_orig, st->L_frame, st->GSC_noisy_speech, &( st->acelp_cfg ), st->next_bit_pos, st->coder_type, st->inactive_coder_type_flag, tc_subfr_tmp, 1, &nb_bits, unbits, st->element_mode, &uc_two_stage_flag, tdm_lp_reuse_flag, tdm_low_rate_mode, st->idchan, tdm_Pitch_reuse_flag, st->tdm_LRTD_flag, st->GSC_IVAS_mode );
616 :
617 210906 : if ( st->coder_type == TRANSITION && tc_subfr < L_SUBFR && st->L_frame == L_FRAME )
618 : {
619 5112 : config_acelp1( DEC, st->total_brate, st->core_brate, st->core, st->extl_orig, st->extl_brate_orig, st->L_frame, -1, &( st->acelp_cfg ), st->next_bit_pos, TRANSITION, -1, tc_subfr, 2, &nb_bits, unbits, st->element_mode, &uc_two_stage_flag, tdm_lp_reuse_flag, tdm_low_rate_mode, st->idchan, tdm_Pitch_reuse_flag, st->tdm_LRTD_flag, st->GSC_IVAS_mode );
620 : }
621 : }
622 :
623 : /*-----------------------------------------------------------------*
624 : * After CNG period, use the most up-to-date LSPs
625 : *-----------------------------------------------------------------*/
626 :
627 210906 : if ( st->hTdCngDec != NULL && ( st->last_core_brate == FRAME_NO_DATA || st->last_core_brate == SID_2k40 ) )
628 : {
629 620 : mvr2r( st->lspCNG, st->lsp_old, M );
630 620 : lsp2lsf( st->lspCNG, st->lsf_old, M, int_fs );
631 : }
632 :
633 : /*-----------------------------------------------------------------*
634 : * Reset higher ACELP pre-quantizer in case of switching
635 : *-----------------------------------------------------------------*/
636 :
637 210906 : if ( !st->use_acelp_preq )
638 : {
639 184398 : st->mem_preemp_preQ = 0.0f;
640 184398 : st->last_nq_preQ = 0;
641 184398 : st->last_code_preq = 0;
642 : }
643 210906 : st->use_acelp_preq = 0;
644 :
645 : /*-----------------------------------------------------------------*
646 : * LSF de-quantization and interpolation
647 : *-----------------------------------------------------------------*/
648 :
649 210906 : if ( !tdm_lp_reuse_flag )
650 : {
651 210763 : lsf_dec( st, tc_subfr, Aq, &LSF_Q_prediction, lsf_new, lsp_new, lsp_mid, tdm_low_rate_mode, tdm_lsfQ_PCh );
652 : }
653 : else
654 : {
655 : const float *pt_interp_2;
656 : int16_t beta_index;
657 :
658 143 : beta_index = get_next_indice( st, TDM_IC_LSF_PRED_BITS );
659 143 : tdm_SCh_lsf_reuse( DEC, st->element_brate, lsf_new, lsp_new, tdm_lsfQ_PCh, NULL, &beta_index );
660 :
661 143 : if ( st->rate_switching_reset )
662 : {
663 : /* extrapolation in case of unstable LSF convert */
664 0 : mvr2r( lsp_new, st->lsp_old, M );
665 0 : mvr2r( lsf_new, st->lsf_old, M );
666 : }
667 :
668 143 : pt_interp_2 = interpol_frac_12k8;
669 143 : if ( tdm_low_rate_mode == 1 && st->coder_type > UNVOICED )
670 : {
671 0 : pt_interp_2 = interpol_frac2;
672 : }
673 :
674 143 : if ( st->active_cnt == 1 )
675 : {
676 0 : mvr2r( lsp_new, st->lsp_old, M );
677 0 : lsp2lsf( lsp_new, st->lsf_old, M, st->sr_core );
678 : }
679 :
680 : /* LSP interpolation and conversion of LSPs to A(z) */
681 143 : int_lsp( st->L_frame, st->lsp_old, lsp_new, Aq, M, pt_interp_2, 0 );
682 : /* Check LSF stability (distance between old LSFs and current LSFs) */
683 143 : st->stab_fac = lsf_stab( lsf_new, st->lsf_old, 0, st->L_frame );
684 : }
685 :
686 210906 : if ( st->last_core == HQ_CORE && st->element_mode > EVS_MONO )
687 : {
688 : /* Prepare ACB memory from last HQ frame */
689 227 : old_exc_s = st->old_exc + L_EXC_MEM_DEC - st->L_frame;
690 227 : tmpF = *old_exc_s;
691 227 : st->mem_deemph = old_exc_s[st->L_frame - 1];
692 227 : preemph( old_exc_s, st->preemph_fac, st->L_frame, &tmpF );
693 227 : mvr2r( old_exc_s + st->L_frame - M, st->mem_syn2, M );
694 227 : residu( Aq, M, old_exc_s, old_exc + L_EXC_MEM_DEC - st->L_frame, st->L_frame );
695 : }
696 :
697 210906 : if ( st->last_core != ACELP_CORE && st->element_mode > EVS_MONO )
698 : {
699 : /* Prepare ACB memory of old_bwe_exc */
700 8249 : if ( st->L_frame == L_FRAME )
701 : {
702 3972 : lerp( old_exc, old_bwe_exc, L_EXC_MEM_DEC * HIBND_ACB_L_FAC, L_EXC_MEM_DEC );
703 : }
704 : else
705 : {
706 4277 : lerp( old_exc, old_bwe_exc, L_EXC_MEM_DEC * 2, L_EXC_MEM_DEC );
707 : }
708 : }
709 :
710 : /*-----------------------------------------------------------------*
711 : * FEC - first good frame after lost frame(s) (possibility to correct the ACB)
712 : *-----------------------------------------------------------------*/
713 :
714 210906 : if ( st->acelp_cfg.FEC_mode > 0 )
715 : {
716 170897 : last_pulse_pos = 0;
717 :
718 : /* decode the last glottal pulse position */
719 170897 : T0_tmp = FEC_pos_dec( st, &last_pulse_pos, &enr_q, nb_bits );
720 :
721 170897 : if ( st->last_core != HQ_CORE || ( st->last_core == HQ_CORE && st->last_con_tcx ) )
722 : {
723 170630 : if ( st->clas_dec == SIN_ONSET && last_pulse_pos != 0 && st->prev_bfi == 1 )
724 : {
725 3 : FEC_SinOnset( old_exc + L_EXC_MEM_DEC - L_EXC_MEM, last_pulse_pos, T0_tmp, enr_q, Aq, st->L_frame );
726 : }
727 170627 : else if ( ( st->coder_type == GENERIC || st->coder_type == VOICED ) && last_pulse_pos != 0 && st->old_bfi_cnt == 1 && st->hWIDec != NULL )
728 : {
729 0 : do_WI = FEC_enhACB( st->L_frame, st->last_L_frame, old_exc + L_EXC_MEM_DEC - L_EXC_MEM, T0_tmp, last_pulse_pos, st->bfi_pitch );
730 : }
731 : }
732 : }
733 :
734 : /*------------------------------------------------------------*
735 : * In case of first frame after an erasure and transition from voiced to unvoiced or inactive
736 : * redo the LPC interpolation
737 : *------------------------------------------------------------*/
738 :
739 210906 : if ( st->stab_fac == 0 && st->old_bfi_cnt > 0 && st->clas_dec != VOICED_CLAS && st->clas_dec != ONSET && st->relax_prev_lsf_interp == 0 && !( st->element_mode == IVAS_CPE_TD && st->idchan == 1 ) )
740 : {
741 200 : int_lsp4( st->L_frame, st->lsp_old, lsp_mid, lsp_new, Aq, M, 2 );
742 : }
743 :
744 : /*---------------------------------------------------------------*
745 : * Decoding of the scaled predicted innovation energy
746 : *---------------------------------------------------------------*/
747 :
748 210906 : if ( nb_bits > 0 )
749 : {
750 177860 : indice = get_next_indice( st, nb_bits );
751 177860 : Es_pred_dec( &Es_pred, indice, nb_bits, uc_two_stage_flag );
752 : }
753 :
754 : /*------------------------------------------------------------*
755 : * Decode excitation according to coding type
756 : *------------------------------------------------------------*/
757 :
758 210906 : if ( tdm_low_rate_mode ) /* tdm stereo low rate mode */
759 : {
760 7781 : if ( st->coder_type <= UNVOICED )
761 : {
762 7781 : tdm_low_rate_dec( st, dct_exc_tmp, &tmp_noise, pitch_buf, voice_factors, exc, exc2, bwe_exc, lsf_new );
763 : }
764 : else /* GENERIC */
765 : {
766 0 : decod_gen_2sbfr( st, sharpFlag, Aq, pitch_buf, voice_factors, exc, exc2, bwe_exc, gain_buf, tdm_Pitch_reuse_flag, p_tdm_Pri_pitch_buf );
767 :
768 0 : if ( st->element_mode == IVAS_CPE_TD )
769 : {
770 0 : tmp_noise = st->lp_gainc;
771 : }
772 : }
773 : }
774 203125 : else if ( st->nelp_mode_dec )
775 : {
776 : /* SC-VBR - NELP frames */
777 0 : decod_nelp( st, &tmp_noise, pitch_buf, exc, exc2, voice_factors, bwe_exc, st->bfi, gain_buf );
778 : }
779 203125 : else if ( st->coder_type == UNVOICED )
780 : {
781 : /* UNVOICED frames */
782 2063 : decod_unvoiced( st, Aq, Es_pred, uc_two_stage_flag, &tmp_noise, pitch_buf, voice_factors, exc, exc2, bwe_exc, gain_buf );
783 : }
784 201062 : else if ( st->ppp_mode_dec )
785 : {
786 : /* SC-VBR - PPP frames */
787 0 : if ( ( error = decod_ppp( st, Aq, pitch_buf, exc, exc2, voice_factors, bwe_exc, gain_buf, st->bfi ) ) != IVAS_ERR_OK )
788 : {
789 0 : return error;
790 : }
791 : }
792 201062 : else if ( st->coder_type == TRANSITION )
793 : {
794 18176 : decod_tran( st, st->L_frame, tc_subfr, Aq, Es_pred, pitch_buf, voice_factors, exc, exc2, bwe_exc, unbits, sharpFlag, gain_buf );
795 : }
796 182886 : else if ( st->coder_type == AUDIO || ( st->coder_type == INACTIVE && st->inactive_coder_type_flag ) )
797 : {
798 : /* AUDIO and INACTIVE frames (coded by GSC technology) */
799 21932 : decod_audio( st, dct_exc_tmp, Aq, &tmp_noise, pitch_buf, voice_factors, exc, exc2, bwe_exc, lsf_new, gain_buf, tdm_lp_reuse_flag, tdm_low_rate_mode, tdm_Pitch_reuse_flag, p_tdm_Pri_pitch_buf );
800 : }
801 : else
802 : {
803 : /* GENERIC, VOICED and INACTIVE frames (coded by AVQ technology) */
804 160954 : if ( ( error = decod_gen_voic( st, st->L_frame, sharpFlag, Aq, Es_pred, do_WI, pitch_buf, voice_factors, exc, exc2, bwe_exc, unbits, gain_buf, tdm_Pitch_reuse_flag, p_tdm_Pri_pitch_buf ) ) != IVAS_ERR_OK )
805 : {
806 0 : return error;
807 : }
808 :
809 160954 : if ( st->element_mode == IVAS_CPE_TD )
810 : {
811 6723 : tmp_noise = st->lp_gainc;
812 : }
813 : }
814 :
815 : #ifdef DEBUG_MODE_ACELP
816 : dbgwrite( exc, sizeof( float ), st->L_frame, 1, fname( debug_dir, "exc", st->idchan, st->id_element, DEC ) );
817 : #endif
818 :
819 : /* synthesis for ACELP core switching and SWB BWE */
820 210906 : syn_12k8( st->L_frame, Aq, exc, temp_buf, st->mem_syn1, 1 );
821 :
822 : /* save and delay synthesis to be used by SWB BWE */
823 210906 : if ( st->hBWE_FD != NULL )
824 : {
825 210788 : save_old_syn( st->L_frame, temp_buf, old_syn_12k8_16k, st->hBWE_FD->old_syn_12k8_16k, st->preemph_fac, &st->hBWE_FD->mem_deemph_old_syn );
826 : }
827 :
828 : /*-----------------------------------------------------------------*
829 : * Apply energy matching when switching to inactive frames
830 : *-----------------------------------------------------------------*/
831 :
832 210906 : inact_switch_ematch( exc2, dct_exc_tmp, st->hGSCDec->lt_ener_per_band, st->coder_type, st->inactive_coder_type_flag, st->L_frame, st->bfi, st->last_core, st->last_codec_mode, tdm_low_rate_mode, st->element_mode );
833 :
834 : /*------------------------------------------------------------*
835 : * Decode information and modify the excitation signal of stationary unvoiced frames
836 : *------------------------------------------------------------*/
837 :
838 210906 : if ( !( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) && st->nelp_mode_dec != 1 && !( st->element_mode == IVAS_SCE && tdm_low_rate_mode ) )
839 : {
840 199480 : stat_noise_uv_dec( st, lsp_new, lsp_mid, Aq, exc2, uc_two_stage_flag );
841 : }
842 :
843 : /*------------------------------------------------------------*
844 : * Save filter memory in case the synthesis is redone after scaling
845 : * Synthesis at 12k8 Hz sampling rate
846 : *------------------------------------------------------------*/
847 :
848 : /* update past excitation signals for LD music post-filter */
849 210906 : if ( st->hMusicPF != NULL )
850 : {
851 210906 : mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
852 210906 : mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
853 210906 : mvr2r( st->hMusicPF->dct_post_old_exc, exc_buffer, DCT_L_POST - OFFSET2 );
854 : }
855 :
856 210906 : if ( ( st->coder_type == AUDIO && !st->GSC_noisy_speech ) || ( st->GSC_IVAS_mode >= 1 && st->L_frame == L_FRAME ) )
857 16483 : {
858 16483 : int16_t last_coder_type = st->last_coder_type;
859 :
860 16483 : if ( ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) || ( st->GSC_IVAS_mode >= 1 && st->GSC_noisy_speech == 0 ) )
861 : {
862 10680 : last_coder_type = AUDIO;
863 : }
864 :
865 : /* Extrapolation of the last future part, windowing and high resolution DCT transform */
866 16483 : Prep_music_postP( exc_buffer, dct_buffer, st->hMusicPF->filt_lfE, st->last_core, pitch_buf, st->hMusicPF->LDm_enh_lp_gbin );
867 :
868 : /* LD music post-filter */
869 16483 : LD_music_post_filter( st->hMusicPF, dct_buffer, dct_buffer, st->core_brate, AUDIO, last_coder_type );
870 :
871 : /* Inverse DCT transform, retrieval of the aligned excitation, re-synthesis */
872 16483 : mvr2r( st->mem_syn2, mem_tmp, M );
873 16483 : Post_music_postP( dct_buffer, exc_buffer, exc2, st->mem_syn2, st->mem_syn2, Aq, syn );
874 : }
875 : else
876 : {
877 : /* Core synthesis at 12.8kHz or 16kHz */
878 194423 : mvr2r( st->mem_syn2, mem_tmp, M );
879 194423 : syn_12k8( st->L_frame, Aq, exc2, syn, st->mem_syn2, 1 );
880 :
881 194423 : if ( st->hMusicPF != NULL )
882 : {
883 124625143 : for ( i = 0; i < DCT_L_POST; i++ )
884 : {
885 124430720 : st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
886 : }
887 : }
888 : }
889 :
890 : /*------------------------------------------------------------*
891 : * FEC - Estimate the classification information
892 : *------------------------------------------------------------*/
893 :
894 210906 : FEC_clas_estim( syn, pitch_buf, st->L_frame, st->coder_type, st->codec_mode, st->mem_syn_clas_estim, &st->clas_dec, &st->lp_ener_bfi, st->Opt_AMR_WB, &st->decision_hyst, NULL, NULL, NULL, NULL, NULL, NULL, temp_buf, 0, 0, 0, 0, 0, 0, st->last_core_brate, st->acelp_cfg.FEC_mode );
895 :
896 : /*------------------------------------------------------------*
897 : * FEC - Estimate pitch
898 : *------------------------------------------------------------*/
899 :
900 210906 : FEC_pitch_estim( st->Opt_AMR_WB, st->last_core, st->L_frame, st->clas_dec, st->last_good, pitch_buf, st->old_pitch_buf, &st->bfi_pitch, &st->bfi_pitch_frame, &st->upd_cnt, st->coder_type );
901 :
902 : /*------------------------------------------------------------*
903 : * FEC - Smooth the speech energy evolution when recovering after a BAD frame
904 : * (smoothing is performed in the excitation domain and signal is resynthesized after)
905 : *------------------------------------------------------------*/
906 :
907 210906 : FEC_scale_syn( st->L_frame, st->clas_dec, st->last_good, syn, pitch_buf, st->enr_old, enr_q, st->coder_type, LSF_Q_prediction, &st->scaling_flag, &st->lp_ener_FEC_av, &st->lp_ener_FEC_max, st->bfi, st->total_brate, st->prev_bfi, st->last_core_brate, exc, exc2, Aq, &st->old_enr_LP, mem_tmp, st->mem_syn2, st->last_con_tcx && ( st->L_frameTCX_past != st->L_frame ) && ( st->last_core != ACELP_CORE ), 0 );
908 :
909 : /* estimate the pitch-synchronous speech energy per sample to be used when normal operation recovers */
910 210906 : if ( ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD && st->total_brate <= ACELP_7k20 ) || ( st->total_brate == ACELP_7k20 ) || ( st->total_brate == ACELP_8k00 ) )
911 : {
912 123 : fer_energy( st->L_frame, st->clas_dec, syn, pitch_buf[( ( st->L_frame ) >> 6 ) - 1], &st->enr_old, st->L_frame );
913 : }
914 : }
915 :
916 : } /* End of GOOD FRAME */
917 :
918 : /*----------------------------------------------------------------*
919 : * BAD frame
920 : *----------------------------------------------------------------*/
921 :
922 : else
923 : {
924 : /* SC-VBR */
925 5931 : if ( st->last_nelp_mode_dec == 1 )
926 : {
927 0 : st->nelp_mode_dec = 1;
928 : }
929 :
930 : /* long burst frame erasures */
931 5931 : if ( st->nbLostCmpt > 5 && st->clas_dec >= VOICED_CLAS && st->clas_dec < INACTIVE_CLAS )
932 : {
933 19 : st->last_good = VOICED_TRANSITION;
934 : }
935 :
936 : /* LSF estimation and A(z) calculation */
937 5931 : lsf_dec_bfi( MODE1, lsf_new, st->lsf_old, st->lsf_adaptive_mean, NULL, st->mem_MA, st->mem_AR, st->stab_fac, st->last_coder_type, st->L_frame, st->last_good, st->nbLostCmpt, 0, NULL, NULL, NULL, st->hGSCDec->Last_GSC_pit_band_idx, st->Opt_AMR_WB, st->bwidth );
938 :
939 5931 : FEC_lsf2lsp_interp( st, st->L_frame, Aq, lsf_new, lsp_new );
940 :
941 5931 : if ( st->nelp_mode_dec == 1 )
942 : {
943 : /* SC-VBR */
944 0 : decod_nelp( st, &tmp_noise, pitch_buf, exc, exc2, voice_factors, bwe_exc, st->bfi, gain_buf );
945 0 : FEC_pitch = pitch_buf[3];
946 : }
947 : else
948 : {
949 : /* calculation of excitation signal */
950 5931 : FEC_exc_estim( st, st->L_frame, exc, exc2, dct_exc_tmp, pitch_buf, voice_factors, &FEC_pitch, bwe_exc, lsf_new, &tmp_noise );
951 :
952 5931 : tmp_noise = st->lp_gainc;
953 :
954 : /* SC-VBR */
955 5931 : st->prev_gain_pit_dec = st->lp_gainp;
956 : }
957 :
958 : /* synthesis for ACELP core switching and SWB BWE */
959 5931 : syn_12k8( st->L_frame, Aq, exc, temp_buf, st->mem_syn1, 1 );
960 :
961 : /* save and delay synthesis to be used by SWB BWE */
962 5931 : if ( st->hBWE_FD != NULL )
963 : {
964 5924 : save_old_syn( st->L_frame, temp_buf, old_syn_12k8_16k, st->hBWE_FD->old_syn_12k8_16k, st->preemph_fac, &st->hBWE_FD->mem_deemph_old_syn );
965 : }
966 :
967 : /* Apply energy matching when switching to inactive frames */
968 5931 : inact_switch_ematch( exc2, dct_exc_tmp, st->hGSCDec->lt_ener_per_band, st->coder_type, st->inactive_coder_type_flag, st->L_frame, st->bfi, st->last_core, st->last_codec_mode, tdm_low_rate_mode, st->element_mode );
969 : /* update past excitation signals for LD music post-filter */
970 5931 : if ( st->hMusicPF != NULL )
971 : {
972 5931 : mvr2r( st->hMusicPF->dct_post_old_exc + L_FRAME, st->hMusicPF->dct_post_old_exc, DCT_L_POST - L_FRAME - OFFSET2 );
973 5931 : mvr2r( exc2, st->hMusicPF->dct_post_old_exc + ( DCT_L_POST - L_FRAME - OFFSET2 ), L_FRAME );
974 :
975 : /* Update music post processing values */
976 : /* Filter energies update */
977 3801771 : for ( i = 0; i < DCT_L_POST; i++ )
978 : {
979 3795840 : st->hMusicPF->filt_lfE[i] = 0.3f + 0.7f * st->hMusicPF->filt_lfE[i];
980 : }
981 : /* Update circular buffer, keep last energy difference unchanged */
982 237240 : for ( i = 1; i < MAX_LT; i++ )
983 : {
984 231309 : st->hMusicPF->LDm_lt_diff_etot[i - 1] = st->hMusicPF->LDm_lt_diff_etot[i];
985 : }
986 : }
987 :
988 : /* synthesis at 12k8 Hz sampling rate */
989 5931 : if ( ( st->total_brate == ACELP_7k20 ) || ( st->total_brate == ACELP_8k00 ) )
990 : {
991 3 : mvr2r( st->mem_syn2, mem_tmp, M );
992 : }
993 5931 : syn_12k8( st->L_frame, Aq, exc2, syn, st->mem_syn2, 1 );
994 :
995 : /* update buffer for classifier */
996 5931 : if ( st->hWIDec != NULL )
997 : {
998 0 : mvr2r( exc2 + st->L_frame - L_EXC_MEM, st->hWIDec->old_exc2, L_EXC_MEM );
999 0 : mvr2r( syn + st->L_frame - L_EXC_MEM, st->hWIDec->old_syn2, L_EXC_MEM );
1000 : }
1001 5931 : mvr2r( syn + st->L_frame - L_SYN_MEM_CLAS_ESTIM, st->mem_syn_clas_estim, L_SYN_MEM_CLAS_ESTIM );
1002 :
1003 : /*------------------------------------------------------------*
1004 : * FEC - Smooth the speech energy evolution when recovering after a BAD frame
1005 : * (smoothing is performed in the excitation domain and signal is resynthesized after)
1006 : *------------------------------------------------------------*/
1007 :
1008 5931 : if ( ( st->total_brate == ACELP_7k20 ) || ( st->total_brate == ACELP_8k00 ) )
1009 : {
1010 3 : FEC_scale_syn( st->L_frame, st->clas_dec, st->last_good, syn, pitch_buf, st->enr_old, enr_q, st->coder_type, LSF_Q_prediction, &st->scaling_flag, &st->lp_ener_FEC_av, &st->lp_ener_FEC_max, st->bfi, st->total_brate, st->prev_bfi, st->last_core_brate, exc, exc2, Aq, &st->old_enr_LP, mem_tmp, st->mem_syn2, st->last_con_tcx && ( st->L_frameTCX_past != st->L_frame ) && ( st->last_core != ACELP_CORE ), 0 );
1011 : }
1012 :
1013 : /* estimate the pitch-synchronous speech energy per sample to be used when normal operation recovers */
1014 5931 : fer_energy( st->L_frame, st->last_good, syn, FEC_pitch, &st->enr_old, st->L_frame );
1015 :
1016 5931 : if ( st->nelp_mode_dec != 1 )
1017 : {
1018 : /* modify the excitation signal of stationary unvoiced frames */
1019 5931 : stat_noise_uv_mod( st->coder_type, 0, st->lsp_old, lsp_new, lsp_new, Aq, exc2, 1, &st->ge_sm, &st->uv_count, &st->act_count, st->lspold_s, &st->noimix_seed, &st->min_alpha, &st->exc_pe, st->core_brate, st->bwidth );
1020 : }
1021 : }
1022 :
1023 236258 : if ( st->hBWE_TD != NULL )
1024 : {
1025 228918 : if ( st->L_frame == L_FRAME )
1026 : {
1027 123126 : mvr2r( Aq + 2 * ( M + 1 ), st->hBWE_TD->cur_sub_Aq, ( M + 1 ) );
1028 : }
1029 : else
1030 : {
1031 105792 : mvr2r( Aq + 3 * ( M + 1 ), st->hBWE_TD->cur_sub_Aq, ( M + 1 ) );
1032 : }
1033 : }
1034 :
1035 : /*--------------------------------------------------------*
1036 : * Apply NB postfilter in case of 8kHz output
1037 : *--------------------------------------------------------*/
1038 :
1039 236258 : if ( st->last_bwidth == NB && st->hPFstat != NULL )
1040 : {
1041 3122 : if ( st->bwidth == NB )
1042 : {
1043 0 : st->hPFstat->on = 1;
1044 0 : nb_post_filt( st->L_frame, L_SUBFR, st->hPFstat, &st->psf_lp_noise, tmp_noise, syn, Aq, pitch_buf, st->coder_type, st->BER_detect, 0 );
1045 : }
1046 : else
1047 : {
1048 3122 : st->hPFstat->on = 0;
1049 3122 : nb_post_filt( st->L_frame, L_SUBFR, st->hPFstat, &st->psf_lp_noise, tmp_noise, syn, Aq, pitch_buf, AUDIO, st->BER_detect, 0 );
1050 : }
1051 : }
1052 : else
1053 : {
1054 233136 : st->psf_lp_noise = st->lp_noise;
1055 : }
1056 :
1057 : /*------------------------------------------------------------------*
1058 : * Perform fixed deemphasis through 1/(1 - g*z^-1)
1059 : *-----------------------------------------------------------------*/
1060 :
1061 : /* update old synthesis buffer - needed for ACELP internal sampling rate switching */
1062 236258 : mvr2r( syn + st->L_frame - L_SYN_MEM, st->mem_syn_r, L_SYN_MEM );
1063 236258 : deemph( syn, st->preemph_fac, st->L_frame, &( st->mem_deemph ) );
1064 :
1065 236258 : AGC_dec( syn, st->agc_mem2, st->L_frame );
1066 :
1067 236258 : if ( st->hTcxDec != NULL )
1068 : {
1069 232458 : mvr2r( syn + st->L_frame / 2, st->hTcxDec->old_syn_Overl, st->L_frame / 2 );
1070 : }
1071 236258 : mvr2r( syn + st->L_frame - M - 1, st->syn, M + 1 );
1072 :
1073 : /*------------------------------------------------------------------*
1074 : * Formant post-filter
1075 : *-----------------------------------------------------------------*/
1076 :
1077 236258 : if ( st->hPFstat != NULL && st->last_bwidth >= WB && ( st->core_brate > ACELP_24k40 || st->element_mode > EVS_MONO ) && st->core_brate <= ACELP_32k )
1078 : {
1079 209115 : mvr2r( syn, temp_buf + L_SYN_MEM, L_FRAME16k );
1080 :
1081 209115 : st->hPFstat->on = 1;
1082 209115 : formant_post_filt( st->hPFstat, temp_buf + L_SYN_MEM, Aq, syn, st->L_frame, L_SUBFR, st->lp_noise, st->total_brate, 0 );
1083 : }
1084 27143 : else if ( st->hPFstat != NULL && st->last_bwidth >= WB )
1085 : {
1086 16775 : if ( st->hPFstat->on )
1087 : {
1088 2436 : mvr2r( st->hPFstat->mem_pf_in + L_SYN_MEM - M, temp_buf, M );
1089 2436 : mvr2r( syn, temp_buf + M, L_SUBFR );
1090 2436 : residu( Aq, M, temp_buf + M, temp_buf + M + L_SUBFR, L_SUBFR );
1091 2436 : syn_filt( Aq, M, temp_buf + M + L_SUBFR, temp_buf, L_SUBFR, st->hPFstat->mem_stp + L_SYN_MEM - M, 0 );
1092 2436 : scale_st( syn, temp_buf, &st->hPFstat->gain_prec, L_SUBFR, -1 );
1093 2436 : mvr2r( temp_buf, syn, L_SUBFR / 2 );
1094 2436 : blend_subfr2( temp_buf + L_SUBFR / 2, syn + L_SUBFR / 2, syn + L_SUBFR / 2 );
1095 : }
1096 16775 : st->hPFstat->on = 0;
1097 : }
1098 :
1099 : /*----------------------------------------------------------------*
1100 : * Comfort noise addition
1101 : *----------------------------------------------------------------*/
1102 :
1103 236258 : if ( ( st->hFdCngDec != NULL || st->idchan == 1 ) && st->element_mode != IVAS_CPE_MDCT )
1104 : {
1105 229012 : if ( st->element_mode == IVAS_CPE_TD || st->flag_cna || ( st->cng_type == FD_CNG && st->total_brate <= ACELP_32k ) || ( st->cng_type == LP_CNG && st->core_brate <= SID_2k40 ) )
1106 : {
1107 : /*VAD only for non inactive frame*/
1108 194160 : st->VAD = st->VAD && ( st->coder_type != INACTIVE );
1109 :
1110 194160 : if ( st->idchan == 0 && ( st->flag_cna || ( st->cng_type == FD_CNG && st->total_brate <= ACELP_32k ) || ( st->cng_type == LP_CNG && st->core_brate <= SID_2k40 ) ) )
1111 : {
1112 : /*Noisy speech detector*/
1113 188863 : noisy_speech_detection( st->hFdCngDec, st->VAD, syn );
1114 :
1115 188863 : st->hFdCngDec->hFdCngCom->likelihood_noisy_speech = 0.99f * st->hFdCngDec->hFdCngCom->likelihood_noisy_speech + 0.01f * (float) st->hFdCngDec->hFdCngCom->flag_noisy_speech;
1116 : }
1117 :
1118 194160 : if ( st->idchan == 0 )
1119 : {
1120 190360 : st->lp_noise = st->hFdCngDec->lp_noise;
1121 : }
1122 :
1123 194160 : if ( st->element_mode != IVAS_CPE_TD && !st->cng_ism_flag )
1124 : {
1125 : /*Noise estimate*/
1126 181466 : ApplyFdCng( syn, NULL, realBuffer, imagBuffer, st, 0, ( st->coder_type == AUDIO && !st->GSC_noisy_speech ) );
1127 : }
1128 :
1129 194160 : if ( !st->cna_dirac_flag )
1130 : {
1131 : /* CNA: Generate additional comfort noise to mask potential coding artefacts */
1132 188012 : if ( st->flag_cna && !( st->coder_type == AUDIO && !( st->element_mode > EVS_MONO && st->GSC_noisy_speech ) ) )
1133 : {
1134 105730 : if ( st->element_mode == IVAS_CPE_TD && nchan_out == 2 )
1135 : {
1136 928 : if ( hStereoCng->flag_cna_fade )
1137 : {
1138 12 : generate_stereo_masking_noise( syn, st, hStereoTD, flag_sec_CNA, 1, hStereoCng, nchan_out );
1139 12 : hStereoCng->flag_cna_fade = 0;
1140 : }
1141 : else
1142 : {
1143 916 : if ( st->element_mode != last_element_mode && st->idchan == 0 )
1144 : {
1145 : /* Clear memory for secondary channel CNA */
1146 3 : set_f( hStereoCng->olapBufferSynth22, 0.0f, st->hFdCngDec->hFdCngCom->frameSize / 2 );
1147 : }
1148 :
1149 916 : generate_stereo_masking_noise( syn, st, hStereoTD, flag_sec_CNA, 0, hStereoCng, nchan_out );
1150 : }
1151 : }
1152 104802 : else if ( st->element_mode != IVAS_CPE_DFT )
1153 : {
1154 51801 : if ( st->idchan == 0 )
1155 : {
1156 51690 : if ( st->element_mode != last_element_mode )
1157 : {
1158 0 : set_f( st->hFdCngDec->hFdCngCom->olapBufferSynth2, 0.0f, st->hFdCngDec->hFdCngCom->fftlen );
1159 : }
1160 51690 : generate_masking_noise( syn, st->hFdCngDec->hFdCngCom, st->hFdCngDec->hFdCngCom->frameSize, 0, 0, 0, st->element_mode, hStereoCng, nchan_out );
1161 : }
1162 : }
1163 : }
1164 82282 : else if ( st->flag_cna && st->coder_type == AUDIO && ( ( st->last_core == ACELP_CORE && !( st->last_coder_type == AUDIO && !( st->element_mode > EVS_MONO && st->Last_GSC_noisy_speech_flag ) ) ) || st->last_core == TCX_20_CORE ) )
1165 : {
1166 2147 : if ( st->element_mode == IVAS_CPE_TD && nchan_out == 2 )
1167 : {
1168 0 : generate_stereo_masking_noise( syn, st, hStereoTD, flag_sec_CNA, 1, hStereoCng, nchan_out );
1169 0 : hStereoCng->flag_cna_fade = 1;
1170 : }
1171 : else
1172 : {
1173 2147 : v_multc( st->hFdCngDec->hFdCngCom->olapBufferSynth2 + 5 * st->hFdCngDec->hFdCngCom->frameSize / 4, (float) ( st->hFdCngDec->hFdCngCom->fftlen / 2 ), temp_buf, st->hFdCngDec->hFdCngCom->frameSize / 2 );
1174 2147 : v_add( temp_buf, syn, syn, st->hFdCngDec->hFdCngCom->frameSize / 2 );
1175 : }
1176 : }
1177 : else
1178 : {
1179 80135 : if ( hStereoCng != NULL )
1180 : {
1181 13101 : hStereoCng->flag_cna_fade = 1;
1182 13101 : hStereoCng->enableSecCNA = 0;
1183 : }
1184 : }
1185 :
1186 188012 : if ( st->element_mode == IVAS_CPE_TD )
1187 : {
1188 : /*Noise estimate*/
1189 7432 : if ( st->idchan == 0 && ( nchan_out == 2 || ( st->core_brate != FRAME_NO_DATA && st->core_brate != SID_2k40 ) ) )
1190 : {
1191 3632 : ApplyFdCng( syn, NULL, realBuffer, imagBuffer, st, 0, ( st->coder_type == AUDIO && !st->GSC_noisy_speech ) );
1192 : }
1193 : }
1194 : }
1195 : }
1196 :
1197 229012 : if ( !st->cna_dirac_flag )
1198 : {
1199 219308 : if ( st->flag_cna == 0 && st->L_frame == L_FRAME16k && st->last_flag_cna == 1 && ( ( st->last_core == ACELP_CORE && !( st->last_coder_type == AUDIO && !( st->element_mode > EVS_MONO && st->Last_GSC_noisy_speech_flag ) ) ) || st->last_core == AMR_WB_CORE ) )
1200 : {
1201 124 : v_multc( st->hFdCngDec->hFdCngCom->olapBufferSynth2 + 5 * st->L_frame / 4, 256.f, temp_buf, st->L_frame / 2 );
1202 124 : v_add( temp_buf, syn, syn, st->L_frame / 2 );
1203 : }
1204 :
1205 219308 : if ( st->flag_cna == 0 || ( st->coder_type == AUDIO && !( st->element_mode > EVS_MONO && st->GSC_noisy_speech ) ) )
1206 : {
1207 113578 : if ( st->idchan == 0 )
1208 : {
1209 110359 : set_f( st->hFdCngDec->hFdCngCom->olapBufferSynth2, 0.f, st->hFdCngDec->hFdCngCom->fftlen );
1210 : }
1211 113578 : if ( hStereoCng != NULL && st->idchan == 0 )
1212 : {
1213 10673 : set_f( hStereoCng->olapBufferSynth22, 0.f, st->hFdCngDec->hFdCngCom->fftlen );
1214 : }
1215 : }
1216 : }
1217 : }
1218 :
1219 : #ifdef DEBUG_MODE_ACELP
1220 : dbgwrite( syn, sizeof( float ), st->L_frame, 1, fname( debug_dir, "syn.intFs", st->idchan, st->id_element, DEC ) );
1221 : #endif
1222 :
1223 : /*----------------------------------------------------------------*
1224 : * Resample to the output sampling rate (8/16/32/48 kHz)
1225 : * Bass post-filter
1226 : *----------------------------------------------------------------*/
1227 :
1228 : /* check if the CLDFB works on the right sample rate */
1229 236258 : if ( ( st->cldfbAna->no_channels * st->cldfbAna->no_col ) != st->L_frame )
1230 : {
1231 3993 : resampleCldfb( st->cldfbAna, st->L_frame * FRAMES_PER_SEC );
1232 3993 : resampleCldfb( st->cldfbBPF, st->L_frame * FRAMES_PER_SEC );
1233 :
1234 3993 : if ( st->ini_frame > 0 )
1235 : {
1236 2347 : st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
1237 : }
1238 : }
1239 :
1240 : /* analyze pitch coherence for bass post-filter */
1241 236258 : bpf_pitch_coherence( st, pitch_buf );
1242 :
1243 236258 : if ( !( st->element_mode == IVAS_CPE_MDCT && st->bpf_off ) )
1244 : {
1245 229012 : if ( st->L_frame != st->last_L_frame && st->last_codec_mode != MODE2 )
1246 : {
1247 2023 : if ( st->L_frame == L_FRAME )
1248 : {
1249 1382 : retro_interp5_4( st->hBPF->pst_old_syn );
1250 : }
1251 641 : else if ( st->L_frame == L_FRAME16k )
1252 : {
1253 641 : retro_interp4_5( syn, st->hBPF->pst_old_syn );
1254 : }
1255 : }
1256 :
1257 : /* bass post-filter */
1258 229012 : bass_psfilter( st->hBPF, st->Opt_AMR_WB, syn, st->L_frame, pitch_buf, st->bpf_off, st->stab_fac, &st->stab_fac_smooth, st->coder_type, bpf_error_signal );
1259 : }
1260 :
1261 236258 : if ( st->element_mode != IVAS_CPE_DFT || use_cldfb_for_dft )
1262 210977 : {
1263 : float realBufferSave[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
1264 : float imagBufferSave[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
1265 : float *pRealSave[CLDFB_NO_COL_MAX], *pImagSave[CLDFB_NO_COL_MAX];
1266 3586609 : for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
1267 : {
1268 3375632 : pRealSave[i] = realBufferSave[i];
1269 3375632 : pImagSave[i] = imagBufferSave[i];
1270 : }
1271 210977 : if ( st->p_bpf_noise_buf )
1272 : {
1273 197669 : mvr2r( bpf_error_signal, st->p_bpf_noise_buf, st->L_frame );
1274 : }
1275 :
1276 : /* analysis of the synthesis at internal sampling rate */
1277 210977 : cldfbAnalysis( syn, realBuffer, imagBuffer, -1, st->cldfbAna );
1278 :
1279 : /* analysis and add the BPF error signal */
1280 210977 : addBassPostFilter( bpf_error_signal, st->bpf_off ? 0 : -1, realBuffer, imagBuffer, st->cldfbBPF );
1281 :
1282 : /* set output mask for upsampling */
1283 210977 : if ( st->bwidth == NB )
1284 : {
1285 : /* set NB mask for upsampling */
1286 0 : st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - 10;
1287 : }
1288 210977 : else if ( st->cldfbSyn->bandsToZero != st->cldfbSyn->no_channels - st->cldfbAna->no_channels )
1289 : {
1290 : /* in case of BW switching, re-init to default */
1291 15788 : st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
1292 : }
1293 :
1294 210977 : if ( !st->cng_sba_flag || st->element_mode == IVAS_CPE_MDCT )
1295 : {
1296 : /*WB/SWB-FD_CNG*/
1297 186026 : if ( ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 ) && ( st->cng_type == FD_CNG ) && ( st->hFdCngDec->hFdCngCom->numCoreBands < st->cldfbSyn->no_channels ) )
1298 : {
1299 14965 : generate_comfort_noise_dec_hf( realBuffer, imagBuffer, st->hFdCngDec->hFdCngCom, st->cng_ism_flag );
1300 :
1301 14965 : if ( st->hFdCngDec->hFdCngCom->regularStopBand < st->cldfbSyn->no_channels )
1302 : {
1303 11320 : st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->hFdCngDec->hFdCngCom->regularStopBand;
1304 : }
1305 : else
1306 : {
1307 3645 : st->cldfbSyn->bandsToZero = 0;
1308 : }
1309 : }
1310 : }
1311 :
1312 210977 : if ( save_hb_synth != NULL )
1313 : {
1314 : /* save and then zero-out lowband */
1315 5551 : for ( int16_t j = 0; j < CLDFB_NO_CHANNELS_MAX; j++ )
1316 : {
1317 92820 : for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
1318 : {
1319 87360 : realBufferSave[i][j] = realBuffer[i][j];
1320 87360 : imagBufferSave[i][j] = imagBuffer[i][j];
1321 87360 : if ( j < st->hFdCngDec->hFdCngCom->numCoreBands && i < st->hFdCngDec->hFdCngCom->numSlots )
1322 : {
1323 29120 : realBuffer[i][j] = 0.0f;
1324 29120 : imagBuffer[i][j] = 0.0f;
1325 : }
1326 : }
1327 : }
1328 :
1329 91 : cldfbSynthesis( realBuffer, imagBuffer, save_hb_synth, -1, st->cldfbSynHB );
1330 :
1331 : /* restore lowband */
1332 5551 : for ( int16_t j = 0; j < CLDFB_NO_CHANNELS_MAX; j++ )
1333 : {
1334 92820 : for ( i = 0; i < CLDFB_NO_COL_MAX; i++ )
1335 : {
1336 87360 : realBuffer[i][j] = realBufferSave[i][j];
1337 87360 : imagBuffer[i][j] = imagBufferSave[i][j];
1338 : }
1339 : }
1340 :
1341 91 : cldfbSynthesis( pRealSave, pImagSave, synth, -1, st->cldfbSyn );
1342 : }
1343 : else
1344 : {
1345 : /* synthesis of the combined signal */
1346 210886 : cldfbSynthesis( realBuffer, imagBuffer, synth, -1, st->cldfbSyn );
1347 : }
1348 :
1349 : /* save synthesis - needed in case of core switching */
1350 210977 : mvr2r( synth, st->previoussynth, output_frame );
1351 :
1352 : #ifdef DEBUG_MODE_ACELP
1353 : dbgwrite( synth, sizeof( float ), output_frame, 1, fname( debug_dir, "output.Fs", st->idchan, st->id_element, DEC ) );
1354 : #endif
1355 : }
1356 : else
1357 : {
1358 25281 : int16_t nSamples = NS2SA( st->L_frame * FRAMES_PER_SEC, FRAME_SIZE_NS /*DELAY_CLDFB_NS*/ ); /* IVAS-64: optimization is likely possible here (don't resample the whole frame) */
1359 :
1360 : /* analysis of the synthesis at internal sampling rate - needed for DFT stereo -> TD stereo switching */
1361 25281 : cldfbAnalysis( syn + st->L_frame - nSamples, realBuffer, imagBuffer, nSamples, st->cldfbAna );
1362 :
1363 : /* analysis and add the BPF error signal - needed for DFT stereo -> TD stereo switching */
1364 25281 : addBassPostFilter( bpf_error_signal + st->L_frame - nSamples, st->bpf_off ? 0 : nSamples, realBuffer, imagBuffer, st->cldfbBPF );
1365 :
1366 : /* synthesis of the combined signal - needed for DFT stereo -> TD stereo switching */
1367 25281 : cldfbSynthesis( realBuffer, imagBuffer, synth /*dummy*/, NS2SA( st->output_Fs, FRAME_SIZE_NS /*DELAY_CLDFB_NS*/ ), st->cldfbSyn );
1368 :
1369 25281 : if ( st->p_bpf_noise_buf )
1370 : {
1371 24486 : mvr2r( bpf_error_signal, st->p_bpf_noise_buf, st->L_frame );
1372 : }
1373 :
1374 : #ifdef DEBUG_MODE_ACELP
1375 : dbgwrite( synth, sizeof( float ), output_frame, 1, fname( debug_dir, "output.Fs", st->idchan, st->id_element, DEC ) );
1376 : #endif
1377 25281 : set_f( synth, 0.0f, output_frame );
1378 : }
1379 :
1380 : /* Copy output signal */
1381 236258 : if ( st->element_mode > EVS_MONO )
1382 : {
1383 234176 : mvr2r( syn, output, st->L_frame );
1384 : }
1385 :
1386 : /*-----------------------------------------------------------------*
1387 : * Bandwidth extension 6kHz-7kHz
1388 : *-----------------------------------------------------------------*/
1389 :
1390 236258 : if ( st->hBWE_zero != NULL )
1391 : {
1392 229012 : if ( ( st->L_frame == L_FRAME && st->bwidth != NB && output_frame >= L_FRAME16k &&
1393 123249 : ( st->extl == -1 || st->extl == SWB_CNG || ( st->extl == WB_BWE && st->extl_brate == 0 && st->coder_type != AUDIO ) ) ) )
1394 : {
1395 15920 : hf_synth( st->hBWE_zero, st->core_brate, output_frame, Aq, exc2, syn, synth );
1396 : }
1397 : else
1398 : {
1399 213092 : hf_synth_reset( st->hBWE_zero );
1400 : }
1401 : }
1402 :
1403 : /*-----------------------------------------------------------------*
1404 : * Populate parameters for SWB TBE
1405 : *-----------------------------------------------------------------*/
1406 :
1407 236258 : if ( st->hBWE_TD != NULL )
1408 : {
1409 228918 : if ( ( !st->bfi && st->prev_bfi ) || ( st->last_vbr_hw_BWE_disable_dec == 1 && st->vbr_hw_BWE_disable_dec == 0 ) || ( ( st->extl == SWB_TBE || st->extl == WB_TBE || st->extl == FB_TBE ) && st->last_extl != SWB_TBE && st->last_extl != WB_TBE && st->last_extl != FB_TBE ) || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD && !st->tdm_LRTD_flag ) )
1410 : {
1411 13388 : st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
1412 13388 : set_f( st->hBWE_TD->old_bwe_exc_extended, 0.0f, NL_BUFF_OFFSET );
1413 : }
1414 :
1415 228918 : if ( !st->ppp_mode_dec && ( st->idchan == 0 || st->element_mode != IVAS_CPE_TD || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD && st->tdm_LRTD_flag ) ) )
1416 : {
1417 228918 : non_linearity( bwe_exc, bwe_exc_extended, st->hBWE_TD->old_bwe_exc_extended, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale, st->coder_type, voice_factors, st->L_frame, st->element_mode );
1418 : }
1419 :
1420 228918 : if ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 )
1421 : {
1422 12206 : st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
1423 : }
1424 : }
1425 :
1426 : /*----------------------------------------------------------------------*
1427 : * Updates
1428 : *----------------------------------------------------------------------*/
1429 :
1430 236258 : updt_dec( st, old_exc, pitch_buf, Es_pred, Aq, lsf_new, lsp_new, voice_factors, old_bwe_exc, gain_buf );
1431 :
1432 236258 : if ( st->core_brate > SID_2k40 && st->hTdCngDec != NULL && st->hFdCngDec != NULL )
1433 : {
1434 : /* update CNG parameters in active frames */
1435 149564 : cng_params_upd( lsp_new, exc, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ, DEC, st->hTdCngDec->ho_env_circ, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth );
1436 :
1437 : /* Set 16k LSP flag for CNG buffer */
1438 149564 : st->hTdCngDec->ho_16k_lsp[st->hTdCngDec->ho_circ_ptr] = ( st->L_frame == L_FRAME ? 0 : 1 );
1439 : }
1440 :
1441 236258 : pop_wmops();
1442 236258 : return error;
1443 : }
|