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 "cnst.h"
38 : #include "ivas_cnst.h"
39 : #include "prot.h"
40 : #include "ivas_prot.h"
41 : #include "wmc_auto.h"
42 : #include "rom_com.h"
43 : #include "ivas_rom_com.h"
44 : #ifdef DEBUGGING
45 : #include "debug.h"
46 : #endif
47 :
48 : /*-------------------------------------------------------------------*
49 : * ic_bwe_dec_reset()
50 : *
51 : * core switching reset of IC BWE memory
52 : *-------------------------------------------------------------------*/
53 :
54 210148 : static void ic_bwe_dec_reset(
55 : STEREO_ICBWE_DEC_HANDLE hStereoICBWE /* i/o: Stereo ICBWE handle */
56 : )
57 : {
58 : /* unscaled & scaled SHB synthesis memory */
59 210148 : set_f( hStereoICBWE->mem_syn_shb_nonref, 0, L_SHB_LAHEAD ); /* use samples from !acelp) */
60 210148 : set_f( hStereoICBWE->mem_lpc_shbsynth_nonref, 0, LPC_SHB_ORDER );
61 210148 : set_f( hStereoICBWE->mem_syn_shb_ola_nonref, 0, L_SHB_LAHEAD ); /* use samples from !acelp) */
62 :
63 : /* inter-channel BWE SP and GSP mem reset */
64 210148 : hStereoICBWE->memShbSpecMapping = 0;
65 :
66 210148 : set_f( hStereoICBWE->memShbHilbert_nonref, 0, HILBERT_MEM_SIZE );
67 210148 : set_f( hStereoICBWE->memShbInterp_nonref, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
68 210148 : set_f( hStereoICBWE->memShb_fsout_nonref, 0, INTERP_3_2_MEM_LEN );
69 210148 : hStereoICBWE->syn_dm_phase_nonref = 0;
70 :
71 210148 : return;
72 : }
73 :
74 :
75 : /*-------------------------------------------------------------------*
76 : * stereo_icBWE_dec()
77 : *
78 : * Spatial mapping of reference to the non-reference channels in SHB
79 : *-------------------------------------------------------------------*/
80 :
81 4279775 : void stereo_icBWE_dec(
82 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
83 : float *synthRef, /* i/o: Reference channel HB synthesis at output Fs */
84 : float *synth, /* o : Non reference channel HB synthesis at output Fs */
85 : const float *fb_synth_ref, /* i : ref. high-band synthesis 16-20 kHz */
86 : const float *voice_factors, /* i : voicing factors */
87 : const int16_t output_frame /* i : frame length */
88 : )
89 : {
90 : int16_t i, j, k, nbSubFr;
91 : Decoder_State *st; /* i/o: decoder state structure, primary channel */
92 : int16_t spIndx, gsIndx;
93 : float excSHB_nonref[L_FRAME16k];
94 : float shb_synth_nonref[L_FRAME16k + L_SHB_LAHEAD];
95 : float error[L_FRAME32k];
96 : float nlMixFac[NB_SUBFR16k];
97 : float gsMapping, specMapping;
98 : float fb_synth_nonref[L_FRAME48k];
99 : float scale, prev_pow, curr_pow, temp;
100 : float alpha, winSlope, winLen, prevgsMapping;
101 : float temp1, temp2;
102 : float icbweM2Ref, ratio_L;
103 :
104 4279775 : STEREO_DFT_DEC_DATA_HANDLE hStereoDft = hCPE->hStereoDft;
105 4279775 : STEREO_ICBWE_DEC_HANDLE hStereoICBWE = hCPE->hStereoICBWE;
106 4279775 : st = hCPE->hCoreCoder[0];
107 :
108 : /*--------------------------------------------------------------------*
109 : * skip IC-BWE in case of mono DMX output *
110 : * -------------------------------------------------------------------*/
111 :
112 4279775 : if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 && hCPE->hStereoDft->hConfig->res_cod_mode > STEREO_DFT_RES_COD_OFF )
113 : {
114 84382 : hCPE->hStereoDft->core_hist[0] = st->core;
115 :
116 84382 : return;
117 : }
118 4195393 : else if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 )
119 : {
120 :
121 420586 : return;
122 : }
123 :
124 : /*--------------------------------------------------------------------*
125 : * skip IC-BWE in case of SID or NO_DATA frame
126 : * -------------------------------------------------------------------*/
127 :
128 3774807 : if ( st->core_brate <= SID_2k40 )
129 : {
130 56097 : return;
131 : }
132 :
133 : /*--------------------------------------------------------------------*
134 : * TD high band stereo filling *
135 : * -------------------------------------------------------------------*/
136 :
137 : /* update buffers for TD stereo filling */
138 3718710 : if ( hCPE->element_mode == IVAS_CPE_DFT )
139 : {
140 651549 : float hb_nrg = EPSILON;
141 651549 : float hb_nrg2 = EPSILON;
142 :
143 651549 : if ( st->core == ACELP_CORE || st->last_core == ACELP_CORE )
144 : {
145 108288728 : for ( i = 0; i < output_frame / 2; i++ )
146 : {
147 107958560 : hb_nrg2 += synthRef[i] * synthRef[i];
148 : }
149 :
150 330168 : hCPE->hStereoDft->hb_nrg_subr[0] = hb_nrg2;
151 330168 : hb_nrg += hb_nrg2;
152 330168 : hb_nrg2 = EPSILON;
153 :
154 108288728 : for ( ; i < output_frame; i++ )
155 : {
156 107958560 : hb_nrg2 += synthRef[i] * synthRef[i];
157 : }
158 :
159 330168 : hCPE->hStereoDft->hb_nrg_subr[1] = hb_nrg2;
160 330168 : hb_nrg += hb_nrg2;
161 :
162 330168 : mvr2r( synthRef, hCPE->hStereoDft->hb_stefi_sig + hCPE->hStereoDft->hb_stefi_delay, output_frame );
163 : }
164 : else
165 : {
166 321381 : set_zero( hCPE->hStereoDft->hb_stefi_sig + hCPE->hStereoDft->hb_stefi_delay, output_frame );
167 : }
168 651549 : hCPE->hStereoDft->hb_nrg_subr[0] *= hCPE->hStereoDft->NFFT / 2;
169 651549 : hCPE->hStereoDft->hb_nrg_subr[1] *= hCPE->hStereoDft->NFFT / 2;
170 651549 : hCPE->hStereoDft->hb_nrg[0] = hb_nrg;
171 651549 : hCPE->hStereoDft->td_gain[0] = 0;
172 651549 : hCPE->hStereoDft->core_hist[0] = st->core;
173 : #ifdef DEBUG_MODE_DFT
174 : {
175 : int16_t tmp[L_FRAME48k];
176 : for ( i = 0; i < output_frame; i++ )
177 : {
178 : tmp[i] = (int16_t) synthRef[i];
179 : }
180 : dbgwrite( tmp, sizeof( int16_t ), output_frame, 1, "res/hb_synth.pcm" );
181 : for ( i = 0; i < output_frame; i++ )
182 : {
183 : tmp[i] = (int16_t) hCPE->hStereoDft->hb_stefi_sig[i + output_frame / 2];
184 : }
185 : dbgwrite( tmp, sizeof( int16_t ), output_frame, 1, "res/hb_stefi_sig.pcm" );
186 : }
187 : #endif /* DEBUG_MODE_DFT */
188 : }
189 :
190 : /*--------------------------------------------------------------------*
191 : * IC-BWE *
192 : * -------------------------------------------------------------------*/
193 :
194 3718710 : if ( st->core != ACELP_CORE || st->extl == -1 || ( hCPE->element_mode == IVAS_CPE_TD && hCPE->hCoreCoder[0]->tdm_LRTD_flag ) )
195 : {
196 3446002 : return;
197 : }
198 272708 : else if ( hCPE->element_mode == IVAS_CPE_DFT && st->core_brate <= SID_2k40 )
199 : {
200 0 : mvr2r( synthRef, synth, output_frame );
201 0 : return;
202 : }
203 :
204 :
205 272708 : set_f( fb_synth_nonref, 0, L_FRAME48k );
206 :
207 : /* core switching reset */
208 272708 : if ( st->last_core != ACELP_CORE || st->bwidth == WB )
209 : {
210 52390 : ic_bwe_dec_reset( hStereoICBWE );
211 :
212 52390 : if ( st->last_core != ACELP_CORE )
213 : {
214 13183 : hStereoICBWE->prevSpecMapping = 0.0f;
215 13183 : hStereoICBWE->prevgsMapping = 1.0f;
216 13183 : hStereoICBWE->icbweM2Ref_prev = 1.0f;
217 : }
218 :
219 52390 : if ( st->bwidth == WB )
220 : {
221 : /* copy to outputHB and reset hb_synth values */
222 40268 : mvr2r( synthRef, synth, output_frame );
223 :
224 40268 : if ( st->element_mode == IVAS_CPE_TD )
225 : {
226 15 : hStereoICBWE->prevSpecMapping = 0.0f;
227 15 : hStereoICBWE->prevgsMapping = 1.0f;
228 15 : hStereoICBWE->icbweM2Ref_prev = 1.0f;
229 : }
230 40253 : else if ( st->element_mode == IVAS_CPE_DFT )
231 : {
232 40253 : hStereoICBWE->refChanIndx_bwe = L_CH_INDX;
233 40253 : hStereoICBWE->prevSpecMapping = 0.0f;
234 :
235 40253 : prevgsMapping = hStereoICBWE->prevgsMapping;
236 40253 : temp1 = hStereoDft->side_gain[2 * STEREO_DFT_BAND_MAX + hStereoDft->nbands - 1];
237 40253 : icbweM2Ref = 1.f + temp1;
238 40253 : gsMapping = 1.f - temp1;
239 :
240 40253 : winLen = (int16_t) ( ( SHB_OVERLAP_LEN * st->output_Fs ) / 16000 );
241 40253 : winSlope = 1.0f / winLen;
242 40253 : alpha = winSlope;
243 929333 : for ( i = 0; i < winLen; i++ )
244 : {
245 889080 : synthRef[i] *= ( alpha * ( icbweM2Ref ) + ( 1.0f - alpha ) * ( hStereoICBWE->icbweM2Ref_prev ) );
246 889080 : synth[i] *= ( alpha * ( gsMapping ) + ( 1.0f - alpha ) * ( prevgsMapping ) );
247 889080 : alpha += winSlope;
248 : }
249 13376453 : for ( ; i < NS2SA( st->output_Fs, FRAME_SIZE_NS ); i++ )
250 : {
251 13336200 : synthRef[i] *= ( icbweM2Ref );
252 13336200 : synth[i] *= ( gsMapping );
253 : }
254 40253 : hStereoICBWE->icbweM2Ref_prev = icbweM2Ref;
255 40253 : hStereoICBWE->prevgsMapping = gsMapping;
256 : }
257 :
258 40268 : return;
259 : }
260 : }
261 :
262 232440 : if ( !st->bfi )
263 : {
264 225021 : hStereoICBWE->refChanIndx_bwe = get_next_indice( st, STEREO_ICBWE_REFBITS );
265 225021 : if ( st->flag_ACELP16k == 1 )
266 : {
267 142273 : spIndx = get_next_indice( st, STEREO_ICBWE_SPBITS );
268 : }
269 : else
270 : {
271 82748 : spIndx = 3;
272 : }
273 225021 : if ( st->element_mode == IVAS_CPE_TD )
274 : {
275 1928 : gsIndx = get_next_indice( st, STEREO_ICBWE_GSBITS );
276 : }
277 : else
278 : {
279 223093 : gsIndx = get_next_indice( st, STEREO_ICBWE_GSBITS_DFT );
280 : }
281 :
282 : /* Store indices in case of frame loss */
283 225021 : hStereoICBWE->prev_spIndx = spIndx;
284 225021 : hStereoICBWE->prev_gsIndx = gsIndx;
285 : }
286 : else /*bfi*/
287 : {
288 : /* Retrieve last decoded indices */
289 7419 : spIndx = hStereoICBWE->prev_spIndx;
290 7419 : gsIndx = hStereoICBWE->prev_gsIndx;
291 7419 : hStereoICBWE->refChanIndx_bwe = hStereoICBWE->prev_refChanIndx_bwe;
292 : }
293 :
294 : /* IC-BWE parameter de-quant */
295 : /* sp Mapping */
296 232440 : hStereoICBWE->prevSpecMapping = usdequant( spIndx, -0.6f, 0.2f );
297 :
298 : /* gs Mapping */
299 232440 : prevgsMapping = hStereoICBWE->prevgsMapping;
300 :
301 232440 : if ( st->element_mode == IVAS_CPE_TD )
302 : {
303 1949 : hStereoICBWE->prevgsMapping = icbwe_gsMapping_tbl[gsIndx];
304 : }
305 : else
306 : {
307 230491 : hStereoICBWE->prevgsMapping = icbwe_gsMappingDFT_tbl[gsIndx];
308 : }
309 :
310 232440 : hStereoICBWE->prevgsMapping = powf( 10, hStereoICBWE->prevgsMapping );
311 :
312 232440 : specMapping = hStereoICBWE->prevSpecMapping;
313 232440 : gsMapping = hStereoICBWE->prevgsMapping;
314 :
315 232440 : if ( ( st->extl == SWB_TBE || st->extl == FB_TBE ) && st->flag_ACELP16k == 1 )
316 : {
317 131754 : mvr2r( voice_factors, nlMixFac, NB_SUBFR16k );
318 131754 : if ( hCPE->hStereoDftDmx != NULL )
319 : {
320 41 : if ( hCPE->hStereoDftDmx->targetGain < 0.5f || hCPE->hStereoDftDmx->targetGain > 2.0f )
321 : {
322 0 : v_multc( voice_factors, 0.5f, nlMixFac, NB_SUBFR16k );
323 : }
324 : }
325 : else
326 : {
327 131713 : if ( hCPE->hStereoTCA->targetGain < 0.5f || hCPE->hStereoTCA->targetGain > 2.0f )
328 : {
329 5 : v_multc( voice_factors, 0.5f, nlMixFac, NB_SUBFR16k );
330 : }
331 : }
332 :
333 131754 : nbSubFr = ( st->flag_ACELP16k == 0 ) ? NB_SUBFR : NB_SUBFR16k;
334 790524 : for ( i = 0, k = 0; i < nbSubFr; i++ )
335 : {
336 658770 : if ( hCPE->hCoreCoder[0]->coder_type == UNVOICED || hStereoICBWE->MSFlag == 1 )
337 : {
338 148725 : temp1 = 0;
339 148725 : temp2 = 1.0f;
340 : }
341 : else
342 : {
343 510045 : temp1 = sqrtf( nlMixFac[i] );
344 510045 : temp2 = sqrtf( 1.0f - nlMixFac[i] );
345 : }
346 :
347 42820050 : for ( j = 0; j < L_FRAME16k / nbSubFr; j++, k++ )
348 : {
349 42161280 : excSHB_nonref[k] = temp1 * hStereoICBWE->nlExc16k[k] + temp2 * hStereoICBWE->mixExc16k[k];
350 : }
351 : }
352 :
353 : /* LP synthesis */
354 131754 : mvr2r( hStereoICBWE->mem_syn_shb_nonref, shb_synth_nonref, L_SHB_LAHEAD );
355 131754 : syn_filt( hStereoICBWE->lpSHBRef, LPC_SHB_ORDER, excSHB_nonref, shb_synth_nonref + L_SHB_LAHEAD, L_FRAME16k, hStereoICBWE->mem_lpc_shbsynth_nonref, 1 );
356 :
357 131754 : prev_pow = sum2_f( shb_synth_nonref, L_SHB_LAHEAD + 10 );
358 131754 : curr_pow = sum2_f( shb_synth_nonref + L_SHB_LAHEAD + 10, L_SHB_LAHEAD + 10 );
359 :
360 131754 : if ( prev_pow == 0 )
361 : {
362 0 : scale = 0;
363 : }
364 : else
365 : {
366 131754 : scale = sqrtf( curr_pow / prev_pow );
367 : }
368 :
369 2766834 : for ( i = 0; i < L_SHB_LAHEAD; i++ )
370 : {
371 2635080 : shb_synth_nonref[i] *= scale;
372 : }
373 :
374 1449294 : for ( ; i < L_SHB_LAHEAD + 10; i++ )
375 : {
376 1317540 : temp = ( i - 19 ) / 10.0f;
377 1317540 : shb_synth_nonref[i] *= ( temp * 1.0f + ( 1.0f - temp ) * scale );
378 : }
379 :
380 : /* spec and gs adjustment */
381 131754 : deemph( shb_synth_nonref + L_SHB_LAHEAD, specMapping, L_FRAME16k, &( hStereoICBWE->memShbSpecMapping ) );
382 131754 : mvr2r( shb_synth_nonref + L_FRAME16k, hStereoICBWE->mem_syn_shb_nonref, L_SHB_LAHEAD );
383 :
384 131754 : ScaleShapedSHB( SHB_OVERLAP_LEN, shb_synth_nonref, hStereoICBWE->mem_syn_shb_ola_nonref, hStereoICBWE->gshapeRef, ( hStereoICBWE->gFrameRef * gsMapping * 0.9f ), window_shb, subwin_shb );
385 :
386 131754 : if ( st->extl == FB_TBE )
387 : {
388 63564 : v_multc( fb_synth_ref, gsMapping, fb_synth_nonref, L_FRAME48k );
389 : }
390 :
391 : /* generate 32kHz SHB synthesis from 12.8(16)kHz signal */
392 131754 : GenSHBSynth( shb_synth_nonref, error, hStereoICBWE->memShbHilbert_nonref, hStereoICBWE->memShbInterp_nonref, st->L_frame, &( hStereoICBWE->syn_dm_phase_nonref ) );
393 : }
394 : else
395 : {
396 100686 : mvr2r( synthRef, synth, output_frame );
397 :
398 100686 : winLen = (int16_t) ( ( SHB_OVERLAP_LEN * st->output_Fs ) / 16000 );
399 100686 : winSlope = 1.0f / winLen;
400 100686 : alpha = winSlope;
401 :
402 100686 : ratio_L = ( hCPE->element_mode == IVAS_CPE_DFT ) ? ( 0.5f ) : ( tdm_ratio_tabl[hCPE->hStereoTD->tdm_last_ratio_idx] );
403 :
404 100686 : icbweM2Ref = gsMapping;
405 100686 : if ( hStereoICBWE->refChanIndx_bwe == L_CH_INDX )
406 : {
407 45133 : if ( ratio_L >= 0.1f )
408 : {
409 44972 : icbweM2Ref = sqrtf( 0.5f - min( 0.5f, ( 1 - ratio_L ) * ( 1 - ratio_L ) * gsMapping * gsMapping ) ) / ratio_L;
410 : }
411 : }
412 : else
413 : {
414 55553 : if ( ratio_L <= 0.9f )
415 : {
416 55515 : icbweM2Ref = sqrtf( 0.5f - min( 0.5f, ratio_L * ratio_L * gsMapping * gsMapping ) ) / ( 1 - ratio_L );
417 : }
418 : }
419 :
420 100686 : icbweM2Ref = max( gsMapping, icbweM2Ref );
421 :
422 4035086 : for ( i = 0; i < winLen; i++ )
423 : {
424 3934400 : synthRef[i] *= ( alpha * ( icbweM2Ref ) + ( 1.0f - alpha ) * ( hStereoICBWE->icbweM2Ref_prev ) );
425 3934400 : synth[i] *= ( alpha * ( gsMapping ) + ( 1.0f - alpha ) * ( prevgsMapping ) );
426 3934400 : alpha += winSlope;
427 : }
428 59116686 : for ( ; i < NS2SA( st->output_Fs, FRAME_SIZE_NS ); i++ )
429 : {
430 59016000 : synthRef[i] *= ( icbweM2Ref );
431 59016000 : synth[i] *= ( gsMapping );
432 : }
433 100686 : hStereoICBWE->icbweM2Ref_prev = icbweM2Ref;
434 :
435 100686 : ic_bwe_dec_reset( hStereoICBWE );
436 100686 : hStereoICBWE->prevSpecMapping = 0.0f;
437 :
438 100686 : return;
439 : }
440 :
441 : /* resample to output FS */
442 131754 : if ( st->output_Fs == 48000 )
443 : {
444 82091 : interpolate_3_over_2_allpass( error, L_FRAME32k, synth, hStereoICBWE->memShb_fsout_nonref );
445 : }
446 49663 : else if ( st->output_Fs == 32000 )
447 : {
448 49663 : mvr2r( error, synth, L_FRAME32k );
449 : }
450 0 : else if ( st->output_Fs == 16000 )
451 : {
452 0 : Decimate_allpass_steep( error, hStereoICBWE->memShb_fsout_nonref, L_FRAME32k, synth );
453 : }
454 :
455 :
456 131754 : if ( st->extl == FB_TBE && st->output_Fs == 48000 )
457 : {
458 51922 : v_add( fb_synth_nonref, synth, synth, L_FRAME48k );
459 : }
460 :
461 : /* copy to outputHB and reset hb_synth values */
462 131754 : ratio_L = ( hCPE->element_mode == IVAS_CPE_DFT ) ? ( 0.5f ) : ( tdm_ratio_tabl[hCPE->hStereoTD->tdm_last_ratio_idx] );
463 :
464 131754 : icbweM2Ref = gsMapping;
465 131754 : if ( hStereoICBWE->refChanIndx_bwe == L_CH_INDX )
466 : {
467 69739 : if ( ratio_L >= 0.1f )
468 : {
469 69558 : icbweM2Ref = sqrtf( 0.5f - min( 0.5f, ( 1 - ratio_L ) * ( 1 - ratio_L ) * gsMapping * gsMapping ) ) / ratio_L;
470 : }
471 : }
472 : else
473 : {
474 62015 : if ( ratio_L <= 0.9f )
475 : {
476 61988 : icbweM2Ref = sqrtf( 0.5f - min( 0.5f, ratio_L * ratio_L * gsMapping * gsMapping ) ) / ( 1 - ratio_L );
477 : }
478 : }
479 :
480 131754 : icbweM2Ref = max( gsMapping, icbweM2Ref );
481 :
482 131754 : winLen = (int16_t) ( ( SHB_OVERLAP_LEN * st->output_Fs ) / 16000 );
483 131754 : winSlope = 1.0f / winLen;
484 131754 : alpha = winSlope;
485 7043734 : for ( i = 0; i < winLen; i++ )
486 : {
487 6911980 : synthRef[i] *= ( alpha * ( icbweM2Ref ) + ( 1.0f - alpha ) * ( hStereoICBWE->icbweM2Ref_prev ) );
488 6911980 : alpha += winSlope;
489 : }
490 :
491 103811454 : for ( ; i < NS2SA( st->output_Fs, FRAME_SIZE_NS ); i++ )
492 : {
493 103679700 : synthRef[i] *= ( icbweM2Ref );
494 : }
495 :
496 131754 : hStereoICBWE->icbweM2Ref_prev = icbweM2Ref;
497 :
498 131754 : return;
499 : }
500 :
501 : /*-------------------------------------------------------------------*
502 : * stereo_icBWE_decproc()
503 : *
504 : * Stereo (inter-channel) BWE mapping - decoder initialization
505 : *-------------------------------------------------------------------*/
506 :
507 4279775 : void stereo_icBWE_decproc(
508 : CPE_DEC_HANDLE hCPE, /* i/o: CPE decoder structure */
509 : float *output[CPE_CHANNELS], /* i/o: output synthesis */
510 : float outputHB[CPE_CHANNELS][L_FRAME48k], /* i : HB synthesis */
511 : const int16_t last_core, /* i : last core, primary channel */
512 : const int16_t last_bwidth, /* i : last bandwidth */
513 : const int16_t output_frame /* i : frame length */
514 : )
515 : {
516 : int16_t i, j, n, decoderDelay, icbweOLASize, dftOvlLen;
517 : int16_t core, memOffset, refChanIndx_bwe;
518 : float temp0[L_FRAME48k + NS2SA( 48000, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS )], temp1[L_FRAME48k + NS2SA( 48000, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS )];
519 : float winSlope, alpha;
520 : const float *win_dft;
521 : int32_t extl_brate, output_Fs;
522 :
523 4279775 : STEREO_ICBWE_DEC_HANDLE hStereoICBWE = hCPE->hStereoICBWE;
524 :
525 :
526 : /*--------------------------------------------------------------------*
527 : * skip IC-BWE in case of SID or NO_DATA frame
528 : * -------------------------------------------------------------------*/
529 :
530 4279775 : if ( ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_DFT ) && hCPE->nchan_out == 2 /*&& hCPE->hCoreCoder[0]->core_brate > SID_2k40*/ && hCPE->hCoreCoder[0]->last_core_brate <= SID_2k40 )
531 : {
532 31996 : stereo_icBWE_init_dec( hCPE->hStereoICBWE );
533 : }
534 :
535 4279775 : if ( hCPE->hCoreCoder[0]->core_brate <= SID_2k40 )
536 : {
537 83164 : return;
538 : }
539 :
540 : /*--------------------------------------------------------------------*
541 : * skip IC-BWE in case of mono DMX output *
542 : * -------------------------------------------------------------------*/
543 :
544 4196611 : if ( hCPE->nchan_out == 1 && hCPE->element_mode == IVAS_CPE_DFT )
545 : {
546 477901 : add_HB_to_mono_dmx( hCPE, output[0], outputHB[0], last_core, output_frame );
547 :
548 477901 : return;
549 : }
550 3718710 : else if ( hCPE->nchan_out == 1 && hCPE->element_mode != IVAS_CPE_TD )
551 : {
552 110368 : return;
553 : }
554 :
555 : /*--------------------------------------------------------------------*
556 : * IC-BWE processing
557 : * -------------------------------------------------------------------*/
558 :
559 3608342 : core = hCPE->hCoreCoder[0]->core;
560 3608342 : extl_brate = hCPE->hCoreCoder[0]->extl_brate;
561 3608342 : output_Fs = hCPE->hCoreCoder[0]->output_Fs;
562 :
563 3608342 : memOffset = NS2SA( output_Fs, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS );
564 :
565 : /* LRTD stereo mode - 2xBWEs used */
566 3608342 : if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->hCoreCoder[0]->tdm_LRTD_flag )
567 : {
568 : /* delay HB synth */
569 108543 : for ( n = 0; n < CPE_CHANNELS; n++ )
570 : {
571 72362 : mvr2r( outputHB[n] + output_frame - memOffset, temp0, memOffset );
572 72362 : mvr2r( outputHB[n], outputHB[n] + memOffset, output_frame - memOffset );
573 72362 : mvr2r( hCPE->prev_hb_synth[n], outputHB[n], memOffset );
574 72362 : mvr2r( temp0, hCPE->prev_hb_synth[n], memOffset );
575 : }
576 :
577 36181 : if ( hCPE->nchan_out == 1 )
578 : {
579 : /* stereo to mono downmix */
580 4500467 : for ( i = 0; i < output_frame; i++ )
581 : {
582 4493120 : outputHB[0][i] = ( outputHB[0][i] + outputHB[1][i] ) * 0.5f;
583 : }
584 7347 : v_add( output[0], outputHB[0], output[0], output_frame );
585 : }
586 : else
587 : {
588 : /* Add the delayed hb_synth component to the delayed ACELP synthesis */
589 86502 : for ( n = 0; n < CPE_CHANNELS; n++ )
590 : {
591 57668 : v_add( output[n], outputHB[n], output[n], output_frame );
592 : }
593 : }
594 : }
595 : else
596 : {
597 10716483 : for ( n = 0; n < CPE_CHANNELS; n++ )
598 : {
599 7144322 : set_f( hCPE->prev_hb_synth[n], 0, memOffset );
600 : }
601 : }
602 :
603 3608342 : if ( hCPE->element_mode != IVAS_CPE_MDCT && !( hCPE->element_mode == IVAS_CPE_TD && hCPE->hCoreCoder[0]->tdm_LRTD_flag ) )
604 : {
605 654502 : if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
606 : {
607 7613 : set_f( hStereoICBWE->memOutHB[0], 0, memOffset );
608 7613 : set_f( hStereoICBWE->memOutHB[1], 0, memOffset );
609 :
610 7613 : set_f( hStereoICBWE->memTransitionHB[0], 0, NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
611 7613 : set_f( hStereoICBWE->memTransitionHB[1], 0, NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
612 : }
613 :
614 654502 : if ( core == ACELP_CORE && extl_brate > 0 )
615 : {
616 270889 : refChanIndx_bwe = hStereoICBWE->refChanIndx_bwe;
617 :
618 270889 : if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->hCoreCoder[0]->bwidth > WB && last_bwidth == WB && hCPE->hCoreCoder[0]->ini_frame > 1 /* counter wass already updated */ )
619 : {
620 : /* fad-in reference HB signal */
621 263 : winSlope = ( memOffset > 0 ) ? ( 1.0f / memOffset ) : 0;
622 9968 : for ( i = 0; i < memOffset; i++ )
623 : {
624 9705 : outputHB[refChanIndx_bwe][i] *= ( i + 1 ) * winSlope;
625 : }
626 : }
627 : /* Resampled LB and HB offset */
628 270889 : mvr2r( outputHB[refChanIndx_bwe], temp0 + memOffset, output_frame - memOffset );
629 270889 : mvr2r( outputHB[!refChanIndx_bwe], temp1 + memOffset, output_frame - memOffset );
630 :
631 270889 : decoderDelay = NS2SA( output_Fs, IVAS_DEC_DELAY_NS );
632 :
633 270889 : if ( last_core != ACELP_CORE && hCPE->element_mode == IVAS_CPE_DFT )
634 : {
635 : /* hb_synth of mid band is faded out in the 1.25 ms prior to DFT analysis and the icbwe is faded in time domain */
636 13209 : icbweOLASize = NS2SA( output_Fs, STEREO_DFT_DELAY_DEC_BWE_NS );
637 :
638 1695981 : for ( i = 0; i < decoderDelay; i++ )
639 : {
640 1682772 : temp0[i] = 0;
641 1682772 : temp1[i] = 0;
642 : }
643 :
644 13209 : assert( icbweOLASize > 0 );
645 13209 : winSlope = 1.0f / icbweOLASize;
646 13209 : alpha = winSlope;
647 660429 : for ( ; i < decoderDelay + icbweOLASize; i++ )
648 : {
649 647220 : temp0[i] *= alpha;
650 647220 : temp1[i] *= alpha;
651 647220 : alpha += winSlope;
652 : }
653 : }
654 : else
655 : {
656 257680 : if ( refChanIndx_bwe != hStereoICBWE->prev_refChanIndx_bwe )
657 : {
658 22085 : winSlope = ( memOffset > 0 ) ? ( 1.0f / memOffset ) : 0;
659 802640 : for ( i = 0; i < memOffset; i++ )
660 : {
661 780555 : temp0[i] = ( ( i + 1 ) * winSlope ) * hStereoICBWE->memOutHB[refChanIndx_bwe][i] +
662 780555 : ( 1 - ( i + 1 ) * winSlope ) * hStereoICBWE->memOutHB[hStereoICBWE->prev_refChanIndx_bwe][i];
663 780555 : temp1[i] = ( ( i + 1 ) * winSlope ) * hStereoICBWE->memOutHB[hStereoICBWE->prev_refChanIndx_bwe][i] +
664 780555 : ( 1 - ( i + 1 ) * winSlope ) * hStereoICBWE->memOutHB[refChanIndx_bwe][i];
665 : }
666 : }
667 : else
668 : {
669 235595 : mvr2r( hStereoICBWE->memOutHB[refChanIndx_bwe], temp0, memOffset );
670 235595 : mvr2r( hStereoICBWE->memOutHB[!refChanIndx_bwe], temp1, memOffset );
671 : }
672 : }
673 :
674 270889 : if ( hCPE->nchan_out == 1 )
675 : {
676 : /* stereo to mono downmix */
677 40378 : for ( i = 0; i < output_frame; i++ )
678 : {
679 40320 : temp0[i] = ( temp0[i] + temp1[i] ) * 0.5f;
680 40320 : output[0][i] += temp0[i];
681 : }
682 : }
683 : else
684 : {
685 :
686 270831 : v_add( temp0, output[0], output[0], output_frame );
687 : #ifdef DEBUG_STEREO_DFT_NOSTEREO
688 : v_add( temp0, output[1], output[1], output_frame );
689 : #else
690 270831 : v_add( temp1, output[1], output[1], output_frame );
691 : #endif
692 : }
693 270889 : mvr2r( outputHB[0] + output_frame - memOffset, hStereoICBWE->memOutHB[0], memOffset );
694 270889 : mvr2r( outputHB[1] + output_frame - memOffset, hStereoICBWE->memOutHB[1], memOffset );
695 :
696 270889 : if ( hCPE->element_mode == IVAS_CPE_DFT )
697 : {
698 268927 : win_dft = hCPE->hStereoDft->win32ms;
699 268927 : dftOvlLen = hCPE->hStereoDft->dft32ms_ovl;
700 :
701 : /* Preparing buffers in anticipation of an ACELP to TCX switch */
702 268927 : j = 0;
703 8930722 : for ( i = 0; i < memOffset; i++ )
704 : {
705 8661795 : hStereoICBWE->memTransitionHB[0][i] = hStereoICBWE->memOutHB[0][i] * win_dft[STEREO_DFT32MS_STEP * ( dftOvlLen - 1 - j )];
706 8661795 : hStereoICBWE->memTransitionHB[1][i] = hStereoICBWE->memOutHB[1][i] * win_dft[STEREO_DFT32MS_STEP * ( dftOvlLen - 1 - j )];
707 8661795 : j++;
708 : }
709 :
710 20479782 : for ( i = 0; j < dftOvlLen; i++ )
711 : {
712 20210855 : hStereoICBWE->memTransitionHB[0][memOffset + i] = outputHB[0][output_frame - i - 1] * win_dft[STEREO_DFT32MS_STEP * ( dftOvlLen - 1 - j )];
713 20210855 : hStereoICBWE->memTransitionHB[1][memOffset + i] = outputHB[1][output_frame - i - 1] * win_dft[STEREO_DFT32MS_STEP * ( dftOvlLen - 1 - j )];
714 20210855 : j++;
715 : }
716 : }
717 :
718 270889 : hStereoICBWE->prev_refChanIndx_bwe = refChanIndx_bwe;
719 : }
720 : else
721 : {
722 383613 : if ( last_core == ACELP_CORE )
723 : {
724 56213 : if ( hCPE->element_mode == IVAS_CPE_TD )
725 : {
726 255 : v_add( output[0], hStereoICBWE->memOutHB[hStereoICBWE->prev_refChanIndx_bwe], output[0], memOffset );
727 255 : v_add( output[1], hStereoICBWE->memOutHB[!hStereoICBWE->prev_refChanIndx_bwe], output[1], memOffset );
728 : }
729 : else
730 : {
731 : /* This is generated in the ACELP frame and windowed. This process is akin to GenTransition for IC-BWE */
732 55958 : v_add( output[0], hStereoICBWE->memTransitionHB[hStereoICBWE->prev_refChanIndx_bwe], output[0], NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
733 55958 : v_add( output[1], hStereoICBWE->memTransitionHB[!hStereoICBWE->prev_refChanIndx_bwe], output[1], NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
734 : }
735 :
736 56213 : set_f( hStereoICBWE->memOutHB[0], 0, memOffset );
737 56213 : set_f( hStereoICBWE->memOutHB[1], 0, memOffset );
738 :
739 56213 : set_f( hStereoICBWE->memTransitionHB[0], 0, NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
740 56213 : set_f( hStereoICBWE->memTransitionHB[1], 0, NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
741 : }
742 : }
743 : }
744 2953840 : else if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->hCoreCoder[0]->tdm_LRTD_flag && hStereoICBWE != NULL )
745 : {
746 0 : stereo_icBWE_init_dec( hStereoICBWE );
747 : }
748 2953840 : else if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->last_element_mode == IVAS_CPE_DFT && last_core == ACELP_CORE )
749 : {
750 11523 : int16_t delay_tdbwe = NS2SA( output_Fs, DELAY_BWE_TOTAL_NS );
751 :
752 34569 : for ( n = 0; n < hCPE->nchan_out; n++ )
753 : {
754 2296252 : for ( i = 0; i < delay_tdbwe; i++ )
755 : {
756 2273206 : output[n][NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) - delay_tdbwe + i] += outputHB[0][i];
757 : }
758 : }
759 : /* reset BWE structs as they are only needed in the transition frame in MDCT Stereo */
760 11523 : td_bwe_dec_init( hCPE->hCoreCoder[0]->hBWE_TD, -1, output_Fs );
761 11523 : fd_bwe_dec_init( hCPE->hCoreCoder[0]->hBWE_FD );
762 : }
763 :
764 3608342 : if ( hCPE->element_mode == IVAS_CPE_DFT && ( max( hCPE->hStereoDft->td_gain[0], hCPE->hStereoDft->td_gain[1] ) > 0 ) )
765 : {
766 : float win_in, win_out, tmp;
767 :
768 232983 : win_dft = hCPE->hStereoDft->win32ms;
769 232983 : dftOvlLen = hCPE->hStereoDft->dft32ms_ovl;
770 :
771 24958133 : for ( i = 0; i < dftOvlLen; i++ )
772 : {
773 24725150 : win_in = win_dft[STEREO_DFT32MS_STEP * i] * win_dft[STEREO_DFT32MS_STEP * i];
774 24725150 : win_out = 1 - win_in;
775 24725150 : tmp = ( win_in * hCPE->hStereoDft->td_gain[0] + win_out * hCPE->hStereoDft->td_gain[1] ) * hCPE->hStereoDft->hb_stefi_sig[i];
776 :
777 24725150 : output[0][i] += tmp;
778 24725150 : output[1][i] -= tmp;
779 : }
780 133748793 : for ( i = dftOvlLen; i < output_frame; i++ )
781 : {
782 133515810 : tmp = hCPE->hStereoDft->td_gain[0] * hCPE->hStereoDft->hb_stefi_sig[i];
783 133515810 : output[0][i] += tmp;
784 133515810 : output[1][i] -= tmp;
785 : }
786 : }
787 :
788 3608342 : return;
789 : }
790 :
791 : /*-------------------------------------------------------------------*
792 : * stereo_icBWE_init_dec()
793 : *
794 : * Stereo (inter-channel) BWE mapping - decoder initialization
795 : *-------------------------------------------------------------------*/
796 :
797 57072 : void stereo_icBWE_init_dec(
798 : STEREO_ICBWE_DEC_HANDLE hStereoICBWE /* i/o: Stereo inter-channel BWE handle */
799 : )
800 : {
801 : /* BWE ref channel */
802 57072 : hStereoICBWE->refChanIndx_bwe = L_CH_INDX;
803 57072 : hStereoICBWE->prev_refChanIndx_bwe = L_CH_INDX;
804 :
805 : /* SHB output memory */
806 57072 : set_f( hStereoICBWE->memOutHB[0], 0, NS2SA( 48000, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS ) );
807 57072 : set_f( hStereoICBWE->memOutHB[1], 0, NS2SA( 48000, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS ) );
808 :
809 :
810 : /* SHB output memory */
811 57072 : set_f( hStereoICBWE->memTransitionHB[0], 0, NS2SA( 48000, STEREO_DFT32MS_OVL_NS ) );
812 57072 : set_f( hStereoICBWE->memTransitionHB[1], 0, NS2SA( 48000, STEREO_DFT32MS_OVL_NS ) );
813 :
814 : /* inter-channel BWE spectral shape adj. */
815 57072 : hStereoICBWE->prevSpecMapping = 0;
816 57072 : hStereoICBWE->prevgsMapping = 1.0f;
817 :
818 57072 : hStereoICBWE->icbweM2Ref_prev = 1.0f;
819 :
820 57072 : hStereoICBWE->prev_spIndx = 0;
821 57072 : hStereoICBWE->prev_gsIndx = 0;
822 :
823 57072 : ic_bwe_dec_reset( hStereoICBWE );
824 :
825 57072 : return;
826 : }
|