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 4908 : 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 4908 : error = IVAS_ERR_OK;
78 :
79 4908 : sba_order_internal = min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
80 :
81 4908 : num_channels_internal = ivas_sba_get_nchan_metadata( sba_order_internal, st_ivas->hDecoderConfig->ivas_total_brate );
82 :
83 4908 : hSpar = st_ivas->hSpar;
84 :
85 4908 : if ( !spar_reconfig_flag )
86 : {
87 : /* SPAR decoder handle */
88 843 : 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 4908 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
95 4908 : 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 4695 : num_decor_chs = num_channels_internal - 1;
102 : }
103 :
104 : /* TD decorr. */
105 4908 : 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 4215 : 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 4908 : 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 4908 : hSpar->hMdDec->td_decorr_flag = 1;
123 4908 : if ( hSpar->hTdDecorr )
124 : {
125 4215 : hSpar->hTdDecorr->ducking_flag = ivas_spar_br_table_consts[hSpar->hMdDec->table_idx].td_ducking;
126 : }
127 :
128 : /* set FB config. */
129 4908 : active_w_mixing = -1;
130 4908 : 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 4908 : fb_cfg->pcm_offset = NS2SA( output_Fs, DELAY_FB_1_NS + IVAS_ENC_DELAY_NS + IVAS_DEC_DELAY_NS );
135 4908 : fb_cfg->remix_order = remix_order_set[hSpar->hMdDec->spar_md_cfg.remix_unmix_order];
136 :
137 : /* FB mixer handle */
138 4908 : 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 4908 : 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 4908 : hSpar->hPCA = NULL;
151 4908 : 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 28065 : for ( i = 0; i < num_channels_internal; i++ )
163 : {
164 148230 : for ( j = 0; j < num_channels_internal; j++ )
165 : {
166 1625949 : for ( b = 0; b < IVAS_MAX_NUM_BANDS; b++ )
167 : {
168 1500876 : hSpar->hMdDec->mixer_mat[i][j][b] = 0.0f;
169 9005256 : for ( int16_t i_ts = 0; i_ts < ( MAX_PARAM_SPATIAL_SUBFRAMES + 1 ); i_ts++ )
170 : {
171 7504380 : hSpar->hMdDec->mixer_mat_prev[i_ts][i][j][b] = 0.0f;
172 : }
173 : }
174 : }
175 : }
176 4908 : hSpar->i_subframe = 0;
177 4908 : hSpar->AGC_flag = 0;
178 :
179 : /*-----------------------------------------------------------------*
180 : * Configuration - set SPAR high-level parameters
181 : *-----------------------------------------------------------------*/
182 :
183 4908 : 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 4908 : switch ( sba_order_internal )
186 : {
187 4131 : case 1:
188 4131 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_FOA;
189 4131 : 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 4908 : ivas_output_init( &( st_ivas->hTransSetup ), st_ivas->transport_config );
199 :
200 4908 : set_s( hSpar->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
201 4908 : set_s( hSpar->subframe_nbslots, JBM_CLDFB_SLOTS_IN_SUBFRAME, DEFAULT_JBM_SUBFRAMES_5MS );
202 4908 : hSpar->nb_subframes = DEFAULT_JBM_SUBFRAMES_5MS;
203 4908 : hSpar->subframes_rendered = 0;
204 4908 : hSpar->slots_rendered = 0;
205 4908 : hSpar->num_slots = DEFAULT_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME;
206 :
207 : /* init render timeslot mapping */
208 4908 : set_s( hSpar->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
209 83436 : for ( map_idx = 0; map_idx < DEFAULT_JBM_CLDFB_TIMESLOTS; map_idx++ )
210 : {
211 78528 : hSpar->render_to_md_map[map_idx] = map_idx;
212 : }
213 :
214 : /* allocate transport channels*/
215 4908 : 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 843 : buffer_mode = TC_BUFFER_MODE_RENDERER;
223 843 : nchan_tc = ivas_jbm_dec_get_num_tc_channels( st_ivas );
224 843 : nchan_to_allocate = num_channels_internal;
225 :
226 843 : 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 843 : 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 801 : 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 843 : granularity = ivas_jbm_dec_get_render_granularity( st_ivas->renderer_type, ivas_renderer_secondary_select( st_ivas ), output_Fs );
251 :
252 843 : 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 4908 : st_ivas->hSpar = hSpar;
259 :
260 4908 : return error;
261 : }
262 :
263 :
264 : /*-------------------------------------------------------------------------
265 : * ivas_spar_dec_close()
266 : *
267 : * Deallocate SPAR handle
268 : *------------------------------------------------------------------------*/
269 :
270 5946 : 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 5946 : if ( hSpar == NULL || *hSpar == NULL )
277 : {
278 1038 : return;
279 : }
280 :
281 : /* MD handle */
282 4908 : ivas_spar_md_dec_close( &( *hSpar )->hMdDec );
283 :
284 : /* TD decorrelator handle */
285 4908 : ivas_td_decorr_dec_close( &( *hSpar )->hTdDecorr );
286 :
287 : /* FB mixer handle */
288 4908 : ivas_FB_mixer_close( &( *hSpar )->hFbMixer, output_Fs, spar_reconfig_flag );
289 :
290 : /* AGC */
291 4908 : ivas_spar_agc_dec_close( &( *hSpar )->hAgcDec );
292 :
293 : /* PCA */
294 4908 : if ( ( *hSpar )->hPCA != NULL )
295 : {
296 75 : free( ( *hSpar )->hPCA );
297 75 : ( *hSpar )->hPCA = NULL;
298 : }
299 :
300 4908 : if ( !spar_reconfig_flag )
301 : {
302 843 : free( ( *hSpar ) );
303 843 : ( *hSpar ) = NULL;
304 : }
305 :
306 4908 : return;
307 : }
308 :
309 :
310 : /*-------------------------------------------------------------------*
311 : * ivas_spar_dec()
312 : *
313 : * Principal IVAS SPAR decoder routine
314 : *-------------------------------------------------------------------*/
315 :
316 471039 : 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 471039 : push_wmops( "ivas_spar_decode" );
329 471039 : error = IVAS_ERR_OK;
330 471039 : hDecoderConfig = st_ivas->hDecoderConfig;
331 :
332 471039 : st0 = ( st_ivas->nSCE > 0 ) ? st_ivas->hSCE[0]->hCoreCoder[0] : st_ivas->hCPE[0]->hCoreCoder[0];
333 471039 : bit_stream_orig = st0->bit_stream;
334 471039 : next_bit_pos_orig = st0->next_bit_pos;
335 :
336 471039 : 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 360018 : *nb_bits_read = 0;
343 360018 : last_bit_pos = 0;
344 : }
345 :
346 : /* read DirAC bitstream */
347 471039 : if ( st_ivas->hQMetaData != NULL )
348 : {
349 471039 : 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 471039 : 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 360018 : last_bit_pos = (int16_t) ( ( hDecoderConfig->ivas_total_brate / FRAMES_PER_SEC ) - 1 );
359 : }
360 :
361 471039 : 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 471039 : nb_bits_read_orig = *nb_bits_read;
366 471039 : last_bit_pos -= nb_bits_read_orig;
367 :
368 : /* reverse the bitstream for easier reading of indices */
369 725843733 : for ( i = 0; i < min( MAX_BITS_METADATA, last_bit_pos ); i++ )
370 : {
371 725372694 : bstr_meta[i] = st_ivas->bit_stream[last_bit_pos - i];
372 : }
373 471039 : st0->bit_stream = bstr_meta;
374 471039 : st0->next_bit_pos = 0;
375 471039 : st0->bits_frame = min( MAX_BITS_METADATA, last_bit_pos + 1 );
376 :
377 471039 : if ( !st0->bfi )
378 : {
379 451731 : st0->total_brate = hDecoderConfig->ivas_total_brate; /* to avoid BER detect */
380 : }
381 :
382 : /*---------------------------------------------------------------------*
383 : * Decode SPAR metadata
384 : *---------------------------------------------------------------------*/
385 :
386 471039 : if ( ( error = ivas_spar_dec_MD( st_ivas, st0 ) ) != IVAS_ERR_OK )
387 : {
388 0 : return error;
389 : }
390 :
391 471039 : *nb_bits_read = st0->next_bit_pos + nb_bits_read_orig;
392 471039 : st0->bit_stream = bit_stream_orig;
393 471039 : st0->next_bit_pos = next_bit_pos_orig;
394 :
395 471039 : 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 471039 : pop_wmops();
405 :
406 471039 : 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 444564 : 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 444564 : j = 0;
427 9335844 : for ( i = 0; i < IVAS_SPAR_BR_TABLE_LEN; i++ )
428 : {
429 8891280 : ind1[j] = 0;
430 8891280 : 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 444564 : ind1[j++] = i;
433 : }
434 : }
435 :
436 444564 : assert( j > 0 );
437 444564 : *bitlen = ivas_get_bits_to_encode( j - 1 );
438 :
439 444564 : ind2 = get_next_indice( st0, *bitlen );
440 :
441 444564 : table_idx = ind1[ind2];
442 :
443 444564 : return table_idx;
444 : }
445 :
446 :
447 : /*---------------------------------------------------------------------*
448 : * Function ivas_parse_spar_header()
449 : *
450 : * Get SPAR table index
451 : *---------------------------------------------------------------------*/
452 :
453 444564 : 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 444564 : *table_idx = ivas_get_spar_table_idx_from_coded_idx( ivas_total_brate, sba_order, st0, &bitlen );
462 :
463 444564 : bwidth = ivas_spar_br_table_consts[( *table_idx )].bwidth;
464 :
465 444564 : return bwidth;
466 : }
467 :
468 :
469 1741680 : static float get_random_number(
470 : int16_t *seed )
471 : {
472 1741680 : float x = (float) own_random( seed ) / 32768.0f;
473 :
474 1741680 : return x;
475 : }
476 :
477 :
478 33336 : static float matrix_det(
479 : const float a00,
480 : const float a01,
481 : const float a10,
482 : const float a11 )
483 : {
484 33336 : return a00 * a11 - a01 * a10;
485 : }
486 :
487 :
488 2778 : 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 2778 : float eps = 1e-16f;
495 :
496 2778 : if ( size == 1 )
497 : {
498 0 : out[0][0] = 1.0f / max( in[0][0], eps );
499 :
500 0 : return;
501 : }
502 2778 : 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 2778 : 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 2778 : fac = 1.0f / max( det, eps );
518 :
519 2778 : out[0][0] = matrix_det( in[1][1], in[1][2], in[2][1], in[2][2] ) * fac;
520 2778 : out[1][0] = matrix_det( in[1][0], in[1][2], in[2][0], in[2][2] ) * ( -fac );
521 2778 : out[2][0] = matrix_det( in[1][0], in[1][1], in[2][0], in[2][1] ) * fac;
522 :
523 2778 : out[0][1] = matrix_det( in[0][1], in[0][2], in[2][1], in[2][2] ) * ( -fac );
524 2778 : out[1][1] = matrix_det( in[0][0], in[0][2], in[2][0], in[2][2] ) * fac;
525 2778 : out[2][1] = matrix_det( in[0][0], in[0][1], in[2][0], in[2][1] ) * ( -fac );
526 :
527 2778 : out[0][2] = matrix_det( in[0][1], in[0][2], in[1][1], in[1][2] ) * fac;
528 2778 : out[1][2] = matrix_det( in[0][0], in[0][2], in[1][0], in[1][2] ) * ( -fac );
529 2778 : out[2][2] = matrix_det( in[0][0], in[0][1], in[1][0], in[1][1] ) * fac;
530 :
531 2778 : return;
532 : }
533 :
534 :
535 : /*---------------------------------------------------------------------*
536 : * Function ivas_spar_get_cldfb_gains()
537 : *
538 : *
539 : *---------------------------------------------------------------------*/
540 :
541 2778 : 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 2778 : 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 2778 : pt_len = cldfbAnaDec0->p_filter_length;
566 2778 : num_cldfb_bands = cldfbAnaDec0->no_channels;
567 :
568 2778 : stride = NS2SA( output_Fs, DELAY_CLDFB_NS );
569 2778 : encfb_delay = NS2SA( output_Fs, IVAS_FB_ENC_DELAY_NS );
570 2778 : decfb_delay = NS2SA( output_Fs, IVAS_FB_DEC_DELAY_NS );
571 :
572 2778 : cf_start = (int16_t) hSpar->hFbMixer->cross_fade_start_offset - encfb_delay + decfb_delay; /* time domain after CLDFB synthesis*/
573 2778 : cf_end = (int16_t) hSpar->hFbMixer->cross_fade_end_offset - encfb_delay + decfb_delay;
574 2778 : cf_len = cf_end - cf_start;
575 2778 : weights = hSpar->hFbMixer->cldfb_cross_fade;
576 :
577 2778 : cf_cldfb_start = (int16_t) ceil( ( cf_start - decfb_delay / 2 ) / (float) stride - 0.5f );
578 2778 : cf_cldfb_end = (int16_t) ( ( cf_start - decfb_delay / 2 + cf_len ) / (float) stride - 0.5f );
579 :
580 2778 : num_cf_slots = cf_cldfb_end - cf_cldfb_start + 1;
581 2778 : num_samples = num_cf_slots * stride + pt_len - stride;
582 2778 : seed = RANDOM_INITSEED;
583 2778 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
584 2778 : pp_ts_im[0] = ts_im;
585 2778 : pp_ts_re[0] = ts_re;
586 2778 : set_f( tgt, 0, ( 3 - 1 ) * CLDFB_NO_CHANNELS_MAX + 10 * CLDFB_NO_CHANNELS_MAX );
587 :
588 2778 : cf_start_s = ( cf_start - decfb_delay / 2 ) / output_Fs;
589 2778 : cf_len_s = hSpar->hFbMixer->cross_fade_end_offset / output_Fs - hSpar->hFbMixer->cross_fade_start_offset / output_Fs;
590 :
591 47226 : for ( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
592 : {
593 44448 : weights[ts] = ( ( ( ts + 0.5f ) * stride / output_Fs ) - cf_start_s ) / cf_len_s;
594 44448 : weights[ts] = max( min( weights[ts], 1.0f ), 0.0f );
595 : }
596 2778 : hSpar->hFbMixer->cldfb_cross_fade_start = cf_cldfb_start;
597 2778 : hSpar->hFbMixer->cldfb_cross_fade_end = cf_cldfb_end;
598 :
599 2778 : 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 2778 : tmp_idx = cf_start - cf_cldfb_start * stride;
607 467226 : for ( sample = 0; sample < cf_len; sample++ )
608 : {
609 : /* increasing window function */
610 464448 : tgt[tmp_idx++] = hSpar->hFbMixer->pFilterbank_cross_fade[sample];
611 : }
612 :
613 1047786 : for ( ; tmp_idx < num_samples; tmp_idx++ )
614 : {
615 : /* fill up with ones*/
616 1045008 : tgt[tmp_idx] = 1.0f;
617 : }
618 :
619 1744458 : for ( sample = 0; sample < num_samples; sample++ )
620 : {
621 : /* initialize trasnform matrix with zeros*/
622 1741680 : T[sample][0] = T[sample][1] = T[sample][2] = 0.0f;
623 : }
624 :
625 1309038 : for ( sample = 0; sample < pt_len - stride; sample++ )
626 : {
627 : /* fill internal CLDFB analysis time buffer with data*/
628 1306260 : float x = get_random_number( &seed );
629 :
630 1306260 : cldfbAnaDec0->cldfb_state[sample] = x;
631 : }
632 :
633 11112 : for ( slot = 0; slot < num_cf_slots; slot++ )
634 : {
635 443754 : for ( sample = 0; sample < stride; sample++ )
636 : {
637 435420 : float x = get_random_number( &seed );
638 435420 : ts_inout[sample] = x;
639 : }
640 :
641 8334 : cldfbAnalysis_ts( ts_inout, ts_re, ts_im, num_cldfb_bands, cldfbAnaDec0 );
642 8334 : cldfb_reset_memory( cldfbSynDec0 );
643 8334 : cldfbSynthesis( pp_ts_re, pp_ts_im, ts_inout, num_cldfb_bands, cldfbSynDec0 );
644 :
645 443754 : for ( sample = 0; sample < stride; sample++ )
646 : {
647 435420 : T[slot * stride + sample][slot] = ts_inout[sample];
648 : }
649 :
650 8334 : tmp_idx = pt_len - 1;
651 3927114 : for ( sample = stride; sample < pt_len; sample++ )
652 : {
653 3918780 : T[slot * stride + sample][slot] = cldfbSynDec0->cldfb_state[tmp_idx--];
654 : }
655 : }
656 :
657 : /* target is synthesis output times the cross-fade window*/
658 1744458 : for ( sample = 0; sample < num_samples; sample++ )
659 : {
660 1741680 : tgt[sample] *= ( T[sample][0] + T[sample][1] + T[sample][2] );
661 : }
662 :
663 : /* compute matrices */
664 11112 : for ( slot_row = 0; slot_row < num_cf_slots; slot_row++ )
665 : {
666 25002 : for ( slot_col = slot_row; slot_col < num_cf_slots; slot_col++ )
667 : {
668 16668 : Tt_T[slot_row][slot_col] = 0.0f;
669 10466748 : for ( sample = 0; sample < num_samples; sample++ )
670 : {
671 10450080 : Tt_T[slot_row][slot_col] += T[sample][slot_row] * T[sample][slot_col];
672 : }
673 : }
674 : }
675 :
676 2778 : Tt_T[1][0] = Tt_T[0][1];
677 2778 : Tt_T[2][0] = Tt_T[0][2];
678 2778 : Tt_T[2][1] = Tt_T[1][2];
679 :
680 11112 : for ( slot_row = 0; slot_row < num_cf_slots; slot_row++ )
681 : {
682 8334 : Tt_tgt[slot_row] = 0.0f;
683 5233374 : for ( sample = 0; sample < num_samples; sample++ )
684 : {
685 5225040 : Tt_tgt[slot_row] += T[sample][slot_row] * tgt[sample];
686 : }
687 : }
688 :
689 2778 : matrix_inverse( Tt_T, Tt_T_inv, num_cf_slots );
690 :
691 : /* compute the optimal coefficients */
692 11112 : for ( slot_row = 0; slot_row < num_cf_slots; slot_row++ )
693 : {
694 8334 : float tmp = 0.0f;
695 33336 : for ( slot_col = 0; slot_col < num_cf_slots; slot_col++ )
696 : {
697 25002 : tmp += Tt_T_inv[slot_row][slot_col] * Tt_tgt[slot_col];
698 : }
699 8334 : weights[cf_cldfb_start + slot_row] = max( min( tmp, 1.0f ), 0.0f );
700 : }
701 :
702 2778 : cldfb_reset_memory( cldfbSynDec0 );
703 2778 : cldfb_reset_memory( cldfbAnaDec0 );
704 :
705 2778 : 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 399983040 : 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 399983040 : 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 399983040 : if ( ch >= FOA_CHANNELS )
729 : {
730 : /* never transmitted */
731 54992448 : return 0;
732 : }
733 344990592 : assert( nchan_transport <= FOA_CHANNELS );
734 :
735 344990592 : 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 471039 : 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 471039 : DECODER_CONFIG_HANDLE hDecoderConfig = st_ivas->hDecoderConfig;
755 471039 : SPAR_DEC_HANDLE hSpar = st_ivas->hSpar;
756 :
757 471039 : push_wmops( "ivas_spar_dec_MD" );
758 :
759 : /*---------------------------------------------------------------------*
760 : * Initialization
761 : *---------------------------------------------------------------------*/
762 :
763 471039 : sba_order = min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
764 471039 : bfi = st_ivas->bfi;
765 471039 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
766 471039 : num_channels = ivas_sba_get_nchan_metadata( sba_order, ivas_total_brate );
767 471039 : 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 471039 : num_bands_out = hSpar->hFbMixer->pFb->filterbank_num_bands;
770 :
771 471039 : if ( ivas_total_brate > FRAME_NO_DATA && !bfi )
772 : {
773 445536 : if ( ivas_total_brate > IVAS_SID_5k2 )
774 : {
775 444564 : ivas_parse_spar_header( hDecoderConfig->ivas_total_brate, sba_order, st0, &table_idx );
776 :
777 444564 : 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 378525 : hSpar->hMdDec->spar_md.num_bands = min( SPAR_DIRAC_SPLIT_START_BAND, IVAS_MAX_NUM_BANDS );
784 : }
785 :
786 444564 : 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 445536 : ivas_spar_md_dec_process( st_ivas, st0, num_bands_out, sba_order );
806 :
807 : /*---------------------------------------------------------------------*
808 : * read PCA bits
809 : *---------------------------------------------------------------------*/
810 :
811 445536 : if ( hSpar->hPCA != NULL )
812 : {
813 32577 : ivas_pca_read_bits( st0, hSpar->hPCA );
814 : }
815 :
816 : /*---------------------------------------------------------------------*
817 : * Read AGC bits
818 : *---------------------------------------------------------------------*/
819 :
820 445536 : if ( ivas_total_brate > IVAS_SID_5k2 && !bfi && hSpar->hMdDec->dtx_vad )
821 : {
822 443862 : if ( hSpar->hMdDec->spar_md_cfg.nchan_transport == 1 )
823 : {
824 113793 : hSpar->AGC_flag = get_next_indice( st0, 1 );
825 : }
826 :
827 443862 : 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 445536 : 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 445161 : 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 471039 : pop_wmops();
854 471039 : return IVAS_ERR_OK;
855 : }
856 :
857 :
858 : /*-------------------------------------------------------------------*
859 : * ivas_spar_get_cldfb_slot_gain()
860 : *
861 : *
862 : *-------------------------------------------------------------------*/
863 :
864 6901107 : 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 6901107 : *weight_lowfreq = hSpar->hFbMixer->cldfb_cross_fade[time_slot_idx];
880 :
881 6901107 : output_Fs = (float) hDecoderConfig->output_Fs;
882 6901107 : encfb_delay = IVAS_FB_ENC_DELAY_NS;
883 6901107 : decfb_delay = IVAS_FB_DEC_DELAY_NS;
884 6901107 : xfade_start_ns = hSpar->hFbMixer->cross_fade_start_offset / output_Fs * 1000000000.f - encfb_delay + decfb_delay * 0.5f;
885 6901107 : xfade_delay_subframes = (int16_t) ( xfade_start_ns / ( FRAME_SIZE_NS / MAX_PARAM_SPATIAL_SUBFRAMES ) );
886 :
887 6901107 : i_hist = 4 - xfade_delay_subframes;
888 6901107 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
889 :
890 6901107 : if ( split_band < IVAS_MAX_NUM_BANDS )
891 : {
892 6901107 : if ( hSpar->i_subframe > 3 )
893 : {
894 6834195 : weight = (float) ( time_slot_idx % MAX_PARAM_SPATIAL_SUBFRAMES ) / (float) MAX_PARAM_SPATIAL_SUBFRAMES;
895 : }
896 : else
897 : {
898 66912 : weight = 0.0f;
899 : }
900 6901107 : *time_slot_idx0 = i_hist;
901 6901107 : *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 6901107 : return weight;
912 : }
913 :
914 :
915 : /*-------------------------------------------------------------------*
916 : * ivas_spar_get_parameters()
917 : *
918 : *
919 : *-------------------------------------------------------------------*/
920 :
921 6901107 : 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 6901107 : weight = ivas_spar_get_cldfb_slot_gain( hSpar, hDecoderConfig, ts, &ts0, &ts1, &weight_20ms );
935 :
936 6901107 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
937 88466487 : for ( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
938 : {
939 462819348 : for ( out_ch = 0; out_ch < num_ch_out; out_ch++ )
940 : {
941 381253968 : if ( split_band < IVAS_MAX_NUM_BANDS
942 : /* 20ms cross-fade for Transport channels in all frequency bands */
943 381253968 : && ( 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 1672992744 : for ( in_ch = 0; in_ch < num_ch_in; in_ch++ )
947 : {
948 1411279968 : if ( hSpar->i_subframe > 3 )
949 : {
950 1397300448 : par_mat[out_ch][in_ch][spar_band] = ( 1.0f - weight ) * hSpar->hMdDec->mixer_mat_prev[ts0][out_ch][in_ch][spar_band] +
951 1397300448 : weight * hSpar->hMdDec->mixer_mat_prev[ts1][out_ch][in_ch][spar_band];
952 : }
953 : else
954 : {
955 13979520 : 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 762683304 : for ( in_ch = 0; in_ch < num_ch_in; in_ch++ )
962 : {
963 : /* 20ms Transport channel reconstruction with matching encoder/decoder processing */
964 643142112 : 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 643142112 : 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 6901107 : return;
972 : }
973 :
974 :
975 : /*-------------------------------------------------------------------*
976 : * ivas_spar_get_skip_mat()
977 : *
978 : *
979 : *-------------------------------------------------------------------*/
980 :
981 1273977 : 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 7515561 : for ( out_ch = 0; out_ch < num_ch_out; out_ch++ )
993 : {
994 42237216 : for ( in_ch = 0; in_ch < num_ch_in; in_ch++ )
995 : {
996 35995632 : skip_mat[out_ch][in_ch] = 1;
997 35995632 : skip_flag = 1;
998 121905273 : for ( i_ts = 0; i_ts < MAX_PARAM_SPATIAL_SUBFRAMES; i_ts++ )
999 : {
1000 1143378534 : for ( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
1001 : {
1002 1057468893 : if ( hSpar->hMdDec->mixer_mat_prev[1 + i_ts][out_ch][in_ch][spar_band] != 0.0f )
1003 : {
1004 14661765 : skip_flag = 0;
1005 14661765 : break;
1006 : }
1007 : }
1008 :
1009 100571406 : if ( skip_flag == 0 )
1010 : {
1011 14661765 : skip_mat[out_ch][in_ch] = 0;
1012 14661765 : break;
1013 : }
1014 : }
1015 :
1016 35995632 : if ( skip_mat[out_ch][in_ch] == 1 )
1017 : {
1018 77075487 : for ( i_ts = 0; i_ts < num_md_sub_frames; i_ts++ )
1019 : {
1020 716576694 : for ( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
1021 : {
1022 660835074 : if ( hSpar->hMdDec->mixer_mat[out_ch][in_ch][spar_band + i_ts * IVAS_MAX_NUM_BANDS] != 0.0f )
1023 : {
1024 94881 : skip_flag = 0;
1025 94881 : break;
1026 : }
1027 : }
1028 :
1029 55836501 : if ( skip_flag == 0 )
1030 : {
1031 94881 : skip_mat[out_ch][in_ch] = 0;
1032 94881 : break;
1033 : }
1034 : }
1035 : }
1036 : }
1037 : }
1038 :
1039 1273977 : 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 319977 : 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 319977 : push_wmops( "ivas_spar_dec_agc_pca" );
1155 :
1156 319977 : hSpar = st_ivas->hSpar;
1157 319977 : hDecoderConfig = st_ivas->hDecoderConfig;
1158 319977 : nchan_transport = hSpar->hMdDec->spar_md_cfg.nchan_transport;
1159 :
1160 319977 : if ( st_ivas->nchan_transport >= 3 )
1161 : {
1162 : float temp;
1163 : int16_t i;
1164 : /*convert WYZX downmix to WYXZ*/
1165 168927408 : for ( i = 0; i < output_frame; i++ )
1166 : {
1167 168721920 : temp = output[2][i];
1168 168721920 : output[2][i] = output[3][i];
1169 168721920 : output[3][i] = temp;
1170 : }
1171 : }
1172 :
1173 319977 : if ( hSpar->hMdDec->td_decorr_flag )
1174 : {
1175 319977 : 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 319977 : 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 319977 : 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 319977 : pop_wmops();
1201 :
1202 319977 : return;
1203 : }
1204 :
1205 :
1206 : /*-------------------------------------------------------------------*
1207 : * ivas_spar_dec_set_render_map()
1208 : *
1209 : *
1210 : *-------------------------------------------------------------------*/
1211 :
1212 431292 : 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 431292 : hSpar = st_ivas->hSpar;
1220 : #ifdef DEBUGGING
1221 : assert( hSpar );
1222 : #endif
1223 :
1224 : /* adapt subframes */
1225 431292 : hSpar->num_slots = nCldfbTs;
1226 431292 : hSpar->slots_rendered = 0;
1227 431292 : hSpar->subframes_rendered = 0;
1228 :
1229 431292 : set_s( hSpar->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
1230 431292 : 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 431292 : if ( !( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC ) )
1235 : {
1236 372246 : st_ivas->hTcBuffer->nb_subframes = hSpar->nb_subframes;
1237 372246 : mvs2s( hSpar->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, hSpar->nb_subframes );
1238 : }
1239 :
1240 431292 : ivas_jbm_dec_get_md_map( DEFAULT_JBM_CLDFB_TIMESLOTS, nCldfbTs, 1, 0, DEFAULT_JBM_CLDFB_TIMESLOTS, hSpar->render_to_md_map );
1241 :
1242 431292 : return;
1243 : }
1244 :
1245 : /*-------------------------------------------------------------------*
1246 : * ivas_spar_dec_set_render_params()
1247 : *
1248 : * IVAS SPAR set rendering parameters
1249 : *-------------------------------------------------------------------*/
1250 :
1251 431292 : 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 431292 : hSpar = st_ivas->hSpar;
1261 431292 : nchan_transport = hSpar->hMdDec->spar_md_cfg.nchan_transport;
1262 431292 : num_bands_out = hSpar->hFbMixer->pFb->filterbank_num_bands;
1263 431292 : 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 431292 : ivas_spar_dec_set_render_map( st_ivas, n_cldfb_slots );
1266 :
1267 431292 : return;
1268 : }
1269 :
1270 :
1271 : /*-------------------------------------------------------------------*
1272 : * ivas_spar_dec_digest_tc()
1273 : *
1274 : *
1275 : *-------------------------------------------------------------------*/
1276 :
1277 431292 : 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 431292 : hSpar = st_ivas->hSpar;
1287 431292 : 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 316977 : ch_sba_idx = 0;
1297 316977 : 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 316977 : default_frame = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / FRAMES_PER_SEC );
1307 316977 : nSamplesLeftForTD = nSamplesForRendering;
1308 316977 : nchan_internal = ivas_sba_get_nchan_metadata( st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate );
1309 :
1310 1871295 : for ( ch = 0; ch < nchan_internal; ch++ )
1311 : {
1312 1554318 : pPcm_tmp[ch] = Pcm_tmp[ch];
1313 1554318 : p_tc[ch] = st_ivas->hTcBuffer->tc[ch + ch_sba_idx];
1314 : }
1315 :
1316 633966 : while ( nSamplesLeftForTD )
1317 : {
1318 316989 : int16_t nSamplesToDecorr = min( nSamplesLeftForTD, default_frame );
1319 :
1320 316989 : if ( hSpar->hTdDecorr )
1321 : {
1322 289779 : ivas_td_decorr_process( hSpar->hTdDecorr, p_tc, pPcm_tmp, nSamplesToDecorr );
1323 289779 : if ( hSpar->hTdDecorr->num_apd_outputs >= ( nchan_internal - nchan_transport ) )
1324 : {
1325 778527 : for ( ch = 0; ch < nchan_internal - nchan_transport; ch++ )
1326 : {
1327 511158 : 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 1871355 : for ( ch = 0; ch < nchan_internal; ch++ )
1343 : {
1344 1554366 : p_tc[ch] += nSamplesToDecorr;
1345 : }
1346 :
1347 316989 : nSamplesLeftForTD -= nSamplesToDecorr;
1348 : }
1349 : }
1350 :
1351 431292 : ivas_spar_dec_set_render_params( st_ivas, nCldfbSlots );
1352 :
1353 431292 : return;
1354 : }
1355 :
1356 :
1357 : /*-------------------------------------------------------------------*
1358 : * ivas_spar_dec_upmixer_sf()
1359 : *
1360 : * IVAS SPAR upmixer
1361 : *-------------------------------------------------------------------*/
1362 :
1363 1273977 : 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[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS][CLDFB_NO_COL_MAX];
1371 : float *cldfb_in_ts_im[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS][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[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS];
1377 : int16_t md_idx;
1378 : float Pcm_tmp[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS][L_FRAME48k];
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 1273977 : push_wmops( "ivas_spar_dec_upmixer_sf" );
1387 1273977 : hSpar = st_ivas->hSpar;
1388 1273977 : hDecoderConfig = st_ivas->hDecoderConfig;
1389 1273977 : nchan_transport = hSpar->hMdDec->spar_md_cfg.nchan_transport;
1390 :
1391 1273977 : num_cldfb_bands = hSpar->hFbMixer->pFb->fb_bin_to_band.num_cldfb_bands;
1392 1273977 : numch_in = hSpar->hFbMixer->fb_cfg->num_in_chans;
1393 1273977 : numch_out = hSpar->hFbMixer->fb_cfg->num_out_chans;
1394 1273977 : 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 1273977 : slot_size = NS2SA( st_ivas->hDecoderConfig->output_Fs, CLDFB_SLOT_NS );
1396 1273977 : slot_idx_start = hSpar->slots_rendered;
1397 :
1398 1273977 : 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 239088 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1410 : {
1411 168000 : for ( i = 0; i < nchan_ism; i++ )
1412 : {
1413 132000 : p_tc[i + nchan_internal] = st_ivas->hTcBuffer->tc[i] + slot_idx_start * slot_size;
1414 : }
1415 : }
1416 : }
1417 : else
1418 : {
1419 5783085 : for ( i = 0; i < nchan_internal; i++ )
1420 : {
1421 4748196 : p_tc[i] = st_ivas->hTcBuffer->tc[i] + slot_idx_start * slot_size;
1422 : }
1423 : }
1424 :
1425 : #ifdef DEBUG_SPAR_BYPASS_EVS_CODEC
1426 : /* by-pass core-coder */
1427 : /*write the core coder output to a file for debugging*/
1428 : {
1429 : float tmp;
1430 : int16_t pcm, j;
1431 : for ( j = 0; j < output_frame; j++ )
1432 : {
1433 : for ( i = 0; i < nchan_transport; i++ )
1434 : {
1435 : tmp = roundf( output[i][j] * PCM16_TO_FLT_FAC );
1436 : pcm = ( tmp > MAX16B_FLT ) ? MAX16B : ( tmp < MIN16B_FLT ) ? MIN16B
1437 : : (short) tmp;
1438 : dbgwrite( &pcm, sizeof( int16_t ), 1, 1, "dmx_dec.raw" );
1439 : }
1440 : }
1441 : }
1442 :
1443 : /*overwrite the core coder output with core input for debugging*/
1444 : {
1445 : static FILE *fid_enc = 0;
1446 : int16_t smp;
1447 :
1448 : if ( !fid_enc )
1449 : {
1450 : fid_enc = fopen( "evs_input_float.raw", "rb" );
1451 : }
1452 : for ( smp = 0; smp < L_FRAME48k; smp++ )
1453 : {
1454 : for ( in_ch = 0; in_ch < nchan_transport; in_ch++ )
1455 : {
1456 : fread( &output[in_ch][smp], sizeof( float ), 1, fid_enc );
1457 : }
1458 : }
1459 : }
1460 : #endif
1461 :
1462 : /*---------------------------------------------------------------------*
1463 : * TD Decorr and pcm ingest
1464 : *---------------------------------------------------------------------*/
1465 :
1466 1273977 : if ( hSpar->hMdDec->td_decorr_flag )
1467 : {
1468 1273977 : num_in_ingest = nchan_internal;
1469 : }
1470 : else
1471 : {
1472 0 : num_in_ingest = nchan_transport;
1473 : }
1474 :
1475 : /*---------------------------------------------------------------------*
1476 : * PCA decoder
1477 : *---------------------------------------------------------------------*/
1478 : #ifdef DEBUG_SBA_AUDIO_DUMP
1479 : hSpar->pca_ingest_channels = num_in_ingest;
1480 : #endif
1481 :
1482 1273977 : hSpar->hFbMixer->fb_cfg->num_in_chans = num_in_ingest;
1483 :
1484 :
1485 : /*---------------------------------------------------------------------*
1486 : * Prepare CLDFB buffers
1487 : *---------------------------------------------------------------------*/
1488 :
1489 : /* set-up pointers */
1490 1273977 : if ( hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_FOA )
1491 : {
1492 : /* at this point, output channels are used as intermediate procesing buffers */
1493 15854706 : for ( in_ch = 0; in_ch < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; in_ch++ )
1494 : {
1495 75498600 : for ( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
1496 : {
1497 60398880 : cldfb_in_ts_re[in_ch][ts] = &Pcm_tmp[in_ch][ts * num_cldfb_bands];
1498 60398880 : cldfb_in_ts_im[in_ch][ts] = &Pcm_tmp[in_ch][ts * num_cldfb_bands + 4 * num_cldfb_bands];
1499 : }
1500 : }
1501 : }
1502 : else
1503 : {
1504 2612955 : for ( in_ch = 0; in_ch < numch_in; in_ch++ )
1505 : {
1506 10469820 : for ( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
1507 : {
1508 8375856 : cldfb_in_ts_re[in_ch][ts] = &Pcm_tmp[in_ch][ts * num_cldfb_bands];
1509 8375856 : cldfb_in_ts_im[in_ch][ts] = &Pcm_tmp[in_ch][ts * num_cldfb_bands + 4 * num_cldfb_bands];
1510 : }
1511 : }
1512 : }
1513 :
1514 : /*---------------------------------------------------------------------*
1515 : * CLDFB Processing and Synthesis
1516 : *---------------------------------------------------------------------*/
1517 :
1518 1273977 : num_spar_bands = hSpar->hFbMixer->pFb->filterbank_num_bands;
1519 :
1520 : /* apply parameters */
1521 : /* determine if we can skip certain data */
1522 1273977 : 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*/
1523 :
1524 1273977 : numch_out_dirac = hDecoderConfig->nchan_out;
1525 :
1526 : #ifdef DEBUG_SBA_AUDIO_DUMP
1527 : /* Dump audio signal after ivas_agc_dec_process */
1528 : ivas_spar_dump_signal_wav( output_frame, p_tc, NULL, numch_in, spar_foa_dec_wav[4], "ivas_spar_upmixer()" );
1529 : #endif
1530 :
1531 : /* CLDFB analysis of incoming frame */
1532 7515561 : for ( in_ch = 0; in_ch < numch_in; in_ch++ )
1533 : {
1534 31112280 : for ( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
1535 : {
1536 24870696 : 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] );
1537 : }
1538 : }
1539 :
1540 1273977 : if ( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1541 : {
1542 168000 : for ( ; in_ch < st_ivas->nchan_ism + numch_in; in_ch++ )
1543 : {
1544 660000 : for ( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
1545 : {
1546 528000 : 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] );
1547 : }
1548 : }
1549 : }
1550 :
1551 1273977 : if ( hDecoderConfig->ivas_total_brate < IVAS_24k4 && ( st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA2 || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA3 ) )
1552 : {
1553 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 );
1554 : }
1555 :
1556 6345975 : for ( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
1557 : {
1558 5071998 : md_idx = hSpar->render_to_md_map[ts + slot_idx_start];
1559 5071998 : ivas_spar_get_parameters( hSpar, hDecoderConfig, md_idx, numch_out, numch_in, num_spar_bands, mixer_mat );
1560 :
1561 5071998 : if ( hDecoderConfig->ivas_total_brate < IVAS_24k4 && ( st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA2 || st_ivas->intern_config == IVAS_AUDIO_CONFIG_HOA3 ) )
1562 : {
1563 1295424 : for ( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
1564 : {
1565 5978880 : for ( out_ch = 0; out_ch < numch_out; out_ch++ )
1566 : {
1567 23915520 : for ( in_ch = 0; in_ch < numch_in; in_ch++ )
1568 : {
1569 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];
1570 19132416 : hSpar->hMdDec->mixer_mat_prev2[out_ch][in_ch][spar_band] = mixer_mat[out_ch][in_ch][spar_band];
1571 : }
1572 : }
1573 : }
1574 : }
1575 :
1576 257873538 : for ( cldfb_band = 0; cldfb_band < num_cldfb_bands; cldfb_band++ )
1577 : {
1578 : float out_re[IVAS_SPAR_MAX_CH];
1579 : float out_im[IVAS_SPAR_MAX_CH];
1580 : float cldfb_par;
1581 252801540 : ivas_fb_bin_to_band_data_t *bin2band = &hSpar->hFbMixer->pFb->fb_bin_to_band;
1582 :
1583 1533517140 : for ( out_ch = 0; out_ch < numch_out; out_ch++ )
1584 : {
1585 1280715600 : out_re[out_ch] = 0.0f;
1586 1280715600 : out_im[out_ch] = 0.0f;
1587 :
1588 9005777040 : for ( in_ch = 0; in_ch < numch_in; in_ch++ )
1589 : {
1590 7725061440 : if ( b_skip_mat[out_ch][in_ch] == 0 )
1591 : {
1592 3106742580 : if ( cldfb_band < CLDFB_PAR_WEIGHT_START_BAND ) /* tuning parameter, depends on how much SPAR Filters overlap for the CLDFB bands */
1593 : {
1594 411681774 : spar_band = bin2band->p_cldfb_map_to_spar_band[cldfb_band];
1595 411681774 : cldfb_par = mixer_mat[out_ch][in_ch][spar_band];
1596 : }
1597 : else
1598 : {
1599 2695060806 : cldfb_par = 0.0f;
1600 13853113851 : for ( spar_band = bin2band->p_spar_start_bands[cldfb_band]; spar_band < num_spar_bands; spar_band++ )
1601 : {
1602 : /* accumulate contributions from all SPAR bands */
1603 11158053045 : cldfb_par += mixer_mat[out_ch][in_ch][spar_band] * bin2band->pp_cldfb_weights_per_spar_band[cldfb_band][spar_band];
1604 : }
1605 : }
1606 :
1607 3106742580 : out_re[out_ch] += cldfb_in_ts_re[in_ch][ts][cldfb_band] * cldfb_par;
1608 3106742580 : out_im[out_ch] += cldfb_in_ts_im[in_ch][ts][cldfb_band] * cldfb_par;
1609 : }
1610 : }
1611 : }
1612 :
1613 : /*update CLDFB data with the parameter-modified data*/
1614 1533517140 : for ( out_ch = 0; out_ch < numch_out; out_ch++ )
1615 : {
1616 1280715600 : cldfb_in_ts_re[out_ch][ts][cldfb_band] = out_re[out_ch];
1617 1280715600 : cldfb_in_ts_im[out_ch][ts][cldfb_band] = out_im[out_ch];
1618 : }
1619 : }
1620 :
1621 5071998 : 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 ) ) )
1622 : {
1623 : /* we have crossed an unadapted parameter sf border, update previous mixing matrices */
1624 1268046 : int16_t md_sf = md_idx / JBM_CLDFB_SLOTS_IN_SUBFRAME;
1625 1268046 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
1626 1268046 : md_sf = ( num_md_sub_frames == MAX_PARAM_SPATIAL_SUBFRAMES ) ? md_sf : 0;
1627 1268046 : if ( split_band < IVAS_MAX_NUM_BANDS )
1628 : {
1629 1268046 : 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 );
1630 1268046 : 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 );
1631 1268046 : 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 );
1632 1268046 : 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 );
1633 :
1634 7485942 : for ( out_ch = 0; out_ch < numch_out; out_ch++ )
1635 : {
1636 42118992 : for ( in_ch = 0; in_ch < numch_in; in_ch++ )
1637 : {
1638 462694536 : for ( b = 0; b < num_spar_bands; b++ )
1639 : {
1640 426793440 : 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];
1641 : }
1642 : }
1643 : }
1644 1268046 : hSpar->i_subframe++;
1645 1268046 : hSpar->i_subframe = min( hSpar->i_subframe, MAX_PARAM_SPATIAL_SUBFRAMES );
1646 : }
1647 : }
1648 : }
1649 :
1650 1273977 : 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 ) )
1651 : {
1652 727986 : ivas_dirac_dec_render_sf( st_ivas, output, nchan_internal, cldfb_in_ts_re, cldfb_in_ts_im );
1653 : }
1654 :
1655 1273977 : if ( st_ivas->hDirAC != NULL )
1656 : {
1657 : int16_t outchannels, idx_in, idx_lfe, ch;
1658 754266 : idx_in = 0;
1659 754266 : idx_lfe = 0;
1660 :
1661 754266 : outchannels = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
1662 :
1663 6323808 : for ( ch = 0; ch < outchannels; ch++ )
1664 : {
1665 5569542 : if ( ( st_ivas->hOutSetup.num_lfe > 0 ) && ( st_ivas->hOutSetup.index_lfe[idx_lfe] == ch ) )
1666 : {
1667 122988 : set_zero( output[ch], hSpar->subframe_nbslots[hSpar->subframes_rendered] * num_cldfb_bands );
1668 122988 : if ( idx_lfe < ( st_ivas->hDirACRend->hOutSetup.num_lfe - 1 ) )
1669 : {
1670 0 : idx_lfe++;
1671 : }
1672 : }
1673 : else
1674 : {
1675 5446554 : 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 ) ) &&
1676 4764192 : !( st_ivas->ivas_format == SBA_ISM_FORMAT && st_ivas->ism_mode == ISM_SBA_MODE_DISC && st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ) )
1677 : {
1678 21482160 : for ( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
1679 : {
1680 17149968 : 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] );
1681 : }
1682 : }
1683 5446554 : idx_in++;
1684 : }
1685 : }
1686 : #ifdef DEBUG_SBA_AUDIO_DUMP
1687 : hSpar->numOutChannels = outchannels;
1688 : #endif
1689 : }
1690 : else
1691 : {
1692 : /* CLDFB to time synthesis (overwrite mixer output) */
1693 2598555 : for ( out_ch = 0; out_ch < numch_out_dirac; out_ch++ )
1694 : {
1695 10349040 : for ( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
1696 : {
1697 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] );
1698 : }
1699 : }
1700 : #ifdef DEBUG_SBA_AUDIO_DUMP
1701 : hSpar->numOutChannels = numch_out_dirac;
1702 : #endif
1703 : }
1704 : #ifdef DEBUG_SBA_AUDIO_DUMP
1705 : /* Dump audio signal after cldfbSynthesis */
1706 : ivas_spar_dump_signal_wav( output_frame, NULL, output, hSpar->numOutChannels, spar_foa_dec_wav[3], "cldfbSynthesis()" );
1707 : #endif
1708 :
1709 1273977 : hSpar->slots_rendered += hSpar->subframe_nbslots[hSpar->subframes_rendered];
1710 1273977 : hSpar->subframes_rendered++;
1711 :
1712 1273977 : pop_wmops();
1713 :
1714 1273977 : return;
1715 : }
|