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 "ivas_stat_dec.h"
39 : #include "prot.h"
40 : #include "string.h"
41 : #include "ivas_prot.h"
42 : #include "ivas_prot_rend.h"
43 : #include "ivas_rom_com.h"
44 : #include "ivas_rom_dec.h"
45 : #include "ivas_stat_com.h"
46 : #include <math.h>
47 : #include <assert.h>
48 : #include "wmc_auto.h"
49 :
50 :
51 : /*-------------------------------------------------------------------*
52 : * Local function prototypes
53 : *--------------------------------------------------------------------*/
54 :
55 : static ivas_error ivas_spar_dec_MD( Decoder_Struct *st_ivas, Decoder_State *st0 );
56 :
57 :
58 : /*-------------------------------------------------------------------------
59 : * ivas_spar_dec_open()
60 : *
61 : * Allocate and initialize SPAR decoder handle and sub-handles
62 : *------------------------------------------------------------------------*/
63 :
64 4911 : ivas_error ivas_spar_dec_open(
65 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
66 : const int16_t spar_reconfig_flag /* i : SPAR reconfiguration flag */
67 : )
68 : {
69 : SPAR_DEC_HANDLE hSpar;
70 : ivas_error error;
71 : int16_t sba_order_internal, num_channels_internal;
72 : IVAS_FB_CFG *fb_cfg;
73 : int16_t i, j, b, active_w_mixing;
74 : int32_t output_Fs;
75 : int16_t num_decor_chs, map_idx;
76 :
77 4911 : error = IVAS_ERR_OK;
78 :
79 4911 : sba_order_internal = min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
80 :
81 4911 : num_channels_internal = ivas_sba_get_nchan_metadata( sba_order_internal, st_ivas->hDecoderConfig->ivas_total_brate );
82 :
83 4911 : hSpar = st_ivas->hSpar;
84 :
85 4911 : if ( !spar_reconfig_flag )
86 : {
87 : /* SPAR decoder handle */
88 846 : if ( ( hSpar = (SPAR_DEC_HANDLE) malloc( sizeof( SPAR_DEC_DATA ) ) ) == NULL )
89 : {
90 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR decoder" );
91 : }
92 : }
93 :
94 4911 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
95 4911 : if ( num_channels_internal > ( SBA_HOA2_ORDER + 1 ) * ( SBA_HOA2_ORDER + 1 ) )
96 : {
97 213 : num_decor_chs = IVAS_HBR_MAX_DECOR_CHS;
98 : }
99 : else
100 : {
101 4698 : num_decor_chs = num_channels_internal - 1;
102 : }
103 :
104 : /* TD decorr. */
105 4911 : if ( ( st_ivas->ivas_format == SBA_FORMAT ) && ( ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO ) || ( st_ivas->hDecoderConfig->ivas_total_brate >= IVAS_256k && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_FOA ) ) )
106 : {
107 693 : hSpar->hTdDecorr = NULL;
108 : }
109 : else
110 : {
111 4218 : if ( ( error = ivas_td_decorr_dec_open( &hSpar->hTdDecorr, output_Fs, num_decor_chs + 1, 1 ) ) != IVAS_ERR_OK )
112 : {
113 0 : return error;
114 : }
115 : }
116 :
117 : /* MD handle */
118 4911 : if ( ( error = ivas_spar_md_dec_open( &hSpar->hMdDec, st_ivas->hDecoderConfig, num_channels_internal, sba_order_internal, st_ivas->sid_format, st_ivas->last_active_ivas_total_brate ) ) != IVAS_ERR_OK )
119 : {
120 0 : return error;
121 : }
122 4911 : hSpar->hMdDec->td_decorr_flag = 1;
123 4911 : if ( hSpar->hTdDecorr )
124 : {
125 4218 : hSpar->hTdDecorr->ducking_flag = ivas_spar_br_table_consts[hSpar->hMdDec->table_idx].td_ducking;
126 : }
127 :
128 : /* set FB config. */
129 4911 : active_w_mixing = -1;
130 4911 : if ( ( error = ivas_fb_set_cfg( &fb_cfg, SBA_FORMAT, num_channels_internal, num_channels_internal, active_w_mixing, output_Fs, 0 ) ) != IVAS_ERR_OK )
131 : {
132 0 : return error;
133 : }
134 4911 : fb_cfg->pcm_offset = NS2SA( output_Fs, DELAY_FB_1_NS + IVAS_ENC_DELAY_NS + IVAS_DEC_DELAY_NS );
135 4911 : fb_cfg->remix_order = remix_order_set[hSpar->hMdDec->spar_md_cfg.remix_unmix_order];
136 :
137 : /* FB mixer handle */
138 4911 : if ( ( error = ivas_FB_mixer_open( &hSpar->hFbMixer, output_Fs, fb_cfg, spar_reconfig_flag ) ) != IVAS_ERR_OK )
139 : {
140 0 : return error;
141 : }
142 :
143 : /* AGC handle */
144 4911 : if ( ( error = ivas_spar_agc_dec_open( &hSpar->hAgcDec, output_Fs ) ) != IVAS_ERR_OK )
145 : {
146 0 : return error;
147 : }
148 :
149 : /* PCA handle */
150 4911 : hSpar->hPCA = NULL;
151 4911 : if ( st_ivas->hDecoderConfig->ivas_total_brate == PCA_BRATE && sba_order_internal == 1 )
152 : {
153 111 : if ( ( hSpar->hPCA = (PCA_DEC_STATE *) malloc( sizeof( PCA_DEC_STATE ) ) ) == NULL )
154 : {
155 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for PCA decoder" );
156 : }
157 :
158 111 : ivas_pca_dec_init( hSpar->hPCA );
159 : }
160 :
161 : /* mixer_mat intitialization */
162 28080 : for ( i = 0; i < num_channels_internal; i++ )
163 : {
164 148290 : for ( j = 0; j < num_channels_internal; j++ )
165 : {
166 1626573 : for ( b = 0; b < IVAS_MAX_NUM_BANDS; b++ )
167 : {
168 1501452 : hSpar->hMdDec->mixer_mat[i][j][b] = 0.0f;
169 9008712 : for ( int16_t i_ts = 0; i_ts < ( MAX_PARAM_SPATIAL_SUBFRAMES + 1 ); i_ts++ )
170 : {
171 7507260 : hSpar->hMdDec->mixer_mat_prev[i_ts][i][j][b] = 0.0f;
172 : }
173 : }
174 : }
175 : }
176 4911 : hSpar->i_subframe = 0;
177 4911 : hSpar->AGC_flag = 0;
178 :
179 : /*-----------------------------------------------------------------*
180 : * Configuration - set SPAR high-level parameters
181 : *-----------------------------------------------------------------*/
182 :
183 4911 : ivas_spar_config( st_ivas->hDecoderConfig->ivas_total_brate, sba_order_internal, &st_ivas->nchan_transport, &st_ivas->nSCE, &st_ivas->nCPE, &hSpar->core_nominal_brate, st_ivas->sid_format );
184 :
185 4911 : switch ( sba_order_internal )
186 : {
187 4134 : case 1:
188 4134 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_FOA;
189 4134 : break;
190 219 : case 2:
191 219 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_HOA2;
192 219 : break;
193 558 : case 3:
194 558 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_HOA3;
195 558 : break;
196 : }
197 :
198 4911 : ivas_output_init( &( st_ivas->hTransSetup ), st_ivas->transport_config );
199 :
200 4911 : set_s( hSpar->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
201 4911 : set_s( hSpar->subframe_nbslots, JBM_CLDFB_SLOTS_IN_SUBFRAME, DEFAULT_JBM_SUBFRAMES_5MS );
202 4911 : hSpar->nb_subframes = DEFAULT_JBM_SUBFRAMES_5MS;
203 4911 : hSpar->subframes_rendered = 0;
204 4911 : hSpar->slots_rendered = 0;
205 4911 : hSpar->num_slots = DEFAULT_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME;
206 :
207 : /* init render timeslot mapping */
208 4911 : set_s( hSpar->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
209 83487 : for ( map_idx = 0; map_idx < DEFAULT_JBM_CLDFB_TIMESLOTS; map_idx++ )
210 : {
211 78576 : hSpar->render_to_md_map[map_idx] = map_idx;
212 : }
213 :
214 : /* allocate transport channels*/
215 4911 : if ( st_ivas->hTcBuffer == NULL )
216 : {
217 : int16_t nchan_to_allocate;
218 : int16_t nchan_tc;
219 : TC_BUFFER_MODE buffer_mode;
220 : int16_t granularity;
221 :
222 846 : buffer_mode = TC_BUFFER_MODE_RENDERER;
223 846 : nchan_tc = ivas_jbm_dec_get_num_tc_channels( st_ivas );
224 846 : nchan_to_allocate = num_channels_internal;
225 :
226 846 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
227 : {
228 57 : nchan_to_allocate += st_ivas->nchan_ism;
229 : }
230 :
231 846 : if ( ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO ) )
232 : {
233 42 : if ( ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO ) )
234 : {
235 3 : nchan_tc = st_ivas->hDecoderConfig->nchan_out + st_ivas->nchan_ism;
236 3 : nchan_to_allocate = nchan_tc;
237 : }
238 : else
239 : {
240 39 : buffer_mode = TC_BUFFER_MODE_BUFFER;
241 39 : nchan_tc = st_ivas->hDecoderConfig->nchan_out;
242 39 : nchan_to_allocate = nchan_tc;
243 : }
244 : }
245 804 : else if ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC )
246 : {
247 147 : nchan_to_allocate = 2 * BINAURAL_CHANNELS;
248 : }
249 :
250 846 : granularity = ivas_jbm_dec_get_render_granularity( st_ivas->renderer_type, ivas_renderer_secondary_select( st_ivas ), output_Fs );
251 :
252 846 : if ( ( error = ivas_jbm_dec_tc_buffer_open( st_ivas, buffer_mode, nchan_tc, nchan_to_allocate, nchan_to_allocate, granularity ) ) != IVAS_ERR_OK )
253 : {
254 0 : return error;
255 : }
256 : }
257 :
258 4911 : st_ivas->hSpar = hSpar;
259 :
260 4911 : return error;
261 : }
262 :
263 :
264 : /*-------------------------------------------------------------------------
265 : * ivas_spar_dec_close()
266 : *
267 : * Deallocate SPAR handle
268 : *------------------------------------------------------------------------*/
269 :
270 5949 : void ivas_spar_dec_close(
271 : SPAR_DEC_HANDLE *hSpar, /* i/o: SPAR decoder handle */
272 : const int32_t output_Fs, /* i : output sampling rate */
273 : const int16_t spar_reconfig_flag /* i : SPAR reconfiguration flag */
274 : )
275 : {
276 5949 : if ( hSpar == NULL || *hSpar == NULL )
277 : {
278 1038 : return;
279 : }
280 :
281 : /* MD handle */
282 4911 : ivas_spar_md_dec_close( &( *hSpar )->hMdDec );
283 :
284 : /* TD decorrelator handle */
285 4911 : ivas_td_decorr_dec_close( &( *hSpar )->hTdDecorr );
286 :
287 : /* FB mixer handle */
288 4911 : ivas_FB_mixer_close( &( *hSpar )->hFbMixer, output_Fs, spar_reconfig_flag );
289 :
290 : /* AGC */
291 4911 : ivas_spar_agc_dec_close( &( *hSpar )->hAgcDec );
292 :
293 : /* PCA */
294 4911 : if ( ( *hSpar )->hPCA != NULL )
295 : {
296 75 : free( ( *hSpar )->hPCA );
297 75 : ( *hSpar )->hPCA = NULL;
298 : }
299 :
300 4911 : if ( !spar_reconfig_flag )
301 : {
302 846 : free( ( *hSpar ) );
303 846 : ( *hSpar ) = NULL;
304 : }
305 :
306 4911 : return;
307 : }
308 :
309 :
310 : /*-------------------------------------------------------------------*
311 : * ivas_spar_dec()
312 : *
313 : * Principal IVAS SPAR decoder routine
314 : *-------------------------------------------------------------------*/
315 :
316 474039 : ivas_error ivas_spar_dec(
317 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder struct */
318 : int16_t *nb_bits_read /* o : number of MD bits read */
319 : )
320 : {
321 : DECODER_CONFIG_HANDLE hDecoderConfig;
322 : int16_t i, nb_bits_read_orig;
323 : Decoder_State *st0;
324 : int16_t next_bit_pos_orig, last_bit_pos;
325 : uint16_t bstr_meta[MAX_BITS_METADATA], *bit_stream_orig;
326 : ivas_error error;
327 :
328 474039 : push_wmops( "ivas_spar_decode" );
329 474039 : error = IVAS_ERR_OK;
330 474039 : hDecoderConfig = st_ivas->hDecoderConfig;
331 :
332 474039 : st0 = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
333 474039 : bit_stream_orig = st0->bit_stream;
334 474039 : next_bit_pos_orig = st0->next_bit_pos;
335 :
336 474039 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
337 : {
338 111021 : last_bit_pos = (int16_t) ( ( hDecoderConfig->ivas_total_brate / FRAMES_PER_SEC ) - 1 ) - nb_bits_read[1];
339 : }
340 : else
341 : {
342 363018 : *nb_bits_read = 0;
343 363018 : last_bit_pos = 0;
344 : }
345 :
346 : /* read DirAC bitstream */
347 474039 : if ( st_ivas->hQMetaData != NULL )
348 : {
349 474039 : ivas_dirac_dec_read_BS( hDecoderConfig->ivas_total_brate, st0, st_ivas->hDirAC, st_ivas->hSpatParamRendCom, st_ivas->hQMetaData, nb_bits_read, last_bit_pos, ivas_get_hodirac_flag( hDecoderConfig->ivas_total_brate, st_ivas->sba_analysis_order ), st_ivas->nchan_transport, st_ivas->hSpar->dirac_to_spar_md_bands );
350 : }
351 :
352 474039 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
353 : {
354 111021 : last_bit_pos = (int16_t) ( ( hDecoderConfig->ivas_total_brate / FRAMES_PER_SEC ) - 1 ) - nb_bits_read[1];
355 : }
356 : else
357 : {
358 363018 : last_bit_pos = (int16_t) ( ( hDecoderConfig->ivas_total_brate / FRAMES_PER_SEC ) - 1 );
359 : }
360 :
361 474039 : if ( !st0->bfi && hDecoderConfig->ivas_total_brate == IVAS_SID_5k2 )
362 : {
363 972 : last_bit_pos -= ( SID_FORMAT_NBITS + SBA_PLANAR_BITS + SBA_ORDER_BITS );
364 : }
365 474039 : nb_bits_read_orig = *nb_bits_read;
366 474039 : last_bit_pos -= nb_bits_read_orig;
367 :
368 : /* reverse the bitstream for easier reading of indices */
369 733343646 : for ( i = 0; i < min( MAX_BITS_METADATA, last_bit_pos ); i++ )
370 : {
371 732869607 : bstr_meta[i] = st_ivas->bit_stream[last_bit_pos - i];
372 : }
373 474039 : st0->bit_stream = bstr_meta;
374 474039 : st0->next_bit_pos = 0;
375 474039 : st0->bits_frame = min( MAX_BITS_METADATA, last_bit_pos + 1 );
376 :
377 474039 : if ( !st0->bfi )
378 : {
379 454731 : st0->total_brate = hDecoderConfig->ivas_total_brate; /* to avoid BER detect */
380 : }
381 :
382 : /*---------------------------------------------------------------------*
383 : * Decode SPAR metadata
384 : *---------------------------------------------------------------------*/
385 :
386 474039 : if ( ( error = ivas_spar_dec_MD( st_ivas, st0 ) ) != IVAS_ERR_OK )
387 : {
388 0 : return error;
389 : }
390 :
391 474039 : *nb_bits_read = st0->next_bit_pos + nb_bits_read_orig;
392 474039 : st0->bit_stream = bit_stream_orig;
393 474039 : st0->next_bit_pos = next_bit_pos_orig;
394 :
395 474039 : if ( !st0->bfi && hDecoderConfig->ivas_total_brate == IVAS_SID_5k2 )
396 : {
397 : int16_t zero_pad_bits;
398 972 : *nb_bits_read += SID_FORMAT_NBITS + SBA_PLANAR_BITS + SBA_ORDER_BITS;
399 972 : zero_pad_bits = (int16_t) ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - *nb_bits_read;
400 972 : assert( zero_pad_bits <= 1 );
401 972 : *nb_bits_read += zero_pad_bits;
402 : }
403 :
404 474039 : pop_wmops();
405 :
406 474039 : return error;
407 : }
408 :
409 :
410 : /*---------------------------------------------------------------------*
411 : * Function ivas_get_spar_table_idx_from_coded_idx()
412 : *
413 : * Get SPAR table index
414 : *---------------------------------------------------------------------*/
415 :
416 447564 : static int16_t ivas_get_spar_table_idx_from_coded_idx(
417 : const int32_t ivas_total_brate, /* i : IVAS total bitrate */
418 : const int16_t sba_order, /* i : Ambisonic (SBA) order */
419 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
420 : int16_t *bitlen /* o : number of bits */
421 : )
422 : {
423 : int16_t table_idx, ind1[IVAS_SPAR_BR_TABLE_LEN];
424 : int16_t i, j, ind2;
425 :
426 447564 : j = 0;
427 9398844 : for ( i = 0; i < IVAS_SPAR_BR_TABLE_LEN; i++ )
428 : {
429 8951280 : ind1[j] = 0;
430 8951280 : if ( ( ivas_spar_br_table_consts[i].ivas_total_brate == ivas_total_brate ) && ( ivas_spar_br_table_consts[i].sba_order == sba_order ) )
431 : {
432 447564 : ind1[j++] = i;
433 : }
434 : }
435 :
436 447564 : assert( j > 0 );
437 447564 : *bitlen = ivas_get_bits_to_encode( j - 1 );
438 :
439 447564 : ind2 = get_next_indice( st0, *bitlen );
440 :
441 447564 : table_idx = ind1[ind2];
442 :
443 447564 : return table_idx;
444 : }
445 :
446 :
447 : /*---------------------------------------------------------------------*
448 : * Function ivas_parse_spar_header()
449 : *
450 : * Get SPAR table index
451 : *---------------------------------------------------------------------*/
452 :
453 447564 : static int16_t ivas_parse_spar_header(
454 : const int32_t ivas_total_brate, /* i : IVAS total bitrate */
455 : const int16_t sba_order, /* i : Ambisonic (SBA) order */
456 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
457 : int16_t *table_idx )
458 : {
459 : int16_t bitlen, bwidth;
460 :
461 447564 : *table_idx = ivas_get_spar_table_idx_from_coded_idx( ivas_total_brate, sba_order, st0, &bitlen );
462 :
463 447564 : bwidth = ivas_spar_br_table_consts[( *table_idx )].bwidth;
464 :
465 447564 : return bwidth;
466 : }
467 :
468 :
469 1743840 : static float get_random_number(
470 : int16_t *seed )
471 : {
472 1743840 : float x = (float) own_random( seed ) / 32768.0f;
473 :
474 1743840 : return x;
475 : }
476 :
477 :
478 33372 : static float matrix_det(
479 : const float a00,
480 : const float a01,
481 : const float a10,
482 : const float a11 )
483 : {
484 33372 : return a00 * a11 - a01 * a10;
485 : }
486 :
487 :
488 2781 : static void matrix_inverse(
489 : float in[3][3],
490 : float out[3][3],
491 : const int16_t size )
492 : {
493 : float det, fac;
494 2781 : float eps = 1e-16f;
495 :
496 2781 : if ( size == 1 )
497 : {
498 0 : out[0][0] = 1.0f / max( in[0][0], eps );
499 :
500 0 : return;
501 : }
502 2781 : else if ( size == 2 )
503 : {
504 0 : det = matrix_det( in[0][1], in[0][1], in[1][0], in[1][1] );
505 0 : fac = 1.0f / max( det, eps );
506 :
507 0 : out[0][0] = in[1][1] * fac;
508 0 : out[1][0] = in[1][0] * ( -fac );
509 :
510 0 : out[0][1] = in[0][1] * ( -fac );
511 0 : out[1][1] = in[0][0] * fac;
512 :
513 0 : return;
514 : }
515 :
516 2781 : det = in[0][0] * matrix_det( in[1][1], in[1][2], in[2][1], in[2][2] ) - in[1][0] * matrix_det( in[0][1], in[0][2], in[2][1], in[2][2] ) + in[2][0] * matrix_det( in[0][1], in[0][2], in[1][1], in[1][2] );
517 2781 : fac = 1.0f / max( det, eps );
518 :
519 2781 : out[0][0] = matrix_det( in[1][1], in[1][2], in[2][1], in[2][2] ) * fac;
520 2781 : out[1][0] = matrix_det( in[1][0], in[1][2], in[2][0], in[2][2] ) * ( -fac );
521 2781 : out[2][0] = matrix_det( in[1][0], in[1][1], in[2][0], in[2][1] ) * fac;
522 :
523 2781 : out[0][1] = matrix_det( in[0][1], in[0][2], in[2][1], in[2][2] ) * ( -fac );
524 2781 : out[1][1] = matrix_det( in[0][0], in[0][2], in[2][0], in[2][2] ) * fac;
525 2781 : out[2][1] = matrix_det( in[0][0], in[0][1], in[2][0], in[2][1] ) * ( -fac );
526 :
527 2781 : out[0][2] = matrix_det( in[0][1], in[0][2], in[1][1], in[1][2] ) * fac;
528 2781 : out[1][2] = matrix_det( in[0][0], in[0][2], in[1][0], in[1][2] ) * ( -fac );
529 2781 : out[2][2] = matrix_det( in[0][0], in[0][1], in[1][0], in[1][1] ) * fac;
530 :
531 2781 : return;
532 : }
533 :
534 :
535 : /*---------------------------------------------------------------------*
536 : * Function ivas_spar_get_cldfb_gains()
537 : *
538 : *
539 : *---------------------------------------------------------------------*/
540 :
541 2781 : void ivas_spar_get_cldfb_gains(
542 : SPAR_DEC_HANDLE hSpar,
543 : HANDLE_CLDFB_FILTER_BANK cldfbAnaDec0,
544 : HANDLE_CLDFB_FILTER_BANK cldfbSynDec0,
545 : const DECODER_CONFIG_HANDLE hDecoderConfig )
546 : {
547 2781 : float output_Fs = (float) hDecoderConfig->output_Fs;
548 : int16_t pt_len, stride, num_cldfb_bands, decfb_delay;
549 : int16_t encfb_delay, cf_start, cf_end, cf_len;
550 : float *weights;
551 : int16_t ts, cf_cldfb_start, cf_cldfb_end;
552 : float cf_start_s, cf_len_s;
553 : int16_t sample, num_cf_slots, num_samples;
554 : float T[3 * CLDFB_NO_CHANNELS_MAX + 10 * CLDFB_NO_CHANNELS_MAX - CLDFB_NO_CHANNELS_MAX][3];
555 : float Tt_T[3][3];
556 : float Tt_T_inv[3][3];
557 : float Tt_tgt[3];
558 : float ts_inout[CLDFB_NO_CHANNELS_MAX];
559 : float ts_re[CLDFB_NO_CHANNELS_MAX];
560 : float ts_im[CLDFB_NO_CHANNELS_MAX];
561 : float *pp_ts_im[1], *pp_ts_re[1];
562 : float tgt[( 3 - 1 ) * CLDFB_NO_CHANNELS_MAX + 10 * CLDFB_NO_CHANNELS_MAX];
563 : int16_t seed, split_band, slot_row, slot_col, slot, tmp_idx;
564 :
565 2781 : pt_len = cldfbAnaDec0->p_filter_length;
566 2781 : num_cldfb_bands = cldfbAnaDec0->no_channels;
567 :
568 2781 : stride = NS2SA( output_Fs, DELAY_CLDFB_NS );
569 2781 : encfb_delay = NS2SA( output_Fs, IVAS_FB_ENC_DELAY_NS );
570 2781 : decfb_delay = NS2SA( output_Fs, IVAS_FB_DEC_DELAY_NS );
571 :
572 2781 : cf_start = (int16_t) hSpar->hFbMixer->cross_fade_start_offset - encfb_delay + decfb_delay; /* time domain after CLDFB synthesis*/
573 2781 : cf_end = (int16_t) hSpar->hFbMixer->cross_fade_end_offset - encfb_delay + decfb_delay;
574 2781 : cf_len = cf_end - cf_start;
575 2781 : weights = hSpar->hFbMixer->cldfb_cross_fade;
576 :
577 2781 : cf_cldfb_start = (int16_t) ceil( ( cf_start - decfb_delay / 2 ) / (float) stride - 0.5f );
578 2781 : cf_cldfb_end = (int16_t) ( ( cf_start - decfb_delay / 2 + cf_len ) / (float) stride - 0.5f );
579 :
580 2781 : num_cf_slots = cf_cldfb_end - cf_cldfb_start + 1;
581 2781 : num_samples = num_cf_slots * stride + pt_len - stride;
582 2781 : seed = RANDOM_INITSEED;
583 2781 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
584 2781 : pp_ts_im[0] = ts_im;
585 2781 : pp_ts_re[0] = ts_re;
586 2781 : set_f( tgt, 0, ( 3 - 1 ) * CLDFB_NO_CHANNELS_MAX + 10 * CLDFB_NO_CHANNELS_MAX );
587 :
588 2781 : cf_start_s = ( cf_start - decfb_delay / 2 ) / output_Fs;
589 2781 : cf_len_s = hSpar->hFbMixer->cross_fade_end_offset / output_Fs - hSpar->hFbMixer->cross_fade_start_offset / output_Fs;
590 :
591 47277 : for ( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
592 : {
593 44496 : weights[ts] = ( ( ( ts + 0.5f ) * stride / output_Fs ) - cf_start_s ) / cf_len_s;
594 44496 : weights[ts] = max( min( weights[ts], 1.0f ), 0.0f );
595 : }
596 2781 : hSpar->hFbMixer->cldfb_cross_fade_start = cf_cldfb_start;
597 2781 : hSpar->hFbMixer->cldfb_cross_fade_end = cf_cldfb_end;
598 :
599 2781 : if ( num_cf_slots > 3 || pt_len > 10 * CLDFB_NO_CHANNELS_MAX || stride > CLDFB_NO_CHANNELS_MAX || split_band == IVAS_MAX_NUM_BANDS )
600 : {
601 0 : return;
602 : }
603 :
604 : /* optimization*/
605 : /* compute time-domain cross-fade for considered time slots*/
606 2781 : tmp_idx = cf_start - cf_cldfb_start * stride;
607 467805 : for ( sample = 0; sample < cf_len; sample++ )
608 : {
609 : /* increasing window function */
610 465024 : tgt[tmp_idx++] = hSpar->hFbMixer->pFilterbank_cross_fade[sample];
611 : }
612 :
613 1049085 : for ( ; tmp_idx < num_samples; tmp_idx++ )
614 : {
615 : /* fill up with ones*/
616 1046304 : tgt[tmp_idx] = 1.0f;
617 : }
618 :
619 1746621 : for ( sample = 0; sample < num_samples; sample++ )
620 : {
621 : /* initialize trasnform matrix with zeros*/
622 1743840 : T[sample][0] = T[sample][1] = T[sample][2] = 0.0f;
623 : }
624 :
625 1310661 : for ( sample = 0; sample < pt_len - stride; sample++ )
626 : {
627 : /* fill internal CLDFB analysis time buffer with data*/
628 1307880 : float x = get_random_number( &seed );
629 :
630 1307880 : cldfbAnaDec0->cldfb_state[sample] = x;
631 : }
632 :
633 11124 : for ( slot = 0; slot < num_cf_slots; slot++ )
634 : {
635 444303 : for ( sample = 0; sample < stride; sample++ )
636 : {
637 435960 : float x = get_random_number( &seed );
638 435960 : ts_inout[sample] = x;
639 : }
640 :
641 8343 : cldfbAnalysis_ts( ts_inout, ts_re, ts_im, num_cldfb_bands, cldfbAnaDec0 );
642 8343 : cldfb_reset_memory( cldfbSynDec0 );
643 8343 : cldfbSynthesis( pp_ts_re, pp_ts_im, ts_inout, num_cldfb_bands, cldfbSynDec0 );
644 :
645 444303 : for ( sample = 0; sample < stride; sample++ )
646 : {
647 435960 : T[slot * stride + sample][slot] = ts_inout[sample];
648 : }
649 :
650 8343 : tmp_idx = pt_len - 1;
651 3931983 : for ( sample = stride; sample < pt_len; sample++ )
652 : {
653 3923640 : T[slot * stride + sample][slot] = cldfbSynDec0->cldfb_state[tmp_idx--];
654 : }
655 : }
656 :
657 : /* target is synthesis output times the cross-fade window*/
658 1746621 : for ( sample = 0; sample < num_samples; sample++ )
659 : {
660 1743840 : tgt[sample] *= ( T[sample][0] + T[sample][1] + T[sample][2] );
661 : }
662 :
663 : /* compute matrices */
664 11124 : for ( slot_row = 0; slot_row < num_cf_slots; slot_row++ )
665 : {
666 25029 : for ( slot_col = slot_row; slot_col < num_cf_slots; slot_col++ )
667 : {
668 16686 : Tt_T[slot_row][slot_col] = 0.0f;
669 10479726 : for ( sample = 0; sample < num_samples; sample++ )
670 : {
671 10463040 : Tt_T[slot_row][slot_col] += T[sample][slot_row] * T[sample][slot_col];
672 : }
673 : }
674 : }
675 :
676 2781 : Tt_T[1][0] = Tt_T[0][1];
677 2781 : Tt_T[2][0] = Tt_T[0][2];
678 2781 : Tt_T[2][1] = Tt_T[1][2];
679 :
680 11124 : for ( slot_row = 0; slot_row < num_cf_slots; slot_row++ )
681 : {
682 8343 : Tt_tgt[slot_row] = 0.0f;
683 5239863 : for ( sample = 0; sample < num_samples; sample++ )
684 : {
685 5231520 : Tt_tgt[slot_row] += T[sample][slot_row] * tgt[sample];
686 : }
687 : }
688 :
689 2781 : matrix_inverse( Tt_T, Tt_T_inv, num_cf_slots );
690 :
691 : /* compute the optimal coefficients */
692 11124 : for ( slot_row = 0; slot_row < num_cf_slots; slot_row++ )
693 : {
694 8343 : float tmp = 0.0f;
695 33372 : for ( slot_col = 0; slot_col < num_cf_slots; slot_col++ )
696 : {
697 25029 : tmp += Tt_T_inv[slot_row][slot_col] * Tt_tgt[slot_col];
698 : }
699 8343 : weights[cf_cldfb_start + slot_row] = max( min( tmp, 1.0f ), 0.0f );
700 : }
701 :
702 2781 : cldfb_reset_memory( cldfbSynDec0 );
703 2781 : cldfb_reset_memory( cldfbAnaDec0 );
704 :
705 2781 : return;
706 : }
707 :
708 :
709 : /*---------------------------------------------------------------------*
710 : * Function ivas_is_res_channel()
711 : *
712 : * determines if an FOA input channel is transmitted as residual channel.
713 : *---------------------------------------------------------------------*/
714 :
715 : /*! r: 1 if prediction residual channel */
716 402431040 : int16_t ivas_is_res_channel(
717 : const int16_t ch, /* i : ch index in WYZX ordering */
718 : const int16_t nchan_transport /* i : number of transport channels (1-4) */
719 : )
720 : {
721 402431040 : const int16_t rc_map[FOA_CHANNELS][FOA_CHANNELS] = {
722 : { 0, 0, 0, 0 },
723 : { 0, 1, 0, 0 },
724 : { 0, 1, 0, 1 },
725 : { 0, 1, 1, 1 }
726 : };
727 :
728 402431040 : if ( ch >= FOA_CHANNELS )
729 : {
730 : /* never transmitted */
731 54992448 : return 0;
732 : }
733 347438592 : assert( nchan_transport <= FOA_CHANNELS );
734 :
735 347438592 : return ( rc_map[nchan_transport - 1][ch] );
736 : }
737 :
738 :
739 : /*-------------------------------------------------------------------*
740 : * ivas_spar_dec_MD()
741 : *
742 : * IVAS SPAR MD decoder
743 : *-------------------------------------------------------------------*/
744 :
745 474039 : static ivas_error ivas_spar_dec_MD(
746 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
747 : Decoder_State *st0 /* i/o: decoder state structure - for bitstream handling*/
748 : )
749 : {
750 : int16_t num_channels, table_idx, num_bands_out, bfi, sba_order;
751 : int32_t ivas_total_brate;
752 : int16_t num_md_sub_frames;
753 : ivas_error error;
754 474039 : DECODER_CONFIG_HANDLE hDecoderConfig = st_ivas->hDecoderConfig;
755 474039 : SPAR_DEC_HANDLE hSpar = st_ivas->hSpar;
756 :
757 474039 : push_wmops( "ivas_spar_dec_MD" );
758 :
759 : /*---------------------------------------------------------------------*
760 : * Initialization
761 : *---------------------------------------------------------------------*/
762 :
763 474039 : sba_order = min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
764 474039 : bfi = st_ivas->bfi;
765 474039 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
766 474039 : num_channels = ivas_sba_get_nchan_metadata( sba_order, ivas_total_brate );
767 474039 : num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( sba_order, hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate );
768 :
769 474039 : num_bands_out = hSpar->hFbMixer->pFb->filterbank_num_bands;
770 :
771 474039 : if ( ivas_total_brate > FRAME_NO_DATA && !bfi )
772 : {
773 448536 : if ( ivas_total_brate > IVAS_SID_5k2 )
774 : {
775 447564 : ivas_parse_spar_header( hDecoderConfig->ivas_total_brate, sba_order, st0, &table_idx );
776 :
777 447564 : if ( hSpar->hMdDec->spar_hoa_md_flag )
778 : {
779 66039 : hSpar->hMdDec->spar_md.num_bands = IVAS_MAX_NUM_BANDS;
780 : }
781 : else
782 : {
783 381525 : hSpar->hMdDec->spar_md.num_bands = min( SPAR_DIRAC_SPLIT_START_BAND, IVAS_MAX_NUM_BANDS );
784 : }
785 :
786 447564 : if ( hSpar->hMdDec->table_idx != table_idx )
787 : {
788 954 : hSpar->hMdDec->table_idx = table_idx;
789 954 : if ( hSpar->hTdDecorr )
790 : {
791 864 : hSpar->hTdDecorr->ducking_flag = ivas_spar_br_table_consts[table_idx].td_ducking;
792 : }
793 :
794 954 : if ( ( error = ivas_spar_md_dec_init( hSpar->hMdDec, hDecoderConfig, num_channels, sba_order ) ) != IVAS_ERR_OK )
795 : {
796 0 : return error;
797 : }
798 : }
799 : }
800 :
801 : /*---------------------------------------------------------------------*
802 : * Decode MD
803 : *---------------------------------------------------------------------*/
804 :
805 448536 : ivas_spar_md_dec_process( st_ivas, st0, num_bands_out, sba_order );
806 :
807 : /*---------------------------------------------------------------------*
808 : * read PCA bits
809 : *---------------------------------------------------------------------*/
810 :
811 448536 : if ( hSpar->hPCA != NULL )
812 : {
813 32577 : ivas_pca_read_bits( st0, hSpar->hPCA );
814 : }
815 :
816 : /*---------------------------------------------------------------------*
817 : * Read AGC bits
818 : *---------------------------------------------------------------------*/
819 :
820 448536 : if ( ivas_total_brate > IVAS_SID_5k2 && !bfi && hSpar->hMdDec->dtx_vad )
821 : {
822 446862 : if ( hSpar->hMdDec->spar_md_cfg.nchan_transport == 1 )
823 : {
824 113793 : hSpar->AGC_flag = get_next_indice( st0, 1 );
825 : }
826 :
827 446862 : ivas_agc_read_bits( hSpar->hAgcDec, st0, hSpar->hMdDec->spar_md_cfg.nchan_transport, hSpar->AGC_flag );
828 : }
829 :
830 : /*---------------------------------------------------------------------*
831 : * MD smoothing
832 : *---------------------------------------------------------------------*/
833 :
834 448536 : if ( st0->m_old_frame_type == ZERO_FRAME && ivas_total_brate == IVAS_SID_5k2 && st0->prev_bfi == 0 && hSpar->hMdDec->spar_md_cfg.nchan_transport == 1 )
835 : {
836 375 : ivas_spar_setup_md_smoothing( hSpar->hMdDec, num_bands_out, num_md_sub_frames );
837 : }
838 : else
839 : {
840 448161 : ivas_spar_update_md_hist( hSpar->hMdDec );
841 : }
842 : }
843 : else
844 : {
845 25503 : if ( !bfi )
846 : {
847 6195 : ivas_spar_smooth_md_dtx( hSpar->hMdDec, num_bands_out, num_md_sub_frames );
848 : }
849 :
850 25503 : set_s( hSpar->hMdDec->valid_bands, 0, IVAS_MAX_NUM_BANDS );
851 : }
852 :
853 474039 : pop_wmops();
854 474039 : return IVAS_ERR_OK;
855 : }
856 :
857 :
858 : /*-------------------------------------------------------------------*
859 : * ivas_spar_get_cldfb_slot_gain()
860 : *
861 : *
862 : *-------------------------------------------------------------------*/
863 :
864 6949107 : static float ivas_spar_get_cldfb_slot_gain(
865 : SPAR_DEC_HANDLE hSpar, /* i/o: SPAR decoder handle */
866 : const DECODER_CONFIG_HANDLE hDecoderConfig, /* i : configuration structure */
867 : const int16_t time_slot_idx,
868 : int16_t *time_slot_idx0,
869 : int16_t *time_slot_idx1,
870 : float *weight_lowfreq )
871 : {
872 : float weight;
873 : float output_Fs, encfb_delay, decfb_delay;
874 : float xfade_start_ns;
875 : int16_t xfade_delay_subframes;
876 : int16_t i_hist;
877 : int16_t split_band;
878 :
879 6949107 : *weight_lowfreq = hSpar->hFbMixer->cldfb_cross_fade[time_slot_idx];
880 :
881 6949107 : output_Fs = (float) hDecoderConfig->output_Fs;
882 6949107 : encfb_delay = IVAS_FB_ENC_DELAY_NS;
883 6949107 : decfb_delay = IVAS_FB_DEC_DELAY_NS;
884 6949107 : xfade_start_ns = hSpar->hFbMixer->cross_fade_start_offset / output_Fs * 1000000000.f - encfb_delay + decfb_delay * 0.5f;
885 6949107 : xfade_delay_subframes = (int16_t) ( xfade_start_ns / ( FRAME_SIZE_NS / MAX_PARAM_SPATIAL_SUBFRAMES ) );
886 :
887 6949107 : i_hist = 4 - xfade_delay_subframes;
888 6949107 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
889 :
890 6949107 : if ( split_band < IVAS_MAX_NUM_BANDS )
891 : {
892 6949107 : if ( hSpar->i_subframe > 3 )
893 : {
894 6882147 : weight = (float) ( time_slot_idx % MAX_PARAM_SPATIAL_SUBFRAMES ) / (float) MAX_PARAM_SPATIAL_SUBFRAMES;
895 : }
896 : else
897 : {
898 66960 : weight = 0.0f;
899 : }
900 6949107 : *time_slot_idx0 = i_hist;
901 6949107 : *time_slot_idx1 = i_hist + 1;
902 : }
903 : else
904 : {
905 : /* determine cross-fade gain for current frame Parameters*/
906 0 : *time_slot_idx0 = hSpar->hFbMixer->cldfb_cross_fade_start;
907 0 : *time_slot_idx1 = hSpar->hFbMixer->cldfb_cross_fade_end;
908 0 : weight = *weight_lowfreq;
909 : }
910 :
911 6949107 : return weight;
912 : }
913 :
914 :
915 : /*-------------------------------------------------------------------*
916 : * ivas_spar_get_parameters()
917 : *
918 : *
919 : *-------------------------------------------------------------------*/
920 :
921 6949107 : void ivas_spar_get_parameters(
922 : SPAR_DEC_HANDLE hSpar, /* i/o: SPAR decoder handle */
923 : const DECODER_CONFIG_HANDLE hDecoderConfig, /* i : configuration structure */
924 : const int16_t ts,
925 : const int16_t num_ch_out,
926 : const int16_t num_ch_in,
927 : const int16_t num_spar_bands,
928 : float par_mat[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS] )
929 : {
930 : int16_t spar_band, out_ch, in_ch;
931 : float weight, weight_20ms;
932 : int16_t ts0, ts1, split_band;
933 :
934 6949107 : weight = ivas_spar_get_cldfb_slot_gain( hSpar, hDecoderConfig, ts, &ts0, &ts1, &weight_20ms );
935 :
936 6949107 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
937 89090487 : for ( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
938 : {
939 465699348 : for ( out_ch = 0; out_ch < num_ch_out; out_ch++ )
940 : {
941 383557968 : if ( split_band < IVAS_MAX_NUM_BANDS
942 : /* 20ms cross-fade for Transport channels in all frequency bands */
943 383557968 : && ( 0 == ivas_is_res_channel( out_ch, hSpar->hMdDec->spar_md_cfg.nchan_transport ) ) /* sub-frame processing for missing channels in all frequency bands*/
944 : )
945 : {
946 1678752744 : for ( in_ch = 0; in_ch < num_ch_in; in_ch++ )
947 : {
948 1415887968 : if ( hSpar->i_subframe > 3 )
949 : {
950 1401903840 : par_mat[out_ch][in_ch][spar_band] = ( 1.0f - weight ) * hSpar->hMdDec->mixer_mat_prev[ts0][out_ch][in_ch][spar_band] +
951 1401903840 : weight * hSpar->hMdDec->mixer_mat_prev[ts1][out_ch][in_ch][spar_band];
952 : }
953 : else
954 : {
955 13984128 : par_mat[out_ch][in_ch][spar_band] = hSpar->hMdDec->mixer_mat[out_ch][in_ch][spar_band];
956 : }
957 : }
958 : }
959 : else
960 : {
961 768443304 : for ( in_ch = 0; in_ch < num_ch_in; in_ch++ )
962 : {
963 : /* 20ms Transport channel reconstruction with matching encoder/decoder processing */
964 647750112 : int16_t prev_idx = SPAR_DIRAC_SPLIT_START_BAND < IVAS_MAX_NUM_BANDS ? 1 : 0; /* if SPAR_DIRAC_SPLIT_START_BAND == IVAS_MAX_NUM_BANDS, then the sub-frame mixer_mat delay line is not active */
965 647750112 : par_mat[out_ch][in_ch][spar_band] = ( 1.0f - weight_20ms ) * hSpar->hMdDec->mixer_mat_prev[prev_idx][out_ch][in_ch][spar_band] + weight_20ms * hSpar->hMdDec->mixer_mat[out_ch][in_ch][spar_band];
966 : }
967 : }
968 : }
969 : }
970 :
971 6949107 : return;
972 : }
973 :
974 :
975 : /*-------------------------------------------------------------------*
976 : * ivas_spar_get_skip_mat()
977 : *
978 : *
979 : *-------------------------------------------------------------------*/
980 :
981 1285977 : static void ivas_spar_get_skip_mat(
982 : SPAR_DEC_HANDLE hSpar, /* i/o: SPAR decoder handle */
983 : const int16_t num_ch_out,
984 : const int16_t num_ch_in,
985 : const int16_t num_spar_bands,
986 : int16_t skip_mat[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
987 : const int16_t num_md_sub_frames )
988 : {
989 : int16_t spar_band, out_ch, in_ch;
990 : int16_t i_ts, skip_flag;
991 :
992 7575561 : for ( out_ch = 0; out_ch < num_ch_out; out_ch++ )
993 : {
994 42477216 : for ( in_ch = 0; in_ch < num_ch_in; in_ch++ )
995 : {
996 36187632 : skip_mat[out_ch][in_ch] = 1;
997 36187632 : skip_flag = 1;
998 122448798 : for ( i_ts = 0; i_ts < MAX_PARAM_SPATIAL_SUBFRAMES; i_ts++ )
999 : {
1000 1148206593 : for ( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
1001 : {
1002 1061945427 : if ( hSpar->hMdDec->mixer_mat_prev[1 + i_ts][out_ch][in_ch][spar_band] != 0.0f )
1003 : {
1004 14765988 : skip_flag = 0;
1005 14765988 : break;
1006 : }
1007 : }
1008 :
1009 101027154 : if ( skip_flag == 0 )
1010 : {
1011 14765988 : skip_mat[out_ch][in_ch] = 0;
1012 14765988 : break;
1013 : }
1014 : }
1015 :
1016 36187632 : if ( skip_mat[out_ch][in_ch] == 1 )
1017 : {
1018 77514144 : for ( i_ts = 0; i_ts < num_md_sub_frames; i_ts++ )
1019 : {
1020 721138416 : for ( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
1021 : {
1022 665045916 : if ( hSpar->hMdDec->mixer_mat[out_ch][in_ch][spar_band + i_ts * IVAS_MAX_NUM_BANDS] != 0.0f )
1023 : {
1024 94938 : skip_flag = 0;
1025 94938 : break;
1026 : }
1027 : }
1028 :
1029 56187438 : if ( skip_flag == 0 )
1030 : {
1031 94938 : skip_mat[out_ch][in_ch] = 0;
1032 94938 : break;
1033 : }
1034 : }
1035 : }
1036 : }
1037 : }
1038 :
1039 1285977 : return;
1040 : }
1041 :
1042 :
1043 24912 : static void ivas_spar_calc_smooth_facs(
1044 : float *cldfb_in_ts_re[CLDFB_NO_COL_MAX],
1045 : float *cldfb_in_ts_im[CLDFB_NO_COL_MAX],
1046 : int16_t nbands_spar,
1047 : const int16_t nSlots,
1048 : const int16_t isFirstSubframe,
1049 : ivas_fb_bin_to_band_data_t *bin2band,
1050 : float *smooth_fac,
1051 : float smooth_buf[IVAS_MAX_NUM_BANDS][2 * SBA_DIRAC_NRG_SMOOTH_LONG + 1] )
1052 : {
1053 : int16_t b, bin, i, ts;
1054 : float subframe_band_nrg[IVAS_MAX_NUM_BANDS];
1055 : float smooth_long_avg[IVAS_MAX_NUM_BANDS];
1056 : float smooth_short_avg[IVAS_MAX_NUM_BANDS];
1057 :
1058 24912 : bin = 0;
1059 307824 : for ( b = 0; b < nbands_spar; b++ )
1060 : {
1061 298944 : if ( bin >= CLDFB_NO_CHANNELS_MAX || ( b > 0 && bin2band->p_cldfb_map_to_spar_band[bin] < bin2band->p_cldfb_map_to_spar_band[bin - 1] ) )
1062 : {
1063 : break;
1064 : }
1065 :
1066 : /* calculate band-wise subframe energies */
1067 282912 : subframe_band_nrg[b] = 0.f;
1068 1456992 : while ( bin < CLDFB_NO_CHANNELS_MAX && b == bin2band->p_cldfb_map_to_spar_band[bin] )
1069 : {
1070 5870400 : for ( ts = 0; ts < nSlots; ts++ )
1071 : {
1072 4696320 : subframe_band_nrg[b] += cldfb_in_ts_re[ts][bin] * cldfb_in_ts_re[ts][bin] + cldfb_in_ts_im[ts][bin] * cldfb_in_ts_im[ts][bin];
1073 : }
1074 1174080 : bin++;
1075 : }
1076 282912 : subframe_band_nrg[b] = sqrtf( subframe_band_nrg[b] );
1077 282912 : if ( isFirstSubframe && nSlots < MAX_PARAM_SPATIAL_SUBFRAMES )
1078 : {
1079 : /* fill up to full 5ms subframe */
1080 0 : smooth_buf[b][0] += subframe_band_nrg[b];
1081 : }
1082 : else
1083 : {
1084 282912 : smooth_buf[b][0] = subframe_band_nrg[b];
1085 : }
1086 : /* calculate short and long energy averages */
1087 282912 : smooth_short_avg[b] = EPSILON;
1088 1980384 : for ( i = 0; i < 2 * SBA_DIRAC_NRG_SMOOTH_SHORT; i++ )
1089 : {
1090 1697472 : smooth_short_avg[b] += smooth_buf[b][i];
1091 : }
1092 :
1093 282912 : smooth_long_avg[b] = smooth_short_avg[b];
1094 4243680 : for ( i = 2 * SBA_DIRAC_NRG_SMOOTH_SHORT; i < 2 * SBA_DIRAC_NRG_SMOOTH_LONG; i++ )
1095 : {
1096 3960768 : smooth_long_avg[b] += smooth_buf[b][i];
1097 : }
1098 282912 : smooth_short_avg[b] /= ( 2 * SBA_DIRAC_NRG_SMOOTH_SHORT );
1099 282912 : smooth_long_avg[b] /= ( 2 * SBA_DIRAC_NRG_SMOOTH_LONG );
1100 :
1101 : /* calculate smoothing factor based on energy averages */
1102 : /* reduce factor for higher short-term energy */
1103 282912 : smooth_fac[b] = min( 1.f, smooth_long_avg[b] / smooth_short_avg[b] );
1104 :
1105 : /* map factor to range [0;1] */
1106 282912 : smooth_fac[b] = max( 0.f, smooth_fac[b] - (float) SBA_DIRAC_NRG_SMOOTH_SHORT / SBA_DIRAC_NRG_SMOOTH_LONG ) * ( (float) SBA_DIRAC_NRG_SMOOTH_LONG / ( SBA_DIRAC_NRG_SMOOTH_LONG - SBA_DIRAC_NRG_SMOOTH_SHORT ) );
1107 :
1108 : /* compress factor (higher compression in lowest bands) */
1109 282912 : if ( b < 2 )
1110 : {
1111 49824 : smooth_fac[b] = powf( smooth_fac[b], 0.25f );
1112 : }
1113 : else
1114 : {
1115 233088 : smooth_fac[b] = powf( smooth_fac[b], 0.5f );
1116 : }
1117 :
1118 : /* apply upper bounds depending on band */
1119 282912 : smooth_fac[b] = max( min_smooth_gains1[b], min( max_smooth_gains2[b], smooth_fac[b] ) );
1120 : }
1121 :
1122 : /* only update if we collected a full 5ms worth of energies for the buffer */
1123 24912 : if ( isFirstSubframe || nSlots == MAX_PARAM_SPATIAL_SUBFRAMES )
1124 : {
1125 323856 : for ( b = 0; b < nbands_spar; b++ )
1126 : {
1127 6277824 : for ( i = 2 * SBA_DIRAC_NRG_SMOOTH_LONG; i > 0; i-- )
1128 : {
1129 5978880 : smooth_buf[b][i] = smooth_buf[b][i - 1];
1130 : }
1131 : }
1132 : }
1133 24912 : return;
1134 : }
1135 :
1136 :
1137 : /*-------------------------------------------------------------------*
1138 : * ivas_spar_dec_agc_pca()
1139 : *
1140 : *
1141 : *-------------------------------------------------------------------*/
1142 :
1143 322977 : void ivas_spar_dec_agc_pca(
1144 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1145 : float *output[], /* i/o: input/output audio channels */
1146 : const int16_t output_frame /* i : output frame length */
1147 : )
1148 : {
1149 : int16_t nchan_transport;
1150 : int16_t num_in_ingest;
1151 : DECODER_CONFIG_HANDLE hDecoderConfig;
1152 : SPAR_DEC_HANDLE hSpar;
1153 :
1154 322977 : push_wmops( "ivas_spar_dec_agc_pca" );
1155 :
1156 322977 : hSpar = st_ivas->hSpar;
1157 322977 : hDecoderConfig = st_ivas->hDecoderConfig;
1158 322977 : nchan_transport = hSpar->hMdDec->spar_md_cfg.nchan_transport;
1159 :
1160 322977 : if ( st_ivas->nchan_transport >= 3 )
1161 : {
1162 : float temp;
1163 : int16_t i;
1164 : /*convert WYZX downmix to WYXZ*/
1165 171810408 : for ( i = 0; i < output_frame; i++ )
1166 : {
1167 171601920 : temp = output[2][i];
1168 171601920 : output[2][i] = output[3][i];
1169 171601920 : output[3][i] = temp;
1170 : }
1171 : }
1172 :
1173 322977 : if ( hSpar->hMdDec->td_decorr_flag )
1174 : {
1175 322977 : num_in_ingest = ivas_sba_get_nchan_metadata( st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate );
1176 : }
1177 : else
1178 : {
1179 0 : num_in_ingest = nchan_transport;
1180 : }
1181 :
1182 : /*---------------------------------------------------------------------*
1183 : * AGC
1184 : *---------------------------------------------------------------------*/
1185 :
1186 322977 : ivas_agc_dec_process( hSpar->hAgcDec, output, output, nchan_transport, output_frame );
1187 : #ifdef DEBUG_SBA_AUDIO_DUMP
1188 : /* Dump audio signal after ivas_agc_dec_process */
1189 : ivas_spar_dump_signal_wav( output_frame, NULL, output, st_ivas->nchan_transport, spar_foa_dec_wav[1], "ivas_agc_dec_process()" );
1190 : #endif
1191 :
1192 322977 : if ( hSpar->hPCA != NULL )
1193 : {
1194 34857 : ivas_pca_dec( hSpar->hPCA, output_frame, num_in_ingest, hDecoderConfig->ivas_total_brate, hDecoderConfig->last_ivas_total_brate, st_ivas->bfi, output );
1195 : #ifdef DEBUG_SBA_AUDIO_DUMP
1196 : /* Dump audio signal after ivas_pca_dec */
1197 : ivas_spar_dump_signal_wav( output_frame, NULL, output, num_in_ingest, spar_foa_dec_wav[2], "ivas_pca_dec()" );
1198 : #endif
1199 : }
1200 322977 : pop_wmops();
1201 :
1202 322977 : return;
1203 : }
1204 :
1205 :
1206 : /*-------------------------------------------------------------------*
1207 : * ivas_spar_dec_set_render_map()
1208 : *
1209 : *
1210 : *-------------------------------------------------------------------*/
1211 :
1212 434292 : void ivas_spar_dec_set_render_map(
1213 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
1214 : const int16_t nCldfbTs /* i : number of CLDFB time slots */
1215 : )
1216 : {
1217 : SPAR_DEC_HANDLE hSpar;
1218 :
1219 434292 : hSpar = st_ivas->hSpar;
1220 : #ifdef DEBUGGING
1221 : assert( hSpar );
1222 : #endif
1223 :
1224 : /* adapt subframes */
1225 434292 : hSpar->num_slots = nCldfbTs;
1226 434292 : hSpar->slots_rendered = 0;
1227 434292 : hSpar->subframes_rendered = 0;
1228 :
1229 434292 : set_s( hSpar->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
1230 434292 : ivas_jbm_dec_get_adapted_subframes( nCldfbTs, hSpar->subframe_nbslots, &hSpar->nb_subframes );
1231 :
1232 : /* copy also to tc buffer */
1233 : /* only for non-combined formats and combinded formats w/o discrete objects */
1234 434292 : if ( !( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC ) )
1235 : {
1236 375246 : st_ivas->hTcBuffer->nb_subframes = hSpar->nb_subframes;
1237 375246 : mvs2s( hSpar->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, hSpar->nb_subframes );
1238 : }
1239 :
1240 434292 : ivas_jbm_dec_get_md_map( DEFAULT_JBM_CLDFB_TIMESLOTS, nCldfbTs, 1, 0, DEFAULT_JBM_CLDFB_TIMESLOTS, hSpar->render_to_md_map );
1241 :
1242 434292 : return;
1243 : }
1244 :
1245 : /*-------------------------------------------------------------------*
1246 : * ivas_spar_dec_set_render_params()
1247 : *
1248 : * IVAS SPAR set rendering parameters
1249 : *-------------------------------------------------------------------*/
1250 :
1251 434292 : void ivas_spar_dec_set_render_params(
1252 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1253 : const int16_t n_cldfb_slots /* i : number of cldfb slots in this frame */
1254 : )
1255 : {
1256 : SPAR_DEC_HANDLE hSpar;
1257 : int16_t nchan_transport;
1258 : int16_t num_bands_out;
1259 :
1260 434292 : hSpar = st_ivas->hSpar;
1261 434292 : nchan_transport = hSpar->hMdDec->spar_md_cfg.nchan_transport;
1262 434292 : num_bands_out = hSpar->hFbMixer->pFb->filterbank_num_bands;
1263 434292 : ivas_spar_dec_gen_umx_mat( hSpar->hMdDec, nchan_transport, num_bands_out, st_ivas->bfi, ivas_get_spar_dec_md_num_subframes( st_ivas->sba_order, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate ) );
1264 :
1265 434292 : ivas_spar_dec_set_render_map( st_ivas, n_cldfb_slots );
1266 :
1267 434292 : return;
1268 : }
1269 :
1270 :
1271 : /*-------------------------------------------------------------------*
1272 : * ivas_spar_dec_digest_tc()
1273 : *
1274 : *
1275 : *-------------------------------------------------------------------*/
1276 :
1277 434292 : void ivas_spar_dec_digest_tc(
1278 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1279 : const int16_t nchan_transport, /* i : number of transport channels */
1280 : const int16_t nCldfbSlots, /* i : number of CLDFB slots */
1281 : const int16_t nSamplesForRendering /* i : number of samples provided */
1282 : )
1283 : {
1284 : SPAR_DEC_HANDLE hSpar;
1285 :
1286 434292 : hSpar = st_ivas->hSpar;
1287 434292 : if ( hSpar->hMdDec->td_decorr_flag && !( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) )
1288 : {
1289 : float Pcm_tmp[MAX_SPAR_INTERNAL_CHANNELS][L_FRAME48k];
1290 : float *pPcm_tmp[MAX_SPAR_INTERNAL_CHANNELS];
1291 : float *p_tc[MAX_SPAR_INTERNAL_CHANNELS];
1292 : int16_t nchan_internal, ch;
1293 : int16_t ch_sba_idx;
1294 : int16_t nSamplesLeftForTD, default_frame;
1295 :
1296 319977 : ch_sba_idx = 0;
1297 319977 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
1298 : {
1299 71196 : if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
1300 : {
1301 59046 : ch_sba_idx = st_ivas->nchan_ism;
1302 : }
1303 : }
1304 :
1305 : /* TD decorrelator */
1306 319977 : default_frame = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / FRAMES_PER_SEC );
1307 319977 : nSamplesLeftForTD = nSamplesForRendering;
1308 319977 : nchan_internal = ivas_sba_get_nchan_metadata( st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate );
1309 :
1310 1886295 : for ( ch = 0; ch < nchan_internal; ch++ )
1311 : {
1312 1566318 : pPcm_tmp[ch] = Pcm_tmp[ch];
1313 1566318 : p_tc[ch] = st_ivas->hTcBuffer->tc[ch + ch_sba_idx];
1314 : }
1315 :
1316 639966 : while ( nSamplesLeftForTD )
1317 : {
1318 319989 : int16_t nSamplesToDecorr = min( nSamplesLeftForTD, default_frame );
1319 :
1320 319989 : if ( hSpar->hTdDecorr )
1321 : {
1322 292779 : ivas_td_decorr_process( hSpar->hTdDecorr, p_tc, pPcm_tmp, nSamplesToDecorr );
1323 292779 : if ( hSpar->hTdDecorr->num_apd_outputs >= ( nchan_internal - nchan_transport ) )
1324 : {
1325 784527 : for ( ch = 0; ch < nchan_internal - nchan_transport; ch++ )
1326 : {
1327 514158 : mvr2r( pPcm_tmp[hSpar->hTdDecorr->num_apd_outputs - 1 - ch], p_tc[nchan_internal - 1 - ch], nSamplesToDecorr );
1328 : }
1329 : }
1330 : else
1331 : {
1332 179280 : for ( ch = 0; ch < nchan_internal - nchan_transport; ch++ )
1333 : {
1334 156870 : set_zero( p_tc[nchan_internal - 1 - ch], nSamplesToDecorr );
1335 : }
1336 67230 : for ( ch = 0; ch < hSpar->hTdDecorr->num_apd_outputs; ch++ )
1337 : {
1338 44820 : mvr2r( pPcm_tmp[hSpar->hTdDecorr->num_apd_outputs - 1 - ch], p_tc[nchan_internal - 1 - ch], nSamplesToDecorr );
1339 : }
1340 : }
1341 : }
1342 1886355 : for ( ch = 0; ch < nchan_internal; ch++ )
1343 : {
1344 1566366 : p_tc[ch] += nSamplesToDecorr;
1345 : }
1346 :
1347 319989 : nSamplesLeftForTD -= nSamplesToDecorr;
1348 : }
1349 : }
1350 :
1351 434292 : ivas_spar_dec_set_render_params( st_ivas, nCldfbSlots );
1352 :
1353 434292 : return;
1354 : }
1355 :
1356 :
1357 : /*-------------------------------------------------------------------*
1358 : * ivas_spar_dec_upmixer_sf()
1359 : *
1360 : * IVAS SPAR upmixer
1361 : *-------------------------------------------------------------------*/
1362 :
1363 1285977 : void ivas_spar_dec_upmixer_sf(
1364 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1365 : float *output[], /* o : output audio channels */
1366 : const int16_t nchan_internal /* i : number of internal channels */
1367 : )
1368 : {
1369 : int16_t cldfb_band, num_cldfb_bands, numch_in, numch_out;
1370 : float *cldfb_in_ts_re[HOA3_CHANNELS][CLDFB_NO_COL_MAX];
1371 : float *cldfb_in_ts_im[HOA3_CHANNELS][CLDFB_NO_COL_MAX];
1372 : int16_t i, b, ts, out_ch, in_ch;
1373 : int16_t num_spar_bands, spar_band, nchan_transport;
1374 : int16_t num_in_ingest, split_band;
1375 : int16_t slot_size, slot_idx_start;
1376 : float *p_tc[HOA3_CHANNELS];
1377 : int16_t md_idx;
1378 : float Pcm_tmp[HOA3_CHANNELS][2 /* Re, Im*/ * L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES];
1379 : int16_t numch_out_dirac;
1380 : float mixer_mat[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
1381 : int16_t b_skip_mat[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
1382 : DECODER_CONFIG_HANDLE hDecoderConfig;
1383 : SPAR_DEC_HANDLE hSpar;
1384 : int16_t num_md_sub_frames;
1385 :
1386 1285977 : push_wmops( "ivas_spar_dec_upmixer_sf" );
1387 1285977 : hSpar = st_ivas->hSpar;
1388 1285977 : hDecoderConfig = st_ivas->hDecoderConfig;
1389 1285977 : nchan_transport = hSpar->hMdDec->spar_md_cfg.nchan_transport;
1390 :
1391 1285977 : num_cldfb_bands = hSpar->hFbMixer->pFb->fb_bin_to_band.num_cldfb_bands;
1392 1285977 : numch_in = hSpar->hFbMixer->fb_cfg->num_in_chans;
1393 1285977 : numch_out = hSpar->hFbMixer->fb_cfg->num_out_chans;
1394 1285977 : num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( st_ivas->sba_order, hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate );
1395 1285977 : slot_size = NS2SA( st_ivas->hDecoderConfig->output_Fs, CLDFB_SLOT_NS );
1396 1285977 : slot_idx_start = hSpar->slots_rendered;
1397 :
1398 1285977 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC )
1399 239088 : {
1400 : int16_t nchan_ism;
1401 :
1402 239088 : nchan_ism = st_ivas->nchan_ism;
1403 :
1404 1732476 : for ( i = 0; i < nchan_internal; i++ )
1405 : {
1406 1493388 : p_tc[i] = st_ivas->hTcBuffer->tc[i + nchan_ism] + slot_idx_start * slot_size;
1407 : }
1408 :
1409 : #ifdef FIX_1372_OSBA_OBJECT_EDITING
1410 239088 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1411 : #else
1412 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1413 : #endif
1414 : {
1415 228000 : for ( i = 0; i < nchan_ism; i++ )
1416 : {
1417 180000 : p_tc[i + nchan_internal] = st_ivas->hTcBuffer->tc[i] + slot_idx_start * slot_size;
1418 : }
1419 : }
1420 : }
1421 : else
1422 : {
1423 5843085 : for ( i = 0; i < nchan_internal; i++ )
1424 : {
1425 4796196 : p_tc[i] = st_ivas->hTcBuffer->tc[i] + slot_idx_start * slot_size;
1426 : }
1427 : }
1428 :
1429 : #ifdef DEBUG_SPAR_BYPASS_EVS_CODEC
1430 : /* by-pass core-coder */
1431 : /*write the core coder output to a file for debugging*/
1432 : {
1433 : float tmp;
1434 : int16_t pcm, j;
1435 : for ( j = 0; j < output_frame; j++ )
1436 : {
1437 : for ( i = 0; i < nchan_transport; i++ )
1438 : {
1439 : tmp = roundf( output[i][j] * PCM16_TO_FLT_FAC );
1440 : pcm = ( tmp > MAX16B_FLT ) ? MAX16B : ( tmp < MIN16B_FLT ) ? MIN16B
1441 : : (short) tmp;
1442 : dbgwrite( &pcm, sizeof( int16_t ), 1, 1, "dmx_dec.raw" );
1443 : }
1444 : }
1445 : }
1446 :
1447 : /*overwrite the core coder output with core input for debugging*/
1448 : {
1449 : static FILE *fid_enc = 0;
1450 : int16_t smp;
1451 :
1452 : if ( !fid_enc )
1453 : {
1454 : fid_enc = fopen( "evs_input_float.raw", "rb" );
1455 : }
1456 : for ( smp = 0; smp < L_FRAME48k; smp++ )
1457 : {
1458 : for ( in_ch = 0; in_ch < nchan_transport; in_ch++ )
1459 : {
1460 : fread( &output[in_ch][smp], sizeof( float ), 1, fid_enc );
1461 : }
1462 : }
1463 : }
1464 : #endif
1465 :
1466 : /*---------------------------------------------------------------------*
1467 : * TD Decorr and pcm ingest
1468 : *---------------------------------------------------------------------*/
1469 :
1470 1285977 : if ( hSpar->hMdDec->td_decorr_flag )
1471 : {
1472 1285977 : num_in_ingest = nchan_internal;
1473 : }
1474 : else
1475 : {
1476 0 : num_in_ingest = nchan_transport;
1477 : }
1478 :
1479 : /*---------------------------------------------------------------------*
1480 : * PCA decoder
1481 : *---------------------------------------------------------------------*/
1482 : #ifdef DEBUG_SBA_AUDIO_DUMP
1483 : hSpar->pca_ingest_channels = num_in_ingest;
1484 : #endif
1485 :
1486 1285977 : hSpar->hFbMixer->fb_cfg->num_in_chans = num_in_ingest;
1487 :
1488 :
1489 : /*---------------------------------------------------------------------*
1490 : * Prepare CLDFB buffers
1491 : *---------------------------------------------------------------------*/
1492 :
1493 : /* set-up pointers */
1494 1285977 : if ( hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_FOA )
1495 : {
1496 : /* at this point, output channels are used as intermediate procesing buffers */
1497 13038762 : for ( in_ch = 0; in_ch < HOA3_CHANNELS; in_ch++ )
1498 : {
1499 61358880 : for ( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
1500 : {
1501 49087104 : cldfb_in_ts_re[in_ch][ts] = &Pcm_tmp[in_ch][ts * num_cldfb_bands];
1502 49087104 : cldfb_in_ts_im[in_ch][ts] = &Pcm_tmp[in_ch][ts * num_cldfb_bands + 4 * num_cldfb_bands];
1503 : }
1504 : }
1505 : }
1506 : else
1507 : {
1508 2612955 : for ( in_ch = 0; in_ch < numch_in; in_ch++ )
1509 : {
1510 10469820 : for ( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
1511 : {
1512 8375856 : cldfb_in_ts_re[in_ch][ts] = &Pcm_tmp[in_ch][ts * num_cldfb_bands];
1513 8375856 : cldfb_in_ts_im[in_ch][ts] = &Pcm_tmp[in_ch][ts * num_cldfb_bands + 4 * num_cldfb_bands];
1514 : }
1515 : }
1516 : }
1517 :
1518 : /*---------------------------------------------------------------------*
1519 : * CLDFB Processing and Synthesis
1520 : *---------------------------------------------------------------------*/
1521 :
1522 1285977 : num_spar_bands = hSpar->hFbMixer->pFb->filterbank_num_bands;
1523 :
1524 : /* apply parameters */
1525 : /* determine if we can skip certain data */
1526 1285977 : ivas_spar_get_skip_mat( hSpar, numch_out, numch_in, num_spar_bands, b_skip_mat, num_md_sub_frames ); /* this can be precomputed based on bitrate and format*/
1527 :
1528 1285977 : numch_out_dirac = hDecoderConfig->nchan_out;
1529 :
1530 : #ifdef DEBUG_SBA_AUDIO_DUMP
1531 : /* Dump audio signal after ivas_agc_dec_process */
1532 : ivas_spar_dump_signal_wav( output_frame, p_tc, NULL, numch_in, spar_foa_dec_wav[4], "ivas_spar_upmixer()" );
1533 : #endif
1534 :
1535 : /* CLDFB analysis of incoming frame */
1536 7575561 : for ( in_ch = 0; in_ch < numch_in; in_ch++ )
1537 : {
1538 31352280 : for ( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
1539 : {
1540 25062696 : cldfbAnalysis_ts( &p_tc[in_ch][ts * num_cldfb_bands], cldfb_in_ts_re[in_ch][ts], cldfb_in_ts_im[in_ch][ts], num_cldfb_bands, st_ivas->cldfbAnaDec[in_ch] );
1541 : }
1542 : }
1543 :
1544 1285977 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1545 : {
1546 228000 : for ( ; in_ch < st_ivas->nchan_ism + numch_in; in_ch++ )
1547 : {
1548 900000 : for ( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
1549 : {
1550 720000 : cldfbAnalysis_ts( &p_tc[in_ch][ts * num_cldfb_bands], cldfb_in_ts_re[in_ch][ts], cldfb_in_ts_im[in_ch][ts], num_cldfb_bands, st_ivas->cldfbAnaDec[in_ch] );
1551 : }
1552 : }
1553 : }
1554 :
1555 1285977 : if ( hDecoderConfig->ivas_total_brate < IVAS_24k4 && ( st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA2 || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA3 ) )
1556 : {
1557 24912 : ivas_spar_calc_smooth_facs( cldfb_in_ts_re[0], cldfb_in_ts_im[0], num_spar_bands, hSpar->subframe_nbslots[hSpar->subframes_rendered], hSpar->subframes_rendered == 0, &hSpar->hFbMixer->pFb->fb_bin_to_band, hSpar->hMdDec->smooth_fac, hSpar->hMdDec->smooth_buf );
1558 : }
1559 :
1560 6405975 : for ( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
1561 : {
1562 5119998 : md_idx = hSpar->render_to_md_map[ts + slot_idx_start];
1563 5119998 : ivas_spar_get_parameters( hSpar, hDecoderConfig, md_idx, numch_out, numch_in, num_spar_bands, mixer_mat );
1564 :
1565 5119998 : if ( hDecoderConfig->ivas_total_brate < IVAS_24k4 && ( st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA2 || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA3 ) )
1566 : {
1567 1295424 : for ( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
1568 : {
1569 5978880 : for ( out_ch = 0; out_ch < numch_out; out_ch++ )
1570 : {
1571 23915520 : for ( in_ch = 0; in_ch < numch_in; in_ch++ )
1572 : {
1573 19132416 : mixer_mat[out_ch][in_ch][spar_band] = ( 1 - hSpar->hMdDec->smooth_fac[spar_band] ) * mixer_mat[out_ch][in_ch][spar_band] + hSpar->hMdDec->smooth_fac[spar_band] * hSpar->hMdDec->mixer_mat_prev2[out_ch][in_ch][spar_band];
1574 19132416 : hSpar->hMdDec->mixer_mat_prev2[out_ch][in_ch][spar_band] = mixer_mat[out_ch][in_ch][spar_band];
1575 : }
1576 : }
1577 : }
1578 : }
1579 :
1580 260801538 : for ( cldfb_band = 0; cldfb_band < num_cldfb_bands; cldfb_band++ )
1581 : {
1582 : float out_re[IVAS_SPAR_MAX_CH];
1583 : float out_im[IVAS_SPAR_MAX_CH];
1584 : float cldfb_par;
1585 255681540 : ivas_fb_bin_to_band_data_t *bin2band = &hSpar->hFbMixer->pFb->fb_bin_to_band;
1586 :
1587 1547917140 : for ( out_ch = 0; out_ch < numch_out; out_ch++ )
1588 : {
1589 1292235600 : out_re[out_ch] = 0.0f;
1590 1292235600 : out_im[out_ch] = 0.0f;
1591 :
1592 9063377040 : for ( in_ch = 0; in_ch < numch_in; in_ch++ )
1593 : {
1594 7771141440 : if ( b_skip_mat[out_ch][in_ch] == 0 )
1595 : {
1596 3131769780 : if ( cldfb_band < CLDFB_PAR_WEIGHT_START_BAND ) /* tuning parameter, depends on how much SPAR Filters overlap for the CLDFB bands */
1597 : {
1598 414601614 : spar_band = bin2band->p_cldfb_map_to_spar_band[cldfb_band];
1599 414601614 : cldfb_par = mixer_mat[out_ch][in_ch][spar_band];
1600 : }
1601 : else
1602 : {
1603 2717168166 : cldfb_par = 0.0f;
1604 13961147931 : for ( spar_band = bin2band->p_spar_start_bands[cldfb_band]; spar_band < num_spar_bands; spar_band++ )
1605 : {
1606 : /* accumulate contributions from all SPAR bands */
1607 11243979765 : cldfb_par += mixer_mat[out_ch][in_ch][spar_band] * bin2band->pp_cldfb_weights_per_spar_band[cldfb_band][spar_band];
1608 : }
1609 : }
1610 :
1611 3131769780 : out_re[out_ch] += cldfb_in_ts_re[in_ch][ts][cldfb_band] * cldfb_par;
1612 3131769780 : out_im[out_ch] += cldfb_in_ts_im[in_ch][ts][cldfb_band] * cldfb_par;
1613 : }
1614 : }
1615 : }
1616 :
1617 : /*update CLDFB data with the parameter-modified data*/
1618 1547917140 : for ( out_ch = 0; out_ch < numch_out; out_ch++ )
1619 : {
1620 1292235600 : cldfb_in_ts_re[out_ch][ts][cldfb_band] = out_re[out_ch];
1621 1292235600 : cldfb_in_ts_im[out_ch][ts][cldfb_band] = out_im[out_ch];
1622 : }
1623 : }
1624 :
1625 5119998 : if ( ( ( slot_idx_start + ts + 1 ) == hSpar->num_slots ) || ( ( md_idx / JBM_CLDFB_SLOTS_IN_SUBFRAME ) != ( hSpar->render_to_md_map[ts + slot_idx_start + 1] / JBM_CLDFB_SLOTS_IN_SUBFRAME ) ) )
1626 : {
1627 : /* we have crossed an unadapted parameter sf border, update previous mixing matrices */
1628 1280046 : int16_t md_sf = md_idx / JBM_CLDFB_SLOTS_IN_SUBFRAME;
1629 1280046 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
1630 1280046 : md_sf = ( num_md_sub_frames == MAX_PARAM_SPATIAL_SUBFRAMES ) ? md_sf : 0;
1631 1280046 : if ( split_band < IVAS_MAX_NUM_BANDS )
1632 : {
1633 1280046 : mvr2r( hSpar->hMdDec->mixer_mat_prev[1][0][0], hSpar->hMdDec->mixer_mat_prev[0][0][0], IVAS_MAX_FB_MIXER_OUT_CH * IVAS_MAX_SPAR_FB_MIXER_IN_CH * IVAS_MAX_NUM_BANDS );
1634 1280046 : mvr2r( hSpar->hMdDec->mixer_mat_prev[2][0][0], hSpar->hMdDec->mixer_mat_prev[1][0][0], IVAS_MAX_FB_MIXER_OUT_CH * IVAS_MAX_SPAR_FB_MIXER_IN_CH * IVAS_MAX_NUM_BANDS );
1635 1280046 : mvr2r( hSpar->hMdDec->mixer_mat_prev[3][0][0], hSpar->hMdDec->mixer_mat_prev[2][0][0], IVAS_MAX_FB_MIXER_OUT_CH * IVAS_MAX_SPAR_FB_MIXER_IN_CH * IVAS_MAX_NUM_BANDS );
1636 1280046 : mvr2r( hSpar->hMdDec->mixer_mat_prev[4][0][0], hSpar->hMdDec->mixer_mat_prev[3][0][0], IVAS_MAX_FB_MIXER_OUT_CH * IVAS_MAX_SPAR_FB_MIXER_IN_CH * IVAS_MAX_NUM_BANDS );
1637 :
1638 7545942 : for ( out_ch = 0; out_ch < numch_out; out_ch++ )
1639 : {
1640 42358992 : for ( in_ch = 0; in_ch < numch_in; in_ch++ )
1641 : {
1642 465190536 : for ( b = 0; b < num_spar_bands; b++ )
1643 : {
1644 429097440 : hSpar->hMdDec->mixer_mat_prev[4][out_ch][in_ch][b] = hSpar->hMdDec->mixer_mat[out_ch][in_ch][b + md_sf * IVAS_MAX_NUM_BANDS];
1645 : }
1646 : }
1647 : }
1648 1280046 : hSpar->i_subframe++;
1649 1280046 : hSpar->i_subframe = min( hSpar->i_subframe, MAX_PARAM_SPATIAL_SUBFRAMES );
1650 : }
1651 : }
1652 : }
1653 :
1654 1285977 : if ( hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_FOA && hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_STEREO && hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_MONO && !( hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL && st_ivas->intern_config == IVAS_AUDIO_CONFIG_FOA ) )
1655 : {
1656 739986 : ivas_dirac_dec_render_sf( st_ivas, output, nchan_internal, cldfb_in_ts_re, cldfb_in_ts_im );
1657 : }
1658 :
1659 1285977 : if ( st_ivas->hDirAC != NULL )
1660 : {
1661 : int16_t outchannels, idx_in, idx_lfe, ch;
1662 766266 : idx_in = 0;
1663 766266 : idx_lfe = 0;
1664 :
1665 766266 : outchannels = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
1666 :
1667 6647808 : for ( ch = 0; ch < outchannels; ch++ )
1668 : {
1669 5881542 : if ( ( st_ivas->hOutSetup.num_lfe > 0 ) && ( st_ivas->hOutSetup.index_lfe[idx_lfe] == ch ) )
1670 : {
1671 122988 : set_zero( output[ch], hSpar->subframe_nbslots[hSpar->subframes_rendered] * num_cldfb_bands );
1672 122988 : if ( idx_lfe < ( st_ivas->hDirACRend->hOutSetup.num_lfe - 1 ) )
1673 : {
1674 0 : idx_lfe++;
1675 : }
1676 : }
1677 : else
1678 : {
1679 5758554 : if ( ( hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_FOA || !( st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL || st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR || st_ivas->hOutSetup.output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ) &&
1680 5100192 : !( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ) )
1681 : {
1682 22442160 : for ( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
1683 : {
1684 17917968 : cldfbSynthesis( &cldfb_in_ts_re[idx_in][ts], &cldfb_in_ts_im[idx_in][ts], &output[ch][ts * num_cldfb_bands], num_cldfb_bands, st_ivas->cldfbSynDec[idx_in] );
1685 : }
1686 : }
1687 5758554 : idx_in++;
1688 : }
1689 : }
1690 : #ifdef DEBUG_SBA_AUDIO_DUMP
1691 : hSpar->numOutChannels = outchannels;
1692 : #endif
1693 : }
1694 : else
1695 : {
1696 : /* CLDFB to time synthesis (overwrite mixer output) */
1697 2598555 : for ( out_ch = 0; out_ch < numch_out_dirac; out_ch++ )
1698 : {
1699 10349040 : for ( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
1700 : {
1701 8270196 : cldfbSynthesis( &cldfb_in_ts_re[out_ch][ts], &cldfb_in_ts_im[out_ch][ts], &output[out_ch][ts * num_cldfb_bands], num_cldfb_bands, st_ivas->cldfbSynDec[out_ch] );
1702 : }
1703 : }
1704 : #ifdef DEBUG_SBA_AUDIO_DUMP
1705 : hSpar->numOutChannels = numch_out_dirac;
1706 : #endif
1707 : }
1708 : #ifdef DEBUG_SBA_AUDIO_DUMP
1709 : /* Dump audio signal after cldfbSynthesis */
1710 : ivas_spar_dump_signal_wav( output_frame, NULL, output, hSpar->numOutChannels, spar_foa_dec_wav[3], "cldfbSynthesis()" );
1711 : #endif
1712 :
1713 1285977 : hSpar->slots_rendered += hSpar->subframe_nbslots[hSpar->subframes_rendered];
1714 1285977 : hSpar->subframes_rendered++;
1715 :
1716 1285977 : pop_wmops();
1717 :
1718 1285977 : return;
1719 : }
|