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 4588213 : 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 4588213 : start = st0->next_bit_pos;
66 :
67 4588213 : if ( st->core == TCX_20_CORE )
68 : {
69 : /* read IGF payload */
70 4416471 : IGFDecReadLevel( st->hIGFDec, st0, ( st->last_core == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, 1 );
71 :
72 4416471 : IGFDecReadData( st->hIGFDec, st0, ( st->last_core == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, 1 );
73 : }
74 :
75 4588213 : if ( st->core == TCX_10_CORE )
76 : {
77 515226 : for ( k = 0; k < 2; k++ )
78 : {
79 343484 : IGFDecReadLevel( st->hIGFDec, st0, IGF_GRID_LB_SHORT, k == 0 ? 1 : 0 );
80 :
81 343484 : IGFDecReadData( st->hIGFDec, st0, IGF_GRID_LB_SHORT, k == 0 ? 1 : 0 );
82 :
83 343484 : IGFDecStoreTCX10SubFrameData( st->hIGFDec, k );
84 : }
85 : }
86 :
87 4588213 : st->side_bits_frame_channel += ( st0->next_bit_pos - start );
88 :
89 4588213 : return;
90 : }
91 :
92 :
93 : /*-----------------------------------------------------------------*
94 : * Function dec_prm_tcx_sidebits()
95 : *
96 : * decode TCX side parameters
97 : *-----------------------------------------------------------------*/
98 :
99 10035669 : 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 10035669 : hm_cfg.indexBuffer = NULL;
124 :
125 10035669 : start_bit_pos = st0->next_bit_pos;
126 :
127 : /* Init LTP data */
128 10035669 : st->hTcxDec->tcx_hm_LtpPitchLag = -1;
129 10035669 : st->hTcxLtpDec->tcxltp_gain = 0.0f;
130 :
131 : /*--------------------------------------------------------------------------------*
132 : * Header
133 : *--------------------------------------------------------------------------------*/
134 :
135 : /* Modes (ACE_GC, ACE_UC, TCX20, TCX10...) */
136 10035669 : getTCXMode( st, st0, MCT_flag );
137 :
138 10035669 : st->flagGuidedAcelp = 0;
139 :
140 10035669 : if ( st->dec_glr )
141 : {
142 0 : st->dec_glr_idx = -1;
143 : }
144 :
145 : /* last_core for core switching and error concealment */
146 10035669 : 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 338081 : st->last_core_from_bs = get_next_indice( st0, 1 );
150 :
151 : /* ACELP -> TCX_10 transitions are forbidden */
152 338081 : if ( st->core == TCX_10_CORE && st->last_core == ACELP_CORE )
153 : {
154 15 : st->last_core = TCX_20_CORE;
155 : }
156 : }
157 : else
158 : {
159 9697588 : st->last_core = get_next_indice( st0, 1 );
160 9697588 : 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 10035669 : if ( st->ini_frame == 0 )
165 : {
166 185181 : st->last_core = TCX_20_CORE;
167 : }
168 :
169 10035669 : getTCXWindowing( st->core, st->last_core, st->element_mode, st->hTcxCfg, st0 );
170 :
171 10035669 : st->hTcxDec->kernel_type[0] = st->hTcxDec->kernel_type[1] = MDCT_IV;
172 10035669 : st->hTcxDec->kernel_type[0] = get_next_indice( st0, st->last_core_from_bs != ACELP_CORE ? 2 : 1 );
173 10035669 : if ( st->core == TCX_10_CORE )
174 : {
175 357209 : st->hTcxDec->kernel_type[1] = 2 * ( st->hTcxDec->kernel_type[0] & 1 ) + get_next_indice( st0, 1 );
176 : }
177 10035669 : if ( st->core == TCX_20_CORE )
178 : {
179 9678460 : st->transform_type[0] = st->transform_type[1] = TCX_20;
180 : }
181 : else
182 : {
183 357209 : if ( st->hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP )
184 : {
185 238560 : st->transform_type[0] = TCX_5;
186 238560 : st->transform_type[1] = TCX_10;
187 : }
188 118649 : else if ( st->hTcxCfg->tcx_last_overlap_mode == FULL_OVERLAP )
189 : {
190 116474 : st->transform_type[0] = TCX_10;
191 116474 : st->transform_type[1] = TCX_5;
192 : }
193 : else
194 : {
195 2175 : st->transform_type[0] = st->transform_type[1] = TCX_5;
196 : }
197 : }
198 :
199 : /*--------------------------------------------------------------------------------*
200 : * TCX20/TCX10 parameters
201 : *--------------------------------------------------------------------------------*/
202 :
203 10035669 : getTCXparam( st, st0, hm_cfg, param, 0, 0, ( ( ch > 0 ) && ( tnsSize ) && ( tnsSize[0] + tnsSize[1] > 0 ) ? tnsSize : NULL ), p_param, nTnsBitsTCX10, 0 );
204 :
205 10035669 : st->side_bits_frame_channel = st0->next_bit_pos - start_bit_pos;
206 :
207 10035669 : return;
208 : }
209 :
210 :
211 : /*-----------------------------------------------------------------*
212 : * Function dec_prm_tcx_spec()
213 : *
214 : * decode TCX core parameters
215 : *-----------------------------------------------------------------*/
216 :
217 10035669 : 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 10035669 : 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 10035669 : start_bit_pos = st->next_bit_pos;
249 :
250 10035669 : nf_bits = 0;
251 10035669 : nbits_tcx = 0;
252 :
253 10035669 : 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 10035669 : if ( st->core == TCX_10_CORE && st->element_mode == IVAS_CPE_MDCT )
257 : {
258 : int16_t nTnsBitsTCX10Tmp[NB_DIV];
259 357209 : nTnsBitsTCX10Tmp[0] = nTnsBitsTCX10[0];
260 357209 : nTnsBitsTCX10Tmp[1] = nTnsBitsTCX10[1];
261 :
262 : /*compute target bits */
263 357209 : nbits_tcx = st->bits_frame_channel + nTnsBitsTCX10[0] + nTnsBitsTCX10[1] + nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL );
264 357209 : ivas_mdct_tcx10_bit_distribution( target_bitsTCX10, nbits_tcx, nTnsBitsTCX10Tmp );
265 : }
266 :
267 : /*--------------------------------------------------------------------------------*
268 : * TCX20/TCX10 parameters
269 : *--------------------------------------------------------------------------------*/
270 :
271 10035669 : getTCXparam( st, st, hm_cfg, param, 0, 0, NULL, p_param, target_bitsTCX10, 1 );
272 :
273 10035669 : nf_bits = nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL );
274 :
275 10035669 : 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 10035669 : bitsRead[0] = st->next_bit_pos - start_bit_pos;
282 :
283 10035669 : 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 5682616 : 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 5682616 : sts = hCPE->hCoreCoder;
314 5682616 : bfi = sts[0]->bfi;
315 :
316 5682616 : set_s( total_nbbits, 0, MCT_MAX_CHANNELS );
317 5682616 : set_s( bitsRead, 0, MCT_MAX_CHANNELS );
318 :
319 17047848 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
320 : {
321 11365232 : if ( MCT_flag )
322 : {
323 : /* Initialization or re-configuration of Stereo TCX */
324 8738596 : stereo_tcx_init_dec( sts[ch], 1, hCPE->last_element_mode );
325 : }
326 11365232 : sts[ch]->enablePlcWaveadjust = 0;
327 : }
328 :
329 5682616 : if ( !bfi )
330 : {
331 17047848 : 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 11365232 : if ( odd_channel_cpe && ch == 1 )
335 : {
336 896910 : continue;
337 : }
338 :
339 10468322 : st = sts[ch];
340 10468322 : if ( MCT_flag )
341 : {
342 7841686 : tmp = get_next_indice( st0, 1 );
343 7841686 : if ( tmp )
344 : {
345 432653 : st->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
346 : }
347 : else
348 : {
349 7409033 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
350 : }
351 : }
352 : }
353 :
354 : /*read channel bitrate ratios from bitstream*/
355 17047848 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
356 : {
357 11365232 : st = sts[ch];
358 11365232 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
359 : {
360 1329563 : sts[ch]->coder_type = INACTIVE;
361 1329563 : sts[ch]->side_bits_frame_channel = 0;
362 1329563 : continue;
363 : }
364 :
365 10035669 : tmp = ch;
366 10035669 : if ( ch == 1 && param_lpc[0][0] == 2 )
367 : {
368 0 : tmp = 3;
369 : }
370 :
371 10035669 : 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 10035669 : assert( st->BER_detect != 1 );
374 : }
375 :
376 : /*--------------------------------------------------------------------------------*
377 : * SNS parameters
378 : *--------------------------------------------------------------------------------*/
379 :
380 5682616 : sns_low_br_mode = 0;
381 5682616 : skipped_first_channel = 0;
382 5682616 : if ( !MCT_flag && sts[0]->sr_core == 25600 && ( ( hCPE->element_brate == IVAS_48k || hCPE->element_brate == IVAS_64k ) ) )
383 : {
384 474782 : param_lpc[0][0] = SNS_STEREO_MODE_LR;
385 474782 : param_lpc[0][1] = SNS_STEREO_MODE_LR;
386 474782 : param_lpc[0][2] = 0;
387 474782 : param_lpc[0][3] = 0;
388 474782 : if ( sts[0]->core == sts[1]->core )
389 : {
390 : int16_t nSubframes;
391 473087 : nSubframes = ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV;
392 :
393 962382 : for ( int16_t n = 0; n < nSubframes; ++n )
394 : {
395 489295 : param_lpc[0][n] = get_next_indice( st0, 1 );
396 : }
397 :
398 : /* zero side flags only get transmitted if needed */
399 962382 : for ( int16_t n = 0; n < nSubframes; ++n )
400 : {
401 489295 : if ( param_lpc[0][n] == SNS_STEREO_MODE_MS )
402 : {
403 459125 : param_lpc[0][n + SNS_STEREO_MODE_OFFSET_INDICES / 2] = get_next_indice( st0, 1 );
404 : }
405 : }
406 : }
407 1424346 : for ( ch = 0; ch < CPE_CHANNELS; ++ch )
408 : {
409 : int16_t nSubframes;
410 : int16_t idxIndices;
411 :
412 949564 : st = sts[ch];
413 949564 : nSubframes = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
414 949564 : idxIndices = 0;
415 :
416 1933239 : for ( int16_t n = 0; n < nSubframes; ++n )
417 : {
418 983675 : const int16_t is_side = ch == 1 && param_lpc[0][n] == SNS_STEREO_MODE_MS;
419 983675 : const int16_t *bits = ( nSubframes == 1 ) ? ivas_sns_cdbks_tcx20_bits : ivas_sns_cdbks_tcx10_bits;
420 983675 : int16_t nStages = ( ( nSubframes == 1 ) ? SNS_MSVQ_NSTAGES_TCX20 : SNS_MSVQ_NSTAGES_TCX10 );
421 :
422 983675 : if ( is_side )
423 : {
424 : /* check for zero-side flag */
425 459125 : if ( param_lpc[0][n + SNS_STEREO_MODE_OFFSET_INDICES / 2] )
426 : {
427 102561 : continue;
428 : }
429 356564 : nStages = SNS_MSVQ_NSTAGES_SIDE;
430 356564 : bits = ( nSubframes == 1 ) ? ivas_sns_cdbks_side_tcx20_bits : ivas_sns_cdbks_side_tcx10_bits;
431 : }
432 :
433 3654801 : for ( int16_t j = 0; j < nStages; ++j )
434 : {
435 : /* plus one in index for stereo mode storage! */
436 2773687 : param_lpc[ch][j + idxIndices + SNS_STEREO_MODE_OFFSET_INDICES] = get_next_indice( st0, bits[j] );
437 : }
438 881114 : idxIndices += nStages;
439 : }
440 : }
441 : }
442 : else
443 : {
444 15623502 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
445 : {
446 10415668 : st = sts[ch];
447 :
448 10415668 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
449 : {
450 1329563 : skipped_first_channel = 1;
451 1329563 : continue;
452 : }
453 :
454 9086105 : start_bit_pos_sns = st0->next_bit_pos;
455 :
456 9086105 : if ( ch == 0 || skipped_first_channel )
457 : {
458 : /* read SNS stereo mode */
459 4984922 : 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 4984922 : if ( sts[0]->element_brate == IVAS_48k && !( ( sts[0]->core == TCX_20 && sts[1]->core == TCX_20 ) ) )
463 : {
464 4944 : sns_low_br_mode = get_next_indice( st0, 1 );
465 : }
466 : }
467 :
468 9086105 : tmp = ch;
469 9086105 : if ( ch == 1 && param_lpc[0][0] == 2 )
470 : {
471 2779639 : tmp = 3;
472 : }
473 :
474 9086105 : getLPCparam( st, ¶m_lpc[ch][0], st0, tmp, sns_low_br_mode && !( sts[0]->core == TCX_20 && sts[1]->core == TCX_20 ) );
475 :
476 9086105 : st->side_bits_frame_channel += st0->next_bit_pos - start_bit_pos_sns;
477 : }
478 : }
479 : }
480 :
481 5682616 : return;
482 : }
483 :
484 :
485 : /*-----------------------------------------------------------------*
486 : * ivas_mdct_core_invQ()
487 : *
488 : * Inverse processing steps up to inverse quantization
489 : *-----------------------------------------------------------------*/
490 :
491 6182951 : 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 6182951 : push_wmops( "mdct_core_invQ" );
533 6182951 : sts = hCPE->hCoreCoder;
534 6182951 : bfi = sts[0]->bfi;
535 6182951 : noise_gen_mode_bfi = NOISE_GEN_MODE_UNDEF;
536 :
537 6182951 : set_f( xn_buf, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX );
538 6182951 : set_s( total_nbbits, 0, CPE_CHANNELS );
539 6182951 : set_s( bitsRead, 0, CPE_CHANNELS );
540 6182951 : tmp_concealment_method = 0;
541 6182951 : 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 18548853 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
544 : {
545 12365902 : sts[ch]->enablePlcWaveadjust = 0;
546 : }
547 :
548 : /* temporarily restore LR representation of previous frame for PLC mode decision (done on the individual channels) */
549 6182951 : if ( bfi && !MCT_flag && ( hCPE->hStereoMdct->mdct_stereo_mode[0] > SMDCT_DUAL_MONO || hCPE->hStereoMdct->mdct_stereo_mode[1] > SMDCT_DUAL_MONO ) )
550 : {
551 56818 : L_frameTCX[0] = sts[0]->L_frameTCX_past;
552 56818 : L_frameTCX[1] = sts[1]->L_frameTCX_past;
553 56818 : mvr2r( sts[0]->hTonalMDCTConc->lastBlockData.spectralData, tmp_ms_sig[0], L_frameTCX[0] );
554 56818 : mvr2r( sts[1]->hTonalMDCTConc->lastBlockData.spectralData, tmp_ms_sig[1], L_frameTCX[0] );
555 56818 : 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 6182951 : if ( bfi )
559 : {
560 500335 : if ( sts[0]->core == sts[1]->core )
561 : {
562 496592 : noise_gen_mode_bfi = EQUAL_CORES;
563 : }
564 3743 : else if ( sts[0]->core == TCX_20 && sts[1]->core == TCX_10 )
565 : {
566 2960 : noise_gen_mode_bfi = TCX10_IN_0_TCX20_IN_1;
567 : }
568 783 : else if ( sts[0]->core == TCX_10 && sts[1]->core == TCX_20 )
569 : {
570 783 : noise_gen_mode_bfi = TCX20_IN_0_TCX10_IN_1;
571 : }
572 : }
573 :
574 : /* parameter decoding */
575 18548853 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
576 : {
577 12365902 : st = sts[ch];
578 :
579 12365902 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
580 : {
581 1447223 : st->total_brate = st->bits_frame_channel;
582 1447223 : continue;
583 : }
584 10918679 : prm[ch] = param[ch]; /* to avoid compilation warnings */
585 :
586 : /*Adjust bit per frame*/
587 10918679 : if ( !bfi )
588 : {
589 10035669 : st->bits_frame_core = st->bits_frame_channel;
590 :
591 10035669 : 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 10918679 : L_frame[ch] = st->L_frame;
596 :
597 : /*--------------------------------------------------------------------------------*
598 : * BITSTREAM DECODING
599 : *--------------------------------------------------------------------------------*/
600 :
601 10918679 : total_nbbits[ch] = st->bits_frame_channel;
602 :
603 10918679 : if ( !bfi )
604 : {
605 10035669 : st->second_last_core = st->last_core;
606 10035669 : if ( hCPE->cpe_id == 0 && ch == 0 )
607 : {
608 : /* add mct and side bits to first handle bitrate to avoid false BER detection */
609 2769890 : st->total_brate += ( st->next_bit_pos * FRAMES_PER_SEC );
610 :
611 2769890 : 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 2769890 : 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 7265779 : dec_prm_tcx_spec( st, param[ch], &total_nbbits[ch], &bitsRead[ch], p_param[ch], nTnsBitsTCX10[ch] );
619 : }
620 :
621 10035669 : assert( st->BER_detect != 1 );
622 : }
623 : else
624 : {
625 883010 : if ( st->nbLostCmpt > 1 )
626 : {
627 536380 : st->flagGuidedAcelp = 0;
628 : }
629 :
630 : /* PLC: [Common: mode decision]
631 : * PLC: Decide which Concealment to use. Update pitch lags if needed */
632 883010 : st->core = GetPLCModeDecision( st );
633 : }
634 :
635 10918679 : if ( ( !st->bfi || st->hTcxCfg->psychParamsCurrent == NULL ) && st->core > ACELP_CORE )
636 : {
637 : int16_t last_frame_was_concealed_cng;
638 10035669 : last_frame_was_concealed_cng = ( st->last_core == ACELP_CORE ) && ( st->last_core != st->last_core_from_bs );
639 10035669 : 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 10918679 : if ( !bfi )
645 : {
646 10035669 : if ( st->prev_bfi == 1 )
647 : {
648 337445 : st->prev_nbLostCmpt = st->nbLostCmpt;
649 : }
650 : else
651 : {
652 9698224 : st->prev_nbLostCmpt = 0;
653 : }
654 :
655 10035669 : st->nbLostCmpt = 0;
656 : }
657 : }
658 :
659 6182951 : 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 56818 : if ( sts[0]->core != sts[1]->core && ( sts[0]->core == ACELP_CORE || sts[1]->core == ACELP_CORE ) )
663 : {
664 1534 : if ( sts[0]->core == ACELP_CORE )
665 : {
666 1208 : sts[0]->core = sts[0]->last_core;
667 : }
668 326 : else if ( sts[1]->core == ACELP_CORE )
669 : {
670 326 : sts[1]->core = sts[1]->last_core;
671 : }
672 : }
673 :
674 56818 : mvr2r( tmp_ms_sig[0], sts[0]->hTonalMDCTConc->lastBlockData.spectralData, L_frameTCX[0] );
675 56818 : mvr2r( tmp_ms_sig[1], sts[1]->hTonalMDCTConc->lastBlockData.spectralData, L_frameTCX[0] );
676 : }
677 :
678 : /*--------------------------------------------------------------------------------*
679 : * LPC PARAMETERS
680 : *--------------------------------------------------------------------------------*/
681 :
682 6182951 : if ( bfi == 0 )
683 : {
684 5682616 : if ( !MCT_flag && sts[0]->sr_core == 25600 && ( ( hCPE->element_brate == IVAS_48k || hCPE->element_brate == IVAS_64k ) ) )
685 : {
686 474782 : dequantize_sns( param_lpc, sns, sts );
687 : }
688 : else
689 : {
690 5207834 : 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 3916556 : 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 3873834 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
697 : {
698 2582556 : st = sts[ch];
699 2582556 : if ( st->mct_chan_mode != MCT_CHAN_MODE_IGNORE )
700 : {
701 1252993 : 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 18548853 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
714 : {
715 12365902 : st = sts[ch];
716 :
717 12365902 : if ( st->rate_switching_reset )
718 : {
719 31997 : lsp2a_stab( st->lsp_old, st->old_Aq_12_8, M );
720 : }
721 : }
722 :
723 : /*--------------------------------------------------------------------------------*
724 : * TCX20/10/5
725 : *--------------------------------------------------------------------------------*/
726 :
727 18548853 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
728 : {
729 12365902 : st = sts[ch];
730 :
731 12365902 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
732 : {
733 1447223 : set_f( x[ch][0], 0.f, st->hTcxCfg->tcx_coded_lines );
734 : /* usually set in decoder_tcx_invQ(), needed for concealment */
735 1447223 : st->hTcxDec->damping = 0.f;
736 1447223 : st->hTcxDec->gainHelper = 1.f;
737 1447223 : st->hTcxDec->stepCompensate = 0.f;
738 :
739 1447223 : continue;
740 : }
741 :
742 10918679 : if ( !bfi || ( bfi && st->core != ACELP_CORE ) )
743 : {
744 10666079 : nSubframes[ch] = ( st->core == TCX_10_CORE ) ? NB_DIV : 1;
745 :
746 21695085 : for ( k = 0; k < nSubframes[ch]; k++ )
747 : {
748 : /* Stability Factor */
749 11029006 : if ( !bfi )
750 : {
751 10392878 : mvr2r( sns[ch][k], &Aq[ch][k * M], M );
752 : }
753 : else
754 : {
755 636128 : st->stab_fac = 1.0f;
756 : }
757 :
758 : /* Set pointer to parameters */
759 11029006 : prm[ch] = param[ch] + ( k * DEC_NPRM_DIV );
760 11029006 : L_frameTCX_global[ch] = st->hTcxDec->L_frameTCX / nSubframes[ch];
761 11029006 : L_spec[ch] = st->hTcxCfg->tcx_coded_lines / nSubframes[ch];
762 :
763 11029006 : 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 11029006 : tmp_concealment_method = 0;
766 11029006 : nf_seed = prm[ch][1 + NOISE_FILL_RANGES + LTPSIZE];
767 11029006 : if ( !bfi && st->hTcxCfg->fIsTNSAllowed && ( ch > 0 ) && ( nf_seed < 0 ) )
768 : {
769 35217 : mvs2s( param[0] + k * DEC_NPRM_DIV + 1 + NOISE_FILL_RANGES + LTPSIZE, prm[ch] + 1 + NOISE_FILL_RANGES + LTPSIZE, nf_seed * -1 );
770 : }
771 11029006 : nf_seed = 0;
772 :
773 11029006 : 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 11029006 : mvr2r( x[ch][k], x_0[ch][k], L_frameTCX[ch] );
776 :
777 11029006 : if ( bfi && !MCT_flag )
778 : {
779 194520 : 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 11029006 : 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 11029006 : 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 6182951 : pop_wmops();
790 6182951 : return;
791 : }
792 :
793 :
794 : /*-----------------------------------------------------------------*
795 : * ivas_mdct_core_reconstruct()
796 : *
797 : * reconstruct time signal
798 : *-----------------------------------------------------------------*/
799 :
800 6182951 : 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 6182951 : set_f( xn_buf, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX );
830 :
831 : /* Initializations */
832 6182951 : sts = hCPE->hCoreCoder;
833 6182951 : bfi = sts[0]->bfi;
834 :
835 : /* TNS, ITF, IMDCT and updates */
836 18548853 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
837 : {
838 12365902 : st = sts[ch];
839 :
840 12365902 : skip_decoding = 0;
841 12365902 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
842 : {
843 1473011 : skip_decoding = 1;
844 : }
845 :
846 12365902 : nSubframes[ch] = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
847 12365902 : synth = synth_buf + st->hTcxDec->old_synth_len;
848 12365902 : synthFB = synth_bufFB + st->hTcxDec->old_synth_lenFB;
849 12365902 : mvr2r( st->hTcxDec->old_synth, synth_buf, st->hTcxDec->old_synth_len );
850 12365902 : mvr2r( st->hTcxDec->old_synthFB, synth_bufFB, st->hTcxDec->old_synth_lenFB );
851 12365902 : set_zero( synth, L_FRAME_PLUS_INTERNAL + M );
852 12365902 : set_zero( synthFB, L_FRAME_PLUS + M );
853 :
854 12365902 : if ( st->core != ACELP_CORE )
855 : {
856 24589531 : for ( k = 0; k < nSubframes[ch]; k++ )
857 : {
858 12476229 : L_spec[ch] = st->hTcxCfg->tcx_coded_lines / nSubframes[ch];
859 12476229 : L_frame_global[ch] = st->L_frame / nSubframes[ch];
860 12476229 : L_frame_globalTCX[ch] = st->hTcxDec->L_frameTCX / nSubframes[ch];
861 :
862 12476229 : init_tcx_info( st, L_frame_global[ch], L_frame_globalTCX[ch], k, bfi, &tcx_offset[ch],
863 12476229 : &tcx_offsetFB[ch], &L_frame[ch], &L_frameTCX[ch], &left_rect[ch], &L_spec[ch] );
864 :
865 12476229 : if ( !skip_decoding )
866 : {
867 11002540 : 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 11002540 : ( ( 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 11002540 : fUseTns[ch][k], &synth[k * L_frame[ch]], &synthFB[k * L_frameTCX[ch]], bfi, k, 0 );
870 : }
871 : else
872 : {
873 1473689 : set_f( &synth[k * L_frame[ch]], 0.f, L_frame[ch] );
874 1473689 : 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 1473689 : set_f( &st->hHQ_core->old_outLB[0], 0.f, L_frame[ch] );
877 1473689 : set_f( &st->hHQ_core->old_out[0], 0.f, L_frameTCX[ch] );
878 1473689 : set_f( &st->hTcxDec->syn_Overl[0], 0.f, L_frame[ch] / 2 );
879 1473689 : set_f( &st->hTcxDec->syn_OverlFB[0], 0.f, L_frameTCX[ch] / 2 );
880 1473689 : set_f( &st->hTcxDec->syn_Overl_TDAC[0], 0.f, L_frame[ch] / 2 );
881 1473689 : set_f( &st->hTcxDec->syn_Overl_TDACFB[0], 0.f, L_frameTCX[ch] / 2 );
882 : }
883 : }
884 :
885 12113302 : if ( !bfi && st->hTonalMDCTConc != NULL )
886 : {
887 11365232 : TonalMDCTConceal_SaveTimeSignal( st->hTonalMDCTConc, synthFB, L_frameTCX[ch] );
888 : }
889 :
890 12113302 : decoder_tcx_post( st, synth, synthFB, NULL, bfi, MCT_flag );
891 : }
892 : else /*ACELP core for ACELP-PLC */
893 : {
894 252600 : assert( st->bfi == 1 );
895 : /* PLC: [TCX: TD PLC] */
896 252600 : if ( MCT_flag )
897 : {
898 220516 : con_tcx( st, &synthFB[0], -1.f, NULL, 0, NULL );
899 : }
900 : else
901 : {
902 32084 : 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 252600 : lerp( synthFB, synth, st->L_frame, st->hTcxDec->L_frameTCX );
906 252600 : st->con_tcx = 1;
907 252600 : 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 252600 : if ( st->hTonalMDCTConc != NULL )
912 : {
913 252600 : TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, 0, 0, 0, 0 );
914 : }
915 : }
916 :
917 : /*--------------------------------------------------------------------------------*
918 : * Updates
919 : *--------------------------------------------------------------------------------*/
920 :
921 12365902 : st = sts[ch];
922 :
923 12365902 : if ( bfi && st->last_core != ACELP_CORE && st->core == ACELP_CORE )
924 : {
925 : /* Update FEC_scale_syn parameters */
926 252600 : 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 252600 : fer_energy( st->L_frame, st->clas_dec, synth, st->old_fpitch, &st->enr_old, st->L_frame );
933 : }
934 : }
935 :
936 : /* Update */
937 12365902 : mvr2r( synth_buf + st->L_frame, st->hTcxDec->old_synth, st->hTcxDec->old_synth_len );
938 12365902 : 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 12365902 : mvr2r( synth_bufFB + st->hTcxDec->L_frameTCX, st->hTcxDec->old_synthFB, st->hTcxDec->old_synth_lenFB );
940 12365902 : if ( st->hHQ_core != NULL )
941 : {
942 12365902 : 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 12365902 : mvr2r( st->lsp_q_cng, st->old_lsp_q_cng, M );
946 12365902 : mvr2r( st->lsf_q_cng, st->old_lsf_q_cng, M );
947 :
948 12365902 : st->last_is_cng = 0;
949 :
950 : /* Postfiltering */
951 12365902 : post_decoder( st, synth_buf, pit_gain[ch], pitch[ch], x[ch][0], st->p_bpf_noise_buf );
952 :
953 12365902 : 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 12365902 : st->last_voice_factor = 0.0f;
985 12365902 : st->last_coder_type = st->coder_type;
986 : }
987 :
988 : /* calculate coherence of signal needed when next frame is lost */
989 6182951 : if ( !bfi && !MCT_flag && hCPE->element_mode == IVAS_CPE_MDCT )
990 : {
991 : int16_t i;
992 : float nrgL, nrgR, xcorr;
993 :
994 1313318 : nrgL = nrgR = xcorr = EPSILON;
995 :
996 923781478 : for ( i = 0; i < sts[0]->hTcxDec->L_frameTCX; i++ )
997 : {
998 922468160 : nrgL += signal_outFB[0][i] * signal_outFB[0][i];
999 922468160 : nrgR += signal_outFB[1][i] * signal_outFB[1][i];
1000 922468160 : xcorr += signal_outFB[0][i] * signal_outFB[1][i];
1001 : }
1002 :
1003 1313318 : hCPE->hStereoMdct->lastCoh = fabsf( xcorr ) * inv_sqrt( nrgL * nrgR );
1004 1313318 : hCPE->hStereoMdct->lastCoh = min( hCPE->hStereoMdct->lastCoh, 1.f );
1005 : }
1006 :
1007 6182951 : return;
1008 : }
1009 :
1010 :
1011 : /*-----------------------------------------------------------------*
1012 : * ivas_mdct_core_tns_ns()
1013 : *
1014 : * reconstruct time signal
1015 : *-----------------------------------------------------------------*/
1016 :
1017 6182951 : 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 6182951 : sts = hCPE->hCoreCoder;
1042 6182951 : bfi = sts[0]->bfi;
1043 :
1044 6182951 : 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 18548853 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
1049 : {
1050 12365902 : st = sts[ch];
1051 12365902 : nSubframes[ch] = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
1052 12365902 : L_frame_global[ch] = st->L_frame / nSubframes[ch];
1053 12365902 : L_frameTCX_glob[ch] = st->hTcxDec->L_frameTCX / nSubframes[ch];
1054 12365902 : L_spec[ch] = st->hTcxCfg->tcx_coded_lines / nSubframes[ch];
1055 :
1056 12365902 : 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 1725611 : if ( st->hTonalMDCTConc != NULL )
1059 : {
1060 1725611 : TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, st->hTcxDec->L_frameTCX, 0, bfi, 0 );
1061 : }
1062 :
1063 : /* nothing to do for missing LFE */
1064 1725611 : continue;
1065 : }
1066 :
1067 21642831 : for ( k = 0; k < nSubframes[ch]; k++ )
1068 : {
1069 11002540 : init_tcx_info( st, L_frame_global[ch], L_frameTCX_glob[ch], k, bfi, &tcx_offset[ch],
1070 11002540 : &tcx_offsetFB[ch], &L_frame[ch], &L_frameTCX[ch], &left_rect[ch], &L_spec[ch] );
1071 :
1072 11002540 : if ( !bfi )
1073 : {
1074 10366412 : sns_interpolate_scalefactors( &sns_int_scf[0], &Aq[ch][k * M], DEC );
1075 :
1076 10366412 : if ( MCT_flag && st->hTonalMDCTConc != NULL && ( ( k + 1 ) == nSubframes[ch] ) )
1077 : {
1078 7383245 : 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 636128 : if ( st->hTonalMDCTConc != NULL )
1084 : {
1085 636128 : if ( !MCT_flag && st->hTcxDec->cummulative_damping_tcx != 1.f )
1086 125004 : {
1087 : float *scf_last, *scf_bg;
1088 : float fade_in, fade_out;
1089 :
1090 125004 : scf_last = &st->hTonalMDCTConc->lastBlockData.scaleFactors[0];
1091 125004 : scf_bg = &st->hTonalMDCTConc->scaleFactorsBackground[0];
1092 :
1093 125004 : st->hTonalMDCTConc->scf_fadeout *= 0.95f;
1094 :
1095 125004 : fade_out = st->hTonalMDCTConc->scf_fadeout;
1096 125004 : fade_in = 1 - fade_out;
1097 :
1098 8125260 : for ( int16_t i = 0; i < st->hTonalMDCTConc->nScaleFactors; i++ )
1099 : {
1100 8000256 : sns_int_scf[i] = fade_out * scf_last[i] + fade_in * scf_bg[i];
1101 : }
1102 : }
1103 : else
1104 : {
1105 511124 : st->hTonalMDCTConc->scf_fadeout = 1.0f;
1106 511124 : mvr2r( st->hTonalMDCTConc->lastBlockData.scaleFactors, &sns_int_scf[0], st->hTonalMDCTConc->nScaleFactors );
1107 : }
1108 : }
1109 : }
1110 :
1111 11002540 : 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 11002540 : 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 11002540 : 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 11002540 : 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 10640291 : if ( bfi && st->tonal_mdct_plc_active )
1126 : {
1127 6402 : TonalMDCTConceal_Apply( st->hTonalMDCTConc, x[ch][0], st->hTcxCfg->psychParamsCurrent );
1128 : }
1129 :
1130 10640291 : if ( ( bfi || MCT_flag ) && st->hTonalMDCTConc != NULL )
1131 : {
1132 8013655 : 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 6182951 : return;
1137 : }
|