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 <math.h>
37 : #include "ivas_cnst.h"
38 : #include "ivas_prot.h"
39 : #include "prot.h"
40 : #include "wmc_auto.h"
41 :
42 :
43 : /*-------------------------------------------------------------------*
44 : * Local function prototypes
45 : *-------------------------------------------------------------------*/
46 :
47 : static void inverseBwMS( const int16_t startLine, const int16_t stopLine, float x0[], float x1[], const float norm_fac );
48 :
49 :
50 : /*-------------------------------------------------------------------*
51 : * parse_stereo_from_bitstream
52 : *
53 : *
54 : *-------------------------------------------------------------------*/
55 :
56 10804796 : void parse_stereo_from_bitstream(
57 : STEREO_MDCT_DEC_DATA_HANDLE hStereoMdct, /* i/o: MDCT stereo decoder structure */
58 : Decoder_State **sts, /* i/o: decoder state structure */
59 : const int16_t mct_on, /* i : flag mct block (1) or stereo (0)*/
60 : const int16_t isSBAStereoMode, /* i : flag core coding for SBA */
61 : Decoder_State *st0, /* i/o: decoder state structure for Bstr*/
62 : int16_t ms_mask[NB_DIV][MAX_SFB] /* o : bandwise MS mask */
63 : )
64 : {
65 : int16_t i, k, nSubframes, mdct_stereo_mode;
66 : STEREO_MDCT_BAND_PARAMETERS *sfbConf;
67 : #ifdef DEBUGGING
68 : int16_t nbits_start = st0->next_bit_pos;
69 : #endif
70 :
71 10804796 : if ( !isSBAStereoMode )
72 : {
73 8618637 : nSubframes = ( sts[0]->core == TCX_10_CORE || ( sts[0]->core != sts[1]->core ) ) ? NB_DIV : 1;
74 8618637 : sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
75 8618637 : if ( sts[0]->last_core_from_bs == ACELP_CORE )
76 : {
77 31751 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
78 : }
79 :
80 8618637 : if ( hStereoMdct->use_itd )
81 : {
82 : int16_t I;
83 :
84 450433 : hStereoMdct->itd_mode = get_next_indice( st0, STEREO_DFT_ITD_MODE_NBITS );
85 : /*(*nb_bits) += STEREO_DFT_ITD_MODE_NBITS;*/ /*ITD mode flag: 1bit*/
86 :
87 450433 : hStereoMdct->itd = 0.f;
88 450433 : if ( hStereoMdct->itd_mode )
89 : {
90 100656 : /*(*nb_bits) += */ read_itd( st0, &I );
91 100656 : stereo_dft_dequantize_itd( &I, &hStereoMdct->itd, st0->output_Fs );
92 : }
93 : }
94 :
95 17471598 : for ( k = 0; k < nSubframes; k++ )
96 : {
97 8852961 : mdct_stereo_mode = get_next_indice( st0, 1 );
98 8852961 : if ( mdct_stereo_mode )
99 : {
100 8530271 : mdct_stereo_mode = 1 + get_next_indice( st0, 1 );
101 : }
102 8852961 : switch ( mdct_stereo_mode )
103 : {
104 322690 : case 0:
105 322690 : hStereoMdct->mdct_stereo_mode[k] = SMDCT_DUAL_MONO;
106 322690 : break;
107 5995791 : case 1:
108 5995791 : hStereoMdct->mdct_stereo_mode[k] = SMDCT_MS_FULL;
109 5995791 : break;
110 2534480 : case 2:
111 2534480 : hStereoMdct->mdct_stereo_mode[k] = SMDCT_BW_MS;
112 2534480 : break;
113 0 : default:
114 0 : assert( !"Not supported stereo mode\n" );
115 : }
116 :
117 8852961 : if ( !mct_on )
118 : {
119 2908468 : if ( sts[0]->core == sts[1]->core || k == 0 )
120 : {
121 2891576 : hStereoMdct->global_ild[k] = get_next_indice( st0, SMDCT_GLOBAL_ILD_BITS );
122 5713080 : assert( ( hStereoMdct->global_ild[k] > 0 ) && ( hStereoMdct->global_ild[k] < SMDCT_ILD_RANGE ) );
123 : }
124 : else
125 : {
126 16892 : hStereoMdct->global_ild[1] = hStereoMdct->global_ild[0];
127 : }
128 : }
129 :
130 8852961 : set_s( ms_mask[k], ( hStereoMdct->mdct_stereo_mode[k] == SMDCT_MS_FULL ) ? 1 : 0, sfbConf->nBandsStereoCore );
131 :
132 8852961 : if ( hStereoMdct->mdct_stereo_mode[k] == SMDCT_BW_MS )
133 : {
134 105801677 : for ( i = 0; i < sfbConf->nBandsStereoCore; i++ )
135 : {
136 103267197 : ms_mask[k][i] = get_next_indice( st0, 1 );
137 : }
138 : }
139 :
140 8852961 : if ( st0->igf )
141 : {
142 5992173 : mdct_stereo_mode = get_next_indice( st0, 1 );
143 5992173 : if ( mdct_stereo_mode )
144 : {
145 3969240 : mdct_stereo_mode = 1 + get_next_indice( st0, 1 );
146 : }
147 :
148 5992173 : switch ( mdct_stereo_mode )
149 : {
150 2022933 : case 0:
151 2022933 : hStereoMdct->IGFStereoMode[k] = SMDCT_DUAL_MONO;
152 2022933 : break;
153 2753143 : case 1:
154 2753143 : hStereoMdct->IGFStereoMode[k] = SMDCT_MS_FULL;
155 2753143 : break;
156 1216097 : case 2:
157 1216097 : hStereoMdct->IGFStereoMode[k] = SMDCT_BW_MS;
158 1216097 : break;
159 0 : default:
160 0 : assert( !"Not supported stereo mode\n" );
161 : }
162 :
163 5992173 : set_s( &ms_mask[k][sfbConf->nBandsStereoCore], ( hStereoMdct->IGFStereoMode[k] == SMDCT_MS_FULL ) ? 1 : 0, sfbConf->sfbCnt - sfbConf->nBandsStereoCore );
164 :
165 5992173 : if ( hStereoMdct->IGFStereoMode[k] == SMDCT_BW_MS )
166 : {
167 8220582 : for ( i = sfbConf->nBandsStereoCore; i < sfbConf->sfbCnt; i++ )
168 : {
169 7004485 : ms_mask[k][i] = get_next_indice( st0, 1 );
170 : }
171 : }
172 : }
173 : else
174 : {
175 2860788 : hStereoMdct->IGFStereoMode[k] = SMDCT_DUAL_MONO;
176 : }
177 : }
178 : }
179 :
180 10804796 : if ( !mct_on )
181 : {
182 5000837 : hStereoMdct->split_ratio = SMDCT_EQUAL_RATIO_RANGE; /* Equal bits to both channels */
183 5000837 : hStereoMdct->split_ratio = get_next_indice( st0, SMDCT_NBBITS_SPLIT_RATIO );
184 :
185 5000837 : assert( hStereoMdct->split_ratio > 0 );
186 : }
187 :
188 : #ifdef DEBUGGING
189 : {
190 : int16_t nBitsStereo = st0->next_bit_pos - nbits_start;
191 :
192 : assert( nBitsStereo <= st0->bits_frame_nominal );
193 : }
194 : #endif
195 :
196 10804796 : return;
197 : }
198 :
199 :
200 : /*-------------------------------------------------------------------*
201 : * stereo_decoder_tcx()
202 : *
203 : * apply stereo processing (inverse MS and global ILD)
204 : *-------------------------------------------------------------------*/
205 :
206 : #define NF_RED_FAC 0.75f
207 :
208 8790386 : void stereo_decoder_tcx(
209 : STEREO_MDCT_DEC_DATA *hStereoMdct, /* i/o: MDCT stereo decoder structure */
210 : int16_t ms_mask[NB_DIV][MAX_SFB], /* i : bandwise MS mask */
211 : float *spec_r_0[NB_DIV], /* i/o: spectrum right channel */
212 : float *spec_l[], /* i/o: spectrum left channel [NB_DIV][N] */
213 : float *spec_r[], /* i/o: spectrum right channel [NB_DIV][N] */
214 : const int16_t mdct_stereo_mode[], /* i : stereo mode (FB/band wise MS, dual mono */
215 : const int16_t core_l, /* i : core for left channel (TCX20/TCX10) */
216 : const int16_t core_r, /* i : core for right channel (TCX20/TCX10) */
217 : const int16_t igf, /* i : flag for IGF activity */
218 : const int16_t L_frameTCX_l, /* i : TCX frame length of left channel */
219 : const int16_t L_frameTCX_r, /* i : TCX frame length of right channel */
220 : const int16_t mct_on, /* i : flag mct block (1) or stereo (0) */
221 : const int16_t last_core_l, /* i : last core for left channel */
222 : const int16_t last_core_r, /* i : last core for right channel */
223 : const int16_t tmp_plc_upmix /* i : indicates temp upmix for PLC decision */
224 : )
225 : {
226 : int16_t i, k, sfb, nSubframes;
227 8790386 : STEREO_MDCT_BAND_PARAMETERS *sfbConf = NULL;
228 : float nrgRatio;
229 :
230 8790386 : nSubframes = 2;
231 8790386 : if ( ( core_l <= TCX_20_CORE && core_r <= TCX_20_CORE ) || tmp_plc_upmix )
232 : {
233 8554731 : nSubframes = 1;
234 : }
235 :
236 17816427 : for ( k = 0; k < nSubframes; k++ )
237 : {
238 9026041 : sfbConf = ( core_l == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
239 :
240 9026041 : if ( last_core_l == ACELP_CORE || last_core_r == ACELP_CORE )
241 : {
242 32113 : sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
243 : }
244 :
245 9026041 : if ( mdct_stereo_mode[k] == SMDCT_MS_FULL )
246 : {
247 3051866297 : for ( i = 0; i < sfbConf->sfbOffset[sfbConf->nBandsStereoCore]; i++ )
248 : {
249 3045766032 : if ( spec_r_0[k][i] == 0.0f )
250 : {
251 997784597 : spec_r[k][i] *= NF_RED_FAC;
252 : }
253 : }
254 6100265 : inverseMS( sfbConf->sfbOffset[sfbConf->nBandsStereoCore], spec_l[k], spec_r[k], SQRT2_OVER_2 );
255 : }
256 2925776 : else if ( mdct_stereo_mode[k] == SMDCT_BW_MS )
257 : {
258 108577169 : for ( sfb = 0; sfb < sfbConf->nBandsStereoCore; sfb++ )
259 : {
260 105979946 : if ( ms_mask[k][sfb] )
261 : {
262 911931354 : for ( i = sfbConf->sfbOffset[sfb]; i < sfbConf->sfbOffset[sfb + 1]; i++ )
263 : {
264 846030800 : if ( spec_r_0[k][i] == 0.0f )
265 : {
266 257871017 : spec_r[k][i] *= NF_RED_FAC;
267 : }
268 : }
269 65900554 : inverseBwMS( sfbConf->sfbOffset[sfb], sfbConf->sfbOffset[sfb + 1], spec_l[k], spec_r[k], SQRT2_OVER_2 );
270 : }
271 : }
272 : }
273 : #ifdef DEBUGGING
274 : else if ( mdct_stereo_mode[k] != SMDCT_DUAL_MONO )
275 : {
276 : assert( !"Not supported MDCT stereo mode!\n" );
277 : }
278 : #endif
279 :
280 9026041 : if ( igf )
281 : {
282 6075219 : if ( hStereoMdct->IGFStereoMode[k] == SMDCT_MS_FULL )
283 : {
284 877677282 : for ( i = sfbConf->sfbOffset[sfbConf->nBandsStereoCore]; i < sfbConf->sfbOffset[sfbConf->sfbCnt]; i++ )
285 : {
286 874890950 : if ( spec_r_0[k][i] == 0.0f )
287 : {
288 244982246 : spec_r[k][i] *= NF_RED_FAC;
289 : }
290 : }
291 2786332 : inverseMS( sfbConf->sfbOffset[sfbConf->sfbCnt] - sfbConf->sfbOffset[sfbConf->nBandsStereoCore], &spec_l[k][sfbConf->sfbOffset[sfbConf->nBandsStereoCore]], &spec_r[k][sfbConf->sfbOffset[sfbConf->nBandsStereoCore]], SQRT2_OVER_2 );
292 : }
293 3288887 : else if ( hStereoMdct->IGFStereoMode[k] == SMDCT_BW_MS )
294 : {
295 8285920 : for ( sfb = sfbConf->nBandsStereoCore; sfb < sfbConf->sfbCnt; sfb++ )
296 : {
297 7061383 : if ( ms_mask[k][sfb] )
298 : {
299 179095024 : for ( i = sfbConf->sfbOffset[sfb]; i < sfbConf->sfbOffset[sfb + 1]; i++ )
300 : {
301 175365968 : if ( spec_r_0[k][i] == 0.0f )
302 : {
303 47907948 : spec_r[k][i] *= NF_RED_FAC;
304 : }
305 : }
306 3729056 : inverseBwMS( sfbConf->sfbOffset[sfb], sfbConf->sfbOffset[sfb + 1], spec_l[k], spec_r[k], SQRT2_OVER_2 );
307 : }
308 : }
309 : }
310 : #ifdef DEBUGGING
311 : else if ( hStereoMdct->IGFStereoMode[k] != SMDCT_DUAL_MONO )
312 : {
313 : assert( !"Not supported MDCT stereo mode!\n" );
314 : }
315 : #endif
316 : }
317 :
318 9026041 : if ( !mct_on )
319 : {
320 3081548 : nrgRatio = (float) SMDCT_ILD_RANGE / hStereoMdct->global_ild[k] - 1; /* nrgRatio = nrg[1]/nrg[0] */
321 3081548 : hStereoMdct->smooth_ratio = 0.8f * hStereoMdct->smooth_ratio + 0.2f * nrgRatio;
322 : /* set flag to reverse dmx computation in case of right-side panning, only relevant for mono output */
323 3081548 : if ( hStereoMdct->smooth_ratio > 1.3f )
324 : {
325 653500 : hStereoMdct->reverse_dmx = 1;
326 : }
327 2428048 : else if ( hStereoMdct->smooth_ratio < 0.9f )
328 : {
329 650191 : hStereoMdct->reverse_dmx = 0;
330 : }
331 :
332 3081548 : if ( ( nrgRatio > 1.0f ) && ( k < ( ( core_r == TCX_10_CORE ) ? NB_DIV : 1 ) ) )
333 : {
334 1003897 : v_multc( spec_r[k], nrgRatio, spec_r[k], L_frameTCX_r );
335 : }
336 2077651 : else if ( ( nrgRatio < 1.0f ) && ( k < ( ( core_l == TCX_10_CORE ) ? NB_DIV : 1 ) ) )
337 : {
338 733220 : v_multc( spec_l[k], 1.0f / nrgRatio, spec_l[k], L_frameTCX_l );
339 : }
340 : }
341 : } /* for k */
342 :
343 8790386 : return;
344 : }
345 :
346 :
347 : /*-------------------------------------------------------------------*
348 : * inverseBwMS()
349 : *
350 : * Band-wise M/S stereo processing
351 : *-------------------------------------------------------------------*/
352 :
353 80639461 : static void inverseBwMS(
354 : const int16_t startLine, /* i : start line of sfb */
355 : const int16_t stopLine, /* i : stop line of sfb */
356 : float x0[], /* i/o: mid/left channel coefficients */
357 : float x1[], /* i/o: side/right channel coefficients */
358 : const float norm_fac /* i : normalization factor */
359 : )
360 : {
361 : int16_t j;
362 : float tmpValue;
363 :
364 5056698751 : for ( j = startLine; j < stopLine; j++ )
365 : {
366 4976059290 : tmpValue = x0[j];
367 4976059290 : x0[j] = ( x0[j] + x1[j] ) * norm_fac;
368 4976059290 : x1[j] = ( tmpValue - x1[j] ) * norm_fac;
369 : }
370 :
371 80639461 : return;
372 : }
373 :
374 :
375 : /*-------------------------------------------------------------------*
376 : * inverseMS()
377 : *
378 : * M/S stereo processing
379 : *-------------------------------------------------------------------*/
380 :
381 11009851 : void inverseMS(
382 : const int16_t L_frame, /* i : frame length */
383 : float x0[], /* i/o: mid/left channel coefficients */
384 : float x1[], /* i/o: side/right channel coefficients */
385 : const float norm_fac /* i : normalization factor */
386 : )
387 : {
388 11009851 : inverseBwMS( 0, L_frame, x0, x1, norm_fac );
389 :
390 11009851 : return;
391 : }
392 :
393 :
394 : /*-------------------------------------------------------------------*
395 : * initMdctStereoDecData()
396 : *
397 : * Initialize MDCT stereo decoder configuration
398 : *-------------------------------------------------------------------*/
399 :
400 18487456 : void initMdctStereoDecData(
401 : STEREO_MDCT_DEC_DATA *hStereoMdct, /* i/o: mdct stereo parameters structure */
402 : const int16_t igf, /* i : flag indicating IGF activity */
403 : const H_IGF_GRID igfGrid, /* i : IGF grid configuration */
404 : const int32_t element_brate, /* i : element bitrate */
405 : const int16_t bwidth /* i : audio bandwidth */
406 : )
407 : {
408 : int16_t tcx_coded_lines;
409 :
410 18487456 : tcx_coded_lines = getNumTcxCodedLines( bwidth );
411 :
412 : /*Initialize sfb parameteres for TCX20 */
413 18487456 : stereo_mdct_init_bands( tcx_coded_lines, TCX_20_CORE, element_brate, igf, &igfGrid[IGF_GRID_LB_NORM], &hStereoMdct->stbParamsTCX20.sfbOffset[0], &hStereoMdct->stbParamsTCX20.sfbCnt );
414 :
415 : /*Initialize sfb parameteres for TCX10 */
416 18487456 : stereo_mdct_init_bands( tcx_coded_lines, TCX_10_CORE, element_brate, igf, &igfGrid[IGF_GRID_LB_SHORT], &hStereoMdct->stbParamsTCX10.sfbOffset[0], &hStereoMdct->stbParamsTCX10.sfbCnt );
417 :
418 : /*Initialize sfb parameteres for transition frames */
419 18487456 : stereo_mdct_init_bands( tcx_coded_lines, -1, element_brate, igf, &igfGrid[IGF_GRID_LB_TRAN], &hStereoMdct->stbParamsTCX20afterACELP.sfbOffset[0], &hStereoMdct->stbParamsTCX20afterACELP.sfbCnt );
420 :
421 18487456 : if ( igf )
422 : {
423 : /* calculate the igf start band from the igf start line */
424 9998209 : stereo_mdct_init_igf_start_band( &( hStereoMdct->stbParamsTCX20 ), 1.0f, bwidth, element_brate );
425 9998209 : stereo_mdct_init_igf_start_band( &( hStereoMdct->stbParamsTCX10 ), 0.5f, bwidth, element_brate );
426 9998209 : stereo_mdct_init_igf_start_band( &( hStereoMdct->stbParamsTCX20afterACELP ), 1.25f, bwidth, element_brate );
427 : }
428 : else
429 : {
430 8489247 : hStereoMdct->stbParamsTCX20.sfbIgfStart = -1;
431 8489247 : hStereoMdct->stbParamsTCX10.sfbIgfStart = -1;
432 8489247 : hStereoMdct->stbParamsTCX20afterACELP.sfbIgfStart = -1;
433 8489247 : hStereoMdct->stbParamsTCX10.nBandsStereoCore = hStereoMdct->stbParamsTCX10.sfbCnt;
434 8489247 : hStereoMdct->stbParamsTCX20.nBandsStereoCore = hStereoMdct->stbParamsTCX20.sfbCnt;
435 8489247 : hStereoMdct->stbParamsTCX20afterACELP.nBandsStereoCore = hStereoMdct->stbParamsTCX20afterACELP.sfbCnt;
436 : }
437 :
438 18487456 : return;
439 : }
440 :
441 :
442 : /*-------------------------------------------------------------------*
443 : * initMdctStereoDtxData()
444 : *
445 : * Allocate and initialize structures for MDCT-Stereo DTX operation
446 : *-------------------------------------------------------------------*/
447 :
448 359 : ivas_error initMdctStereoDtxData(
449 : CPE_DEC_HANDLE hCPE /* i/o: CPE handle */
450 : )
451 : {
452 : int16_t ch;
453 : ivas_error error;
454 :
455 359 : error = IVAS_ERR_OK;
456 :
457 1077 : for ( ch = 0; ch < CPE_CHANNELS; ch++ )
458 : {
459 718 : DEC_CORE_HANDLE st = hCPE->hCoreCoder[ch];
460 :
461 718 : if ( st->hFdCngDec == NULL )
462 : {
463 : /* Create FD_CNG instance */
464 0 : if ( ( error = createFdCngDec( &st->hFdCngDec ) ) != IVAS_ERR_OK )
465 : {
466 0 : return error;
467 : }
468 :
469 : /* Init FD-CNG */
470 0 : initFdCngDec( st );
471 : }
472 :
473 718 : if ( st->first_CNG == 0 )
474 : {
475 676 : if ( ch == 1 && st->cng_sba_flag )
476 : {
477 214 : st->hFdCngDec->hFdCngCom->seed += 3;
478 : }
479 : }
480 :
481 718 : if ( st->cldfbAna == NULL )
482 : {
483 : /* open analysis for max. sampling rate 48kHz */
484 500 : if ( ( error = openCldfb( &st->cldfbAna, CLDFB_ANALYSIS, 48000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
485 : {
486 0 : return error;
487 : }
488 : }
489 :
490 718 : if ( st->cldfbBPF == NULL )
491 : {
492 : /* open analysis BPF for max. internal sampling rate 16kHz */
493 500 : if ( ( error = openCldfb( &st->cldfbBPF, CLDFB_ANALYSIS, 16000, CLDFB_PROTOTYPE_1_25MS ) ) != IVAS_ERR_OK )
494 : {
495 0 : return error;
496 : }
497 : }
498 : }
499 :
500 359 : return error;
501 : }
502 :
503 : /*-------------------------------------------------------------------*
504 : * synchonize_channels_mdct_sid()
505 : *
506 : * Synchronize channels in SID frame in MDCT stereo
507 : *-------------------------------------------------------------------*/
508 :
509 58154704 : void synchonize_channels_mdct_sid(
510 : Decoder_State *sts[CPE_CHANNELS], /* i/o: decoder state structure */
511 : const int16_t n /* i : channel number */
512 : )
513 : {
514 : Decoder_State *st;
515 :
516 58154704 : st = sts[n];
517 :
518 58154704 : if ( st->element_mode == IVAS_CPE_MDCT && st->total_brate == SID_2k40 )
519 : {
520 13020 : if ( n == 1 )
521 : {
522 : /* synchronize channels */
523 6510 : sts[1]->L_frame = sts[0]->L_frame;
524 6510 : sts[1]->cng_type = sts[0]->cng_type;
525 6510 : sts[1]->bwidth = sts[0]->bwidth;
526 : #ifdef NONBE_MDCT_ST_DTX_FIX_SUBOPT_SPATIAL_CNG
527 6510 : sts[0]->hFdCngDec->hFdCngCom->coherence[0] = sts[1]->hFdCngDec->hFdCngCom->coherence[0]; /* coherence is stored in sts[1] - see ivas_decision_matrix_dec() and FdCngDecodeMDCTStereoSID() */
528 : #else
529 : sts[0]->hFdCngDec->hFdCngCom->coherence = sts[1]->hFdCngDec->hFdCngCom->coherence; /* coherence is stored in sts[1] - see ivas_decision_matrix_dec() */
530 : #endif
531 6510 : sts[0]->hFdCngDec->hFdCngCom->no_side_flag = sts[1]->hFdCngDec->hFdCngCom->no_side_flag;
532 :
533 : /* configure when there is a switching from DFT CNG to MDCT CNG */
534 6510 : if ( sts[0]->first_CNG == 1 && sts[1]->first_CNG == 0 )
535 : {
536 0 : configureFdCngDec( st->hFdCngDec, st->bwidth, st->element_brate, st->L_frame, st->last_L_frame, st->element_mode );
537 : }
538 : }
539 :
540 13020 : if ( sts[0]->first_CNG == 0 )
541 : {
542 : /* configure CNG after reading first side info from SID to get correct values for L_frame and bwidth if first SID is also first valid frame */
543 634 : configureFdCngDec( st->hFdCngDec, st->bwidth, st->element_brate, st->L_frame, st->last_L_frame, st->element_mode );
544 : }
545 : }
546 :
547 58154704 : return;
548 : }
549 :
550 :
551 : /*-------------------------------------------------------------------*
552 : * updateBuffersForDmxMdctStereo()
553 : *
554 : * synch buffers between channels for mono output and
555 : * apply passive downmix to certain buffers to enable smooth transitions
556 : * between active/inactive coding in MDCT-Stereo DTX
557 : *-------------------------------------------------------------------*/
558 :
559 13830 : void updateBuffersForDmxMdctStereo(
560 : CPE_DEC_HANDLE hCPE, /* i/o: CPE handle */
561 : const int16_t output_frame, /* i : output frame length */
562 : float *output[CPE_CHANNELS], /* i/o: decoder output */
563 : float synth[CPE_CHANNELS][L_FRAME48k] /* i/o: decoder synthesis */
564 : )
565 : {
566 : int16_t delay_buf_out_len, tcxltp_mem_in_len, delta, i;
567 : Decoder_State *sts[CPE_CHANNELS];
568 :
569 13830 : sts[0] = hCPE->hCoreCoder[0];
570 13830 : sts[1] = hCPE->hCoreCoder[1];
571 :
572 : /* synch buffers for inactive frames, but not for transition frames */
573 13830 : if ( hCPE->last_element_brate <= IVAS_SID_5k2 )
574 : {
575 13309 : mvr2r( output[0], output[1], output_frame );
576 13309 : mvr2r( synth[0], synth[1], output_frame );
577 : }
578 :
579 13830 : if ( hCPE->element_brate == IVAS_SID_5k2 && hCPE->last_element_brate > IVAS_SID_5k2 )
580 : {
581 : /* in the first SID frame after an active frame, create mid noise shape here, in SID frames that follow inactive frames, it is done directly in the SID decoding since the mid shape is being used in CNG then */
582 12602 : for ( int16_t p = 0; p < sts[0]->hFdCngDec->hFdCngCom->npart; p++ )
583 : {
584 12081 : sts[0]->hFdCngDec->hFdCngCom->sidNoiseEst[p] = 0.5f * ( sts[0]->hFdCngDec->hFdCngCom->sidNoiseEst[p] + sts[1]->hFdCngDec->hFdCngCom->sidNoiseEst[p] );
585 : }
586 : }
587 :
588 : /* for transition of active->inactive frame, apply passive downmix on buffers */
589 13830 : if ( hCPE->last_element_brate <= IVAS_SID_5k2 )
590 : {
591 13309 : delta = 1;
592 13309 : if ( output_frame == L_FRAME16k )
593 : {
594 4050 : delta = 2;
595 : }
596 9259 : else if ( output_frame == L_FRAME32k )
597 : {
598 4201 : delta = 4;
599 : }
600 5058 : else if ( output_frame == L_FRAME48k )
601 : {
602 5058 : delta = 6;
603 : }
604 :
605 13309 : delay_buf_out_len = delta * HQ_DELAY_COMP;
606 13309 : tcxltp_mem_in_len = NS2SA( sts[0]->output_Fs, TCXLTP_DELAY_NS );
607 :
608 13309 : assert( delay_buf_out_len > tcxltp_mem_in_len );
609 :
610 123813 : for ( i = 0; i < tcxltp_mem_in_len; i++ )
611 : {
612 110504 : sts[0]->hTcxLtpDec->tcxltp_mem_in[i] = INV_SQRT2 * ( sts[0]->hTcxLtpDec->tcxltp_mem_in[i] + sts[1]->hTcxLtpDec->tcxltp_mem_in[i] );
613 110504 : sts[0]->delay_buf_out[i] = INV_SQRT2 * ( sts[0]->delay_buf_out[i] + sts[1]->delay_buf_out[i] );
614 110504 : sts[0]->hHQ_core->old_out[i] = INV_SQRT2 * ( sts[0]->hHQ_core->old_out[i] + sts[1]->hHQ_core->old_out[i] );
615 110504 : sts[0]->hTcxLtpDec->tcxltp_mem_out[i] = INV_SQRT2 * ( sts[0]->hTcxLtpDec->tcxltp_mem_out[i] + sts[1]->hTcxLtpDec->tcxltp_mem_out[i] );
616 : }
617 455325 : for ( ; i < delay_buf_out_len; i++ )
618 : {
619 442016 : sts[0]->delay_buf_out[i] = INV_SQRT2 * ( sts[0]->delay_buf_out[i] + sts[1]->delay_buf_out[i] );
620 442016 : sts[0]->hHQ_core->old_out[i] = INV_SQRT2 * ( sts[0]->hHQ_core->old_out[i] + sts[1]->hHQ_core->old_out[i] );
621 442016 : sts[0]->hTcxLtpDec->tcxltp_mem_out[i] = INV_SQRT2 * ( sts[0]->hTcxLtpDec->tcxltp_mem_out[i] + sts[1]->hTcxLtpDec->tcxltp_mem_out[i] );
622 : }
623 8301109 : for ( ; i < output_frame; i++ )
624 : {
625 8287800 : sts[0]->hHQ_core->old_out[i] = INV_SQRT2 * ( sts[0]->hHQ_core->old_out[i] + sts[1]->hHQ_core->old_out[i] );
626 8287800 : sts[0]->hTcxLtpDec->tcxltp_mem_out[i] = INV_SQRT2 * ( sts[0]->hTcxLtpDec->tcxltp_mem_out[i] + sts[1]->hTcxLtpDec->tcxltp_mem_out[i] );
627 : }
628 : }
629 :
630 13830 : return;
631 : }
632 :
633 :
634 : /*-------------------------------------------------------------------*
635 : * applyDmxMdctStereo()
636 : *
637 : * apply passive downmix to certain buffers to enable smooth transitions
638 : * between active/inactive coding in MDCT-Stereo DTX
639 : *-------------------------------------------------------------------*/
640 :
641 240850 : void applyDmxMdctStereo(
642 : const CPE_DEC_HANDLE hCPE, /* i : CPE handle */
643 : float *output[CPE_CHANNELS], /* i/o: core decoder output */
644 : const int16_t output_frame /* i : output frame length */
645 : )
646 : {
647 : int16_t crossfade_len, i;
648 : int16_t dmx_len;
649 : float fade, step;
650 :
651 240850 : step = 1.f;
652 240850 : fade = 1.f;
653 240850 : dmx_len = output_frame;
654 :
655 240850 : if ( hCPE->last_element_brate <= IVAS_SID_5k2 )
656 : {
657 502 : crossfade_len = NS2SA( hCPE->hCoreCoder[0]->output_Fs, IVAS_DEC_DELAY_NS - DELAY_CLDFB_NS );
658 502 : step /= crossfade_len;
659 : }
660 : /* for first inactive CNG frame after active decoding we have to do a fade-OUT FROM the passive DMX */
661 240348 : else if ( hCPE->element_brate <= IVAS_SID_5k2 && hCPE->last_element_brate > IVAS_SID_5k2 )
662 : {
663 521 : crossfade_len = output_frame / 4;
664 521 : step /= -crossfade_len;
665 521 : fade = 0.f;
666 521 : dmx_len = crossfade_len;
667 : }
668 239827 : else if ( hCPE->last_element_mode == IVAS_CPE_DFT && hCPE->last_element_brate <= IVAS_32k )
669 : {
670 585 : crossfade_len = NS2SA( hCPE->hCoreCoder[0]->output_Fs, DELAY_CLDFB_NS );
671 585 : step /= crossfade_len;
672 : }
673 : else
674 : {
675 239242 : crossfade_len = 0;
676 : }
677 :
678 : /* apply crossfade */
679 400670 : for ( i = 0; i < crossfade_len; i++ )
680 : {
681 159820 : output[0][i] = output[0][i] * fade + ( output[0][i] + output[1][i] ) * INV_SQRT2 * ( 1 - fade );
682 159820 : fade -= step;
683 : }
684 :
685 : /* apply passive downmix on all-active-frame part */
686 181220710 : for ( ; i < dmx_len; i++ )
687 : {
688 180979860 : output[0][i] = ( output[0][i] + output[1][i] ) * INV_SQRT_2;
689 : }
690 :
691 240850 : return;
692 : }
|