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