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 36150474 : 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 36150474 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
69 36150474 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
70 :
71 36150474 : st->rate_switching_init = 0;
72 36150474 : st->m_frame_type = ACTIVE_FRAME;
73 36150474 : st->core_brate = st->total_brate;
74 :
75 : /*sampling rate*/
76 36150474 : 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 36150474 : st->fscale = sr2fscale( st->sr_core );
78 :
79 : /*frame size*/
80 36150474 : st->L_frame = (int16_t) ( st->sr_core / FRAMES_PER_SEC );
81 36150474 : hTcxDec->L_frameTCX = (int16_t) ( st->output_Fs / FRAMES_PER_SEC );
82 :
83 36150474 : 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 20651043 : st->nb_subfr = NB_SUBFR16k;
86 : }
87 : else
88 : {
89 15499431 : st->nb_subfr = NB_SUBFR;
90 : }
91 :
92 : /*TCX tools*/
93 36150474 : st->hTcxCfg->ctx_hm = getCtxHm( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, st->rf_flag );
94 36150474 : st->hTcxCfg->resq = getResq( st->bits_frame_nominal * FRAMES_PER_SEC );
95 36150474 : hTcxDec->tcx_lpc_shaped_ari = getTcxLpcShapedAri( st->bits_frame_nominal * FRAMES_PER_SEC, st->rf_flag, st->element_mode );
96 36150474 : st->igf = getIgfPresent( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, st->bwidth, st->rf_flag );
97 :
98 36150474 : if ( st->element_mode != EVS_MONO )
99 : {
100 36150474 : st->hTcxCfg->fIsTNSAllowed = getTnsAllowed( st->bits_frame_nominal * FRAMES_PER_SEC, st->igf, st->element_mode );
101 : }
102 36150474 : if ( hTcxLtpDec != NULL )
103 : {
104 36150474 : hTcxLtpDec->tcxltp = getTcxLtp( st->sr_core );
105 : }
106 :
107 36150474 : if ( st->element_mode == IVAS_SCE )
108 : {
109 5988567 : st->tcxonly = getTcxonly( st->element_mode, st->bits_frame_nominal * FRAMES_PER_SEC, MCT_flag, st->is_ism_format );
110 :
111 : /* LPC quantization */
112 5988567 : if ( st->sr_core <= INT_FS_16k && st->tcxonly == 0 )
113 : {
114 3834453 : st->lpcQuantization = 1;
115 : }
116 : else
117 : {
118 2154114 : st->lpcQuantization = 0;
119 : }
120 :
121 5988567 : if ( st->tcxonly == 0 )
122 : {
123 3834453 : st->numlpc = 1;
124 : }
125 : else
126 : {
127 2154114 : st->numlpc = 2;
128 : }
129 : }
130 :
131 36150474 : if ( ( st->bits_frame_nominal != st->last_bits_frame_nominal ) ||
132 35494011 : ( st->bwidth != st->last_bwidth ) ||
133 35416929 : ( 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 35210031 : ( st->idchan == 1 && st->element_mode == IVAS_CPE_MDCT && last_element_mode != IVAS_CPE_MDCT ) )
135 : {
136 : /*re-initialization*/
137 950400 : stereo_tcx_dec_mode_switch_reconf( st, MCT_flag, last_element_mode );
138 : }
139 :
140 36150474 : return;
141 : }
142 :
143 :
144 : /*-------------------------------------------------------------------*
145 : * stereo_tcx_core_dec()
146 : *
147 : * stereo TCX decoder
148 : *-------------------------------------------------------------------*/
149 :
150 6951162 : 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 6951162 : TCX_LTP_DEC_HANDLE hTcxLtpDec = st->hTcxLtpDec;
193 6951162 : TCX_DEC_HANDLE hTcxDec = st->hTcxDec;
194 :
195 6951162 : push_wmops( "stereo_tcx_core_dec" );
196 :
197 : /*Sanity check*/
198 6951162 : assert( !( st->total_brate == FRAME_NO_DATA || st->total_brate == SID_2k40 ) ); /*Active frame*/
199 6951162 : assert( st->rf_flag == 0 );
200 6951162 : assert( st->prev_use_partial_copy == 0 ); /* No channel aware mode */
201 6951162 : assert( st->codec_mode == MODE1 ); /* must be in Mode 1 */
202 6951162 : assert( st->mdct_sw == MODE1 ); /* must be switched to TCX in MODE1 */
203 :
204 6951162 : bfi = 0;
205 :
206 : /*--------------------------------------------------------------------------------*
207 : * Initialization or re-configuration of Stereo TCX
208 : *--------------------------------------------------------------------------------*/
209 :
210 6951162 : stereo_tcx_init_dec( st, 0, last_element_mode );
211 :
212 : /*--------------------------------------------------------------------------------*
213 : * Initializations
214 : *--------------------------------------------------------------------------------*/
215 :
216 6951162 : total_nbbits = (int16_t) ( st->total_brate / FRAMES_PER_SEC );
217 6951162 : bitsRead = 0;
218 6951162 : LSF_Q_prediction = -1; /* to avoid compilation warnings */
219 :
220 6951162 : if ( frameMode == FRAMEMODE_NORMAL )
221 : {
222 6688689 : st->m_decodeMode = DEC_NO_FRAM_LOSS;
223 6688689 : bfi = 0;
224 : }
225 :
226 6951162 : if ( frameMode == FRAMEMODE_MISSING )
227 : {
228 262473 : st->m_decodeMode = DEC_CONCEALMENT_EXT;
229 262473 : bfi = 1;
230 : }
231 :
232 : /* for bass postfilter */
233 6951162 : if ( st->tcxonly )
234 : {
235 2154114 : st->p_bpf_noise_buf = NULL;
236 : }
237 : else
238 : {
239 4797048 : st->p_bpf_noise_buf = st->bpf_noise_buf;
240 4797048 : set_s( pitch, L_SUBFR, st->nb_subfr );
241 4797048 : set_zero( pit_gain, st->nb_subfr );
242 : }
243 :
244 : /* Initialize pointers */
245 6951162 : synth = synth_buf + hTcxDec->old_synth_len;
246 6951162 : synthFB = synth_bufFB + hTcxDec->old_synth_lenFB;
247 6951162 : mvr2r( hTcxDec->old_synth, synth_buf, hTcxDec->old_synth_len );
248 6951162 : mvr2r( hTcxDec->old_synthFB, synth_bufFB, hTcxDec->old_synth_lenFB );
249 6951162 : set_zero( synth, L_FRAME_PLUS_INTERNAL + M );
250 6951162 : 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 6951162 : if ( !bfi )
262 : {
263 6688689 : st->second_last_core = st->last_core;
264 :
265 6688689 : dec_prm_tcx( st, param, param_lpc, &total_nbbits, last_element_mode, &bitsRead );
266 : }
267 : else
268 : {
269 262473 : if ( st->nbLostCmpt > 1 )
270 : {
271 119238 : st->flagGuidedAcelp = 0;
272 : }
273 :
274 : /* PLC: [Common: mode decision]
275 : * PLC: Decide which Concealment to use. Update pitch lags if needed */
276 262473 : st->core = GetPLCModeDecision( st );
277 : }
278 :
279 : /*--------------------------------------------------------------------------------*
280 : * LPC envelope decoding
281 : *--------------------------------------------------------------------------------*/
282 :
283 6951162 : st->acelp_cfg.midLpc = 0;
284 :
285 6951162 : if ( !bfi )
286 : {
287 6688689 : if ( hTcxDec->enableTcxLpc )
288 : {
289 : int16_t tcx_lpc_cdk;
290 :
291 654483 : tcx_lpc_cdk = tcxlpc_get_cdk( st->coder_type );
292 :
293 654483 : mvr2r( st->lsf_old, &lsf[0], M );
294 654483 : mvr2r( st->lsp_old, &lsp[0], M );
295 :
296 654483 : D_lsf_tcxlpc( param_lpc, &lsf[M], lspind, st->narrowBand, tcx_lpc_cdk, st->mem_MA );
297 :
298 654483 : lsf2lsp( &lsf[M], &lsp[M], M, st->sr_core );
299 :
300 654483 : lsf_update_memory( st->narrowBand, &lsf[M], st->mem_MA, st->mem_MA );
301 654483 : mvr2r( &lsf[M], st->mem_AR, M );
302 654483 : hTcxDec->envWeighted = 1;
303 :
304 654483 : E_LPC_lsp_unweight( &lsp[M], lspnew_uw, lsfnew_uw, 1.0f / st->gamma );
305 : }
306 : else
307 : {
308 6034206 : 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 6034206 : lpc_unquantize( st, lsf, lsp, param_lpc, lspmid, lsfmid, AUDIO, &LSF_Q_prediction );
316 :
317 12096438 : for ( k = 0; k < st->numlpc; ++k )
318 : {
319 6062232 : mvr2r( &lsp[( k + 1 ) * M], &lspnew_uw[k * M], M );
320 6062232 : 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 13405404 : for ( k = 0; k < st->numlpc; k++ )
326 : {
327 114184155 : for ( i = 0; i < M; i++ )
328 : {
329 107467440 : st->lsf_adaptive_mean[i] = ( st->lsfoldbfi1[i] + st->lsfoldbfi0[i] + lsfnew_uw[k * M + i] ) / 3;
330 107467440 : st->lsfoldbfi1[i] = st->lsfoldbfi0[i];
331 107467440 : 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 262473 : if ( st->tcxonly == 0 || st->core < TCX_10_CORE )
341 : {
342 262278 : st->numlpc = 1;
343 : }
344 : else
345 : {
346 195 : st->numlpc = 2;
347 : }
348 :
349 262473 : if ( st->nbLostCmpt == 1 )
350 : {
351 143235 : mvr2r( st->lsf_old, st->old_lsf_q_cng, M );
352 143235 : mvr2r( st->lsp_old, st->old_lsp_q_cng, M );
353 : }
354 :
355 262473 : lsfBase = PlcGetlsfBase( st->lpcQuantization, st->narrowBand, st->sr_core );
356 :
357 262473 : 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 262473 : hTcxDec->envWeighted = 0;
360 :
361 262473 : mvr2r( st->lspold_uw, lsp, M );
362 262473 : mvr2r( st->lsfold_uw, lsf, M );
363 :
364 525141 : for ( k = 0; k < st->numlpc; k++ )
365 : {
366 262668 : mvr2r( &lsfnew_uw[k * M], &lsf[( k + 1 ) * M], M );
367 :
368 262668 : lsf2lsp( &lsf[( k + 1 ) * M], &lsp[( k + 1 ) * M], M, st->sr_core );
369 262668 : lsf2lsp( st->lsf_q_cng, st->lsp_q_cng, M, st->sr_core );
370 :
371 262668 : mvr2r( &lsp[( k + 1 ) * M], &lspnew_uw[k * M], M );
372 : }
373 : }
374 :
375 : /*---------------------------------------------------------------*
376 : * Rate switching
377 : *---------------------------------------------------------------*/
378 :
379 6951162 : if ( st->rate_switching_reset )
380 : {
381 55170 : mvr2r( &( lsf[M] ), &( lsf[0] ), M );
382 55170 : mvr2r( &( lsp[M] ), &( lsp[0] ), M );
383 55170 : mvr2r( &( lsf[M] ), st->lsf_old, M );
384 55170 : mvr2r( &( lsp[M] ), st->lsp_old, M );
385 55170 : mvr2r( &( lsf[M] ), lsfmid, M );
386 55170 : mvr2r( &( lsp[M] ), lspmid, M );
387 55170 : lsp2a_stab( st->lsp_old, st->old_Aq_12_8, M );
388 : }
389 :
390 : /*--------------------------------------------------------------------------------*
391 : * TD-TCX concealment
392 : *--------------------------------------------------------------------------------*/
393 :
394 6951162 : if ( st->core == ACELP_CORE )
395 : {
396 70524 : assert( bfi ); /*must only happen in BFI*/
397 :
398 70524 : if ( !st->tcxonly )
399 : {
400 : /*LPC interpolation*/
401 63510 : int_lsp( st->L_frame, &lsp[0], &lsp[M], Aq, M, interpol_frac_12k8, 0 );
402 63510 : 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 70524 : con_tcx( st, &synthFB[0], -1.f, NULL, 0, NULL );
407 70524 : lerp( synthFB, synth, st->L_frame, hTcxDec->L_frameTCX );
408 70524 : st->con_tcx = 1;
409 70524 : set_f( &st->mem_pitch_gain[2], st->lp_gainp, st->nb_subfr );
410 :
411 : /* LPC for ACELP/BWE */
412 70524 : if ( st->narrowBand || st->sr_core == INT_FS_12k8 || st->sr_core == INT_FS_16k )
413 : {
414 63510 : 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 70524 : if ( st->hTonalMDCTConc != NULL )
420 : {
421 70524 : TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, 0, 0, 0, 0 );
422 : }
423 : }
424 :
425 : /*--------------------------------------------------------------------------------*
426 : * TCX20
427 : *--------------------------------------------------------------------------------*/
428 :
429 : /* Set pointer to parameters */
430 6951162 : prm = param;
431 6951162 : if ( st->core == TCX_20_CORE )
432 : {
433 : /* Stability Factor */
434 6852417 : if ( !bfi )
435 : {
436 6660663 : st->stab_fac = lsf_stab( &lsf[M], &lsf[0], 0, st->L_frame );
437 : }
438 :
439 6852417 : if ( hTcxDec->enableTcxLpc )
440 : {
441 : /* Convert quantized lsp to A */
442 664791 : lsp2a_stab( &lsp[M], Aq, M );
443 : }
444 : else
445 : {
446 6187626 : if ( !st->tcxonly )
447 : {
448 4068747 : if ( !bfi && st->prev_bfi && !( st->safety_net ) && st->rate_switching_reset )
449 : {
450 : /* diffuse LPC power on rate switching*/
451 1254 : RecLpcSpecPowDiffuseLc( &lsp[M], &lsp[0], &lsf[M], st, 0 );
452 1254 : int_lsp( st->L_frame, &lsp[0], &lsp[M], Aq, M, interpol_frac_12k8, 0 );
453 1254 : mvr2r( &lsf[M], lsfnew_uw, M );
454 : }
455 : else
456 : {
457 : /* LPC Interpolation for TCX */
458 4067493 : E_LPC_int_lpc_tcx( &lsp[0], &lsp[M], Aq );
459 : }
460 : }
461 : else
462 : {
463 2118879 : lsp2a_stab( &lsp[M], Aq, M );
464 : }
465 : }
466 :
467 6852417 : if ( !bfi && hTcxDec->tcx_lpc_shaped_ari )
468 : {
469 654483 : basop_E_LPC_f_lsp_a_conversion( lspind, Aind, M );
470 : }
471 :
472 : /* TCX decoder */
473 6852417 : decoder_tcx( st, prm, Aq, Aind, &synth[0], &synthFB[0], bfi, 0, sba_dirac_stereo_flag );
474 : }
475 :
476 : /*--------------------------------------------------------------------------------*
477 : * TCX10
478 : *--------------------------------------------------------------------------------*/
479 :
480 6951162 : if ( st->core == TCX_10_CORE )
481 : {
482 28221 : prm = NULL; /* just to avoid MSVC warnings */
483 :
484 84663 : for ( k = 0; k < 2; k++ )
485 : {
486 : /* Set pointer to parameters */
487 56442 : prm = param + ( k * DEC_NPRM_DIV );
488 :
489 : /* Stability Factor */
490 56442 : if ( !bfi )
491 : {
492 56052 : st->stab_fac = lsf_stab( &lsf[( k + 1 ) * M], &lsf[k * M], 0, st->L_frame );
493 : }
494 :
495 56442 : lsp2a_stab( &lsp[( k + 1 ) * M], Aq, M );
496 : {
497 56442 : IGFDecRestoreTCX10SubFrameData( st->hIGFDec, k );
498 : }
499 :
500 : /* TCX decoder */
501 56442 : 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 6951162 : if ( st->core == TCX_10_CORE || st->core == TCX_20_CORE )
522 : {
523 6880638 : if ( !bfi && st->hTonalMDCTConc != NULL )
524 : {
525 6688689 : TonalMDCTConceal_SaveTimeSignal( st->hTonalMDCTConc, synthFB, hTcxDec->L_frameTCX );
526 : }
527 :
528 6880638 : decoder_tcx_post( st, synth, synthFB, Aq, bfi, 0 );
529 :
530 6880638 : if ( st->core == TCX_20_CORE )
531 : {
532 : /* LPC Interpolation for BWE/post-processing */
533 6852417 : if ( st->narrowBand || st->sr_core == INT_FS_12k8 || st->sr_core == INT_FS_16k )
534 : {
535 4756821 : int_lsp( st->L_frame, st->lspold_uw, lspnew_uw, Aq, M, interpol_frac_12k8, 0 );
536 4756821 : 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 6951162 : if ( st->sr_core <= INT_FS_16k )
545 : {
546 4820403 : 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 4756893 : 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 4756893 : 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 6951162 : if ( bfi && st->last_core != ACELP_CORE )
563 : {
564 : /* Update FEC_scale_syn parameters */
565 262473 : if ( hTcxLtpDec->tcxltp_gain == 0 )
566 : {
567 174819 : fer_energy( st->L_frame, UNVOICED, synth, (float) ( st->L_frame / 2 ), &st->enr_old, st->L_frame );
568 : }
569 : else
570 : {
571 87654 : fer_energy( st->L_frame, st->clas_dec, synth, st->old_fpitch, &st->enr_old, st->L_frame );
572 : }
573 : }
574 :
575 6951162 : if ( !bfi && st->clas_dec >= VOICED_TRANSITION && st->clas_dec < INACTIVE_CLAS )
576 : {
577 : /* use latest LPC set */
578 3174903 : st->old_enr_LP = enr_1_Az( Aq, L_SUBFR );
579 : }
580 :
581 : /* Update past buffers */
582 6951162 : mvr2r( synth_buf + st->L_frame, hTcxDec->old_synth, hTcxDec->old_synth_len );
583 6951162 : 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 6951162 : mvr2r( synth_bufFB + hTcxDec->L_frameTCX, hTcxDec->old_synthFB, hTcxDec->old_synth_lenFB );
585 6951162 : 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 6951162 : mvr2r( &lspnew_uw[( st->numlpc - 1 ) * M], st->lspold_uw, M );
588 6951162 : mvr2r( &lsfnew_uw[( st->numlpc - 1 ) * M], st->lsfold_uw, M );
589 :
590 6951162 : if ( bfi == 1 )
591 : {
592 262473 : mvr2r( st->lspold_uw, st->lsp_old, M ); /* for recovery */
593 262473 : mvr2r( st->lsfold_uw, st->lsf_old, M ); /* for recovery */
594 : }
595 : else
596 : {
597 6688689 : mvr2r( &lsp[st->numlpc * M], st->lsp_old, M );
598 6688689 : mvr2r( &lsf[st->numlpc * M], st->lsf_old, M );
599 : }
600 6951162 : mvr2r( st->lsp_q_cng, st->old_lsp_q_cng, M );
601 6951162 : mvr2r( st->lsf_q_cng, st->old_lsf_q_cng, M );
602 :
603 : /* Update CNG parameters */
604 6951162 : if ( !st->tcxonly && st->hTdCngDec != NULL )
605 : {
606 : /* update CNG parameters in active frames */
607 3616140 : 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 3616140 : else if ( st->hTdCngDec != NULL )
627 : {
628 3616140 : 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 3616140 : st->hTdCngDec->ho_16k_lsp[st->hTdCngDec->ho_circ_ptr] = ( st->L_frame == L_FRAME ? 0 : 1 );
633 : }
634 6951162 : st->last_is_cng = 0;
635 :
636 : /* Postfiltering */
637 6951162 : post_decoder( st, synth_buf, pit_gain, pitch, signal_out, st->p_bpf_noise_buf );
638 :
639 6951162 : if ( signal_outFB )
640 : {
641 6951162 : mvr2r( synthFB, signal_outFB, hTcxDec->L_frameTCX );
642 : }
643 :
644 6951162 : if ( !bfi )
645 : {
646 6688689 : if ( param[1 + NOISE_FILL_RANGES] != 0 )
647 : {
648 2527740 : 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 4160949 : set_f( pitch_buf, L_SUBFR, NB_SUBFR16k );
653 : }
654 : }
655 :
656 6951162 : if ( bfi )
657 : {
658 : /*"LPD dec - All BFI"*/
659 262473 : bitsRead = 0; /*to avoid empty counting */
660 : }
661 :
662 : /* updates */
663 6951162 : st->last_voice_factor = 0.0f;
664 6951162 : st->last_coder_type = st->coder_type;
665 :
666 : /* -------------------------------------------------------------- *
667 : * In FFT domain: perform noise estimation during active frames
668 : * -------------------------------------------------------------- */
669 :
670 6951162 : if ( st->hFdCngDec != NULL && ( st->sr_core == INT_FS_12k8 || st->sr_core == INT_FS_16k ) && st->total_brate <= MAX_ACELP_BRATE )
671 : {
672 4696203 : noisy_speech_detection( st->hFdCngDec, st->VAD && st->m_frame_type == ACTIVE_FRAME, signal_out );
673 :
674 4696203 : 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 4696203 : st->lp_noise = st->hFdCngDec->lp_noise;
677 :
678 4696203 : if ( st->element_mode != IVAS_CPE_TD )
679 : {
680 4693488 : if ( ivas_format == ISM_FORMAT )
681 : {
682 : float buffer[L_FRAME16k];
683 1570671 : lerp( signal_outFB, buffer, st->L_frame, hTcxDec->L_frameTCX );
684 1570671 : ApplyFdCng( buffer, NULL, NULL, NULL, st, st->bfi, 0 );
685 : }
686 : else
687 : {
688 3122817 : ApplyFdCng( signal_out, NULL, NULL, NULL, st, st->bfi, 0 );
689 : }
690 : }
691 :
692 : /* Generate additional comfort noise to mask potential coding artefacts */
693 4696203 : if ( st->m_frame_type == ACTIVE_FRAME && st->flag_cna && !st->cna_dirac_flag )
694 : {
695 2416233 : if ( st->element_mode == IVAS_CPE_TD && nchan_out == 2 )
696 : {
697 9 : 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 9 : expand_range( st->hFdCngDec->msPsd, psd_part, st->hFdCngDec->nFFTpart_shaping );
703 9 : 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 9 : generate_stereo_masking_noise( signal_out, st, hStereoTD, flag_sec_CNA, 0, hStereoCng, nchan_out );
705 : }
706 2416224 : else if ( st->element_mode != IVAS_CPE_DFT )
707 : {
708 1474089 : 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 4696203 : if ( st->element_mode == IVAS_CPE_TD && st->idchan == 0 )
713 : {
714 2715 : 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 6951162 : pop_wmops();
744 6951162 : return;
745 : }
746 :
747 :
748 : /*-----------------------------------------------------------------*
749 : * Function dec_prm_tcx()
750 : *
751 : * Decode TCX parameters
752 : *-----------------------------------------------------------------*/
753 :
754 6688689 : 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 6688689 : assert( st->mdct_sw == MODE1 );
774 :
775 : /*--------------------------------------------------------------------------------*
776 : * INIT
777 : *--------------------------------------------------------------------------------*/
778 :
779 6688689 : 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 6688689 : if ( st->element_mode != IVAS_CPE_MDCT )
785 : {
786 6688689 : st->bits_frame_core = (int16_t) ( ( st->total_brate / FRAMES_PER_SEC ) - st->next_bit_pos );
787 : }
788 6688689 : start_bit_pos = st->next_bit_pos;
789 :
790 : /* Init LTP data */
791 6688689 : st->hTcxDec->tcx_hm_LtpPitchLag = -1;
792 6688689 : st->hTcxLtpDec->tcxltp_gain = 0.0f;
793 :
794 : /*--------------------------------------------------------------------------------*
795 : * header
796 : *--------------------------------------------------------------------------------*/
797 :
798 : /* Modes (ACE_GC, ACE_UC, TCX20, TCX10...) */
799 6688689 : getTCXMode( st, st, 0 /* <- MCT_flag */ );
800 :
801 : /* last_core for error concealment */
802 6688689 : if ( st->element_mode != IVAS_CPE_MDCT )
803 : {
804 6688689 : st->last_core_from_bs = get_next_indice( st, 1 ); /* Store decoder memory of last_core */
805 6688689 : 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 6255 : 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 6688689 : 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 1938 : stereo_tcx_dec_mode_switch_reconf( st, 0, last_element_mode );
817 : }
818 :
819 6688689 : 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 6688689 : if ( st->core == TCX_10_CORE )
823 : {
824 28026 : st->last_core = TCX_20_CORE;
825 : }
826 : }
827 :
828 6688689 : if ( st->element_mode != IVAS_CPE_MDCT )
829 : {
830 6688689 : getTCXWindowing( st->core, st->last_core, st->element_mode, st->hTcxCfg, st );
831 : }
832 6688689 : st->flagGuidedAcelp = 0;
833 :
834 6688689 : 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 6688689 : getLPCparam( st, param_lpc, st, -1, 0 );
848 :
849 6688689 : 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 6688689 : getTCXparam( st, st, hm_cfg, param, bits_common, start_bit_pos, NULL, NULL, NULL, -1 );
860 :
861 6688689 : 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 6688689 : bitsRead[0] = st->next_bit_pos - start_bit_pos;
868 :
869 6688689 : return;
870 : }
871 :
872 :
873 : /*-----------------------------------------------------------------*
874 : * Function stereo_tcx_dec_mode_switch_reconf()
875 : *
876 : * Reconfigure stereo TCX parameters
877 : *-----------------------------------------------------------------*/
878 :
879 952338 : 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 952338 : st->rate_switching_init = 1;
888 :
889 : /* Identify frame type - TCX Reconfiguration */
890 9066951 : for ( frame_size_index = 0; frame_size_index < FRAME_SIZE_NB; frame_size_index++ )
891 : {
892 9066951 : if ( frame_size_index < FRAME_SIZE_NB - 1 )
893 : {
894 8929005 : if ( ( FrameSizeConfig[frame_size_index].frame_bits <= st->bits_frame_nominal ) && ( FrameSizeConfig[frame_size_index + 1].frame_bits > st->bits_frame_nominal ) )
895 : {
896 814392 : break;
897 : }
898 : }
899 : else
900 : {
901 137946 : if ( FrameSizeConfig[frame_size_index].frame_bits <= st->bits_frame_nominal )
902 : {
903 137946 : break;
904 : }
905 : }
906 : }
907 :
908 : /* Reconfigure Core */
909 952338 : 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 952338 : return;
912 : }
|