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 <math.h>
43 : #include "cnst.h"
44 : #include "rom_com.h"
45 : #include "prot.h"
46 : #include "ivas_cnst.h"
47 : #include "wmc_auto.h"
48 :
49 : /*---------------------------------------------------------------------*
50 : * Local prototypes
51 : *---------------------------------------------------------------------*/
52 :
53 : static void core_switch_lb_upsamp( Decoder_State *st, float *output );
54 : static void smoothTransitionDtxToTcx( float synth[], const int16_t output_frame, const int16_t delay_comp );
55 :
56 : /*---------------------------------------------------------------------*
57 : * core_switching_pre_dec()
58 : *
59 : * Preprocessing/preparation for ACELP/HQ core switching
60 : *---------------------------------------------------------------------*/
61 :
62 61230136 : ivas_error core_switching_pre_dec(
63 : Decoder_State *st, /* i/o: decoder state structure */
64 : const int16_t output_frame, /* i : frame length */
65 : const int32_t last_core_brate_st0, /* i : channel 0 last core bitrate */
66 : const int16_t nchan_out, /* i : number of output channels */
67 : const int16_t last_element_mode, /* i : last_element_mode */
68 : const int32_t last_element_brate /* i : last element bitrate */
69 : )
70 : {
71 : int16_t i, oldLenClasBuff, newLenClasBuff;
72 : ivas_error error;
73 : float tmp;
74 :
75 61230136 : error = IVAS_ERR_OK;
76 :
77 : /* Codec mode switching */
78 61230136 : if ( st->last_codec_mode == MODE2 || ( ( st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE ) && st->element_mode > EVS_MONO ) )
79 : {
80 54488556 : mvr2r( st->mem_syn2, st->mem_syn1, M );
81 54488556 : set_f( st->agc_mem2, 0, 2 );
82 54488556 : st->mem_deemph = st->syn[M];
83 54488556 : st->bpf_off = 1;
84 54488556 : if ( st->hBPF != NULL )
85 : {
86 10987876 : set_f( st->hBPF->pst_old_syn, 0, NBPSF_PIT_MAX );
87 10987876 : st->hBPF->pst_mem_deemp_err = 0;
88 : }
89 54488556 : st->psf_lp_noise = st->lp_noise;
90 :
91 : /* reset old HB synthesis buffer */
92 54488556 : if ( st->last_L_frame == L_FRAME )
93 : {
94 2338000 : st->old_bwe_delay = NS2SA( st->output_Fs, MAX_DELAY_TBE_NS - DELAY_SWB_TBE_12k8_NS );
95 : }
96 : else
97 : {
98 52150556 : st->old_bwe_delay = NS2SA( st->output_Fs, MAX_DELAY_TBE_NS - DELAY_SWB_TBE_16k_NS );
99 : }
100 54488556 : set_f( st->hb_prev_synth_buffer, 0, NS2SA( 48000, DELAY_BWE_TOTAL_NS ) );
101 :
102 54488556 : if ( st->hBWE_TD != NULL && st->last_core != ACELP_CORE )
103 : {
104 : /* reset BWE memories */
105 11609555 : set_f( st->hBWE_TD->old_bwe_exc, 0, PIT16k_MAX * 2 );
106 11609555 : st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
107 : }
108 :
109 : /* reset upd_cnt */
110 54488556 : st->upd_cnt = MAX_UPD_CNT;
111 :
112 54488556 : st->igf = 0;
113 :
114 54488556 : if ( st->output_Fs >= 16000 && st->hBWE_zero != NULL )
115 : {
116 10987836 : hf_synth_reset( st->hBWE_zero );
117 : }
118 :
119 54488556 : if ( st->hBWE_FD != NULL )
120 : {
121 11609816 : set_f( st->hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
122 : }
123 :
124 54488556 : if ( st->hHQ_core != NULL )
125 : {
126 54487672 : set_f( st->hHQ_core->prev_env, 0, SFM_N_WB );
127 54487672 : set_f( st->hHQ_core->prev_normq, 0, SFM_N_WB );
128 :
129 54487672 : set_f( st->hHQ_core->last_ni_gain, 0, BANDS_MAX );
130 54487672 : set_f( st->hHQ_core->last_env, 0, BANDS_MAX );
131 54487672 : st->hHQ_core->last_max_pos_pulse = 0;
132 :
133 54487672 : if ( st->output_Fs > 16000 )
134 : {
135 46331215 : set_f( st->hHQ_core->prev_coeff_out, 0, L_HQ_WB_BWE );
136 : }
137 :
138 : /* pre-echo */
139 54487672 : st->hHQ_core->pastpre = 0;
140 : }
141 :
142 : /* reset the GSC pre echo energy threshold in case of switching */
143 54488556 : if ( st->hGSCDec != NULL )
144 : {
145 10987876 : st->hGSCDec->Last_frame_ener = (float) MAX_32;
146 : }
147 :
148 54488556 : if ( st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE )
149 : {
150 54488295 : if ( st->element_mode == EVS_MONO )
151 : {
152 952 : st->last_core = HQ_CORE;
153 952 : mvr2r( st->hTcxDec->FBTCXdelayBuf, st->prev_synth_buffer, NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS - DELAY_CLDFB_NS ) );
154 : }
155 :
156 54488295 : if ( st->hHQ_core != NULL )
157 : {
158 54487411 : set_f( st->hHQ_core->last_ni_gain, 0, BANDS_MAX );
159 54487411 : set_f( st->hHQ_core->last_env, 0, BANDS_MAX );
160 54487411 : st->hHQ_core->last_max_pos_pulse = 0;
161 :
162 54487411 : set_s( st->hHQ_core->prev_SWB_peak_pos, 0, SPT_SHORTEN_SBNUM );
163 54487411 : st->hHQ_core->prev_frm_hfe2 = 0;
164 54487411 : st->hHQ_core->prev_stab_hfe2 = 0;
165 : }
166 : }
167 :
168 54488556 : if ( st->prev_bfi != 0 )
169 : {
170 : int16_t delay_comp;
171 :
172 : /*switch off Hq Voicing as it was not updated in MODE2*/
173 2658098 : if ( st->hHQ_core != NULL )
174 : {
175 2658071 : st->hHQ_core->oldHqVoicing = 0;
176 2658071 : st->hHQ_core->HqVoicing = 0;
177 : }
178 :
179 2658098 : delay_comp = NS2SA( st->output_Fs, DELAY_CLDFB_NS );
180 :
181 2658098 : if ( !st->last_con_tcx && st->last_core_bfi == ACELP_CORE && st->core == HQ_CORE )
182 : {
183 : float *realBuffer[CLDFB_NO_COL_MAX_SWITCH], *imagBuffer[CLDFB_NO_COL_MAX_SWITCH];
184 : float realBufferTmp[CLDFB_NO_COL_MAX_SWITCH][CLDFB_NO_CHANNELS_MAX], imagBufferTmp[CLDFB_NO_COL_MAX_SWITCH][CLDFB_NO_CHANNELS_MAX];
185 :
186 0 : for ( i = 0; i < CLDFB_NO_COL_MAX_SWITCH; i++ )
187 : {
188 0 : set_f( realBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
189 0 : set_f( imagBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
190 0 : realBuffer[i] = realBufferTmp[i];
191 0 : imagBuffer[i] = imagBufferTmp[i];
192 : }
193 :
194 : /* CLDFB analysis of the synthesis at internal sampling rate */
195 0 : if ( ( error = cldfb_save_memory( st->cldfbAna ) ) != IVAS_ERR_OK )
196 : {
197 0 : return error;
198 : }
199 :
200 0 : cldfbAnalysis( st->hTcxDec->syn_Overl, realBuffer, imagBuffer, delay_comp, st->cldfbAna );
201 0 : cldfb_restore_memory( st->cldfbAna );
202 :
203 : /* CLDFB synthesis of the combined signal */
204 0 : if ( ( error = cldfb_save_memory( st->cldfbSyn ) ) != IVAS_ERR_OK )
205 : {
206 0 : return error;
207 : }
208 :
209 0 : cldfbSynthesis( realBuffer, imagBuffer, st->hHQ_core->fer_samples, delay_comp, st->cldfbSyn );
210 0 : cldfb_restore_memory( st->cldfbSyn );
211 : }
212 :
213 2658098 : if ( !st->last_con_tcx && st->last_core_bfi == ACELP_CORE && st->core == HQ_CORE )
214 : {
215 0 : lerp( st->hTcxDec->syn_Overl, st->hHQ_core->fer_samples + delay_comp, output_frame / 2, st->last_L_frame / 2 );
216 : /*Set to zero the remaining part*/
217 0 : set_f( st->hHQ_core->fer_samples + delay_comp + output_frame / 2, 0, ( output_frame / 2 ) - delay_comp );
218 : }
219 : }
220 :
221 54488556 : st->use_acelp_preq = 0;
222 54488556 : st->reset_mem_AR = 0;
223 : }
224 :
225 : /*FEC*/
226 61230136 : if ( st->L_frame <= L_FRAME16k )
227 : {
228 59647101 : if ( st->last_L_frame <= L_FRAME16k && st->core != HQ_CORE )
229 : {
230 21628740 : if ( st->L_frame != st->last_L_frame )
231 : {
232 160585 : if ( st->L_frame > st->last_L_frame )
233 : {
234 117945 : oldLenClasBuff = L_SYN_MEM_CLAS_ESTIM * st->last_L_frame / st->L_frame;
235 117945 : newLenClasBuff = L_SYN_MEM_CLAS_ESTIM;
236 : }
237 : else
238 : {
239 42640 : oldLenClasBuff = L_SYN_MEM_CLAS_ESTIM;
240 42640 : newLenClasBuff = L_SYN_MEM_CLAS_ESTIM * st->L_frame / st->last_L_frame;
241 : }
242 160585 : lerp( &st->mem_syn_clas_estim[L_SYN_MEM_CLAS_ESTIM - oldLenClasBuff], &st->mem_syn_clas_estim[L_SYN_MEM_CLAS_ESTIM - newLenClasBuff], newLenClasBuff, oldLenClasBuff );
243 : }
244 : }
245 : else
246 : {
247 38018361 : set_zero( st->mem_syn_clas_estim, L_SYN_MEM_CLAS_ESTIM );
248 : }
249 : }
250 :
251 : /* Here we only handle cases where last_ppp and last_nelp not updated when coming from CodecB or other cores
252 : within ACELP_CORE if switching from another bitarate to vbr, last_ppp and last_nelp is always updated in the previous frame */
253 61230136 : if ( st->core == ACELP_CORE && ( st->last_core != ACELP_CORE || st->last_codec_mode == MODE2 ) )
254 : {
255 313189 : st->last_ppp_mode_dec = 0;
256 313189 : st->last_nelp_mode_dec = 0;
257 : }
258 :
259 : /* Handle state reset of stat_noise_uv_mod memory */
260 61230136 : if ( st->core == ACELP_CORE && ( st->last_core != ACELP_CORE || st->last_codec_mode == MODE2 || st->last_total_brate <= PPP_NELP_2k80 ) )
261 : {
262 1507916 : st->act_count = 3;
263 1507916 : st->uv_count = 0;
264 : }
265 :
266 61230136 : if ( ( ( st->core == ACELP_CORE || st->core == AMR_WB_CORE ) && st->last_core == HQ_CORE ) /* EVS and HQ -> ACELP */ ||
267 61223428 : ( ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD || ( st->element_mode == IVAS_CPE_MDCT && last_element_mode == IVAS_CPE_DFT ) ) && nchan_out == 2 && st->core_brate != SID_2k40 && st->core_brate != FRAME_NO_DATA && ( last_core_brate_st0 == FRAME_NO_DATA || last_core_brate_st0 == SID_2k40 ) ) ||
268 61220540 : ( st->core == ACELP_CORE && st->last_L_frame > L_FRAME16k ) /* TCX @ 25.6/32 kHz -> ACELP */ )
269 : {
270 54713 : if ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD )
271 : {
272 36775 : st->hPFstat->reset = 1;
273 : }
274 :
275 54713 : if ( st->L_frame == L_FRAME16k )
276 : {
277 18648 : mvr2r( TRWB2_Ave, st->lsf_old, M ); /* init of LSP */
278 18648 : mvr2r( TRWB2_Ave, st->lsfoldbfi1, M );
279 18648 : mvr2r( TRWB2_Ave, st->lsfoldbfi0, M );
280 18648 : mvr2r( TRWB2_Ave, st->lsf_adaptive_mean, M );
281 18648 : lsf2lsp( st->lsf_old, st->lsp_old, M, INT_FS_16k );
282 : }
283 : else
284 : {
285 36065 : mvr2r( TRWB_Ave, st->lsf_old, M ); /* init of LSP */
286 36065 : mvr2r( TRWB_Ave, st->lsfoldbfi1, M );
287 36065 : mvr2r( TRWB_Ave, st->lsfoldbfi0, M );
288 36065 : mvr2r( TRWB_Ave, st->lsf_adaptive_mean, M );
289 36065 : lsf2lsp( st->lsf_old, st->lsp_old, M, INT_FS_12k8 );
290 : }
291 :
292 54713 : if ( ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD ) && nchan_out == 2 && st->core_brate > SID_2k40 && ( last_core_brate_st0 == FRAME_NO_DATA || last_core_brate_st0 == SID_2k40 ) && st->hTcxDec != NULL )
293 : {
294 : /* Last frame was Stereo CNG and the synthesis memory is outdated -- reset */
295 2763 : set_f( st->hTcxDec->old_syn_Overl, 0.0f, L_FRAME32k / 2 );
296 2763 : set_f( st->hFdCngDec->hFdCngCom->olapBufferAna, 0.0f, FFTLEN );
297 : }
298 :
299 54713 : set_f( st->agc_mem2, 0, 2 );
300 54713 : st->mem_deemph = 0;
301 54713 : if ( !st->last_con_tcx )
302 : {
303 54538 : set_f( st->mem_syn2, 0.0f, M );
304 : }
305 54713 : set_f( st->mem_syn1, 0.0f, M );
306 54713 : if ( st->hBWE_TD != NULL )
307 : {
308 51917 : st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
309 : }
310 :
311 : /* Reset ACELP parameters */
312 54713 : set_zero( st->mem_MA, M );
313 54713 : if ( st->sr_core == INT_FS_16k )
314 : {
315 18648 : mvr2r( GEWB2_Ave, st->mem_AR, M );
316 : }
317 : else
318 : {
319 36065 : mvr2r( GEWB_Ave, st->mem_AR, M );
320 : }
321 54713 : st->tilt_code = 0.0f;
322 54713 : st->gc_threshold = 0.0f;
323 54713 : set_f( st->dispMem, 0, 8 );
324 :
325 54713 : st->last_coder_type = GENERIC;
326 :
327 54713 : fer_energy( output_frame, UNVOICED_CLAS, st->previoussynth, -1, &st->enr_old, 1 );
328 54713 : st->lp_gainp = 0.0f;
329 54713 : st->lp_gainc = (float) sqrt( st->lp_ener );
330 :
331 54713 : st->last_voice_factor = 0;
332 54713 : st->Last_GSC_noisy_speech_flag = 0;
333 :
334 : /* reset CLDFB memories */
335 54713 : cldfb_reset_memory( st->cldfbAna );
336 54713 : cldfb_reset_memory( st->cldfbBPF );
337 54713 : cldfb_reset_memory( st->cldfbSyn );
338 :
339 : /* reset TBE memories */
340 54713 : if ( !st->last_con_tcx && !( ( st->last_core == HQ_CORE ) && st->element_mode > EVS_MONO ) )
341 : {
342 48813 : set_f( st->old_exc, 0, L_EXC_MEM_DEC );
343 : }
344 5900 : else if ( st->L_frame < L_FRAME16k )
345 : {
346 : /* resample from 16kHz to 12.8kHZ */
347 265 : synth_mem_updt2( st->L_frame, L_FRAME16k, st->old_exc, st->mem_syn_r, st->mem_syn2, NULL, DEC );
348 : }
349 :
350 54713 : if ( st->hBWE_TD != NULL )
351 : {
352 51917 : set_f( st->hBWE_TD->old_bwe_exc, 0, PIT16k_MAX * 2 );
353 : }
354 :
355 54713 : if ( st->output_Fs >= 16000 && st->hBWE_zero != NULL )
356 : {
357 51641 : hf_synth_reset( st->hBWE_zero );
358 : }
359 :
360 54713 : if ( st->hBWE_FD != NULL )
361 : {
362 51917 : set_f( st->hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
363 : }
364 : }
365 :
366 61230136 : if ( ( st->core == ACELP_CORE || st->core == AMR_WB_CORE ) && ( st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE ) )
367 : {
368 306390 : if ( st->hBWE_TD != NULL )
369 : {
370 302510 : st->hBWE_TD->bwe_non_lin_prev_scale = 0.0f;
371 302510 : set_f( st->hBWE_TD->old_bwe_exc, 0, PIT16k_MAX * 2 );
372 : }
373 :
374 306390 : st->tilt_code = 0.0f;
375 306390 : st->gc_threshold = 0.0f;
376 306390 : set_f( st->dispMem, 0, 8 );
377 :
378 306390 : st->last_coder_type = GENERIC;
379 :
380 306390 : fer_energy( output_frame, UNVOICED_CLAS, st->previoussynth, -1, &st->enr_old, 1 );
381 306390 : st->lp_gainp = 0.0f;
382 306390 : st->lp_gainc = (float) sqrt( st->lp_ener );
383 :
384 306390 : st->last_voice_factor = 0;
385 306390 : st->Last_GSC_noisy_speech_flag = 0;
386 :
387 306390 : if ( st->output_Fs >= 16000 && st->hBWE_zero != NULL )
388 : {
389 302360 : hf_synth_reset( st->hBWE_zero );
390 : }
391 :
392 306390 : if ( st->hBWE_FD != NULL )
393 : {
394 302510 : set_f( st->hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
395 : }
396 :
397 306390 : if ( nchan_out == 1 && st->element_mode == IVAS_CPE_DFT && st->element_brate <= IVAS_24k4 && last_element_brate > IVAS_24k4 )
398 : {
399 : /* update cldbf state with previous frame TCX synthesis when going from a bitrate with residual coding to a bitrate without it */
400 : int16_t offset;
401 30125 : offset = st->cldfbAna->p_filter_length - st->cldfbAna->no_channels;
402 30125 : mvr2r( st->hTcxDec->old_synthFB + st->hTcxDec->old_synth_lenFB - offset, st->cldfbAna->cldfb_state, offset );
403 : }
404 : }
405 :
406 61230136 : if ( st->core == HQ_CORE && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE || ( ( st->element_mode != EVS_MONO ) && ( st->last_core != HQ_CORE ) ) ) )
407 : {
408 17623 : set_f( st->hHQ_core->prev_env, 0, SFM_N_WB );
409 17623 : set_f( st->hHQ_core->prev_normq, 0, SFM_N_WB );
410 :
411 17623 : set_f( st->hHQ_core->last_ni_gain, 0, BANDS_MAX );
412 17623 : set_f( st->hHQ_core->last_env, 0, BANDS_MAX );
413 17623 : st->hHQ_core->last_max_pos_pulse = 0;
414 :
415 17623 : set_s( st->hHQ_core->prev_SWB_peak_pos, 0, SPT_SHORTEN_SBNUM );
416 17623 : st->hHQ_core->prev_frm_hfe2 = 0;
417 17623 : st->hHQ_core->prev_stab_hfe2 = 0;
418 17623 : if ( st->output_Fs > 16000 )
419 : {
420 15091 : set_f( st->hHQ_core->prev_coeff_out, 0, L_HQ_WB_BWE );
421 : }
422 :
423 17623 : if ( st->element_mode != EVS_MONO )
424 : {
425 : /* Estimate mem_env_delta to reinit env_stab */
426 16896 : tmp = max( 0, ENV_STAB_EST1 + ( ENV_STAB_EST2 * st->stab_fac_smooth_lt ) + ( ENV_STAB_EST3 * st->log_energy_diff_lt ) );
427 16896 : st->hHQ_core->mem_env_delta = (int16_t) min( MAX16B, (int32_t) ( tmp * ( 1 << 12 ) ) ); /* Convert to Q12 and handle saturation */
428 :
429 16896 : if ( st->last_core != TCX_20_CORE && st->last_core != TCX_10_CORE )
430 : {
431 5105 : set_f( st->hHQ_core->old_out, 0, output_frame );
432 5105 : set_f( st->hHQ_core->old_outLB, 0, L_FRAME16k );
433 : }
434 :
435 16896 : st->hHQ_core->no_att_hangover = 0;
436 16896 : st->hHQ_core->energy_lt = 300.0f;
437 :
438 16896 : set_s( st->hHQ_core->old_is_transient, 0, 3 );
439 16896 : set_f( st->hHQ_core->prev_noise_level, 0.0f, 2 );
440 16896 : st->hHQ_core->prev_R = 0;
441 16896 : set_s( st->hHQ_core->mem_norm + 1, 39, SFM_N_ENV_STAB - 1 );
442 16896 : st->hHQ_core->prev_hqswb_clas = HQ_NORMAL;
443 16896 : st->hHQ_core->prev_ni_ratio = 0.5f;
444 16896 : set_f( st->hHQ_core->prev_En_sb, 0.0f, NB_SWB_SUBBANDS );
445 : }
446 : else
447 : {
448 727 : set_f( st->hHQ_core->old_out, 0, output_frame );
449 727 : set_f( st->hHQ_core->old_outLB, 0, L_FRAME16k );
450 : }
451 : }
452 :
453 : /* handle switching cases where preecho_sb was not called in the last frame (memory not up to date) */
454 61230136 : if ( st->hHQ_core != NULL )
455 : {
456 61138052 : st->hHQ_core->pastpre--;
457 61138052 : if ( st->hHQ_core->pastpre <= 0 )
458 : {
459 60970878 : reset_preecho_dec( st->hHQ_core );
460 : }
461 : }
462 :
463 61230136 : if ( st->core_brate == FRAME_NO_DATA )
464 : {
465 282438 : st->VAD = 0;
466 282438 : st->m_frame_type = ZERO_FRAME;
467 : }
468 60947698 : else if ( st->core_brate == SID_2k40 || st->core_brate == SID_1k75 )
469 : {
470 43447 : st->VAD = 0;
471 43447 : st->m_frame_type = SID_FRAME;
472 : }
473 : else
474 : {
475 60904251 : st->VAD = 1;
476 60904251 : st->m_frame_type = ACTIVE_FRAME;
477 : }
478 :
479 : /*switch on CNA on active frames*/
480 61230136 : if ( st->element_mode == EVS_MONO ) /* for IVAS modes, st->flag_cna is set earlier */
481 : {
482 108138 : if ( st->VAD && ( ( st->core != AMR_WB_CORE && st->total_brate <= CNA_MAX_BRATE ) || ( st->core == AMR_WB_CORE && st->total_brate <= ACELP_8k85 ) ) )
483 : {
484 49735 : st->flag_cna = 1;
485 : }
486 58403 : else if ( st->VAD || ( ( st->cng_type == FD_CNG ) && ( st->L_frame == L_FRAME16k ) ) )
487 : {
488 55483 : st->flag_cna = 0;
489 : }
490 : }
491 :
492 61230136 : if ( st->core == AMR_WB_CORE )
493 : {
494 7295 : st->cng_type = LP_CNG;
495 : }
496 :
497 : /* Reconfigure CNG */
498 61230136 : if ( st->hFdCngDec && ( ( st->last_L_frame != st->L_frame ) || ( st->hFdCngDec->hFdCngCom->frameSize != st->L_frame ) || st->ini_frame == 0 || st->bwidth != st->last_bwidth ) )
499 : {
500 : /* || st->last_core == AMR_WB_CORE || st->last_codec_mode == MODE2)){*/
501 28796834 : if ( st->core != AMR_WB_CORE )
502 : {
503 28796727 : configureFdCngDec( st->hFdCngDec, st->bwidth, st->rf_flag == 1 && st->total_brate == ACELP_13k20 ? ACELP_9k60 : st->total_brate, st->L_frame, st->last_L_frame, st->element_mode );
504 : }
505 : else
506 : {
507 107 : configureFdCngDec( st->hFdCngDec, WB, ACELP_8k00, st->L_frame, st->last_L_frame, st->element_mode );
508 :
509 107 : if ( st->VAD )
510 : {
511 107 : st->hFdCngDec->hFdCngCom->CngBitrate = st->total_brate;
512 : }
513 : }
514 28796834 : if ( st->last_L_frame != st->L_frame && st->L_frame <= L_FRAME16k && st->last_L_frame <= L_FRAME16k )
515 : {
516 159980 : if ( st->element_mode == IVAS_CPE_DFT || st->element_mode == IVAS_CPE_TD )
517 : {
518 24321 : lerp( st->hFdCngDec->hFdCngCom->olapBufferAna + st->last_L_frame, st->hFdCngDec->hFdCngCom->olapBufferAna + st->L_frame, st->L_frame, st->last_L_frame );
519 : }
520 :
521 159980 : lerp( st->hFdCngDec->hFdCngCom->olapBufferSynth2, st->hFdCngDec->hFdCngCom->olapBufferSynth2, st->L_frame * 2, st->last_L_frame * 2 );
522 :
523 159980 : if ( st->total_brate <= SID_2k40 && st->last_total_brate <= SID_2k40 )
524 : {
525 411 : lerp( st->hFdCngDec->hFdCngCom->olapBufferSynth, st->hFdCngDec->hFdCngCom->olapBufferSynth, st->L_frame * 2, st->last_L_frame * 2 );
526 :
527 411 : if ( st->L_frame == L_FRAME )
528 : {
529 117477 : for ( i = 0; i < st->L_frame * 2; i++ )
530 : {
531 117248 : st->hFdCngDec->hFdCngCom->olapBufferSynth[i] = st->hFdCngDec->hFdCngCom->olapBufferSynth[i] * 0.6250f;
532 : }
533 : }
534 : else
535 : {
536 116662 : for ( i = 0; i < st->L_frame * 2; i++ )
537 : {
538 116480 : st->hFdCngDec->hFdCngCom->olapBufferSynth[i] = st->hFdCngDec->hFdCngCom->olapBufferSynth[i] * 1.6f;
539 : }
540 : }
541 : }
542 : }
543 : }
544 :
545 61230136 : return error;
546 : }
547 :
548 :
549 : /*---------------------------------------------------------------------*
550 : * core_switching_post_dec()
551 : *
552 : * Postprocessing for ACELP/HQ core switching
553 : *---------------------------------------------------------------------*/
554 :
555 61222841 : ivas_error core_switching_post_dec(
556 : Decoder_State *st, /* i/o: decoder state structure */
557 : float *synth, /* i/o: output synthesis */
558 : float *output, /* i/o: LB synth/upsampled LB synth */
559 : float output_mem[], /* i : OLA memory from last TCX/HQ frame */
560 : const int16_t use_cldfb_for_dft, /* i : flag to use of CLDFB for DFT Stereo */
561 : const int16_t output_frame, /* i : frame length */
562 : const int16_t core_switching_flag, /* i : ACELP->HQ switching flag */
563 : const int16_t sba_dirac_stereo_flag, /* i : signal stereo output for SBA DirAC */
564 : const int16_t nchan_out, /* i : number of output channels */
565 : const int16_t last_element_mode /* i : element mode of previous frame */
566 : )
567 : {
568 : int16_t i, delay_comp, delta;
569 : float tmpF;
570 : float tmpDelta;
571 : float synth_subfr_out[SWITCH_MAX_GAP], synth_subfr_bwe[SWITCH_MAX_GAP];
572 : float mem_synth[NS2SA( 16000, DELAY_CLDFB_NS ) + 2];
573 : int16_t nZeros;
574 : int16_t offset;
575 : ivas_error error;
576 :
577 61222841 : error = IVAS_ERR_OK;
578 :
579 61222841 : if ( st->core == ACELP_CORE && st->bfi && st->hHQ_core != NULL && !st->con_tcx )
580 : {
581 281859 : if ( ( error = acelp_core_switch_dec_bfi( st ) ) != IVAS_ERR_OK )
582 : {
583 0 : return error;
584 : }
585 : }
586 :
587 : /* set multiplication factor according to the sampling rate */
588 61222841 : delta = 1;
589 61222841 : if ( output_frame == L_FRAME16k )
590 : {
591 9301286 : delta = 2;
592 : }
593 51921555 : else if ( output_frame == L_FRAME32k )
594 : {
595 14161341 : delta = 4;
596 : }
597 37760214 : else if ( output_frame == L_FRAME48k )
598 : {
599 37753507 : delta = 6;
600 : }
601 :
602 : /* set delay compensation between HQ synthesis and ACELP synthesis */
603 61222841 : delay_comp = delta * HQ_DELAY_COMP;
604 :
605 : /* Core switching done in DFT domain afterward*/
606 61222841 : if ( ( st->element_mode != IVAS_CPE_DFT || use_cldfb_for_dft ) && ( !sba_dirac_stereo_flag || ( sba_dirac_stereo_flag && st->core_brate == SID_2k40 && st->cng_type == FD_CNG ) ) )
607 : {
608 58204517 : if ( st->core == HQ_CORE || st->core == TCX_20_CORE || st->core == TCX_10_CORE || ( st->core == ACELP_CORE && st->bfi == 1 && st->con_tcx == 1 ) )
609 : {
610 52837880 : st->use_acelp_preq = 0;
611 52837880 : if ( st->hBWE_FD != NULL )
612 : {
613 10903395 : st->hBWE_FD->mem_deemph_old_syn = 0.0f;
614 : }
615 :
616 52837880 : if ( st->element_mode == EVS_MONO && st->core == HQ_CORE ) /* ACELP->HQ-CORE */
617 : {
618 22642 : if ( core_switching_flag && st->last_L_frame == st->last_L_frame_ori && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE ) )
619 : {
620 724 : if ( ( error = acelp_core_switch_dec( st, synth_subfr_out, synth_subfr_bwe, output_frame, core_switching_flag, mem_synth, nchan_out ) ) != IVAS_ERR_OK )
621 : {
622 0 : return error;
623 : }
624 : }
625 :
626 22642 : if ( core_switching_flag && st->last_core == HQ_CORE && st->prev_bfi )
627 : {
628 0 : mvr2r( st->delay_buf_out, synth_subfr_out, delay_comp );
629 : }
630 : }
631 :
632 : /* delay HQ synthesis to synchronize with ACELP synthesis */
633 52837880 : delay_signal( synth, output_frame, st->delay_buf_out, delay_comp );
634 :
635 52837880 : if ( st->element_mode == EVS_MONO && st->core == HQ_CORE ) /* ACELP->HQ-CORE */
636 : {
637 22642 : if ( core_switching_flag && st->last_L_frame == st->last_L_frame_ori && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE ) )
638 : {
639 724 : core_switching_OLA( mem_synth, st->last_L_frame, st->output_Fs, synth, synth_subfr_out, synth_subfr_bwe, output_frame, st->bwidth );
640 : }
641 21918 : else if ( core_switching_flag && st->last_core == HQ_CORE && st->prev_bfi ) /* HQ | ACELP | TRANSITION with ACELP frame lost */
642 : {
643 : /* Overlap between old->out[] (stocked in st->fer_samples[]) and good HQ frame on L/2 */
644 0 : nZeros = (int16_t) ( NS2SA( st->output_Fs, N_ZERO_MDCT_NS ) );
645 0 : tmpDelta = 1.0f / (float) ( output_frame >> 1 );
646 0 : for ( i = 0; i < ( output_frame >> 1 ); i++ )
647 : {
648 0 : tmpF = (float) i * tmpDelta;
649 0 : synth[i + delay_comp] = ( 1 - tmpF ) * st->hHQ_core->fer_samples[i + nZeros] + synth[i + delay_comp] * tmpF;
650 : }
651 : }
652 21918 : else if ( ( !core_switching_flag && st->core == HQ_CORE && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE ) ) || /* ACELP | TRANSITION | HQ with TRANSITION lost */
653 0 : ( core_switching_flag && st->prev_bfi && st->last_L_frame != st->last_L_frame_ori ) ) /* ACELP@12k8 | ACELP@16k | TRANSITION with ACELP@16k lost */
654 : {
655 : /* Overlap between CELP estimation (BFI) and good HQ frame on L/2 */
656 3 : tmpDelta = 1.0f / (float) ( output_frame >> 1 );
657 563 : for ( i = 0; i < ( output_frame >> 1 ); i++ )
658 : {
659 560 : tmpF = (float) i * tmpDelta;
660 560 : synth[i] = synth[i] * tmpF + ( 1 - tmpF ) * st->hHQ_core->fer_samples[i];
661 : }
662 : }
663 : }
664 52815238 : else if ( ( ( st->last_core == ACELP_CORE || st->last_core_bfi == ACELP_CORE ) && !( st->prev_bfi == 1 && st->last_con_tcx == 1 ) ) || st->last_core == AMR_WB_CORE ) /*ACELP->TCX/HQ*/
665 : {
666 : /* if this is first active MDCT-Stereo frame after a CNG frame and output format is mono DMX, this should only be done for the zero-th channel, the other one will simply be copied over after this function */
667 358807 : if ( ( ( st->last_core_brate != SID_2k40 && st->last_core_brate != FRAME_NO_DATA ) || ( st->element_mode != IVAS_CPE_DFT && st->element_mode != IVAS_CPE_TD ) || nchan_out == 1 ) && !( st->element_mode == IVAS_CPE_MDCT && st->idchan == 1 && ( nchan_out == 1 || last_element_mode == IVAS_CPE_DFT ) ) )
668 : {
669 329109 : core_switch_lb_upsamp( st, output );
670 : }
671 :
672 358807 : mvr2r( st->previoussynth, synth, delay_comp );
673 :
674 : /* Overlap between TCX-LB and TCX-FB*/
675 358807 : tmpDelta = NS2SA( st->output_Fs, DELAY_BWE_TOTAL_NS );
676 33728478 : for ( i = 0; i < tmpDelta; i++ )
677 : {
678 33369671 : synth[i + delay_comp] = ( synth[i + delay_comp] * i + ( tmpDelta - i ) * st->previoussynth[i + delay_comp] ) / tmpDelta;
679 : }
680 :
681 358807 : if ( ( st->element_mode == IVAS_CPE_MDCT || ( st->is_ism_format && st->core == TCX_20_CORE /* <- means TCX in general, TCX10 is forbidden after ACELP */ ) ) && st->last_core_brate <= SID_2k40 && st->core_brate > SID_2k40 )
682 : {
683 : /* smooth transitions to avoid pops in car noise items */
684 10289 : smoothTransitionDtxToTcx( synth, output_frame, delay_comp );
685 : }
686 :
687 : /* Reset memories of CLDFBs */
688 358807 : if ( st->cldfbAna != NULL )
689 : {
690 358807 : if ( st->cldfbAna->no_channels * st->cldfbAna->no_col != st->L_frame )
691 : {
692 29561 : configureCldfb( st->cldfbAna, st->L_frame * FRAMES_PER_SEC );
693 29561 : configureCldfb( st->cldfbBPF, min( 16000, st->L_frame * FRAMES_PER_SEC ) );
694 : }
695 :
696 358807 : cldfb_reset_memory( st->cldfbAna );
697 358807 : cldfb_reset_memory( st->cldfbBPF );
698 : }
699 358807 : cldfb_reset_memory( st->cldfbSyn );
700 :
701 : /* Update memories for CLDFB ana for eventual next ACELP frame */
702 358807 : if ( st->cldfbAna != NULL )
703 : {
704 358807 : delta = st->cldfbAna->no_channels;
705 358807 : offset = st->cldfbAna->p_filter_length - st->cldfbAna->no_channels;
706 8197419 : for ( i = 0; i < delta; i++ )
707 : {
708 7838612 : st->cldfbAna->cldfb_state[offset - delta + i] =
709 7838612 : output[st->L_frame - delta + i] * ( (float) ( i + 1 ) ) / ( (float) delta );
710 : }
711 : }
712 : }
713 52456431 : else if ( st->element_mode != EVS_MONO )
714 : {
715 : /* Reset memories of CLDFBs */
716 52456431 : if ( st->cldfbAna != NULL )
717 : {
718 11671261 : if ( st->cldfbAna->no_channels * st->cldfbAna->no_col != st->L_frame )
719 : {
720 215944 : configureCldfb( st->cldfbAna, st->L_frame * FRAMES_PER_SEC );
721 215944 : configureCldfb( st->cldfbBPF, min( 16000, st->L_frame * FRAMES_PER_SEC ) );
722 : }
723 :
724 11671261 : cldfb_reset_memory( st->cldfbAna );
725 11671261 : cldfb_reset_memory( st->cldfbBPF );
726 : }
727 :
728 52456431 : if ( st->cldfbSyn != NULL )
729 : {
730 52456431 : cldfb_reset_memory( st->cldfbSyn );
731 : }
732 :
733 : /* Update memories for CLDFB ana for eventual next ACELP frame */
734 : /* Analysis CLDF memory is fed with ramped signal for last slot */
735 52456431 : if ( st->cldfbAna != NULL )
736 : {
737 11671261 : delta = st->cldfbAna->no_channels;
738 11671261 : offset = st->cldfbAna->p_filter_length - st->cldfbAna->no_channels;
739 322788661 : for ( i = 0; i < delta; i++ )
740 : {
741 311117400 : st->cldfbAna->cldfb_state[offset - delta + i] =
742 311117400 : output[st->L_frame - delta + i] * ( (float) ( i + 1 ) ) / ( (float) delta );
743 : }
744 : }
745 : }
746 :
747 52837880 : if ( st->hBWE_TD != NULL )
748 : {
749 10903395 : st->hBWE_TD->bwe_non_lin_prev_scale = 0.0;
750 : }
751 :
752 52837880 : if ( st->hHQ_core != NULL && !( inner_frame_tbl[st->bwidth] == L_FRAME16k && st->core_brate <= HQ_32k ) )
753 : {
754 51938420 : set_f( st->hHQ_core->prev_env, 0, SFM_N_WB );
755 51938420 : set_f( st->hHQ_core->prev_normq, 0, SFM_N_WB );
756 : }
757 :
758 52837880 : mvr2r( synth, st->previoussynth, output_frame );
759 :
760 : /*Set post-filtering flag to zero*/
761 52837880 : if ( st->hBPF != NULL )
762 : {
763 10228748 : st->hPFstat->on = 0;
764 : }
765 : }
766 : else
767 : {
768 : /* MDCT to ACELP transition */
769 5366637 : if ( st->last_core == HQ_CORE || st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE )
770 : {
771 272712 : nZeros = (int16_t) ( NS2SA( st->output_Fs, N_ZERO_MDCT_NS ) );
772 272712 : mvr2r( st->delay_buf_out, synth, delay_comp ); /* copy the HQ/ACELP delay synchronization buffer at the beginning of ACELP frame */
773 :
774 272712 : if ( st->prev_bfi && st->hHQ_core != NULL && st->hHQ_core->HqVoicing && st->last_core == HQ_CORE )
775 : {
776 0 : mvr2r( st->hHQ_core->fer_samples, st->hHQ_core->old_out + nZeros, NS2SA( st->output_Fs, 3000000 ) );
777 : }
778 :
779 272712 : tmpF = 1.0f / (float) NS2SA( st->output_Fs, 3000000 );
780 :
781 272712 : if ( st->element_mode == IVAS_CPE_TD && st->hHQ_core == NULL )
782 : {
783 113252 : for ( i = 0; i < NS2SA( st->output_Fs, 3000000 ); i++ )
784 : {
785 112368 : synth[i + delay_comp] = ( 1 - tmpF * (float) i ) * output_mem[i] + tmpF * (float) i * synth[i + delay_comp];
786 : }
787 : }
788 271828 : else if ( st->element_mode == IVAS_CPE_MDCT && st->core_brate <= SID_2k40 && st->prev_bfi )
789 : {
790 20344 : for ( i = 0; i < NS2SA( st->output_Fs, 3000000 ); i++ )
791 : {
792 20160 : synth[i + delay_comp] = ( 1 - tmpF * (float) i ) * st->hHQ_core->old_out[i + nZeros] * st->hTcxDec->conceal_eof_gain + tmpF * (float) i * synth[i + delay_comp];
793 : }
794 : }
795 : else
796 : {
797 32954268 : for ( i = 0; i < NS2SA( st->output_Fs, 3000000 ); i++ )
798 : {
799 32682624 : synth[i + delay_comp] = ( 1 - tmpF * (float) i ) * st->hHQ_core->old_out[i + nZeros] + tmpF * (float) i * synth[i + delay_comp];
800 : }
801 : }
802 : }
803 :
804 5366637 : set_f( st->delay_buf_out, 0, HQ_DELTA_MAX * HQ_DELAY_COMP );
805 5366637 : if ( st->hHQ_core != NULL )
806 : {
807 5274553 : st->hHQ_core->oldHqVoicing = 0;
808 : }
809 : }
810 : }
811 : else
812 : {
813 : /* memory update needed for DFT stereo -> TD stereo switching */
814 3018324 : mvr2r( synth + output_frame - delay_comp, st->delay_buf_out, delay_comp );
815 : }
816 :
817 : /* reset SWB BWE buffers */
818 61222841 : if ( st->bws_cnt == 0 || ( st->bws_cnt > 0 && st->coder_type != INACTIVE && st->coder_type != AUDIO ) )
819 : {
820 60991896 : st->attenu1 = 0.1f;
821 : }
822 :
823 61222841 : if ( st->hBWE_FD != NULL &&
824 18239217 : ( ( st->last_extl != SWB_BWE && st->extl == SWB_BWE ) || ( st->last_extl != FB_BWE && st->extl == FB_BWE ) ||
825 18176340 : ( ( st->last_core == HQ_CORE || st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE || st->last_extl == SWB_TBE ) && st->extl < 0 && st->core == ACELP_CORE ) || ( st->last_core == ACELP_CORE && st->core == ACELP_CORE && ( ( st->prev_coder_type != INACTIVE && st->coder_type != INACTIVE ) || ( st->prev_coder_type != AUDIO && st->coder_type == AUDIO ) ) && st->bws_cnt > 0 ) ) )
826 : {
827 103984 : set_f( st->hBWE_FD->old_wtda_swb, 0, output_frame );
828 :
829 103984 : if ( st->last_extl != WB_BWE )
830 : {
831 101833 : st->hBWE_FD->prev_mode = NORMAL;
832 : }
833 :
834 103984 : st->hBWE_FD->prev_Energy = 0.0f;
835 103984 : st->hBWE_FD->prev_L_swb_norm = 8;
836 103984 : st->hBWE_FD->prev_frica_flag = 0;
837 103984 : set_f( st->hBWE_FD->mem_imdct, 0, L_FRAME48k );
838 103984 : st->hBWE_FD->prev_td_energy = 0.0f;
839 103984 : st->hBWE_FD->prev_weight = 0.2f;
840 103984 : st->hBWE_FD->prev_fb_ener_adjust = 0.0f;
841 : }
842 :
843 : /* reset WB BWE buffers */
844 61222841 : if ( st->last_extl != WB_BWE && st->extl == WB_BWE && st->hBWE_FD != NULL )
845 : {
846 27501 : set_f( st->hBWE_FD->old_wtda_swb, 0, output_frame );
847 :
848 27501 : if ( st->last_extl != SWB_BWE && st->last_extl != FB_BWE )
849 : {
850 26315 : st->hBWE_FD->prev_mode = NORMAL;
851 : }
852 :
853 27501 : st->hBWE_FD->prev_Energy_wb = 0.0f;
854 27501 : st->hBWE_FD->prev_L_swb_norm = 8;
855 27501 : set_f( st->hBWE_FD->mem_imdct, 0, L_FRAME48k );
856 27501 : st->hBWE_FD->prev_flag = 0;
857 : }
858 :
859 : /* reset TBE buffers */
860 61222841 : if ( st->hBWE_TD != NULL )
861 : {
862 : /* reset SWB TBE buffers */
863 18239217 : if ( ( ( st->extl == SWB_TBE || st->extl == FB_TBE || st->extl == SWB_CNG ) &&
864 3865794 : ( st->L_frame != st->last_L_frame || ( st->last_extl != SWB_TBE && st->last_extl != FB_TBE && st->last_core != TCX_20_CORE && st->last_core != TCX_10_CORE ) || st->last_core == HQ_CORE ) ) ||
865 17960611 : ( st->bwidth < st->last_bwidth && st->last_extl != SWB_TBE ) || st->old_ppp_mode || ( ( st->prev_coder_type == AUDIO || st->prev_coder_type == INACTIVE ) && st->bws_cnt > 0 ) || ( st->bws_cnt == 0 && st->prev_bws_cnt == N_WS2N_FRAMES ) )
866 : {
867 549861 : swb_tbe_reset( st->hBWE_TD->mem_csfilt, st->hBWE_TD->mem_genSHBexc_filt_down_shb, st->hBWE_TD->state_lpc_syn, st->hBWE_TD->syn_overlap, st->hBWE_TD->state_syn_shbexc, &( st->hBWE_TD->tbe_demph ), &( st->hBWE_TD->tbe_premph ), st->hBWE_TD->mem_stp_swb, &( st->hBWE_TD->gain_prec_swb ) );
868 :
869 : /* reset GainShape delay for SWB TBE FEC */
870 549861 : set_f( st->hBWE_TD->GainShape_Delay, 0, NUM_SHB_SUBFR / 2 );
871 :
872 549861 : swb_tbe_reset_synth( st->hBWE_TD->genSHBsynth_Hilbert_Mem, st->hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local );
873 :
874 549861 : if ( output_frame == L_FRAME16k )
875 : {
876 21307 : set_f( st->hBWE_TD->mem_resamp_HB_32k, 0, 2 * ALLPASSSECTIONS_STEEP + 1 ); /* reset in case that SWB TBE layer is transmitted, but the output is 16kHz sampled */
877 : }
878 :
879 549861 : set_f( st->hBWE_TD->int_3_over_2_tbemem_dec, 0.0f, INTERP_3_2_MEM_LEN );
880 549861 : st->hBWE_TD->prev_pow_exc16kWhtnd = 1.0f;
881 549861 : st->hBWE_TD->prev_mix_factor = 1.0f;
882 : }
883 17689356 : else if ( ( st->extl == SWB_TBE || st->extl == FB_TBE ) && ( ( st->element_mode == IVAS_CPE_TD && st->last_extl != SWB_TBE && st->last_extl != FB_TBE ) || ( st->element_mode != IVAS_CPE_TD && st->last_total_brate != st->total_brate ) || ( st->last_bwidth != st->bwidth ) || ( st->last_codec_mode != MODE1 ) || ( st->rf_flag != st->rf_flag_last ) ) )
884 : {
885 2583491 : set_f( st->hBWE_TD->state_lpc_syn, 0.0f, LPC_SHB_ORDER );
886 2583491 : set_f( st->hBWE_TD->state_syn_shbexc, 0.0f, L_SHB_LAHEAD );
887 2583491 : set_f( st->hBWE_TD->mem_stp_swb, 0.0f, LPC_SHB_ORDER );
888 2583491 : set_f( st->hBWE_TD->mem_zero_swb, 0, LPC_SHB_ORDER );
889 2583491 : st->hBWE_TD->gain_prec_swb = 1.0f;
890 : }
891 15105865 : else if ( st->hBWE_TD != NULL && ( st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE ) )
892 : {
893 11405479 : TBEreset_dec( st );
894 : }
895 :
896 : /* reset FB TBE buffers */
897 18239217 : if ( ( st->L_frame != st->last_L_frame || st->last_extl != FB_TBE ) && st->extl == FB_TBE )
898 : {
899 99032 : set_f( st->hBWE_TD->fb_state_lpc_syn, 0, LPC_SHB_ORDER );
900 99032 : st->hBWE_TD->fb_tbe_demph = 0;
901 99032 : fb_tbe_reset_synth( st->hBWE_TD->fbbwe_hpf_mem, &st->hBWE_TD->prev_fbbwe_ratio );
902 : }
903 :
904 : /* reset WB TBE buffers */
905 18239217 : if ( st->last_extl != WB_TBE && st->extl == WB_TBE && st->last_core != TCX_20_CORE && st->last_core != TCX_10_CORE )
906 : {
907 7198 : wb_tbe_extras_reset( st->hBWE_TD->mem_genSHBexc_filt_down_wb2, st->hBWE_TD->mem_genSHBexc_filt_down_wb3 );
908 7198 : wb_tbe_extras_reset_synth( st->hBWE_TD->state_lsyn_filt_shb, st->hBWE_TD->state_lsyn_filt_dwn_shb, st->hBWE_TD->mem_resamp_HB );
909 :
910 7198 : set_f( st->hBWE_TD->state_syn_shbexc, 0, L_SHB_LAHEAD / 4 );
911 7198 : set_f( st->hBWE_TD->syn_overlap, 0, L_SHB_LAHEAD );
912 7198 : set_f( st->hBWE_TD->mem_csfilt, 0, 2 );
913 : }
914 : }
915 :
916 : /* Interp_3_2 CNG buffers reset */
917 61222841 : if ( st->hTdCngDec != NULL && st->output_Fs == 48000 && ( st->last_core_brate > SID_2k40 ) && ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 ) && st->hTdCngDec != NULL )
918 : {
919 6706 : set_f( st->hTdCngDec->interpol_3_2_cng_dec, 0.0f, INTERP_3_2_MEM_LEN );
920 : }
921 :
922 61222841 : return error;
923 : }
924 :
925 :
926 : /*---------------------------------------------------------------------*
927 : * core_switching_hq_prepare_dec()
928 : *
929 : * Preprocessing in the first HQ frame after ACELP frame
930 : * Modify bit allocation for HQ core by removing ACELP subframe budget
931 : *---------------------------------------------------------------------*/
932 :
933 724 : void core_switching_hq_prepare_dec(
934 : Decoder_State *st, /* i/o: decoder state structure */
935 : int16_t *num_bits, /* i/o: bit budget update */
936 : const int16_t output_frame /* i : output frame length */
937 : )
938 : {
939 : int32_t cbrate;
940 :
941 724 : if ( st->last_core == HQ_CORE && st->prev_bfi )
942 : {
943 0 : mvr2r( st->hHQ_core->old_out, st->hHQ_core->fer_samples, output_frame );
944 : }
945 :
946 : /* set switching frame bitrate */
947 724 : if ( st->last_L_frame == L_FRAME )
948 : {
949 155 : if ( st->core_brate > ACELP_24k40 )
950 : {
951 1 : cbrate = ACELP_24k40;
952 : }
953 : else
954 : {
955 154 : cbrate = st->core_brate;
956 : }
957 :
958 : /* subtract ACELP switching frame bits */
959 155 : if ( st->core_brate >= ACELP_11k60 )
960 : {
961 146 : ( *num_bits )--; /* LP_FLAG bit */
962 : }
963 :
964 155 : *num_bits -= ACB_bits_tbl[BIT_ALLOC_IDX( cbrate, GENERIC, 0, 0 )]; /* pitch bits*/
965 155 : *num_bits -= gain_bits_tbl[BIT_ALLOC_IDX( cbrate, TRANSITION, 0, 0 )]; /* gain bits */
966 155 : *num_bits -= FCB_bits_tbl[BIT_ALLOC_IDX( cbrate, GENERIC, 0, 0 )]; /* FCB bits */
967 : }
968 : else /* L_frame == L_FRAME16k */
969 : {
970 569 : if ( st->core_brate <= ACELP_8k00 )
971 : {
972 0 : cbrate = ACELP_8k00;
973 : }
974 569 : else if ( st->core_brate <= ACELP_14k80 )
975 : {
976 0 : cbrate = ACELP_14k80;
977 : }
978 : else
979 : {
980 569 : cbrate = min( st->core_brate, ACELP_22k60 );
981 : }
982 :
983 : /* subtract ACELP switching frame bits */
984 569 : if ( st->core_brate >= ACELP_11k60 )
985 : {
986 : /* subtract one bit for LP filtering flag */
987 569 : ( *num_bits )--;
988 : }
989 569 : *num_bits -= ACB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( cbrate, GENERIC, 0, 0 )]; /* pitch bits*/
990 569 : *num_bits -= gain_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( cbrate, GENERIC, 0, 0 )]; /* gain bits */
991 569 : *num_bits -= FCB_bits_16kHz_tbl[BIT_ALLOC_IDX_16KHZ( cbrate, GENERIC, 0, 0 )]; /* FCB bits */
992 : }
993 :
994 : /* subtract BWE bits */
995 724 : if ( !( ( inner_frame_tbl[st->bwidth] == L_FRAME16k && st->last_L_frame == L_FRAME16k ) || inner_frame_tbl[st->bwidth] == L_FRAME8k ) )
996 : {
997 656 : *num_bits -= ( NOOFGAINBITS1 + AUDIODELAYBITS );
998 : }
999 :
1000 : /* reset state of old_out if switching */
1001 724 : set_f( st->hHQ_core->old_out, 0.0f, output_frame );
1002 :
1003 724 : return;
1004 : }
1005 :
1006 :
1007 : /*---------------------------------------------------------------------*
1008 : * bandwidth_switching_detect()
1009 : *
1010 : * Classification for band-width switching
1011 : *---------------------------------------------------------------------*/
1012 :
1013 61222841 : void bandwidth_switching_detect(
1014 : Decoder_State *st /* i/o: decoder state structure */
1015 : )
1016 : {
1017 61222841 : if ( ( st->element_mode == IVAS_CPE_TD && st->idchan == 1 ) || st->element_mode == IVAS_CPE_MDCT )
1018 : {
1019 : /* there is no BWE in TD stereo secondary channel and in MDCT stereo, IGF is part of the core decoding -> no BW switching -> reset BWS counters */
1020 43744576 : st->prev_bws_cnt = 0;
1021 43744576 : st->bws_cnt = 0;
1022 43744576 : st->bws_cnt1 = 0;
1023 :
1024 43744576 : return;
1025 : }
1026 :
1027 : /* update band-width switching counter */
1028 17478265 : if ( st->bws_cnt1 >= N_NS2W_FRAMES )
1029 : {
1030 255641 : st->bws_cnt1 = 0;
1031 : }
1032 17222624 : else if ( st->total_brate > ACELP_9k60 && st->last_core_brate < ACELP_9k60 && st->bwidth == SWB && st->last_bwidth == WB && st->last_low_rate_mode == 0 )
1033 : {
1034 282 : st->bws_cnt1++;
1035 : }
1036 17222342 : else if ( st->bws_cnt1 > 0 )
1037 : {
1038 28371 : st->bws_cnt = st->bwidth < st->last_bwidth ? 2 * ( N_NS2W_FRAMES - st->bws_cnt1 ) - 1 : 0;
1039 28371 : st->bws_cnt1 = st->bwidth < st->last_bwidth ? 0 : ( ( st->bwidth == SWB ) ? st->bws_cnt1 + 1 : 0 );
1040 : }
1041 :
1042 : /* update band-width switching counter */
1043 17478265 : if ( st->bws_cnt >= N_WS2N_FRAMES )
1044 : {
1045 254110 : st->bws_cnt = 0;
1046 : }
1047 17224155 : else if ( st->total_brate < ACELP_9k60 && st->last_core_brate > ACELP_9k60 && st->bwidth < st->last_bwidth && st->bwidth == WB )
1048 : {
1049 16618 : st->bws_cnt++;
1050 : }
1051 17207537 : else if ( st->bws_cnt > 0 )
1052 : {
1053 227430 : st->bws_cnt1 = st->bwidth > st->last_bwidth ? ( ( N_WS2N_FRAMES - st->bws_cnt ) >> 1 ) : 0;
1054 227430 : st->bws_cnt = st->bwidth > st->last_bwidth ? 0 : ( ( st->bwidth == WB ) ? st->bws_cnt + 1 : 0 );
1055 : }
1056 :
1057 17478265 : return;
1058 : }
1059 :
1060 :
1061 : /*---------------------------------------------------------------------*
1062 : * bw_switching_pre_proc()
1063 : *
1064 : * Band-width switching pre-processing
1065 : *---------------------------------------------------------------------*/
1066 :
1067 28266659 : void bw_switching_pre_proc(
1068 : Decoder_State *st, /* i/o: decoder state structure */
1069 : const float *old_syn_12k8_16k, /* i : ACELP core synthesis at 12.8kHz or 16kHz */
1070 : const int32_t last_element_brate, /* i : last element bitrate */
1071 : const int16_t nchan_out /* i : number of output channels */
1072 : )
1073 : {
1074 : int16_t i;
1075 : float syn_dct[L_FRAME];
1076 :
1077 28266659 : if ( st->element_mode > EVS_MONO )
1078 : {
1079 28165816 : if ( st->core == ACELP_CORE && !( st->bfi == 1 && st->con_tcx == 1 ) && st->hBWE_FD != NULL && !( st->core_brate <= SID_2k40 && st->element_mode == IVAS_CPE_DFT && nchan_out == 2 ) && !( st->element_mode == IVAS_CPE_MDCT && nchan_out == 1 && st->idchan == 1 && last_element_brate <= IVAS_SID_5k2 ) )
1080 : {
1081 : /* Calculate tilt of the ACELP core synthesis - needed in SWB BWE decoding */
1082 6142410 : calc_tilt_bwe( old_syn_12k8_16k, &st->tilt_wb, st->L_frame );
1083 : }
1084 :
1085 28165816 : return;
1086 : }
1087 :
1088 100843 : if ( st->core == ACELP_CORE && !( st->bfi == 1 && st->con_tcx == 1 ) )
1089 : {
1090 : /*----------------------------------------------------------------------*
1091 : * Calculate tilt of the ACELP core synthesis
1092 : *----------------------------------------------------------------------*/
1093 :
1094 78201 : calc_tilt_bwe( old_syn_12k8_16k, &st->tilt_wb, st->L_frame );
1095 :
1096 : /*-------------------------------------------------------------------------------*
1097 : * Calculate frequency energy of 0~3.2kHz and 3.2~6.4kHz the ACELP core synthesis
1098 : *-------------------------------------------------------------------------------*/
1099 :
1100 78201 : edct( old_syn_12k8_16k, syn_dct, L_FRAME, st->element_mode );
1101 :
1102 78201 : st->enerLL = EPSILON;
1103 10087929 : for ( i = 0; i < L_FRAME / 2; i++ )
1104 : {
1105 10009728 : st->enerLL += syn_dct[i] * syn_dct[i];
1106 : }
1107 78201 : st->enerLL = (float) sqrt( st->enerLL / 128 );
1108 :
1109 78201 : st->enerLH = EPSILON;
1110 10087929 : for ( ; i < L_FRAME; i++ )
1111 : {
1112 10009728 : st->enerLH += syn_dct[i] * syn_dct[i];
1113 : }
1114 78201 : st->enerLH = (float) sqrt( st->enerLH / 128 );
1115 : }
1116 : else
1117 : {
1118 22642 : if ( st->hHQ_core->old_is_transient[0] )
1119 : {
1120 1136 : st->enerLL = EPSILON;
1121 37488 : for ( i = 0; i < 32; i++ )
1122 : {
1123 36352 : st->enerLL += st->t_audio_q[i] * st->t_audio_q[i];
1124 : }
1125 1136 : st->enerLL = (float) sqrt( st->enerLL / 32 );
1126 :
1127 1136 : st->enerLH = EPSILON;
1128 37488 : for ( ; i < 64; i++ )
1129 : {
1130 36352 : st->enerLH += st->t_audio_q[i] * st->t_audio_q[i];
1131 : }
1132 1136 : st->enerLH = (float) sqrt( st->enerLH / 32 );
1133 : }
1134 : else
1135 : {
1136 21506 : st->enerLL = EPSILON;
1137 2774274 : for ( i = 0; i < L_FRAME / 2; i++ )
1138 : {
1139 2752768 : st->enerLL += st->t_audio_q[i] * st->t_audio_q[i];
1140 : }
1141 21506 : st->enerLL = (float) sqrt( st->enerLL / 128 );
1142 :
1143 21506 : st->enerLH = EPSILON;
1144 2774274 : for ( ; i < L_FRAME; i++ )
1145 : {
1146 2752768 : st->enerLH += st->t_audio_q[i] * st->t_audio_q[i];
1147 : }
1148 21506 : st->enerLH = (float) sqrt( st->enerLH / 128 );
1149 : }
1150 : }
1151 :
1152 100843 : if ( st->last_bwidth == 0 && st->extl <= SWB_CNG )
1153 : {
1154 6090 : st->prev_ener_shb = 0.0f;
1155 6090 : if ( st->hBWE_FD != NULL )
1156 : {
1157 6090 : set_f( st->hBWE_FD->prev_SWB_fenv, 0, SWB_FENV );
1158 : }
1159 : }
1160 94753 : else if ( ( ( st->core == ACELP_CORE && ( st->last_core == HQ_CORE || st->last_core == TCX_10_CORE || st->last_core == TCX_20_CORE ) ) || ( st->core == st->last_core && st->extl != st->last_extl ) ) && st->last_bwidth >= SWB )
1161 : {
1162 1577 : st->attenu1 = 0.1f;
1163 : }
1164 :
1165 100843 : if ( st->last_core == HQ_CORE || ( st->last_core == ACELP_CORE && !( st->last_extl == WB_TBE || st->last_extl == SWB_TBE || st->last_extl == FB_TBE ) && st->core_brate > ACELP_8k00 ) )
1166 : {
1167 58598 : st->prev_fractive = 0;
1168 : }
1169 :
1170 100843 : return;
1171 : }
1172 :
1173 :
1174 : /*---------------------------------------------------------------------*
1175 : * core_switch_lb_upsamp()
1176 : *
1177 : * Resample HQ/TCX-LB to the output sampling rate (8/16/32/48 kHz)
1178 : *---------------------------------------------------------------------*/
1179 :
1180 329109 : static void core_switch_lb_upsamp(
1181 : Decoder_State *st, /* i/o: Decoder state */
1182 : float *output /* i/o: LB synth/upsampled LB synth */
1183 : )
1184 : {
1185 : int16_t i;
1186 : float *realBuffer[CLDFB_OVRLP_MIN_SLOTS], *imagBuffer[CLDFB_OVRLP_MIN_SLOTS];
1187 : float realBufferTmp[CLDFB_OVRLP_MIN_SLOTS][CLDFB_NO_CHANNELS_MAX];
1188 : float imagBufferTmp[CLDFB_OVRLP_MIN_SLOTS][CLDFB_NO_CHANNELS_MAX];
1189 :
1190 : /* open CLDFB buffer up to CLDFB_NO_CHANNELS_MAX bands for 48kHz */
1191 1316436 : for ( i = 0; i < CLDFB_OVRLP_MIN_SLOTS; i++ )
1192 : {
1193 987327 : set_f( realBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
1194 987327 : set_f( imagBufferTmp[i], 0, CLDFB_NO_CHANNELS_MAX );
1195 987327 : realBuffer[i] = realBufferTmp[i];
1196 987327 : imagBuffer[i] = imagBufferTmp[i];
1197 : }
1198 :
1199 : /* check if the CLDFB works on the right sample rate */
1200 329109 : if ( ( st->cldfbAna->no_channels * st->cldfbAna->no_col ) != st->L_frame )
1201 : {
1202 55703 : resampleCldfb( st->cldfbAna, st->L_frame * FRAMES_PER_SEC );
1203 :
1204 55703 : if ( st->cldfbBPF != NULL && st->L_frame <= L_FRAME16k )
1205 : {
1206 9990 : resampleCldfb( st->cldfbBPF, st->L_frame * FRAMES_PER_SEC );
1207 : }
1208 :
1209 55703 : if ( st->ini_frame > 0 )
1210 : {
1211 55368 : st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
1212 : }
1213 : }
1214 :
1215 : /* analysis of the synthesis at internal sampling rate */
1216 329109 : cldfbAnalysis( output, realBuffer, imagBuffer, CLDFB_OVRLP_MIN_SLOTS * st->cldfbAna->no_channels, st->cldfbAna );
1217 :
1218 : /* analysis and add the BPF error signal */
1219 329109 : if ( st->p_bpf_noise_buf )
1220 : {
1221 281997 : addBassPostFilter( st->p_bpf_noise_buf, st->bpf_off ? 0 : CLDFB_OVRLP_MIN_SLOTS * st->cldfbBPF->no_channels, realBuffer, imagBuffer, st->cldfbBPF );
1222 : }
1223 :
1224 : /* set output mask for upsampling */
1225 329109 : if ( st->bwidth == NB )
1226 : {
1227 : /* set NB mask for upsampling */
1228 0 : st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - 10;
1229 : }
1230 329109 : else if ( st->cldfbSyn->bandsToZero != st->cldfbSyn->no_channels - st->cldfbAna->no_channels )
1231 : {
1232 : /* in case of BW switching, re-init to default */
1233 6468 : st->cldfbSyn->bandsToZero = st->cldfbSyn->no_channels - st->cldfbAna->no_channels;
1234 : }
1235 :
1236 : /* synthesis of the combined signal */
1237 329109 : cldfbSynthesis( realBuffer, imagBuffer, output, CLDFB_OVRLP_MIN_SLOTS * st->cldfbSyn->no_channels, st->cldfbSyn );
1238 :
1239 : /* save synthesis - needed in case of core switching */
1240 329109 : if ( st->hTcxDec != NULL )
1241 : {
1242 329109 : mvr2r( output, st->previoussynth, st->hTcxDec->L_frameTCX );
1243 : }
1244 :
1245 329109 : return;
1246 : }
1247 :
1248 : /*---------------------------------------------------------------------*
1249 : * smoothTransitionDtxToTcx()
1250 : *
1251 : * apply smoothing to the transition part for inactive to active transitions in DTX
1252 : *---------------------------------------------------------------------*/
1253 :
1254 : #define TRANSITION_SMOOTHING_LEN_16k 15
1255 : #define TRANSITION_SMOOTHING_LEN_32k 31
1256 : #define TRANSITION_SMOOTHING_LEN_48k 47
1257 :
1258 10289 : static void smoothTransitionDtxToTcx(
1259 : float synth[], /* i/o: synthesis */
1260 : const int16_t output_frame, /* i : output frame length */
1261 : const int16_t delay_comp /* i : delay compensation in samples */
1262 : )
1263 : {
1264 : int16_t i, filter_len;
1265 : float w, mem, step, fade_in;
1266 : float smoothing_input_buffer[2 * NS2SA( 48000, DELAY_CLDFB_NS ) + TRANSITION_SMOOTHING_LEN_48k];
1267 : float smoothing_out_buffer[2 * NS2SA( 48000, DELAY_CLDFB_NS ) + TRANSITION_SMOOTHING_LEN_48k];
1268 :
1269 10289 : filter_len = TRANSITION_SMOOTHING_LEN_16k;
1270 10289 : if ( output_frame == L_FRAME32k )
1271 : {
1272 1366 : filter_len = TRANSITION_SMOOTHING_LEN_32k;
1273 : }
1274 8923 : else if ( output_frame == L_FRAME48k )
1275 : {
1276 8035 : filter_len = TRANSITION_SMOOTHING_LEN_48k;
1277 : }
1278 :
1279 : /* prepare buffer */
1280 221800 : for ( i = 0; i < filter_len / 2; i++ )
1281 : {
1282 211511 : smoothing_input_buffer[i] = synth[0];
1283 : }
1284 10289 : mvr2r( synth, smoothing_input_buffer + filter_len / 2, 2 * delay_comp + filter_len / 2 );
1285 :
1286 : /* apply Mean filter */
1287 10289 : w = 1.f / filter_len;
1288 10289 : mem = sum_f( smoothing_input_buffer, filter_len );
1289 1119289 : for ( i = 0; i < 2 * delay_comp; i++ )
1290 : {
1291 1109000 : smoothing_out_buffer[i] = w * mem;
1292 1109000 : mem = mem - smoothing_input_buffer[i] + smoothing_input_buffer[i + filter_len];
1293 : }
1294 :
1295 : /* apply fades around transition */
1296 10289 : step = 1.f / delay_comp;
1297 10289 : fade_in = 0.f;
1298 564789 : for ( i = 0; i < delay_comp; i++ )
1299 : {
1300 554500 : synth[i] = smoothing_out_buffer[i] * fade_in + synth[i] * ( 1 - fade_in );
1301 554500 : fade_in += step;
1302 : }
1303 :
1304 10289 : fade_in = 0.f;
1305 564789 : for ( ; i < 2 * delay_comp; i++ )
1306 : {
1307 554500 : synth[i] = synth[i] * fade_in + smoothing_out_buffer[i] * ( 1 - fade_in );
1308 554500 : fade_in += step;
1309 : }
1310 :
1311 10289 : return;
1312 : }
|