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 <math.h>
35 : #include <assert.h>
36 : #include "options.h"
37 : #include "ivas_stat_dec.h"
38 : #include "prot_fx.h"
39 : #include "string.h"
40 : #include "ivas_prot_rend_fx.h"
41 : #include "rom_com.h"
42 : #include "ivas_rom_com.h"
43 : #include "ivas_rom_dec.h"
44 : #include "ivas_stat_com.h"
45 : #include "stat_com.h"
46 : #include "wmc_auto.h"
47 : #include "ivas_prot_fx.h"
48 : #ifdef DEBUGGING
49 : #include "debug.h"
50 : #endif
51 :
52 : /*-------------------------------------------------------------------*
53 : * Local function prototypes
54 : *--------------------------------------------------------------------*/
55 :
56 : static ivas_error ivas_spar_dec_MD_fx( Decoder_Struct *st_ivas, Decoder_State *st0 );
57 :
58 :
59 : /*-------------------------------------------------------------------------
60 : * ivas_spar_dec_open()
61 : *
62 : * Allocate and initialize SPAR decoder handle and sub-handles
63 : *------------------------------------------------------------------------*/
64 :
65 1557 : ivas_error ivas_spar_dec_open_fx(
66 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
67 : const Word16 spar_reconfig_flag /* i : SPAR reconfiguration flag Q0*/
68 : )
69 : {
70 : SPAR_DEC_HANDLE hSpar;
71 : ivas_error error;
72 : Word16 sba_order_internal, num_channels_internal;
73 : IVAS_FB_CFG *fb_cfg;
74 : Word16 i, j, b, active_w_mixing;
75 : Word32 output_Fs;
76 : Word16 num_decor_chs, map_idx;
77 :
78 1557 : error = IVAS_ERR_OK;
79 1557 : move32();
80 :
81 1557 : sba_order_internal = s_min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER ); /*Q0*/
82 1557 : move16();
83 :
84 1557 : num_channels_internal = ivas_sba_get_nchan_metadata_fx( sba_order_internal, st_ivas->hDecoderConfig->ivas_total_brate ); /*Q0*/
85 :
86 1557 : hSpar = st_ivas->hSpar;
87 :
88 1557 : IF( !spar_reconfig_flag )
89 : {
90 : /* SPAR decoder handle */
91 278 : IF( ( hSpar = (SPAR_DEC_HANDLE) malloc( sizeof( SPAR_DEC_DATA ) ) ) == NULL )
92 : {
93 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR decoder" );
94 : }
95 : }
96 :
97 1557 : output_Fs = st_ivas->hDecoderConfig->output_Fs; /*Q0*/
98 1557 : move32();
99 1557 : IF( GT_16( num_channels_internal, ( SBA_HOA2_ORDER + 1 ) * ( SBA_HOA2_ORDER + 1 ) ) )
100 : {
101 71 : num_decor_chs = IVAS_HBR_MAX_DECOR_CHS; /*Q0*/
102 71 : move16();
103 : }
104 : ELSE
105 : {
106 1486 : num_decor_chs = sub( num_channels_internal, 1 ); /*Q0*/
107 : }
108 :
109 : /* TD decorr. */
110 1557 : test();
111 1557 : test();
112 1557 : test();
113 1557 : test();
114 1557 : IF( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) && ( ( EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_MONO ) || EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) ) || ( GE_32( st_ivas->hDecoderConfig->ivas_total_brate, IVAS_256k ) && EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_FOA ) ) ) )
115 : {
116 231 : hSpar->hTdDecorr = NULL;
117 : }
118 : ELSE
119 : {
120 1326 : IF( ( error = ivas_td_decorr_dec_open_fx( &hSpar->hTdDecorr, output_Fs, add( num_decor_chs, 1 ), 1 ) ) != IVAS_ERR_OK )
121 : {
122 0 : return error;
123 : }
124 : }
125 :
126 : /* MD handle */
127 1557 : 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 )
128 : {
129 0 : return error;
130 : }
131 1557 : hSpar->hMdDec->td_decorr_flag = 1;
132 1557 : move16();
133 1557 : if ( hSpar->hTdDecorr )
134 : {
135 1326 : hSpar->hTdDecorr->ducking_flag = ivas_spar_br_table_consts[hSpar->hMdDec->table_idx].td_ducking; /*Q0*/
136 1326 : move16();
137 : }
138 :
139 : /* set FB config. */
140 1557 : active_w_mixing = -1;
141 1557 : move16();
142 1557 : IF( NE_32( ( error = ivas_fb_set_cfg( &fb_cfg, SBA_FORMAT, num_channels_internal, num_channels_internal, active_w_mixing, output_Fs, 0 ) ), IVAS_ERR_OK ) )
143 : {
144 0 : return error;
145 : }
146 1557 : fb_cfg->pcm_offset = NS2SA_FX2( output_Fs, DELAY_FB_1_NS + IVAS_ENC_DELAY_NS + IVAS_DEC_DELAY_NS ); /*Q0*/
147 1557 : move16();
148 1557 : fb_cfg->remix_order = remix_order_set[hSpar->hMdDec->spar_md_cfg.remix_unmix_order]; /*Q0*/
149 1557 : move16();
150 :
151 : /* FB mixer handle */
152 1557 : IF( NE_32( ( error = ivas_FB_mixer_open_fx( &hSpar->hFbMixer, output_Fs, fb_cfg, spar_reconfig_flag ) ), IVAS_ERR_OK ) )
153 : {
154 0 : return error;
155 : }
156 :
157 : /* AGC handle */
158 1557 : IF( NE_32( ( error = ivas_spar_agc_dec_open_fx( &hSpar->hAgcDec, output_Fs ) ), IVAS_ERR_OK ) )
159 : {
160 0 : return error;
161 : }
162 :
163 : /* PCA handle */
164 1557 : hSpar->hPCA = NULL;
165 1557 : test();
166 1557 : IF( EQ_32( st_ivas->hDecoderConfig->ivas_total_brate, PCA_BRATE ) && EQ_16( sba_order_internal, 1 ) )
167 : {
168 37 : IF( ( hSpar->hPCA = (PCA_DEC_STATE *) malloc( sizeof( PCA_DEC_STATE ) ) ) == NULL )
169 : {
170 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for PCA decoder" );
171 : }
172 :
173 37 : ivas_pca_dec_init_fx( hSpar->hPCA );
174 : }
175 :
176 : /* mixer_mat intitialization */
177 8899 : FOR( i = 0; i < num_channels_internal; i++ )
178 : {
179 47046 : FOR( j = 0; j < num_channels_internal; j++ )
180 : {
181 516152 : FOR( b = 0; b < IVAS_MAX_NUM_BANDS; b++ )
182 : {
183 476448 : hSpar->hMdDec->mixer_mat_fx[i][j][b] = 0;
184 476448 : move32();
185 2858688 : FOR( Word16 i_ts = 0; i_ts < ( MAX_PARAM_SPATIAL_SUBFRAMES + 1 ); i_ts++ )
186 : {
187 2382240 : hSpar->hMdDec->mixer_mat_prev_fx[i_ts][i][j][b] = 0;
188 2382240 : move32();
189 : }
190 : }
191 : }
192 : }
193 1557 : hSpar->hMdDec->Q_mixer_mat = Q31;
194 1557 : move16();
195 1557 : hSpar->i_subframe = 0;
196 1557 : move16();
197 1557 : hSpar->AGC_flag = 0;
198 1557 : move16();
199 :
200 : /*-----------------------------------------------------------------*
201 : * Configuration - set SPAR high-level parameters
202 : *-----------------------------------------------------------------*/
203 :
204 1557 : ivas_spar_config_fx( 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 );
205 :
206 1557 : SWITCH( sba_order_internal )
207 : {
208 1317 : case 1:
209 1317 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_FOA;
210 1317 : move32();
211 1317 : BREAK;
212 55 : case 2:
213 55 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_HOA2;
214 55 : move32();
215 55 : BREAK;
216 185 : case 3:
217 185 : st_ivas->transport_config = IVAS_AUDIO_CONFIG_HOA3;
218 185 : move32();
219 185 : BREAK;
220 : }
221 :
222 1557 : ivas_output_init( &( st_ivas->hTransSetup ), st_ivas->transport_config );
223 :
224 1557 : set16_fx( hSpar->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
225 1557 : set16_fx( hSpar->subframe_nbslots, JBM_CLDFB_SLOTS_IN_SUBFRAME, DEFAULT_JBM_SUBFRAMES_5MS ); /*Q0*/
226 1557 : hSpar->nb_subframes = DEFAULT_JBM_SUBFRAMES_5MS; /*Q0*/
227 1557 : move16();
228 1557 : hSpar->subframes_rendered = 0;
229 1557 : move16();
230 1557 : hSpar->slots_rendered = 0;
231 1557 : move16();
232 1557 : hSpar->num_slots = DEFAULT_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME; /*Q0*/
233 1557 : move16();
234 :
235 : /* init render timeslot mapping */
236 1557 : set16_fx( hSpar->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
237 26469 : FOR( map_idx = 0; map_idx < DEFAULT_JBM_CLDFB_TIMESLOTS; map_idx++ )
238 : {
239 24912 : hSpar->render_to_md_map[map_idx] = map_idx; /*Q0*/
240 24912 : move16();
241 : }
242 :
243 : /* allocate transport channels*/
244 1557 : IF( st_ivas->hTcBuffer == NULL )
245 : {
246 : Word16 nchan_to_allocate;
247 : Word16 nchan_tc;
248 : TC_BUFFER_MODE buffer_mode;
249 : Word16 granularity;
250 :
251 278 : buffer_mode = TC_BUFFER_MODE_RENDERER;
252 278 : move32();
253 278 : nchan_tc = ivas_jbm_dec_get_num_tc_channels_fx( st_ivas ); /*Q0*/
254 278 : nchan_to_allocate = num_channels_internal; /*Q0*/
255 278 : move16();
256 :
257 278 : test();
258 278 : if ( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
259 : {
260 17 : nchan_to_allocate = add( nchan_to_allocate, st_ivas->nchan_ism ); /*Q0*/
261 : }
262 :
263 278 : granularity = NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, CLDFB_SLOT_NS ); /*Q0*/
264 278 : move16();
265 :
266 278 : test();
267 278 : test();
268 278 : test();
269 278 : IF( ( EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) || EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_MONO ) ) )
270 : {
271 14 : test();
272 14 : test();
273 14 : IF( ( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) && EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) ) )
274 : {
275 1 : nchan_tc = add( st_ivas->hDecoderConfig->nchan_out, st_ivas->nchan_ism ); /*Q0*/
276 1 : nchan_to_allocate = nchan_tc; /*Q0*/
277 1 : move16();
278 : }
279 : ELSE
280 : {
281 13 : buffer_mode = TC_BUFFER_MODE_BUFFER;
282 13 : move32();
283 13 : nchan_tc = st_ivas->hDecoderConfig->nchan_out; /*Q0*/
284 13 : move16();
285 13 : nchan_to_allocate = nchan_tc; /*Q0*/
286 13 : move16();
287 : }
288 : }
289 264 : ELSE IF( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) || EQ_32( st_ivas->renderer_type, RENDERER_STEREO_PARAMETRIC ) )
290 : {
291 48 : nchan_to_allocate = 2 * BINAURAL_CHANNELS;
292 48 : move16();
293 : }
294 :
295 278 : test();
296 278 : test();
297 :
298 278 : IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_BINAURAL ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
299 : {
300 : /* get correct granularity in case of binaural rendering of the discrete objects with the td obj renderer */
301 : Word32 quo, rem;
302 5 : iDiv_and_mod_32( st_ivas->hDecoderConfig->output_Fs, FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES, &quo, &rem, 0 );
303 5 : granularity = extract_l( quo ); /*Q0*/
304 : }
305 :
306 278 : IF( NE_32( ( error = ivas_jbm_dec_tc_buffer_open_fx( st_ivas, buffer_mode, nchan_tc, nchan_to_allocate, nchan_to_allocate, granularity ) ), IVAS_ERR_OK ) )
307 : {
308 0 : return error;
309 : }
310 : }
311 :
312 1557 : st_ivas->hSpar = hSpar;
313 :
314 1557 : return error;
315 : }
316 :
317 :
318 : /*-------------------------------------------------------------------------
319 : * ivas_spar_dec_close()
320 : *
321 : * Deallocate SPAR handle
322 : *------------------------------------------------------------------------*/
323 :
324 1892 : void ivas_spar_dec_close_fx(
325 : SPAR_DEC_HANDLE *hSpar, /* i/o: SPAR decoder handle */
326 : const Word32 output_Fs, /* i : output sampling rate Q0*/
327 : const Word16 spar_reconfig_flag /* i : SPAR reconfiguration flag Q0*/
328 : )
329 : {
330 1892 : test();
331 1892 : IF( hSpar == NULL || *hSpar == NULL )
332 : {
333 335 : return;
334 : }
335 :
336 : /* MD handle */
337 1557 : ivas_spar_md_dec_close( &( *hSpar )->hMdDec );
338 :
339 : /* TD decorrelator handle */
340 1557 : ivas_td_decorr_dec_close( &( *hSpar )->hTdDecorr );
341 :
342 : /* FB mixer handle */
343 1557 : ivas_FB_mixer_close_fx( &( *hSpar )->hFbMixer, output_Fs, spar_reconfig_flag );
344 :
345 : /* AGC */
346 1557 : ivas_spar_agc_dec_close_fx( &( *hSpar )->hAgcDec );
347 :
348 : /* PCA */
349 1557 : IF( ( *hSpar )->hPCA != NULL )
350 : {
351 25 : free( ( *hSpar )->hPCA );
352 25 : ( *hSpar )->hPCA = NULL;
353 : }
354 :
355 1557 : IF( !spar_reconfig_flag )
356 : {
357 278 : free( ( *hSpar ) );
358 278 : ( *hSpar ) = NULL;
359 : }
360 :
361 1557 : return;
362 : }
363 :
364 :
365 : /*-------------------------------------------------------------------*
366 : * ivas_spar_dec()
367 : *
368 : * Principal IVAS SPAR decoder routine
369 : *-------------------------------------------------------------------*/
370 :
371 154022 : ivas_error ivas_spar_dec_fx(
372 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder struct */
373 : Word16 *nb_bits_read /* o : number of MD bits read Q0*/
374 : )
375 : {
376 : DECODER_CONFIG_HANDLE hDecoderConfig;
377 : Word16 i, nb_bits_read_orig;
378 : Decoder_State *st0;
379 : Word16 next_bit_pos_orig, last_bit_pos;
380 : UWord16 bstr_meta[MAX_BITS_METADATA], *bit_stream_orig;
381 : ivas_error error;
382 : Word32 quo, rem;
383 :
384 154022 : push_wmops( "ivas_spar_decode" );
385 154022 : error = IVAS_ERR_OK;
386 154022 : move32();
387 154022 : hDecoderConfig = st_ivas->hDecoderConfig;
388 :
389 154022 : st0 = NULL;
390 154022 : IF( st_ivas->nSCE > 0 )
391 : {
392 41908 : st0 = st_ivas->hSCE[0]->hCoreCoder[0];
393 : }
394 : ELSE
395 : {
396 112114 : st0 = st_ivas->hCPE[0]->hCoreCoder[0];
397 : }
398 :
399 154022 : bit_stream_orig = st0->bit_stream; /*Q0*/
400 154022 : next_bit_pos_orig = st0->next_bit_pos; /*Q0*/
401 154022 : move16();
402 :
403 154022 : IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
404 : {
405 34004 : iDiv_and_mod_32( hDecoderConfig->ivas_total_brate, FRAMES_PER_SEC, &quo, &rem, 0 );
406 34004 : last_bit_pos = sub( extract_l( L_sub( quo, 1 ) ), nb_bits_read[1] ); /*Q0*/
407 : }
408 : ELSE
409 : {
410 120018 : *nb_bits_read = 0;
411 120018 : move16();
412 120018 : last_bit_pos = 0;
413 120018 : move16();
414 : }
415 :
416 : /* read DirAC bitstream */
417 154022 : IF( st_ivas->hQMetaData != NULL )
418 : {
419 : #ifdef NONBE_FIX_1052_SBA_EXT
420 154022 : ivas_dirac_dec_read_BS_fx( hDecoderConfig->ivas_total_brate, st0, st_ivas->hDirAC, st_ivas->hSpatParamRendCom, st_ivas->hQMetaData, nb_bits_read, last_bit_pos, ivas_get_hodirac_flag_fx( hDecoderConfig->ivas_total_brate, st_ivas->sba_analysis_order ), st_ivas->nchan_transport, st_ivas->hSpar->dirac_to_spar_md_bands );
421 : #else
422 : ivas_dirac_dec_read_BS_fx( hDecoderConfig->ivas_total_brate, st0, st_ivas->hDirAC, st_ivas->hSpatParamRendCom, st_ivas->hQMetaData, nb_bits_read, last_bit_pos, ivas_get_hodirac_flag_fx( hDecoderConfig->ivas_total_brate, st_ivas->sba_analysis_order ), st_ivas->hSpar->dirac_to_spar_md_bands );
423 : #endif
424 : }
425 :
426 154022 : IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
427 : {
428 34004 : iDiv_and_mod_32( hDecoderConfig->ivas_total_brate, FRAMES_PER_SEC, &quo, &rem, 0 );
429 34004 : last_bit_pos = sub( extract_l( L_sub( quo, 1 ) ), nb_bits_read[1] ); /*Q0*/
430 : }
431 : ELSE
432 : {
433 120018 : iDiv_and_mod_32( hDecoderConfig->ivas_total_brate, FRAMES_PER_SEC, &quo, &rem, 0 );
434 120018 : last_bit_pos = extract_l( L_sub( quo, 1 ) ); /*Q0*/
435 : }
436 :
437 154022 : test();
438 154022 : if ( !st0->bfi && EQ_32( hDecoderConfig->ivas_total_brate, IVAS_SID_5k2 ) )
439 : {
440 : #ifdef NONBE_FIX_1052_SBA_EXT
441 324 : last_bit_pos = sub( last_bit_pos, ( SID_FORMAT_NBITS + SBA_PLANAR_BITS + SBA_ORDER_BITS ) ); /*Q0*/
442 : #else
443 : last_bit_pos = sub( last_bit_pos, SID_FORMAT_NBITS ); /*Q0*/
444 : #endif
445 : }
446 154022 : nb_bits_read_orig = *nb_bits_read; /*Q0*/
447 154022 : move16();
448 154022 : last_bit_pos = sub( last_bit_pos, nb_bits_read_orig ); /*Q0*/
449 :
450 : /* reverse the bitstream for easier reading of indices */
451 235509636 : FOR( i = 0; i < s_min( MAX_BITS_METADATA, last_bit_pos ); i++ )
452 : {
453 235355614 : bstr_meta[i] = st_ivas->bit_stream[( last_bit_pos - i )]; /*Q0*/
454 235355614 : move16();
455 : }
456 154022 : st0->bit_stream = bstr_meta; /*Q0*/
457 154022 : st0->next_bit_pos = 0;
458 154022 : move16();
459 154022 : st0->bits_frame = s_min( MAX_BITS_METADATA, add( last_bit_pos, 1 ) ); /*Q0*/
460 154022 : move16();
461 :
462 154022 : if ( !st0->bfi )
463 : {
464 147897 : st0->total_brate = hDecoderConfig->ivas_total_brate; /* to avoid BER detect */ /*Q0*/
465 147897 : move32();
466 : }
467 :
468 : /*---------------------------------------------------------------------*
469 : * Decode SPAR metadata
470 : *---------------------------------------------------------------------*/
471 :
472 154022 : IF( NE_32( ( error = ivas_spar_dec_MD_fx( st_ivas, st0 ) ), IVAS_ERR_OK ) )
473 : {
474 0 : return error;
475 : }
476 :
477 154022 : *nb_bits_read = add( st0->next_bit_pos, nb_bits_read_orig ); /*Q0*/
478 154022 : move16();
479 154022 : st0->bit_stream = bit_stream_orig; /*Q0*/
480 154022 : st0->next_bit_pos = next_bit_pos_orig; /*Q0*/
481 154022 : move16();
482 :
483 154022 : test();
484 154022 : IF( !st0->bfi && EQ_32( hDecoderConfig->ivas_total_brate, IVAS_SID_5k2 ) )
485 : {
486 : Word16 zero_pad_bits;
487 : #ifdef NONBE_FIX_1052_SBA_EXT
488 324 : *nb_bits_read = add( *nb_bits_read, SID_FORMAT_NBITS + SBA_PLANAR_BITS + SBA_ORDER_BITS ); /*Q0*/
489 : #else
490 : *nb_bits_read = add( *nb_bits_read, SID_FORMAT_NBITS ); /*Q0*/
491 : #endif
492 324 : move16();
493 324 : zero_pad_bits = sub( ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC, *nb_bits_read ); /*Q0*/
494 324 : assert( zero_pad_bits <= 1 );
495 324 : *nb_bits_read = add( *nb_bits_read, zero_pad_bits ); /*Q0*/
496 324 : move16();
497 : }
498 :
499 154022 : pop_wmops();
500 :
501 154022 : return error;
502 : }
503 :
504 :
505 : /*---------------------------------------------------------------------*
506 : * Function ivas_get_spar_table_idx_from_coded_idx()
507 : *
508 : * Get SPAR table index
509 : *---------------------------------------------------------------------*/
510 :
511 145509 : static Word16 ivas_get_spar_table_idx_from_coded_idx(
512 : const Word32 ivas_total_brate, /* i : IVAS total bitrate Q0*/
513 : const Word16 sba_order, /* i : Ambisonic (SBA) order Q0*/
514 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
515 : Word16 *bitlen /* o : number of bits Q0*/
516 : )
517 : {
518 : Word16 table_idx, ind1[IVAS_SPAR_BR_TABLE_LEN];
519 : Word16 i, j, ind2;
520 :
521 145509 : j = 0;
522 145509 : move16();
523 3055689 : FOR( i = 0; i < IVAS_SPAR_BR_TABLE_LEN; i++ )
524 : {
525 2910180 : ind1[j] = 0;
526 2910180 : move16();
527 2910180 : test();
528 2910180 : IF( EQ_32( ivas_spar_br_table_consts[i].ivas_total_brate, ivas_total_brate ) && EQ_16( ivas_spar_br_table_consts[i].sba_order, sba_order ) )
529 : {
530 145509 : ind1[j] = i;
531 145509 : move16();
532 145509 : j = add( j, 1 );
533 : }
534 : }
535 :
536 145509 : assert( j > 0 );
537 145509 : *bitlen = ivas_get_bits_to_encode( sub( j, 1 ) ); /*Q0*/
538 145509 : move16();
539 :
540 145509 : ind2 = get_next_indice_fx( st0, *bitlen ); /*Q0*/
541 :
542 145509 : table_idx = ind1[ind2]; /*Q0*/
543 145509 : move16();
544 :
545 145509 : return table_idx;
546 : }
547 :
548 :
549 : /*---------------------------------------------------------------------*
550 : * Function ivas_parse_spar_header()
551 : *
552 : * Get SPAR table index
553 : *---------------------------------------------------------------------*/
554 :
555 145509 : static Word16 ivas_parse_spar_header(
556 : const Word32 ivas_total_brate, /* i : IVAS total bitrate Q0*/
557 : const Word16 sba_order, /* i : Ambisonic (SBA) order Q0*/
558 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
559 : Word16 *table_idx /*Q0*/ )
560 : {
561 : Word16 bitlen, bwidth;
562 :
563 145509 : *table_idx = ivas_get_spar_table_idx_from_coded_idx( ivas_total_brate, sba_order, st0, &bitlen ); /*Q0*/
564 145509 : move16();
565 :
566 145509 : bwidth = ivas_spar_br_table_consts[( *table_idx )].bwidth; /*Q0*/
567 145509 : move16();
568 :
569 145509 : return bwidth;
570 : }
571 :
572 :
573 578400 : static Word16 get_random_number_fx(
574 : Word16 *seed )
575 : {
576 578400 : return Random( seed ); /*Q15*/
577 : }
578 :
579 :
580 11076 : static Word32 matrix_det_fx(
581 : const Word32 a00, /*Q27*/
582 : const Word32 a01, /*Q27*/
583 : const Word32 a10, /*Q27*/
584 : const Word32 a11 /*Q27*/
585 : )
586 : {
587 11076 : return L_sub( Mpy_32_32( a00, a11 ), Mpy_32_32( a01, a10 ) ); /*Q23*/
588 : }
589 :
590 :
591 923 : static void matrix_inverse_fx(
592 : Word32 in[3][3], /*Q27*/
593 : Word32 out[3][3], /*out_q*/
594 : const Word16 size,
595 : Word16 *out_q )
596 : {
597 : Word32 det, fac, tmp_32;
598 923 : Word32 eps_fx = 1;
599 923 : move32();
600 923 : Word16 q_fac, shift = 0, tmp_e = 0;
601 923 : move16();
602 923 : move16();
603 :
604 923 : IF( EQ_16( size, 1 ) )
605 : {
606 0 : tmp_32 = BASOP_Util_Divide3232_Scale( ONE_IN_Q27, L_max( in[0][0], eps_fx ), &tmp_e ); /*Q: 15 - tmp_e*/
607 0 : shift = norm_l( tmp_32 );
608 0 : out[0][0] = L_shl( tmp_32, shift ); /*out_q*/
609 0 : move32();
610 0 : *out_q = add( shift, sub( Q15, tmp_e ) );
611 0 : move16();
612 :
613 0 : return;
614 : }
615 923 : ELSE IF( EQ_16( size, 2 ) )
616 : {
617 0 : det = matrix_det_fx( in[0][1], in[0][1], in[1][0], in[1][1] ); /*Q23*/
618 0 : tmp_32 = BASOP_Util_Divide3232_Scale( ONE_IN_Q23, L_max( det, eps_fx ), &tmp_e ); /*Q: 15 - tmp_e*/
619 0 : shift = norm_l( tmp_32 );
620 0 : fac = L_shl( tmp_32, shift ); /*q_fac*/
621 0 : q_fac = add( shift, sub( Q15, tmp_e ) );
622 :
623 0 : out[0][0] = Mpy_32_32( in[1][1], fac ); /*q_fac - 4*/
624 0 : move32();
625 0 : out[1][0] = Mpy_32_32( in[1][0], L_negate( fac ) ); /*q_fac - 4*/
626 0 : move32();
627 :
628 0 : out[0][1] = Mpy_32_32( in[0][1], L_negate( fac ) ); /*q_fac - 4*/
629 0 : move32();
630 0 : out[1][1] = Mpy_32_32( in[0][0], fac ); /*q_fac - 4*/
631 0 : move32();
632 :
633 0 : *out_q = add( Q27, sub( q_fac, 31 ) );
634 0 : move16();
635 :
636 0 : return;
637 : }
638 :
639 923 : det = L_add( L_sub( Mpy_32_32( in[0][0], matrix_det_fx( in[1][1], in[1][2], in[2][1], in[2][2] ) ), Mpy_32_32( in[1][0], matrix_det_fx( in[0][1], in[0][2], in[2][1], in[2][2] ) ) ), Mpy_32_32( in[2][0], matrix_det_fx( in[0][1], in[0][2], in[1][1], in[1][2] ) ) ); /*Q19*/
640 923 : tmp_32 = BASOP_Util_Divide3232_Scale( ONE_IN_Q19, L_max( det, eps_fx ), &tmp_e ); /*Q: 31 - tmp_e*/
641 923 : shift = norm_l( tmp_32 );
642 923 : fac = L_shl( tmp_32, shift ); /*q_fac*/
643 923 : q_fac = add( shift, sub( Q15, tmp_e ) );
644 :
645 923 : out[0][0] = Mpy_32_32( matrix_det_fx( in[1][1], in[1][2], in[2][1], in[2][2] ), fac ); /*q_fac-8*/
646 923 : move32();
647 923 : out[1][0] = Mpy_32_32( matrix_det_fx( in[1][0], in[1][2], in[2][0], in[2][2] ), L_negate( fac ) ); /*q_fac-8*/
648 923 : move32();
649 923 : out[2][0] = Mpy_32_32( matrix_det_fx( in[1][0], in[1][1], in[2][0], in[2][1] ), fac ); /*q_fac-8*/
650 923 : move32();
651 :
652 923 : out[0][1] = Mpy_32_32( matrix_det_fx( in[0][1], in[0][2], in[2][1], in[2][2] ), L_negate( fac ) ); /*q_fac-8*/
653 923 : move32();
654 923 : out[1][1] = Mpy_32_32( matrix_det_fx( in[0][0], in[0][2], in[2][0], in[2][2] ), fac ); /*q_fac-8*/
655 923 : move32();
656 923 : out[2][1] = Mpy_32_32( matrix_det_fx( in[0][0], in[0][1], in[2][0], in[2][1] ), L_negate( fac ) ); /*q_fac-8*/
657 923 : move32();
658 :
659 923 : out[0][2] = Mpy_32_32( matrix_det_fx( in[0][1], in[0][2], in[1][1], in[1][2] ), fac ); /*q_fac-8*/
660 923 : move32();
661 923 : out[1][2] = Mpy_32_32( matrix_det_fx( in[0][0], in[0][2], in[1][0], in[1][2] ), L_negate( fac ) ); /*q_fac-8*/
662 923 : move32();
663 923 : out[2][2] = Mpy_32_32( matrix_det_fx( in[0][0], in[0][1], in[1][0], in[1][1] ), fac ); /*q_fac-8*/
664 923 : move32();
665 :
666 923 : *out_q = add( Q23, sub( q_fac, 31 ) );
667 923 : move16();
668 :
669 923 : return;
670 : }
671 :
672 :
673 : /*---------------------------------------------------------------------*
674 : * Function ivas_spar_get_cldfb_gains()
675 : *
676 : *
677 : *---------------------------------------------------------------------*/
678 :
679 923 : void ivas_spar_get_cldfb_gains_fx(
680 : SPAR_DEC_HANDLE hSpar,
681 : HANDLE_CLDFB_FILTER_BANK cldfbAnaDec0,
682 : HANDLE_CLDFB_FILTER_BANK cldfbSynDec0,
683 : const DECODER_CONFIG_HANDLE hDecoderConfig )
684 : {
685 923 : Word32 output_Fs_fx = hDecoderConfig->output_Fs;
686 : Word16 *weights_fx;
687 : Word16 cf_start_s_fx, cf_len_s_fx;
688 : Word32 T_fx[3 * CLDFB_NO_CHANNELS_MAX + 10 * CLDFB_NO_CHANNELS_MAX - CLDFB_NO_CHANNELS_MAX][3];
689 : Word32 Tt_T_fx[3][3];
690 : Word32 Tt_T_inv_fx[3][3];
691 : Word32 Tt_tgt_fx[3];
692 : Word32 ts_inout_fx[CLDFB_NO_CHANNELS_MAX];
693 : Word32 ts_re_fx[CLDFB_NO_CHANNELS_MAX];
694 : Word32 ts_im_fx[CLDFB_NO_CHANNELS_MAX];
695 : Word32 *pp_ts_im_fx[1], *pp_ts_re_fx[1];
696 : Word32 tgt_fx[( 3 - 1 ) * CLDFB_NO_CHANNELS_MAX + 10 * CLDFB_NO_CHANNELS_MAX];
697 : Word16 pt_len, stride, num_cldfb_bands, decfb_delay;
698 : Word16 encfb_delay, cf_start, cf_end, cf_len;
699 : Word16 ts, cf_cldfb_start, cf_cldfb_end;
700 : Word16 sample, num_cf_slots, num_samples;
701 : Word16 seed, split_band, slot_row, slot_col, slot, tmp_idx;
702 : Word16 Q_cf_start_s;
703 : Word16 Q_cf_len_s;
704 : Word16 Q_weights;
705 :
706 923 : pt_len = cldfbAnaDec0->p_filter_length; /*Q0*/
707 923 : move16();
708 923 : num_cldfb_bands = cldfbAnaDec0->no_channels; /*Q0*/
709 923 : move16();
710 :
711 923 : stride = NS2SA_FX2( output_Fs_fx, DELAY_CLDFB_NS ); /*Q0*/
712 923 : encfb_delay = NS2SA_FX2( output_Fs_fx, IVAS_FB_ENC_DELAY_NS ); /*Q0*/
713 923 : decfb_delay = NS2SA_FX2( output_Fs_fx, IVAS_FB_DEC_DELAY_NS ); /*Q0*/
714 :
715 923 : cf_start = add( sub( hSpar->hFbMixer->cross_fade_start_offset, encfb_delay ), decfb_delay ); /* time domain after CLDFB synthesis*/ /*Q0*/
716 923 : cf_end = add( sub( hSpar->hFbMixer->cross_fade_end_offset, encfb_delay ), decfb_delay ); /*Q0*/
717 923 : cf_len = sub( cf_end, cf_start );
718 923 : weights_fx = hSpar->hFbMixer->cldfb_cross_fade_fx; /*hSpar->hFbMixer->cldfb_cross_fade_q*/
719 923 : cf_cldfb_start = shr( extract_l( ceil_fixed( sub( divide1616( sub( cf_start, shr( decfb_delay, 1 ) ), shl( stride, 9 ) ), 32 ), 6 ) ), 6 ); /*Q0*/
720 923 : cf_cldfb_end = shr( divide1616( add( sub( cf_start, shr( decfb_delay, 1 ) ), cf_len ), shl( stride, 9 ) ), 6 ); /*q-factor of stride is 9(as max value is 60)*/ /*Q0*/
721 923 : num_cf_slots = add( sub( cf_cldfb_end, cf_cldfb_start ), 1 ); /*Q0*/
722 923 : num_samples = add( imult1616( num_cf_slots, stride ), sub( pt_len, stride ) ); /*Q0*/
723 923 : seed = RANDOM_INITSEED;
724 923 : move16();
725 923 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
726 923 : move16();
727 923 : pp_ts_im_fx[0] = ts_im_fx; /*Q22*/
728 923 : pp_ts_re_fx[0] = ts_re_fx; /*Q22*/
729 923 : set32_fx( tgt_fx, 0, ( 3 - 1 ) * CLDFB_NO_CHANNELS_MAX + 10 * CLDFB_NO_CHANNELS_MAX );
730 923 : cf_start_s_fx = divide3232( ( sub( cf_start, shr( decfb_delay, 1 ) ) ), output_Fs_fx ); /*Q_cf_start_s*/
731 923 : cf_len_s_fx = divide3232( sub( hSpar->hFbMixer->cross_fade_end_offset, hSpar->hFbMixer->cross_fade_start_offset ), output_Fs_fx ); /*Q_cf_len_s*/
732 923 : Q_cf_start_s = sub( norm_s( cf_start_s_fx ), 1 );
733 923 : Q_cf_len_s = norm_s( cf_len_s_fx );
734 923 : Q_weights = add( 15, sub( Q_cf_start_s, Q_cf_len_s ) );
735 15691 : FOR( ts = 0; ts < CLDFB_NO_COL_MAX; ts++ )
736 : {
737 14768 : weights_fx[ts] = divide1616( shl( sub( divide3232( L_mult0( add( shl( ts, 1 ), 1 ), shr( stride, 1 ) ), output_Fs_fx ), cf_start_s_fx ), Q_cf_start_s ), shl( cf_len_s_fx, Q_cf_len_s ) ); /*Q_weights*/
738 14768 : move16();
739 14768 : weights_fx[ts] = s_max( s_min( weights_fx[ts], shl( 1, Q_weights ) ), 0 ); /*Q_weights*/
740 14768 : move16();
741 : }
742 923 : hSpar->hFbMixer->cldfb_cross_fade_start = cf_cldfb_start; /*Q0*/
743 923 : move16();
744 923 : hSpar->hFbMixer->cldfb_cross_fade_end = cf_cldfb_end; /*Q0*/
745 923 : move16();
746 :
747 923 : test();
748 923 : test();
749 923 : test();
750 923 : if ( GT_16( num_cf_slots, 3 ) || GT_16( pt_len, 10 * CLDFB_NO_CHANNELS_MAX ) || GT_16( stride, CLDFB_NO_CHANNELS_MAX ) || EQ_16( split_band, IVAS_MAX_NUM_BANDS ) )
751 : {
752 0 : return;
753 : }
754 :
755 : /* optimization*/
756 : /* compute time-domain cross-fade for considered time slots*/
757 923 : tmp_idx = sub( cf_start, imult1616( cf_cldfb_start, stride ) ); /*Q0*/
758 155163 : FOR( sample = 0; sample < cf_len; sample++ )
759 : {
760 154240 : tgt_fx[tmp_idx] = L_deposit_h( hSpar->hFbMixer->pFilterbank_cross_fade_fx[sample] ); /*Q31*/
761 154240 : move32();
762 154240 : tmp_idx = add( tmp_idx, 1 );
763 : /* increasing window function */
764 : }
765 347963 : FOR( ; tmp_idx < num_samples; tmp_idx++ )
766 : {
767 : /* fill up with ones*/
768 347040 : tgt_fx[tmp_idx] = MAX_32; /*Q31*/
769 347040 : move32();
770 : }
771 579323 : FOR( sample = 0; sample < num_samples; sample++ )
772 : {
773 : /* initialize trasnform matrix with zeros*/
774 578400 : T_fx[sample][0] = T_fx[sample][1] = T_fx[sample][2] = 0;
775 578400 : move32();
776 578400 : move32();
777 578400 : move32();
778 : }
779 :
780 434723 : FOR( sample = 0; sample < sub( pt_len, stride ); sample++ )
781 : {
782 : /* fill internal CLDFB analysis time buffer with data*/
783 433800 : Word16 x_fx = get_random_number_fx( &seed ); /*Q15*/
784 433800 : cldfbAnaDec0->cldfb_state_fx[sample] = L_shl( x_fx, 12 ); /*Q27*/
785 433800 : move32();
786 : }
787 923 : Word16 q_cldfb = 27;
788 923 : move16();
789 923 : cldfbAnaDec0->Q_cldfb_state = q_cldfb;
790 923 : move16();
791 3692 : FOR( slot = 0; slot < num_cf_slots; slot++ )
792 : {
793 147369 : FOR( sample = 0; sample < stride; sample++ )
794 : {
795 144600 : Word16 x_fx = get_random_number_fx( &seed ); /*Q15*/
796 144600 : ts_inout_fx[sample] = L_shl( x_fx, 12 ); /*Q27*/
797 144600 : move32();
798 : }
799 :
800 2769 : cldfbAnalysis_ts_fx_fixed_q( ts_inout_fx, ts_re_fx, ts_im_fx, num_cldfb_bands, cldfbAnaDec0, &q_cldfb );
801 2769 : cldfb_reset_memory_fx( cldfbSynDec0 );
802 2769 : cldfbSynthesis_ivas_fx( pp_ts_re_fx, pp_ts_im_fx, ts_inout_fx, num_cldfb_bands, 0, 0, cldfbSynDec0 );
803 147369 : FOR( sample = 0; sample < stride; sample++ )
804 : {
805 144600 : T_fx[( ( slot * stride ) + sample )][slot] = ts_inout_fx[sample]; /*Q21*/
806 144600 : move32();
807 : }
808 2769 : tmp_idx = sub( pt_len, 1 ); /*Q0*/
809 1304169 : FOR( sample = stride; sample < pt_len; sample++ )
810 : {
811 1301400 : T_fx[( ( slot * stride ) + sample )][slot] = cldfbSynDec0->cldfb_state_fx[tmp_idx]; /*Q21*/
812 1301400 : move32();
813 1301400 : tmp_idx = sub( tmp_idx, 1 ); /*Q0*/
814 : }
815 : }
816 :
817 : /* target is synthesis output times the cross-fade window*/
818 579323 : FOR( sample = 0; sample < num_samples; sample++ )
819 : {
820 578400 : tgt_fx[sample] = L_shl( Mpy_32_32( tgt_fx[sample], L_add( T_fx[sample][0], L_add( T_fx[sample][1], T_fx[sample][2] ) ) ), 10 ); /*Q31*/
821 578400 : move32();
822 : }
823 : /* compute matrices */
824 3692 : FOR( slot_row = 0; slot_row < num_cf_slots; slot_row++ )
825 : {
826 8307 : FOR( slot_col = slot_row; slot_col < num_cf_slots; slot_col++ )
827 : {
828 5538 : Tt_T_fx[slot_row][slot_col] = 0;
829 5538 : move32();
830 3475938 : FOR( sample = 0; sample < num_samples; sample++ )
831 : {
832 3470400 : Tt_T_fx[slot_row][slot_col] = L_add( Tt_T_fx[slot_row][slot_col], Mpy_32_32( L_shl( T_fx[sample][slot_row], 8 ), L_shl( T_fx[sample][slot_col], 8 ) ) ); /*Q58-Q31*/
833 3470400 : move32();
834 : }
835 : }
836 : }
837 :
838 923 : Tt_T_fx[1][0] = Tt_T_fx[0][1]; /*Q27*/
839 923 : move32();
840 923 : Tt_T_fx[2][0] = Tt_T_fx[0][2]; /*Q27*/
841 923 : move32();
842 923 : Tt_T_fx[2][1] = Tt_T_fx[1][2]; /*Q27*/
843 923 : move32();
844 3692 : FOR( slot_row = 0; slot_row < num_cf_slots; slot_row++ )
845 : {
846 2769 : Tt_tgt_fx[slot_row] = 0;
847 2769 : move32();
848 1737969 : FOR( sample = 0; sample < num_samples; sample++ )
849 : {
850 1735200 : Tt_tgt_fx[slot_row] = L_add( Tt_tgt_fx[slot_row], Mpy_32_32( T_fx[sample][slot_row], tgt_fx[sample] ) ); /*Q21*/
851 1735200 : move32();
852 : }
853 : }
854 923 : Word16 output_q = 27;
855 923 : move16();
856 923 : matrix_inverse_fx( Tt_T_fx, Tt_T_inv_fx, num_cf_slots, &output_q );
857 : /* compute the optimal coefficients */
858 3692 : FOR( slot_row = 0; slot_row < num_cf_slots; slot_row++ )
859 : {
860 2769 : Word32 tmp = 0;
861 2769 : move32();
862 11076 : FOR( slot_col = 0; slot_col < num_cf_slots; slot_col++ )
863 : {
864 8307 : tmp = L_add( tmp, Mpy_32_32( Tt_T_inv_fx[slot_row][slot_col], Tt_tgt_fx[slot_col] ) ); /*output_q-10*/
865 : }
866 2769 : weights_fx[( cf_cldfb_start + slot_row )] = extract_l( L_shr( L_max( L_min( tmp, L_shl( 1, sub( output_q, 10 ) ) ), 0 ), sub( sub( output_q, 10 ), Q_weights ) ) ); /*Q_weights*/
867 2769 : move16();
868 : }
869 923 : hSpar->hFbMixer->cldfb_cross_fade_q = Q_weights;
870 923 : move16();
871 :
872 923 : cldfb_reset_memory_fx( cldfbSynDec0 );
873 923 : cldfb_reset_memory_fx( cldfbAnaDec0 );
874 :
875 923 : return;
876 : }
877 16588084 : Word16 ivas_is_res_channel(
878 : const Word16 ch, /* i : ch index in WYZX ordering Q0*/
879 : const Word16 nchan_transport /* i : number of transport channels (1-4) Q0*/
880 : )
881 : {
882 16588084 : const Word16 rc_map[FOA_CHANNELS][FOA_CHANNELS] = {
883 : { 0, 0, 0, 0 },
884 : { 0, 1, 0, 0 },
885 : { 0, 1, 0, 1 },
886 : { 0, 1, 1, 1 }
887 : };
888 :
889 16588084 : if ( GE_16( ch, FOA_CHANNELS ) )
890 : {
891 : /* never transmitted */
892 1453376 : return 0;
893 : }
894 15134708 : assert( nchan_transport <= FOA_CHANNELS );
895 :
896 15134708 : return ( rc_map[nchan_transport - 1][ch] ); /*Q0*/
897 : }
898 :
899 :
900 : /*-------------------------------------------------------------------*
901 : * ivas_spar_dec_MD()
902 : *
903 : * IVAS SPAR MD decoder
904 : *-------------------------------------------------------------------*/
905 :
906 154022 : static ivas_error ivas_spar_dec_MD_fx(
907 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
908 : Decoder_State *st0 /* i/o: decoder state structure - for bitstream handling*/
909 : )
910 : {
911 : Word16 num_channels, table_idx, num_bands_out, bfi, sba_order;
912 : Word32 ivas_total_brate;
913 : Word16 num_md_sub_frames;
914 : ivas_error error;
915 154022 : DECODER_CONFIG_HANDLE hDecoderConfig = st_ivas->hDecoderConfig;
916 154022 : SPAR_DEC_HANDLE hSpar = st_ivas->hSpar;
917 :
918 154022 : push_wmops( "ivas_spar_dec_MD" );
919 :
920 : /*---------------------------------------------------------------------*
921 : * Initialization
922 : *---------------------------------------------------------------------*/
923 :
924 154022 : sba_order = s_min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER ); /*Q0*/
925 154022 : bfi = st_ivas->bfi; /*Q0*/
926 154022 : move16();
927 154022 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate; /*Q0*/
928 154022 : move32();
929 154022 : num_channels = ivas_sba_get_nchan_metadata_fx( sba_order, ivas_total_brate ); /*Q0*/
930 154022 : num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( sba_order, hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate ); /*Q0*/
931 :
932 154022 : num_bands_out = hSpar->hFbMixer->pFb->filterbank_num_bands; /*Q0*/
933 154022 : move16();
934 :
935 154022 : test();
936 154022 : IF( ( ivas_total_brate > FRAME_NO_DATA ) && !bfi )
937 : {
938 145833 : IF( GT_32( ivas_total_brate, IVAS_SID_5k2 ) )
939 : {
940 145509 : ivas_parse_spar_header( hDecoderConfig->ivas_total_brate, sba_order, st0, &table_idx );
941 :
942 145509 : IF( hSpar->hMdDec->spar_hoa_md_flag )
943 : {
944 20813 : hSpar->hMdDec->spar_md.num_bands = IVAS_MAX_NUM_BANDS;
945 20813 : move16();
946 : }
947 : ELSE
948 : {
949 124696 : hSpar->hMdDec->spar_md.num_bands = s_min( SPAR_DIRAC_SPLIT_START_BAND, IVAS_MAX_NUM_BANDS ); /*Q0*/
950 124696 : move16();
951 : }
952 :
953 145509 : IF( NE_16( hSpar->hMdDec->table_idx, table_idx ) )
954 : {
955 303 : hSpar->hMdDec->table_idx = table_idx; /*Q0*/
956 303 : move16();
957 303 : if ( hSpar->hTdDecorr )
958 : {
959 273 : hSpar->hTdDecorr->ducking_flag = ivas_spar_br_table_consts[table_idx].td_ducking; /*Q0*/
960 273 : move16();
961 : }
962 :
963 303 : IF( NE_32( ( error = ivas_spar_md_dec_init( hSpar->hMdDec, hDecoderConfig, num_channels, sba_order ) ), IVAS_ERR_OK ) )
964 : {
965 0 : return error;
966 : }
967 : }
968 : }
969 :
970 : /*---------------------------------------------------------------------*
971 : * Decode MD
972 : *---------------------------------------------------------------------*/
973 :
974 145833 : ivas_spar_md_dec_process_fx( st_ivas, st0, num_bands_out, sba_order );
975 :
976 : /*---------------------------------------------------------------------*
977 : * read PCA bits
978 : *---------------------------------------------------------------------*/
979 :
980 145833 : IF( hSpar->hPCA != NULL )
981 : {
982 10880 : ivas_pca_read_bits_fx( st0, hSpar->hPCA );
983 : }
984 :
985 : /*---------------------------------------------------------------------*
986 : * Read AGC bits
987 : *---------------------------------------------------------------------*/
988 :
989 145833 : test();
990 145833 : test();
991 145833 : IF( GT_32( ivas_total_brate, IVAS_SID_5k2 ) && !bfi && hSpar->hMdDec->dtx_vad )
992 : {
993 145276 : IF( EQ_16( hSpar->hMdDec->spar_md_cfg.nchan_transport, 1 ) )
994 : {
995 37666 : hSpar->AGC_flag = get_next_indice_fx( st0, 1 );
996 37666 : move16();
997 : }
998 :
999 145276 : ivas_agc_read_bits_fx( hSpar->hAgcDec, st0, hSpar->hMdDec->spar_md_cfg.nchan_transport, hSpar->AGC_flag );
1000 : }
1001 :
1002 : /*---------------------------------------------------------------------*
1003 : * MD smoothing
1004 : *---------------------------------------------------------------------*/
1005 :
1006 145833 : test();
1007 145833 : test();
1008 145833 : test();
1009 145833 : IF( EQ_16( st0->m_old_frame_type, ZERO_FRAME ) && EQ_32( ivas_total_brate, IVAS_SID_5k2 ) && ( st0->prev_bfi == 0 ) && EQ_16( hSpar->hMdDec->spar_md_cfg.nchan_transport, 1 ) )
1010 : {
1011 125 : ivas_spar_setup_md_smoothing_fx( hSpar->hMdDec, num_bands_out, num_md_sub_frames );
1012 : }
1013 : ELSE
1014 : {
1015 145708 : ivas_spar_update_md_hist_fx( hSpar->hMdDec );
1016 : }
1017 : }
1018 : ELSE
1019 : {
1020 8189 : IF( !bfi )
1021 : {
1022 2064 : ivas_spar_smooth_md_dtx_fx( hSpar->hMdDec, num_bands_out, num_md_sub_frames );
1023 : }
1024 :
1025 8189 : set16_fx( hSpar->hMdDec->valid_bands, 0, IVAS_MAX_NUM_BANDS );
1026 : }
1027 :
1028 154022 : pop_wmops();
1029 154022 : return IVAS_ERR_OK;
1030 : }
1031 :
1032 :
1033 : /*-------------------------------------------------------------------*
1034 : * ivas_spar_get_cldfb_slot_gain()
1035 : *
1036 : *
1037 : *-------------------------------------------------------------------*/
1038 :
1039 2252390 : static Word16 ivas_spar_get_cldfb_slot_gain_fx(
1040 : SPAR_DEC_HANDLE hSpar, /* i/o: SPAR decoder handle */
1041 : const DECODER_CONFIG_HANDLE hDecoderConfig, /* i : configuration structure */
1042 : const Word16 time_slot_idx, /*Q0*/
1043 : Word16 *time_slot_idx0, /*Q0*/
1044 : Word16 *time_slot_idx1, /*Q0*/
1045 : Word16 *weight_lowfreq_fx /*Q15*/
1046 : )
1047 : {
1048 : Word16 weight_fx;
1049 : Word32 encfb_delay_fx, decfb_delay_fx;
1050 : Word32 xfade_start_ns_fx;
1051 : Word16 xfade_delay_subframes;
1052 : Word16 i_hist;
1053 : Word16 split_band;
1054 :
1055 2252390 : *weight_lowfreq_fx = hSpar->hFbMixer->cldfb_cross_fade_fx[time_slot_idx]; /*Q15*/
1056 2252390 : move16();
1057 :
1058 2252390 : encfb_delay_fx = IVAS_FB_ENC_DELAY_NS;
1059 2252390 : move32();
1060 2252390 : decfb_delay_fx = IVAS_FB_DEC_DELAY_NS;
1061 2252390 : move32();
1062 2252390 : Word32 one_by_outfs = 0; // Q15
1063 2252390 : move32();
1064 2252390 : SWITCH( hDecoderConfig->output_Fs )
1065 : {
1066 208000 : case 16000:
1067 208000 : one_by_outfs = 2048000000; /* 1000000000.0f/(output_Fs) in Q15 */
1068 208000 : move32();
1069 208000 : BREAK;
1070 948159 : case 32000:
1071 948159 : one_by_outfs = 1024000000; /* 1000000000.0f/(output_Fs) in Q15 */
1072 948159 : move32();
1073 948159 : BREAK;
1074 1096231 : case 48000:
1075 1096231 : one_by_outfs = 682666688; /* 1000000000.0f/(output_Fs) in Q15 */
1076 1096231 : move32();
1077 1096231 : BREAK;
1078 0 : default:
1079 0 : assert( 0 );
1080 : }
1081 :
1082 2252390 : Word64 fade_start = W_mult0_32_32( one_by_outfs, hSpar->hFbMixer->cross_fade_start_offset ); /*Q15*/
1083 2252390 : move64();
1084 2252390 : fade_start = W_shr( fade_start, 15 ); /*Q0*/
1085 2252390 : xfade_start_ns_fx = L_add( L_sub( W_extract_l( fade_start ), encfb_delay_fx ), L_shr( decfb_delay_fx, 1 ) ); /*Q0*/
1086 2252390 : xfade_delay_subframes = extract_l( Mpy_32_32( xfade_start_ns_fx, 429 /* 1 / ( FRAME_SIZE_NS / MAX_PARAM_SPATIAL_SUBFRAMES ) in Q31 -> 429 */ ) ); /*Q0*/
1087 :
1088 2252390 : i_hist = sub( 4, xfade_delay_subframes ); /*Q0*/
1089 2252390 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
1090 2252390 : move16();
1091 :
1092 2252390 : IF( LT_16( split_band, IVAS_MAX_NUM_BANDS ) )
1093 : {
1094 2252390 : IF( GT_16( hSpar->i_subframe, 3 ) )
1095 : {
1096 2231350 : Word16 mod_res = time_slot_idx % MAX_PARAM_SPATIAL_SUBFRAMES;
1097 2231350 : move16();
1098 2231350 : SWITCH( mod_res )
1099 : {
1100 557851 : case 0:
1101 557851 : weight_fx = 0;
1102 557851 : move16();
1103 557851 : BREAK;
1104 557834 : case 1:
1105 557834 : weight_fx = 8191; /*0.25f in Q15*/
1106 557834 : move16();
1107 557834 : BREAK;
1108 557834 : case 2:
1109 557834 : weight_fx = 16383; /*0.5f in Q15*/
1110 557834 : move16();
1111 557834 : BREAK;
1112 557831 : case 3:
1113 557831 : weight_fx = 24575; /*0.75f in Q15*/
1114 557831 : move16();
1115 557831 : BREAK;
1116 0 : default:
1117 0 : weight_fx = 0;
1118 0 : move16();
1119 0 : BREAK;
1120 : }
1121 : }
1122 : ELSE
1123 : {
1124 21040 : weight_fx = 0;
1125 21040 : move16();
1126 : }
1127 2252390 : *time_slot_idx0 = i_hist; /*Q0*/
1128 2252390 : move16();
1129 2252390 : *time_slot_idx1 = add( i_hist, 1 ); /*Q0*/
1130 2252390 : move16();
1131 : }
1132 : ELSE
1133 : {
1134 : /* determine cross-fade gain for current frame Parameters*/
1135 0 : *time_slot_idx0 = hSpar->hFbMixer->cldfb_cross_fade_start; /*Q0*/
1136 0 : move16();
1137 0 : *time_slot_idx1 = hSpar->hFbMixer->cldfb_cross_fade_end; /*Q0*/
1138 0 : move16();
1139 0 : weight_fx = *weight_lowfreq_fx; /*Q15*/
1140 0 : move16();
1141 : }
1142 :
1143 2252390 : return weight_fx; /*Q15*/
1144 : }
1145 :
1146 :
1147 : /*-------------------------------------------------------------------*
1148 : * ivas_spar_get_parameters()
1149 : *
1150 : *
1151 : *-------------------------------------------------------------------*/
1152 :
1153 2252390 : void ivas_spar_get_parameters_fx(
1154 : SPAR_DEC_HANDLE hSpar, /* i/o: SPAR decoder handle */
1155 : const DECODER_CONFIG_HANDLE hDecoderConfig, /* i : configuration structure */
1156 : const Word16 ts, /*Q0*/
1157 : const Word16 num_ch_out, /*Q0*/
1158 : const Word16 num_ch_in, /*Q0*/
1159 : const Word16 num_spar_bands, /*Q0*/
1160 : Word32 par_mat_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS] /*hSpar->hMdDec->Q_mixer_mat*/
1161 : )
1162 : {
1163 : Word16 spar_band, out_ch, in_ch;
1164 : Word16 weight_fx, weight_20ms_fx;
1165 : Word16 ts0, ts1, split_band;
1166 :
1167 : // weight = ivas_spar_get_cldfb_slot_gain(hSpar, hDecoderConfig, ts, &ts0, &ts1, &weight_20ms);
1168 2252390 : weight_fx = ivas_spar_get_cldfb_slot_gain_fx( hSpar, hDecoderConfig, ts, &ts0, &ts1, &weight_20ms_fx ); /*Q15*/
1169 :
1170 2252390 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
1171 2252390 : move16();
1172 2252390 : Word16 add_weight_fx = sub( MAX_WORD16, weight_fx );
1173 2252390 : Word16 add_weight_20ms_fx = sub( MAX_WORD16, weight_20ms_fx );
1174 : Word16 out_flag[IVAS_MAX_FB_MIXER_OUT_CH];
1175 :
1176 2252390 : Word32 band_bool = LT_16( split_band, IVAS_MAX_NUM_BANDS );
1177 :
1178 12715326 : FOR( out_ch = 0; out_ch < num_ch_out; out_ch++ )
1179 : {
1180 : /* 20ms cross-fade for Transport channels in all frequency bands */
1181 : /* sub-frame processing for missing channels in all frequency bands*/
1182 10462936 : out_flag[out_ch] = band_bool && ( 0 == ivas_is_res_channel( out_ch, hSpar->hMdDec->spar_md_cfg.nchan_transport ) );
1183 10462936 : move16();
1184 : }
1185 2252390 : Word32 frame_bool = GT_16( hSpar->i_subframe, 3 );
1186 :
1187 12715326 : FOR( out_ch = 0; out_ch < num_ch_out; out_ch++ )
1188 : {
1189 10462936 : IF( out_flag[out_ch] )
1190 : {
1191 7206116 : IF( frame_bool )
1192 : {
1193 91558996 : FOR( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
1194 : {
1195 538130736 : FOR( in_ch = 0; in_ch < num_ch_in; in_ch++ )
1196 : {
1197 453707328 : par_mat_fx[out_ch][in_ch][spar_band] = Madd_32_16( Mpy_32_16_1( hSpar->hMdDec->mixer_mat_prev_fx[ts1][out_ch][in_ch][spar_band], weight_fx ),
1198 453707328 : hSpar->hMdDec->mixer_mat_prev_fx[ts0][out_ch][in_ch][spar_band], add_weight_fx );
1199 453707328 : move32();
1200 : }
1201 : }
1202 : }
1203 : ELSE
1204 : {
1205 :
1206 :
1207 906688 : FOR( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
1208 : {
1209 5233344 : FOR( in_ch = 0; in_ch < num_ch_in; in_ch++ )
1210 : {
1211 : {
1212 4397184 : par_mat_fx[out_ch][in_ch][spar_band] = hSpar->hMdDec->mixer_mat_fx[out_ch][in_ch][spar_band]; /*hSpar->hMdDec->Q_mixer_mat*/
1213 4397184 : move32();
1214 : }
1215 : }
1216 : }
1217 : }
1218 : }
1219 : ELSE
1220 : {
1221 41888484 : FOR( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
1222 : {
1223 245479856 : FOR( in_ch = 0; in_ch < num_ch_in; in_ch++ )
1224 : {
1225 : /* 20ms Transport channel reconstruction with matching encoder/decoder processing */
1226 206848192 : Word16 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 */
1227 206848192 : move16();
1228 206848192 : par_mat_fx[out_ch][in_ch][spar_band] = Madd_32_16( Mpy_32_16_1( hSpar->hMdDec->mixer_mat_prev_fx[prev_idx][out_ch][in_ch][spar_band], add_weight_20ms_fx ),
1229 206848192 : hSpar->hMdDec->mixer_mat_fx[out_ch][in_ch][spar_band], weight_20ms_fx ); /*hSpar->hMdDec->Q_mixer_mat*/
1230 206848192 : move32();
1231 : }
1232 : }
1233 : }
1234 : }
1235 2252390 : return;
1236 : }
1237 :
1238 :
1239 : /*-------------------------------------------------------------------*
1240 : * ivas_spar_get_skip_mat()
1241 : *
1242 : *
1243 : *-------------------------------------------------------------------*/
1244 :
1245 414193 : static void ivas_spar_get_skip_mat_fx(
1246 : SPAR_DEC_HANDLE hSpar, /* i/o: SPAR decoder handle */
1247 : const Word16 num_ch_out, /*Q0*/
1248 : const Word16 num_ch_in, /*Q0*/
1249 : const Word16 num_spar_bands, /*Q0*/
1250 : Word16 skip_mat[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH], /*Q0*/
1251 : const Word16 num_md_sub_frames /*Q0*/
1252 : )
1253 : {
1254 : Word16 spar_band, out_ch, in_ch;
1255 : Word16 i_ts, skip_flag;
1256 :
1257 2434529 : FOR( out_ch = 0; out_ch < num_ch_out; out_ch++ )
1258 : {
1259 13631912 : FOR( in_ch = 0; in_ch < num_ch_in; in_ch++ )
1260 : {
1261 11611576 : skip_mat[out_ch][in_ch] = 1;
1262 11611576 : move16();
1263 11611576 : skip_flag = 1;
1264 11611576 : move16();
1265 39495227 : FOR( i_ts = 0; i_ts < MAX_PARAM_SPATIAL_SUBFRAMES; i_ts++ )
1266 : {
1267 370610581 : FOR( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
1268 : {
1269 342726930 : IF( hSpar->hMdDec->mixer_mat_prev_fx[1 + i_ts][out_ch][in_ch][spar_band] != 0 )
1270 : {
1271 4689991 : skip_flag = 0;
1272 4689991 : move16();
1273 4689991 : BREAK;
1274 : }
1275 : }
1276 :
1277 32573642 : IF( skip_flag == 0 )
1278 : {
1279 4689991 : skip_mat[out_ch][in_ch] = 0;
1280 4689991 : move16();
1281 4689991 : BREAK;
1282 : }
1283 : }
1284 :
1285 11611576 : IF( EQ_16( skip_mat[out_ch][in_ch], 1 ) )
1286 : {
1287 24780599 : FOR( i_ts = 0; i_ts < num_md_sub_frames; i_ts++ )
1288 : {
1289 229475173 : FOR( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
1290 : {
1291 211616159 : IF( hSpar->hMdDec->mixer_mat_fx[out_ch][in_ch][( spar_band + ( i_ts * IVAS_MAX_NUM_BANDS ) )] != 0 )
1292 : {
1293 31486 : skip_flag = 0;
1294 31486 : move16();
1295 31486 : BREAK;
1296 : }
1297 : }
1298 :
1299 17890500 : IF( skip_flag == 0 )
1300 : {
1301 31486 : skip_mat[out_ch][in_ch] = 0;
1302 31486 : move16();
1303 31486 : BREAK;
1304 : }
1305 : }
1306 : }
1307 : }
1308 : }
1309 :
1310 414193 : return;
1311 : }
1312 :
1313 :
1314 8316 : static void ivas_spar_calc_smooth_facs_fx(
1315 : Word32 *cldfb_in_ts_re_fx[CLDFB_NO_COL_MAX], // i q_cldfb
1316 : Word32 *cldfb_in_ts_im_fx[CLDFB_NO_COL_MAX], // i q_cldfb
1317 : Word16 q_cldfb,
1318 : Word16 nbands_spar, /*Q0*/
1319 : const Word16 nSlots, /*Q0*/
1320 : const Word16 isFirstSubframe, /*Q0*/
1321 : ivas_fb_bin_to_band_data_t *bin2band,
1322 : Word16 *smooth_fac_fx, // o Q15
1323 : Word32 smooth_buf_fx[IVAS_MAX_NUM_BANDS][2 * SBA_DIRAC_NRG_SMOOTH_LONG + 1] ) // o Q0
1324 : {
1325 : Word16 b, bin, i, ts;
1326 : Word32 subframe_band_nrg_fx[IVAS_MAX_NUM_BANDS];
1327 : Word32 smooth_long_avg_fx[IVAS_MAX_NUM_BANDS];
1328 : Word32 smooth_short_avg_fx[IVAS_MAX_NUM_BANDS];
1329 : Word32 L_temp;
1330 : Word16 exp_tmp, q_tmp;
1331 8316 : bin = 0;
1332 8316 : move16();
1333 102764 : FOR( b = 0; b < nbands_spar; b++ )
1334 : {
1335 99792 : test();
1336 99792 : test();
1337 99792 : if ( GE_16( bin, CLDFB_NO_CHANNELS_MAX ) || ( ( b > 0 ) && LT_16( bin2band->p_cldfb_map_to_spar_band[bin], bin2band->p_cldfb_map_to_spar_band[bin - 1] ) ) )
1338 : {
1339 : BREAK;
1340 : }
1341 :
1342 : /* calculate band-wise subframe energies */
1343 94448 : subframe_band_nrg_fx[b] = 0;
1344 94448 : move32();
1345 486528 : WHILE( LT_16( bin, CLDFB_NO_CHANNELS_MAX ) && EQ_16( b, bin2band->p_cldfb_map_to_spar_band[bin] ) )
1346 : {
1347 392080 : test();
1348 1960400 : FOR( ts = 0; ts < nSlots; ts++ )
1349 : {
1350 1568320 : L_temp = L_add( L_shr( Mpy_32_32( cldfb_in_ts_re_fx[ts][bin], cldfb_in_ts_re_fx[ts][bin] ), 4 ), L_shr( Mpy_32_32( cldfb_in_ts_im_fx[ts][bin], cldfb_in_ts_im_fx[ts][bin] ), 4 ) ); // 2*q_cldfb - 35
1351 1568320 : subframe_band_nrg_fx[b] = L_add_sat( subframe_band_nrg_fx[b], L_temp ); // 2*q_cldfb - 35 (saturation reached in 1 orig pytest)
1352 1568320 : move32();
1353 : }
1354 392080 : bin = add( bin, 1 );
1355 : }
1356 94448 : exp_tmp = sub( 66, shl( q_cldfb, 1 ) );
1357 94448 : subframe_band_nrg_fx[b] = Sqrt32( subframe_band_nrg_fx[b], &exp_tmp );
1358 94448 : move32();
1359 94448 : q_tmp = sub( 31, exp_tmp );
1360 94448 : test();
1361 94448 : IF( isFirstSubframe && LT_16( nSlots, MAX_PARAM_SPATIAL_SUBFRAMES ) )
1362 : {
1363 : /* fill up to full 5ms subframe */
1364 0 : smooth_buf_fx[b][0] = L_add( smooth_buf_fx[b][0], L_shr( subframe_band_nrg_fx[b], q_tmp ) ); // Q0
1365 0 : move32();
1366 : }
1367 : ELSE
1368 : {
1369 94448 : smooth_buf_fx[b][0] = L_shr( subframe_band_nrg_fx[b], q_tmp ); // Q0
1370 94448 : move32();
1371 : }
1372 : /* calculate short and long energy averages */
1373 94448 : smooth_short_avg_fx[b] = 0;
1374 94448 : move32();
1375 661136 : FOR( i = 0; i < 2 * SBA_DIRAC_NRG_SMOOTH_SHORT; i++ )
1376 : {
1377 566688 : smooth_short_avg_fx[b] = L_add( smooth_short_avg_fx[b], smooth_buf_fx[b][i] ); // Q0
1378 566688 : move32();
1379 : }
1380 :
1381 94448 : smooth_long_avg_fx[b] = smooth_short_avg_fx[b]; // Q0
1382 94448 : move32();
1383 1416720 : FOR( i = 2 * SBA_DIRAC_NRG_SMOOTH_SHORT; i < 2 * SBA_DIRAC_NRG_SMOOTH_LONG; i++ )
1384 : {
1385 1322272 : smooth_long_avg_fx[b] = L_add( smooth_long_avg_fx[b], smooth_buf_fx[b][i] ); // Q0
1386 1322272 : move32();
1387 : }
1388 94448 : smooth_short_avg_fx[b] = Mpy_32_32( smooth_short_avg_fx[b], 357913941 /*(1/6 in Q31)*/ ); // Q0
1389 94448 : move32();
1390 94448 : smooth_long_avg_fx[b] = Mpy_32_32( smooth_long_avg_fx[b], 107374182 /*(1/20 in Q31)*/ ); // Q0
1391 94448 : move32();
1392 :
1393 : /* calculate smoothing factor based on energy averages */
1394 : /* reduce factor for higher short-term energy */
1395 94448 : IF( smooth_long_avg_fx[b] <= 0 )
1396 : {
1397 38363 : smooth_fac_fx[b] = 0;
1398 38363 : move16();
1399 : }
1400 56085 : ELSE IF( GE_32( smooth_long_avg_fx[b], smooth_short_avg_fx[b] ) )
1401 : {
1402 26248 : smooth_fac_fx[b] = MAX_16; // 1.0f in Q15
1403 26248 : move16();
1404 : }
1405 : ELSE
1406 : {
1407 29837 : smooth_fac_fx[b] = divide3232( smooth_long_avg_fx[b], smooth_short_avg_fx[b] ); // Q15
1408 29837 : move16();
1409 : }
1410 :
1411 : /* map factor to range [0;1] */
1412 94448 : smooth_fac_fx[b] = shl( mult_r( s_max( 0, sub( smooth_fac_fx[b], 9830 ) ), 23405 /*Q14*/ ), 1 ); // Q15
1413 94448 : move16();
1414 :
1415 : /* compress factor (higher compression in lowest bands) */
1416 94448 : IF( LT_16( b, 2 ) )
1417 : {
1418 16632 : exp_tmp = 0;
1419 16632 : move16();
1420 16632 : smooth_fac_fx[b] = Sqrt16( smooth_fac_fx[b], &exp_tmp );
1421 16632 : move16();
1422 16632 : smooth_fac_fx[b] = Sqrt16( smooth_fac_fx[b], &exp_tmp );
1423 16632 : move16();
1424 16632 : smooth_fac_fx[b] = shl( smooth_fac_fx[b], exp_tmp ); // Q15
1425 16632 : move16();
1426 : }
1427 : ELSE
1428 : {
1429 77816 : exp_tmp = 0;
1430 77816 : move16();
1431 77816 : smooth_fac_fx[b] = Sqrt16( smooth_fac_fx[b], &exp_tmp );
1432 77816 : move16();
1433 77816 : smooth_fac_fx[b] = shl( smooth_fac_fx[b], exp_tmp ); // Q15
1434 77816 : move16();
1435 : }
1436 :
1437 : /* apply upper bounds depending on band */
1438 94448 : smooth_fac_fx[b] = s_max( min_smooth_gains1_fx[b], s_min( max_smooth_gains2_fx[b], smooth_fac_fx[b] ) ); /*Q15*/
1439 94448 : move16();
1440 : }
1441 :
1442 : /* only update if we collected a full 5ms worth of energies for the buffer */
1443 8316 : test();
1444 8316 : IF( isFirstSubframe || EQ_16( nSlots, MAX_PARAM_SPATIAL_SUBFRAMES ) )
1445 : {
1446 108108 : FOR( b = 0; b < nbands_spar; b++ )
1447 : {
1448 2095632 : FOR( i = 2 * SBA_DIRAC_NRG_SMOOTH_LONG; i > 0; i-- )
1449 : {
1450 1995840 : smooth_buf_fx[b][i] = smooth_buf_fx[b][i - 1]; /*Q0*/
1451 1995840 : move32();
1452 : }
1453 : }
1454 : }
1455 8316 : return;
1456 : }
1457 :
1458 :
1459 : /*-------------------------------------------------------------------*
1460 : * ivas_spar_dec_agc_pca()
1461 : *
1462 : *
1463 : *-------------------------------------------------------------------*/
1464 :
1465 104192 : void ivas_spar_dec_agc_pca_fx(
1466 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1467 : Word32 *output[], /* i/o: input/output audio channels Q14*/
1468 : const Word16 output_frame /* i : output frame length Q0*/
1469 : )
1470 : {
1471 : Word16 nchan_transport;
1472 : Word16 num_in_ingest;
1473 : DECODER_CONFIG_HANDLE hDecoderConfig;
1474 : SPAR_DEC_HANDLE hSpar;
1475 :
1476 104192 : push_wmops( "ivas_spar_dec_agc_pca" );
1477 :
1478 104192 : hSpar = st_ivas->hSpar;
1479 104192 : hDecoderConfig = st_ivas->hDecoderConfig;
1480 104192 : nchan_transport = hSpar->hMdDec->spar_md_cfg.nchan_transport; /*Q0*/
1481 104192 : move16();
1482 :
1483 104192 : IF( GE_16( st_ivas->nchan_transport, 3 ) )
1484 : {
1485 : Word32 temp;
1486 : Word16 i;
1487 : /*convert WYZX downmix to WYXZ*/
1488 53935787 : FOR( i = 0; i < output_frame; i++ )
1489 : {
1490 53869760 : temp = output[2][i]; /*Q14*/
1491 53869760 : move32();
1492 53869760 : output[2][i] = output[3][i]; /*Q14*/
1493 53869760 : move32();
1494 53869760 : output[3][i] = temp; /*Q14*/
1495 53869760 : move32();
1496 : }
1497 : }
1498 :
1499 104192 : IF( hSpar->hMdDec->td_decorr_flag )
1500 : {
1501 104192 : num_in_ingest = ivas_sba_get_nchan_metadata_fx( st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate ); /*Q0*/
1502 : }
1503 : ELSE
1504 : {
1505 0 : num_in_ingest = nchan_transport; /*Q0*/
1506 0 : move16();
1507 : }
1508 :
1509 : /*---------------------------------------------------------------------*
1510 : * AGC
1511 : *---------------------------------------------------------------------*/
1512 :
1513 104192 : ivas_agc_dec_process_fx( hSpar->hAgcDec, ( output ), ( output ), nchan_transport, output_frame );
1514 :
1515 104192 : IF( hSpar->hPCA != NULL )
1516 : {
1517 11620 : ivas_pca_dec_fx( hSpar->hPCA, output_frame, num_in_ingest, hDecoderConfig->ivas_total_brate, hDecoderConfig->last_ivas_total_brate, st_ivas->bfi, output );
1518 : }
1519 104192 : pop_wmops();
1520 :
1521 104192 : return;
1522 : }
1523 :
1524 :
1525 : /*-------------------------------------------------------------------*
1526 : * ivas_spar_dec_set_render_map()
1527 : *
1528 : *
1529 : *-------------------------------------------------------------------*/
1530 :
1531 140772 : void ivas_spar_dec_set_render_map_fx(
1532 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
1533 : const Word16 nCldfbTs /* i : number of CLDFB time slots Q0*/
1534 : )
1535 : {
1536 : SPAR_DEC_HANDLE hSpar;
1537 :
1538 140772 : hSpar = st_ivas->hSpar;
1539 :
1540 : /* adapt subframes */
1541 140772 : hSpar->num_slots = nCldfbTs; /*Q0*/
1542 140772 : move16();
1543 140772 : hSpar->slots_rendered = 0;
1544 140772 : move16();
1545 140772 : hSpar->subframes_rendered = 0;
1546 140772 : move16();
1547 :
1548 140772 : set16_fx( hSpar->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
1549 140772 : ivas_jbm_dec_get_adapted_subframes( nCldfbTs, hSpar->subframe_nbslots, &hSpar->nb_subframes );
1550 :
1551 : /* copy also to tc buffer */
1552 : /* only for non-combined formats and combinded formats w/o discrete objects */
1553 140772 : test();
1554 140772 : IF( !( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) ) )
1555 : {
1556 123492 : st_ivas->hTcBuffer->nb_subframes = hSpar->nb_subframes; /*Q0*/
1557 123492 : move16();
1558 123492 : Copy( hSpar->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, hSpar->nb_subframes ); /*Q0*/
1559 : }
1560 :
1561 140772 : ivas_jbm_dec_get_md_map( DEFAULT_JBM_CLDFB_TIMESLOTS, nCldfbTs, 1, 0, DEFAULT_JBM_CLDFB_TIMESLOTS, hSpar->render_to_md_map );
1562 :
1563 140772 : return;
1564 : }
1565 :
1566 :
1567 : /*-------------------------------------------------------------------*
1568 : * ivas_spar_dec_set_render_params()
1569 : *
1570 : * IVAS SPAR set rendering parameters
1571 : *-------------------------------------------------------------------*/
1572 :
1573 140772 : void ivas_spar_dec_set_render_params_fx(
1574 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1575 : const Word16 n_cldfb_slots /* i : number of cldfb slots in this frame Q0*/
1576 : )
1577 : {
1578 : SPAR_DEC_HANDLE hSpar;
1579 : Word16 nchan_transport;
1580 : Word16 num_bands_out;
1581 :
1582 140772 : hSpar = st_ivas->hSpar;
1583 140772 : nchan_transport = hSpar->hMdDec->spar_md_cfg.nchan_transport; /*Q0*/
1584 140772 : move16();
1585 140772 : num_bands_out = hSpar->hFbMixer->pFb->filterbank_num_bands; /*Q0*/
1586 140772 : move16();
1587 140772 : ivas_spar_dec_gen_umx_mat_fx( 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 ) );
1588 :
1589 140772 : ivas_spar_dec_set_render_map_fx( st_ivas, n_cldfb_slots );
1590 :
1591 140772 : return;
1592 : }
1593 :
1594 :
1595 : /*-------------------------------------------------------------------*
1596 : * ivas_spar_dec_digest_tc()
1597 : *
1598 : *
1599 : *-------------------------------------------------------------------*/
1600 :
1601 140772 : void ivas_spar_dec_digest_tc_fx(
1602 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1603 : const Word16 nchan_transport, /* i : number of transport channels Q0*/
1604 : const Word16 nCldfbSlots, /* i : number of CLDFB slots Q0*/
1605 : const Word16 nSamplesForRendering /* i : number of samples provided Q0*/
1606 : )
1607 : {
1608 : SPAR_DEC_HANDLE hSpar;
1609 :
1610 140772 : hSpar = st_ivas->hSpar;
1611 140772 : test();
1612 140772 : test();
1613 140772 : IF( hSpar->hMdDec->td_decorr_flag && !( EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC ) || EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_PARAMETRIC_ROOM ) ) )
1614 : {
1615 : Word16 nchan_internal, ch;
1616 : Word16 ch_sba_idx;
1617 : Word16 nSamplesLeftForTD, default_frame;
1618 : Word32 *pPcm_tmp[MAX_SPAR_INTERNAL_CHANNELS];
1619 : Word32 *p_tc[MAX_SPAR_INTERNAL_CHANNELS];
1620 : Word32 Pcm_tmp[MAX_SPAR_INTERNAL_CHANNELS][L_FRAME48k];
1621 103192 : Word16 q_format = Q11;
1622 103192 : move16();
1623 :
1624 103192 : ch_sba_idx = 0;
1625 103192 : move16();
1626 103192 : IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) )
1627 : {
1628 21259 : if ( EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
1629 : {
1630 17280 : ch_sba_idx = st_ivas->nchan_ism; /*Q0*/
1631 17280 : move16();
1632 : }
1633 : }
1634 :
1635 : /* TD decorrelator */
1636 : Word32 quo, rem;
1637 103192 : iDiv_and_mod_32( st_ivas->hDecoderConfig->output_Fs, FRAMES_PER_SEC, &quo, &rem, 0 );
1638 103192 : default_frame = extract_l( quo ); /*Q0*/
1639 103192 : nSamplesLeftForTD = nSamplesForRendering; /*Q0*/
1640 103192 : move16();
1641 103192 : nchan_internal = ivas_sba_get_nchan_metadata_fx( st_ivas->sba_analysis_order, st_ivas->hDecoderConfig->ivas_total_brate ); /*Q0*/
1642 :
1643 606796 : FOR( ch = 0; ch < nchan_internal; ch++ )
1644 : {
1645 503604 : pPcm_tmp[ch] = Pcm_tmp[ch]; /*Q11*/
1646 503604 : p_tc[ch] = st_ivas->hTcBuffer->tc_fx[( ch + ch_sba_idx )]; /*Q11*/
1647 : }
1648 :
1649 206386 : WHILE( nSamplesLeftForTD )
1650 : {
1651 103194 : Word16 nSamplesToDecorr = s_min( nSamplesLeftForTD, default_frame ); /*Q0*/
1652 :
1653 103194 : IF( hSpar->hTdDecorr )
1654 : {
1655 :
1656 94124 : ivas_td_decorr_process_fx( hSpar->hTdDecorr, p_tc, pPcm_tmp, nSamplesToDecorr );
1657 94124 : st_ivas->hTcBuffer->q_tc_fx = s_min( st_ivas->hTcBuffer->q_tc_fx, q_format );
1658 94124 : move16();
1659 94124 : IF( GE_16( hSpar->hTdDecorr->num_apd_outputs, sub( nchan_internal, nchan_transport ) ) )
1660 : {
1661 251151 : FOR( ch = 0; ch < sub( nchan_internal, nchan_transport ); ch++ )
1662 : {
1663 164497 : Copy32( pPcm_tmp[( ( hSpar->hTdDecorr->num_apd_outputs - 1 ) - ch )], p_tc[( ( nchan_internal - 1 ) - ch )], nSamplesToDecorr ); /*Q11*/
1664 : }
1665 : }
1666 : ELSE
1667 : {
1668 59760 : FOR( ch = 0; ch < sub( nchan_internal, nchan_transport ); ch++ )
1669 : {
1670 52290 : set32_fx( p_tc[( ( nchan_internal - 1 ) - ch )], 0, nSamplesToDecorr );
1671 : }
1672 22410 : FOR( ch = 0; ch < hSpar->hTdDecorr->num_apd_outputs; ch++ )
1673 : {
1674 14940 : Copy32( pPcm_tmp[( ( hSpar->hTdDecorr->num_apd_outputs - 1 ) - ch )], p_tc[( ( nchan_internal - 1 ) - ch )], nSamplesToDecorr ); /*Q11*/
1675 : }
1676 : }
1677 : }
1678 606806 : FOR( ch = 0; ch < nchan_internal; ch++ )
1679 : {
1680 503612 : p_tc[ch] = p_tc[ch] + nSamplesToDecorr; /*Q11*/
1681 : }
1682 :
1683 103194 : nSamplesLeftForTD = sub( nSamplesLeftForTD, nSamplesToDecorr ); /*Q0*/
1684 : }
1685 : }
1686 :
1687 140772 : ivas_spar_dec_set_render_params_fx( st_ivas, nCldfbSlots );
1688 :
1689 140772 : return;
1690 : }
1691 :
1692 :
1693 : /*-------------------------------------------------------------------*
1694 : * ivas_spar_dec_upmixer_sf()
1695 : *
1696 : * IVAS SPAR upmixer
1697 : *-------------------------------------------------------------------*/
1698 :
1699 414193 : void ivas_spar_dec_upmixer_sf_fx(
1700 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1701 : Word32 *output_fx[], /* o : output audio channels Q11*/
1702 : const Word16 nchan_internal /* i : number of internal channels Q0*/
1703 : )
1704 : {
1705 : Word16 cldfb_band, num_cldfb_bands, numch_in, numch_out;
1706 : Word32 *cldfb_in_ts_re_fx[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS][CLDFB_NO_COL_MAX];
1707 : Word32 *cldfb_in_ts_im_fx[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS][CLDFB_NO_COL_MAX];
1708 : Word16 i, b, ts, out_ch, in_ch;
1709 : Word16 num_spar_bands, spar_band, nchan_transport;
1710 : Word16 num_in_ingest, split_band;
1711 : Word16 slot_size, slot_idx_start;
1712 : Word16 md_idx;
1713 : Word32 *p_tc_fx[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS];
1714 : Word32 Pcm_tmp_fx[MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS][L_FRAME48k];
1715 : Word16 numch_out_dirac;
1716 : Word32 mixer_mat_fx[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH][IVAS_MAX_NUM_BANDS];
1717 : Word16 b_skip_mat[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
1718 : DECODER_CONFIG_HANDLE hDecoderConfig;
1719 : SPAR_DEC_HANDLE hSpar;
1720 : Word16 num_md_sub_frames;
1721 414193 : Word16 q1 = 30;
1722 : Word16 prod;
1723 414193 : move16();
1724 414193 : push_wmops( "ivas_spar_dec_upmixer_sf_fx" );
1725 414193 : hSpar = st_ivas->hSpar;
1726 414193 : hDecoderConfig = st_ivas->hDecoderConfig;
1727 414193 : nchan_transport = hSpar->hMdDec->spar_md_cfg.nchan_transport; /*Q0*/
1728 414193 : move16();
1729 :
1730 414193 : num_cldfb_bands = hSpar->hFbMixer->pFb->fb_bin_to_band.num_cldfb_bands; /*Q0*/
1731 414193 : move16();
1732 414193 : numch_in = hSpar->hFbMixer->fb_cfg->num_in_chans; /*Q0*/
1733 414193 : move16();
1734 414193 : numch_out = hSpar->hFbMixer->fb_cfg->num_out_chans; /*Q0*/
1735 414193 : move16();
1736 414193 : 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 ); /*Q0*/
1737 414193 : slot_size = NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, CLDFB_SLOT_NS ); /*Q0*/
1738 414193 : move16();
1739 414193 : slot_idx_start = hSpar->slots_rendered; /*Q0*/
1740 414193 : move16();
1741 :
1742 414193 : prod = i_mult( slot_idx_start, slot_size );
1743 :
1744 414193 : test();
1745 414193 : IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) )
1746 69490 : {
1747 : Word16 nchan_ism;
1748 :
1749 69490 : nchan_ism = st_ivas->nchan_ism;
1750 69490 : move16();
1751 :
1752 508150 : FOR( i = 0; i < nchan_internal; i++ )
1753 : {
1754 438660 : p_tc_fx[i] = st_ivas->hTcBuffer->tc_fx[( i + nchan_ism )] + prod; /*Q11*/
1755 : }
1756 :
1757 69490 : test();
1758 69490 : test();
1759 69490 : IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) && EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) )
1760 : {
1761 56000 : FOR( i = 0; i < nchan_ism; i++ )
1762 : {
1763 44000 : p_tc_fx[( i + nchan_internal )] = st_ivas->hTcBuffer->tc_fx[i] + prod; /*Q11*/
1764 : }
1765 : }
1766 : }
1767 : ELSE
1768 : {
1769 1926379 : FOR( i = 0; i < nchan_internal; i++ )
1770 : {
1771 1581676 : p_tc_fx[i] = st_ivas->hTcBuffer->tc_fx[i] + prod; /*Q11*/
1772 : }
1773 : }
1774 :
1775 :
1776 : /*---------------------------------------------------------------------*
1777 : * TD Decorr and pcm ingest
1778 : *---------------------------------------------------------------------*/
1779 :
1780 414193 : IF( hSpar->hMdDec->td_decorr_flag )
1781 : {
1782 414193 : num_in_ingest = nchan_internal; /*Q0*/
1783 414193 : move16();
1784 : }
1785 : ELSE
1786 : {
1787 0 : num_in_ingest = nchan_transport; /*Q0*/
1788 0 : move16();
1789 : }
1790 :
1791 : /*---------------------------------------------------------------------*
1792 : * PCA decoder
1793 : *---------------------------------------------------------------------*/
1794 :
1795 414193 : hSpar->hFbMixer->fb_cfg->num_in_chans = num_in_ingest; /*Q0*/
1796 414193 : move16();
1797 :
1798 :
1799 : /*---------------------------------------------------------------------*
1800 : * Prepare CLDFB buffers
1801 : *---------------------------------------------------------------------*/
1802 :
1803 414193 : set_zero_fx( &Pcm_tmp_fx[0][0], ( MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS ) * L_FRAME48k );
1804 : /* set-up pointers */
1805 414193 : IF( NE_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_FOA ) )
1806 : {
1807 : /* at this point, output channels are used as intermediate procesing buffers */
1808 5169213 : FOR( in_ch = 0; in_ch < MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS; in_ch++ )
1809 : {
1810 24615300 : FOR( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
1811 : {
1812 19692240 : cldfb_in_ts_re_fx[in_ch][ts] = &Pcm_tmp_fx[in_ch][( ts * num_cldfb_bands )]; /*Q11*/
1813 19692240 : cldfb_in_ts_im_fx[in_ch][ts] = &Pcm_tmp_fx[in_ch][( ( ts * num_cldfb_bands ) + ( 4 * num_cldfb_bands ) )]; /*Q11*/
1814 : }
1815 : }
1816 : }
1817 : ELSE
1818 : {
1819 846200 : FOR( in_ch = 0; in_ch < numch_in; in_ch++ )
1820 : {
1821 3390800 : FOR( ts = 0; ts < MAX_PARAM_SPATIAL_SUBFRAMES; ts++ )
1822 : {
1823 2712640 : cldfb_in_ts_re_fx[in_ch][ts] = &Pcm_tmp_fx[in_ch][( ts * num_cldfb_bands )]; /*Q11*/
1824 2712640 : cldfb_in_ts_im_fx[in_ch][ts] = &Pcm_tmp_fx[in_ch][( ( ts * num_cldfb_bands ) + ( 4 * num_cldfb_bands ) )]; /*Q11*/
1825 : }
1826 : }
1827 : }
1828 :
1829 : /*---------------------------------------------------------------------*
1830 : * CLDFB Processing and Synthesis
1831 : *---------------------------------------------------------------------*/
1832 :
1833 414193 : num_spar_bands = hSpar->hFbMixer->pFb->filterbank_num_bands; /*Q0*/
1834 414193 : move16();
1835 :
1836 : /* apply parameters */
1837 : /* determine if we can skip certain data */
1838 414193 : ivas_spar_get_skip_mat_fx( hSpar, numch_out, numch_in, num_spar_bands, b_skip_mat, num_md_sub_frames ); /* this can be precomputed based on bitrate and format*/
1839 :
1840 414193 : numch_out_dirac = hDecoderConfig->nchan_out;
1841 414193 : move16();
1842 :
1843 :
1844 : /* CLDFB analysis of incoming frame */
1845 2434529 : FOR( in_ch = 0; in_ch < numch_in; in_ch++ )
1846 : {
1847 10078060 : FOR( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
1848 : {
1849 8057724 : Word16 q_cldfb = 11;
1850 8057724 : move16();
1851 8057724 : cldfbAnalysis_ts_fx_fixed_q( &p_tc_fx[in_ch][( ts * num_cldfb_bands )], cldfb_in_ts_re_fx[in_ch][ts], cldfb_in_ts_im_fx[in_ch][ts], num_cldfb_bands, st_ivas->cldfbAnaDec[in_ch], &q_cldfb );
1852 : }
1853 : }
1854 :
1855 414193 : test();
1856 414193 : test();
1857 414193 : IF( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) && EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) )
1858 : {
1859 56000 : FOR( ; in_ch < ( st_ivas->nchan_ism + numch_in ); in_ch++ )
1860 : {
1861 220000 : FOR( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
1862 : {
1863 176000 : Word16 q_cldfb = 11;
1864 176000 : move16();
1865 176000 : cldfbAnalysis_ts_fx_fixed_q( &p_tc_fx[in_ch][( ts * num_cldfb_bands )], cldfb_in_ts_re_fx[in_ch][ts], cldfb_in_ts_im_fx[in_ch][ts], num_cldfb_bands, st_ivas->cldfbAnaDec[in_ch], &q_cldfb );
1866 : }
1867 : }
1868 : }
1869 :
1870 414193 : test();
1871 414193 : test();
1872 : #ifdef NONBE_FIX_1052_SBA_EXT_FIX
1873 414193 : IF( LT_32( hDecoderConfig->ivas_total_brate, IVAS_24k4 ) && ( EQ_32( st_ivas->intern_config, IVAS_AUDIO_CONFIG_HOA2 ) || EQ_32( st_ivas->intern_config, IVAS_AUDIO_CONFIG_HOA3 ) ) )
1874 : #else
1875 : IF( ( LT_32( hDecoderConfig->ivas_total_brate, IVAS_24k4 ) ) && ( ( EQ_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_HOA2 ) ) || ( EQ_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_HOA3 ) ) ) )
1876 : #endif
1877 : {
1878 8316 : Word16 q_cldfb = 6;
1879 8316 : move16();
1880 8316 : ivas_spar_calc_smooth_facs_fx( cldfb_in_ts_re_fx[0], cldfb_in_ts_im_fx[0], q_cldfb, num_spar_bands, hSpar->subframe_nbslots[hSpar->subframes_rendered],
1881 8316 : hSpar->subframes_rendered == 0, &hSpar->hFbMixer->pFb->fb_bin_to_band, hSpar->hMdDec->smooth_fac_fx, hSpar->hMdDec->smooth_buf_fx );
1882 : }
1883 :
1884 414193 : Word16 sh_l = sub( 31, q1 );
1885 2065280 : FOR( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
1886 : {
1887 1651087 : md_idx = hSpar->render_to_md_map[( ts + slot_idx_start )]; /*Q0*/
1888 1651087 : move16();
1889 1651087 : Scale_sig( hSpar->hFbMixer->cldfb_cross_fade_fx, CLDFB_NO_COL_MAX, Q15 - st_ivas->hSpar->hFbMixer->cldfb_cross_fade_q ); /*Q15*/
1890 1651087 : st_ivas->hSpar->hFbMixer->cldfb_cross_fade_q = Q15;
1891 1651087 : move16();
1892 1651087 : ivas_spar_get_parameters_fx( hSpar, hDecoderConfig, md_idx, numch_out, numch_in, num_spar_bands, mixer_mat_fx );
1893 :
1894 1651087 : test();
1895 1651087 : test();
1896 : #ifdef NONBE_FIX_1052_SBA_EXT_FIX
1897 1651087 : IF( LT_32( hDecoderConfig->ivas_total_brate, IVAS_24k4 ) && ( EQ_32( st_ivas->intern_config, IVAS_AUDIO_CONFIG_HOA2 ) || EQ_32( st_ivas->intern_config, IVAS_AUDIO_CONFIG_HOA3 ) ) )
1898 : #else
1899 : IF( ( LT_32( hDecoderConfig->ivas_total_brate, IVAS_24k4 ) ) && ( ( EQ_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_HOA2 ) ) || ( EQ_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_HOA3 ) ) ) )
1900 : #endif
1901 : {
1902 432432 : FOR( spar_band = 0; spar_band < num_spar_bands; spar_band++ )
1903 : {
1904 399168 : Word16 diff = sub( 32767, hSpar->hMdDec->smooth_fac_fx[spar_band] );
1905 1995840 : FOR( out_ch = 0; out_ch < numch_out; out_ch++ )
1906 : {
1907 7983360 : FOR( in_ch = 0; in_ch < numch_in; in_ch++ )
1908 : {
1909 6386688 : mixer_mat_fx[out_ch][in_ch][spar_band] = Madd_32_16( Mpy_32_16_1( mixer_mat_fx[out_ch][in_ch][spar_band], diff ), hSpar->hMdDec->mixer_mat_prev2_fx[out_ch][in_ch][spar_band], hSpar->hMdDec->smooth_fac_fx[spar_band] ); /*q1*/
1910 6386688 : move32();
1911 6386688 : hSpar->hMdDec->mixer_mat_prev2_fx[out_ch][in_ch][spar_band] = mixer_mat_fx[out_ch][in_ch][spar_band]; /*q1*/
1912 6386688 : move32();
1913 : }
1914 : }
1915 : }
1916 : }
1917 : /* Note: This version splits the cldfb band loop into 2 loops, removing some inner-loop IF_statements */
1918 1651087 : Word16 min_cldf_band = s_min( CLDFB_PAR_WEIGHT_START_BAND, num_cldfb_bands );
1919 : Word32 out_re_fx[IVAS_SPAR_MAX_CH];
1920 : Word32 out_im_fx[IVAS_SPAR_MAX_CH];
1921 : Word32 cldfb_par_fx; /*q1*/
1922 1651087 : ivas_fb_bin_to_band_data_t *bin2band = &hSpar->hFbMixer->pFb->fb_bin_to_band;
1923 :
1924 : /* First loop from cldfb_band=0 till min_cldf_band (CLDFB_PAR_WEIGHT_START_BAND) */
1925 13208696 : FOR( cldfb_band = 0; cldfb_band < min_cldf_band; cldfb_band++ )
1926 : {
1927 11557609 : spar_band = bin2band->p_cldfb_map_to_spar_band[cldfb_band]; /*Q0*/
1928 11557609 : move16();
1929 67961677 : FOR( out_ch = 0; out_ch < numch_out; out_ch++ )
1930 : {
1931 56404068 : out_re_fx[out_ch] = 0;
1932 56404068 : move32();
1933 56404068 : out_im_fx[out_ch] = 0;
1934 56404068 : move32();
1935 380829876 : FOR( in_ch = 0; in_ch < numch_in; in_ch++ )
1936 : {
1937 324425808 : IF( b_skip_mat[out_ch][in_ch] == 0 )
1938 : {
1939 131820325 : cldfb_par_fx = mixer_mat_fx[out_ch][in_ch][spar_band]; /*q1*/
1940 131820325 : move32();
1941 131820325 : out_re_fx[out_ch] = Madd_32_32( out_re_fx[out_ch], cldfb_in_ts_re_fx[in_ch][ts][cldfb_band], cldfb_par_fx ); /*q1-25*/
1942 131820325 : move32();
1943 131820325 : out_im_fx[out_ch] = Madd_32_32( out_im_fx[out_ch], cldfb_in_ts_im_fx[in_ch][ts][cldfb_band], cldfb_par_fx ); /*q1-25*/
1944 131820325 : move32();
1945 : }
1946 : }
1947 : }
1948 : /*update CLDFB data with the parameter-modified data*/
1949 67961677 : FOR( out_ch = 0; out_ch < numch_out; out_ch++ )
1950 : {
1951 56404068 : cldfb_in_ts_re_fx[out_ch][ts][cldfb_band] = L_shl( out_re_fx[out_ch], sh_l ); /*Q=6*/
1952 56404068 : move32();
1953 56404068 : cldfb_in_ts_im_fx[out_ch][ts][cldfb_band] = L_shl( out_im_fx[out_ch], sh_l ); /*Q=6*/
1954 56404068 : move32();
1955 : }
1956 : }
1957 :
1958 :
1959 : /* Second loop from min_cldf_band (CLDFB_PAR_WEIGHT_START_BAND) till num_cldfb_bands */
1960 71984958 : FOR( ; cldfb_band < num_cldfb_bands; cldfb_band++ )
1961 : {
1962 426880683 : FOR( out_ch = 0; out_ch < numch_out; out_ch++ )
1963 : {
1964 356546812 : Word32 Out_re_fx = L_add( 0, 0 );
1965 356546812 : Word32 Out_im_fx = L_add( 0, 0 );
1966 2515919404 : FOR( in_ch = 0; in_ch < numch_in; in_ch++ )
1967 : {
1968 2159372592 : IF( b_skip_mat[out_ch][in_ch] == 0 )
1969 : {
1970 858312755 : Word64 acc = 0;
1971 858312755 : move64();
1972 :
1973 858312755 : cldfb_par_fx = 0;
1974 858312755 : move32();
1975 4420271740 : FOR( spar_band = bin2band->p_spar_start_bands[cldfb_band]; spar_band < num_spar_bands; spar_band++ )
1976 : {
1977 : /* accumulate contributions from all SPAR bands */
1978 3561958985 : acc = W_mac_32_32( acc, mixer_mat_fx[out_ch][in_ch][spar_band], bin2band->pp_cldfb_weights_per_spar_band_fx[cldfb_band][spar_band] ); // q1+ Q23
1979 : }
1980 858312755 : cldfb_par_fx = W_shl_sat_l( acc, -23 ); // q1
1981 858312755 : Out_re_fx = Madd_32_32( Out_re_fx, cldfb_in_ts_re_fx[in_ch][ts][cldfb_band], cldfb_par_fx ); /*q1-25*/
1982 858312755 : Out_im_fx = Madd_32_32( Out_im_fx, cldfb_in_ts_im_fx[in_ch][ts][cldfb_band], cldfb_par_fx ); /*q1-25*/
1983 : }
1984 : }
1985 356546812 : out_re_fx[out_ch] = Out_re_fx;
1986 356546812 : out_im_fx[out_ch] = Out_im_fx;
1987 : }
1988 :
1989 : /*update CLDFB data with the parameter-modified data*/
1990 426880683 : FOR( out_ch = 0; out_ch < numch_out; out_ch++ )
1991 : {
1992 356546812 : cldfb_in_ts_re_fx[out_ch][ts][cldfb_band] = L_shl( out_re_fx[out_ch], sh_l ); /*Q=6*/
1993 356546812 : move32();
1994 356546812 : cldfb_in_ts_im_fx[out_ch][ts][cldfb_band] = L_shl( out_im_fx[out_ch], sh_l ); /*Q=6*/
1995 356546812 : move32();
1996 : }
1997 : }
1998 1651087 : test();
1999 1651087 : IF( ( EQ_16( ( add( add( slot_idx_start, ts ), 1 ) ), hSpar->num_slots ) ) || ( NE_16( ( shr( md_idx, 2 ) /* md_idx / JBM_CLDFB_SLOTS_IN_SUBFRAME */ ), ( hSpar->render_to_md_map[( ( slot_idx_start + ts ) + 1 )] / JBM_CLDFB_SLOTS_IN_SUBFRAME /*It's value is 4*/ ) ) ) )
2000 : {
2001 : /* we have crossed an unadapted parameter sf border, update previous mixing matrices */
2002 412768 : Word16 md_sf = shr( md_idx, 2 ) /* md_idx / JBM_CLDFB_SLOTS_IN_SUBFRAME */; /*Q0*/
2003 412768 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
2004 412768 : move16();
2005 412768 : if ( NE_16( num_md_sub_frames, MAX_PARAM_SPATIAL_SUBFRAMES ) )
2006 : {
2007 60440 : md_sf = 0;
2008 60440 : move16();
2009 : }
2010 412768 : IF( LT_16( split_band, IVAS_MAX_NUM_BANDS ) )
2011 : {
2012 :
2013 2427184 : FOR( out_ch = 0; out_ch < numch_out; out_ch++ )
2014 : {
2015 13600992 : FOR( in_ch = 0; in_ch < numch_in; in_ch++ )
2016 : {
2017 149285456 : FOR( b = 0; b < num_spar_bands; b++ )
2018 : {
2019 137698880 : hSpar->hMdDec->mixer_mat_prev_fx[0][out_ch][in_ch][b] = hSpar->hMdDec->mixer_mat_prev_fx[1][out_ch][in_ch][b];
2020 137698880 : hSpar->hMdDec->mixer_mat_prev_fx[1][out_ch][in_ch][b] = hSpar->hMdDec->mixer_mat_prev_fx[2][out_ch][in_ch][b];
2021 137698880 : hSpar->hMdDec->mixer_mat_prev_fx[2][out_ch][in_ch][b] = hSpar->hMdDec->mixer_mat_prev_fx[3][out_ch][in_ch][b];
2022 137698880 : hSpar->hMdDec->mixer_mat_prev_fx[3][out_ch][in_ch][b] = hSpar->hMdDec->mixer_mat_prev_fx[4][out_ch][in_ch][b];
2023 137698880 : move32();
2024 137698880 : move32();
2025 137698880 : move32();
2026 137698880 : move32();
2027 :
2028 137698880 : hSpar->hMdDec->mixer_mat_prev_fx[4][out_ch][in_ch][b] = hSpar->hMdDec->mixer_mat_fx[out_ch][in_ch][( b + ( md_sf * IVAS_MAX_NUM_BANDS ) )]; /*hSpar->hMdDec->Q_mixer_mat*/
2029 137698880 : move32();
2030 : }
2031 : }
2032 : }
2033 412768 : hSpar->i_subframe = add( hSpar->i_subframe, 1 ); /*Q0*/
2034 412768 : move16();
2035 412768 : hSpar->i_subframe = s_min( hSpar->i_subframe, MAX_PARAM_SPATIAL_SUBFRAMES ); /*Q0*/
2036 412768 : move16();
2037 : }
2038 : }
2039 : }
2040 :
2041 414193 : test();
2042 414193 : test();
2043 : #ifdef NONBE_FIX_1052_SBA_EXT_FIX
2044 414193 : test();
2045 414193 : test();
2046 414193 : IF( NE_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_FOA ) && NE_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) && NE_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_MONO ) && !( EQ_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_EXTERNAL ) && EQ_32( st_ivas->intern_config, IVAS_AUDIO_CONFIG_FOA ) ) )
2047 : #else
2048 : IF( NE_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_FOA ) && NE_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_STEREO ) && NE_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_MONO ) )
2049 : #endif
2050 : {
2051 237153 : ivas_dirac_dec_render_sf_fx( st_ivas, output_fx, nchan_internal, cldfb_in_ts_re_fx, cldfb_in_ts_im_fx );
2052 : }
2053 :
2054 : /*------------------------------------------------------------------ends*/
2055 :
2056 414193 : IF( st_ivas->hDirAC != NULL )
2057 : {
2058 : Word16 outchannels, idx_in, idx_lfe, ch;
2059 245913 : idx_in = 0;
2060 245913 : move16();
2061 245913 : idx_lfe = 0;
2062 245913 : move16();
2063 :
2064 245913 : outchannels = add( st_ivas->hOutSetup.nchan_out_woLFE, st_ivas->hOutSetup.num_lfe ); /*Q0*/
2065 :
2066 2091573 : FOR( ch = 0; ch < outchannels; ch++ )
2067 : {
2068 1845660 : test();
2069 1845660 : IF( ( st_ivas->hOutSetup.num_lfe > 0 ) && ( EQ_16( st_ivas->hOutSetup.index_lfe[idx_lfe], ch ) ) )
2070 : {
2071 41000 : set_zero_fx( output_fx[ch], i_mult( hSpar->subframe_nbslots[hSpar->subframes_rendered], num_cldfb_bands ) );
2072 41000 : if ( LT_16( idx_lfe, ( sub( st_ivas->hDirACRend->hOutSetup.num_lfe, 1 ) ) ) )
2073 : {
2074 0 : idx_lfe = add( idx_lfe, 1 );
2075 : }
2076 : }
2077 : ELSE
2078 : {
2079 1804660 : test();
2080 1804660 : test();
2081 1804660 : test();
2082 1804660 : test();
2083 1804660 : test();
2084 1804660 : test();
2085 1804660 : IF( ( EQ_32( hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_FOA ) || !( EQ_32( st_ivas->hOutSetup.output_config, IVAS_AUDIO_CONFIG_BINAURAL ) || EQ_32( st_ivas->hOutSetup.output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR ) || EQ_32( st_ivas->hOutSetup.output_config, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) || EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ) || EQ_32( st_ivas->hDecoderConfig->output_config, IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ) ) &&
2086 : !( EQ_32( st_ivas->ivas_format, SBA_ISM_FORMAT ) && EQ_32( st_ivas->ism_mode, ISM_SBA_MODE_DISC ) && EQ_32( st_ivas->renderer_type, RENDERER_BINAURAL_FASTCONV_ROOM ) ) )
2087 : {
2088 7161680 : FOR( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
2089 : {
2090 5717424 : cldfbSynthesis_ivas_fx( &cldfb_in_ts_re_fx[idx_in][ts], &cldfb_in_ts_im_fx[idx_in][ts], &output_fx[ch][i_mult( ts, num_cldfb_bands )], num_cldfb_bands, 6, 0, st_ivas->cldfbSynDec[idx_in] );
2091 : }
2092 : }
2093 :
2094 1804660 : idx_in = add( idx_in, 1 );
2095 : }
2096 : }
2097 : }
2098 : ELSE
2099 : {
2100 : /* CLDFB to time synthesis (overwrite mixer output) */
2101 841400 : FOR( out_ch = 0; out_ch < numch_out_dirac; out_ch++ )
2102 : {
2103 3365600 : FOR( ts = 0; ts < hSpar->subframe_nbslots[hSpar->subframes_rendered]; ts++ )
2104 : {
2105 2692480 : cldfbSynthesis_ivas_fx( &cldfb_in_ts_re_fx[out_ch][ts], &cldfb_in_ts_im_fx[out_ch][ts], &output_fx[out_ch][i_mult( ts, num_cldfb_bands )], num_cldfb_bands, 6, 0, st_ivas->cldfbSynDec[out_ch] );
2106 : }
2107 : }
2108 : }
2109 :
2110 414193 : hSpar->slots_rendered = add( hSpar->slots_rendered, hSpar->subframe_nbslots[hSpar->subframes_rendered] ); /*Q0*/
2111 414193 : move16();
2112 414193 : hSpar->subframes_rendered = add( hSpar->subframes_rendered, 1 ); /*Q0*/
2113 414193 : move16();
2114 :
2115 414193 : pop_wmops();
2116 :
2117 414193 : return;
2118 : }
|