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 <assert.h>
34 : #include <stdint.h>
35 : #include "options.h"
36 : #include "cnst.h"
37 : #include "rom_com.h"
38 : #include "prot.h"
39 : #include "ivas_prot.h"
40 : #include "ivas_cnst.h"
41 : #ifdef DEBUGGING
42 : #include "debug.h"
43 : #endif
44 : #include "wmc_auto.h"
45 :
46 : /*-------------------------------------------------------------------------
47 : * ivas_post_proc()
48 : *
49 : * IVAS post-processing
50 : * - in SCE, post-processing of output channel
51 : * - in CPE_DFT, post-processing on output channels
52 : * - in CPE_TD, post-processing on decoded channels before upmixing
53 : *-------------------------------------------------------------------------*/
54 :
55 35600948 : void ivas_post_proc(
56 : SCE_DEC_HANDLE hSCE, /* i/o: SCE decoder structure */
57 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
58 : const int16_t n, /* i : channel number */
59 : float synth[], /* i/o: output synthesis signal */
60 : float *output[CPE_CHANNELS], /* i/o: output synthesis signal */
61 : const int16_t output_frame, /* i : output frame length */
62 : const int16_t sba_dirac_stereo_flag /* i : signal stereo output for SBA DirAC */
63 : )
64 : {
65 : #ifndef DEBUG_STEREO_DFT_NOCORE
66 : int16_t k;
67 : #endif
68 : int16_t delay_comp;
69 : int32_t output_Fs;
70 : Decoder_State **sts;
71 : TCX_LTP_DEC_HANDLE hTcxLtpDec;
72 :
73 35600948 : if ( hSCE != NULL )
74 : {
75 8564959 : sts = hSCE->hCoreCoder;
76 : }
77 : else
78 : {
79 27035989 : sts = hCPE->hCoreCoder;
80 : }
81 :
82 35600948 : output_Fs = sts[0]->output_Fs;
83 :
84 35600948 : if ( ( sts[n]->element_mode != IVAS_CPE_DFT && !( sba_dirac_stereo_flag && sts[n]->element_mode != IVAS_CPE_MDCT ) ) || ( sts[n]->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 && hCPE->hStereoDft->hConfig->res_cod_mode == STEREO_DFT_RES_COD_OFF ) )
85 : {
86 34719161 : if ( sts[n]->hTcxLtpDec != NULL )
87 : {
88 34680027 : hTcxLtpDec = sts[n]->hTcxLtpDec;
89 :
90 34680027 : if ( sts[n]->core != TCX_20_CORE && sts[n]->core != TCX_10_CORE )
91 : {
92 : /* TCX-LTP Postfilter: used in Mode 1 to update memories and to avoid discontinuities when the past frame was TCX */
93 3646698 : tcx_ltp_post( sts[n], hTcxLtpDec, ACELP_CORE, output_frame, 0, synth, NULL );
94 : }
95 : else
96 : {
97 : /* set delay */
98 31033329 : delay_comp = NS2SA( output_Fs, DELAY_CLDFB_NS );
99 :
100 31033329 : if ( !( sts[n]->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 && hCPE->hStereoDft->hConfig->res_cod_mode == STEREO_DFT_RES_COD_OFF ) && sts[n]->element_mode != IVAS_CPE_MDCT )
101 : {
102 5797244 : mvr2r( sts[n]->prev_synth_buffer, sts[n]->hTcxDec->FBTCXdelayBuf, 0 );
103 5797244 : mvr2r( sts[n]->delay_buf_out, sts[n]->hTcxDec->FBTCXdelayBuf + 0, delay_comp );
104 : }
105 25236085 : else if ( sba_dirac_stereo_flag && sts[n]->element_mode == IVAS_CPE_MDCT )
106 : {
107 554590 : int16_t numZeros = (int16_t) ( NS2SA( output_Fs, N_ZERO_MDCT_NS ) );
108 554590 : mvr2r( sts[n]->hHQ_core->old_out + numZeros, sts[n]->hTcxDec->FBTCXdelayBuf, delay_comp );
109 : }
110 :
111 31033329 : tcx_ltp_post( sts[n], hTcxLtpDec, sts[n]->core, output_frame, NS2SA( output_Fs, ACELP_LOOK_NS ) + delay_comp, synth, sts[n]->hTcxDec->FBTCXdelayBuf );
112 : }
113 : }
114 : }
115 : else /* IVAS_CPE_DFT */
116 : {
117 : #ifndef DEBUG_STEREO_DFT_NOCORE
118 : int16_t pit_res_max_past_tmp;
119 :
120 881787 : pit_res_max_past_tmp = sts[0]->pit_res_max_past;
121 2560979 : for ( k = 0; k < hCPE->nchan_out; k++ )
122 : {
123 1679192 : if ( k == 0 )
124 : {
125 881787 : hTcxLtpDec = sts[0]->hTcxLtpDec;
126 : }
127 : else
128 : {
129 797405 : hTcxLtpDec = hCPE->hStereoDft->hTcxLtpDec;
130 : /* copy LTP side-info of downmix channel also to right channel struct */
131 797405 : hTcxLtpDec->tcxltp = sts[0]->hTcxLtpDec->tcxltp;
132 797405 : hTcxLtpDec->tcxltp_gain = sts[0]->hTcxLtpDec->tcxltp_gain;
133 797405 : hTcxLtpDec->tcxltp_pitch_int = sts[0]->hTcxLtpDec->tcxltp_pitch_int;
134 797405 : hTcxLtpDec->tcxltp_pitch_fr = sts[0]->hTcxLtpDec->tcxltp_pitch_fr;
135 : /* revert update of pit_res_max_past to have correct value also in right channel */
136 797405 : sts[0]->pit_res_max_past = pit_res_max_past_tmp;
137 : }
138 :
139 : /*TCX-LTP*/
140 1679192 : if ( sts[0]->core != TCX_20_CORE && sts[0]->core != TCX_10_CORE )
141 : {
142 : /* update memories and to avoid discontinuities when the past frame was TCX */
143 881725 : tcx_ltp_post( sts[0], hTcxLtpDec, ACELP_CORE, output_frame, 0, output[k], NULL );
144 : }
145 : else
146 : {
147 : /*Use channel 0 side info.*/
148 797467 : tcx_ltp_post( sts[0], hTcxLtpDec, TCX_20_CORE, output_frame, NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ), output[k], hCPE->output_mem[k] );
149 : }
150 : }
151 : #endif
152 : }
153 :
154 35600948 : return;
155 : }
156 :
157 :
158 : /*-------------------------------------------------------------------------
159 : * stereo_dft_dec_core_switching()
160 : *
161 : * core switching in DFT stereo
162 : *-------------------------------------------------------------------------*/
163 :
164 1310565 : void stereo_dft_dec_core_switching(
165 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
166 : float output[], /* i/o: synthesis @internal Fs */
167 : float synth[], /* i/o: synthesis @output Fs */
168 : float hb_synth[], /* i/o: hb synthesis */
169 : float DFT[CPE_CHANNELS][STEREO_DFT_BUF_MAX], /* o : DFT buffers */
170 : const int16_t output_frame, /* i : output frame length */
171 : const int16_t use_cldfb_for_dft, /* i : flag to use of CLDFB for DFT Stereo */
172 : const int16_t sba_dirac_stereo_dtx_flag /* i : DTX indicator for SBA DirAC stereo */
173 : )
174 : {
175 : int16_t i, tmps;
176 : int16_t delay_dft_dec, delay_dft_dec_lb, delay_tdbwe, delay_comp;
177 : int16_t L_frameTCX;
178 : int32_t output_Fs;
179 : Decoder_State *st;
180 : int16_t predelay, ap_fade_len;
181 : float pAp_input[L_FRAME16k];
182 : #ifndef DEBUG_STEREO_DFT_NOCORE
183 : float tcx_core_buf[L_FRAME16k];
184 : float synth_tmp[L_FRAME48k];
185 : #endif
186 :
187 1310565 : st = hCPE->hCoreCoder[0]; /* in DFT stereo, only M channel is decoded by the CoreCoder */
188 1310565 : output_Fs = st->output_Fs;
189 1310565 : L_frameTCX = st->hTcxDec->L_frameTCX;
190 :
191 : /*TBE*/
192 1310565 : delay_tdbwe = NS2SA( output_Fs, DELAY_BWE_TOTAL_NS );
193 1310565 : delay_dft_dec = NS2SA( output_Fs, STEREO_DFT_DELAY_DEC_BWE_NS );
194 1310565 : delay_dft_dec_lb = NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT_DELAY_DEC_BWE_NS );
195 :
196 : /* TCX/ACELP/HQ-CORE->TCX */
197 1310565 : tmps = NS2SA( output_Fs, DELAY_BWE_TOTAL_NS ); /*cross-fading size @ FB*/
198 1310565 : delay_comp = NS2SA( st->L_frame * FRAMES_PER_SEC, DELAY_BWE_TOTAL_NS ); /*cross-fading size @ LB*/
199 :
200 : #if defined( DEBUG_MODE_DFT ) && defined( DEBUG_STEREO_DFT_NOCORE )
201 : stereo_dft_dec_analyze( hCPE, output, DFT, 0, output_frame, output_frame, DFT_STEREO_DEC_ANA_NOCORE, 0, 0 );
202 : #else
203 : /* resample DFT analysis memories in case of internal sampling rate change */
204 1310565 : if ( st->last_L_frame != st->L_frame && st->last_L_frame <= L_FRAME16k && st->L_frame <= L_FRAME16k )
205 : {
206 8789 : lerp( hCPE->input_mem_LB[0], hCPE->input_mem_LB[0], NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ), NS2SA( st->last_L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
207 8789 : lerp( hCPE->input_mem_BPF[0], hCPE->input_mem_BPF[0], NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ), NS2SA( st->last_L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
208 : }
209 :
210 1310565 : if ( st->prev_bfi && !( st->last_core_bfi == ACELP_CORE && st->last_con_tcx == 1 ) )
211 : {
212 : /* last_core needed for correctly decoding ACELP->TCX/HQ switching frames in ivas_core_dec().
213 : In the following steps the decoder needs to consider if the core was changed due to a lost frame to apply the correct transition */
214 51203 : st->last_core = st->last_core_bfi;
215 : }
216 :
217 1310565 : if ( st->core == TCX_20_CORE || st->core == TCX_10_CORE || st->core == HQ_CORE || ( st->bfi == 1 && st->core == ACELP_CORE && st->con_tcx == 1 ) )
218 : {
219 : #ifdef DEBUGGING
220 : assert( L_frameTCX == output_frame );
221 : #endif
222 548976 : if ( ( ( st->last_core != ACELP_CORE || ( st->prev_bfi == 1 && st->last_core == ACELP_CORE && st->last_con_tcx == 1 ) ) && st->last_core != AMR_WB_CORE ) || ( sba_dirac_stereo_dtx_flag && st->cng_type == FD_CNG ) ) /* TCX / HQ-CORE -> TCX / HQ-CORE */
223 : {
224 : /* In case of a TCX to ACELP switch next frame */
225 525611 : mvr2r( &output[st->L_frame - NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS )], hCPE->hStereoDft->buff_LBTCX_mem, NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
226 :
227 : /* BPF */
228 525611 : if ( st->p_bpf_noise_buf && st->core != HQ_CORE )
229 : {
230 501382 : stereo_dft_dec_analyze( hCPE, st->p_bpf_noise_buf, DFT, 0, st->L_frame, output_frame, DFT_STEREO_DEC_ANA_BPF, 2, 0 );
231 : }
232 : /* st->p_bpf_noise_buf not updated for HQ core -> skip analysis and set input memory to zero */
233 24229 : else if ( st->p_bpf_noise_buf && st->core == HQ_CORE )
234 : {
235 23960 : set_zero( hCPE->input_mem_BPF[0], STEREO_DFT32MS_OVL_16k );
236 : }
237 :
238 : /* TCX */
239 525611 : stereo_dft_dec_analyze( hCPE, synth, DFT, 0, L_frameTCX, output_frame, DFT_STEREO_DEC_ANA_FB, 0, 0 );
240 : }
241 23365 : else if ( st->core == TCX_20_CORE || st->core == TCX_10_CORE || st->core == HQ_CORE ) /* ACELP -> TCX/HQ */
242 : {
243 23365 : if ( st->last_core_brate <= SID_2k40 && st->element_mode == IVAS_SCE )
244 8 : {
245 8 : int16_t mem_len = NS2SA( L_frameTCX * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS );
246 1208 : for ( i = 0; i < mem_len; i++ )
247 : {
248 1200 : hCPE->input_mem[0][i] *= ( mem_len - i ) / mem_len;
249 : }
250 :
251 : /* Update FB input buff with hb synth for last delay_tdbwe sampled */
252 896 : for ( i = 0; i < delay_tdbwe; i++ )
253 : {
254 888 : hCPE->input_mem[0][NS2SA( L_frameTCX * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) - delay_tdbwe + i] += hb_synth[i];
255 : }
256 : }
257 : else
258 : {
259 : /* Update FB input buff with hb synth for last delay_tdbwe sampled */
260 1948097 : for ( i = 0; i < delay_tdbwe; i++ )
261 : {
262 1924740 : hCPE->input_mem[0][NS2SA( L_frameTCX * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) - delay_tdbwe + i] = hb_synth[i];
263 : }
264 : }
265 :
266 : /* cross-fading between (delayed) TBE and FB-TCX over 2.3125ms */
267 23365 : mvr2r( synth, synth_tmp, output_frame );
268 1948993 : for ( i = 0; i < tmps; i++ )
269 : {
270 1925628 : synth_tmp[i] = ( hb_synth[i + delay_tdbwe] * ( tmps - i ) + synth[i] * i ) / tmps;
271 : }
272 :
273 : /* FB-TCX */
274 23365 : stereo_dft_dec_analyze( hCPE, synth_tmp, DFT, 0, L_frameTCX, output_frame, DFT_STEREO_DEC_ANA_FB, 0, 0 );
275 :
276 23365 : if ( !st->tcxonly )
277 : {
278 23319 : if ( hCPE->hStereoDft->hConfig->res_pred_mode == STEREO_DFT_RESPRED_ESF )
279 : {
280 15352 : mvr2r( output, tcx_core_buf, L_FRAME16k );
281 : }
282 : /* fading-out LB-TCX/ACELP */
283 832578 : for ( i = 0; i < delay_comp; i++ )
284 : {
285 809259 : output[i] = ( output[i] * ( delay_comp - i ) ) / delay_comp;
286 : }
287 :
288 : /* In case of TCX frames, output LB TCX is zeroed out until the end but in case of an TCX->ACELP switch, this memory is needed, hence it is backed up */
289 : /* Unlike the case when DFT32MS is disabled, there is only 1 DFT analysis window, hence in the TCX frame we don't want to do a DFT analysis for LB TCX
290 : but in a potential ACELP frame, we want the memories of the LB TCX for the last OLA samples so that HB analysis can be skipped */
291 23319 : mvr2r( &output[st->L_frame - NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS )], hCPE->hStereoDft->buff_LBTCX_mem, NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
292 :
293 : /*zero the rest for avoiding adding contribution except the last samples which are not considered in DFT analysis (potentially used in next ACELP frame)*/
294 23319 : if ( st->last_core_brate > SID_2k40 )
295 : {
296 6147888 : for ( ; i < st->L_frame; i++ )
297 : {
298 6124932 : output[i] = 0.0f;
299 : }
300 :
301 22956 : stereo_dft_dec_analyze( hCPE, output, DFT, 0, st->L_frame, output_frame, DFT_STEREO_DEC_ANA_LB_ADD, 1, 0 );
302 :
303 : /* BPF */
304 22956 : if ( st->p_bpf_noise_buf )
305 : {
306 22956 : set_zero( hCPE->input_mem_BPF[0], NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
307 : }
308 : }
309 : }
310 : }
311 : }
312 : else /* ACELP core */
313 : {
314 761589 : if ( st->core_brate <= SID_2k40 && !( sba_dirac_stereo_dtx_flag ) )
315 : {
316 59063 : set_zero( hCPE->input_mem[0], NS2SA( st->hTcxDec->L_frameTCX * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
317 59063 : set_zero( hCPE->input_mem_LB[0], NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
318 :
319 : /* CNG generated in ivas_cpe_dec() */
320 : }
321 702526 : else if ( st->last_core == TCX_20_CORE || st->last_core == TCX_10_CORE || st->last_core == HQ_CORE ) /* TCX/HQ -> ACELP */
322 : {
323 32808 : if ( ( st->last_L_frame <= L_FRAME16k && st->L_frame <= L_FRAME16k ) || ( sba_dirac_stereo_dtx_flag && st->core_brate == SID_2k40 && st->cng_type == FD_CNG ) )
324 : {
325 : /* In case of a TCX to ACELP switch, retrieve the LB-TCX memories for the first STEREO_DFT32MS_OVL_NS NS of OLA */
326 21287 : lerp( hCPE->hStereoDft->buff_LBTCX_mem, hCPE->input_mem_LB[0], NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ), NS2SA( st->last_L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
327 : }
328 :
329 32808 : if ( sba_dirac_stereo_dtx_flag && st->core_brate == SID_2k40 && st->cng_type == FD_CNG )
330 16 : {
331 : int16_t nZeros;
332 16 : nZeros = (int16_t) ( NS2SA( st->output_Fs, N_ZERO_MDCT_NS ) );
333 16 : delay_comp = NS2SA( st->output_Fs, DELAY_CLDFB_NS );
334 :
335 16 : mvr2r( &st->hHQ_core->old_out[nZeros - delay_comp - NS2SA( output_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS )], hCPE->input_mem[0], NS2SA( output_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
336 16 : mvr2r( synth, synth_tmp, output_frame );
337 :
338 : int16_t mem_len;
339 16 : mem_len = NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS );
340 : float fac;
341 16 : fac = 1.0f / mem_len;
342 816 : for ( i = 0; i < mem_len; i++ )
343 : {
344 800 : hCPE->input_mem_LB[0][i] *= ( 1 - i * fac );
345 : }
346 16 : set_zero( output, st->L_frame );
347 :
348 16 : stereo_dft_dec_analyze( hCPE, output, DFT, 0, st->L_frame, output_frame, DFT_STEREO_DEC_ANA_LB, 0, 0 );
349 :
350 : /* BPF */
351 16 : if ( st->p_bpf_noise_buf )
352 : {
353 16 : stereo_dft_dec_analyze( hCPE, st->p_bpf_noise_buf, DFT, 0, st->L_frame, output_frame, DFT_STEREO_DEC_ANA_BPF, 0, 0 );
354 : }
355 : }
356 : else
357 : {
358 : /* ACELP */
359 913730 : for ( i = 0; i < ( NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) - delay_dft_dec_lb ); i++ )
360 : {
361 880938 : hCPE->input_mem_LB[0][i] = 0.f;
362 : }
363 : /* ACELP fading-in*/
364 620084 : for ( i = 0; i < delay_dft_dec_lb; i++ )
365 : {
366 587292 : hCPE->input_mem_LB[0][NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) - delay_dft_dec_lb + i] *= (float) ( i ) / (float) ( delay_dft_dec_lb );
367 : }
368 :
369 32792 : stereo_dft_dec_analyze( hCPE, output, DFT, 0, st->L_frame, output_frame, DFT_STEREO_DEC_ANA_LB, 0, 0 );
370 :
371 : /* BPF */
372 32792 : if ( st->p_bpf_noise_buf )
373 : {
374 20935 : stereo_dft_dec_analyze( hCPE, st->p_bpf_noise_buf, DFT, 0, st->L_frame, output_frame, DFT_STEREO_DEC_ANA_BPF, 0, 0 );
375 : }
376 :
377 : /* Fading-in TD-BWE, Fading-out FB-TCX*/
378 32792 : mvr2r( synth, synth_tmp, output_frame );
379 32792 : if ( hCPE->last_element_mode != IVAS_CPE_MDCT )
380 : {
381 937421 : for ( i = 0; i < delay_dft_dec; i++ ) /* 1.25 ms of hb_synth fade-in and TCX fade-out */
382 : {
383 916760 : hCPE->input_mem[0][NS2SA( L_frameTCX * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) - delay_dft_dec + i] = ( hb_synth[delay_tdbwe - delay_dft_dec + i] * ( i ) + hCPE->input_mem[0][NS2SA( L_frameTCX * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) - delay_dft_dec + i] * ( delay_dft_dec - i ) ) / ( delay_dft_dec );
384 : }
385 937421 : for ( i = 0; i < delay_dft_dec; i++ ) /* 1.25 ms of hb_synth fade out. ICBWE fade in performed in time domain here */
386 : {
387 916760 : synth_tmp[i] = ( hb_synth[delay_tdbwe - delay_dft_dec + i] * ( delay_dft_dec - i ) ) / delay_dft_dec;
388 : }
389 :
390 13772061 : for ( ; i < output_frame; i++ )
391 : {
392 13751400 : synth_tmp[i] = 0;
393 : }
394 : }
395 : }
396 :
397 32808 : if ( sba_dirac_stereo_dtx_flag )
398 : {
399 16 : stereo_dft_dec_analyze( hCPE, synth_tmp, DFT, 0, output_frame, output_frame, DFT_STEREO_DEC_ANA_FB_ADD, 0, 0 );
400 : }
401 32792 : else if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
402 : {
403 12131 : stereo_dft_dec_analyze( hCPE, synth_tmp, DFT, 0, output_frame, output_frame, DFT_STEREO_DEC_ANA_HB_ADD, 1, 0 );
404 : }
405 : else
406 : {
407 20661 : stereo_dft_dec_analyze( hCPE, synth_tmp, DFT, 0, output_frame, output_frame, DFT_STEREO_DEC_ANA_FB_ADD, 1, 0 );
408 : }
409 : }
410 : else /* ACELP -> ACELP */
411 : {
412 669718 : if ( sba_dirac_stereo_dtx_flag && st->core_brate <= SID_2k40 && st->cng_type == FD_CNG )
413 : {
414 508 : set_zero( output, st->L_frame );
415 : }
416 : /* this needs an indication for sba2stereo in general */
417 669210 : else if ( hCPE->element_mode == IVAS_SCE && st->last_core_brate <= SID_2k40 && st->cng_type == FD_CNG )
418 : {
419 12 : lerp( hCPE->input_mem[0], hCPE->input_mem_LB[0], NS2SA( st->L_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ), NS2SA( output_frame * FRAMES_PER_SEC, STEREO_DFT32MS_OVL_NS ) );
420 : }
421 :
422 : /* ACELP synthesis @ internal sampling rate */
423 669718 : stereo_dft_dec_analyze( hCPE, output, DFT, 0, st->L_frame, output_frame, DFT_STEREO_DEC_ANA_LB, 0, 0 );
424 :
425 : /* BPF */
426 669718 : if ( st->p_bpf_noise_buf )
427 : {
428 628801 : stereo_dft_dec_analyze( hCPE, st->p_bpf_noise_buf, DFT, 0, st->L_frame, output_frame, DFT_STEREO_DEC_ANA_BPF, 0, 0 );
429 : }
430 :
431 : /* BWE */
432 669718 : if ( sba_dirac_stereo_dtx_flag && st->core_brate <= SID_2k40 && st->cng_type == FD_CNG )
433 : {
434 508 : stereo_dft_dec_analyze( hCPE, synth, DFT, 0, output_frame, output_frame, DFT_STEREO_DEC_ANA_FB_ADD, 0, 0 );
435 : }
436 669210 : else if ( st->extl != -1 || ( st->bws_cnt > 0 && st->core == ACELP_CORE ) )
437 : {
438 593190 : stereo_dft_dec_analyze( hCPE, hb_synth, DFT, 0, output_frame, output_frame, DFT_STEREO_DEC_ANA_FB_ADD, 2, -delay_tdbwe );
439 : }
440 : }
441 : }
442 : #endif
443 :
444 : /*----------------------------------------------------------------*
445 : * enhanced stereo filling: allpass filter
446 : *----------------------------------------------------------------*/
447 :
448 1310565 : if ( hCPE->hStereoDft->hConfig->res_pred_mode == STEREO_DFT_RESPRED_ESF )
449 : {
450 : #if defined( DEBUG_MODE_INFO ) && defined( DEBUG_STEREO_DFT_NOCORE )
451 : {
452 : char file_name[100];
453 : int16_t tmp[320];
454 : {
455 : sprintf( file_name, "./res/stereo_dft_enc_M_S_%d_c%d_b0.pcm", st->L_frame * FRAMES_PER_SEC, 0 );
456 : }
457 : dbgread( tmp, sizeof( int16_t ), st->L_frame, file_name );
458 : for ( i = 0; i < st->L_frame; i++ )
459 : {
460 : pAp_input[i] = (float) ( tmp[i] );
461 : }
462 : }
463 : #endif
464 688763 : if ( st->sr_core == INT_FS_12k8 )
465 : {
466 341319 : ap_fade_len = STEREO_DFT_ALLPASS_FADELEN_12k8;
467 : }
468 : else
469 : {
470 347444 : ap_fade_len = STEREO_DFT_ALLPASS_FADELEN_16k;
471 : }
472 :
473 : #if !defined( DEBUG_MODE_INFO ) || !defined( DEBUG_STEREO_DFT_NOCORE )
474 688763 : if ( st->core == TCX_20_CORE || st->core == TCX_10_CORE || st->core == HQ_CORE || ( st->bfi == 1 && st->core == ACELP_CORE && st->con_tcx == 1 ) )
475 334527 : {
476 334527 : int16_t numZeros = (int16_t) ( NS2SA( st->sr_core, N_ZERO_MDCT_NS ) );
477 : float tmp_fade[max( STEREO_DFT_ALLPASS_FADELEN_12k8, STEREO_DFT_ALLPASS_FADELEN_16k )];
478 :
479 334527 : mvr2r( st->hHQ_core->old_outLB + numZeros, hCPE->hStereoDft->ap_fade_mem, ap_fade_len );
480 :
481 334527 : if ( st->last_core == ACELP_CORE && !( st->prev_bfi == 1 && st->last_core == ACELP_CORE && st->last_con_tcx == 1 ) && !st->tcxonly )
482 : {
483 15352 : mvr2r( tcx_core_buf, pAp_input, st->L_frame );
484 : }
485 : else
486 : {
487 319175 : mvr2r( output, pAp_input, st->L_frame );
488 : }
489 :
490 334527 : if ( st->last_core == ACELP_CORE && !( st->prev_bfi == 1 && st->last_core == ACELP_CORE && st->last_con_tcx == 1 ) && !st->tcxonly ) /* ACELP -> TCX/HQ-Core */
491 : {
492 15352 : mvr2r( tcx_core_buf, tmp_fade, ap_fade_len );
493 587584 : for ( i = 0; i < ap_fade_len; i++ )
494 : {
495 572232 : pAp_input[i] = ( pAp_input[i] * i + tmp_fade[i] * ( ap_fade_len - i ) ) / ap_fade_len;
496 : }
497 : }
498 : }
499 : else
500 : {
501 354236 : mvr2r( output, pAp_input, st->L_frame );
502 354236 : if ( st->last_core != ACELP_CORE ) /* TCX/HQ-Core -> ACELP */
503 : {
504 838475 : for ( i = 0; i < ap_fade_len; i++ )
505 : {
506 815320 : pAp_input[i] = ( pAp_input[i] * i + hCPE->hStereoDft->ap_fade_mem[i] * ( ap_fade_len - i ) ) / ap_fade_len;
507 : }
508 : }
509 : }
510 : #endif
511 688763 : predelay = NS2SA( st->sr_core, DELAY_BWE_TOTAL_NS );
512 :
513 : /* apply predelay to have same overall filter delay for all cases */
514 688763 : delay_signal( pAp_input, st->L_frame, hCPE->hStereoDft->ap_delay_mem, predelay );
515 :
516 : #ifdef DEBUG_MODE_DFT
517 : {
518 : int16_t v;
519 : int16_t out_ap[L_FRAME16k];
520 :
521 : for ( v = 0; v < st->L_frame; v++ )
522 : {
523 : out_ap[v] = (int16_t) pAp_input[v];
524 : }
525 :
526 : dbgwrite( out_ap, sizeof( int16_t ), st->L_frame, 1, "res/stereo_dft_allpass_input.pcm" );
527 : }
528 : #endif /*DEBUG_MODE_DFT*/
529 :
530 : /* input zeroes for transient frames */
531 688763 : if ( hCPE->hStereoDft->attackPresent )
532 : {
533 16048 : float inv_tmps = 1.f / ap_fade_len;
534 16048 : if ( hCPE->hStereoDft->ap_wasTransient )
535 : {
536 1060 : set_zero( pAp_input, ap_fade_len );
537 : }
538 : else
539 : {
540 562396 : for ( i = 0; i < ap_fade_len; i++ )
541 : {
542 547408 : pAp_input[i] *= ( ap_fade_len - i ) * inv_tmps;
543 : }
544 : }
545 16048 : set_zero( pAp_input + ap_fade_len, st->L_frame - ap_fade_len );
546 16048 : hCPE->hStereoDft->ap_wasTransient = 1;
547 : }
548 672715 : else if ( hCPE->hStereoDft->ap_wasTransient )
549 : {
550 14905 : float inv_tmps = 1.f / ap_fade_len;
551 559057 : for ( i = 0; i < ap_fade_len; i++ )
552 : {
553 544152 : pAp_input[i] *= i * inv_tmps;
554 : }
555 14905 : hCPE->hStereoDft->ap_wasTransient = 0;
556 : }
557 :
558 : /* apply 5-stage allpass, each stage consisting of a nested allpass pair */
559 688763 : filter_with_allpass( pAp_input, pAp_input, st->L_frame, &hCPE->hStereoDft->ap1 );
560 688763 : filter_with_allpass( pAp_input, pAp_input, st->L_frame, &hCPE->hStereoDft->ap2 );
561 688763 : filter_with_allpass( pAp_input, pAp_input, st->L_frame, &hCPE->hStereoDft->ap3 );
562 :
563 : #ifdef DEBUG_MODE_DFT
564 : {
565 : int16_t v;
566 : int16_t out_ap[L_FRAME16k];
567 : for ( v = 0; v < st->L_frame; v++ )
568 : {
569 : out_ap[v] = (int16_t) pAp_input[v];
570 : }
571 :
572 : dbgwrite( out_ap, sizeof( int16_t ), st->L_frame, 1, "res/stereo_dft_allpass.pcm" );
573 : }
574 : #endif /*DEBUG_MODE_DFT*/
575 : /* apply DFT to allpass-filtered signal */
576 688763 : stereo_dft_dec_analyze( hCPE, pAp_input, DFT, 1, st->L_frame, output_frame, DFT_STEREO_DEC_ANA_FB, 0, 0 );
577 : }
578 :
579 1310565 : if ( st->core == ACELP_CORE )
580 : {
581 765948 : if ( !use_cldfb_for_dft ) /* Skip this for DFT Stereo mono output at non-residual bitrates */
582 : {
583 446099 : lerp( output, synth, output_frame, hCPE->hCoreCoder[0]->L_frame ); /* Dirty resampling, but should be good enough for ECU analysis */
584 : }
585 765948 : if ( !use_cldfb_for_dft || ( ( st->core_brate == FRAME_NO_DATA || st->core_brate == SID_2k40 ) && st->cng_type == LP_CNG && st->extl == SWB_CNG && hCPE->nchan_out == 1 ) )
586 : {
587 450440 : v_add( synth, hb_synth, synth, output_frame ); /* Use one channel TD-BWE for ECU analysis buffer */
588 : }
589 : }
590 :
591 1310565 : return;
592 : }
|