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 395351 : 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 395351 : assert( bwidth > 0 ); /*NB BW is not supported*/
70 395351 : num_active_bands = ivas_num_active_bands[bwidth - 1];
71 :
72 395351 : 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 2272 : static int16_t ivas_get_num_bands(
83 : const int32_t sampling_rate )
84 : {
85 2272 : int16_t bwidth = ivas_get_bw_idx_from_sample_rate( sampling_rate );
86 2272 : int16_t num_active_bands = ivas_get_num_bands_from_bw_idx( bwidth );
87 :
88 2272 : 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 7721 : 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 7721 : 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 7721 : pFb_cfg->num_in_chans = num_in_chans;
116 7721 : pFb_cfg->num_out_chans = num_out_chans;
117 7721 : pFb_cfg->nchan_fb_in = nchan_fb_in;
118 :
119 7721 : pFb_cfg->pcm_offset = 0; /* note: in SPAR decoder, this parameter is overwritten later */
120 7721 : pFb_cfg->active_w_mixing = active_w_mixing;
121 7721 : pFb_cfg->windowed_fr_offset = 0;
122 :
123 7721 : if ( ivas_format == ISM_FORMAT )
124 : {
125 320 : pFb_cfg->fb_latency = NS2SA( sampling_rate, DELAY_FB_4_NS );
126 320 : pFb_cfg->fade_len = NS2SA( sampling_rate, DELAY_FB_4_NS );
127 320 : pFb_cfg->prior_input_length = NS2SA( sampling_rate, DELAY_DIRAC_ENC_CMP_NS_PARAM_ISM ) + NS2SA( sampling_rate, DIRAC_SLOT_ENC_NS );
128 : }
129 7401 : else if ( ivas_format == SBA_FORMAT )
130 : {
131 6544 : pFb_cfg->fb_latency = NS2SA( sampling_rate, DELAY_FB_1_NS );
132 :
133 6544 : pFb_cfg->fade_len = NS2SA( sampling_rate, DELAY_FB_4_NS );
134 6544 : pFb_cfg->prior_input_length = NS2SA( sampling_rate, FRAME_SIZE_NS );
135 6544 : 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 857 : else if ( ivas_format == MASA_FORMAT )
138 : {
139 566 : pFb_cfg->fb_latency = NS2SA( sampling_rate, DELAY_FB_1_NS );
140 566 : pFb_cfg->fade_len = NS2SA( sampling_rate, DELAY_FB_1_NS );
141 566 : pFb_cfg->prior_input_length = NS2SA( sampling_rate, DELAY_DIRAC_ENC_CMP_NS ) + NS2SA( sampling_rate, DIRAC_SLOT_ENC_NS );
142 : }
143 291 : else if ( ivas_format == MC_FORMAT )
144 : {
145 291 : pFb_cfg->fb_latency = NS2SA( sampling_rate, DELAY_FB_1_NS );
146 291 : pFb_cfg->fade_len = NS2SA( sampling_rate, DELAY_FB_1_NS );
147 291 : pFb_cfg->prior_input_length = NS2SA( sampling_rate, DELAY_DIRAC_ENC_CMP_NS ) + NS2SA( sampling_rate, PARAM_MC_SLOT_ENC_NS );
148 : }
149 :
150 7721 : *pFb_cfg_out = pFb_cfg;
151 :
152 7721 : return IVAS_ERR_OK;
153 : }
154 :
155 :
156 : /*-------------------------------------------------------------------------
157 : * ivas_FB_mixer_open()
158 : *
159 : * Allocate and initialize FB mixer handle
160 : *------------------------------------------------------------------------*/
161 :
162 7721 : 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 7721 : error = IVAS_ERR_OK;
175 :
176 7721 : frame_len = (int16_t) ( sampling_rate / FRAMES_PER_SEC );
177 :
178 7721 : hFbMixer = *hFbMixer_out;
179 :
180 7721 : if ( !spar_reconfig_flag )
181 : {
182 2301 : 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 2301 : if ( fb_cfg->num_out_chans > 0 )
188 : {
189 1136 : 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 1165 : hFbMixer->pFb = NULL;
197 : }
198 : }
199 :
200 7721 : if ( fb_cfg->active_w_mixing == -1 )
201 : {
202 4908 : num_chs_alloc = 0;
203 : }
204 2813 : else if ( fb_cfg->active_w_mixing )
205 : {
206 567 : num_chs_alloc = max( fb_cfg->num_in_chans, fb_cfg->nchan_fb_in );
207 : }
208 : else
209 : {
210 2246 : num_chs_alloc = 1; /* only W channel processed for predicting YZX */
211 : }
212 :
213 12235 : for ( i = 0; i < num_chs_alloc; i++ )
214 : {
215 4514 : if ( fb_cfg->num_out_chans == 0 )
216 : {
217 1165 : hFbMixer->ppFilterbank_inFR_re[i] = NULL;
218 1165 : hFbMixer->ppFilterbank_inFR_im[i] = NULL;
219 : }
220 : else
221 : {
222 3349 : j = fb_cfg->remix_order[i];
223 :
224 3349 : 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 3349 : 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 7721 : if ( fb_cfg->active_w_mixing == -1 )
237 : {
238 4908 : num_chs_alloc = 0;
239 : }
240 : else
241 : {
242 2813 : num_chs_alloc = max( fb_cfg->num_in_chans, fb_cfg->nchan_fb_in );
243 : }
244 :
245 21289 : for ( i = 0; i < num_chs_alloc; i++ )
246 : {
247 13568 : 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 13568 : set_f( hFbMixer->ppFilterbank_prior_input[i], 0, fb_cfg->prior_input_length );
252 : }
253 :
254 7721 : if ( ( fb_cfg->active_w_mixing != -1 ) && ( fb_cfg->num_out_chans > 0 ) )
255 : {
256 : float *pTemp_mem;
257 1648 : 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 5488 : for ( i = 0; i < fb_cfg->num_out_chans; i++ )
262 : {
263 24092 : for ( j = 0; j < fb_cfg->num_in_chans; j++ )
264 : {
265 20252 : hFbMixer->prior_mixer[i][j] = pTemp_mem;
266 20252 : pTemp_mem += IVAS_MAX_NUM_BANDS;
267 20252 : set_f( hFbMixer->prior_mixer[i][j], 0, IVAS_MAX_NUM_BANDS );
268 : }
269 : }
270 : }
271 :
272 7721 : if ( !spar_reconfig_flag )
273 : {
274 2301 : 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 1136 : num_bands = ivas_get_num_bands( sampling_rate );
279 :
280 1136 : ivas_get_active_bins( &pActive_bins_per_band, &pActive_bins_per_band_abs, &pStart_offset, &pStart_offset_abs, sampling_rate );
281 :
282 1136 : if ( fb_cfg->active_w_mixing != -1 )
283 : {
284 3731 : for ( i = 0; i < num_bands; i++ )
285 : {
286 3438 : 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 1136 : 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 1165 : hFbMixer->num_diff_bands = 0;
320 : }
321 : }
322 :
323 7721 : hFbMixer->fb_cfg = fb_cfg;
324 7721 : set_s( hFbMixer->first_frame, 1, hFbMixer->fb_cfg->num_out_chans );
325 7721 : set_s( hFbMixer->first_frame + hFbMixer->fb_cfg->num_out_chans, 0, IVAS_SPAR_MAX_CH - hFbMixer->fb_cfg->num_out_chans );
326 :
327 7721 : if ( !spar_reconfig_flag )
328 : {
329 2301 : if ( ( error = ivas_filterbank_setup( hFbMixer, sampling_rate ) ) != IVAS_ERR_OK )
330 : {
331 0 : return error;
332 : }
333 : }
334 :
335 7721 : *hFbMixer_out = hFbMixer;
336 :
337 7721 : return error;
338 : }
339 :
340 :
341 : /*-------------------------------------------------------------------------
342 : * ivas_FB_mixer_close()
343 : *
344 : * Deallocate FB mixer handle
345 : *------------------------------------------------------------------------*/
346 :
347 7721 : 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 7721 : hFbMixer = *hFbMixer_in;
359 7721 : fb_cfg = hFbMixer->fb_cfg;
360 :
361 7721 : if ( fb_cfg->active_w_mixing == -1 )
362 : {
363 4908 : num_chs_alloc = 0;
364 : }
365 2813 : else if ( fb_cfg->active_w_mixing )
366 : {
367 567 : num_chs_alloc = max( fb_cfg->num_in_chans, fb_cfg->nchan_fb_in );
368 : }
369 : else
370 : {
371 2246 : num_chs_alloc = 1; /* only W channel processed for predicting YZX */
372 : }
373 :
374 7721 : if ( hFbMixer != NULL )
375 : {
376 12235 : for ( i = 0; i < num_chs_alloc; i++ )
377 : {
378 4514 : if ( fb_cfg->num_out_chans > 0 )
379 : {
380 3349 : j = fb_cfg->remix_order[i];
381 :
382 3349 : free( hFbMixer->ppFilterbank_inFR_re[j] );
383 3349 : hFbMixer->ppFilterbank_inFR_re[j] = NULL;
384 :
385 3349 : free( hFbMixer->ppFilterbank_inFR_im[j] );
386 3349 : hFbMixer->ppFilterbank_inFR_im[j] = NULL;
387 : }
388 : }
389 :
390 7721 : if ( fb_cfg->active_w_mixing == -1 )
391 : {
392 4908 : num_chs_alloc = 0;
393 : }
394 : else
395 : {
396 2813 : num_chs_alloc = max( fb_cfg->num_in_chans, fb_cfg->nchan_fb_in );
397 : }
398 :
399 21289 : for ( i = 0; i < num_chs_alloc; i++ )
400 : {
401 13568 : free( hFbMixer->ppFilterbank_prior_input[i] );
402 13568 : hFbMixer->ppFilterbank_prior_input[i] = NULL;
403 : }
404 :
405 7721 : if ( ( fb_cfg->active_w_mixing != -1 ) && ( fb_cfg->num_out_chans > 0 ) )
406 : {
407 1648 : free( hFbMixer->prior_mixer[0][0] );
408 1648 : hFbMixer->prior_mixer[0][0] = NULL;
409 : }
410 :
411 7721 : if ( !spar_reconfig_flag )
412 : {
413 2301 : if ( fb_cfg->num_out_chans > 0 )
414 : {
415 1136 : num_bands = hFbMixer->pFb->filterbank_num_bands;
416 :
417 1136 : if ( fb_cfg->active_w_mixing != -1 )
418 : {
419 3731 : for ( i = 0; i < num_bands; i++ )
420 : {
421 3438 : free( hFbMixer->pFb->fb_bin_to_band.pFb_bin_to_band[i] );
422 3438 : hFbMixer->pFb->fb_bin_to_band.pFb_bin_to_band[i] = NULL;
423 : }
424 : }
425 :
426 1136 : 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 2301 : if ( hFbMixer->pFb != NULL )
442 : {
443 1136 : free( hFbMixer->pFb );
444 1136 : hFbMixer->pFb = NULL;
445 : }
446 : }
447 :
448 7721 : if ( hFbMixer->fb_cfg != NULL )
449 : {
450 7721 : free( hFbMixer->fb_cfg );
451 7721 : hFbMixer->fb_cfg = NULL;
452 : }
453 :
454 7721 : if ( !spar_reconfig_flag )
455 : {
456 2301 : free( hFbMixer );
457 2301 : hFbMixer = NULL;
458 : }
459 : }
460 :
461 7721 : return;
462 : }
463 :
464 :
465 : /*-----------------------------------------------------------------------------------------*
466 : * Function ivas_fb_mixer_pcm_ingest()
467 : *
468 : * PCM ingest block
469 : *-----------------------------------------------------------------------------------------*/
470 :
471 159500 : 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 159500 : IVAS_FB_CFG *fb_cfg = hFbMixer->fb_cfg;
481 :
482 159500 : if ( fb_cfg->active_w_mixing )
483 : {
484 44251 : num_chs_ingest = fb_cfg->num_in_chans;
485 : }
486 : else
487 : {
488 115249 : num_chs_ingest = 1; /* forward Filterbank MDFT only on W */
489 : }
490 :
491 899700 : for ( i = 0; i < fb_cfg->num_in_chans; i++ )
492 : {
493 740200 : mvr2r( &hFbMixer->ppFilterbank_prior_input[i][fb_cfg->prior_input_length - frame_len], ppOut_pcm[i], frame_len );
494 740200 : mvr2r( pcm_in[HOA_md_ind[i]], &ppOut_pcm[i][frame_len], frame_len );
495 : }
496 :
497 451753 : for ( i = 0; i < num_chs_ingest; i++ )
498 : {
499 292253 : 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 159500 : return;
503 : }
504 :
505 :
506 : /*-----------------------------------------------------------------------------------------*
507 : * Function ivas_fb_mixer_update_prior_input()
508 : *
509 : *
510 : *-----------------------------------------------------------------------------------------*/
511 :
512 896980 : 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 5144340 : for ( i = 0; i < nchan_fb_in; i++ )
522 : {
523 4247360 : mvr2r( &hFbMixer->ppFilterbank_prior_input[i][length], hFbMixer->ppFilterbank_prior_input[i], hFbMixer->fb_cfg->prior_input_length - length );
524 4247360 : mvr2r( pcm_in[i], &hFbMixer->ppFilterbank_prior_input[i][hFbMixer->fb_cfg->prior_input_length - length], length );
525 : }
526 :
527 896980 : return;
528 : }
529 :
530 :
531 : /*-----------------------------------------------------------------------------------------*
532 : * Function ivas_fb_mixer_get_windowed_fr()
533 : *
534 : *
535 : *-----------------------------------------------------------------------------------------*/
536 :
537 895311 : 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 895311 : n_old_samples = min( ( hFbMixer->fb_cfg->prior_input_length - hFbMixer->fb_cfg->windowed_fr_offset ), ( 2 * mdft_len ) );
554 895311 : n_new_samples = max( 0, 2 * length - n_old_samples );
555 895311 : offset = (int16_t) ( 2 * mdft_len - length - hFbMixer->ana_window_offset );
556 895311 : rev_offset = (int16_t) ( 2 * mdft_len - hFbMixer->ana_window_offset );
557 895311 : set_zero( fr_in_block, offset );
558 :
559 5131803 : for ( ch_idx = 0; ch_idx < nchan_fb_in; ch_idx++ )
560 : {
561 4236492 : mvr2r( &hFbMixer->ppFilterbank_prior_input[ch_idx][offset + hFbMixer->fb_cfg->windowed_fr_offset], &fr_in_block[offset], n_old_samples - offset );
562 4236492 : mvr2r( pcm_in[ch_idx], &fr_in_block[n_old_samples], n_new_samples );
563 4236492 : win_ptr = hFbMixer->pAna_window;
564 :
565 225320236 : for ( j = offset; j < 2 * mdft_len - length; j++ )
566 : {
567 221083744 : fr_in_block[j] *= ( *( win_ptr++ ) );
568 : }
569 :
570 225320236 : for ( j = rev_offset; j < 2 * mdft_len; j++ )
571 : {
572 221083744 : fr_in_block[j] *= ( *( --win_ptr ) );
573 : }
574 :
575 4236492 : ivas_mdft( fr_in_block, frame_f_real[ch_idx], frame_f_imag[ch_idx], mdft_len << 1, mdft_len );
576 : }
577 :
578 895311 : return;
579 : }
580 :
581 :
582 : /*-----------------------------------------------------------------------------------------*
583 : * Function ivas_fb_mixer_cross_fading()
584 : *
585 : * FB Mixer cross fading
586 : *-----------------------------------------------------------------------------------------*/
587 :
588 274579 : 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 274579 : if ( hFbMixer->first_frame[ch] == 0 )
600 : {
601 271856 : fade_start_offset = hFbMixer->cross_fade_start_offset;
602 271856 : fade_end_offset = hFbMixer->cross_fade_end_offset;
603 :
604 271856 : for ( k = 0; k < fade_start_offset; k++ )
605 : {
606 0 : ppOut_pcm[ch][k] = pMdft_out_old[k + cf_offset];
607 : }
608 :
609 43397872 : for ( k = fade_start_offset; k < fade_end_offset; k++ )
610 : {
611 43126016 : 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 172775920 : for ( k = fade_end_offset; k < frame_len; k++ )
615 : {
616 172504064 : ppOut_pcm[ch][k] = pMdft_out_new[k + cf_offset];
617 : }
618 : }
619 : else
620 : {
621 2723 : hFbMixer->first_frame[ch] = 0;
622 :
623 2355683 : for ( k = 0; k < frame_len; k++ )
624 : {
625 2352960 : ppOut_pcm[ch][k] = pMdft_out_new[k + cf_offset];
626 : }
627 : }
628 :
629 274579 : return;
630 : }
631 :
632 :
633 : /*-----------------------------------------------------------------------------------------*
634 : * Function ivas_fb_mixer_process()
635 : *
636 : * Filter bank process
637 : *-----------------------------------------------------------------------------------------*/
638 :
639 159500 : 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 159500 : ivas_filterbank_t *pFb = hFbMixer->pFb;
648 159500 : 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 159500 : pOut_fr_re = Out_fr_re;
657 159500 : pOut_fr_im = Out_fr_im;
658 159500 : pMdft_out[0] = Mdft_out_0;
659 159500 : pMdft_out[1] = Mdft_out_1;
660 :
661 434079 : 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 823737 : for ( hist = 0; hist < 2; hist++ )
665 : {
666 549158 : set_zero( pOut_fr_re, frame_len );
667 549158 : set_zero( pOut_fr_im, frame_len );
668 :
669 3358990 : for ( j = 0; j < hFbMixer->fb_cfg->num_in_chans; j++ )
670 : {
671 2809832 : 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 814664 : float *pFb_inFR_re = hFbMixer->ppFilterbank_inFR_re[j];
676 814664 : float *pFb_inFR_im = hFbMixer->ppFilterbank_inFR_im[j];
677 :
678 814664 : set_zero( filterbank_mixer_bins_re, frame_len );
679 814664 : set_zero( filterbank_mixer_bins_im, frame_len );
680 :
681 10451632 : for ( i = 0; i < num_bands; i++ )
682 : {
683 9636968 : int16_t start_offset = pFb->fb_consts.pFilterbank_bins_start_offset[i];
684 9636968 : int16_t num_bins = pFb->fb_consts.pFilterbank_bins_per_band[i];
685 :
686 9636968 : float mixer_const = hFbMixer->prior_mixer[ch][j][i];
687 :
688 9636968 : pFilterbank_bin_to_band_re = pFb->fb_consts.ppFilterbank_FRs[0][i];
689 9636968 : pFilterbank_bin_to_band_im = pFb->fb_consts.ppFilterbank_FRs[1][i];
690 :
691 2973164980 : for ( k = start_offset; k < num_bins + start_offset; k++ )
692 : {
693 2963528012 : filterbank_mixer_bins_re[k] += *pFilterbank_bin_to_band_re++ * mixer_const;
694 2963528012 : filterbank_mixer_bins_im[k] += *pFilterbank_bin_to_band_im++ * mixer_const;
695 : }
696 9636968 : hFbMixer->prior_mixer[ch][j][i] = mixer_mat[ch][j][i];
697 : }
698 :
699 629247304 : for ( k = 0; k < frame_len; k++ )
700 : {
701 : float temp_out_re, temp_out_im;
702 :
703 628432640 : 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 628432640 : pOut_fr_re[k] += temp_out_re;
707 628432640 : pOut_fr_im[k] += temp_out_im;
708 : }
709 : }
710 : }
711 :
712 549158 : ivas_imdft( pOut_fr_re, pOut_fr_im, pMdft_out[hist], frame_len );
713 : }
714 :
715 274579 : ivas_fb_mixer_cross_fading( hFbMixer, ppOut_pcm, pMdft_out[0], pMdft_out[1], ch, frame_len, frame_len );
716 : }
717 :
718 159500 : return;
719 : }
720 :
721 :
722 : /*-----------------------------------------------------------------------------------------*
723 : * Function ivas_fb_mixer_get_in_out_mapping()
724 : *
725 : *
726 : *-----------------------------------------------------------------------------------------*/
727 :
728 159500 : 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 159500 : set_s( (int16_t *) in_out_mixer_map, 0, IVAS_MAX_FB_MIXER_OUT_CH * IVAS_MAX_SPAR_FB_MIXER_IN_CH );
736 :
737 159500 : if ( fb_cfg->active_w_mixing )
738 : {
739 88502 : for ( i = 0; i < fb_cfg->num_out_chans; i++ )
740 : {
741 221255 : for ( j = 0; j < fb_cfg->num_in_chans; j++ )
742 : {
743 177004 : in_out_mixer_map[i][j] = 1;
744 : }
745 : }
746 : }
747 : else
748 : {
749 115249 : in_out_mixer_map[0][0] = 1; /* W depends on only W input*/
750 345577 : for ( i = 1; i < fb_cfg->num_out_chans; i++ )
751 : {
752 230328 : in_out_mixer_map[i][0] = 1;
753 : }
754 : }
755 :
756 159500 : return;
757 : }
758 :
759 :
760 : /*-----------------------------------------------------------------------------------------*
761 : * Function ivas_calculate_abs_fr()
762 : *
763 : * Function to calculate number of active bands
764 : *-----------------------------------------------------------------------------------------*/
765 :
766 1136 : 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 1136 : int16_t bands = pFb->filterbank_num_bands;
774 1136 : int16_t i, j, num_active_bands = 0;
775 1136 : int16_t idx_short_stride_bin_to_band = 0;
776 :
777 14444 : for ( i = 0; i < bands; i++ )
778 : {
779 13308 : const float *long_mdft_ptr_re = pFb->fb_consts.ppFilterbank_FRs[0][i];
780 13308 : const float *long_mdft_ptr_im = pFb->fb_consts.ppFilterbank_FRs[1][i];
781 13308 : int16_t start_offset = pFb->fb_consts.pFilterbank_bins_start_offset[i];
782 13308 : int16_t num_bins = pFb->fb_consts.pFilterbank_bins_per_band[i];
783 13308 : int16_t short_mdft_start_bin = -1;
784 : float short_stride_pow_spec[MDFT_FB_BANDS_240];
785 13308 : float short_stride_nrg = 0.0f;
786 13308 : float cldfb_nrg = 0.0f;
787 13308 : int16_t short_stride = pFb->fb_bin_to_band.short_stride;
788 13308 : const int16_t num_bins_per_short_stride_bin = ( const int16_t )( ( sampling_rate / FRAMES_PER_SEC ) / short_stride );
789 13308 : 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 13308 : float short_stride_max_per_spar_band = 1e-9f;
791 :
792 : /*loop over all stored Filter Bank Response MDFT coefficients*/
793 13308 : set_f( short_stride_pow_spec, 0, MDFT_FB_BANDS_240 );
794 4040314 : for ( j = start_offset; j < num_bins + start_offset; j++ )
795 : {
796 : float sq_abs;
797 :
798 : /*calculate bin energy */
799 4027006 : IVAS_CALCULATE_SQ_ABS( *long_mdft_ptr_re, *long_mdft_ptr_im, sq_abs );
800 4027006 : long_mdft_ptr_re++;
801 4027006 : long_mdft_ptr_im++;
802 :
803 : /* accumulate bin energies within a short stride bin */
804 4027006 : short_stride_nrg += sq_abs;
805 4027006 : if ( !( ( j + 1 ) % num_bins_per_short_stride_bin ) )
806 : { /* new short stride bin */
807 1004601 : short_stride_pow_spec[j / num_bins_per_short_stride_bin] = short_stride_nrg; /* energy rather than magnitude works better for covariance weighting*/
808 1004601 : short_stride_max_per_spar_band = max( short_stride_nrg, short_stride_max_per_spar_band ); /*compute highest magnitude per band*/
809 1004601 : short_stride_nrg = 0.0f;
810 : }
811 :
812 : /* accumulate bin energies within a CLDFB band */
813 4027006 : cldfb_nrg += sq_abs;
814 4027006 : if ( !( ( j + 1 ) % num_bins_per_cldfb_band ) )
815 : {
816 249141 : pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j / num_bins_per_cldfb_band][i] = sqrtf( cldfb_nrg );
817 249141 : cldfb_nrg = 0.0f;
818 : }
819 : }
820 :
821 : /*loop over the short MDFT bins*/
822 2507388 : for ( j = 0; j < short_stride; j++ )
823 : {
824 : /* normalize and sparsify the energies */
825 2494080 : short_stride_pow_spec[j] /= short_stride_max_per_spar_band;
826 2494080 : short_stride_pow_spec[j] = max( short_stride_pow_spec[j] - 0.3f, 0.0f ) / 0.7f;
827 2494080 : if ( short_stride_pow_spec[j] > 0.0f )
828 : {
829 225568 : assert( idx_short_stride_bin_to_band < 2 * MDFT_FB_BANDS_240 ); /* array size of p_short_stride_bin_to_band */
830 225568 : if ( short_mdft_start_bin == -1 )
831 : {
832 13308 : short_mdft_start_bin = j;
833 13308 : pFb->fb_bin_to_band.p_short_stride_start_bin_per_band[i] = j;
834 13308 : 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 225568 : pFb->fb_bin_to_band.p_short_stride_bin_to_band[idx_short_stride_bin_to_band++] = short_stride_pow_spec[j];
837 225568 : pFb->fb_bin_to_band.p_short_stride_num_bins_per_band[i]++;
838 : }
839 : }
840 : }
841 :
842 : /*loop over CLDFB bands*/
843 53636 : for ( j = 0; j < pFb->fb_bin_to_band.num_cldfb_bands; j++ )
844 : {
845 52500 : float sum_over_spar_bands = 0.0f;
846 52500 : float max_spar_band_contribution = 0.0f;
847 52500 : int16_t spar_start = 0;
848 52500 : int16_t any_non_zero = 0;
849 :
850 676020 : for ( i = 0; i < bands; i++ )
851 : {
852 623520 : sum_over_spar_bands += pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i];
853 623520 : if ( pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i] > max_spar_band_contribution )
854 : {
855 154519 : max_spar_band_contribution = pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i];
856 154519 : pFb->fb_bin_to_band.p_cldfb_map_to_spar_band[j] = i;
857 : }
858 : }
859 52500 : sum_over_spar_bands = max( sum_over_spar_bands, EPSILON );
860 676020 : for ( i = 0; i < bands; i++ )
861 : {
862 623520 : if ( pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i] == 0.0f && !any_non_zero )
863 : {
864 333149 : spar_start++;
865 : }
866 : else
867 : {
868 290371 : any_non_zero = 1;
869 : }
870 623520 : pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[j][i] /= sum_over_spar_bands;
871 : }
872 52500 : pFb->fb_bin_to_band.p_spar_start_bands[j] = spar_start;
873 : }
874 :
875 1136 : frame_len = (int16_t) ( sampling_rate / FRAMES_PER_SEC );
876 :
877 1136 : 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 14444 : for ( i = 0; i < bands; i++ )
881 : {
882 13308 : const float *pFilterbank_bin_to_band_re = pFb->fb_consts.ppFilterbank_FRs[0][i];
883 13308 : const float *pFilterbank_bin_to_band_im = pFb->fb_consts.ppFilterbank_FRs[1][i];
884 :
885 13308 : int16_t start_offset = pFb->fb_consts.pFilterbank_bins_start_offset[i];
886 13308 : int16_t num_bins = pFb->fb_consts.pFilterbank_bins_per_band[i];
887 13308 : int16_t idx = 0;
888 13308 : int16_t abs_active_bins = pFb->fb_bin_to_band.pFb_active_bins_per_band[i];
889 13308 : int16_t abs_start_offset = pFb->fb_bin_to_band.pFb_start_bin_per_band[i];
890 :
891 4040314 : for ( j = start_offset; j < num_bins + start_offset; j++ )
892 : {
893 4027006 : float temp = 0;
894 :
895 4027006 : IVAS_CALCULATE_ABS( *pFilterbank_bin_to_band_re, *pFilterbank_bin_to_band_im, temp );
896 :
897 4027006 : pFilterbank_bin_to_band_re++;
898 4027006 : pFilterbank_bin_to_band_im++;
899 :
900 4027006 : temp -= 0.3f;
901 :
902 4027006 : if ( temp < 0.0f )
903 : {
904 2886039 : temp = 0;
905 : }
906 :
907 4027006 : if ( j < ( abs_active_bins + abs_start_offset ) && j >= abs_start_offset && alloc_fb_resp != -1 )
908 : {
909 296911 : pFb->fb_bin_to_band.pFb_bin_to_band[i][idx++] = temp;
910 : }
911 :
912 4027006 : ppFilterbank_FRs_s[j] += temp;
913 : }
914 : }
915 :
916 841136 : for ( i = 0; i < frame_len; i++ )
917 : {
918 840000 : if ( ppFilterbank_FRs_s[i] < 0.1f )
919 : {
920 0 : ppFilterbank_FRs_s[i] = 0.1f;
921 : }
922 : }
923 :
924 1136 : if ( alloc_fb_resp != -1 )
925 : {
926 3731 : for ( j = 0; j < bands; j++ )
927 : {
928 3438 : int16_t abs_active_bins = pFb->fb_bin_to_band.pFb_active_bins_per_band[j];
929 3438 : int16_t abs_start_offset = pFb->fb_bin_to_band.pFb_start_bin_per_band[j];
930 :
931 300349 : for ( i = 0; i < abs_active_bins; i++ )
932 : {
933 296911 : 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 1136 : return num_active_bands;
944 : }
945 :
946 :
947 : /*-----------------------------------------------------------------------------------------*
948 : * Function ivas_get_active_bins()
949 : *
950 : *
951 : *-----------------------------------------------------------------------------------------*/
952 :
953 2272 : 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 2272 : if ( sampling_rate == 32000 )
963 : {
964 918 : sr_idx = 1;
965 : }
966 1354 : else if ( sampling_rate == 16000 )
967 : {
968 324 : sr_idx = 2;
969 : }
970 : else
971 : {
972 1030 : sr_idx = 0;
973 : }
974 :
975 2272 : *pActive_bins_abs = ivas_fb_abs_bins_per_band_12band_1ms[sr_idx];
976 2272 : *pActive_bins = ivas_fb_bins_per_band_12band_1ms[sr_idx];
977 2272 : *pStart_offset_abs = ivas_fb_abs_bins_start_offset_12band_1ms[sr_idx];
978 2272 : *pStart_offset = ivas_fb_bins_start_offset_12band_1ms[sr_idx];
979 :
980 2272 : return;
981 : }
982 :
983 :
984 : /*-----------------------------------------------------------------------------------------*
985 : * Function ivas_filterbank_setup()
986 : *
987 : * Filterbank setup, initialization
988 : *-----------------------------------------------------------------------------------------*/
989 :
990 2301 : 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 2301 : const int16_t *pAll_bins_start_offset = NULL;
997 2301 : const int16_t *pAll_bins_per_band = NULL;
998 2301 : const int16_t *pAll_bins_start_offset_abs = NULL;
999 2301 : const int16_t *pAll_bins_per_band_abs = NULL;
1000 2301 : const int16_t *pAll_bins_per_band_48k = NULL;
1001 : ivas_error error;
1002 2301 : IVAS_FB_CFG *pCfg = hFbMixer->fb_cfg;
1003 :
1004 2301 : error = IVAS_ERR_OK;
1005 :
1006 2301 : if ( pCfg->num_out_chans > 0 )
1007 : {
1008 1136 : hFbMixer->pFb->filterbank_num_bands = ivas_get_num_bands( sampling_rate );
1009 :
1010 1136 : 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 1136 : if ( pCfg->fb_latency == NS2SA( sampling_rate, DELAY_FB_1_NS ) )
1013 : {
1014 1136 : pAll_fb_fr[0] = ivas_fb_fr_12band_1ms_re;
1015 1136 : pAll_fb_fr[1] = ivas_fb_fr_12band_1ms_im;
1016 1136 : 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 2301 : hFbMixer->cross_fade_end_offset = pCfg->fade_len + pCfg->pcm_offset;
1025 2301 : hFbMixer->cross_fade_start_offset = hFbMixer->cross_fade_end_offset - pCfg->fade_len;
1026 2301 : hFbMixer->ana_window_offset = pCfg->fb_latency + pCfg->pcm_offset;
1027 :
1028 2301 : 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 2301 : 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 2301 : if ( pCfg->num_out_chans > 0 )
1039 : {
1040 1136 : ivas_filterbank_t *pFb = hFbMixer->pFb;
1041 1136 : int16_t offset = 0;
1042 :
1043 1136 : pFb->fb_consts.pFilterbank_bins_per_band = pAll_bins_per_band;
1044 1136 : pFb->fb_consts.pFilterbank_bins_start_offset = pAll_bins_start_offset;
1045 1136 : pFb->fb_bin_to_band.pFb_active_bins_per_band = pAll_bins_per_band_abs;
1046 1136 : 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 1136 : 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 1136 : pFb->fb_bin_to_band.short_stride = (int16_t) ( ( sampling_rate / FRAMES_PER_SEC ) / 4 );
1052 1136 : set_f( pFb->fb_bin_to_band.p_short_stride_bin_to_band, 0.0f, 2 * MDFT_FB_BANDS_240 );
1053 1136 : set_s( pFb->fb_bin_to_band.p_cldfb_map_to_spar_band, 0, CLDFB_NO_CHANNELS_MAX );
1054 1136 : set_s( pFb->fb_bin_to_band.p_spar_start_bands, 0, CLDFB_NO_CHANNELS_MAX );
1055 :
1056 23856 : for ( j = 0; j < IVAS_MAX_NUM_FB_BANDS; j++ )
1057 : {
1058 22720 : pFb->fb_bin_to_band.p_short_stride_num_bins_per_band[j] = 0; /* aka num_active_bins per SPAR band */
1059 22720 : pFb->fb_bin_to_band.p_short_stride_start_bin_per_band[j] = 0; /* first considered bin index per SPAR band */
1060 22720 : pFb->fb_bin_to_band.pp_short_stride_bin_to_band[j] = NULL;
1061 1385920 : for ( i = 0; i < CLDFB_NO_CHANNELS_MAX; i++ )
1062 : {
1063 1363200 : pFb->fb_bin_to_band.pp_cldfb_weights_per_spar_band[i][j] = 0.0f;
1064 : }
1065 : }
1066 1136 : if ( sampling_rate == 48000 )
1067 : {
1068 6695 : for ( j = 0; j < pFb->filterbank_num_bands; j++ )
1069 : {
1070 6180 : pFb->fb_consts.ppFilterbank_FRs[0][j] = &pAll_fb_fr[0][offset];
1071 6180 : pFb->fb_consts.ppFilterbank_FRs[1][j] = &pAll_fb_fr[1][offset];
1072 6180 : offset += pFb->fb_consts.pFilterbank_bins_per_band[j];
1073 : }
1074 :
1075 515 : 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 2301 : return error;
1131 : }
1132 :
1133 :
1134 : /*-----------------------------------------------------------------------------------------*
1135 : * Function ivas_fb_mixer_get_window()
1136 : *
1137 : *
1138 : *-----------------------------------------------------------------------------------------*/
1139 :
1140 4602 : 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 4602 : error = IVAS_ERR_OK;
1149 :
1150 4602 : if ( fade_len == NS2SA( sampling_rate, DELAY_FB_4_NS ) )
1151 : {
1152 1764 : switch ( sampling_rate )
1153 : {
1154 1143 : case 48000:
1155 1143 : *pWindow = ivas_fb_cf_4ms_48k;
1156 1143 : 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 2838 : else if ( fade_len == NS2SA( sampling_rate, DELAY_FB_1_NS ) )
1168 : {
1169 2838 : switch ( sampling_rate )
1170 : {
1171 1949 : case 48000:
1172 1949 : *pWindow = ivas_fb_cf_1ms_48k;
1173 1949 : break;
1174 727 : case 32000:
1175 727 : *pWindow = ivas_fb_cf_1ms_32k;
1176 727 : 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 4602 : 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 : }
|