Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : #include <assert.h>
34 : #include <stdint.h>
35 : #include "options.h"
36 : #include "prot.h"
37 : #include "rom_com.h"
38 : #include "ivas_rom_com.h"
39 : #ifdef DEBUGGING
40 : #include "debug.h"
41 : #endif
42 : #include "wmc_auto.h"
43 : #include "cnst.h"
44 : #include "basop_proto_func.h"
45 : #include "stat_com.h"
46 : #include "ivas_prot.h"
47 : #include "ivas_stat_dec.h"
48 : #include "ivas_stat_com.h"
49 : #include <math.h>
50 :
51 :
52 : /*-----------------------------------------------------------------*
53 : * Function mdct_read_IGF_bits()
54 : *
55 : * read IGF side bits
56 : *-----------------------------------------------------------------*/
57 :
58 4462948 : void mdct_read_IGF_bits(
59 : Decoder_State *st, /* i/o: Decoder state handle */
60 : Decoder_State *st0 /* i : pointer to handle where bitstream is read */
61 : )
62 : {
63 : int16_t k, start;
64 :
65 4462948 : start = st0->next_bit_pos;
66 :
67 4462948 : if ( st->core == TCX_20_CORE )
68 : {
69 : /* read IGF payload */
70 4295142 : IGFDecReadLevel( st->hIGFDec, st0, ( st->last_core == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, 1 );
71 :
72 4295142 : IGFDecReadData( st->hIGFDec, st0, ( st->last_core == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, 1 );
73 : }
74 :
75 4462948 : if ( st->core == TCX_10_CORE )
76 : {
77 503418 : for ( k = 0; k < 2; k++ )
78 : {
79 335612 : IGFDecReadLevel( st->hIGFDec, st0, IGF_GRID_LB_SHORT, k == 0 ? 1 : 0 );
80 :
81 335612 : IGFDecReadData( st->hIGFDec, st0, IGF_GRID_LB_SHORT, k == 0 ? 1 : 0 );
82 :
83 335612 : IGFDecStoreTCX10SubFrameData( st->hIGFDec, k );
84 : }
85 : }
86 :
87 4462948 : st->side_bits_frame_channel += ( st0->next_bit_pos - start );
88 :
89 4462948 : return;
90 : }
91 :
92 :
93 : /*-----------------------------------------------------------------*
94 : * Function dec_prm_tcx_sidebits()
95 : *
96 : * decode TCX side parameters
97 : *-----------------------------------------------------------------*/
98 :
99 9836413 : static void dec_prm_tcx_sidebits(
100 : int16_t param[], /* o : decoded parameters */
101 : Decoder_State *st, /* i/o: decoder memory state */
102 : const int16_t tnsSize[NB_DIV], /* i : TNS activity/subframe (in channel 0 in case of 2) */
103 : int16_t p_param[NB_DIV], /* o : pointer to parameters for next round of bs reading*/
104 : int16_t nTnsBitsTCX10[NB_DIV], /* o : number of TNS bits per TCX10 subframe */
105 : Decoder_State *st0, /* i/o: core decoder state handle - for bitstream */
106 : const int16_t MCT_flag, /* i : hMCT handle allocated (1) or not (0) */
107 : const int16_t ch /* i : channel */
108 : )
109 : {
110 : CONTEXT_HM_CONFIG hm_cfg;
111 : int16_t start_bit_pos;
112 : #ifdef DEBUG_MODE_TCX
113 : int16_t bits_common;
114 : static FILE *pF = NULL;
115 : if ( pF == NULL )
116 : pF = fopen( "./res/stereo_tcx_dec_ind.txt", "w" );
117 : #endif
118 :
119 : /*--------------------------------------------------------------------------------*
120 : * Initialization
121 : *--------------------------------------------------------------------------------*/
122 :
123 9836413 : hm_cfg.indexBuffer = NULL;
124 :
125 9836413 : start_bit_pos = st0->next_bit_pos;
126 :
127 : /* Init LTP data */
128 9836413 : st->hTcxDec->tcx_hm_LtpPitchLag = -1;
129 9836413 : st->hTcxLtpDec->tcxltp_gain = 0.0f;
130 :
131 : /*--------------------------------------------------------------------------------*
132 : * Header
133 : *--------------------------------------------------------------------------------*/
134 :
135 : /* Modes (ACE_GC, ACE_UC, TCX20, TCX10...) */
136 9836413 : getTCXMode( st, st0, MCT_flag );
137 :
138 9836413 : st->flagGuidedAcelp = 0;
139 :
140 9836413 : if ( st->dec_glr )
141 : {
142 0 : st->dec_glr_idx = -1;
143 : }
144 :
145 : /* last_core for core switching and error concealment */
146 9836413 : if ( ( st->last_core == ACELP_CORE && st->last_total_brate == FRAME_NO_DATA ) || st->prev_bfi )
147 : {
148 : /* needed for cases where first TCX frame after a certain transition (e.g. inactive SID/zero frame -> active or stereo switching) is lost */
149 335597 : st->last_core_from_bs = get_next_indice( st0, 1 );
150 :
151 : /* ACELP -> TCX_10 transitions are forbidden */
152 335597 : if ( st->core == TCX_10_CORE && st->last_core == ACELP_CORE )
153 : {
154 0 : st->last_core = TCX_20_CORE;
155 : }
156 : }
157 : else
158 : {
159 9500816 : st->last_core = get_next_indice( st0, 1 );
160 9500816 : st->last_core_from_bs = st->last_core;
161 : }
162 :
163 9836413 : getTCXWindowing( st->core, st->last_core, st->element_mode, st->hTcxCfg, st0 );
164 :
165 9836413 : st->hTcxDec->kernel_type[0] = st->hTcxDec->kernel_type[1] = MDCT_IV;
166 9836413 : st->hTcxDec->kernel_type[0] = get_next_indice( st0, st->last_core_from_bs != ACELP_CORE ? 2 : 1 );
167 9836413 : if ( st->core == TCX_10_CORE )
168 : {
169 351301 : st->hTcxDec->kernel_type[1] = 2 * ( st->hTcxDec->kernel_type[0] & 1 ) + get_next_indice( st0, 1 );
170 : }
171 9836413 : if ( st->core == TCX_20_CORE )
172 : {
173 9485112 : st->transform_type[0] = st->transform_type[1] = TCX_20;
174 : }
175 : else
176 : {
177 351301 : if ( st->hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP )
178 : {
179 234764 : st->transform_type[0] = TCX_5;
180 234764 : st->transform_type[1] = TCX_10;
181 : }
182 116537 : else if ( st->hTcxCfg->tcx_last_overlap_mode == FULL_OVERLAP )
183 : {
184 114515 : st->transform_type[0] = TCX_10;
185 114515 : st->transform_type[1] = TCX_5;
186 : }
187 : else
188 : {
189 2022 : st->transform_type[0] = st->transform_type[1] = TCX_5;
190 : }
191 : }
192 :
193 : /*--------------------------------------------------------------------------------*
194 : * TCX20/TCX10 parameters
195 : *--------------------------------------------------------------------------------*/
196 :
197 9836413 : getTCXparam( st, st0, hm_cfg, param, 0, 0, ( ( ch > 0 ) && ( tnsSize ) && ( tnsSize[0] + tnsSize[1] > 0 ) ? tnsSize : NULL ), p_param, nTnsBitsTCX10, 0 );
198 :
199 9836413 : st->side_bits_frame_channel = st0->next_bit_pos - start_bit_pos;
200 :
201 9836413 : return;
202 : }
203 :
204 :
205 : /*-----------------------------------------------------------------*
206 : * Function dec_prm_tcx_spec()
207 : *
208 : * decode TCX core parameters
209 : *-----------------------------------------------------------------*/
210 :
211 9836413 : static void dec_prm_tcx_spec(
212 : Decoder_State *st, /* i/o: decoder memory state */
213 : int16_t param[], /* o : decoded parameters */
214 : int16_t *total_nbbits, /* i/o: number of bits / decoded bits */
215 : int16_t *bitsRead, /* o : number of read bits */
216 : int16_t p_param[NB_DIV], /* o : pointer to parameters for next round of bs reading*/
217 : int16_t nTnsBitsTCX10[NB_DIV] /* i : number of TNS bits per TCX10 subframe */
218 : )
219 : {
220 : int16_t nSubframes;
221 : int16_t start_bit_pos, nbits_tcx;
222 : int16_t nf_bits;
223 : int16_t target_bitsTCX10[NB_DIV];
224 : int16_t indexBuffer[N_MAX + 1];
225 : CONTEXT_HM_CONFIG hm_cfg;
226 : #ifdef DEBUG_MODE_TCX
227 : static FILE *pF = NULL;
228 : if ( pF == NULL )
229 : pF = fopen( "./res/stereo_tcx_dec_ind.txt", "w" );
230 : #endif
231 :
232 : /*--------------------------------------------------------------------------------*
233 : * Initialization
234 : *--------------------------------------------------------------------------------*/
235 :
236 9836413 : hm_cfg.indexBuffer = indexBuffer;
237 : #ifdef DEBUG_MODE_TCX
238 : 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 ) );
239 : fprintf( pF, "stereo Common Header: %d bits\n", st->next_bit_pos );
240 : #endif
241 :
242 9836413 : start_bit_pos = st->next_bit_pos;
243 :
244 9836413 : nf_bits = 0;
245 9836413 : nbits_tcx = 0;
246 :
247 9836413 : nSubframes = ( st->core == TCX_10_CORE ) ? NB_DIV : 1;
248 :
249 : /*calculate TCX10 target bits before to assure minimum amount is distributed between subframes*/
250 9836413 : if ( st->core == TCX_10_CORE && st->element_mode == IVAS_CPE_MDCT )
251 : {
252 : int16_t nTnsBitsTCX10Tmp[NB_DIV];
253 351301 : nTnsBitsTCX10Tmp[0] = nTnsBitsTCX10[0];
254 351301 : nTnsBitsTCX10Tmp[1] = nTnsBitsTCX10[1];
255 :
256 : /*compute target bits */
257 351301 : nbits_tcx = st->bits_frame_channel + nTnsBitsTCX10[0] + nTnsBitsTCX10[1] + nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL );
258 351301 : ivas_mdct_tcx10_bit_distribution( target_bitsTCX10, nbits_tcx, nTnsBitsTCX10Tmp );
259 : }
260 :
261 : /*--------------------------------------------------------------------------------*
262 : * TCX20/TCX10 parameters
263 : *--------------------------------------------------------------------------------*/
264 :
265 9836413 : getTCXparam( st, st, hm_cfg, param, 0, 0, NULL, p_param, target_bitsTCX10, 1 );
266 :
267 9836413 : nf_bits = nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL );
268 :
269 9836413 : if ( *total_nbbits - bitsRead[0] + nf_bits < ( st->next_bit_pos - start_bit_pos ) )
270 : {
271 0 : st->BER_detect = 1;
272 0 : st->next_bit_pos = start_bit_pos + *total_nbbits - bitsRead[0];
273 : }
274 :
275 9836413 : bitsRead[0] = st->next_bit_pos - start_bit_pos;
276 :
277 9836413 : return;
278 : }
279 :
280 :
281 : /*-----------------------------------------------------------------*
282 : * Function ivas_mdct_dec_side_bits_frame_channel()
283 : *
284 : * Initialize TCX and read TCX side parameters
285 : *-----------------------------------------------------------------*/
286 :
287 5582482 : void ivas_mdct_dec_side_bits_frame_channel(
288 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
289 : int16_t param_lpc[CPE_CHANNELS][NPRM_LPC_NEW], /* o : lpc_parameters */
290 : int16_t p_param[CPE_CHANNELS][NB_DIV], /* o : pointer to param buffer */
291 : Decoder_State *st0, /* i : pointer to bitstream handle */
292 : int16_t nTnsBitsTCX10[CPE_CHANNELS][NB_DIV], /* o : number of bits for TNS */
293 : int16_t param[CPE_CHANNELS][DEC_NPRM_DIV * NB_DIV], /* i/o: parameters buffer */
294 : const int16_t MCT_flag, /* i : hMCT handle allocated (1) or not (0)*/
295 : const int16_t odd_channel_cpe /* i : flag cpe with odd nb of tc channels */
296 : )
297 : {
298 : int16_t ch, bfi;
299 : Decoder_State **sts, *st;
300 : int16_t total_nbbits[MCT_MAX_CHANNELS];
301 : int16_t bitsRead[MCT_MAX_CHANNELS];
302 : int16_t tmp;
303 : int16_t sns_low_br_mode;
304 : int16_t start_bit_pos_sns;
305 : int8_t skipped_first_channel;
306 :
307 5582482 : sts = hCPE->hCoreCoder;
308 5582482 : bfi = sts[0]->bfi;
309 :
310 5582482 : set_s( total_nbbits, 0, MCT_MAX_CHANNELS );
311 5582482 : set_s( bitsRead, 0, MCT_MAX_CHANNELS );
312 :
313 16747446 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
314 : {
315 11164964 : if ( MCT_flag )
316 : {
317 : /* Initialization or re-configuration of Stereo TCX */
318 8738260 : stereo_tcx_init_dec( sts[ch], 1, hCPE->last_element_mode );
319 : }
320 11164964 : sts[ch]->enablePlcWaveadjust = 0;
321 : }
322 :
323 5582482 : if ( !bfi )
324 : {
325 16747446 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
326 : {
327 : /* disguard reading second channel of odd transport channels, mct_chan_mode is set by default*/
328 11164964 : if ( odd_channel_cpe && ch == 1 )
329 : {
330 896826 : continue;
331 : }
332 :
333 10268138 : st = sts[ch];
334 10268138 : if ( MCT_flag )
335 : {
336 7841434 : tmp = get_next_indice( st0, 1 );
337 7841434 : if ( tmp )
338 : {
339 431725 : st->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
340 : }
341 : else
342 : {
343 7409709 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
344 : }
345 : }
346 : }
347 :
348 : /*read channel bitrate ratios from bitstream*/
349 16747446 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
350 : {
351 11164964 : st = sts[ch];
352 11164964 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
353 : {
354 1328551 : sts[ch]->coder_type = INACTIVE;
355 1328551 : sts[ch]->side_bits_frame_channel = 0;
356 1328551 : continue;
357 : }
358 :
359 9836413 : tmp = ch;
360 9836413 : if ( ch == 1 && param_lpc[0][0] == 2 )
361 : {
362 0 : tmp = 3;
363 : }
364 :
365 9836413 : dec_prm_tcx_sidebits( param[ch], st, ( ( st->element_mode == IVAS_CPE_MDCT && !MCT_flag ) ? sts[0]->hTcxDec->tnsActive : NULL ), p_param[ch], nTnsBitsTCX10[ch], st0, MCT_flag, tmp );
366 :
367 9836413 : assert( st->BER_detect != 1 );
368 : }
369 :
370 : /*--------------------------------------------------------------------------------*
371 : * SNS parameters
372 : *--------------------------------------------------------------------------------*/
373 :
374 5582482 : sns_low_br_mode = 0;
375 5582482 : skipped_first_channel = 0;
376 5582482 : if ( !MCT_flag && sts[0]->sr_core == 25600 && ( ( hCPE->element_brate == IVAS_48k || hCPE->element_brate == IVAS_64k ) ) )
377 : {
378 446486 : param_lpc[0][0] = SNS_STEREO_MODE_LR;
379 446486 : param_lpc[0][1] = SNS_STEREO_MODE_LR;
380 446486 : param_lpc[0][2] = 0;
381 446486 : param_lpc[0][3] = 0;
382 446486 : if ( sts[0]->core == sts[1]->core )
383 : {
384 : int16_t nSubframes;
385 445065 : nSubframes = ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV;
386 :
387 905426 : for ( int16_t n = 0; n < nSubframes; ++n )
388 : {
389 460361 : param_lpc[0][n] = get_next_indice( st0, 1 );
390 : }
391 :
392 : /* zero side flags only get transmitted if needed */
393 905426 : for ( int16_t n = 0; n < nSubframes; ++n )
394 : {
395 460361 : if ( param_lpc[0][n] == SNS_STEREO_MODE_MS )
396 : {
397 432216 : param_lpc[0][n + SNS_STEREO_MODE_OFFSET_INDICES / 2] = get_next_indice( st0, 1 );
398 : }
399 : }
400 : }
401 1339458 : for ( ch = 0; ch < CPE_CHANNELS; ++ch )
402 : {
403 : int16_t nSubframes;
404 : int16_t idxIndices;
405 :
406 892972 : st = sts[ch];
407 892972 : nSubframes = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
408 892972 : idxIndices = 0;
409 :
410 1817957 : for ( int16_t n = 0; n < nSubframes; ++n )
411 : {
412 924985 : const int16_t is_side = ch == 1 && param_lpc[0][n] == SNS_STEREO_MODE_MS;
413 924985 : const int16_t *bits = ( nSubframes == 1 ) ? ivas_sns_cdbks_tcx20_bits : ivas_sns_cdbks_tcx10_bits;
414 924985 : int16_t nStages = ( ( nSubframes == 1 ) ? SNS_MSVQ_NSTAGES_TCX20 : SNS_MSVQ_NSTAGES_TCX10 );
415 :
416 924985 : if ( is_side )
417 : {
418 : /* check for zero-side flag */
419 432216 : if ( param_lpc[0][n + SNS_STEREO_MODE_OFFSET_INDICES / 2] )
420 : {
421 86601 : continue;
422 : }
423 345615 : nStages = SNS_MSVQ_NSTAGES_SIDE;
424 345615 : bits = ( nSubframes == 1 ) ? ivas_sns_cdbks_side_tcx20_bits : ivas_sns_cdbks_side_tcx10_bits;
425 : }
426 :
427 3465487 : for ( int16_t j = 0; j < nStages; ++j )
428 : {
429 : /* plus one in index for stereo mode storage! */
430 2627103 : param_lpc[ch][j + idxIndices + SNS_STEREO_MODE_OFFSET_INDICES] = get_next_indice( st0, bits[j] );
431 : }
432 838384 : idxIndices += nStages;
433 : }
434 : }
435 : }
436 : else
437 : {
438 15407988 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
439 : {
440 10271992 : st = sts[ch];
441 :
442 10271992 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
443 : {
444 1328551 : skipped_first_channel = 1;
445 1328551 : continue;
446 : }
447 :
448 8943441 : start_bit_pos_sns = st0->next_bit_pos;
449 :
450 8943441 : if ( ch == 0 || skipped_first_channel )
451 : {
452 : /* read SNS stereo mode */
453 4912979 : param_lpc[0][0] = get_next_indice( st0, 1 ) << 1;
454 :
455 : /* read low br mode flag (if it is possible to be non-zero) */
456 4912979 : if ( sts[0]->element_brate == IVAS_48k && !( ( sts[0]->core == TCX_20 && sts[1]->core == TCX_20 ) ) )
457 : {
458 4923 : sns_low_br_mode = get_next_indice( st0, 1 );
459 : }
460 : }
461 :
462 8943441 : tmp = ch;
463 8943441 : if ( ch == 1 && param_lpc[0][0] == 2 )
464 : {
465 2713684 : tmp = 3;
466 : }
467 :
468 8943441 : getLPCparam( st, ¶m_lpc[ch][0], st0, tmp, sns_low_br_mode && !( sts[0]->core == TCX_20 && sts[1]->core == TCX_20 ) );
469 :
470 8943441 : st->side_bits_frame_channel += st0->next_bit_pos - start_bit_pos_sns;
471 : }
472 : }
473 : }
474 :
475 5582482 : return;
476 : }
477 :
478 :
479 : /*-----------------------------------------------------------------*
480 : * ivas_mdct_core_invQ()
481 : *
482 : * Inverse processing steps up to inverse quantization
483 : *-----------------------------------------------------------------*/
484 :
485 6080816 : void ivas_mdct_core_invQ(
486 : CPE_DEC_HANDLE hCPE, /* i/o: CPE handle */
487 : int16_t nTnsBitsTCX10[CPE_CHANNELS][NB_DIV], /* i : number of TNS bits */
488 : int16_t p_param[CPE_CHANNELS][NB_DIV], /* i : pointer to param buffer */
489 : int16_t param_lpc[CPE_CHANNELS][NPRM_LPC_NEW], /* i : lpc parameters */
490 : int16_t param[CPE_CHANNELS][DEC_NPRM_DIV * NB_DIV], /* i : param buffer */
491 : int16_t fUseTns[CPE_CHANNELS][NB_DIV], /* i : flag TNS enabled */
492 : STnsData tnsData[CPE_CHANNELS][NB_DIV], /* i : TNS parameter */
493 : float *x_0[CPE_CHANNELS][NB_DIV], /* i/o: signal buffer */
494 : float *x[CPE_CHANNELS][NB_DIV], /* i/o: signal buffer */
495 : float Aq[CPE_CHANNELS][( NB_SUBFR16k + 1 ) * ( M + 1 )], /* i : LP coefficients */
496 : int16_t ms_mask[NB_DIV][MAX_SFB], /* i : M/S mask */
497 : const int16_t MCT_flag /* i : hMCT handle allocated (1) or not (0) */
498 : )
499 : {
500 : int16_t ch, bfi, k;
501 : Decoder_State **sts, *st;
502 : /* bitstream */
503 : int16_t total_nbbits[CPE_CHANNELS];
504 : int16_t bitsRead[CPE_CHANNELS];
505 : int16_t *prm[CPE_CHANNELS];
506 : /* LPC */
507 : Word16 Aind[CPE_CHANNELS][M + 1];
508 : float sns[CPE_CHANNELS][NB_DIV][M];
509 : /* TCX */
510 : float xn_buf[L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX];
511 : int16_t tcx_offset[CPE_CHANNELS];
512 : int16_t tcx_offsetFB[CPE_CHANNELS];
513 : int16_t left_rect[CPE_CHANNELS];
514 : int16_t L_spec[CPE_CHANNELS];
515 : /* Framing */
516 : int16_t L_frame[CPE_CHANNELS], nSubframes[CPE_CHANNELS], L_frameTCX[CPE_CHANNELS];
517 : int16_t tmp_concealment_method;
518 : float gain_tcx;
519 : int16_t nf_seed;
520 : const int16_t *prm_sqQ;
521 : int16_t L_frameTCX_global[CPE_CHANNELS];
522 : float tmp_ms_sig[CPE_CHANNELS][N_MAX];
523 : float concealment_noise[CPE_CHANNELS][L_FRAME48k];
524 : TONALMDCTCONC_NOISE_GEN_MODE noise_gen_mode_bfi;
525 :
526 6080816 : push_wmops( "mdct_core_invQ" );
527 6080816 : sts = hCPE->hCoreCoder;
528 6080816 : bfi = sts[0]->bfi;
529 6080816 : noise_gen_mode_bfi = NOISE_GEN_MODE_UNDEF;
530 :
531 6080816 : set_f( xn_buf, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX );
532 6080816 : set_s( total_nbbits, 0, CPE_CHANNELS );
533 6080816 : set_s( bitsRead, 0, CPE_CHANNELS );
534 6080816 : tmp_concealment_method = 0;
535 6080816 : prm_sqQ = NULL; /* set prm_sqQ to NULL - in case of bfi == 1 it's not set or needed, but it triggers sanitizers */
536 :
537 18242448 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
538 : {
539 12161632 : sts[ch]->enablePlcWaveadjust = 0;
540 : }
541 :
542 : /* temporarily restore LR representation of previous frame for PLC mode decision (done on the individual channels) */
543 6080816 : if ( bfi && !MCT_flag && ( hCPE->hStereoMdct->mdct_stereo_mode[0] > SMDCT_DUAL_MONO || hCPE->hStereoMdct->mdct_stereo_mode[1] > SMDCT_DUAL_MONO ) )
544 : {
545 55024 : L_frameTCX[0] = sts[0]->L_frameTCX_past;
546 55024 : L_frameTCX[1] = sts[1]->L_frameTCX_past;
547 55024 : mvr2r( sts[0]->hTonalMDCTConc->lastBlockData.spectralData, tmp_ms_sig[0], L_frameTCX[0] );
548 55024 : mvr2r( sts[1]->hTonalMDCTConc->lastBlockData.spectralData, tmp_ms_sig[1], L_frameTCX[0] );
549 55024 : stereo_decoder_tcx( hCPE->hStereoMdct, ms_mask, x_0[1], &sts[0]->hTonalMDCTConc->lastBlockData.spectralData, &sts[1]->hTonalMDCTConc->lastBlockData.spectralData, &hCPE->hStereoMdct->mdct_stereo_mode[0], sts[0]->core, sts[1]->core, sts[0]->igf, L_frameTCX[0], L_frameTCX[1], 0, sts[0]->last_core, sts[1]->last_core, 1 );
550 : }
551 :
552 6080816 : if ( bfi )
553 : {
554 498334 : if ( sts[0]->core == sts[1]->core )
555 : {
556 494591 : noise_gen_mode_bfi = EQUAL_CORES;
557 : }
558 3743 : else if ( sts[0]->core == TCX_20 && sts[1]->core == TCX_10 )
559 : {
560 2960 : noise_gen_mode_bfi = TCX10_IN_0_TCX20_IN_1;
561 : }
562 783 : else if ( sts[0]->core == TCX_10 && sts[1]->core == TCX_20 )
563 : {
564 783 : noise_gen_mode_bfi = TCX20_IN_0_TCX10_IN_1;
565 : }
566 : }
567 :
568 : /* parameter decoding */
569 18242448 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
570 : {
571 12161632 : st = sts[ch];
572 :
573 12161632 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
574 : {
575 1446190 : st->total_brate = st->bits_frame_channel;
576 1446190 : continue;
577 : }
578 10715442 : prm[ch] = param[ch]; /* to avoid compilation warnings */
579 :
580 : /*Adjust bit per frame*/
581 10715442 : if ( !bfi )
582 : {
583 9836413 : st->bits_frame_core = st->bits_frame_channel;
584 :
585 9836413 : sts[ch]->total_brate = ( sts[ch]->bits_frame_channel + sts[ch]->side_bits_frame_channel + sts[ch]->core * NF_GAIN_BITS ) * FRAMES_PER_SEC;
586 : }
587 :
588 : /* Framing parameters */
589 10715442 : L_frame[ch] = st->L_frame;
590 :
591 : /*--------------------------------------------------------------------------------*
592 : * BITSTREAM DECODING
593 : *--------------------------------------------------------------------------------*/
594 :
595 10715442 : total_nbbits[ch] = st->bits_frame_channel;
596 :
597 10715442 : if ( !bfi )
598 : {
599 9836413 : st->second_last_core = st->last_core;
600 9836413 : if ( hCPE->cpe_id == 0 && ch == 0 )
601 : {
602 : /* add mct and side bits to first handle bitrate to avoid false BER detection */
603 2670257 : st->total_brate += ( st->next_bit_pos * FRAMES_PER_SEC );
604 :
605 2670257 : dec_prm_tcx_spec( st, param[ch], &total_nbbits[ch], &bitsRead[ch], p_param[ch], nTnsBitsTCX10[ch] );
606 :
607 : /*revert to actual total bitrate assigned to ch0 */
608 2670257 : sts[ch]->total_brate = ( sts[ch]->bits_frame_channel + sts[ch]->side_bits_frame_channel + sts[ch]->core * NF_GAIN_BITS ) * FRAMES_PER_SEC;
609 : }
610 : else
611 : {
612 7166156 : dec_prm_tcx_spec( st, param[ch], &total_nbbits[ch], &bitsRead[ch], p_param[ch], nTnsBitsTCX10[ch] );
613 : }
614 :
615 9836413 : assert( st->BER_detect != 1 );
616 : }
617 : else
618 : {
619 879029 : if ( st->nbLostCmpt > 1 )
620 : {
621 534946 : st->flagGuidedAcelp = 0;
622 : }
623 :
624 : /* PLC: [Common: mode decision]
625 : * PLC: Decide which Concealment to use. Update pitch lags if needed */
626 879029 : st->core = GetPLCModeDecision( st );
627 : }
628 :
629 10715442 : if ( ( !st->bfi || st->hTcxCfg->psychParamsCurrent == NULL ) && st->core > ACELP_CORE )
630 : {
631 : int16_t last_frame_was_concealed_cng;
632 9836413 : last_frame_was_concealed_cng = ( st->last_core == ACELP_CORE ) && ( st->last_core != st->last_core_from_bs );
633 9836413 : SetCurrentPsychParams( st->core, last_frame_was_concealed_cng, st->hTcxCfg );
634 : }
635 :
636 : /* PLC: [Common: Memory update]
637 : * PLC: Update the number of lost frames */
638 10715442 : if ( !bfi )
639 : {
640 9836413 : if ( st->prev_bfi == 1 )
641 : {
642 334919 : st->prev_nbLostCmpt = st->nbLostCmpt;
643 : }
644 : else
645 : {
646 9501494 : st->prev_nbLostCmpt = 0;
647 : }
648 :
649 9836413 : st->nbLostCmpt = 0;
650 : }
651 : }
652 :
653 6080816 : if ( bfi && !MCT_flag && ( hCPE->hStereoMdct->mdct_stereo_mode[0] > SMDCT_DUAL_MONO || hCPE->hStereoMdct->mdct_stereo_mode[1] > SMDCT_DUAL_MONO ) )
654 : {
655 : /* avoid using TD-PLC in only one channel when stereo mode isn't dual mono */
656 55024 : if ( sts[0]->core != sts[1]->core && ( sts[0]->core == ACELP_CORE || sts[1]->core == ACELP_CORE ) )
657 : {
658 1453 : if ( sts[0]->core == ACELP_CORE )
659 : {
660 1172 : sts[0]->core = sts[0]->last_core;
661 : }
662 281 : else if ( sts[1]->core == ACELP_CORE )
663 : {
664 281 : sts[1]->core = sts[1]->last_core;
665 : }
666 : }
667 :
668 55024 : mvr2r( tmp_ms_sig[0], sts[0]->hTonalMDCTConc->lastBlockData.spectralData, L_frameTCX[0] );
669 55024 : mvr2r( tmp_ms_sig[1], sts[1]->hTonalMDCTConc->lastBlockData.spectralData, L_frameTCX[0] );
670 : }
671 :
672 : /*--------------------------------------------------------------------------------*
673 : * LPC PARAMETERS
674 : *--------------------------------------------------------------------------------*/
675 :
676 6080816 : if ( bfi == 0 )
677 : {
678 5582482 : if ( !MCT_flag && sts[0]->sr_core == 25600 && ( ( hCPE->element_brate == IVAS_48k || hCPE->element_brate == IVAS_64k ) ) )
679 : {
680 446486 : dequantize_sns( param_lpc, sns, sts );
681 : }
682 : else
683 : {
684 5135996 : if ( sts[0]->core == TCX_20_CORE && sts[1]->core == TCX_20_CORE && sts[0]->mct_chan_mode != MCT_CHAN_MODE_IGNORE && sts[1]->mct_chan_mode != MCT_CHAN_MODE_IGNORE )
685 : {
686 3847992 : sns_avq_dec_stereo( param_lpc[0], param_lpc[1], sts[0]->L_frame, &sns[0][0][0], &sns[1][0][0] );
687 : }
688 : else
689 : {
690 3864012 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
691 : {
692 2576008 : st = sts[ch];
693 2576008 : if ( st->mct_chan_mode != MCT_CHAN_MODE_IGNORE )
694 : {
695 1247457 : sns_avq_dec( param_lpc[ch], sns[ch], st->L_frame, st->numlpc );
696 : }
697 : }
698 : }
699 : }
700 : }
701 :
702 :
703 : /*--------------------------------------------------------------*
704 : * Rate switching
705 : *---------------------------------------------------------------*/
706 :
707 18242448 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
708 : {
709 12161632 : st = sts[ch];
710 :
711 12161632 : if ( st->rate_switching_reset )
712 : {
713 17411 : lsp2a_stab( st->lsp_old, st->old_Aq_12_8, M );
714 : }
715 : }
716 :
717 : /*--------------------------------------------------------------------------------*
718 : * TCX20/10/5
719 : *--------------------------------------------------------------------------------*/
720 :
721 18242448 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
722 : {
723 12161632 : st = sts[ch];
724 :
725 12161632 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
726 : {
727 1446190 : set_f( x[ch][0], 0.f, st->hTcxCfg->tcx_coded_lines );
728 : /* usually set in decoder_tcx_invQ(), needed for concealment */
729 1446190 : st->hTcxDec->damping = 0.f;
730 1446190 : st->hTcxDec->gainHelper = 1.f;
731 1446190 : st->hTcxDec->stepCompensate = 0.f;
732 :
733 1446190 : continue;
734 : }
735 :
736 10715442 : if ( !bfi || ( bfi && st->core != ACELP_CORE ) )
737 : {
738 10463337 : nSubframes[ch] = ( st->core == TCX_10_CORE ) ? NB_DIV : 1;
739 :
740 21283579 : for ( k = 0; k < nSubframes[ch]; k++ )
741 : {
742 : /* Stability Factor */
743 10820242 : if ( !bfi )
744 : {
745 10187714 : mvr2r( sns[ch][k], &Aq[ch][k * M], M );
746 : }
747 : else
748 : {
749 632528 : st->stab_fac = 1.0f;
750 : }
751 :
752 : /* Set pointer to parameters */
753 10820242 : prm[ch] = param[ch] + ( k * DEC_NPRM_DIV );
754 10820242 : L_frameTCX_global[ch] = st->hTcxDec->L_frameTCX / nSubframes[ch];
755 10820242 : L_spec[ch] = st->hTcxCfg->tcx_coded_lines / nSubframes[ch];
756 :
757 10820242 : init_tcx_info( st, st->L_frame / nSubframes[ch], st->hTcxDec->L_frameTCX / nSubframes[ch], k, bfi, &tcx_offset[ch], &tcx_offsetFB[ch], &L_frame[ch], &L_frameTCX[ch], &left_rect[ch], &L_spec[ch] );
758 :
759 10820242 : tmp_concealment_method = 0;
760 10820242 : nf_seed = prm[ch][1 + NOISE_FILL_RANGES + LTPSIZE];
761 10820242 : if ( !bfi && st->hTcxCfg->fIsTNSAllowed && ( ch > 0 ) && ( nf_seed < 0 ) )
762 : {
763 32491 : mvs2s( param[0] + k * DEC_NPRM_DIV + 1 + NOISE_FILL_RANGES + LTPSIZE, prm[ch] + 1 + NOISE_FILL_RANGES + LTPSIZE, nf_seed * -1 );
764 : }
765 10820242 : nf_seed = 0;
766 :
767 10820242 : decoder_tcx_invQ( st, prm[ch], Aq[ch], Aind[ch], L_spec[ch], L_frame[ch], L_frameTCX[ch], x[ch][k], NULL, xn_buf, &fUseTns[ch][k], &tnsData[ch][k], &gain_tcx, &prm_sqQ, &nf_seed, bfi, k );
768 :
769 10820242 : mvr2r( x[ch][k], x_0[ch][k], L_frameTCX[ch] );
770 :
771 10820242 : if ( bfi && !MCT_flag )
772 : {
773 190923 : TonalMdctConceal_create_concealment_noise( concealment_noise[ch], hCPE, L_frameTCX[ch], L_frame[ch], ch, k, st->core, st->hTcxDec->cummulative_damping_tcx, noise_gen_mode_bfi );
774 : }
775 :
776 10820242 : decoder_tcx_noisefilling( st, concealment_noise[ch], Aq[ch], L_frameTCX_global[ch], L_spec[ch], L_frame[ch], L_frameTCX[ch], x[ch][k], NULL, &tmp_concealment_method, gain_tcx, prm_sqQ, nf_seed, bfi, MCT_flag, k );
777 :
778 10820242 : decoder_tcx_noiseshaping_igf( st, L_spec[ch], L_frame[ch], L_frameTCX[ch], left_rect[ch], x[ch][k], NULL, &tmp_concealment_method, bfi );
779 : }
780 : }
781 : }
782 :
783 6080816 : pop_wmops();
784 6080816 : return;
785 : }
786 :
787 :
788 : /*-----------------------------------------------------------------*
789 : * ivas_mdct_core_reconstruct()
790 : *
791 : * reconstruct time signal
792 : *-----------------------------------------------------------------*/
793 :
794 6080816 : void ivas_mdct_core_reconstruct(
795 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
796 : float *x[][NB_DIV], /* i/o: synthesis @internal_FS */
797 : float signal_outFB[CPE_CHANNELS][L_FRAME_PLUS], /* o : synthesis @output_FS */
798 : int16_t fUseTns[CPE_CHANNELS][NB_DIV], /* i : flage TNS enabled */
799 : const int16_t MCT_flag /* i : hMCT handle allocated (1) or not (0)*/
800 : )
801 : {
802 : int16_t ch, k, bfi;
803 : Decoder_State **sts, *st;
804 : /* Framing */
805 : int16_t L_frame[CPE_CHANNELS], L_frameTCX[CPE_CHANNELS], nSubframes[CPE_CHANNELS];
806 : int16_t L_frame_global[CPE_CHANNELS], L_frame_globalTCX[CPE_CHANNELS];
807 : /* Synth */
808 : float synth_buf[OLD_SYNTH_INTERNAL_DEC + L_FRAME_PLUS_INTERNAL + M];
809 : float *synth;
810 : float synth_bufFB[OLD_SYNTH_SIZE_DEC + L_FRAME_PLUS + M];
811 : float *synthFB;
812 : /* TCX */
813 : float xn_buf[L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX];
814 : int16_t tcx_offset[CPE_CHANNELS];
815 : int16_t tcx_offsetFB[CPE_CHANNELS];
816 : int16_t left_rect[CPE_CHANNELS];
817 : int16_t L_spec[CPE_CHANNELS];
818 :
819 : int16_t pitch[CPE_CHANNELS][NB_SUBFR16k];
820 : float pit_gain[CPE_CHANNELS][NB_SUBFR16k];
821 : int16_t skip_decoding;
822 :
823 6080816 : set_f( xn_buf, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX );
824 :
825 : /* Initializations */
826 6080816 : sts = hCPE->hCoreCoder;
827 6080816 : bfi = sts[0]->bfi;
828 :
829 : /* TNS, ITF, IMDCT and updates */
830 18242448 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
831 : {
832 12161632 : st = sts[ch];
833 :
834 12161632 : skip_decoding = 0;
835 12161632 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
836 : {
837 1471978 : skip_decoding = 1;
838 : }
839 :
840 12161632 : nSubframes[ch] = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
841 12161632 : synth = synth_buf + st->hTcxDec->old_synth_len;
842 12161632 : synthFB = synth_bufFB + st->hTcxDec->old_synth_lenFB;
843 12161632 : mvr2r( st->hTcxDec->old_synth, synth_buf, st->hTcxDec->old_synth_len );
844 12161632 : mvr2r( st->hTcxDec->old_synthFB, synth_bufFB, st->hTcxDec->old_synth_lenFB );
845 12161632 : set_zero( synth, L_FRAME_PLUS_INTERNAL + M );
846 12161632 : set_zero( synthFB, L_FRAME_PLUS + M );
847 :
848 12161632 : if ( st->core != ACELP_CORE )
849 : {
850 24175959 : for ( k = 0; k < nSubframes[ch]; k++ )
851 : {
852 12266432 : L_spec[ch] = st->hTcxCfg->tcx_coded_lines / nSubframes[ch];
853 12266432 : L_frame_global[ch] = st->L_frame / nSubframes[ch];
854 12266432 : L_frame_globalTCX[ch] = st->hTcxDec->L_frameTCX / nSubframes[ch];
855 :
856 12266432 : init_tcx_info( st, L_frame_global[ch], L_frame_globalTCX[ch], k, bfi, &tcx_offset[ch],
857 12266432 : &tcx_offsetFB[ch], &L_frame[ch], &L_frameTCX[ch], &left_rect[ch], &L_spec[ch] );
858 :
859 12266432 : if ( !skip_decoding )
860 : {
861 10793776 : decoder_tcx_imdct( st, L_frame_global[ch], L_frame_globalTCX[ch], L_spec[ch], tcx_offset[ch], tcx_offsetFB[ch], L_frame[ch], L_frameTCX[ch], left_rect[ch], &x[ch][k][0], xn_buf,
862 10793776 : ( ( hCPE->nchan_out == 1 && st->hTcxDec->kernel_type[k] == MDST_IV ) || st->hTcxCfg->tcx_last_overlap_mode == TRANSITION_OVERLAP ) ? MDCT_IV : st->hTcxDec->kernel_type[k],
863 10793776 : fUseTns[ch][k], &synth[k * L_frame[ch]], &synthFB[k * L_frameTCX[ch]], bfi, k, 0 );
864 : }
865 : else
866 : {
867 1472656 : set_f( &synth[k * L_frame[ch]], 0.f, L_frame[ch] );
868 1472656 : set_f( &synthFB[k * L_frame[ch]], 0.f, L_frameTCX[ch] );
869 : /* Note: these buffers are not subframe-based, hence no indexing with k */
870 1472656 : set_f( &st->hHQ_core->old_outLB[0], 0.f, L_frame[ch] );
871 1472656 : set_f( &st->hHQ_core->old_out[0], 0.f, L_frameTCX[ch] );
872 1472656 : set_f( &st->hTcxDec->syn_Overl[0], 0.f, L_frame[ch] / 2 );
873 1472656 : set_f( &st->hTcxDec->syn_OverlFB[0], 0.f, L_frameTCX[ch] / 2 );
874 1472656 : set_f( &st->hTcxDec->syn_Overl_TDAC[0], 0.f, L_frame[ch] / 2 );
875 1472656 : set_f( &st->hTcxDec->syn_Overl_TDACFB[0], 0.f, L_frameTCX[ch] / 2 );
876 : }
877 : }
878 :
879 11909527 : if ( !bfi && st->hTonalMDCTConc != NULL )
880 : {
881 11164964 : TonalMDCTConceal_SaveTimeSignal( st->hTonalMDCTConc, synthFB, L_frameTCX[ch] );
882 : }
883 :
884 11909527 : decoder_tcx_post( st, synth, synthFB, NULL, bfi, MCT_flag );
885 : }
886 : else /*ACELP core for ACELP-PLC */
887 : {
888 252105 : assert( st->bfi == 1 );
889 : /* PLC: [TCX: TD PLC] */
890 252105 : if ( MCT_flag )
891 : {
892 220516 : con_tcx( st, &synthFB[0], -1.f, NULL, 0, NULL );
893 : }
894 : else
895 : {
896 31589 : con_tcx( st, &synthFB[0], hCPE->hStereoMdct->lastCoh, &sts[0]->seed_acelp, ( sts[1]->core != ACELP_CORE ) ? 1 : 0, &st->hFdCngDec->hFdCngCom->A_cng[0] );
897 : }
898 :
899 252105 : lerp( synthFB, synth, st->L_frame, st->hTcxDec->L_frameTCX );
900 252105 : st->con_tcx = 1;
901 252105 : set_f( &st->mem_pitch_gain[2], st->lp_gainp, st->nb_subfr );
902 :
903 : /* PLC: [TCX: Tonal Concealment] */
904 : /* Signal that this frame is not TCX */
905 252105 : if ( st->hTonalMDCTConc != NULL )
906 : {
907 252105 : TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, 0, 0, 0, 0 );
908 : }
909 : }
910 :
911 : /*--------------------------------------------------------------------------------*
912 : * Updates
913 : *--------------------------------------------------------------------------------*/
914 :
915 12161632 : st = sts[ch];
916 :
917 12161632 : if ( bfi && st->last_core != ACELP_CORE && st->core == ACELP_CORE )
918 : {
919 : /* Update FEC_scale_syn parameters */
920 252105 : if ( st->hTcxLtpDec->tcxltp_gain == 0 )
921 : {
922 0 : fer_energy( st->L_frame, UNVOICED, synth, (float) ( st->L_frame / 2 ), &st->enr_old, st->L_frame );
923 : }
924 : else
925 : {
926 252105 : fer_energy( st->L_frame, st->clas_dec, synth, st->old_fpitch, &st->enr_old, st->L_frame );
927 : }
928 : }
929 :
930 : /* Update */
931 12161632 : mvr2r( synth_buf + st->L_frame, st->hTcxDec->old_synth, st->hTcxDec->old_synth_len );
932 12161632 : mvr2r( st->hTcxDec->old_synthFB + st->hTcxDec->L_frameTCX - NS2SA( st->output_Fs, PH_ECU_MEM_NS ), st->hTcxDec->synth_history, NS2SA( st->output_Fs, PH_ECU_MEM_NS ) );
933 12161632 : mvr2r( synth_bufFB + st->hTcxDec->L_frameTCX, st->hTcxDec->old_synthFB, st->hTcxDec->old_synth_lenFB );
934 12161632 : if ( st->hHQ_core != NULL )
935 : {
936 12161632 : mvr2r( st->hHQ_core->old_out + NS2SA( st->output_Fs, N_ZERO_MDCT_NS ), st->hTcxDec->old_synthFB + st->hTcxDec->old_synth_lenFB, NS2SA( st->output_Fs, PH_ECU_LOOKAHEAD_NS ) );
937 : }
938 :
939 12161632 : mvr2r( st->lsp_q_cng, st->old_lsp_q_cng, M );
940 12161632 : mvr2r( st->lsf_q_cng, st->old_lsf_q_cng, M );
941 :
942 12161632 : st->last_is_cng = 0;
943 :
944 : /* Postfiltering */
945 12161632 : post_decoder( st, synth_buf, pit_gain[ch], pitch[ch], x[ch][0], st->p_bpf_noise_buf );
946 :
947 12161632 : mvr2r( synthFB, signal_outFB[ch], st->hTcxDec->L_frameTCX );
948 : #ifdef DEBUG_PLC_INFO
949 : {
950 : int16_t i;
951 : int16_t tmp[L_FRAME48k];
952 : static FILE *sP = NULL;
953 :
954 : for ( i = 0; i < st->hTcxDec->L_frameTCX; i++ )
955 : {
956 : tmp[i] = (int16_t) ( signal_outFB[ch][i] + 0.5f );
957 : }
958 : if ( ch == 0 )
959 : {
960 : dbgwrite( tmp, sizeof( int16_t ), st->hTcxDec->L_frameTCX, 1, "./res/mdct_stereo_dec_synthFB_ch0.pcm" );
961 : #ifndef DEBUG_MODE_INFO
962 : dbgwrite( &st->bfi, sizeof( int16_t ), 1, st->hTcxDec->L_frameTCX, "./res/bfi.pcm" );
963 : #endif
964 : dbgwrite( &st->tonal_mdct_plc_active, sizeof( int16_t ), 1, L_frameTCX[ch], "./res/tonal_mdct_active_ch0.pcm" );
965 : dbgwrite( &st->tonal_mdct_plc_active, sizeof( int16_t ), 1, L_frameTCX[ch], "./res/tonal_mdct_active_ch0.pcm" );
966 : dbgwrite( &st->con_tcx, sizeof( int16_t ), 1, 640, "./res/con_tcx_ch0.pcm" );
967 : }
968 : else
969 : {
970 : dbgwrite( tmp, sizeof( int16_t ), st->hTcxDec->L_frameTCX, 1, "./res/mdct_stereo_dec_synthFB_ch1.pcm" );
971 : dbgwrite( &st->tonal_mdct_plc_active, sizeof( int16_t ), 1, L_frameTCX[ch], "./res/tonal_mdct_active_ch1.pcm" );
972 : dbgwrite( &st->con_tcx, sizeof( int16_t ), 1, 640, "./res/con_tcx_ch1.pcm" );
973 : }
974 : }
975 : #endif
976 :
977 : /* updates */
978 12161632 : st->last_voice_factor = 0.0f;
979 12161632 : st->last_coder_type = st->coder_type;
980 : }
981 :
982 : /* calculate coherence of signal needed when next frame is lost */
983 6080816 : if ( !bfi && !MCT_flag && hCPE->element_mode == IVAS_CPE_MDCT )
984 : {
985 : int16_t i;
986 : float nrgL, nrgR, xcorr;
987 :
988 1213352 : nrgL = nrgR = xcorr = EPSILON;
989 :
990 831221032 : for ( i = 0; i < sts[0]->hTcxDec->L_frameTCX; i++ )
991 : {
992 830007680 : nrgL += signal_outFB[0][i] * signal_outFB[0][i];
993 830007680 : nrgR += signal_outFB[1][i] * signal_outFB[1][i];
994 830007680 : xcorr += signal_outFB[0][i] * signal_outFB[1][i];
995 : }
996 :
997 1213352 : hCPE->hStereoMdct->lastCoh = fabsf( xcorr ) * inv_sqrt( nrgL * nrgR );
998 1213352 : hCPE->hStereoMdct->lastCoh = min( hCPE->hStereoMdct->lastCoh, 1.f );
999 : }
1000 :
1001 6080816 : return;
1002 : }
1003 :
1004 :
1005 : /*-----------------------------------------------------------------*
1006 : * ivas_mdct_core_tns_ns()
1007 : *
1008 : * reconstruct time signal
1009 : *-----------------------------------------------------------------*/
1010 :
1011 6080816 : void ivas_mdct_core_tns_ns(
1012 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
1013 : int16_t fUseTns[CPE_CHANNELS][NB_DIV], /* i : two entries for each channel in TCX10 */
1014 : STnsData tnsData[CPE_CHANNELS][NB_DIV], /* o : TNS parameter */
1015 : float *x[CPE_CHANNELS][NB_DIV], /* o : synthesis @internal_FS */
1016 : float Aq[CPE_CHANNELS][( NB_SUBFR16k + 1 ) * ( M + 1 )], /* o : LP coefficients */
1017 : const int16_t MCT_flag /* i : hMCT handle allocated (1) or not (0) */
1018 : )
1019 : {
1020 : int16_t ch, k, bfi;
1021 : Decoder_State **sts, *st;
1022 : /* Framing */
1023 : int16_t L_frame[CPE_CHANNELS], L_frameTCX[CPE_CHANNELS], nSubframes[CPE_CHANNELS];
1024 : int16_t L_frame_global[CPE_CHANNELS], L_frameTCX_glob[CPE_CHANNELS];
1025 :
1026 : /* TCX */
1027 : float xn_buf[L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX];
1028 : int16_t tcx_offset[CPE_CHANNELS];
1029 : int16_t tcx_offsetFB[CPE_CHANNELS];
1030 : int16_t left_rect[CPE_CHANNELS];
1031 : int16_t L_spec[CPE_CHANNELS];
1032 : float sns_int_scf[FDNS_NPTS];
1033 :
1034 : /* Initializations */
1035 6080816 : sts = hCPE->hCoreCoder;
1036 6080816 : bfi = sts[0]->bfi;
1037 :
1038 6080816 : set_f( xn_buf, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX );
1039 :
1040 :
1041 : /* TNS, ITF, IMDCT and updates */
1042 18242448 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
1043 : {
1044 12161632 : st = sts[ch];
1045 12161632 : nSubframes[ch] = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
1046 12161632 : L_frame_global[ch] = st->L_frame / nSubframes[ch];
1047 12161632 : L_frameTCX_glob[ch] = st->hTcxDec->L_frameTCX / nSubframes[ch];
1048 12161632 : L_spec[ch] = st->hTcxCfg->tcx_coded_lines / nSubframes[ch];
1049 :
1050 12161632 : if ( ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE ) || ( st->bfi && st->core == ACELP_CORE ) ) /* indicates LFE with no content, or odd number of channels */
1051 : {
1052 1724083 : if ( st->hTonalMDCTConc != NULL )
1053 : {
1054 1724083 : TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, st->hTcxDec->L_frameTCX, 0, bfi, 0 );
1055 : }
1056 :
1057 : /* nothing to do for missing LFE */
1058 1724083 : continue;
1059 : }
1060 :
1061 21231325 : for ( k = 0; k < nSubframes[ch]; k++ )
1062 : {
1063 10793776 : init_tcx_info( st, L_frame_global[ch], L_frameTCX_glob[ch], k, bfi, &tcx_offset[ch],
1064 10793776 : &tcx_offsetFB[ch], &L_frame[ch], &L_frameTCX[ch], &left_rect[ch], &L_spec[ch] );
1065 :
1066 10793776 : if ( !bfi )
1067 : {
1068 10161248 : sns_interpolate_scalefactors( &sns_int_scf[0], &Aq[ch][k * M], DEC );
1069 :
1070 10161248 : if ( MCT_flag && st->hTonalMDCTConc != NULL && ( ( k + 1 ) == nSubframes[ch] ) )
1071 : {
1072 7383921 : TonalMDCTConceal_SaveFreqSignal( st->hTonalMDCTConc, x[ch][k], L_frameTCX[ch], L_frame[ch], &sns_int_scf[0], get_igf_startline( st, L_frame[ch], L_frameTCX[ch] ) );
1073 : }
1074 : }
1075 : else
1076 : {
1077 632528 : if ( st->hTonalMDCTConc != NULL )
1078 : {
1079 632528 : if ( !MCT_flag && st->hTcxDec->cummulative_damping_tcx != 1.f )
1080 123675 : {
1081 : float *scf_last, *scf_bg;
1082 : float fade_in, fade_out;
1083 :
1084 123675 : scf_last = &st->hTonalMDCTConc->lastBlockData.scaleFactors[0];
1085 123675 : scf_bg = &st->hTonalMDCTConc->scaleFactorsBackground[0];
1086 :
1087 123675 : st->hTonalMDCTConc->scf_fadeout *= 0.95f;
1088 :
1089 123675 : fade_out = st->hTonalMDCTConc->scf_fadeout;
1090 123675 : fade_in = 1 - fade_out;
1091 :
1092 8038875 : for ( int16_t i = 0; i < st->hTonalMDCTConc->nScaleFactors; i++ )
1093 : {
1094 7915200 : sns_int_scf[i] = fade_out * scf_last[i] + fade_in * scf_bg[i];
1095 : }
1096 : }
1097 : else
1098 : {
1099 508853 : st->hTonalMDCTConc->scf_fadeout = 1.0f;
1100 508853 : mvr2r( st->hTonalMDCTConc->lastBlockData.scaleFactors, &sns_int_scf[0], st->hTonalMDCTConc->nScaleFactors );
1101 : }
1102 : }
1103 : }
1104 :
1105 10793776 : decoder_tcx_tns( st, L_frame_global[ch], L_spec[ch], L_frame[ch], L_frameTCX[ch], &x[ch][k][0], fUseTns[ch][k], &tnsData[ch][k], bfi, k, 1 );
1106 :
1107 10793776 : sns_shape_spectrum( x[ch][k], st->hTcxCfg->psychParamsCurrent, &sns_int_scf[0], st->hTcxCfg->psychParamsCurrent->nBins );
1108 : /*
1109 : 2025-09-07, mul:
1110 : in case of PLC, applying SNS up to L_spec might not be enough : In case the transition frame from DTX after an inactive period is lost, L_spec is assumed to represent a regular TCX frame, however, this frame is nevertheless acting as an transition frame as also visible in L_frameTCX;
1111 : thus, the safer approach to prevent high frequency artifacts is to apply the SNS up to L_frameTCX;
1112 : in case this is not necessary, x[] is filled with zeros, and the multiplication is not causing any additional harm * /
1113 : */
1114 10793776 : v_multc( x[ch][k] + st->hTcxCfg->psychParamsCurrent->nBins, sns_int_scf[FDNS_NPTS - 1], x[ch][k] + st->hTcxCfg->psychParamsCurrent->nBins, max( L_spec[ch], L_frameTCX[ch] ) - st->hTcxCfg->psychParamsCurrent->nBins );
1115 :
1116 10793776 : decoder_tcx_tns( st, L_frame_global[ch], L_spec[ch], L_frame[ch], L_frameTCX[ch], &x[ch][k][0], fUseTns[ch][k], &tnsData[ch][k], bfi, k, 0 );
1117 : }
1118 :
1119 10437549 : if ( bfi && st->tonal_mdct_plc_active )
1120 : {
1121 6342 : TonalMDCTConceal_Apply( st->hTonalMDCTConc, x[ch][0], st->hTcxCfg->psychParamsCurrent );
1122 : }
1123 :
1124 10437549 : if ( ( bfi || MCT_flag ) && st->hTonalMDCTConc != NULL )
1125 : {
1126 8010845 : TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, L_frameTCX[ch], ( st->hTcxDec->tcxltp_last_gain_unmodified > 0 ) ? st->old_fpitch : 0, bfi, bfi && st->tonal_mdct_plc_active );
1127 : }
1128 : }
1129 :
1130 6080816 : return;
1131 : }
|