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 : #include <stdint.h>
34 : #include "options.h"
35 : #ifdef DEBUGGING
36 : #include "debug.h"
37 : #endif
38 : #include <assert.h>
39 : #include <math.h>
40 : #include "cnst.h"
41 : #include "rom_com.h"
42 : #include "prot.h"
43 : #include "ivas_prot.h"
44 : #include "ivas_rom_com.h"
45 : #include "wmc_auto.h"
46 : #ifdef DEBUG_PLOT
47 : #include "deb_out.h"
48 : #endif
49 :
50 :
51 : /*-------------------------------------------------------------------*
52 : * ivas_core_dec()
53 : *
54 : * Principal IVAS core decoder routine, where number of core channels is 1 or 2
55 : *-------------------------------------------------------------------*/
56 :
57 2308260 : ivas_error ivas_core_dec(
58 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
59 : SCE_DEC_HANDLE hSCE, /* i/o: SCE decoder structure */
60 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
61 : MCT_DEC_HANDLE hMCT, /* i/o: MCT decoder structure */
62 : const int16_t n_channels, /* i : number of channels to be decoded */
63 : float *output[], /* o : output synthesis signal */
64 : float hb_synth[][L_FRAME48k], /* o : output HB synthesis signal */
65 : float DFT[CPE_CHANNELS][STEREO_DFT_BUF_MAX], /* o : DFT buffers */
66 : const int16_t sba_dirac_stereo_flag /* i : signal stereo output for SBA DirAC */
67 : )
68 : {
69 : int16_t i, n, output_frame;
70 : Decoder_State **sts, *st;
71 : STEREO_ICBWE_DEC_HANDLE hStereoICBWE;
72 : STEREO_TD_DEC_DATA_HANDLE hStereoTD;
73 : STEREO_CNG_DEC_HANDLE hStereoCng;
74 : int16_t sharpFlag[CPE_CHANNELS];
75 : float synth[CPE_CHANNELS][L_FRAME48k];
76 : float tmp_buffer[L_FRAME48k];
77 : int16_t tmps, incr;
78 : #ifdef FIX_1320_STACK_CPE_DECODER
79 2308260 : float *bwe_exc_extended[CPE_CHANNELS] = { NULL, NULL };
80 : int16_t flag_bwe_bws;
81 : #else
82 : float bwe_exc_extended[CPE_CHANNELS][L_FRAME32k + NL_BUFF_OFFSET];
83 : #endif
84 : float voice_factors[CPE_CHANNELS][NB_SUBFR16k];
85 : int16_t core_switching_flag[CPE_CHANNELS];
86 : float old_syn_12k8_16k[CPE_CHANNELS][L_FRAME16k];
87 : float tmp;
88 : float pitch_buf[CPE_CHANNELS][NB_SUBFR16k];
89 : int16_t unbits[CPE_CHANNELS];
90 : int16_t sid_bw[CPE_CHANNELS];
91 : FRAME_MODE frameMode[CPE_CHANNELS];
92 : float tdm_lspQ_PCh[M], tdm_lsfQ_PCh[M];
93 : int16_t tdm_LRTD_flag;
94 : int32_t element_brate, output_Fs;
95 : int32_t last_element_brate;
96 : int16_t use_cldfb_for_dft;
97 : float *p_output_mem;
98 : int16_t flag_sec_CNA;
99 : int16_t read_sid_info;
100 : int16_t last_element_mode;
101 : int16_t nchan_out;
102 : float *save_hb_synth;
103 : ivas_error error;
104 :
105 2308260 : error = IVAS_ERR_OK;
106 2308260 : push_wmops( "ivas_core_dec" );
107 :
108 : /*------------------------------------------------------------------*
109 : * General initialization
110 : *-----------------------------------------------------------------*/
111 :
112 2308260 : use_cldfb_for_dft = 0;
113 2308260 : tdm_LRTD_flag = -1;
114 2308260 : read_sid_info = 1; /* read SID by default */
115 :
116 2308260 : if ( hSCE != NULL )
117 : {
118 1047102 : sts = hSCE->hCoreCoder;
119 1047102 : hStereoICBWE = NULL;
120 1047102 : element_brate = hSCE->element_brate;
121 1047102 : last_element_brate = hSCE->last_element_brate; /* note: this parameter is unused */
122 1047102 : last_element_mode = IVAS_SCE;
123 1047102 : hStereoTD = NULL;
124 1047102 : hStereoCng = NULL;
125 1047102 : p_output_mem = NULL;
126 1047102 : nchan_out = 1;
127 1047102 : if ( st_ivas != NULL && st_ivas->ivas_format == ISM_FORMAT )
128 : {
129 807027 : if ( st_ivas->hISMDTX.sce_id_dtx != hSCE->sce_id )
130 : {
131 495975 : read_sid_info = 0;
132 : }
133 : }
134 : }
135 : else
136 : {
137 1261158 : sts = hCPE->hCoreCoder;
138 1261158 : element_brate = hCPE->element_brate;
139 1261158 : last_element_brate = hCPE->last_element_brate;
140 1261158 : last_element_mode = hCPE->last_element_mode;
141 1261158 : hStereoICBWE = hCPE->hStereoICBWE;
142 1261158 : hStereoTD = hCPE->hStereoTD;
143 1261158 : hStereoCng = hCPE->hStereoCng;
144 1261158 : p_output_mem = hCPE->output_mem[1];
145 1261158 : nchan_out = hCPE->nchan_out;
146 :
147 1261158 : if ( hCPE->hStereoTD != NULL )
148 : {
149 11340 : tdm_LRTD_flag = hCPE->hCoreCoder[0]->tdm_LRTD_flag;
150 : }
151 :
152 1261158 : if ( sts[0]->element_mode == IVAS_CPE_DFT )
153 : {
154 179058 : use_cldfb_for_dft = ( hCPE->nchan_out == 1 && hCPE->hStereoDft->hConfig->res_cod_mode == STEREO_DFT_RES_COD_OFF ) ? 1 : 0;
155 : }
156 : }
157 :
158 2308260 : output_Fs = sts[0]->output_Fs;
159 2308260 : output_frame = (int16_t) ( output_Fs / FRAMES_PER_SEC );
160 :
161 5698620 : for ( n = 0; n < n_channels; n++ )
162 : {
163 3390360 : st = sts[n];
164 :
165 : /*------------------------------------------------------------------*
166 : * Initialization per core-decoder channel
167 : *-----------------------------------------------------------------*/
168 :
169 3390360 : frameMode[n] = FRAMEMODE_NORMAL;
170 3390360 : st->rate_switching_reset = 0;
171 :
172 3390360 : st->mdct_sw = MODE1;
173 :
174 3390360 : sid_bw[n] = -1;
175 3390360 : core_switching_flag[n] = 0;
176 3390360 : sharpFlag[n] = 0;
177 3390360 : unbits[n] = 0;
178 3390360 : st->GSC_IVAS_mode = 0;
179 3390360 : st->element_brate = element_brate;
180 :
181 3390360 : st->use_partial_copy = 0;
182 3390360 : st->rf_flag = 0;
183 3390360 : st->rf_frame_type = RF_NO_DATA;
184 :
185 3390360 : if ( st->bfi == 1 )
186 : {
187 66945 : frameMode[n] = FRAMEMODE_MISSING;
188 66945 : st->coder_type = st->last_coder_type;
189 : }
190 : else
191 : {
192 3323415 : if ( !( st->element_mode == IVAS_CPE_TD && n == 1 ) ) /* coder_type for SCh in TD stereo is already read in tdm_configure_dec() */
193 : {
194 3312459 : st->coder_type = INACTIVE;
195 : }
196 3323415 : st->extl = -1;
197 3323415 : st->flagGuidedAcelp = 0;
198 : }
199 :
200 3390360 : if ( !st->bfi && st->prev_bfi && st->total_brate > SID_2k40 && ( st->last_core_bfi == TCX_20_CORE || st->last_core_bfi == TCX_10_CORE ) && st->hTcxDec != NULL )
201 : {
202 22038 : v_multc( st->hHQ_core->old_out, st->hTcxDec->conceal_eof_gain * st->last_concealed_gain_syn_deemph, st->hHQ_core->old_out, st->hTcxDec->L_frameTCX );
203 22038 : v_multc( st->hHQ_core->old_outLB, st->hTcxDec->conceal_eof_gain * st->last_concealed_gain_syn_deemph, st->hHQ_core->old_outLB, st->L_frame );
204 : }
205 :
206 3390360 : set_f( voice_factors[n], 0.f, NB_SUBFR16k );
207 3390360 : set_f( hb_synth[n], 0.0f, L_FRAME48k );
208 : #ifdef FIX_1320_STACK_CPE_DECODER
209 3390360 : bwe_exc_extended[n] = hb_synth[n]; /* note: reuse the buffer */
210 : #endif
211 :
212 : /*------------------------------------------------------------------*
213 : * Decision matrix (selection of technologies)
214 : *-----------------------------------------------------------------*/
215 :
216 3390360 : if ( st->bfi != 1 )
217 : {
218 3323415 : ivas_decision_matrix_dec( st, &sharpFlag[n], &core_switching_flag[n], element_brate, nchan_out );
219 :
220 3323415 : synchonize_channels_mdct_sid( sts, n );
221 :
222 3323415 : if ( st->bfi != 1 ) /* note: st->bfi can be changed from 0 to 1 in ivas_decision_matrix_dec() when BER is detected */
223 : {
224 3323415 : st->sr_core = st->L_frame * FRAMES_PER_SEC;
225 3323415 : st->fscale_old = st->fscale;
226 3323415 : st->fscale = sr2fscale( st->sr_core );
227 : }
228 : else
229 : {
230 0 : frameMode[n] = FRAMEMODE_MISSING;
231 : }
232 : }
233 66945 : else if ( st->element_mode >= IVAS_SCE && st->prev_bfi == 1 && st->last_con_tcx == 1 )
234 : {
235 7572 : st->core = TCX_20_CORE;
236 : }
237 : } /* n_channels loop */
238 :
239 : /* MDCT stereo -> DFT stereo switching */
240 2308260 : if ( last_element_mode == IVAS_CPE_MDCT && sts[0]->element_mode == IVAS_CPE_DFT )
241 : {
242 : int16_t ovl, fade_len;
243 2148 : if ( sts[0]->L_frame != sts[0]->last_L_frame )
244 : {
245 2145 : lerp( sts[0]->hHQ_core->old_outLB, sts[0]->hHQ_core->old_outLB, sts[0]->L_frame, sts[0]->last_L_frame );
246 : }
247 2148 : if ( sts[0]->L_frame != L_FRAME16k )
248 : {
249 1308 : ovl = (int16_t) ( STEREO_DFT32MS_OVL_16k * ( (float) sts[0]->L_frame / L_FRAME16k ) );
250 1308 : lerp( hCPE->input_mem_LB[0], hCPE->input_mem_LB[0], ovl, STEREO_DFT32MS_OVL_16k );
251 :
252 1308 : fade_len = (int16_t) ( STEREO_MDCT2DFT_FADE_LEN_48k * ( (float) sts[0]->L_frame / L_FRAME16k ) );
253 1308 : lerp( hCPE->old_outLB_mdct, hCPE->old_outLB_mdct, fade_len, STEREO_MDCT2DFT_FADE_LEN_48k );
254 : }
255 : }
256 :
257 2308260 : if ( hStereoCng != NULL )
258 : {
259 190398 : hStereoCng->flag_cna_fade = 0;
260 : }
261 :
262 2308260 : save_hb_synth = NULL;
263 2308260 : if ( sba_dirac_stereo_flag && hSCE && sts[0]->total_brate <= SID_2k40 && sts[0]->cng_type == FD_CNG )
264 : {
265 273 : save_hb_synth = hSCE->save_hb_synth;
266 : }
267 :
268 : /*------------------------------------------------------------------*
269 : * Decode SID for MDCT-Stereo DTX mode
270 : *-----------------------------------------------------------------*/
271 :
272 2308260 : if ( sts[0]->element_mode == IVAS_CPE_MDCT && sts[0]->total_brate == SID_2k40 )
273 : {
274 1704 : if ( sts[0]->cng_sba_flag )
275 : {
276 501 : FdCngDecodeDiracMDCTStereoSID( hCPE );
277 : }
278 : else
279 : {
280 1203 : FdCngDecodeMDCTStereoSID( hCPE );
281 : }
282 : }
283 :
284 : /*------------------------------------------------------------------*
285 : * Sanity check in combined format coding
286 : *-----------------------------------------------------------------*/
287 :
288 2308260 : if ( hCPE != NULL && hCPE->element_mode == IVAS_CPE_DFT && hCPE->brate_surplus > 0 )
289 : {
290 306 : ivas_combined_format_brate_sanity( element_brate, sts[0]->core, sts[0]->total_brate, &( sts[0]->core_brate ), &( sts[0]->inactive_coder_type_flag ), &tmps );
291 : }
292 :
293 : /*------------------------------------------------------------------*
294 : * Core Decoding
295 : *-----------------------------------------------------------------*/
296 :
297 5698620 : for ( n = 0; n < n_channels; n++ )
298 : {
299 3390360 : st = sts[n];
300 :
301 : /*------------------------------------------------------------------*
302 : * Initialization
303 : *-----------------------------------------------------------------*/
304 :
305 3390360 : if ( st->bfi == 1 )
306 : {
307 66945 : st->nbLostCmpt++;
308 : }
309 : else
310 : {
311 3323415 : if ( st->prev_bfi == 1 )
312 : {
313 39447 : st->prev_nbLostCmpt = st->nbLostCmpt;
314 : }
315 : else
316 : {
317 3283968 : st->prev_nbLostCmpt = 0;
318 : }
319 :
320 3323415 : st->nbLostCmpt = 0;
321 : }
322 3390360 : st->enablePlcWaveadjust = 0;
323 :
324 3390360 : if ( n == 1 )
325 : {
326 1082100 : sts[1]->BER_detect |= sts[0]->BER_detect;
327 : }
328 :
329 : /*---------------------------------------------------------------------*
330 : * Detect bandwidth switching
331 : *---------------------------------------------------------------------*/
332 :
333 3390360 : bandwidth_switching_detect( st );
334 :
335 : /*---------------------------------------------------------------------*
336 : * Preprocessing (preparing) for ACELP/HQ core switching
337 : *---------------------------------------------------------------------*/
338 :
339 3390360 : if ( ( error = core_switching_pre_dec( st, output_frame, sts[0]->last_core_brate, nchan_out, last_element_mode, last_element_brate ) ) != IVAS_ERR_OK )
340 : {
341 0 : return error;
342 : }
343 :
344 3390360 : flag_sec_CNA = -1;
345 3390360 : if ( hCPE != NULL )
346 : {
347 2343258 : flag_sec_CNA = sts[1]->flag_cna;
348 : }
349 :
350 : /*---------------------------------------------------------------------*
351 : * ACELP core decoding
352 : * TCX core decoding
353 : * HQ core decoding
354 : *---------------------------------------------------------------------*/
355 :
356 3390360 : if ( st->core == ACELP_CORE )
357 : {
358 : /* ACELP core decoder */
359 545622 : if ( ( error = acelp_core_dec( st, output[n], synth[n], save_hb_synth, bwe_exc_extended[n], voice_factors[n], old_syn_12k8_16k[n], sharpFlag[n], pitch_buf[n], &unbits[n], &sid_bw[n], hStereoTD, tdm_lspQ_PCh, tdm_lsfQ_PCh, use_cldfb_for_dft, last_element_mode, last_element_brate, flag_sec_CNA, nchan_out, hStereoCng, read_sid_info ) ) != IVAS_ERR_OK )
360 : {
361 0 : return error;
362 : }
363 : }
364 :
365 3390360 : if ( ( st->core == TCX_20_CORE || st->core == TCX_10_CORE ) && st->element_mode != IVAS_CPE_MDCT )
366 : {
367 : /* TCX core decoder */
368 704082 : stereo_tcx_core_dec( st, frameMode[n], output[n], synth[n], pitch_buf[n], sba_dirac_stereo_flag, hStereoTD, last_element_mode, flag_sec_CNA, hCPE == NULL ? NULL : hCPE->hStereoCng, nchan_out, st_ivas == NULL ? 0 : st_ivas->ivas_format );
369 : }
370 :
371 3390360 : if ( st->core == HQ_CORE )
372 : {
373 : /* HQ core decoder */
374 24114 : hq_core_dec( st, synth[n], output_frame, NORMAL_HQ_CORE, core_switching_flag[n], output[n] );
375 : }
376 :
377 : /*---------------------------------------------------------------------*
378 : * TD stereo updates
379 : *---------------------------------------------------------------------*/
380 :
381 3390360 : if ( st->element_mode == IVAS_CPE_TD && n == 0 )
382 : {
383 11340 : td_stereo_param_updt( st->lsp_old, st->lsf_old, st->old_pitch_buf + st->nb_subfr, tdm_lspQ_PCh, tdm_lsfQ_PCh, hStereoTD->tdm_Pri_pitch_buf, st->flag_ACELP16k, hStereoTD->tdm_use_IAWB_Ave_lpc );
384 : }
385 :
386 : } /* n_channels loop */
387 :
388 : /*---------------------------------------------------------------------*
389 : * MDCT stereo: joint TCX Core Decoding
390 : *---------------------------------------------------------------------*/
391 :
392 2308260 : if ( sts[0]->element_mode == IVAS_CPE_MDCT )
393 : {
394 : /* active-frame decoding */
395 1070760 : if ( sts[0]->core_brate > SID_2k40 )
396 : {
397 1058271 : if ( hMCT )
398 : {
399 811008 : pop_wmops();
400 :
401 811008 : return error;
402 : }
403 : else
404 : {
405 247263 : stereo_mdct_core_dec( st_ivas, hCPE, output, synth );
406 : }
407 : }
408 : /* for inactive frames with mono output, copy and (if necessary) downmix buffers */
409 12489 : else if ( hCPE->nchan_out == 1 )
410 : {
411 3357 : updateBuffersForDmxMdctStereo( hCPE, output_frame, output, synth );
412 : }
413 :
414 : /* On first good active frame after frameloss undo the whitening of the bg noise shape */
415 259752 : if ( sts[0]->core_brate > SID_2k40 && sts[0]->bfi == 0 && sts[0]->prev_bfi == 1 )
416 : {
417 11736 : for ( n = 0; n < n_channels; ++n )
418 : {
419 7824 : if ( sts[n]->last_core_bfi != ACELP_CORE )
420 : {
421 5811 : TonalMdctConceal_whiten_noise_shape( sts[n], L_FRAME16k, ON_FIRST_GOOD_FRAME );
422 : }
423 : }
424 : }
425 : }
426 :
427 : /*---------------------------------------------------------------------*
428 : * Stereo CNG updates
429 : *---------------------------------------------------------------------*/
430 :
431 1497252 : if ( sts[0]->element_mode == IVAS_CPE_TD && hStereoCng != NULL )
432 : {
433 11340 : stereo_cng_compute_PScorr( output, &hStereoCng->c_PS_LT, sts[0]->L_frame, sts[1]->L_frame );
434 : }
435 :
436 : /*---------------------------------------------------------------------*
437 : * Postprocessing, BWEs and updates
438 : *---------------------------------------------------------------------*/
439 :
440 3252060 : for ( n = 0; n < n_channels; n++ )
441 : {
442 : #ifdef DEBUG_PLOT
443 : setDeboutVars( -1, -1, n, -1 );
444 : #endif
445 1768344 : st = sts[n];
446 :
447 : /*---------------------------------------------------------------------*
448 : * TD-BWE for ACELP to TCX transitions
449 : *---------------------------------------------------------------------*/
450 :
451 1768344 : if ( st->last_core == ACELP_CORE && ( st->core == TCX_20_CORE || st->core == TCX_10_CORE || st->core == HQ_CORE ) && st->hBWE_TD != NULL )
452 : {
453 25842 : if ( ( st->bwidth == SWB || st->bwidth == FB ) && ( st->last_extl == SWB_TBE || st->last_extl == FB_TBE ) )
454 : {
455 19206 : GenTransition( st->hBWE_TD, hb_synth[n], output_Fs, st->element_mode, st->L_frame, st->rf_flag, st->total_brate );
456 : }
457 6636 : else if ( st->bwidth == WB && st->last_extl == WB_TBE )
458 : {
459 360 : GenTransition_WB( st->hBWE_TD, hb_synth[n], output_Fs );
460 : }
461 : }
462 :
463 : /*---------------------------------------------------------------------*
464 : * Postprocessing for ACELP/MDCT core switching
465 : *---------------------------------------------------------------------*/
466 :
467 : /* save synth and output in case of SBA DirAC stereo output as core switching is done outside of core decoder */
468 1768344 : if ( sba_dirac_stereo_flag && st->element_mode != IVAS_CPE_MDCT && !( st->core_brate == SID_2k40 && st->cng_type == FD_CNG ) )
469 : {
470 13497 : mvr2r( synth[n], hSCE->save_synth, output_frame );
471 : }
472 :
473 1768344 : if ( ( error = core_switching_post_dec( st, synth[n], output[n], p_output_mem, use_cldfb_for_dft, output_frame, 0 /*core_switching_flag*/, sba_dirac_stereo_flag, nchan_out, last_element_mode ) ) != IVAS_ERR_OK )
474 : {
475 0 : return error;
476 : }
477 :
478 : /* for FD-CNG we need the delay compensation in the synth, so do this afterwards */
479 1768344 : if ( sba_dirac_stereo_flag && hSCE && st->core_brate == SID_2k40 && st->cng_type == FD_CNG )
480 : {
481 39 : mvr2r( synth[n], hSCE->save_synth, output_frame );
482 : }
483 :
484 : /* if we transition from inactive to active coding in MDCT-Stereo DTX and the output format is mono DMX, we need to sync the upsampled buffer between channels here */
485 1768344 : if ( n == 0 && st->element_mode == IVAS_CPE_MDCT && st->last_core == ACELP_CORE && st->core != ACELP_CORE && ( nchan_out == 1 || last_element_mode == IVAS_CPE_DFT ) )
486 : {
487 1785 : mvr2r( sts[0]->previoussynth, sts[1]->previoussynth, st->hTcxDec->L_frameTCX );
488 : }
489 :
490 : /*---------------------------------------------------------------------*
491 : * Pre-processing for bandwidth switching
492 : *---------------------------------------------------------------------*/
493 :
494 1768344 : bw_switching_pre_proc( st, old_syn_12k8_16k[n], last_element_brate, nchan_out );
495 :
496 : /*---------------------------------------------------------------------*
497 : * WB TBE decoding
498 : * WB BWE decoding
499 : *---------------------------------------------------------------------*/
500 :
501 1768344 : if ( st->extl == WB_TBE )
502 : {
503 : /* WB TBE decoder */
504 20967 : wb_tbe_dec( st, bwe_exc_extended[n], voice_factors[n], hb_synth[n] );
505 : }
506 1747377 : else if ( st->element_mode == IVAS_CPE_TD && n == 1 && !tdm_LRTD_flag && st->extl != -1 && st->bws_cnt == 0 && st->extl_brate == 0 )
507 : {
508 : /* do nothing */
509 : }
510 1747002 : else if ( st->extl == WB_BWE && st->bws_cnt == 0 )
511 : {
512 : /* WB BWE decoder */
513 27591 : wb_bwe_dec( st, output[n], synth[n], hb_synth[n], use_cldfb_for_dft, output_frame, voice_factors[n], pitch_buf[n] );
514 : }
515 :
516 : /*---------------------------------------------------------------------*
517 : * SWB(FB) TBE decoding
518 : * SWB(FB) BWE decoding
519 : *---------------------------------------------------------------------*/
520 :
521 : #ifdef FIX_1320_STACK_CPE_DECODER
522 1768344 : flag_bwe_bws = ( output_Fs >= 32000 && st->core == ACELP_CORE && st->bwidth > NB && st->bws_cnt > 0 && st->bfi == 0 );
523 :
524 : #endif
525 1768344 : if ( st->extl == SWB_TBE || st->extl == FB_TBE || ( st->coder_type != AUDIO && st->coder_type != INACTIVE && st->core_brate >= SID_2k40 && st->core == ACELP_CORE && !st->con_tcx && output_Fs >= 32000 && st->bwidth > NB && st->bws_cnt > 0 ) )
526 : {
527 : /* SWB TBE decoder */
528 341412 : swb_tbe_dec( st, hStereoICBWE, bwe_exc_extended[n], voice_factors[n], old_syn_12k8_16k[n], tmp_buffer /*fb_exc*/, hb_synth[n], pitch_buf[n] );
529 :
530 : /* FB TBE decoder */
531 341412 : if ( st->extl == FB_TBE )
532 : {
533 129792 : fb_tbe_dec( st, tmp_buffer /*fb_exc*/, hb_synth[n], tmp_buffer /*fb_synth_ref*/, output_frame );
534 : }
535 : }
536 : #ifdef FIX_1320_STACK_CPE_DECODER
537 1426932 : else if ( st->extl == SWB_BWE || st->extl == FB_BWE || flag_bwe_bws )
538 : #else
539 : else if ( st->extl == SWB_BWE || st->extl == FB_BWE || ( output_Fs >= 32000 && st->core == ACELP_CORE && st->bwidth > NB && st->bws_cnt > 0 && !st->ppp_mode_dec && !( st->nelp_mode_dec == 1 && st->bfi == 1 ) ) )
540 : #endif
541 : {
542 : /* SWB BWE decoder */
543 45306 : swb_bwe_dec( st, output[n], synth[n], hb_synth[n], use_cldfb_for_dft, output_frame );
544 : }
545 :
546 : #ifdef FIX_1320_STACK_CPE_DECODER
547 1768344 : if ( ( st->core == ACELP_CORE && ( st->extl == -1 || st->extl == SWB_CNG ) ) && flag_bwe_bws == 0 )
548 : {
549 113502 : set_f( hb_synth[n], 0.0f, L_FRAME48k );
550 : }
551 :
552 : #endif
553 : /*---------------------------------------------------------------------*
554 : * FEC - recovery after lost HQ core (smoothing of the BWE component)
555 : *---------------------------------------------------------------------*/
556 :
557 1768344 : if ( st->prev_bfi && st->last_core == HQ_CORE && st->extl != -1 )
558 : {
559 6 : tmp = FRAC_BWE_SMOOTH / output_frame;
560 :
561 2886 : for ( i = 0; i < output_frame / FRAC_BWE_SMOOTH; i++ )
562 : {
563 2880 : hb_synth[n][i] *= ( i * tmp );
564 : }
565 : }
566 :
567 : /*---------------------------------------------------------------------*
568 : * SWB CNG
569 : *---------------------------------------------------------------------*/
570 :
571 1768344 : if ( ( output_frame >= L_FRAME32k && st->hTdCngDec != NULL ) || ( st->element_mode == IVAS_CPE_DFT && st->bwidth >= SWB && st->hTdCngDec != NULL ) )
572 : {
573 : /* SHB CNG decoder */
574 682767 : swb_CNG_dec( st, synth[n], hb_synth[n], sid_bw[n] );
575 : }
576 :
577 : /*-------------------------------------------------------------------*
578 : * Inter-channel BWE decoding
579 : *-------------------------------------------------------------------*/
580 :
581 1768344 : if ( n == 0 && st->element_mode >= IVAS_CPE_DFT )
582 : {
583 450150 : stereo_icBWE_dec( hCPE, hb_synth[0], hb_synth[1], tmp_buffer /*fb_synth_ref*/, voice_factors[0], output_frame );
584 : }
585 :
586 : #ifndef FIX_1320_STACK_CPE_DECODER
587 : if ( st->element_mode == EVS_MONO )
588 : {
589 : /*----------------------------------------------------------------*
590 : * BFI waveform adjustment
591 : *----------------------------------------------------------------*/
592 :
593 : if ( st->core == ACELP_CORE && !st->bfi && st->prev_bfi && st->last_total_brate >= HQ_48k && st->last_codec_mode == MODE2 && ( st->last_core_bfi == TCX_20_CORE || st->last_core_bfi == TCX_10_CORE ) && st->hPlcInfo->concealment_method == TCX_NONTONAL && st->hPlcInfo->nbLostCmpt < 4 )
594 : {
595 : tmps = NS2SA( output_Fs, DELAY_CLDFB_NS );
596 :
597 : waveform_adj2( st->hPlcInfo, st->hTonalMDCTConc->secondLastPcmOut, synth[n] + tmps, tmps, st->hPlcInfo->nbLostCmpt + 1, st->bfi );
598 :
599 : st->hPlcInfo->Pitch = 0;
600 : }
601 : }
602 : #endif
603 : /*----------------------------------------------------------------*
604 : * Transition and synchronization of BWE components
605 : *----------------------------------------------------------------*/
606 :
607 1768344 : if ( ( st->extl != -1 && ( st->extl != IGF_BWE || st->last_core == ACELP_CORE ) ) || ( st->bws_cnt > 0 && st->core == ACELP_CORE ) )
608 : {
609 : /* Calculate an additional delay of extension layer components to be synchronized with ACELP synthesis */
610 514521 : if ( st->L_frame == L_FRAME )
611 : {
612 : /* TBE on top of ACELP@12.8kHz */
613 283311 : tmps = NS2SA( output_Fs, MAX_DELAY_TBE_NS - DELAY_SWB_TBE_12k8_NS );
614 : }
615 : else
616 : {
617 : #ifndef FIX_1320_STACK_CPE_DECODER
618 : if ( st->extl == SWB_BWE_HIGHRATE || st->extl == FB_BWE_HIGHRATE )
619 : {
620 : /* HR SWB BWE on top of ACELP@16kHz */
621 : tmps = NS2SA( output_Fs, DELAY_BWE_TOTAL_NS );
622 : }
623 : else
624 : {
625 : #endif
626 : /* TBE on top of ACELP@16kHz */
627 231210 : tmps = NS2SA( output_Fs, MAX_DELAY_TBE_NS - DELAY_SWB_TBE_16k_NS );
628 : #ifndef FIX_1320_STACK_CPE_DECODER
629 : }
630 : #endif
631 : }
632 :
633 : /* Smooth transitions when switching between different technologies */
634 514521 : if ( !( ( st->core == TCX_20_CORE || st->core == TCX_10_CORE || st->core == HQ_CORE ) && st->last_core == ACELP_CORE ) &&
635 491214 : ( st->extl != st->last_extl || ( st->extl == st->last_extl && ( st->core ^ st->last_core ) == HQ_CORE ) ) && !( st->extl == SWB_CNG && st->last_extl == SWB_TBE ) && ( st->element_mode != IVAS_CPE_TD || ( hCPE->element_mode == IVAS_CPE_TD && tdm_LRTD_flag ) ) )
636 : {
637 : /* switching between BWE and TBE technologies */
638 34509 : incr = (int16_t) ( L_FRAME / ( tmps + 0.5f ) );
639 196641 : for ( i = 0; i < tmps; i++ )
640 : {
641 162132 : hb_synth[n][i] *= sin_table256[i * incr];
642 : }
643 :
644 34509 : set_f( st->hb_prev_synth_buffer, 0.0f, tmps );
645 : }
646 480012 : else if ( tmps < st->old_bwe_delay )
647 : {
648 : /* the previous frame was TBE on top of ACELP@16kHz and the current frame is TBE on top of ACELP@12.8kHz */
649 462 : incr = (int16_t) ( L_FRAME / ( tmps + 0.5f ) );
650 1776 : for ( i = 0; i < tmps; i++ )
651 : {
652 1314 : tmp_buffer[i] = st->hb_prev_synth_buffer[i] * sin_table256[255 - i * incr] +
653 1314 : st->hb_prev_synth_buffer[st->old_bwe_delay - 1 - i] * sin_table256[i * incr];
654 : }
655 :
656 462 : mvr2r( tmp_buffer, st->hb_prev_synth_buffer, tmps );
657 : }
658 479550 : else if ( tmps > st->old_bwe_delay )
659 : {
660 : /* the previous frame was TBE on top of ACELP@12.8kHz and the current frame is TBE on top of ACELP@16kHz */
661 8829 : incr = (int16_t) ( L_FRAME / ( st->old_bwe_delay + 0.5f ) );
662 13053 : for ( i = 0; i < st->old_bwe_delay; i++ )
663 : {
664 4224 : tmp_buffer[i] = st->hb_prev_synth_buffer[i] * sin_table256[255 - i * incr];
665 : }
666 :
667 57042 : for ( ; i < tmps; i++ )
668 : {
669 48213 : tmp_buffer[i] = 0.0f;
670 : }
671 :
672 13053 : for ( i = 0; i < st->old_bwe_delay; i++ )
673 : {
674 4224 : tmp_buffer[tmps - 1 - i] += st->hb_prev_synth_buffer[st->old_bwe_delay - 1 - i] * sin_table256[i * incr];
675 : }
676 :
677 8829 : mvr2r( tmp_buffer, st->hb_prev_synth_buffer, tmps );
678 : }
679 :
680 514521 : if ( ( st->element_mode != IVAS_CPE_TD && !use_cldfb_for_dft ) || ( st->element_mode == IVAS_CPE_TD && tdm_LRTD_flag ) )
681 : {
682 : /* Delay hb_synth */
683 477123 : delay_signal( hb_synth[n], output_frame, st->hb_prev_synth_buffer, tmps );
684 : }
685 : else
686 : {
687 37398 : mvr2r( hb_synth[n] + output_frame - tmps, st->hb_prev_synth_buffer, tmps );
688 : }
689 :
690 514521 : st->old_bwe_delay = tmps;
691 514521 : if ( st->hBWE_TD != NULL )
692 : {
693 496692 : mvr2r( hb_synth[n], st->hBWE_TD->old_hb_synth, output_frame );
694 : }
695 :
696 : /* SWB CNG/DTX - calculate SHB energy */
697 514521 : if ( output_frame >= L_FRAME32k && st->extl > SWB_CNG && st->core == ACELP_CORE && st->hTdCngDec != NULL )
698 : {
699 243639 : st->hTdCngDec->last_shb_ener = sum2_f( hb_synth[n], output_frame ) + 0.001f;
700 243639 : st->hTdCngDec->last_shb_ener /= (float) output_frame;
701 243639 : st->hTdCngDec->last_shb_ener = 10 * log10f( st->hTdCngDec->last_shb_ener );
702 : }
703 : }
704 :
705 1768344 : if ( sba_dirac_stereo_flag && st->element_mode != IVAS_CPE_MDCT )
706 : {
707 : /* for SBA DirAC stereo output DFT Stereo core switching and updates are done in ivas_sba_dirac_stereo_dec() as hCPE is not available at this point */
708 13536 : break;
709 : }
710 :
711 : /*----------------------------------------------------------------*
712 : * Post-processing
713 : * - TCX-LTP Postfilter (except DFT stereo)
714 : * - core switching in DFT stereo
715 : * - updates for potential TD->DFT stereo switching
716 : *----------------------------------------------------------------*/
717 :
718 1754808 : if ( st->element_mode != IVAS_CPE_DFT )
719 : {
720 1575750 : if ( st->element_mode != IVAS_CPE_MDCT || sba_dirac_stereo_flag )
721 : {
722 1064268 : ivas_post_proc( hSCE, hCPE, n, synth[n], NULL, output_frame, sba_dirac_stereo_flag );
723 : }
724 :
725 : /* update OLA buffers - needed for switching to DFT stereo */
726 1575750 : if ( !sba_dirac_stereo_flag )
727 : {
728 1567728 : stereo_td2dft_update( hCPE, n, output[n], synth[n], hb_synth[n], output_frame );
729 : }
730 : }
731 : else /* IVAS_CPE_DFT */
732 : {
733 179058 : if ( last_element_mode == IVAS_CPE_MDCT )
734 : {
735 2148 : stereo_mdct2dft_update( hCPE, output[0], synth[0] );
736 : }
737 :
738 179058 : stereo_dft_dec_core_switching( hCPE, output[0], synth[0], hb_synth[0], DFT, output_frame, use_cldfb_for_dft, 0 );
739 :
740 179058 : if ( hCPE->nchan_out == 1 && hCPE->hStereoDft->hConfig->res_cod_mode == STEREO_DFT_RES_COD_OFF )
741 : {
742 : /* mono output for non-residual coding modes uses CLDFB instead of DFT - requires DFT buffer update in case of bitrate switching */
743 51339 : stereo_td2dft_update( hCPE, n, output[n], synth[n], hb_synth[n], output_frame );
744 : }
745 : }
746 :
747 1754808 : mvr2r( synth[n], output[n], output_frame );
748 :
749 : #ifdef DEBUG_PLOT
750 : sendDebout( "mdct_dec", output_frame, 1, "aftLPD", MTV_FLOAT, output[n] );
751 : #endif
752 : /*--------------------------------------------------------*
753 : * Common updates
754 : *--------------------------------------------------------*/
755 :
756 : /* Save synthesis for HQ FEC */
757 1754808 : save_synthesis_hq_fec( st, output[n], output_frame, hCPE );
758 :
759 : /* Updates */
760 1754808 : updt_dec_common( st, NORMAL_HQ_CORE, -1, output[n] );
761 :
762 : } /* n_channels loop */
763 :
764 : #ifdef DEBUG_MODE_INFO
765 : output_debug_mode_info_dec( sts, n_channels, output_frame, pitch_buf );
766 : #endif
767 1497252 : pop_wmops();
768 1497252 : return error;
769 : }
770 :
771 :
772 : #ifdef DEBUG_MODE_INFO
773 : /*-------------------------------------------------------------------*
774 : * output_debug_mode_info_dec()
775 : *
776 : * Debugging function to output most important codec parameters
777 : *-------------------------------------------------------------------*/
778 :
779 : void output_debug_mode_info_dec(
780 : Decoder_State **sts,
781 : const int16_t n_channels,
782 : const int16_t output_frame,
783 : float pitch_buf[CPE_CHANNELS][NB_SUBFR16k] )
784 : {
785 : int16_t n;
786 : Decoder_State *st;
787 :
788 : for ( n = 0; n < n_channels; n++ )
789 : {
790 : float tmpF;
791 : int16_t tmpS, id;
792 :
793 : if ( pitch_buf == NULL )
794 : {
795 : /* very first frame is lost */
796 : id = 0;
797 : }
798 : else
799 : {
800 : id = sts[n]->id_element;
801 : }
802 :
803 : st = sts[n];
804 :
805 : dbgwrite( &st->core, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "core", n, id, DEC ) );
806 : dbgwrite( &st->extl, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "extl", n, id, DEC ) );
807 : dbgwrite( &st->bwidth, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "bwidth", n, id, DEC ) );
808 : dbgwrite( &st->cng_type, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "cng_type", n, id, DEC ) );
809 : dbgwrite( &st->clas_dec, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "clas", n, id, DEC ) );
810 : tmpF = st->extl_brate / 1000.0f;
811 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "extl_brate", n, id, DEC ) );
812 : tmpF = st->core_brate / 1000.0f;
813 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "core_brate", n, id, DEC ) );
814 : tmpF = st->total_brate / 1000.0f;
815 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "total_brate", n, id, DEC ) );
816 : tmpS = st->bits_frame_nominal;
817 : dbgwrite( &tmpS, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "bits_nominal", n, id, DEC ) );
818 :
819 : #ifdef DEBUG_MODE_INFO_PLC
820 : dbgwrite( &st->last_core_bfi, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "last_core_bfi", n, id, DEC ) );
821 : dbgwrite( &st->second_last_core, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "second_last_core", n, id, DEC ) );
822 : dbgwrite( &st->con_tcx, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "con_tcx", n, id, DEC ) );
823 : dbgwrite( &st->last_con_tcx, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "last_con_tcx", n, id, DEC ) );
824 : dbgwrite( &st->prev_bfi, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "prev_bfi", n, id, DEC ) );
825 : dbgwrite( &st->prev_old_bfi, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "prev_old_bfi", n, id, DEC ) );
826 : #endif
827 :
828 : if ( pitch_buf != NULL )
829 : {
830 : dbgwrite( &st->coder_type, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "coder_type", n, id, DEC ) );
831 : }
832 : else
833 : {
834 : tmpS = -1;
835 : dbgwrite( &tmpS, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "coder_type", n, id, DEC ) );
836 : }
837 :
838 : if ( st->hTcxCfg != NULL )
839 : tmpS = st->hTcxCfg->coder_type;
840 : else
841 : tmpS = -1;
842 : #ifdef DEBUG_MODE_TCX
843 : dbgwrite( &tmpS, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "tcx_core_type", n, id, DEC ) );
844 : #endif
845 : dbgwrite( &st->L_frame, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "L_frame", n, id, DEC ) );
846 : dbgwrite( &st->VAD, 2, 1, output_frame, fname( debug_dir, "VAD", n, id, DEC ) );
847 : dbgwrite( &st->flag_cna, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "cna_flag", n, id, DEC ) );
848 :
849 : #if ( defined DEBUG_MODE_ACELP ) || ( defined DEBUG_MODE_TCX )
850 : if ( pitch_buf != NULL && st->core != HQ_CORE )
851 : {
852 : int16_t i;
853 : for ( i = 0; i < ( st->L_frame / L_SUBFR ); i++ )
854 : {
855 : dbgwrite( &pitch_buf[n][i], sizeof( float ), 1, output_frame / ( st->L_frame / L_SUBFR ), fname( debug_dir, "pitch_buf", n, id, DEC ) );
856 : }
857 : }
858 : else
859 : {
860 : tmpF = 0;
861 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "pitch_buf", n, id, DEC ) );
862 : }
863 : #endif
864 :
865 : #ifdef DEBUG_MODE_ACELP
866 : if ( st->core != ACELP_CORE )
867 : {
868 : tmpF = 0.0f;
869 : dbgwrite( &tmpF, sizeof( float ), 1, st->L_frame, fname( debug_dir, "exc", n, id, DEC ) );
870 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "output.Fs", n, id, DEC ) );
871 : dbgwrite( &tmpF, sizeof( float ), 1, min( st->L_frame, L_FRAME16k ), fname( debug_dir, "syn.intFs", n, id, DEC ) );
872 : }
873 :
874 : if ( sts[0]->element_mode == IVAS_CPE_DFT )
875 : {
876 : tmpF = 0.0f;
877 : dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "output.Fs", 1, id, DEC ) );
878 : dbgwrite( &tmpF, sizeof( float ), 1, L_FRAME, fname( debug_dir, "syn.intFs", 1, id, DEC ) );
879 : }
880 : #endif
881 : }
882 :
883 : return;
884 : }
885 : #endif
|