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 69461 : 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 69461 : set_f( hStereoICBWE->mem_syn_shb_nonref, 0, L_SHB_LAHEAD ); /* use samples from !acelp) */
60 69461 : set_f( hStereoICBWE->mem_lpc_shbsynth_nonref, 0, LPC_SHB_ORDER );
61 69461 : 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 69461 : hStereoICBWE->memShbSpecMapping = 0;
65 :
66 69461 : set_f( hStereoICBWE->memShbHilbert_nonref, 0, HILBERT_MEM_SIZE );
67 69461 : set_f( hStereoICBWE->memShbInterp_nonref, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
68 69461 : set_f( hStereoICBWE->memShb_fsout_nonref, 0, INTERP_3_2_MEM_LEN );
69 69461 : hStereoICBWE->syn_dm_phase_nonref = 0;
70 :
71 69461 : 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 1845201 : 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 1845201 : STEREO_DFT_DEC_DATA_HANDLE hStereoDft = hCPE->hStereoDft;
105 1845201 : STEREO_ICBWE_DEC_HANDLE hStereoICBWE = hCPE->hStereoICBWE;
106 1845201 : st = hCPE->hCoreCoder[0];
107 :
108 : /*--------------------------------------------------------------------*
109 : * skip IC-BWE in case of mono DMX output *
110 : * -------------------------------------------------------------------*/
111 :
112 1845201 : if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 && hCPE->hStereoDft->hConfig->res_cod_mode > STEREO_DFT_RES_COD_OFF )
113 : {
114 17008 : hCPE->hStereoDft->core_hist[0] = st->core;
115 :
116 17008 : return;
117 : }
118 1828193 : else if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 )
119 : {
120 :
121 229985 : return;
122 : }
123 :
124 : /*--------------------------------------------------------------------*
125 : * skip IC-BWE in case of SID or NO_DATA frame
126 : * -------------------------------------------------------------------*/
127 :
128 1598208 : if ( st->core_brate <= SID_2k40 )
129 : {
130 31436 : return;
131 : }
132 :
133 : /*--------------------------------------------------------------------*
134 : * TD high band stereo filling *
135 : * -------------------------------------------------------------------*/
136 :
137 : /* update buffers for TD stereo filling */
138 1566772 : if ( hCPE->element_mode == IVAS_CPE_DFT )
139 : {
140 232209 : float hb_nrg = EPSILON;
141 232209 : float hb_nrg2 = EPSILON;
142 :
143 232209 : if ( st->core == ACELP_CORE || st->last_core == ACELP_CORE )
144 : {
145 37401623 : for ( i = 0; i < output_frame / 2; i++ )
146 : {
147 37285280 : hb_nrg2 += synthRef[i] * synthRef[i];
148 : }
149 :
150 116343 : hCPE->hStereoDft->hb_nrg_subr[0] = hb_nrg2;
151 116343 : hb_nrg += hb_nrg2;
152 116343 : hb_nrg2 = EPSILON;
153 :
154 37401623 : for ( ; i < output_frame; i++ )
155 : {
156 37285280 : hb_nrg2 += synthRef[i] * synthRef[i];
157 : }
158 :
159 116343 : hCPE->hStereoDft->hb_nrg_subr[1] = hb_nrg2;
160 116343 : hb_nrg += hb_nrg2;
161 :
162 116343 : mvr2r( synthRef, hCPE->hStereoDft->hb_stefi_sig + hCPE->hStereoDft->hb_stefi_delay, output_frame );
163 : }
164 : else
165 : {
166 115866 : set_zero( hCPE->hStereoDft->hb_stefi_sig + hCPE->hStereoDft->hb_stefi_delay, output_frame );
167 : }
168 232209 : hCPE->hStereoDft->hb_nrg_subr[0] *= hCPE->hStereoDft->NFFT / 2;
169 232209 : hCPE->hStereoDft->hb_nrg_subr[1] *= hCPE->hStereoDft->NFFT / 2;
170 232209 : hCPE->hStereoDft->hb_nrg[0] = hb_nrg;
171 232209 : hCPE->hStereoDft->td_gain[0] = 0;
172 232209 : 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 1566772 : if ( st->core != ACELP_CORE || st->extl == -1 || ( hCPE->element_mode == IVAS_CPE_TD && hCPE->hCoreCoder[0]->tdm_LRTD_flag ) )
195 : {
196 1477756 : return;
197 : }
198 89016 : 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 89016 : set_f( fb_synth_nonref, 0, L_FRAME48k );
206 :
207 : /* core switching reset */
208 89016 : if ( st->last_core != ACELP_CORE || st->bwidth == WB )
209 : {
210 16645 : ic_bwe_dec_reset( hStereoICBWE );
211 :
212 16645 : if ( st->last_core != ACELP_CORE )
213 : {
214 5562 : hStereoICBWE->prevSpecMapping = 0.0f;
215 5562 : hStereoICBWE->prevgsMapping = 1.0f;
216 5562 : hStereoICBWE->icbweM2Ref_prev = 1.0f;
217 : }
218 :
219 16645 : if ( st->bwidth == WB )
220 : {
221 : /* copy to outputHB and reset hb_synth values */
222 11329 : mvr2r( synthRef, synth, output_frame );
223 :
224 11329 : if ( st->element_mode == IVAS_CPE_TD )
225 : {
226 9 : hStereoICBWE->prevSpecMapping = 0.0f;
227 9 : hStereoICBWE->prevgsMapping = 1.0f;
228 9 : hStereoICBWE->icbweM2Ref_prev = 1.0f;
229 : }
230 11320 : else if ( st->element_mode == IVAS_CPE_DFT )
231 : {
232 11320 : hStereoICBWE->refChanIndx_bwe = L_CH_INDX;
233 11320 : hStereoICBWE->prevSpecMapping = 0.0f;
234 :
235 11320 : prevgsMapping = hStereoICBWE->prevgsMapping;
236 11320 : temp1 = hStereoDft->side_gain[2 * STEREO_DFT_BAND_MAX + hStereoDft->nbands - 1];
237 11320 : icbweM2Ref = 1.f + temp1;
238 11320 : gsMapping = 1.f - temp1;
239 :
240 11320 : winLen = (int16_t) ( ( SHB_OVERLAP_LEN * st->output_Fs ) / 16000 );
241 11320 : winSlope = 1.0f / winLen;
242 11320 : alpha = winSlope;
243 248940 : for ( i = 0; i < winLen; i++ )
244 : {
245 237620 : synthRef[i] *= ( alpha * ( icbweM2Ref ) + ( 1.0f - alpha ) * ( hStereoICBWE->icbweM2Ref_prev ) );
246 237620 : synth[i] *= ( alpha * ( gsMapping ) + ( 1.0f - alpha ) * ( prevgsMapping ) );
247 237620 : alpha += winSlope;
248 : }
249 3575620 : for ( ; i < NS2SA( st->output_Fs, FRAME_SIZE_NS ); i++ )
250 : {
251 3564300 : synthRef[i] *= ( icbweM2Ref );
252 3564300 : synth[i] *= ( gsMapping );
253 : }
254 11320 : hStereoICBWE->icbweM2Ref_prev = icbweM2Ref;
255 11320 : hStereoICBWE->prevgsMapping = gsMapping;
256 : }
257 :
258 11329 : return;
259 : }
260 : }
261 :
262 77687 : if ( !st->bfi )
263 : {
264 74463 : hStereoICBWE->refChanIndx_bwe = get_next_indice( st, STEREO_ICBWE_REFBITS );
265 74463 : if ( st->flag_ACELP16k == 1 )
266 : {
267 50017 : spIndx = get_next_indice( st, STEREO_ICBWE_SPBITS );
268 : }
269 : else
270 : {
271 24446 : spIndx = 3;
272 : }
273 74463 : if ( st->element_mode == IVAS_CPE_TD )
274 : {
275 447 : gsIndx = get_next_indice( st, STEREO_ICBWE_GSBITS );
276 : }
277 : else
278 : {
279 74016 : gsIndx = get_next_indice( st, STEREO_ICBWE_GSBITS_DFT );
280 : }
281 :
282 : /* Store indices in case of frame loss */
283 74463 : hStereoICBWE->prev_spIndx = spIndx;
284 74463 : hStereoICBWE->prev_gsIndx = gsIndx;
285 : }
286 : else /*bfi*/
287 : {
288 : /* Retrieve last decoded indices */
289 3224 : spIndx = hStereoICBWE->prev_spIndx;
290 3224 : gsIndx = hStereoICBWE->prev_gsIndx;
291 3224 : hStereoICBWE->refChanIndx_bwe = hStereoICBWE->prev_refChanIndx_bwe;
292 : }
293 :
294 : /* IC-BWE parameter de-quant */
295 : /* sp Mapping */
296 77687 : hStereoICBWE->prevSpecMapping = usdequant( spIndx, -0.6f, 0.2f );
297 :
298 : /* gs Mapping */
299 77687 : prevgsMapping = hStereoICBWE->prevgsMapping;
300 :
301 77687 : if ( st->element_mode == IVAS_CPE_TD )
302 : {
303 468 : hStereoICBWE->prevgsMapping = icbwe_gsMapping_tbl[gsIndx];
304 : }
305 : else
306 : {
307 77219 : hStereoICBWE->prevgsMapping = icbwe_gsMappingDFT_tbl[gsIndx];
308 : }
309 :
310 77687 : hStereoICBWE->prevgsMapping = powf( 10, hStereoICBWE->prevgsMapping );
311 :
312 77687 : specMapping = hStereoICBWE->prevSpecMapping;
313 77687 : gsMapping = hStereoICBWE->prevgsMapping;
314 :
315 77687 : if ( ( st->extl == SWB_TBE || st->extl == FB_TBE ) && st->flag_ACELP16k == 1 )
316 : {
317 50600 : mvr2r( voice_factors, nlMixFac, NB_SUBFR16k );
318 50600 : if ( hCPE->hStereoDftDmx != NULL )
319 : {
320 18 : 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 50582 : if ( hCPE->hStereoTCA->targetGain < 0.5f || hCPE->hStereoTCA->targetGain > 2.0f )
328 : {
329 0 : v_multc( voice_factors, 0.5f, nlMixFac, NB_SUBFR16k );
330 : }
331 : }
332 :
333 50600 : nbSubFr = ( st->flag_ACELP16k == 0 ) ? NB_SUBFR : NB_SUBFR16k;
334 303600 : for ( i = 0, k = 0; i < nbSubFr; i++ )
335 : {
336 253000 : if ( hCPE->hCoreCoder[0]->coder_type == UNVOICED || hStereoICBWE->MSFlag == 1 )
337 : {
338 47695 : temp1 = 0;
339 47695 : temp2 = 1.0f;
340 : }
341 : else
342 : {
343 205305 : temp1 = sqrtf( nlMixFac[i] );
344 205305 : temp2 = sqrtf( 1.0f - nlMixFac[i] );
345 : }
346 :
347 16445000 : for ( j = 0; j < L_FRAME16k / nbSubFr; j++, k++ )
348 : {
349 16192000 : excSHB_nonref[k] = temp1 * hStereoICBWE->nlExc16k[k] + temp2 * hStereoICBWE->mixExc16k[k];
350 : }
351 : }
352 :
353 : /* LP synthesis */
354 50600 : mvr2r( hStereoICBWE->mem_syn_shb_nonref, shb_synth_nonref, L_SHB_LAHEAD );
355 50600 : 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 50600 : prev_pow = sum2_f( shb_synth_nonref, L_SHB_LAHEAD + 10 );
358 50600 : curr_pow = sum2_f( shb_synth_nonref + L_SHB_LAHEAD + 10, L_SHB_LAHEAD + 10 );
359 :
360 50600 : if ( prev_pow == 0 )
361 : {
362 0 : scale = 0;
363 : }
364 : else
365 : {
366 50600 : scale = sqrtf( curr_pow / prev_pow );
367 : }
368 :
369 1062600 : for ( i = 0; i < L_SHB_LAHEAD; i++ )
370 : {
371 1012000 : shb_synth_nonref[i] *= scale;
372 : }
373 :
374 556600 : for ( ; i < L_SHB_LAHEAD + 10; i++ )
375 : {
376 506000 : temp = ( i - 19 ) / 10.0f;
377 506000 : shb_synth_nonref[i] *= ( temp * 1.0f + ( 1.0f - temp ) * scale );
378 : }
379 :
380 : /* spec and gs adjustment */
381 50600 : deemph( shb_synth_nonref + L_SHB_LAHEAD, specMapping, L_FRAME16k, &( hStereoICBWE->memShbSpecMapping ) );
382 50600 : mvr2r( shb_synth_nonref + L_FRAME16k, hStereoICBWE->mem_syn_shb_nonref, L_SHB_LAHEAD );
383 :
384 50600 : 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 50600 : if ( st->extl == FB_TBE )
387 : {
388 20136 : 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 50600 : GenSHBSynth( shb_synth_nonref, error, hStereoICBWE->memShbHilbert_nonref, hStereoICBWE->memShbInterp_nonref, st->L_frame, &( hStereoICBWE->syn_dm_phase_nonref ) );
393 : }
394 : else
395 : {
396 27087 : mvr2r( synthRef, synth, output_frame );
397 :
398 27087 : winLen = (int16_t) ( ( SHB_OVERLAP_LEN * st->output_Fs ) / 16000 );
399 27087 : winSlope = 1.0f / winLen;
400 27087 : alpha = winSlope;
401 :
402 27087 : ratio_L = ( hCPE->element_mode == IVAS_CPE_DFT ) ? ( 0.5f ) : ( tdm_ratio_tabl[hCPE->hStereoTD->tdm_last_ratio_idx] );
403 :
404 27087 : icbweM2Ref = gsMapping;
405 27087 : if ( hStereoICBWE->refChanIndx_bwe == L_CH_INDX )
406 : {
407 8475 : if ( ratio_L >= 0.1f )
408 : {
409 8447 : icbweM2Ref = sqrtf( 0.5f - min( 0.5f, ( 1 - ratio_L ) * ( 1 - ratio_L ) * gsMapping * gsMapping ) ) / ratio_L;
410 : }
411 : }
412 : else
413 : {
414 18612 : if ( ratio_L <= 0.9f )
415 : {
416 18612 : icbweM2Ref = sqrtf( 0.5f - min( 0.5f, ratio_L * ratio_L * gsMapping * gsMapping ) ) / ( 1 - ratio_L );
417 : }
418 : }
419 :
420 27087 : icbweM2Ref = max( gsMapping, icbweM2Ref );
421 :
422 1113867 : for ( i = 0; i < winLen; i++ )
423 : {
424 1086780 : synthRef[i] *= ( alpha * ( icbweM2Ref ) + ( 1.0f - alpha ) * ( hStereoICBWE->icbweM2Ref_prev ) );
425 1086780 : synth[i] *= ( alpha * ( gsMapping ) + ( 1.0f - alpha ) * ( prevgsMapping ) );
426 1086780 : alpha += winSlope;
427 : }
428 16328787 : for ( ; i < NS2SA( st->output_Fs, FRAME_SIZE_NS ); i++ )
429 : {
430 16301700 : synthRef[i] *= ( icbweM2Ref );
431 16301700 : synth[i] *= ( gsMapping );
432 : }
433 27087 : hStereoICBWE->icbweM2Ref_prev = icbweM2Ref;
434 :
435 27087 : ic_bwe_dec_reset( hStereoICBWE );
436 27087 : hStereoICBWE->prevSpecMapping = 0.0f;
437 :
438 27087 : return;
439 : }
440 :
441 : /* resample to output FS */
442 50600 : if ( st->output_Fs == 48000 )
443 : {
444 27361 : interpolate_3_over_2_allpass( error, L_FRAME32k, synth, hStereoICBWE->memShb_fsout_nonref );
445 : }
446 23239 : else if ( st->output_Fs == 32000 )
447 : {
448 23239 : 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 50600 : if ( st->extl == FB_TBE && st->output_Fs == 48000 )
457 : {
458 17325 : v_add( fb_synth_nonref, synth, synth, L_FRAME48k );
459 : }
460 :
461 : /* copy to outputHB and reset hb_synth values */
462 50600 : ratio_L = ( hCPE->element_mode == IVAS_CPE_DFT ) ? ( 0.5f ) : ( tdm_ratio_tabl[hCPE->hStereoTD->tdm_last_ratio_idx] );
463 :
464 50600 : icbweM2Ref = gsMapping;
465 50600 : if ( hStereoICBWE->refChanIndx_bwe == L_CH_INDX )
466 : {
467 24305 : if ( ratio_L >= 0.1f )
468 : {
469 24281 : icbweM2Ref = sqrtf( 0.5f - min( 0.5f, ( 1 - ratio_L ) * ( 1 - ratio_L ) * gsMapping * gsMapping ) ) / ratio_L;
470 : }
471 : }
472 : else
473 : {
474 26295 : if ( ratio_L <= 0.9f )
475 : {
476 26278 : icbweM2Ref = sqrtf( 0.5f - min( 0.5f, ratio_L * ratio_L * gsMapping * gsMapping ) ) / ( 1 - ratio_L );
477 : }
478 : }
479 :
480 50600 : icbweM2Ref = max( gsMapping, icbweM2Ref );
481 :
482 50600 : winLen = (int16_t) ( ( SHB_OVERLAP_LEN * st->output_Fs ) / 16000 );
483 50600 : winSlope = 1.0f / winLen;
484 50600 : alpha = winSlope;
485 2621820 : for ( i = 0; i < winLen; i++ )
486 : {
487 2571220 : synthRef[i] *= ( alpha * ( icbweM2Ref ) + ( 1.0f - alpha ) * ( hStereoICBWE->icbweM2Ref_prev ) );
488 2571220 : alpha += winSlope;
489 : }
490 :
491 38618900 : for ( ; i < NS2SA( st->output_Fs, FRAME_SIZE_NS ); i++ )
492 : {
493 38568300 : synthRef[i] *= ( icbweM2Ref );
494 : }
495 :
496 50600 : hStereoICBWE->icbweM2Ref_prev = icbweM2Ref;
497 :
498 50600 : return;
499 : }
500 :
501 : /*-------------------------------------------------------------------*
502 : * stereo_icBWE_decproc()
503 : *
504 : * Stereo (inter-channel) BWE mapping - decoder initialization
505 : *-------------------------------------------------------------------*/
506 :
507 1845201 : 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 1845201 : 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 1845201 : 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 17839 : stereo_icBWE_init_dec( hCPE->hStereoICBWE );
533 : }
534 :
535 1845201 : if ( hCPE->hCoreCoder[0]->core_brate <= SID_2k40 )
536 : {
537 45935 : return;
538 : }
539 :
540 : /*--------------------------------------------------------------------*
541 : * skip IC-BWE in case of mono DMX output *
542 : * -------------------------------------------------------------------*/
543 :
544 1799266 : if ( hCPE->nchan_out == 1 && hCPE->element_mode == IVAS_CPE_DFT )
545 : {
546 232494 : add_HB_to_mono_dmx( hCPE, output[0], outputHB[0], last_core, output_frame );
547 :
548 232494 : return;
549 : }
550 1566772 : else if ( hCPE->nchan_out == 1 && hCPE->element_mode != IVAS_CPE_TD )
551 : {
552 48327 : return;
553 : }
554 :
555 : /*--------------------------------------------------------------------*
556 : * IC-BWE processing
557 : * -------------------------------------------------------------------*/
558 :
559 1518445 : core = hCPE->hCoreCoder[0]->core;
560 1518445 : extl_brate = hCPE->hCoreCoder[0]->extl_brate;
561 1518445 : output_Fs = hCPE->hCoreCoder[0]->output_Fs;
562 :
563 1518445 : memOffset = NS2SA( output_Fs, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS );
564 :
565 : /* LRTD stereo mode - 2xBWEs used */
566 1518445 : if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->hCoreCoder[0]->tdm_LRTD_flag )
567 : {
568 : /* delay HB synth */
569 40968 : for ( n = 0; n < CPE_CHANNELS; n++ )
570 : {
571 27312 : mvr2r( outputHB[n] + output_frame - memOffset, temp0, memOffset );
572 27312 : mvr2r( outputHB[n], outputHB[n] + memOffset, output_frame - memOffset );
573 27312 : mvr2r( hCPE->prev_hb_synth[n], outputHB[n], memOffset );
574 27312 : mvr2r( temp0, hCPE->prev_hb_synth[n], memOffset );
575 : }
576 :
577 13656 : if ( hCPE->nchan_out == 1 )
578 : {
579 : /* stereo to mono downmix */
580 2383815 : for ( i = 0; i < output_frame; i++ )
581 : {
582 2379840 : outputHB[0][i] = ( outputHB[0][i] + outputHB[1][i] ) * 0.5f;
583 : }
584 3975 : 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 29043 : for ( n = 0; n < CPE_CHANNELS; n++ )
590 : {
591 19362 : v_add( output[n], outputHB[n], output[n], output_frame );
592 : }
593 : }
594 : }
595 : else
596 : {
597 4514367 : for ( n = 0; n < CPE_CHANNELS; n++ )
598 : {
599 3009578 : set_f( hCPE->prev_hb_synth[n], 0, memOffset );
600 : }
601 : }
602 :
603 1518445 : if ( hCPE->element_mode != IVAS_CPE_MDCT && !( hCPE->element_mode == IVAS_CPE_TD && hCPE->hCoreCoder[0]->tdm_LRTD_flag ) )
604 : {
605 233157 : if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
606 : {
607 1069 : set_f( hStereoICBWE->memOutHB[0], 0, memOffset );
608 1069 : set_f( hStereoICBWE->memOutHB[1], 0, memOffset );
609 :
610 1069 : set_f( hStereoICBWE->memTransitionHB[0], 0, NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
611 1069 : set_f( hStereoICBWE->memTransitionHB[1], 0, NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
612 : }
613 :
614 233157 : if ( core == ACELP_CORE && extl_brate > 0 )
615 : {
616 88395 : refChanIndx_bwe = hStereoICBWE->refChanIndx_bwe;
617 :
618 88395 : 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 57 : winSlope = ( memOffset > 0 ) ? ( 1.0f / memOffset ) : 0;
622 2217 : for ( i = 0; i < memOffset; i++ )
623 : {
624 2160 : outputHB[refChanIndx_bwe][i] *= ( i + 1 ) * winSlope;
625 : }
626 : }
627 : /* Resampled LB and HB offset */
628 88395 : mvr2r( outputHB[refChanIndx_bwe], temp0 + memOffset, output_frame - memOffset );
629 88395 : mvr2r( outputHB[!refChanIndx_bwe], temp1 + memOffset, output_frame - memOffset );
630 :
631 88395 : decoderDelay = NS2SA( output_Fs, IVAS_DEC_DELAY_NS );
632 :
633 88395 : 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 6885 : icbweOLASize = NS2SA( output_Fs, STEREO_DFT_DELAY_DEC_BWE_NS );
637 :
638 865873 : for ( i = 0; i < decoderDelay; i++ )
639 : {
640 858988 : temp0[i] = 0;
641 858988 : temp1[i] = 0;
642 : }
643 :
644 6885 : assert( icbweOLASize > 0 );
645 6885 : winSlope = 1.0f / icbweOLASize;
646 6885 : alpha = winSlope;
647 337265 : for ( ; i < decoderDelay + icbweOLASize; i++ )
648 : {
649 330380 : temp0[i] *= alpha;
650 330380 : temp1[i] *= alpha;
651 330380 : alpha += winSlope;
652 : }
653 : }
654 : else
655 : {
656 81510 : if ( refChanIndx_bwe != hStereoICBWE->prev_refChanIndx_bwe )
657 : {
658 10542 : winSlope = ( memOffset > 0 ) ? ( 1.0f / memOffset ) : 0;
659 398187 : for ( i = 0; i < memOffset; i++ )
660 : {
661 387645 : temp0[i] = ( ( i + 1 ) * winSlope ) * hStereoICBWE->memOutHB[refChanIndx_bwe][i] +
662 387645 : ( 1 - ( i + 1 ) * winSlope ) * hStereoICBWE->memOutHB[hStereoICBWE->prev_refChanIndx_bwe][i];
663 387645 : temp1[i] = ( ( i + 1 ) * winSlope ) * hStereoICBWE->memOutHB[hStereoICBWE->prev_refChanIndx_bwe][i] +
664 387645 : ( 1 - ( i + 1 ) * winSlope ) * hStereoICBWE->memOutHB[refChanIndx_bwe][i];
665 : }
666 : }
667 : else
668 : {
669 70968 : mvr2r( hStereoICBWE->memOutHB[refChanIndx_bwe], temp0, memOffset );
670 70968 : mvr2r( hStereoICBWE->memOutHB[!refChanIndx_bwe], temp1, memOffset );
671 : }
672 : }
673 :
674 88395 : if ( hCPE->nchan_out == 1 )
675 : {
676 : /* stereo to mono downmix */
677 17307 : for ( i = 0; i < output_frame; i++ )
678 : {
679 17280 : temp0[i] = ( temp0[i] + temp1[i] ) * 0.5f;
680 17280 : output[0][i] += temp0[i];
681 : }
682 : }
683 : else
684 : {
685 :
686 88368 : 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 88368 : v_add( temp1, output[1], output[1], output_frame );
691 : #endif
692 : }
693 88395 : mvr2r( outputHB[0] + output_frame - memOffset, hStereoICBWE->memOutHB[0], memOffset );
694 88395 : mvr2r( outputHB[1] + output_frame - memOffset, hStereoICBWE->memOutHB[1], memOffset );
695 :
696 88395 : if ( hCPE->element_mode == IVAS_CPE_DFT )
697 : {
698 87918 : win_dft = hCPE->hStereoDft->win32ms;
699 87918 : dftOvlLen = hCPE->hStereoDft->dft32ms_ovl;
700 :
701 : /* Preparing buffers in anticipation of an ACELP to TCX switch */
702 87918 : j = 0;
703 2971053 : for ( i = 0; i < memOffset; i++ )
704 : {
705 2883135 : hStereoICBWE->memTransitionHB[0][i] = hStereoICBWE->memOutHB[0][i] * win_dft[STEREO_DFT32MS_STEP * ( dftOvlLen - 1 - j )];
706 2883135 : hStereoICBWE->memTransitionHB[1][i] = hStereoICBWE->memOutHB[1][i] * win_dft[STEREO_DFT32MS_STEP * ( dftOvlLen - 1 - j )];
707 2883135 : j++;
708 : }
709 :
710 6815233 : for ( i = 0; j < dftOvlLen; i++ )
711 : {
712 6727315 : hStereoICBWE->memTransitionHB[0][memOffset + i] = outputHB[0][output_frame - i - 1] * win_dft[STEREO_DFT32MS_STEP * ( dftOvlLen - 1 - j )];
713 6727315 : hStereoICBWE->memTransitionHB[1][memOffset + i] = outputHB[1][output_frame - i - 1] * win_dft[STEREO_DFT32MS_STEP * ( dftOvlLen - 1 - j )];
714 6727315 : j++;
715 : }
716 : }
717 :
718 88395 : hStereoICBWE->prev_refChanIndx_bwe = refChanIndx_bwe;
719 : }
720 : else
721 : {
722 144762 : if ( last_core == ACELP_CORE )
723 : {
724 25641 : if ( hCPE->element_mode == IVAS_CPE_TD )
725 : {
726 168 : v_add( output[0], hStereoICBWE->memOutHB[hStereoICBWE->prev_refChanIndx_bwe], output[0], memOffset );
727 168 : 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 25473 : v_add( output[0], hStereoICBWE->memTransitionHB[hStereoICBWE->prev_refChanIndx_bwe], output[0], NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
733 25473 : v_add( output[1], hStereoICBWE->memTransitionHB[!hStereoICBWE->prev_refChanIndx_bwe], output[1], NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
734 : }
735 :
736 25641 : set_f( hStereoICBWE->memOutHB[0], 0, memOffset );
737 25641 : set_f( hStereoICBWE->memOutHB[1], 0, memOffset );
738 :
739 25641 : set_f( hStereoICBWE->memTransitionHB[0], 0, NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
740 25641 : set_f( hStereoICBWE->memTransitionHB[1], 0, NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
741 : }
742 : }
743 : }
744 1285288 : 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 1285288 : else if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->last_element_mode == IVAS_CPE_DFT && last_core == ACELP_CORE )
749 : {
750 2661 : int16_t delay_tdbwe = NS2SA( output_Fs, DELAY_BWE_TOTAL_NS );
751 :
752 7983 : for ( n = 0; n < hCPE->nchan_out; n++ )
753 : {
754 484694 : for ( i = 0; i < delay_tdbwe; i++ )
755 : {
756 479372 : 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 2661 : td_bwe_dec_init( hCPE->hCoreCoder[0]->hBWE_TD, -1, output_Fs );
761 2661 : fd_bwe_dec_init( hCPE->hCoreCoder[0]->hBWE_FD );
762 : }
763 :
764 1518445 : 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 79133 : win_dft = hCPE->hStereoDft->win32ms;
769 79133 : dftOvlLen = hCPE->hStereoDft->dft32ms_ovl;
770 :
771 8636233 : for ( i = 0; i < dftOvlLen; i++ )
772 : {
773 8557100 : win_in = win_dft[STEREO_DFT32MS_STEP * i] * win_dft[STEREO_DFT32MS_STEP * i];
774 8557100 : win_out = 1 - win_in;
775 8557100 : tmp = ( win_in * hCPE->hStereoDft->td_gain[0] + win_out * hCPE->hStereoDft->td_gain[1] ) * hCPE->hStereoDft->hb_stefi_sig[i];
776 :
777 8557100 : output[0][i] += tmp;
778 8557100 : output[1][i] -= tmp;
779 : }
780 46287473 : for ( i = dftOvlLen; i < output_frame; i++ )
781 : {
782 46208340 : tmp = hCPE->hStereoDft->td_gain[0] * hCPE->hStereoDft->hb_stefi_sig[i];
783 46208340 : output[0][i] += tmp;
784 46208340 : output[1][i] -= tmp;
785 : }
786 : }
787 :
788 1518445 : return;
789 : }
790 :
791 : /*-------------------------------------------------------------------*
792 : * stereo_icBWE_init_dec()
793 : *
794 : * Stereo (inter-channel) BWE mapping - decoder initialization
795 : *-------------------------------------------------------------------*/
796 :
797 25729 : 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 25729 : hStereoICBWE->refChanIndx_bwe = L_CH_INDX;
803 25729 : hStereoICBWE->prev_refChanIndx_bwe = L_CH_INDX;
804 :
805 : /* SHB output memory */
806 25729 : set_f( hStereoICBWE->memOutHB[0], 0, NS2SA( 48000, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS ) );
807 25729 : set_f( hStereoICBWE->memOutHB[1], 0, NS2SA( 48000, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS ) );
808 :
809 :
810 : /* SHB output memory */
811 25729 : set_f( hStereoICBWE->memTransitionHB[0], 0, NS2SA( 48000, STEREO_DFT32MS_OVL_NS ) );
812 25729 : set_f( hStereoICBWE->memTransitionHB[1], 0, NS2SA( 48000, STEREO_DFT32MS_OVL_NS ) );
813 :
814 : /* inter-channel BWE spectral shape adj. */
815 25729 : hStereoICBWE->prevSpecMapping = 0;
816 25729 : hStereoICBWE->prevgsMapping = 1.0f;
817 :
818 25729 : hStereoICBWE->icbweM2Ref_prev = 1.0f;
819 :
820 25729 : hStereoICBWE->prev_spIndx = 0;
821 25729 : hStereoICBWE->prev_gsIndx = 0;
822 :
823 25729 : ic_bwe_dec_reset( hStereoICBWE );
824 :
825 25729 : return;
826 : }
|