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 : /*====================================================================================
34 : EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
35 : ====================================================================================*/
36 :
37 : #include <assert.h>
38 : #include "prot.h"
39 : #include "ivas_prot.h"
40 : #include <stdint.h>
41 : #include "options.h"
42 : #ifdef DEBUGGING
43 : #include "debug.h"
44 : #endif
45 : #include <math.h>
46 : #include "stat_com.h"
47 : #include "cnst.h"
48 : #include "wmc_auto.h"
49 : #include "ivas_rom_com.h"
50 : #ifdef DEBUG_PLOT
51 : #include "deb_out.h"
52 : #endif
53 :
54 :
55 : /*-----------------------------------------------------------------*
56 : * decoder_tcx()
57 : *
58 : *
59 : *-----------------------------------------------------------------*/
60 :
61 6397143 : void decoder_tcx(
62 : Decoder_State *st, /* i/o: coder memory state */
63 : int16_t prm[], /* i : parameters */
64 : float A[], /* i : coefficients NxAz[M+1] */
65 : Word16 Aind[], /* i : frame-independent coefficients Az[M+1] */
66 : float synth[], /* i/o: synthesis @internal_FS */
67 : float synthFB[], /* i/o: synthesis @output_FS */
68 : const int16_t bfi, /* i : Bad frame indicator */
69 : const int16_t frame_cnt, /* i : frame counter in the super frame */
70 : const int16_t sba_dirac_stereo_flag /* i : signal stereo output for SBA DirAC */
71 : )
72 : {
73 : float x[N_MAX];
74 : float gainlpc2[FDNS_NPTS];
75 : float gain_tcx;
76 : int16_t fUseTns, L_frame_glob, L_frameTCX_glob;
77 : STnsData tnsData;
78 : int16_t tcx_offset, tcx_offsetFB, L_frame, L_frameTCX;
79 : int16_t left_rect, L_spec, tmp_concealment_method, nf_seed;
80 : const int16_t *prm_sqQ;
81 : float xn_buf[L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX];
82 :
83 6397143 : L_spec = st->hTcxCfg->tcx_coded_lines;
84 6397143 : L_frame_glob = st->L_frame;
85 6397143 : L_frameTCX_glob = st->hTcxDec->L_frameTCX;
86 6397143 : if ( st->core == TCX_10_CORE )
87 : {
88 93736 : L_spec /= 2;
89 93736 : L_frame_glob /= 2;
90 93736 : L_frameTCX_glob /= 2;
91 : }
92 :
93 6397143 : tmp_concealment_method = 0;
94 6397143 : nf_seed = 0;
95 6397143 : fUseTns = 0; /* flag that is set if TNS data is present */
96 6397143 : prm_sqQ = NULL;
97 :
98 6397143 : set_f( xn_buf, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX );
99 :
100 6397143 : init_tcx_info( st, L_frame_glob, L_frameTCX_glob, frame_cnt, bfi, &tcx_offset, &tcx_offsetFB, &L_frame, &L_frameTCX, &left_rect, &L_spec );
101 :
102 6397143 : decoder_tcx_invQ( st, prm, A, Aind, L_spec, L_frame, L_frameTCX, &x[0], &gainlpc2[0], &xn_buf[0], &fUseTns, &tnsData, &gain_tcx, &prm_sqQ, &nf_seed, bfi, frame_cnt );
103 :
104 6397143 : decoder_tcx_noisefilling( st, NULL, A, L_frameTCX_glob, L_spec, L_frame, L_frameTCX, &x[0], &gainlpc2[0], &tmp_concealment_method, gain_tcx, prm_sqQ, nf_seed, bfi, 0, frame_cnt );
105 :
106 6397143 : decoder_tcx_noiseshaping_igf( st, L_spec, L_frame, L_frameTCX, left_rect, &x[0], &gainlpc2[0], &tmp_concealment_method, bfi );
107 :
108 6397143 : decoder_tcx_tns( st, L_frame_glob, L_spec, L_frame, L_frameTCX, &x[0], fUseTns, &tnsData, bfi, frame_cnt, 0 );
109 :
110 6397143 : decoder_tcx_imdct( st, L_frame_glob, L_frameTCX_glob, L_spec, tcx_offset, tcx_offsetFB, L_frame, L_frameTCX, left_rect, &x[0], &xn_buf[0], MDCT_IV,
111 : fUseTns, &synth[0], &synthFB[0], bfi, frame_cnt, sba_dirac_stereo_flag );
112 :
113 6397143 : return;
114 : }
115 :
116 :
117 : /*-------------------------------------------------------------------*
118 : * decoder_tcx_post()
119 : *
120 : *
121 : *-------------------------------------------------------------------*/
122 :
123 31487879 : void decoder_tcx_post(
124 : Decoder_State *st, /* i/o: decoder memory state */
125 : float *synth, /* i/o: synthesis */
126 : float *synthFB, /* i/o: FB synthesis */
127 : float *A, /* i : A(z) filter coefficients */
128 : const int16_t bfi, /* i : Bad frame indicator */
129 : const int16_t MCT_flag /* i : hMCT handle allocated (1) or not (0) */
130 : )
131 : {
132 : int16_t i;
133 31487879 : float level_syn, gainCNG = 0.0f, step;
134 : float xn_buf[L_FRAME_MAX];
135 31487879 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
136 :
137 : /* TCX output */
138 31487879 : mvr2r( synth, xn_buf, st->L_frame );
139 :
140 : /* first TCX frame after ACELP; overwrite ltp initialization done during acelp PLC */
141 31487879 : if ( !bfi && st->prev_bfi && !st->last_core )
142 : {
143 6062 : hTcxDec->tcxltp_last_gain_unmodified = 0.0f;
144 : }
145 :
146 31487879 : if ( st->hTcxLtpDec != NULL && st->element_mode > EVS_MONO && ( st->clas_dec == UNVOICED_CLAS || st->clas_dec == INACTIVE_CLAS ) )
147 : {
148 : /* deactivate TCX LTP for non-voiced frames */
149 15891080 : st->hTcxLtpDec->tcxltp = 0;
150 15891080 : st->hTcxLtpDec->tcxltp_gain = 0.0f;
151 : }
152 :
153 31487879 : if ( bfi && !st->use_partial_copy )
154 : {
155 : /* run lpc gain compensation not for waveform adjustment */
156 1756370 : if ( !st->enablePlcWaveadjust || ( st->hPlcInfo != NULL && st->hPlcInfo->concealment_method == TCX_TONAL ) )
157 : {
158 1756294 : float gainHelperFB = hTcxDec->gainHelper;
159 1756294 : float stepCompensateFB = hTcxDec->stepCompensate * st->L_frame / hTcxDec->L_frameTCX;
160 :
161 1144912294 : for ( i = 0; i < hTcxDec->L_frameTCX; i++ )
162 : {
163 1143156000 : synthFB[i] *= gainHelperFB;
164 1143156000 : gainHelperFB -= stepCompensateFB;
165 : }
166 : }
167 :
168 820659154 : for ( i = 0; i < st->L_frame; i++ )
169 : {
170 818902784 : xn_buf[i] *= hTcxDec->gainHelper;
171 818902784 : hTcxDec->gainHelper -= hTcxDec->stepCompensate;
172 : }
173 : }
174 :
175 : /* PLC: [TCX: Fade-out]
176 : * PLC: estimate and update CNG energy */
177 31487879 : level_syn = (float) sqrt( ( dotp( synthFB, synthFB, hTcxDec->L_frameTCX ) ) / hTcxDec->L_frameTCX );
178 :
179 : /* PLC: [TCX: Fade-out]
180 : * PLC: update or retrieve the background level */
181 31487879 : if ( bfi == 0 && st->tcxonly && ( st->element_mode != IVAS_CPE_MDCT || MCT_flag ) && st->clas_dec == UNVOICED_CLAS )
182 : {
183 10920091 : minimumStatistics( hTcxDec->NoiseLevelMemory_bfi, &hTcxDec->NoiseLevelIndex_bfi, &hTcxDec->CurrLevelIndex_bfi, &hTcxDec->CngLevelBackgroundTrace_bfi, &hTcxDec->LastFrameLevel_bfi, level_syn, PLC_MIN_CNG_LEV, PLC_MIN_STAT_BUFF_SIZE );
184 : }
185 :
186 : /* PLC: [TCX: Fade-out]
187 : * PLC: fade-out in time domain */
188 31487879 : if ( bfi )
189 : {
190 : float conceal_eof_gainFB;
191 :
192 1756370 : if ( st->tcxonly )
193 : {
194 1512398 : gainCNG = hTcxDec->CngLevelBackgroundTrace_bfi / ( level_syn + 0.01f );
195 :
196 1512398 : if ( st->element_mode == IVAS_CPE_MDCT && !MCT_flag )
197 : {
198 412684 : if ( st->nbLostCmpt > MDCT_ST_PLC_FADEOUT_MAX_CONC_FRAME + MDCT_ST_PLC_FADEOUT_TO_ZERO_LEN )
199 : {
200 0 : gainCNG = 0.f;
201 : }
202 412684 : else if ( st->nbLostCmpt > MDCT_ST_PLC_FADEOUT_MAX_CONC_FRAME )
203 : {
204 0 : gainCNG *= 1.f - (float) ( st->nbLostCmpt - MDCT_ST_PLC_FADEOUT_MAX_CONC_FRAME ) / MDCT_ST_PLC_FADEOUT_TO_ZERO_LEN;
205 : }
206 : }
207 : }
208 : else
209 : {
210 243972 : gainCNG = st->cngTDLevel / ( level_syn + 0.01f );
211 : }
212 1756370 : if ( st->nbLostCmpt == 1 )
213 : {
214 647622 : hTcxDec->conceal_eof_gain = 1.0f;
215 : }
216 :
217 1756370 : step = ( hTcxDec->conceal_eof_gain - ( hTcxDec->conceal_eof_gain * hTcxDec->damping + gainCNG * ( 1 - hTcxDec->damping ) ) ) / st->L_frame;
218 : {
219 1756370 : float stepFB = step * st->L_frame / hTcxDec->L_frameTCX;
220 1756370 : conceal_eof_gainFB = hTcxDec->conceal_eof_gain;
221 :
222 1144955890 : for ( i = 0; i < hTcxDec->L_frameTCX; i++ )
223 : {
224 1143199520 : synthFB[i] *= conceal_eof_gainFB;
225 1143199520 : conceal_eof_gainFB -= stepFB;
226 : }
227 : }
228 :
229 820659154 : for ( i = 0; i < st->L_frame; i++ )
230 : {
231 818902784 : xn_buf[i] *= hTcxDec->conceal_eof_gain;
232 818902784 : hTcxDec->conceal_eof_gain -= step;
233 : }
234 :
235 : /* run lpc gain compensation not for waveform adjustment */
236 1756370 : if ( st->hPlcInfo != NULL )
237 : {
238 196 : if ( ( !st->enablePlcWaveadjust || st->hPlcInfo->concealment_method == TCX_TONAL ) && !st->use_partial_copy )
239 : {
240 120 : st->hPlcInfo->recovery_gain = conceal_eof_gainFB * st->last_concealed_gain_syn_deemph;
241 : }
242 : else
243 : {
244 76 : st->hPlcInfo->recovery_gain = conceal_eof_gainFB;
245 : }
246 196 : st->hPlcInfo->step_concealgain = step * st->L_frame / hTcxDec->L_frameTCX;
247 : }
248 : }
249 :
250 :
251 : /*-----------------------------------------------------------*
252 : * Memory update *
253 : *-----------------------------------------------------------*/
254 :
255 : /* Update synth, exc and old_Aq */
256 31487879 : tcx_decoder_memory_update( st, xn_buf, synth, A );
257 :
258 : #ifdef DEBUG_MODE_TCX
259 : {
260 : int16_t tmp[320];
261 : for ( i = 0; i < L_EXC_MEM; i++ )
262 : {
263 : tmp[i] = (int16_t) ( st->old_exc[L_EXC_MEM_DEC - L_EXC_MEM + i] + 0.5f );
264 : }
265 : dbgwrite( tmp, 2, L_EXC_MEM, 1, "res/exc_old.dec.pcm" );
266 : }
267 : #endif
268 :
269 : /* PLC: [TCX: Memory update] */
270 31487879 : st->old_pitch_buf[0] = st->old_pitch_buf[st->nb_subfr];
271 31487879 : st->old_pitch_buf[1] = st->old_pitch_buf[st->nb_subfr + 1];
272 31487879 : mvr2r( &st->old_pitch_buf[st->nb_subfr + 2], &st->old_pitch_buf[2], st->nb_subfr );
273 31487879 : set_f( &st->old_pitch_buf[st->nb_subfr + 2], st->old_fpitch, st->nb_subfr );
274 31487879 : st->bfi_pitch = st->old_fpitch;
275 31487879 : st->bfi_pitch_frame = st->L_frame;
276 :
277 31487879 : st->mem_pitch_gain[2 * st->nb_subfr + 1] = st->mem_pitch_gain[st->nb_subfr + 1];
278 31487879 : st->mem_pitch_gain[2 * st->nb_subfr] = st->mem_pitch_gain[st->nb_subfr];
279 :
280 178338842 : for ( i = 0; i < st->nb_subfr; i++ )
281 : {
282 146850963 : st->mem_pitch_gain[2 * st->nb_subfr - 1 - i] = st->mem_pitch_gain[st->nb_subfr - 1 - i];
283 146850963 : st->mem_pitch_gain[st->nb_subfr - 1 - i] = hTcxDec->tcxltp_last_gain_unmodified;
284 : }
285 :
286 31487879 : return;
287 : }
288 :
289 :
290 : /*-------------------------------------------------------------------*
291 : * IMDCT()
292 : *
293 : *
294 : *-------------------------------------------------------------------*/
295 :
296 59131716 : void IMDCT(
297 : float *x,
298 : float *old_syn_overl,
299 : float *syn_Overl_TDAC,
300 : float *xn_buf,
301 : const float *tcx_aldo_window_1_trunc,
302 : const float *tcx_aldo_window_2,
303 : const float *tcx_mdct_window_half,
304 : const float *tcx_mdct_window_minimum,
305 : const float *tcx_mdct_window_trans,
306 : const int16_t tcx_mdct_window_half_length,
307 : const int16_t tcx_mdct_window_min_length,
308 : int16_t index,
309 : const uint16_t kernel_type, /* i : TCX transform kernel type */
310 : const int16_t left_rect,
311 : const int16_t tcx_offset,
312 : const int16_t overlap,
313 : const int16_t L_frame,
314 : const int16_t L_frameTCX,
315 : const int16_t L_spec_TCX5,
316 : const int16_t L_frame_glob,
317 : const int16_t frame_cnt,
318 : const int16_t bfi,
319 : float *old_out,
320 : const int16_t FB_flag,
321 : Decoder_State *st,
322 : const int16_t fullbandScale,
323 : float *acelp_zir )
324 : {
325 : int16_t i, nz, aldo, w, L_win, L_ola;
326 : float win[( L_FRAME_PLUS + L_MDCT_OVLP_MAX ) / 2];
327 59131716 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
328 59131716 : TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
329 :
330 59131716 : aldo = 0;
331 :
332 : /* number of zero for ALDO windows*/
333 59131716 : nz = NS2SA( st->output_Fs, N_ZERO_MDCT_NS ) * L_frame / L_frameTCX;
334 :
335 59131716 : if ( st->element_mode != EVS_MONO && frame_cnt == 0 && !bfi && st->prev_bfi && ( st->last_core_bfi == TCX_20_CORE || st->last_core_bfi == TCX_10_CORE ) && !hTcxCfg->last_aldo )
336 : {
337 9784 : v_multc( old_syn_overl, hTcxDec->conceal_eof_gain * st->last_concealed_gain_syn_deemph, old_syn_overl, overlap );
338 : }
339 :
340 59131716 : if ( ( L_frameTCX == hTcxDec->L_frameTCX >> 1 ) && st->tcxonly )
341 : {
342 : /* Mode decision in PLC
343 :
344 : last OL curr OL left TCX-10 right TCX-10
345 : -------------------------------------------------------------
346 : 0 0 2x TCX-5* 1x TCX-10
347 : 0 2 1x TCX-10 1x TCX-10
348 : 0 3 1x TCX-10 1x TCX-10
349 : 2 0 2x TCX-5 1x TCX-10
350 : 2 2 2x TCX-5 2x TCX-5
351 : 2 3 2x TCX-5 2x TCX-5
352 : 3 0 2x TCX-5 1x TCX-10
353 : 3 2 2x TCX-5 2x TCX-5
354 : 3 3 2x TCX-5 2x TCX-5
355 : */
356 :
357 2539696 : if ( ( !bfi && hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) || ( bfi && ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) && ( hTcxCfg->tcx_curr_overlap_mode != FULL_OVERLAP ) ) )
358 : {
359 : /* minimum or half overlap, two transforms, grouping into one window */
360 639792 : L_win = L_frame >> 1;
361 639792 : L_ola = ( hTcxCfg->tcx_last_overlap_mode == MIN_OVERLAP ) ? tcx_mdct_window_min_length : tcx_mdct_window_half_length;
362 :
363 639792 : set_f( win, 0, ( L_FRAME_PLUS + L_MDCT_OVLP_MAX ) / 2 );
364 639792 : set_zero( xn_buf, tcx_offset + ( L_ola >> 1 ) ); /* zero left end of buffer */
365 :
366 1919376 : for ( w = 0; w < 2; w++ )
367 : {
368 1279584 : if ( kernel_type == MDST_IV || ( kernel_type & w ) )
369 : {
370 174 : TCX_MDST_Inverse( x + w * L_spec_TCX5, win, L_ola, L_win - L_ola, L_ola, st->element_mode );
371 : }
372 1279410 : else if ( kernel_type != 0 && w == 0 ) /* type 1 or 2 */
373 : {
374 224 : TCX_MDXT_Inverse( x + w * L_spec_TCX5, win, L_ola, L_win - L_ola, L_ola, kernel_type );
375 : }
376 : else
377 : {
378 1279186 : TCX_MDCT_Inverse( x + w * L_spec_TCX5, win, L_ola, L_win - L_ola, L_ola, st->element_mode );
379 : }
380 :
381 1279584 : tcx_windowing_synthesis_current_frame( win, tcx_aldo_window_2, tcx_mdct_window_half, tcx_mdct_window_minimum, L_ola, tcx_mdct_window_half_length, tcx_mdct_window_min_length, ( w > 0 ) ? 0 : left_rect, ( w > 0 ) || ( w == 0 && index == 2 ) ? MIN_OVERLAP : hTcxCfg->tcx_last_overlap_mode, acelp_zir, hTcxDec->old_syn_Overl, syn_Overl_TDAC, st->old_Aq_12_8, tcx_mdct_window_trans, L_win, tcx_offset < 0 ? -tcx_offset : 0, ( w > 0 ) || ( frame_cnt > 0 ) ? 1 : st->last_core, ( w > 0 ) || ( frame_cnt > 0 ) ? 0 : st->last_is_cng, fullbandScale );
382 :
383 1279584 : if ( w > 0 )
384 : {
385 639792 : tcx_windowing_synthesis_past_frame( xn_buf + tcx_offset - ( L_ola >> 1 ) + w * L_win, tcx_aldo_window_1_trunc, tcx_mdct_window_half, tcx_mdct_window_minimum, L_ola, tcx_mdct_window_half_length, tcx_mdct_window_min_length, MIN_OVERLAP );
386 : }
387 :
388 : /* add part of current sub-window overlapping with previous window */
389 1279584 : v_add( win, xn_buf + tcx_offset - ( L_ola >> 1 ) + w * L_win, xn_buf + tcx_offset - ( L_ola >> 1 ) + w * L_win, L_ola );
390 :
391 : /* copy new sub-window region not overlapping with previous window */
392 1279584 : mvr2r( win + L_ola, xn_buf + tcx_offset + ( L_ola >> 1 ) + w * L_win, L_win );
393 : }
394 :
395 : /* To assure that no garbage values are passed to overlap */
396 639792 : set_zero( xn_buf + L_frame + tcx_offset + ( L_ola >> 1 ), overlap - tcx_offset - ( L_ola >> 1 ) );
397 :
398 639792 : if ( st->prev_bfi && frame_cnt == 0 && st->last_core != st->last_core_bfi && st->last_core_bfi == ACELP_CORE )
399 : {
400 66 : tcx_windowing_synthesis_past_frame( old_syn_overl, tcx_aldo_window_1_trunc, tcx_mdct_window_half, tcx_mdct_window_minimum, overlap, tcx_mdct_window_half_length, tcx_mdct_window_min_length, hTcxCfg->tcx_last_overlap_mode );
401 66 : v_add( xn_buf, old_syn_overl, xn_buf, overlap );
402 : }
403 : }
404 1899904 : else if ( !bfi && ( frame_cnt == 0 ) && ( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) )
405 :
406 : {
407 : /* special overlap attempt, two transforms, grouping into one window */
408 665102 : L_win = L_frame >> 1;
409 665102 : L_ola = tcx_mdct_window_min_length;
410 :
411 665102 : set_f( win, 0, ( L_FRAME_PLUS + L_MDCT_OVLP_MAX ) / 2 );
412 :
413 : /* 1st TCX-5 window, special MDCT with minimum overlap on right side */
414 665102 : if ( kernel_type == MDST_IV )
415 : {
416 0 : TCX_MDST_Inverse( x, win + L_win, 0, L_win - ( L_ola >> 1 ), L_ola, st->element_mode );
417 : }
418 665102 : else if ( kernel_type != MDCT_IV ) /* type 1 or 2 */
419 : {
420 40 : TCX_MDXT_Inverse( x, win + L_win, 0, L_win - ( L_ola >> 1 ), L_ola, kernel_type );
421 : }
422 : else
423 : {
424 665062 : TCX_MDCT_Inverse( x, win + L_win, 0, L_win - ( L_ola >> 1 ), L_ola, st->element_mode );
425 : }
426 :
427 665102 : set_zero( xn_buf, ( overlap >> 1 ) );
428 :
429 : /* copy new sub-window region not overlapping with previous window */
430 665102 : mvr2r( win + L_win, xn_buf + ( overlap >> 1 ), L_win + ( L_ola >> 1 ) );
431 :
432 : /* 2nd TCX-5 window, regular MDCT with minimum overlap on both sides */
433 665102 : if ( kernel_type & 1 )
434 : {
435 36 : TCX_MDST_Inverse( x + L_spec_TCX5, win, L_ola, L_win - L_ola, L_ola, st->element_mode );
436 : }
437 : else
438 : {
439 665066 : TCX_MDCT_Inverse( x + L_spec_TCX5, win, L_ola, L_win - L_ola, L_ola, st->element_mode );
440 : }
441 :
442 665102 : tcx_windowing_synthesis_current_frame( win, tcx_aldo_window_2, tcx_mdct_window_half, tcx_mdct_window_minimum, L_ola, tcx_mdct_window_half_length, tcx_mdct_window_min_length, 0,
443 665102 : /* left_rect */ MIN_OVERLAP, /* left_mode */ acelp_zir, hTcxDec->old_syn_Overl, syn_Overl_TDAC, st->old_Aq_12_8, tcx_mdct_window_trans, L_win, tcx_offset < 0 ? -tcx_offset : 0, 1, /* st->last_mode_bfi */ 0, /* st->last_is_cng */ fullbandScale );
444 :
445 665102 : tcx_windowing_synthesis_past_frame( xn_buf + ( overlap >> 1 ) + L_win - ( L_ola >> 1 ), tcx_aldo_window_1_trunc, tcx_mdct_window_half, tcx_mdct_window_minimum, L_ola, tcx_mdct_window_half_length, tcx_mdct_window_min_length, 2 );
446 :
447 : /* add part of current sub-window overlapping with previous window */
448 665102 : v_add( win, xn_buf + ( overlap >> 1 ) + L_win - ( L_ola >> 1 ), xn_buf + ( overlap >> 1 ) + L_win - ( L_ola >> 1 ), L_ola );
449 :
450 : /* copy new sub-window region not overlapping with previous window */
451 665102 : mvr2r( win + L_ola, xn_buf + ( overlap >> 1 ) + L_win + ( L_ola >> 1 ), L_win );
452 :
453 : /* extra folding-out on left side of win, for perfect reconstruction */
454 665102 : if ( kernel_type >= MDCT_II )
455 : {
456 648 : for ( w = ( overlap >> 1 ); w < overlap; w++ )
457 : {
458 644 : xn_buf[overlap - 1 - w] = xn_buf[w];
459 : }
460 : }
461 : else
462 : {
463 90408808 : for ( w = ( overlap >> 1 ); w < overlap; w++ )
464 : {
465 89743710 : xn_buf[overlap - 1 - w] = -1.0f * xn_buf[w];
466 : }
467 : }
468 665102 : tcx_windowing_synthesis_current_frame( xn_buf, tcx_aldo_window_2, tcx_mdct_window_half, tcx_mdct_window_minimum, overlap, tcx_mdct_window_half_length, tcx_mdct_window_min_length, left_rect, 0, /* left_mode */ acelp_zir, hTcxDec->old_syn_Overl, syn_Overl_TDAC, st->old_Aq_12_8, tcx_mdct_window_trans, 2 * L_win, tcx_offset < 0 ? -tcx_offset : 0, st->last_core_bfi, st->last_is_cng, fullbandScale );
469 : }
470 : else
471 : {
472 : /* default, i.e. maximum overlap, single transform, no grouping */
473 1234802 : if ( kernel_type == MDST_IV )
474 : {
475 42 : TCX_MDST_Inverse( x, xn_buf, overlap, L_frame - overlap, overlap, st->element_mode );
476 : }
477 1234760 : else if ( kernel_type != MDCT_IV ) /* type 1 or 2 */
478 : {
479 636 : TCX_MDXT_Inverse( x, xn_buf, overlap, L_frame - overlap, overlap, kernel_type );
480 : }
481 : else
482 : {
483 1234124 : TCX_MDCT_Inverse( x, xn_buf, overlap, L_frame - overlap, overlap, st->element_mode );
484 : }
485 :
486 1234802 : tcx_windowing_synthesis_current_frame( xn_buf, tcx_aldo_window_2, tcx_mdct_window_half, tcx_mdct_window_minimum, overlap, tcx_mdct_window_half_length, tcx_mdct_window_min_length, left_rect, !bfi && ( frame_cnt > 0 ) && ( index == 0 ) && ( st->last_core != ACELP_CORE ) ? MIN_OVERLAP : index, acelp_zir, hTcxDec->old_syn_Overl, syn_Overl_TDAC, st->old_Aq_12_8, tcx_mdct_window_trans, L_frame_glob >> 1, tcx_offset < 0 ? -tcx_offset : 0, ( frame_cnt > 0 /*|| (st->last_con_tcx )*/ ) ? 1 : st->last_core_bfi, ( frame_cnt > 0 ) ? 0 : st->last_is_cng, fullbandScale );
487 :
488 : } /* tcx_last_overlap_mode != FULL_OVERLAP */
489 : }
490 : else
491 : {
492 : /* frame is TCX-20 or not TCX-only */
493 56592020 : assert( frame_cnt == 0 );
494 56592020 : if ( hTcxCfg->tcx_last_overlap_mode != TRANSITION_OVERLAP )
495 : {
496 : float tmp[L_FRAME_PLUS];
497 56123652 : if ( kernel_type != MDCT_IV ) /* inverse transform */
498 : {
499 15546 : if ( kernel_type == MDST_IV )
500 : {
501 3774 : edst( x, xn_buf + overlap / 2 + nz, L_frame, st->element_mode );
502 : }
503 : else /* type 1 or 2 */
504 : {
505 11772 : edxt( x, xn_buf + overlap / 2 + nz, L_frame, kernel_type, TRUE );
506 : }
507 15546 : v_multc( xn_buf + overlap / 2 + nz, (float) sqrt( (float) L_frame / NORM_MDCT_FACTOR ), tmp, L_frame );
508 15546 : window_ola_ext( tmp, xn_buf, old_out, L_frame, hTcxCfg->tcx_last_overlap_mode, hTcxCfg->tcx_curr_overlap_mode, kernel_type );
509 : }
510 : else
511 : {
512 56108106 : edct( x, xn_buf + overlap / 2 + nz, L_frame, st->element_mode );
513 :
514 56108106 : v_multc( xn_buf + overlap / 2 + nz, (float) sqrt( (float) L_frame / NORM_MDCT_FACTOR ), tmp, L_frame );
515 56108106 : window_ola( tmp, xn_buf, old_out, L_frame, hTcxCfg->tcx_last_overlap_mode, hTcxCfg->tcx_curr_overlap_mode, 0, 0, NULL );
516 : }
517 56123652 : aldo = 1;
518 : }
519 : else
520 : {
521 468368 : int16_t acelp_mem_len = tcx_offset < 0 ? -tcx_offset : 0;
522 :
523 468368 : if ( kernel_type == MDST_IV )
524 : {
525 0 : TCX_MDST_Inverse( x, xn_buf, overlap, L_frame - overlap, overlap, st->element_mode );
526 : }
527 468368 : else if ( kernel_type != MDCT_IV ) /* type 1 or 2 */
528 : {
529 0 : TCX_MDXT_Inverse( x, xn_buf, overlap, L_frame - overlap, overlap, kernel_type );
530 : }
531 : else
532 : {
533 468368 : TCX_MDCT_Inverse( x, xn_buf, overlap, L_frame - overlap, overlap, st->element_mode );
534 : }
535 :
536 : /*-----------------------------------------------------------*
537 : * Windowing, overlap and add *
538 : *-----------------------------------------------------------*/
539 :
540 468368 : if ( st->element_mode == IVAS_CPE_MDCT && ( st->last_core_brate <= SID_2k40 || st->last_core == ACELP_CORE ) && !fullbandScale )
541 : {
542 : float buf[L_FRAME_MAX / 4];
543 : float window_buf[L_FRAME_MAX / 4];
544 : float r[M + 1];
545 24612 : const int16_t analysis_len = L_frame_glob / 4;
546 :
547 : /* get the first 5 ms of non-aliased TCX syntesis */
548 24612 : mvr2r( xn_buf + overlap / 2 + 2 * acelp_mem_len, &buf[0], analysis_len );
549 :
550 : /* get LPC from that signal part to use for acelp zir smoothing */
551 24612 : ham_cos_window( &window_buf[0], analysis_len / 2, analysis_len / 2 );
552 24612 : autocorr( &buf[0], &r[0], M, analysis_len, &window_buf[0], 0, 0, 0 );
553 24612 : lag_wind( r, M, L_frame_glob * FRAMES_PER_SEC, LAGW_STRONG );
554 24612 : lev_dur( &st->old_Aq_12_8[0], &r[0], M, NULL );
555 : }
556 :
557 : /* Window current frame */
558 468368 : tcx_windowing_synthesis_current_frame( xn_buf, tcx_aldo_window_2, tcx_mdct_window_half, tcx_mdct_window_minimum, overlap, tcx_mdct_window_half_length, tcx_mdct_window_min_length, left_rect, hTcxCfg->tcx_last_overlap_mode, acelp_zir, hTcxDec->old_syn_Overl, syn_Overl_TDAC, st->old_Aq_12_8, tcx_mdct_window_trans, L_frame_glob >> 1, acelp_mem_len, st->last_core_bfi, st->last_is_cng, fullbandScale );
559 : }
560 : } /* TCX-20 and TCX-only */
561 :
562 : /* Window and overlap-add past frame if past frame is TCX */
563 59131716 : if ( ( frame_cnt != 0 ) || ( st->last_core_bfi > ACELP_CORE ) )
564 : {
565 57526284 : if ( ( ( L_frameTCX == hTcxDec->L_frameTCX >> 1 ) && ( st->tcxonly ) ) ||
566 55481624 : ( hTcxCfg->tcx_last_overlap_mode == TRANSITION_OVERLAP ) )
567 : {
568 2084770 : if ( !bfi && ( frame_cnt > 0 ) && ( index == 0 ) && ( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) && ( st->last_core != ACELP_CORE ) )
569 : {
570 665102 : index = MIN_OVERLAP; /* use minimum overlap between the two TCX-10 windows */
571 : }
572 :
573 2084770 : if ( hTcxCfg->last_aldo )
574 : {
575 179052478 : for ( i = 0; i < overlap - tcx_mdct_window_min_length; i++ )
576 : {
577 178284144 : xn_buf[i + overlap / 2 - tcx_offset] += old_out[i + nz];
578 : }
579 :
580 : /* fade truncated ALDO window */
581 768334 : if ( L_frameTCX == hTcxDec->L_frameTCX >> 1 && st->element_mode == IVAS_CPE_MDCT && frame_cnt == 0 && hTcxCfg->tcx_last_overlap_mode == FULL_OVERLAP )
582 : {
583 22476008 : for ( ; i < overlap; i++ ) /* perfectly reconstructing ALDO shortening */
584 : {
585 21912340 : xn_buf[i + overlap / 2 - tcx_offset] += old_out[i + nz];
586 : }
587 22476008 : for ( i = 0; i < tcx_mdct_window_min_length; i++ )
588 : {
589 21912340 : xn_buf[i + overlap / 2 - tcx_offset + overlap] += old_out[i + nz + overlap] * tcx_mdct_window_minimum[tcx_mdct_window_min_length - 1 - i];
590 : }
591 : }
592 : else
593 : {
594 8006350 : for ( ; i < overlap; i++ )
595 : {
596 7801684 : xn_buf[i + overlap / 2 - tcx_offset] += old_out[i + nz] * tcx_mdct_window_minimum[overlap - 1 - i];
597 : }
598 : }
599 : }
600 : else
601 : {
602 1316436 : tcx_windowing_synthesis_past_frame( old_syn_overl, tcx_aldo_window_1_trunc, tcx_mdct_window_half, tcx_mdct_window_minimum, overlap, tcx_mdct_window_half_length, tcx_mdct_window_min_length, ( index == 0 || hTcxCfg->tcx_last_overlap_mode == MIN_OVERLAP ) ? hTcxCfg->tcx_last_overlap_mode : index );
603 :
604 1316436 : if ( bfi )
605 : {
606 7188980 : for ( i = 0; i < tcx_mdct_window_half_length; i++ )
607 : {
608 7123680 : xn_buf[i + overlap / 2 - tcx_offset] += old_syn_overl[i] * tcx_mdct_window_half[tcx_mdct_window_half_length - 1 - i];
609 : }
610 : }
611 1251136 : else if ( !left_rect )
612 : {
613 338619032 : for ( i = 0; i < overlap; i++ )
614 : {
615 337367968 : xn_buf[i] += old_syn_overl[i];
616 : }
617 : }
618 : else
619 : {
620 15192 : for ( i = 0; i < overlap; i++ )
621 : {
622 15120 : xn_buf[i + ( overlap >> 1 )] += old_syn_overl[i];
623 : }
624 : }
625 : }
626 : }
627 : }
628 :
629 59131716 : if ( !aldo && ( ( ( L_frameTCX == hTcxDec->L_frameTCX >> 1 ) && frame_cnt > 0 ) || L_frameTCX != ( hTcxDec->L_frameTCX >> 1 ) ) )
630 : {
631 : /* Compute windowed synthesis in case of switching to ALDO windows in next frame */
632 1738216 : mvr2r( xn_buf + L_frame - nz, old_out, nz + overlap );
633 1738216 : set_zero( old_out + nz + overlap, nz );
634 :
635 1738216 : tcx_windowing_synthesis_past_frame( old_out + nz, tcx_aldo_window_1_trunc, tcx_mdct_window_half, tcx_mdct_window_minimum, overlap, tcx_mdct_window_half_length, tcx_mdct_window_min_length, hTcxCfg->tcx_curr_overlap_mode );
636 :
637 : /* If current overlap mode = FULL_OVERLAP -> ALDO_WINDOW */
638 1738216 : if ( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP )
639 : {
640 1269048 : if ( kernel_type & 1 )
641 : {
642 83368 : for ( i = 0; i < nz; i++ )
643 : {
644 82908 : old_out[nz + overlap + i] = -1.0f * xn_buf[L_frame - 1 - i] * tcx_aldo_window_1_trunc[-1 - i];
645 : }
646 : }
647 : else
648 : {
649 212653001 : for ( i = 0; i < nz; i++ )
650 : {
651 211384413 : old_out[nz + overlap + i] = xn_buf[L_frame - 1 - i] * tcx_aldo_window_1_trunc[-1 - i];
652 : }
653 : }
654 1269048 : aldo = 1;
655 : }
656 : }
657 :
658 59131716 : if ( FB_flag )
659 : {
660 29565858 : hTcxCfg->last_aldo = aldo;
661 : }
662 :
663 : /* Smoothing between the ACELP PLC and TCX Transition frame. Using the shape of the half overlap window for the crossfading. */
664 59131716 : if ( left_rect && ( frame_cnt == 0 ) && ( st->last_core_bfi == ACELP_CORE ) && st->prev_bfi )
665 : {
666 9606 : if ( FB_flag )
667 : {
668 629163 : for ( i = 0; i < tcx_mdct_window_half_length; i++ )
669 : {
670 624360 : xn_buf[i + overlap / 2 - tcx_offset] *= tcx_mdct_window_half[i];
671 624360 : xn_buf[i + overlap / 2 - tcx_offset] += hTcxDec->syn_OverlFB[i] * tcx_mdct_window_half[tcx_mdct_window_half_length - 1 - i] * tcx_mdct_window_half[tcx_mdct_window_half_length - 1 - i];
672 : }
673 : }
674 : else
675 : {
676 329067 : for ( i = 0; i < tcx_mdct_window_half_length; i++ )
677 : {
678 324264 : xn_buf[i + overlap / 2 - tcx_offset] *= tcx_mdct_window_half[i];
679 324264 : xn_buf[i + overlap / 2 - tcx_offset] += hTcxDec->syn_Overl[i] * tcx_mdct_window_half[tcx_mdct_window_half_length - 1 - i] * tcx_mdct_window_half[tcx_mdct_window_half_length - 1 - i];
680 : }
681 : }
682 : }
683 :
684 59131716 : return;
685 : }
686 :
687 :
688 : /*-------------------------------------------------------------------*
689 : * decoder_tcx_invQ
690 : *
691 : * TCX: inverse quantization
692 : *-------------------------------------------------------------------*/
693 :
694 29620013 : void decoder_tcx_invQ(
695 : Decoder_State *st, /* i/o: coder memory state */
696 : int16_t prm[], /* i : parameters */
697 : float A[], /* i : coefficients NxAz[M+1] */
698 : Word16 Aind[], /* i : frame-independent coefficients Az[M+1] */
699 : const int16_t L_spec,
700 : const int16_t L_frame,
701 : const int16_t L_frameTCX,
702 : float x[],
703 : float gainlpc2[],
704 : float xn_buf[],
705 : int16_t *fUseTns, /* o : flag that is set if TNS data is present */
706 : STnsData *tnsData,
707 : float *gain_tcx,
708 : const int16_t **prm_sqQ1,
709 : int16_t *nf_seed,
710 : const int16_t bfi, /* i : Bad frame indicator */
711 : const int16_t frame_cnt /* i : frame counter in the super frame */
712 : )
713 : {
714 : int16_t i, index;
715 : int16_t start_zeroing;
716 : float Ap[M + 2];
717 :
718 : int16_t noiseFillingSize;
719 : int16_t tnsSize; /* number of tns parameters put into prm */
720 :
721 : float gamma1;
722 : float gamma;
723 29620013 : float gainCompensate = 1.f;
724 : float h1[L_FRAME_MAX / 4 + 1];
725 : float mem[M];
726 : float tmp2;
727 : int16_t arith_bits, signaling_bits;
728 : const int16_t *prm_ltp, *prm_tns, *prm_hm, *prm_sqQ, *prm_target;
729 29620013 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
730 29620013 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
731 29620013 : TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
732 :
733 29620013 : tnsSize = 0;
734 :
735 : /* just to suppress MSVC warnigs */
736 29620013 : prm_target = NULL;
737 29620013 : prm_ltp = NULL;
738 29620013 : prm_tns = NULL;
739 29620013 : prm_sqQ = NULL;
740 :
741 : /*-----------------------------------------------------------------*
742 : * Initializations
743 : *-----------------------------------------------------------------*/
744 :
745 : /* Init lengths */
746 29620013 : gamma1 = st->gamma;
747 :
748 29620013 : if ( hTcxDec->enableTcxLpc )
749 : {
750 381376 : gamma1 = 1.0f;
751 : }
752 :
753 29620013 : noiseFillingSize = L_spec;
754 29620013 : if ( st->igf )
755 : {
756 15826883 : noiseFillingSize = st->hIGFDec->infoIGFStartLine;
757 : }
758 :
759 : /*-----------------------------------------------------------*
760 : * Read TCX parameters *
761 : *-----------------------------------------------------------*/
762 :
763 29620013 : index = 0;
764 :
765 29620013 : if ( !bfi )
766 : {
767 28032295 : index = prm[0];
768 28032295 : prm_ltp = &prm[1 + NOISE_FILL_RANGES];
769 28032295 : prm_tns = prm_ltp + LTPSIZE;
770 :
771 : /* read noise level (fac_ns) */
772 28032295 : st->hTcxDec->noise_filling_index[frame_cnt] = prm[1];
773 : }
774 :
775 : /* read TNS data */
776 29620013 : if ( !bfi && hTcxCfg->fIsTNSAllowed )
777 : {
778 25895515 : *fUseTns = DecodeTnsData( hTcxCfg->pCurrentTnsConfig, prm_tns, &tnsSize, tnsData );
779 : }
780 : else
781 : {
782 3724498 : *fUseTns = 0;
783 : }
784 :
785 : /*-----------------------------------------------------------*
786 : * Spectrum data *
787 : *-----------------------------------------------------------*/
788 :
789 29620013 : if ( !bfi )
790 : {
791 28032295 : prm_hm = prm_tns + tnsSize;
792 28032295 : prm_sqQ = prm_hm + NPRM_CTX_HM;
793 28032295 : *prm_sqQ1 = prm_sqQ;
794 :
795 : /*-----------------------------------------------------------*
796 : * Context HM *
797 : *-----------------------------------------------------------*/
798 :
799 28032295 : if ( hTcxCfg->ctx_hm && ( ( st->last_core_from_bs != ACELP_CORE ) || ( frame_cnt > 0 ) ) )
800 : {
801 4458583 : st->last_ctx_hm_enabled = prm_hm[0];
802 :
803 3219755863 : for ( i = 0; i < L_spec; i++ )
804 : {
805 : /* no context harmonic model, copy MDCT coefficients to x */
806 3215297280 : x[i] = (float) prm_sqQ[i];
807 : }
808 : }
809 : else /* hTcxCfg->ctx_hm == 0 */
810 : {
811 23573712 : if ( hTcxDec->tcx_lpc_shaped_ari ) /* low rates: envelope based arithmetic coder */
812 : {
813 371149 : prm_target = prm_sqQ;
814 371149 : prm_sqQ = prm_target + 1;
815 :
816 371149 : tcx_arith_decode_envelope( st, x, L_frame, L_spec, Aind, *prm_target, prm_sqQ, st->last_core_from_bs != ACELP_CORE, prm_hm, /* HM parameter area */ hTcxDec->tcx_hm_LtpPitchLag, &arith_bits, &signaling_bits, ( st->bwidth > WB ) ? 1 : 0 );
817 :
818 371149 : hTcxDec->resQBits[frame_cnt] = *prm_target - arith_bits;
819 :
820 : /* Noise filling seed */
821 76136087 : for ( i = 0; i < noiseFillingSize; ++i )
822 : {
823 75764938 : *nf_seed += (int16_t) ( abs( (int16_t) x[i] ) * i * 2 );
824 : }
825 : }
826 : else /* TCX-only: context based arithmetic coder */
827 : {
828 16332051523 : for ( i = 0; i < L_spec; i++ )
829 : {
830 16308848960 : x[i] = (float) prm_sqQ[i];
831 : }
832 :
833 1021739923 : for ( i = L_spec; i < L_frameTCX; i++ )
834 : {
835 998537360 : x[i] = 0.0f;
836 : }
837 : }
838 :
839 : } /* else of if hTcxCfg->ctx_hm */
840 :
841 28032295 : start_zeroing = ( st->last_core != st->last_core_from_bs ) ? min( L_spec, L_frame ) : L_spec;
842 :
843 28032295 : if ( frame_cnt == 0 && st->last_core == ACELP_CORE && st->last_core_from_bs != ACELP_CORE )
844 : {
845 646 : int16_t L_spec_con = L_spec + ( st->hTcxCfg->tcx_coded_lines >> 2 );
846 :
847 276966 : for ( i = start_zeroing; i < L_spec_con; i++ )
848 : {
849 276320 : x[i] = 0.0f;
850 : }
851 :
852 646 : start_zeroing = L_spec_con;
853 : }
854 :
855 1336451311 : for ( i = start_zeroing; i < max( L_frame, L_frameTCX ); i++ )
856 : {
857 1308419016 : x[i] = 0.0f;
858 : }
859 :
860 : /*-----------------------------------------------------------*
861 : * adaptive low frequency deemphasis. *
862 : *-----------------------------------------------------------*/
863 :
864 28032295 : if ( st->element_mode != IVAS_CPE_MDCT )
865 : {
866 6044184 : weight_a( A, Ap, gamma1, M );
867 6044184 : lpc2mdct( Ap, M, gainlpc2, FDNS_NPTS, 0 );
868 : }
869 :
870 : /* initialize LF deemphasis factors in xn_buf */
871 28032295 : if ( !st->tcxonly || ( hTcxCfg->resq && hTcxDec->tcx_lpc_shaped_ari ) )
872 : {
873 2948278706 : for ( i = 0; i < max( L_spec, L_frameTCX ); i++ )
874 : {
875 2944453000 : xn_buf[i] = 1.0f;
876 : }
877 : }
878 :
879 28032295 : if ( !st->tcxonly )
880 : {
881 3825706 : AdaptLowFreqDeemph( x, hTcxDec->tcx_lpc_shaped_ari, gainlpc2, L_frame, xn_buf /* LF deemphasis factors */ );
882 : }
883 : }
884 :
885 29620013 : hTcxDec->damping = 0.f;
886 :
887 29620013 : if ( bfi == 0 )
888 : {
889 : /*-----------------------------------------------------------*
890 : * Compute global gain *
891 : *-----------------------------------------------------------*/
892 :
893 28032295 : if ( st->element_mode > EVS_MONO )
894 : {
895 : float ener;
896 :
897 : /*To be in sync with encoder gain computating*/
898 27998656 : ener = (float) sqrt( (float) L_spec / (float) NORM_MDCT_FACTOR );
899 27998656 : *gain_tcx = (float) pow( 10.0f, ( (float) index ) / 28.0f ) / ener;
900 : }
901 : else
902 : {
903 33639 : *gain_tcx = (float) pow( 10.0f, index / 28.0f ) * (float) sqrt( (float) NORM_MDCT_FACTOR / (float) L_spec );
904 : }
905 :
906 28032295 : hTcxDec->old_gaintcx_bfi = *gain_tcx;
907 :
908 28032295 : hTcxDec->cummulative_damping_tcx = 1.0f;
909 : }
910 : else /* bfi = 1 */
911 : {
912 : /* PLC: [TCX: Fade-out]
913 : * derivation of damping factor */
914 1587718 : if ( st->use_partial_copy )
915 : {
916 0 : if ( st->rf_frame_type == RF_TCXFD )
917 : {
918 0 : *gain_tcx = (float) pow( 10.0f, (int16_t) hTcxDec->old_gaintcx_bfi / 28.0f ) * (float) sqrt( (float) NORM_MDCT_FACTOR / (float) L_spec );
919 0 : hTcxDec->old_gaintcx_bfi = *gain_tcx;
920 : }
921 : else
922 : {
923 0 : *gain_tcx = hTcxDec->old_gaintcx_bfi;
924 : }
925 :
926 0 : hTcxDec->damping = 1;
927 : }
928 : else
929 : {
930 1587718 : *gain_tcx = hTcxDec->old_gaintcx_bfi;
931 1587718 : hTcxDec->damping = Damping_fact( st->coder_type, st->nbLostCmpt, st->last_good, st->stab_fac, &( st->lp_gainp ), st->last_core );
932 : }
933 :
934 1587718 : hTcxDec->cummulative_damping_tcx *= hTcxDec->damping;
935 : }
936 :
937 29620013 : if ( bfi )
938 : {
939 1587718 : if ( hTcxDec->envWeighted )
940 : {
941 0 : gamma = st->gamma;
942 : }
943 : else
944 : {
945 1587718 : gamma = gamma1;
946 : }
947 :
948 : /* PLC: [TCX: Fade-out]
949 : * PLC: invert LPC weighting in case of PLC */
950 :
951 1587718 : if ( hTcxDec->enableTcxLpc )
952 : {
953 10227 : gamma = hTcxDec->cummulative_damping_tcx * ( st->gamma - 1 ) + 1;
954 : }
955 : else
956 : {
957 1577491 : gamma = hTcxDec->cummulative_damping_tcx * ( gamma1 - 1 ) + 1;
958 : }
959 :
960 1587718 : if ( st->element_mode != IVAS_CPE_MDCT )
961 : {
962 352959 : weight_a( A, Ap, gamma, M );
963 352959 : lpc2mdct( Ap, M, gainlpc2, FDNS_NPTS, 0 );
964 : }
965 : }
966 :
967 29620013 : if ( st->element_mode != IVAS_CPE_MDCT )
968 : {
969 6397143 : tmp2 = 0;
970 6397143 : set_zero( h1, L_SUBFR + 1 );
971 6397143 : set_zero( mem, M );
972 6397143 : h1[0] = 1.0f;
973 6397143 : syn_filt( Ap, M, h1, h1, L_SUBFR, mem, 0 ); /* impulse response of LPC */
974 6397143 : deemph( h1, st->preemph_fac, L_SUBFR, &tmp2 ); /* impulse response of deemph */
975 : }
976 :
977 : /* impulse response level = gain introduced by synthesis+deemphasis */
978 29620013 : if ( !bfi )
979 : {
980 28032295 : if ( st->element_mode == IVAS_CPE_MDCT )
981 : {
982 21988111 : st->last_gain_syn_deemph = 0.0;
983 : }
984 : else
985 : {
986 6044184 : st->last_gain_syn_deemph = (float) sqrt( dotp( h1, h1, L_SUBFR ) );
987 : }
988 :
989 : /*for avoiding compiler warnings*/
990 28032295 : hTcxDec->gainHelper = 1.f;
991 28032295 : hTcxDec->stepCompensate = 0.f;
992 : }
993 1587718 : else if ( st->core == TCX_20_CORE || frame_cnt == 1 )
994 : {
995 1567949 : if ( st->element_mode == IVAS_CPE_MDCT )
996 : {
997 1215151 : gainCompensate = 1.0f;
998 : }
999 : else
1000 : {
1001 352798 : gainCompensate = st->last_gain_syn_deemph / (float) sqrt( dotp( h1, h1, L_SUBFR ) );
1002 : }
1003 :
1004 1567949 : if ( st->nbLostCmpt == 1 )
1005 : {
1006 573932 : hTcxDec->stepCompensate = ( 1.f - gainCompensate ) / st->L_frame;
1007 573932 : hTcxDec->gainHelper = 1.f;
1008 : }
1009 : else
1010 : {
1011 994017 : hTcxDec->stepCompensate = ( st->last_concealed_gain_syn_deemph - gainCompensate ) / st->L_frame;
1012 994017 : hTcxDec->gainHelper = st->last_concealed_gain_syn_deemph;
1013 : }
1014 1567949 : st->last_concealed_gain_syn_deemph = gainCompensate;
1015 : }
1016 :
1017 : /*-----------------------------------------------------------*
1018 : * Residual inv. Q. *
1019 : *-----------------------------------------------------------*/
1020 :
1021 29620013 : if ( !bfi && hTcxCfg->resq )
1022 : {
1023 11951221 : if ( hTcxDec->tcx_lpc_shaped_ari )
1024 : {
1025 : /* envelope based arithmetic coder */
1026 : const int16_t *prm_resq;
1027 371149 : prm_resq = prm_sqQ + *prm_target /* = targetBits */ - hTcxDec->resQBits[frame_cnt];
1028 371149 : i = tcx_ari_res_invQ_spec( x, L_spec, prm_resq, hTcxDec->resQBits[frame_cnt], 0, hTcxCfg->sq_rounding, xn_buf /* LF deemphasis factors */ );
1029 : }
1030 : else
1031 : {
1032 : /* context based arithmetic coder */
1033 11580072 : i = tcx_res_invQ_gain( gain_tcx, &prm_sqQ[L_spec], hTcxDec->resQBits[frame_cnt] );
1034 11580072 : tcx_res_invQ_spec( x, L_spec, &prm_sqQ[L_spec], hTcxDec->resQBits[frame_cnt], i, hTcxCfg->sq_rounding, st->tcxonly ? NULL : xn_buf /* LF deemphasis factors */ );
1035 : }
1036 : }
1037 :
1038 29620013 : if ( !bfi && st->tcxonly && st->element_mode != IVAS_CPE_MDCT )
1039 : {
1040 2218478 : if ( hTcxLtpDec->tcxltp && ( hTcxLtpDec->tcxltp_gain > 0.0f ) && !( *fUseTns ) )
1041 : {
1042 333109 : PsychAdaptLowFreqDeemph( x, gainlpc2, NULL );
1043 : }
1044 : }
1045 :
1046 29620013 : if ( !bfi && !st->tcxonly )
1047 : {
1048 : /* Replication of ACELP formant enhancement for low rates */
1049 3825706 : if ( st->total_brate < ACELP_13k20 )
1050 : {
1051 648355 : if ( st->element_mode == IVAS_CPE_MDCT )
1052 : {
1053 0 : assert( !"Not adapted to warped scale" );
1054 : }
1055 648355 : tcxFormantEnhancement( xn_buf, gainlpc2, x, L_frame );
1056 : }
1057 : }
1058 :
1059 : /*-----------------------------------------------------------*
1060 : * Add gain to the lpc gains *
1061 : *-----------------------------------------------------------*/
1062 :
1063 29620013 : if ( st->VAD == 0 )
1064 : {
1065 19159051 : *gain_tcx *= hTcxCfg->na_scale;
1066 : }
1067 :
1068 29620013 : if ( st->element_mode != IVAS_CPE_MDCT )
1069 : {
1070 6397143 : v_multc( gainlpc2, *gain_tcx, gainlpc2, FDNS_NPTS );
1071 : }
1072 :
1073 29620013 : return;
1074 : }
1075 :
1076 :
1077 : /*-------------------------------------------------------------------*
1078 : * decoder_tcx_noisefilling()
1079 : *
1080 : * TCX: core noise filling, IGF updates
1081 : *-------------------------------------------------------------------*/
1082 :
1083 29620013 : void decoder_tcx_noisefilling(
1084 : Decoder_State *st, /* i/o: coder memory state */
1085 : float concealment_noise[L_FRAME48k],
1086 : const float A[], /* i : coefficients NxAz[M+1] */
1087 : const int16_t L_frameTCX_glob,
1088 : const int16_t L_spec,
1089 : const int16_t L_frame,
1090 : const int16_t L_frameTCX,
1091 : float x[],
1092 : float gainlpc2[],
1093 : int16_t *temp_concealment_method,
1094 : const float gain_tcx,
1095 : const int16_t *prm_sqQ,
1096 : int16_t nf_seed,
1097 : const int16_t bfi, /* i : Bad frame indicator */
1098 : const int16_t MCT_flag,
1099 : const int16_t frame_cnt /* i : frame counter in the super frame*/
1100 : )
1101 : {
1102 : int16_t i;
1103 : int16_t firstLine;
1104 : float fac_ns;
1105 : int16_t noiseFillingSize;
1106 29620013 : int16_t noiseTransWidth = MIN_NOISE_FILLING_HOLE;
1107 : float tmp2;
1108 : int32_t sum_word32;
1109 : int16_t infoIGFStartLine;
1110 : float f, noiseTiltFactor, smooth_gain;
1111 : float E_2ndlast, E_last;
1112 29620013 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
1113 29620013 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
1114 29620013 : TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
1115 29620013 : int32_t total_brate = ( st->element_mode == IVAS_CPE_MDCT ? st->element_brate : st->total_brate );
1116 :
1117 : /*-----------------------------------------------------------------*
1118 : * Initializations
1119 : *-----------------------------------------------------------------*/
1120 :
1121 : /* Init lengths */
1122 29620013 : infoIGFStartLine = get_igf_startline( st, L_frame, L_frameTCX );
1123 :
1124 29620013 : noiseFillingSize = L_spec;
1125 29620013 : if ( st->igf )
1126 : {
1127 15826883 : noiseFillingSize = st->hIGFDec->infoIGFStartLine;
1128 : }
1129 :
1130 29620013 : fac_ns = (float) hTcxDec->noise_filling_index[frame_cnt] * 0.75f / ( 1 << NBITS_NOISE_FILL_LEVEL );
1131 :
1132 : /*-----------------------------------------------------------*
1133 : * Noise filling. *
1134 : *-----------------------------------------------------------*/
1135 :
1136 29620013 : if ( !bfi && ( fac_ns > 0.0f ) )
1137 : {
1138 25571959 : firstLine = tcxGetNoiseFillingTilt( A, L_frame, ( total_brate >= ACELP_13k20 && !st->rf_flag ), &noiseTiltFactor );
1139 :
1140 25571959 : if ( st->tcxonly )
1141 : {
1142 21958707 : if ( st->element_mode == IVAS_CPE_MDCT )
1143 : {
1144 20378182 : if ( frame_cnt == 0 )
1145 : {
1146 19858762 : mvr2r( hTcxDec->ltpGainMemory, &hTcxDec->ltpGainMemory[1], N_LTP_GAIN_MEMS - 1 );
1147 19858762 : hTcxDec->ltpGainMemory[0] = hTcxLtpDec->tcxltp_gain;
1148 : }
1149 :
1150 20378182 : smooth_gain = dotp( hTcxDec->ltpGainMemory, nf_tw_smoothing_coeffs, N_LTP_GAIN_MEMS );
1151 20378182 : noiseTransWidth = HOLE_SIZE_FROM_LTP( max( smooth_gain, ( hTcxCfg->ctx_hm && st->last_core != ACELP_CORE ) ? 0.3125f * st->last_ctx_hm_enabled : 0 ) );
1152 : }
1153 : else
1154 : {
1155 1580525 : noiseTransWidth = HOLE_SIZE_FROM_LTP( max( hTcxLtpDec->tcxltp_gain, ( hTcxCfg->ctx_hm && st->last_core != ACELP_CORE ) ? 0.3125f * st->last_ctx_hm_enabled : 0 ) );
1156 : }
1157 :
1158 21958707 : if ( L_frame == st->L_frame >> 1 )
1159 : {
1160 906950 : noiseTransWidth = 3; /* minimum transition fading for noise filling in TCX-10 */
1161 : }
1162 : }
1163 :
1164 25571959 : if ( !hTcxDec->tcx_lpc_shaped_ari )
1165 : {
1166 18083852891 : for ( i = 0; i < L_spec; i++ )
1167 : {
1168 18058635680 : nf_seed += (int16_t) ( abs( prm_sqQ[i] ) * i * 2 );
1169 : }
1170 : }
1171 25571959 : tcx_noise_filling( x, nf_seed, firstLine, noiseFillingSize, noiseTransWidth, L_frame, noiseTiltFactor, fac_ns, st->igf ? st->hIGFDec->infoTCXNoise : NULL, st->element_mode );
1172 :
1173 25571959 : st->seed_tcx_plc = nf_seed;
1174 : }
1175 :
1176 29620013 : if ( st->enablePlcWaveadjust )
1177 : {
1178 13789 : if ( bfi )
1179 : {
1180 105 : if ( st->nbLostCmpt == 1 )
1181 : {
1182 105 : st->hPlcInfo->concealment_method = TCX_NONTONAL;
1183 :
1184 : /* tonal/non-tonal decision */
1185 105 : if ( st->hPlcInfo->Transient[0] == 1 && st->hPlcInfo->Transient[1] == 1 && st->hPlcInfo->Transient[2] == 1 )
1186 : {
1187 39 : sum_word32 = 0;
1188 :
1189 429 : for ( i = 9; i >= 0; i-- )
1190 : {
1191 390 : sum_word32 += st->hPlcInfo->TCX_Tonality[i];
1192 : }
1193 :
1194 39 : if ( sum_word32 >= 6 )
1195 : {
1196 25 : st->hPlcInfo->concealment_method = TCX_TONAL;
1197 : }
1198 : }
1199 :
1200 105 : if ( st->tonal_mdct_plc_active )
1201 : {
1202 12 : st->hPlcInfo->concealment_method = TCX_TONAL;
1203 : }
1204 : }
1205 :
1206 105 : if ( L_frameTCX > hTcxDec->L_frameTCX )
1207 : {
1208 0 : st->hPlcInfo->concealment_method = TCX_TONAL;
1209 : }
1210 :
1211 105 : *temp_concealment_method = st->hPlcInfo->concealment_method;
1212 :
1213 105 : if ( st->core == TCX_10_CORE )
1214 : {
1215 6 : *temp_concealment_method = TCX_TONAL;
1216 : }
1217 : }
1218 :
1219 : /* get the starting location of the subframe in the frame */
1220 13789 : if ( st->core == TCX_10_CORE )
1221 : {
1222 1042 : st->hPlcInfo->subframe = frame_cnt * L_frameTCX_glob;
1223 : }
1224 : }
1225 :
1226 : /* PLC: [TCX: Tonal Concealment] */
1227 : /* PLC: [TCX: Fade-out]
1228 : * PLC: Fade out to white noise */
1229 29620013 : if ( st->hTonalMDCTConc != NULL )
1230 : {
1231 29620013 : if ( !bfi && st->element_mode != IVAS_CPE_MDCT )
1232 : {
1233 6044184 : TonalMDCTConceal_SaveFreqSignal( st->hTonalMDCTConc, x, L_frameTCX, L_frame, gainlpc2, infoIGFStartLine );
1234 : }
1235 23575829 : else if ( bfi )
1236 : {
1237 :
1238 1587718 : if ( !st->enablePlcWaveadjust || ( *temp_concealment_method == TCX_TONAL ) )
1239 : {
1240 : /* set f to 1 to not fade out */
1241 : /* set f to 0 to immediately switch to white noise */
1242 1587645 : if ( st->tcxonly && ( st->element_mode != IVAS_CPE_MDCT || MCT_flag ) )
1243 : {
1244 924199 : f = 1.0f;
1245 : }
1246 : else
1247 : {
1248 663446 : f = hTcxDec->cummulative_damping_tcx;
1249 : }
1250 :
1251 1587645 : if ( ( frame_cnt == 0 ) && ( L_frameTCX == hTcxDec->L_frameTCX >> 1 ) && ( st->tcxonly ) && ( !st->tonal_mdct_plc_active ) && ( st->nbLostCmpt == 1 ) && ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) && ( hTcxCfg->tcx_curr_overlap_mode != FULL_OVERLAP ) )
1252 : {
1253 4871 : E_2ndlast = E_last = EPSILON;
1254 4871 : if ( st->element_mode > EVS_MONO )
1255 : {
1256 808548 : for ( i = 0; i < L_frameTCX; i = i + 2 )
1257 : {
1258 803680 : E_2ndlast += st->hTonalMDCTConc->lastBlockData.spectralData[i] * st->hTonalMDCTConc->lastBlockData.spectralData[i];
1259 803680 : E_last += st->hTonalMDCTConc->lastBlockData.spectralData[i + 1] * st->hTonalMDCTConc->lastBlockData.spectralData[i + 1];
1260 : }
1261 : }
1262 : else
1263 : {
1264 339 : for ( i = 0; i < infoIGFStartLine; i = i + 2 )
1265 : {
1266 336 : E_2ndlast += st->hTonalMDCTConc->lastBlockData.spectralData[i] * st->hTonalMDCTConc->lastBlockData.spectralData[i];
1267 336 : E_last += st->hTonalMDCTConc->lastBlockData.spectralData[i + 1] * st->hTonalMDCTConc->lastBlockData.spectralData[i + 1];
1268 : }
1269 : }
1270 4871 : tmp2 = E_2ndlast / E_last;
1271 :
1272 : /* replace higher energy TCX5 frame by lower one to avoid energy fluctuation */
1273 4871 : if ( tmp2 > 2 )
1274 : {
1275 140 : if ( st->element_mode > EVS_MONO )
1276 : {
1277 23260 : for ( i = 0; i < L_frameTCX; i = i + 2 )
1278 : {
1279 23120 : st->hTonalMDCTConc->lastBlockData.spectralData[i] = st->hTonalMDCTConc->lastBlockData.spectralData[i + 1];
1280 : }
1281 : }
1282 : else
1283 : {
1284 0 : for ( i = 0; i < infoIGFStartLine; i = i + 2 )
1285 : {
1286 0 : st->hTonalMDCTConc->lastBlockData.spectralData[i] = st->hTonalMDCTConc->lastBlockData.spectralData[i + 1];
1287 : }
1288 : }
1289 : }
1290 4731 : else if ( tmp2 < 0.5 )
1291 : {
1292 2478 : if ( st->element_mode > EVS_MONO )
1293 : {
1294 415275 : for ( i = 0; i < L_frameTCX; i = i + 2 )
1295 : {
1296 412800 : st->hTonalMDCTConc->lastBlockData.spectralData[i + 1] = st->hTonalMDCTConc->lastBlockData.spectralData[i];
1297 : }
1298 : }
1299 : else
1300 : {
1301 339 : for ( i = 0; i < infoIGFStartLine; i = i + 2 )
1302 : {
1303 336 : st->hTonalMDCTConc->lastBlockData.spectralData[i + 1] = st->hTonalMDCTConc->lastBlockData.spectralData[i];
1304 : }
1305 : }
1306 : }
1307 : }
1308 :
1309 1587645 : noiseTiltFactor = 1.0f;
1310 1587645 : tcxGetNoiseFillingTilt( A, L_frame, ( total_brate >= ACELP_13k20 && !st->rf_flag ), &noiseTiltFactor );
1311 :
1312 1587645 : if ( st->element_mode == IVAS_CPE_MDCT && !MCT_flag )
1313 : {
1314 419474 : TonalMDCTConceal_InsertNoise( st->hTonalMDCTConc, x, st->tonal_mdct_plc_active, &st->seed_tcx_plc, noiseTiltFactor, f, concealment_noise, hTcxDec->CngLevelBackgroundTrace_bfi, infoIGFStartLine );
1315 : }
1316 : else
1317 : {
1318 1168171 : TonalMDCTConceal_InsertNoise( st->hTonalMDCTConc, x, st->tonal_mdct_plc_active, &st->seed_tcx_plc, noiseTiltFactor, f, NULL, hTcxDec->CngLevelBackgroundTrace_bfi, infoIGFStartLine );
1319 : }
1320 : }
1321 : }
1322 : }
1323 :
1324 : /*------------------------- SPLIT 2 noise filling ------------------------*/
1325 29620013 : if ( !bfi )
1326 : {
1327 28032295 : if ( st->element_mode == IVAS_CPE_MDCT )
1328 : {
1329 21988111 : v_multc( x, gain_tcx, x, L_spec );
1330 : }
1331 : }
1332 :
1333 29620013 : if ( L_spec < L_frame )
1334 : {
1335 1281 : set_zero( x + L_spec, L_frame - L_spec );
1336 : }
1337 29618732 : else if ( L_spec > L_frameTCX )
1338 : {
1339 1147643 : set_zero( x + L_frameTCX, L_spec - L_frameTCX );
1340 : }
1341 :
1342 29620013 : if ( bfi && ( !st->enablePlcWaveadjust || ( *temp_concealment_method == TCX_TONAL ) ) && st->igf && ( frame_cnt == 0 ) && ( L_frameTCX == hTcxDec->L_frameTCX >> 1 ) && ( st->tcxonly ) && ( !st->tonal_mdct_plc_active ) && ( st->nbLostCmpt == 1 ) && ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) && ( hTcxCfg->tcx_curr_overlap_mode != FULL_OVERLAP ) )
1343 : {
1344 1663 : IGFDecCopyLPCFlatSpectrum( st->hIGFDec, x, IGF_GRID_LB_SHORT );
1345 : /* also replace flat spectrum for the second TCX10 sub frame */
1346 1663 : IGFDecUpdateInfo( st->hIGFDec, 1, IGF_GRID_LB_SHORT );
1347 1663 : IGFDecCopyLPCFlatSpectrum( st->hIGFDec, x, IGF_GRID_LB_SHORT );
1348 1663 : IGFDecUpdateInfo( st->hIGFDec, 0, IGF_GRID_LB_SHORT );
1349 1663 : mvs2s( st->hIGFDec->igfData.igf_curr_subframe[0][0], st->hIGFDec->igfData.igf_curr_subframe[1][0], IGF_MAX_SFB );
1350 : }
1351 29618350 : else if ( bfi && st->igf && ( frame_cnt == 0 ) && ( L_frameTCX == hTcxDec->L_frameTCX >> 1 ) )
1352 : {
1353 : /* copy second to first subframe */
1354 3460 : IGFDecReplicateTCX10State( st->hIGFDec );
1355 : }
1356 :
1357 29620013 : if ( st->element_mode != EVS_MONO )
1358 : {
1359 29586175 : if ( bfi )
1360 : {
1361 1587519 : nf_seed = st->seed_tcx_plc;
1362 : }
1363 27998656 : else if ( nf_seed == 0 )
1364 : {
1365 2507029 : nf_seed = *st->hIGFDec->igfData.igfInfo.nfSeed;
1366 : }
1367 : }
1368 :
1369 29620013 : if ( st->igf )
1370 : {
1371 15826883 : *st->hIGFDec->igfData.igfInfo.nfSeed = (int16_t) ( nf_seed * 31821L + 13849L );
1372 : }
1373 :
1374 29620013 : return;
1375 : }
1376 :
1377 :
1378 : /*-------------------------------------------------------------------*
1379 : * decoder_tcx_noiseshaping_igf()
1380 : *
1381 : * TCX: FDNS and IGF application
1382 : *-------------------------------------------------------------------*/
1383 :
1384 29620013 : void decoder_tcx_noiseshaping_igf(
1385 : Decoder_State *st, /* i/o: coder memory state */
1386 : const int16_t L_spec,
1387 : const int16_t L_frame,
1388 : const int16_t L_frameTCX,
1389 : const int16_t left_rect,
1390 : float x[],
1391 : const float gainlpc2[],
1392 : int16_t *temp_concealment_method,
1393 : const int16_t bfi /* i : Bad frame indicator */
1394 : )
1395 : {
1396 29620013 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
1397 :
1398 : /*-----------------------------------------------------------*
1399 : * Noise shaping in frequency domain (1/Wz) *
1400 : *-----------------------------------------------------------*/
1401 :
1402 29620013 : if ( st->igf && ( !bfi || ( st->element_mode == IVAS_CPE_MDCT && st->prev_bfi ) ) )
1403 : {
1404 15495662 : if ( ( L_frame == st->L_frame >> 1 ) && ( st->tcxonly ) )
1405 : {
1406 643982 : IGFDecCopyLPCFlatSpectrum( st->hIGFDec, x, IGF_GRID_LB_SHORT );
1407 : }
1408 : else
1409 : {
1410 14851680 : IGFDecCopyLPCFlatSpectrum( st->hIGFDec, x, ( st->last_core == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM );
1411 : }
1412 : }
1413 : /* LPC gains already available */
1414 :
1415 29620013 : if ( st->element_mode != IVAS_CPE_MDCT && ( !st->enablePlcWaveadjust || !bfi || ( *temp_concealment_method == TCX_TONAL ) ) )
1416 : {
1417 6397070 : mdct_noiseShaping( x, L_frame, gainlpc2, FDNS_NPTS );
1418 :
1419 6397070 : if ( !bfi )
1420 : {
1421 6044184 : v_multc( x + L_frame, gainlpc2[FDNS_NPTS - 1], x + L_frame, L_spec - L_frame );
1422 : }
1423 :
1424 6397070 : set_zero( x + L_spec, L_frameTCX - L_spec );
1425 : }
1426 :
1427 : /* PLC: [TCX: Tonal Concealment] */
1428 29620013 : if ( bfi && st->tonal_mdct_plc_active && st->element_mode != IVAS_CPE_MDCT )
1429 : {
1430 50484 : TonalMDCTConceal_Apply( st->hTonalMDCTConc, x, st->hTcxCfg->psychParamsCurrent );
1431 : }
1432 :
1433 29620013 : if ( st->hTonalMDCTConc != NULL && st->element_mode != IVAS_CPE_MDCT )
1434 : {
1435 6397143 : TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, L_frameTCX, ( hTcxDec->tcxltp_last_gain_unmodified > 0 ) ? st->old_fpitch : 0, bfi, bfi && st->tonal_mdct_plc_active );
1436 : }
1437 :
1438 29620013 : if ( st->enablePlcWaveadjust )
1439 : {
1440 : /* spectrum concealment */
1441 13789 : if ( bfi && *temp_concealment_method == TCX_NONTONAL )
1442 : {
1443 73 : concealment_decode( st->core, x, st->hPlcInfo );
1444 : }
1445 :
1446 : /* update spectrum buffer, tonality flag, etc. */
1447 13789 : concealment_update( bfi, st->core, st->tonality_flag, x, st->hPlcInfo );
1448 : }
1449 :
1450 : /*-----------------------------------------------------------*
1451 : * IGF *
1452 : *-----------------------------------------------------------*/
1453 :
1454 29620013 : if ( st->igf && !( ( L_frame == st->L_frame >> 1 ) && st->tcxonly ) )
1455 : {
1456 15177957 : if ( st->element_mode != IVAS_CPE_MDCT )
1457 : {
1458 4791741 : IGFDecApplyMono( st->hIGFDec, x, ( st->last_core == ACELP_CORE || ( left_rect && bfi ) ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, bfi, st->element_mode );
1459 : }
1460 : }
1461 :
1462 29620013 : if ( st->igf && ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly ) )
1463 : {
1464 648926 : if ( st->element_mode != IVAS_CPE_MDCT )
1465 : {
1466 48638 : IGFDecApplyMono( st->hIGFDec, x, IGF_GRID_LB_SHORT, bfi, st->element_mode );
1467 : }
1468 : }
1469 :
1470 29620013 : return;
1471 : }
1472 :
1473 :
1474 : /*-------------------------------------------------------------------*
1475 : * decoder_tcx_tns()
1476 : *
1477 : * TCX: TNS application
1478 : *-------------------------------------------------------------------*/
1479 :
1480 52728227 : void decoder_tcx_tns(
1481 : Decoder_State *st, /* i/o: coder memory state */
1482 : const int16_t L_frame_glob, /* i : frame length */
1483 : const int16_t L_spec,
1484 : const int16_t L_frame,
1485 : const int16_t L_frameTCX,
1486 : float x[N_MAX],
1487 : const int16_t fUseTns, /* i : flag that is set if TNS data is present */
1488 : STnsData *tnsData,
1489 : const int16_t bfi, /* i : Bad frame indicator */
1490 : const int16_t frame_cnt, /* i : frame counter in the super frame */
1491 : const int16_t whitenedDomain )
1492 : {
1493 : int16_t index, isTCX5, L;
1494 52728227 : TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
1495 :
1496 52728227 : index = hTcxCfg->tcx_last_overlap_mode; /* backup last TCX overlap mode */
1497 :
1498 52728227 : isTCX5 = 0;
1499 52728227 : L = L_frameTCX;
1500 :
1501 52728227 : if ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly )
1502 : {
1503 2445960 : if ( frame_cnt && !bfi && st->last_core != ACELP_CORE )
1504 : {
1505 : /* fix sub-window overlap */
1506 1183603 : hTcxCfg->tcx_last_overlap_mode = hTcxCfg->tcx_curr_overlap_mode;
1507 : }
1508 :
1509 2445960 : if ( ( hTcxCfg->fIsTNSAllowed && fUseTns != 0 && bfi != 1 && whitenedDomain ) || ( L_spec > L_frameTCX ) )
1510 : {
1511 676202 : L = L_spec;
1512 : }
1513 :
1514 2445960 : if ( ( ( !bfi ) && ( ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) ||
1515 1877754 : ( ( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) && ( frame_cnt == 0 ) && ( index == 0 ) ) ) ) ||
1516 78754 : ( ( bfi ) && ( ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) &&
1517 50788 : !( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) ) ) )
1518 : {
1519 1257212 : isTCX5 = 1;
1520 :
1521 1257212 : tcx5SpectrumDeinterleaving( L >> 1, x );
1522 : }
1523 : }
1524 :
1525 : #ifdef DEBUG_PLOT
1526 : if ( !whitenedDomain )
1527 : sendDebout( "tnsSpec2", L_frameTCX, 1, "aftInvertGrouping", MTV_FLOAT, x );
1528 : #endif
1529 :
1530 : /*-----------------------------------------------------------*
1531 : * Temporal Noise Shaping Synthesis *
1532 : *-----------------------------------------------------------*/
1533 :
1534 52728227 : if ( hTcxCfg->fIsTNSAllowed && fUseTns != 0 && bfi != 1 && tnsData->tnsOnWhitenedSpectra == whitenedDomain )
1535 : {
1536 : /* Apply TNS to get the reconstructed signal */
1537 2161895 : SetTnsConfig( hTcxCfg, L_frame_glob == st->L_frame, ( st->last_core == ACELP_CORE ) && ( frame_cnt == 0 ) );
1538 :
1539 2161895 : if ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly && isTCX5 )
1540 : {
1541 506486 : tcx5TnsGrouping( L >> 1, hTcxCfg->tnsConfig[0][0].iFilterBorders[0] >> 1, x );
1542 : }
1543 :
1544 2161895 : ApplyTnsFilter( hTcxCfg->pCurrentTnsConfig, tnsData, x, 0 );
1545 : #ifdef DEBUG_PLOT
1546 : sendDebout( "tnsSpec2", L_frameTCX, 1, "aftTNS", MTV_FLOAT, x );
1547 : #endif
1548 :
1549 2161895 : if ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly && isTCX5 )
1550 : {
1551 506486 : if ( st->element_mode == EVS_MONO || ( L_spec < L_frameTCX && !whitenedDomain ) ) /* todo: this is temporary to maintain EVS BE, this is a bug and should be fixed also for EVS (see issue 13) */
1552 : {
1553 26213 : tcx5TnsUngrouping( L_frameTCX >> 1, hTcxCfg->tnsConfig[0][0].iFilterBorders[0] >> 1, x, DEC );
1554 : }
1555 : else
1556 : {
1557 480273 : tcx5TnsUngrouping( L >> 1, hTcxCfg->tnsConfig[0][0].iFilterBorders[0] >> 1, x, DEC );
1558 : }
1559 : }
1560 : }
1561 :
1562 52728227 : if ( whitenedDomain && isTCX5 )
1563 : {
1564 604765 : tcx5SpectrumInterleaving( L >> 1, x );
1565 : }
1566 :
1567 : #ifdef DEBUG_PLOT
1568 : if ( !whitenedDomain )
1569 : sendDebout( "tnsSpec1", L_frameTCX, 1, "aftTNS", MTV_FLOAT, x );
1570 : #endif
1571 : /* restore index */
1572 52728227 : if ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly && frame_cnt && !bfi && st->last_core != ACELP_CORE )
1573 : {
1574 : /* restore sub-window overlap */
1575 1183603 : hTcxCfg->tcx_last_overlap_mode = index;
1576 : }
1577 :
1578 52728227 : return;
1579 : }
1580 :
1581 :
1582 : /*-------------------------------------------------------------------*
1583 : * decoder_tcx_imdct()
1584 : *
1585 : * TCX: ITF and IMDCT
1586 : *-------------------------------------------------------------------*/
1587 :
1588 29562685 : void decoder_tcx_imdct(
1589 : Decoder_State *st, /* i/o: coder memory state */
1590 : const int16_t L_frame_glob, /* i : frame length */
1591 : const int16_t L_frameTCX_glob,
1592 : const int16_t L_spec,
1593 : const int16_t tcx_offset,
1594 : const int16_t tcx_offsetFB,
1595 : const int16_t L_frame,
1596 : const int16_t L_frameTCX,
1597 : const int16_t left_rect,
1598 : float x[N_MAX],
1599 : float xn_buf[],
1600 : const uint16_t kernelType, /* i : TCX transform kernel type */
1601 : const int16_t fUseTns, /* i : flag that is set if TNS data is present */
1602 : float synth[], /* i/o: synth[-M..L_frame] */
1603 : float synthFB[],
1604 : const int16_t bfi, /* i : Bad frame indicator */
1605 : const int16_t frame_cnt, /* i : frame counter in the super frame */
1606 : const int16_t sba_dirac_stereo_flag /* i : signal stereo output for SBA DirAC */
1607 : )
1608 : {
1609 : int16_t j, L, overlap, curr_order, startLine, endLine, isTCX5;
1610 : int16_t overlapFB;
1611 : float x_tmp[L_FRAME_PLUS];
1612 : float xn_bufFB[L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX];
1613 : float acelp_zir[L_FRAME_MAX / 2];
1614 : float x_itf[N_MAX_TCX - IGF_START_MN];
1615 : float A_itf[ITF_MAX_FILTER_ORDER + 1];
1616 : float predictionGain;
1617 : int16_t index, proc;
1618 29562685 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
1619 29562685 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
1620 29562685 : TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
1621 :
1622 : /*-----------------------------------------------------------------*
1623 : * Initializations
1624 : *-----------------------------------------------------------------*/
1625 :
1626 : /* Init lengths */
1627 29562685 : overlap = hTcxCfg->tcx_mdct_window_length;
1628 29562685 : overlapFB = hTcxCfg->tcx_mdct_window_lengthFB;
1629 :
1630 29562685 : index = hTcxCfg->tcx_last_overlap_mode; /* backup last TCX overlap mode */
1631 29562685 : if ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly && frame_cnt && !bfi && st->last_core != ACELP_CORE )
1632 : {
1633 : /* fix sub-window overlap */
1634 615155 : hTcxCfg->tcx_last_overlap_mode = hTcxCfg->tcx_curr_overlap_mode;
1635 : }
1636 :
1637 29562685 : if ( st->igf )
1638 : {
1639 15806692 : proc = st->hIGFDec->flatteningTrigger;
1640 :
1641 15806692 : if ( proc && fUseTns != 0 )
1642 : {
1643 1163889 : proc = 0;
1644 : }
1645 :
1646 15806692 : if ( proc )
1647 : {
1648 13972961 : startLine = st->hIGFDec->infoIGFStartLine;
1649 13972961 : endLine = st->hIGFDec->infoIGFStopLine;
1650 13972961 : curr_order = 0;
1651 13972961 : predictionGain = 0;
1652 13972961 : L = L_frameTCX;
1653 13972961 : isTCX5 = 0;
1654 :
1655 : /* interleave again for ITF */
1656 13972961 : if ( ( L_frame == st->L_frame >> 1 ) && st->tcxonly )
1657 : {
1658 275711 : if ( ( hTcxCfg->fIsTNSAllowed && fUseTns != 0 && bfi != 1 ) || ( L_spec > L_frameTCX ) )
1659 : {
1660 24583 : L = L_spec;
1661 : }
1662 :
1663 275711 : if ( ( ( !bfi ) && ( ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) ||
1664 243283 : ( ( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) && ( frame_cnt == 0 ) && ( index == 0 ) ) ) ) ||
1665 9503 : ( ( bfi ) && ( ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) &&
1666 7609 : !( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) ) ) )
1667 : {
1668 52102 : isTCX5 = 1;
1669 :
1670 52102 : tcx5SpectrumInterleaving( L >> 1, x );
1671 : }
1672 : }
1673 :
1674 4212613307 : for ( j = startLine; j < endLine; j++ )
1675 : {
1676 4198640346 : if ( st->hIGFDec->flag_sparse[j - IGF_START_MN] == 2 )
1677 : {
1678 8676756 : x_itf[j - IGF_START_MN] = x[j];
1679 8676756 : x[j] = st->hIGFDec->virtualSpec[j - IGF_START_MN];
1680 : }
1681 : }
1682 :
1683 13972961 : ITF_Detect( x + IGF_START_MN, startLine, endLine, 8 /*maxOrder*/, A_itf, &predictionGain, &curr_order );
1684 :
1685 13972961 : ITF_Apply( x, startLine, endLine, A_itf, curr_order );
1686 :
1687 4212613307 : for ( j = startLine; j < endLine; j++ )
1688 : {
1689 4198640346 : if ( st->hIGFDec->flag_sparse[j - IGF_START_MN] == 2 )
1690 : {
1691 8676756 : x[j] = x_itf[j - IGF_START_MN];
1692 : }
1693 : }
1694 :
1695 : /* deinterleave */
1696 13972961 : if ( isTCX5 )
1697 : {
1698 52102 : tcx5SpectrumDeinterleaving( L >> 1, x );
1699 : }
1700 : }
1701 : }
1702 :
1703 : /*-----------------------------------------------------------*
1704 : * Prepare OLA buffer after waveadjustment. *
1705 : * Compute inverse MDCT of x[]. *
1706 : *-----------------------------------------------------------*/
1707 :
1708 : #ifdef DEBUG_PLOT
1709 : sendDebout( "tnsSpec", L_frameTCX, 1, "befIMDCT", MTV_FLOAT, x );
1710 : #endif
1711 :
1712 29562685 : if ( st->element_mode == IVAS_CPE_MDCT )
1713 : {
1714 23165542 : set_f( x_tmp, 0.0f, L_FRAME_PLUS );
1715 23165542 : mvr2r( x, x_tmp, min( L_FRAME48k, max( L_spec, max( L_frame, L_frameTCX ) ) ) );
1716 23165542 : mvr2r( x, xn_bufFB, min( L_FRAME48k, max( L_spec, max( L_frame, L_frameTCX ) ) ) );
1717 : }
1718 6397143 : else if ( st->element_mode == EVS_MONO )
1719 : {
1720 33838 : mvr2r( x, xn_bufFB, max( L_spec, max( L_frame, L_frameTCX ) ) );
1721 : }
1722 : else
1723 : {
1724 6363305 : mvr2r( x, x_tmp, max( L_spec, max( L_frame, L_frameTCX ) ) );
1725 6363305 : mvr2r( x, xn_bufFB, max( L_spec, max( L_frame, L_frameTCX ) ) );
1726 : }
1727 :
1728 29562685 : if ( st->igf )
1729 : {
1730 15806692 : set_zero( xn_bufFB + st->hIGFDec->infoIGFStartLine, L_frameTCX - st->hIGFDec->infoIGFStartLine );
1731 : }
1732 :
1733 29562685 : if ( st->element_mode != IVAS_CPE_DFT && !sba_dirac_stereo_flag )
1734 : {
1735 29042991 : IMDCT( xn_bufFB, hTcxDec->syn_Overl, hTcxDec->syn_Overl_TDAC, xn_buf, hTcxCfg->tcx_aldo_window_1_trunc, hTcxCfg->tcx_aldo_window_2,
1736 29042991 : hTcxCfg->tcx_mdct_window_half, hTcxCfg->tcx_mdct_window_minimum, hTcxCfg->tcx_mdct_window_trans, hTcxCfg->tcx_mdct_window_half_length, hTcxCfg->tcx_mdct_window_min_length, index,
1737 29042991 : kernelType, left_rect, tcx_offset, overlap, L_frame, L_frameTCX, max( L_frameTCX, L_spec ) >> 1, L_frame_glob, frame_cnt, bfi, st->hHQ_core->old_outLB, 0, st, 0, acelp_zir );
1738 : }
1739 :
1740 : /* Generate additional comfort noise to mask potential coding artefacts */
1741 29562685 : if ( st->flag_cna && st->element_mode != IVAS_CPE_TD && st->element_mode != IVAS_CPE_DFT && !st->cna_dirac_flag )
1742 : {
1743 1168280 : generate_masking_noise_mdct( x, st->hFdCngDec->hFdCngCom );
1744 : }
1745 :
1746 29562685 : if ( st->element_mode == IVAS_CPE_DFT || sba_dirac_stereo_flag )
1747 : {
1748 519694 : mvr2r( x, xn_bufFB, max( L_spec, max( L_frame, L_frameTCX ) ) );
1749 :
1750 519694 : IMDCT( xn_bufFB, hTcxDec->syn_Overl, hTcxDec->syn_Overl_TDAC, xn_buf, hTcxCfg->tcx_aldo_window_1_trunc, hTcxCfg->tcx_aldo_window_2, hTcxCfg->tcx_mdct_window_half, hTcxCfg->tcx_mdct_window_minimum, hTcxCfg->tcx_mdct_window_trans, hTcxCfg->tcx_mdct_window_half_length, hTcxCfg->tcx_mdct_window_min_length, index,
1751 519694 : kernelType, left_rect, tcx_offset, overlap, L_frame, L_frameTCX, max( L_frameTCX, L_spec ) >> 1, L_frame_glob, frame_cnt, bfi, st->hHQ_core->old_outLB, 0, st, 0, acelp_zir );
1752 : }
1753 :
1754 29562685 : if ( st->element_mode != EVS_MONO )
1755 : {
1756 29528847 : IMDCT( x_tmp, hTcxDec->syn_OverlFB, hTcxDec->syn_Overl_TDACFB, xn_bufFB, hTcxCfg->tcx_aldo_window_1_FB_trunc, hTcxCfg->tcx_aldo_window_2_FB,
1757 29528847 : hTcxCfg->tcx_mdct_window_halfFB, hTcxCfg->tcx_mdct_window_minimumFB, hTcxCfg->tcx_mdct_window_transFB, hTcxCfg->tcx_mdct_window_half_lengthFB, hTcxCfg->tcx_mdct_window_min_lengthFB, index,
1758 29528847 : kernelType, left_rect, tcx_offsetFB, overlapFB, L_frameTCX, L_frameTCX, max( L_frameTCX, L_spec ) >> 1, L_frameTCX_glob, frame_cnt, bfi, st->hHQ_core->old_out, 1, st, FSCALE_DENOM * L_frameTCX_glob / L_frame_glob, acelp_zir );
1759 : }
1760 : else
1761 : {
1762 33838 : IMDCT( x, hTcxDec->syn_OverlFB, hTcxDec->syn_Overl_TDACFB, xn_bufFB, hTcxCfg->tcx_aldo_window_1_FB_trunc, hTcxCfg->tcx_aldo_window_2_FB, hTcxCfg->tcx_mdct_window_halfFB, hTcxCfg->tcx_mdct_window_minimumFB, hTcxCfg->tcx_mdct_window_transFB, hTcxCfg->tcx_mdct_window_half_lengthFB, hTcxCfg->tcx_mdct_window_min_lengthFB, index,
1763 33838 : kernelType, left_rect, tcx_offsetFB, overlapFB, L_frameTCX, L_frameTCX, max( L_frameTCX, L_spec ) >> 1, L_frameTCX_glob, frame_cnt, bfi, st->hHQ_core->old_out, 1, st, FSCALE_DENOM * L_frameTCX_glob / L_frame_glob, acelp_zir );
1764 : }
1765 :
1766 : /* PLC: [TCX: Tonal Concealment] */
1767 29562685 : if ( !bfi )
1768 : {
1769 27974967 : st->second_last_tns_active = st->last_tns_active;
1770 27974967 : st->last_tns_active = hTcxCfg->fIsTNSAllowed & fUseTns;
1771 27974967 : hTcxDec->tcxltp_third_last_pitch = hTcxDec->tcxltp_second_last_pitch;
1772 27974967 : hTcxDec->tcxltp_second_last_pitch = st->old_fpitch;
1773 27974967 : st->old_fpitch = hTcxLtpDec->tcxltp_pitch_int + hTcxLtpDec->tcxltp_pitch_fr / (float) st->pit_res_max;
1774 :
1775 27974967 : if ( st->element_mode == IVAS_CPE_MDCT )
1776 : {
1777 21930783 : st->old_fpitch *= (float) L_frame_glob / (float) L_FRAME;
1778 : }
1779 :
1780 27974967 : if ( st->element_mode > EVS_MONO )
1781 : {
1782 27941328 : st->old_fpitchFB = st->old_fpitch * (float) L_frameTCX_glob / (float) L_frame_glob;
1783 : }
1784 : else
1785 : {
1786 33639 : st->old_fpitchFB = st->old_fpitch * (float) L_frameTCX / (float) L_frame;
1787 : }
1788 : }
1789 :
1790 : /* Update old_syn_overl */
1791 29562685 : if ( !hTcxCfg->last_aldo )
1792 : {
1793 869508 : mvr2r( xn_buf + L_frame, hTcxDec->syn_Overl, overlap );
1794 869508 : mvr2r( xn_bufFB + L_frameTCX, hTcxDec->syn_OverlFB, overlapFB );
1795 : }
1796 :
1797 : /* Output */
1798 29562685 : mvr2r( xn_buf + ( overlap >> 1 ) - tcx_offset, synth, L_frame_glob );
1799 29562685 : mvr2r( xn_bufFB + ( overlapFB >> 1 ) - tcx_offsetFB, synthFB, L_frameTCX_glob );
1800 :
1801 29562685 : return;
1802 : }
1803 :
1804 :
1805 : /*-------------------------------------------------------------------*
1806 : * init_tcx_info()
1807 : *
1808 : * Initialize lengths for TCX processing, update IGF subframe info
1809 : *-------------------------------------------------------------------*/
1810 :
1811 90008771 : void init_tcx_info(
1812 : Decoder_State *st, /* i/o: coder memory state */
1813 : const int16_t L_frame_glob, /* i : global frame length */
1814 : const int16_t L_frameTCX_glob, /* i : FB global frame length */
1815 : const int16_t frame_cnt, /* i : frame counter in the super_frame */
1816 : const int16_t bfi, /* i : bad frame indicator */
1817 : int16_t *tcx_offset, /* o : folding point offset relative to the end of the previous frame */
1818 : int16_t *tcx_offsetFB, /* o : FB folding point offset relative to the end of the previous frame*/
1819 : int16_t *L_frame, /* o : frame length */
1820 : int16_t *L_frameTCX, /* o : TCX frame length */
1821 : int16_t *left_rect, /* o : left part is rectangular */
1822 : int16_t *L_spec /* o : spectrum length */
1823 : )
1824 : {
1825 90008771 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
1826 90008771 : TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
1827 :
1828 : /* Init lengths */
1829 90008771 : *tcx_offset = hTcxCfg->tcx_offset;
1830 90008771 : *tcx_offsetFB = hTcxCfg->tcx_offsetFB;
1831 :
1832 90008771 : if ( bfi )
1833 : {
1834 : /* PLC: [TCX: Memory update]
1835 : * PLC: Init buffers */
1836 :
1837 4781943 : assert( st->L_frame_past > 0 );
1838 4781943 : *L_frame = st->L_frame_past;
1839 4781943 : *L_frameTCX = st->L_frameTCX_past;
1840 :
1841 4781943 : *left_rect = hTcxDec->prev_widow_left_rect;
1842 :
1843 4781943 : if ( *left_rect )
1844 : {
1845 20267 : *tcx_offset = hTcxCfg->lfacNext;
1846 20267 : *tcx_offsetFB = hTcxCfg->lfacNextFB;
1847 20267 : *L_spec += st->hTcxCfg->tcx_coded_lines >> 2;
1848 : }
1849 : }
1850 : else
1851 : {
1852 85226828 : if ( frame_cnt == 0 && st->last_core == ACELP_CORE )
1853 : {
1854 289364 : if ( !st->prev_bfi )
1855 : {
1856 280552 : hTcxCfg->last_aldo = 0;
1857 : }
1858 :
1859 : /* if past frame is ACELP */
1860 289364 : *L_frame = L_frame_glob + *tcx_offset;
1861 289364 : *L_frameTCX = L_frameTCX_glob + *tcx_offsetFB;
1862 289364 : if ( st->last_core == st->last_core_from_bs )
1863 : {
1864 : /* case: last frame was lost and concealed as CNG */
1865 : /* using the longer transition spec length causes reading of uninitialized data */
1866 286759 : *L_spec += st->hTcxCfg->tcx_coded_lines >> 2;
1867 : }
1868 :
1869 289364 : assert( hTcxCfg->lfacNext <= 0 );
1870 289364 : *L_frame -= hTcxCfg->lfacNext;
1871 289364 : *L_frameTCX -= hTcxCfg->lfacNextFB;
1872 289364 : *tcx_offset = hTcxCfg->lfacNext;
1873 289364 : *tcx_offsetFB = hTcxCfg->lfacNextFB;
1874 :
1875 289364 : *left_rect = 1;
1876 289364 : hTcxDec->prev_widow_left_rect = 1;
1877 : }
1878 : else
1879 : {
1880 84937464 : *L_frame = L_frame_glob;
1881 84937464 : *L_frameTCX = L_frameTCX_glob;
1882 84937464 : *left_rect = 0;
1883 84937464 : hTcxDec->prev_widow_left_rect = 0;
1884 : }
1885 :
1886 85226828 : st->L_frame_past = *L_frame;
1887 85226828 : st->L_frameTCX_past = *L_frameTCX;
1888 : }
1889 :
1890 90008771 : if ( st->igf )
1891 : {
1892 48264327 : if ( ( *L_frame == st->L_frame >> 1 ) && ( st->tcxonly ) )
1893 : {
1894 2356867 : IGFDecUpdateInfo( st->hIGFDec, frame_cnt, IGF_GRID_LB_SHORT );
1895 : }
1896 : else
1897 : {
1898 45907460 : IGFDecUpdateInfo( st->hIGFDec, frame_cnt, ( st->last_core == ACELP_CORE || ( *left_rect && st->bfi ) ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM );
1899 : }
1900 : }
1901 :
1902 90008771 : return;
1903 : }
1904 :
1905 :
1906 : /*-------------------------------------------------------------------*
1907 : * decoder_tcx_IGF_mono()
1908 : *
1909 : * TCX: apply mono IGF
1910 : *-------------------------------------------------------------------*/
1911 :
1912 6015008 : void decoder_tcx_IGF_mono(
1913 : Decoder_State *st, /* i/o: coder memory state */
1914 : float x[], /* o : de-quatized coefficients */
1915 : const int16_t L_frame, /* i : frame length */
1916 : const int16_t left_rect, /* i : left part is rectangular */
1917 : const int16_t bfi, /* i : bad frame indicator */
1918 : const int16_t frame_cnt /* i : frame counter in the super_frame */
1919 : )
1920 : {
1921 : int16_t igfGridIdx;
1922 :
1923 11710808 : IGFDecUpdateInfo( st->hIGFDec, frame_cnt, ( L_frame == st->L_frame >> 1 ) && ( st->tcxonly ) ? IGF_GRID_LB_SHORT : ( st->last_core == ACELP_CORE || left_rect ) ? IGF_GRID_LB_TRAN
1924 5695800 : : IGF_GRID_LB_NORM );
1925 :
1926 6015008 : if ( st->igf )
1927 : {
1928 6015008 : igfGridIdx = IGF_GRID_LB_SHORT;
1929 : /*st->hIGFDec.igfData.igfInfo.nfSeed = (int16_t)(*nf_seed * 31821L + 13849L);*/
1930 6015008 : if ( !( L_frame == st->L_frame >> 1 ) && st->tcxonly )
1931 : {
1932 5695800 : igfGridIdx = ( st->last_core == ACELP_CORE || ( left_rect && bfi ) ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM;
1933 : }
1934 :
1935 6015008 : IGFDecApplyMono( st->hIGFDec, x, igfGridIdx, bfi, st->element_mode );
1936 : }
1937 :
1938 6015008 : return;
1939 : }
1940 :
1941 :
1942 : /*-------------------------------------------------------------------*
1943 : * decoder_tcx_IGF_stereo()
1944 : *
1945 : * TCX: apply stereo IGF processing
1946 : *-------------------------------------------------------------------*/
1947 :
1948 2485748 : void decoder_tcx_IGF_stereo(
1949 : Decoder_State **sts, /* i/o: coder memory states */
1950 : STEREO_MDCT_DEC_DATA_HANDLE hStereoMdct, /* i/o: MDCT stereo structure */
1951 : int16_t ms_mask[NB_DIV][MAX_SFB], /* i : bandwise MS mask */
1952 : float *x[CPE_CHANNELS][NB_DIV], /* o : de-quatized coefficients */
1953 : const int16_t L_frame, /* i : frame length */
1954 : const int16_t left_rect, /* i : left part is rectangular */
1955 : const int16_t k, /* i : Subframe index */
1956 : const int16_t bfi, /* i : bad frame indicator */
1957 : const int16_t MCT_flag /* i : hMCT handle allocated (1) or not (0) */
1958 : )
1959 : {
1960 : int16_t coreMsMask[N_MAX];
1961 : STEREO_MDCT_BAND_PARAMETERS *sfbConf;
1962 : int16_t core, sfb, igfGridIdx;
1963 :
1964 2485748 : core = sts[0]->core;
1965 :
1966 : /* assumptions: stereo filling was already done on the flattened spectra
1967 : * IGF region is always coded M/S, never L/R (to be done in the encoder)
1968 : * for residual bands with stereo filling infoTcxNoise is set to zero
1969 : * both channels have the same IGF configuration
1970 : */
1971 :
1972 : #ifdef DEBUGGING
1973 : /* sanity checks: check if both channels have the same configuration...*/
1974 : assert( ( sts[0]->core == sts[1]->core ) );
1975 : if ( sts[0]->last_core_from_bs == ACELP_CORE || sts[1]->last_core_from_bs == ACELP_CORE )
1976 : {
1977 : assert( ( sts[0]->last_core_from_bs == sts[1]->last_core_from_bs ) );
1978 : }
1979 : #endif
1980 :
1981 : /* initialization */
1982 2485748 : sfbConf = ( core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
1983 :
1984 2485748 : if ( sts[0]->last_core == ACELP_CORE )
1985 : {
1986 168 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
1987 : }
1988 :
1989 : /* create line wise ms mask for the core bands */
1990 2485748 : set_s( coreMsMask, 0, N_MAX );
1991 :
1992 104635908 : for ( sfb = 0; sfb < sfbConf->sfbCnt; sfb++ )
1993 : {
1994 102150160 : set_s( &coreMsMask[sfbConf->sfbOffset[sfb]], ms_mask[k][sfb], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] );
1995 : }
1996 :
1997 4830956 : IGFDecUpdateInfo( sts[0]->hIGFDec, k, ( L_frame == sts[0]->L_frame >> 1 ) && ( sts[0]->tcxonly ) ? IGF_GRID_LB_SHORT : ( sts[0]->last_core == ACELP_CORE || left_rect ) ? IGF_GRID_LB_TRAN
1998 2345208 : : IGF_GRID_LB_NORM );
1999 :
2000 4830956 : IGFDecUpdateInfo( sts[1]->hIGFDec, k, ( L_frame == sts[1]->L_frame >> 1 ) && ( sts[1]->tcxonly ) ? IGF_GRID_LB_SHORT : ( sts[1]->last_core == ACELP_CORE || left_rect ) ? IGF_GRID_LB_TRAN
2001 2345208 : : IGF_GRID_LB_NORM );
2002 :
2003 2485748 : if ( sts[0]->igf )
2004 : {
2005 2485748 : igfGridIdx = IGF_GRID_LB_SHORT;
2006 2485748 : if ( !( L_frame == sts[0]->L_frame >> 1 ) && ( sts[0]->tcxonly ) )
2007 : {
2008 2345208 : igfGridIdx = ( sts[0]->last_core == ACELP_CORE || ( left_rect && bfi ) ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM;
2009 : }
2010 :
2011 2485748 : IGFDecApplyStereo( sts[0]->hIGFDec, sts[1]->hIGFDec, x[0][k], x[1][k], igfGridIdx, coreMsMask, hStereoMdct->IGFStereoMode[k] == SMDCT_BW_MS, bfi, MCT_flag );
2012 : }
2013 :
2014 2485748 : return;
2015 : }
|