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 1101699 : 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 1101699 : start = st0->next_bit_pos;
66 :
67 1101699 : if ( st->core == TCX_20_CORE )
68 : {
69 : /* read IGF payload */
70 1072878 : IGFDecReadLevel( st->hIGFDec, st0, ( st->last_core == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, 1 );
71 :
72 1072878 : IGFDecReadData( st->hIGFDec, st0, ( st->last_core == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, 1 );
73 : }
74 :
75 1101699 : if ( st->core == TCX_10_CORE )
76 : {
77 86463 : for ( k = 0; k < 2; k++ )
78 : {
79 57642 : IGFDecReadLevel( st->hIGFDec, st0, IGF_GRID_LB_SHORT, k == 0 ? 1 : 0 );
80 :
81 57642 : IGFDecReadData( st->hIGFDec, st0, IGF_GRID_LB_SHORT, k == 0 ? 1 : 0 );
82 :
83 57642 : IGFDecStoreTCX10SubFrameData( st->hIGFDec, k );
84 : }
85 : }
86 :
87 1101699 : st->side_bits_frame_channel += ( st0->next_bit_pos - start );
88 :
89 1101699 : return;
90 : }
91 :
92 :
93 : /*-----------------------------------------------------------------*
94 : * Function dec_prm_tcx_sidebits()
95 : *
96 : * decode TCX side parameters
97 : *-----------------------------------------------------------------*/
98 :
99 1877364 : 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 1877364 : hm_cfg.indexBuffer = NULL;
124 :
125 1877364 : start_bit_pos = st0->next_bit_pos;
126 :
127 : /* Init LTP data */
128 1877364 : st->hTcxDec->tcx_hm_LtpPitchLag = -1;
129 1877364 : st->hTcxLtpDec->tcxltp_gain = 0.0f;
130 :
131 : /*--------------------------------------------------------------------------------*
132 : * Header
133 : *--------------------------------------------------------------------------------*/
134 :
135 : /* Modes (ACE_GC, ACE_UC, TCX20, TCX10...) */
136 1877364 : getTCXMode( st, st0, MCT_flag );
137 :
138 1877364 : st->flagGuidedAcelp = 0;
139 :
140 1877364 : if ( st->dec_glr )
141 : {
142 0 : st->dec_glr_idx = -1;
143 : }
144 :
145 : /* last_core for core switching and error concealment */
146 1877364 : 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 1857000 : st->last_core = get_next_indice( st0, 1 );
160 1857000 : st->last_core_from_bs = st->last_core;
161 : }
162 :
163 1877364 : getTCXWindowing( st->core, st->last_core, st->element_mode, st->hTcxCfg, st0 );
164 :
165 1877364 : st->hTcxDec->kernel_type[0] = st->hTcxDec->kernel_type[1] = MDCT_IV;
166 1877364 : st->hTcxDec->kernel_type[0] = get_next_indice( st0, st->last_core_from_bs != ACELP_CORE ? 2 : 1 );
167 1877364 : if ( st->core == TCX_10_CORE )
168 : {
169 43509 : st->hTcxDec->kernel_type[1] = 2 * ( st->hTcxDec->kernel_type[0] & 1 ) + get_next_indice( st0, 1 );
170 : }
171 1877364 : if ( st->core == TCX_20_CORE )
172 : {
173 1833855 : st->transform_type[0] = st->transform_type[1] = TCX_20;
174 : }
175 : else
176 : {
177 43509 : if ( st->hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP )
178 : {
179 29400 : st->transform_type[0] = TCX_5;
180 29400 : st->transform_type[1] = TCX_10;
181 : }
182 14109 : else if ( st->hTcxCfg->tcx_last_overlap_mode == FULL_OVERLAP )
183 : {
184 12633 : st->transform_type[0] = TCX_10;
185 12633 : st->transform_type[1] = TCX_5;
186 : }
187 : else
188 : {
189 1476 : st->transform_type[0] = st->transform_type[1] = TCX_5;
190 : }
191 : }
192 :
193 : /*--------------------------------------------------------------------------------*
194 : * TCX20/TCX10 parameters
195 : *--------------------------------------------------------------------------------*/
196 :
197 1877364 : getTCXparam( st, st0, hm_cfg, param, 0, 0, ( ( ch > 0 ) && ( tnsSize ) && ( tnsSize[0] + tnsSize[1] > 0 ) ? tnsSize : NULL ), p_param, nTnsBitsTCX10, 0 );
198 :
199 1877364 : st->side_bits_frame_channel = st0->next_bit_pos - start_bit_pos;
200 :
201 1877364 : return;
202 : }
203 :
204 :
205 : /*-----------------------------------------------------------------*
206 : * Function dec_prm_tcx_spec()
207 : *
208 : * decode TCX core parameters
209 : *-----------------------------------------------------------------*/
210 :
211 1877364 : 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 1877364 : 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 1877364 : start_bit_pos = st->next_bit_pos;
243 :
244 1877364 : nf_bits = 0;
245 1877364 : nbits_tcx = 0;
246 :
247 1877364 : 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 1877364 : if ( st->core == TCX_10_CORE && st->element_mode == IVAS_CPE_MDCT )
251 : {
252 : int16_t nTnsBitsTCX10Tmp[NB_DIV];
253 43509 : nTnsBitsTCX10Tmp[0] = nTnsBitsTCX10[0];
254 43509 : nTnsBitsTCX10Tmp[1] = nTnsBitsTCX10[1];
255 :
256 : /*compute target bits */
257 43509 : nbits_tcx = st->bits_frame_channel + nTnsBitsTCX10[0] + nTnsBitsTCX10[1] + nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL );
258 43509 : ivas_mdct_tcx10_bit_distribution( target_bitsTCX10, nbits_tcx, nTnsBitsTCX10Tmp );
259 : }
260 :
261 : /*--------------------------------------------------------------------------------*
262 : * TCX20/TCX10 parameters
263 : *--------------------------------------------------------------------------------*/
264 :
265 1877364 : getTCXparam( st, st, hm_cfg, param, 0, 0, NULL, p_param, target_bitsTCX10, 1 );
266 :
267 1877364 : nf_bits = nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL );
268 :
269 1877364 : 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 1877364 : bitsRead[0] = st->next_bit_pos - start_bit_pos;
276 :
277 1877364 : 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 1038039 : 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 1038039 : sts = hCPE->hCoreCoder;
308 1038039 : bfi = sts[0]->bfi;
309 :
310 1038039 : set_s( total_nbbits, 0, MCT_MAX_CHANNELS );
311 1038039 : set_s( bitsRead, 0, MCT_MAX_CHANNELS );
312 :
313 3114117 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
314 : {
315 2076078 : if ( MCT_flag )
316 : {
317 : /* Initialization or re-configuration of Stereo TCX */
318 1595766 : stereo_tcx_init_dec( sts[ch], 1, hCPE->last_element_mode );
319 : }
320 2076078 : sts[ch]->enablePlcWaveadjust = 0;
321 : }
322 :
323 1038039 : if ( !bfi )
324 : {
325 3114117 : 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 2076078 : if ( odd_channel_cpe && ch == 1 )
329 : {
330 173136 : continue;
331 : }
332 :
333 1902942 : st = sts[ch];
334 1902942 : if ( MCT_flag )
335 : {
336 1422630 : tmp = get_next_indice( st0, 1 );
337 1422630 : if ( tmp )
338 : {
339 25578 : st->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
340 : }
341 : else
342 : {
343 1397052 : st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
344 : }
345 : }
346 : }
347 :
348 : /*read channel bitrate ratios from bitstream*/
349 3114117 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
350 : {
351 2076078 : st = sts[ch];
352 2076078 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
353 : {
354 198714 : sts[ch]->coder_type = INACTIVE;
355 198714 : sts[ch]->side_bits_frame_channel = 0;
356 198714 : continue;
357 : }
358 :
359 1877364 : tmp = ch;
360 1877364 : if ( ch == 1 && param_lpc[0][0] == 2 )
361 : {
362 0 : tmp = 3;
363 : }
364 :
365 1877364 : 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 1877364 : assert( st->BER_detect != 1 );
368 : }
369 :
370 : /*--------------------------------------------------------------------------------*
371 : * SNS parameters
372 : *--------------------------------------------------------------------------------*/
373 :
374 1038039 : sns_low_br_mode = 0;
375 1038039 : skipped_first_channel = 0;
376 1038039 : 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 2694357 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
439 : {
440 1796238 : st = sts[ch];
441 :
442 1796238 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
443 : {
444 198714 : skipped_first_channel = 1;
445 198714 : continue;
446 : }
447 :
448 1597524 : start_bit_pos_sns = st0->next_bit_pos;
449 :
450 1597524 : if ( ch == 0 || skipped_first_channel )
451 : {
452 : /* read SNS stereo mode */
453 887118 : 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 887118 : 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 1597524 : tmp = ch;
463 1597524 : if ( ch == 1 && param_lpc[0][0] == 2 )
464 : {
465 551061 : tmp = 3;
466 : }
467 :
468 1597524 : getLPCparam( st, ¶m_lpc[ch][0], st0, tmp, sns_low_br_mode && !( sts[0]->core == TCX_20 && sts[1]->core == TCX_20 ) );
469 :
470 1597524 : st->side_bits_frame_channel += st0->next_bit_pos - start_bit_pos_sns;
471 : }
472 : }
473 : }
474 :
475 1038039 : return;
476 : }
477 :
478 :
479 : /*-----------------------------------------------------------------*
480 : * ivas_mdct_core_invQ()
481 : *
482 : * Inverse processing steps up to inverse quantization
483 : *-----------------------------------------------------------------*/
484 :
485 1058271 : 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 1058271 : push_wmops( "mdct_core_invQ" );
527 1058271 : sts = hCPE->hCoreCoder;
528 1058271 : bfi = sts[0]->bfi;
529 1058271 : noise_gen_mode_bfi = NOISE_GEN_MODE_UNDEF;
530 :
531 1058271 : set_f( xn_buf, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX );
532 1058271 : set_s( total_nbbits, 0, CPE_CHANNELS );
533 1058271 : set_s( bitsRead, 0, CPE_CHANNELS );
534 1058271 : tmp_concealment_method = 0;
535 :
536 3174813 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
537 : {
538 2116542 : sts[ch]->enablePlcWaveadjust = 0;
539 : }
540 :
541 : /* temporarily restore LR representation of previous frame for PLC mode decision (done on the individual channels) */
542 1058271 : 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 1058271 : 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 3174813 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
569 : {
570 2116542 : st = sts[ch];
571 :
572 2116542 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
573 : {
574 202164 : st->total_brate = st->bits_frame_channel;
575 202164 : continue;
576 : }
577 1914378 : prm[ch] = param[ch]; /* to avoid compilation warnings */
578 :
579 : /*Adjust bit per frame*/
580 1914378 : if ( !bfi )
581 : {
582 1877364 : st->bits_frame_core = st->bits_frame_channel;
583 :
584 1877364 : 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 1914378 : L_frame[ch] = st->L_frame;
589 :
590 : /*--------------------------------------------------------------------------------*
591 : * BITSTREAM DECODING
592 : *--------------------------------------------------------------------------------*/
593 :
594 1914378 : total_nbbits[ch] = st->bits_frame_channel;
595 :
596 1914378 : if ( !bfi )
597 : {
598 1877364 : st->second_last_core = st->last_core;
599 1877364 : if ( hCPE->cpe_id == 0 && ch == 0 )
600 : {
601 : /* add mct and side bits to first handle bitrate to avoid false BER detection */
602 535809 : st->total_brate += ( st->next_bit_pos * FRAMES_PER_SEC );
603 :
604 535809 : 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 535809 : 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 1341555 : dec_prm_tcx_spec( st, param[ch], &total_nbbits[ch], &bitsRead[ch], p_param[ch], nTnsBitsTCX10[ch] );
612 : }
613 :
614 1877364 : 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 1914378 : if ( ( !st->bfi || st->hTcxCfg->psychParamsCurrent == NULL ) && st->core > ACELP_CORE )
629 : {
630 : int16_t last_frame_was_concealed_cng;
631 1877364 : last_frame_was_concealed_cng = ( st->last_core == ACELP_CORE ) && ( st->last_core != st->last_core_from_bs );
632 1877364 : 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 1914378 : if ( !bfi )
638 : {
639 1877364 : if ( st->prev_bfi == 1 )
640 : {
641 19758 : st->prev_nbLostCmpt = st->nbLostCmpt;
642 : }
643 : else
644 : {
645 1857606 : st->prev_nbLostCmpt = 0;
646 : }
647 :
648 1877364 : st->nbLostCmpt = 0;
649 : }
650 : }
651 :
652 1058271 : 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 1058271 : if ( bfi == 0 )
676 : {
677 1038039 : 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 898119 : 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 687453 : 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 631998 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
690 : {
691 421332 : st = sts[ch];
692 421332 : if ( st->mct_chan_mode != MCT_CHAN_MODE_IGNORE )
693 : {
694 222618 : 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 3174813 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
707 : {
708 2116542 : st = sts[ch];
709 :
710 2116542 : 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 3174813 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
721 : {
722 2116542 : st = sts[ch];
723 :
724 2116542 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
725 : {
726 202164 : set_f( x[ch][0], 0.f, st->hTcxCfg->tcx_coded_lines );
727 : /* usually set in decoder_tcx_invQ(), needed for concealment */
728 202164 : st->hTcxDec->damping = 0.f;
729 202164 : st->hTcxDec->gainHelper = 1.f;
730 202164 : st->hTcxDec->stepCompensate = 0.f;
731 :
732 202164 : continue;
733 : }
734 :
735 1914378 : if ( !bfi || ( bfi && st->core != ACELP_CORE ) )
736 : {
737 1902210 : nSubframes[ch] = ( st->core == TCX_10_CORE ) ? NB_DIV : 1;
738 :
739 3848313 : for ( k = 0; k < nSubframes[ch]; k++ )
740 : {
741 : /* Stability Factor */
742 1946103 : if ( !bfi )
743 : {
744 1920873 : 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 1946103 : prm[ch] = param[ch] + ( k * DEC_NPRM_DIV );
753 1946103 : L_frameTCX_global[ch] = st->hTcxDec->L_frameTCX / nSubframes[ch];
754 1946103 : L_spec[ch] = st->hTcxCfg->tcx_coded_lines / nSubframes[ch];
755 :
756 1946103 : 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 1946103 : tmp_concealment_method = 0;
759 1946103 : nf_seed = prm[ch][1 + NOISE_FILL_RANGES + LTPSIZE];
760 1946103 : 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 1946103 : nf_seed = 0;
765 :
766 1946103 : 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 1946103 : mvr2r( x[ch][k], x_0[ch][k], L_frameTCX[ch] );
769 :
770 1946103 : 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 1946103 : 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 1946103 : 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 1058271 : pop_wmops();
783 1058271 : return;
784 : }
785 :
786 :
787 : /*-----------------------------------------------------------------*
788 : * ivas_mdct_core_reconstruct()
789 : *
790 : * reconstruct time signal
791 : *-----------------------------------------------------------------*/
792 :
793 1058271 : 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 : #ifdef FIX_1320_STACK_CPE_DECODER
808 : float synth_buf[OLD_SYNTH_INTERNAL_DEC + L_FRAME_PLUS_INTERNAL + M];
809 : #else
810 : float synth_buf[OLD_SYNTH_SIZE_DEC + L_FRAME_PLUS + M];
811 : #endif
812 : float *synth;
813 : float synth_bufFB[OLD_SYNTH_SIZE_DEC + L_FRAME_PLUS + M];
814 : float *synthFB;
815 : /* TCX */
816 : float xn_buf[L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX];
817 : int16_t tcx_offset[CPE_CHANNELS];
818 : int16_t tcx_offsetFB[CPE_CHANNELS];
819 : int16_t left_rect[CPE_CHANNELS];
820 : int16_t L_spec[CPE_CHANNELS];
821 :
822 : int16_t pitch[CPE_CHANNELS][NB_SUBFR16k];
823 : float pit_gain[CPE_CHANNELS][NB_SUBFR16k];
824 : int16_t skip_decoding;
825 :
826 1058271 : set_f( xn_buf, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX );
827 :
828 : /* Initializations */
829 1058271 : sts = hCPE->hCoreCoder;
830 1058271 : bfi = sts[0]->bfi;
831 :
832 : /* TNS, ITF, IMDCT and updates */
833 3174813 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
834 : {
835 2116542 : st = sts[ch];
836 :
837 2116542 : skip_decoding = 0;
838 2116542 : if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
839 : {
840 213138 : skip_decoding = 1;
841 : }
842 :
843 2116542 : nSubframes[ch] = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
844 2116542 : synth = synth_buf + st->hTcxDec->old_synth_len;
845 2116542 : synthFB = synth_bufFB + st->hTcxDec->old_synth_lenFB;
846 2116542 : mvr2r( st->hTcxDec->old_synth, synth_buf, st->hTcxDec->old_synth_len );
847 2116542 : mvr2r( st->hTcxDec->old_synthFB, synth_bufFB, st->hTcxDec->old_synth_lenFB );
848 : #ifdef FIX_1320_STACK_CPE_DECODER
849 2116542 : set_zero( synth, L_FRAME_PLUS_INTERNAL + M );
850 : #else
851 : set_zero( synth, L_FRAME_PLUS + M );
852 : #endif
853 2116542 : set_zero( synthFB, L_FRAME_PLUS + M );
854 :
855 2116542 : if ( st->core != ACELP_CORE )
856 : {
857 4252641 : for ( k = 0; k < nSubframes[ch]; k++ )
858 : {
859 2148267 : L_spec[ch] = st->hTcxCfg->tcx_coded_lines / nSubframes[ch];
860 2148267 : L_frame_global[ch] = st->L_frame / nSubframes[ch];
861 2148267 : L_frame_globalTCX[ch] = st->hTcxDec->L_frameTCX / nSubframes[ch];
862 :
863 2148267 : init_tcx_info( st, L_frame_global[ch], L_frame_globalTCX[ch], k, bfi, &tcx_offset[ch],
864 2148267 : &tcx_offsetFB[ch], &L_frame[ch], &L_frameTCX[ch], &left_rect[ch], &L_spec[ch] );
865 :
866 2148267 : if ( !skip_decoding )
867 : {
868 1934868 : 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,
869 1934868 : ( ( 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],
870 1934868 : fUseTns[ch][k], &synth[k * L_frame[ch]], &synthFB[k * L_frameTCX[ch]], bfi, k, 0 );
871 : }
872 : else
873 : {
874 213399 : set_f( &synth[k * L_frame[ch]], 0.f, L_frame[ch] );
875 213399 : set_f( &synthFB[k * L_frame[ch]], 0.f, L_frameTCX[ch] );
876 : /* Note: these buffers are not subframe-based, hence no indexing with k */
877 213399 : set_f( &st->hHQ_core->old_outLB[0], 0.f, L_frame[ch] );
878 213399 : set_f( &st->hHQ_core->old_out[0], 0.f, L_frameTCX[ch] );
879 213399 : set_f( &st->hTcxDec->syn_Overl[0], 0.f, L_frame[ch] / 2 );
880 213399 : set_f( &st->hTcxDec->syn_OverlFB[0], 0.f, L_frameTCX[ch] / 2 );
881 213399 : set_f( &st->hTcxDec->syn_Overl_TDAC[0], 0.f, L_frame[ch] / 2 );
882 213399 : set_f( &st->hTcxDec->syn_Overl_TDACFB[0], 0.f, L_frameTCX[ch] / 2 );
883 : }
884 : }
885 :
886 2104374 : if ( !bfi && st->hTonalMDCTConc != NULL )
887 : {
888 2076078 : TonalMDCTConceal_SaveTimeSignal( st->hTonalMDCTConc, synthFB, L_frameTCX[ch] );
889 : }
890 :
891 2104374 : decoder_tcx_post( st, synth, synthFB, NULL, bfi, MCT_flag );
892 : }
893 : else /*ACELP core for ACELP-PLC */
894 : {
895 12168 : assert( st->bfi == 1 );
896 : /* PLC: [TCX: TD PLC] */
897 12168 : if ( MCT_flag )
898 : {
899 8877 : con_tcx( st, &synthFB[0], -1.f, NULL, 0, NULL );
900 : }
901 : else
902 : {
903 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] );
904 : }
905 :
906 12168 : lerp( synthFB, synth, st->L_frame, st->hTcxDec->L_frameTCX );
907 12168 : st->con_tcx = 1;
908 12168 : set_f( &st->mem_pitch_gain[2], st->lp_gainp, st->nb_subfr );
909 :
910 : /* PLC: [TCX: Tonal Concealment] */
911 : /* Signal that this frame is not TCX */
912 12168 : if ( st->hTonalMDCTConc != NULL )
913 : {
914 12168 : TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, 0, 0, 0, 0 );
915 : }
916 : }
917 :
918 : /*--------------------------------------------------------------------------------*
919 : * Updates
920 : *--------------------------------------------------------------------------------*/
921 :
922 2116542 : st = sts[ch];
923 :
924 2116542 : if ( bfi && st->last_core != ACELP_CORE && st->core == ACELP_CORE )
925 : {
926 : /* Update FEC_scale_syn parameters */
927 12168 : if ( st->hTcxLtpDec->tcxltp_gain == 0 )
928 : {
929 0 : fer_energy( st->L_frame, UNVOICED, synth, (float) ( st->L_frame / 2 ), &st->enr_old, st->L_frame );
930 : }
931 : else
932 : {
933 12168 : fer_energy( st->L_frame, st->clas_dec, synth, st->old_fpitch, &st->enr_old, st->L_frame );
934 : }
935 : }
936 :
937 : /* Update */
938 2116542 : mvr2r( synth_buf + st->L_frame, st->hTcxDec->old_synth, st->hTcxDec->old_synth_len );
939 2116542 : 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 ) );
940 2116542 : mvr2r( synth_bufFB + st->hTcxDec->L_frameTCX, st->hTcxDec->old_synthFB, st->hTcxDec->old_synth_lenFB );
941 2116542 : if ( st->hHQ_core != NULL )
942 : {
943 2116542 : 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 ) );
944 : }
945 :
946 2116542 : mvr2r( st->lsp_q_cng, st->old_lsp_q_cng, M );
947 2116542 : mvr2r( st->lsf_q_cng, st->old_lsf_q_cng, M );
948 :
949 2116542 : st->last_is_cng = 0;
950 :
951 : /* Postfiltering */
952 2116542 : post_decoder( st, synth_buf, pit_gain[ch], pitch[ch], x[ch][0], st->p_bpf_noise_buf );
953 :
954 2116542 : if ( signal_outFB[ch] )
955 : {
956 2116542 : mvr2r( synthFB, signal_outFB[ch], st->hTcxDec->L_frameTCX );
957 : }
958 :
959 : #ifdef DEBUG_PLC_INFO
960 : {
961 : int16_t i;
962 : int16_t tmp[L_FRAME48k];
963 : static FILE *sP = NULL;
964 :
965 : for ( i = 0; i < st->hTcxDec->L_frameTCX; i++ )
966 : {
967 : tmp[i] = (int16_t) ( signal_outFB[ch][i] + 0.5f );
968 : }
969 : if ( ch == 0 )
970 : {
971 : dbgwrite( tmp, sizeof( int16_t ), st->hTcxDec->L_frameTCX, 1, "./res/mdct_stereo_dec_synthFB_ch0.pcm" );
972 : #ifndef DEBUG_MODE_INFO
973 : dbgwrite( &st->bfi, sizeof( int16_t ), 1, st->hTcxDec->L_frameTCX, "./res/bfi.pcm" );
974 : #endif
975 : dbgwrite( &st->tonal_mdct_plc_active, sizeof( int16_t ), 1, L_frameTCX[ch], "./res/tonal_mdct_active_ch0.pcm" );
976 : dbgwrite( &st->tonal_mdct_plc_active, sizeof( int16_t ), 1, L_frameTCX[ch], "./res/tonal_mdct_active_ch0.pcm" );
977 : dbgwrite( &st->con_tcx, sizeof( int16_t ), 1, 640, "./res/con_tcx_ch0.pcm" );
978 : }
979 : else
980 : {
981 : dbgwrite( tmp, sizeof( int16_t ), st->hTcxDec->L_frameTCX, 1, "./res/mdct_stereo_dec_synthFB_ch1.pcm" );
982 : dbgwrite( &st->tonal_mdct_plc_active, sizeof( int16_t ), 1, L_frameTCX[ch], "./res/tonal_mdct_active_ch1.pcm" );
983 : dbgwrite( &st->con_tcx, sizeof( int16_t ), 1, 640, "./res/con_tcx_ch1.pcm" );
984 : }
985 : }
986 : #endif
987 :
988 : /* updates */
989 2116542 : st->last_voice_factor = 0.0f;
990 2116542 : st->last_coder_type = st->coder_type;
991 : }
992 :
993 : /* calculate coherence of signal needed when next frame is lost */
994 1058271 : if ( !bfi && !MCT_flag && hCPE->element_mode == IVAS_CPE_MDCT )
995 : {
996 : int16_t i;
997 : float nrgL, nrgR, xcorr;
998 :
999 240156 : nrgL = nrgR = xcorr = EPSILON;
1000 :
1001 180078876 : for ( i = 0; i < sts[0]->hTcxDec->L_frameTCX; i++ )
1002 : {
1003 179838720 : nrgL += signal_outFB[0][i] * signal_outFB[0][i];
1004 179838720 : nrgR += signal_outFB[1][i] * signal_outFB[1][i];
1005 179838720 : xcorr += signal_outFB[0][i] * signal_outFB[1][i];
1006 : }
1007 :
1008 240156 : hCPE->hStereoMdct->lastCoh = fabsf( xcorr ) * inv_sqrt( nrgL * nrgR );
1009 240156 : hCPE->hStereoMdct->lastCoh = min( hCPE->hStereoMdct->lastCoh, 1.f );
1010 : }
1011 :
1012 1058271 : return;
1013 : }
1014 :
1015 :
1016 : /*-----------------------------------------------------------------*
1017 : * ivas_mdct_core_tns_ns()
1018 : *
1019 : * reconstruct time signal
1020 : *-----------------------------------------------------------------*/
1021 :
1022 1058271 : void ivas_mdct_core_tns_ns(
1023 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
1024 : int16_t fUseTns[CPE_CHANNELS][NB_DIV], /* i : two entries for each channel in TCX10 */
1025 : STnsData tnsData[CPE_CHANNELS][NB_DIV], /* o : TNS parameter */
1026 : float *x[CPE_CHANNELS][NB_DIV], /* o : synthesis @internal_FS */
1027 : float Aq[CPE_CHANNELS][( NB_SUBFR16k + 1 ) * ( M + 1 )], /* o : LP coefficients */
1028 : const int16_t MCT_flag /* i : hMCT handle allocated (1) or not (0) */
1029 : )
1030 : {
1031 : int16_t ch, k, bfi;
1032 : Decoder_State **sts, *st;
1033 : /* Framing */
1034 : int16_t L_frame[CPE_CHANNELS], L_frameTCX[CPE_CHANNELS], nSubframes[CPE_CHANNELS];
1035 : int16_t L_frame_global[CPE_CHANNELS], L_frameTCX_glob[CPE_CHANNELS];
1036 :
1037 : /* TCX */
1038 : float xn_buf[L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX];
1039 : int16_t tcx_offset[CPE_CHANNELS];
1040 : int16_t tcx_offsetFB[CPE_CHANNELS];
1041 : int16_t left_rect[CPE_CHANNELS];
1042 : int16_t L_spec[CPE_CHANNELS];
1043 : float sns_int_scf[FDNS_NPTS];
1044 :
1045 : /* Initializations */
1046 1058271 : sts = hCPE->hCoreCoder;
1047 1058271 : bfi = sts[0]->bfi;
1048 :
1049 1058271 : set_f( xn_buf, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX );
1050 :
1051 :
1052 : /* TNS, ITF, IMDCT and updates */
1053 3174813 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
1054 : {
1055 2116542 : st = sts[ch];
1056 2116542 : nSubframes[ch] = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
1057 2116542 : L_frame_global[ch] = st->L_frame / nSubframes[ch];
1058 2116542 : L_frameTCX_glob[ch] = st->hTcxDec->L_frameTCX / nSubframes[ch];
1059 2116542 : L_spec[ch] = st->hTcxCfg->tcx_coded_lines / nSubframes[ch];
1060 :
1061 2116542 : 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 */
1062 : {
1063 225306 : if ( st->hTonalMDCTConc != NULL )
1064 : {
1065 225306 : TonalMDCTConceal_UpdateState( st->hTonalMDCTConc, st->hTcxDec->L_frameTCX, 0, bfi, 0 );
1066 : }
1067 :
1068 : /* nothing to do for missing LFE */
1069 225306 : continue;
1070 : }
1071 :
1072 3826104 : for ( k = 0; k < nSubframes[ch]; k++ )
1073 : {
1074 1934868 : init_tcx_info( st, L_frame_global[ch], L_frameTCX_glob[ch], k, bfi, &tcx_offset[ch],
1075 1934868 : &tcx_offsetFB[ch], &L_frame[ch], &L_frameTCX[ch], &left_rect[ch], &L_spec[ch] );
1076 :
1077 1934868 : if ( !bfi )
1078 : {
1079 1909638 : sns_interpolate_scalefactors( &sns_int_scf[0], &Aq[ch][k * M], DEC );
1080 :
1081 1909638 : if ( MCT_flag && st->hTonalMDCTConc != NULL && ( ( k + 1 ) == nSubframes[ch] ) )
1082 : {
1083 1386078 : 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] ) );
1084 : }
1085 : }
1086 : else
1087 : {
1088 25230 : if ( st->hTonalMDCTConc != NULL )
1089 : {
1090 25230 : if ( !MCT_flag && st->hTcxDec->cummulative_damping_tcx != 1.f )
1091 4899 : {
1092 : float *scf_last, *scf_bg;
1093 : float fade_in, fade_out;
1094 :
1095 4899 : scf_last = &st->hTonalMDCTConc->lastBlockData.scaleFactors[0];
1096 4899 : scf_bg = &st->hTonalMDCTConc->scaleFactorsBackground[0];
1097 :
1098 4899 : st->hTonalMDCTConc->scf_fadeout *= 0.95f;
1099 :
1100 4899 : fade_out = st->hTonalMDCTConc->scf_fadeout;
1101 4899 : fade_in = 1 - fade_out;
1102 :
1103 318435 : for ( int16_t i = 0; i < st->hTonalMDCTConc->nScaleFactors; i++ )
1104 : {
1105 313536 : sns_int_scf[i] = fade_out * scf_last[i] + fade_in * scf_bg[i];
1106 : }
1107 : }
1108 : else
1109 : {
1110 20331 : st->hTonalMDCTConc->scf_fadeout = 1.0f;
1111 20331 : mvr2r( st->hTonalMDCTConc->lastBlockData.scaleFactors, &sns_int_scf[0], st->hTonalMDCTConc->nScaleFactors );
1112 : }
1113 : }
1114 : }
1115 :
1116 1934868 : 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 );
1117 :
1118 1934868 : sns_shape_spectrum( x[ch][k], st->hTcxCfg->psychParamsCurrent, &sns_int_scf[0], st->hTcxCfg->psychParamsCurrent->nBins );
1119 1934868 : 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 );
1120 :
1121 1934868 : 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 );
1122 : }
1123 :
1124 1891236 : if ( bfi && st->tonal_mdct_plc_active )
1125 : {
1126 1035 : TonalMDCTConceal_Apply( st->hTonalMDCTConc, x[ch][0], st->hTcxCfg->psychParamsCurrent );
1127 : }
1128 :
1129 1891236 : if ( ( bfi || MCT_flag ) && st->hTonalMDCTConc != NULL )
1130 : {
1131 1410924 : 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 );
1132 : }
1133 : }
1134 :
1135 1058271 : return;
1136 : }
|