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 "cnst.h"
36 : #include "rom_com.h"
37 : #include "prot_fx.h"
38 : #include "ivas_prot_fx.h"
39 : #include "ivas_rom_com.h"
40 : #include "assert.h"
41 : #include "wmc_auto.h"
42 : #include <math.h>
43 : #include "ivas_prot_fx.h"
44 : #include "ivas_rom_com_fx.h"
45 :
46 : /*-------------------------------------------------------------------*
47 : * Local constants
48 : *-------------------------------------------------------------------*/
49 : #define DFT2TD_CORR_THRESH_FX 1932735283
50 :
51 : /*-------------------------------------------------------------------*
52 : * Function allocate_CoreCoder_TCX()
53 : *
54 : * Allocate CoreCoder TCX modules
55 : *-------------------------------------------------------------------*/
56 :
57 683 : static ivas_error allocate_CoreCoder_TCX_fx(
58 : DEC_CORE_HANDLE st /* i/o: Core decoder state structure */
59 : )
60 : {
61 683 : IF( st->hTcxDec == NULL )
62 : {
63 683 : IF( ( st->hTcxDec = (TCX_DEC_HANDLE) malloc( sizeof( TCX_DEC_DATA ) ) ) == NULL )
64 : {
65 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for hTcxDec" ) );
66 : }
67 :
68 683 : reset_tcx_overl_buf_fx( st->hTcxDec );
69 : // st->hTcxDec->CngLevelBackgroundTrace_bfi = 0; //initializing to avoid garbage overflow;
70 683 : set16_fx( st->hTcxDec->syn_OverlFB, 0, L_FRAME48k / 2 );
71 683 : set16_fx( st->hTcxDec->old_synth, 0, OLD_SYNTH_INTERNAL_DEC );
72 683 : st->hTcxDec->q_old_synth = 0;
73 683 : move16();
74 683 : set16_fx( st->hTcxDec->synth_history_fx, 0, L_PROT48k + L_FRAME48k );
75 : }
76 :
77 683 : IF( st->hTcxCfg == NULL )
78 : {
79 683 : IF( ( st->hTcxCfg = (TCX_CONFIG_HANDLE) malloc( sizeof( TCX_config ) ) ) == NULL )
80 : {
81 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for hTcxCfg" ) );
82 : }
83 : }
84 :
85 : /* allocated TCX-LTP structure for second channel */
86 683 : IF( st->hTcxLtpDec == NULL )
87 : {
88 683 : IF( ( st->hTcxLtpDec = (TCX_LTP_DEC_HANDLE) malloc( sizeof( TCX_LTP_DEC_DATA ) ) ) == NULL )
89 : {
90 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for TCX-LTP handle\n" ) );
91 : }
92 :
93 683 : tcxltp_dec_init_fx( st->hTcxLtpDec, 0, st->last_codec_mode, st->element_mode, st->pit_max, st->sr_core );
94 : }
95 :
96 : /* allocate HQ structure */
97 683 : IF( st->hHQ_core == NULL )
98 : {
99 683 : IF( ( st->hHQ_core = (HQ_DEC_HANDLE) malloc( sizeof( HQ_DEC_DATA ) ) ) == NULL )
100 : {
101 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for HQ core\n" ) );
102 : }
103 :
104 683 : HQ_core_dec_init_fx( st->hHQ_core );
105 : }
106 :
107 683 : IF( st->hIGFDec == NULL )
108 : {
109 683 : IF( ( st->hIGFDec = (IGF_DEC_INSTANCE_HANDLE) malloc( sizeof( IGFDEC_INSTANCE ) ) ) == NULL )
110 : {
111 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IGF\n" ) );
112 : }
113 :
114 683 : st->igf = 0;
115 683 : move16();
116 683 : init_igf_dec( st->hIGFDec );
117 : }
118 :
119 683 : IF( st->hTonalMDCTConc == NULL )
120 : {
121 683 : IF( ( st->hTonalMDCTConc = (TonalMDCTConcealPtr) malloc( sizeof( TonalMDCTConceal_INSTANCE ) ) ) == NULL )
122 : {
123 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for TonalMDCTConcealment\n" ) );
124 : }
125 : }
126 :
127 683 : st->last_con_tcx = 0;
128 683 : move16();
129 683 : st->hTonalMDCTConc->nSamples = 0;
130 683 : move16();
131 :
132 683 : return IVAS_ERR_OK;
133 : }
134 :
135 :
136 : /*-------------------------------------------------------------------*
137 : * Function allocate_CoreCoder()
138 : *
139 : * Allocate CoreCoder modules
140 : *-------------------------------------------------------------------*/
141 :
142 725 : static ivas_error allocate_CoreCoder_fx(
143 : DEC_CORE_HANDLE st /* i/o: Core decoder state structure */
144 : )
145 : {
146 : ivas_error error;
147 :
148 725 : error = IVAS_ERR_OK;
149 :
150 725 : IF( st->hGSCDec == NULL )
151 : {
152 725 : IF( ( st->hGSCDec = (GSC_DEC_HANDLE) malloc( sizeof( GSC_DEC_DATA ) ) ) == NULL )
153 : {
154 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for GSC\n" ) );
155 : }
156 :
157 725 : GSC_dec_init_ivas_fx( st->hGSCDec );
158 : }
159 :
160 725 : IF( st->hPFstat == NULL )
161 : {
162 725 : IF( ( st->hPFstat = (PFSTAT_HANDLE) malloc( sizeof( PFSTAT ) ) ) == NULL )
163 : {
164 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for NB/formant postflter\n" ) );
165 : }
166 :
167 725 : Init_post_filter_fx( st->hPFstat );
168 725 : st->psf_lp_noise_fx = 0;
169 725 : move16();
170 : }
171 :
172 725 : IF( st->hMusicPF == NULL )
173 : {
174 725 : IF( ( st->hMusicPF = (MUSIC_POSTFILT_HANDLE) malloc( sizeof( MUSIC_POSTFILT_DATA ) ) ) == NULL )
175 : {
176 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LD music postflter\n" ) );
177 : }
178 :
179 725 : music_postfilt_init( st->hMusicPF );
180 : }
181 :
182 725 : IF( st->hBPF == NULL )
183 : {
184 725 : IF( ( st->hBPF = (BPF_DEC_HANDLE) malloc( sizeof( BPF_DEC_DATA ) ) ) == NULL )
185 : {
186 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for BPF\n" ) );
187 : }
188 :
189 725 : bass_psfilter_init_fx( st->hBPF );
190 : }
191 :
192 725 : IF( st->hBWE_zero == NULL )
193 : {
194 725 : IF( ( st->hBWE_zero = (ZERO_BWE_DEC_HANDLE) malloc( sizeof( ZERO_BWE_DEC_DATA ) ) ) == NULL )
195 : {
196 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for zero BWE\n" ) );
197 : }
198 :
199 725 : hf_synth_init_fx( st->hBWE_zero );
200 725 : set16_fx( st->hBWE_zero->mem_hp400_fx, 0, 6 );
201 : }
202 :
203 725 : IF( st->cldfbAna == NULL )
204 : {
205 : /* open analysis for max. sampling rate 48kHz */
206 53 : IF( NE_32( ( error = openCldfb_ivas_fx( &st->cldfbAna, CLDFB_ANALYSIS, 48000, CLDFB_PROTOTYPE_1_25MS, DEC ) ), IVAS_ERR_OK ) )
207 : {
208 0 : return error;
209 : }
210 : }
211 :
212 725 : IF( st->cldfbBPF == NULL )
213 : {
214 : /* open analysis BPF for max. internal sampling rate 16kHz */
215 53 : IF( NE_32( ( error = openCldfb_ivas_fx( &st->cldfbBPF, CLDFB_ANALYSIS, 16000, CLDFB_PROTOTYPE_1_25MS, DEC ) ), IVAS_ERR_OK ) )
216 : {
217 0 : return error;
218 : }
219 : }
220 :
221 725 : return error;
222 : }
223 :
224 :
225 : /*-------------------------------------------------------------------*
226 : * Function deallocate_CoreCoder_TCX()
227 : *
228 : * Deallocate CoreCoder TCX modules
229 : *-------------------------------------------------------------------*/
230 :
231 773 : static void deallocate_CoreCoder_TCX(
232 : DEC_CORE_HANDLE st /* i/o: Core decoder state structure */
233 : )
234 : {
235 773 : IF( st->hTcxDec != NULL )
236 : {
237 667 : free( st->hTcxDec );
238 667 : st->hTcxDec = NULL;
239 : }
240 :
241 773 : IF( st->hTcxCfg != NULL )
242 : {
243 667 : free( st->hTcxCfg );
244 667 : st->hTcxCfg = NULL;
245 : }
246 :
247 773 : IF( st->hIGFDec != NULL )
248 : {
249 667 : free( st->hIGFDec );
250 667 : st->hIGFDec = NULL;
251 : }
252 :
253 773 : IF( st->hTonalMDCTConc != NULL )
254 : {
255 667 : free( st->hTonalMDCTConc );
256 667 : st->hTonalMDCTConc = NULL;
257 : }
258 :
259 773 : return;
260 : }
261 :
262 :
263 : /*-------------------------------------------------------------------*
264 : * Function deallocate_CoreCoder()
265 : *
266 : * Deallocate CoreCoder modules
267 : *-------------------------------------------------------------------*/
268 :
269 2081 : static void deallocate_CoreCoder_fx(
270 : DEC_CORE_HANDLE st /* i/o: Core decoder state structure */
271 : )
272 : {
273 2081 : IF( st->hGSCDec != NULL )
274 : {
275 740 : free( st->hGSCDec );
276 740 : st->hGSCDec = NULL;
277 : }
278 :
279 2081 : IF( st->hPFstat != NULL )
280 : {
281 740 : free( st->hPFstat );
282 740 : st->hPFstat = NULL;
283 : }
284 :
285 2081 : IF( st->hMusicPF != NULL )
286 : {
287 740 : free( st->hMusicPF );
288 740 : st->hMusicPF = NULL;
289 : }
290 :
291 2081 : IF( st->hBPF != NULL )
292 : {
293 740 : free( st->hBPF );
294 740 : st->hBPF = NULL;
295 : }
296 :
297 2081 : IF( st->hBWE_zero != NULL )
298 : {
299 740 : free( st->hBWE_zero );
300 740 : st->hBWE_zero = NULL;
301 : }
302 :
303 : /* CLDFB BPF & resampling tools */
304 2081 : IF( NE_16( st->element_mode, IVAS_CPE_MDCT ) )
305 : {
306 715 : deleteCldfb_ivas_fx( &st->cldfbAna ); /* delete analysis at max. sampling rate 48kHz */
307 715 : deleteCldfb_ivas_fx( &st->cldfbBPF ); /* delete analysis BPF at max. internal sampling rate 16kHz */
308 : }
309 :
310 2081 : IF( NE_16( st->element_mode, IVAS_CPE_MDCT ) )
311 : {
312 715 : deallocate_CoreCoder_TCX( st );
313 : }
314 :
315 2081 : return;
316 : }
317 :
318 : /*-------------------------------------------------------------------*
319 : * Function cpy_tcx_ltp_data()
320 : *
321 : * In case of MDCT<->DFT Stereo switching, copy TCX-LTP data of the right
322 : * channel to the correct structure in the new mode
323 : *-------------------------------------------------------------------*/
324 :
325 2022 : static void cpy_tcx_ltp_data_fx(
326 : TCX_LTP_DEC_HANDLE hTcxLtpDecOld, /* i : TCX-LTP structure to copy from */
327 : TCX_LTP_DEC_HANDLE hTcxLtpDecNew, /* o : TCX-LTP structure to copy to */
328 : const Word32 output_Fs /* i : output sampling rate Q0*/
329 : )
330 : {
331 : Word16 sz;
332 2022 : hTcxLtpDecNew->tcxltp_pitch_int_post_prev = hTcxLtpDecOld->tcxltp_pitch_int_post_prev; /* Q0 */
333 2022 : move16();
334 2022 : hTcxLtpDecNew->tcxltp_pitch_fr_post_prev = hTcxLtpDecOld->tcxltp_pitch_fr_post_prev; /* Q0 */
335 2022 : move16();
336 2022 : hTcxLtpDecNew->tcxltp_gain_post_prev = hTcxLtpDecOld->tcxltp_gain_post_prev; /* Qx */
337 2022 : move16();
338 2022 : hTcxLtpDecNew->tcxltp_filt_idx_prev = hTcxLtpDecOld->tcxltp_filt_idx_prev; /* Q0 */
339 2022 : move16();
340 : /* (int16_t) ( ( TCXLTP_MAX_DELAY * output_Fs ) / 48000 ) */
341 2022 : sz = extract_l( Mpy_32_32_r( TCXLTP_MAX_DELAY * output_Fs, 44739 /* 1 / 48000 in Q31 */ ) ); /* Q0 */
342 2022 : Copy( hTcxLtpDecOld->tcxltp_mem_in, hTcxLtpDecNew->tcxltp_mem_in, sz ); // TODO: One of these will be removed later /* exp(exp_tcxltp_mem_in) */
343 2022 : Copy32( hTcxLtpDecOld->tcxltp_mem_in_32, hTcxLtpDecNew->tcxltp_mem_in_32, sz ); /* exp(exp_tcxltp_mem_in) */
344 2022 : hTcxLtpDecNew->exp_tcxltp_mem_in = hTcxLtpDecOld->exp_tcxltp_mem_in; /* exp(exp_tcxltp_mem_in) */
345 2022 : move16();
346 : /* (int16_t) ( ( L_FRAME48k * output_Fs ) / 48000 ) */
347 2022 : sz = extract_l( Mpy_32_32_r( L_FRAME48k * output_Fs, 44739 /* 1 / 48000 in Q31 */ ) ); /* Q0 */
348 2022 : Copy( hTcxLtpDecOld->tcxltp_mem_out, hTcxLtpDecNew->tcxltp_mem_out, sz ); // TODO: One of these will be removed later /* exp(exp_tcxltp_mem_in) */
349 2022 : Copy32( hTcxLtpDecOld->tcxltp_mem_out_32, hTcxLtpDecNew->tcxltp_mem_out_32, sz ); /* exp(exp_tcxltp_mem_in) */
350 2022 : hTcxLtpDecNew->exp_tcxltp_mem_out = hTcxLtpDecOld->exp_tcxltp_mem_out; /* exp(exp_tcxltp_mem_in) */
351 2022 : move16();
352 :
353 2022 : return;
354 : }
355 :
356 :
357 : /*-------------------------------------------------------------------*
358 : * Function stereo_memory_dec()
359 : *
360 : * Dynamically allocate/deallocate data structures depending on the actual CPE mode
361 : *-------------------------------------------------------------------*/
362 386540 : ivas_error stereo_memory_dec_fx(
363 : const Word32 ivas_total_brate, /* i : IVAS total bitrate Q0*/
364 : CPE_DEC_HANDLE hCPE, /* i : CPE decoder structure */
365 : const Word16 nb_bits_metadata, /* i : number of metadata bits Q0*/
366 : const Word32 output_Fs, /* i : output sampling rate Q0*/
367 : const IVAS_FORMAT ivas_format, /* i : IVAS format */
368 : const MC_MODE mc_mode, /* i : MC mode */
369 : const Word16 nchan_transport /* i : number of transport channels Q0*/
370 : )
371 : {
372 : DEC_CORE_HANDLE st;
373 : Word16 i, n, delay_comp_DFT;
374 : ivas_error error;
375 : Word16 div_m, div_e, tmp16;
376 :
377 386540 : error = IVAS_ERR_OK;
378 386540 : move32();
379 :
380 386540 : assert( GE_16( hCPE->last_element_mode, IVAS_CPE_DFT ) && "Switching from SCE to CPE is not a valid configuration!" );
381 :
382 :
383 386540 : hCPE->hCoreCoder[0]->element_mode = hCPE->element_mode; /* Q0 */
384 386540 : move16();
385 386540 : hCPE->hCoreCoder[1]->element_mode = hCPE->element_mode; /* Q0 */
386 386540 : move16();
387 :
388 : /*--------------------------------------------------------------*
389 : * stereo switching (using parameters that will be freed)
390 : *---------------------------------------------------------------*/
391 :
392 : /* handling of DFT->TD switching */
393 386540 : test();
394 386540 : test();
395 386540 : IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) && ( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) || EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) ) )
396 : {
397 732 : delay_comp_DFT = NS2SA_FX2( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS ); /* Q0 */
398 732 : move16();
399 :
400 : /* redressing of the DFT OLA part */
401 2196 : FOR( n = 0; n < CPE_CHANNELS; n++ )
402 : {
403 183960 : FOR( i = delay_comp_DFT; i < hCPE->hStereoDft->dft32ms_ovl; i++ )
404 : {
405 182496 : div_m = BASOP_Util_Divide3216_Scale( hCPE->output_mem_fx[n][i - delay_comp_DFT], hCPE->hStereoDft->win32ms_fx[STEREO_DFT32MS_STEP * ( hCPE->hStereoDft->dft32ms_ovl - 1 + delay_comp_DFT - i )], &div_e );
406 182496 : hCPE->output_mem_fx[n][i - delay_comp_DFT] = L_shl( div_m, add( div_e, 16 ) ); /* div_e + Q16 */
407 182496 : move32();
408 : // hCPE->output_mem_fx[n][sub(i, delay_comp_DFT)] = hCPE->hStereoDft->win32ms_fx[i_mult(STEREO_DFT32MS_STEP, ( sub(add(sub(hCPE->hStereoDft->dft32ms_ovl, 1), delay_comp_DFT), i) ))];
409 : }
410 : }
411 :
412 732 : IF( hCPE->hCoreCoder[0]->last_core != ACELP_CORE )
413 : {
414 189 : Copy32( hCPE->hStereoDft->buff_LBTCX_mem_fx, hCPE->input_mem_LB_fx[0], NS2SA_FX2( i_mult( hCPE->hCoreCoder[0]->last_L_frame, FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ) ); /* Q11 */
415 : }
416 : }
417 :
418 386540 : test();
419 386540 : IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) && EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
420 : {
421 660 : v_add_32( hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, hCPE->hCoreCoder[1]->hHQ_core->old_out_fx32, hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, extract_l( Mpy_32_16_1( output_Fs, INV_FRAME_PER_SEC_Q15 ) ) ); /* exp(exp_old_out) */
422 660 : v_multc_fixed_16( hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, 16384 /* 0.5 in Q15 */, hCPE->hCoreCoder[0]->hHQ_core->old_out_fx32, extract_l( Mpy_32_16_1( output_Fs, INV_FRAME_PER_SEC_Q15 ) ) ); /* exp(exp_old_out) */
423 :
424 660 : v_add_32( hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, hCPE->hCoreCoder[1]->hHQ_core->old_out_LB_fx32, hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, L_FRAME32k ); /* q_old_outLB_fx */
425 660 : v_multc_fixed_16( hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, 16384 /* 0.5 in Q15 */, hCPE->hCoreCoder[0]->hHQ_core->old_out_LB_fx32, L_FRAME32k ); /* exp(exp_old_out) */
426 : }
427 :
428 : /*--------------------------------------------------------------*
429 : * allocate/deallocate data structures
430 : *---------------------------------------------------------------*/
431 :
432 386540 : IF( NE_16( hCPE->element_mode, hCPE->last_element_mode ) )
433 : {
434 : /*--------------------------------------------------------------*
435 : * switching CPE mode to DFT stereo
436 : *---------------------------------------------------------------*/
437 :
438 1456 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
439 : {
440 : /* deallocate data structure of the previous CPE mode */
441 715 : IF( hCPE->hStereoTD != NULL )
442 : {
443 55 : free( hCPE->hStereoTD );
444 55 : hCPE->hStereoTD = NULL;
445 : }
446 :
447 715 : IF( hCPE->hStereoMdct != NULL )
448 : {
449 660 : free( hCPE->hStereoMdct );
450 660 : hCPE->hStereoMdct = NULL;
451 : }
452 :
453 : /* deallocate secondary channel */
454 715 : deallocate_CoreCoder_fx( hCPE->hCoreCoder[1] );
455 :
456 : /* allocate DFT stereo data structure */
457 715 : IF( NE_32( ( error = stereo_dft_dec_create_fx( &( hCPE->hStereoDft ), hCPE->element_brate, output_Fs, 0, nchan_transport ) ), IVAS_ERR_OK ) )
458 : {
459 0 : return error;
460 : }
461 :
462 715 : IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
463 : {
464 660 : cpy_tcx_ltp_data_fx( hCPE->hCoreCoder[1]->hTcxLtpDec, hCPE->hStereoDft->hTcxLtpDec, output_Fs );
465 660 : deleteFdCngDec_fx( &hCPE->hCoreCoder[1]->hFdCngDec );
466 : }
467 :
468 : /* memory update - needed in TD stereo, TCX/HQ frame -> DFT stereo, ACELP frame switching */
469 715 : Copy32( hCPE->input_mem_LB_fx[0], hCPE->hStereoDft->buff_LBTCX_mem_fx, NS2SA_FX2( s_min( i_mult( hCPE->hCoreCoder[0]->last_L_frame, FRAMES_PER_SEC ), 16000 ), STEREO_DFT32MS_OVL_NS ) ); /* Q11 */
470 :
471 : /* allocate ICBWE structure */
472 715 : IF( hCPE->hStereoICBWE == NULL )
473 : {
474 683 : IF( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
475 : {
476 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
477 : }
478 :
479 683 : stereo_icBWE_init_dec_fx( hCPE->hStereoICBWE );
480 : }
481 :
482 : /* allocate HQ core */
483 715 : st = hCPE->hCoreCoder[0];
484 715 : IF( st->hHQ_core == NULL )
485 : {
486 0 : IF( ( st->hHQ_core = (HQ_DEC_HANDLE) malloc( sizeof( HQ_DEC_DATA ) ) ) == NULL )
487 : {
488 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HQ core\n" ) );
489 : }
490 :
491 0 : HQ_core_dec_init_fx( st->hHQ_core );
492 : }
493 :
494 : /* allocate TD CNG handle */
495 715 : IF( EQ_16( st->idchan, 0 ) && st->hTdCngDec == NULL )
496 : {
497 2 : IF( ( st->hTdCngDec = (TD_CNG_DEC_HANDLE) malloc( sizeof( TD_CNG_DEC_DATA ) ) ) == NULL )
498 : {
499 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DTX/TD CNG\n" ) );
500 : }
501 :
502 2 : td_cng_dec_init_fx( st );
503 : }
504 : }
505 :
506 : /*--------------------------------------------------------------*
507 : * switching CPE mode to TD stereo
508 : *---------------------------------------------------------------*/
509 :
510 1456 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
511 : {
512 : /* deallocate data structure of the previous CPE mode */
513 58 : IF( hCPE->hStereoDft != NULL )
514 : {
515 51 : stereo_dft_dec_destroy( &( hCPE->hStereoDft ) );
516 51 : hCPE->hStereoDft = NULL;
517 : }
518 :
519 58 : IF( hCPE->hStereoMdct != NULL )
520 : {
521 7 : free( hCPE->hStereoMdct );
522 7 : hCPE->hStereoMdct = NULL;
523 : }
524 :
525 : /* deallocated TCX/IGF structures for second channel */
526 58 : deallocate_CoreCoder_TCX( hCPE->hCoreCoder[1] );
527 :
528 58 : IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
529 : {
530 7 : deleteFdCngDec_fx( &hCPE->hCoreCoder[1]->hFdCngDec );
531 : }
532 :
533 : /* allocate TD stereo data structure */
534 58 : IF( hCPE->hStereoTD != NULL )
535 : {
536 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: TD Stereo memory already allocated\n" );
537 : }
538 :
539 58 : IF( ( hCPE->hStereoTD = (STEREO_TD_DEC_DATA_HANDLE) malloc( sizeof( STEREO_TD_DEC_DATA ) ) ) == NULL )
540 : {
541 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
542 : }
543 :
544 58 : stereo_td_init_dec_fx( hCPE->hStereoTD, hCPE->last_element_mode );
545 :
546 : /* allocate CoreCoder secondary channel */
547 58 : IF( NE_32( ( error = allocate_CoreCoder_fx( hCPE->hCoreCoder[1] ) ), IVAS_ERR_OK ) )
548 : {
549 0 : return error;
550 : }
551 : }
552 :
553 : /*--------------------------------------------------------------*
554 : * allocate DFT/TD stereo structures after MDCT stereo frame
555 : *---------------------------------------------------------------*/
556 :
557 1456 : test();
558 1456 : test();
559 1456 : IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) && ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->element_mode, IVAS_CPE_TD ) ) )
560 : {
561 : /* deallocated TCX-LTP for second channel */
562 667 : st = hCPE->hCoreCoder[1];
563 667 : IF( st->hTcxLtpDec != NULL )
564 : {
565 667 : free( st->hTcxLtpDec );
566 667 : st->hTcxLtpDec = NULL;
567 : }
568 :
569 667 : IF( EQ_16( st->element_mode, IVAS_CPE_TD ) )
570 : {
571 : /* re-use an existing buffer for MDCT->TD stereo switching */
572 : Word16 nZeros, len;
573 7 : nZeros = extract_l( NS2SA_FX2( st->output_Fs, N_ZERO_MDCT_NS ) ); /* Q0 */
574 7 : move16();
575 7 : len = NS2SA_FX2( st->output_Fs, 3000000 ); /* Q0 */
576 7 : move16();
577 7 : Copy32( st->hHQ_core->old_out_fx32 + nZeros, hCPE->output_mem_fx[1], len ); /* exp(exp_old_out) */
578 : }
579 :
580 : /* deallocated HQ-core for second channel */
581 667 : IF( st->hHQ_core != NULL )
582 : {
583 667 : free( st->hHQ_core );
584 667 : st->hHQ_core = NULL;
585 : }
586 :
587 : /* allocate DFT stereo mono DMX data structure */
588 667 : test();
589 667 : IF( EQ_16( hCPE->nchan_out, 1 ) && hCPE->hStereoDftDmx == NULL )
590 : {
591 398 : IF( ( hCPE->hStereoDftDmx = (STEREO_DFT_DMX_DATA_HANDLE) malloc( sizeof( STEREO_DFT_DMX_DATA ) ) ) == NULL )
592 : {
593 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo DFT mono output\n" ) );
594 : }
595 :
596 398 : stereo_dft_dmx_out_reset_fx( hCPE->hStereoDftDmx );
597 : }
598 :
599 : /* allocate TCA data structure */
600 667 : test();
601 667 : IF( NE_16( hCPE->nchan_out, 1 ) && hCPE->hStereoTCA == NULL )
602 : {
603 258 : IF( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
604 : {
605 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
606 : }
607 :
608 258 : stereo_tca_init_dec_fx( hCPE->hStereoTCA );
609 : }
610 :
611 667 : st = hCPE->hCoreCoder[0];
612 :
613 : /* allocate primary channel substructures */
614 667 : IF( NE_32( ( error = allocate_CoreCoder_fx( st ) ), IVAS_ERR_OK ) )
615 : {
616 0 : return error;
617 : }
618 :
619 : /* allocate BWEs for primary channel */
620 667 : IF( st->hBWE_TD == NULL )
621 : {
622 2 : IF( ( st->hBWE_TD = (TD_BWE_DEC_HANDLE) malloc( sizeof( TD_BWE_DEC_DATA ) ) ) == NULL )
623 : {
624 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD BWE\n" ) );
625 : }
626 :
627 2 : td_bwe_dec_init_fx( st->hBWE_TD, -1, st->output_Fs );
628 :
629 2 : st->prev_Q_bwe_exc = 31;
630 2 : move16();
631 :
632 2 : IF( ( st->hBWE_FD = (FD_BWE_DEC_HANDLE) malloc( sizeof( FD_BWE_DEC_DATA ) ) ) == NULL )
633 : {
634 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FD BWE\n" ) );
635 : }
636 :
637 2 : fd_bwe_dec_init_fx( st->hBWE_FD );
638 : }
639 :
640 : /* Allocated FD_CNG instance for primary channel*/
641 667 : IF( st->hFdCngDec == NULL )
642 : {
643 : /* Create FD_CNG instance */
644 0 : IF( NE_32( ( error = createFdCngDec_fx( &st->hFdCngDec ) ), IVAS_ERR_OK ) )
645 : {
646 0 : return error;
647 : }
648 :
649 0 : initFdCngDec_ivas_fx( st, st->cldfbSyn->scale );
650 :
651 0 : configureFdCngDec_ivas_fx( st->hFdCngDec, st->bwidth, st->total_brate, st->L_frame, st->last_L_frame, st->element_mode );
652 : }
653 :
654 : /* allocate stereo CNG structure */
655 667 : IF( hCPE->hStereoCng == NULL )
656 : {
657 667 : IF( ( hCPE->hStereoCng = (STEREO_CNG_DEC_HANDLE) malloc( sizeof( STEREO_CNG_DEC ) ) ) == NULL )
658 : {
659 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo CNG\n" ) );
660 : }
661 667 : stereo_cng_init_dec_fx( hCPE->hStereoCng, &st->hFdCngDec->hFdCngCom->frameSize );
662 : }
663 : }
664 :
665 : /*--------------------------------------------------------------*
666 : * switching CPE mode to MDCT stereo
667 : *---------------------------------------------------------------*/
668 :
669 1456 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
670 : {
671 : Word32 tmpF_buff[L_FRAME16k / 2];
672 : TCX_LTP_DEC_DATA tcxLtpTmp;
673 :
674 683 : IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_TD ) )
675 : {
676 2 : Copy32( hCPE->hStereoTD->TCX_old_syn_Overl_fx, tmpF_buff, L_FRAME16k / 2 ); /* Q11 */
677 : }
678 :
679 683 : IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
680 : {
681 681 : cpy_tcx_ltp_data_fx( hCPE->hStereoDft->hTcxLtpDec, &tcxLtpTmp, output_Fs );
682 : }
683 :
684 : /* deallocate data structure of the previous CPE mode */
685 683 : IF( hCPE->hStereoDft != NULL )
686 : {
687 681 : stereo_dft_dec_destroy( &( hCPE->hStereoDft ) );
688 681 : hCPE->hStereoDft = NULL;
689 : }
690 :
691 683 : IF( hCPE->hStereoTD != NULL )
692 : {
693 2 : free( hCPE->hStereoTD );
694 2 : hCPE->hStereoTD = NULL;
695 : }
696 :
697 683 : IF( hCPE->hStereoDftDmx != NULL )
698 : {
699 421 : free( hCPE->hStereoDftDmx );
700 421 : hCPE->hStereoDftDmx = NULL;
701 : }
702 :
703 683 : IF( hCPE->hStereoICBWE != NULL )
704 : {
705 668 : free( hCPE->hStereoICBWE );
706 668 : hCPE->hStereoICBWE = NULL;
707 : }
708 :
709 : /* de-allocate stereo CNG structure */
710 683 : IF( hCPE->hStereoCng != NULL )
711 : {
712 683 : free( hCPE->hStereoCng );
713 683 : hCPE->hStereoCng = NULL;
714 : }
715 :
716 2049 : FOR( i = 0; i < CPE_CHANNELS; i++ )
717 : {
718 1366 : st = hCPE->hCoreCoder[i];
719 1366 : st->element_mode = hCPE->element_mode; /* Q0 */
720 1366 : move16();
721 :
722 : /* deallocate core-decoder substructures */
723 1366 : deallocate_CoreCoder_fx( st );
724 :
725 1366 : st->first_CNG = 0;
726 1366 : move16();
727 : }
728 :
729 : /* allocate CLDFB structures for second channel */
730 683 : st = hCPE->hCoreCoder[1];
731 :
732 683 : IF( st->cldfbAna == NULL )
733 : {
734 681 : IF( NE_32( ( error = openCldfb_ivas_fx( &st->cldfbAna, CLDFB_ANALYSIS, 48000, CLDFB_PROTOTYPE_1_25MS, DEC ) ), IVAS_ERR_OK ) )
735 : {
736 0 : return error;
737 : }
738 : }
739 :
740 683 : IF( st->cldfbBPF == NULL )
741 : {
742 : /* open analysis BPF for max. internal sampling rate 16kHz */
743 681 : IF( NE_32( ( error = openCldfb_ivas_fx( &st->cldfbBPF, CLDFB_ANALYSIS, 16000, CLDFB_PROTOTYPE_1_25MS, DEC ) ), IVAS_ERR_OK ) )
744 : {
745 0 : return error;
746 : }
747 : }
748 :
749 : /* allocate Fd-Cng structure for second channel */
750 683 : IF( NE_32( ( error = createFdCngDec_fx( &st->hFdCngDec ) ), IVAS_ERR_OK ) )
751 : {
752 0 : return error;
753 : }
754 :
755 : /* Init FD-CNG */
756 683 : initFdCngDec_ivas_fx( st, st->cldfbSyn->scale );
757 :
758 683 : if ( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
759 : {
760 681 : st->last_core = ACELP_CORE; /* needed to set-up TCX core in SetTCXModeInfo() Q0*/
761 681 : move16();
762 : }
763 :
764 : /*Allocate CoreCoder TCX modules for second channel */
765 683 : IF( NE_32( ( error = allocate_CoreCoder_TCX_fx( hCPE->hCoreCoder[1] ) ), IVAS_ERR_OK ) )
766 : {
767 0 : return error;
768 : }
769 :
770 683 : IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
771 : {
772 681 : IF( EQ_16( hCPE->nchan_out, 1 ) )
773 : {
774 17 : cpy_tcx_ltp_data_fx( hCPE->hCoreCoder[0]->hTcxLtpDec, hCPE->hCoreCoder[1]->hTcxLtpDec, output_Fs );
775 : }
776 : ELSE
777 : {
778 664 : cpy_tcx_ltp_data_fx( &tcxLtpTmp, hCPE->hCoreCoder[1]->hTcxLtpDec, output_Fs );
779 : }
780 : }
781 :
782 683 : IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_TD ) )
783 : {
784 2 : Copy32( tmpF_buff, st->hTcxDec->old_syn_Overl_32, L_FRAME16k / 2 ); /* Q11 */
785 2 : Copy_Scale_sig32_16( st->hTcxDec->old_syn_Overl_32, st->hTcxDec->old_syn_Overl, L_FRAME16k / 2, add( st->Q_syn, 5 ) ); //(st->Qsyn - (11 - 16))
786 2 : st->hTcxDec->Q_old_syn_Overl = st->Q_syn;
787 : }
788 683 : st->hTcxDec->q_old_synth = st->Q_syn;
789 683 : move16();
790 683 : set16_fx( st->hTcxDec->FBTCXdelayBuf, 0, 111 );
791 683 : set32_fx( st->hTcxDec->FBTCXdelayBuf_32, 0, 111 );
792 683 : st->hTcxDec->old_synthFB_fx = st->hTcxDec->synth_history_fx + NS2SA_FX2( st->output_Fs, PH_ECU_MEM_NS ); /* q_synth_history_fx */
793 683 : st->hTcxDec->prev_good_synth_fx = st->hTcxDec->old_synthFB_fx + NS2SA_FX2( st->output_Fs, PH_ECU_LOOKAHEAD_NS ); /* q_synth_history_fx */
794 :
795 : /* allocate and initialize MDCT stereo structure */
796 683 : IF( ( hCPE->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_DEC_DATA ) ) ) == NULL )
797 : {
798 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
799 : }
800 683 : IF( EQ_16( ivas_format, STEREO_FORMAT ) && hCPE->element_brate <= MAX_MDCT_ITD_BRATE )
801 : {
802 16 : hCPE->hStereoMdct->use_itd = 1; /* Q0 */
803 16 : move16();
804 : }
805 : ELSE
806 : {
807 667 : hCPE->hStereoMdct->use_itd = 0;
808 667 : move16();
809 : }
810 683 : hCPE->hStereoMdct->reverse_dmx = 0;
811 683 : move16();
812 683 : hCPE->hStereoMdct->smooth_ratio_fx = ONE_IN_Q26; /* Q26 */
813 683 : move32();
814 : }
815 : }
816 :
817 : /*--------------------------------------------------------------*
818 : * normal TD / LRTD switching
819 : *---------------------------------------------------------------*/
820 :
821 386540 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
822 : {
823 3751 : IF( hCPE->hCoreCoder[0]->bfi == 0 )
824 : {
825 3695 : st = hCPE->hCoreCoder[1];
826 3695 : IF( hCPE->brate_surplus < 0 )
827 : {
828 0 : tmp16 = extract_l( L_negate( Mpy_32_32( L_abs( hCPE->brate_surplus ), ONE_BY_FRAMES_PER_SEC_Q31 ) ) ); /* Q0 */
829 : }
830 : ELSE
831 : {
832 3695 : tmp16 = extract_l( Mpy_32_32( hCPE->brate_surplus, ONE_BY_FRAMES_PER_SEC_Q31 ) ); /* Q0 */
833 : }
834 :
835 3695 : hCPE->hStereoTD->tdm_LRTD_flag = get_indice_st( hCPE->hCoreCoder[0], L_add( hCPE->element_brate, hCPE->brate_surplus ), ( sub( add( sub( extract_l( Mpy_32_16_1( hCPE->element_brate, INV_FRAME_PER_SEC_Q15 ) ), nb_bits_metadata ), tmp16 ), ( TDM_SECONDARY_SIGNALLING + TDM_RATIO_BITS + TDM_LP_REUSE_BITS + TDM_LR_CONTENT_BITS ) ) ), TDM_LR_CONTENT_BITS ); /* Q0 */
836 3695 : move16();
837 :
838 3695 : IF( hCPE->hStereoTD->tdm_LRTD_flag != 0 )
839 : {
840 : /* deallocate ICBWE structure */
841 3569 : IF( hCPE->hStereoICBWE != NULL )
842 : {
843 39 : free( hCPE->hStereoICBWE );
844 39 : hCPE->hStereoICBWE = NULL;
845 : }
846 :
847 : /* allocate BWEs for secondary channel */
848 3569 : IF( st->hBWE_TD == NULL )
849 : {
850 41 : IF( ( st->hBWE_TD = (TD_BWE_DEC_HANDLE) malloc( sizeof( TD_BWE_DEC_DATA ) ) ) == NULL )
851 : {
852 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD BWE\n" ) );
853 : }
854 :
855 41 : td_bwe_dec_init_fx( st->hBWE_TD, -1, st->output_Fs );
856 :
857 41 : st->prev_Q_bwe_exc = 31;
858 41 : move16();
859 :
860 41 : IF( ( st->hBWE_FD = (FD_BWE_DEC_HANDLE) malloc( sizeof( FD_BWE_DEC_DATA ) ) ) == NULL )
861 : {
862 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FD BWE\n" ) );
863 : }
864 :
865 41 : fd_bwe_dec_init_fx( st->hBWE_FD );
866 : }
867 : }
868 : ELSE /* tdm_LRTD_flag == 0 */
869 : {
870 : /* deallocate BWEs for secondary channel */
871 126 : IF( st->hBWE_TD != NULL )
872 : {
873 33 : IF( st->hBWE_TD != NULL )
874 : {
875 33 : free( st->hBWE_TD );
876 33 : st->hBWE_TD = NULL;
877 : }
878 :
879 33 : IF( st->hBWE_FD != NULL )
880 : {
881 33 : free( st->hBWE_FD );
882 33 : st->hBWE_FD = NULL;
883 : }
884 : }
885 :
886 : /* allocate ICBWE structure */
887 126 : IF( hCPE->hStereoICBWE == NULL )
888 : {
889 33 : IF( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
890 : {
891 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
892 : }
893 :
894 33 : stereo_icBWE_init_dec_fx( hCPE->hStereoICBWE );
895 : }
896 : }
897 : }
898 : }
899 :
900 : /*--------------------------------------------------------------*
901 : * MDCT stereo bitrate switching
902 : *---------------------------------------------------------------*/
903 :
904 386540 : test();
905 386540 : IF( EQ_16( ivas_format, STEREO_FORMAT ) && EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
906 : {
907 20451 : test();
908 20451 : IF( LE_32( hCPE->element_brate, MAX_MDCT_ITD_BRATE ) && GT_32( ivas_total_brate, IVAS_SID_5k2 ) )
909 : {
910 11551 : IF( hCPE->hStereoMdct->use_itd == 0 )
911 : {
912 19 : IF( hCPE->hStereoTCA == NULL )
913 : {
914 : /* allocate TCA data structure */
915 10 : IF( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
916 : {
917 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
918 : }
919 : }
920 :
921 19 : stereo_tca_init_dec_fx( hCPE->hStereoTCA );
922 : }
923 :
924 11551 : hCPE->hStereoMdct->use_itd = 1;
925 11551 : move16();
926 : }
927 : ELSE
928 : {
929 : /* de-allocate TCA data structure */
930 8900 : test();
931 8900 : test();
932 8900 : test();
933 8900 : IF( EQ_16( hCPE->hStereoMdct->use_itd, 1 ) && GT_32( ivas_total_brate, IVAS_SID_5k2 ) && hCPE->hStereoTCA != NULL )
934 : {
935 16 : free( hCPE->hStereoTCA );
936 16 : hCPE->hStereoTCA = NULL;
937 16 : hCPE->hStereoMdct->use_itd = 0;
938 16 : move16();
939 : }
940 8884 : ELSE IF( EQ_16( hCPE->hStereoMdct->use_itd, 1 ) && LE_32( ivas_total_brate, IVAS_SID_5k2 ) )
941 : {
942 2920 : hCPE->hStereoMdct->itd_fx = 0;
943 2920 : move32();
944 : }
945 : ELSE
946 : {
947 5964 : hCPE->hStereoMdct->use_itd = 0;
948 5964 : move16();
949 : }
950 : }
951 : }
952 :
953 : /*--------------------------------------------------------------*
954 : * Bitrate switching in MASA format
955 : *---------------------------------------------------------------*/
956 :
957 386540 : test();
958 386540 : test();
959 386540 : IF( ( EQ_16( ivas_format, MASA_FORMAT ) || EQ_16( ivas_format, MASA_ISM_FORMAT ) ) && EQ_16( nchan_transport, 2 ) )
960 : {
961 24826 : IF( EQ_16( hCPE->nchan_out, 1 ) )
962 : {
963 5793 : IF( hCPE->hStereoDftDmx == NULL )
964 : {
965 122 : IF( ( hCPE->hStereoDftDmx = (STEREO_DFT_DMX_DATA_HANDLE) malloc( sizeof( STEREO_DFT_DMX_DATA ) ) ) == NULL )
966 : {
967 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo DFT mono output\n" ) );
968 : }
969 :
970 122 : stereo_dft_dmx_out_reset_fx( hCPE->hStereoDftDmx );
971 : }
972 :
973 5793 : IF( hCPE->prev_synth_chs_fx[1] != NULL )
974 : {
975 503 : free( hCPE->prev_synth_chs_fx[1] );
976 503 : hCPE->prev_synth_chs_fx[1] = NULL;
977 : }
978 :
979 5793 : IF( hCPE->hStereoTCA != NULL )
980 : {
981 122 : free( hCPE->hStereoTCA );
982 122 : hCPE->hStereoTCA = NULL;
983 : }
984 : }
985 : ELSE /* nchan_out == 2 */
986 : {
987 19033 : IF( hCPE->hStereoDftDmx != NULL )
988 : {
989 113 : free( hCPE->hStereoDftDmx );
990 113 : hCPE->hStereoDftDmx = NULL;
991 : }
992 :
993 19033 : IF( hCPE->prev_synth_chs_fx[1] == NULL )
994 : {
995 516 : st = hCPE->hCoreCoder[1];
996 516 : IF( ( hCPE->prev_synth_chs_fx[1] = (Word32 *) malloc( sizeof( Word32 ) * NS2SA_FX2( st->output_Fs, FRAME_SIZE_NS ) ) ) == NULL )
997 : {
998 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
999 : }
1000 516 : set32_fx( hCPE->prev_synth_chs_fx[1], 0, NS2SA_FX2( st->output_Fs, FRAME_SIZE_NS ) );
1001 : }
1002 :
1003 19033 : IF( hCPE->hStereoICBWE == NULL && EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
1004 : {
1005 5 : IF( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
1006 : {
1007 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
1008 : }
1009 :
1010 5 : stereo_icBWE_init_dec_fx( hCPE->hStereoICBWE );
1011 : }
1012 :
1013 19033 : IF( hCPE->hStereoTCA == NULL && ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->element_mode, IVAS_CPE_TD ) ) )
1014 : {
1015 113 : IF( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
1016 : {
1017 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
1018 : }
1019 :
1020 113 : stereo_tca_init_dec_fx( hCPE->hStereoTCA );
1021 : }
1022 :
1023 19033 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1024 : {
1025 13009 : IF( hCPE->hStereoTCA != NULL )
1026 : {
1027 : /* note: in MASA, hCPE->hStereoMdct->itd = 0 */
1028 244 : free( hCPE->hStereoTCA );
1029 244 : hCPE->hStereoTCA = NULL;
1030 : }
1031 :
1032 13009 : IF( hCPE->hStereoICBWE != NULL )
1033 : {
1034 0 : free( hCPE->hStereoICBWE );
1035 0 : hCPE->hStereoICBWE = NULL;
1036 : }
1037 : }
1038 : }
1039 : }
1040 :
1041 : /*--------------------------------------------------------------*
1042 : * Bitrate switching in MASA format
1043 : *---------------------------------------------------------------*/
1044 :
1045 386540 : test();
1046 386540 : IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1047 : {
1048 91750 : test();
1049 91750 : IF( EQ_16( mc_mode, MC_MODE_MCT ) || EQ_16( mc_mode, MC_MODE_PARAMUPMIX ) )
1050 : {
1051 : /* deallocate the FdCNG handle */
1052 245970 : FOR( i = 0; i < CPE_CHANNELS; ++i )
1053 : {
1054 163980 : deleteFdCngDec_fx( &hCPE->hCoreCoder[i]->hFdCngDec );
1055 : }
1056 : }
1057 : ELSE
1058 : {
1059 : /* allocate the FdCNG handle (for noise estimation for TCX PLC fadeout)*/
1060 29280 : FOR( i = 0; i < CPE_CHANNELS; ++i )
1061 : {
1062 19520 : IF( hCPE->hCoreCoder[i]->cldfbSyn == NULL ) /* could be NULL when we had the MCT LFE channel */
1063 : {
1064 0 : IF( NE_32( ( error = openCldfb_ivas_fx( &hCPE->hCoreCoder[i]->cldfbSyn, CLDFB_SYNTHESIS, hCPE->hCoreCoder[i]->output_Fs, CLDFB_PROTOTYPE_1_25MS, DEC ) ), IVAS_ERR_OK ) )
1065 : {
1066 0 : return error;
1067 : }
1068 : }
1069 :
1070 19520 : IF( hCPE->hCoreCoder[i]->hFdCngDec == NULL )
1071 : {
1072 308 : IF( NE_32( ( error = createFdCngDec_fx( &hCPE->hCoreCoder[i]->hFdCngDec ) ), IVAS_ERR_OK ) )
1073 : {
1074 0 : return error;
1075 : }
1076 308 : initFdCngDec_ivas_fx( hCPE->hCoreCoder[i], hCPE->hCoreCoder[i]->cldfbSyn->scale );
1077 : }
1078 : }
1079 : }
1080 : }
1081 :
1082 386540 : return error;
1083 : }
1084 :
1085 :
1086 : /*-------------------------------------------------------------------*
1087 : * Function synchro_synthesis()
1088 : *
1089 : * Synchronize upmixed DFT/TD/MDCT stereo synthesis to match the overall delay of 32ms
1090 : * Handling of TD stereo <-> DFT stereo transitions
1091 : *-------------------------------------------------------------------*/
1092 386110 : void synchro_synthesis_fx(
1093 : const Word32 ivas_total_brate, /* i : IVAS total bitrate Q0*/
1094 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
1095 : Word32 *output_fx[CPE_CHANNELS], /* i/o: output synthesis signal Q11*/
1096 : const Word16 output_frame, /* i : Number of samples Q0*/
1097 : const Word16 sba_dirac_stereo_flag, /* i : signal stereo output FOR SBA DirAC Q0*/
1098 : Word16 output_fx_q )
1099 : {
1100 : Word16 n, delay_comp_TD, delay_comp_DFT;
1101 : Word32 output_Fs;
1102 : Decoder_State **sts;
1103 : Word16 i, delay_cldfb, dft32ms_ovl;
1104 :
1105 : Word32 *p_output_mem_fx[CPE_CHANNELS];
1106 : Word32 tmp_out_fx[CPE_CHANNELS][NS2SA( 48000, DELAY_CLDFB_NS )];
1107 : Word32 tmp_out_TD_fx[CPE_CHANNELS][STEREO_DFT32MS_OVL_MAX];
1108 : Word32 tmp_out_TD2_fx[CPE_CHANNELS][STEREO_DFT32MS_OVL_MAX];
1109 :
1110 : Word16 use_cldfb_for_last_dft;
1111 : Word16 dft_mono_brate_switch;
1112 : Word16 delay_diff;
1113 386110 : Word32 tmpF_fx = 0;
1114 386110 : move32();
1115 : Word16 nChannels;
1116 :
1117 386110 : sts = hCPE->hCoreCoder;
1118 386110 : output_Fs = sts[0]->output_Fs; /* Q0 */
1119 :
1120 386110 : use_cldfb_for_last_dft = 0;
1121 386110 : move16();
1122 :
1123 386110 : test();
1124 386110 : test();
1125 386110 : test();
1126 386110 : test();
1127 386110 : test();
1128 386110 : if ( ( NE_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->nchan_out, 1 ) && LE_32( hCPE->last_element_brate, IVAS_24k4 ) ) /* note: this is to mimic the DFT stereo condition "hCPE->hStereoDft->hConfig->res_cod_mode == 0" in last frame */
1129 384429 : || ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->nchan_out, 1 ) && EQ_16( hCPE->hStereoDft->hConfig->res_cod_mode, STEREO_DFT_RES_COD_OFF ) ) )
1130 : {
1131 18682 : use_cldfb_for_last_dft = 1; /* Q0 */
1132 18682 : move16();
1133 : }
1134 :
1135 386110 : dft_mono_brate_switch = 0;
1136 386110 : move16();
1137 386110 : test();
1138 386110 : test();
1139 386110 : test();
1140 386110 : test();
1141 386110 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && ( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) ) && EQ_16( hCPE->nchan_out, 1 ) && NE_32( hCPE->element_brate, hCPE->last_element_brate ) )
1142 : {
1143 2144 : test();
1144 2144 : test();
1145 2144 : IF( GE_32( hCPE->last_element_brate, IVAS_32k ) && EQ_16( hCPE->hStereoDft->hConfig->res_cod_mode, STEREO_DFT_RES_COD_OFF ) )
1146 : {
1147 529 : dft_mono_brate_switch = -1; /* switch from residual coding mode or MDCT Stereo Q0*/
1148 529 : move16();
1149 : }
1150 1615 : ELSE IF( LE_32( hCPE->last_element_brate, IVAS_24k4 ) && GT_16( hCPE->hStereoDft->hConfig->res_cod_mode, STEREO_DFT_RES_COD_OFF ) )
1151 : {
1152 58 : dft_mono_brate_switch = 1; /* switch to residual coding mode Q0*/
1153 58 : move16();
1154 : }
1155 : }
1156 :
1157 386110 : IF( use_cldfb_for_last_dft )
1158 : {
1159 18682 : test();
1160 18682 : test();
1161 18682 : test();
1162 18682 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_TD ) && ( GT_32( ivas_total_brate, IVAS_SID_5k2 ) || EQ_16( hCPE->nchan_out, 2 ) ) )
1163 : {
1164 7 : stereo_tca_scale_R_channel_fx( hCPE, output_fx[0], output_frame );
1165 : }
1166 : }
1167 :
1168 : /* set delays */
1169 386110 : delay_comp_DFT = NS2SA_FX2( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS ); /* Q0 */
1170 386110 : move16();
1171 386110 : delay_comp_TD = NS2SA_FX2( output_Fs, IVAS_DEC_DELAY_NS - DELAY_CLDFB_NS ); /* Q0 */
1172 386110 : move16();
1173 386110 : delay_diff = sub( delay_comp_TD, delay_comp_DFT ); /* Q0 */
1174 :
1175 386110 : dft32ms_ovl = extract_l( Mpy_32_32( imult3216( output_Fs, STEREO_DFT32MS_OVL_MAX ), 44740 ) ); /* Q0 */
1176 386110 : delay_cldfb = NS2SA_FX2( output_Fs, DELAY_CLDFB_NS ); /* Q0 */
1177 386110 : move16();
1178 :
1179 : /* initialize pointers */
1180 386110 : test();
1181 386110 : IF( GE_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->output_mem_fx[0] != NULL )
1182 : {
1183 319734 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1184 : {
1185 213156 : p_output_mem_fx[n] = hCPE->output_mem_fx[n]; /* Q11 */
1186 : }
1187 : }
1188 :
1189 : /*----------------------------------------------------------------*
1190 : * DFT stereo synchro
1191 : *----------------------------------------------------------------*/
1192 :
1193 386110 : test();
1194 386110 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || sba_dirac_stereo_flag )
1195 : {
1196 : /* handling of bitrate switching from residual (using DFT) to non-residual mode (using CLDFB) FOR mono output - as in DFT->TD switching */
1197 69837 : IF( EQ_16( dft_mono_brate_switch, -1 ) )
1198 : {
1199 41389 : FOR( i = delay_comp_DFT; i < delay_comp_TD; i++ )
1200 : {
1201 40860 : sts[0]->prev_synth_buffer32_fx[i] = p_output_mem_fx[0][i - delay_comp_DFT]; /* Q11 */
1202 40860 : move32();
1203 : }
1204 :
1205 27769 : FOR( i = delay_comp_TD; i < add( delay_comp_TD, delay_cldfb ); i++ )
1206 : {
1207 27240 : tmp_out_fx[0][i - delay_comp_TD] = p_output_mem_fx[0][i - delay_comp_DFT]; /* Q11 */
1208 27240 : move32();
1209 : }
1210 : }
1211 :
1212 69837 : test();
1213 69837 : IF( EQ_16( hCPE->nchan_out, 1 ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
1214 : {
1215 2526 : FOR( Word32 ind = 0; ind < delay_comp_DFT; ind++ )
1216 : {
1217 2128 : Word32 temp = L_add( sts[0]->prev_synth_buffer32_fx[ind], sts[1]->prev_synth_buffer32_fx[ind] ); /* Q11 */
1218 2128 : sts[0]->prev_synth_buffer32_fx[ind] = Mpy_32_32( temp, INV_SQRT2_FX ); /* Q11 */
1219 2128 : move32();
1220 : }
1221 : }
1222 :
1223 69837 : IF( use_cldfb_for_last_dft )
1224 : {
1225 : /* delay CLDFB-based mono output (<= 24.4 kbps) to be aligned with DFT-based mono output (32 kbps), needed to avoid discontinuities with TCX-LTP. */
1226 17001 : Copy32( sts[0]->prev_synth_buffer32_fx + delay_comp_DFT, hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf_32, delay_diff ); /* Q11 */
1227 17001 : delay_signal32_fx( output_fx[0], output_frame, hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf_32, delay_diff );
1228 : }
1229 :
1230 69837 : IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1231 : {
1232 62857 : ivas_post_proc_fx( NULL, hCPE, 0, output_fx[0], output_fx, output_frame, sba_dirac_stereo_flag, output_fx_q );
1233 : }
1234 :
1235 : /* zero padding in order to synchronize the upmixed DFT stereo synthesis with the TD/MDCT stereo synthesis */
1236 187165 : FOR( n = 0; n < hCPE->nchan_out; n++ )
1237 : {
1238 117328 : IF( sba_dirac_stereo_flag )
1239 : {
1240 21580 : delay_signal_q_adj_fx( output_fx[n], output_frame, hCPE->prev_synth_fx[n], delay_comp_DFT, hCPE->hStereoDft->q_dft, hCPE->q_prev_synth_fx );
1241 : }
1242 : ELSE
1243 : {
1244 95748 : delay_signal32_fx( output_fx[n], output_frame, sts[n]->prev_synth_buffer32_fx, delay_comp_DFT );
1245 : }
1246 : }
1247 69837 : if ( sba_dirac_stereo_flag )
1248 : {
1249 10790 : hCPE->q_prev_synth_fx = hCPE->hStereoDft->q_dft;
1250 10790 : move16();
1251 : }
1252 69837 : IF( use_cldfb_for_last_dft )
1253 : {
1254 17001 : Copy32( hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf_32, sts[0]->prev_synth_buffer32_fx + delay_comp_DFT, delay_diff ); /* Q11 */
1255 : }
1256 :
1257 : /* handling of TD->DFT switching */
1258 187165 : FOR( n = 0; n < hCPE->nchan_out; n++ )
1259 : {
1260 117328 : test();
1261 117328 : test();
1262 117328 : test();
1263 117328 : IF( ( NE_16( hCPE->last_element_mode, IVAS_CPE_DFT ) && !sba_dirac_stereo_flag && NE_16( dft_mono_brate_switch, -1 ) ) || EQ_16( dft_mono_brate_switch, 1 ) )
1264 670 : {
1265 : Word32 *pPrev_synth_fx;
1266 670 : Word32 inv_fade_len_fx = 0;
1267 670 : move32();
1268 670 : SWITCH( output_Fs )
1269 : {
1270 88 : case 16000:
1271 88 : inv_fade_len_fx = 71582792; /* Q31 */
1272 88 : move32();
1273 88 : BREAK;
1274 178 : case 32000:
1275 178 : inv_fade_len_fx = 35791396; /* Q31 */
1276 178 : move32();
1277 178 : BREAK;
1278 404 : case 48000:
1279 404 : inv_fade_len_fx = 23860930; /* Q31 */
1280 404 : move32();
1281 404 : BREAK;
1282 0 : default:
1283 0 : assert( 0 );
1284 : }
1285 :
1286 : /* cross-fading between TD synchro memory and the DFT output */
1287 670 : IF( sba_dirac_stereo_flag )
1288 : {
1289 0 : pPrev_synth_fx = hCPE->prev_synth_fx[n]; /* Q11 */
1290 0 : move32();
1291 : }
1292 : ELSE
1293 : {
1294 670 : pPrev_synth_fx = sts[n]->prev_synth_buffer32_fx; /* Q11 */
1295 670 : move32();
1296 : }
1297 :
1298 670 : IF( NE_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
1299 : {
1300 8420 : FOR( i = delay_comp_DFT; i < delay_comp_TD; i++ )
1301 : {
1302 8280 : Word64 res1 = W_mult0_32_32( pPrev_synth_fx[i], ( sub( delay_comp_TD, i ) ) ); /* Q11 */
1303 8280 : Word64 res2 = W_mult0_32_32( output_fx[n][i], ( sub( i, delay_comp_DFT ) ) ); /* Q11 */
1304 8280 : Word64 res = W_add( W_shr( res1, 6 ), W_shr( res2, 6 ) ); /* Q11 - 6 */
1305 8280 : res = res * inv_fade_len_fx; /* Q11 + 31 - 6 */
1306 8280 : move64();
1307 8280 : output_fx[n][i] = W_extract_l( W_shr( res, 31 - 6 ) ); /* Q11 */
1308 8280 : move32();
1309 : // change 6 to less number to get better precision
1310 : }
1311 : }
1312 : }
1313 116658 : ELSE IF( EQ_16( dft_mono_brate_switch, -1 ) )
1314 : {
1315 529 : Word32 inv_fade_len_1_fx = 0;
1316 529 : move32();
1317 529 : Word32 inv_fade_len_2_fx = 0;
1318 529 : move32();
1319 :
1320 529 : SWITCH( output_Fs )
1321 : {
1322 63 : case 16000:
1323 63 : inv_fade_len_1_fx = 71582792; /* Q31 */
1324 63 : move32();
1325 63 : inv_fade_len_2_fx = 107374184; /* Q31 */
1326 63 : move32();
1327 63 : BREAK;
1328 99 : case 32000:
1329 99 : inv_fade_len_1_fx = 35791396; /* Q31 */
1330 99 : move32();
1331 99 : inv_fade_len_2_fx = 53687092; /* Q31 */
1332 99 : move32();
1333 99 : BREAK;
1334 367 : case 48000:
1335 367 : inv_fade_len_1_fx = 23860930; /* Q31 */
1336 367 : move32();
1337 367 : inv_fade_len_2_fx = 35791396; /* Q31 */
1338 367 : move32();
1339 367 : BREAK;
1340 0 : default:
1341 0 : assert( 0 );
1342 : }
1343 41389 : FOR( i = 0; i < delay_diff; i++ )
1344 : {
1345 40860 : Word64 res1 = W_mult0_32_32( output_fx[0][i + delay_comp_DFT], ( sub( delay_diff, i ) ) ); /* Q11 */
1346 40860 : Word64 res2 = W_mult0_32_32( p_output_mem_fx[0][i], i ); /* Q11 */
1347 40860 : Word64 res = W_add( W_shr( res1, 6 ), W_shr( res2, 6 ) ); /* Q11 - 6 */
1348 40860 : res = res * inv_fade_len_1_fx; /* Q11 + 31 - 6 */
1349 40860 : move64();
1350 40860 : output_fx[0][i + delay_comp_DFT] = W_extract_l( W_shr( res, 31 - 6 ) ); /* Q11 */
1351 40860 : move32();
1352 : }
1353 :
1354 27769 : FOR( i = 0; i < delay_cldfb; i++ )
1355 : {
1356 27240 : Word64 res1 = W_mult0_32_32( tmp_out_fx[0][i], ( sub( delay_cldfb, i ) ) ); /* Q11 */
1357 27240 : Word64 res2 = W_mult0_32_32( output_fx[0][i + delay_comp_TD], i ); /* Q11 */
1358 27240 : Word64 res = W_add( W_shr( res1, 6 ), W_shr( res2, 6 ) ); /* Q11 - 6 */
1359 27240 : res = res * inv_fade_len_2_fx; /* Q11 + 31 - 6 */
1360 27240 : move64();
1361 27240 : output_fx[0][i + delay_comp_TD] = W_extract_l( W_shr( res, 31 - 6 ) ); /* Q11 */
1362 27240 : move32();
1363 : }
1364 : }
1365 : }
1366 : }
1367 :
1368 : /*----------------------------------------------------------------*
1369 : * TD/MDCT stereo synchro
1370 : *----------------------------------------------------------------*/
1371 :
1372 386110 : IF( sba_dirac_stereo_flag )
1373 : {
1374 10790 : return;
1375 : }
1376 :
1377 375320 : test();
1378 375320 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) || EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1379 : {
1380 : /* handling of DFT->TD switching */
1381 316273 : test();
1382 316273 : test();
1383 316273 : IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_last_dft && hCPE->output_mem_fx[0] != NULL )
1384 : {
1385 : /* use redressed DFT stereo OLA part to reconstruct the TD stereo synchro memory */
1386 2103 : FOR( n = 0; n < hCPE->nchan_out; n++ )
1387 : {
1388 110838 : FOR( i = delay_comp_DFT; i < delay_comp_TD; i++ )
1389 : {
1390 109440 : sts[n]->prev_synth_buffer32_fx[i] = p_output_mem_fx[n][i - delay_comp_DFT]; /* Q11 */
1391 109440 : move32();
1392 : }
1393 :
1394 74358 : FOR( i = delay_comp_TD; i < add( delay_comp_TD, delay_cldfb ); i++ )
1395 : {
1396 72960 : tmp_out_fx[n][i - delay_comp_TD] = p_output_mem_fx[n][i - delay_comp_DFT]; /* Q11 */
1397 72960 : move32();
1398 : }
1399 : }
1400 : }
1401 :
1402 : /* IF previous frame had only one channel copy buffers to other channel */
1403 316273 : test();
1404 316273 : test();
1405 316273 : IF( EQ_16( hCPE->nchan_out, 1 ) && EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
1406 : {
1407 17 : Copy32( sts[0]->prev_synth_buffer32_fx, sts[1]->prev_synth_buffer32_fx, delay_comp_TD ); /* Q11 */
1408 17 : Copy32( tmp_out_fx[0], tmp_out_fx[1], delay_cldfb ); /* Q11 */
1409 17 : Copy32( p_output_mem_fx[0], p_output_mem_fx[1], delay_diff ); /* Q11 */
1410 : }
1411 :
1412 : /*----------------------------------------------------------------*
1413 : * update DFT synthesis overlap memory @output_Fs; needed FOR TD->DFT stereo switching
1414 : *----------------------------------------------------------------*/
1415 :
1416 : /* resample LB synthesis to output_Fs */
1417 316273 : test();
1418 316273 : IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) && !use_cldfb_for_last_dft )
1419 : {
1420 8812 : FOR( n = 0; n < hCPE->nchan_out; n++ )
1421 : {
1422 5608 : IF( EQ_16( sts[n]->core, ACELP_CORE ) )
1423 : {
1424 5458 : L_lerp_fx_q11( hCPE->input_mem_LB_fx[n], tmp_out_TD_fx[n], dft32ms_ovl, NS2SA_FX2( sts[n]->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
1425 : }
1426 : ELSE /* TCX/HQ core */
1427 : {
1428 150 : L_lerp_fx_q11( hCPE->input_mem_LB_fx[n], tmp_out_TD_fx[n], dft32ms_ovl, NS2SA_FX2( sts[n]->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
1429 :
1430 : /* use TCX synchro memory (perfect signal is available) */
1431 6370 : FOR( i = delay_diff; i < dft32ms_ovl; i++ )
1432 : {
1433 6220 : tmp_out_TD_fx[n][i] = sts[n]->delay_buf_out32_fx[i - delay_diff]; /* Q11 */
1434 6220 : move32();
1435 : }
1436 : }
1437 : }
1438 :
1439 3204 : IF( EQ_16( hCPE->nchan_out, CPE_CHANNELS ) )
1440 : {
1441 : /* upmix the resampled LB / the TCX synchro memory */
1442 2404 : tdm_upmix_plain_fx( tmp_out_TD2_fx[0], tmp_out_TD2_fx[1], tmp_out_TD_fx[0], tmp_out_TD_fx[1], tdm_ratio_tabl_fx[hCPE->hStereoTD->tdm_last_ratio_idx], tdm_den_ratio_tabl_fx[hCPE->hStereoTD->tdm_last_ratio_idx], 0, dft32ms_ovl, 1 );
1443 : }
1444 : ELSE
1445 : {
1446 800 : Copy32( tmp_out_TD_fx[0], tmp_out_TD2_fx[0], dft32ms_ovl ); /* Q11 */
1447 : }
1448 :
1449 8812 : FOR( n = 0; n < hCPE->nchan_out; n++ )
1450 : {
1451 5608 : IF( sts[0]->core == ACELP_CORE ) /* ACELP core in primary channel */
1452 : {
1453 5354 : SWITCH( output_Fs )
1454 : {
1455 1264 : case 16000:
1456 1264 : tmpF_fx = 71582792; /* 0.033 in Q31 */
1457 1264 : move32();
1458 1264 : BREAK;
1459 2343 : case 32000:
1460 2343 : tmpF_fx = 35791396; /* 0.016 in Q31 */
1461 2343 : move32();
1462 2343 : BREAK;
1463 1747 : case 48000:
1464 1747 : tmpF_fx = 23860930; /* 0.011 in Q31 */
1465 1747 : move32();
1466 1747 : BREAK;
1467 0 : default:
1468 0 : assert( 0 );
1469 : }
1470 : /* cross-fading between regular output synthesis and lerp_flt() resampled synthesis in 3.125 - 1.25 ms OLA part */
1471 341084 : FOR( i = 0; i < delay_diff; i++ )
1472 : {
1473 :
1474 335730 : Word64 res1 = W_mult0_32_32( output_fx[n][output_frame - dft32ms_ovl + delay_cldfb + i], ( sub( delay_diff, i ) ) ); /* Q11 */
1475 335730 : Word64 res2 = W_mult0_32_32( tmp_out_TD2_fx[n][i], i ); /* Q11 */
1476 335730 : Word64 res = W_add( W_shr( res1, 6 ), W_shr( res2, 6 ) ); /* Q11 - Q6 */
1477 335730 : res = res * tmpF_fx; /* Q11 + Q31 - Q6 */
1478 335730 : move64();
1479 335730 : p_output_mem_fx[n][i] = W_extract_l( W_shr( res, 31 - 6 ) ); /* Q11 */
1480 : }
1481 : }
1482 : ELSE /* TCX core */
1483 : {
1484 : /* reconstruct the 3.125 - 1.25 ms OLA part */
1485 16154 : FOR( i = 0; i < delay_diff; i++ )
1486 : {
1487 15900 : p_output_mem_fx[n][i] = output_fx[n][output_frame - dft32ms_ovl + delay_cldfb + i]; /* Q11 */
1488 15900 : move32();
1489 : }
1490 : }
1491 :
1492 : /* reconstruct the last 1.25 ms part of OLA window */
1493 240028 : FOR( i = delay_diff; i < dft32ms_ovl; i++ )
1494 : {
1495 234420 : p_output_mem_fx[n][i] = tmp_out_TD2_fx[n][i]; /* Q11 */
1496 234420 : move32();
1497 : }
1498 : }
1499 : }
1500 :
1501 : /*----------------------------------------------------------------*
1502 : * zero padding TD/MDCT synthesis in order to synchronize
1503 : * the upmixed TD/MDCT stereo synthesis with the DFT stereo synthesis
1504 : *----------------------------------------------------------------*/
1505 :
1506 316273 : test();
1507 316273 : test();
1508 316273 : test();
1509 316273 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && hCPE->stereo_switching_counter == 0 && EQ_16( hCPE->nchan_out, 1 ) && LE_32( hCPE->last_element_brate, IVAS_24k4 ) )
1510 : {
1511 15 : Word32 step_fx = 0;
1512 15 : move32();
1513 15 : tmpF_fx = ONE_IN_Q31; /* Q31 */
1514 15 : move32();
1515 15 : SWITCH( output_Fs )
1516 : {
1517 7 : case 16000:
1518 7 : step_fx = 33554432; /* 0.0156 in Q31 */
1519 7 : move32();
1520 7 : BREAK;
1521 5 : case 32000:
1522 5 : step_fx = 16777216; /* 0.0078 in Q31 */
1523 5 : move32();
1524 5 : BREAK;
1525 3 : case 48000:
1526 3 : step_fx = 11184811; /* 0.0052 in Q31 */
1527 3 : move32();
1528 3 : BREAK;
1529 0 : default:
1530 0 : assert( 0 );
1531 : }
1532 : /* FOR the first switching frame from DFT to TD, downmix memory too */
1533 847 : FOR( n = 0; n < delay_comp_TD; n++ )
1534 : {
1535 832 : sts[0]->prev_synth_buffer32_fx[n] = Mpy_32_32( sts[0]->prev_synth_buffer32_fx[n], tmpF_fx ); /* Q11 */
1536 832 : move32();
1537 832 : tmpF_fx = L_sub_sat( tmpF_fx, step_fx ); /* Q31 */
1538 : }
1539 : }
1540 :
1541 316273 : test();
1542 316273 : test();
1543 316273 : test();
1544 316273 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( hCPE->nchan_out, 1 ) && !is_DTXrate( hCPE->element_brate ) && is_DTXrate( hCPE->last_element_brate ) )
1545 : {
1546 34 : Copy32( sts[0]->prev_synth_buffer32_fx, sts[1]->prev_synth_buffer32_fx, delay_comp_TD ); /* Q11 */
1547 : }
1548 :
1549 : // nChannels = ( hCPE->element_mode == IVAS_CPE_MDCT ) ? 2 : hCPE->nchan_out;
1550 316273 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1551 : {
1552 312522 : nChannels = 2; /* Q0 */
1553 312522 : move16();
1554 : }
1555 : ELSE
1556 : {
1557 3751 : nChannels = hCPE->nchan_out; /* Q0 */
1558 3751 : move16();
1559 : }
1560 947472 : FOR( n = 0; n < nChannels; n++ )
1561 : {
1562 631199 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1563 : {
1564 625044 : Copy32( sts[n]->prev_synth_buffer32_fx + delay_comp_DFT, hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf_32, delay_diff ); /* Q11 */
1565 625044 : delay_signal32_fx( output_fx[n], output_frame, hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf_32, delay_diff ); /* Q11 */
1566 625044 : ivas_post_proc_fx( NULL, hCPE, n, output_fx[n], output_fx, output_frame, 0, output_fx_q );
1567 :
1568 625044 : delay_signal32_fx( output_fx[n], output_frame, sts[n]->prev_synth_buffer32_fx, delay_comp_DFT );
1569 625044 : Copy32( hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf_32, sts[n]->prev_synth_buffer32_fx + delay_comp_DFT, delay_diff ); /* Q11 */
1570 : }
1571 : ELSE
1572 : {
1573 6155 : delay_signal32_fx( output_fx[n], output_frame, sts[n]->prev_synth_buffer32_fx, delay_comp_TD );
1574 : }
1575 : }
1576 :
1577 : /* handling of DFT->TD switching */
1578 316273 : test();
1579 316273 : IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_last_dft )
1580 : {
1581 705 : test();
1582 705 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && hCPE->hStereoCng->prev_sid_nodata )
1583 : {
1584 21 : FOR( n = 0; n < hCPE->nchan_out; n++ )
1585 : {
1586 14 : SWITCH( output_Fs )
1587 : {
1588 2 : case 16000:
1589 2 : tmpF_fx = 107374184; /* 0.05 in Q31 */
1590 2 : move32();
1591 2 : BREAK;
1592 6 : case 32000:
1593 6 : tmpF_fx = 53687092; /* 0.025 in Q31 */
1594 6 : move32();
1595 6 : BREAK;
1596 6 : case 48000:
1597 6 : tmpF_fx = 35791396; /* 0.016 in Q31 */
1598 6 : move32();
1599 6 : BREAK;
1600 0 : default:
1601 0 : assert( 0 );
1602 : }
1603 654 : FOR( i = 0; i < delay_cldfb; i++ )
1604 : {
1605 640 : tmp_out_fx[n][i] = W_extract_l( W_mult0_32_32( Mpy_32_32( tmp_out_fx[n][i], tmpF_fx ), ( sub( delay_cldfb, i ) ) ) ); /* Q11 */
1606 640 : move32();
1607 : }
1608 : }
1609 : }
1610 :
1611 : /* cross-fading between DFT OLA memory and TD output */
1612 2108 : FOR( n = 0; n < nChannels; n++ )
1613 : {
1614 1403 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1615 : {
1616 1338 : SWITCH( output_Fs )
1617 : {
1618 108 : case 16000:
1619 108 : tmpF_fx = 71582792; /* Q31 */
1620 108 : move32();
1621 108 : BREAK;
1622 272 : case 32000:
1623 272 : tmpF_fx = 35791396; /* Q31 */
1624 272 : move32();
1625 272 : BREAK;
1626 958 : case 48000:
1627 958 : tmpF_fx = 23860930; /* Q31 */
1628 958 : move32();
1629 958 : BREAK;
1630 0 : default:
1631 0 : assert( 0 );
1632 : }
1633 107118 : FOR( i = 0; i < delay_diff; i++ )
1634 : {
1635 :
1636 105780 : Word64 res1 = W_mult0_32_32( output_fx[n][i + delay_comp_DFT], ( sub( delay_diff, i ) ) ); /* Q11 */
1637 105780 : Word64 res2 = W_mult0_32_32( p_output_mem_fx[n][i], i ); /* Q11 */
1638 105780 : Word64 res = W_add( W_shr( res1, 6 ), W_shr( res2, 6 ) ); /* Q11 - Q6 */
1639 105780 : res = res * tmpF_fx; /* Q11 + Q31 - Q6 */
1640 105780 : move64();
1641 105780 : output_fx[n][i + delay_comp_DFT] = W_extract_l( W_shr( res, 31 - 6 ) ); /* Q11 */
1642 105780 : move32();
1643 : }
1644 : }
1645 1403 : SWITCH( output_Fs )
1646 : {
1647 122 : case 16000:
1648 122 : tmpF_fx = 107374184; /* Q31 */
1649 122 : move32();
1650 122 : BREAK;
1651 302 : case 32000:
1652 302 : tmpF_fx = 53687092; /* Q31 */
1653 302 : move32();
1654 302 : BREAK;
1655 979 : case 48000:
1656 979 : tmpF_fx = 35791396; /* Q31 */
1657 979 : move32();
1658 979 : BREAK;
1659 0 : default:
1660 0 : assert( 0 );
1661 : }
1662 74663 : FOR( i = 0; i < delay_cldfb; i++ )
1663 : {
1664 73260 : Word64 res1 = W_mult0_32_32( tmp_out_fx[n][i], ( sub( delay_cldfb, i ) ) ); /* Q11 */
1665 73260 : Word64 res2 = W_mult0_32_32( output_fx[n][i + delay_comp_TD], i ); /* Q11 */
1666 73260 : Word64 res = W_add( W_shr( res1, 6 ), W_shr( res2, 6 ) ); /* Q11 - Q6 */
1667 73260 : res = res * tmpF_fx; /* Q11 + Q31 - Q6 */
1668 73260 : move64();
1669 73260 : output_fx[n][i + delay_comp_TD] = W_extract_l( W_shr( res, 31 - 6 ) ); /* Q11 */
1670 73260 : move32();
1671 : }
1672 : }
1673 : }
1674 : }
1675 :
1676 375320 : return;
1677 : }
1678 :
1679 : /*-------------------------------------------------------------------*
1680 : * Function stereo_switching_dec()
1681 : *
1682 : * Handling of memories in case of CPE modes switching
1683 : *-------------------------------------------------------------------*/
1684 :
1685 : Word32 side_gain_table[32 + 1] = { -ONE_IN_Q31, -2040109440, -1932735232, -1825361152, -1717986944,
1686 : -1610612736, -1503238528, -1395864320, -1288490240, -1181115904, -1073741824,
1687 : -966367616, -858993408, -751619200, -644245120, -536870912, -429496704,
1688 : -322122496, -214748288, -107374208, 0, 107374336, 214748416,
1689 : 322122496, 429496832, 536870912, 644245248, 751619328, 858993408,
1690 : 966367744, 1073741824, 1181116160, 1288490240 };
1691 386540 : void stereo_switching_dec(
1692 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
1693 : const Word32 ivas_total_brate /* i : IVAS total bitrate Q0*/
1694 : )
1695 : {
1696 : Word16 i, n;
1697 : Word16 dft32ms_ovl;
1698 : Decoder_State **sts;
1699 : Word32 tmpF_fx;
1700 : Word16 delay_comp_TD;
1701 :
1702 386540 : sts = hCPE->hCoreCoder;
1703 :
1704 386540 : delay_comp_TD = NS2SA_FX2( sts[0]->output_Fs, IVAS_DEC_DELAY_NS - DELAY_CLDFB_NS );
1705 386540 : move16();
1706 :
1707 : /* prevent CPE mode switching in the first received frame */
1708 386540 : IF( sts[0]->ini_frame == 0 )
1709 : {
1710 2401 : hCPE->last_element_mode = hCPE->element_mode; /* Q0 */
1711 2401 : move16();
1712 2401 : hCPE->stereo_switching_counter = 10; /* Q0 */
1713 2401 : move16();
1714 2401 : hCPE->NbFrameMod = 7; /* Q0 */
1715 2401 : move16();
1716 2401 : hCPE->lt_es_em_fx = 0;
1717 2401 : move16();
1718 : }
1719 :
1720 386540 : IF( EQ_16( hCPE->element_mode, hCPE->last_element_mode ) )
1721 : {
1722 385084 : hCPE->stereo_switching_counter = add( hCPE->stereo_switching_counter, 1 ); /* Q0 */
1723 385084 : hCPE->stereo_switching_counter = s_min( 10, hCPE->stereo_switching_counter ); /* Q0 */
1724 : }
1725 : ELSE
1726 : {
1727 1456 : hCPE->stereo_switching_counter = 0;
1728 1456 : move16();
1729 : }
1730 :
1731 386540 : test();
1732 386540 : test();
1733 386540 : test();
1734 386540 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->nchan_out, 1 ) && GE_32( hCPE->element_brate, IVAS_32k ) && LE_32( hCPE->last_element_brate, IVAS_24k4 ) )
1735 : {
1736 58 : dft32ms_ovl = extract_l( Mpy_32_32( imult3216( sts[0]->output_Fs, STEREO_DFT32MS_OVL_MAX ), 44740 ) ); // 1/48000 = 44740 (Q31) /* Q0 */
1737 58 : set32_fx( hCPE->output_mem_fx[0], 0, dft32ms_ovl );
1738 : }
1739 386540 : test();
1740 386540 : test();
1741 386540 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && NE_16( hCPE->last_element_mode, IVAS_CPE_DFT ) && hCPE->hCoreCoder[0]->ini_frame > 0 )
1742 : {
1743 : /* windowing the OLA memory */
1744 715 : dft32ms_ovl = extract_l( Mpy_32_32( imult3216( sts[0]->output_Fs, STEREO_DFT32MS_OVL_MAX ), 44740 ) ); // 1/48000 = 44740 (Q31) /* Q0 */
1745 2145 : FOR( n = 0; n < CPE_CHANNELS; n++ )
1746 : {
1747 1430 : IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
1748 : {
1749 : /* copy memories from previous MDCT Stereo frame to output_mem */
1750 1320 : Copy32( hCPE->input_mem_fx[n], hCPE->output_mem_fx[n], dft32ms_ovl ); /* Q11 */
1751 : }
1752 :
1753 1430 : test();
1754 1430 : test();
1755 1430 : test();
1756 1430 : IF( GT_32( ivas_total_brate, IVAS_SID_5k2 ) || n == 0 || NE_16( hCPE->last_element_mode, IVAS_CPE_TD ) || EQ_16( hCPE->nchan_out, 1 ) )
1757 : {
1758 185516 : FOR( i = 0; i < dft32ms_ovl; i++ )
1759 : {
1760 184100 : hCPE->output_mem_fx[n][i] = Mpy_32_16_1( hCPE->output_mem_fx[n][i], hCPE->hStereoDft->win32ms_fx[STEREO_DFT32MS_STEP * ( dft32ms_ovl - 1 - i )] ); /* q_output_mem_fx */
1761 184100 : move32();
1762 : }
1763 : }
1764 : ELSE
1765 : {
1766 14 : IF( EQ_32( hCPE->hStereoTCA->prevTargetGain_fx, ONE_IN_Q29 ) )
1767 : {
1768 14 : tmpF_fx = ONE_IN_Q27; /* Q27 */
1769 14 : move32();
1770 : }
1771 : ELSE
1772 : {
1773 0 : Word16 temp_b = extract_l( L_shr( hCPE->hStereoTCA->prevTargetGain_fx, 16 ) ); /* Q13 */
1774 0 : Word16 temp_b_q = 2;
1775 0 : move16();
1776 0 : tmpF_fx = Inv16( temp_b, &temp_b_q );
1777 0 : tmpF_fx = L_shl( tmpF_fx, sub( sub( 31, 4 ), sub( 15, temp_b_q ) ) ); /* Q27 */
1778 : }
1779 1614 : FOR( i = 0; i < dft32ms_ovl; i++ )
1780 : {
1781 1600 : Word32 temp_result = Mpy_32_16_1( tmpF_fx, hCPE->hStereoDft->win32ms_fx[STEREO_DFT32MS_STEP * ( dft32ms_ovl - 1 - i )] ); /* Q27 */
1782 1600 : hCPE->output_mem_fx[n][i] = L_shl_sat( Mpy_32_32( hCPE->output_mem_fx[n][i], temp_result ), 4 ); /* q_output_mem_fx */
1783 : }
1784 1038 : FOR( i = 0; i < delay_comp_TD; i++ )
1785 : {
1786 : Word64 temp_result;
1787 1024 : temp_result = W_mult0_32_32( hCPE->hCoreCoder[1]->prev_synth_buffer32_fx[i], tmpF_fx ); /* q_prev_synth + Q27 */
1788 1024 : hCPE->hCoreCoder[1]->prev_synth_buffer32_fx[i] = W_sat_l( W_shr( temp_result, 27 ) ); /* q_prev_synth */
1789 1024 : move32();
1790 : }
1791 3836 : FOR( i = 0; i < L_DEC_MEM_LEN_ICA; i++ )
1792 : {
1793 3822 : hCPE->hStereoTCA->memChanR_fx[i] = L_shl_sat( Mpy_32_32( hCPE->hStereoTCA->memChanR_fx[i], tmpF_fx ), 4 ); /* q_mem_chanR */
1794 3822 : move32();
1795 : }
1796 : }
1797 : }
1798 :
1799 715 : IF( EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
1800 : {
1801 : /* create passive downmix of MDCT Stereo memories FOR DFT input memory */
1802 33660 : FOR( Word32 ind = 0; ind < STEREO_DFT32MS_OVL_16k; ind++ )
1803 : {
1804 33000 : Word32 result_int = L_add_sat( hCPE->input_mem_LB_fx[0][ind], hCPE->input_mem_LB_fx[1][ind] ); /* Q11 */
1805 33000 : hCPE->input_mem_LB_fx[0][ind] = L_shr( result_int, 1 ); /* Q11 */
1806 33000 : move32();
1807 : }
1808 :
1809 87910 : FOR( Word32 ind = 0; ind < dft32ms_ovl; ind++ )
1810 : {
1811 87250 : Word32 result_int = L_add_sat( hCPE->input_mem_fx[0][ind], hCPE->input_mem_fx[1][ind] ); /* Q11 */
1812 87250 : hCPE->input_mem_fx[0][ind] = L_shr( result_int, 1 ); /* Q11 */
1813 87250 : move32();
1814 : }
1815 :
1816 660 : IF( EQ_16( hCPE->nchan_out, 1 ) )
1817 : {
1818 53598 : FOR( Word32 ind = 0; ind < dft32ms_ovl; ind++ )
1819 : {
1820 53200 : Word32 result_int = L_add_sat( hCPE->output_mem_fx[0][ind], hCPE->output_mem_fx[1][ind] ); /* q_output_mem_fx */
1821 53200 : hCPE->output_mem_fx[0][ind] = Mpy_32_32( result_int, INV_SQRT_2_Q31 ); /* q_output_mem_fx */
1822 53200 : move32();
1823 : }
1824 : }
1825 : }
1826 :
1827 : /* Update the side_gain[] parameters */
1828 715 : IF( NE_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
1829 : {
1830 55 : tmpF_fx = 0;
1831 55 : move32();
1832 55 : if ( hCPE->hStereoTCA != NULL )
1833 : {
1834 29 : tmpF_fx = side_gain_table[hCPE->hStereoTCA->indx_ica_gD]; /* Q31 */
1835 29 : move32();
1836 : }
1837 55 : set32_fx( hCPE->hStereoDft->side_gain_fx + STEREO_DFT_NBDIV * STEREO_DFT_BAND_MAX, tmpF_fx, STEREO_DFT_BAND_MAX );
1838 : }
1839 :
1840 : /* reset residual coding / ESF (secondary channel) */
1841 715 : set32_fx( hCPE->hStereoDft->res_cod_mem_fx, 0, STEREO_DFT_OVL_8k );
1842 715 : hCPE->hStereoDft->q_res_cod_mem_fx = Q15;
1843 715 : move16();
1844 :
1845 715 : set32_fx( hCPE->input_mem_fx[1], 0, NS2SA_FX2( sts[0]->output_Fs, STEREO_DFT32MS_OVL_NS ) );
1846 : }
1847 :
1848 386540 : test();
1849 386540 : test();
1850 386540 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && NE_16( hCPE->last_element_mode, IVAS_CPE_TD ) && hCPE->hCoreCoder[0]->ini_frame > 0 )
1851 : {
1852 58 : hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_MID_IS_PRIM; /* Q0 */
1853 58 : move16();
1854 58 : hCPE->hStereoTD->tdm_last_SM_flag = 0;
1855 58 : move16();
1856 58 : hCPE->hStereoTD->tdm_prev_last_SM_flag = 0;
1857 58 : move16();
1858 :
1859 : /* First frame after DFT frame AND the content is uncorrelated or xtalk -> the primary channel is forced to left */
1860 58 : if ( EQ_16( hCPE->hStereoTD->tdm_LRTD_flag, 1 ) )
1861 : {
1862 58 : hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM; /* Q0 */
1863 58 : move16();
1864 : }
1865 : }
1866 :
1867 : /* no secondary channel in the previous frame -> memory resets */
1868 386540 : test();
1869 386540 : test();
1870 386540 : IF( GT_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) )
1871 : {
1872 732 : test();
1873 732 : IF( LE_32( hCPE->last_element_brate, IVAS_SID_5k2 ) && EQ_16( hCPE->nchan_out, 2 ) )
1874 : {
1875 : /* reset CLDFB memories */
1876 :
1877 7 : cldfb_reset_memory_fx( sts[0]->cldfbAna );
1878 7 : cldfb_reset_memory_fx( sts[0]->cldfbBPF );
1879 7 : cldfb_reset_memory_fx( sts[0]->cldfbSyn );
1880 :
1881 7 : sts[0]->mem_deemph_fx = 0;
1882 7 : move16();
1883 :
1884 :
1885 7 : sts[0]->tilt_code_fx = 0;
1886 7 : move16();
1887 7 : sts[0]->gc_threshold_fx = 0;
1888 7 : move16();
1889 :
1890 :
1891 7 : set16_fx( sts[0]->mem_syn1_fx, 0, M );
1892 7 : set16_fx( sts[0]->mem_syn2_fx, 0, M );
1893 7 : set16_fx( sts[0]->mem_syn3_fx, 0, M );
1894 7 : set16_fx( sts[0]->mem_syn_r, 0, L_SYN_MEM );
1895 :
1896 7 : sts[1]->last_L_frame = sts[0]->last_L_frame; /* Q0 */
1897 7 : move16();
1898 :
1899 :
1900 7 : set16_fx( sts[0]->old_exc_fx, 0, L_EXC_MEM_DEC );
1901 7 : set16_fx( sts[0]->lsf_old_fx, 0, M );
1902 7 : set16_fx( sts[0]->lsp_old_fx, 0, M );
1903 : }
1904 732 : sts[1]->last_extl = -1;
1905 732 : move16();
1906 :
1907 732 : sts[1]->prev_bfi = sts[0]->prev_bfi; /* Q0 */
1908 732 : move16();
1909 :
1910 :
1911 : // fix need to do correctly
1912 732 : set32_fx( sts[1]->old_pitch_buf_fx, L_deposit_h( L_SUBFR ), 2 * NB_SUBFR16k );
1913 732 : sts[1]->old_fpitchFB = L_deposit_h( 2 * L_SUBFR ); /* 15Q16 */
1914 732 : move32();
1915 :
1916 : /* reset CLDFB memories */
1917 :
1918 732 : cldfb_reset_memory_fx( sts[1]->cldfbAna );
1919 732 : cldfb_reset_memory_fx( sts[1]->cldfbBPF );
1920 732 : cldfb_reset_memory_fx( sts[1]->cldfbSyn );
1921 :
1922 732 : sts[1]->mem_deemph_fx = 0;
1923 732 : move16();
1924 :
1925 732 : sts[1]->tilt_code_fx = 0;
1926 732 : move16();
1927 732 : sts[1]->gc_threshold_fx = 0;
1928 732 : move16();
1929 :
1930 :
1931 732 : set16_fx( sts[1]->mem_syn1_fx, 0, M );
1932 732 : set16_fx( sts[1]->mem_syn2_fx, 0, M );
1933 732 : set16_fx( sts[1]->mem_syn3_fx, 0, M );
1934 732 : set16_fx( sts[1]->mem_syn_r, 0, L_SYN_MEM );
1935 :
1936 732 : sts[1]->last_L_frame = sts[0]->last_L_frame; /* Q0 */
1937 732 : move16();
1938 : /* Should not force sts[1]->Q_exc on the data of st[0], instead it better to just reuse the current scaling of st[0]*/
1939 732 : Copy( sts[0]->old_exc_fx, sts[1]->old_exc_fx, L_EXC_MEM_DEC ); /* Q_exc */
1940 732 : sts[1]->Q_exc = sts[0]->Q_exc;
1941 732 : move16();
1942 732 : sts[1]->Q_exc_cng = sts[0]->Q_exc_cng;
1943 732 : move16();
1944 732 : sts[1]->prev_Q_exc = sts[0]->prev_Q_exc;
1945 732 : move16();
1946 732 : Copy( sts[0]->Q_subfr, sts[1]->Q_subfr, L_Q_MEM );
1947 :
1948 732 : test();
1949 732 : IF( sts[0]->hBWE_TD != NULL && sts[1]->hBWE_TD != NULL )
1950 : {
1951 147 : sts[1]->hBWE_TD->prev_Q_bwe_syn = sts[0]->hBWE_TD->prev_Q_bwe_syn;
1952 147 : move16();
1953 147 : sts[1]->hBWE_TD->prev_Q_bwe_syn2 = sts[0]->hBWE_TD->prev_Q_bwe_syn2;
1954 147 : move16();
1955 :
1956 147 : sts[1]->hBWE_TD->prev_Q_bwe_exc_fb = sts[0]->hBWE_TD->prev_Q_bwe_exc_fb;
1957 147 : move16();
1958 147 : sts[1]->hBWE_TD->prev_Qx = sts[0]->hBWE_TD->prev_Qx;
1959 147 : move16();
1960 : }
1961 :
1962 732 : test();
1963 732 : IF( sts[0]->hBWE_FD != NULL && sts[1]->hBWE_FD != NULL )
1964 : {
1965 147 : sts[1]->hBWE_FD->prev_Q_synth = sts[0]->hBWE_FD->prev_Q_synth;
1966 147 : move16();
1967 : }
1968 :
1969 732 : sts[1]->prev_Q_bwe_exc = sts[0]->prev_Q_bwe_exc;
1970 732 : move16();
1971 732 : sts[1]->Q_syn = sts[0]->Q_syn;
1972 732 : move16();
1973 732 : sts[1]->Q_syn2 = sts[0]->Q_syn2;
1974 732 : move16();
1975 732 : sts[1]->Q_syn_cng = sts[0]->Q_syn_cng;
1976 732 : move16();
1977 732 : sts[1]->prev_Q_syn = sts[0]->prev_Q_syn;
1978 732 : move16();
1979 :
1980 732 : Copy( sts[0]->lsf_old_fx, sts[1]->lsf_old_fx, M ); /* Q2.56 */
1981 732 : Copy( sts[0]->lsp_old_fx, sts[1]->lsp_old_fx, M ); /* Q15 */
1982 732 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
1983 : {
1984 681 : sts[1]->last_core = sts[0]->last_core; /* Q0 */
1985 681 : move16();
1986 681 : sts[1]->last_coder_type = sts[0]->last_coder_type; /* Q0 */
1987 681 : move16();
1988 :
1989 :
1990 : // 32bit buffer
1991 681 : Copy32( sts[0]->hHQ_core->old_out_fx32, sts[1]->hHQ_core->old_out_fx32, L_FRAME48k ); /* exp(exp_old_out) */
1992 681 : Copy32( sts[0]->delay_buf_out32_fx, sts[1]->delay_buf_out32_fx, HQ_DELTA_MAX * HQ_DELAY_COMP ); /* Q11 */
1993 681 : Copy32( sts[0]->hTcxDec->old_syn_Overl_32, sts[1]->hTcxDec->old_syn_Overl_32, 256 ); /* Q_old_syn_Overl*/
1994 : // 16 bit buffer
1995 681 : Copy( sts[0]->hHQ_core->old_out_fx, sts[1]->hHQ_core->old_out_fx, L_FRAME48k ); /* exp(exp_old_out) */
1996 :
1997 681 : sts[1]->hHQ_core->Q_old_wtda = sts[0]->hHQ_core->Q_old_wtda;
1998 681 : move16();
1999 681 : Copy( sts[0]->delay_buf_out_fx, sts[1]->delay_buf_out_fx, HQ_DELTA_MAX * HQ_DELAY_COMP ); /* Q0 */
2000 681 : Copy( sts[0]->hTcxDec->old_syn_Overl, sts[1]->hTcxDec->old_syn_Overl, 256 ); /* Q_old_syn_Overl*/
2001 681 : sts[1]->hTcxDec->Q_old_syn_Overl = sts[0]->hTcxDec->Q_old_syn_Overl;
2002 : }
2003 : }
2004 385808 : ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
2005 : {
2006 7 : set16_fx( sts[0]->old_exc_fx, 0, L_EXC_MEM_DEC );
2007 7 : set16_fx( sts[1]->old_exc_fx, 0, L_EXC_MEM_DEC );
2008 : }
2009 :
2010 : /* TD/DFT -> MDCT stereo switching (there is no TCX in the TD stereo secondary channel, or DFT stereo) */
2011 386540 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && NE_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) )
2012 : {
2013 683 : sts[1]->hTcxCfg->last_aldo = sts[0]->hTcxCfg->last_aldo; /* Q0 */
2014 683 : move16();
2015 683 : sts[1]->hTcxCfg->tcx_curr_overlap_mode = sts[0]->hTcxCfg->tcx_curr_overlap_mode; /* Q0 */
2016 683 : move16();
2017 683 : sts[1]->fscale = sts[0]->fscale; /* Q0 */
2018 683 : move16();
2019 683 : sts[1]->hTcxCfg->tcx_mdct_window_length = sts[0]->hTcxCfg->tcx_mdct_window_length; /* Q0 */
2020 683 : move16();
2021 683 : sts[1]->pit_res_max = sts[0]->pit_res_max; /* Q0 */
2022 683 : move16();
2023 683 : sts[1]->pit_res_max_past = sts[0]->pit_res_max_past; /* Q0 */
2024 683 : move16();
2025 683 : sts[1]->hTcxDec->L_frameTCX = sts[0]->hTcxDec->L_frameTCX; /* Q0 */
2026 683 : move16();
2027 683 : sts[1]->hTcxDec->conceal_eof_gain = sts[0]->hTcxDec->conceal_eof_gain; /* Q14 */
2028 683 : move16();
2029 683 : sts[1]->hTcxDec->conceal_eof_gain32 = sts[0]->hTcxDec->conceal_eof_gain32; /* sts[0]->hTcxDec->conceal_eof_gain_e */
2030 683 : move32();
2031 683 : sts[1]->hTcxDec->conceal_eof_gain_e = sts[0]->hTcxDec->conceal_eof_gain_e;
2032 683 : move16();
2033 : }
2034 :
2035 386540 : return;
2036 : }
2037 : /*-------------------------------------------------------------------*
2038 : * Function stereo_td2dft_update()
2039 : *
2040 : * update OLA buffers - needed for switching from TD stereo to DFT stereo
2041 : *-------------------------------------------------------------------*/
2042 187035 : void stereo_td2dft_update_fx(
2043 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
2044 : const Word16 n, /* i : channel number Q0*/
2045 : Word32 output_fx[], /* i/o: synthesis @internal Fs Q11*/
2046 : Word32 synth_fx[], /* i/o: synthesis @output Fs Q11*/
2047 : Word32 hb_synth_fx[], /* i/o: hb synthesis Q11*/
2048 : const Word16 output_frame /* i : frame length Q0*/
2049 : )
2050 : {
2051 : Word16 ovl, ovl_TCX, dft32ms_ovl, hq_delay_comp;
2052 : Word16 ns, nsLB, i;
2053 : Word16 old_out_len, old_outLB_len;
2054 : Decoder_State **sts;
2055 :
2056 187035 : IF( hCPE == NULL )
2057 : {
2058 0 : return;
2059 : }
2060 :
2061 : /* initialization */
2062 187035 : sts = hCPE->hCoreCoder;
2063 187035 : ovl = NS2SA_FX2( i_mult( sts[n]->L_frame, FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ); /* Q0 */
2064 187035 : move16();
2065 187035 : dft32ms_ovl = extract_l( Mpy_32_32( imult3216( sts[0]->output_Fs, STEREO_DFT32MS_OVL_MAX ), 44740 ) ); // 1/48000 = 44740 (Q31) /* Q0 */
2066 187035 : move16();
2067 187035 : hq_delay_comp = NS2SA_FX2( sts[0]->output_Fs, DELAY_CLDFB_NS ); /* Q0 */
2068 187035 : move16();
2069 :
2070 187035 : test();
2071 187035 : test();
2072 187035 : IF( GE_16( hCPE->element_mode, IVAS_CPE_DFT ) && NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
2073 : {
2074 24503 : IF( sts[n]->core == ACELP_CORE )
2075 : {
2076 22078 : IF( n == 0 )
2077 : {
2078 : /* update DFT analysis overlap memory @internal_fs: core synthesis */
2079 18327 : Copy32( output_fx + sts[n]->L_frame - ovl, hCPE->input_mem_LB_fx[n], ovl ); /* Q11 */
2080 :
2081 : /* update DFT analysis overlap memory @internal_fs: BPF */
2082 18327 : IF( sts[n]->p_bpf_noise_buf_32 )
2083 : {
2084 16195 : Copy32( sts[n]->p_bpf_noise_buf_32 + sub( sts[n]->L_frame, ovl ), hCPE->input_mem_BPF_fx[n], ovl ); /* Q11 */
2085 : }
2086 :
2087 :
2088 : /* update DFT analysis overlap memory @output_Fs: BWE */
2089 18327 : test();
2090 18327 : test();
2091 18327 : IF( NE_16( sts[n]->extl, -1 ) || ( sts[n]->bws_cnt > 0 && sts[n]->core == ACELP_CORE ) )
2092 : {
2093 15060 : Copy32( hb_synth_fx + sub( output_frame, dft32ms_ovl ), hCPE->input_mem_fx[n], dft32ms_ovl ); /* Q11 */
2094 : }
2095 : }
2096 : ELSE
2097 : {
2098 : /* update DFT analysis overlap memory @internal_fs: core synthesis, secondary channel */
2099 3751 : Copy32( output_fx + sub( sts[n]->L_frame, ovl ), hCPE->input_mem_LB_fx[n], ovl ); /* Q11 */
2100 : }
2101 : }
2102 : ELSE /* TCX core */
2103 : {
2104 : /* LB-TCX synthesis */
2105 2425 : Copy32( output_fx + sub( sts[n]->L_frame, ovl ), hCPE->input_mem_LB_fx[n], ovl ); /* Q11 */
2106 :
2107 : /* BPF */
2108 2425 : IF( n == 0 && sts[n]->p_bpf_noise_buf_32 )
2109 : {
2110 2425 : Copy32( sts[n]->p_bpf_noise_buf_32 + sub( sts[n]->L_frame, ovl ), hCPE->input_mem_BPF_fx[n], ovl ); /* Q11 */
2111 : }
2112 :
2113 : /* TCX synthesis (it was already delayed in TD stereo in core_switching_post_dec()) */
2114 2425 : IF( sts[n]->hTcxDec != NULL )
2115 : {
2116 2425 : ovl_TCX = NS2SA_FX2( L_mult0( sts[n]->hTcxDec->L_frameTCX, FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ); /* Q0 */
2117 2425 : move16();
2118 2425 : Copy32( synth_fx + sts[n]->hTcxDec->L_frameTCX + hq_delay_comp - ovl_TCX, hCPE->input_mem_fx[n], sub( ovl_TCX, hq_delay_comp ) ); /* Q11 */
2119 2425 : Copy32( sts[n]->delay_buf_out32_fx, hCPE->input_mem_fx[n] + ovl_TCX - hq_delay_comp, hq_delay_comp ); /* Q11 */
2120 : }
2121 : }
2122 : }
2123 162532 : ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && hCPE->input_mem_fx[0] != NULL )
2124 : {
2125 :
2126 : /* update DFT stereo OLA memories */
2127 : /*set_zero( hCPE->input_mem_LB[n], STEREO_DFT32MS_OVL_16k );*/
2128 68760 : L_lerp_fx_q11( output_fx + sub( sts[n]->L_frame, ovl ), hCPE->input_mem_LB_fx[n], STEREO_DFT32MS_OVL_16k, ovl );
2129 :
2130 :
2131 : /* TCX synthesis (it was already delayed in TD stereo in core_switching_post_dec()) */
2132 68760 : IF( sts[n]->hTcxDec != NULL )
2133 : {
2134 68760 : ovl_TCX = NS2SA_FX2( L_mult0( sts[n]->hTcxDec->L_frameTCX, FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ); /* Q0 */
2135 68760 : move16();
2136 68760 : Copy32( synth_fx + add( sts[n]->hTcxDec->L_frameTCX, sub( hq_delay_comp, ovl_TCX ) ), hCPE->input_mem_fx[n], sub( ovl_TCX, hq_delay_comp ) ); /* Q11 */
2137 68760 : Copy32( sts[n]->delay_buf_out32_fx, hCPE->input_mem_fx[n] + sub( ovl_TCX, hq_delay_comp ), hq_delay_comp ); /* Q11 */
2138 : }
2139 :
2140 68760 : IF( EQ_16( n, 1 ) )
2141 : {
2142 34380 : nsLB = NS2SA_FX2( L_mult0( sts[n]->L_frame, FRAMES_PER_SEC ), N_ZERO_MDCT_NS ); /* Q0 */
2143 34380 : move16();
2144 34380 : ns = NS2SA_FX2( sts[n]->output_Fs, N_ZERO_MDCT_NS ); /* Q0 */
2145 34380 : move16();
2146 34380 : old_outLB_len = extract_l( Mpy_32_32( ( L_mult0( STEREO_MDCT2DFT_FADE_LEN_48k * FRAMES_PER_SEC, i_mult( 3, sts[0]->L_frame ) ) ), 44740 ) ); /* Q0 */
2147 34380 : move16();
2148 34380 : old_out_len = extract_l( Mpy_32_32( imult3216( sts[0]->output_Fs, STEREO_MDCT2DFT_FADE_LEN_48k ), 44740 ) ); /* Q0 */
2149 34380 : move16();
2150 :
2151 : /* update buffers used for fading when switching to DFT Stereo */
2152 34380 : v_add_fx( sts[0]->hHQ_core->old_out_LB_fx32 + nsLB, sts[1]->hHQ_core->old_out_LB_fx32 + nsLB, hCPE->old_outLB_mdct_fx, old_outLB_len );
2153 34380 : L_lerp_fx_q11( hCPE->old_outLB_mdct_fx, hCPE->old_outLB_mdct_fx, STEREO_MDCT2DFT_FADE_LEN_48k, old_outLB_len );
2154 6320820 : FOR( i = 0; i < old_outLB_len; i++ )
2155 : {
2156 6286440 : hCPE->old_outLB_mdct_fx[i] = L_shr( hCPE->old_outLB_mdct_fx[i], 1 ); /* Q11 */
2157 6286440 : move32();
2158 : }
2159 34380 : v_add_fx( sts[0]->hHQ_core->old_out_fx32 + ns, sts[1]->hHQ_core->old_out_fx32 + ns, hCPE->old_out_mdct_fx, old_out_len ); /* exp(exp_old_out) */
2160 3070740 : FOR( i = 0; i < old_out_len; i++ )
2161 : {
2162 3036360 : hCPE->old_out_mdct_fx[i] = L_shr( hCPE->old_out_mdct_fx[i], 1 ); /* q_old_out_mdct */
2163 3036360 : move32();
2164 : }
2165 : }
2166 :
2167 68760 : IF( n == 0 )
2168 : {
2169 34380 : set32_fx( hCPE->input_mem_BPF_fx[n], 0, STEREO_DFT32MS_OVL_16k );
2170 : }
2171 : }
2172 :
2173 : /* update ovl buffer for possible switching from TD stereo SCh ACELP frame to MDCT stereo TCX frame */
2174 187035 : test();
2175 187035 : test();
2176 187035 : IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && EQ_16( n, 1 ) && sts[n]->hTcxDec == NULL )
2177 : {
2178 3751 : Copy32( output_fx + shr( sts[n]->L_frame, 1 ), hCPE->hStereoTD->TCX_old_syn_Overl_fx, shr( sts[n]->L_frame, 1 ) ); /* Q11*/
2179 : }
2180 :
2181 187035 : return;
2182 : }
2183 : /*-------------------------------------------------------------------*
2184 : * Function stereo_mdct2dft_update()
2185 : *
2186 : * update OLA buffers - needed for switching from MDCT stereo to DFT stereo
2187 : *-------------------------------------------------------------------*/
2188 660 : void stereo_mdct2dft_update_fx(
2189 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
2190 : Word32 output0_fx[], /* i/o: synthesis @internal Fs, ch0 Q11*/
2191 : Word32 synth0_fx[] /* i/o: synthesis @output Fs, ch0 Q11*/
2192 : )
2193 : {
2194 : Word16 i;
2195 : Word16 fade_len, fade_len_LB;
2196 660 : Word32 tmpF_fx = 0;
2197 : Decoder_State *st;
2198 :
2199 660 : IF( hCPE == NULL )
2200 : {
2201 0 : return;
2202 : }
2203 :
2204 660 : st = hCPE->hCoreCoder[0];
2205 :
2206 660 : fade_len = extract_l( Mpy_32_32( imult3216( st->output_Fs, STEREO_MDCT2DFT_FADE_LEN_48k ), 44740 ) ); // 1/48000 = 44740 (Q31) /* Q0 */
2207 660 : fade_len_LB = extract_l( Mpy_32_32( imult3216( 3 * STEREO_MDCT2DFT_FADE_LEN_48k * FRAMES_PER_SEC, st->L_frame ), 44740 ) ); // 1/48000 = 44740 (Q31) /* Q0 */
2208 660 : SWITCH( st->output_Fs )
2209 : {
2210 51 : case 16000:
2211 51 : tmpF_fx = 53687092; /* Q31 */
2212 51 : move32();
2213 51 : BREAK;
2214 133 : case 32000:
2215 133 : tmpF_fx = 26843546; /* Q31 */
2216 133 : move32();
2217 133 : BREAK;
2218 476 : case 48000:
2219 476 : tmpF_fx = 17895698; /* Q31 */
2220 476 : move32();
2221 476 : BREAK;
2222 : }
2223 70460 : FOR( i = 0; i < fade_len; i++ )
2224 : {
2225 : Word32 descen_gain;
2226 69800 : IF( EQ_16( i, 0 ) )
2227 : {
2228 660 : descen_gain = ONE_IN_Q31; /* Q31 */
2229 660 : move32();
2230 : }
2231 : ELSE
2232 : {
2233 69140 : descen_gain = W_extract_l( W_mult0_32_32( ( sub( fade_len, i ) ), tmpF_fx ) ); /* Q31 */
2234 : }
2235 :
2236 69800 : Word32 temp_a = Mpy_32_32( hCPE->old_out_mdct_fx[i], descen_gain ); /* Q11 */
2237 69800 : Word32 ascend_gain = W_extract_l( W_mult0_32_32( i, tmpF_fx ) ); /* Q31 */
2238 69800 : Word32 temp_b = Mpy_32_32( synth0_fx[i], ascend_gain ); /* Q11 */
2239 69800 : synth0_fx[i] = L_add_sat( temp_a, temp_b ); /* Q11 */
2240 69800 : move32();
2241 : }
2242 :
2243 660 : SWITCH( st->L_frame )
2244 : {
2245 0 : case 80:
2246 0 : tmpF_fx = 71582792; /* Q31 */
2247 0 : move32();
2248 0 : BREAK;
2249 0 : case 160:
2250 0 : tmpF_fx = 35791396; /* Q31 */
2251 0 : move32();
2252 0 : BREAK;
2253 400 : case 256:
2254 400 : tmpF_fx = 22369622; /* Q31 */
2255 400 : move32();
2256 400 : BREAK;
2257 260 : case 320:
2258 260 : tmpF_fx = 17895698; /* Q31 */
2259 260 : move32();
2260 260 : BREAK;
2261 0 : case 512:
2262 0 : tmpF_fx = 11184811; /* Q31 */
2263 0 : move32();
2264 0 : BREAK;
2265 0 : case 640:
2266 0 : tmpF_fx = 8947849; /* Q31 */
2267 0 : move32();
2268 0 : BREAK;
2269 0 : case 960:
2270 0 : tmpF_fx = 5965232; /* Q31 */
2271 0 : move32();
2272 0 : BREAK;
2273 0 : default:
2274 0 : assert( 0 );
2275 : }
2276 70260 : FOR( i = 0; i < fade_len_LB; i++ )
2277 : {
2278 : Word32 descen_gain;
2279 69600 : IF( i == 0 )
2280 : {
2281 660 : descen_gain = ONE_IN_Q31; /* Q31 */
2282 660 : move32();
2283 : }
2284 : ELSE
2285 : {
2286 68940 : descen_gain = W_extract_l( W_mult0_32_32( sub( fade_len_LB, i ), tmpF_fx ) ); /* Q31 */
2287 : }
2288 69600 : Word32 temp_a = Mpy_32_32( hCPE->old_outLB_mdct_fx[i], descen_gain ); /* Q11 */
2289 69600 : Word32 ascend_gain = W_extract_l( W_mult0_32_32( i, tmpF_fx ) ); /* Q31 */
2290 69600 : Word32 temp_b = Mpy_32_32( output0_fx[i], ascend_gain ); /* Q11 */
2291 69600 : output0_fx[i] = L_add_sat( temp_a, temp_b ); /* Q11 */
2292 : }
2293 :
2294 660 : return;
2295 : }
2296 :
2297 :
2298 320 : static Word32 ncross_corr_self_fx(
2299 : Word32 *signal_fx, /* Q11*/
2300 : const Word16 x, /* Q0 */
2301 : const Word16 y, /* Q0 */
2302 : const Word16 corr_len, /* Q0 */
2303 : const Word16 subsampling /* Q0 */
2304 : )
2305 : {
2306 : Word64 c_c_fx;
2307 : Word32 c_c_fx_return;
2308 : Word64 energy_x_fx, energy_y_fx;
2309 : #ifndef OPT_STEREO_32KBPS_V1
2310 : Word16 c_c_fx_q;
2311 : Word64 energy_xy_fx;
2312 : #endif /* OPT_STEREO_32KBPS_V1 */
2313 : UWord16 j;
2314 : Word32 *signal_a_fx, *signal_b_fx;
2315 : Word32 temp_x, temp_y;
2316 : Word16 headroom_left_x, headroom_left_y;
2317 : #ifndef OPT_STEREO_32KBPS_V1
2318 : Word16 x_inv_q, y_inv_q;
2319 : Word16 x_q, y_q;
2320 : Word16 res_q;
2321 : #endif /* OPT_STEREO_32KBPS_V1 */
2322 320 : c_c_fx = 0;
2323 320 : move64();
2324 320 : energy_x_fx = 0;
2325 320 : move64();
2326 320 : energy_y_fx = 0;
2327 320 : move64();
2328 320 : signal_a_fx = &signal_fx[x]; /* Q11 */
2329 320 : signal_b_fx = &signal_fx[y]; /* Q11 */
2330 2880 : FOR( j = 0; j < corr_len; j += subsampling )
2331 : {
2332 : #ifdef OPT_STEREO_32KBPS_V1
2333 2560 : c_c_fx = W_mac_32_32( c_c_fx, signal_a_fx[j], signal_b_fx[j] ); /* 2 * Q11 + 1*/
2334 2560 : energy_x_fx = W_mac_32_32( energy_x_fx, signal_a_fx[j], signal_a_fx[j] ); /* 2 * Q11+ 1 */
2335 2560 : energy_y_fx = W_mac_32_32( energy_y_fx, signal_b_fx[j], signal_b_fx[j] ); /* 2 * Q11+ 1 */
2336 : #else /* OPT_STEREO_32KBPS_V1 */
2337 : c_c_fx = W_add( c_c_fx, W_mult0_32_32( ( signal_a_fx[j] ), ( signal_b_fx[j] ) ) ); /* 2 * Q11 */
2338 : energy_x_fx = W_add( energy_x_fx, W_mult0_32_32( ( signal_a_fx[j] ), ( signal_a_fx[j] ) ) ); /* 2 * Q11 */
2339 : energy_y_fx = W_add( energy_y_fx, W_mult0_32_32( ( signal_b_fx[j] ), ( signal_b_fx[j] ) ) ); /* 2 * Q11 */
2340 : #endif /* OPT_STEREO_32KBPS_V1 */
2341 : }
2342 :
2343 320 : headroom_left_x = W_norm( energy_x_fx );
2344 320 : headroom_left_y = W_norm( energy_y_fx );
2345 : #ifdef OPT_STEREO_32KBPS_V1
2346 320 : temp_x = W_extract_h( W_shl( energy_x_fx, headroom_left_x ) ); // Q23 + headroom_left_x -32
2347 320 : temp_y = W_extract_h( W_shl( energy_y_fx, headroom_left_y ) ); // Q23 + headroom_left_y -32
2348 320 : Word64 prod = W_mult0_32_32( temp_x, temp_y ); // Q(headroom_left_x + headroom_left_y - 18)
2349 320 : Word16 q_prod = W_norm( prod );
2350 320 : Word32 energy = W_extract_h( W_shl( prod, q_prod ) ); // Q(headroom_left_x + headroom_left_y + q_prod - 18) - 32
2351 320 : q_prod = sub( 81, add( add( headroom_left_x, headroom_left_y ), q_prod ) );
2352 320 : energy = Sqrt32( energy, &q_prod );
2353 :
2354 320 : IF( LE_32( energy, L_shl_sat( 1, sub( 31, q_prod ) ) ) )
2355 : {
2356 0 : c_c_fx_return = W_shl_sat_l( c_c_fx, 31 - ( 2 * OUTPUT_Q + 1 ) ); // Q31
2357 : }
2358 : ELSE
2359 : {
2360 : // Maximize c_c_fx
2361 320 : Word16 q_cc = W_norm( c_c_fx );
2362 320 : Word32 num = W_extract_h( W_shl( c_c_fx, q_cc ) ); // Q(23 + q_cc - 32) -> e(40 - q_cc)
2363 : Word16 quo_e;
2364 320 : num = BASOP_Util_Divide3232_Scale_newton( num, energy, &quo_e );
2365 320 : quo_e = add( sub( sub( 40, q_cc ), q_prod ), quo_e );
2366 320 : c_c_fx_return = L_shl_sat( num, quo_e ); // Q31
2367 : }
2368 : #else /* OPT_STEREO_32KBPS_V1 */
2369 : IF( LT_16( headroom_left_x, 32 ) )
2370 : {
2371 : energy_x_fx = W_shr( energy_x_fx, sub( 32, headroom_left_x ) ); /* 2 * Q11 - (32 -headroom_left_x) */
2372 : x_q = sub( 31, ( sub( ( 2 * OUTPUT_Q ), sub( 32, headroom_left_x ) ) ) );
2373 : }
2374 : ELSE
2375 : {
2376 : x_q = 31 - ( 2 * OUTPUT_Q );
2377 : }
2378 : IF( LT_16( headroom_left_y, 32 ) )
2379 : {
2380 : energy_y_fx = W_shr( energy_y_fx, sub( 32, headroom_left_y ) ); /* 2 * Q11 - (32 -headroom_left_y) */
2381 : y_q = sub( 31, ( sub( ( 2 * OUTPUT_Q ), ( sub( 32, headroom_left_y ) ) ) ) );
2382 : }
2383 : ELSE
2384 : {
2385 : y_q = 31 - ( 2 * OUTPUT_Q );
2386 : }
2387 :
2388 : x_inv_q = x_q;
2389 : move16();
2390 : y_inv_q = y_q;
2391 : move16();
2392 :
2393 : temp_x = Sqrt32( W_extract_l( energy_x_fx ), &x_q );
2394 : IF( x_q < 0 )
2395 : {
2396 : temp_x = L_shr( temp_x, i_mult( -1, x_q ) ); /* Q0 */
2397 : x_q = 0;
2398 : move16();
2399 : }
2400 :
2401 : temp_y = Sqrt32( W_extract_l( energy_y_fx ), &y_q );
2402 : IF( y_q < 0 )
2403 : {
2404 : temp_y = L_shr( temp_y, i_mult( -1, y_q ) ); /* Q0 */
2405 : y_q = 0;
2406 : move16();
2407 : }
2408 :
2409 : energy_xy_fx = Mpy_32_32( temp_x, temp_y ); /* x_q + y_q - 31 */
2410 : res_q = add( y_q, x_q );
2411 :
2412 : IF( ( energy_xy_fx < L_shr( ONE_IN_Q31, res_q ) ) || energy_xy_fx == 0 )
2413 : {
2414 : energy_xy_fx = ONE_IN_Q31; /* conceal silent frames */
2415 : res_q = 0;
2416 : c_c_fx = W_shl( c_c_fx, 31 - ( 2 * OUTPUT_Q ) ); /* Q31 */
2417 : }
2418 : ELSE
2419 : {
2420 : Word32 temp_x_inv = ISqrt32( W_extract_l( energy_x_fx ), &x_inv_q );
2421 : Word32 temp_y_inv = ISqrt32( W_extract_l( energy_y_fx ), &y_inv_q );
2422 : Word16 headroom_left_c_c;
2423 : energy_xy_fx = Mpy_32_32( temp_x_inv, temp_y_inv ); /* exp(x_inv_q + y_inv_q) */
2424 : res_q = add( x_inv_q, y_inv_q );
2425 : headroom_left_c_c = W_norm( c_c_fx );
2426 : c_c_fx_q = OUTPUT_Q * 2;
2427 : move16();
2428 : IF( LT_16( headroom_left_c_c, 32 ) )
2429 : {
2430 : c_c_fx = W_shr( c_c_fx, extract_l( sub( 32, headroom_left_c_c ) ) ); /* c_c_fx_q - (32 - headroom_left_c_c) */
2431 : c_c_fx_q = sub( c_c_fx_q, sub( 32, headroom_left_c_c ) );
2432 : }
2433 : c_c_fx = W_deposit32_l( Mpy_32_32( W_extract_l( c_c_fx ), W_extract_l( energy_xy_fx ) ) ); /* c_c_fx_q + (31 - res_q) - 31 */
2434 : c_c_fx_q = add( c_c_fx_q, sub( sub( 31, res_q ), 31 ) );
2435 : c_c_fx = W_extract_l( W_shl_sat_l( c_c_fx, extract_l( sub( 31, c_c_fx_q ) ) ) ); /* Q31 */
2436 : }
2437 :
2438 : c_c_fx_return = W_extract_l( c_c_fx ); /* Q31 */
2439 : move32();
2440 : #endif /* OPT_STEREO_32KBPS_V1 */
2441 320 : return c_c_fx_return;
2442 : }
2443 :
2444 :
2445 : /*-------------------------------------------------------------------*
2446 : * Function smooth_dft2td_transition()
2447 : *
2448 : * apply smoothing to avoid discontinuities and energy variation when
2449 : * switching from DFT stereo to TD stereo
2450 : *-------------------------------------------------------------------*/
2451 :
2452 145402 : void smooth_dft2td_transition_fx(
2453 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
2454 : Word32 *output_fx[CPE_CHANNELS], /* i/o: synthesis @external Fs Q11*/
2455 : const Word16 output_frame /* i : output frame lenght Q0*/
2456 : )
2457 : {
2458 :
2459 : Decoder_State **sts;
2460 : Word16 ipit, i, ipit_max, ipit_min, idiff, ilen, j, ch, lsearch, corr_len, subsampl;
2461 : Word32 flen_fx, ftmp_corr_fx, fmaxcorr_fx, fac_fs_fx;
2462 : Word32 tmp_out_fx[L_FRAME48k + L_FRAME48k / 2], tmp_out2_fx[L_FRAME48k], *ptO2_fx, *ptO_fx, *pt1_fx, *ptE_fx;
2463 :
2464 145402 : IF( hCPE == NULL )
2465 : {
2466 0 : return;
2467 : }
2468 :
2469 : /* initialization */
2470 145402 : sts = hCPE->hCoreCoder;
2471 145402 : test();
2472 145402 : test();
2473 145402 : test();
2474 145402 : test();
2475 145402 : test();
2476 145402 : test();
2477 145402 : test();
2478 145402 : IF( ( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && ( EQ_16( hCPE->last_element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->last_element_mode, IVAS_CPE_MDCT ) ) && ( EQ_16( sts[0]->clas_dec, VOICED_CLAS ) && LT_16( sts[0]->coder_type, TRANSITION ) && GT_16( sts[0]->coder_type, UNVOICED ) && GT_16( sts[0]->last_coder_type, UNVOICED ) && GT_16( sts[1]->coder_type, UNVOICED ) ) ) )
2479 : {
2480 : /* length of OVA */
2481 1 : ilen = shr( output_frame, 1 ); /* Q0 */
2482 :
2483 : /* correlation length */
2484 1 : corr_len = idiv1616( output_frame, 20 ); /* Q0 */
2485 1 : subsampl = 4;
2486 :
2487 3 : FOR( ch = 0; ch < hCPE->nchan_out; ch++ )
2488 : {
2489 : /* core to external sampling frequency ratio */
2490 2 : Word16 q = norm_l( output_frame );
2491 2 : Word32 check = BASOP_Util_Divide3232_Scale( output_frame, sts[ch]->L_frame, &q ); /* q */
2492 2 : fac_fs_fx = imult3216( check, shl( 1, add( 26 - 15, q ) ) ); /* Q11 */
2493 : /* Find minimum and maximum pitch*/
2494 2 : ipit_min = add( minimum_32_fx( sts[ch]->old_pitch_buf_fx + 4, 4, &flen_fx ), 4 ); /* Q0 */
2495 2 : ipit_max = add( maximum_32_fx( sts[ch]->old_pitch_buf_fx + 4, 4, &flen_fx ), 4 ); /* Q0 */
2496 2 : ipit_min = (Word16) L_shr( L_add( Mpy_32_32( sts[ch]->old_pitch_buf_fx[ipit_min], fac_fs_fx ), ONE_IN_Q10 ), 11 ); /* Q0 */
2497 2 : ipit_max = (Word16) L_shr( L_add( Mpy_32_32( sts[ch]->old_pitch_buf_fx[ipit_max], fac_fs_fx ), ONE_IN_Q10 ), 11 ); /* Q0 */
2498 :
2499 2 : IF( GT_16( add( ipit_max, corr_len ), ilen ) ) /*ensure the search is performed on the available memory*/
2500 : {
2501 1 : CONTINUE;
2502 : }
2503 :
2504 1 : lsearch = add( sub( ipit_max, ipit_min ), corr_len ); /* Q0 */
2505 1 : lsearch = s_min( lsearch, shr( output_frame, 2 ) ); /* Q0 */
2506 :
2507 : /* ptr init for search of the best correlation in the past frame */
2508 1 : ptE_fx = hCPE->prev_synth_chs_fx[ch] + sub( output_frame, add( ipit_max, corr_len ) ); /* Q11 */
2509 :
2510 1 : idiff = 0;
2511 1 : move16();
2512 1 : fmaxcorr_fx = -ONE_IN_Q31; /* Q31 */
2513 1 : move32();
2514 161 : FOR( i = 0; i < lsearch; i++ )
2515 : {
2516 160 : ftmp_corr_fx = ncross_corr_self_fx( ptE_fx, i, ipit_max, corr_len, subsampl ); /* Q31 */
2517 160 : IF( GT_32( ftmp_corr_fx, fmaxcorr_fx ) )
2518 : {
2519 14 : idiff = i; /* Q0 */
2520 14 : move16();
2521 : }
2522 160 : fmaxcorr_fx = L_max( fmaxcorr_fx, ftmp_corr_fx ); /* Q11 */
2523 : }
2524 :
2525 1 : ipit = sub( ipit_max, idiff ); /* Q0 */
2526 :
2527 1 : ptO_fx = tmp_out_fx + output_frame; /* Q11 */
2528 :
2529 : /* If the correlation is too low, don't use the prediction */
2530 1 : IF( LT_32( fmaxcorr_fx, DFT2TD_CORR_THRESH_FX ) )
2531 : {
2532 0 : Copy32( &output_fx[ch][0], ptO_fx, ilen ); /* Q11 */
2533 : }
2534 : ELSE
2535 : {
2536 1 : Copy32( hCPE->prev_synth_chs_fx[ch], tmp_out_fx, output_frame ); /* Q11 */
2537 1 : pt1_fx = tmp_out_fx + output_frame - ipit; /* Q11 */
2538 321 : FOR( i = 0; i < ilen; i++ )
2539 : {
2540 320 : ptO_fx[i] = pt1_fx[i]; /* Q11 */
2541 320 : move32();
2542 : }
2543 : }
2544 :
2545 : /* Set buffer for the reserved buffer of the current frame */
2546 1 : ptO2_fx = tmp_out2_fx + sub( output_frame, ilen ); /* Q11 */
2547 1 : set32_fx( tmp_out2_fx + sub( output_frame, ilen ), 0, ilen );
2548 641 : FOR( i = 0; i < output_frame; i++ )
2549 : {
2550 640 : tmp_out2_fx[i] = output_fx[ch][output_frame - 1 - i]; /* Q11 */
2551 640 : move32();
2552 : }
2553 : /* ptr init for search of the best correlation of the current frame */
2554 1 : ptE_fx = ptO2_fx - ( ipit_max + corr_len ); /* Q11 */
2555 :
2556 1 : idiff = 0;
2557 1 : move16();
2558 1 : fmaxcorr_fx = -ONE_IN_Q31; /* Q31 */
2559 1 : move32();
2560 161 : FOR( i = 0; i < lsearch; i++ )
2561 : {
2562 160 : ftmp_corr_fx = ncross_corr_self_fx( ptE_fx, i, ipit_max, corr_len, subsampl ); /* Q31 */
2563 160 : IF( GT_32( ftmp_corr_fx, fmaxcorr_fx ) )
2564 : {
2565 9 : idiff = i; /* Q0 */
2566 9 : move16();
2567 : }
2568 160 : fmaxcorr_fx = L_max( fmaxcorr_fx, ftmp_corr_fx ); /* Q31 */
2569 : }
2570 :
2571 :
2572 1 : ipit = sub( ipit_max, idiff ); /* Q0 */
2573 :
2574 : /* If the correlation is too low, don't use the prediction */
2575 1 : IF( GT_32( fmaxcorr_fx, DFT2TD_CORR_THRESH_FX ) )
2576 : {
2577 1 : pt1_fx = tmp_out2_fx + sub( output_frame, add( ilen, ipit ) ); /* Q11 */
2578 321 : FOR( i = 0; i < ilen; i++ )
2579 : {
2580 320 : ptO2_fx[i] = pt1_fx[i]; /* Q11 */
2581 320 : move32();
2582 : }
2583 : }
2584 :
2585 : /* perform OVA between predicted signals */
2586 1 : SWITCH( ilen )
2587 : {
2588 0 : case 160:
2589 0 : flen_fx = 13421772; /* Q31 */
2590 0 : move32();
2591 0 : BREAK;
2592 1 : case 320:
2593 1 : flen_fx = 6710886; /* Q31 */
2594 1 : move32();
2595 1 : BREAK;
2596 0 : case 480:
2597 0 : flen_fx = 4473924; /* Q31 */
2598 0 : move32();
2599 0 : BREAK;
2600 0 : default:
2601 0 : assert( 0 );
2602 : BREAK;
2603 : }
2604 5 : FOR( i = 0; i < 4; i++ )
2605 : {
2606 4 : Word64 temp_a = W_shr( W_add( W_mult0_32_32( sub( 4, i ), output_fx[ch][i] ), W_mult0_32_32( i, ptO_fx[i] ) ), 2 ) * ( ilen - i ); /* Q11 */
2607 4 : move64();
2608 4 : Word64 temp_b = W_mult0_32_32( tmp_out2_fx[output_frame - 1 - i], i ); /* Q11 */
2609 4 : output_fx[ch][i] = W_extract_l( W_shr( ( W_add( temp_a, temp_b ) * flen_fx ), 31 ) ); /* Q11 */
2610 4 : move32();
2611 : }
2612 313 : FOR( ; i < ilen - 4; i++ )
2613 : {
2614 312 : Word64 temp_a = W_mult0_32_32( ptO_fx[i], sub( ilen, i ) ); /* Q11 */
2615 312 : Word64 temp_b = W_mult0_32_32( tmp_out2_fx[output_frame - 1 - i], i ); /* Q11 */
2616 312 : output_fx[ch][i] = W_extract_l( W_shr( ( W_add( temp_a, temp_b ) * flen_fx ), 31 ) ); /* Q11 */
2617 312 : move32();
2618 : }
2619 1 : j = 0;
2620 1 : move16();
2621 5 : FOR( ; i < ilen; i++ )
2622 : {
2623 4 : Word64 temp_a = W_mult0_32_32( ptO_fx[i], sub( ilen, i ) ); /* Q11 */
2624 4 : Word64 temp_b = W_shr( ( W_add( W_mult0_32_32( sub( 4, j ), tmp_out2_fx[output_frame - 1 - i] ), W_mult0_32_32( j, output_fx[ch][i] ) ) * i ), 2 ); /* Q11 */
2625 4 : output_fx[ch][i] = W_extract_l( W_shr( ( W_add( temp_a, temp_b ) * flen_fx ), 31 ) ); /* Q11 */
2626 4 : move32();
2627 4 : j = add( j, 1 );
2628 : }
2629 : }
2630 : }
2631 145401 : ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
2632 : {
2633 : /* Updates */
2634 154795 : FOR( ch = 0; ch < hCPE->nchan_out; ch++ )
2635 : {
2636 95748 : Copy32( output_fx[ch], hCPE->prev_synth_chs_fx[ch], output_frame ); /* Q11 */
2637 : }
2638 : }
2639 :
2640 145402 : return;
2641 : }
|