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 <stdint.h>
34 : #include "options.h"
35 : #ifdef DEBUGGING
36 : #include "debug.h"
37 : #endif
38 : #include <math.h>
39 : #include "prot.h"
40 : #include "ivas_prot.h"
41 : #include "ivas_rom_com.h"
42 : #include <assert.h>
43 : #include "wmc_auto.h"
44 :
45 :
46 : /*------------------------------------------------------------------------------------------*
47 : * Static functions declarations
48 : *------------------------------------------------------------------------------------------*/
49 :
50 : static void ivas_get_active_bins( const int16_t **pActive_bins, const int16_t **pActive_bins_abs, const int16_t **pStart_offset, const int16_t **pStart_offset_ab, const int32_t sampling_rate );
51 : static void ivas_get_ld_fb_resp( float **ppIdeal_FRs_re, float **ppIdeal_FRs_im, float **ppNew_FRs_re, float **ppNew_FRs_im, const int16_t *pActive_bins, const int16_t *pStart_offset, const int16_t num_bands, const int16_t delay, const int32_t sampling_rate );
52 : static ivas_error ivas_filterbank_setup( IVAS_FB_MIXER_HANDLE hFbMixer, const int32_t sampling_rate );
53 : static ivas_error ivas_fb_mixer_get_window( const int16_t fade_len, const int32_t sampling_rate, const float **pWindow );
54 :
55 :
56 : /*-----------------------------------------------------------------------------------------*
57 : * Function ivas_get_num_bands_from_bw_idx()
58 : *
59 : * Get number of bands from BW index
60 : *-----------------------------------------------------------------------------------------*/
61 :
62 : /*! r: number of spectral bands */
63 5542123 : int16_t ivas_get_num_bands_from_bw_idx(
64 : const int16_t bwidth /* i : audio bandwidth */
65 : )
66 : {
67 : int16_t num_active_bands;
68 :
69 5542123 : assert( bwidth > 0 ); /*NB BW is not supported*/
70 5542123 : num_active_bands = ivas_num_active_bands[bwidth - 1];
71 :
72 5542123 : return num_active_bands;
73 : }
74 :
75 :
76 : /*-----------------------------------------------------------------------------------------*
77 : * Function ivas_get_num_bands()
78 : *
79 : * Get number of bands depending on the sampling rates
80 : *-----------------------------------------------------------------------------------------*/
81 :
82 3554 : static int16_t ivas_get_num_bands(
83 : const int32_t sampling_rate )
84 : {
85 3554 : int16_t bwidth = ivas_get_bw_idx_from_sample_rate( sampling_rate );
86 3554 : int16_t num_active_bands = ivas_get_num_bands_from_bw_idx( bwidth );
87 :
88 3554 : return num_active_bands;
89 : }
90 :
91 :
92 : /*---------------------------------------------------------------------*
93 : * Function ivas_fb_set_cfg()
94 : *
95 : * Set default configs for FB mixer
96 : *---------------------------------------------------------------------*/
97 :
98 67429 : ivas_error ivas_fb_set_cfg(
99 : IVAS_FB_CFG **pFb_cfg_out, /* o : FB config. handle */
100 : const int16_t ivas_format, /* i : IVAS format */
101 : const int16_t num_in_chans, /* i : number of FB input channels */
102 : const int16_t num_out_chans, /* i : number of FB output channels */
103 : const int16_t active_w_mixing, /* i : active_w_mixing flag */
104 : const int32_t sampling_rate, /* i : sampling rate */
105 : const int16_t nchan_fb_in /* i : number of dirAC analysis channels*/
106 : )
107 : {
108 : IVAS_FB_CFG *pFb_cfg;
109 :
110 67429 : if ( ( pFb_cfg = (IVAS_FB_CFG *) malloc( sizeof( IVAS_FB_CFG ) ) ) == NULL )
111 : {
112 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FB mixer config" );
113 : }
114 :
115 67429 : pFb_cfg->num_in_chans = num_in_chans;
116 67429 : pFb_cfg->num_out_chans = num_out_chans;
117 67429 : pFb_cfg->nchan_fb_in = nchan_fb_in;
118 :
119 67429 : pFb_cfg->pcm_offset = 0; /* note: in SPAR decoder, this parameter is overwritten later */
120 67429 : pFb_cfg->active_w_mixing = active_w_mixing;
121 67429 : pFb_cfg->windowed_fr_offset = 0;
122 :
123 67429 : if ( ivas_format == ISM_FORMAT )
124 : {
125 1861 : pFb_cfg->fb_latency = NS2SA( sampling_rate, DELAY_FB_4_NS );
126 1861 : pFb_cfg->fade_len = NS2SA( sampling_rate, DELAY_FB_4_NS );
127 1861 : pFb_cfg->prior_input_length = NS2SA( sampling_rate, DELAY_DIRAC_ENC_CMP_NS_PARAM_ISM ) + NS2SA( sampling_rate, DIRAC_SLOT_ENC_NS );
128 : }
129 65568 : else if ( ivas_format == SBA_FORMAT )
130 : {
131 50000 : pFb_cfg->fb_latency = NS2SA( sampling_rate, DELAY_FB_1_NS );
132 :
133 50000 : pFb_cfg->fade_len = NS2SA( sampling_rate, DELAY_FB_4_NS );
134 50000 : pFb_cfg->prior_input_length = NS2SA( sampling_rate, FRAME_SIZE_NS );
135 50000 : pFb_cfg->windowed_fr_offset = (int16_t) ( (float) ( sampling_rate / FRAMES_PER_SEC ) * 3.0f / 4.0f ) - NS2SA( sampling_rate, DELAY_DIRAC_SPAR_ENC_CMP_NS );
136 : }
137 15568 : else if ( ivas_format == MASA_FORMAT )
138 : {
139 11395 : pFb_cfg->fb_latency = NS2SA( sampling_rate, DELAY_FB_1_NS );
140 11395 : pFb_cfg->fade_len = NS2SA( sampling_rate, DELAY_FB_1_NS );
141 11395 : pFb_cfg->prior_input_length = NS2SA( sampling_rate, DELAY_DIRAC_ENC_CMP_NS ) + NS2SA( sampling_rate, DIRAC_SLOT_ENC_NS );
142 : }
143 4173 : else if ( ivas_format == MC_FORMAT )
144 : {
145 4173 : pFb_cfg->fb_latency = NS2SA( sampling_rate, DELAY_FB_1_NS );
146 4173 : pFb_cfg->fade_len = NS2SA( sampling_rate, DELAY_FB_1_NS );
147 4173 : pFb_cfg->prior_input_length = NS2SA( sampling_rate, DELAY_DIRAC_ENC_CMP_NS ) + NS2SA( sampling_rate, PARAM_MC_SLOT_ENC_NS );
148 : }
149 :
150 67429 : *pFb_cfg_out = pFb_cfg;
151 :
152 67429 : return IVAS_ERR_OK;
153 : }
154 :
155 :
156 : /*-------------------------------------------------------------------------
157 : * ivas_FB_mixer_open()
158 : *
159 : * Allocate and initialize FB mixer handle
160 : *------------------------------------------------------------------------*/
161 :
162 67429 : ivas_error ivas_FB_mixer_open(
163 : IVAS_FB_MIXER_HANDLE *hFbMixer_out, /* i/o: FB mixer handle */
164 : const int32_t sampling_rate, /* i : sampling rate */
165 : IVAS_FB_CFG *fb_cfg, /* i : FB config. handle */
166 : const int16_t spar_reconfig_flag /* i : SPAR reconfiguration flag */
167 : )
168 : {
169 : IVAS_FB_MIXER_HANDLE hFbMixer;
170 : int16_t i, j, frame_len, num_bands;
171 : int16_t num_chs_alloc;
172 : ivas_error error;
173 :
174 67429 : error = IVAS_ERR_OK;
175 :
176 67429 : frame_len = (int16_t) ( sampling_rate / FRAMES_PER_SEC );
177 :
178 67429 : hFbMixer = *hFbMixer_out;
179 :
180 67429 : if ( !spar_reconfig_flag )
181 : {
182 18553 : if ( ( hFbMixer = (IVAS_FB_MIXER_HANDLE) malloc( sizeof( IVAS_FB_MIXER_DATA ) ) ) == NULL )
183 : {
184 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FB mixer encoder" );
185 : }
186 :
187 18553 : if ( fb_cfg->num_out_chans > 0 )
188 : {
189 1777 : if ( ( hFbMixer->pFb = (ivas_filterbank_t *) malloc( sizeof( ivas_filterbank_t ) ) ) == NULL )
190 : {
191 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FB mixer encoder" );
192 : }
193 : }
194 : else
195 : {
196 16776 : hFbMixer->pFb = NULL;
197 : }
198 : }
199 :
200 67429 : if ( fb_cfg->active_w_mixing == -1 )
201 : {
202 37500 : num_chs_alloc = 0;
203 : }
204 29929 : else if ( fb_cfg->active_w_mixing )
205 : {
206 3532 : num_chs_alloc = max( fb_cfg->num_in_chans, fb_cfg->nchan_fb_in );
207 : }
208 : else
209 : {
210 26397 : num_chs_alloc = 1; /* only W channel processed for predicting YZX */
211 : }
212 :
213 107954 : for ( i = 0; i < num_chs_alloc; i++ )
214 : {
215 40525 : if ( fb_cfg->num_out_chans == 0 )
216 : {
217 16776 : hFbMixer->ppFilterbank_inFR_re[i] = NULL;
218 16776 : hFbMixer->ppFilterbank_inFR_im[i] = NULL;
219 : }
220 : else
221 : {
222 23749 : j = fb_cfg->remix_order[i];
223 :
224 23749 : if ( ( hFbMixer->ppFilterbank_inFR_re[j] = (float *) malloc( sizeof( float ) * frame_len ) ) == NULL )
225 : {
226 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FB mixer encoder" );
227 : }
228 :
229 23749 : if ( ( hFbMixer->ppFilterbank_inFR_im[j] = (float *) malloc( sizeof( float ) * frame_len ) ) == NULL )
230 : {
231 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FB mixer encoder" );
232 : }
233 : }
234 : }
235 :
236 67429 : if ( fb_cfg->active_w_mixing == -1 )
237 : {
238 37500 : num_chs_alloc = 0;
239 : }
240 : else
241 : {
242 29929 : num_chs_alloc = max( fb_cfg->num_in_chans, fb_cfg->nchan_fb_in );
243 : }
244 :
245 231849 : for ( i = 0; i < num_chs_alloc; i++ )
246 : {
247 164420 : if ( ( hFbMixer->ppFilterbank_prior_input[i] = (float *) malloc( sizeof( float ) * fb_cfg->prior_input_length ) ) == NULL )
248 : {
249 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FB mixer encoder" );
250 : }
251 164420 : set_f( hFbMixer->ppFilterbank_prior_input[i], 0, fb_cfg->prior_input_length );
252 : }
253 :
254 67429 : if ( ( fb_cfg->active_w_mixing != -1 ) && ( fb_cfg->num_out_chans > 0 ) )
255 : {
256 : float *pTemp_mem;
257 13153 : if ( ( pTemp_mem = (float *) malloc( sizeof( float ) * fb_cfg->num_out_chans * max( fb_cfg->num_in_chans, fb_cfg->nchan_fb_in ) * IVAS_MAX_NUM_BANDS ) ) == NULL )
258 : {
259 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FB mixer" );
260 : }
261 46168 : for ( i = 0; i < fb_cfg->num_out_chans; i++ )
262 : {
263 214503 : for ( j = 0; j < fb_cfg->num_in_chans; j++ )
264 : {
265 181488 : hFbMixer->prior_mixer[i][j] = pTemp_mem;
266 181488 : pTemp_mem += IVAS_MAX_NUM_BANDS;
267 181488 : set_f( hFbMixer->prior_mixer[i][j], 0, IVAS_MAX_NUM_BANDS );
268 : }
269 : }
270 : }
271 :
272 67429 : if ( !spar_reconfig_flag )
273 : {
274 18553 : if ( fb_cfg->num_out_chans > 0 )
275 : {
276 : const int16_t *pActive_bins_per_band, *pActive_bins_per_band_abs, *pStart_offset, *pStart_offset_abs;
277 :
278 1777 : num_bands = ivas_get_num_bands( sampling_rate );
279 :
280 1777 : ivas_get_active_bins( &pActive_bins_per_band, &pActive_bins_per_band_abs, &pStart_offset, &pStart_offset_abs, sampling_rate );
281 :
282 1777 : if ( fb_cfg->active_w_mixing != -1 )
283 : {
284 12064 : for ( i = 0; i < num_bands; i++ )
285 : {
286 11130 : if ( ( hFbMixer->pFb->fb_bin_to_band.pFb_bin_to_band[i] = (float *) malloc( sizeof( float ) * pActive_bins_per_band_abs[i] ) ) == NULL )
287 : {
288 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FB mixer encoder" );
289 : }
290 : }
291 : }
292 :
293 1777 : if ( sampling_rate != 48000 )
294 : {
295 : int16_t num_diff_bands, start_diff_band_non48k;
296 :
297 621 : num_diff_bands = MAX_NUM_BANDS_DIFF_NON48K;
298 621 : start_diff_band_non48k = num_bands - num_diff_bands;
299 :
300 621 : hFbMixer->num_diff_bands = num_diff_bands;
301 :
302 2484 : for ( j = start_diff_band_non48k; j < num_bands; j++ )
303 : {
304 1863 : if ( ( hFbMixer->pFb->fb_consts.ppFilterbank_FRs_non48k[0][j] = (float *) malloc( sizeof( float ) * pActive_bins_per_band[j] ) ) == NULL )
305 : {
306 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FB mixer encoder" );
307 : }
308 :
309 1863 : if ( ( hFbMixer->pFb->fb_consts.ppFilterbank_FRs_non48k[1][j] = (float *) malloc( sizeof( float ) * pActive_bins_per_band[j] ) ) == NULL )
310 : {
311 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for FB mixer encoder" );
312 : }
313 : }
314 : }
315 : }
316 : else
317 : {
318 : /* ignore all the deeper filter bank stuff for now */
319 16776 : hFbMixer->num_diff_bands = 0;
320 : }
321 : }
322 :
323 67429 : hFbMixer->fb_cfg = fb_cfg;
324 67429 : set_s( hFbMixer->first_frame, 1, hFbMixer->fb_cfg->num_out_chans );
325 67429 : set_s( hFbMixer->first_frame + hFbMixer->fb_cfg->num_out_chans, 0, IVAS_SPAR_MAX_CH - hFbMixer->fb_cfg->num_out_chans );
326 :
327 67429 : if ( !spar_reconfig_flag )
328 : {
329 18553 : if ( ( error = ivas_filterbank_setup( hFbMixer, sampling_rate ) ) != IVAS_ERR_OK )
330 : {
331 0 : return error;
332 : }
333 : }
334 :
335 67429 : *hFbMixer_out = hFbMixer;
336 :
337 67429 : return error;
338 : }
339 :
340 :
341 : /*-------------------------------------------------------------------------
342 : * ivas_FB_mixer_close()
343 : *
344 : * Deallocate FB mixer handle
345 : *------------------------------------------------------------------------*/
346 :
347 67429 : void ivas_FB_mixer_close(
348 : IVAS_FB_MIXER_HANDLE *hFbMixer_in, /* i/o: FB mixer handle */
349 : const int32_t sampling_rate, /* i : sampling rate in Hz */
350 : const int16_t spar_reconfig_flag /* i : SPAR reconfiguration flag */
351 : )
352 : {
353 : IVAS_FB_MIXER_HANDLE hFbMixer;
354 : IVAS_FB_CFG *fb_cfg;
355 : int16_t i, j, num_bands;
356 : int16_t num_chs_alloc;
357 :
358 67429 : hFbMixer = *hFbMixer_in;
359 67429 : fb_cfg = hFbMixer->fb_cfg;
360 :
361 67429 : if ( fb_cfg->active_w_mixing == -1 )
362 : {
363 37500 : num_chs_alloc = 0;
364 : }
365 29929 : else if ( fb_cfg->active_w_mixing )
366 : {
367 3532 : num_chs_alloc = max( fb_cfg->num_in_chans, fb_cfg->nchan_fb_in );
368 : }
369 : else
370 : {
371 26397 : num_chs_alloc = 1; /* only W channel processed for predicting YZX */
372 : }
373 :
374 67429 : if ( hFbMixer != NULL )
375 : {
376 107954 : for ( i = 0; i < num_chs_alloc; i++ )
377 : {
378 40525 : if ( fb_cfg->num_out_chans > 0 )
379 : {
380 23749 : j = fb_cfg->remix_order[i];
381 :
382 23749 : free( hFbMixer->ppFilterbank_inFR_re[j] );
383 23749 : hFbMixer->ppFilterbank_inFR_re[j] = NULL;
384 :
385 23749 : free( hFbMixer->ppFilterbank_inFR_im[j] );
386 23749 : hFbMixer->ppFilterbank_inFR_im[j] = NULL;
387 : }
388 : }
389 :
390 67429 : if ( fb_cfg->active_w_mixing == -1 )
391 : {
392 37500 : num_chs_alloc = 0;
393 : }
394 : else
395 : {
396 29929 : num_chs_alloc = max( fb_cfg->num_in_chans, fb_cfg->nchan_fb_in );
397 : }
398 :
399 231849 : for ( i = 0; i < num_chs_alloc; i++ )
400 : {
401 164420 : free( hFbMixer->ppFilterbank_prior_input[i] );
402 164420 : hFbMixer->ppFilterbank_prior_input[i] = NULL;
403 : }
404 :
405 67429 : if ( ( fb_cfg->active_w_mixing != -1 ) && ( fb_cfg->num_out_chans > 0 ) )
406 : {
407 13153 : free( hFbMixer->prior_mixer[0][0] );
408 13153 : hFbMixer->prior_mixer[0][0] = NULL;
409 : }
410 :
411 67429 : if ( !spar_reconfig_flag )
412 : {
413 18553 : if ( fb_cfg->num_out_chans > 0 )
414 : {
415 1777 : num_bands = hFbMixer->pFb->filterbank_num_bands;
416 :
417 1777 : if ( fb_cfg->active_w_mixing != -1 )
418 : {
419 12064 : for ( i = 0; i < num_bands; i++ )
420 : {
421 11130 : free( hFbMixer->pFb->fb_bin_to_band.pFb_bin_to_band[i] );
422 11130 : hFbMixer->pFb->fb_bin_to_band.pFb_bin_to_band[i] = NULL;
423 : }
424 : }
425 :
426 1777 : if ( sampling_rate != 48000 )
427 : {
428 : int16_t start_diff_band_non48k;
429 621 : start_diff_band_non48k = num_bands - hFbMixer->num_diff_bands;
430 :
431 2484 : for ( j = start_diff_band_non48k; j < num_bands; j++ )
432 : {
433 1863 : free( hFbMixer->pFb->fb_consts.ppFilterbank_FRs_non48k[0][j] );
434 1863 : hFbMixer->pFb->fb_consts.ppFilterbank_FRs_non48k[0][j] = NULL;
435 :
436 1863 : free( hFbMixer->pFb->fb_consts.ppFilterbank_FRs_non48k[1][j] );
437 1863 : hFbMixer->pFb->fb_consts.ppFilterbank_FRs_non48k[1][j] = NULL;
438 : }
439 : }
440 : }
441 18553 : if ( hFbMixer->pFb != NULL )
442 : {
443 1777 : free( hFbMixer->pFb );
444 1777 : hFbMixer->pFb = NULL;
445 : }
446 : }
447 :
448 67429 : if ( hFbMixer->fb_cfg != NULL )
449 : {
450 67429 : free( hFbMixer->fb_cfg );
451 67429 : hFbMixer->fb_cfg = NULL;
452 : }
453 :
454 67429 : if ( !spar_reconfig_flag )
455 : {
456 18553 : free( hFbMixer );
457 18553 : hFbMixer = NULL;
458 : }
459 : }
460 :
461 67429 : return;
462 : }
463 :
464 :
465 : /*-----------------------------------------------------------------------------------------*
466 : * Function ivas_fb_mixer_pcm_ingest()
467 : *
468 : * PCM ingest block
469 : *-----------------------------------------------------------------------------------------*/
470 :
471 2276011 : void ivas_fb_mixer_pcm_ingest(
472 : IVAS_FB_MIXER_HANDLE hFbMixer, /* i/o: FB mixer handle */
473 : float *pcm_in[], /* i : input audio channels */
474 : float **ppOut_pcm, /* o : output audio channels */
475 : const int16_t frame_len, /* i : frame length */
476 : const int16_t HOA_md_ind[IVAS_SPAR_MAX_CH] )
477 : {
478 : int16_t i;
479 : int16_t num_chs_ingest;
480 2276011 : IVAS_FB_CFG *fb_cfg = hFbMixer->fb_cfg;
481 :
482 2276011 : if ( fb_cfg->active_w_mixing )
483 : {
484 772915 : num_chs_ingest = fb_cfg->num_in_chans;
485 : }
486 : else
487 : {
488 1503096 : num_chs_ingest = 1; /* forward Filterbank MDFT only on W */
489 : }
490 :
491 12200024 : for ( i = 0; i < fb_cfg->num_in_chans; i++ )
492 : {
493 9924013 : mvr2r( &hFbMixer->ppFilterbank_prior_input[i][fb_cfg->prior_input_length - frame_len], ppOut_pcm[i], frame_len );
494 9924013 : mvr2r( pcm_in[HOA_md_ind[i]], &ppOut_pcm[i][frame_len], frame_len );
495 : }
496 :
497 6870767 : for ( i = 0; i < num_chs_ingest; i++ )
498 : {
499 4594756 : ivas_mdft( ppOut_pcm[fb_cfg->remix_order[i]], hFbMixer->ppFilterbank_inFR_re[fb_cfg->remix_order[i]], hFbMixer->ppFilterbank_inFR_im[fb_cfg->remix_order[i]], frame_len << 1, frame_len );
500 : }
501 :
502 2276011 : return;
503 : }
504 :
505 :
506 : /*-----------------------------------------------------------------------------------------*
507 : * Function ivas_fb_mixer_update_prior_input()
508 : *
509 : *
510 : *-----------------------------------------------------------------------------------------*/
511 :
512 11837820 : void ivas_fb_mixer_update_prior_input(
513 : IVAS_FB_MIXER_HANDLE hFbMixer, /* i/o: FB mixer handle */
514 : float *pcm_in[], /* i : input audio channels */
515 : const int16_t length, /* i : length of time slot */
516 : const int16_t nchan_fb_in /* i : number of analysis channels */
517 : )
518 : {
519 : int16_t i;
520 :
521 67190048 : for ( i = 0; i < nchan_fb_in; i++ )
522 : {
523 55352228 : mvr2r( &hFbMixer->ppFilterbank_prior_input[i][length], hFbMixer->ppFilterbank_prior_input[i], hFbMixer->fb_cfg->prior_input_length - length );
524 55352228 : mvr2r( pcm_in[i], &hFbMixer->ppFilterbank_prior_input[i][hFbMixer->fb_cfg->prior_input_length - length], length );
525 : }
526 :
527 11837820 : return;
528 : }
529 :
530 :
531 : /*-----------------------------------------------------------------------------------------*
532 : * Function ivas_fb_mixer_get_windowed_fr()
533 : *
534 : *
535 : *-----------------------------------------------------------------------------------------*/
536 :
537 11819018 : void ivas_fb_mixer_get_windowed_fr(
538 : IVAS_FB_MIXER_HANDLE hFbMixer, /* i/o: FB mixer handle */
539 : float *pcm_in[], /* i : input audio channels */
540 : float *frame_f_real[], /* o : real freq domain values */
541 : float *frame_f_imag[], /* o : imag freq domain values */
542 : const int16_t length, /* i : number of new samples in time slot */
543 : const int16_t mdft_len, /* i : MDFT frame length */
544 : const int16_t nchan_fb_in /* i : number of analysis channels */
545 : )
546 : {
547 : int16_t ch_idx, j, offset, rev_offset;
548 : int16_t n_old_samples;
549 : int16_t n_new_samples;
550 : float fr_in_block[L_FRAME48k * 2];
551 : const float *win_ptr;
552 :
553 11819018 : n_old_samples = min( ( hFbMixer->fb_cfg->prior_input_length - hFbMixer->fb_cfg->windowed_fr_offset ), ( 2 * mdft_len ) );
554 11819018 : n_new_samples = max( 0, 2 * length - n_old_samples );
555 11819018 : offset = (int16_t) ( 2 * mdft_len - length - hFbMixer->ana_window_offset );
556 11819018 : rev_offset = (int16_t) ( 2 * mdft_len - hFbMixer->ana_window_offset );
557 11819018 : set_zero( fr_in_block, offset );
558 :
559 67029310 : for ( ch_idx = 0; ch_idx < nchan_fb_in; ch_idx++ )
560 : {
561 55210292 : mvr2r( &hFbMixer->ppFilterbank_prior_input[ch_idx][offset + hFbMixer->fb_cfg->windowed_fr_offset], &fr_in_block[offset], n_old_samples - offset );
562 55210292 : mvr2r( pcm_in[ch_idx], &fr_in_block[n_old_samples], n_new_samples );
563 55210292 : win_ptr = hFbMixer->pAna_window;
564 :
565 2545419028 : for ( j = offset; j < 2 * mdft_len - length; j++ )
566 : {
567 2490208736 : fr_in_block[j] *= ( *( win_ptr++ ) );
568 : }
569 :
570 2545419028 : for ( j = rev_offset; j < 2 * mdft_len; j++ )
571 : {
572 2490208736 : fr_in_block[j] *= ( *( --win_ptr ) );
573 : }
574 :
575 55210292 : ivas_mdft( fr_in_block, frame_f_real[ch_idx], frame_f_imag[ch_idx], mdft_len << 1, mdft_len );
576 : }
577 :
578 11819018 : return;
579 : }
580 :
581 :
582 : /*-----------------------------------------------------------------------------------------*
583 : * Function ivas_fb_mixer_cross_fading()
584 : *
585 : * FB Mixer cross fading
586 : *-----------------------------------------------------------------------------------------*/
587 :
588 3715572 : void ivas_fb_mixer_cross_fading(
589 : IVAS_FB_MIXER_HANDLE hFbMixer,
590 : float **ppOut_pcm,
591 : float *pMdft_out_old,
592 : float *pMdft_out_new,
593 : const int16_t ch,
594 : const int16_t frame_len,
595 : const int16_t cf_offset )
596 : {
597 : int16_t k, fade_start_offset, fade_end_offset;
598 :
599 3715572 : if ( hFbMixer->first_frame[ch] == 0 )
600 : {
601 3694137 : fade_start_offset = hFbMixer->cross_fade_start_offset;
602 3694137 : fade_end_offset = hFbMixer->cross_fade_end_offset;
603 :
604 3694137 : for ( k = 0; k < fade_start_offset; k++ )
605 : {
606 0 : ppOut_pcm[ch][k] = pMdft_out_old[k + cf_offset];
607 : }
608 :
609 561111993 : for ( k = fade_start_offset; k < fade_end_offset; k++ )
610 : {
611 557417856 : ppOut_pcm[ch][k] = pMdft_out_new[k + cf_offset] * hFbMixer->pFilterbank_cross_fade[k - fade_start_offset] + pMdft_out_old[k + cf_offset] * ( 1.0f - hFbMixer->pFilterbank_cross_fade[k - fade_start_offset] );
612 : }
613 :
614 2233365561 : for ( k = fade_end_offset; k < frame_len; k++ )
615 : {
616 2229671424 : ppOut_pcm[ch][k] = pMdft_out_new[k + cf_offset];
617 : }
618 : }
619 : else
620 : {
621 21435 : hFbMixer->first_frame[ch] = 0;
622 :
623 18715515 : for ( k = 0; k < frame_len; k++ )
624 : {
625 18694080 : ppOut_pcm[ch][k] = pMdft_out_new[k + cf_offset];
626 : }
627 : }
628 :
629 3715572 : return;
630 : }
631 :
632 :
633 : /*-----------------------------------------------------------------------------------------*
634 : * Function ivas_fb_mixer_process()
635 : *
636 : * Filter bank process
637 : *-----------------------------------------------------------------------------------------*/
638 :
639 2276011 : void ivas_fb_mixer_process(
640 : IVAS_FB_MIXER_HANDLE hFbMixer, /* i/o: FB mixer handle */
641 : float ***mixer_mat, /* i : mixer matrix */
642 : float **ppOut_pcm, /* o : output audio channels */
643 : const int16_t frame_len, /* i : frame length in samples */
644 : int16_t in_out_mixer_map[IVAS_MAX_FB_MIXER_OUT_CH][IVAS_MAX_SPAR_FB_MIXER_IN_CH] /* i/o: mixing mapping */
645 : )
646 : {
647 2276011 : ivas_filterbank_t *pFb = hFbMixer->pFb;
648 2276011 : int16_t num_bands = pFb->filterbank_num_bands;
649 : int16_t i, j, k, ch, hist;
650 : const float *pFilterbank_bin_to_band_re;
651 : const float *pFilterbank_bin_to_band_im;
652 : float *pMdft_out[2], *pOut_fr_re, *pOut_fr_im;
653 : float Out_fr_re[L_FRAME48k], Out_fr_im[L_FRAME48k];
654 : float Mdft_out_0[L_FRAME48k * 2], Mdft_out_1[L_FRAME48k * 2];
655 :
656 2276011 : pOut_fr_re = Out_fr_re;
657 2276011 : pOut_fr_im = Out_fr_im;
658 2276011 : pMdft_out[0] = Mdft_out_0;
659 2276011 : pMdft_out[1] = Mdft_out_1;
660 :
661 5991583 : for ( ch = ( hFbMixer->fb_cfg->active_w_mixing == 0 ); ch < hFbMixer->fb_cfg->num_out_chans; ch++ )
662 : {
663 : /* Run a loop of 2 to calculate current frame's filterbank output and prev frame's output */
664 11146716 : for ( hist = 0; hist < 2; hist++ )
665 : {
666 7431144 : set_zero( pOut_fr_re, frame_len );
667 7431144 : set_zero( pOut_fr_im, frame_len );
668 :
669 42075534 : for ( j = 0; j < hFbMixer->fb_cfg->num_in_chans; j++ )
670 : {
671 34644390 : if ( in_out_mixer_map[ch][j] != 0 )
672 : {
673 : float filterbank_mixer_bins_re[L_FRAME48k];
674 : float filterbank_mixer_bins_im[L_FRAME48k];
675 12068634 : float *pFb_inFR_re = hFbMixer->ppFilterbank_inFR_re[j];
676 12068634 : float *pFb_inFR_im = hFbMixer->ppFilterbank_inFR_im[j];
677 :
678 12068634 : set_zero( filterbank_mixer_bins_re, frame_len );
679 12068634 : set_zero( filterbank_mixer_bins_im, frame_len );
680 :
681 153235734 : for ( i = 0; i < num_bands; i++ )
682 : {
683 141167100 : int16_t start_offset = pFb->fb_consts.pFilterbank_bins_start_offset[i];
684 141167100 : int16_t num_bins = pFb->fb_consts.pFilterbank_bins_per_band[i];
685 :
686 141167100 : float mixer_const = hFbMixer->prior_mixer[ch][j][i];
687 :
688 141167100 : pFilterbank_bin_to_band_re = pFb->fb_consts.ppFilterbank_FRs[0][i];
689 141167100 : pFilterbank_bin_to_band_im = pFb->fb_consts.ppFilterbank_FRs[1][i];
690 :
691 42737510370 : for ( k = start_offset; k < num_bins + start_offset; k++ )
692 : {
693 42596343270 : filterbank_mixer_bins_re[k] += *pFilterbank_bin_to_band_re++ * mixer_const;
694 42596343270 : filterbank_mixer_bins_im[k] += *pFilterbank_bin_to_band_im++ * mixer_const;
695 : }
696 141167100 : hFbMixer->prior_mixer[ch][j][i] = mixer_mat[ch][j][i];
697 : }
698 :
699 8869502874 : for ( k = 0; k < frame_len; k++ )
700 : {
701 : float temp_out_re, temp_out_im;
702 :
703 8857434240 : IVAS_CMULT_FLOAT( filterbank_mixer_bins_re[k], filterbank_mixer_bins_im[k], pFb_inFR_re[k],
704 : pFb_inFR_im[k], temp_out_re, temp_out_im );
705 :
706 8857434240 : pOut_fr_re[k] += temp_out_re;
707 8857434240 : pOut_fr_im[k] += temp_out_im;
708 : }
709 : }
710 : }
711 :
712 7431144 : ivas_imdft( pOut_fr_re, pOut_fr_im, pMdft_out[hist], frame_len );
713 : }
714 :
715 3715572 : ivas_fb_mixer_cross_fading( hFbMixer, ppOut_pcm, pMdft_out[0], pMdft_out[1], ch, frame_len, frame_len );
716 : }
717 :
718 2276011 : return;
719 : }
720 :
721 :
722 : /*-----------------------------------------------------------------------------------------*
723 : * Function ivas_fb_mixer_get_in_out_mapping()
724 : *
725 : *
726 : *-----------------------------------------------------------------------------------------*/
727 :
728 2276011 : void ivas_fb_mixer_get_in_out_mapping(
729 : const IVAS_FB_CFG *fb_cfg, /* i : FB config. handle */
730 : int16_t in_out_mixer_map[IVAS_MAX_FB_MIXER_OUT_CH][IVAS_MAX_SPAR_FB_MIXER_IN_CH] /* i/o: mixing mapping */
731 : )
732 : {
733 : int16_t i, j;
734 :
735 2276011 : set_s( (int16_t *) in_out_mixer_map, 0, IVAS_MAX_FB_MIXER_OUT_CH * IVAS_MAX_SPAR_FB_MIXER_IN_CH );
736 :
737 2276011 : if ( fb_cfg->active_w_mixing )
738 : {
739 1545830 : for ( i = 0; i < fb_cfg->num_out_chans; i++ )
740 : {
741 3864575 : for ( j = 0; j < fb_cfg->num_in_chans; j++ )
742 : {
743 3091660 : in_out_mixer_map[i][j] = 1;
744 : }
745 : }
746 : }
747 : else
748 : {
749 1503096 : in_out_mixer_map[0][0] = 1; /* W depends on only W input*/
750 4445753 : for ( i = 1; i < fb_cfg->num_out_chans; i++ )
751 : {
752 2942657 : in_out_mixer_map[i][0] = 1;
753 : }
754 : }
755 :
756 2276011 : return;
757 : }
758 :
759 :
760 : /*-----------------------------------------------------------------------------------------*
761 : * Function ivas_calculate_abs_fr()
762 : *
763 : * Function to calculate number of active bands
764 : *-----------------------------------------------------------------------------------------*/
765 :
766 1777 : static int16_t ivas_calculate_abs_fr(
767 : ivas_filterbank_t *pFb,
768 : const int32_t sampling_rate,
769 : const int16_t alloc_fb_resp )
770 : {
771 : int16_t frame_len;
772 : float ppFilterbank_FRs_s[L_FRAME48k];
773 1777 : int16_t bands = pFb->filterbank_num_bands;
774 1777 : int16_t i, j, num_active_bands = 0;
775 1777 : int16_t idx_short_stride_bin_to_band = 0;
776 :
777 22777 : for ( i = 0; i < bands; i++ )
778 : {
779 21000 : const float *long_mdft_ptr_re = pFb->fb_consts.ppFilterbank_FRs[0][i];
780 21000 : const float *long_mdft_ptr_im = pFb->fb_consts.ppFilterbank_FRs[1][i];
781 21000 : int16_t start_offset = pFb->fb_consts.pFilterbank_bins_start_offset[i];
782 21000 : int16_t num_bins = pFb->fb_consts.pFilterbank_bins_per_band[i];
783 21000 : int16_t short_mdft_start_bin = -1;
784 : float short_stride_pow_spec[MDFT_FB_BANDS_240];
785 21000 : float short_stride_nrg = 0.0f;
786 21000 : float cldfb_nrg = 0.0f;
787 21000 : int16_t short_stride = pFb->fb_bin_to_band.short_stride;
788 21000 : const int16_t num_bins_per_short_stride_bin = ( const int16_t )( ( sampling_rate / FRAMES_PER_SEC ) / short_stride );
789 21000 : const int16_t num_bins_per_cldfb_band = ( const int16_t )( ( sampling_rate / FRAMES_PER_SEC ) / pFb->fb_bin_to_band.num_cldfb_bands );
790 21000 : float short_stride_max_per_spar_band = 1e-9f;
791 :
792 : /*loop over all stored Filter Bank Response MDFT coefficients*/
793 21000 : set_f( short_stride_pow_spec, 0, MDFT_FB_BANDS_240 );
794 6629954 : for ( j = start_offset; j < num_bins + start_offset; j++ )
795 : {
796 : float sq_abs;
797 :
798 : /*calculate bin energy */
799 6608954 : IVAS_CALCULATE_SQ_ABS( *long_mdft_ptr_re, *long_mdft_ptr_im, sq_abs );
800 6608954 : long_mdft_ptr_re++;
801 6608954 : long_mdft_ptr_im++;
802 :
803 : /* accumulate bin energies within a short stride bin */
804 6608954 : short_stride_nrg += sq_abs;
805 6608954 : if ( !( ( j + 1 ) % num_bins_per_short_stride_bin ) )
806 : { /* new short stride bin */
807 1648806 : short_stride_pow_spec[j / num_bins_per_short_stride_bin] = short_stride_nrg; /* energy rather than magnitude works better for covariance weighting*/
808 1648806 : short_stride_max_per_spar_band = max( short_stride_nrg, short_stride_max_per_spar_band ); /*compute highest magnitude per band*/
809 1648806 : short_stride_nrg = 0.0f;
810 : }
811 :
812 : /* accumulate bin energies within a CLDFB band */
813 6608954 : cldfb_nrg += sq_abs;
814 6608954 : if ( !( ( j + 1 ) % num_bins_per_cldfb_band ) )
815 : {
816 408750 : pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j / num_bins_per_cldfb_band][i] = sqrtf( cldfb_nrg );
817 408750 : cldfb_nrg = 0.0f;
818 : }
819 : }
820 :
821 : /*loop over the short MDFT bins*/
822 4361160 : for ( j = 0; j < short_stride; j++ )
823 : {
824 : /* normalize and sparsify the energies */
825 4340160 : short_stride_pow_spec[j] /= short_stride_max_per_spar_band;
826 4340160 : short_stride_pow_spec[j] = max( short_stride_pow_spec[j] - 0.3f, 0.0f ) / 0.7f;
827 4340160 : if ( short_stride_pow_spec[j] > 0.0f )
828 : {
829 384536 : assert( idx_short_stride_bin_to_band < 2 * MDFT_FB_BANDS_240 ); /* array size of p_short_stride_bin_to_band */
830 384536 : if ( short_mdft_start_bin == -1 )
831 : {
832 21000 : short_mdft_start_bin = j;
833 21000 : pFb->fb_bin_to_band.p_short_stride_start_bin_per_band[i] = j;
834 21000 : pFb->fb_bin_to_band.pp_short_stride_bin_to_band[i] = &pFb->fb_bin_to_band.p_short_stride_bin_to_band[idx_short_stride_bin_to_band];
835 : }
836 384536 : pFb->fb_bin_to_band.p_short_stride_bin_to_band[idx_short_stride_bin_to_band++] = short_stride_pow_spec[j];
837 384536 : pFb->fb_bin_to_band.p_short_stride_num_bins_per_band[i]++;
838 : }
839 : }
840 : }
841 :
842 : /*loop over CLDFB bands*/
843 92737 : for ( j = 0; j < pFb->fb_bin_to_band.num_cldfb_bands; j++ )
844 : {
845 90960 : float sum_over_spar_bands = 0.0f;
846 90960 : float max_spar_band_contribution = 0.0f;
847 90960 : int16_t spar_start = 0;
848 90960 : int16_t any_non_zero = 0;
849 :
850 1176000 : for ( i = 0; i < bands; i++ )
851 : {
852 1085040 : sum_over_spar_bands += pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i];
853 1085040 : if ( pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i] > max_spar_band_contribution )
854 : {
855 257720 : max_spar_band_contribution = pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i];
856 257720 : pFb->fb_bin_to_band.p_cldfb_map_to_spar_band[j] = i;
857 : }
858 : }
859 90960 : sum_over_spar_bands = max( sum_over_spar_bands, EPSILON );
860 1176000 : for ( i = 0; i < bands; i++ )
861 : {
862 1085040 : if ( pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i] == 0.0f && !any_non_zero )
863 : {
864 608779 : spar_start++;
865 : }
866 : else
867 : {
868 476261 : any_non_zero = 1;
869 : }
870 1085040 : pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i] /= sum_over_spar_bands;
871 : }
872 90960 : pFb->fb_bin_to_band.p_spar_start_bands[j] = spar_start;
873 : }
874 :
875 1777 : frame_len = (int16_t) ( sampling_rate / FRAMES_PER_SEC );
876 :
877 1777 : set_f( ppFilterbank_FRs_s, 0, frame_len );
878 :
879 : /*Commented logic is for calculating number of active bands, can be removed if not needed */
880 22777 : for ( i = 0; i < bands; i++ )
881 : {
882 21000 : const float *pFilterbank_bin_to_band_re = pFb->fb_consts.ppFilterbank_FRs[0][i];
883 21000 : const float *pFilterbank_bin_to_band_im = pFb->fb_consts.ppFilterbank_FRs[1][i];
884 :
885 21000 : int16_t start_offset = pFb->fb_consts.pFilterbank_bins_start_offset[i];
886 21000 : int16_t num_bins = pFb->fb_consts.pFilterbank_bins_per_band[i];
887 21000 : int16_t idx = 0;
888 21000 : int16_t abs_active_bins = pFb->fb_bin_to_band.pFb_active_bins_per_band[i];
889 21000 : int16_t abs_start_offset = pFb->fb_bin_to_band.pFb_start_bin_per_band[i];
890 :
891 6629954 : for ( j = start_offset; j < num_bins + start_offset; j++ )
892 : {
893 6608954 : float temp = 0;
894 :
895 6608954 : IVAS_CALCULATE_ABS( *pFilterbank_bin_to_band_re, *pFilterbank_bin_to_band_im, temp );
896 :
897 6608954 : pFilterbank_bin_to_band_re++;
898 6608954 : pFilterbank_bin_to_band_im++;
899 :
900 6608954 : temp -= 0.3f;
901 :
902 6608954 : if ( temp < 0.0f )
903 : {
904 4662891 : temp = 0;
905 : }
906 :
907 6608954 : if ( j < ( abs_active_bins + abs_start_offset ) && j >= abs_start_offset && alloc_fb_resp != -1 )
908 : {
909 1102007 : pFb->fb_bin_to_band.pFb_bin_to_band[i][idx++] = temp;
910 : }
911 :
912 6608954 : ppFilterbank_FRs_s[j] += temp;
913 : }
914 : }
915 :
916 1457137 : for ( i = 0; i < frame_len; i++ )
917 : {
918 1455360 : if ( ppFilterbank_FRs_s[i] < 0.1f )
919 : {
920 0 : ppFilterbank_FRs_s[i] = 0.1f;
921 : }
922 : }
923 :
924 1777 : if ( alloc_fb_resp != -1 )
925 : {
926 12064 : for ( j = 0; j < bands; j++ )
927 : {
928 11130 : int16_t abs_active_bins = pFb->fb_bin_to_band.pFb_active_bins_per_band[j];
929 11130 : int16_t abs_start_offset = pFb->fb_bin_to_band.pFb_start_bin_per_band[j];
930 :
931 1113137 : for ( i = 0; i < abs_active_bins; i++ )
932 : {
933 1102007 : pFb->fb_bin_to_band.pFb_bin_to_band[j][i] /= ppFilterbank_FRs_s[i + abs_start_offset];
934 : /*if(pFb->fb_bin_to_band.pFb_bin_to_band[j][i] > 0.5f)
935 : {
936 : num_active_bands = j + 1;
937 : break;
938 : }*/
939 : }
940 : }
941 : }
942 :
943 1777 : return num_active_bands;
944 : }
945 :
946 :
947 : /*-----------------------------------------------------------------------------------------*
948 : * Function ivas_get_active_bins()
949 : *
950 : *
951 : *-----------------------------------------------------------------------------------------*/
952 :
953 3554 : static void ivas_get_active_bins(
954 : const int16_t **pActive_bins,
955 : const int16_t **pActive_bins_abs,
956 : const int16_t **pStart_offset,
957 : const int16_t **pStart_offset_abs,
958 : const int32_t sampling_rate )
959 : {
960 : int16_t sr_idx;
961 :
962 3554 : if ( sampling_rate == 32000 )
963 : {
964 918 : sr_idx = 1;
965 : }
966 2636 : else if ( sampling_rate == 16000 )
967 : {
968 324 : sr_idx = 2;
969 : }
970 : else
971 : {
972 2312 : sr_idx = 0;
973 : }
974 :
975 3554 : *pActive_bins_abs = ivas_fb_abs_bins_per_band_12band_1ms[sr_idx];
976 3554 : *pActive_bins = ivas_fb_bins_per_band_12band_1ms[sr_idx];
977 3554 : *pStart_offset_abs = ivas_fb_abs_bins_start_offset_12band_1ms[sr_idx];
978 3554 : *pStart_offset = ivas_fb_bins_start_offset_12band_1ms[sr_idx];
979 :
980 3554 : return;
981 : }
982 :
983 :
984 : /*-----------------------------------------------------------------------------------------*
985 : * Function ivas_filterbank_setup()
986 : *
987 : * Filterbank setup, initialization
988 : *-----------------------------------------------------------------------------------------*/
989 :
990 18553 : static ivas_error ivas_filterbank_setup(
991 : IVAS_FB_MIXER_HANDLE hFbMixer,
992 : const int32_t sampling_rate )
993 : {
994 : int16_t i, j;
995 : const float *pAll_fb_fr[2];
996 18553 : const int16_t *pAll_bins_start_offset = NULL;
997 18553 : const int16_t *pAll_bins_per_band = NULL;
998 18553 : const int16_t *pAll_bins_start_offset_abs = NULL;
999 18553 : const int16_t *pAll_bins_per_band_abs = NULL;
1000 18553 : const int16_t *pAll_bins_per_band_48k = NULL;
1001 : ivas_error error;
1002 18553 : IVAS_FB_CFG *pCfg = hFbMixer->fb_cfg;
1003 :
1004 18553 : error = IVAS_ERR_OK;
1005 :
1006 18553 : if ( pCfg->num_out_chans > 0 )
1007 : {
1008 1777 : hFbMixer->pFb->filterbank_num_bands = ivas_get_num_bands( sampling_rate );
1009 :
1010 1777 : ivas_get_active_bins( &pAll_bins_per_band, &pAll_bins_per_band_abs, &pAll_bins_start_offset, &pAll_bins_start_offset_abs, sampling_rate );
1011 :
1012 1777 : if ( pCfg->fb_latency == NS2SA( sampling_rate, DELAY_FB_1_NS ) )
1013 : {
1014 1777 : pAll_fb_fr[0] = ivas_fb_fr_12band_1ms_re;
1015 1777 : pAll_fb_fr[1] = ivas_fb_fr_12band_1ms_im;
1016 1777 : pAll_bins_per_band_48k = ivas_fb_bins_per_band_12band_1ms[0];
1017 : }
1018 : else
1019 : {
1020 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong FB in ivas_filterbank_setup()!" );
1021 : }
1022 : }
1023 :
1024 18553 : hFbMixer->cross_fade_end_offset = pCfg->fade_len + pCfg->pcm_offset;
1025 18553 : hFbMixer->cross_fade_start_offset = hFbMixer->cross_fade_end_offset - pCfg->fade_len;
1026 18553 : hFbMixer->ana_window_offset = pCfg->fb_latency + pCfg->pcm_offset;
1027 :
1028 18553 : if ( ( error = ivas_fb_mixer_get_window( pCfg->fb_latency, sampling_rate, &( hFbMixer->pAna_window ) ) ) != IVAS_ERR_OK )
1029 : {
1030 0 : return error;
1031 : }
1032 :
1033 18553 : if ( ( error = ivas_fb_mixer_get_window( pCfg->fade_len, sampling_rate, &( hFbMixer->pFilterbank_cross_fade ) ) ) != IVAS_ERR_OK )
1034 : {
1035 0 : return error;
1036 : }
1037 :
1038 18553 : if ( pCfg->num_out_chans > 0 )
1039 : {
1040 1777 : ivas_filterbank_t *pFb = hFbMixer->pFb;
1041 1777 : int16_t offset = 0;
1042 :
1043 1777 : pFb->fb_consts.pFilterbank_bins_per_band = pAll_bins_per_band;
1044 1777 : pFb->fb_consts.pFilterbank_bins_start_offset = pAll_bins_start_offset;
1045 1777 : pFb->fb_bin_to_band.pFb_active_bins_per_band = pAll_bins_per_band_abs;
1046 1777 : pFb->fb_bin_to_band.pFb_start_bin_per_band = pAll_bins_start_offset_abs;
1047 :
1048 : /* Initialization for short stride Parameter calculation and SPAR CLDFB reconstruction */
1049 1777 : pFb->fb_bin_to_band.num_cldfb_bands = (int16_t) ( sampling_rate * INV_CLDFB_BANDWIDTH + 0.5f );
1050 : /*pFb->fb_bin_to_band.cldfb_stride = ( int16_t )( ( sampling_rate / FRAMES_PER_SEC ) / CLDFB_NO_COL_MAX );*/ /* equals num_cldfb_bands*/
1051 1777 : pFb->fb_bin_to_band.short_stride = (int16_t) ( ( sampling_rate / FRAMES_PER_SEC ) / 4 );
1052 1777 : set_f( pFb->fb_bin_to_band.p_short_stride_bin_to_band, 0.0f, 2 * MDFT_FB_BANDS_240 );
1053 1777 : set_s( pFb->fb_bin_to_band.p_cldfb_map_to_spar_band, 0, CLDFB_NO_CHANNELS_MAX );
1054 1777 : set_s( pFb->fb_bin_to_band.p_spar_start_bands, 0, CLDFB_NO_CHANNELS_MAX );
1055 :
1056 37317 : for ( j = 0; j < IVAS_MAX_NUM_FB_BANDS; j++ )
1057 : {
1058 35540 : pFb->fb_bin_to_band.p_short_stride_num_bins_per_band[j] = 0; /* aka num_active_bins per SPAR band */
1059 35540 : pFb->fb_bin_to_band.p_short_stride_start_bin_per_band[j] = 0; /* first considered bin index per SPAR band */
1060 35540 : pFb->fb_bin_to_band.pp_short_stride_bin_to_band[j] = NULL;
1061 2167940 : for ( i = 0; i < CLDFB_NO_CHANNELS_MAX; i++ )
1062 : {
1063 2132400 : pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[i][j] = 0.0f;
1064 : }
1065 : }
1066 1777 : if ( sampling_rate == 48000 )
1067 : {
1068 15028 : for ( j = 0; j < pFb->filterbank_num_bands; j++ )
1069 : {
1070 13872 : pFb->fb_consts.ppFilterbank_FRs[0][j] = &pAll_fb_fr[0][offset];
1071 13872 : pFb->fb_consts.ppFilterbank_FRs[1][j] = &pAll_fb_fr[1][offset];
1072 13872 : offset += pFb->fb_consts.pFilterbank_bins_per_band[j];
1073 : }
1074 :
1075 1156 : ivas_calculate_abs_fr( pFb, sampling_rate, pCfg->active_w_mixing );
1076 : }
1077 : else
1078 : {
1079 : float *ppFilterbank_FRs_re_temp[MAX_NUM_BANDS_DIFF_NON48K];
1080 : float *ppFilterbank_FRs_im_temp[MAX_NUM_BANDS_DIFF_NON48K];
1081 : int16_t active_bins_temp[MAX_NUM_BANDS_DIFF_NON48K];
1082 : int16_t start_offset_temp[MAX_NUM_BANDS_DIFF_NON48K];
1083 : int16_t num_diff_bands, start_diff_band_non48k;
1084 :
1085 621 : num_diff_bands = MAX_NUM_BANDS_DIFF_NON48K;
1086 621 : start_diff_band_non48k = pFb->filterbank_num_bands - num_diff_bands;
1087 :
1088 621 : pFb->fb_consts.pFilterbank_bins_per_band = pAll_bins_per_band;
1089 621 : pFb->fb_consts.pFilterbank_bins_start_offset = pAll_bins_start_offset;
1090 :
1091 7749 : for ( j = 0; j < pFb->filterbank_num_bands; j++ )
1092 : {
1093 7128 : int16_t num_active_bins = pFb->fb_consts.pFilterbank_bins_per_band[j];
1094 :
1095 7128 : if ( j < start_diff_band_non48k )
1096 : {
1097 5265 : pFb->fb_consts.ppFilterbank_FRs[0][j] = &pAll_fb_fr[0][offset];
1098 5265 : pFb->fb_consts.ppFilterbank_FRs[1][j] = &pAll_fb_fr[1][offset];
1099 : }
1100 : else
1101 : {
1102 1863 : mvr2r( &pAll_fb_fr[0][offset], pFb->fb_consts.ppFilterbank_FRs_non48k[0][j], num_active_bins );
1103 1863 : mvr2r( &pAll_fb_fr[1][offset], pFb->fb_consts.ppFilterbank_FRs_non48k[1][j], num_active_bins );
1104 : }
1105 :
1106 7128 : offset += pAll_bins_per_band_48k[j];
1107 : }
1108 :
1109 2484 : for ( j = start_diff_band_non48k; j < pFb->filterbank_num_bands; j++ )
1110 : {
1111 1863 : ppFilterbank_FRs_re_temp[j - start_diff_band_non48k] = pFb->fb_consts.ppFilterbank_FRs_non48k[0][j];
1112 1863 : ppFilterbank_FRs_im_temp[j - start_diff_band_non48k] = pFb->fb_consts.ppFilterbank_FRs_non48k[1][j];
1113 1863 : active_bins_temp[j - start_diff_band_non48k] = pFb->fb_consts.pFilterbank_bins_per_band[j];
1114 1863 : start_offset_temp[j - start_diff_band_non48k] = pFb->fb_consts.pFilterbank_bins_start_offset[j];
1115 : }
1116 :
1117 621 : ivas_get_ld_fb_resp( ppFilterbank_FRs_re_temp, ppFilterbank_FRs_im_temp, ppFilterbank_FRs_re_temp, ppFilterbank_FRs_im_temp,
1118 621 : active_bins_temp, start_offset_temp, num_diff_bands, pCfg->fb_latency, sampling_rate );
1119 :
1120 2484 : for ( j = start_diff_band_non48k; j < pFb->filterbank_num_bands; j++ )
1121 : {
1122 1863 : pFb->fb_consts.ppFilterbank_FRs[0][j] = (const float *) pFb->fb_consts.ppFilterbank_FRs_non48k[0][j];
1123 1863 : pFb->fb_consts.ppFilterbank_FRs[1][j] = (const float *) pFb->fb_consts.ppFilterbank_FRs_non48k[1][j];
1124 : }
1125 :
1126 621 : ivas_calculate_abs_fr( pFb, sampling_rate, pCfg->active_w_mixing );
1127 : }
1128 : }
1129 :
1130 18553 : return error;
1131 : }
1132 :
1133 :
1134 : /*-----------------------------------------------------------------------------------------*
1135 : * Function ivas_fb_mixer_get_window()
1136 : *
1137 : *
1138 : *-----------------------------------------------------------------------------------------*/
1139 :
1140 37106 : static ivas_error ivas_fb_mixer_get_window(
1141 : const int16_t fade_len, /* i : window fading length in samples */
1142 : const int32_t sampling_rate, /* i : sampling rate */
1143 : const float **pWindow /* o : pointer to the window coefficents */
1144 : )
1145 : {
1146 : ivas_error error;
1147 :
1148 37106 : error = IVAS_ERR_OK;
1149 :
1150 37106 : if ( fade_len == NS2SA( sampling_rate, DELAY_FB_4_NS ) )
1151 : {
1152 4846 : switch ( sampling_rate )
1153 : {
1154 4225 : case 48000:
1155 4225 : *pWindow = ivas_fb_cf_4ms_48k;
1156 4225 : break;
1157 459 : case 32000:
1158 459 : *pWindow = ivas_fb_cf_4ms_32k;
1159 459 : break;
1160 162 : case 16000:
1161 162 : *pWindow = ivas_fb_cf_4ms_16k;
1162 162 : break;
1163 0 : default:
1164 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Unsupported Sampling frequency!" );
1165 : }
1166 : }
1167 32260 : else if ( fade_len == NS2SA( sampling_rate, DELAY_FB_1_NS ) )
1168 : {
1169 32260 : switch ( sampling_rate )
1170 : {
1171 29465 : case 48000:
1172 29465 : *pWindow = ivas_fb_cf_1ms_48k;
1173 29465 : break;
1174 2633 : case 32000:
1175 2633 : *pWindow = ivas_fb_cf_1ms_32k;
1176 2633 : break;
1177 162 : case 16000:
1178 162 : *pWindow = ivas_fb_cf_1ms_16k;
1179 162 : break;
1180 0 : default:
1181 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Unsupported Sampling frequency!" );
1182 : }
1183 : }
1184 : else
1185 : {
1186 0 : IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Error: Incorrect FB window fade len (%f ms)\n", ( fade_len / 1000000.f ) );
1187 : }
1188 :
1189 37106 : return error;
1190 : }
1191 :
1192 :
1193 621 : static const float *ivas_get_cheby_ramp(
1194 : const int16_t delay )
1195 : {
1196 621 : const float *pCheby = NULL;
1197 :
1198 621 : switch ( delay )
1199 : {
1200 459 : case IVAS_FB_1MS_32K_SAMP:
1201 459 : pCheby = ivas_fb_resp_cheby_ramp_32del;
1202 459 : break;
1203 162 : case IVAS_FB_1MS_16K_SAMP:
1204 162 : pCheby = ivas_fb_resp_cheby_ramp_16del;
1205 162 : break;
1206 0 : default:
1207 0 : assert( !"Unsupported cheby ramp length!" );
1208 : }
1209 :
1210 621 : return pCheby;
1211 : }
1212 :
1213 :
1214 : /*get first half of the hanning window*/
1215 621 : static void ivas_get_hanning_win(
1216 : const int16_t len,
1217 : float *pH_win )
1218 : {
1219 : int16_t i;
1220 :
1221 346221 : for ( i = 0; i < len; i++ )
1222 : {
1223 345600 : pH_win[i] = 0.5f * ( 1.0f - cosf( ( PI2 * ( i + 1 ) ) / ( ( 2 * len ) + 1 ) ) );
1224 : }
1225 :
1226 621 : return;
1227 : }
1228 :
1229 :
1230 621 : static void ivas_get_ld_fb_resp(
1231 : float **ppIdeal_FRs_re,
1232 : float **ppIdeal_FRs_im,
1233 : float **ppNew_FRs_re,
1234 : float **ppNew_FRs_im,
1235 : const int16_t *pActive_bins,
1236 : const int16_t *pStart_offset,
1237 : const int16_t num_bands,
1238 : const int16_t delay,
1239 : const int32_t sampling_rate )
1240 : {
1241 : int16_t b, s, frame_len;
1242 : const float *pCheby;
1243 :
1244 : /*common scratch buffers for computing impulse/frequency responses,
1245 : pre-ring, post-ring and circular shifted outputs to optimize stack*/
1246 : float scratch1[L_FRAME32k * 2];
1247 : float scratch2[L_FRAME32k * 2];
1248 : float han_win[L_FRAME32k];
1249 :
1250 621 : frame_len = (int16_t) ( sampling_rate / FRAMES_PER_SEC );
1251 621 : pCheby = ivas_get_cheby_ramp( delay );
1252 621 : b = 0;
1253 621 : s = 0;
1254 :
1255 621 : assert( sampling_rate == 32000 || sampling_rate == 16000 );
1256 :
1257 621 : ivas_get_hanning_win( frame_len, han_win );
1258 2484 : for ( b = 0; b < num_bands; b++ )
1259 : {
1260 1863 : set_f( scratch2, 0, frame_len << 1 );
1261 1863 : mvr2r( ppIdeal_FRs_re[b], &scratch2[pStart_offset[b]], pActive_bins[b] );
1262 1863 : mvr2r( ppIdeal_FRs_im[b], &scratch2[frame_len + pStart_offset[b]], pActive_bins[b] );
1263 1863 : ivas_imdft( scratch2, &scratch2[frame_len], scratch1, frame_len );
1264 :
1265 : /*apply circular shift and hanning window*/
1266 :
1267 1038663 : for ( s = delay; s < frame_len + delay; s++ )
1268 : {
1269 1036800 : scratch2[s - delay] = scratch1[s] * ( 1 - han_win[s - delay] );
1270 : }
1271 :
1272 986823 : for ( ; s < 2 * frame_len; s++ )
1273 : {
1274 984960 : scratch2[s - delay] = scratch1[s] * han_win[s - ( frame_len + delay )];
1275 : }
1276 :
1277 53703 : for ( s = 0; s < delay; s++ )
1278 : {
1279 51840 : scratch2[2 * frame_len - delay + s] = -1 * scratch1[s] * han_win[frame_len - delay + s];
1280 : }
1281 :
1282 : /*apply heavy/cheby ramp window and compute pre ring*/
1283 :
1284 55566 : for ( s = 0; s < delay + 1; s++ )
1285 : {
1286 53703 : scratch1[s] = scratch2[s] * pCheby[delay - s];
1287 : }
1288 :
1289 984960 : for ( ; s < frame_len; s++ )
1290 : {
1291 983097 : scratch1[s] = 0;
1292 : }
1293 :
1294 986823 : for ( ; s < 2 * frame_len - delay; s++ )
1295 : {
1296 984960 : scratch1[s] = scratch2[s];
1297 : }
1298 :
1299 53703 : for ( ; s < 2 * frame_len; s++ )
1300 : {
1301 51840 : scratch1[s] = scratch2[s] * ( 1 - pCheby[s - ( 2 * frame_len - delay )] );
1302 : }
1303 :
1304 : /*IR - pre ring + post ring*/
1305 2073600 : for ( s = 1; s < 2 * frame_len; s++ )
1306 : {
1307 2071737 : scratch2[s] = scratch2[s] /*pre ring*/ - scratch1[s] /*post ring*/ - scratch1[( 2 * frame_len ) - s];
1308 : }
1309 :
1310 2023623 : for ( s = 0; s < 2 * frame_len - delay; s++ )
1311 : {
1312 2021760 : scratch1[s + delay] = scratch2[s];
1313 : }
1314 :
1315 53703 : for ( ; s < 2 * frame_len; s++ )
1316 : {
1317 51840 : scratch1[s - ( 2 * frame_len - delay )] = -1 * ( scratch2[s] );
1318 : }
1319 :
1320 : /* apply final window*/
1321 53703 : for ( s = 0; s < delay; s++ )
1322 : {
1323 51840 : scratch1[s] = scratch1[s] * sinf( ( EVS_PI * ( s + 1 ) ) / ( 2 * delay ) );
1324 : }
1325 :
1326 936846 : for ( s = 2 * delay; s < frame_len + 1; s++ )
1327 : {
1328 934983 : scratch1[s] = scratch1[s] * sinf( ( EVS_PI * ( frame_len - s ) ) / ( 2 * ( frame_len + 1 - ( 2 * delay ) ) ) );
1329 : }
1330 :
1331 1036800 : for ( ; s < 2 * frame_len; s++ )
1332 : {
1333 1034937 : scratch1[s] = 0;
1334 : }
1335 :
1336 : /*compute frequency response*/
1337 1863 : ivas_mdft( scratch1, scratch2, &scratch2[frame_len], frame_len << 1, frame_len );
1338 :
1339 1863 : mvr2r( &scratch2[pStart_offset[b]], ppNew_FRs_re[b], pActive_bins[b] );
1340 1863 : mvr2r( &scratch2[frame_len + pStart_offset[b]], ppNew_FRs_im[b], pActive_bins[b] );
1341 : }
1342 :
1343 621 : return;
1344 : }
|