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 15145280 : 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 15145280 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
69 15145280 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
70 :
71 15145280 : st->rate_switching_init = 0;
72 15145280 : st->m_frame_type = ACTIVE_FRAME;
73 15145280 : st->core_brate = st->total_brate;
74 :
75 : /*sampling rate*/
76 15145280 : 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 15145280 : st->fscale = sr2fscale( st->sr_core );
78 :
79 : /*frame size*/
80 15145280 : st->L_frame = (int16_t) ( st->sr_core / FRAMES_PER_SEC );
81 15145280 : hTcxDec->L_frameTCX = (int16_t) ( st->output_Fs / FRAMES_PER_SEC );
82 :
83 15145280 : 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 10197373 : st->nb_subfr = NB_SUBFR16k;
86 : }
87 : else
88 : {
89 4947907 : st->nb_subfr = NB_SUBFR;
90 : }
91 :
92 : /*TCX tools*/
93 15145280 : st->hTcxCfg->ctx_hm = getCtxHm( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, st->rf_flag );
94 15145280 : st->hTcxCfg->resq = getResq( st->bits_frame_nominal * FRAMES_PER_SEC );
95 15145280 : hTcxDec->tcx_lpc_shaped_ari = getTcxLpcShapedAri( st->bits_frame_nominal * FRAMES_PER_SEC, st->rf_flag, st->element_mode );
96 15145280 : st->igf = getIgfPresent( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, st->bwidth, st->rf_flag );
97 :
98 15145280 : if ( st->element_mode != EVS_MONO )
99 : {
100 15145280 : st->hTcxCfg->fIsTNSAllowed = getTnsAllowed( st->bits_frame_nominal * FRAMES_PER_SEC, st->igf, st->element_mode );
101 : }
102 15145280 : if ( hTcxLtpDec != NULL )
103 : {
104 15145280 : hTcxLtpDec->tcxltp = getTcxLtp( st->sr_core );
105 : }
106 :
107 15145280 : if ( st->element_mode == IVAS_SCE )
108 : {
109 3606665 : st->tcxonly = getTcxonly( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, MCT_flag, st->is_ism_format );
110 :
111 : /* LPC quantization */
112 3606665 : if ( st->sr_core <= INT_FS_16k && st->tcxonly == 0 )
113 : {
114 1966287 : st->lpcQuantization = 1;
115 : }
116 : else
117 : {
118 1640378 : st->lpcQuantization = 0;
119 : }
120 :
121 3606665 : if ( st->tcxonly == 0 )
122 : {
123 1966287 : st->numlpc = 1;
124 : }
125 : else
126 : {
127 1640378 : st->numlpc = 2;
128 : }
129 : }
130 :
131 15145280 : if ( ( st->bits_frame_nominal != st->last_bits_frame_nominal ) ||
132 14841224 : ( st->bwidth != st->last_bwidth ) ||
133 14835058 : ( 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 14729884 : ( st->idchan == 1 && st->element_mode == IVAS_CPE_MDCT && last_element_mode != IVAS_CPE_MDCT ) )
135 : {
136 : /*re-initialization*/
137 415606 : stereo_tcx_dec_mode_switch_reconf( st, MCT_flag, last_element_mode );
138 : }
139 :
140 15145280 : return;
141 : }
142 :
143 :
144 : /*-------------------------------------------------------------------*
145 : * stereo_tcx_core_dec()
146 : *
147 : * stereo TCX decoder
148 : *-------------------------------------------------------------------*/
149 :
150 3758762 : 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 3758762 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
193 3758762 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
194 :
195 3758762 : push_wmops( "stereo_tcx_core_dec" );
196 :
197 : /*Sanity check*/
198 3758762 : assert( !( st->total_brate == FRAME_NO_DATA || st->total_brate == SID_2k40 ) ); /*Active frame*/
199 3758762 : assert( st->rf_flag == 0 );
200 3758762 : assert( st->prev_use_partial_copy == 0 ); /* No channel aware mode */
201 3758762 : assert( st->codec_mode == MODE1 ); /* must be in Mode 1 */
202 3758762 : assert( st->mdct_sw == MODE1 ); /* must be switched to TCX in MODE1 */
203 :
204 3758762 : bfi = 0;
205 :
206 : /*--------------------------------------------------------------------------------*
207 : * Initialization or re-configuration of Stereo TCX
208 : *--------------------------------------------------------------------------------*/
209 :
210 3758762 : stereo_tcx_init_dec( st, 0, last_element_mode );
211 :
212 : /*--------------------------------------------------------------------------------*
213 : * Initializations
214 : *--------------------------------------------------------------------------------*/
215 :
216 3758762 : total_nbbits = (int16_t) ( st->total_brate / FRAMES_PER_SEC );
217 3758762 : bitsRead = 0;
218 3758762 : LSF_Q_prediction = -1; /* to avoid compilation warnings */
219 :
220 3758762 : if ( frameMode == FRAMEMODE_NORMAL )
221 : {
222 3443908 : st->m_decodeMode = DEC_NO_FRAM_LOSS;
223 3443908 : bfi = 0;
224 : }
225 :
226 3758762 : if ( frameMode == FRAMEMODE_MISSING )
227 : {
228 314854 : st->m_decodeMode = DEC_CONCEALMENT_EXT;
229 314854 : bfi = 1;
230 : }
231 :
232 : /* for bass postfilter */
233 3758762 : if ( st->tcxonly )
234 : {
235 1640378 : st->p_bpf_noise_buf = NULL;
236 : }
237 : else
238 : {
239 2118384 : st->p_bpf_noise_buf = st->bpf_noise_buf;
240 2118384 : set_s( pitch, L_SUBFR, st->nb_subfr );
241 2118384 : set_zero( pit_gain, st->nb_subfr );
242 : }
243 :
244 : /* Initialize pointers */
245 3758762 : synth = synth_buf + hTcxDec->old_synth_len;
246 3758762 : synthFB = synth_bufFB + hTcxDec->old_synth_lenFB;
247 3758762 : mvr2r( hTcxDec->old_synth, synth_buf, hTcxDec->old_synth_len );
248 3758762 : mvr2r( hTcxDec->old_synthFB, synth_bufFB, hTcxDec->old_synth_lenFB );
249 3758762 : set_zero( synth, L_FRAME_PLUS_INTERNAL + M );
250 3758762 : 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 3758762 : if ( !bfi )
262 : {
263 3443908 : st->second_last_core = st->last_core;
264 :
265 3443908 : dec_prm_tcx( st, param, param_lpc, &total_nbbits, last_element_mode, &bitsRead );
266 : }
267 : else
268 : {
269 314854 : if ( st->nbLostCmpt > 1 )
270 : {
271 194489 : st->flagGuidedAcelp = 0;
272 : }
273 :
274 : /* PLC: [Common: mode decision]
275 : * PLC: Decide which Concealment to use. Update pitch lags if needed */
276 314854 : st->core = GetPLCModeDecision( st );
277 : }
278 :
279 : /*--------------------------------------------------------------------------------*
280 : * LPC envelope decoding
281 : *--------------------------------------------------------------------------------*/
282 :
283 3758762 : st->acelp_cfg.midLpc = 0;
284 :
285 3758762 : if ( !bfi )
286 : {
287 3443908 : if ( hTcxDec->enableTcxLpc )
288 : {
289 : int16_t tcx_lpc_cdk;
290 :
291 162588 : tcx_lpc_cdk = tcxlpc_get_cdk( st->coder_type );
292 :
293 162588 : mvr2r( st->lsf_old, &lsf[0], M );
294 162588 : mvr2r( st->lsp_old, &lsp[0], M );
295 :
296 162588 : D_lsf_tcxlpc( param_lpc, &lsf[M], lspind, st->narrowBand, tcx_lpc_cdk, st->mem_MA );
297 :
298 162588 : lsf2lsp( &lsf[M], &lsp[M], M, st->sr_core );
299 :
300 162588 : lsf_update_memory( st->narrowBand, &lsf[M], st->mem_MA, st->mem_MA );
301 162588 : mvr2r( &lsf[M], st->mem_AR, M );
302 162588 : hTcxDec->envWeighted = 1;
303 :
304 162588 : E_LPC_lsp_unweight( &lsp[M], lspnew_uw, lsfnew_uw, 1.0f / st->gamma );
305 : }
306 : else
307 : {
308 3281320 : if ( hTcxDec->envWeighted )
309 : {
310 0 : mvr2r( st->lspold_uw, st->lsp_old, M );
311 0 : mvr2r( st->lsfold_uw, st->lsf_old, M );
312 0 : hTcxDec->envWeighted = 0;
313 : }
314 :
315 3281320 : lpc_unquantize( st, lsf, lsp, param_lpc, lspmid, lsfmid, AUDIO, &LSF_Q_prediction );
316 :
317 6599924 : for ( k = 0; k < st->numlpc; ++k )
318 : {
319 3318604 : mvr2r( &lsp[( k + 1 ) * M], &lspnew_uw[k * M], M );
320 3318604 : mvr2r( &lsf[( k + 1 ) * M], &lsfnew_uw[k * M], M );
321 : }
322 : }
323 :
324 : /* PLC: [LPD: LPC concealment] built the moving average for the LPC concealment */
325 6925100 : for ( k = 0; k < st->numlpc; k++ )
326 : {
327 59180264 : for ( i = 0; i < M; i++ )
328 : {
329 55699072 : st->lsf_adaptive_mean[i] = ( st->lsfoldbfi1[i] + st->lsfoldbfi0[i] + lsfnew_uw[k * M + i] ) / 3;
330 55699072 : st->lsfoldbfi1[i] = st->lsfoldbfi0[i];
331 55699072 : st->lsfoldbfi0[i] = lsfnew_uw[k * M + i];
332 : }
333 : }
334 : }
335 : else
336 : {
337 : /* PLC: [LPD: LPC concealment] Conceal the LPC from the lost frame */
338 : const float *lsfBase; /* base for differential lsf coding */
339 :
340 314854 : if ( st->tcxonly == 0 || st->core < TCX_10_CORE )
341 : {
342 314758 : st->numlpc = 1;
343 : }
344 : else
345 : {
346 96 : st->numlpc = 2;
347 : }
348 :
349 314854 : if ( st->nbLostCmpt == 1 )
350 : {
351 120365 : mvr2r( st->lsf_old, st->old_lsf_q_cng, M );
352 120365 : mvr2r( st->lsp_old, st->old_lsp_q_cng, M );
353 : }
354 :
355 314854 : lsfBase = PlcGetlsfBase( st->lpcQuantization, st->narrowBand, st->sr_core );
356 :
357 314854 : 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 );
358 :
359 314854 : hTcxDec->envWeighted = 0;
360 :
361 314854 : mvr2r( st->lspold_uw, lsp, M );
362 314854 : mvr2r( st->lsfold_uw, lsf, M );
363 :
364 629804 : for ( k = 0; k < st->numlpc; k++ )
365 : {
366 314950 : mvr2r( &lsfnew_uw[k * M], &lsf[( k + 1 ) * M], M );
367 :
368 314950 : lsf2lsp( &lsf[( k + 1 ) * M], &lsp[( k + 1 ) * M], M, st->sr_core );
369 314950 : lsf2lsp( st->lsf_q_cng, st->lsp_q_cng, M, st->sr_core );
370 :
371 314950 : mvr2r( &lsp[( k + 1 ) * M], &lspnew_uw[k * M], M );
372 : }
373 : }
374 :
375 : /*---------------------------------------------------------------*
376 : * Rate switching
377 : *---------------------------------------------------------------*/
378 :
379 3758762 : if ( st->rate_switching_reset )
380 : {
381 8879 : mvr2r( &( lsf[M] ), &( lsf[0] ), M );
382 8879 : mvr2r( &( lsp[M] ), &( lsp[0] ), M );
383 8879 : mvr2r( &( lsf[M] ), st->lsf_old, M );
384 8879 : mvr2r( &( lsp[M] ), st->lsp_old, M );
385 8879 : mvr2r( &( lsf[M] ), lsfmid, M );
386 8879 : mvr2r( &( lsp[M] ), lspmid, M );
387 8879 : lsp2a_stab( st->lsp_old, st->old_Aq_12_8, M );
388 : }
389 :
390 : /*--------------------------------------------------------------------------------*
391 : * TD-TCX concealment
392 : *--------------------------------------------------------------------------------*/
393 :
394 3758762 : if ( st->core == ACELP_CORE )
395 : {
396 100501 : assert( bfi ); /*must only happen in BFI*/
397 :
398 100501 : if ( !st->tcxonly )
399 : {
400 : /*LPC interpolation*/
401 62880 : int_lsp( st->L_frame, &lsp[0], &lsp[M], Aq, M, interpol_frac_12k8, 0 );
402 62880 : int_lsp( st->L_frame, st->old_lsp_q_cng, st->lsp_q_cng, st->Aq_cng, M, interpol_frac_12k8, 0 );
403 : }
404 :
405 : /* PLC: [TCX: TD PLC] */
406 100501 : con_tcx( st, &synthFB[0], -1.f, NULL, 0, NULL );
407 100501 : lerp( synthFB, synth, st->L_frame, hTcxDec->L_frameTCX );
408 100501 : st->con_tcx = 1;
409 100501 : set_f( &st->mem_pitch_gain[2], st->lp_gainp, st->nb_subfr );
410 :
411 : /* LPC for ACELP/BWE */
412 100501 : if ( st->narrowBand || st->sr_core == INT_FS_12k8 || st->sr_core == INT_FS_16k )
413 : {
414 74668 : mvr2r( Aq, st->mem_Aq, st->nb_subfr * ( M + 1 ) );
415 : }
416 :
417 : /* PLC: [TCX: Tonal Concealment] */
418 : /* Signal that this frame is not TCX */
419 100501 : if ( st->hTonalMDCTConc != NULL )
420 : {
421 100501 : TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, 0, 0, 0, 0 );
422 : }
423 : }
424 :
425 : /*--------------------------------------------------------------------------------*
426 : * TCX20
427 : *--------------------------------------------------------------------------------*/
428 :
429 : /* Set pointer to parameters */
430 3758762 : prm = param;
431 3758762 : if ( st->core == TCX_20_CORE )
432 : {
433 : /* Stability Factor */
434 3620881 : if ( !bfi )
435 : {
436 3406624 : st->stab_fac = lsf_stab( &lsf[M], &lsf[0], 0, st->L_frame );
437 : }
438 :
439 3620881 : if ( hTcxDec->enableTcxLpc )
440 : {
441 : /* Convert quantized lsp to A */
442 169613 : lsp2a_stab( &lsp[M], Aq, M );
443 : }
444 : else
445 : {
446 3451268 : if ( !st->tcxonly )
447 : {
448 1885891 : if ( !bfi && st->prev_bfi && !( st->safety_net ) && st->rate_switching_reset )
449 : {
450 : /* diffuse LPC power on rate switching*/
451 352 : RecLpcSpecPowDiffuseLc( &lsp[M], &lsp[0], &lsf[M], st, 0 );
452 352 : int_lsp( st->L_frame, &lsp[0], &lsp[M], Aq, M, interpol_frac_12k8, 0 );
453 352 : mvr2r( &lsf[M], lsfnew_uw, M );
454 : }
455 : else
456 : {
457 : /* LPC Interpolation for TCX */
458 1885539 : E_LPC_int_lpc_tcx( &lsp[0], &lsp[M], Aq );
459 : }
460 : }
461 : else
462 : {
463 1565377 : lsp2a_stab( &lsp[M], Aq, M );
464 : }
465 : }
466 :
467 3620881 : if ( !bfi && hTcxDec->tcx_lpc_shaped_ari )
468 : {
469 162588 : basop_E_LPC_f_lsp_a_conversion( lspind, Aind, M );
470 : }
471 :
472 : /* TCX decoder */
473 3620881 : decoder_tcx( st, prm, Aq, Aind, &synth[0], &synthFB[0], bfi, 0, sba_dirac_stereo_flag );
474 : }
475 :
476 : /*--------------------------------------------------------------------------------*
477 : * TCX10
478 : *--------------------------------------------------------------------------------*/
479 :
480 3758762 : if ( st->core == TCX_10_CORE )
481 : {
482 37380 : prm = NULL; /* just to avoid MSVC warnings */
483 :
484 112140 : for ( k = 0; k < 2; k++ )
485 : {
486 : /* Set pointer to parameters */
487 74760 : prm = param + ( k * DEC_NPRM_DIV );
488 :
489 : /* Stability Factor */
490 74760 : if ( !bfi )
491 : {
492 74568 : st->stab_fac = lsf_stab( &lsf[( k + 1 ) * M], &lsf[k * M], 0, st->L_frame );
493 : }
494 :
495 74760 : lsp2a_stab( &lsp[( k + 1 ) * M], Aq, M );
496 : {
497 74760 : IGFDecRestoreTCX10SubFrameData( st->hIGFDec, k );
498 : }
499 :
500 : /* TCX decoder */
501 74760 : decoder_tcx( st, prm, Aq, Aind, &synth[k * st->L_frame / 2], &synthFB[k * hTcxDec->L_frameTCX / 2], bfi, k, sba_dirac_stereo_flag );
502 : }
503 : }
504 :
505 : #ifdef DEBUG_MODE_TCX
506 : {
507 : int16_t tmp[L_FRAME48k];
508 :
509 : for ( i = 0; i < st->L_frame; i++ )
510 : {
511 : tmp[i] = (int16_t) ( synth[i] + 0.5f );
512 : }
513 : dbgwrite( tmp, sizeof( int16_t ), st->L_frame, 1, "./res/stereo_tcx_dec_synth.pcm" );
514 : }
515 : #endif
516 :
517 : /*--------------------------------------------------------------------------------*
518 : * Post-processing
519 : *--------------------------------------------------------------------------------*/
520 :
521 3758762 : if ( st->core == TCX_10_CORE || st->core == TCX_20_CORE )
522 : {
523 3658261 : if ( !bfi && st->hTonalMDCTConc != NULL )
524 : {
525 3443908 : TonalMDCTConceal_SaveTimeSignal( st->hTonalMDCTConc, synthFB, hTcxDec->L_frameTCX );
526 : }
527 :
528 3658261 : decoder_tcx_post( st, synth, synthFB, Aq, bfi, 0 );
529 :
530 3658261 : if ( st->core == TCX_20_CORE )
531 : {
532 : /* LPC Interpolation for BWE/post-processing */
533 3620881 : if ( st->narrowBand || st->sr_core == INT_FS_12k8 || st->sr_core == INT_FS_16k )
534 : {
535 2446658 : int_lsp( st->L_frame, st->lspold_uw, lspnew_uw, Aq, M, interpol_frac_12k8, 0 );
536 2446658 : mvr2r( Aq, st->mem_Aq, st->nb_subfr * ( M + 1 ) );
537 : }
538 : }
539 : }
540 :
541 : /* PLC: [Common: Classification] */
542 : /* the classifier buffer is always updated if the sr is at
543 : 16000 or below - the classification itself is just performed if(!st->tcxonly ) */
544 3758762 : if ( st->sr_core <= INT_FS_16k )
545 : {
546 2536444 : if ( st->core == TCX_20_CORE || st->core == TCX_10_CORE || ( st->tcxonly && st->bfi ) )
547 : {
548 : float pitch_C[4];
549 :
550 : /* note: the classifier needs the pitch only for tcx_only == 0, i.e. not for TCX10 */
551 2473564 : set_f( pitch_C, floorf( st->old_fpitch + 0.5f ), 4 );
552 :
553 : /* note: codec_mode is forced to MODE2, since FEC_clas_estim() considers only TCX being in Mode2*/
554 2473564 : 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 );
555 : }
556 : }
557 :
558 : /*--------------------------------------------------------------------------------*
559 : * Updates
560 : *--------------------------------------------------------------------------------*/
561 :
562 3758762 : if ( bfi && st->last_core != ACELP_CORE )
563 : {
564 : /* Update FEC_scale_syn parameters */
565 314854 : if ( hTcxLtpDec->tcxltp_gain == 0 )
566 : {
567 177251 : fer_energy( st->L_frame, UNVOICED, synth, (float) ( st->L_frame / 2 ), &st->enr_old, st->L_frame );
568 : }
569 : else
570 : {
571 137603 : fer_energy( st->L_frame, st->clas_dec, synth, st->old_fpitch, &st->enr_old, st->L_frame );
572 : }
573 : }
574 :
575 3758762 : if ( !bfi && st->clas_dec >= VOICED_TRANSITION && st->clas_dec < INACTIVE_CLAS )
576 : {
577 : /* use latest LPC set */
578 2595467 : st->old_enr_LP = enr_1_Az( Aq, L_SUBFR );
579 : }
580 :
581 : /* Update past buffers */
582 3758762 : mvr2r( synth_buf + st->L_frame, hTcxDec->old_synth, hTcxDec->old_synth_len );
583 3758762 : 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 ) );
584 3758762 : mvr2r( synth_bufFB + hTcxDec->L_frameTCX, hTcxDec->old_synthFB, hTcxDec->old_synth_lenFB );
585 3758762 : 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 ) );
586 :
587 3758762 : mvr2r( &lspnew_uw[( st->numlpc - 1 ) * M], st->lspold_uw, M );
588 3758762 : mvr2r( &lsfnew_uw[( st->numlpc - 1 ) * M], st->lsfold_uw, M );
589 :
590 3758762 : if ( bfi == 1 )
591 : {
592 314854 : mvr2r( st->lspold_uw, st->lsp_old, M ); /* for recovery */
593 314854 : mvr2r( st->lsfold_uw, st->lsf_old, M ); /* for recovery */
594 : }
595 : else
596 : {
597 3443908 : mvr2r( &lsp[st->numlpc * M], st->lsp_old, M );
598 3443908 : mvr2r( &lsf[st->numlpc * M], st->lsf_old, M );
599 : }
600 3758762 : mvr2r( st->lsp_q_cng, st->old_lsp_q_cng, M );
601 3758762 : mvr2r( st->lsf_q_cng, st->old_lsf_q_cng, M );
602 :
603 : /* Update CNG parameters */
604 3758762 : if ( !st->tcxonly && st->hTdCngDec != NULL )
605 : {
606 : /* update CNG parameters in active frames */
607 1165731 : if ( st->bwidth == NB && hTcxDec->enableTcxLpc && st->core != ACELP_CORE )
608 0 : {
609 : float buf[L_LP], res[L_FRAME], A[M + 1], r[M + 1], tmp, lsptmp[M];
610 0 : assert( st->L_frame == L_FRAME );
611 :
612 0 : mvr2r( synth + L_FRAME - L_LP, buf, L_LP );
613 0 : tmp = synth[L_FRAME - L_LP - 1];
614 0 : preemph( buf, st->preemph_fac, L_LP, &tmp );
615 0 : autocorr( buf, r, M, L_LP, LP_assym_window, 0, 0, 0 );
616 0 : lag_wind( r, M, INT_FS_12k8, LAGW_WEAK );
617 0 : lev_dur( A, r, M, NULL );
618 0 : a2lsp_stab( A, lsptmp, &lspnew_uw[0] );
619 0 : residu( A, M, buf + L_LP - L_FRAME, res, L_FRAME );
620 :
621 0 : if ( st->hTdCngDec != NULL )
622 : {
623 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 );
624 : }
625 : }
626 1165731 : else if ( st->hTdCngDec != NULL )
627 : {
628 1165731 : 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 );
629 : }
630 :
631 : /* Set 16k LSP flag for CNG buffer */
632 1165731 : st->hTdCngDec->ho_16k_lsp[st->hTdCngDec->ho_circ_ptr] = ( st->L_frame == L_FRAME ? 0 : 1 );
633 : }
634 3758762 : st->last_is_cng = 0;
635 :
636 : /* Postfiltering */
637 3758762 : post_decoder( st, synth_buf, pit_gain, pitch, signal_out, st->p_bpf_noise_buf );
638 :
639 3758762 : if ( signal_outFB )
640 : {
641 3758762 : mvr2r( synthFB, signal_outFB, hTcxDec->L_frameTCX );
642 : }
643 :
644 3758762 : if ( !bfi )
645 : {
646 3443908 : if ( param[1 + NOISE_FILL_RANGES] != 0 )
647 : {
648 1808872 : set_f( pitch_buf, hTcxLtpDec->tcxltp_pitch_int + (float) hTcxLtpDec->tcxltp_pitch_fr / (float) st->pit_res_max, NB_SUBFR16k );
649 : }
650 : else
651 : {
652 1635036 : set_f( pitch_buf, L_SUBFR, NB_SUBFR16k );
653 : }
654 : }
655 :
656 3758762 : if ( bfi )
657 : {
658 : /*"LPD dec - All BFI"*/
659 314854 : bitsRead = 0; /*to avoid empty counting */
660 : }
661 :
662 : /* updates */
663 3758762 : st->last_voice_factor = 0.0f;
664 3758762 : st->last_coder_type = st->coder_type;
665 :
666 : /* -------------------------------------------------------------- *
667 : * In FFT domain: perform noise estimation during active frames
668 : * -------------------------------------------------------------- */
669 :
670 3758762 : if ( st->hFdCngDec != NULL && ( st->sr_core == INT_FS_12k8 || st->sr_core == INT_FS_16k ) && st->total_brate <= MAX_ACELP_BRATE )
671 : {
672 2141307 : noisy_speech_detection( st->hFdCngDec, st->VAD && st->m_frame_type == ACTIVE_FRAME, signal_out );
673 :
674 2141307 : st->hFdCngDec->hFdCngCom->likelihood_noisy_speech = 0.99f * st->hFdCngDec->hFdCngCom->likelihood_noisy_speech + 0.01f * (float) st->hFdCngDec->hFdCngCom->flag_noisy_speech;
675 :
676 2141307 : st->lp_noise = st->hFdCngDec->lp_noise;
677 :
678 2141307 : if ( st->element_mode != IVAS_CPE_TD )
679 : {
680 2140618 : if ( ivas_format == ISM_FORMAT )
681 : {
682 : float buffer[L_FRAME16k];
683 1106789 : lerp( signal_outFB, buffer, st->L_frame, hTcxDec->L_frameTCX );
684 1106789 : ApplyFdCng( buffer, NULL, NULL, NULL, st, st->bfi, 0 );
685 : }
686 : else
687 : {
688 1033829 : ApplyFdCng( signal_out, NULL, NULL, NULL, st, st->bfi, 0 );
689 : }
690 : }
691 :
692 : /* Generate additional comfort noise to mask potential coding artefacts */
693 2141307 : if ( st->m_frame_type == ACTIVE_FRAME && st->flag_cna && !st->cna_dirac_flag )
694 : {
695 339491 : if ( st->element_mode == IVAS_CPE_TD && nchan_out == 2 )
696 : {
697 0 : if ( st->element_mode != last_element_mode )
698 : {
699 : /* Clear memory for secondary channel CNA */
700 0 : set_f( hStereoCng->olapBufferSynth22, 0.0f, st->hFdCngDec->hFdCngCom->frameSize / 2 );
701 : }
702 0 : expand_range( st->hFdCngDec->msPsd, psd_part, st->hFdCngDec->nFFTpart_shaping );
703 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 );
704 0 : generate_stereo_masking_noise( signal_out, st, hStereoTD, flag_sec_CNA, 0, hStereoCng, nchan_out );
705 : }
706 339491 : else if ( st->element_mode != IVAS_CPE_DFT )
707 : {
708 203149 : generate_masking_noise( signal_out, st->hFdCngDec->hFdCngCom, st->hFdCngDec->hFdCngCom->frameSize, 0, 0, 0, st->element_mode, hStereoCng, nchan_out );
709 : }
710 : }
711 :
712 2141307 : if ( st->element_mode == IVAS_CPE_TD && st->idchan == 0 )
713 : {
714 689 : ApplyFdCng( signal_out, NULL, NULL, NULL, st, st->bfi, 0 );
715 : }
716 : }
717 :
718 : #ifdef DEBUG_MODE_TCX
719 : {
720 : int16_t tmp[L_FRAME48k];
721 : static FILE *sP = NULL;
722 :
723 : for ( i = 0; i < hTcxDec->L_frameTCX; i++ )
724 : {
725 : tmp[i] = (int16_t) ( signal_outFB[i] + 0.5f );
726 : }
727 : dbgwrite( tmp, sizeof( int16_t ), hTcxDec->L_frameTCX, 1, "./res/stereo_tcx_dec_synthFB.pcm" );
728 :
729 : for ( i = 0; i < hTcxDec->L_frameTCX; i++ )
730 : {
731 : tmp[i] = (int16_t) ( signal_out[i] + 0.5f );
732 : }
733 : dbgwrite( tmp, sizeof( int16_t ), hTcxDec->L_frameTCX, 1, "./res/stereo_tcx_dec_synthLB.pcm" );
734 :
735 : if ( sP == NULL )
736 : sP = fopen( "./res/stereo_tcx_core_dec_swicthes.txt", "w" );
737 :
738 : 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 );
739 : }
740 : #endif
741 :
742 :
743 3758762 : pop_wmops();
744 3758762 : return;
745 : }
746 :
747 :
748 : /*-----------------------------------------------------------------*
749 : * Function dec_prm_tcx()
750 : *
751 : * Decode TCX parameters
752 : *-----------------------------------------------------------------*/
753 :
754 3443908 : static void dec_prm_tcx(
755 : Decoder_State *st, /* i/o: decoder memory state */
756 : int16_t param[], /* o : decoded parameters */
757 : int16_t param_lpc[], /* o : LPC parameters */
758 : int16_t *total_nbbits, /* i/o: number of bits / decoded bits */
759 : const int16_t last_element_mode, /* i : last element mode */
760 : int16_t *bitsRead /* o : number of read bits */
761 : )
762 : {
763 : int16_t start_bit_pos, bits_common;
764 : CONTEXT_HM_CONFIG hm_cfg;
765 : int16_t indexBuffer[N_MAX + 1];
766 : #ifdef DEBUG_MODE_TCX
767 : int16_t nbits_tcx;
768 : static FILE *pF = NULL;
769 : if ( pF == NULL )
770 : pF = fopen( "./res/stereo_tcx_dec_ind.txt", "w" );
771 : #endif
772 :
773 3443908 : assert( st->mdct_sw == MODE1 );
774 :
775 : /*--------------------------------------------------------------------------------*
776 : * INIT
777 : *--------------------------------------------------------------------------------*/
778 :
779 3443908 : hm_cfg.indexBuffer = indexBuffer;
780 : #ifdef DEBUG_MODE_TCX
781 : 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 ) );
782 : fprintf( pF, "stereo Common Header: %d bits\n", st->next_bit_pos );
783 : #endif
784 3443908 : if ( st->element_mode != IVAS_CPE_MDCT )
785 : {
786 3443908 : st->bits_frame_core = (int16_t) ( ( st->total_brate / FRAMES_PER_SEC ) - st->next_bit_pos );
787 : }
788 3443908 : start_bit_pos = st->next_bit_pos;
789 :
790 : /* Init LTP data */
791 3443908 : st->hTcxDec->tcx_hm_LtpPitchLag = -1;
792 3443908 : st->hTcxLtpDec->tcxltp_gain = 0.0f;
793 :
794 : /*--------------------------------------------------------------------------------*
795 : * header
796 : *--------------------------------------------------------------------------------*/
797 :
798 : /* Modes (ACE_GC, ACE_UC, TCX20, TCX10...) */
799 3443908 : getTCXMode( st, st, 0 /* <- MCT_flag */ );
800 :
801 : /* last_core for error concealment */
802 3443908 : if ( st->element_mode != IVAS_CPE_MDCT )
803 : {
804 3443908 : st->last_core_from_bs = get_next_indice( st, 1 ); /* Store decoder memory of last_core */
805 3443908 : if ( st->last_core == ACELP_CORE && st->last_core_from_bs != ACELP_CORE )
806 : {
807 : /* A mismatch between the memory and the last_core
808 : from the bitstream indicates a frame was lost. If prev_bfi is
809 : not set the frame loss occured during CNG and the prev_bfi needs to be set. */
810 2604 : st->prev_bfi = 1;
811 : }
812 :
813 : /* possible need for reconfiguration can only be decided correctly once last_core_from_bs has been decoded */
814 3443908 : 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 ) ) )
815 : {
816 1414 : stereo_tcx_dec_mode_switch_reconf( st, 0, last_element_mode );
817 : }
818 :
819 3443908 : st->last_core = st->last_core_from_bs;
820 :
821 : /*for TCX 10 force last_core to be TCX since ACELP as previous core is forbidden*/
822 3443908 : if ( st->core == TCX_10_CORE )
823 : {
824 37284 : st->last_core = TCX_20_CORE;
825 : }
826 : }
827 :
828 3443908 : if ( st->element_mode != IVAS_CPE_MDCT )
829 : {
830 3443908 : getTCXWindowing( st->core, st->last_core, st->element_mode, st->hTcxCfg, st );
831 : }
832 3443908 : st->flagGuidedAcelp = 0;
833 :
834 3443908 : if ( st->dec_glr )
835 : {
836 0 : st->dec_glr_idx = -1;
837 : }
838 : #ifdef DEBUG_MODE_TCX
839 : 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 );
840 : nbits_tcx = st->next_bit_pos;
841 : #endif
842 :
843 : /*--------------------------------------------------------------------------------*
844 : * LPC parameters
845 : *--------------------------------------------------------------------------------*/
846 :
847 3443908 : getLPCparam( st, param_lpc, st, -1, 0 );
848 :
849 3443908 : bits_common = st->next_bit_pos - start_bit_pos;
850 :
851 : #ifdef DEBUG_MODE_TCX
852 : fprintf( pF, "\t TCX LPC: %d bits\n", st->next_bit_pos - nbits_tcx );
853 : #endif
854 :
855 : /*--------------------------------------------------------------------------------*
856 : * TCX20/10 parameters
857 : *--------------------------------------------------------------------------------*/
858 :
859 3443908 : getTCXparam( st, st, hm_cfg, param, bits_common, start_bit_pos, NULL, NULL, NULL, -1 );
860 :
861 3443908 : if ( *total_nbbits - bitsRead[0] < ( st->next_bit_pos - start_bit_pos ) )
862 : {
863 0 : st->BER_detect = 1;
864 0 : st->next_bit_pos = start_bit_pos + *total_nbbits - bitsRead[0];
865 : }
866 :
867 3443908 : bitsRead[0] = st->next_bit_pos - start_bit_pos;
868 :
869 3443908 : return;
870 : }
871 :
872 :
873 : /*-----------------------------------------------------------------*
874 : * Function stereo_tcx_dec_mode_switch_reconf()
875 : *
876 : * Reconfigure stereo TCX parameters
877 : *-----------------------------------------------------------------*/
878 :
879 417020 : static void stereo_tcx_dec_mode_switch_reconf(
880 : Decoder_State *st, /* i/o: decoder state structure */
881 : const int16_t MCT_flag, /* i : hMCT handle allocated (1) or not (0)*/
882 : const int16_t last_element_mode /* i : element mode of previous frame */
883 : )
884 : {
885 : int16_t frame_size_index;
886 :
887 417020 : st->rate_switching_init = 1;
888 :
889 : /* Identify frame type - TCX Reconfiguration */
890 4282436 : for ( frame_size_index = 0; frame_size_index < FRAME_SIZE_NB; frame_size_index++ )
891 : {
892 4282436 : if ( frame_size_index < FRAME_SIZE_NB - 1 )
893 : {
894 4141672 : if ( ( FrameSizeConfig[frame_size_index].frame_bits <= st->bits_frame_nominal ) && ( FrameSizeConfig[frame_size_index + 1].frame_bits > st->bits_frame_nominal ) )
895 : {
896 276256 : break;
897 : }
898 : }
899 : else
900 : {
901 140764 : if ( FrameSizeConfig[frame_size_index].frame_bits <= st->bits_frame_nominal )
902 : {
903 140764 : break;
904 : }
905 : }
906 : }
907 :
908 : /* Reconfigure Core */
909 417020 : 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 );
910 :
911 417020 : return;
912 : }
|