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 : #ifdef DEBUGGING
37 : #include "debug.h"
38 : #endif
39 : #include <math.h>
40 : #include "prot.h"
41 : #include "rom_com.h"
42 : #include "stat_dec.h"
43 : #include "wmc_auto.h"
44 : #include "basop_proto_func.h"
45 : #include "stat_com.h"
46 : #include "ivas_prot.h"
47 :
48 : /*-------------------------------------------------------------*
49 : * Local prototypes
50 : *-------------------------------------------------------------*/
51 :
52 : static void dec_prm_tcx( Decoder_State *st, int16_t param[], int16_t param_lpc[], int16_t *total_nbbits, const int16_t last_element_mode, int16_t *bitsRead );
53 : static void stereo_tcx_dec_mode_switch_reconf( Decoder_State *st, const int16_t MCT_flag, const int16_t last_element_mode );
54 :
55 :
56 : /*-------------------------------------------------------------*
57 : * stereo_tcx_init_dec()
58 : *
59 : * Initialize stereo TCX decoder
60 : *-------------------------------------------------------------*/
61 :
62 2806374 : void stereo_tcx_init_dec(
63 : Decoder_State *st, /* i/o: decoder state structure */
64 : const int16_t MCT_flag, /* i : hMCT handle allocated (1) or not (0)*/
65 : const int16_t last_element_mode /* i : element mode of previous frame */
66 : )
67 : {
68 2806374 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
69 2806374 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
70 :
71 2806374 : st->rate_switching_init = 0;
72 2806374 : st->m_frame_type = ACTIVE_FRAME;
73 2806374 : st->core_brate = st->total_brate;
74 :
75 : /*sampling rate*/
76 2806374 : st->sr_core = getCoreSamplerateMode2( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, st->bwidth, st->flag_ACELP16k, st->rf_flag, st->is_ism_format );
77 2806374 : st->fscale = sr2fscale( st->sr_core );
78 :
79 : /*frame size*/
80 2806374 : st->L_frame = (int16_t) ( st->sr_core / FRAMES_PER_SEC );
81 2806374 : hTcxDec->L_frameTCX = (int16_t) ( st->output_Fs / FRAMES_PER_SEC );
82 :
83 2806374 : if ( ( st->L_frame == L_FRAME16k && ( st->bits_frame_nominal * FRAMES_PER_SEC ) <= MAX_ACELP_BRATE ) || ( st->tcxonly && ( st->sr_core == 32000 || st->sr_core == INT_FS_16k ) ) )
84 : {
85 1753731 : st->nb_subfr = NB_SUBFR16k;
86 : }
87 : else
88 : {
89 1052643 : st->nb_subfr = NB_SUBFR;
90 : }
91 :
92 : /*TCX tools*/
93 2806374 : st->hTcxCfg->ctx_hm = getCtxHm( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, st->rf_flag );
94 2806374 : st->hTcxCfg->resq = getResq( st->bits_frame_nominal * FRAMES_PER_SEC );
95 2806374 : hTcxDec->tcx_lpc_shaped_ari = getTcxLpcShapedAri( st->bits_frame_nominal * FRAMES_PER_SEC, st->rf_flag, st->element_mode );
96 2806374 : st->igf = getIgfPresent( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, st->bwidth, st->rf_flag );
97 :
98 2806374 : if ( st->element_mode != EVS_MONO )
99 : {
100 2806374 : st->hTcxCfg->fIsTNSAllowed = getTnsAllowed( st->bits_frame_nominal * FRAMES_PER_SEC, st->igf, st->element_mode );
101 : }
102 2806374 : if ( hTcxLtpDec != NULL )
103 : {
104 2806374 : hTcxLtpDec->tcxltp = getTcxLtp( st->sr_core );
105 : }
106 :
107 2806374 : if ( st->element_mode == IVAS_SCE )
108 : {
109 660345 : st->tcxonly = getTcxonly( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, MCT_flag, st->is_ism_format );
110 :
111 : /* LPC quantization */
112 660345 : if ( st->sr_core <= INT_FS_16k && st->tcxonly == 0 )
113 : {
114 339603 : st->lpcQuantization = 1;
115 : }
116 : else
117 : {
118 320742 : st->lpcQuantization = 0;
119 : }
120 :
121 660345 : if ( st->tcxonly == 0 )
122 : {
123 339603 : st->numlpc = 1;
124 : }
125 : else
126 : {
127 320742 : st->numlpc = 2;
128 : }
129 : }
130 :
131 2806374 : if ( ( st->bits_frame_nominal != st->last_bits_frame_nominal ) ||
132 2761698 : ( st->bwidth != st->last_bwidth ) ||
133 2755680 : ( st->last_core != TCX_20_CORE && st->last_core != TCX_10_CORE && !( st->prev_bfi == 1 && st->last_core == ACELP_CORE && st->last_con_tcx == 1 ) ) ||
134 2732484 : ( st->idchan == 1 && st->element_mode == IVAS_CPE_MDCT && last_element_mode != IVAS_CPE_MDCT ) )
135 : {
136 : /*re-initialization*/
137 74091 : stereo_tcx_dec_mode_switch_reconf( st, MCT_flag, last_element_mode );
138 : }
139 :
140 2806374 : return;
141 : }
142 :
143 :
144 : /*-------------------------------------------------------------------*
145 : * stereo_tcx_core_dec()
146 : *
147 : * stereo TCX decoder
148 : *-------------------------------------------------------------------*/
149 :
150 704082 : void stereo_tcx_core_dec(
151 : Decoder_State *st, /* i/o: decoder state structure */
152 : const FRAME_MODE frameMode, /* i : Decoder frame mode */
153 : float *signal_out, /* o : synthesis @internal_Fs */
154 : float *signal_outFB, /* o : synthesis @output_Fs */
155 : float pitch_buf[], /* o : floating pitch for each subframe */
156 : const int16_t sba_dirac_stereo_flag, /* i : signal stereo output for SBA DirAC */
157 : STEREO_TD_DEC_DATA_HANDLE hStereoTD, /* i/o: TD stereo decoder handle */
158 : const int16_t last_element_mode, /* i : last element mode */
159 : const int16_t flag_sec_CNA, /* i : CNA flag for secondary channel */
160 : STEREO_CNG_DEC_HANDLE hStereoCng, /* i : Stereo CNG handle */
161 : const int16_t nchan_out, /* i : number of output channels */
162 : const IVAS_FORMAT ivas_format /* i : IVAS format */
163 : )
164 : {
165 : int16_t i, k;
166 :
167 : /*Bitstream*/
168 : int16_t total_nbbits, bitsRead;
169 : int16_t *prm, param[DEC_NPRM_DIV * NB_DIV];
170 : int16_t param_lpc[NPRM_LPC_NEW];
171 :
172 : /*LPC*/
173 : int16_t LSF_Q_prediction; /* o : LSF prediction mode */
174 : float lsf[( NB_DIV + 1 ) * M], lsp[( NB_DIV + 1 ) * M], lspmid[M], lsfmid[M];
175 : float lspnew_uw[NB_DIV * M], lsfnew_uw[NB_DIV * M];
176 : float Aq[( NB_SUBFR16k + 1 ) * ( M + 1 )];
177 : int16_t pitch[NB_SUBFR16k];
178 : float pit_gain[NB_SUBFR16k];
179 : Word16 Aind[M + 1], lspind[M];
180 :
181 : /*Synth*/
182 : float synth_buf[OLD_SYNTH_INTERNAL_DEC + L_FRAME_PLUS_INTERNAL + M];
183 : float *synth;
184 : float synth_bufFB[OLD_SYNTH_SIZE_DEC + L_FRAME_PLUS + M];
185 : float *synthFB;
186 :
187 : /*Concealment*/
188 : int16_t bfi;
189 :
190 : float psd[L_FRAME16k], psd_part[NPART_SHAPING];
191 :
192 704082 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
193 704082 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
194 :
195 704082 : push_wmops( "stereo_tcx_core_dec" );
196 :
197 : /*Sanity check*/
198 704082 : assert( !( st->total_brate == FRAME_NO_DATA || st->total_brate == SID_2k40 ) ); /*Active frame*/
199 704082 : assert( st->rf_flag == 0 );
200 704082 : assert( st->prev_use_partial_copy == 0 ); /* No channel aware mode */
201 704082 : assert( st->codec_mode == MODE1 ); /* must be in Mode 1 */
202 704082 : assert( st->mdct_sw == MODE1 ); /* must be switched to TCX in MODE1 */
203 :
204 704082 : bfi = 0;
205 :
206 : /*--------------------------------------------------------------------------------*
207 : * Initialization or re-configuration of Stereo TCX
208 : *--------------------------------------------------------------------------------*/
209 :
210 704082 : stereo_tcx_init_dec( st, 0, last_element_mode );
211 :
212 : /*--------------------------------------------------------------------------------*
213 : * Initializations
214 : *--------------------------------------------------------------------------------*/
215 :
216 704082 : total_nbbits = (int16_t) ( st->total_brate / FRAMES_PER_SEC );
217 704082 : bitsRead = 0;
218 704082 : LSF_Q_prediction = -1; /* to avoid compilation warnings */
219 :
220 704082 : if ( frameMode == FRAMEMODE_NORMAL )
221 : {
222 690318 : st->m_decodeMode = DEC_NO_FRAM_LOSS;
223 690318 : bfi = 0;
224 : }
225 :
226 704082 : if ( frameMode == FRAMEMODE_MISSING )
227 : {
228 13764 : st->m_decodeMode = DEC_CONCEALMENT_EXT;
229 13764 : bfi = 1;
230 : }
231 :
232 : /* for bass postfilter */
233 704082 : if ( st->tcxonly )
234 : {
235 320742 : st->p_bpf_noise_buf = NULL;
236 : }
237 : else
238 : {
239 383340 : st->p_bpf_noise_buf = st->bpf_noise_buf;
240 383340 : set_s( pitch, L_SUBFR, st->nb_subfr );
241 383340 : set_zero( pit_gain, st->nb_subfr );
242 : }
243 :
244 : /* Initialize pointers */
245 704082 : synth = synth_buf + hTcxDec->old_synth_len;
246 704082 : synthFB = synth_bufFB + hTcxDec->old_synth_lenFB;
247 704082 : mvr2r( hTcxDec->old_synth, synth_buf, hTcxDec->old_synth_len );
248 704082 : mvr2r( hTcxDec->old_synthFB, synth_bufFB, hTcxDec->old_synth_lenFB );
249 704082 : set_zero( synth, L_FRAME_PLUS_INTERNAL + M );
250 704082 : set_zero( synthFB, L_FRAME_PLUS + M );
251 :
252 : #ifdef DEBUG_MODE_INFO_PLC
253 : dbgwrite( synth_buf, sizeof( float ), OLD_SYNTH_SIZE_DEC + L_FRAME_PLUS + M, 1, "res/synth_buf_init" );
254 : dbgwrite( synth_bufFB, sizeof( float ), OLD_SYNTH_SIZE_DEC + L_FRAME_PLUS + M, 1, "res/synthFB_buf_init" );
255 :
256 : #endif
257 : /*--------------------------------------------------------------------------------*
258 : * BITSTREAM DECODING
259 : *--------------------------------------------------------------------------------*/
260 :
261 704082 : if ( !bfi )
262 : {
263 690318 : st->second_last_core = st->last_core;
264 :
265 690318 : dec_prm_tcx( st, param, param_lpc, &total_nbbits, last_element_mode, &bitsRead );
266 : }
267 : else
268 : {
269 13764 : if ( st->nbLostCmpt > 1 )
270 : {
271 4131 : st->flagGuidedAcelp = 0;
272 : }
273 :
274 : /* PLC: [Common: mode decision]
275 : * PLC: Decide which Concealment to use. Update pitch lags if needed */
276 13764 : st->core = GetPLCModeDecision( st );
277 : }
278 :
279 : /*--------------------------------------------------------------------------------*
280 : * LPC PARAMETERS
281 : *--------------------------------------------------------------------------------*/
282 :
283 704082 : st->acelp_cfg.midLpc = 0;
284 :
285 704082 : if ( !bfi )
286 : {
287 690318 : if ( hTcxDec->enableTcxLpc )
288 : {
289 : int16_t tcx_lpc_cdk;
290 :
291 50958 : if ( bfi && st->use_partial_copy && st->rf_frame_type == RF_TCXFD )
292 : {
293 0 : tcx_lpc_cdk = tcxlpc_get_cdk( GENERIC );
294 : }
295 : else
296 : {
297 50958 : tcx_lpc_cdk = tcxlpc_get_cdk( st->coder_type );
298 : }
299 :
300 50958 : mvr2r( st->lsf_old, &lsf[0], M );
301 50958 : mvr2r( st->lsp_old, &lsp[0], M );
302 :
303 50958 : D_lsf_tcxlpc( param_lpc, &lsf[M], lspind, st->narrowBand, tcx_lpc_cdk, st->mem_MA );
304 :
305 50958 : lsf2lsp( &lsf[M], &lsp[M], M, st->sr_core );
306 :
307 50958 : lsf_update_memory( st->narrowBand, &lsf[M], st->mem_MA, st->mem_MA );
308 50958 : mvr2r( &lsf[M], st->mem_AR, M );
309 50958 : hTcxDec->envWeighted = 1;
310 :
311 50958 : E_LPC_lsp_unweight( &lsp[M], lspnew_uw, lsfnew_uw, 1.0f / st->gamma );
312 : }
313 : else
314 : {
315 639360 : if ( hTcxDec->envWeighted )
316 : {
317 0 : mvr2r( st->lspold_uw, st->lsp_old, M );
318 0 : mvr2r( st->lsfold_uw, st->lsf_old, M );
319 0 : hTcxDec->envWeighted = 0;
320 : }
321 :
322 639360 : lpc_unquantize( st, lsf, lsp, param_lpc, lspmid, lsfmid, AUDIO, &LSF_Q_prediction );
323 :
324 1284831 : for ( k = 0; k < st->numlpc; ++k )
325 : {
326 645471 : mvr2r( &lsp[( k + 1 ) * M], &lspnew_uw[k * M], M );
327 645471 : mvr2r( &lsf[( k + 1 ) * M], &lsfnew_uw[k * M], M );
328 : }
329 : }
330 :
331 : /* PLC: [LPD: LPC concealment] built the moving average for the LPC concealment */
332 1386747 : for ( k = 0; k < st->numlpc; k++ )
333 : {
334 11839293 : for ( i = 0; i < M; i++ )
335 : {
336 11142864 : st->lsf_adaptive_mean[i] = ( st->lsfoldbfi1[i] + st->lsfoldbfi0[i] + lsfnew_uw[k * M + i] ) / 3;
337 11142864 : st->lsfoldbfi1[i] = st->lsfoldbfi0[i];
338 11142864 : st->lsfoldbfi0[i] = lsfnew_uw[k * M + i];
339 : }
340 : }
341 : }
342 : else
343 : {
344 : /* PLC: [LPD: LPC concealment] Conceal the LPC from the lost frame */
345 : const float *lsfBase; /* base for differential lsf coding */
346 :
347 13764 : if ( st->tcxonly == 0 || st->core < TCX_10_CORE )
348 : {
349 13671 : st->numlpc = 1;
350 : }
351 : else
352 : {
353 93 : st->numlpc = 2;
354 : }
355 :
356 13764 : if ( st->nbLostCmpt == 1 )
357 : {
358 9633 : mvr2r( st->lsf_old, st->old_lsf_q_cng, M );
359 9633 : mvr2r( st->lsp_old, st->old_lsp_q_cng, M );
360 : }
361 :
362 13764 : lsfBase = PlcGetlsfBase( st->lpcQuantization, st->narrowBand, st->sr_core );
363 :
364 13764 : dlpc_bfi( st->L_frame, lsfnew_uw, st->lsfold_uw, st->last_good, st->nbLostCmpt, st->mem_MA, st->mem_AR, &( st->stab_fac ), st->lsf_adaptive_mean, st->numlpc, st->lsf_cng, st->plcBackgroundNoiseUpdated, st->lsf_q_cng, st->old_lsf_q_cng, lsfBase );
365 :
366 13764 : hTcxDec->envWeighted = 0;
367 :
368 13764 : mvr2r( st->lspold_uw, lsp, M );
369 13764 : mvr2r( st->lsfold_uw, lsf, M );
370 :
371 27621 : for ( k = 0; k < st->numlpc; k++ )
372 : {
373 13857 : mvr2r( &lsfnew_uw[k * M], &lsf[( k + 1 ) * M], M );
374 :
375 13857 : lsf2lsp( &lsf[( k + 1 ) * M], &lsp[( k + 1 ) * M], M, st->sr_core );
376 13857 : lsf2lsp( st->lsf_q_cng, st->lsp_q_cng, M, st->sr_core );
377 :
378 13857 : mvr2r( &lsp[( k + 1 ) * M], &lspnew_uw[k * M], M );
379 : }
380 : }
381 :
382 : /*---------------------------------------------------------------*
383 : * Rate switching
384 : *---------------------------------------------------------------*/
385 :
386 704082 : if ( st->rate_switching_reset )
387 : {
388 4638 : mvr2r( &( lsf[M] ), &( lsf[0] ), M );
389 4638 : mvr2r( &( lsp[M] ), &( lsp[0] ), M );
390 4638 : mvr2r( &( lsf[M] ), st->lsf_old, M );
391 4638 : mvr2r( &( lsp[M] ), st->lsp_old, M );
392 4638 : mvr2r( &( lsf[M] ), lsfmid, M );
393 4638 : mvr2r( &( lsp[M] ), lspmid, M );
394 4638 : lsp2a_stab( st->lsp_old, st->old_Aq_12_8, M );
395 : }
396 :
397 704082 : if ( st->enablePlcWaveadjust && bfi )
398 : {
399 0 : st->hPlcInfo->nbLostCmpt++;
400 : }
401 :
402 : /*--------------------------------------------------------------------------------*
403 : * TD-TCX concealment
404 : *--------------------------------------------------------------------------------*/
405 :
406 704082 : if ( st->core == ACELP_CORE )
407 : {
408 4563 : assert( bfi ); /*must only happen in BFI*/
409 :
410 4563 : if ( !st->tcxonly )
411 : {
412 : /*LPC interpolation*/
413 3204 : int_lsp( st->L_frame, &lsp[0], &lsp[M], Aq, M, interpol_frac_12k8, 0 );
414 3204 : int_lsp( st->L_frame, st->old_lsp_q_cng, st->lsp_q_cng, st->Aq_cng, M, interpol_frac_12k8, 0 );
415 : }
416 :
417 : /* PLC: [TCX: TD PLC] */
418 4563 : con_tcx( st, &synthFB[0], -1.f, NULL, 0, NULL );
419 4563 : lerp( synthFB, synth, st->L_frame, hTcxDec->L_frameTCX );
420 4563 : st->con_tcx = 1;
421 4563 : set_f( &st->mem_pitch_gain[2], st->lp_gainp, st->nb_subfr );
422 :
423 : /* LPC for ACELP/BWE */
424 4563 : if ( st->narrowBand || st->sr_core == INT_FS_12k8 || st->sr_core == INT_FS_16k )
425 : {
426 3204 : mvr2r( Aq, st->mem_Aq, st->nb_subfr * ( M + 1 ) );
427 : }
428 :
429 : /* PLC: [TCX: Tonal Concealment] */
430 : /* Signal that this frame is not TCX */
431 4563 : if ( st->hTonalMDCTConc != NULL )
432 : {
433 4563 : TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, 0, 0, 0, 0 );
434 : }
435 : }
436 :
437 : /*--------------------------------------------------------------------------------*
438 : * TCX20
439 : *--------------------------------------------------------------------------------*/
440 :
441 : /* Set pointer to parameters */
442 704082 : prm = param;
443 704082 : if ( st->core == TCX_20_CORE )
444 : {
445 : /* Stability Factor */
446 693315 : if ( !bfi )
447 : {
448 684207 : st->stab_fac = lsf_stab( &lsf[M], &lsf[0], 0, st->L_frame );
449 : }
450 :
451 693315 : if ( hTcxDec->enableTcxLpc )
452 : {
453 : /* Convert quantized lsp to A */
454 52128 : lsp2a_stab( &lsp[M], Aq, M );
455 : }
456 : else
457 : {
458 641187 : if ( !st->tcxonly )
459 : {
460 328008 : if ( !bfi && st->prev_bfi && !( st->safety_net ) && st->rate_switching_reset )
461 : {
462 : /* diffuse LPC power on rate switching*/
463 75 : RecLpcSpecPowDiffuseLc( &lsp[M], &lsp[0], &lsf[M], st, 0 );
464 75 : int_lsp( st->L_frame, &lsp[0], &lsp[M], Aq, M, interpol_frac_12k8, 0 );
465 75 : mvr2r( &lsf[M], lsfnew_uw, M );
466 : }
467 : else
468 : {
469 : /* LPC Interpolation for TCX */
470 327933 : E_LPC_int_lpc_tcx( &lsp[0], &lsp[M], Aq );
471 : }
472 : }
473 : else
474 : {
475 313179 : lsp2a_stab( &lsp[M], Aq, M );
476 : }
477 : }
478 :
479 693315 : if ( !bfi && hTcxDec->tcx_lpc_shaped_ari )
480 : {
481 50958 : basop_E_LPC_f_lsp_a_conversion( lspind, Aind, M );
482 : }
483 :
484 : /* TCX decoder */
485 693315 : decoder_tcx( st, prm, Aq, Aind, &synth[0], &synthFB[0], bfi, 0, sba_dirac_stereo_flag );
486 : }
487 :
488 : /*--------------------------------------------------------------------------------*
489 : * TCX10
490 : *--------------------------------------------------------------------------------*/
491 :
492 704082 : if ( st->core == TCX_10_CORE )
493 : {
494 6204 : prm = NULL; /* just to avoid MSVC warnings */
495 :
496 18612 : for ( k = 0; k < 2; k++ )
497 : {
498 : /* Set pointer to parameters */
499 12408 : prm = param + ( k * DEC_NPRM_DIV );
500 :
501 : /* Stability Factor */
502 12408 : if ( !bfi )
503 : {
504 12222 : st->stab_fac = lsf_stab( &lsf[( k + 1 ) * M], &lsf[k * M], 0, st->L_frame );
505 : }
506 :
507 12408 : lsp2a_stab( &lsp[( k + 1 ) * M], Aq, M );
508 : {
509 12408 : IGFDecRestoreTCX10SubFrameData( st->hIGFDec, k );
510 : }
511 :
512 : /* TCX decoder */
513 12408 : decoder_tcx( st, prm, Aq, Aind, &synth[k * st->L_frame / 2], &synthFB[k * hTcxDec->L_frameTCX / 2], bfi, k, sba_dirac_stereo_flag );
514 : }
515 : }
516 :
517 : #ifdef DEBUG_MODE_TCX
518 : {
519 : int16_t tmp[L_FRAME48k];
520 :
521 : for ( i = 0; i < st->L_frame; i++ )
522 : {
523 : tmp[i] = (int16_t) ( synth[i] + 0.5f );
524 : }
525 : dbgwrite( tmp, sizeof( int16_t ), st->L_frame, 1, "./res/stereo_tcx_dec_synth.pcm" );
526 : }
527 : #endif
528 :
529 : /*--------------------------------------------------------------------------------*
530 : * Post-processing
531 : *--------------------------------------------------------------------------------*/
532 :
533 704082 : if ( st->core == TCX_10_CORE || st->core == TCX_20_CORE )
534 : {
535 699519 : if ( st->enablePlcWaveadjust || /* bfi */
536 699519 : ( st->last_total_brate >= HQ_48k && /* recovery */
537 297630 : st->last_codec_mode == MODE2 ) )
538 : {
539 : /* waveform adjustment */
540 0 : concealment_signal_tuning( st, bfi, synthFB, st->last_core_bfi );
541 :
542 0 : if ( ( bfi || st->prev_bfi ) && st->hPlcInfo->Pitch && st->hPlcInfo->concealment_method == TCX_NONTONAL )
543 : {
544 0 : lerp( synthFB, synth, st->L_frame, hTcxDec->L_frameTCX );
545 :
546 0 : if ( !bfi && st->prev_bfi )
547 : {
548 0 : st->hPlcInfo->Pitch = 0;
549 : }
550 : }
551 : }
552 :
553 699519 : if ( !bfi && st->hTonalMDCTConc != NULL )
554 : {
555 690318 : TonalMDCTConceal_SaveTimeSignal( st->hTonalMDCTConc, synthFB, hTcxDec->L_frameTCX );
556 : }
557 :
558 699519 : decoder_tcx_post( st, synth, synthFB, Aq, bfi, 0 );
559 :
560 699519 : if ( st->core == TCX_20_CORE )
561 : {
562 : /* LPC Interpolation for BWE/post-processing */
563 693315 : if ( st->narrowBand || st->sr_core == INT_FS_12k8 || st->sr_core == INT_FS_16k )
564 : {
565 381594 : int_lsp( st->L_frame, st->lspold_uw, lspnew_uw, Aq, M, interpol_frac_12k8, 0 );
566 381594 : mvr2r( Aq, st->mem_Aq, st->nb_subfr * ( M + 1 ) );
567 : }
568 : }
569 : }
570 :
571 : /* PLC: [Common: Classification] */
572 : /* the classifier buffer is always updated if the sr is at
573 : 16000 or below - the classification itself is just performed if(!st->tcxonly ) */
574 704082 : if ( st->sr_core <= INT_FS_16k )
575 : {
576 384840 : if ( st->core == TCX_20_CORE || st->core == TCX_10_CORE || ( st->tcxonly && st->bfi ) )
577 : {
578 : float pitch_C[4];
579 :
580 : /* note: the classifier needs the pitch only for tcx_only == 0, i.e. not for TCX10 */
581 381636 : set_f( pitch_C, floorf( st->old_fpitch + 0.5f ), 4 );
582 :
583 : /* note: codec_mode is forced to MODE2, since FEC_clas_estim() considers only TCX being in Mode2*/
584 381636 : FEC_clas_estim( synth, pitch_C, st->L_frame, st->tcxonly ? GENERIC : st->core_ext_mode, MODE2, st->mem_syn_clas_estim, &st->clas_dec, &st->lp_ener_bfi, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, hTcxLtpDec->tcxltp ? hTcxDec->tcxltp_last_gain_unmodified : -1.0f, st->narrowBand, CLASSIFIER_TCX, bfi, st->preemph_fac, st->tcxonly, st->last_core_brate, -1 );
585 : }
586 : }
587 :
588 : /*--------------------------------------------------------------------------------*
589 : * Updates
590 : *--------------------------------------------------------------------------------*/
591 :
592 704082 : if ( bfi && st->last_core != ACELP_CORE )
593 : {
594 : /* Update FEC_scale_syn parameters */
595 13764 : if ( hTcxLtpDec->tcxltp_gain == 0 )
596 : {
597 8250 : fer_energy( st->L_frame, UNVOICED, synth, (float) ( st->L_frame / 2 ), &st->enr_old, st->L_frame );
598 : }
599 : else
600 : {
601 5514 : fer_energy( st->L_frame, st->clas_dec, synth, st->old_fpitch, &st->enr_old, st->L_frame );
602 : }
603 : }
604 :
605 704082 : if ( !bfi && st->clas_dec >= VOICED_TRANSITION && st->clas_dec < INACTIVE_CLAS )
606 : {
607 : /* use latest LPC set */
608 425190 : st->old_enr_LP = enr_1_Az( Aq, L_SUBFR );
609 : }
610 :
611 : /* Update past buffers */
612 704082 : mvr2r( synth_buf + st->L_frame, hTcxDec->old_synth, hTcxDec->old_synth_len );
613 704082 : mvr2r( hTcxDec->old_synthFB + hTcxDec->L_frameTCX - NS2SA( st->output_Fs, PH_ECU_MEM_NS ), hTcxDec->synth_history, NS2SA( st->output_Fs, PH_ECU_MEM_NS ) );
614 704082 : mvr2r( synth_bufFB + hTcxDec->L_frameTCX, hTcxDec->old_synthFB, hTcxDec->old_synth_lenFB );
615 704082 : mvr2r( st->hHQ_core->old_out + NS2SA( st->output_Fs, N_ZERO_MDCT_NS ), hTcxDec->old_synthFB + hTcxDec->old_synth_lenFB, NS2SA( st->output_Fs, PH_ECU_LOOKAHEAD_NS ) );
616 :
617 704082 : mvr2r( &lspnew_uw[( st->numlpc - 1 ) * M], st->lspold_uw, M );
618 704082 : mvr2r( &lsfnew_uw[( st->numlpc - 1 ) * M], st->lsfold_uw, M );
619 :
620 704082 : if ( bfi == 1 )
621 : {
622 13764 : mvr2r( st->lspold_uw, st->lsp_old, M ); /* for recovery */
623 13764 : mvr2r( st->lsfold_uw, st->lsf_old, M ); /* for recovery */
624 : }
625 : else
626 : {
627 690318 : mvr2r( &lsp[st->numlpc * M], st->lsp_old, M );
628 690318 : mvr2r( &lsf[st->numlpc * M], st->lsf_old, M );
629 : }
630 704082 : mvr2r( st->lsp_q_cng, st->old_lsp_q_cng, M );
631 704082 : mvr2r( st->lsf_q_cng, st->old_lsf_q_cng, M );
632 :
633 : /* Update CNG parameters */
634 704082 : if ( !st->tcxonly && st->hTdCngDec != NULL )
635 : {
636 : /* update CNG parameters in active frames */
637 236196 : if ( st->bwidth == NB && hTcxDec->enableTcxLpc && st->core != ACELP_CORE )
638 0 : {
639 : float buf[L_LP], res[L_FRAME], A[M + 1], r[M + 1], tmp, lsptmp[M];
640 0 : assert( st->L_frame == L_FRAME );
641 :
642 0 : mvr2r( synth + L_FRAME - L_LP, buf, L_LP );
643 0 : tmp = synth[L_FRAME - L_LP - 1];
644 0 : preemph( buf, st->preemph_fac, L_LP, &tmp );
645 0 : autocorr( buf, r, M, L_LP, LP_assym_window, 0, 0, 0 );
646 0 : lag_wind( r, M, INT_FS_12k8, LAGW_WEAK );
647 0 : lev_dur( A, r, M, NULL );
648 0 : a2lsp_stab( A, lsptmp, &lspnew_uw[0] );
649 0 : residu( A, M, buf + L_LP - L_FRAME, res, L_FRAME );
650 :
651 0 : if ( st->hTdCngDec != NULL )
652 : {
653 0 : cng_params_upd( lsptmp, res, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ, DEC, st->hTdCngDec->ho_env_circ, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth );
654 : }
655 : }
656 236196 : else if ( st->hTdCngDec != NULL )
657 : {
658 236196 : cng_params_upd( &lsp[M], st->old_exc + L_EXC_MEM_DEC - st->L_frame, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ, DEC, st->hTdCngDec->ho_env_circ, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth );
659 : }
660 :
661 : /* Set 16k LSP flag for CNG buffer */
662 236196 : st->hTdCngDec->ho_16k_lsp[st->hTdCngDec->ho_circ_ptr] = ( st->L_frame == L_FRAME ? 0 : 1 );
663 : }
664 704082 : st->last_is_cng = 0;
665 :
666 : /* Postfiltering */
667 704082 : post_decoder( st, synth_buf, pit_gain, pitch, signal_out, st->p_bpf_noise_buf );
668 :
669 704082 : if ( signal_outFB )
670 : {
671 704082 : mvr2r( synthFB, signal_outFB, hTcxDec->L_frameTCX );
672 : }
673 :
674 704082 : if ( !bfi )
675 : {
676 690318 : if ( st->enablePlcWaveadjust )
677 : {
678 0 : st->hPlcInfo->nbLostCmpt = 0;
679 : }
680 :
681 690318 : if ( param[1 + NOISE_FILL_RANGES] != 0 )
682 : {
683 342108 : set_f( pitch_buf, hTcxLtpDec->tcxltp_pitch_int + (float) hTcxLtpDec->tcxltp_pitch_fr / (float) st->pit_res_max, NB_SUBFR16k );
684 : }
685 : else
686 : {
687 348210 : set_f( pitch_buf, L_SUBFR, NB_SUBFR16k );
688 : }
689 : }
690 :
691 704082 : if ( bfi )
692 : {
693 : /*"LPD dec - All BFI"*/
694 13764 : bitsRead = 0; /*to avoid empty counting */
695 : }
696 :
697 : /* updates */
698 704082 : st->last_voice_factor = 0.0f;
699 704082 : st->last_coder_type = st->coder_type;
700 :
701 : /* -------------------------------------------------------------- *
702 : * In FFT domain: perform noise estimation during active frames
703 : * -------------------------------------------------------------- */
704 :
705 704082 : if ( st->hFdCngDec != NULL && ( st->sr_core == INT_FS_12k8 || st->sr_core == INT_FS_16k ) && st->total_brate <= MAX_ACELP_BRATE )
706 : {
707 376149 : noisy_speech_detection( st->hFdCngDec, st->VAD && st->m_frame_type == ACTIVE_FRAME, signal_out );
708 :
709 376149 : st->hFdCngDec->hFdCngCom->likelihood_noisy_speech = 0.99f * st->hFdCngDec->hFdCngCom->likelihood_noisy_speech + 0.01f * (float) st->hFdCngDec->hFdCngCom->flag_noisy_speech;
710 :
711 376149 : st->lp_noise = st->hFdCngDec->lp_noise;
712 :
713 376149 : if ( st->element_mode != IVAS_CPE_TD )
714 : {
715 375660 : if ( ivas_format == ISM_FORMAT )
716 : {
717 : float buffer[L_FRAME16k];
718 232107 : lerp( signal_outFB, buffer, st->L_frame, hTcxDec->L_frameTCX );
719 232107 : ApplyFdCng( buffer, NULL, NULL, NULL, st, st->bfi, 0 );
720 : }
721 : else
722 : {
723 143553 : ApplyFdCng( signal_out, NULL, NULL, NULL, st, st->bfi, 0 );
724 : }
725 : }
726 :
727 : /* Generate additional comfort noise to mask potential coding artefacts */
728 376149 : if ( st->m_frame_type == ACTIVE_FRAME && st->flag_cna && !st->cna_dirac_flag )
729 : {
730 121728 : if ( st->element_mode == IVAS_CPE_TD && nchan_out == 2 )
731 : {
732 0 : if ( st->element_mode != last_element_mode )
733 : {
734 : /* Clear memory for secondary channel CNA */
735 0 : set_f( hStereoCng->olapBufferSynth22, 0.0f, st->hFdCngDec->hFdCngCom->frameSize / 2 );
736 : }
737 0 : expand_range( st->hFdCngDec->msPsd, psd_part, st->hFdCngDec->nFFTpart_shaping );
738 0 : scalebands( psd_part, st->hFdCngDec->part_shaping, st->hFdCngDec->nFFTpart_shaping, st->hFdCngDec->midband_shaping, st->hFdCngDec->nFFTpart_shaping, st->hFdCngDec->hFdCngCom->stopFFTbin - st->hFdCngDec->hFdCngCom->startBand, psd, 1 );
739 0 : generate_stereo_masking_noise( signal_out, st, hStereoTD, flag_sec_CNA, 0, hStereoCng, nchan_out );
740 : }
741 121728 : else if ( st->element_mode != IVAS_CPE_DFT )
742 : {
743 79041 : generate_masking_noise( signal_out, st->hFdCngDec->hFdCngCom, st->hFdCngDec->hFdCngCom->frameSize, 0, 0, 0, st->element_mode, hStereoCng, nchan_out );
744 : }
745 : }
746 :
747 376149 : if ( st->element_mode == IVAS_CPE_TD && st->idchan == 0 )
748 : {
749 489 : ApplyFdCng( signal_out, NULL, NULL, NULL, st, st->bfi, 0 );
750 : }
751 : }
752 :
753 : #ifdef DEBUG_MODE_TCX
754 : {
755 : int16_t tmp[L_FRAME48k];
756 : static FILE *sP = NULL;
757 :
758 : for ( i = 0; i < hTcxDec->L_frameTCX; i++ )
759 : {
760 : tmp[i] = (int16_t) ( signal_outFB[i] + 0.5f );
761 : }
762 : dbgwrite( tmp, sizeof( int16_t ), hTcxDec->L_frameTCX, 1, "./res/stereo_tcx_dec_synthFB.pcm" );
763 :
764 : for ( i = 0; i < hTcxDec->L_frameTCX; i++ )
765 : {
766 : tmp[i] = (int16_t) ( signal_out[i] + 0.5f );
767 : }
768 : dbgwrite( tmp, sizeof( int16_t ), hTcxDec->L_frameTCX, 1, "./res/stereo_tcx_dec_synthLB.pcm" );
769 :
770 : if ( sP == NULL )
771 : sP = fopen( "./res/stereo_tcx_core_dec_swicthes.txt", "w" );
772 :
773 : fprintf( sP, "frame:%d\t mdct_sw=%d\t rf_mode=%d tcxonly=%d\t tcxMode=%d\t core=%d\t, enableTcxLpc=%d\t igf=%d\t envWeighted=%d\t lpcQuantization=%d\t enablePlcWaveadjust=%d\t tcxltp=%d\t fIsTNSAllowed=%d\t tcx_lpc_shaped_ari=%d\t ctx_hm=%d\t \n", frame, st->mdct_sw, 0, st->tcxonly, 0, st->core, hTcxDec->enableTcxLpc, st->igf, hTcxDec->envWeighted, st->lpcQuantization, st->enablePlcWaveadjust, hTcxLtpDec->tcxltp, st->hTcxCfg->fIsTNSAllowed, 0, st->hTcxCfg->ctx_hm );
774 : }
775 : #endif
776 :
777 :
778 704082 : pop_wmops();
779 704082 : return;
780 : }
781 :
782 :
783 : /*-----------------------------------------------------------------*
784 : * Function dec_prm_tcx()
785 : *
786 : * Decode TCX parameters
787 : *-----------------------------------------------------------------*/
788 :
789 690318 : static void dec_prm_tcx(
790 : Decoder_State *st, /* i/o: decoder memory state */
791 : int16_t param[], /* o : decoded parameters */
792 : int16_t param_lpc[], /* o : LPC parameters */
793 : int16_t *total_nbbits, /* i/o: number of bits / decoded bits */
794 : const int16_t last_element_mode, /* i : last element mode */
795 : int16_t *bitsRead /* o : number of read bits */
796 : )
797 : {
798 : int16_t start_bit_pos, bits_common;
799 : CONTEXT_HM_CONFIG hm_cfg;
800 : int16_t indexBuffer[N_MAX + 1];
801 : #ifdef DEBUG_MODE_TCX
802 : int16_t nbits_tcx;
803 : static FILE *pF = NULL;
804 : if ( pF == NULL )
805 : pF = fopen( "./res/stereo_tcx_dec_ind.txt", "w" );
806 : #endif
807 :
808 690318 : assert( st->mdct_sw == MODE1 );
809 :
810 : /*--------------------------------------------------------------------------------*
811 : * INIT
812 : *--------------------------------------------------------------------------------*/
813 :
814 690318 : hm_cfg.indexBuffer = indexBuffer;
815 : #ifdef DEBUG_MODE_TCX
816 : fprintf( pF, "== stereo Chan %d - Nominal Bits %d - Allocated Bits %d ==\n", st->idchan, st->bits_frame_nominal, (int16_t) ( st->total_brate / FRAMES_PER_SEC ) );
817 : fprintf( pF, "stereo Common Header: %d bits\n", st->next_bit_pos );
818 : #endif
819 690318 : if ( st->element_mode != IVAS_CPE_MDCT )
820 : {
821 690318 : st->bits_frame_core = (int16_t) ( ( st->total_brate / FRAMES_PER_SEC ) - st->next_bit_pos );
822 : }
823 690318 : start_bit_pos = st->next_bit_pos;
824 :
825 : /* Init LTP data */
826 690318 : st->hTcxDec->tcx_hm_LtpPitchLag = -1;
827 690318 : st->hTcxLtpDec->tcxltp_gain = 0.0f;
828 :
829 : /*--------------------------------------------------------------------------------*
830 : * header
831 : *--------------------------------------------------------------------------------*/
832 :
833 : /* Modes (ACE_GC, ACE_UC, TCX20, TCX10...) */
834 690318 : getTCXMode( st, st, 0 /* <- MCT_flag */ );
835 :
836 : /* last_core for error concealment */
837 690318 : if ( !st->use_partial_copy && st->element_mode != IVAS_CPE_MDCT )
838 : {
839 690318 : st->last_core_from_bs = get_next_indice( st, 1 ); /* Store decoder memory of last_core */
840 690318 : if ( st->last_core == ACELP_CORE && st->last_core_from_bs != ACELP_CORE )
841 : {
842 : /* A mismatch between the memory and the last_core
843 : from the bitstream indicates a frame was lost. If prev_bfi is
844 : not set the frame loss occured during CNG and the prev_bfi needs to be set. */
845 294 : st->prev_bfi = 1;
846 : }
847 :
848 : /* possible need for reconfiguration can only be decided correctly once last_core_from_bs has been decoded */
849 690318 : if ( ( st->last_core != st->last_core_from_bs ) && ( st->last_core_from_bs != TCX_20_CORE && st->last_core_from_bs != TCX_10_CORE && !( st->prev_bfi == 1 && st->last_core_from_bs == ACELP_CORE && st->last_con_tcx == 1 ) ) )
850 : {
851 177 : stereo_tcx_dec_mode_switch_reconf( st, 0, last_element_mode );
852 : }
853 :
854 690318 : st->last_core = st->last_core_from_bs;
855 :
856 : /*for TCX 10 force last_core to be TCX since ACELP as previous core is forbidden*/
857 690318 : if ( st->core == TCX_10_CORE )
858 : {
859 6111 : st->last_core = TCX_20_CORE;
860 : }
861 : }
862 :
863 690318 : if ( !st->use_partial_copy )
864 : {
865 690318 : if ( st->element_mode != IVAS_CPE_MDCT )
866 : {
867 690318 : getTCXWindowing( st->core, st->last_core, st->element_mode, st->hTcxCfg, st );
868 : }
869 690318 : st->flagGuidedAcelp = 0;
870 :
871 690318 : if ( st->dec_glr )
872 : {
873 0 : st->dec_glr_idx = -1;
874 : }
875 : }
876 :
877 : #ifdef DEBUG_MODE_TCX
878 : fprintf( pF, "\t TCX Header: %d bits: %d %d %d %d\n", st->next_bit_pos - start_bit_pos, st->tcxonly, st->core, st->tcxonly ? st->clas_dec : st->hTcxCfg->coder_type, st->hTcxCfg->tcx_curr_overlap_mode );
879 : nbits_tcx = st->next_bit_pos;
880 : #endif
881 :
882 : /*--------------------------------------------------------------------------------*
883 : * LPC parameters
884 : *--------------------------------------------------------------------------------*/
885 :
886 690318 : getLPCparam( st, param_lpc, st, -1, 0 );
887 :
888 690318 : bits_common = st->next_bit_pos - start_bit_pos;
889 :
890 : #ifdef DEBUG_MODE_TCX
891 : fprintf( pF, "\t TCX LPC: %d bits\n", st->next_bit_pos - nbits_tcx );
892 : #endif
893 :
894 : /*--------------------------------------------------------------------------------*
895 : * TCX20/10 parameters
896 : *--------------------------------------------------------------------------------*/
897 :
898 690318 : if ( st->use_partial_copy == 0 )
899 : {
900 690318 : getTCXparam( st, st, hm_cfg, param, bits_common, start_bit_pos, NULL, NULL, NULL, -1 );
901 : }
902 :
903 690318 : if ( !st->use_partial_copy )
904 : {
905 690318 : if ( *total_nbbits - bitsRead[0] < ( st->next_bit_pos - start_bit_pos ) )
906 : {
907 0 : st->BER_detect = 1;
908 0 : st->next_bit_pos = start_bit_pos + *total_nbbits - bitsRead[0];
909 : }
910 :
911 690318 : bitsRead[0] = st->next_bit_pos - start_bit_pos;
912 : }
913 :
914 690318 : return;
915 : }
916 :
917 : /*-----------------------------------------------------------------*
918 : * Function stereo_tcx_dec_mode_switch_reconf()
919 : *
920 : * Reconfigure stereo TCX parameters
921 : *-----------------------------------------------------------------*/
922 :
923 74268 : static void stereo_tcx_dec_mode_switch_reconf(
924 : Decoder_State *st, /* i/o: decoder state structure */
925 : const int16_t MCT_flag, /* i : hMCT handle allocated (1) or not (0)*/
926 : const int16_t last_element_mode /* i : element mode of previous frame */
927 : )
928 : {
929 : int16_t frame_size_index;
930 :
931 74268 : st->rate_switching_init = 1;
932 :
933 : /* Identify frame type - TCX Reconfiguration */
934 690765 : for ( frame_size_index = 0; frame_size_index < FRAME_SIZE_NB; frame_size_index++ )
935 : {
936 690765 : if ( frame_size_index < FRAME_SIZE_NB - 1 )
937 : {
938 677169 : if ( ( FrameSizeConfig[frame_size_index].frame_bits <= st->bits_frame_nominal ) && ( FrameSizeConfig[frame_size_index + 1].frame_bits > st->bits_frame_nominal ) )
939 : {
940 60672 : break;
941 : }
942 : }
943 : else
944 : {
945 13596 : if ( FrameSizeConfig[frame_size_index].frame_bits <= st->bits_frame_nominal )
946 : {
947 13596 : break;
948 : }
949 : }
950 : }
951 :
952 : /* Reconfigure Core */
953 74268 : mode_switch_decoder_LPD( st, st->bwidth, st->bits_frame_nominal * FRAMES_PER_SEC, st->last_bits_frame_nominal * FRAMES_PER_SEC, frame_size_index, MCT_flag, last_element_mode );
954 :
955 74268 : return;
956 : }
|