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