Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : #include <stdint.h>
34 : #include "options.h"
35 : #ifdef DEBUGGING
36 : #include "debug.h"
37 : #endif
38 : #include "cnst.h"
39 : #include "rom_com.h"
40 : #include "prot.h"
41 : #include "ivas_prot.h"
42 : #include "ivas_rom_com.h"
43 : #include "assert.h"
44 : #include "wmc_auto.h"
45 : #include <math.h>
46 :
47 : /*-------------------------------------------------------------------*
48 : * Local constants
49 : *-------------------------------------------------------------------*/
50 :
51 : #define DFT2TD_CORR_THRESH 0.9f
52 :
53 :
54 : /*-------------------------------------------------------------------*
55 : * Function allocate_CoreCoder_TCX()
56 : *
57 : * Allocate CoreCoder TCX modules
58 : *-------------------------------------------------------------------*/
59 :
60 2223 : static ivas_error allocate_CoreCoder_TCX(
61 : DEC_CORE_HANDLE st /* i/o: Core decoder state structure */
62 : )
63 : {
64 2223 : if ( st->hTcxDec == NULL )
65 : {
66 2223 : if ( ( st->hTcxDec = (TCX_DEC_HANDLE) malloc( sizeof( TCX_DEC_DATA ) ) ) == NULL )
67 : {
68 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for hTcxDec" ) );
69 : }
70 :
71 2223 : reset_tcx_overl_buf( st->hTcxDec );
72 :
73 2223 : set_zero( st->hTcxDec->syn_OverlFB, L_FRAME48k / 2 );
74 2223 : set_zero( st->hTcxDec->old_synth, OLD_SYNTH_INTERNAL_DEC );
75 2223 : set_zero( st->hTcxDec->synth_history, L_PROT48k + L_FRAME48k );
76 : }
77 :
78 2223 : if ( st->hTcxCfg == NULL )
79 : {
80 2223 : if ( ( st->hTcxCfg = (TCX_CONFIG_HANDLE) malloc( sizeof( TCX_config ) ) ) == NULL )
81 : {
82 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for hTcxCfg" ) );
83 : }
84 : }
85 :
86 : /* allocated TCX-LTP structure for second channel */
87 2223 : if ( st->hTcxLtpDec == NULL )
88 : {
89 2223 : if ( ( st->hTcxLtpDec = (TCX_LTP_DEC_HANDLE) malloc( sizeof( TCX_LTP_DEC_DATA ) ) ) == NULL )
90 : {
91 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for TCX-LTP handle\n" ) );
92 : }
93 :
94 2223 : tcxltp_dec_init( st->hTcxLtpDec, 0, st->last_codec_mode, st->element_mode, st->pit_max, st->sr_core );
95 : }
96 :
97 : /* allocate HQ structure */
98 2223 : if ( st->hHQ_core == NULL )
99 : {
100 2223 : if ( ( st->hHQ_core = (HQ_DEC_HANDLE) malloc( sizeof( HQ_DEC_DATA ) ) ) == NULL )
101 : {
102 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for HQ core\n" ) );
103 : }
104 :
105 2223 : HQ_core_dec_init( st->hHQ_core );
106 : }
107 :
108 2223 : if ( st->hIGFDec == NULL )
109 : {
110 2223 : if ( ( st->hIGFDec = (IGF_DEC_INSTANCE_HANDLE) malloc( sizeof( IGFDEC_INSTANCE ) ) ) == NULL )
111 : {
112 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IGF\n" ) );
113 : }
114 :
115 2223 : st->igf = 0;
116 2223 : init_igf_dec( st->hIGFDec );
117 : }
118 :
119 2223 : if ( st->hTonalMDCTConc == NULL )
120 : {
121 2223 : 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 2223 : st->last_con_tcx = 0;
128 2223 : st->hTonalMDCTConc->nSamples = 0;
129 :
130 2223 : return IVAS_ERR_OK;
131 : }
132 :
133 :
134 : /*-------------------------------------------------------------------*
135 : * Function allocate_CoreCoder()
136 : *
137 : * Allocate CoreCoder modules
138 : *-------------------------------------------------------------------*/
139 :
140 2349 : static ivas_error allocate_CoreCoder(
141 : DEC_CORE_HANDLE st /* i/o: Core decoder state structure */
142 : )
143 : {
144 : ivas_error error;
145 :
146 2349 : error = IVAS_ERR_OK;
147 :
148 2349 : if ( st->hGSCDec == NULL )
149 : {
150 2349 : if ( ( st->hGSCDec = (GSC_DEC_HANDLE) malloc( sizeof( GSC_DEC_DATA ) ) ) == NULL )
151 : {
152 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for GSC\n" ) );
153 : }
154 :
155 2349 : GSC_dec_init( st->hGSCDec );
156 : }
157 :
158 2349 : if ( st->hPFstat == NULL )
159 : {
160 2349 : if ( ( st->hPFstat = (PFSTAT_HANDLE) malloc( sizeof( PFSTAT ) ) ) == NULL )
161 : {
162 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for NB/formant postflter\n" ) );
163 : }
164 :
165 2349 : Init_post_filter( st->hPFstat );
166 2349 : st->psf_lp_noise = 0.0f;
167 : }
168 :
169 2349 : if ( st->hMusicPF == NULL )
170 : {
171 2349 : if ( ( st->hMusicPF = (MUSIC_POSTFILT_HANDLE) malloc( sizeof( MUSIC_POSTFILT_DATA ) ) ) == NULL )
172 : {
173 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LD music postflter\n" ) );
174 : }
175 :
176 2349 : music_postfilt_init( st->hMusicPF );
177 : }
178 :
179 2349 : if ( st->hBPF == NULL )
180 : {
181 2349 : if ( ( st->hBPF = (BPF_DEC_HANDLE) malloc( sizeof( BPF_DEC_DATA ) ) ) == NULL )
182 : {
183 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for BPF\n" ) );
184 : }
185 :
186 2349 : bass_psfilter_init( st->hBPF );
187 : }
188 :
189 2349 : if ( st->hBWE_zero == NULL )
190 : {
191 2349 : if ( ( st->hBWE_zero = (ZERO_BWE_DEC_HANDLE) malloc( sizeof( ZERO_BWE_DEC_DATA ) ) ) == NULL )
192 : {
193 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for zero BWE\n" ) );
194 : }
195 :
196 2349 : hf_synth_init( st->hBWE_zero );
197 : }
198 :
199 2349 : if ( st->cldfbAna == NULL )
200 : {
201 : /* open analysis for max. sampling rate 48kHz */
202 162 : if ( ( error = openCldfb( &st->cldfbAna, CLDFB_ANALYSIS, 48000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
203 : {
204 0 : return error;
205 : }
206 : }
207 :
208 2349 : if ( st->cldfbBPF == NULL )
209 : {
210 : /* open analysis BPF for max. internal sampling rate 16kHz */
211 162 : if ( ( error = openCldfb( &st->cldfbBPF, CLDFB_ANALYSIS, 16000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
212 : {
213 0 : return error;
214 : }
215 : }
216 :
217 2349 : return error;
218 : }
219 :
220 :
221 : /*-------------------------------------------------------------------*
222 : * Function deallocate_CoreCoder_TCX()
223 : *
224 : * Deallocate CoreCoder TCX modules
225 : *-------------------------------------------------------------------*/
226 :
227 2490 : static void deallocate_CoreCoder_TCX(
228 : DEC_CORE_HANDLE st /* i/o: Core decoder state structure */
229 : )
230 : {
231 2490 : if ( st->hTcxDec != NULL )
232 : {
233 2172 : free( st->hTcxDec );
234 2172 : st->hTcxDec = NULL;
235 : }
236 :
237 2490 : if ( st->hTcxCfg != NULL )
238 : {
239 2172 : free( st->hTcxCfg );
240 2172 : st->hTcxCfg = NULL;
241 : }
242 :
243 2490 : if ( st->hIGFDec != NULL )
244 : {
245 2172 : free( st->hIGFDec );
246 2172 : st->hIGFDec = NULL;
247 : }
248 :
249 2490 : if ( st->hTonalMDCTConc != NULL )
250 : {
251 2172 : free( st->hTonalMDCTConc );
252 2172 : st->hTonalMDCTConc = NULL;
253 : }
254 :
255 2490 : return;
256 : }
257 :
258 :
259 : /*-------------------------------------------------------------------*
260 : * Function deallocate_CoreCoder()
261 : *
262 : * Deallocate CoreCoder modules
263 : *-------------------------------------------------------------------*/
264 :
265 6759 : static void deallocate_CoreCoder(
266 : DEC_CORE_HANDLE st /* i/o: Core decoder state structure */
267 : )
268 : {
269 6759 : if ( st->hGSCDec != NULL )
270 : {
271 2397 : free( st->hGSCDec );
272 2397 : st->hGSCDec = NULL;
273 : }
274 :
275 6759 : if ( st->hPFstat != NULL )
276 : {
277 2397 : free( st->hPFstat );
278 2397 : st->hPFstat = NULL;
279 : }
280 :
281 6759 : if ( st->hMusicPF != NULL )
282 : {
283 2397 : free( st->hMusicPF );
284 2397 : st->hMusicPF = NULL;
285 : }
286 :
287 6759 : if ( st->hBPF != NULL )
288 : {
289 2397 : free( st->hBPF );
290 2397 : st->hBPF = NULL;
291 : }
292 :
293 6759 : if ( st->hBWE_zero != NULL )
294 : {
295 2397 : free( st->hBWE_zero );
296 2397 : st->hBWE_zero = NULL;
297 : }
298 :
299 : /* CLDFB BPF & resampling tools */
300 6759 : if ( st->element_mode != IVAS_CPE_MDCT )
301 : {
302 2313 : deleteCldfb( &st->cldfbAna ); /* delete analysis at max. sampling rate 48kHz */
303 2313 : deleteCldfb( &st->cldfbBPF ); /* delete analysis BPF at max. internal sampling rate 16kHz */
304 : }
305 :
306 6759 : if ( st->element_mode != IVAS_CPE_MDCT )
307 : {
308 2313 : deallocate_CoreCoder_TCX( st );
309 : }
310 :
311 6759 : return;
312 : }
313 :
314 :
315 : /*-------------------------------------------------------------------*
316 : * Function cpy_tcx_ltp_data()
317 : *
318 : * In case of MDCT<->DFT Stereo switching, copy TCX-LTP data of the right
319 : * channel to the correct structure in the new mode
320 : *-------------------------------------------------------------------*/
321 :
322 6576 : static void cpy_tcx_ltp_data(
323 : TCX_LTP_DEC_HANDLE hTcxLtpDecOld, /* i : TCX-LTP structure to copy from */
324 : TCX_LTP_DEC_HANDLE hTcxLtpDecNew, /* o : TCX-LTP structure to copy to */
325 : const int32_t output_Fs /* i : output sampling rate */
326 : )
327 : {
328 6576 : hTcxLtpDecNew->tcxltp_pitch_int_post_prev = hTcxLtpDecOld->tcxltp_pitch_int_post_prev;
329 6576 : hTcxLtpDecNew->tcxltp_pitch_fr_post_prev = hTcxLtpDecOld->tcxltp_pitch_fr_post_prev;
330 6576 : hTcxLtpDecNew->tcxltp_gain_post_prev = hTcxLtpDecOld->tcxltp_gain_post_prev;
331 6576 : hTcxLtpDecNew->tcxltp_filt_idx_prev = hTcxLtpDecOld->tcxltp_filt_idx_prev;
332 :
333 6576 : mvr2r( hTcxLtpDecOld->tcxltp_mem_in, hTcxLtpDecNew->tcxltp_mem_in, (int16_t) ( ( TCXLTP_MAX_DELAY * output_Fs ) / 48000 ) );
334 6576 : mvr2r( hTcxLtpDecOld->tcxltp_mem_out, hTcxLtpDecNew->tcxltp_mem_out, (int16_t) ( ( L_FRAME48k * output_Fs ) / 48000 ) );
335 :
336 6576 : return;
337 : }
338 :
339 :
340 : /*-------------------------------------------------------------------*
341 : * Function stereo_memory_dec()
342 : *
343 : * Dynamically allocate/deallocate data structures depending on the actual CPE mode
344 : *-------------------------------------------------------------------*/
345 :
346 1261158 : ivas_error stereo_memory_dec(
347 : const int32_t ivas_total_brate, /* i : IVAS total bitrate */
348 : CPE_DEC_HANDLE hCPE, /* i : CPE decoder structure */
349 : const int16_t nb_bits_metadata, /* i : number of metadata bits */
350 : const int32_t output_Fs, /* i : output sampling rate */
351 : const IVAS_FORMAT ivas_format, /* i : IVAS format */
352 : const MC_MODE mc_mode, /* i : MC mode */
353 : const int16_t nchan_transport /* i : number of transport channels*/
354 : )
355 : {
356 : DEC_CORE_HANDLE st;
357 : int16_t i, n, delay_comp_DFT;
358 : ivas_error error;
359 :
360 1261158 : error = IVAS_ERR_OK;
361 :
362 1261158 : assert( hCPE->last_element_mode >= IVAS_CPE_DFT && "Switching from SCE to CPE is not a valid configuration!" );
363 :
364 :
365 1261158 : hCPE->hCoreCoder[0]->element_mode = hCPE->element_mode;
366 1261158 : hCPE->hCoreCoder[1]->element_mode = hCPE->element_mode;
367 :
368 : /*--------------------------------------------------------------*
369 : * stereo switching (using parameters that will be freed)
370 : *---------------------------------------------------------------*/
371 :
372 : /* handling of DFT->TD switching */
373 1261158 : if ( hCPE->last_element_mode == IVAS_CPE_DFT && ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_MDCT ) )
374 : {
375 2367 : delay_comp_DFT = NS2SA( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS );
376 :
377 : /* redressing of the DFT OLA part */
378 7101 : for ( n = 0; n < CPE_CHANNELS; n++ )
379 : {
380 601758 : for ( i = delay_comp_DFT; i < hCPE->hStereoDft->dft32ms_ovl; i++ )
381 : {
382 597024 : hCPE->output_mem[n][i - delay_comp_DFT] /= hCPE->hStereoDft->win32ms[STEREO_DFT32MS_STEP * ( hCPE->hStereoDft->dft32ms_ovl - 1 + delay_comp_DFT - i )];
383 : }
384 : }
385 :
386 2367 : if ( hCPE->hCoreCoder[0]->last_core != ACELP_CORE )
387 : {
388 615 : mvr2r( hCPE->hStereoDft->buff_LBTCX_mem, hCPE->input_mem_LB[0], NS2SA( hCPE->hCoreCoder[0]->last_L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
389 : }
390 : }
391 :
392 1261158 : if ( hCPE->last_element_mode == IVAS_CPE_MDCT && hCPE->element_mode == IVAS_CPE_DFT )
393 : {
394 2148 : v_add( hCPE->hCoreCoder[0]->hHQ_core->old_out, hCPE->hCoreCoder[1]->hHQ_core->old_out, hCPE->hCoreCoder[0]->hHQ_core->old_out, (int16_t) ( output_Fs / FRAMES_PER_SEC ) );
395 2148 : v_multc( hCPE->hCoreCoder[0]->hHQ_core->old_out, 0.5f, hCPE->hCoreCoder[0]->hHQ_core->old_out, (int16_t) ( output_Fs / FRAMES_PER_SEC ) );
396 :
397 2148 : v_add( hCPE->hCoreCoder[0]->hHQ_core->old_outLB, hCPE->hCoreCoder[1]->hHQ_core->old_outLB, hCPE->hCoreCoder[0]->hHQ_core->old_outLB, L_FRAME32k );
398 2148 : v_multc( hCPE->hCoreCoder[0]->hHQ_core->old_outLB, 0.5f, hCPE->hCoreCoder[0]->hHQ_core->old_outLB, L_FRAME32k );
399 : }
400 :
401 : /*--------------------------------------------------------------*
402 : * allocate/deallocate data structures
403 : *---------------------------------------------------------------*/
404 :
405 1261158 : if ( hCPE->element_mode != hCPE->last_element_mode )
406 : {
407 : /*--------------------------------------------------------------*
408 : * switching CPE mode to DFT stereo
409 : *---------------------------------------------------------------*/
410 :
411 4713 : if ( hCPE->element_mode == IVAS_CPE_DFT )
412 : {
413 : /* deallocate data structure of the previous CPE mode */
414 2313 : if ( hCPE->hStereoTD != NULL )
415 : {
416 165 : free( hCPE->hStereoTD );
417 165 : hCPE->hStereoTD = NULL;
418 : }
419 :
420 2313 : if ( hCPE->hStereoMdct != NULL )
421 : {
422 2148 : free( hCPE->hStereoMdct );
423 2148 : hCPE->hStereoMdct = NULL;
424 : }
425 :
426 : /* deallocate secondary channel */
427 2313 : deallocate_CoreCoder( hCPE->hCoreCoder[1] );
428 :
429 : /* allocate DFT stereo data structure */
430 2313 : if ( ( error = stereo_dft_dec_create( &( hCPE->hStereoDft ), hCPE->element_brate, output_Fs, 0, nchan_transport ) ) != IVAS_ERR_OK )
431 : {
432 0 : return error;
433 : }
434 :
435 2313 : if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
436 : {
437 2148 : cpy_tcx_ltp_data( hCPE->hCoreCoder[1]->hTcxLtpDec, hCPE->hStereoDft->hTcxLtpDec, output_Fs );
438 2148 : deleteFdCngDec( &hCPE->hCoreCoder[1]->hFdCngDec );
439 : }
440 :
441 : /* memory update - needed in TD stereo, TCX/HQ frame -> DFT stereo, ACELP frame switching */
442 2313 : mvr2r( hCPE->input_mem_LB[0], hCPE->hStereoDft->buff_LBTCX_mem, NS2SA( min( hCPE->hCoreCoder[0]->last_L_frame * FRAMES_PER_SEC, 16000 ), STEREO_DFT32MS_OVL_NS ) );
443 :
444 : /* allocate ICBWE structure */
445 2313 : if ( hCPE->hStereoICBWE == NULL )
446 : {
447 2217 : if ( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
448 : {
449 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
450 : }
451 :
452 2217 : stereo_icBWE_init_dec( hCPE->hStereoICBWE );
453 : }
454 :
455 : /* allocate HQ core */
456 2313 : st = hCPE->hCoreCoder[0];
457 2313 : if ( st->hHQ_core == NULL )
458 : {
459 0 : if ( ( st->hHQ_core = (HQ_DEC_HANDLE) malloc( sizeof( HQ_DEC_DATA ) ) ) == NULL )
460 : {
461 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HQ core\n" ) );
462 : }
463 :
464 0 : HQ_core_dec_init( st->hHQ_core );
465 : }
466 :
467 : /* allocate TD CNG handle */
468 2313 : if ( st->idchan == 0 && st->hTdCngDec == NULL )
469 : {
470 9 : if ( ( st->hTdCngDec = (TD_CNG_DEC_HANDLE) malloc( sizeof( TD_CNG_DEC_DATA ) ) ) == NULL )
471 : {
472 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DTX/TD CNG\n" ) );
473 : }
474 :
475 9 : td_cng_dec_init( st );
476 : }
477 : }
478 :
479 : /*--------------------------------------------------------------*
480 : * switching CPE mode to TD stereo
481 : *---------------------------------------------------------------*/
482 :
483 4713 : if ( hCPE->element_mode == IVAS_CPE_TD )
484 : {
485 : /* deallocate data structure of the previous CPE mode */
486 177 : if ( hCPE->hStereoDft != NULL )
487 : {
488 153 : stereo_dft_dec_destroy( &( hCPE->hStereoDft ) );
489 153 : hCPE->hStereoDft = NULL;
490 : }
491 :
492 177 : if ( hCPE->hStereoMdct != NULL )
493 : {
494 24 : free( hCPE->hStereoMdct );
495 24 : hCPE->hStereoMdct = NULL;
496 : }
497 :
498 : /* deallocated TCX/IGF structures for second channel */
499 177 : deallocate_CoreCoder_TCX( hCPE->hCoreCoder[1] );
500 :
501 177 : if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
502 : {
503 24 : deleteFdCngDec( &hCPE->hCoreCoder[1]->hFdCngDec );
504 : }
505 :
506 : /* allocate TD stereo data structure */
507 177 : if ( hCPE->hStereoTD != NULL )
508 : {
509 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: TD Stereo memory already allocated\n" );
510 : }
511 :
512 177 : if ( ( hCPE->hStereoTD = (STEREO_TD_DEC_DATA_HANDLE) malloc( sizeof( STEREO_TD_DEC_DATA ) ) ) == NULL )
513 : {
514 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
515 : }
516 :
517 177 : stereo_td_init_dec( hCPE->hStereoTD, hCPE->last_element_mode );
518 :
519 : /* allocate CoreCoder secondary channel */
520 177 : if ( ( error = allocate_CoreCoder( hCPE->hCoreCoder[1] ) ) != IVAS_ERR_OK )
521 : {
522 0 : return error;
523 : }
524 : }
525 :
526 : /*--------------------------------------------------------------*
527 : * allocate DFT/TD stereo structures after MDCT stereo frame
528 : *---------------------------------------------------------------*/
529 :
530 4713 : if ( hCPE->last_element_mode == IVAS_CPE_MDCT && ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD ) )
531 : {
532 : /* deallocated TCX-LTP for second channel */
533 2172 : st = hCPE->hCoreCoder[1];
534 2172 : if ( st->hTcxLtpDec != 0 )
535 : {
536 2172 : free( st->hTcxLtpDec );
537 2172 : st->hTcxLtpDec = NULL;
538 : }
539 :
540 2172 : if ( st->element_mode == IVAS_CPE_TD )
541 : {
542 : /* re-use an existing buffer for MDCT->TD stereo switching */
543 : int16_t nZeros, len;
544 24 : nZeros = (int16_t) ( NS2SA( st->output_Fs, N_ZERO_MDCT_NS ) );
545 24 : len = NS2SA( st->output_Fs, 3000000 );
546 24 : mvr2r( st->hHQ_core->old_out + nZeros, hCPE->output_mem[1], len );
547 : }
548 :
549 : /* deallocated HQ-core for second channel */
550 2172 : if ( st->hHQ_core != 0 )
551 : {
552 2172 : free( st->hHQ_core );
553 2172 : st->hHQ_core = NULL;
554 : }
555 :
556 : /* allocate DFT stereo mono DMX data structure */
557 2172 : if ( hCPE->nchan_out == 1 && hCPE->hStereoDftDmx == NULL )
558 : {
559 1275 : if ( ( hCPE->hStereoDftDmx = (STEREO_DFT_DMX_DATA_HANDLE) malloc( sizeof( STEREO_DFT_DMX_DATA ) ) ) == NULL )
560 : {
561 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo DFT mono output\n" ) );
562 : }
563 1275 : stereo_dft_dmx_out_reset( hCPE->hStereoDftDmx );
564 : }
565 :
566 : /* allocate TCA data structure */
567 2172 : if ( hCPE->nchan_out != 1 && hCPE->hStereoTCA == NULL )
568 : {
569 831 : if ( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
570 : {
571 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
572 : }
573 :
574 831 : stereo_tca_init_dec( hCPE->hStereoTCA );
575 : }
576 :
577 2172 : st = hCPE->hCoreCoder[0];
578 :
579 : /* allocate primary channel substructures */
580 2172 : if ( ( error = allocate_CoreCoder( st ) ) != IVAS_ERR_OK )
581 : {
582 0 : return error;
583 : }
584 :
585 : /* allocate BWEs for primary channel */
586 2172 : if ( st->hBWE_TD == NULL )
587 : {
588 9 : if ( ( st->hBWE_TD = (TD_BWE_DEC_HANDLE) malloc( sizeof( TD_BWE_DEC_DATA ) ) ) == NULL )
589 : {
590 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD BWE\n" ) );
591 : }
592 :
593 9 : td_bwe_dec_init( st->hBWE_TD, -1, st->output_Fs );
594 :
595 9 : if ( ( st->hBWE_FD = (FD_BWE_DEC_HANDLE) malloc( sizeof( FD_BWE_DEC_DATA ) ) ) == NULL )
596 : {
597 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FD BWE\n" ) );
598 : }
599 :
600 9 : fd_bwe_dec_init( st->hBWE_FD );
601 : }
602 :
603 : /* Allocated FD_CNG instance for primary channel*/
604 2172 : if ( st->hFdCngDec == NULL )
605 : {
606 : /* Create FD_CNG instance */
607 0 : if ( ( error = createFdCngDec( &st->hFdCngDec ) ) != IVAS_ERR_OK )
608 : {
609 0 : return error;
610 : }
611 0 : initFdCngDec( st );
612 0 : configureFdCngDec( st->hFdCngDec, st->bwidth, st->total_brate, st->L_frame, st->last_L_frame, st->element_mode );
613 : }
614 :
615 : /* allocate stereo CNG structure */
616 2172 : if ( hCPE->hStereoCng == NULL )
617 : {
618 2172 : if ( ( hCPE->hStereoCng = (STEREO_CNG_DEC_HANDLE) malloc( sizeof( STEREO_CNG_DEC ) ) ) == NULL )
619 : {
620 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo CNG\n" ) );
621 : }
622 2172 : stereo_cng_init_dec( hCPE->hStereoCng, &st->hFdCngDec->hFdCngCom->frameSize );
623 : }
624 : }
625 :
626 : /*--------------------------------------------------------------*
627 : * switching CPE mode to MDCT stereo
628 : *---------------------------------------------------------------*/
629 :
630 4713 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
631 : {
632 : float tmpF_buff[L_FRAME16k / 2];
633 : TCX_LTP_DEC_DATA tcxLtpTmp;
634 :
635 2223 : if ( hCPE->last_element_mode == IVAS_CPE_TD )
636 : {
637 9 : mvr2r( hCPE->hStereoTD->TCX_old_syn_Overl, tmpF_buff, L_FRAME16k / 2 );
638 : }
639 :
640 2223 : if ( hCPE->last_element_mode == IVAS_CPE_DFT )
641 : {
642 2214 : cpy_tcx_ltp_data( hCPE->hStereoDft->hTcxLtpDec, &tcxLtpTmp, output_Fs );
643 : }
644 :
645 : /* deallocate data structure of the previous CPE mode */
646 2223 : if ( hCPE->hStereoDft != NULL )
647 : {
648 2214 : stereo_dft_dec_destroy( &( hCPE->hStereoDft ) );
649 2214 : hCPE->hStereoDft = NULL;
650 : }
651 :
652 2223 : if ( hCPE->hStereoTD != NULL )
653 : {
654 9 : free( hCPE->hStereoTD );
655 9 : hCPE->hStereoTD = NULL;
656 : }
657 :
658 2223 : if ( hCPE->hStereoDftDmx != NULL )
659 : {
660 1338 : free( hCPE->hStereoDftDmx );
661 1338 : hCPE->hStereoDftDmx = NULL;
662 : }
663 :
664 2223 : if ( hCPE->hStereoICBWE != NULL )
665 : {
666 2175 : free( hCPE->hStereoICBWE );
667 2175 : hCPE->hStereoICBWE = NULL;
668 : }
669 :
670 : /* de-allocate stereo CNG structure */
671 2223 : if ( hCPE->hStereoCng != NULL )
672 : {
673 2223 : free( hCPE->hStereoCng );
674 2223 : hCPE->hStereoCng = NULL;
675 : }
676 :
677 6669 : for ( i = 0; i < CPE_CHANNELS; i++ )
678 : {
679 4446 : st = hCPE->hCoreCoder[i];
680 4446 : st->element_mode = hCPE->element_mode;
681 :
682 : /* deallocate core-decoder substructures */
683 4446 : deallocate_CoreCoder( st );
684 :
685 4446 : st->first_CNG = 0;
686 : }
687 :
688 : /* allocate CLDFB structures for second channel */
689 2223 : st = hCPE->hCoreCoder[1];
690 :
691 2223 : if ( st->cldfbAna == NULL )
692 : {
693 2214 : if ( ( error = openCldfb( &st->cldfbAna, CLDFB_ANALYSIS, 48000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
694 : {
695 0 : return error;
696 : }
697 : }
698 :
699 2223 : if ( st->cldfbBPF == NULL )
700 : {
701 : /* open analysis BPF for max. internal sampling rate 16kHz */
702 2214 : if ( ( error = openCldfb( &st->cldfbBPF, CLDFB_ANALYSIS, 16000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
703 : {
704 0 : return error;
705 : }
706 : }
707 :
708 : /* allocate Fd-Cng structure for second channel */
709 2223 : if ( ( error = createFdCngDec( &st->hFdCngDec ) ) != IVAS_ERR_OK )
710 : {
711 0 : return error;
712 : }
713 :
714 : /* Init FD-CNG */
715 2223 : initFdCngDec( st );
716 :
717 2223 : if ( hCPE->last_element_mode == IVAS_CPE_DFT )
718 : {
719 2214 : st->last_core = ACELP_CORE; /* needed to set-up TCX core in SetTCXModeInfo() */
720 : }
721 :
722 : /*Allocate CoreCoder TCX modules for second channel */
723 2223 : if ( ( error = allocate_CoreCoder_TCX( hCPE->hCoreCoder[1] ) ) != IVAS_ERR_OK )
724 : {
725 0 : return error;
726 : }
727 :
728 2223 : if ( hCPE->last_element_mode == IVAS_CPE_DFT )
729 : {
730 2214 : if ( hCPE->nchan_out == 1 )
731 : {
732 51 : cpy_tcx_ltp_data( hCPE->hCoreCoder[0]->hTcxLtpDec, hCPE->hCoreCoder[1]->hTcxLtpDec, output_Fs );
733 : }
734 : else
735 : {
736 2163 : cpy_tcx_ltp_data( &tcxLtpTmp, hCPE->hCoreCoder[1]->hTcxLtpDec, output_Fs );
737 : }
738 : }
739 :
740 2223 : if ( hCPE->last_element_mode == IVAS_CPE_TD )
741 : {
742 9 : mvr2r( tmpF_buff, st->hTcxDec->old_syn_Overl, L_FRAME16k / 2 );
743 : }
744 :
745 2223 : set_f( st->hTcxDec->FBTCXdelayBuf, 0.0f, 111 );
746 2223 : st->hTcxDec->old_synthFB = st->hTcxDec->synth_history + NS2SA( st->output_Fs, PH_ECU_MEM_NS );
747 2223 : st->hTcxDec->prev_good_synth = st->hTcxDec->old_synthFB + NS2SA( st->output_Fs, PH_ECU_LOOKAHEAD_NS );
748 :
749 : /* allocate and initialize MDCT stereo structure */
750 2223 : if ( ( hCPE->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_DEC_DATA ) ) ) == NULL )
751 : {
752 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
753 : }
754 2223 : if ( ivas_format == STEREO_FORMAT && hCPE->element_brate <= MAX_MDCT_ITD_BRATE )
755 : {
756 72 : hCPE->hStereoMdct->use_itd = 1;
757 : }
758 : else
759 : {
760 2151 : hCPE->hStereoMdct->use_itd = 0;
761 : }
762 2223 : hCPE->hStereoMdct->reverse_dmx = 0;
763 2223 : hCPE->hStereoMdct->smooth_ratio = 1.f;
764 : }
765 : }
766 :
767 : /*--------------------------------------------------------------*
768 : * normal TD / LRTD switching
769 : *---------------------------------------------------------------*/
770 :
771 1261158 : if ( hCPE->element_mode == IVAS_CPE_TD )
772 : {
773 11340 : if ( hCPE->hCoreCoder[0]->bfi == 0 )
774 : {
775 10956 : st = hCPE->hCoreCoder[1];
776 10956 : hCPE->hStereoTD->tdm_LRTD_flag = get_indice_st( hCPE->hCoreCoder[0], hCPE->element_brate + hCPE->brate_surplus, (int16_t) ( ( hCPE->element_brate / FRAMES_PER_SEC ) - nb_bits_metadata + ( hCPE->brate_surplus / FRAMES_PER_SEC ) - TDM_SECONDARY_SIGNALLING - TDM_RATIO_BITS - TDM_LP_REUSE_BITS - TDM_LR_CONTENT_BITS ), TDM_LR_CONTENT_BITS );
777 :
778 10956 : if ( hCPE->hStereoTD->tdm_LRTD_flag )
779 : {
780 : /* deallocate ICBWE structure */
781 10602 : if ( hCPE->hStereoICBWE != NULL )
782 : {
783 117 : free( hCPE->hStereoICBWE );
784 117 : hCPE->hStereoICBWE = NULL;
785 : }
786 :
787 : /* allocate BWEs for secondary channel */
788 10602 : if ( st->hBWE_TD == NULL )
789 : {
790 126 : if ( ( st->hBWE_TD = (TD_BWE_DEC_HANDLE) malloc( sizeof( TD_BWE_DEC_DATA ) ) ) == NULL )
791 : {
792 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD BWE\n" ) );
793 : }
794 :
795 126 : td_bwe_dec_init( st->hBWE_TD, -1, st->output_Fs );
796 :
797 126 : if ( ( st->hBWE_FD = (FD_BWE_DEC_HANDLE) malloc( sizeof( FD_BWE_DEC_DATA ) ) ) == NULL )
798 : {
799 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FD BWE\n" ) );
800 : }
801 :
802 126 : fd_bwe_dec_init( st->hBWE_FD );
803 : }
804 : }
805 : else /* tdm_LRTD_flag == 0 */
806 : {
807 : /* deallocate BWEs for secondary channel */
808 354 : if ( st->hBWE_TD != NULL )
809 : {
810 99 : if ( st->hBWE_TD != NULL )
811 : {
812 99 : free( st->hBWE_TD );
813 99 : st->hBWE_TD = NULL;
814 : }
815 :
816 99 : if ( st->hBWE_FD != NULL )
817 : {
818 99 : free( st->hBWE_FD );
819 99 : st->hBWE_FD = NULL;
820 : }
821 : }
822 :
823 : /* allocate ICBWE structure */
824 354 : if ( hCPE->hStereoICBWE == NULL )
825 : {
826 99 : if ( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
827 : {
828 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
829 : }
830 :
831 99 : stereo_icBWE_init_dec( hCPE->hStereoICBWE );
832 : }
833 : }
834 : }
835 : }
836 :
837 : /*--------------------------------------------------------------*
838 : * MDCT stereo bitrate switching
839 : *---------------------------------------------------------------*/
840 :
841 1261158 : if ( ivas_format == STEREO_FORMAT && hCPE->element_mode == IVAS_CPE_MDCT )
842 : {
843 62991 : if ( hCPE->element_brate <= MAX_MDCT_ITD_BRATE && ivas_total_brate > IVAS_SID_5k2 )
844 : {
845 35301 : if ( hCPE->hStereoMdct->use_itd == 0 )
846 : {
847 87 : if ( hCPE->hStereoTCA == NULL )
848 : {
849 : /* allocate TCA data structure */
850 36 : if ( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
851 : {
852 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
853 : }
854 : }
855 87 : stereo_tca_init_dec( hCPE->hStereoTCA );
856 : }
857 :
858 35301 : hCPE->hStereoMdct->use_itd = 1;
859 : }
860 : else
861 : {
862 : /* de-allocate TCA data structure */
863 27690 : if ( hCPE->hStereoMdct->use_itd == 1 && ivas_total_brate > IVAS_SID_5k2 && hCPE->hStereoTCA != NULL )
864 : {
865 72 : free( hCPE->hStereoTCA );
866 72 : hCPE->hStereoTCA = NULL;
867 72 : hCPE->hStereoMdct->use_itd = 0;
868 : }
869 27618 : else if ( hCPE->hStereoMdct->use_itd == 1 && ivas_total_brate <= IVAS_SID_5k2 )
870 : {
871 8733 : hCPE->hStereoMdct->itd = 0.0f;
872 : }
873 : else
874 : {
875 18885 : hCPE->hStereoMdct->use_itd = 0;
876 : }
877 : }
878 : }
879 :
880 : /*--------------------------------------------------------------*
881 : * Bitrate switching in MASA format
882 : *---------------------------------------------------------------*/
883 :
884 1261158 : if ( ( ivas_format == MASA_FORMAT || ivas_format == MASA_ISM_FORMAT ) && nchan_transport == 2 )
885 : {
886 76716 : if ( hCPE->nchan_out == 1 )
887 : {
888 17763 : if ( hCPE->hStereoDftDmx == NULL )
889 : {
890 414 : if ( ( hCPE->hStereoDftDmx = (STEREO_DFT_DMX_DATA_HANDLE) malloc( sizeof( STEREO_DFT_DMX_DATA ) ) ) == NULL )
891 : {
892 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo DFT mono output\n" ) );
893 : }
894 414 : stereo_dft_dmx_out_reset( hCPE->hStereoDftDmx );
895 : }
896 :
897 17763 : if ( hCPE->prev_synth_chs[1] != NULL )
898 : {
899 1638 : free( hCPE->prev_synth_chs[1] );
900 1638 : hCPE->prev_synth_chs[1] = NULL;
901 : }
902 :
903 17763 : if ( hCPE->hStereoTCA != NULL )
904 : {
905 414 : free( hCPE->hStereoTCA );
906 414 : hCPE->hStereoTCA = NULL;
907 : }
908 : }
909 : else /* nchan_out == 2 */
910 : {
911 58953 : if ( hCPE->hStereoDftDmx != NULL )
912 : {
913 390 : free( hCPE->hStereoDftDmx );
914 390 : hCPE->hStereoDftDmx = NULL;
915 : }
916 :
917 58953 : if ( hCPE->prev_synth_chs[1] == NULL )
918 : {
919 1674 : st = hCPE->hCoreCoder[1];
920 1674 : if ( ( hCPE->prev_synth_chs[1] = (float *) malloc( sizeof( float ) * NS2SA( st->output_Fs, FRAME_SIZE_NS ) ) ) == NULL )
921 : {
922 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
923 : }
924 1674 : set_zero( hCPE->prev_synth_chs[1], NS2SA( st->output_Fs, FRAME_SIZE_NS ) );
925 : }
926 :
927 58953 : if ( hCPE->hStereoICBWE == NULL && hCPE->element_mode == IVAS_CPE_DFT )
928 : {
929 15 : if ( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
930 : {
931 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
932 : }
933 :
934 15 : stereo_icBWE_init_dec( hCPE->hStereoICBWE );
935 : }
936 :
937 58953 : if ( hCPE->hStereoTCA == NULL && ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD ) )
938 : {
939 390 : if ( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
940 : {
941 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
942 : }
943 :
944 390 : stereo_tca_init_dec( hCPE->hStereoTCA );
945 : }
946 :
947 58953 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
948 : {
949 40683 : if ( hCPE->hStereoTCA != NULL )
950 : {
951 : /* note: in MASA, hCPE->hStereoMdct->itd = 0 */
952 777 : free( hCPE->hStereoTCA );
953 777 : hCPE->hStereoTCA = NULL;
954 : }
955 :
956 40683 : if ( hCPE->hStereoICBWE != NULL )
957 : {
958 0 : free( hCPE->hStereoICBWE );
959 0 : hCPE->hStereoICBWE = NULL;
960 : }
961 : }
962 : }
963 : }
964 :
965 : /*--------------------------------------------------------------*
966 : * Bitrate switching in MASA format
967 : *---------------------------------------------------------------*/
968 :
969 1261158 : if ( ivas_format == MC_FORMAT && hCPE->element_mode == IVAS_CPE_MDCT )
970 : {
971 298506 : if ( mc_mode == MC_MODE_MCT || mc_mode == MC_MODE_PARAMUPMIX )
972 : {
973 : /* deallocate the FdCNG handle */
974 792864 : for ( i = 0; i < CPE_CHANNELS; ++i )
975 : {
976 528576 : deleteFdCngDec( &hCPE->hCoreCoder[i]->hFdCngDec );
977 : }
978 : }
979 : else
980 : {
981 : /* allocate the FdCNG handle (for noise estimation for TCX PLC fadeout)*/
982 102654 : for ( i = 0; i < CPE_CHANNELS; ++i )
983 : {
984 68436 : if ( hCPE->hCoreCoder[i]->cldfbSyn == NULL ) /* could be NULL when we had the MCT LFE channel */
985 : {
986 0 : if ( ( error = openCldfb( &hCPE->hCoreCoder[i]->cldfbSyn, CLDFB_SYNTHESIS, hCPE->hCoreCoder[i]->output_Fs, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
987 : {
988 0 : return error;
989 : }
990 : }
991 :
992 68436 : if ( hCPE->hCoreCoder[i]->hFdCngDec == NULL )
993 : {
994 1080 : if ( ( error = createFdCngDec( &hCPE->hCoreCoder[i]->hFdCngDec ) ) != IVAS_ERR_OK )
995 : {
996 0 : return error;
997 : }
998 1080 : initFdCngDec( hCPE->hCoreCoder[i] );
999 : }
1000 : }
1001 : }
1002 : }
1003 :
1004 1261158 : return error;
1005 : }
1006 :
1007 :
1008 : /*-------------------------------------------------------------------*
1009 : * Function synchro_synthesis()
1010 : *
1011 : * Synchronize upmixed DFT/TD/MDCT stereo synthesis to match the overall delay of 32ms
1012 : * Handling of TD stereo <-> DFT stereo transitions
1013 : *-------------------------------------------------------------------*/
1014 :
1015 1259877 : void synchro_synthesis(
1016 : const int32_t ivas_total_brate, /* i : IVAS total bitrate */
1017 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
1018 : float *output[CPE_CHANNELS], /* i/o: output synthesis signal */
1019 : const int16_t output_frame, /* i : Number of samples */
1020 : const int16_t sba_dirac_stereo_flag /* i : signal stereo output for SBA DirAC */
1021 : )
1022 : {
1023 : int16_t n, delay_comp_TD, delay_comp_DFT;
1024 : int32_t output_Fs;
1025 : Decoder_State **sts;
1026 : int16_t i, delay_cldfb, dft32ms_ovl;
1027 : float *p_output_mem[CPE_CHANNELS];
1028 : float tmp_out[CPE_CHANNELS][NS2SA( 48000, DELAY_CLDFB_NS )];
1029 : float tmp_out_TD[CPE_CHANNELS][STEREO_DFT32MS_OVL_MAX];
1030 : float tmp_out_TD2[CPE_CHANNELS][STEREO_DFT32MS_OVL_MAX];
1031 : int16_t use_cldfb_for_last_dft;
1032 : int16_t dft_mono_brate_switch;
1033 : int16_t delay_diff;
1034 : float tmpF;
1035 : int16_t nChannels;
1036 :
1037 1259877 : sts = hCPE->hCoreCoder;
1038 1259877 : output_Fs = sts[0]->output_Fs;
1039 :
1040 1259877 : use_cldfb_for_last_dft = 0;
1041 1259877 : if ( ( hCPE->element_mode != IVAS_CPE_DFT && hCPE->nchan_out == 1 && 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 */
1042 1254855 : || ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 && hCPE->hStereoDft->hConfig->res_cod_mode == STEREO_DFT_RES_COD_OFF ) )
1043 : {
1044 56361 : use_cldfb_for_last_dft = 1;
1045 : }
1046 :
1047 1259877 : dft_mono_brate_switch = 0;
1048 1259877 : if ( hCPE->element_mode == IVAS_CPE_DFT && ( hCPE->last_element_mode == IVAS_CPE_DFT || hCPE->last_element_mode == IVAS_CPE_MDCT ) && hCPE->nchan_out == 1 && hCPE->element_brate != hCPE->last_element_brate )
1049 : {
1050 6396 : if ( hCPE->last_element_brate >= IVAS_32k && hCPE->hStereoDft->hConfig->res_cod_mode == STEREO_DFT_RES_COD_OFF )
1051 : {
1052 1689 : dft_mono_brate_switch = -1; /* switch from residual coding mode or MDCT Stereo */
1053 : }
1054 4707 : else if ( hCPE->last_element_brate <= IVAS_24k4 && hCPE->hStereoDft->hConfig->res_cod_mode > STEREO_DFT_RES_COD_OFF )
1055 : {
1056 174 : dft_mono_brate_switch = 1; /* switch to residual coding mode*/
1057 : }
1058 : }
1059 :
1060 1259877 : if ( use_cldfb_for_last_dft )
1061 : {
1062 56361 : if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->last_element_mode == IVAS_CPE_TD && ( ivas_total_brate > IVAS_SID_5k2 || hCPE->nchan_out == 2 ) )
1063 : {
1064 21 : stereo_tca_scale_R_channel( hCPE, output[0], output_frame );
1065 : }
1066 : }
1067 :
1068 : /* set delays */
1069 1259877 : delay_comp_DFT = NS2SA( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS );
1070 1259877 : delay_comp_TD = NS2SA( output_Fs, IVAS_DEC_DELAY_NS - DELAY_CLDFB_NS );
1071 1259877 : delay_diff = delay_comp_TD - delay_comp_DFT;
1072 :
1073 1259877 : dft32ms_ovl = (int16_t) ( ( STEREO_DFT32MS_OVL_MAX * output_Fs ) / 48000 );
1074 1259877 : delay_cldfb = NS2SA( output_Fs, DELAY_CLDFB_NS );
1075 :
1076 : /* initialize pointers */
1077 1259877 : if ( hCPE->element_mode >= IVAS_CPE_DFT && hCPE->output_mem[0] != NULL )
1078 : {
1079 977913 : for ( n = 0; n < CPE_CHANNELS; n++ )
1080 : {
1081 651942 : p_output_mem[n] = hCPE->output_mem[n];
1082 : }
1083 : }
1084 :
1085 : /*----------------------------------------------------------------*
1086 : * DFT stereo synchro
1087 : *----------------------------------------------------------------*/
1088 :
1089 1259877 : if ( hCPE->element_mode == IVAS_CPE_DFT || sba_dirac_stereo_flag )
1090 : {
1091 : /* handling of bitrate switching from residual (using DFT) to non-residual mode (using CLDFB) for mono output - as in DFT->TD switching */
1092 211422 : if ( dft_mono_brate_switch == -1 )
1093 : {
1094 133449 : for ( i = delay_comp_DFT; i < delay_comp_TD; i++ )
1095 : {
1096 131760 : sts[0]->prev_synth_buffer[i] = p_output_mem[0][i - delay_comp_DFT];
1097 : }
1098 :
1099 89529 : for ( i = delay_comp_TD; i < delay_comp_TD + delay_cldfb; i++ )
1100 : {
1101 87840 : tmp_out[0][i - delay_comp_TD] = p_output_mem[0][i - delay_comp_DFT];
1102 : }
1103 : }
1104 :
1105 211422 : if ( hCPE->nchan_out == 1 && hCPE->last_element_mode == IVAS_CPE_MDCT )
1106 : {
1107 1275 : v_add( sts[0]->prev_synth_buffer, sts[1]->prev_synth_buffer, sts[0]->prev_synth_buffer, delay_comp_DFT );
1108 1275 : v_multc( sts[0]->prev_synth_buffer, INV_SQRT_2, sts[0]->prev_synth_buffer, delay_comp_DFT );
1109 : }
1110 :
1111 211422 : if ( use_cldfb_for_last_dft )
1112 : {
1113 : /* 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. */
1114 51339 : mvr2r( sts[0]->prev_synth_buffer + delay_comp_DFT, hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf, delay_diff );
1115 51339 : delay_signal( output[0], output_frame, hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf, delay_diff );
1116 : }
1117 :
1118 211422 : if ( hCPE->element_mode != IVAS_CPE_MDCT )
1119 : {
1120 190482 : ivas_post_proc( NULL, hCPE, 0, output[0], output, output_frame, sba_dirac_stereo_flag );
1121 : }
1122 :
1123 : /* zero padding in order to synchronize the upmixed DFT stereo synthesis with the TD/MDCT stereo synthesis */
1124 566901 : for ( n = 0; n < hCPE->nchan_out; n++ )
1125 : {
1126 355479 : if ( sba_dirac_stereo_flag )
1127 : {
1128 64728 : delay_signal( output[n], output_frame, hCPE->prev_synth[n], delay_comp_DFT );
1129 : }
1130 : else
1131 : {
1132 290751 : delay_signal( output[n], output_frame, sts[n]->prev_synth_buffer, delay_comp_DFT );
1133 : }
1134 : }
1135 :
1136 211422 : if ( use_cldfb_for_last_dft )
1137 : {
1138 51339 : mvr2r( hCPE->hCoreCoder[0]->hTcxDec->FBTCXdelayBuf, sts[0]->prev_synth_buffer + delay_comp_DFT, delay_diff );
1139 : }
1140 :
1141 : /* handling of TD->DFT switching */
1142 566901 : for ( n = 0; n < hCPE->nchan_out; n++ )
1143 : {
1144 355479 : if ( ( !use_cldfb_for_last_dft && hCPE->last_element_mode != IVAS_CPE_DFT && !sba_dirac_stereo_flag && dft_mono_brate_switch != -1 ) || dft_mono_brate_switch == 1 )
1145 2109 : {
1146 : float *pPrev_synth;
1147 2109 : float inv_fade_len = 1.f / delay_diff;
1148 :
1149 : /* cross-fading between TD synchro memory and the DFT output */
1150 2109 : if ( sba_dirac_stereo_flag )
1151 : {
1152 0 : pPrev_synth = hCPE->prev_synth[n];
1153 : }
1154 : else
1155 : {
1156 2109 : pPrev_synth = sts[n]->prev_synth_buffer;
1157 : }
1158 :
1159 2109 : if ( hCPE->last_element_mode != IVAS_CPE_MDCT )
1160 : {
1161 21138 : for ( i = delay_comp_DFT; i < delay_comp_TD; i++ )
1162 : {
1163 20790 : output[n][i] = ( pPrev_synth[i] * ( delay_comp_TD - i ) + output[n][i] * ( i - delay_comp_DFT ) ) * inv_fade_len;
1164 : }
1165 : }
1166 : }
1167 353370 : else if ( dft_mono_brate_switch == -1 )
1168 : {
1169 1689 : float inv_fade_len_1 = 1.0f / (float) delay_diff;
1170 1689 : float inv_fade_len_2 = 1.0f / (float) delay_cldfb;
1171 :
1172 133449 : for ( i = 0; i < delay_diff; i++ )
1173 : {
1174 131760 : output[0][i + delay_comp_DFT] = ( output[0][i + delay_comp_DFT] * ( delay_diff - i ) + p_output_mem[0][i] * i ) * inv_fade_len_1;
1175 : }
1176 :
1177 89529 : for ( i = 0; i < delay_cldfb; i++ )
1178 : {
1179 87840 : output[0][i + delay_comp_TD] = ( tmp_out[0][i] * ( delay_cldfb - i ) + output[0][i + delay_comp_TD] * i ) * inv_fade_len_2;
1180 : }
1181 : }
1182 : }
1183 : }
1184 :
1185 : /*----------------------------------------------------------------*
1186 : * TD/MDCT stereo synchro
1187 : *----------------------------------------------------------------*/
1188 :
1189 1259877 : if ( sba_dirac_stereo_flag )
1190 : {
1191 32364 : return;
1192 : }
1193 :
1194 1227513 : if ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_MDCT )
1195 : {
1196 : /* handling of DFT->TD switching */
1197 1048455 : if ( hCPE->last_element_mode == IVAS_CPE_DFT && !use_cldfb_for_last_dft && hCPE->output_mem[0] != NULL )
1198 : {
1199 : /* use redressed DFT stereo OLA part to reconstruct the TD stereo synchro memory */
1200 6822 : for ( n = 0; n < hCPE->nchan_out; n++ )
1201 : {
1202 363816 : for ( i = delay_comp_DFT; i < delay_comp_TD; i++ )
1203 : {
1204 359280 : sts[n]->prev_synth_buffer[i] = p_output_mem[n][i - delay_comp_DFT];
1205 : }
1206 :
1207 244056 : for ( i = delay_comp_TD; i < delay_comp_TD + delay_cldfb; i++ )
1208 : {
1209 239520 : tmp_out[n][i - delay_comp_TD] = p_output_mem[n][i - delay_comp_DFT];
1210 : }
1211 : }
1212 : }
1213 :
1214 : /* if previous frame had only one channel copy buffers to other channel */
1215 1048455 : if ( hCPE->nchan_out == 1 && hCPE->element_mode == IVAS_CPE_MDCT && hCPE->last_element_mode == IVAS_CPE_DFT )
1216 : {
1217 51 : mvr2r( sts[0]->prev_synth_buffer, sts[1]->prev_synth_buffer, delay_comp_TD );
1218 51 : mvr2r( tmp_out[0], tmp_out[1], delay_cldfb );
1219 51 : mvr2r( p_output_mem[0], p_output_mem[1], delay_diff );
1220 : }
1221 :
1222 : /*----------------------------------------------------------------*
1223 : * update DFT synthesis overlap memory @output_Fs; needed for TD->DFT stereo switching
1224 : *----------------------------------------------------------------*/
1225 :
1226 : /* resample LB synthesis to output_Fs */
1227 1048455 : if ( hCPE->element_mode != IVAS_CPE_MDCT && !use_cldfb_for_last_dft )
1228 : {
1229 26667 : for ( n = 0; n < hCPE->nchan_out; n++ )
1230 : {
1231 16974 : if ( sts[n]->core == ACELP_CORE )
1232 : {
1233 16509 : lerp( hCPE->input_mem_LB[n], tmp_out_TD[n], dft32ms_ovl, NS2SA( sts[n]->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
1234 : }
1235 : else /* TCX/HQ core */
1236 : {
1237 465 : lerp( hCPE->input_mem_LB[n], tmp_out_TD[n], dft32ms_ovl, NS2SA( sts[n]->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
1238 :
1239 : /* use TCX synchro memory (perfect signal is available) */
1240 20025 : for ( i = delay_diff; i < dft32ms_ovl; i++ )
1241 : {
1242 19560 : tmp_out_TD[n][i] = sts[n]->delay_buf_out[i - delay_diff];
1243 : }
1244 : }
1245 : }
1246 :
1247 9693 : if ( hCPE->nchan_out == CPE_CHANNELS )
1248 : {
1249 : /* upmix the resampled LB / the TCX synchro memory */
1250 7281 : tdm_upmix_plain( tmp_out_TD2[0], tmp_out_TD2[1], tmp_out_TD[0], tmp_out_TD[1], tdm_ratio_tabl[hCPE->hStereoTD->tdm_last_ratio_idx], tdm_den_ratio_tabl[hCPE->hStereoTD->tdm_last_ratio_idx], 0, dft32ms_ovl, 1 );
1251 : }
1252 : else
1253 : {
1254 2412 : mvr2r( tmp_out_TD[0], tmp_out_TD2[0], dft32ms_ovl );
1255 : }
1256 :
1257 26667 : for ( n = 0; n < hCPE->nchan_out; n++ )
1258 : {
1259 16974 : if ( sts[0]->core == ACELP_CORE ) /* ACELP core in primary channel */
1260 : {
1261 16182 : tmpF = 1.0f / (float) delay_diff;
1262 :
1263 : /* cross-fading between regular output synthesis and lerp() resampled synthesis in 3.125 - 1.25 ms OLA part */
1264 1033362 : for ( i = 0; i < delay_diff; i++ )
1265 : {
1266 1017180 : p_output_mem[n][i] = ( output[n][output_frame - dft32ms_ovl + delay_cldfb + i] * ( delay_diff - i ) + tmp_out_TD2[n][i] * i ) * tmpF;
1267 : }
1268 : }
1269 : else /* TCX core */
1270 : {
1271 : /* reconstruct the 3.125 - 1.25 ms OLA part */
1272 51192 : for ( i = 0; i < delay_diff; i++ )
1273 : {
1274 50400 : p_output_mem[n][i] = output[n][output_frame - dft32ms_ovl + delay_cldfb + i];
1275 : }
1276 : }
1277 :
1278 : /* reconstruct the last 1.25 ms part of OLA window */
1279 728694 : for ( i = delay_diff; i < dft32ms_ovl; i++ )
1280 : {
1281 711720 : p_output_mem[n][i] = tmp_out_TD2[n][i];
1282 : }
1283 : }
1284 : }
1285 :
1286 : /*----------------------------------------------------------------*
1287 : * zero padding TD/MDCT synthesis in order to synchronize
1288 : * the upmixed TD/MDCT stereo synthesis with the DFT stereo synthesis
1289 : *----------------------------------------------------------------*/
1290 :
1291 1048455 : if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->stereo_switching_counter == 0 && hCPE->nchan_out == 1 && hCPE->last_element_brate <= IVAS_24k4 )
1292 : {
1293 : float step;
1294 45 : tmpF = 1.0f;
1295 45 : step = 0.5f / delay_comp_TD;
1296 :
1297 : /* for the first switching frame from DFT to TD, downmix memory too */
1298 2541 : for ( n = 0; n < delay_comp_TD; n++ )
1299 : {
1300 2496 : sts[0]->prev_synth_buffer[n] = ( sts[0]->prev_synth_buffer[n] ) * tmpF;
1301 2496 : tmpF -= step;
1302 : }
1303 : }
1304 :
1305 1048455 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->nchan_out == 1 && !is_DTXrate( hCPE->element_brate ) && is_DTXrate( hCPE->last_element_brate ) )
1306 : {
1307 105 : mvr2r( sts[0]->prev_synth_buffer, sts[1]->prev_synth_buffer, delay_comp_TD );
1308 : }
1309 :
1310 1048455 : nChannels = ( hCPE->element_mode == IVAS_CPE_MDCT ) ? 2 : hCPE->nchan_out;
1311 3141306 : for ( n = 0; n < nChannels; n++ )
1312 : {
1313 2092851 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
1314 : {
1315 2074230 : mvr2r( sts[n]->prev_synth_buffer + delay_comp_DFT, hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf, delay_diff );
1316 2074230 : delay_signal( output[n], output_frame, hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf, delay_diff );
1317 2074230 : ivas_post_proc( NULL, hCPE, n, output[n], output, output_frame, 0 );
1318 2074230 : delay_signal( output[n], output_frame, sts[n]->prev_synth_buffer, delay_comp_DFT );
1319 2074230 : mvr2r( hCPE->hCoreCoder[n]->hTcxDec->FBTCXdelayBuf, sts[n]->prev_synth_buffer + delay_comp_DFT, delay_diff );
1320 : }
1321 : else
1322 : {
1323 18621 : delay_signal( output[n], output_frame, sts[n]->prev_synth_buffer, delay_comp_TD );
1324 : }
1325 : }
1326 :
1327 : /* handling of DFT->TD switching */
1328 1048455 : if ( hCPE->last_element_mode == IVAS_CPE_DFT && !use_cldfb_for_last_dft )
1329 : {
1330 2286 : if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->hStereoCng->prev_sid_nodata )
1331 : {
1332 63 : for ( n = 0; n < hCPE->nchan_out; n++ )
1333 : {
1334 42 : tmpF = 1.0f / (float) delay_cldfb;
1335 :
1336 1962 : for ( i = 0; i < delay_cldfb; i++ )
1337 : {
1338 1920 : tmp_out[n][i] = tmp_out[n][i] * ( delay_cldfb - i ) * tmpF;
1339 : }
1340 : }
1341 : }
1342 :
1343 : /* cross-fading between DFT OLA memory and TD output */
1344 6837 : for ( n = 0; n < nChannels; n++ )
1345 : {
1346 4551 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
1347 : {
1348 4356 : tmpF = 1.0f / (float) delay_diff;
1349 :
1350 352656 : for ( i = 0; i < delay_diff; i++ )
1351 : {
1352 348300 : output[n][i + delay_comp_DFT] = ( output[n][i + delay_comp_DFT] * ( delay_diff - i ) + p_output_mem[n][i] * i ) * tmpF;
1353 : }
1354 : }
1355 :
1356 4551 : tmpF = 1.0f / (float) delay_cldfb;
1357 :
1358 244971 : for ( i = 0; i < delay_cldfb; i++ )
1359 : {
1360 240420 : output[n][i + delay_comp_TD] = ( tmp_out[n][i] * ( delay_cldfb - i ) + output[n][i + delay_comp_TD] * i ) * tmpF;
1361 : }
1362 : }
1363 : }
1364 : }
1365 :
1366 1227513 : return;
1367 : }
1368 :
1369 :
1370 : /*-------------------------------------------------------------------*
1371 : * Function stereo_switching_dec()
1372 : *
1373 : * Handling of memories in case of CPE modes switching
1374 : *-------------------------------------------------------------------*/
1375 :
1376 1261158 : void stereo_switching_dec(
1377 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
1378 : const int32_t ivas_total_brate /* i : IVAS total bitrate */
1379 : )
1380 : {
1381 : int16_t i, n;
1382 : int16_t dft32ms_ovl;
1383 : Decoder_State **sts;
1384 : float tmpF;
1385 : int16_t delay_comp_TD;
1386 :
1387 1261158 : sts = hCPE->hCoreCoder;
1388 :
1389 1261158 : delay_comp_TD = NS2SA( sts[0]->output_Fs, IVAS_DEC_DELAY_NS - DELAY_CLDFB_NS );
1390 :
1391 : /* prevent CPE mode switching in the first received frame */
1392 1261158 : if ( sts[0]->ini_frame == 0 )
1393 : {
1394 8772 : hCPE->last_element_mode = hCPE->element_mode;
1395 8772 : hCPE->stereo_switching_counter = 10;
1396 8772 : hCPE->NbFrameMod = 7;
1397 8772 : hCPE->lt_es_em = 0.0f;
1398 : }
1399 :
1400 1261158 : if ( hCPE->element_mode == hCPE->last_element_mode )
1401 : {
1402 1256445 : hCPE->stereo_switching_counter++;
1403 1256445 : hCPE->stereo_switching_counter = min( 10, hCPE->stereo_switching_counter );
1404 : }
1405 : else
1406 : {
1407 4713 : hCPE->stereo_switching_counter = 0;
1408 : }
1409 :
1410 1261158 : if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 && hCPE->element_brate >= IVAS_32k && hCPE->last_element_brate <= IVAS_24k4 )
1411 : {
1412 174 : dft32ms_ovl = (int16_t) ( ( STEREO_DFT32MS_OVL_MAX * sts[0]->output_Fs ) / 48000 );
1413 174 : set_zero( hCPE->output_mem[0], dft32ms_ovl );
1414 : }
1415 :
1416 1261158 : if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->last_element_mode != IVAS_CPE_DFT && hCPE->hCoreCoder[0]->ini_frame > 0 )
1417 : {
1418 : /* windowing the OLA memory */
1419 2313 : dft32ms_ovl = (int16_t) ( ( STEREO_DFT32MS_OVL_MAX * sts[0]->output_Fs ) / 48000 );
1420 6939 : for ( n = 0; n < CPE_CHANNELS; n++ )
1421 : {
1422 4626 : if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
1423 : {
1424 : /* copy memories from previous MDCT Stereo frame to output_mem */
1425 4296 : mvr2r( hCPE->input_mem[n], hCPE->output_mem[n], dft32ms_ovl );
1426 : }
1427 :
1428 4626 : if ( ivas_total_brate > IVAS_SID_5k2 || n == 0 || hCPE->last_element_mode != IVAS_CPE_TD || hCPE->nchan_out == 1 )
1429 : {
1430 607584 : for ( i = 0; i < dft32ms_ovl; i++ )
1431 : {
1432 603000 : hCPE->output_mem[n][i] *= hCPE->hStereoDft->win32ms[STEREO_DFT32MS_STEP * ( dft32ms_ovl - 1 - i )];
1433 : }
1434 : }
1435 : else
1436 : {
1437 42 : tmpF = 1.0f / hCPE->hStereoTCA->prevTargetGain;
1438 4842 : for ( i = 0; i < dft32ms_ovl; i++ )
1439 : {
1440 4800 : hCPE->output_mem[n][i] *= tmpF * hCPE->hStereoDft->win32ms[STEREO_DFT32MS_STEP * ( dft32ms_ovl - 1 - i )];
1441 : }
1442 3114 : for ( i = 0; i < delay_comp_TD; i++ )
1443 : {
1444 3072 : hCPE->hCoreCoder[1]->prev_synth_buffer[i] *= tmpF;
1445 : }
1446 11508 : for ( i = 0; i < L_DEC_MEM_LEN_ICA; i++ )
1447 : {
1448 11466 : hCPE->hStereoTCA->memChanR[i] *= tmpF;
1449 : }
1450 : }
1451 : }
1452 :
1453 2313 : if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
1454 : {
1455 : /* create passive downmix of MDCT Stereo memories for DFT input memory */
1456 2148 : v_add( hCPE->input_mem_LB[0], hCPE->input_mem_LB[1], hCPE->input_mem_LB[0], STEREO_DFT32MS_OVL_16k );
1457 2148 : v_multc( hCPE->input_mem_LB[0], 0.5f, hCPE->input_mem_LB[0], STEREO_DFT32MS_OVL_16k );
1458 :
1459 2148 : v_add( hCPE->input_mem[0], hCPE->input_mem[1], hCPE->input_mem[0], dft32ms_ovl );
1460 2148 : v_multc( hCPE->input_mem[0], 0.5f, hCPE->input_mem[0], dft32ms_ovl );
1461 :
1462 2148 : if ( hCPE->nchan_out == 1 )
1463 : {
1464 1275 : v_add( hCPE->output_mem[0], hCPE->output_mem[1], hCPE->output_mem[0], dft32ms_ovl );
1465 1275 : v_multc( hCPE->output_mem[0], INV_SQRT_2, hCPE->output_mem[0], dft32ms_ovl );
1466 : }
1467 : }
1468 :
1469 : /* Update the side_gain[] parameters */
1470 2313 : if ( hCPE->last_element_mode != IVAS_CPE_MDCT )
1471 : {
1472 165 : tmpF = 0;
1473 165 : if ( hCPE->hStereoTCA != NULL )
1474 : {
1475 87 : tmpF = usdequant( hCPE->hStereoTCA->indx_ica_gD, STEREO_TCA_GDMIN, STEREO_TCA_GDSTEP );
1476 : }
1477 :
1478 165 : set_f( hCPE->hStereoDft->side_gain + STEREO_DFT_NBDIV * STEREO_DFT_BAND_MAX, tmpF, STEREO_DFT_BAND_MAX );
1479 : }
1480 :
1481 : /* reset residual coding / ESF (secondary channel) */
1482 2313 : set_zero( hCPE->hStereoDft->res_cod_mem, STEREO_DFT_OVL_8k );
1483 2313 : set_zero( hCPE->input_mem[1], NS2SA( sts[0]->output_Fs, STEREO_DFT32MS_OVL_NS ) );
1484 : }
1485 :
1486 1261158 : if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->last_element_mode != IVAS_CPE_TD && hCPE->hCoreCoder[0]->ini_frame > 0 )
1487 : {
1488 177 : hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_MID_IS_PRIM;
1489 177 : hCPE->hStereoTD->tdm_last_SM_flag = 0;
1490 177 : hCPE->hStereoTD->tdm_prev_last_SM_flag = 0;
1491 :
1492 : /* First frame after DFT frame AND the content is uncorrelated or xtalk -> the primary channel is forced to left */
1493 177 : if ( hCPE->hStereoTD->tdm_LRTD_flag == 1 )
1494 : {
1495 177 : hCPE->hStereoTD->tdm_last_ratio_idx = LRTD_STEREO_LEFT_IS_PRIM;
1496 : }
1497 : }
1498 :
1499 : /* no secondary channel in the previous frame -> memory resets */
1500 1261158 : if ( hCPE->element_mode > IVAS_CPE_DFT && hCPE->last_element_mode == IVAS_CPE_DFT )
1501 : {
1502 2367 : if ( hCPE->last_element_brate <= IVAS_SID_5k2 && hCPE->nchan_out == 2 )
1503 : {
1504 : /* reset CLDFB memories */
1505 21 : cldfb_reset_memory( sts[0]->cldfbAna );
1506 21 : cldfb_reset_memory( sts[0]->cldfbBPF );
1507 21 : cldfb_reset_memory( sts[0]->cldfbSyn );
1508 :
1509 21 : sts[0]->mem_deemph = 0;
1510 :
1511 21 : sts[0]->tilt_code = 0.0f;
1512 21 : sts[0]->gc_threshold = 0.0f;
1513 :
1514 21 : set_f( sts[0]->mem_syn1, 0, M );
1515 21 : set_f( sts[0]->mem_syn2, 0, M );
1516 21 : set_f( sts[0]->mem_syn3, 0, M );
1517 21 : set_f( sts[0]->mem_syn_r, 0.0f, L_SYN_MEM );
1518 :
1519 21 : sts[1]->last_L_frame = sts[0]->last_L_frame;
1520 :
1521 : /* reset PCh memories */
1522 21 : set_f( sts[0]->old_exc, 0, L_EXC_MEM_DEC );
1523 21 : set_f( sts[0]->lsf_old, 0, M );
1524 21 : set_f( sts[0]->lsp_old, 0, M );
1525 : }
1526 2367 : sts[1]->last_extl = -1;
1527 :
1528 2367 : sts[1]->prev_bfi = sts[0]->prev_bfi;
1529 :
1530 2367 : set_f( sts[1]->old_pitch_buf, (float) L_SUBFR, 2 * NB_SUBFR16k );
1531 2367 : sts[1]->old_fpitchFB = 2 * (float) L_SUBFR;
1532 :
1533 : /* reset CLDFB memories */
1534 2367 : cldfb_reset_memory( sts[1]->cldfbAna );
1535 2367 : cldfb_reset_memory( sts[1]->cldfbBPF );
1536 2367 : cldfb_reset_memory( sts[1]->cldfbSyn );
1537 :
1538 2367 : sts[1]->mem_deemph = 0;
1539 :
1540 2367 : sts[1]->tilt_code = 0.0f;
1541 2367 : sts[1]->gc_threshold = 0.0f;
1542 :
1543 2367 : set_f( sts[1]->mem_syn1, 0, M );
1544 2367 : set_f( sts[1]->mem_syn2, 0, M );
1545 2367 : set_f( sts[1]->mem_syn3, 0, M );
1546 2367 : set_f( sts[1]->mem_syn_r, 0.0f, L_SYN_MEM );
1547 :
1548 2367 : sts[1]->last_L_frame = sts[0]->last_L_frame;
1549 :
1550 : /* populate PCh memories into the SCh */
1551 2367 : mvr2r( sts[0]->old_exc, sts[1]->old_exc, L_EXC_MEM_DEC );
1552 2367 : mvr2r( sts[0]->lsf_old, sts[1]->lsf_old, M );
1553 2367 : mvr2r( sts[0]->lsp_old, sts[1]->lsp_old, M );
1554 :
1555 2367 : sts[1]->last_core_brate = sts[0]->last_core_brate;
1556 :
1557 2367 : if ( hCPE->element_mode == IVAS_CPE_MDCT )
1558 : {
1559 2214 : sts[1]->last_core = sts[0]->last_core;
1560 2214 : sts[1]->last_coder_type = sts[0]->last_coder_type;
1561 :
1562 2214 : mvr2r( sts[0]->hHQ_core->old_out, sts[1]->hHQ_core->old_out, L_FRAME48k );
1563 2214 : mvr2r( sts[0]->delay_buf_out, sts[1]->delay_buf_out, HQ_DELTA_MAX * HQ_DELAY_COMP );
1564 2214 : mvr2r( sts[0]->hTcxDec->old_syn_Overl, sts[1]->hTcxDec->old_syn_Overl, 256 );
1565 : }
1566 : }
1567 1258791 : else if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->last_element_mode == IVAS_CPE_MDCT )
1568 : {
1569 24 : set_f( sts[0]->old_exc, 0.0f, L_EXC_MEM_DEC );
1570 24 : set_f( sts[1]->old_exc, 0.0f, L_EXC_MEM_DEC );
1571 : }
1572 :
1573 : /* TD/DFT -> MDCT stereo switching (there is no TCX in the TD stereo secondary channel, or DFT stereo) */
1574 1261158 : if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->last_element_mode != IVAS_CPE_MDCT )
1575 : {
1576 2223 : sts[1]->hTcxCfg->last_aldo = sts[0]->hTcxCfg->last_aldo;
1577 2223 : sts[1]->hTcxCfg->tcx_curr_overlap_mode = sts[0]->hTcxCfg->tcx_curr_overlap_mode;
1578 2223 : sts[1]->fscale = sts[0]->fscale;
1579 2223 : sts[1]->hTcxCfg->tcx_mdct_window_length = sts[0]->hTcxCfg->tcx_mdct_window_length;
1580 2223 : sts[1]->pit_res_max = sts[0]->pit_res_max;
1581 2223 : sts[1]->pit_res_max_past = sts[0]->pit_res_max_past;
1582 2223 : sts[1]->hTcxDec->L_frameTCX = sts[0]->hTcxDec->L_frameTCX;
1583 2223 : sts[1]->hTcxDec->conceal_eof_gain = sts[0]->hTcxDec->conceal_eof_gain;
1584 : }
1585 :
1586 1261158 : return;
1587 : }
1588 :
1589 :
1590 : /*-------------------------------------------------------------------*
1591 : * Function stereo_td2dft_update()
1592 : *
1593 : * update OLA buffers - needed for switching from TD stereo to DFT stereo
1594 : *-------------------------------------------------------------------*/
1595 :
1596 1619067 : void stereo_td2dft_update(
1597 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
1598 : const int16_t n, /* i : channel number */
1599 : float output[], /* i/o: synthesis @internal Fs */
1600 : float synth[], /* i/o: synthesis @output Fs */
1601 : float hb_synth[], /* i/o: hb synthesis */
1602 : const int16_t output_frame /* i : frame length */
1603 : )
1604 : {
1605 : int16_t ovl, ovl_TCX, dft32ms_ovl, hq_delay_comp;
1606 : int16_t ns, nsLB;
1607 : int16_t old_out_len, old_outLB_len;
1608 : Decoder_State **sts;
1609 :
1610 1619067 : if ( hCPE == NULL )
1611 : {
1612 1033566 : return;
1613 : }
1614 :
1615 : /* initialization */
1616 585501 : sts = hCPE->hCoreCoder;
1617 585501 : ovl = NS2SA( sts[n]->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS );
1618 585501 : dft32ms_ovl = (int16_t) ( ( STEREO_DFT32MS_OVL_MAX * sts[0]->output_Fs ) / 48000 );
1619 585501 : hq_delay_comp = NS2SA( sts[0]->output_Fs, DELAY_CLDFB_NS );
1620 :
1621 585501 : if ( hCPE->element_mode >= IVAS_CPE_DFT && hCPE->element_mode != IVAS_CPE_MDCT )
1622 : {
1623 74019 : if ( sts[n]->core == ACELP_CORE )
1624 : {
1625 66519 : if ( n == 0 )
1626 : {
1627 : /* update DFT analysis overlap memory @internal_fs: core synthesis */
1628 55179 : mvr2r( output + sts[n]->L_frame - ovl, hCPE->input_mem_LB[n], ovl );
1629 :
1630 : /* update DFT analysis overlap memory @internal_fs: BPF */
1631 55179 : if ( sts[n]->p_bpf_noise_buf )
1632 : {
1633 49287 : mvr2r( sts[n]->p_bpf_noise_buf + sts[n]->L_frame - ovl, hCPE->input_mem_BPF[n], ovl );
1634 : }
1635 :
1636 : /* update DFT analysis overlap memory @output_Fs: BWE */
1637 55179 : if ( sts[n]->extl != -1 || ( sts[n]->bws_cnt > 0 && sts[n]->core == ACELP_CORE ) )
1638 : {
1639 45417 : mvr2r( hb_synth + output_frame - dft32ms_ovl, hCPE->input_mem[n], dft32ms_ovl );
1640 : }
1641 : }
1642 : else
1643 : {
1644 : /* update DFT analysis overlap memory @internal_fs: core synthesis, secondary channel */
1645 11340 : mvr2r( output + sts[n]->L_frame - ovl, hCPE->input_mem_LB[n], ovl );
1646 : }
1647 : }
1648 : else /* TCX core */
1649 : {
1650 : /* LB-TCX synthesis */
1651 7500 : mvr2r( output + sts[n]->L_frame - ovl, hCPE->input_mem_LB[n], ovl );
1652 :
1653 : /* BPF */
1654 7500 : if ( n == 0 && sts[n]->p_bpf_noise_buf )
1655 : {
1656 7500 : mvr2r( sts[n]->p_bpf_noise_buf + sts[n]->L_frame - ovl, hCPE->input_mem_BPF[n], ovl );
1657 : }
1658 :
1659 : /* TCX synthesis (it was already delayed in TD stereo in core_switching_post_dec()) */
1660 7500 : if ( sts[n]->hTcxDec != NULL )
1661 : {
1662 7500 : ovl_TCX = NS2SA( sts[n]->hTcxDec->L_frameTCX * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS );
1663 7500 : mvr2r( synth + sts[n]->hTcxDec->L_frameTCX + hq_delay_comp - ovl_TCX, hCPE->input_mem[n], ovl_TCX - hq_delay_comp );
1664 7500 : mvr2r( sts[n]->delay_buf_out, hCPE->input_mem[n] + ovl_TCX - hq_delay_comp, hq_delay_comp );
1665 : }
1666 : }
1667 : }
1668 511482 : else if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->input_mem[0] != NULL )
1669 : {
1670 :
1671 : /* update DFT stereo OLA memories */
1672 : /*set_zero( hCPE->input_mem_LB[n], STEREO_DFT32MS_OVL_16k );*/
1673 213606 : lerp( output + sts[n]->L_frame - ovl, hCPE->input_mem_LB[n], STEREO_DFT32MS_OVL_16k, ovl );
1674 :
1675 :
1676 : /* TCX synthesis (it was already delayed in TD stereo in core_switching_post_dec()) */
1677 213606 : if ( sts[n]->hTcxDec != NULL )
1678 : {
1679 213606 : ovl_TCX = NS2SA( sts[n]->hTcxDec->L_frameTCX * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS );
1680 213606 : mvr2r( synth + sts[n]->hTcxDec->L_frameTCX + hq_delay_comp - ovl_TCX, hCPE->input_mem[n], ovl_TCX - hq_delay_comp );
1681 213606 : mvr2r( sts[n]->delay_buf_out, hCPE->input_mem[n] + ovl_TCX - hq_delay_comp, hq_delay_comp );
1682 : }
1683 :
1684 213606 : if ( n == 1 )
1685 : {
1686 106803 : nsLB = NS2SA( sts[n]->L_frame * FRAMES_PER_SEC, N_ZERO_MDCT_NS );
1687 106803 : ns = NS2SA( sts[n]->output_Fs, N_ZERO_MDCT_NS );
1688 106803 : old_outLB_len = (int16_t) ( ( 3 * STEREO_MDCT2DFT_FADE_LEN_48k * sts[0]->L_frame * FRAMES_PER_SEC ) / 48000 );
1689 106803 : old_out_len = (int16_t) ( ( STEREO_MDCT2DFT_FADE_LEN_48k * sts[0]->output_Fs ) / 48000 );
1690 :
1691 : /* update buffers used for fading when switching to DFT Stereo */
1692 106803 : v_add( sts[0]->hHQ_core->old_outLB + nsLB, sts[1]->hHQ_core->old_outLB + nsLB, hCPE->old_outLB_mdct, old_outLB_len );
1693 106803 : lerp( hCPE->old_outLB_mdct, hCPE->old_outLB_mdct, STEREO_MDCT2DFT_FADE_LEN_48k, old_outLB_len );
1694 106803 : v_multc( hCPE->old_outLB_mdct, 0.5f, hCPE->old_outLB_mdct, STEREO_MDCT2DFT_FADE_LEN_48k );
1695 :
1696 106803 : v_add( sts[0]->hHQ_core->old_out + ns, sts[1]->hHQ_core->old_out + ns, hCPE->old_out_mdct, old_out_len );
1697 106803 : v_multc( hCPE->old_out_mdct, 0.5f, hCPE->old_out_mdct, old_out_len );
1698 : }
1699 :
1700 213606 : if ( n == 0 )
1701 : {
1702 106803 : set_zero( hCPE->input_mem_BPF[n], STEREO_DFT32MS_OVL_16k );
1703 : }
1704 : }
1705 :
1706 : /* update ovl buffer for possible switching from TD stereo SCh ACELP frame to MDCT stereo TCX frame */
1707 585501 : if ( hCPE->element_mode == IVAS_CPE_TD && n == 1 && sts[n]->hTcxDec == NULL )
1708 : {
1709 11340 : mvr2r( output + sts[n]->L_frame / 2, hCPE->hStereoTD->TCX_old_syn_Overl, sts[n]->L_frame / 2 );
1710 : }
1711 :
1712 585501 : return;
1713 : }
1714 :
1715 :
1716 : /*-------------------------------------------------------------------*
1717 : * Function stereo_mdct2dft_update()
1718 : *
1719 : * update OLA buffers - needed for switching from MDCT stereo to DFT stereo
1720 : *-------------------------------------------------------------------*/
1721 :
1722 2148 : void stereo_mdct2dft_update(
1723 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
1724 : float output0[], /* i/o: synthesis @internal Fs, ch0 */
1725 : float synth0[] /* i/o: synthesis @output Fs, ch0 */
1726 : )
1727 : {
1728 : int16_t i;
1729 : int16_t fade_len, fade_len_LB;
1730 : float tmpF;
1731 : Decoder_State *st;
1732 :
1733 2148 : if ( hCPE == NULL )
1734 : {
1735 0 : return;
1736 : }
1737 :
1738 2148 : st = hCPE->hCoreCoder[0];
1739 :
1740 2148 : fade_len = (int16_t) ( ( STEREO_MDCT2DFT_FADE_LEN_48k * st->output_Fs ) / 48000 );
1741 2148 : fade_len_LB = (int16_t) ( 3 * ( STEREO_MDCT2DFT_FADE_LEN_48k * st->L_frame * FRAMES_PER_SEC ) / 48000 );
1742 :
1743 2148 : tmpF = 1.f / fade_len;
1744 231828 : for ( i = 0; i < fade_len; i++ )
1745 : {
1746 229680 : synth0[i] = ( hCPE->old_out_mdct[i] * ( fade_len - i ) + synth0[i] * i ) * tmpF;
1747 : }
1748 :
1749 2148 : tmpF = 1.f / fade_len_LB;
1750 228516 : for ( i = 0; i < fade_len_LB; i++ )
1751 : {
1752 226368 : output0[i] = ( hCPE->old_outLB_mdct[i] * ( fade_len_LB - i ) + output0[i] * i ) * tmpF;
1753 : }
1754 :
1755 2148 : return;
1756 : }
1757 :
1758 :
1759 960 : static float ncross_corr_self(
1760 : float *signal,
1761 : const int16_t x,
1762 : const int16_t y,
1763 : const int16_t corr_len,
1764 : const int16_t subsampling )
1765 : {
1766 : float c_c;
1767 : float energy_xy, energy_x, energy_y;
1768 : uint16_t j;
1769 : float *signal_a, *signal_b;
1770 :
1771 960 : c_c = 0.0f;
1772 960 : energy_x = 0.0f;
1773 960 : energy_y = 0.0f;
1774 960 : signal_a = &signal[x];
1775 960 : signal_b = &signal[y];
1776 :
1777 8640 : for ( j = 0; j < corr_len; j += subsampling )
1778 : {
1779 7680 : c_c += ( signal_a[j] * signal_b[j] );
1780 7680 : energy_x += ( signal_a[j] ) * ( signal_a[j] );
1781 7680 : energy_y += ( signal_b[j] ) * ( signal_b[j] );
1782 : }
1783 :
1784 960 : energy_xy = sqrtf( energy_x * energy_y );
1785 :
1786 960 : if ( energy_xy < 1.0f )
1787 : {
1788 0 : energy_xy = 1.0f; /* conceal silent frames */
1789 : }
1790 960 : c_c = c_c / energy_xy;
1791 :
1792 960 : return c_c;
1793 : }
1794 :
1795 :
1796 : /*-------------------------------------------------------------------*
1797 : * Function smooth_dft2td_transition()
1798 : *
1799 : * apply smoothing to avoid discontinuities and energy variation when
1800 : * switching from DFT stereo to TD stereo
1801 : *-------------------------------------------------------------------*/
1802 :
1803 450150 : void smooth_dft2td_transition(
1804 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
1805 : float *output[CPE_CHANNELS], /* i/o: synthesis @external Fs */
1806 : const int16_t output_frame /* i : output frame lenght */
1807 : )
1808 : {
1809 : Decoder_State **sts;
1810 : int16_t ipit, i, ipit_max, ipit_min, idiff, ilen, j, ch, lsearch, corr_len, subsampl;
1811 : float flen, ftmp_corr, fmaxcorr, fac_fs;
1812 : float tmp_out[L_FRAME48k + L_FRAME48k / 2], tmp_out2[L_FRAME48k], *ptO2, *ptO, *pt1, *ptE;
1813 :
1814 450150 : if ( hCPE == NULL )
1815 : {
1816 0 : return;
1817 : }
1818 :
1819 : /* initialization */
1820 450150 : sts = hCPE->hCoreCoder;
1821 :
1822 450150 : if ( ( hCPE->element_mode == IVAS_CPE_TD && ( hCPE->last_element_mode == IVAS_CPE_DFT || hCPE->last_element_mode == IVAS_CPE_MDCT ) && ( sts[0]->clas_dec == VOICED_CLAS && sts[0]->coder_type < TRANSITION && sts[0]->coder_type > UNVOICED && sts[0]->last_coder_type > UNVOICED && sts[1]->coder_type > UNVOICED ) ) )
1823 : {
1824 : /* length of OVA */
1825 3 : ilen = output_frame / 2;
1826 :
1827 : /* correlation length */
1828 3 : corr_len = output_frame / 20;
1829 3 : subsampl = 4;
1830 :
1831 9 : for ( ch = 0; ch < hCPE->nchan_out; ch++ )
1832 : {
1833 : /* core to external sampling frequency ratio */
1834 6 : fac_fs = (float) output_frame / sts[ch]->L_frame;
1835 :
1836 : /* Find minimum and maximum pitch*/
1837 6 : ipit_min = minimum( sts[ch]->old_pitch_buf + 4, 4, &flen ) + 4;
1838 6 : ipit_max = maximum( sts[ch]->old_pitch_buf + 4, 4, &flen ) + 4;
1839 6 : ipit_min = (short) ( sts[ch]->old_pitch_buf[ipit_min] * fac_fs + 0.5f );
1840 6 : ipit_max = (short) ( sts[ch]->old_pitch_buf[ipit_max] * fac_fs + 0.5f );
1841 :
1842 6 : if ( ( ipit_max + corr_len ) > ilen ) /*ensure the search is performed on the available memory*/
1843 : {
1844 : #ifdef DEBUG_MODE_TD
1845 : /*printf( "**********Pitch too long = %d, skipping this loop at frame %d\n", ipit_max + corr_len, frame );*/
1846 : #endif
1847 3 : continue;
1848 : }
1849 :
1850 3 : lsearch = ipit_max - ipit_min + corr_len;
1851 3 : lsearch = min( lsearch, output_frame / 4 );
1852 :
1853 : /* ptr init for search of the best correlation in the past frame */
1854 3 : ptE = hCPE->prev_synth_chs[ch] + output_frame - ipit_max - corr_len;
1855 :
1856 3 : idiff = 0;
1857 3 : fmaxcorr = -1.0f;
1858 483 : for ( i = 0; i < lsearch; i++ )
1859 : {
1860 480 : ftmp_corr = ncross_corr_self( ptE, i, ipit_max, corr_len, subsampl );
1861 480 : if ( ftmp_corr > fmaxcorr )
1862 : {
1863 42 : idiff = i;
1864 : }
1865 480 : fmaxcorr = max( fmaxcorr, ftmp_corr );
1866 : }
1867 :
1868 3 : ipit = ipit_max - idiff;
1869 :
1870 3 : ptO = tmp_out + output_frame;
1871 :
1872 : /* If the correlation is too low, don't use the prediction */
1873 3 : if ( fmaxcorr < DFT2TD_CORR_THRESH )
1874 : {
1875 0 : mvr2r( &output[ch][0], ptO, ilen );
1876 : }
1877 : else
1878 : {
1879 3 : mvr2r( hCPE->prev_synth_chs[ch], tmp_out, output_frame );
1880 3 : pt1 = tmp_out + output_frame - ipit;
1881 963 : for ( i = 0; i < ilen; i++ )
1882 : {
1883 960 : ptO[i] = pt1[i];
1884 : }
1885 : }
1886 : #ifdef DEBUG_MODE_TD
1887 : /*printf( "ch %d, ipit_max %d ipit_min %d, ipit %d :::: lsearch %d\tidiff %d\t%.4f At frame : \t%d\n", ch, ipit_max, ipit_min, ipit, lsearch, idiff, fmaxcorr, frame );*/
1888 : #endif
1889 :
1890 : /* Set buffer for the reserved buffer of the current frame */
1891 3 : ptO2 = tmp_out2 + output_frame - ilen;
1892 3 : set_f( tmp_out2 + output_frame - ilen, 0.0f, ilen );
1893 1923 : for ( i = 0; i < output_frame; i++ )
1894 : {
1895 1920 : tmp_out2[i] = output[ch][output_frame - 1 - i];
1896 : }
1897 : /* ptr init for search of the best correlation of the current frame */
1898 3 : ptE = ptO2 - ( ipit_max + corr_len );
1899 :
1900 3 : idiff = 0;
1901 3 : fmaxcorr = -1.0f;
1902 483 : for ( i = 0; i < lsearch; i++ )
1903 : {
1904 480 : ftmp_corr = ncross_corr_self( ptE, i, ipit_max, corr_len, subsampl );
1905 480 : if ( ftmp_corr > fmaxcorr )
1906 : {
1907 27 : idiff = i;
1908 : }
1909 480 : fmaxcorr = max( fmaxcorr, ftmp_corr );
1910 : }
1911 :
1912 3 : ipit = ipit_max - idiff;
1913 :
1914 : /* If the correlation is too low, don't use the prediction */
1915 3 : if ( fmaxcorr > DFT2TD_CORR_THRESH )
1916 : {
1917 3 : pt1 = tmp_out2 + output_frame - ilen - ipit;
1918 963 : for ( i = 0; i < ilen; i++ )
1919 : {
1920 960 : ptO2[i] = pt1[i];
1921 : }
1922 : }
1923 : #ifdef DEBUG_MODE_TD
1924 : /*printf( "ch %d, ipit_max %d ipit_min %d, ipit %d :::: lsearch %d\tidiff %d\t%.4f At frame : \t%d\n", ch, ipit_max, ipit_min, ipit, lsearch, idiff, fmaxcorr, frame );*/
1925 : #endif
1926 :
1927 : /* perform OVA between predicted signals */
1928 3 : flen = 1.0f / ilen;
1929 :
1930 15 : for ( i = 0; i < 4; i++ )
1931 : {
1932 12 : output[ch][i] = ( ( ( 4.0f - i ) * output[ch][i] + i * ptO[i] ) * 0.25f * ( ilen - i ) + tmp_out2[output_frame - 1 - i] * i ) * flen;
1933 : }
1934 939 : for ( ; i < ilen - 4; i++ )
1935 : {
1936 936 : output[ch][i] = ( ptO[i] * ( ilen - i ) + tmp_out2[output_frame - 1 - i] * i ) * flen;
1937 : }
1938 3 : j = 0;
1939 15 : for ( ; i < ilen; i++ )
1940 : {
1941 12 : output[ch][i] = ( ptO[i] * ( ilen - i ) + ( ( 4.0f - j ) * tmp_out2[output_frame - 1 - i] + j * output[ch][i] ) * i * 0.25f ) * flen;
1942 12 : j++;
1943 : }
1944 : }
1945 : }
1946 450147 : else if ( hCPE->element_mode == IVAS_CPE_DFT )
1947 : {
1948 : /* Updates */
1949 469809 : for ( ch = 0; ch < hCPE->nchan_out; ch++ )
1950 : {
1951 290751 : mvr2r( output[ch], hCPE->prev_synth_chs[ch], output_frame );
1952 : }
1953 : }
1954 :
1955 450150 : return;
1956 : }
|