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 21776580 : 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 21776580 : start = st0->next_bit_pos;
66 :
67 21776580 : if ( st->core == TCX_20_CORE )
68 : {
69 : /* read IGF payload */
70 21170295 : IGFDecReadLevel( st->hIGFDec, st0, ( st->last_core == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, 1 );
71 :
72 21170295 : IGFDecReadData( st->hIGFDec, st0, ( st->last_core == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, 1 );
73 : }
74 :
75 21776580 : if ( st->core == TCX_10_CORE )
76 : {
77 1818855 : for ( k = 0; k < 2; k++ )
78 : {
79 1212570 : IGFDecReadLevel( st->hIGFDec, st0, IGF_GRID_LB_SHORT, k == 0 ? 1 : 0 );
80 :
81 1212570 : IGFDecReadData( st->hIGFDec, st0, IGF_GRID_LB_SHORT, k == 0 ? 1 : 0 );
82 :
83 1212570 : IGFDecStoreTCX10SubFrameData( st->hIGFDec, k );
84 : }
85 : }
86 :
87 21776580 : st->side_bits_frame_channel += ( st0->next_bit_pos - start );
88 :
89 21776580 : return;
90 : }
91 :
92 :
93 : /*-----------------------------------------------------------------*
94 : * Function dec_prm_tcx_sidebits()
95 : *
96 : * decode TCX side parameters
97 : *-----------------------------------------------------------------*/
98 :
99 37636496 : 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 37636496 : hm_cfg.indexBuffer = NULL;
124 :
125 37636496 : start_bit_pos = st0->next_bit_pos;
126 :
127 : /* Init LTP data */
128 37636496 : st->hTcxDec->tcx_hm_LtpPitchLag = -1;
129 37636496 : st->hTcxLtpDec->tcxltp_gain = 0.0f;
130 :
131 : /*--------------------------------------------------------------------------------*
132 : * Header
133 : *--------------------------------------------------------------------------------*/
134 :
135 : /* Modes (ACE_GC, ACE_UC, TCX20, TCX10...) */
136 37636496 : getTCXMode( st, st0, MCT_flag );
137 :
138 37636496 : st->flagGuidedAcelp = 0;
139 :
140 37636496 : if ( st->dec_glr )
141 : {
142 0 : st->dec_glr_idx = -1;
143 : }
144 :
145 : /* last_core for core switching and error concealment */
146 37636496 : 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 815253 : st->last_core_from_bs = get_next_indice( st0, 1 );
150 :
151 : /* ACELP -> TCX_10 transitions are forbidden */
152 815253 : if ( st->core == TCX_10_CORE && st->last_core == ACELP_CORE )
153 : {
154 143 : st->last_core = TCX_20_CORE;
155 : }
156 : }
157 : else
158 : {
159 36821243 : st->last_core = get_next_indice( st0, 1 );
160 36821243 : st->last_core_from_bs = st->last_core;
161 : }
162 :
163 : /* for first frame reset last_core to initialization value for better error-robustness*/
164 37636496 : if ( st->ini_frame == 0 )
165 : {
166 361724 : st->last_core = TCX_20_CORE;
167 : }
168 :
169 37636496 : getTCXWindowing( st->core, st->last_core, st->element_mode, st->hTcxCfg, st0 );
170 :
171 37636496 : st->hTcxDec->kernel_type[0] = st->hTcxDec->kernel_type[1] = MDCT_IV;
172 37636496 : st->hTcxDec->kernel_type[0] = get_next_indice( st0, st->last_core_from_bs != ACELP_CORE ? 2 : 1 );
173 37636496 : if ( st->core == TCX_10_CORE )
174 : {
175 985677 : st->hTcxDec->kernel_type[1] = 2 * ( st->hTcxDec->kernel_type[0] & 1 ) + get_next_indice( st0, 1 );
176 : }
177 37636496 : if ( st->core == TCX_20_CORE )
178 : {
179 36650819 : st->transform_type[0] = st->transform_type[1] = TCX_20;
180 : }
181 : else
182 : {
183 985677 : if ( st->hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP )
184 : {
185 619334 : st->transform_type[0] = TCX_5;
186 619334 : st->transform_type[1] = TCX_10;
187 : }
188 366343 : else if ( st->hTcxCfg->tcx_last_overlap_mode == FULL_OVERLAP )
189 : {
190 342636 : st->transform_type[0] = TCX_10;
191 342636 : st->transform_type[1] = TCX_5;
192 : }
193 : else
194 : {
195 23707 : st->transform_type[0] = st->transform_type[1] = TCX_5;
196 : }
197 : }
198 :
199 : /*--------------------------------------------------------------------------------*
200 : * TCX20/TCX10 parameters
201 : *--------------------------------------------------------------------------------*/
202 :
203 37636496 : getTCXparam( st, st0, hm_cfg, param, 0, 0, ( ( ch > 0 ) && ( tnsSize ) && ( tnsSize[0] + tnsSize[1] > 0 ) ? tnsSize : NULL ), p_param, nTnsBitsTCX10, 0 );
204 :
205 37636496 : st->side_bits_frame_channel = st0->next_bit_pos - start_bit_pos;
206 :
207 37636496 : return;
208 : }
209 :
210 :
211 : /*-----------------------------------------------------------------*
212 : * Function dec_prm_tcx_spec()
213 : *
214 : * decode TCX core parameters
215 : *-----------------------------------------------------------------*/
216 :
217 37636496 : static void dec_prm_tcx_spec(
218 : Decoder_State *st, /* i/o: decoder memory state */
219 : int16_t param[], /* o : decoded parameters */
220 : int16_t *total_nbbits, /* i/o: number of bits / decoded bits */
221 : int16_t *bitsRead, /* o : number of read bits */
222 : int16_t p_param[NB_DIV], /* o : pointer to parameters for next round of bs reading*/
223 : int16_t nTnsBitsTCX10[NB_DIV] /* i : number of TNS bits per TCX10 subframe */
224 : )
225 : {
226 : int16_t nSubframes;
227 : int16_t start_bit_pos, nbits_tcx;
228 : int16_t nf_bits;
229 : int16_t target_bitsTCX10[NB_DIV];
230 : int16_t indexBuffer[N_MAX + 1];
231 : CONTEXT_HM_CONFIG hm_cfg;
232 : #ifdef DEBUG_MODE_TCX
233 : static FILE *pF = NULL;
234 : if ( pF == NULL )
235 : pF = fopen( "./res/stereo_tcx_dec_ind.txt", "w" );
236 : #endif
237 :
238 : /*--------------------------------------------------------------------------------*
239 : * Initialization
240 : *--------------------------------------------------------------------------------*/
241 :
242 37636496 : hm_cfg.indexBuffer = indexBuffer;
243 : #ifdef DEBUG_MODE_TCX
244 : 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 ) );
245 : fprintf( pF, "stereo Common Header: %d bits\n", st->next_bit_pos );
246 : #endif
247 :
248 37636496 : start_bit_pos = st->next_bit_pos;
249 :
250 37636496 : nf_bits = 0;
251 37636496 : nbits_tcx = 0;
252 :
253 37636496 : nSubframes = ( st->core == TCX_10_CORE ) ? NB_DIV : 1;
254 :
255 : /*calculate TCX10 target bits before to assure minimum amount is distributed between subframes*/
256 37636496 : if ( st->core == TCX_10_CORE && st->element_mode == IVAS_CPE_MDCT )
257 : {
258 : int16_t nTnsBitsTCX10Tmp[NB_DIV];
259 985677 : nTnsBitsTCX10Tmp[0] = nTnsBitsTCX10[0];
260 985677 : nTnsBitsTCX10Tmp[1] = nTnsBitsTCX10[1];
261 :
262 : /*compute target bits */
263 985677 : nbits_tcx = st->bits_frame_channel + nTnsBitsTCX10[0] + nTnsBitsTCX10[1] + nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL );
264 985677 : ivas_mdct_tcx10_bit_distribution( target_bitsTCX10, nbits_tcx, nTnsBitsTCX10Tmp );
265 : }
266 :
267 : /*--------------------------------------------------------------------------------*
268 : * TCX20/TCX10 parameters
269 : *--------------------------------------------------------------------------------*/
270 :
271 37636496 : getTCXparam( st, st, hm_cfg, param, 0, 0, NULL, p_param, target_bitsTCX10, 1 );
272 :
273 37636496 : nf_bits = nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL );
274 :
275 37636496 : if ( *total_nbbits - bitsRead[0] + nf_bits < ( st->next_bit_pos - start_bit_pos ) )
276 : {
277 0 : st->BER_detect = 1;
278 0 : st->next_bit_pos = start_bit_pos + *total_nbbits - bitsRead[0];
279 : }
280 :
281 37636496 : bitsRead[0] = st->next_bit_pos - start_bit_pos;
282 :
283 37636496 : return;
284 : }
285 :
286 :
287 : /*-----------------------------------------------------------------*
288 : * Function ivas_mdct_dec_side_bits_frame_channel()
289 : *
290 : * Initialize TCX and read TCX side parameters
291 : *-----------------------------------------------------------------*/
292 :
293 20900294 : void ivas_mdct_dec_side_bits_frame_channel(
294 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
295 : int16_t param_lpc[CPE_CHANNELS][NPRM_LPC_NEW], /* o : lpc_parameters */
296 : int16_t p_param[CPE_CHANNELS][NB_DIV], /* o : pointer to param buffer */
297 : Decoder_State *st0, /* i : pointer to bitstream handle */
298 : int16_t nTnsBitsTCX10[CPE_CHANNELS][NB_DIV], /* o : number of bits for TNS */
299 : int16_t param[CPE_CHANNELS][DEC_NPRM_DIV * NB_DIV], /* i/o: parameters buffer */
300 : const int16_t MCT_flag, /* i : hMCT handle allocated (1) or not (0)*/
301 : const int16_t odd_channel_cpe /* i : flag cpe with odd nb of tc channels */
302 : )
303 : {
304 : int16_t ch, bfi;
305 : Decoder_State **sts, *st;
306 : int16_t total_nbbits[MCT_MAX_CHANNELS];
307 : int16_t bitsRead[MCT_MAX_CHANNELS];
308 : int16_t tmp;
309 : int16_t sns_low_br_mode;
310 : int16_t start_bit_pos_sns;
311 : int8_t skipped_first_channel;
312 :
313 20900294 : sts = hCPE->hCoreCoder;
314 20900294 : bfi = sts[0]->bfi;
315 :
316 20900294 : set_s( total_nbbits, 0, MCT_MAX_CHANNELS );
317 20900294 : set_s( bitsRead, 0, MCT_MAX_CHANNELS );
318 :
319 62700882 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
320 : {
321 41800588 : if ( MCT_flag )
322 : {
323 : /* Initialization or re-configuration of Stereo TCX */
324 31474168 : stereo_tcx_init_dec( sts[ch], 1, hCPE->last_element_mode );
325 : }
326 41800588 : sts[ch]->enablePlcWaveadjust = 0;
327 : }
328 :
329 20900294 : if ( !bfi )
330 : {
331 62700882 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
332 : {
333 : /* disguard reading second channel of odd transport channels, mct_chan_mode is set by default*/
334 41800588 : if ( odd_channel_cpe && ch == 1 )
335 : {
336 3325760 : continue;
337 : }
338 :
339 38474828 : st = sts[ch];
340 38474828 : if ( MCT_flag )
341 : {
342 28148408 : tmp = get_next_indice( st0, 1 );
343 28148408 : if ( tmp )
344 : {
345 838332 : st->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
346 : }
347 : else
348 : {
349 27310076 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
350 : }
351 : }
352 : }
353 :
354 : /*read channel bitrate ratios from bitstream*/
355 62700882 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
356 : {
357 41800588 : st = sts[ch];
358 41800588 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
359 : {
360 4164092 : sts[ch]->coder_type = INACTIVE;
361 4164092 : sts[ch]->side_bits_frame_channel = 0;
362 4164092 : continue;
363 : }
364 :
365 37636496 : tmp = ch;
366 37636496 : if ( ch == 1 && param_lpc[0][0] == 2 )
367 : {
368 0 : tmp = 3;
369 : }
370 :
371 37636496 : 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 );
372 :
373 37636496 : assert( st->BER_detect != 1 );
374 : }
375 :
376 : /*--------------------------------------------------------------------------------*
377 : * SNS parameters
378 : *--------------------------------------------------------------------------------*/
379 :
380 20900294 : sns_low_br_mode = 0;
381 20900294 : skipped_first_channel = 0;
382 20900294 : if ( !MCT_flag && sts[0]->sr_core == 25600 && ( ( hCPE->element_brate == IVAS_48k || hCPE->element_brate == IVAS_64k ) ) )
383 : {
384 2479872 : param_lpc[0][0] = SNS_STEREO_MODE_LR;
385 2479872 : param_lpc[0][1] = SNS_STEREO_MODE_LR;
386 2479872 : param_lpc[0][2] = 0;
387 2479872 : param_lpc[0][3] = 0;
388 2479872 : if ( sts[0]->core == sts[1]->core )
389 : {
390 : int16_t nSubframes;
391 2465156 : nSubframes = ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV;
392 :
393 4994621 : for ( int16_t n = 0; n < nSubframes; ++n )
394 : {
395 2529465 : param_lpc[0][n] = get_next_indice( st0, 1 );
396 : }
397 :
398 : /* zero side flags only get transmitted if needed */
399 4994621 : for ( int16_t n = 0; n < nSubframes; ++n )
400 : {
401 2529465 : if ( param_lpc[0][n] == SNS_STEREO_MODE_MS )
402 : {
403 2180329 : param_lpc[0][n + SNS_STEREO_MODE_OFFSET_INDICES / 2] = get_next_indice( st0, 1 );
404 : }
405 : }
406 : }
407 7439616 : for ( ch = 0; ch < CPE_CHANNELS; ++ch )
408 : {
409 : int16_t nSubframes;
410 : int16_t idxIndices;
411 :
412 4959744 : st = sts[ch];
413 4959744 : nSubframes = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
414 4959744 : idxIndices = 0;
415 :
416 10062822 : for ( int16_t n = 0; n < nSubframes; ++n )
417 : {
418 5103078 : const int16_t is_side = ch == 1 && param_lpc[0][n] == SNS_STEREO_MODE_MS;
419 5103078 : const int16_t *bits = ( nSubframes == 1 ) ? ivas_sns_cdbks_tcx20_bits : ivas_sns_cdbks_tcx10_bits;
420 5103078 : int16_t nStages = ( ( nSubframes == 1 ) ? SNS_MSVQ_NSTAGES_TCX20 : SNS_MSVQ_NSTAGES_TCX10 );
421 :
422 5103078 : if ( is_side )
423 : {
424 : /* check for zero-side flag */
425 2180329 : if ( param_lpc[0][n + SNS_STEREO_MODE_OFFSET_INDICES / 2] )
426 : {
427 564174 : continue;
428 : }
429 1616155 : nStages = SNS_MSVQ_NSTAGES_SIDE;
430 1616155 : bits = ( nSubframes == 1 ) ? ivas_sns_cdbks_side_tcx20_bits : ivas_sns_cdbks_side_tcx10_bits;
431 : }
432 :
433 19293271 : for ( int16_t j = 0; j < nStages; ++j )
434 : {
435 : /* plus one in index for stereo mode storage! */
436 14754367 : param_lpc[ch][j + idxIndices + SNS_STEREO_MODE_OFFSET_INDICES] = get_next_indice( st0, bits[j] );
437 : }
438 4538904 : idxIndices += nStages;
439 : }
440 : }
441 : }
442 : else
443 : {
444 55261266 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
445 : {
446 36840844 : st = sts[ch];
447 :
448 36840844 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
449 : {
450 4164092 : skipped_first_channel = 1;
451 4164092 : continue;
452 : }
453 :
454 32676752 : start_bit_pos_sns = st0->next_bit_pos;
455 :
456 32676752 : if ( ch == 0 || skipped_first_channel )
457 : {
458 : /* read SNS stereo mode */
459 18071396 : param_lpc[0][0] = get_next_indice( st0, 1 ) << 1;
460 :
461 : /* read low br mode flag (if it is possible to be non-zero) */
462 18071396 : if ( sts[0]->element_brate == IVAS_48k && !( ( sts[0]->core == TCX_20 && sts[1]->core == TCX_20 ) ) )
463 : {
464 8920 : sns_low_br_mode = get_next_indice( st0, 1 );
465 : }
466 : }
467 :
468 32676752 : tmp = ch;
469 32676752 : if ( ch == 1 && param_lpc[0][0] == 2 )
470 : {
471 11457460 : tmp = 3;
472 : }
473 :
474 32676752 : getLPCparam( st, ¶m_lpc[ch][0], st0, tmp, sns_low_br_mode && !( sts[0]->core == TCX_20 && sts[1]->core == TCX_20 ) );
475 :
476 32676752 : st->side_bits_frame_channel += st0->next_bit_pos - start_bit_pos_sns;
477 : }
478 : }
479 : }
480 :
481 20900294 : return;
482 : }
483 :
484 :
485 : /*-----------------------------------------------------------------*
486 : * ivas_mdct_core_invQ()
487 : *
488 : * Inverse processing steps up to inverse quantization
489 : *-----------------------------------------------------------------*/
490 :
491 21948071 : void ivas_mdct_core_invQ(
492 : CPE_DEC_HANDLE hCPE, /* i/o: CPE handle */
493 : int16_t nTnsBitsTCX10[CPE_CHANNELS][NB_DIV], /* i : number of TNS bits */
494 : int16_t p_param[CPE_CHANNELS][NB_DIV], /* i : pointer to param buffer */
495 : int16_t param_lpc[CPE_CHANNELS][NPRM_LPC_NEW], /* i : lpc parameters */
496 : int16_t param[CPE_CHANNELS][DEC_NPRM_DIV * NB_DIV], /* i : param buffer */
497 : int16_t fUseTns[CPE_CHANNELS][NB_DIV], /* i : flag TNS enabled */
498 : STnsData tnsData[CPE_CHANNELS][NB_DIV], /* i : TNS parameter */
499 : float *x_0[CPE_CHANNELS][NB_DIV], /* i/o: signal buffer */
500 : float *x[CPE_CHANNELS][NB_DIV], /* i/o: signal buffer */
501 : float Aq[CPE_CHANNELS][( NB_SUBFR16k + 1 ) * ( M + 1 )], /* i : LP coefficients */
502 : int16_t ms_mask[NB_DIV][MAX_SFB], /* i : M/S mask */
503 : const int16_t MCT_flag /* i : hMCT handle allocated (1) or not (0) */
504 : )
505 : {
506 : int16_t ch, bfi, k;
507 : Decoder_State **sts, *st;
508 : /* bitstream */
509 : int16_t total_nbbits[CPE_CHANNELS];
510 : int16_t bitsRead[CPE_CHANNELS];
511 : int16_t *prm[CPE_CHANNELS];
512 : /* LPC */
513 : Word16 Aind[CPE_CHANNELS][M + 1];
514 : float sns[CPE_CHANNELS][NB_DIV][M];
515 : /* TCX */
516 : float xn_buf[L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX];
517 : int16_t tcx_offset[CPE_CHANNELS];
518 : int16_t tcx_offsetFB[CPE_CHANNELS];
519 : int16_t left_rect[CPE_CHANNELS];
520 : int16_t L_spec[CPE_CHANNELS];
521 : /* Framing */
522 : int16_t L_frame[CPE_CHANNELS], nSubframes[CPE_CHANNELS], L_frameTCX[CPE_CHANNELS];
523 : int16_t tmp_concealment_method;
524 : float gain_tcx;
525 : int16_t nf_seed;
526 : const int16_t *prm_sqQ;
527 : int16_t L_frameTCX_global[CPE_CHANNELS];
528 : float tmp_ms_sig[CPE_CHANNELS][N_MAX];
529 : float concealment_noise[CPE_CHANNELS][L_FRAME48k];
530 : TONALMDCTCONC_NOISE_GEN_MODE noise_gen_mode_bfi;
531 :
532 21948071 : push_wmops( "mdct_core_invQ" );
533 21948071 : sts = hCPE->hCoreCoder;
534 21948071 : bfi = sts[0]->bfi;
535 21948071 : noise_gen_mode_bfi = NOISE_GEN_MODE_UNDEF;
536 :
537 21948071 : set_f( xn_buf, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX );
538 21948071 : set_s( total_nbbits, 0, CPE_CHANNELS );
539 21948071 : set_s( bitsRead, 0, CPE_CHANNELS );
540 21948071 : tmp_concealment_method = 0;
541 21948071 : prm_sqQ = NULL; /* set prm_sqQ to NULL - in case of bfi == 1 it's not set or needed, but it triggers sanitizers */
542 :
543 65844213 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
544 : {
545 43896142 : sts[ch]->enablePlcWaveadjust = 0;
546 : }
547 :
548 : /* temporarily restore LR representation of previous frame for PLC mode decision (done on the individual channels) */
549 21948071 : if ( bfi && !MCT_flag && ( hCPE->hStereoMdct->mdct_stereo_mode[0] > SMDCT_DUAL_MONO || hCPE->hStereoMdct->mdct_stereo_mode[1] > SMDCT_DUAL_MONO ) )
550 : {
551 94855 : L_frameTCX[0] = sts[0]->L_frameTCX_past;
552 94855 : L_frameTCX[1] = sts[1]->L_frameTCX_past;
553 94855 : mvr2r( sts[0]->hTonalMDCTConc->lastBlockData.spectralData, tmp_ms_sig[0], L_frameTCX[0] );
554 94855 : mvr2r( sts[1]->hTonalMDCTConc->lastBlockData.spectralData, tmp_ms_sig[1], L_frameTCX[0] );
555 94855 : 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 );
556 : }
557 :
558 21948071 : if ( bfi )
559 : {
560 1047777 : if ( sts[0]->core == sts[1]->core )
561 : {
562 1037444 : noise_gen_mode_bfi = EQUAL_CORES;
563 : }
564 10333 : else if ( sts[0]->core == TCX_20 && sts[1]->core == TCX_10 )
565 : {
566 4294 : noise_gen_mode_bfi = TCX10_IN_0_TCX20_IN_1;
567 : }
568 6039 : else if ( sts[0]->core == TCX_10 && sts[1]->core == TCX_20 )
569 : {
570 6039 : noise_gen_mode_bfi = TCX20_IN_0_TCX10_IN_1;
571 : }
572 : }
573 :
574 : /* parameter decoding */
575 65844213 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
576 : {
577 43896142 : st = sts[ch];
578 :
579 43896142 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
580 : {
581 4371610 : st->total_brate = st->bits_frame_channel;
582 4371610 : continue;
583 : }
584 39524532 : prm[ch] = param[ch]; /* to avoid compilation warnings */
585 :
586 : /*Adjust bit per frame*/
587 39524532 : if ( !bfi )
588 : {
589 37636496 : st->bits_frame_core = st->bits_frame_channel;
590 :
591 37636496 : sts[ch]->total_brate = ( sts[ch]->bits_frame_channel + sts[ch]->side_bits_frame_channel + sts[ch]->core * NF_GAIN_BITS ) * FRAMES_PER_SEC;
592 : }
593 :
594 : /* Framing parameters */
595 39524532 : L_frame[ch] = st->L_frame;
596 :
597 : /*--------------------------------------------------------------------------------*
598 : * BITSTREAM DECODING
599 : *--------------------------------------------------------------------------------*/
600 :
601 39524532 : total_nbbits[ch] = st->bits_frame_channel;
602 :
603 39524532 : if ( !bfi )
604 : {
605 37636496 : st->second_last_core = st->last_core;
606 37636496 : if ( hCPE->cpe_id == 0 && ch == 0 )
607 : {
608 : /* add mct and side bits to first handle bitrate to avoid false BER detection */
609 10570459 : st->total_brate += ( st->next_bit_pos * FRAMES_PER_SEC );
610 :
611 10570459 : dec_prm_tcx_spec( st, param[ch], &total_nbbits[ch], &bitsRead[ch], p_param[ch], nTnsBitsTCX10[ch] );
612 :
613 : /*revert to actual total bitrate assigned to ch0 */
614 10570459 : sts[ch]->total_brate = ( sts[ch]->bits_frame_channel + sts[ch]->side_bits_frame_channel + sts[ch]->core * NF_GAIN_BITS ) * FRAMES_PER_SEC;
615 : }
616 : else
617 : {
618 27066037 : dec_prm_tcx_spec( st, param[ch], &total_nbbits[ch], &bitsRead[ch], p_param[ch], nTnsBitsTCX10[ch] );
619 : }
620 :
621 37636496 : assert( st->BER_detect != 1 );
622 : }
623 : else
624 : {
625 1888036 : if ( st->nbLostCmpt > 1 )
626 : {
627 1061445 : st->flagGuidedAcelp = 0;
628 : }
629 :
630 : /* PLC: [Common: mode decision]
631 : * PLC: Decide which Concealment to use. Update pitch lags if needed */
632 1888036 : st->core = GetPLCModeDecision( st );
633 : }
634 :
635 39524532 : if ( ( !st->bfi || st->hTcxCfg->psychParamsCurrent == NULL ) && st->core > ACELP_CORE )
636 : {
637 : int16_t last_frame_was_concealed_cng;
638 37636496 : last_frame_was_concealed_cng = ( st->last_core == ACELP_CORE ) && ( st->last_core != st->last_core_from_bs );
639 37636496 : SetCurrentPsychParams( st->core, last_frame_was_concealed_cng, st->hTcxCfg );
640 : }
641 :
642 : /* PLC: [Common: Memory update]
643 : * PLC: Update the number of lost frames */
644 39524532 : if ( !bfi )
645 : {
646 37636496 : if ( st->prev_bfi == 1 )
647 : {
648 812395 : st->prev_nbLostCmpt = st->nbLostCmpt;
649 : }
650 : else
651 : {
652 36824101 : st->prev_nbLostCmpt = 0;
653 : }
654 :
655 37636496 : st->nbLostCmpt = 0;
656 : }
657 : }
658 :
659 21948071 : if ( bfi && !MCT_flag && ( hCPE->hStereoMdct->mdct_stereo_mode[0] > SMDCT_DUAL_MONO || hCPE->hStereoMdct->mdct_stereo_mode[1] > SMDCT_DUAL_MONO ) )
660 : {
661 : /* avoid using TD-PLC in only one channel when stereo mode isn't dual mono */
662 94855 : if ( sts[0]->core != sts[1]->core && ( sts[0]->core == ACELP_CORE || sts[1]->core == ACELP_CORE ) )
663 : {
664 4762 : if ( sts[0]->core == ACELP_CORE )
665 : {
666 2673 : sts[0]->core = sts[0]->last_core;
667 : }
668 2089 : else if ( sts[1]->core == ACELP_CORE )
669 : {
670 2089 : sts[1]->core = sts[1]->last_core;
671 : }
672 : }
673 :
674 94855 : mvr2r( tmp_ms_sig[0], sts[0]->hTonalMDCTConc->lastBlockData.spectralData, L_frameTCX[0] );
675 94855 : mvr2r( tmp_ms_sig[1], sts[1]->hTonalMDCTConc->lastBlockData.spectralData, L_frameTCX[0] );
676 : }
677 :
678 : /*--------------------------------------------------------------------------------*
679 : * LPC PARAMETERS
680 : *--------------------------------------------------------------------------------*/
681 :
682 21948071 : if ( bfi == 0 )
683 : {
684 20900294 : if ( !MCT_flag && sts[0]->sr_core == 25600 && ( ( hCPE->element_brate == IVAS_48k || hCPE->element_brate == IVAS_64k ) ) )
685 : {
686 2479872 : dequantize_sns( param_lpc, sns, sts );
687 : }
688 : else
689 : {
690 18420422 : 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 )
691 : {
692 14135804 : sns_avq_dec_stereo( param_lpc[0], param_lpc[1], sts[0]->L_frame, &sns[0][0][0], &sns[1][0][0] );
693 : }
694 : else
695 : {
696 12853854 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
697 : {
698 8569236 : st = sts[ch];
699 8569236 : if ( st->mct_chan_mode != MCT_CHAN_MODE_IGNORE )
700 : {
701 4405144 : sns_avq_dec( param_lpc[ch], sns[ch], st->L_frame, st->numlpc );
702 : }
703 : }
704 : }
705 : }
706 : }
707 :
708 :
709 : /*--------------------------------------------------------------*
710 : * Rate switching
711 : *---------------------------------------------------------------*/
712 :
713 65844213 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
714 : {
715 43896142 : st = sts[ch];
716 :
717 43896142 : if ( st->rate_switching_reset )
718 : {
719 276878 : lsp2a_stab( st->lsp_old, st->old_Aq_12_8, M );
720 : }
721 : }
722 :
723 : /*--------------------------------------------------------------------------------*
724 : * TCX20/10/5
725 : *--------------------------------------------------------------------------------*/
726 :
727 65844213 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
728 : {
729 43896142 : st = sts[ch];
730 :
731 43896142 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
732 : {
733 4371610 : set_f( x[ch][0], 0.f, st->hTcxCfg->tcx_coded_lines );
734 : /* usually set in decoder_tcx_invQ(), needed for concealment */
735 4371610 : st->hTcxDec->damping = 0.f;
736 4371610 : st->hTcxDec->gainHelper = 1.f;
737 4371610 : st->hTcxDec->stepCompensate = 0.f;
738 :
739 4371610 : continue;
740 : }
741 :
742 39524532 : if ( !bfi || ( bfi && st->core != ACELP_CORE ) )
743 : {
744 39004081 : nSubframes[ch] = ( st->core == TCX_10_CORE ) ? NB_DIV : 1;
745 :
746 79018205 : for ( k = 0; k < nSubframes[ch]; k++ )
747 : {
748 : /* Stability Factor */
749 40014124 : if ( !bfi )
750 : {
751 38622173 : mvr2r( sns[ch][k], &Aq[ch][k * M], M );
752 : }
753 : else
754 : {
755 1391951 : st->stab_fac = 1.0f;
756 : }
757 :
758 : /* Set pointer to parameters */
759 40014124 : prm[ch] = param[ch] + ( k * DEC_NPRM_DIV );
760 40014124 : L_frameTCX_global[ch] = st->hTcxDec->L_frameTCX / nSubframes[ch];
761 40014124 : L_spec[ch] = st->hTcxCfg->tcx_coded_lines / nSubframes[ch];
762 :
763 40014124 : 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] );
764 :
765 40014124 : tmp_concealment_method = 0;
766 40014124 : nf_seed = prm[ch][1 + NOISE_FILL_RANGES + LTPSIZE];
767 40014124 : if ( !bfi && st->hTcxCfg->fIsTNSAllowed && ( ch > 0 ) && ( nf_seed < 0 ) )
768 : {
769 199094 : mvs2s( param[0] + k * DEC_NPRM_DIV + 1 + NOISE_FILL_RANGES + LTPSIZE, prm[ch] + 1 + NOISE_FILL_RANGES + LTPSIZE, nf_seed * -1 );
770 : }
771 40014124 : nf_seed = 0;
772 :
773 40014124 : 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 );
774 :
775 40014124 : mvr2r( x[ch][k], x_0[ch][k], L_frameTCX[ch] );
776 :
777 40014124 : if ( bfi && !MCT_flag )
778 : {
779 488062 : 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 );
780 : }
781 :
782 40014124 : 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 );
783 :
784 40014124 : 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 );
785 : }
786 : }
787 : }
788 :
789 21948071 : pop_wmops();
790 21948071 : return;
791 : }
792 :
793 :
794 : /*-----------------------------------------------------------------*
795 : * ivas_mdct_core_reconstruct()
796 : *
797 : * reconstruct time signal
798 : *-----------------------------------------------------------------*/
799 :
800 21948071 : void ivas_mdct_core_reconstruct(
801 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
802 : float *x[][NB_DIV], /* i/o: synthesis @internal_FS */
803 : float signal_outFB[CPE_CHANNELS][L_FRAME_PLUS], /* o : synthesis @output_FS */
804 : int16_t fUseTns[CPE_CHANNELS][NB_DIV], /* i : flage TNS enabled */
805 : const int16_t MCT_flag /* i : hMCT handle allocated (1) or not (0)*/
806 : )
807 : {
808 : int16_t ch, k, bfi;
809 : Decoder_State **sts, *st;
810 : /* Framing */
811 : int16_t L_frame[CPE_CHANNELS], L_frameTCX[CPE_CHANNELS], nSubframes[CPE_CHANNELS];
812 : int16_t L_frame_global[CPE_CHANNELS], L_frame_globalTCX[CPE_CHANNELS];
813 : /* Synth */
814 : float synth_buf[OLD_SYNTH_INTERNAL_DEC + L_FRAME_PLUS_INTERNAL + M];
815 : float *synth;
816 : float synth_bufFB[OLD_SYNTH_SIZE_DEC + L_FRAME_PLUS + M];
817 : float *synthFB;
818 : /* TCX */
819 : float xn_buf[L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX];
820 : int16_t tcx_offset[CPE_CHANNELS];
821 : int16_t tcx_offsetFB[CPE_CHANNELS];
822 : int16_t left_rect[CPE_CHANNELS];
823 : int16_t L_spec[CPE_CHANNELS];
824 :
825 : int16_t pitch[CPE_CHANNELS][NB_SUBFR16k];
826 : float pit_gain[CPE_CHANNELS][NB_SUBFR16k];
827 : int16_t skip_decoding;
828 :
829 21948071 : set_f( xn_buf, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX );
830 :
831 : /* Initializations */
832 21948071 : sts = hCPE->hCoreCoder;
833 21948071 : bfi = sts[0]->bfi;
834 :
835 : /* TNS, ITF, IMDCT and updates */
836 65844213 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
837 : {
838 43896142 : st = sts[ch];
839 :
840 43896142 : skip_decoding = 0;
841 43896142 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
842 : {
843 4491293 : skip_decoding = 1;
844 : }
845 :
846 43896142 : nSubframes[ch] = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
847 43896142 : synth = synth_buf + st->hTcxDec->old_synth_len;
848 43896142 : synthFB = synth_bufFB + st->hTcxDec->old_synth_lenFB;
849 43896142 : mvr2r( st->hTcxDec->old_synth, synth_buf, st->hTcxDec->old_synth_len );
850 43896142 : mvr2r( st->hTcxDec->old_synthFB, synth_bufFB, st->hTcxDec->old_synth_lenFB );
851 43896142 : set_zero( synth, L_FRAME_PLUS_INTERNAL + M );
852 43896142 : set_zero( synthFB, L_FRAME_PLUS + M );
853 :
854 43896142 : if ( st->core != ACELP_CORE )
855 : {
856 87761425 : for ( k = 0; k < nSubframes[ch]; k++ )
857 : {
858 44385734 : L_spec[ch] = st->hTcxCfg->tcx_coded_lines / nSubframes[ch];
859 44385734 : L_frame_global[ch] = st->L_frame / nSubframes[ch];
860 44385734 : L_frame_globalTCX[ch] = st->hTcxDec->L_frameTCX / nSubframes[ch];
861 :
862 44385734 : init_tcx_info( st, L_frame_global[ch], L_frame_globalTCX[ch], k, bfi, &tcx_offset[ch],
863 44385734 : &tcx_offsetFB[ch], &L_frame[ch], &L_frameTCX[ch], &left_rect[ch], &L_spec[ch] );
864 :
865 44385734 : if ( !skip_decoding )
866 : {
867 39891327 : 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,
868 39891327 : ( ( 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],
869 39891327 : fUseTns[ch][k], &synth[k * L_frame[ch]], &synthFB[k * L_frameTCX[ch]], bfi, k, 0 );
870 : }
871 : else
872 : {
873 4494407 : set_f( &synth[k * L_frame[ch]], 0.f, L_frame[ch] );
874 4494407 : set_f( &synthFB[k * L_frame[ch]], 0.f, L_frameTCX[ch] );
875 : /* Note: these buffers are not subframe-based, hence no indexing with k */
876 4494407 : set_f( &st->hHQ_core->old_outLB[0], 0.f, L_frame[ch] );
877 4494407 : set_f( &st->hHQ_core->old_out[0], 0.f, L_frameTCX[ch] );
878 4494407 : set_f( &st->hTcxDec->syn_Overl[0], 0.f, L_frame[ch] / 2 );
879 4494407 : set_f( &st->hTcxDec->syn_OverlFB[0], 0.f, L_frameTCX[ch] / 2 );
880 4494407 : set_f( &st->hTcxDec->syn_Overl_TDAC[0], 0.f, L_frame[ch] / 2 );
881 4494407 : set_f( &st->hTcxDec->syn_Overl_TDACFB[0], 0.f, L_frameTCX[ch] / 2 );
882 : }
883 : }
884 :
885 43375691 : if ( !bfi && st->hTonalMDCTConc != NULL )
886 : {
887 41800588 : TonalMDCTConceal_SaveTimeSignal( st->hTonalMDCTConc, synthFB, L_frameTCX[ch] );
888 : }
889 :
890 43375691 : decoder_tcx_post( st, synth, synthFB, NULL, bfi, MCT_flag );
891 : }
892 : else /*ACELP core for ACELP-PLC */
893 : {
894 520451 : assert( st->bfi == 1 );
895 : /* PLC: [TCX: TD PLC] */
896 520451 : if ( MCT_flag )
897 : {
898 387003 : con_tcx( st, &synthFB[0], -1.f, NULL, 0, NULL );
899 : }
900 : else
901 : {
902 133448 : 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] );
903 : }
904 :
905 520451 : lerp( synthFB, synth, st->L_frame, st->hTcxDec->L_frameTCX );
906 520451 : st->con_tcx = 1;
907 520451 : set_f( &st->mem_pitch_gain[2], st->lp_gainp, st->nb_subfr );
908 :
909 : /* PLC: [TCX: Tonal Concealment] */
910 : /* Signal that this frame is not TCX */
911 520451 : if ( st->hTonalMDCTConc != NULL )
912 : {
913 520451 : TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, 0, 0, 0, 0 );
914 : }
915 : }
916 :
917 : /*--------------------------------------------------------------------------------*
918 : * Updates
919 : *--------------------------------------------------------------------------------*/
920 :
921 43896142 : st = sts[ch];
922 :
923 43896142 : if ( bfi && st->last_core != ACELP_CORE && st->core == ACELP_CORE )
924 : {
925 : /* Update FEC_scale_syn parameters */
926 520451 : if ( st->hTcxLtpDec->tcxltp_gain == 0 )
927 : {
928 0 : fer_energy( st->L_frame, UNVOICED, synth, (float) ( st->L_frame / 2 ), &st->enr_old, st->L_frame );
929 : }
930 : else
931 : {
932 520451 : fer_energy( st->L_frame, st->clas_dec, synth, st->old_fpitch, &st->enr_old, st->L_frame );
933 : }
934 : }
935 :
936 : /* Update */
937 43896142 : mvr2r( synth_buf + st->L_frame, st->hTcxDec->old_synth, st->hTcxDec->old_synth_len );
938 43896142 : 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 ) );
939 43896142 : mvr2r( synth_bufFB + st->hTcxDec->L_frameTCX, st->hTcxDec->old_synthFB, st->hTcxDec->old_synth_lenFB );
940 43896142 : if ( st->hHQ_core != NULL )
941 : {
942 43896142 : 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 ) );
943 : }
944 :
945 43896142 : mvr2r( st->lsp_q_cng, st->old_lsp_q_cng, M );
946 43896142 : mvr2r( st->lsf_q_cng, st->old_lsf_q_cng, M );
947 :
948 43896142 : st->last_is_cng = 0;
949 :
950 : /* Postfiltering */
951 43896142 : post_decoder( st, synth_buf, pit_gain[ch], pitch[ch], x[ch][0], st->p_bpf_noise_buf );
952 :
953 43896142 : mvr2r( synthFB, signal_outFB[ch], st->hTcxDec->L_frameTCX );
954 : #ifdef DEBUG_PLC_INFO
955 : {
956 : int16_t i;
957 : int16_t tmp[L_FRAME48k];
958 : static FILE *sP = NULL;
959 :
960 : for ( i = 0; i < st->hTcxDec->L_frameTCX; i++ )
961 : {
962 : tmp[i] = (int16_t) ( signal_outFB[ch][i] + 0.5f );
963 : }
964 : if ( ch == 0 )
965 : {
966 : dbgwrite( tmp, sizeof( int16_t ), st->hTcxDec->L_frameTCX, 1, "./res/mdct_stereo_dec_synthFB_ch0.pcm" );
967 : #ifndef DEBUG_MODE_INFO
968 : dbgwrite( &st->bfi, sizeof( int16_t ), 1, st->hTcxDec->L_frameTCX, "./res/bfi.pcm" );
969 : #endif
970 : dbgwrite( &st->tonal_mdct_plc_active, sizeof( int16_t ), 1, L_frameTCX[ch], "./res/tonal_mdct_active_ch0.pcm" );
971 : dbgwrite( &st->tonal_mdct_plc_active, sizeof( int16_t ), 1, L_frameTCX[ch], "./res/tonal_mdct_active_ch0.pcm" );
972 : dbgwrite( &st->con_tcx, sizeof( int16_t ), 1, 640, "./res/con_tcx_ch0.pcm" );
973 : }
974 : else
975 : {
976 : dbgwrite( tmp, sizeof( int16_t ), st->hTcxDec->L_frameTCX, 1, "./res/mdct_stereo_dec_synthFB_ch1.pcm" );
977 : dbgwrite( &st->tonal_mdct_plc_active, sizeof( int16_t ), 1, L_frameTCX[ch], "./res/tonal_mdct_active_ch1.pcm" );
978 : dbgwrite( &st->con_tcx, sizeof( int16_t ), 1, 640, "./res/con_tcx_ch1.pcm" );
979 : }
980 : }
981 : #endif
982 :
983 : /* updates */
984 43896142 : st->last_voice_factor = 0.0f;
985 43896142 : st->last_coder_type = st->coder_type;
986 : }
987 :
988 : /* calculate coherence of signal needed when next frame is lost */
989 21948071 : if ( !bfi && !MCT_flag && hCPE->element_mode == IVAS_CPE_MDCT )
990 : {
991 : int16_t i;
992 : float nrgL, nrgR, xcorr;
993 :
994 5163210 : nrgL = nrgR = xcorr = EPSILON;
995 :
996 3944400010 : for ( i = 0; i < sts[0]->hTcxDec->L_frameTCX; i++ )
997 : {
998 3939236800 : nrgL += signal_outFB[0][i] * signal_outFB[0][i];
999 3939236800 : nrgR += signal_outFB[1][i] * signal_outFB[1][i];
1000 3939236800 : xcorr += signal_outFB[0][i] * signal_outFB[1][i];
1001 : }
1002 :
1003 5163210 : hCPE->hStereoMdct->lastCoh = fabsf( xcorr ) * inv_sqrt( nrgL * nrgR );
1004 5163210 : hCPE->hStereoMdct->lastCoh = min( hCPE->hStereoMdct->lastCoh, 1.f );
1005 : }
1006 :
1007 21948071 : return;
1008 : }
1009 :
1010 :
1011 : /*-----------------------------------------------------------------*
1012 : * ivas_mdct_core_tns_ns()
1013 : *
1014 : * reconstruct time signal
1015 : *-----------------------------------------------------------------*/
1016 :
1017 21948071 : void ivas_mdct_core_tns_ns(
1018 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
1019 : int16_t fUseTns[CPE_CHANNELS][NB_DIV], /* i : two entries for each channel in TCX10 */
1020 : STnsData tnsData[CPE_CHANNELS][NB_DIV], /* o : TNS parameter */
1021 : float *x[CPE_CHANNELS][NB_DIV], /* o : synthesis @internal_FS */
1022 : float Aq[CPE_CHANNELS][( NB_SUBFR16k + 1 ) * ( M + 1 )], /* o : LP coefficients */
1023 : const int16_t MCT_flag /* i : hMCT handle allocated (1) or not (0) */
1024 : )
1025 : {
1026 : int16_t ch, k, bfi;
1027 : Decoder_State **sts, *st;
1028 : /* Framing */
1029 : int16_t L_frame[CPE_CHANNELS], L_frameTCX[CPE_CHANNELS], nSubframes[CPE_CHANNELS];
1030 : int16_t L_frame_global[CPE_CHANNELS], L_frameTCX_glob[CPE_CHANNELS];
1031 :
1032 : /* TCX */
1033 : float xn_buf[L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX];
1034 : int16_t tcx_offset[CPE_CHANNELS];
1035 : int16_t tcx_offsetFB[CPE_CHANNELS];
1036 : int16_t left_rect[CPE_CHANNELS];
1037 : int16_t L_spec[CPE_CHANNELS];
1038 : float sns_int_scf[FDNS_NPTS];
1039 :
1040 : /* Initializations */
1041 21948071 : sts = hCPE->hCoreCoder;
1042 21948071 : bfi = sts[0]->bfi;
1043 :
1044 21948071 : set_f( xn_buf, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX );
1045 :
1046 :
1047 : /* TNS, ITF, IMDCT and updates */
1048 65844213 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
1049 : {
1050 43896142 : st = sts[ch];
1051 43896142 : nSubframes[ch] = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
1052 43896142 : L_frame_global[ch] = st->L_frame / nSubframes[ch];
1053 43896142 : L_frameTCX_glob[ch] = st->hTcxDec->L_frameTCX / nSubframes[ch];
1054 43896142 : L_spec[ch] = st->hTcxCfg->tcx_coded_lines / nSubframes[ch];
1055 :
1056 43896142 : 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 */
1057 : {
1058 5011744 : if ( st->hTonalMDCTConc != NULL )
1059 : {
1060 5011744 : TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, st->hTcxDec->L_frameTCX, 0, bfi, 0 );
1061 : }
1062 :
1063 : /* nothing to do for missing LFE */
1064 5011744 : continue;
1065 : }
1066 :
1067 78775725 : for ( k = 0; k < nSubframes[ch]; k++ )
1068 : {
1069 39891327 : init_tcx_info( st, L_frame_global[ch], L_frameTCX_glob[ch], k, bfi, &tcx_offset[ch],
1070 39891327 : &tcx_offsetFB[ch], &L_frame[ch], &L_frameTCX[ch], &left_rect[ch], &L_spec[ch] );
1071 :
1072 39891327 : if ( !bfi )
1073 : {
1074 38499376 : sns_interpolate_scalefactors( &sns_int_scf[0], &Aq[ch][k * M], DEC );
1075 :
1076 38499376 : if ( MCT_flag && st->hTonalMDCTConc != NULL && ( ( k + 1 ) == nSubframes[ch] ) )
1077 : {
1078 27190393 : 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] ) );
1079 : }
1080 : }
1081 : else
1082 : {
1083 1391951 : if ( st->hTonalMDCTConc != NULL )
1084 : {
1085 1391951 : if ( !MCT_flag && st->hTcxDec->cummulative_damping_tcx != 1.f )
1086 271332 : {
1087 : float *scf_last, *scf_bg;
1088 : float fade_in, fade_out;
1089 :
1090 271332 : scf_last = &st->hTonalMDCTConc->lastBlockData.scaleFactors[0];
1091 271332 : scf_bg = &st->hTonalMDCTConc->scaleFactorsBackground[0];
1092 :
1093 271332 : st->hTonalMDCTConc->scf_fadeout *= 0.95f;
1094 :
1095 271332 : fade_out = st->hTonalMDCTConc->scf_fadeout;
1096 271332 : fade_in = 1 - fade_out;
1097 :
1098 17636580 : for ( int16_t i = 0; i < st->hTonalMDCTConc->nScaleFactors; i++ )
1099 : {
1100 17365248 : sns_int_scf[i] = fade_out * scf_last[i] + fade_in * scf_bg[i];
1101 : }
1102 : }
1103 : else
1104 : {
1105 1120619 : st->hTonalMDCTConc->scf_fadeout = 1.0f;
1106 1120619 : mvr2r( st->hTonalMDCTConc->lastBlockData.scaleFactors, &sns_int_scf[0], st->hTonalMDCTConc->nScaleFactors );
1107 : }
1108 : }
1109 : }
1110 :
1111 39891327 : 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 );
1112 :
1113 39891327 : sns_shape_spectrum( x[ch][k], st->hTcxCfg->psychParamsCurrent, &sns_int_scf[0], st->hTcxCfg->psychParamsCurrent->nBins );
1114 : /*
1115 : 2025-09-07, mul:
1116 : 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;
1117 : thus, the safer approach to prevent high frequency artifacts is to apply the SNS up to L_frameTCX;
1118 : in case this is not necessary, x[] is filled with zeros, and the multiplication is not causing any additional harm * /
1119 : */
1120 39891327 : 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 );
1121 :
1122 39891327 : 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 );
1123 : }
1124 :
1125 38884398 : if ( bfi && st->tonal_mdct_plc_active )
1126 : {
1127 100604 : TonalMDCTConceal_Apply( st->hTonalMDCTConc, x[ch][0], st->hTcxCfg->psychParamsCurrent );
1128 : }
1129 :
1130 38884398 : if ( ( bfi || MCT_flag ) && st->hTonalMDCTConc != NULL )
1131 : {
1132 28557978 : 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 );
1133 : }
1134 : }
1135 :
1136 21948071 : return;
1137 : }
|