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 : #include <assert.h>
36 : #include <math.h>
37 : #include "cnst.h"
38 : #include "rom_com.h"
39 : #include "prot_fx.h"
40 : #include "ivas_prot_fx.h"
41 : #include "ivas_rom_com.h"
42 : #include "wmc_auto.h"
43 : #include "ivas_prot_fx.h"
44 :
45 :
46 : /*-------------------------------------------------------------------*
47 : * ivas_core_dec()
48 : *
49 : * Principal IVAS core decoder routine, where number of core channels is 1 or 2
50 : *-------------------------------------------------------------------*/
51 :
52 702097 : ivas_error ivas_core_dec_fx(
53 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
54 : SCE_DEC_HANDLE hSCE, /* i/o: SCE decoder structure */
55 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
56 : MCT_DEC_HANDLE hMCT, /* i/o: MCT decoder structure */
57 : const Word16 n_channels, /* i : number of channels to be decoded */
58 : Word32 *output_32_fx[], /* o : output synthesis signal Q11*/
59 : Word32 hb_synth_32_fx[][L_FRAME48k], /* o : output HB synthesis signal Q11*/
60 : Word32 DFT_fx[CPE_CHANNELS][STEREO_DFT_BUF_MAX], /* o : DFT buffers Qx*/
61 : const Word16 sba_dirac_stereo_flag /* i : signal stereo output for SBA DirAC */
62 : )
63 : {
64 : Word16 i, n, output_frame;
65 : Decoder_State **sts, *st;
66 : STEREO_ICBWE_DEC_HANDLE hStereoICBWE;
67 : STEREO_TD_DEC_DATA_HANDLE hStereoTD;
68 : STEREO_CNG_DEC_HANDLE hStereoCng;
69 : TD_BWE_DEC_HANDLE hBWE_TD;
70 : FD_BWE_DEC_HANDLE hBWE_FD;
71 : Word16 sharpFlag[CPE_CHANNELS];
72 : Word16 tmp_buffer_fx[L_FRAME48k];
73 : Word16 tmp16, tmp16_2, j;
74 : Word16 Q_white_exc;
75 :
76 702097 : set16_fx( tmp_buffer_fx, 0, L_FRAME48k );
77 :
78 : Word16 tmps, incr;
79 : Word32 bwe_exc_extended_fx[CPE_CHANNELS][L_FRAME32k + NL_BUFF_OFFSET];
80 :
81 : Word16 voice_factors_fx[CPE_CHANNELS][NB_SUBFR16k]; // Q15
82 : Word16 core_switching_flag[CPE_CHANNELS];
83 :
84 : Word16 pitch_buf_fx[CPE_CHANNELS][NB_SUBFR16k]; // Q6
85 : Word32 old_syn_12k8_16k_fx[CPE_CHANNELS][L_FRAME16k];
86 :
87 : Word16 unbits[CPE_CHANNELS];
88 : Word16 sid_bw[CPE_CHANNELS];
89 : FRAME_MODE frameMode[CPE_CHANNELS];
90 : Word16 tdm_LRTD_flag;
91 : Word32 element_brate, output_Fs;
92 : Word32 last_element_brate;
93 : Word16 use_cldfb_for_dft;
94 : Word32 *p_output_mem_fx;
95 : Word16 flag_sec_CNA;
96 : Word16 read_sid_info;
97 : Word16 last_element_mode;
98 : Word16 nchan_out;
99 : Word32 *save_hb_synth_32_fx;
100 : ivas_error error;
101 : Word32 L_tmp;
102 : Word16 Q_synth;
103 : Word16 output_16_fx[CPE_CHANNELS][L_FRAME48k];
104 : Word16 hb_synth_16_fx[CPE_CHANNELS][L_FRAME48k];
105 :
106 : Word16 synth_16_fx[CPE_CHANNELS][L_FRAME48k];
107 : Word32 synth_32_fx[CPE_CHANNELS][L_FRAME48k];
108 : Word16 e_sig[CPE_CHANNELS];
109 702097 : e_sig[0] = e_sig[1] = 15;
110 702097 : move16();
111 702097 : move16();
112 :
113 : Word16 tdm_lsfQ_PCh_fx[M], tdm_lspQ_PCh_fx[M];
114 : Word32 conceal_eof_gain32;
115 : Flag Overflow;
116 :
117 702097 : error = IVAS_ERR_OK;
118 702097 : move32();
119 702097 : push_wmops( "ivas_core_dec" );
120 :
121 : /*------------------------------------------------------------------*
122 : * General initialization
123 : *-----------------------------------------------------------------*/
124 :
125 702097 : use_cldfb_for_dft = 0;
126 702097 : move16();
127 702097 : tdm_LRTD_flag = -1;
128 702097 : move16();
129 702097 : read_sid_info = 1; /* read SID by default */
130 702097 : move16();
131 :
132 702097 : IF( hSCE != NULL )
133 : {
134 315557 : sts = hSCE->hCoreCoder;
135 315557 : hStereoICBWE = NULL;
136 315557 : element_brate = hSCE->element_brate;
137 315557 : move32();
138 315557 : last_element_brate = hSCE->last_element_brate; /* note: this parameter is unused */
139 315557 : move32();
140 315557 : last_element_mode = IVAS_SCE;
141 315557 : move16();
142 315557 : hStereoTD = NULL;
143 315557 : hStereoCng = NULL;
144 315557 : p_output_mem_fx = NULL;
145 315557 : nchan_out = 1;
146 315557 : move16();
147 :
148 315557 : test();
149 315557 : IF( st_ivas != NULL && EQ_32( st_ivas->ivas_format, ISM_FORMAT ) )
150 : {
151 239500 : if ( NE_16( st_ivas->hISMDTX.sce_id_dtx, hSCE->sce_id ) )
152 : {
153 145816 : read_sid_info = 0;
154 145816 : move16();
155 : }
156 : }
157 : }
158 : ELSE
159 : {
160 386540 : sts = hCPE->hCoreCoder;
161 386540 : element_brate = hCPE->element_brate;
162 386540 : move32();
163 386540 : last_element_brate = hCPE->last_element_brate;
164 386540 : move32();
165 386540 : last_element_mode = hCPE->last_element_mode;
166 386540 : move16();
167 386540 : hStereoICBWE = hCPE->hStereoICBWE;
168 386540 : hStereoTD = hCPE->hStereoTD;
169 386540 : hStereoCng = hCPE->hStereoCng;
170 386540 : p_output_mem_fx = hCPE->output_mem_fx[1];
171 386540 : nchan_out = hCPE->nchan_out;
172 386540 : move16();
173 :
174 386540 : if ( hCPE->hStereoTD != NULL )
175 : {
176 3751 : tdm_LRTD_flag = hCPE->hCoreCoder[0]->tdm_LRTD_flag;
177 3751 : move16();
178 : }
179 :
180 386540 : test();
181 386540 : test();
182 386540 : IF( EQ_16( sts[0]->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->nchan_out, 1 ) && EQ_16( hCPE->hStereoDft->hConfig->res_cod_mode, STEREO_DFT_RES_COD_OFF ) )
183 : {
184 17001 : use_cldfb_for_dft = 1;
185 17001 : move16();
186 : }
187 : }
188 :
189 1731687 : FOR( n = 0; n < n_channels; n++ )
190 : {
191 1029590 : st = sts[n];
192 1029590 : IF( st->cldfbAna )
193 : {
194 414500 : scale_sig32( st->cldfbAna->cldfb_state_fx, st->cldfbAna->cldfb_size, Q10 - Q11 ); // Q10
195 414500 : st->cldfbAna->Q_cldfb_state = Q10;
196 414500 : move16();
197 : }
198 1029590 : IF( st->cldfbSynHB )
199 : {
200 2970 : scale_sig32( st->cldfbSynHB->cldfb_state_fx, st->cldfbSynHB->cldfb_size, Q10 - Q11 ); // Q10
201 2970 : st->cldfbSynHB->Q_cldfb_state = Q10;
202 2970 : move16();
203 : }
204 : }
205 :
206 702097 : output_Fs = sts[0]->output_Fs;
207 702097 : move32();
208 702097 : output_frame = extract_l( Mpy_32_16_1( output_Fs, INV_FRAME_PER_SEC_Q15 ) );
209 :
210 1731687 : FOR( n = 0; n < n_channels; n++ )
211 : {
212 1029590 : st = sts[n];
213 :
214 : /*------------------------------------------------------------------*
215 : * Initialization per core-decoder channel
216 : *-----------------------------------------------------------------*/
217 :
218 1029590 : frameMode[n] = FRAMEMODE_NORMAL;
219 1029590 : move32();
220 1029590 : st->rate_switching_reset = 0;
221 1029590 : move16();
222 1029590 : st->mdct_sw = MODE1;
223 1029590 : move16();
224 1029590 : sid_bw[n] = -1;
225 1029590 : move16();
226 1029590 : core_switching_flag[n] = 0;
227 1029590 : move16();
228 1029590 : sharpFlag[n] = 0;
229 1029590 : move16();
230 1029590 : unbits[n] = 0;
231 1029590 : move16();
232 1029590 : st->GSC_IVAS_mode = 0;
233 1029590 : move16();
234 1029590 : st->element_brate = element_brate;
235 1029590 : move32();
236 1029590 : st->use_partial_copy = 0;
237 1029590 : move16();
238 1029590 : st->rf_flag = 0;
239 1029590 : move16();
240 1029590 : st->rf_frame_type = RF_NO_DATA;
241 1029590 : move16();
242 :
243 1029590 : IF( EQ_16( st->bfi, 1 ) )
244 : {
245 17028 : frameMode[n] = FRAMEMODE_MISSING;
246 17028 : move32();
247 17028 : st->coder_type = st->last_coder_type;
248 17028 : move16();
249 : }
250 : ELSE
251 : {
252 1012562 : test();
253 1012562 : if ( !( EQ_16( st->element_mode, IVAS_CPE_TD ) && EQ_16( n, 1 ) ) ) /* coder_type for SCh in TD stereo is already read in tdm_configure_dec() */
254 : {
255 1008867 : st->coder_type = INACTIVE;
256 1008867 : move16();
257 : }
258 1012562 : st->extl = -1;
259 1012562 : move16();
260 1012562 : st->flagGuidedAcelp = 0;
261 1012562 : move16();
262 : }
263 :
264 1029590 : test();
265 1029590 : test();
266 1029590 : test();
267 1029590 : test();
268 1029590 : test();
269 1029590 : IF( !st->bfi && st->prev_bfi && GT_32( st->total_brate, SID_2k40 ) && ( EQ_16( st->last_core_bfi, TCX_20_CORE ) || EQ_16( st->last_core_bfi, TCX_10_CORE ) ) && st->hTcxDec != NULL )
270 : {
271 5016 : conceal_eof_gain32 = L_shr_sat( st->hTcxDec->conceal_eof_gain32, sub( 16, st->hTcxDec->conceal_eof_gain_e ) ); // e = 31 - Q , 16 - e => 16 - (31 - Q) => Q - 15,
272 : // shr(16 -e ) = shr(Q -15) => 15 - Q ==> Q15
273 3647896 : FOR( i = 0; i < st->hTcxDec->L_frameTCX; i++ )
274 : {
275 3642880 : L_tmp = Mpy_32_16_1( conceal_eof_gain32, st->hHQ_core->old_out_fx[i] ); // Q0 (15+1+0 - (15 + 1)
276 3642880 : L_tmp = Mpy_32_16_1( L_tmp, st->last_concealed_gain_syn_deemph ); // Q(0+15 - last_concealed_gain_syn_deemph_e -15)
277 3642880 : L_tmp = L_shl( L_tmp, st->last_concealed_gain_syn_deemph_e ); // Q0 (-last_concealed_gain_syn_deemph_e +last_concealed_gain_syn_deemph_e)
278 3642880 : st->hHQ_core->old_out_fx[i] = extract_l( L_tmp ); // Q0
279 3642880 : move16();
280 : }
281 :
282 2442200 : FOR( i = 0; i < st->L_frame; i++ )
283 : {
284 2437184 : L_tmp = Mpy_32_16_1( conceal_eof_gain32, st->hHQ_core->old_out_LB_fx[i] ); // Q0 (15+1+0 - (15 + 1)
285 2437184 : L_tmp = Mpy_32_16_1( L_tmp, st->last_concealed_gain_syn_deemph ); // Q(0+15 - last_concealed_gain_syn_deemph_e -15)
286 2437184 : L_tmp = L_shl( L_tmp, st->last_concealed_gain_syn_deemph_e ); // Q0 (-last_concealed_gain_syn_deemph_e +last_concealed_gain_syn_deemph_e)
287 2437184 : st->hHQ_core->old_out_LB_fx[i] = extract_l( L_tmp ); // Q0
288 2437184 : move16();
289 : }
290 : }
291 :
292 1029590 : set16_fx( voice_factors_fx[n], 0, NB_SUBFR16k );
293 1029590 : set32_fx( hb_synth_32_fx[n], 0, L_FRAME48k );
294 1029590 : set16_fx( hb_synth_16_fx[n], 0, L_FRAME48k );
295 :
296 : /*------------------------------------------------------------------*
297 : * Decision matrix (selection of technologies)
298 : *-----------------------------------------------------------------*/
299 :
300 1029590 : test();
301 1029590 : test();
302 1029590 : IF( NE_16( st->bfi, 1 ) )
303 : {
304 1012562 : ivas_decision_matrix_dec_fx( st, &sharpFlag[n], &core_switching_flag[n], element_brate, nchan_out );
305 :
306 1012562 : synchonize_channels_mdct_sid_fx( sts, n );
307 :
308 1012562 : IF( NE_16( st->bfi, 1 ) ) /* note: st->bfi can be changed from 0 to 1 in ivas_decision_matrix_dec() when BER is detected */
309 : {
310 1012562 : st->sr_core = L_mult0( st->L_frame, FRAMES_PER_SEC );
311 1012562 : move32();
312 1012562 : st->fscale_old = st->fscale;
313 1012562 : move16();
314 1012562 : st->fscale = sr2fscale_fx( st->sr_core );
315 1012562 : move16();
316 : }
317 : ELSE
318 : {
319 0 : frameMode[n] = FRAMEMODE_MISSING;
320 0 : move32();
321 : }
322 : }
323 17028 : ELSE IF( GE_16( st->element_mode, IVAS_SCE ) && EQ_16( st->prev_bfi, 1 ) && EQ_16( st->last_con_tcx, 1 ) )
324 : {
325 2275 : st->core = TCX_20_CORE;
326 2275 : move16();
327 : }
328 : } /* n_channels loop */
329 :
330 : /* MDCT stereo -> DFT stereo switching */
331 702097 : test();
332 702097 : IF( EQ_16( last_element_mode, IVAS_CPE_MDCT ) && EQ_16( sts[0]->element_mode, IVAS_CPE_DFT ) )
333 : {
334 : Word16 ovl, fade_len;
335 660 : IF( NE_16( sts[0]->L_frame, sts[0]->last_L_frame ) )
336 : {
337 659 : Copy_Scale_sig_16_32_no_sat( sts[0]->hHQ_core->old_out_LB_fx, sts[0]->hHQ_core->old_out_LB_fx32, L_FRAME32k, sub( Q11, sts[0]->hHQ_core->Q_old_wtda_LB ) ); // Q11
338 659 : L_lerp_fx_q11( sts[0]->hHQ_core->old_out_LB_fx32, sts[0]->hHQ_core->old_out_LB_fx32, sts[0]->L_frame, sts[0]->last_L_frame );
339 659 : Copy_Scale_sig_32_16( sts[0]->hHQ_core->old_out_LB_fx32, sts[0]->hHQ_core->old_out_LB_fx, L_FRAME32k, sub( sts[0]->hHQ_core->Q_old_wtda_LB, Q11 ) ); // Q_old_wtda_LB
340 : }
341 660 : IF( NE_16( sts[0]->L_frame, L_FRAME16k ) )
342 : {
343 400 : ovl = mult( sts[0]->L_frame, 5120 ); /*STEREO_DFT32MS_OVL_16k/L_FRAME16k = 5/32 = 5120 (Q15)*/
344 400 : L_lerp_fx_q11( hCPE->input_mem_LB_fx[0], hCPE->input_mem_LB_fx[0], ovl, STEREO_DFT32MS_OVL_16k );
345 :
346 400 : fade_len = mult( sts[0]->L_frame, 12288 ); /* STEREO_MDCT2DFT_FADE_LEN_48k/L_FRAME16k = 3/8 = 12288 (Q15)*/
347 400 : L_lerp_fx_q11( hCPE->old_outLB_mdct_fx, hCPE->old_outLB_mdct_fx, fade_len, STEREO_MDCT2DFT_FADE_LEN_48k );
348 : }
349 : }
350 :
351 702097 : test();
352 702097 : if ( hStereoCng != NULL )
353 : {
354 62798 : hStereoCng->flag_cna_fade = 0;
355 62798 : move16();
356 : }
357 :
358 702097 : test();
359 702097 : test();
360 702097 : test();
361 702097 : IF( sba_dirac_stereo_flag && hSCE && LE_32( sts[0]->total_brate, SID_2k40 ) && EQ_16( sts[0]->cng_type, FD_CNG ) )
362 : {
363 91 : save_hb_synth_32_fx = hSCE->save_hb_synth_fx;
364 : }
365 : ELSE
366 : {
367 702006 : save_hb_synth_32_fx = NULL;
368 : }
369 :
370 : /*------------------------------------------------------------------*
371 : * Decode SID for MDCT-Stereo DTX mode
372 : *-----------------------------------------------------------------*/
373 :
374 702097 : test();
375 702097 : IF( EQ_16( sts[0]->element_mode, IVAS_CPE_MDCT ) && EQ_32( sts[0]->total_brate, SID_2k40 ) )
376 : {
377 577 : IF( sts[0]->cng_sba_flag )
378 : {
379 167 : FdCngDecodeDiracMDCTStereoSID_fx( hCPE );
380 : }
381 : ELSE
382 : {
383 410 : FdCngDecodeMDCTStereoSID_fx( hCPE );
384 : }
385 : }
386 :
387 : /*------------------------------------------------------------------*
388 : * Sanity check in combined format coding
389 : *-----------------------------------------------------------------*/
390 :
391 702097 : test();
392 702097 : test();
393 702097 : IF( hCPE != NULL && EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->brate_surplus > 0 )
394 : {
395 95 : ivas_combined_format_brate_sanity_fx( element_brate, sts[0]->core, sts[0]->total_brate, &( sts[0]->core_brate ), &( sts[0]->inactive_coder_type_flag ), &tmps );
396 : }
397 :
398 : /*------------------------------------------------------------------*
399 : * Core Decoding
400 : *-----------------------------------------------------------------*/
401 :
402 1731687 : FOR( n = 0; n < n_channels; n++ )
403 : {
404 1029590 : st = sts[n];
405 :
406 : /*------------------------------------------------------------------*
407 : * Initialization
408 : *-----------------------------------------------------------------*/
409 :
410 1029590 : IF( EQ_16( st->bfi, 1 ) )
411 : {
412 17028 : st->nbLostCmpt = add( st->nbLostCmpt, 1 );
413 17028 : move16();
414 : }
415 : ELSE
416 : {
417 1012562 : IF( EQ_16( st->prev_bfi, 1 ) )
418 : {
419 8975 : st->prev_nbLostCmpt = st->nbLostCmpt;
420 8975 : move16();
421 : }
422 : ELSE
423 : {
424 1003587 : st->prev_nbLostCmpt = 0;
425 1003587 : move16();
426 : }
427 :
428 1012562 : st->nbLostCmpt = 0;
429 1012562 : move16();
430 : }
431 1029590 : st->enablePlcWaveadjust = 0;
432 1029590 : move16();
433 :
434 1029590 : IF( EQ_16( n, 1 ) )
435 : {
436 327493 : sts[1]->BER_detect = s_or( sts[1]->BER_detect, sts[0]->BER_detect );
437 327493 : move16();
438 : }
439 :
440 : /*---------------------------------------------------------------------*
441 : * Detect bandwidth switching
442 : *---------------------------------------------------------------------*/
443 :
444 1029590 : bandwidth_switching_detect_ivas_fx( st );
445 :
446 : /*---------------------------------------------------------------------*
447 : * Preprocessing (preparing) for ACELP/HQ core switching
448 : *---------------------------------------------------------------------*/
449 :
450 : Word16 Q_olapBufferSynth, Q_olapBufferSynth2;
451 :
452 1029590 : Q_olapBufferSynth = Q15; /*Initializing with max values to avoid warnings*/
453 1029590 : Q_olapBufferSynth2 = Q15; /*Initializing with max values to avoid warnings*/
454 1029590 : move16();
455 1029590 : move16();
456 :
457 1029590 : Copy_Scale_sig_16_32_DEPREC( st->previoussynth_fx, st->previoussynth_fx_32, L_FRAME48k, 0 ); // Q0
458 :
459 1029590 : IF( NE_32( ( error = core_switching_pre_dec_ivas_fx( st, output_frame, sts[0]->last_core_brate, nchan_out, last_element_mode, last_element_brate, st->Q_syn, &Q_olapBufferSynth, &Q_olapBufferSynth2 ) ), IVAS_ERR_OK ) )
460 : {
461 0 : return error;
462 : }
463 :
464 1029590 : flag_sec_CNA = -1;
465 1029590 : move16();
466 1029590 : if ( hCPE != NULL )
467 : {
468 714033 : flag_sec_CNA = sts[1]->flag_cna;
469 714033 : move16();
470 : }
471 :
472 : /*---------------------------------------------------------------------*
473 : * ACELP core decoding
474 : * TCX core decoding
475 : * HQ core decoding
476 : *---------------------------------------------------------------------*/
477 :
478 1029590 : set16_fx( output_16_fx[n], 0, L_FRAME48k ); /* this is needed for instances like L_norm_arr( p_output_fx[i], L_FRAME48k ) */
479 :
480 1029590 : IF( st->core == ACELP_CORE )
481 : {
482 : /* ACELP core decoder */
483 : Word16 old_syn_12k8_16k_fx_16[L_FRAME16k];
484 : Word16 save_hb_synth_fx_arr[L_FRAME48k], *save_hb_synth_16_fx;
485 :
486 167724 : IF( save_hb_synth_32_fx )
487 : {
488 91 : save_hb_synth_16_fx = save_hb_synth_fx_arr;
489 : }
490 : ELSE
491 : {
492 167633 : save_hb_synth_16_fx = NULL;
493 : }
494 :
495 167724 : IF( st->cldfbAna )
496 : {
497 167724 : scale_sig32( st->cldfbAna->cldfb_state_fx, st->cldfbAna->cldfb_size, Q11 - Q10 ); // Q11
498 167724 : st->cldfbAna->Q_cldfb_state = Q11;
499 167724 : move16();
500 : }
501 167724 : IF( st->cldfbBPF )
502 : {
503 167724 : scale_sig32( st->cldfbBPF->cldfb_state_fx, st->cldfbBPF->cldfb_size, Q10 - Q11 ); // Q10
504 167724 : st->cldfbBPF->Q_cldfb_state = Q10;
505 167724 : move16();
506 : }
507 167724 : IF( st->cldfbSyn )
508 : {
509 167724 : scale_sig32( st->cldfbSyn->cldfb_state_fx, st->cldfbSyn->cldfb_size, Q10 - Q11 ); // Q10
510 167724 : st->cldfbSyn->Q_cldfb_state = Q10;
511 167724 : move16();
512 : }
513 :
514 167724 : IF( st->hFdCngDec != NULL )
515 : {
516 163973 : Scale_sig( st->hFdCngDec->hFdCngCom->A_cng, add( M, 1 ), sub( norm_s( sub( st->hFdCngDec->hFdCngCom->A_cng[0], 1 ) ), 3 ) ); // Qx
517 : }
518 :
519 167724 : IF( NE_32( ( error = acelp_core_dec_fx( st, output_16_fx[n], synth_16_fx[n], save_hb_synth_16_fx, bwe_exc_extended_fx[n], voice_factors_fx[n], old_syn_12k8_16k_fx_16, sharpFlag[n], pitch_buf_fx[n], &unbits[n], &sid_bw[n], hStereoTD, tdm_lspQ_PCh_fx, tdm_lsfQ_PCh_fx, use_cldfb_for_dft, last_element_mode, last_element_brate, flag_sec_CNA, nchan_out, hStereoCng, read_sid_info ) ), IVAS_ERR_OK ) )
520 : {
521 0 : return error;
522 : }
523 :
524 167724 : Copy_Scale_sig_16_32_no_sat( output_16_fx[n], output_32_fx[n], L_FRAME48k, sub( Q11, st->Q_syn2 ) ); // Q_syn2->Q11
525 167724 : Scale_sig( output_16_fx[n], L_FRAME48k, negate( st->Q_syn2 ) ); // Q0
526 167724 : IF( st->cldfbAna )
527 : {
528 167724 : scale_sig32( st->cldfbAna->cldfb_state_fx, st->cldfbAna->cldfb_size, Q10 - Q11 ); /* 9 * (Word16)(st->L_frame * FRAMES_PER_SEC * INV_CLDFB_BANDWIDTH + 0.5f) , Q10 */
529 167724 : st->cldfbAna->Q_cldfb_state = Q10;
530 167724 : move16();
531 : }
532 167724 : IF( st->cldfbBPF )
533 : {
534 167724 : scale_sig32( st->cldfbBPF->cldfb_state_fx, st->cldfbBPF->cldfb_size, Q11 - Q10 ); // Q11
535 167724 : st->cldfbBPF->Q_cldfb_state = Q11;
536 167724 : move16();
537 : }
538 167724 : IF( st->cldfbSyn )
539 : {
540 167724 : scale_sig32( st->cldfbSyn->cldfb_state_fx, st->cldfbSyn->cldfb_size, Q11 - Q10 ); // Q11
541 167724 : st->cldfbSyn->Q_cldfb_state = Q11;
542 167724 : move16();
543 : }
544 167724 : move16();
545 :
546 167724 : IF( save_hb_synth_32_fx )
547 : {
548 91 : Copy_Scale_sig_16_32_DEPREC( save_hb_synth_16_fx, save_hb_synth_32_fx, output_frame, Q11 ); // Q11
549 91 : hSCE->q_save_hb_synth_fx = Q11;
550 91 : move16();
551 : }
552 :
553 167724 : Copy_Scale_sig_16_32_DEPREC( old_syn_12k8_16k_fx_16, old_syn_12k8_16k_fx[n], L_FRAME16k, Q11 - ( -Q1 ) ); // Q(11 - (-1))
554 : }
555 :
556 1029590 : Copy_Scale_sig_32_16( st->previoussynth_fx_32, st->previoussynth_fx, L_FRAME48k, 0 ); // Q0
557 :
558 1029590 : test();
559 1029590 : test();
560 1029590 : IF( ( EQ_16( st->core, TCX_20_CORE ) || EQ_16( st->core, TCX_10_CORE ) ) && NE_16( st->element_mode, IVAS_CPE_MDCT ) )
561 : {
562 : Word16 Qsyn_temp;
563 : IVAS_FORMAT ivas_format;
564 :
565 215251 : Qsyn_temp = st->Q_syn;
566 215251 : move16();
567 215251 : st->Q_syn = 0;
568 215251 : move16();
569 215251 : st->prev_Q_syn = st->Q_syn;
570 215251 : move16();
571 215251 : st->hTcxDec->conNoiseLevelIndex = st->hTcxDec->NoiseLevelIndex_bfi;
572 215251 : move16();
573 215251 : st->hTcxDec->conCurrLevelIndex = st->hTcxDec->CurrLevelIndex_bfi;
574 215251 : move16();
575 215251 : st->mem_error = st->hBPF->pst_mem_deemp_err_fx;
576 215251 : move32();
577 :
578 : /* TCX decoder */
579 215251 : Scale_sig( st->hPFstat->mem_stp, L_SUBFR, -Qsyn_temp ); // Q0
580 215251 : Scale_sig( st->hPFstat->mem_pf_in, L_SUBFR, -Qsyn_temp ); // Q0
581 215251 : Scale_sig( st->hHQ_core->old_out_LB_fx, L_FRAME32k, -st->hHQ_core->Q_old_wtda_LB ); // Q0
582 215251 : Scale_sig( st->hHQ_core->old_out_fx, L_FRAME48k, -st->hHQ_core->Q_old_wtda ); // Q0
583 215251 : st->hHQ_core->Q_old_wtda_LB = 0;
584 215251 : move16();
585 215251 : st->hHQ_core->Q_old_wtda = 0;
586 215251 : move16();
587 :
588 215251 : IF( st_ivas == NULL )
589 : {
590 11784 : ivas_format = 0;
591 11784 : move32();
592 : }
593 : ELSE
594 : {
595 203467 : ivas_format = st_ivas->ivas_format;
596 203467 : move32();
597 : }
598 :
599 215251 : stereo_tcx_core_dec_fx( st, frameMode[n], output_16_fx[n], synth_16_fx[n], pitch_buf_fx[n], sba_dirac_stereo_flag, hStereoTD, last_element_mode, flag_sec_CNA, hStereoCng, nchan_out, ivas_format );
600 :
601 215251 : st->hHQ_core->Q_old_wtda_LB = st->hHQ_core->Q_old_wtda;
602 215251 : move16();
603 215251 : Copy_Scale_sig_16_32_DEPREC( output_16_fx[n], output_32_fx[n], output_frame, Q11 ); // Q11
604 :
605 215251 : IF( st->hTcxDec )
606 : {
607 215251 : st->hTcxDec->conNoiseLevelIndex = st->hTcxDec->NoiseLevelIndex_bfi;
608 215251 : move16();
609 215251 : st->hTcxDec->conCurrLevelIndex = st->hTcxDec->CurrLevelIndex_bfi;
610 215251 : move16();
611 215251 : st->hTcxDec->conLastFrameLevel = st->hTcxDec->LastFrameLevel_bfi_fx;
612 215251 : move16();
613 : }
614 215251 : st->hBPF->pst_mem_deemp_err_fx = extract_l( st->mem_error );
615 215251 : move16();
616 : }
617 :
618 1029590 : IF( EQ_16( st->core, HQ_CORE ) )
619 : {
620 : /* HQ core decoder */
621 : Word16 Q_output;
622 :
623 7475 : Q_output = 0;
624 7475 : move16();
625 7475 : Q_synth = 0;
626 7475 : move16();
627 :
628 7475 : ivas_hq_core_dec_fx( st, synth_16_fx[n], &Q_synth, output_frame, NORMAL_HQ_CORE, core_switching_flag[n], output_16_fx[n], &Q_output );
629 :
630 7475 : Copy_Scale_sig_16_32_DEPREC( output_16_fx[n], output_32_fx[n], L_FRAME48k, sub( Q11, Q_output ) ); // Q11
631 7475 : Scale_sig( synth_16_fx[n], output_frame, negate( Q_synth ) ); // Q0
632 7475 : Scale_sig( output_16_fx[n], L_FRAME48k, negate( Q_output ) ); // Q0
633 : }
634 :
635 : /*---------------------------------------------------------------------*
636 : * TD stereo updates
637 : *---------------------------------------------------------------------*/
638 :
639 1029590 : test();
640 1029590 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) && n == 0 )
641 : {
642 3751 : Copy_Scale_sig_32_16( hCPE->hCoreCoder[0]->old_pitch_buf_fx, hCPE->hCoreCoder[0]->old_pitch_buf_16_fx, add( imult1616( 2, NB_SUBFR16k ), 2 ), -Q10 ); // Q6
643 :
644 3751 : td_stereo_param_updt_fx( st->lsp_old_fx, st->lsf_old_fx, st->old_pitch_buf_16_fx + st->nb_subfr,
645 3751 : tdm_lspQ_PCh_fx, tdm_lsfQ_PCh_fx, hStereoTD->tdm_Pri_pitch_buf_fx, st->flag_ACELP16k, hStereoTD->tdm_use_IAWB_Ave_lpc );
646 3751 : Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[0]->old_pitch_buf_16_fx, hCPE->hCoreCoder[0]->old_pitch_buf_fx, add( imult1616( 2, NB_SUBFR16k ), 2 ), Q10 ); // Q16
647 : }
648 :
649 : } /* n_channels loop */
650 :
651 :
652 : /*---------------------------------------------------------------------*
653 : * MDCT stereo: joint TCX Core Decoding
654 : *---------------------------------------------------------------------*/
655 :
656 702097 : IF( EQ_16( sts[0]->element_mode, IVAS_CPE_MDCT ) )
657 : {
658 : /* active-frame decoding */
659 323742 : IF( GT_32( sts[0]->core_brate, SID_2k40 ) )
660 : {
661 319570 : IF( hMCT )
662 : {
663 723414 : FOR( n = 0; n < n_channels; n++ )
664 : {
665 482276 : st = sts[n];
666 482276 : IF( st->cldfbAna )
667 : {
668 120 : scale_sig32( st->cldfbAna->cldfb_state_fx, st->cldfbAna->cldfb_size, Q11 - Q10 ); // Q11
669 120 : st->cldfbAna->Q_cldfb_state = Q11;
670 120 : move16();
671 : }
672 482276 : IF( st->cldfbSynHB )
673 : {
674 0 : scale_sig32( st->cldfbSynHB->cldfb_state_fx, st->cldfbSynHB->cldfb_size, Q11 - Q10 ); // Q11
675 0 : st->cldfbSynHB->Q_cldfb_state = Q11;
676 0 : move16();
677 : }
678 : }
679 :
680 241138 : pop_wmops();
681 241138 : return error;
682 : }
683 : ELSE
684 : {
685 : Word16 ch;
686 :
687 78432 : e_sig[0] = 17;
688 78432 : e_sig[1] = 17;
689 78432 : move16();
690 78432 : move16();
691 78432 : sts = hCPE->hCoreCoder;
692 :
693 235296 : FOR( ch = 0; ch < CPE_CHANNELS; ch++ )
694 : {
695 156864 : st = sts[ch];
696 :
697 156864 : st->last_gain_syn_deemph = 0;
698 156864 : move16();
699 156864 : if ( st->hHQ_core )
700 : {
701 156864 : st->hHQ_core->Q_fer_samples = 0;
702 156864 : move16();
703 : }
704 156864 : IF( NE_16( st->core, st->last_core ) )
705 : {
706 5101 : IF( st->hTcxDec )
707 : {
708 5101 : Scale_sig( st->hTcxDec->syn_OverlFB, L_FRAME_MAX / 2, negate( st->Q_syn ) ); // Q0
709 : }
710 5101 : st->Q_syn = 0;
711 5101 : move16();
712 : }
713 :
714 156864 : st->prev_Q_syn = st->Q_syn;
715 156864 : move16();
716 :
717 156864 : if ( st->hTcxDec )
718 : {
719 156864 : st->hTcxDec->conNoiseLevelIndex = st->hTcxDec->NoiseLevelIndex_bfi;
720 156864 : move16();
721 : }
722 156864 : if ( st->hTcxDec )
723 : {
724 156864 : st->hTcxDec->conCurrLevelIndex = st->hTcxDec->CurrLevelIndex_bfi;
725 156864 : move16();
726 : }
727 : }
728 :
729 78432 : stereo_mdct_core_dec_fx( st_ivas, hCPE, output_32_fx, synth_16_fx, e_sig );
730 : }
731 : }
732 : /* for inactive frames with mono output, copy and (if necessary) downmix buffers */
733 4172 : ELSE IF( EQ_16( hCPE->nchan_out, 1 ) )
734 : {
735 : Word16 shift1, shift2;
736 :
737 1128 : sts[0] = hCPE->hCoreCoder[0];
738 1128 : sts[1] = hCPE->hCoreCoder[1];
739 :
740 1128 : IF( LE_32( last_element_brate, IVAS_SID_5k2 ) )
741 : {
742 1090 : sts[0]->hHQ_core->exp_old_out = sub( 15, sts[0]->hHQ_core->Q_old_wtda );
743 1090 : move16();
744 1090 : sts[1]->hHQ_core->exp_old_out = sub( 15, sts[1]->hHQ_core->Q_old_wtda );
745 1090 : move16();
746 : }
747 :
748 1128 : updateBuffersForDmxMdctStereo_fx( hCPE, output_frame, output_32_fx[0], output_32_fx[1], synth_16_fx );
749 :
750 1128 : IF( LE_32( last_element_brate, IVAS_SID_5k2 ) )
751 : {
752 1090 : sts[0]->hHQ_core->Q_old_wtda = sub( 15, sts[0]->hHQ_core->exp_old_out );
753 1090 : move16();
754 1090 : sts[1]->hHQ_core->Q_old_wtda = sub( 15, sts[1]->hHQ_core->exp_old_out );
755 1090 : move16();
756 :
757 1090 : shift1 = norm_arr( sts[0]->hHQ_core->old_out_fx, L_FRAME48k );
758 1090 : shift2 = norm_arr( sts[1]->hHQ_core->old_out_fx, L_FRAME48k );
759 1090 : scale_sig( sts[0]->hHQ_core->old_out_fx, L_FRAME48k, shift1 );
760 1090 : scale_sig( sts[1]->hHQ_core->old_out_fx, L_FRAME48k, shift2 );
761 1090 : sts[0]->hHQ_core->Q_old_wtda = add( sts[0]->hHQ_core->Q_old_wtda, shift1 );
762 1090 : sts[1]->hHQ_core->Q_old_wtda = add( sts[1]->hHQ_core->Q_old_wtda, shift2 );
763 1090 : move16();
764 1090 : move16();
765 : }
766 : }
767 :
768 82604 : test();
769 82604 : test();
770 : /* On first good active frame after frameloss undo the whitening of the bg noise shape */
771 82604 : IF( GT_32( sts[0]->core_brate, SID_2k40 ) && sts[0]->bfi == 0 && EQ_16( sts[0]->prev_bfi, 1 ) )
772 : {
773 2958 : FOR( n = 0; n < n_channels; ++n )
774 : {
775 1972 : IF( NE_16( sts[n]->last_core_bfi, ACELP_CORE ) )
776 : {
777 1485 : TonalMdctConceal_whiten_noise_shape_ivas_fx( sts[n], L_FRAME16k, ON_FIRST_GOOD_FRAME );
778 : }
779 : }
780 : }
781 : }
782 :
783 : /*---------------------------------------------------------------------*
784 : * Stereo CNG updates
785 : *---------------------------------------------------------------------*/
786 :
787 460959 : test();
788 460959 : IF( EQ_16( sts[0]->element_mode, IVAS_CPE_TD ) && hStereoCng != NULL )
789 : {
790 : /* To be cleaned up once the caller function is converted // These changes are for system testing of fixed changes made */
791 : Word16 Q_c_PS_LT, Q_output;
792 : Word32 c_PS_LT_fx;
793 :
794 3751 : Q_c_PS_LT = 31;
795 3751 : move16();
796 3751 : Q_output = 11;
797 3751 : move16();
798 3751 : c_PS_LT_fx = L_deposit_h( hStereoCng->c_PS_LT_fx );
799 3751 : Q_c_PS_LT = Q31;
800 3751 : move16();
801 :
802 3751 : stereo_cng_compute_PScorr_fx( output_32_fx[0], output_32_fx[1], &Q_output, &c_PS_LT_fx, Q_c_PS_LT, sts[0]->L_frame, sts[1]->L_frame );
803 :
804 3751 : hStereoCng->c_PS_LT_fx = extract_h( c_PS_LT_fx );
805 : }
806 :
807 : /*---------------------------------------------------------------------*
808 : * Postprocessing, BWEs and updates
809 : *---------------------------------------------------------------------*/
810 :
811 1003762 : FOR( n = 0; n < n_channels; n++ )
812 : {
813 547314 : st = sts[n];
814 547314 : hBWE_TD = st->hBWE_TD;
815 547314 : hBWE_FD = st->hBWE_FD;
816 :
817 : /*---------------------------------------------------------------------*
818 : * TD-BWE for ACELP to TCX transitions
819 : *---------------------------------------------------------------------*/
820 :
821 : /*core_switching_post_dec*/
822 547314 : Q_synth = sub( 15, e_sig[0] );
823 :
824 : /*------------------fix-to-fix-end-----------------------*/
825 :
826 : Word16 output_mem_16_fx[L_FRAME48k];
827 : Word16 *p_output_mem_16;
828 :
829 547314 : IF( p_output_mem_fx != NULL )
830 : {
831 137985 : p_output_mem_16 = output_mem_16_fx;
832 137985 : Copy_Scale_sig_32_16( p_output_mem_fx, p_output_mem_16, NS2SA_FX2( output_Fs, STEREO_DFT32MS_OVL_NS ), -Q11 ); // Q(q_output_mem_fx-11)
833 : }
834 : ELSE
835 : {
836 409329 : p_output_mem_16 = NULL;
837 409329 : set16_fx( output_mem_16_fx, 0, NS2SA_FX2( st->output_Fs, 3125000 ) );
838 : }
839 :
840 547314 : Scale_sig32( output_32_fx[n], L_FRAME48k, Q4 - Q11 ); // Q4
841 :
842 : /*size of synth is choosen as delay comp to start with*/
843 : /*-------------------cldfb-start-------------------------*/
844 :
845 547314 : IF( st->cldfbSyn != NULL )
846 : {
847 547314 : scale_sig32( st->cldfbSyn->cldfb_state_fx, st->cldfbSyn->p_filter_length, Q4 - Q11 ); // Q4
848 547314 : st->cldfbSyn->Q_cldfb_state = Q4;
849 547314 : move16();
850 : }
851 :
852 : Word16 q_audio, old_syn_fx;
853 547314 : old_syn_fx = Q11;
854 547314 : move16();
855 547314 : q_audio = Q12;
856 547314 : move16();
857 547314 : test();
858 547314 : test();
859 547314 : test();
860 547314 : test();
861 547314 : IF( ( st->last_core == ACELP_CORE ) && ( EQ_16( st->core, TCX_20_CORE ) || EQ_16( st->core, TCX_10_CORE ) || EQ_16( st->core, HQ_CORE ) ) && hBWE_TD != NULL )
862 : {
863 7641 : test();
864 7641 : test();
865 7641 : test();
866 7641 : test();
867 7641 : IF( ( EQ_16( st->bwidth, SWB ) || EQ_16( st->bwidth, FB ) ) && ( EQ_16( st->last_extl, SWB_TBE ) || EQ_16( st->last_extl, FB_TBE ) ) )
868 : {
869 5722 : GenTransition_fx32( hBWE_TD, hb_synth_32_fx[n], output_Fs, st->L_frame, hBWE_TD->prev_Qx );
870 : }
871 1919 : ELSE IF( EQ_16( st->bwidth, WB ) && EQ_16( st->last_extl, WB_TBE ) )
872 : {
873 116 : GenTransition_WB_fx32( hBWE_TD, hb_synth_32_fx[n], output_Fs );
874 : }
875 :
876 : /* Memories Scaling */
877 7641 : Copy_Scale_sig_32_16( hBWE_TD->syn_overlap_fx_32, hBWE_TD->syn_overlap_fx, L_SHB_LAHEAD, sub( hBWE_TD->prev_Q_bwe_syn2, Q11 ) ); // prev_Q_bwe_syn2
878 7641 : Copy_Scale_sig_32_16( hBWE_TD->old_tbe_synth_fx_32, hBWE_TD->old_tbe_synth_fx, L_SHB_TRANSITION_LENGTH, sub( hBWE_TD->prev_Qx, Q11 ) ); // prev_Qx
879 7641 : Copy_Scale_sig_32_16( hBWE_TD->state_lsyn_filt_dwn_shb_fx_32, hBWE_TD->state_lsyn_filt_dwn_shb_fx, ALLPASSSECTIONS_STEEP * 2, sub( hBWE_TD->prev_Qx, Q11 ) ); // prev_Qx
880 7641 : Copy_Scale_sig_32_16( hBWE_TD->state_lsyn_filt_shb_fx_32, hBWE_TD->state_lsyn_filt_shb_fx, ALLPASSSECTIONS_STEEP * 2, sub( hBWE_TD->prev_Qx, Q11 ) ); // prev_Qx
881 7641 : Copy_Scale_sig_32_16( hBWE_TD->mem_resamp_HB_fx_32, hBWE_TD->mem_resamp_HB_fx, INTERP_3_1_MEM_LEN, sub( hBWE_TD->prev_Qx, Q11 ) ); // prev_Qx
882 : }
883 :
884 : /* Memories Re-Scaling */
885 547314 : IF( hBWE_TD != NULL )
886 : {
887 390912 : Copy_Scale_sig_16_32_no_sat( hBWE_TD->syn_overlap_fx, hBWE_TD->syn_overlap_fx_32, L_SHB_LAHEAD, sub( Q11, hBWE_TD->prev_Q_bwe_syn2 ) ); // Q11
888 390912 : Copy_Scale_sig_16_32_no_sat( hBWE_TD->old_tbe_synth_fx, hBWE_TD->old_tbe_synth_fx_32, L_SHB_TRANSITION_LENGTH, sub( Q11, hBWE_TD->prev_Qx ) ); // Q11
889 390912 : Copy_Scale_sig_16_32_no_sat( hBWE_TD->state_lsyn_filt_dwn_shb_fx, hBWE_TD->state_lsyn_filt_dwn_shb_fx_32, ALLPASSSECTIONS_STEEP * 2, sub( Q11, hBWE_TD->prev_Qx ) ); // Q11
890 390912 : Copy_Scale_sig_16_32_no_sat( hBWE_TD->state_lsyn_filt_shb_fx, hBWE_TD->state_lsyn_filt_shb_fx_32, ALLPASSSECTIONS_STEEP * 2, sub( Q11, hBWE_TD->prev_Qx ) ); // Q11
891 390912 : Copy_Scale_sig_16_32_no_sat( hBWE_TD->mem_resamp_HB_fx, hBWE_TD->mem_resamp_HB_fx_32, INTERP_3_1_MEM_LEN, sub( Q11, hBWE_TD->prev_Qx ) ); // Q11
892 390912 : Copy( hBWE_TD->mem_resamp_HB_fx, hBWE_TD->state_32and48k_WB_upsample_fx, ( 2 * ALLPASSSECTIONS_STEEP ) );
893 : }
894 :
895 : /*---------------------------------------------------------------------*
896 : * Postprocessing for ACELP/MDCT core switching
897 : *---------------------------------------------------------------------*/
898 :
899 : /* save synth and output in case of SBA DirAC stereo output as core switching is done outside of core decoder */
900 547314 : test();
901 547314 : test();
902 547314 : test();
903 547314 : IF( sba_dirac_stereo_flag && NE_16( st->element_mode, IVAS_CPE_MDCT ) && !( EQ_32( st->core_brate, SID_2k40 ) && EQ_16( st->cng_type, FD_CNG ) ) )
904 : {
905 4498 : Copy_Scale_sig_16_32_no_sat( synth_16_fx[n], hSCE->save_synth_fx, output_frame, sub( hSCE->q_save_synth_fx, Q_synth ) ); // q_save_synth_fx
906 : }
907 :
908 547314 : IF( NE_32( ( error = core_switching_post_dec_ivas_fx( st, synth_16_fx[n], output_32_fx[n], p_output_mem_16, use_cldfb_for_dft, output_frame, 0 /*core_switching_flag*/, sba_dirac_stereo_flag, nchan_out, last_element_mode, &Q_synth ) ), IVAS_ERR_OK ) )
909 : {
910 0 : return error;
911 : }
912 :
913 : /* for FD-CNG we need the delay compensation in the synth, so do this afterwards */
914 547314 : test();
915 547314 : test();
916 547314 : test();
917 547314 : IF( sba_dirac_stereo_flag && hSCE && EQ_32( st->core_brate, SID_2k40 ) && EQ_16( st->cng_type, FD_CNG ) )
918 : {
919 13 : Copy_Scale_sig_16_32_no_sat( synth_16_fx[n], hSCE->save_synth_fx, output_frame, sub( hSCE->q_save_synth_fx, Q_synth ) ); // q_save_synth_fx
920 : }
921 :
922 : /* 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 */
923 547314 : test();
924 547314 : test();
925 547314 : test();
926 547314 : test();
927 547314 : test();
928 547314 : test();
929 547314 : IF( n == 0 && EQ_16( st->element_mode, IVAS_CPE_MDCT ) && st->last_core == ACELP_CORE && st->core != ACELP_CORE && ( EQ_16( nchan_out, 1 ) || EQ_16( last_element_mode, IVAS_CPE_DFT ) ) )
930 : {
931 552 : Copy( sts[0]->previoussynth_fx, sts[1]->previoussynth_fx, st->hTcxDec->L_frameTCX );
932 : }
933 :
934 : /*---------------------------------------------------------------------*
935 : * Pre-processing for bandwidth switching
936 : *---------------------------------------------------------------------*/
937 :
938 547314 : ivas_bw_switching_pre_proc_fx( st, last_element_brate, nchan_out, old_syn_12k8_16k_fx[n], old_syn_fx, q_audio );
939 :
940 547314 : IF( st->hHQ_core == NULL )
941 : {
942 3751 : Copy_Scale_sig_16_32_DEPREC( st->delay_buf_out_fx, st->delay_buf_out32_fx, ( HQ_DELTA_MAX * HQ_DELAY_COMP ), Q11 ); // Q11
943 : }
944 : ELSE
945 : {
946 543563 : Copy_Scale_sig_16_32_no_sat( st->delay_buf_out_fx, st->delay_buf_out32_fx, ( HQ_DELTA_MAX * HQ_DELAY_COMP ), sub( Q11, st->hHQ_core->Q_old_postdec ) ); // Q11
947 : }
948 :
949 547314 : Scale_sig32( output_32_fx[n], L_FRAME48k, ( Q11 - Q4 ) ); // Q11
950 :
951 : /*note : cldfb_size here signifies the original size which was assigned to cldfb_state_fx buffer not its current size*/
952 :
953 547314 : IF( st->cldfbSyn != NULL )
954 : {
955 547314 : scale_sig32( st->cldfbSyn->cldfb_state_fx, st->cldfbSyn->p_filter_length, ( Q11 - Q4 ) ); // Q11
956 547314 : st->cldfbSyn->Q_cldfb_state = Q11;
957 547314 : move16();
958 : }
959 :
960 547314 : IF( Q_synth > 0 )
961 : {
962 144925 : Scale_sig( synth_16_fx[n], output_frame, negate( Q_synth ) ); // Q0
963 144925 : Q_synth = 0;
964 144925 : move16();
965 : }
966 :
967 : /*------------------reset-code-start---------------------*/
968 :
969 547314 : test();
970 547314 : test();
971 547314 : IF( NE_16( st->last_extl, WB_BWE ) && EQ_16( st->extl, WB_BWE ) && hBWE_FD != NULL )
972 : {
973 440 : test();
974 440 : if ( NE_16( st->last_extl, SWB_BWE ) && NE_16( st->last_extl, FB_BWE ) )
975 : {
976 439 : hBWE_FD->prev_mode = hBWE_FD->prev_mode;
977 439 : move16();
978 : }
979 440 : hBWE_FD->prev_L_swb_norm = hBWE_FD->prev_L_swb_norm;
980 440 : move16();
981 440 : hBWE_FD->prev_flag = hBWE_FD->prev_flag;
982 440 : move16();
983 : }
984 :
985 : /*---------------------------------------------------------------------*
986 : * WB TBE decoding
987 : * WB BWE decoding
988 : *---------------------------------------------------------------------*/
989 :
990 : Word16 Q_input, Q_hb_synth_fx, Q_synth_fx;
991 : Word16 Q_syn_hb, sf;
992 :
993 547314 : sf = getScaleFactor32( output_32_fx[n], L_FRAME48k );
994 :
995 547314 : Q_input = 0;
996 547314 : move16();
997 547314 : if ( LT_16( sf, 5 ) )
998 : {
999 12989 : Q_input = sub( sf, 5 );
1000 : }
1001 547314 : Q_hb_synth_fx = 0;
1002 547314 : move16();
1003 547314 : Q_synth_fx = Q_synth;
1004 547314 : move16();
1005 :
1006 547314 : Copy_Scale_sig_32_16( output_32_fx[n], output_16_fx[n], L_FRAME48k, sub( Q_input, Q11 ) ); // Q_input
1007 547314 : Copy_Scale_sig_32_16( hb_synth_32_fx[n], hb_synth_16_fx[n], L_FRAME48k, -( Q11 ) ); // Q0
1008 547314 : test();
1009 547314 : test();
1010 547314 : test();
1011 547314 : test();
1012 547314 : test();
1013 547314 : test();
1014 547314 : IF( EQ_16( st->extl, WB_TBE ) )
1015 : {
1016 : /* WB TBE decoder */
1017 :
1018 6950 : ivas_wb_tbe_dec_fx( st, st->coder_type, bwe_exc_extended_fx[n], st->Q_exc, voice_factors_fx[n], hb_synth_16_fx[n], &Q_hb_synth_fx );
1019 : }
1020 540364 : ELSE IF( EQ_16( st->element_mode, IVAS_CPE_TD ) && EQ_16( n, 1 ) && !tdm_LRTD_flag && NE_16( st->extl, -1 ) && st->bws_cnt == 0 && st->extl_brate == 0 )
1021 : {
1022 : /* do nothing */
1023 : }
1024 540238 : ELSE IF( EQ_16( st->extl, WB_BWE ) && st->bws_cnt == 0 )
1025 : {
1026 : /* WB BWE decoder */
1027 8889 : Q_hb_synth_fx = ivas_wb_bwe_dec_fx( st, output_16_fx[n], synth_16_fx[n], hb_synth_16_fx[n], use_cldfb_for_dft, output_frame, voice_factors_fx[n], pitch_buf_fx[n], &Q_synth_fx );
1028 : }
1029 :
1030 : /* Memories Re-Scaling */
1031 547314 : Copy_Scale_sig_16_32_no_sat( hb_synth_16_fx[n], hb_synth_32_fx[n], L_FRAME48k, sub( Q11, Q_hb_synth_fx ) ); // Q11
1032 547314 : Copy_Scale_sig_16_32_no_sat( output_16_fx[n], output_32_fx[n], L_FRAME48k, sub( Q11, Q_input ) ); // Q11 // Q_input can get value <= -5
1033 547314 : Copy_Scale_sig_16_32_no_sat( synth_16_fx[n], synth_32_fx[n], output_frame, sub( Q11, Q_synth_fx ) ); // Q11
1034 :
1035 547314 : IF( hBWE_FD != NULL )
1036 : {
1037 390912 : Copy_Scale_sig_16_32_no_sat( hBWE_FD->L_old_wtda_swb_fx, hBWE_FD->L_old_wtda_swb_fx32, L_FRAME48k, sub( Q11, hBWE_FD->old_wtda_swb_fx_exp ) ); // Q11
1038 : }
1039 547314 : IF( hBWE_TD != NULL )
1040 : {
1041 390912 : Copy_Scale_sig_16_32_no_sat( hBWE_TD->old_tbe_synth_fx, hBWE_TD->old_tbe_synth_fx_32, L_SHB_TRANSITION_LENGTH, sub( Q11, hBWE_TD->prev_Qx ) ); // Q11
1042 : }
1043 :
1044 : /*---------------------------------------------------------------------*
1045 : * SWB(FB) TBE decoding
1046 : * SWB(FB) BWE decoding
1047 : *---------------------------------------------------------------------*/
1048 :
1049 547314 : Q_white_exc = 0;
1050 547314 : move16();
1051 :
1052 547314 : test();
1053 547314 : test();
1054 547314 : test();
1055 547314 : test();
1056 547314 : test();
1057 547314 : test();
1058 547314 : test();
1059 547314 : test();
1060 547314 : test();
1061 547314 : test();
1062 547314 : test();
1063 547314 : test();
1064 547314 : test();
1065 547314 : test();
1066 547314 : test();
1067 547314 : test();
1068 547314 : test();
1069 547314 : IF( EQ_16( st->extl, SWB_TBE ) || EQ_16( st->extl, FB_TBE ) || ( NE_16( st->coder_type, AUDIO ) && NE_16( st->coder_type, INACTIVE ) && GE_32( st->core_brate, SID_2k40 ) && EQ_16( st->core, ACELP_CORE ) && !st->con_tcx && GE_32( output_Fs, 32000 ) && GT_16( st->bwidth, NB ) && st->bws_cnt > 0 ) )
1070 : {
1071 : /* SWB TBE decoder */
1072 102554 : ivas_swb_tbe_dec_fx( st, hStereoICBWE, bwe_exc_extended_fx[n], st->Q_exc, voice_factors_fx[n], old_syn_12k8_16k_fx[n], tmp_buffer_fx /*fb_exc*/, hb_synth_32_fx[n], pitch_buf_fx[n], &Q_white_exc );
1073 :
1074 102554 : Copy_Scale_sig_16_32_no_sat( hBWE_TD->syn_overlap_fx, hBWE_TD->syn_overlap_fx_32, L_SHB_LAHEAD, sub( Q11, hBWE_TD->prev_Q_bwe_syn2 ) ); // Q11
1075 102554 : Copy_Scale_sig_32_16( hBWE_TD->old_tbe_synth_fx_32, hBWE_TD->old_tbe_synth_fx, L_SHB_TRANSITION_LENGTH, sub( hBWE_TD->prev_Qx, Q11 ) ); // prev_Qx
1076 :
1077 102554 : IF( GT_16( Q_white_exc, 31 ) )
1078 : {
1079 284 : Scale_sig( tmp_buffer_fx, L_FRAME48k, sub( 31, Q_white_exc ) ); // Q31
1080 284 : Q_white_exc = 31;
1081 284 : move16();
1082 : }
1083 :
1084 : /* FB TBE decoder */
1085 102554 : IF( EQ_16( st->extl, FB_TBE ) )
1086 : {
1087 39990 : fb_tbe_dec_ivas_fx( st, tmp_buffer_fx /*fb_exc*/, Q_white_exc, hb_synth_32_fx[n], 0, tmp_buffer_fx /*fb_synth_ref*/, Q_white_exc, output_frame );
1088 : }
1089 : }
1090 444760 : ELSE IF( EQ_16( st->extl, SWB_BWE ) || EQ_16( st->extl, FB_BWE ) || ( GE_32( output_Fs, 32000 ) && st->core == ACELP_CORE && st->bwidth > NB && st->bws_cnt > 0 && !st->ppp_mode_dec && !( EQ_16( st->nelp_mode_dec, 1 ) && EQ_16( st->bfi, 1 ) ) ) )
1091 : {
1092 : /* SWB BWE decoder */
1093 12399 : Q_syn_hb = swb_bwe_dec_fx32( st, output_32_fx[n], synth_32_fx[n], hb_synth_32_fx[n], use_cldfb_for_dft, output_frame );
1094 :
1095 12399 : Scale_sig32( hb_synth_32_fx[n], output_frame, sub( Q11, Q_syn_hb ) ); // Q11
1096 :
1097 12399 : Copy_Scale_sig_32_16( hBWE_FD->L_old_wtda_swb_fx32, hBWE_FD->L_old_wtda_swb_fx, output_frame, sub( hBWE_FD->old_wtda_swb_fx_exp, Q11 ) ); // old_wtda_swb_fx_exp
1098 : }
1099 :
1100 : /*---------------------------------------------------------------------*
1101 : * FEC - recovery after lost HQ core (smoothing of the BWE component)
1102 : *---------------------------------------------------------------------*/
1103 :
1104 547314 : test();
1105 547314 : test();
1106 547314 : IF( st->prev_bfi && EQ_16( st->last_core, HQ_CORE ) && NE_16( st->extl, -1 ) )
1107 : {
1108 : /*tmp = FRAC_BWE_SMOOTH/output_frame;*/
1109 9 : tmp16 = shr( 410, shr( output_frame, 8 ) ); /* Q15 */
1110 9 : if ( EQ_16( output_frame, L_FRAME48k ) )
1111 : {
1112 5 : tmp16 = 68;
1113 5 : move16();
1114 : }
1115 : /*output_frame/FRAC_BWE_SMOOTH*/
1116 9 : j = shr( output_frame, 1 );
1117 9 : tmp16_2 = 0;
1118 9 : move16();
1119 3689 : FOR( i = 0; i < j; i++ )
1120 : {
1121 3680 : hb_synth_32_fx[n][i] = Mpy_32_16_1( hb_synth_32_fx[n][i], tmp16_2 ); /* Q11 */
1122 3680 : move32();
1123 3680 : tmp16_2 = add_o( tmp16_2, tmp16, &Overflow ); /* Q15 */
1124 : }
1125 : }
1126 :
1127 : /*---------------------------------------------------------------------*
1128 : * SWB CNG
1129 : *---------------------------------------------------------------------*/
1130 :
1131 547314 : test();
1132 547314 : test();
1133 547314 : test();
1134 547314 : test();
1135 547314 : IF( ( GE_16( output_frame, L_FRAME32k ) && st->hTdCngDec != NULL ) || ( EQ_16( st->element_mode, IVAS_CPE_DFT ) && GE_16( st->bwidth, SWB ) && st->hTdCngDec != NULL ) )
1136 : {
1137 : /* SHB CNG decoder */
1138 : Word16 synth_fxl[960]; /* Q-2 */
1139 : Word16 q;
1140 :
1141 213448 : q = 2;
1142 213448 : move16();
1143 213448 : Copy_Scale_sig_32_16( hb_synth_32_fx[n], hb_synth_16_fx[n], L_FRAME48k, -( Q11 ) ); // Q0
1144 213448 : Copy_Scale_sig_32_16( synth_32_fx[n], synth_fxl, output_frame, negate( add( Q11, q ) ) ); // Q0
1145 213448 : Scale_sig( hBWE_TD->state_lpc_syn_fx, LPC_SHB_ORDER, sub( Q8, hBWE_TD->prev_Q_bwe_syn ) ); // Q8
1146 213448 : Copy_Scale_sig_32_16( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, ( 2 * ALLPASSSECTIONS_STEEP ), sub( hBWE_TD->prev_Q_bwe_syn2, Q11 ) ); // prev_Q_bew_syn2
1147 :
1148 213448 : swb_CNG_dec_ivas_fx( st, synth_fxl, hb_synth_16_fx[n], sid_bw[n], negate( q ) );
1149 :
1150 213448 : test();
1151 213448 : IF( st->core_brate == FRAME_NO_DATA || EQ_32( st->core_brate, SID_2k40 ) )
1152 : {
1153 12817 : Copy_Scale_sig_16_32_DEPREC( hb_synth_16_fx[n], hb_synth_32_fx[n], L_FRAME48k, ( Q11 ) ); // Q11
1154 : }
1155 :
1156 213448 : Scale_sig( hBWE_TD->state_lpc_syn_fx, LPC_SHB_ORDER, negate( sub( Q8, hBWE_TD->prev_Q_bwe_syn ) ) ); // Q0
1157 213448 : Copy_Scale_sig_16_32_no_sat( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, 2 * ALLPASSSECTIONS_STEEP, negate( sub( hBWE_TD->prev_Q_bwe_syn2, Q11 ) ) );
1158 : }
1159 :
1160 : /*-------------------------------------------------------------------*
1161 : * Inter-channel BWE decoding
1162 : *-------------------------------------------------------------------*/
1163 :
1164 547314 : test();
1165 547314 : IF( n == 0 && GE_16( st->element_mode, IVAS_CPE_DFT ) )
1166 : {
1167 : Word16 q;
1168 145402 : q = 11;
1169 145402 : move16();
1170 :
1171 145402 : if ( hCPE->hStereoDft != NULL )
1172 : {
1173 60385 : hCPE->hStereoDft->td_gain_fx[0] = 1;
1174 60385 : move32();
1175 : }
1176 145402 : stereo_icBWE_dec_fx( hCPE, hb_synth_32_fx[0], hb_synth_32_fx[1], tmp_buffer_fx /*fb_synth_ref*/, voice_factors_fx[0], output_frame, &q, Q_white_exc );
1177 145402 : test();
1178 145402 : test();
1179 145402 : test();
1180 145402 : test();
1181 145402 : test();
1182 145402 : test();
1183 145402 : IF( ( GT_32( st->core_brate, SID_2k40 ) && ( !( ( ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->nchan_out, 1 ) ) || ( ( NE_16( st->core, ACELP_CORE ) || EQ_16( st->extl, -1 ) ) || ( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && hCPE->hCoreCoder[0]->tdm_LRTD_flag != 0 ) ) ) ) ) ) )
1184 : {
1185 17853 : Scale_sig32( hb_synth_32_fx[0], output_frame, sub( Q11, q ) ); // Q11
1186 17853 : Scale_sig32( hb_synth_32_fx[1], output_frame, sub( Q11, q ) ); // Q11
1187 : }
1188 : }
1189 :
1190 547314 : IF( EQ_16( st->element_mode, EVS_MONO ) )
1191 : {
1192 : /*----------------------------------------------------------------*
1193 : * BFI waveform adjustment
1194 : *----------------------------------------------------------------*/
1195 :
1196 0 : test();
1197 0 : test();
1198 0 : test();
1199 0 : test();
1200 0 : test();
1201 0 : test();
1202 0 : test();
1203 0 : test();
1204 0 : IF( EQ_16( st->core, ACELP_CORE ) && !st->bfi && st->prev_bfi && GE_32( st->last_total_brate, HQ_48k ) && EQ_16( st->last_codec_mode, MODE2 ) && ( EQ_16( st->last_core_bfi, TCX_20_CORE ) || EQ_16( st->last_core_bfi, TCX_10_CORE ) ) && EQ_16( st->hPlcInfo->concealment_method, TCX_NONTONAL ) && LT_16( st->hPlcInfo->nbLostCmpt, 4 ) )
1205 : {
1206 0 : tmps = NS2SA_FX2( output_Fs, DELAY_CLDFB_NS );
1207 0 : IF( st->hTonalMDCTConc->q_lastPcmOut != 0 )
1208 : {
1209 0 : Scale_sig( st->hTonalMDCTConc->secondLastPcmOut, shr( st->hPlcInfo->L_frameTCX, 1 ), negate( st->hTonalMDCTConc->q_lastPcmOut ) );
1210 0 : Scale_sig( st->hTonalMDCTConc->lastPcmOut, st->hPlcInfo->L_frameTCX, negate( st->hTonalMDCTConc->q_lastPcmOut ) );
1211 0 : st->hTonalMDCTConc->q_lastPcmOut = 0;
1212 0 : move16();
1213 : }
1214 0 : waveform_adj2_fix( st->hPlcInfo, st->hTonalMDCTConc->secondLastPcmOut, synth_16_fx[n] + tmps, tmps, add( st->hPlcInfo->nbLostCmpt, 1 ), st->bfi );
1215 :
1216 0 : st->hPlcInfo->Pitch_fx = 0;
1217 0 : move16();
1218 : }
1219 : }
1220 :
1221 : /*----------------------------------------------------------------*
1222 : * Transition and synchronization of BWE components
1223 : *----------------------------------------------------------------*/
1224 :
1225 547314 : test();
1226 547314 : test();
1227 547314 : test();
1228 547314 : test();
1229 547314 : IF( ( NE_16( st->extl, -1 ) && ( NE_16( st->extl, IGF_BWE ) || st->last_core == ACELP_CORE ) ) || ( st->bws_cnt > 0 && st->core == ACELP_CORE ) )
1230 : {
1231 : /* Calculate an additional delay of extension layer components to be synchronized with ACELP synthesis */
1232 156408 : IF( EQ_16( st->L_frame, L_FRAME ) )
1233 : {
1234 : /* TBE on top of ACELP@12.8kHz */
1235 84837 : tmps = NS2SA_FX2( output_Fs, MAX_DELAY_TBE_NS - DELAY_SWB_TBE_12k8_NS );
1236 : }
1237 : ELSE
1238 : {
1239 71571 : test();
1240 71571 : IF( EQ_16( st->extl, SWB_BWE_HIGHRATE ) || EQ_16( st->extl, FB_BWE_HIGHRATE ) )
1241 : {
1242 : /* HR SWB BWE on top of ACELP@16kHz */
1243 0 : tmps = NS2SA_FX2( output_Fs, DELAY_BWE_TOTAL_NS );
1244 : }
1245 : ELSE
1246 : {
1247 : /* TBE on top of ACELP@16kHz */
1248 71571 : tmps = NS2SA_FX2( output_Fs, MAX_DELAY_TBE_NS - DELAY_SWB_TBE_16k_NS );
1249 : }
1250 : }
1251 :
1252 : /* Smooth transitions when switching between different technologies */
1253 156408 : test();
1254 156408 : test();
1255 156408 : test();
1256 156408 : test();
1257 156408 : test();
1258 156408 : test();
1259 156408 : test();
1260 156408 : test();
1261 156408 : test();
1262 156408 : test();
1263 156408 : test();
1264 156408 : IF( !( ( EQ_16( st->core, TCX_20_CORE ) || EQ_16( st->core, TCX_10_CORE ) || EQ_16( st->core, HQ_CORE ) ) && st->last_core == ACELP_CORE ) &&
1265 : ( NE_16( st->extl, st->last_extl ) || ( EQ_16( st->extl, st->last_extl ) && EQ_16( s_xor( st->core, st->last_core ), HQ_CORE ) ) ) && !( EQ_16( st->extl, SWB_CNG ) && EQ_16( st->last_extl, SWB_TBE ) ) && ( NE_16( st->element_mode, IVAS_CPE_TD ) || ( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && tdm_LRTD_flag ) ) )
1266 : {
1267 : /* switching between BWE and TBE technologies */
1268 10264 : incr = idiv1616( ( L_FRAME * 2 ), add( shl( tmps, 1 ), 1 ) );
1269 10264 : tmp16 = 0;
1270 10264 : move16();
1271 58901 : FOR( i = 0; i < tmps; i++ )
1272 : {
1273 48637 : hb_synth_32_fx[n][i] = Mpy_32_16_1( hb_synth_32_fx[n][i], sin_table256_fx[tmp16] ); // Q11
1274 48637 : move16();
1275 48637 : tmp16 = add( tmp16, incr );
1276 : }
1277 :
1278 10264 : set16_fx( st->hb_prev_synth_buffer_fx, 0, tmps );
1279 : }
1280 146144 : ELSE IF( LT_16( tmps, st->old_bwe_delay ) )
1281 : {
1282 : /* the previous frame was TBE on top of ACELP@16kHz and the current frame is TBE on top of ACELP@12.8kHz */
1283 :
1284 140 : incr = idiv1616( ( L_FRAME * 2 ), add( shl( tmps, 1 ), 1 ) );
1285 140 : tmp16 = 0;
1286 140 : move16();
1287 536 : FOR( i = 0; i < tmps; i++ )
1288 : {
1289 :
1290 396 : tmp_buffer_fx[i] = round_fx_sat( L_mac_sat( L_mult( st->hb_prev_synth_buffer_fx[i], sin_table256_fx[255 - tmp16] ), st->hb_prev_synth_buffer_fx[st->old_bwe_delay - 1 - i], sin_table256_fx[tmp16] ) ); // Q0 (0+15-15)
1291 396 : move16();
1292 396 : tmp16 = add( tmp16, incr );
1293 : }
1294 140 : Copy( tmp_buffer_fx, st->hb_prev_synth_buffer_fx, tmps );
1295 : }
1296 146004 : ELSE IF( GT_16( tmps, st->old_bwe_delay ) )
1297 : {
1298 : /* the previous frame was TBE on top of ACELP@12.8kHz and the current frame is TBE on top of ACELP@16kHz */
1299 2419 : incr = idiv1616( imult1616( L_FRAME, 2 ), add( shl( st->old_bwe_delay, 1 ), 1 ) );
1300 2419 : tmp16 = 0;
1301 2419 : move16();
1302 3705 : FOR( i = 0; i < st->old_bwe_delay; i++ )
1303 : {
1304 1286 : tmp_buffer_fx[i] = mult_r( st->hb_prev_synth_buffer_fx[i], sin_table256_fx[255 - tmp16] ); // Q0(0+15-15)
1305 1286 : move16();
1306 1286 : tmp16 = add( tmp16, incr );
1307 : }
1308 16285 : FOR( ; i < tmps; i++ )
1309 : {
1310 13866 : tmp_buffer_fx[i] = 0;
1311 13866 : move16();
1312 : }
1313 2419 : tmp16 = 0;
1314 2419 : move16();
1315 3705 : FOR( i = 0; i < st->old_bwe_delay; i++ )
1316 : {
1317 1286 : tmp_buffer_fx[tmps - 1 - i] = round_fx( L_mac( L_mult( tmp_buffer_fx[tmps - 1 - i], 32767 ), st->hb_prev_synth_buffer_fx[st->old_bwe_delay - 1 - i], sin_table256_fx[tmp16 /*i * incr*/] ) ); // 32767-> (1.0f in Q15) -1
1318 1286 : move16();
1319 1286 : tmp16 = add( tmp16, incr );
1320 : }
1321 :
1322 2419 : Copy( tmp_buffer_fx, st->hb_prev_synth_buffer_fx, tmps );
1323 : }
1324 :
1325 156408 : test();
1326 156408 : test();
1327 156408 : test();
1328 156408 : IF( ( NE_16( st->element_mode, IVAS_CPE_TD ) && !use_cldfb_for_dft ) || ( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && tdm_LRTD_flag ) )
1329 144002 : {
1330 : /* Delay hb_synth */
1331 : Word32 hb_prev_synth_buffer_fx_32[111];
1332 :
1333 144002 : Copy_Scale_sig_16_32_DEPREC( st->hb_prev_synth_buffer_fx, hb_prev_synth_buffer_fx_32, 111, 11 ); // Q11
1334 144002 : delay_signal32_fx( hb_synth_32_fx[n], output_frame, hb_prev_synth_buffer_fx_32, tmps );
1335 144002 : Copy_Scale_sig_32_16( hb_prev_synth_buffer_fx_32, st->hb_prev_synth_buffer_fx, 111, -( 11 ) ); // Q0
1336 : }
1337 : ELSE
1338 : {
1339 12406 : Copy_Scale_sig_32_16( hb_synth_32_fx[n] + sub( output_frame, tmps ), st->hb_prev_synth_buffer_fx, tmps, -( Q11 ) ); // Q0
1340 : }
1341 :
1342 156408 : st->old_bwe_delay = tmps;
1343 156408 : move16();
1344 :
1345 : /* SWB CNG/DTX - calculate SHB energy */
1346 156408 : test();
1347 156408 : test();
1348 156408 : test();
1349 156408 : IF( GE_16( output_frame, L_FRAME32k ) && GT_16( st->extl, SWB_CNG ) && st->core == ACELP_CORE && st->hTdCngDec != NULL )
1350 : {
1351 : Word16 exp;
1352 : Word32 fra;
1353 :
1354 75796 : tmp16 = 34; /*ouput_frame == L_FRAME 48k: Q15*/
1355 75796 : move16();
1356 75796 : if ( EQ_16( output_frame, L_FRAME32k ) )
1357 : {
1358 25373 : tmp16 = 51; /*Q15*/
1359 25373 : move16();
1360 : }
1361 :
1362 75796 : L_tmp = L_deposit_l( 2 ); /*0.001 in Q11*/
1363 :
1364 : Word16 exp2;
1365 75796 : exp = 20;
1366 75796 : move16();
1367 75796 : Word32 L_tmp2 = sum2_32_fx( hb_synth_32_fx[n], output_frame, &exp );
1368 75796 : L_tmp = BASOP_Util_Add_Mant32Exp( L_tmp, 20, L_tmp2, exp, &exp );
1369 75796 : L_tmp = Mpy_32_16_1( L_tmp, tmp16 );
1370 75796 : exp2 = norm_l( L_tmp );
1371 75796 : exp = sub( exp, exp2 );
1372 75796 : fra = BASOP_Util_Log2( L_shl_sat( L_tmp, exp2 ) );
1373 75796 : exp2 = norm_l( exp );
1374 75796 : L_tmp = L_shl( exp, exp2 );
1375 75796 : exp = sub( Q31, exp2 );
1376 75796 : L_tmp = BASOP_Util_Add_Mant32Exp( fra, 6, L_tmp, exp, &exp );
1377 75796 : L_tmp = Mpy_32_16_1( L_tmp, 24660 );
1378 75796 : exp = add( exp, 2 );
1379 75796 : st->hTdCngDec->last_shb_ener_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 7 ) ) ); /*Q8*/
1380 75796 : move16();
1381 75796 : st->hTdCngDec->last_shb_ener_fx_32 = L_shl_sat( L_tmp, sub( exp, 20 ) ); /*Q11*/
1382 75796 : move32();
1383 : }
1384 : }
1385 :
1386 547314 : test();
1387 547314 : IF( sba_dirac_stereo_flag && NE_16( st->element_mode, IVAS_CPE_MDCT ) )
1388 : {
1389 : /* 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 */
1390 4511 : BREAK;
1391 : }
1392 :
1393 : /*----------------------------------------------------------------*
1394 : * Post-processing
1395 : * - TCX-LTP Postfilter (except DFT stereo)
1396 : * - core switching in DFT stereo
1397 : * - updates for potential TD->DFT stereo switching
1398 : *----------------------------------------------------------------*/
1399 :
1400 542803 : IF( st->hHQ_core != NULL )
1401 : {
1402 539052 : Copy_Scale_sig_16_32_no_sat( st->hHQ_core->old_out_LB_fx, st->hHQ_core->old_out_LB_fx32, L_FRAME32k, sub( Q11, st->hHQ_core->Q_old_wtda_LB ) ); // Q11
1403 539052 : Copy_Scale_sig_16_32_no_sat( st->hHQ_core->old_out_fx, st->hHQ_core->old_out_fx32, L_FRAME48k, sub( Q11, st->hHQ_core->Q_old_wtda ) ); // Q11
1404 : }
1405 :
1406 542803 : IF( NE_16( st->element_mode, IVAS_CPE_DFT ) )
1407 : {
1408 483756 : test();
1409 483756 : IF( NE_16( st->element_mode, IVAS_CPE_MDCT ) || sba_dirac_stereo_flag )
1410 : {
1411 321224 : ivas_post_proc_fx( hSCE, hCPE, n, synth_32_fx[n], NULL, output_frame, sba_dirac_stereo_flag, Q11 );
1412 : }
1413 :
1414 : /* update OLA buffers - needed for switching to DFT stereo */
1415 483756 : IF( !sba_dirac_stereo_flag )
1416 : {
1417 481080 : IF( hCPE != NULL )
1418 : {
1419 170034 : stereo_td2dft_update_fx( hCPE, n, output_32_fx[n], synth_32_fx[n], hb_synth_32_fx[n], output_frame );
1420 : }
1421 : }
1422 : }
1423 : ELSE /* IVAS_CPE_DFT */
1424 : {
1425 : Word16 q;
1426 59047 : q = 11;
1427 59047 : move16();
1428 59047 : IF( EQ_16( last_element_mode, IVAS_CPE_MDCT ) )
1429 : {
1430 660 : stereo_mdct2dft_update_fx( hCPE, output_32_fx[0], synth_32_fx[0] );
1431 : }
1432 59047 : Word16 q_DFT[2] = { 3, 3 };
1433 59047 : move16();
1434 59047 : move16();
1435 :
1436 59047 : stereo_dft_dec_core_switching_fx( hCPE, output_32_fx[0], synth_32_fx[0], hb_synth_32_fx[0], DFT_fx, output_frame, use_cldfb_for_dft, 0, &q, q_DFT );
1437 :
1438 59047 : hCPE->hStereoDft->q_dft = s_min( q_DFT[0], q_DFT[1] );
1439 59047 : move16();
1440 :
1441 59047 : IF( DFT_fx != NULL )
1442 : {
1443 42046 : Scale_sig32( DFT_fx[0], STEREO_DFT_BUF_MAX, sub( hCPE->hStereoDft->q_dft, q_DFT[0] ) ); // q_dft
1444 42046 : Scale_sig32( DFT_fx[1], STEREO_DFT_BUF_MAX, sub( hCPE->hStereoDft->q_dft, q_DFT[1] ) ); // q_dft
1445 : }
1446 :
1447 59047 : test();
1448 59047 : IF( EQ_16( hCPE->nchan_out, 1 ) && EQ_16( hCPE->hStereoDft->hConfig->res_cod_mode, STEREO_DFT_RES_COD_OFF ) )
1449 : {
1450 : /* mono output for non-residual coding modes uses CLDFB instead of DFT - requires DFT buffer update in case of bitrate switching */
1451 17001 : stereo_td2dft_update_fx( hCPE, n, output_32_fx[n], synth_32_fx[n], hb_synth_32_fx[n], output_frame );
1452 : }
1453 : }
1454 :
1455 542803 : Copy32( synth_32_fx[n], output_32_fx[n], output_frame );
1456 :
1457 : /*--------------------------------------------------------*
1458 : * Common updates
1459 : *--------------------------------------------------------*/
1460 :
1461 : Word16 exp_max;
1462 : Word32 output_fx_loc[L_FRAME48k];
1463 :
1464 542803 : exp_max = 0;
1465 542803 : move16();
1466 :
1467 : /*Scale Memories*/
1468 542803 : test();
1469 542803 : test();
1470 542803 : test();
1471 542803 : test();
1472 542803 : test();
1473 542803 : IF( ( EQ_16( st->codec_mode, MODE1 ) && st->hTcxDec != NULL ) && ( ( ( st->core == ACELP_CORE ) && !( EQ_16( st->bfi, 1 ) && EQ_16( st->con_tcx, 1 ) ) ) || EQ_16( st->core, HQ_CORE ) ) )
1474 : {
1475 169247 : Word16 exp_prev_synth_buffer = 0, exp_old_out = 0, exp_delay_buf_out = 0, exp_ouput = 0, exp_synth_history = 0;
1476 169247 : move16();
1477 169247 : move16();
1478 169247 : move16();
1479 169247 : move16();
1480 169247 : move16();
1481 :
1482 169247 : Copy_Scale_sig32_16( st->prev_synth_buffer32_fx, st->prev_synth_buffer_fx, NS2SA( 48000, DELAY_BWE_TOTAL_NS - DELAY_CLDFB_NS ), -( Q11 ) ); // Q0
1483 169247 : st->q_prev_synth_buffer_fx = 0;
1484 169247 : move16();
1485 :
1486 169247 : exp_ouput = Find_Max_Norm32( output_32_fx[n], output_frame );
1487 169247 : exp_ouput = add( exp_ouput, Q11 );
1488 169247 : exp_prev_synth_buffer = Find_Max_Norm16( st->prev_synth_buffer_fx, NS2SA( 48000, DELAY_BWE_TOTAL_NS - DELAY_CLDFB_NS ) );
1489 169247 : exp_prev_synth_buffer = add( exp_prev_synth_buffer, st->q_prev_synth_buffer_fx );
1490 169247 : exp_old_out = Find_Max_Norm16( st->hHQ_core->old_out_fx + NS2SA_FX2( st->output_Fs, N_ZERO_MDCT_NS ), sub( add( NS2SA_FX2( st->output_Fs, N_ZERO_MDCT_NS ), NS2SA_FX2( st->output_Fs, PH_ECU_LOOKAHEAD_NS ) ), NS2SA_FX2( st->output_Fs, N_ZERO_MDCT_NS ) ) );
1491 169247 : exp_delay_buf_out = Find_Max_Norm16( st->delay_buf_out_fx, NS2SA_FX2( st->output_Fs, DELAY_CLDFB_NS ) );
1492 169247 : exp_synth_history = Find_Max_Norm16( st->hTcxDec->synth_history_fx + output_frame, sub( add( sub( imult1616( 2, output_frame ), NS2SA_FX2( st->output_Fs, DELAY_CLDFB_NS ) ), NS2SA_FX2( st->output_Fs, PH_ECU_MEM_NS ) ), output_frame ) );
1493 169247 : exp_max = s_min( exp_synth_history, exp_ouput );
1494 169247 : exp_max = s_min( exp_max, exp_prev_synth_buffer );
1495 169247 : exp_max = s_min( exp_max, exp_old_out );
1496 169247 : exp_max = s_min( exp_max, exp_delay_buf_out );
1497 :
1498 169247 : Copy32( output_32_fx[n], output_fx_loc, output_frame );
1499 :
1500 169247 : Scale_sig32( output_fx_loc, output_frame, sub( exp_max, Q11 ) ); // Q(31-exp_max)
1501 169247 : Scale_sig( st->delay_buf_out_fx, NS2SA_FX2( st->output_Fs, DELAY_CLDFB_NS ), exp_max ); // exp_max
1502 169247 : Scale_sig( st->hHQ_core->old_out_fx + NS2SA_FX2( st->output_Fs, N_ZERO_MDCT_NS ), sub( NS2SA_FX2( st->output_Fs, PH_ECU_LOOKAHEAD_NS ), NS2SA_FX2( st->output_Fs, N_ZERO_MDCT_NS ) ), exp_max ); // Q(31-exp_max)
1503 169247 : Scale_sig( st->prev_synth_buffer_fx, NS2SA_FX2( 48000, DELAY_BWE_TOTAL_NS - DELAY_CLDFB_NS ), sub( exp_max, st->q_prev_synth_buffer_fx ) ); // Q(exp_max - prev_synth_buffer_fx)
1504 169247 : Scale_sig( st->hTcxDec->synth_history_fx + output_frame, sub( add( sub( imult1616( 2, output_frame ), NS2SA_FX2( st->output_Fs, DELAY_CLDFB_NS ) ), NS2SA_FX2( st->output_Fs, PH_ECU_MEM_NS ) ), output_frame ), sub( exp_max, 0 ) ); // Q(31-exp_max)
1505 :
1506 169247 : st->q_prev_synth_buffer_fx = sub( exp_max, st->q_prev_synth_buffer_fx );
1507 169247 : move16();
1508 : }
1509 :
1510 : /* Save synthesis for HQ FEC */
1511 542803 : save_synthesis_hq_fec_fx( st, NULL, output_fx_loc, output_frame, 0, hCPE );
1512 :
1513 : /* Updates */
1514 542803 : ivas_updt_dec_common_fx( st, NORMAL_HQ_CORE, -1, output_32_fx[n], Q11 );
1515 :
1516 542803 : Scale_sig( st->delay_buf_out_fx, NS2SA_FX2( st->output_Fs, DELAY_CLDFB_NS ), negate( exp_max ) ); // Q0
1517 :
1518 : } /* n_channels loop */
1519 :
1520 1008273 : FOR( n = 0; n < n_channels; n++ )
1521 : {
1522 547314 : st = sts[n];
1523 547314 : IF( st->cldfbAna )
1524 : {
1525 414380 : scale_sig32( st->cldfbAna->cldfb_state_fx, st->cldfbAna->cldfb_size, ( Q11 - Q10 ) ); // Q11
1526 414380 : st->cldfbAna->Q_cldfb_state = Q11;
1527 414380 : move16();
1528 : }
1529 547314 : IF( st->cldfbSynHB )
1530 : {
1531 2970 : scale_sig32( st->cldfbSynHB->cldfb_state_fx, st->cldfbSynHB->cldfb_size, ( Q11 - Q10 ) ); // Q11
1532 2970 : st->cldfbSynHB->Q_cldfb_state = Q11;
1533 2970 : move16();
1534 : }
1535 : }
1536 :
1537 460959 : pop_wmops();
1538 460959 : return error;
1539 : }
|