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 6725015 : 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 6725015 : assert( bwidth > 0 ); /*NB BW is not supported*/
70 6725015 : num_active_bands = ivas_num_active_bands[bwidth - 1];
71 :
72 6725015 : 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 98068 : static int16_t ivas_get_num_bands(
83 : const int32_t sampling_rate )
84 : {
85 98068 : int16_t bwidth = ivas_get_bw_idx_from_sample_rate( sampling_rate );
86 98068 : int16_t num_active_bands = ivas_get_num_bands_from_bw_idx( bwidth );
87 :
88 98068 : 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 134374 : 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 134374 : 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 134374 : pFb_cfg->num_in_chans = num_in_chans;
116 134374 : pFb_cfg->num_out_chans = num_out_chans;
117 134374 : pFb_cfg->nchan_fb_in = nchan_fb_in;
118 :
119 134374 : pFb_cfg->pcm_offset = 0; /* note: in SPAR decoder, this parameter is overwritten later */
120 134374 : pFb_cfg->active_w_mixing = active_w_mixing;
121 134374 : pFb_cfg->windowed_fr_offset = 0;
122 :
123 134374 : if ( ivas_format == ISM_FORMAT )
124 : {
125 2571 : pFb_cfg->fb_latency = NS2SA( sampling_rate, DELAY_FB_4_NS );
126 2571 : pFb_cfg->fade_len = NS2SA( sampling_rate, DELAY_FB_4_NS );
127 2571 : pFb_cfg->prior_input_length = NS2SA( sampling_rate, DELAY_DIRAC_ENC_CMP_NS_PARAM_ISM ) + NS2SA( sampling_rate, DIRAC_SLOT_ENC_NS );
128 : }
129 131803 : else if ( ivas_format == SBA_FORMAT )
130 : {
131 114316 : pFb_cfg->fb_latency = NS2SA( sampling_rate, DELAY_FB_1_NS );
132 :
133 114316 : pFb_cfg->fade_len = NS2SA( sampling_rate, DELAY_FB_4_NS );
134 114316 : pFb_cfg->prior_input_length = NS2SA( sampling_rate, FRAME_SIZE_NS );
135 114316 : 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 17487 : else if ( ivas_format == MASA_FORMAT )
138 : {
139 12605 : pFb_cfg->fb_latency = NS2SA( sampling_rate, DELAY_FB_1_NS );
140 12605 : pFb_cfg->fade_len = NS2SA( sampling_rate, DELAY_FB_1_NS );
141 12605 : pFb_cfg->prior_input_length = NS2SA( sampling_rate, DELAY_DIRAC_ENC_CMP_NS ) + NS2SA( sampling_rate, DIRAC_SLOT_ENC_NS );
142 : }
143 4882 : else if ( ivas_format == MC_FORMAT )
144 : {
145 4882 : pFb_cfg->fb_latency = NS2SA( sampling_rate, DELAY_FB_1_NS );
146 4882 : pFb_cfg->fade_len = NS2SA( sampling_rate, DELAY_FB_1_NS );
147 4882 : pFb_cfg->prior_input_length = NS2SA( sampling_rate, DELAY_DIRAC_ENC_CMP_NS ) + NS2SA( sampling_rate, PARAM_MC_SLOT_ENC_NS );
148 : }
149 :
150 134374 : *pFb_cfg_out = pFb_cfg;
151 :
152 134374 : return IVAS_ERR_OK;
153 : }
154 :
155 :
156 : /*-------------------------------------------------------------------------
157 : * ivas_FB_mixer_open()
158 : *
159 : * Allocate and initialize FB mixer handle
160 : *------------------------------------------------------------------------*/
161 :
162 134374 : 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 134374 : error = IVAS_ERR_OK;
175 :
176 134374 : frame_len = (int16_t) ( sampling_rate / FRAMES_PER_SEC );
177 :
178 134374 : hFbMixer = *hFbMixer_out;
179 :
180 134374 : if ( !spar_reconfig_flag )
181 : {
182 68348 : 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 68348 : if ( fb_cfg->num_out_chans > 0 )
188 : {
189 49034 : 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 19314 : hFbMixer->pFb = NULL;
197 : }
198 : }
199 :
200 134374 : if ( fb_cfg->active_w_mixing == -1 )
201 : {
202 96540 : num_chs_alloc = 0;
203 : }
204 37834 : else if ( fb_cfg->active_w_mixing )
205 : {
206 5125 : num_chs_alloc = max( fb_cfg->num_in_chans, fb_cfg->nchan_fb_in );
207 : }
208 : else
209 : {
210 32709 : num_chs_alloc = 1; /* only W channel processed for predicting YZX */
211 : }
212 :
213 187583 : for ( i = 0; i < num_chs_alloc; i++ )
214 : {
215 53209 : if ( fb_cfg->num_out_chans == 0 )
216 : {
217 19314 : hFbMixer->ppFilterbank_inFR_re[i] = NULL;
218 19314 : hFbMixer->ppFilterbank_inFR_im[i] = NULL;
219 : }
220 : else
221 : {
222 33895 : j = fb_cfg->remix_order[i];
223 :
224 33895 : 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 33895 : 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 134374 : if ( fb_cfg->active_w_mixing == -1 )
237 : {
238 96540 : num_chs_alloc = 0;
239 : }
240 : else
241 : {
242 37834 : num_chs_alloc = max( fb_cfg->num_in_chans, fb_cfg->nchan_fb_in );
243 : }
244 :
245 338781 : for ( i = 0; i < num_chs_alloc; i++ )
246 : {
247 204407 : 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 204407 : set_f( hFbMixer->ppFilterbank_prior_input[i], 0, fb_cfg->prior_input_length );
252 : }
253 :
254 134374 : if ( ( fb_cfg->active_w_mixing != -1 ) && ( fb_cfg->num_out_chans > 0 ) )
255 : {
256 : float *pTemp_mem;
257 18520 : 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 65092 : for ( i = 0; i < fb_cfg->num_out_chans; i++ )
262 : {
263 301688 : for ( j = 0; j < fb_cfg->num_in_chans; j++ )
264 : {
265 255116 : hFbMixer->prior_mixer[i][j] = pTemp_mem;
266 255116 : pTemp_mem += IVAS_MAX_NUM_BANDS;
267 255116 : set_f( hFbMixer->prior_mixer[i][j], 0, IVAS_MAX_NUM_BANDS );
268 : }
269 : }
270 : }
271 :
272 134374 : if ( !spar_reconfig_flag )
273 : {
274 68348 : 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 49034 : num_bands = ivas_get_num_bands( sampling_rate );
279 :
280 49034 : ivas_get_active_bins( &pActive_bins_per_band, &pActive_bins_per_band_abs, &pStart_offset, &pStart_offset_abs, sampling_rate );
281 :
282 49034 : if ( fb_cfg->active_w_mixing != -1 )
283 : {
284 45013 : for ( i = 0; i < num_bands; i++ )
285 : {
286 41464 : 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 49034 : if ( sampling_rate != 48000 )
294 : {
295 : int16_t num_diff_bands, start_diff_band_non48k;
296 :
297 31421 : num_diff_bands = MAX_NUM_BANDS_DIFF_NON48K;
298 31421 : start_diff_band_non48k = num_bands - num_diff_bands;
299 :
300 31421 : hFbMixer->num_diff_bands = num_diff_bands;
301 :
302 125684 : for ( j = start_diff_band_non48k; j < num_bands; j++ )
303 : {
304 94263 : 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 94263 : 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 19314 : hFbMixer->num_diff_bands = 0;
320 : }
321 : }
322 :
323 134374 : hFbMixer->fb_cfg = fb_cfg;
324 134374 : set_s( hFbMixer->first_frame, 1, hFbMixer->fb_cfg->num_out_chans );
325 134374 : set_s( hFbMixer->first_frame + hFbMixer->fb_cfg->num_out_chans, 0, IVAS_SPAR_MAX_CH - hFbMixer->fb_cfg->num_out_chans );
326 :
327 134374 : if ( !spar_reconfig_flag )
328 : {
329 68348 : if ( ( error = ivas_filterbank_setup( hFbMixer, sampling_rate ) ) != IVAS_ERR_OK )
330 : {
331 0 : return error;
332 : }
333 : }
334 :
335 134374 : *hFbMixer_out = hFbMixer;
336 :
337 134374 : return error;
338 : }
339 :
340 :
341 : /*-------------------------------------------------------------------------
342 : * ivas_FB_mixer_close()
343 : *
344 : * Deallocate FB mixer handle
345 : *------------------------------------------------------------------------*/
346 :
347 134374 : 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 134374 : hFbMixer = *hFbMixer_in;
359 134374 : fb_cfg = hFbMixer->fb_cfg;
360 :
361 134374 : if ( fb_cfg->active_w_mixing == -1 )
362 : {
363 96540 : num_chs_alloc = 0;
364 : }
365 37834 : else if ( fb_cfg->active_w_mixing )
366 : {
367 5125 : num_chs_alloc = max( fb_cfg->num_in_chans, fb_cfg->nchan_fb_in );
368 : }
369 : else
370 : {
371 32709 : num_chs_alloc = 1; /* only W channel processed for predicting YZX */
372 : }
373 :
374 134374 : if ( hFbMixer != NULL )
375 : {
376 187583 : for ( i = 0; i < num_chs_alloc; i++ )
377 : {
378 53209 : if ( fb_cfg->num_out_chans > 0 )
379 : {
380 33895 : j = fb_cfg->remix_order[i];
381 :
382 33895 : free( hFbMixer->ppFilterbank_inFR_re[j] );
383 33895 : hFbMixer->ppFilterbank_inFR_re[j] = NULL;
384 :
385 33895 : free( hFbMixer->ppFilterbank_inFR_im[j] );
386 33895 : hFbMixer->ppFilterbank_inFR_im[j] = NULL;
387 : }
388 : }
389 :
390 134374 : if ( fb_cfg->active_w_mixing == -1 )
391 : {
392 96540 : num_chs_alloc = 0;
393 : }
394 : else
395 : {
396 37834 : num_chs_alloc = max( fb_cfg->num_in_chans, fb_cfg->nchan_fb_in );
397 : }
398 :
399 338781 : for ( i = 0; i < num_chs_alloc; i++ )
400 : {
401 204407 : free( hFbMixer->ppFilterbank_prior_input[i] );
402 204407 : hFbMixer->ppFilterbank_prior_input[i] = NULL;
403 : }
404 :
405 134374 : if ( ( fb_cfg->active_w_mixing != -1 ) && ( fb_cfg->num_out_chans > 0 ) )
406 : {
407 18520 : free( hFbMixer->prior_mixer[0][0] );
408 18520 : hFbMixer->prior_mixer[0][0] = NULL;
409 : }
410 :
411 134374 : if ( !spar_reconfig_flag )
412 : {
413 68348 : if ( fb_cfg->num_out_chans > 0 )
414 : {
415 49034 : num_bands = hFbMixer->pFb->filterbank_num_bands;
416 :
417 49034 : if ( fb_cfg->active_w_mixing != -1 )
418 : {
419 45013 : for ( i = 0; i < num_bands; i++ )
420 : {
421 41464 : free( hFbMixer->pFb->fb_bin_to_band.pFb_bin_to_band[i] );
422 41464 : hFbMixer->pFb->fb_bin_to_band.pFb_bin_to_band[i] = NULL;
423 : }
424 : }
425 :
426 49034 : if ( sampling_rate != 48000 )
427 : {
428 : int16_t start_diff_band_non48k;
429 31421 : start_diff_band_non48k = num_bands - hFbMixer->num_diff_bands;
430 :
431 125684 : for ( j = start_diff_band_non48k; j < num_bands; j++ )
432 : {
433 94263 : free( hFbMixer->pFb->fb_consts.ppFilterbank_FRs_non48k[0][j] );
434 94263 : hFbMixer->pFb->fb_consts.ppFilterbank_FRs_non48k[0][j] = NULL;
435 :
436 94263 : free( hFbMixer->pFb->fb_consts.ppFilterbank_FRs_non48k[1][j] );
437 94263 : hFbMixer->pFb->fb_consts.ppFilterbank_FRs_non48k[1][j] = NULL;
438 : }
439 : }
440 : }
441 68348 : if ( hFbMixer->pFb != NULL )
442 : {
443 49034 : free( hFbMixer->pFb );
444 49034 : hFbMixer->pFb = NULL;
445 : }
446 : }
447 :
448 134374 : if ( hFbMixer->fb_cfg != NULL )
449 : {
450 134374 : free( hFbMixer->fb_cfg );
451 134374 : hFbMixer->fb_cfg = NULL;
452 : }
453 :
454 134374 : if ( !spar_reconfig_flag )
455 : {
456 68348 : free( hFbMixer );
457 68348 : hFbMixer = NULL;
458 : }
459 : }
460 :
461 134374 : return;
462 : }
463 :
464 :
465 : /*-----------------------------------------------------------------------------------------*
466 : * Function ivas_fb_mixer_pcm_ingest()
467 : *
468 : * PCM ingest block
469 : *-----------------------------------------------------------------------------------------*/
470 :
471 2728986 : 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 2728986 : IVAS_FB_CFG *fb_cfg = hFbMixer->fb_cfg;
481 :
482 2728986 : if ( fb_cfg->active_w_mixing )
483 : {
484 867692 : num_chs_ingest = fb_cfg->num_in_chans;
485 : }
486 : else
487 : {
488 1861294 : num_chs_ingest = 1; /* forward Filterbank MDFT only on W */
489 : }
490 :
491 14898769 : for ( i = 0; i < fb_cfg->num_in_chans; i++ )
492 : {
493 12169783 : mvr2r( &hFbMixer->ppFilterbank_prior_input[i][fb_cfg->prior_input_length - frame_len], ppOut_pcm[i], frame_len );
494 12169783 : mvr2r( pcm_in[HOA_md_ind[i]], &ppOut_pcm[i][frame_len], frame_len );
495 : }
496 :
497 8061048 : for ( i = 0; i < num_chs_ingest; i++ )
498 : {
499 5332062 : 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 2728986 : return;
503 : }
504 :
505 :
506 : /*-----------------------------------------------------------------------------------------*
507 : * Function ivas_fb_mixer_update_prior_input()
508 : *
509 : *
510 : *-----------------------------------------------------------------------------------------*/
511 :
512 14239640 : 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 82306588 : for ( i = 0; i < nchan_fb_in; i++ )
522 : {
523 68066948 : mvr2r( &hFbMixer->ppFilterbank_prior_input[i][length], hFbMixer->ppFilterbank_prior_input[i], hFbMixer->fb_cfg->prior_input_length - length );
524 68066948 : mvr2r( pcm_in[i], &hFbMixer->ppFilterbank_prior_input[i][hFbMixer->fb_cfg->prior_input_length - length], length );
525 : }
526 :
527 14239640 : return;
528 : }
529 :
530 :
531 : /*-----------------------------------------------------------------------------------------*
532 : * Function ivas_fb_mixer_get_windowed_fr()
533 : *
534 : *
535 : *-----------------------------------------------------------------------------------------*/
536 :
537 14216144 : 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 14216144 : n_old_samples = min( ( hFbMixer->fb_cfg->prior_input_length - hFbMixer->fb_cfg->windowed_fr_offset ), ( 2 * mdft_len ) );
554 14216144 : n_new_samples = max( 0, 2 * length - n_old_samples );
555 14216144 : offset = (int16_t) ( 2 * mdft_len - length - hFbMixer->ana_window_offset );
556 14216144 : rev_offset = (int16_t) ( 2 * mdft_len - hFbMixer->ana_window_offset );
557 14216144 : set_zero( fr_in_block, offset );
558 :
559 82101754 : for ( ch_idx = 0; ch_idx < nchan_fb_in; ch_idx++ )
560 : {
561 67885610 : mvr2r( &hFbMixer->ppFilterbank_prior_input[ch_idx][offset + hFbMixer->fb_cfg->windowed_fr_offset], &fr_in_block[offset], n_old_samples - offset );
562 67885610 : mvr2r( pcm_in[ch_idx], &fr_in_block[n_old_samples], n_new_samples );
563 67885610 : win_ptr = hFbMixer->pAna_window;
564 :
565 3184759018 : for ( j = offset; j < 2 * mdft_len - length; j++ )
566 : {
567 3116873408 : fr_in_block[j] *= ( *( win_ptr++ ) );
568 : }
569 :
570 3184759018 : for ( j = rev_offset; j < 2 * mdft_len; j++ )
571 : {
572 3116873408 : fr_in_block[j] *= ( *( --win_ptr ) );
573 : }
574 :
575 67885610 : ivas_mdft( fr_in_block, frame_f_real[ch_idx], frame_f_imag[ch_idx], mdft_len << 1, mdft_len );
576 : }
577 :
578 14216144 : return;
579 : }
580 :
581 :
582 : /*-----------------------------------------------------------------------------------------*
583 : * Function ivas_fb_mixer_cross_fading()
584 : *
585 : * FB Mixer cross fading
586 : *-----------------------------------------------------------------------------------------*/
587 :
588 4601209 : 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 4601209 : if ( hFbMixer->first_frame[ch] == 0 )
600 : {
601 4570194 : fade_start_offset = hFbMixer->cross_fade_start_offset;
602 4570194 : fade_end_offset = hFbMixer->cross_fade_end_offset;
603 :
604 4570194 : for ( k = 0; k < fade_start_offset; k++ )
605 : {
606 0 : ppOut_pcm[ch][k] = pMdft_out_old[k + cf_offset];
607 : }
608 :
609 708944146 : for ( k = fade_start_offset; k < fade_end_offset; k++ )
610 : {
611 704373952 : 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 2822066002 : for ( k = fade_end_offset; k < frame_len; k++ )
615 : {
616 2817495808 : ppOut_pcm[ch][k] = pMdft_out_new[k + cf_offset];
617 : }
618 : }
619 : else
620 : {
621 31015 : hFbMixer->first_frame[ch] = 0;
622 :
623 26530535 : for ( k = 0; k < frame_len; k++ )
624 : {
625 26499520 : ppOut_pcm[ch][k] = pMdft_out_new[k + cf_offset];
626 : }
627 : }
628 :
629 4601209 : return;
630 : }
631 :
632 :
633 : /*-----------------------------------------------------------------------------------------*
634 : * Function ivas_fb_mixer_process()
635 : *
636 : * Filter bank process
637 : *-----------------------------------------------------------------------------------------*/
638 :
639 2728986 : 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 2728986 : ivas_filterbank_t *pFb = hFbMixer->pFb;
648 2728986 : 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 2728986 : pOut_fr_re = Out_fr_re;
657 2728986 : pOut_fr_im = Out_fr_im;
658 2728986 : pMdft_out[0] = Mdft_out_0;
659 2728986 : pMdft_out[1] = Mdft_out_1;
660 :
661 7329541 : 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 13801665 : for ( hist = 0; hist < 2; hist++ )
665 : {
666 9201110 : set_zero( pOut_fr_re, frame_len );
667 9201110 : set_zero( pOut_fr_im, frame_len );
668 :
669 53528584 : for ( j = 0; j < hFbMixer->fb_cfg->num_in_chans; j++ )
670 : {
671 44327474 : 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 14407262 : float *pFb_inFR_re = hFbMixer->ppFilterbank_inFR_re[j];
676 14407262 : float *pFb_inFR_im = hFbMixer->ppFilterbank_inFR_im[j];
677 :
678 14407262 : set_zero( filterbank_mixer_bins_re, frame_len );
679 14407262 : set_zero( filterbank_mixer_bins_im, frame_len );
680 :
681 183179298 : for ( i = 0; i < num_bands; i++ )
682 : {
683 168772036 : int16_t start_offset = pFb->fb_consts.pFilterbank_bins_start_offset[i];
684 168772036 : int16_t num_bins = pFb->fb_consts.pFilterbank_bins_per_band[i];
685 :
686 168772036 : float mixer_const = hFbMixer->prior_mixer[ch][j][i];
687 :
688 168772036 : pFilterbank_bin_to_band_re = pFb->fb_consts.ppFilterbank_FRs[0][i];
689 168772036 : pFilterbank_bin_to_band_im = pFb->fb_consts.ppFilterbank_FRs[1][i];
690 :
691 51416670030 : for ( k = start_offset; k < num_bins + start_offset; k++ )
692 : {
693 51247897994 : filterbank_mixer_bins_re[k] += *pFilterbank_bin_to_band_re++ * mixer_const;
694 51247897994 : filterbank_mixer_bins_im[k] += *pFilterbank_bin_to_band_im++ * mixer_const;
695 : }
696 168772036 : hFbMixer->prior_mixer[ch][j][i] = mixer_mat[ch][j][i];
697 : }
698 :
699 10766946782 : for ( k = 0; k < frame_len; k++ )
700 : {
701 : float temp_out_re, temp_out_im;
702 :
703 10752539520 : 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 10752539520 : pOut_fr_re[k] += temp_out_re;
707 10752539520 : pOut_fr_im[k] += temp_out_im;
708 : }
709 : }
710 : }
711 :
712 9201110 : ivas_imdft( pOut_fr_re, pOut_fr_im, pMdft_out[hist], frame_len );
713 : }
714 :
715 4600555 : ivas_fb_mixer_cross_fading( hFbMixer, ppOut_pcm, pMdft_out[0], pMdft_out[1], ch, frame_len, frame_len );
716 : }
717 :
718 2728986 : return;
719 : }
720 :
721 :
722 : /*-----------------------------------------------------------------------------------------*
723 : * Function ivas_fb_mixer_get_in_out_mapping()
724 : *
725 : *
726 : *-----------------------------------------------------------------------------------------*/
727 :
728 2728986 : 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 2728986 : set_s( (int16_t *) in_out_mixer_map, 0, IVAS_MAX_FB_MIXER_OUT_CH * IVAS_MAX_SPAR_FB_MIXER_IN_CH );
736 :
737 2728986 : if ( fb_cfg->active_w_mixing )
738 : {
739 1735384 : for ( i = 0; i < fb_cfg->num_out_chans; i++ )
740 : {
741 4338460 : for ( j = 0; j < fb_cfg->num_in_chans; j++ )
742 : {
743 3470768 : in_out_mixer_map[i][j] = 1;
744 : }
745 : }
746 : }
747 : else
748 : {
749 1861294 : in_out_mixer_map[0][0] = 1; /* W depends on only W input*/
750 5594157 : for ( i = 1; i < fb_cfg->num_out_chans; i++ )
751 : {
752 3732863 : in_out_mixer_map[i][0] = 1;
753 : }
754 : }
755 :
756 2728986 : return;
757 : }
758 :
759 :
760 : /*-----------------------------------------------------------------------------------------*
761 : * Function ivas_calculate_abs_fr()
762 : *
763 : * Function to calculate number of active bands
764 : *-----------------------------------------------------------------------------------------*/
765 :
766 49034 : 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 49034 : int16_t bands = pFb->filterbank_num_bands;
774 49034 : int16_t i, j, num_active_bands = 0;
775 49034 : int16_t idx_short_stride_bin_to_band = 0;
776 :
777 606728 : for ( i = 0; i < bands; i++ )
778 : {
779 557694 : const float *long_mdft_ptr_re = pFb->fb_consts.ppFilterbank_FRs[0][i];
780 557694 : const float *long_mdft_ptr_im = pFb->fb_consts.ppFilterbank_FRs[1][i];
781 557694 : int16_t start_offset = pFb->fb_consts.pFilterbank_bins_start_offset[i];
782 557694 : int16_t num_bins = pFb->fb_consts.pFilterbank_bins_per_band[i];
783 557694 : int16_t short_mdft_start_bin = -1;
784 : float short_stride_pow_spec[MDFT_FB_BANDS_240];
785 557694 : float short_stride_nrg = 0.0f;
786 557694 : float cldfb_nrg = 0.0f;
787 557694 : int16_t short_stride = pFb->fb_bin_to_band.short_stride;
788 557694 : const int16_t num_bins_per_short_stride_bin = ( const int16_t )( ( sampling_rate / FRAMES_PER_SEC ) / short_stride );
789 557694 : 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 557694 : float short_stride_max_per_spar_band = 1e-9f;
791 :
792 : /*loop over all stored Filter Bank Response MDFT coefficients*/
793 557694 : set_f( short_stride_pow_spec, 0, MDFT_FB_BANDS_240 );
794 161742219 : for ( j = start_offset; j < num_bins + start_offset; j++ )
795 : {
796 : float sq_abs;
797 :
798 : /*calculate bin energy */
799 161184525 : IVAS_CALCULATE_SQ_ABS( *long_mdft_ptr_re, *long_mdft_ptr_im, sq_abs );
800 161184525 : long_mdft_ptr_re++;
801 161184525 : long_mdft_ptr_im++;
802 :
803 : /* accumulate bin energies within a short stride bin */
804 161184525 : short_stride_nrg += sq_abs;
805 161184525 : if ( !( ( j + 1 ) % num_bins_per_short_stride_bin ) )
806 : { /* new short stride bin */
807 40209581 : short_stride_pow_spec[j / num_bins_per_short_stride_bin] = short_stride_nrg; /* energy rather than magnitude works better for covariance weighting*/
808 40209581 : short_stride_max_per_spar_band = max( short_stride_nrg, short_stride_max_per_spar_band ); /*compute highest magnitude per band*/
809 40209581 : short_stride_nrg = 0.0f;
810 : }
811 :
812 : /* accumulate bin energies within a CLDFB band */
813 161184525 : cldfb_nrg += sq_abs;
814 161184525 : if ( !( ( j + 1 ) % num_bins_per_cldfb_band ) )
815 : {
816 9973313 : pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j / num_bins_per_cldfb_band][i] = sqrtf( cldfb_nrg );
817 9973313 : cldfb_nrg = 0.0f;
818 : }
819 : }
820 :
821 : /*loop over the short MDFT bins*/
822 94411614 : for ( j = 0; j < short_stride; j++ )
823 : {
824 : /* normalize and sparsify the energies */
825 93853920 : short_stride_pow_spec[j] /= short_stride_max_per_spar_band;
826 93853920 : short_stride_pow_spec[j] = max( short_stride_pow_spec[j] - 0.3f, 0.0f ) / 0.7f;
827 93853920 : if ( short_stride_pow_spec[j] > 0.0f )
828 : {
829 8703102 : assert( idx_short_stride_bin_to_band < 2 * MDFT_FB_BANDS_240 ); /* array size of p_short_stride_bin_to_band */
830 8703102 : if ( short_mdft_start_bin == -1 )
831 : {
832 557694 : short_mdft_start_bin = j;
833 557694 : pFb->fb_bin_to_band.p_short_stride_start_bin_per_band[i] = j;
834 557694 : 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 8703102 : pFb->fb_bin_to_band.p_short_stride_bin_to_band[idx_short_stride_bin_to_band++] = short_stride_pow_spec[j];
837 8703102 : pFb->fb_bin_to_band.p_short_stride_num_bins_per_band[i]++;
838 : }
839 : }
840 : }
841 :
842 : /*loop over CLDFB bands*/
843 2055514 : for ( j = 0; j < pFb->fb_bin_to_band.num_cldfb_bands; j++ )
844 : {
845 2006480 : float sum_over_spar_bands = 0.0f;
846 2006480 : float max_spar_band_contribution = 0.0f;
847 2006480 : int16_t spar_start = 0;
848 2006480 : int16_t any_non_zero = 0;
849 :
850 25469960 : for ( i = 0; i < bands; i++ )
851 : {
852 23463480 : sum_over_spar_bands += pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i];
853 23463480 : if ( pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i] > max_spar_band_contribution )
854 : {
855 6165102 : max_spar_band_contribution = pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i];
856 6165102 : pFb->fb_bin_to_band.p_cldfb_map_to_spar_band[j] = i;
857 : }
858 : }
859 2006480 : sum_over_spar_bands = max( sum_over_spar_bands, EPSILON );
860 25469960 : for ( i = 0; i < bands; i++ )
861 : {
862 23463480 : if ( pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i] == 0.0f && !any_non_zero )
863 : {
864 11986554 : spar_start++;
865 : }
866 : else
867 : {
868 11476926 : any_non_zero = 1;
869 : }
870 23463480 : pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i] /= sum_over_spar_bands;
871 : }
872 2006480 : pFb->fb_bin_to_band.p_spar_start_bands[j] = spar_start;
873 : }
874 :
875 49034 : frame_len = (int16_t) ( sampling_rate / FRAMES_PER_SEC );
876 :
877 49034 : 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 606728 : for ( i = 0; i < bands; i++ )
881 : {
882 557694 : const float *pFilterbank_bin_to_band_re = pFb->fb_consts.ppFilterbank_FRs[0][i];
883 557694 : const float *pFilterbank_bin_to_band_im = pFb->fb_consts.ppFilterbank_FRs[1][i];
884 :
885 557694 : int16_t start_offset = pFb->fb_consts.pFilterbank_bins_start_offset[i];
886 557694 : int16_t num_bins = pFb->fb_consts.pFilterbank_bins_per_band[i];
887 557694 : int16_t idx = 0;
888 557694 : int16_t abs_active_bins = pFb->fb_bin_to_band.pFb_active_bins_per_band[i];
889 557694 : int16_t abs_start_offset = pFb->fb_bin_to_band.pFb_start_bin_per_band[i];
890 :
891 161742219 : for ( j = start_offset; j < num_bins + start_offset; j++ )
892 : {
893 161184525 : float temp = 0;
894 :
895 161184525 : IVAS_CALCULATE_ABS( *pFilterbank_bin_to_band_re, *pFilterbank_bin_to_band_im, temp );
896 :
897 161184525 : pFilterbank_bin_to_band_re++;
898 161184525 : pFilterbank_bin_to_band_im++;
899 :
900 161184525 : temp -= 0.3f;
901 :
902 161184525 : if ( temp < 0.0f )
903 : {
904 117061175 : temp = 0;
905 : }
906 :
907 161184525 : if ( j < ( abs_active_bins + abs_start_offset ) && j >= abs_start_offset && alloc_fb_resp != -1 )
908 : {
909 3758746 : pFb->fb_bin_to_band.pFb_bin_to_band[i][idx++] = temp;
910 : }
911 :
912 161184525 : ppFilterbank_FRs_s[j] += temp;
913 : }
914 : }
915 :
916 32152714 : for ( i = 0; i < frame_len; i++ )
917 : {
918 32103680 : if ( ppFilterbank_FRs_s[i] < 0.1f )
919 : {
920 0 : ppFilterbank_FRs_s[i] = 0.1f;
921 : }
922 : }
923 :
924 49034 : if ( alloc_fb_resp != -1 )
925 : {
926 45013 : for ( j = 0; j < bands; j++ )
927 : {
928 41464 : int16_t abs_active_bins = pFb->fb_bin_to_band.pFb_active_bins_per_band[j];
929 41464 : int16_t abs_start_offset = pFb->fb_bin_to_band.pFb_start_bin_per_band[j];
930 :
931 3800210 : for ( i = 0; i < abs_active_bins; i++ )
932 : {
933 3758746 : 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 49034 : return num_active_bands;
944 : }
945 :
946 :
947 : /*-----------------------------------------------------------------------------------------*
948 : * Function ivas_get_active_bins()
949 : *
950 : *
951 : *-----------------------------------------------------------------------------------------*/
952 :
953 98068 : 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 98068 : if ( sampling_rate == 32000 )
963 : {
964 32128 : sr_idx = 1;
965 : }
966 65940 : else if ( sampling_rate == 16000 )
967 : {
968 30714 : sr_idx = 2;
969 : }
970 : else
971 : {
972 35226 : sr_idx = 0;
973 : }
974 :
975 98068 : *pActive_bins_abs = ivas_fb_abs_bins_per_band_12band_1ms[sr_idx];
976 98068 : *pActive_bins = ivas_fb_bins_per_band_12band_1ms[sr_idx];
977 98068 : *pStart_offset_abs = ivas_fb_abs_bins_start_offset_12band_1ms[sr_idx];
978 98068 : *pStart_offset = ivas_fb_bins_start_offset_12band_1ms[sr_idx];
979 :
980 98068 : return;
981 : }
982 :
983 :
984 : /*-----------------------------------------------------------------------------------------*
985 : * Function ivas_filterbank_setup()
986 : *
987 : * Filterbank setup, initialization
988 : *-----------------------------------------------------------------------------------------*/
989 :
990 68348 : 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 68348 : const int16_t *pAll_bins_start_offset = NULL;
997 68348 : const int16_t *pAll_bins_per_band = NULL;
998 68348 : const int16_t *pAll_bins_start_offset_abs = NULL;
999 68348 : const int16_t *pAll_bins_per_band_abs = NULL;
1000 68348 : const int16_t *pAll_bins_per_band_48k = NULL;
1001 : ivas_error error;
1002 68348 : IVAS_FB_CFG *pCfg = hFbMixer->fb_cfg;
1003 :
1004 68348 : error = IVAS_ERR_OK;
1005 :
1006 68348 : if ( pCfg->num_out_chans > 0 )
1007 : {
1008 49034 : hFbMixer->pFb->filterbank_num_bands = ivas_get_num_bands( sampling_rate );
1009 :
1010 49034 : 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 49034 : if ( pCfg->fb_latency == NS2SA( sampling_rate, DELAY_FB_1_NS ) )
1013 : {
1014 49034 : pAll_fb_fr[0] = ivas_fb_fr_12band_1ms_re;
1015 49034 : pAll_fb_fr[1] = ivas_fb_fr_12band_1ms_im;
1016 49034 : 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 68348 : hFbMixer->cross_fade_end_offset = pCfg->fade_len + pCfg->pcm_offset;
1025 68348 : hFbMixer->cross_fade_start_offset = hFbMixer->cross_fade_end_offset - pCfg->fade_len;
1026 68348 : hFbMixer->ana_window_offset = pCfg->fb_latency + pCfg->pcm_offset;
1027 :
1028 68348 : 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 68348 : 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 68348 : if ( pCfg->num_out_chans > 0 )
1039 : {
1040 49034 : ivas_filterbank_t *pFb = hFbMixer->pFb;
1041 49034 : int16_t offset = 0;
1042 :
1043 49034 : pFb->fb_consts.pFilterbank_bins_per_band = pAll_bins_per_band;
1044 49034 : pFb->fb_consts.pFilterbank_bins_start_offset = pAll_bins_start_offset;
1045 49034 : pFb->fb_bin_to_band.pFb_active_bins_per_band = pAll_bins_per_band_abs;
1046 49034 : 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 49034 : 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 49034 : pFb->fb_bin_to_band.short_stride = (int16_t) ( ( sampling_rate / FRAMES_PER_SEC ) / 4 );
1052 49034 : set_f( pFb->fb_bin_to_band.p_short_stride_bin_to_band, 0.0f, 2 * MDFT_FB_BANDS_240 );
1053 49034 : set_s( pFb->fb_bin_to_band.p_cldfb_map_to_spar_band, 0, CLDFB_NO_CHANNELS_MAX );
1054 49034 : set_s( pFb->fb_bin_to_band.p_spar_start_bands, 0, CLDFB_NO_CHANNELS_MAX );
1055 :
1056 1029714 : for ( j = 0; j < IVAS_MAX_NUM_FB_BANDS; j++ )
1057 : {
1058 980680 : pFb->fb_bin_to_band.p_short_stride_num_bins_per_band[j] = 0; /* aka num_active_bins per SPAR band */
1059 980680 : pFb->fb_bin_to_band.p_short_stride_start_bin_per_band[j] = 0; /* first considered bin index per SPAR band */
1060 980680 : pFb->fb_bin_to_band.pp_short_stride_bin_to_band[j] = NULL;
1061 59821480 : for ( i = 0; i < CLDFB_NO_CHANNELS_MAX; i++ )
1062 : {
1063 58840800 : pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[i][j] = 0.0f;
1064 : }
1065 : }
1066 49034 : if ( sampling_rate == 48000 )
1067 : {
1068 228969 : for ( j = 0; j < pFb->filterbank_num_bands; j++ )
1069 : {
1070 211356 : pFb->fb_consts.ppFilterbank_FRs[0][j] = &pAll_fb_fr[0][offset];
1071 211356 : pFb->fb_consts.ppFilterbank_FRs[1][j] = &pAll_fb_fr[1][offset];
1072 211356 : offset += pFb->fb_consts.pFilterbank_bins_per_band[j];
1073 : }
1074 :
1075 17613 : 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 31421 : num_diff_bands = MAX_NUM_BANDS_DIFF_NON48K;
1086 31421 : start_diff_band_non48k = pFb->filterbank_num_bands - num_diff_bands;
1087 :
1088 31421 : pFb->fb_consts.pFilterbank_bins_per_band = pAll_bins_per_band;
1089 31421 : pFb->fb_consts.pFilterbank_bins_start_offset = pAll_bins_start_offset;
1090 :
1091 377759 : for ( j = 0; j < pFb->filterbank_num_bands; j++ )
1092 : {
1093 346338 : int16_t num_active_bins = pFb->fb_consts.pFilterbank_bins_per_band[j];
1094 :
1095 346338 : if ( j < start_diff_band_non48k )
1096 : {
1097 252075 : pFb->fb_consts.ppFilterbank_FRs[0][j] = &pAll_fb_fr[0][offset];
1098 252075 : pFb->fb_consts.ppFilterbank_FRs[1][j] = &pAll_fb_fr[1][offset];
1099 : }
1100 : else
1101 : {
1102 94263 : mvr2r( &pAll_fb_fr[0][offset], pFb->fb_consts.ppFilterbank_FRs_non48k[0][j], num_active_bins );
1103 94263 : mvr2r( &pAll_fb_fr[1][offset], pFb->fb_consts.ppFilterbank_FRs_non48k[1][j], num_active_bins );
1104 : }
1105 :
1106 346338 : offset += pAll_bins_per_band_48k[j];
1107 : }
1108 :
1109 125684 : for ( j = start_diff_band_non48k; j < pFb->filterbank_num_bands; j++ )
1110 : {
1111 94263 : ppFilterbank_FRs_re_temp[j - start_diff_band_non48k] = pFb->fb_consts.ppFilterbank_FRs_non48k[0][j];
1112 94263 : ppFilterbank_FRs_im_temp[j - start_diff_band_non48k] = pFb->fb_consts.ppFilterbank_FRs_non48k[1][j];
1113 94263 : active_bins_temp[j - start_diff_band_non48k] = pFb->fb_consts.pFilterbank_bins_per_band[j];
1114 94263 : start_offset_temp[j - start_diff_band_non48k] = pFb->fb_consts.pFilterbank_bins_start_offset[j];
1115 : }
1116 :
1117 31421 : ivas_get_ld_fb_resp( ppFilterbank_FRs_re_temp, ppFilterbank_FRs_im_temp, ppFilterbank_FRs_re_temp, ppFilterbank_FRs_im_temp,
1118 31421 : active_bins_temp, start_offset_temp, num_diff_bands, pCfg->fb_latency, sampling_rate );
1119 :
1120 125684 : for ( j = start_diff_band_non48k; j < pFb->filterbank_num_bands; j++ )
1121 : {
1122 94263 : pFb->fb_consts.ppFilterbank_FRs[0][j] = (const float *) pFb->fb_consts.ppFilterbank_FRs_non48k[0][j];
1123 94263 : pFb->fb_consts.ppFilterbank_FRs[1][j] = (const float *) pFb->fb_consts.ppFilterbank_FRs_non48k[1][j];
1124 : }
1125 :
1126 31421 : ivas_calculate_abs_fr( pFb, sampling_rate, pCfg->active_w_mixing );
1127 : }
1128 : }
1129 :
1130 68348 : return error;
1131 : }
1132 :
1133 :
1134 : /*-----------------------------------------------------------------------------------------*
1135 : * Function ivas_fb_mixer_get_window()
1136 : *
1137 : *
1138 : *-----------------------------------------------------------------------------------------*/
1139 :
1140 136696 : 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 136696 : error = IVAS_ERR_OK;
1149 :
1150 136696 : if ( fade_len == NS2SA( sampling_rate, DELAY_FB_4_NS ) )
1151 : {
1152 53432 : switch ( sampling_rate )
1153 : {
1154 21917 : case 48000:
1155 21917 : *pWindow = ivas_fb_cf_4ms_48k;
1156 21917 : break;
1157 16111 : case 32000:
1158 16111 : *pWindow = ivas_fb_cf_4ms_32k;
1159 16111 : break;
1160 15404 : case 16000:
1161 15404 : *pWindow = ivas_fb_cf_4ms_16k;
1162 15404 : break;
1163 0 : default:
1164 0 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Unsupported Sampling frequency!" );
1165 : }
1166 : }
1167 83264 : else if ( fade_len == NS2SA( sampling_rate, DELAY_FB_1_NS ) )
1168 : {
1169 83264 : switch ( sampling_rate )
1170 : {
1171 48799 : case 48000:
1172 48799 : *pWindow = ivas_fb_cf_1ms_48k;
1173 48799 : break;
1174 18941 : case 32000:
1175 18941 : *pWindow = ivas_fb_cf_1ms_32k;
1176 18941 : break;
1177 15524 : case 16000:
1178 15524 : *pWindow = ivas_fb_cf_1ms_16k;
1179 15524 : 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 136696 : return error;
1190 : }
1191 :
1192 :
1193 31421 : static const float *ivas_get_cheby_ramp(
1194 : const int16_t delay )
1195 : {
1196 31421 : const float *pCheby = NULL;
1197 :
1198 31421 : switch ( delay )
1199 : {
1200 16064 : case IVAS_FB_1MS_32K_SAMP:
1201 16064 : pCheby = ivas_fb_resp_cheby_ramp_32del;
1202 16064 : break;
1203 15357 : case IVAS_FB_1MS_16K_SAMP:
1204 15357 : pCheby = ivas_fb_resp_cheby_ramp_16del;
1205 15357 : break;
1206 0 : default:
1207 0 : assert( !"Unsupported cheby ramp length!" );
1208 : }
1209 :
1210 31421 : return pCheby;
1211 : }
1212 :
1213 :
1214 : /*get first half of the hanning window*/
1215 31421 : static void ivas_get_hanning_win(
1216 : const int16_t len,
1217 : float *pH_win )
1218 : {
1219 : int16_t i;
1220 :
1221 15226621 : for ( i = 0; i < len; i++ )
1222 : {
1223 15195200 : pH_win[i] = 0.5f * ( 1.0f - cosf( ( PI2 * ( i + 1 ) ) / ( ( 2 * len ) + 1 ) ) );
1224 : }
1225 :
1226 31421 : return;
1227 : }
1228 :
1229 :
1230 31421 : 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 31421 : frame_len = (int16_t) ( sampling_rate / FRAMES_PER_SEC );
1251 31421 : pCheby = ivas_get_cheby_ramp( delay );
1252 31421 : b = 0;
1253 31421 : s = 0;
1254 :
1255 31421 : assert( sampling_rate == 32000 || sampling_rate == 16000 );
1256 :
1257 31421 : ivas_get_hanning_win( frame_len, han_win );
1258 125684 : for ( b = 0; b < num_bands; b++ )
1259 : {
1260 94263 : set_f( scratch2, 0, frame_len << 1 );
1261 94263 : mvr2r( ppIdeal_FRs_re[b], &scratch2[pStart_offset[b]], pActive_bins[b] );
1262 94263 : mvr2r( ppIdeal_FRs_im[b], &scratch2[frame_len + pStart_offset[b]], pActive_bins[b] );
1263 94263 : ivas_imdft( scratch2, &scratch2[frame_len], scratch1, frame_len );
1264 :
1265 : /*apply circular shift and hanning window*/
1266 :
1267 45679863 : for ( s = delay; s < frame_len + delay; s++ )
1268 : {
1269 45585600 : scratch2[s - delay] = scratch1[s] * ( 1 - han_win[s - delay] );
1270 : }
1271 :
1272 43400583 : for ( ; s < 2 * frame_len; s++ )
1273 : {
1274 43306320 : scratch2[s - delay] = scratch1[s] * han_win[s - ( frame_len + delay )];
1275 : }
1276 :
1277 2373543 : for ( s = 0; s < delay; s++ )
1278 : {
1279 2279280 : 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 2467806 : for ( s = 0; s < delay + 1; s++ )
1285 : {
1286 2373543 : scratch1[s] = scratch2[s] * pCheby[delay - s];
1287 : }
1288 :
1289 43306320 : for ( ; s < frame_len; s++ )
1290 : {
1291 43212057 : scratch1[s] = 0;
1292 : }
1293 :
1294 43400583 : for ( ; s < 2 * frame_len - delay; s++ )
1295 : {
1296 43306320 : scratch1[s] = scratch2[s];
1297 : }
1298 :
1299 2373543 : for ( ; s < 2 * frame_len; s++ )
1300 : {
1301 2279280 : scratch1[s] = scratch2[s] * ( 1 - pCheby[s - ( 2 * frame_len - delay )] );
1302 : }
1303 :
1304 : /*IR - pre ring + post ring*/
1305 91171200 : for ( s = 1; s < 2 * frame_len; s++ )
1306 : {
1307 91076937 : scratch2[s] = scratch2[s] /*pre ring*/ - scratch1[s] /*post ring*/ - scratch1[( 2 * frame_len ) - s];
1308 : }
1309 :
1310 88986183 : for ( s = 0; s < 2 * frame_len - delay; s++ )
1311 : {
1312 88891920 : scratch1[s + delay] = scratch2[s];
1313 : }
1314 :
1315 2373543 : for ( ; s < 2 * frame_len; s++ )
1316 : {
1317 2279280 : scratch1[s - ( 2 * frame_len - delay )] = -1 * ( scratch2[s] );
1318 : }
1319 :
1320 : /* apply final window*/
1321 2373543 : for ( s = 0; s < delay; s++ )
1322 : {
1323 2279280 : scratch1[s] = scratch1[s] * sinf( ( EVS_PI * ( s + 1 ) ) / ( 2 * delay ) );
1324 : }
1325 :
1326 41215566 : for ( s = 2 * delay; s < frame_len + 1; s++ )
1327 : {
1328 41121303 : scratch1[s] = scratch1[s] * sinf( ( EVS_PI * ( frame_len - s ) ) / ( 2 * ( frame_len + 1 - ( 2 * delay ) ) ) );
1329 : }
1330 :
1331 45585600 : for ( ; s < 2 * frame_len; s++ )
1332 : {
1333 45491337 : scratch1[s] = 0;
1334 : }
1335 :
1336 : /*compute frequency response*/
1337 94263 : ivas_mdft( scratch1, scratch2, &scratch2[frame_len], frame_len << 1, frame_len );
1338 :
1339 94263 : mvr2r( &scratch2[pStart_offset[b]], ppNew_FRs_re[b], pActive_bins[b] );
1340 94263 : mvr2r( &scratch2[frame_len + pStart_offset[b]], ppNew_FRs_im[b], pActive_bins[b] );
1341 : }
1342 :
1343 31421 : return;
1344 : }
|