Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2026 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : #include <stdint.h>
34 : #include "options.h"
35 : #ifdef DEBUGGING
36 : #include "debug.h"
37 : #endif
38 : #include "math.h"
39 : #include "prot.h"
40 : #include "ivas_prot.h"
41 : #include "ivas_rom_com.h"
42 : #include <assert.h>
43 : #include "wmc_auto.h"
44 : #include "ivas_stat_dec.h"
45 :
46 :
47 : /*------------------------------------------------------------------------------------------*
48 : * Local constants
49 : *------------------------------------------------------------------------------------------*/
50 :
51 : #define IVAS_DEFAULT_DTX_CNG_RAMP ( 8 )
52 :
53 : /* PLC constants */
54 : static const int16_t ivas_spar_dec_plc_num_frames_keep = 9;
55 : static const int16_t ivas_spar_dec_plc_num_frames_fade_out = 9;
56 : static const int16_t ivas_spar_dec_plc_per_frame_ramp_down_gain_dB = 3;
57 : static const int16_t ivas_spar_dec_plc_max_num_frames_ramp_down = 33;
58 : static const int16_t ivas_spar_dec_plc_spatial_target[IVAS_SPAR_MAX_CH] = { 1, 0, 0, 0, 0, 0, 0, 0 };
59 :
60 :
61 : /*------------------------------------------------------------------------------------------*
62 : * Static functions declaration
63 : *------------------------------------------------------------------------------------------*/
64 :
65 : static void ivas_get_spar_matrices( ivas_spar_md_dec_state_t *hMdDec, const int16_t num_bands_out, const int16_t n_ts, const int16_t bw, const int16_t dtx_vad, const int16_t nB, const int16_t numch_out, const int16_t active_w_vlbr, const int16_t dyn_active_w_flag );
66 :
67 : static void ivas_decode_arith_bs( ivas_spar_md_dec_state_t *hMdDec, Decoder_State *st, const uint16_t qsi, const int16_t nB, const int16_t bands_bw, int16_t *pDo_diff, const int16_t strat, const int32_t ivas_total_brate );
68 :
69 : static void ivas_decode_huffman_bs( ivas_spar_md_dec_state_t *hMdDec, Decoder_State *st, const uint16_t qsi, const int16_t nB, const int16_t bands_bw );
70 :
71 : static void ivas_fill_band_coeffs_idx( ivas_band_coeffs_ind_t *pBands_idx, const int16_t nB, int16_t *pSymbol_re, ivas_cell_dim_t *pCell_dims, ivas_coeffs_type_t coeff_type );
72 :
73 : static void ivas_mat_col_rearrange( float in_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH], const int16_t order[IVAS_SPAR_MAX_CH], const int16_t i_ts, float ***mixer_mat, const int16_t bands, const int16_t num_ch );
74 :
75 : static void ivas_spar_dec_compute_ramp_down_post_matrix( ivas_spar_md_dec_state_t *hMdDec, const int16_t num_bands, const int16_t bfi, const int16_t num_md_sub_frames );
76 :
77 : static void ivas_spar_md_fill_invalid_bands( ivas_spar_dec_matrices_t *pSpar_coeffs, ivas_spar_dec_matrices_t *pSpar_coeffs_prev, const int16_t *valid_bands, int16_t *base_band_age, const int16_t num_bands, const int16_t numch_out, const int16_t num_md_sub_frames );
78 :
79 : static void ivas_spar_md_fill_invalid_bandcoeffs( ivas_band_coeffs_t *pBand_coeffs, ivas_band_coeffs_t *pBand_coeffs_prev, const int16_t *valid_bands, int16_t *base_band_age, int16_t *first_valid_frame, const int16_t num_bands );
80 : static ivas_error ivas_spar_set_dec_config( ivas_spar_md_dec_state_t *hMdDec, const int16_t nchan_transport, float *pFC );
81 :
82 : static void ivas_parse_parameter_bitstream_dtx( ivas_spar_md_t *pSpar_md, Decoder_State *st, const int16_t bw, const int16_t num_bands, int16_t *num_dmx_per_band, int16_t *num_dec_per_band );
83 :
84 : static ivas_error ivas_deindex_real_index( const int16_t *index, const int16_t q_levels, const float min_value, const float max_value, float *quant, const int16_t num_ch_dim2 );
85 :
86 : static void ivas_spar_dec_parse_md_bs( ivas_spar_md_dec_state_t *hMdDec, Decoder_State *st, int16_t *nB, int16_t *bands_bw, int16_t *dtx_vad, const int32_t ivas_total_brate, const int16_t sba_inactive_mode );
87 :
88 :
89 : /*-------------------------------------------------------------------------
90 : * ivas_spar_md_dec_matrix_open()
91 : *
92 : * Allocate and initialize SPAR MD decoder matrices
93 : *------------------------------------------------------------------------*/
94 :
95 39321 : ivas_error ivas_spar_md_dec_matrix_open(
96 : ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle */
97 : const int16_t num_channels, /* i : number of internal channels */
98 : const int16_t num_md_sub_frames /* i : number of MD subframes */
99 : )
100 : {
101 : int16_t i, j;
102 : int16_t k;
103 39321 : if ( ( hMdDec->spar_md.band_coeffs = (ivas_band_coeffs_t *) malloc( IVAS_MAX_NUM_BANDS * num_md_sub_frames * sizeof( ivas_band_coeffs_t ) ) ) == NULL )
104 : {
105 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for band_coeffs in SPAR MD" );
106 : }
107 39321 : if ( ( hMdDec->band_coeffs_prev = (ivas_band_coeffs_t *) malloc( IVAS_MAX_NUM_BANDS * sizeof( ivas_band_coeffs_t ) ) ) == NULL )
108 : {
109 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for band_coeffs in SPAR MD" );
110 : }
111 39321 : if ( ( hMdDec->mixer_mat = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
112 : {
113 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
114 : }
115 225840 : for ( i = 0; i < num_channels; i++ )
116 : {
117 186519 : if ( ( hMdDec->mixer_mat[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
118 : {
119 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
120 : }
121 1203558 : for ( j = 0; j < num_channels; j++ )
122 : {
123 1017039 : if ( ( hMdDec->mixer_mat[i][j] = (float *) malloc( num_md_sub_frames * IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
124 : {
125 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
126 : }
127 : }
128 : }
129 :
130 39321 : if ( ( hMdDec->spar_coeffs.C_re = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
131 : {
132 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
133 : }
134 225840 : for ( i = 0; i < num_channels; i++ )
135 : {
136 186519 : if ( ( hMdDec->spar_coeffs.C_re[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
137 : {
138 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
139 : }
140 1203558 : for ( j = 0; j < num_channels; j++ )
141 : {
142 1017039 : if ( ( hMdDec->spar_coeffs.C_re[i][j] = (float *) malloc( num_md_sub_frames * IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
143 : {
144 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
145 : }
146 : }
147 : }
148 :
149 39321 : if ( ( hMdDec->spar_coeffs.P_re = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
150 : {
151 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
152 : }
153 225840 : for ( i = 0; i < num_channels; i++ )
154 : {
155 186519 : if ( ( hMdDec->spar_coeffs.P_re[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
156 : {
157 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
158 : }
159 1203558 : for ( j = 0; j < num_channels; j++ )
160 : {
161 1017039 : if ( ( hMdDec->spar_coeffs.P_re[i][j] = (float *) malloc( num_md_sub_frames * IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
162 : {
163 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
164 : }
165 : }
166 : }
167 :
168 39321 : if ( ( hMdDec->spar_coeffs_prev.C_re = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
169 : {
170 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
171 : }
172 225840 : for ( i = 0; i < num_channels; i++ )
173 : {
174 186519 : if ( ( hMdDec->spar_coeffs_prev.C_re[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
175 : {
176 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
177 : }
178 1203558 : for ( j = 0; j < num_channels; j++ )
179 : {
180 1017039 : if ( ( hMdDec->spar_coeffs_prev.C_re[i][j] = (float *) malloc( IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
181 : {
182 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
183 : }
184 : }
185 : }
186 :
187 39321 : if ( ( hMdDec->spar_coeffs_prev.P_re = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
188 : {
189 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
190 : }
191 225840 : for ( i = 0; i < num_channels; i++ )
192 : {
193 186519 : if ( ( hMdDec->spar_coeffs_prev.P_re[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
194 : {
195 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
196 : }
197 1203558 : for ( j = 0; j < num_channels; j++ )
198 : {
199 1017039 : if ( ( hMdDec->spar_coeffs_prev.P_re[i][j] = (float *) malloc( IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
200 : {
201 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
202 : }
203 : }
204 : }
205 :
206 39321 : if ( ( hMdDec->spar_coeffs_tar.C_re = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
207 : {
208 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
209 : }
210 225840 : for ( i = 0; i < num_channels; i++ )
211 : {
212 186519 : if ( ( hMdDec->spar_coeffs_tar.C_re[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
213 : {
214 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
215 : }
216 1203558 : for ( j = 0; j < num_channels; j++ )
217 : {
218 1017039 : if ( ( hMdDec->spar_coeffs_tar.C_re[i][j] = (float *) malloc( IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
219 : {
220 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
221 : }
222 : }
223 : }
224 :
225 39321 : if ( ( hMdDec->spar_coeffs_tar.P_re = (float ***) malloc( num_channels * sizeof( float ** ) ) ) == NULL )
226 : {
227 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
228 : }
229 225840 : for ( i = 0; i < num_channels; i++ )
230 : {
231 186519 : if ( ( hMdDec->spar_coeffs_tar.P_re[i] = (float **) malloc( num_channels * sizeof( float * ) ) ) == NULL )
232 : {
233 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
234 : }
235 1203558 : for ( j = 0; j < num_channels; j++ )
236 : {
237 1017039 : if ( ( hMdDec->spar_coeffs_tar.P_re[i][j] = (float *) malloc( IVAS_MAX_NUM_BANDS * sizeof( float ) ) ) == NULL )
238 : {
239 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD matrix" );
240 : }
241 : }
242 : }
243 225840 : for ( i = 0; i < num_channels; i++ )
244 : {
245 1203558 : for ( j = 0; j < num_channels; j++ )
246 : {
247 13221507 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
248 : {
249 12204468 : hMdDec->spar_coeffs_prev.C_re[i][j][k] = 0.0f;
250 12204468 : hMdDec->spar_coeffs_prev.P_re[i][j][k] = 0.0f;
251 12204468 : hMdDec->spar_coeffs_tar.C_re[i][j][k] = 0.0f;
252 12204468 : hMdDec->spar_coeffs_tar.P_re[i][j][k] = 0.0f;
253 : }
254 : }
255 : }
256 :
257 39321 : return IVAS_ERR_OK;
258 : }
259 :
260 :
261 : /*-------------------------------------------------------------------------
262 : * ivas_get_spar_dec_md_num_subframes()
263 : *
264 : * return number of MD subframes
265 : *------------------------------------------------------------------------*/
266 :
267 : /*! r: number of MD subframes */
268 59915394 : int16_t ivas_get_spar_dec_md_num_subframes(
269 : const int16_t sba_order, /* i : Ambisonic (SBA) order */
270 : const int32_t ivas_total_brate, /* i : IVAS total bitrate */
271 : const int32_t ivas_last_active_brate /* i : IVAS last active bitrate */
272 : )
273 : {
274 : int16_t num_subframes;
275 :
276 59915394 : num_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
277 59915394 : if ( sba_order > SBA_FOA_ORDER )
278 : {
279 8656353 : if ( ivas_total_brate >= IVAS_512k )
280 : {
281 1669512 : num_subframes = 1;
282 : }
283 : }
284 :
285 59915394 : if ( ( ivas_total_brate <= IVAS_SID_5k2 && ivas_last_active_brate < IVAS_24k4 ) || ( ivas_total_brate > IVAS_SID_5k2 && ivas_total_brate < IVAS_24k4 ) )
286 : {
287 :
288 3100227 : num_subframes = 1;
289 : }
290 :
291 59915394 : return ( num_subframes );
292 : }
293 :
294 :
295 : /*-------------------------------------------------------------------------
296 : * ivas_spar_md_dec_open()
297 : *
298 : * Allocate and initialize SPAR MD decoder handle
299 : *------------------------------------------------------------------------*/
300 :
301 37500 : ivas_error ivas_spar_md_dec_open(
302 : ivas_spar_md_dec_state_t **hMdDec_out, /* i/o: SPAR MD decoder handle */
303 : const DECODER_CONFIG_HANDLE hDecoderConfig, /* i : configuration structure */
304 : const int16_t num_channels, /* i : number of internal channels */
305 : const int16_t sba_order, /* i : SBA order */
306 : const int16_t sid_format, /* i : SID format */
307 : const int32_t last_active_ivas_total_brate /* i : IVAS last active bitrate */
308 : )
309 : {
310 : ivas_spar_md_dec_state_t *hMdDec;
311 : ivas_error error;
312 : int16_t num_md_sub_frames;
313 :
314 37500 : error = IVAS_ERR_OK;
315 :
316 37500 : if ( ( hMdDec = (ivas_spar_md_dec_state_t *) malloc( sizeof( ivas_spar_md_dec_state_t ) ) ) == NULL )
317 : {
318 0 : return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR MD decoder" );
319 : }
320 :
321 37500 : num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( sba_order, hDecoderConfig->ivas_total_brate, last_active_ivas_total_brate );
322 :
323 37500 : if ( ( error = ivas_spar_md_dec_matrix_open( hMdDec, num_channels, num_md_sub_frames ) ) != IVAS_ERR_OK )
324 : {
325 0 : return error;
326 : }
327 :
328 37500 : if ( hDecoderConfig->ivas_total_brate == IVAS_SID_5k2 )
329 : {
330 0 : if ( sid_format == SID_SBA_2TC )
331 : {
332 0 : hMdDec->table_idx = ivas_get_spar_table_idx( IVAS_48k, sba_order, SPAR_CONFIG_BW, NULL, NULL );
333 : }
334 : else
335 : {
336 0 : hMdDec->table_idx = ivas_get_spar_table_idx( IVAS_24k4, sba_order, SPAR_CONFIG_BW, NULL, NULL );
337 : }
338 : }
339 : else
340 : {
341 37500 : hMdDec->table_idx = ivas_get_spar_table_idx( hDecoderConfig->ivas_total_brate, sba_order, SPAR_CONFIG_BW, NULL, NULL );
342 : }
343 :
344 37500 : if ( ( error = ivas_spar_md_dec_init( hMdDec, hDecoderConfig, num_channels, sba_order ) ) != IVAS_ERR_OK )
345 : {
346 0 : return error;
347 : }
348 :
349 37500 : *hMdDec_out = hMdDec;
350 :
351 37500 : return error;
352 : }
353 :
354 :
355 : /*-------------------------------------------------------------------------
356 : * ivas_spar_md_dec_matrix_close()
357 : *
358 : * Deallocate SPAR MD decoder matrices
359 : *------------------------------------------------------------------------*/
360 :
361 39321 : void ivas_spar_md_dec_matrix_close(
362 : ivas_spar_md_dec_state_t *hMdDecoder, /* i/o: SPAR MD decoder handle */
363 : const int16_t num_channels /* i : number of internal channels */
364 : )
365 : {
366 : int16_t i, j;
367 :
368 39321 : if ( hMdDecoder->spar_md.band_coeffs != NULL )
369 : {
370 39321 : free( hMdDecoder->spar_md.band_coeffs );
371 39321 : hMdDecoder->spar_md.band_coeffs = NULL;
372 : }
373 39321 : if ( hMdDecoder->band_coeffs_prev != NULL )
374 : {
375 39321 : free( hMdDecoder->band_coeffs_prev );
376 39321 : hMdDecoder->band_coeffs_prev = NULL;
377 : }
378 :
379 39321 : if ( hMdDecoder->mixer_mat != NULL )
380 : {
381 225840 : for ( i = 0; i < num_channels; i++ )
382 : {
383 1203558 : for ( j = 0; j < num_channels; j++ )
384 : {
385 1017039 : free( hMdDecoder->mixer_mat[i][j] );
386 : }
387 186519 : free( hMdDecoder->mixer_mat[i] );
388 : }
389 39321 : free( hMdDecoder->mixer_mat );
390 : }
391 :
392 39321 : if ( hMdDecoder->spar_coeffs.C_re != NULL )
393 : {
394 225840 : for ( i = 0; i < num_channels; i++ )
395 : {
396 1203558 : for ( j = 0; j < num_channels; j++ )
397 : {
398 1017039 : free( hMdDecoder->spar_coeffs.C_re[i][j] );
399 : }
400 186519 : free( hMdDecoder->spar_coeffs.C_re[i] );
401 : }
402 39321 : free( hMdDecoder->spar_coeffs.C_re );
403 : }
404 :
405 39321 : if ( hMdDecoder->spar_coeffs.P_re != NULL )
406 : {
407 225840 : for ( i = 0; i < num_channels; i++ )
408 : {
409 1203558 : for ( j = 0; j < num_channels; j++ )
410 : {
411 1017039 : free( hMdDecoder->spar_coeffs.P_re[i][j] );
412 : }
413 186519 : free( hMdDecoder->spar_coeffs.P_re[i] );
414 : }
415 39321 : free( hMdDecoder->spar_coeffs.P_re );
416 : }
417 :
418 39321 : if ( hMdDecoder->spar_coeffs_prev.C_re != NULL )
419 : {
420 225840 : for ( i = 0; i < num_channels; i++ )
421 : {
422 1203558 : for ( j = 0; j < num_channels; j++ )
423 : {
424 1017039 : free( hMdDecoder->spar_coeffs_prev.C_re[i][j] );
425 : }
426 186519 : free( hMdDecoder->spar_coeffs_prev.C_re[i] );
427 : }
428 39321 : free( hMdDecoder->spar_coeffs_prev.C_re );
429 : }
430 :
431 39321 : if ( hMdDecoder->spar_coeffs_prev.P_re != NULL )
432 : {
433 225840 : for ( i = 0; i < num_channels; i++ )
434 : {
435 1203558 : for ( j = 0; j < num_channels; j++ )
436 : {
437 1017039 : free( hMdDecoder->spar_coeffs_prev.P_re[i][j] );
438 : }
439 186519 : free( hMdDecoder->spar_coeffs_prev.P_re[i] );
440 : }
441 39321 : free( hMdDecoder->spar_coeffs_prev.P_re );
442 : }
443 :
444 39321 : if ( hMdDecoder->spar_coeffs_tar.C_re != NULL )
445 : {
446 225840 : for ( i = 0; i < num_channels; i++ )
447 : {
448 1203558 : for ( j = 0; j < num_channels; j++ )
449 : {
450 1017039 : free( hMdDecoder->spar_coeffs_tar.C_re[i][j] );
451 : }
452 186519 : free( hMdDecoder->spar_coeffs_tar.C_re[i] );
453 : }
454 39321 : free( hMdDecoder->spar_coeffs_tar.C_re );
455 : }
456 :
457 39321 : if ( hMdDecoder->spar_coeffs_tar.P_re != NULL )
458 : {
459 225840 : for ( i = 0; i < num_channels; i++ )
460 : {
461 1203558 : for ( j = 0; j < num_channels; j++ )
462 : {
463 1017039 : free( hMdDecoder->spar_coeffs_tar.P_re[i][j] );
464 : }
465 186519 : free( hMdDecoder->spar_coeffs_tar.P_re[i] );
466 : }
467 39321 : free( hMdDecoder->spar_coeffs_tar.P_re );
468 : }
469 :
470 39321 : return;
471 : }
472 :
473 :
474 : /*-------------------------------------------------------------------------
475 : * ivas_spar_md_dec_close()
476 : *
477 : * Deallocate SPAR MD decoder handle
478 : *------------------------------------------------------------------------*/
479 :
480 37500 : void ivas_spar_md_dec_close(
481 : ivas_spar_md_dec_state_t **hMdDec /* i/o: SPAR MD decoder handle */
482 : )
483 : {
484 : ivas_spar_md_dec_state_t *hMdDecoder;
485 : int16_t num_channels;
486 :
487 37500 : hMdDecoder = *hMdDec;
488 37500 : num_channels = hMdDecoder->spar_md_cfg.num_umx_chs;
489 :
490 37500 : ivas_spar_md_dec_matrix_close( hMdDecoder, num_channels );
491 :
492 37500 : free( *hMdDec );
493 37500 : *hMdDec = NULL;
494 :
495 37500 : return;
496 : }
497 :
498 :
499 : /*-----------------------------------------------------------------------------------------*
500 : * Function ivas_spar_md_dec_init()
501 : *
502 : * SPAR MD decoder initialization
503 : *-----------------------------------------------------------------------------------------*/
504 :
505 48090 : ivas_error ivas_spar_md_dec_init(
506 : ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle */
507 : const DECODER_CONFIG_HANDLE hDecoderConfig, /* i : configuration structure */
508 : const int16_t num_channels, /* i : number of internal channels */
509 : const int16_t sba_order /* i : SBA order */
510 : )
511 : {
512 : int16_t i, j;
513 : int16_t nchan_transport;
514 : float pFC[IVAS_MAX_NUM_BANDS], PR_minmax[2];
515 : ivas_error error;
516 :
517 48090 : ivas_sba_get_spar_hoa_md_flag( sba_order, hDecoderConfig->ivas_total_brate, &hMdDec->spar_hoa_md_flag, &hMdDec->spar_hoa_dirac2spar_md_flag );
518 :
519 48090 : ivas_sba_get_spar_hoa_ch_ind( num_channels, hDecoderConfig->ivas_total_brate, hMdDec->HOA_md_ind );
520 :
521 48090 : hMdDec->spar_md.num_bands = ( hMdDec->spar_hoa_md_flag ) ? IVAS_MAX_NUM_BANDS : min( IVAS_MAX_NUM_BANDS, SPAR_DIRAC_SPLIT_START_BAND );
522 :
523 48090 : ivas_spar_set_bitrate_config( &hMdDec->spar_md_cfg, hMdDec->table_idx, hMdDec->spar_md.num_bands, hMdDec->spar_hoa_dirac2spar_md_flag, 0, 0, 0 );
524 :
525 48090 : nchan_transport = hMdDec->spar_md_cfg.nchan_transport;
526 :
527 : /* get FB coefficients */
528 625170 : for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
529 : {
530 577080 : pFC[i] = ivas_fb_fcs_12band_1ms[i] * hDecoderConfig->output_Fs * 0.5f;
531 : }
532 :
533 48090 : if ( ( error = ivas_spar_set_dec_config( hMdDec, nchan_transport, pFC ) ) != IVAS_ERR_OK )
534 : {
535 0 : return error;
536 : }
537 :
538 48090 : if ( nchan_transport != 2 && ( ( hMdDec->spar_md_cfg.remix_unmix_order == 2 ) || ( hMdDec->spar_md_cfg.remix_unmix_order == 1 ) ) )
539 : {
540 0 : return IVAS_ERR_INTERNAL;
541 : }
542 :
543 : /* DTX quant init */
544 48090 : PR_minmax[0] = hMdDec->spar_md_cfg.quant_strat[0].PR.min;
545 48090 : PR_minmax[1] = hMdDec->spar_md_cfg.quant_strat[0].PR.max;
546 48090 : ivas_spar_quant_dtx_init( &hMdDec->spar_md, PR_minmax );
547 :
548 48090 : ivas_spar_arith_coeffs_com_init( &hMdDec->arith_coeffs, &hMdDec->spar_md_cfg, hMdDec->table_idx, DEC );
549 48090 : ivas_spar_huff_coeffs_com_init( &hMdDec->huff_coeffs, &hMdDec->spar_md_cfg, hMdDec->table_idx, DEC );
550 :
551 48090 : hMdDec->spar_md_cfg.prev_quant_idx = -1;
552 :
553 : /* initialize PLC state */
554 48090 : set_s( hMdDec->valid_bands, 0, IVAS_MAX_NUM_BANDS );
555 48090 : set_s( hMdDec->base_band_age, 0, IVAS_MAX_NUM_BANDS );
556 48090 : set_s( hMdDec->base_band_coeffs_age, 0, IVAS_MAX_NUM_BANDS );
557 48090 : hMdDec->spar_plc_num_lost_frames = 0;
558 48090 : hMdDec->spar_plc_enable_fadeout_flag = 1;
559 48090 : hMdDec->dtx_md_smoothing_cntr = 1;
560 :
561 48090 : ivas_clear_band_coeffs( hMdDec->spar_md.band_coeffs, IVAS_MAX_NUM_BANDS );
562 48090 : ivas_clear_band_coeffs( hMdDec->band_coeffs_prev, IVAS_MAX_NUM_BANDS );
563 48090 : ivas_clear_band_coeff_idx( hMdDec->spar_md.band_coeffs_idx, IVAS_MAX_NUM_BANDS );
564 48090 : ivas_clear_band_coeff_idx( hMdDec->spar_md_prev.band_coeffs_idx, IVAS_MAX_NUM_BANDS );
565 48090 : ivas_clear_band_coeff_idx( hMdDec->spar_md_prev.band_coeffs_idx_mapped, IVAS_MAX_NUM_BANDS );
566 :
567 48090 : hMdDec->spar_md.dtx_vad = 0;
568 48090 : hMdDec->td_decorr_flag = 1;
569 :
570 48090 : set_f( hMdDec->spar_md.en_ratio_slow, 0.0f, IVAS_MAX_NUM_BANDS );
571 48090 : set_f( hMdDec->spar_md.ref_pow_slow, 0.0f, IVAS_MAX_NUM_BANDS );
572 :
573 48090 : set_zero( hMdDec->smooth_fac, IVAS_MAX_NUM_BANDS );
574 625170 : for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
575 : {
576 577080 : set_zero( hMdDec->smooth_buf[i], 2 * SBA_DIRAC_NRG_SMOOTH_LONG + 1 );
577 : }
578 :
579 577080 : for ( i = 0; i < IVAS_SPAR_MAX_CH; i++ )
580 : {
581 6347880 : for ( j = 0; j < IVAS_SPAR_MAX_CH; j++ )
582 : {
583 5818890 : set_zero( hMdDec->mixer_mat_prev2[i][j], IVAS_MAX_NUM_BANDS );
584 : }
585 : }
586 48090 : hMdDec->first_valid_frame = 1;
587 :
588 48090 : hMdDec->spar_md.res_ind = 0;
589 48090 : hMdDec->spar_md.prior_dyn_active_w_flag = 0;
590 :
591 48090 : return IVAS_ERR_OK;
592 : }
593 :
594 :
595 : /*-----------------------------------------------------------------------------------------*
596 : * Function ivas_spar_set_dec_config()
597 : *
598 : * Set configuration for SPAR MD decoder
599 : *-----------------------------------------------------------------------------------------*/
600 :
601 48090 : static ivas_error ivas_spar_set_dec_config(
602 : ivas_spar_md_dec_state_t *hMdDec,
603 : const int16_t nchan_transport,
604 : float *pFC )
605 : {
606 : int16_t i, j, nchan, dmx_ch;
607 :
608 160218 : for ( i = 0; i < nchan_transport; i++ )
609 : {
610 112128 : hMdDec->spar_md_cfg.max_freq_per_chan[i] = ivas_spar_br_table_consts[hMdDec->table_idx].fpcs;
611 : }
612 :
613 48090 : nchan = ivas_sba_get_nchan_metadata( ivas_spar_br_table_consts[hMdDec->table_idx].sba_order, ivas_spar_br_table_consts[hMdDec->table_idx].ivas_total_brate );
614 :
615 48090 : switch ( nchan )
616 : {
617 41505 : case 4: /* FOA_CHANNELS */
618 41505 : hMdDec->num_decorr = IVAS_TD_DECORR_OUT_3CH;
619 41505 : break;
620 624 : case 9: /* IVAS_HOA_2_CH */
621 624 : hMdDec->num_decorr = IVAS_TD_DECORR_OUT_5CH;
622 624 : break;
623 1164 : case 6: /* IVAS_HOA_2_CH */
624 1164 : hMdDec->num_decorr = IVAS_TD_DECORR_OUT_2CH;
625 1164 : break;
626 2970 : case 8: /* IVAS_HOA_3_CH */
627 2970 : hMdDec->num_decorr = IVAS_TD_DECORR_OUT_4CH;
628 2970 : break;
629 : }
630 :
631 48090 : hMdDec->spar_md_cfg.num_umx_chs = nchan;
632 :
633 48090 : dmx_ch = 0;
634 625170 : for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
635 : {
636 577080 : dmx_ch = 0;
637 1922616 : for ( j = 0; j < nchan_transport; j++ )
638 : {
639 1345536 : if ( pFC[i] < hMdDec->spar_md_cfg.max_freq_per_chan[j] )
640 : {
641 1345536 : dmx_ch += 1;
642 : }
643 : }
644 :
645 577080 : hMdDec->spar_md_cfg.num_dmx_chans_per_band[i] = hMdDec->spar_md_cfg.nchan_transport;
646 577080 : hMdDec->spar_md_cfg.num_decorr_per_band[i] = nchan - hMdDec->spar_md_cfg.nchan_transport;
647 : }
648 :
649 48090 : hMdDec->spar_md_cfg.nchan_transport = dmx_ch;
650 :
651 48090 : return IVAS_ERR_OK;
652 : }
653 :
654 :
655 : /*-----------------------------------------------------------------------------------------*
656 : * Function ivas_dec_mono_sba_handling()
657 : *
658 : *
659 : *-----------------------------------------------------------------------------------------*/
660 :
661 5989479 : static void ivas_dec_mono_sba_handling(
662 : Decoder_Struct *st_ivas /* i/o: IVAS decoder handle */
663 : )
664 : {
665 : int16_t mono_flag, b, block;
666 :
667 5989479 : mono_flag = 1;
668 :
669 26743143 : for ( b = 0; b < st_ivas->hQMetaData->q_direction[0].cfg.nbands; b++ )
670 : {
671 103768320 : for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; ++block )
672 : {
673 83014656 : float azimuth = st_ivas->hQMetaData->q_direction[0].band_data[b].azimuth[block];
674 83014656 : float elevation = st_ivas->hQMetaData->q_direction[0].band_data[b].azimuth[block];
675 83014656 : float energy_ratio = st_ivas->hQMetaData->q_direction[0].band_data[0].energy_ratio[block];
676 83014656 : if (
677 21263028 : ( azimuth != 0.0f ) ||
678 21263028 : ( elevation != 0.0f ) ||
679 : ( energy_ratio > 0.15f ) ) /* 0.15f is just above the lowest quantised value. */
680 : {
681 72643116 : mono_flag = 0;
682 : }
683 : }
684 : }
685 :
686 : /* Combine the SPAR prediction coefs flag with the azimuth, elevation and energy ratio flag.*/
687 5989479 : mono_flag = mono_flag && ivas_spar_chk_zero_coefs( st_ivas );
688 :
689 5989479 : if ( mono_flag )
690 : {
691 : /* Set Energy Ratio values to be zero */
692 0 : for ( b = 0; b < st_ivas->hQMetaData->q_direction[0].cfg.nbands; b++ )
693 : {
694 0 : set_zero( st_ivas->hQMetaData->q_direction[0].band_data[b].energy_ratio, MAX_PARAM_SPATIAL_SUBFRAMES );
695 : }
696 0 : if ( st_ivas->hDirAC != NULL )
697 : {
698 0 : for ( block = 0; block < st_ivas->hSpatParamRendCom->dirac_md_buffer_length; ++block )
699 : {
700 : /* Set directional Energy Ratio values to be zero */
701 0 : set_zero( st_ivas->hSpatParamRendCom->energy_ratio1[block], st_ivas->hSpatParamRendCom->num_freq_bands );
702 0 : if ( st_ivas->hQMetaData->no_directions == 2 )
703 : {
704 0 : set_zero( st_ivas->hSpatParamRendCom->energy_ratio2[block], st_ivas->hSpatParamRendCom->num_freq_bands );
705 : }
706 : /* Set Diffuseness values to be 1.0 */
707 0 : set_f( st_ivas->hSpatParamRendCom->diffuseness_vector[block], 1.0f, st_ivas->hSpatParamRendCom->num_freq_bands );
708 : }
709 : }
710 : }
711 :
712 5989479 : return;
713 : }
714 :
715 :
716 : /*-----------------------------------------------------------------------------------------*
717 : * Function ivas_spar_md_dec_process()
718 : *
719 : * SPAR Meta Data decoder process
720 : *-----------------------------------------------------------------------------------------*/
721 :
722 5989479 : void ivas_spar_md_dec_process(
723 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
724 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling */
725 : const int16_t num_bands_out, /* i : number of output bands */
726 : const int16_t sba_order /* i : Ambisonic (SBA) order */
727 : )
728 : {
729 : int16_t j, k, b, bw, dtx_vad, nB, i_ts;
730 : ivas_spar_md_dec_state_t *hMdDec;
731 : int16_t num_md_chs;
732 : int16_t num_md_sub_frames;
733 : int16_t dyn_active_w_flag;
734 : int16_t active_w_vlbr;
735 :
736 5989479 : hMdDec = st_ivas->hSpar->hMdDec;
737 :
738 5989479 : active_w_vlbr = ( st_ivas->hDecoderConfig->ivas_total_brate < IVAS_24k4 ) ? 1 : 0;
739 :
740 5989479 : num_md_chs = ivas_sba_get_nchan_metadata( sba_order, st_ivas->hDecoderConfig->ivas_total_brate );
741 :
742 5989479 : num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( sba_order, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate );
743 :
744 5989479 : if ( hMdDec->spar_md_cfg.nchan_transport > 1 && hMdDec->spar_md_cfg.nchan_transport <= 3 )
745 : {
746 2683212 : hMdDec->spar_md.res_ind = 0;
747 2683212 : dyn_active_w_flag = get_next_indice( st0, 1 );
748 2683212 : if ( dyn_active_w_flag == 1 )
749 : {
750 0 : if ( hMdDec->spar_md_cfg.nchan_transport == 2 )
751 : {
752 0 : hMdDec->spar_md.res_ind = get_next_indice( st0, 1 );
753 0 : hMdDec->spar_md.res_ind += hMdDec->spar_md_cfg.nchan_transport;
754 : }
755 0 : else if ( hMdDec->spar_md_cfg.nchan_transport == 3 )
756 : {
757 0 : hMdDec->spar_md.res_ind = remix_order_set[hMdDec->spar_md_cfg.remix_unmix_order][hMdDec->spar_md_cfg.nchan_transport];
758 : }
759 : }
760 : }
761 : else
762 : {
763 3306267 : dyn_active_w_flag = 0;
764 3306267 : if ( hMdDec->spar_md_cfg.nchan_transport == FOA_CHANNELS )
765 : {
766 1477263 : get_next_indice( st0, 1 );
767 : }
768 : }
769 :
770 5989479 : ivas_spar_dec_parse_md_bs( hMdDec, st0, &nB, &bw, &dtx_vad, st_ivas->hDecoderConfig->ivas_total_brate,
771 5989479 : st_ivas->hQMetaData->sba_inactive_mode );
772 :
773 5989479 : assert( nB == hMdDec->spar_md.num_bands );
774 5989479 : assert( bw == 1 );
775 5989479 : ivas_spar_md_fill_invalid_bandcoeffs(
776 : hMdDec->spar_md.band_coeffs,
777 : hMdDec->band_coeffs_prev,
778 5989479 : &hMdDec->valid_bands[0],
779 : &hMdDec->base_band_coeffs_age[0],
780 : &hMdDec->first_valid_frame,
781 : nB );
782 :
783 5989479 : ivas_dec_mono_sba_handling( st_ivas );
784 :
785 : /* SPAR to DirAC conversion */
786 5989479 : if ( hMdDec->spar_hoa_dirac2spar_md_flag == 1 )
787 : {
788 5748321 : ivas_spar_to_dirac( st_ivas, hMdDec, dtx_vad, num_bands_out, bw, dyn_active_w_flag );
789 : }
790 :
791 : /* set correct number of bands*/
792 5989479 : nB = IVAS_MAX_NUM_BANDS;
793 : #ifdef DEBUG_LBR_SBA
794 : /* Dumping SPAR Coefficients */
795 : char f_name[100];
796 : int16_t nbands = 4; // 6 total, just looking at SPAR
797 : int16_t num_subframes = 1;
798 : int16_t num_elements = 6;
799 : int16_t num_block_group = 1;
800 : int16_t byte_size = sizeof( float );
801 :
802 : sprintf( f_name, "SBA_MD_values_dec.bin" );
803 : ( frame == 0 ) ? dbgwrite( &nbands, sizeof( nbands ), 1, 1, f_name ) : false;
804 : ( frame == 0 ) ? dbgwrite( &num_elements, sizeof( num_elements ), 1, 1, f_name ) : false;
805 : ( frame == 0 ) ? dbgwrite( &num_subframes, sizeof( num_subframes ), 1, 1, f_name ) : false;
806 : ( frame == 0 ) ? dbgwrite( &num_block_group, sizeof( num_block_group ), 1, 1, f_name ) : false;
807 : ( frame == 0 ) ? dbgwrite( &byte_size, sizeof( byte_size ), 1, 1, f_name ) : false;
808 :
809 : for ( b = 0; b < nbands; b++ )
810 : {
811 : for ( int16_t sf = 0; sf < num_subframes; sf++ )
812 : {
813 : for ( int16_t bl = 0; bl < num_block_group; bl++ )
814 : {
815 : dbgwrite( &hMdDec->spar_md.band_coeffs[b].pred_re[0], byte_size, 1, 1, f_name );
816 : dbgwrite( &hMdDec->spar_md.band_coeffs[b].pred_re[1], byte_size, 1, 1, f_name );
817 : dbgwrite( &hMdDec->spar_md.band_coeffs[b].pred_re[2], byte_size, 1, 1, f_name );
818 : dbgwrite( &hMdDec->spar_md.band_coeffs[b].P_re[0], byte_size, 1, 1, f_name );
819 : dbgwrite( &hMdDec->spar_md.band_coeffs[b].P_re[1], byte_size, 1, 1, f_name );
820 : dbgwrite( &hMdDec->spar_md.band_coeffs[b].P_re[2], byte_size, 1, 1, f_name );
821 : // fprintf(stdout, "%f\t%f\t%f\t%d\t%d\n", dirac_md_kbps, spar_md_kbps, sba_md_kbps, qsi, code_strat );
822 : }
823 : }
824 : }
825 : #endif
826 :
827 : /* expand DirAC MD to all time slots */
828 22403715 : for ( i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
829 : {
830 151168896 : for ( b = 0; b < hMdDec->spar_md.num_bands; b++ )
831 : {
832 1482301260 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
833 : {
834 1347546600 : hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].pred_re[j] = hMdDec->spar_md.band_coeffs[b].pred_re[j];
835 : }
836 :
837 1078037280 : for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
838 : {
839 3773130480 : for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
840 : {
841 2829847860 : hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].C_re[j][k] = hMdDec->spar_md.band_coeffs[b].C_re[j][k];
842 : }
843 : }
844 :
845 1482301260 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
846 : {
847 1347546600 : hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].P_re[j] = hMdDec->spar_md.band_coeffs[b].P_re[j];
848 : }
849 : }
850 : }
851 :
852 5989479 : ivas_get_spar_matrices( hMdDec, num_bands_out, num_md_sub_frames, bw, dtx_vad, nB, num_md_chs, active_w_vlbr, dyn_active_w_flag );
853 :
854 : #ifdef DEBUG_SPAR_DIRAC_WRITE_OUT_PRED_PARS
855 : {
856 : static FILE *fid = 0;
857 : int16_t band = 9;
858 : if ( !fid )
859 : {
860 : fid = fopen( "pred_coeffs_dec.txt", "wt" );
861 : }
862 : fprintf( fid, "%.6f\n", hMdDec->mixer_mat[1][0][band] );
863 : }
864 : #endif
865 5989479 : ivas_spar_md_fill_invalid_bands( &hMdDec->spar_coeffs, &hMdDec->spar_coeffs_prev, &hMdDec->valid_bands[0], &hMdDec->base_band_age[0], num_bands_out, num_md_chs, num_md_sub_frames );
866 :
867 :
868 5989479 : hMdDec->dtx_md_smoothing_cntr = 1;
869 :
870 5989479 : return;
871 : }
872 :
873 :
874 : /*-----------------------------------------------------------------------------------------*
875 : * Function ivas_spar_chk_zero_coefs()
876 : *
877 : * Check for zeroed SPAR coefficients
878 : *-----------------------------------------------------------------------------------------*/
879 :
880 20835 : int16_t ivas_spar_chk_zero_coefs(
881 : Decoder_Struct *st_ivas /* i/o: IVAS decoder handle */
882 : )
883 : {
884 : int16_t j, k, b;
885 : ivas_spar_md_dec_state_t *hMdDec;
886 20835 : int16_t mono = 1;
887 : int16_t ndec, ndm;
888 :
889 20835 : hMdDec = st_ivas->hSpar->hMdDec;
890 20835 : ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
891 20835 : ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
892 :
893 187515 : for ( b = 0; b < min( hMdDec->spar_md.num_bands, SPAR_DIRAC_SPLIT_START_BAND ); b++ )
894 : {
895 669888 : for ( j = 0; j < ndm + ndec - 1; j++ )
896 : {
897 503208 : if ( hMdDec->spar_md.band_coeffs[b].pred_re[j] != 0.0f )
898 : {
899 343794 : mono = 0;
900 : }
901 : }
902 621984 : for ( j = 0; j < ndec; j++ )
903 : {
904 504120 : for ( k = 0; k < ndm - 1; k++ )
905 : {
906 48816 : if ( hMdDec->spar_md.band_coeffs[b].C_re[j][k] != 0.0f )
907 : {
908 10647 : mono = 0;
909 : }
910 : }
911 : }
912 621984 : for ( j = 0; j < ndec; j++ )
913 : {
914 455304 : if ( hMdDec->spar_md.band_coeffs[b].P_re[j] != 0.0f )
915 : {
916 412614 : mono = 0;
917 : }
918 : }
919 : }
920 :
921 20835 : return mono;
922 : }
923 :
924 :
925 : /*-----------------------------------------------------------------------------------------*
926 : * Function ivas_spar_smooth_md_dtx()
927 : *
928 : * Smooth MD during no data frame during DTX
929 : *-----------------------------------------------------------------------------------------*/
930 :
931 9465 : void ivas_spar_smooth_md_dtx(
932 : ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle */
933 : const int16_t num_bands_out, /* i : number of output bands */
934 : const int16_t num_md_sub_frames /* i : number of metadata subframes */
935 : )
936 : {
937 : int16_t j, k, b, dmx_ch;
938 : float ramp, tar, prev, new_val;
939 :
940 9465 : ramp = (float) hMdDec->dtx_md_smoothing_cntr / IVAS_DEFAULT_DTX_CNG_RAMP;
941 :
942 119277 : for ( b = 0; b < num_bands_out; b++ )
943 : {
944 109812 : dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[b];
945 :
946 439248 : for ( j = 1; j < FOA_CHANNELS; j++ )
947 : {
948 1222056 : for ( k = dmx_ch; k < FOA_CHANNELS; k++ )
949 : {
950 892620 : prev = hMdDec->spar_coeffs_prev.P_re[j][k][b];
951 892620 : tar = hMdDec->spar_coeffs_tar.P_re[j][k][b];
952 892620 : new_val = prev + ( ramp * ( tar - prev ) );
953 892620 : hMdDec->spar_coeffs.P_re[j][k][b] = new_val;
954 : }
955 : }
956 :
957 549060 : for ( j = 0; j < FOA_CHANNELS; j++ )
958 : {
959 1006080 : for ( k = 0; k < dmx_ch; k++ )
960 : {
961 566832 : prev = hMdDec->spar_coeffs_prev.C_re[j][k][b];
962 566832 : tar = hMdDec->spar_coeffs_tar.C_re[j][k][b];
963 566832 : new_val = prev + ( ramp * ( tar - prev ) );
964 566832 : hMdDec->spar_coeffs.C_re[j][k][b] = new_val;
965 : }
966 : }
967 : }
968 :
969 : /* expand MD to all time slots */
970 36726 : for ( int16_t i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
971 : {
972 343089 : for ( b = 0; b < num_bands_out; b++ )
973 : {
974 315828 : dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[b];
975 :
976 1263312 : for ( j = 1; j < FOA_CHANNELS; j++ )
977 : {
978 3502872 : for ( k = dmx_ch; k < FOA_CHANNELS; k++ )
979 :
980 : {
981 2555388 : hMdDec->spar_coeffs.P_re[j][k][b + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.P_re[j][k][b];
982 : }
983 : }
984 :
985 1579140 : for ( j = 0; j < FOA_CHANNELS; j++ )
986 : {
987 2909376 : for ( k = 0; k < dmx_ch; k++ )
988 : {
989 1646064 : hMdDec->spar_coeffs.C_re[j][k][b + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.C_re[j][k][b];
990 : }
991 : }
992 : }
993 : }
994 :
995 9465 : hMdDec->dtx_md_smoothing_cntr = min( hMdDec->dtx_md_smoothing_cntr + 1, IVAS_DEFAULT_DTX_CNG_RAMP );
996 :
997 9465 : return;
998 : }
999 :
1000 :
1001 : /*-----------------------------------------------------------------------------------------*
1002 : * Function ivas_spar_setup_md_smoothing()
1003 : *
1004 : * Set up smoothing of SPAR MD when SID update frame is received
1005 : *-----------------------------------------------------------------------------------------*/
1006 :
1007 516 : void ivas_spar_setup_md_smoothing(
1008 : ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle */
1009 : const int16_t num_bands_out, /* i : number of output bands */
1010 : const int16_t num_md_sub_frames /* i : number of metadata subframes */
1011 : )
1012 : {
1013 : /* copy the coeffs */
1014 : int16_t num_channels, i, j, k;
1015 :
1016 516 : num_channels = hMdDec->spar_md_cfg.num_umx_chs;
1017 :
1018 2580 : for ( i = 0; i < num_channels; i++ )
1019 : {
1020 10320 : for ( j = 0; j < num_channels; j++ )
1021 : {
1022 107328 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1023 : {
1024 99072 : hMdDec->spar_coeffs_prev.C_re[i][j][k] = hMdDec->spar_coeffs_tar.C_re[i][j][k];
1025 : }
1026 : }
1027 : }
1028 :
1029 2580 : for ( i = 0; i < num_channels; i++ )
1030 : {
1031 10320 : for ( j = 0; j < num_channels; j++ )
1032 : {
1033 107328 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1034 : {
1035 99072 : hMdDec->spar_coeffs_prev.P_re[i][j][k] = hMdDec->spar_coeffs_tar.P_re[i][j][k];
1036 : }
1037 : }
1038 : }
1039 :
1040 2580 : for ( i = 0; i < num_channels; i++ )
1041 : {
1042 10320 : for ( j = 0; j < num_channels; j++ )
1043 : {
1044 107328 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1045 : {
1046 99072 : hMdDec->spar_coeffs_tar.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
1047 : }
1048 : }
1049 : }
1050 :
1051 2580 : for ( i = 0; i < num_channels; i++ )
1052 : {
1053 10320 : for ( j = 0; j < num_channels; j++ )
1054 : {
1055 107328 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1056 : {
1057 99072 : hMdDec->spar_coeffs_tar.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
1058 : }
1059 : }
1060 : }
1061 :
1062 516 : ivas_spar_smooth_md_dtx( hMdDec, num_bands_out, num_md_sub_frames );
1063 :
1064 516 : return;
1065 : }
1066 :
1067 :
1068 : /*-----------------------------------------------------------------------------------------*
1069 : * Function ivas_spar_update_md_hist()
1070 : *
1071 : * Update previous and target MD
1072 : *-----------------------------------------------------------------------------------------*/
1073 :
1074 5988963 : void ivas_spar_update_md_hist(
1075 : ivas_spar_md_dec_state_t *hMdDec /* i/o: SPAR MD decoder handle */
1076 : )
1077 : {
1078 : int16_t num_channels, i, j, k;
1079 :
1080 5988963 : num_channels = hMdDec->spar_md_cfg.num_umx_chs;
1081 :
1082 32401875 : for ( i = 0; i < num_channels; i++ )
1083 : {
1084 155369142 : for ( j = 0; j < num_channels; j++ )
1085 : {
1086 1676430990 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1087 : {
1088 1547474760 : hMdDec->spar_coeffs_prev.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
1089 : }
1090 : }
1091 : }
1092 :
1093 32401875 : for ( i = 0; i < num_channels; i++ )
1094 : {
1095 155369142 : for ( j = 0; j < num_channels; j++ )
1096 : {
1097 1676430990 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1098 : {
1099 1547474760 : hMdDec->spar_coeffs_prev.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
1100 : }
1101 : }
1102 : }
1103 :
1104 32401875 : for ( i = 0; i < num_channels; i++ )
1105 : {
1106 155369142 : for ( j = 0; j < num_channels; j++ )
1107 : {
1108 1676430990 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1109 : {
1110 1547474760 : hMdDec->spar_coeffs_tar.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
1111 : }
1112 : }
1113 : }
1114 :
1115 32401875 : for ( i = 0; i < num_channels; i++ )
1116 : {
1117 155369142 : for ( j = 0; j < num_channels; j++ )
1118 : {
1119 1676430990 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1120 : {
1121 1547474760 : hMdDec->spar_coeffs_tar.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
1122 : }
1123 : }
1124 : }
1125 :
1126 5988963 : return;
1127 : }
1128 :
1129 :
1130 : /*-----------------------------------------------------------------------------------------*
1131 : * Function ivas_get_spar_matrices()
1132 : *
1133 : * Get SPAR matrices
1134 : *-----------------------------------------------------------------------------------------*/
1135 :
1136 5989479 : static void ivas_get_spar_matrices(
1137 : ivas_spar_md_dec_state_t *hMdDec,
1138 : const int16_t num_bands_out,
1139 : const int16_t n_ts,
1140 : const int16_t bw,
1141 : const int16_t dtx_vad,
1142 : const int16_t nB,
1143 : const int16_t numch_out,
1144 : const int16_t active_w_vlbr,
1145 : const int16_t dyn_active_w_flag )
1146 : {
1147 : int16_t num_bands, dmx_ch, split_band;
1148 : int16_t i, j, k, m, b, i_ts, active_w;
1149 : const int16_t *order;
1150 : float active_w_dm_fac, re;
1151 :
1152 :
1153 5989479 : num_bands = num_bands_out;
1154 5989479 : order = remix_order_set[hMdDec->spar_md_cfg.remix_unmix_order];
1155 :
1156 5989479 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
1157 5989479 : if ( split_band >= IVAS_MAX_NUM_BANDS )
1158 : {
1159 : /*store previous 4x4 parameters for linear interpolation to current*/
1160 0 : for ( i = 0; i < numch_out; i++ )
1161 : {
1162 0 : for ( j = 0; j < numch_out; j++ )
1163 : {
1164 0 : for ( b = 0; b < num_bands; b++ )
1165 : {
1166 0 : hMdDec->mixer_mat_prev[0][i][j][b] = hMdDec->mixer_mat[i][j][b];
1167 : }
1168 : }
1169 : }
1170 : }
1171 :
1172 5989479 : if ( bw == IVAS_RED_BAND_FACT )
1173 : {
1174 0 : num_bands = num_bands >> 1;
1175 : }
1176 :
1177 5989479 : active_w = ( dyn_active_w_flag == 1 ) || ( hMdDec->spar_md_cfg.active_w == 1 );
1178 5989479 : active_w_dm_fac = ( dtx_vad == 0 ) ? IVAS_ACTIVEW_DM_F_SCALE_DTX : ( ( active_w_vlbr ) ? IVAS_ACTIVEW_DM_F_SCALE_VLBR : IVAS_ACTIVEW_DM_F_SCALE );
1179 :
1180 28393194 : for ( i_ts = 0; i_ts < n_ts; i_ts++ )
1181 : {
1182 117112635 : for ( i = 0; i < numch_out; i++ )
1183 : {
1184 516337638 : for ( j = 0; j < numch_out; j++ )
1185 : {
1186 421628718 : set_zero( &hMdDec->spar_coeffs.C_re[i][j][i_ts * IVAS_MAX_NUM_BANDS], IVAS_MAX_NUM_BANDS );
1187 421628718 : set_zero( &hMdDec->spar_coeffs.P_re[i][j][i_ts * IVAS_MAX_NUM_BANDS], IVAS_MAX_NUM_BANDS );
1188 : }
1189 : }
1190 22403715 : num_bands = min( num_bands, nB );
1191 :
1192 284762805 : for ( b = 0; b < num_bands; b++ )
1193 : {
1194 : float tmp_C1_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
1195 : float tmp_C2_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
1196 : float tmp_dm_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
1197 :
1198 262359090 : dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bw * b];
1199 :
1200 1372541322 : for ( j = 0; j < numch_out; j++ )
1201 : {
1202 1110182232 : set_zero( tmp_C1_re[j], numch_out );
1203 1110182232 : set_zero( tmp_C2_re[j], numch_out );
1204 1110182232 : set_zero( tmp_dm_re[j], numch_out );
1205 :
1206 1110182232 : tmp_C1_re[j][j] = 1.0f;
1207 1110182232 : tmp_C2_re[j][j] = 1.0f;
1208 1110182232 : tmp_dm_re[j][j] = 1.0f;
1209 : }
1210 :
1211 1110182232 : for ( j = 1; j < numch_out; j++ )
1212 : {
1213 847823142 : tmp_C1_re[j][0] = hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].pred_re[j - 1];
1214 : }
1215 :
1216 262359090 : if ( active_w == 1 )
1217 : {
1218 300877896 : for ( j = 1; j < numch_out; j++ )
1219 : {
1220 225658422 : tmp_C2_re[0][j] = active_w_dm_fac * -hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].pred_re[j - 1];
1221 : }
1222 :
1223 75219474 : IVAS_RMULT_FLOAT( tmp_C2_re[0][1], tmp_C1_re[1][0], re );
1224 75219474 : tmp_dm_re[0][0] = 1 + re;
1225 :
1226 75219474 : IVAS_RMULT_FLOAT( tmp_C2_re[0][2], tmp_C1_re[2][0], re );
1227 75219474 : tmp_dm_re[0][0] += re;
1228 :
1229 75219474 : IVAS_RMULT_FLOAT( tmp_C2_re[0][3], tmp_C1_re[3][0], re );
1230 75219474 : tmp_dm_re[0][0] += re;
1231 :
1232 75219474 : if ( dyn_active_w_flag == 1 )
1233 : {
1234 0 : tmp_dm_re[0][0] *= IVAS_SPAR_DYN_ACTIVEW_THRESH;
1235 : }
1236 :
1237 75219474 : tmp_dm_re[0][1] = tmp_C2_re[0][1];
1238 :
1239 75219474 : tmp_dm_re[0][2] = tmp_C2_re[0][2];
1240 :
1241 75219474 : tmp_dm_re[0][3] = tmp_C2_re[0][3];
1242 :
1243 75219474 : tmp_dm_re[1][0] = tmp_C1_re[1][0];
1244 :
1245 75219474 : tmp_dm_re[2][0] = tmp_C1_re[2][0];
1246 :
1247 75219474 : tmp_dm_re[3][0] = tmp_C1_re[3][0];
1248 :
1249 75219474 : if ( hMdDec->spar_md_cfg.remix_unmix_order != 3 )
1250 : {
1251 75219474 : ivas_mat_col_rearrange( tmp_dm_re, order, i_ts, hMdDec->mixer_mat, b, numch_out );
1252 : }
1253 : }
1254 : else
1255 : {
1256 187139616 : if ( hMdDec->spar_md_cfg.remix_unmix_order != 3 )
1257 : {
1258 187139616 : ivas_mat_col_rearrange( tmp_C1_re, order, i_ts, hMdDec->mixer_mat, b, numch_out );
1259 : }
1260 : }
1261 :
1262 262359090 : if ( dmx_ch > 0 )
1263 : {
1264 : float tmpC_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
1265 : float tmpP_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH];
1266 :
1267 1372541322 : for ( j = 0; j < numch_out; j++ )
1268 : {
1269 1110182232 : set_zero( tmpC_re[j], numch_out );
1270 1110182232 : set_zero( tmpP_re[j], numch_out );
1271 : }
1272 :
1273 1372541322 : for ( j = 0; j < numch_out; j++ )
1274 : {
1275 1110182232 : set_zero( tmpC_re[j], numch_out );
1276 : }
1277 :
1278 887276364 : for ( k = 0; k < dmx_ch; k++ )
1279 : {
1280 624917274 : tmpC_re[k][k] = 1;
1281 : }
1282 :
1283 747624048 : for ( j = dmx_ch; j < numch_out; j++ )
1284 : {
1285 920173326 : for ( k = 1; k < dmx_ch; k++ )
1286 : {
1287 434908368 : tmpC_re[j][k] = hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].C_re[j - dmx_ch][k - 1];
1288 : }
1289 : }
1290 :
1291 747624048 : for ( j = dmx_ch; j < numch_out; j++ )
1292 : {
1293 1774387344 : for ( k = dmx_ch; k < numch_out; k++ )
1294 : {
1295 1289122386 : if ( ( j - dmx_ch ) == ( k - dmx_ch ) )
1296 : {
1297 485264958 : tmpP_re[j][k] = hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].P_re[k - dmx_ch];
1298 : }
1299 : else
1300 : {
1301 803857428 : tmpP_re[j][k] = 0;
1302 : }
1303 : }
1304 : }
1305 :
1306 1110182232 : for ( j = 1; j < numch_out; j++ )
1307 : {
1308 2571853896 : for ( k = dmx_ch; k < numch_out; k++ )
1309 : {
1310 10792743954 : for ( m = 0; m < numch_out; m++ )
1311 : {
1312 9068713200 : IVAS_RMULT_FLOAT( hMdDec->mixer_mat[j][m][b + i_ts * IVAS_MAX_NUM_BANDS], tmpP_re[m][k], re );
1313 9068713200 : hMdDec->spar_coeffs.P_re[j][k][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] += re;
1314 : }
1315 : }
1316 : }
1317 :
1318 1372541322 : for ( j = 0; j < numch_out; j++ )
1319 : {
1320 3852834816 : for ( k = 0; k < dmx_ch; k++ )
1321 : {
1322 15758140392 : for ( m = 0; m < numch_out; m++ )
1323 : {
1324 13015487808 : IVAS_RMULT_FLOAT( hMdDec->mixer_mat[j][m][b + i_ts * IVAS_MAX_NUM_BANDS], tmpC_re[m][k], re );
1325 13015487808 : hMdDec->spar_coeffs.C_re[j][k][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] += re;
1326 : }
1327 : }
1328 : }
1329 :
1330 262359090 : hMdDec->spar_coeffs.C_re[0][0][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] =
1331 262359090 : max( 0, hMdDec->spar_coeffs.C_re[0][0][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] );
1332 : }
1333 : }
1334 :
1335 : /* band mixing */
1336 22403715 : if ( bw == IVAS_RED_BAND_FACT )
1337 : {
1338 0 : for ( b = 0; b < num_bands_out; b = b + bw )
1339 : {
1340 0 : dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[b];
1341 0 : for ( j = 0; j < numch_out; j++ )
1342 : {
1343 0 : for ( k = dmx_ch; k < numch_out; k++ )
1344 : {
1345 0 : hMdDec->spar_coeffs.P_re[j][k][( b + 1 ) + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.P_re[j][k][b + i_ts * IVAS_MAX_NUM_BANDS];
1346 : }
1347 : }
1348 :
1349 0 : for ( j = 0; j < numch_out; j++ )
1350 : {
1351 0 : for ( k = 0; k < dmx_ch; k++ )
1352 : {
1353 0 : hMdDec->spar_coeffs.C_re[j][k][( b + 1 ) + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.C_re[j][k][b + i_ts * IVAS_MAX_NUM_BANDS];
1354 : }
1355 : }
1356 : }
1357 : }
1358 : }
1359 :
1360 5989479 : return;
1361 : }
1362 :
1363 :
1364 : /*-----------------------------------------------------------------------------------------*
1365 : * Function ivas_mat_col_rearrange()
1366 : *
1367 : * reorders the input matrix based on order
1368 : *-----------------------------------------------------------------------------------------*/
1369 :
1370 262359090 : static void ivas_mat_col_rearrange(
1371 : float in_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
1372 : const int16_t order[IVAS_SPAR_MAX_CH],
1373 : const int16_t i_ts,
1374 : float ***mixer_mat,
1375 : const int16_t bands,
1376 : const int16_t num_ch )
1377 : {
1378 : int16_t i, j, idx;
1379 :
1380 1372541322 : for ( i = 0; i < num_ch; i++ )
1381 : {
1382 1110182232 : idx = order[i];
1383 :
1384 6062130528 : for ( j = 0; j < num_ch; j++ )
1385 : {
1386 4951948296 : mixer_mat[j][i][bands + i_ts * IVAS_MAX_NUM_BANDS] = in_re[j][idx];
1387 : }
1388 : }
1389 :
1390 262359090 : return;
1391 : }
1392 :
1393 :
1394 : /*-----------------------------------------------------------------------------------------*
1395 : * Function ivas_spar_dec_gen_umx_mat()
1396 : *
1397 : * generates upmix matrix
1398 : *-----------------------------------------------------------------------------------------*/
1399 :
1400 7574382 : void ivas_spar_dec_gen_umx_mat(
1401 : ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle */
1402 : const int16_t nchan_transport, /* i : number of transport channels */
1403 : const int16_t num_bands_out, /* i : number of output bands */
1404 : const int16_t bfi, /* i : bad frame indicator */
1405 : const int16_t num_md_sub_frames )
1406 : {
1407 : int16_t i, j, b, i_ts, num_out_ch;
1408 :
1409 7574382 : num_out_ch = hMdDec->spar_md_cfg.num_umx_chs;
1410 :
1411 35997102 : for ( i_ts = 0; i_ts < num_md_sub_frames; i_ts++ )
1412 : {
1413 28422720 : if ( hMdDec->td_decorr_flag == 1 )
1414 : {
1415 146955753 : for ( i = 0; i < num_out_ch; i++ )
1416 : {
1417 390766521 : for ( j = 0; j < nchan_transport; j++ )
1418 : {
1419 3471069744 : for ( b = 0; b < num_bands_out; b++ )
1420 : {
1421 3198836256 : hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.C_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS];
1422 : }
1423 : }
1424 : }
1425 :
1426 146955753 : for ( i = 0; i < num_out_ch; i++ )
1427 : {
1428 361544622 : for ( j = nchan_transport; j < num_out_ch; j++ )
1429 : {
1430 3095134305 : for ( b = 0; b < num_bands_out; b++ )
1431 : {
1432 2852122716 : hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.P_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS];
1433 : }
1434 : }
1435 : }
1436 : }
1437 : else
1438 : {
1439 0 : for ( i = 0; i < num_out_ch; i++ )
1440 : {
1441 0 : for ( j = 0; j < nchan_transport; j++ )
1442 : {
1443 0 : for ( b = 0; b < num_bands_out; b++ )
1444 : {
1445 0 : hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = hMdDec->spar_coeffs.C_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS];
1446 : }
1447 : }
1448 : }
1449 : }
1450 :
1451 : #ifdef DEBUG_SBA_MD_DUMP
1452 : {
1453 : static FILE *f_mat = 0;
1454 :
1455 : if ( f_mat == 0 )
1456 : f_mat = fopen( "mixer_mat_dec", "w" );
1457 :
1458 : for ( i = 0; i < num_out_ch; i++ )
1459 : {
1460 : for ( j = 0; j < num_out_ch; j++ )
1461 : {
1462 : for ( b = 0; b < num_bands_out; b++ )
1463 : {
1464 : fprintf( f_mat, "%f\n", hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] );
1465 : }
1466 : }
1467 : }
1468 : }
1469 : #endif
1470 : }
1471 :
1472 7574382 : ivas_spar_dec_compute_ramp_down_post_matrix( hMdDec, num_bands_out, bfi, num_md_sub_frames );
1473 :
1474 7574382 : return;
1475 : }
1476 :
1477 289182 : static void ivas_spar_md_band_upmix(
1478 : ivas_band_coeffs_t *band_coeffs,
1479 : int16_t *nB,
1480 : int16_t *bands_bw,
1481 : int16_t *valid_bands,
1482 : int16_t bw_final,
1483 : int16_t ndec,
1484 : int16_t ndm )
1485 : {
1486 : int16_t i, ii, jj, b, idx, bw_fact;
1487 :
1488 289182 : bw_fact = *bands_bw / bw_final;
1489 1420218 : for ( i = *nB - 1; i >= 0; i-- )
1490 : {
1491 :
1492 3444492 : for ( b = bw_fact - 1; b >= 0; b-- )
1493 : {
1494 2313456 : idx = i * bw_fact + b;
1495 9253824 : for ( ii = 0; ii < ndec + ndm - 1; ii++ )
1496 : {
1497 6940368 : band_coeffs[idx].pred_re[ii] = band_coeffs[i].pred_re[ii];
1498 : }
1499 9206136 : for ( ii = 0; ii < ndec; ii++ )
1500 : {
1501 6988056 : for ( jj = 0; jj < ndm - 1; jj++ )
1502 : {
1503 95376 : band_coeffs[idx].C_re[ii][jj] = band_coeffs[i].C_re[ii][jj];
1504 : }
1505 : }
1506 9206136 : for ( jj = 0; jj < ndec; jj++ )
1507 : {
1508 6892680 : band_coeffs[idx].P_re[jj] = band_coeffs[i].P_re[jj];
1509 : }
1510 2313456 : valid_bands[idx] = valid_bands[i];
1511 : }
1512 : }
1513 289182 : *nB = ( *nB ) * ( *bands_bw ) / bw_final;
1514 289182 : *bands_bw = bw_final;
1515 :
1516 289182 : return;
1517 : }
1518 :
1519 : /*-----------------------------------------------------------------------------------------*
1520 : * Function ivas_spar_dec_parse_md_bs()
1521 : *
1522 : * Parse SPAR MD bitstream
1523 : *-----------------------------------------------------------------------------------------*/
1524 :
1525 5989479 : static void ivas_spar_dec_parse_md_bs(
1526 : ivas_spar_md_dec_state_t *hMdDec,
1527 : Decoder_State *st0,
1528 : int16_t *nB,
1529 : int16_t *bands_bw,
1530 : int16_t *dtx_vad,
1531 : const int32_t ivas_total_brate,
1532 : const int16_t sba_inactive_mode )
1533 : {
1534 : int16_t i, j, k, num_bands;
1535 : int16_t ii, jj, ndec, ndm;
1536 : uint16_t qsi;
1537 : ivas_quant_strat_t qs;
1538 : int16_t strat, no_ec;
1539 : int16_t do_diff[IVAS_MAX_NUM_BANDS];
1540 : float quant[IVAS_SPAR_MAX_C_COEFF];
1541 : int16_t do_repeat[IVAS_MAX_NUM_BANDS];
1542 5989479 : *dtx_vad = 1;
1543 5989479 : *bands_bw = 1;
1544 5989479 : qsi = 0;
1545 5989479 : num_bands = hMdDec->spar_md.num_bands;
1546 :
1547 5989479 : if ( ivas_total_brate > IVAS_SID_5k2 )
1548 : {
1549 5987505 : if ( hMdDec->spar_md_cfg.quant_strat_bits > 0 )
1550 : {
1551 5987505 : if ( ivas_total_brate >= BRATE_SPAR_Q_STRAT )
1552 : {
1553 : /*only one bit written for quantization strategy to indicate either a fixed quantization strategy or dtx_vad==0 */
1554 1477263 : qsi = get_next_indice( st0, 1 );
1555 1477263 : if ( qsi == 1 )
1556 : {
1557 0 : *dtx_vad = 0;
1558 : }
1559 : }
1560 : else
1561 : {
1562 4510242 : if ( sba_inactive_mode == 1 )
1563 : {
1564 10872 : *dtx_vad = 0;
1565 10872 : qsi = hMdDec->spar_md_cfg.quant_strat_bits + 1;
1566 : }
1567 : else
1568 : {
1569 4499370 : qsi = get_next_indice( st0, hMdDec->spar_md_cfg.quant_strat_bits );
1570 : }
1571 : }
1572 : }
1573 : else
1574 : {
1575 0 : qsi = 0;
1576 : }
1577 : }
1578 : else
1579 : {
1580 1974 : *dtx_vad = 0;
1581 : }
1582 :
1583 5989479 : hMdDec->dtx_vad = *dtx_vad;
1584 :
1585 5989479 : if ( *dtx_vad == 0 )
1586 : {
1587 12846 : *nB = SPAR_DTX_BANDS;
1588 12846 : *bands_bw = num_bands / *nB;
1589 :
1590 38538 : for ( i = 0; i < *nB; i++ )
1591 : {
1592 282612 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
1593 : {
1594 256920 : hMdDec->spar_md.band_coeffs[i].pred_re[j] = 0;
1595 256920 : hMdDec->spar_md.band_coeffs[i].P_re[j] = 0;
1596 : }
1597 25692 : hMdDec->valid_bands[i] = 1;
1598 : }
1599 :
1600 115614 : for ( i = 0; i < num_bands; i++ )
1601 : {
1602 822144 : for ( j = 0; j < ( IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS ); j++ )
1603 : {
1604 2877504 : for ( k = 0; k < ( IVAS_SPAR_MAX_DMX_CHS - 1 ); k++ )
1605 : {
1606 2158128 : hMdDec->spar_md.band_coeffs[i].C_re[j][k] = 0;
1607 : }
1608 : }
1609 : }
1610 :
1611 12846 : ivas_parse_parameter_bitstream_dtx( &hMdDec->spar_md, st0, *bands_bw, *nB, hMdDec->spar_md_cfg.num_dmx_chans_per_band, hMdDec->spar_md_cfg.num_decorr_per_band );
1612 :
1613 12846 : if ( *bands_bw != 1 )
1614 : {
1615 12846 : ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
1616 12846 : ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
1617 12846 : ivas_spar_md_band_upmix(
1618 : hMdDec->spar_md.band_coeffs,
1619 : nB,
1620 : bands_bw,
1621 12846 : hMdDec->valid_bands,
1622 : 1,
1623 : ndec,
1624 : ndm );
1625 : }
1626 :
1627 12846 : return;
1628 : }
1629 :
1630 5976633 : qs = hMdDec->spar_md_cfg.quant_strat[qsi];
1631 :
1632 5976633 : strat = get_next_indice( st0, 3 );
1633 :
1634 5976633 : no_ec = 0;
1635 :
1636 5976633 : if ( strat < 2 )
1637 : {
1638 1911732 : *bands_bw = strat + 1;
1639 1911732 : *nB = num_bands / *bands_bw;
1640 19292052 : for ( i = 0; i < *nB; i++ )
1641 : {
1642 17380320 : do_diff[i] = 0;
1643 17380320 : do_repeat[i] = 0;
1644 : }
1645 : }
1646 4064901 : else if ( strat < 4 )
1647 : {
1648 145125 : *bands_bw = strat - 1;
1649 145125 : *nB = num_bands / *bands_bw;
1650 1300149 : for ( i = 0; i < *nB; i++ )
1651 : {
1652 1155024 : do_diff[i] = 0;
1653 1155024 : do_repeat[i] = 0;
1654 : }
1655 145125 : no_ec = 1;
1656 : }
1657 3919776 : else if ( ivas_total_brate < IVAS_24k4 )
1658 : {
1659 268569 : *bands_bw = 2;
1660 268569 : *nB = num_bands / *bands_bw;
1661 :
1662 1342845 : for ( i = 0; i < *nB; i++ )
1663 : {
1664 1074276 : do_diff[i] = 0;
1665 1074276 : do_repeat[i] = ( ( strat % 2 ) == ( ( i + 1 ) % 2 ) );
1666 : }
1667 : }
1668 : else
1669 : {
1670 3651207 : *bands_bw = 1;
1671 3651207 : *nB = num_bands;
1672 :
1673 32860863 : for ( i = 0; i < *nB; i++ )
1674 : {
1675 29209656 : do_diff[i] = ( ( ( i + 1 ) & 3 ) != strat - 4 );
1676 29209656 : do_repeat[i] = 0;
1677 : }
1678 3651207 : if ( hMdDec->spar_md_cfg.prev_quant_idx >= 0 )
1679 : {
1680 3650718 : ivas_map_prior_coeffs_quant( &hMdDec->spar_md_prev, &hMdDec->spar_md_cfg, qsi, *nB );
1681 : }
1682 : }
1683 : #ifdef SPAR_HOA_DBG
1684 : fprintf( stdout, "\n\n no_ec = %d, strat = %d\n", no_ec, strat );
1685 : #endif
1686 5976633 : hMdDec->spar_md_cfg.prev_quant_idx = qsi;
1687 :
1688 5976633 : if ( no_ec == 0 )
1689 : {
1690 5831508 : ivas_decode_arith_bs( hMdDec, st0, qsi, *nB, *bands_bw, do_diff, strat, ivas_total_brate );
1691 : }
1692 : else
1693 : {
1694 145125 : ivas_decode_huffman_bs( hMdDec, st0, qsi, *nB, *bands_bw );
1695 : }
1696 :
1697 54795909 : for ( i = 0; i < *nB; i++ )
1698 : {
1699 48819276 : ndec = hMdDec->spar_md_cfg.num_decorr_per_band[( *bands_bw ) * i];
1700 48819276 : ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[( *bands_bw ) * i];
1701 :
1702 48819276 : ivas_deindex_real_index( hMdDec->spar_md.band_coeffs_idx[i].pred_index_re, qs.PR.q_levels[0], qs.PR.min, qs.PR.max, hMdDec->spar_md.band_coeffs[i].pred_re, ndm + ndec - 1 );
1703 :
1704 48819276 : j = 0;
1705 152412516 : for ( ii = 0; ii < ndec; ii++ )
1706 : {
1707 234883416 : for ( jj = 0; jj < ndm - 1; jj++ )
1708 : {
1709 131290176 : quant[j] = hMdDec->spar_md.band_coeffs[i].C_re[ii][jj];
1710 131290176 : j++;
1711 : }
1712 : }
1713 :
1714 48819276 : ivas_deindex_real_index( hMdDec->spar_md.band_coeffs_idx[i].drct_index_re, qs.C.q_levels[0], qs.C.min, qs.C.max, quant, ndec * ( ndm - 1 ) );
1715 :
1716 48819276 : j = 0;
1717 152412516 : for ( ii = 0; ii < ndec; ii++ )
1718 : {
1719 234883416 : for ( jj = 0; jj < ndm - 1; jj++ )
1720 : {
1721 131290176 : hMdDec->spar_md.band_coeffs[i].C_re[ii][jj] = quant[j];
1722 131290176 : j++;
1723 : }
1724 : }
1725 :
1726 48819276 : ivas_deindex_real_index( hMdDec->spar_md.band_coeffs_idx[i].decd_index_re, qs.P_r.q_levels[0], qs.P_r.min, qs.P_r.max, hMdDec->spar_md.band_coeffs[i].P_re, ndm + ndec - 1 );
1727 :
1728 : /* Store prior coefficient indices */
1729 224761824 : for ( j = 0; j < ndm + ndec - 1; j++ )
1730 : {
1731 175942548 : hMdDec->spar_md_prev.band_coeffs_idx[i].pred_index_re[j] = hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j];
1732 : }
1733 180109452 : for ( j = 0; j < ndec * ( ndm - 1 ); j++ )
1734 : {
1735 131290176 : hMdDec->spar_md_prev.band_coeffs_idx[i].drct_index_re[j] = hMdDec->spar_md.band_coeffs_idx[i].drct_index_re[j];
1736 : }
1737 152412516 : for ( j = 0; j < ndec; j++ )
1738 : {
1739 103593240 : hMdDec->spar_md_prev.band_coeffs_idx[i].decd_index_re[j] = hMdDec->spar_md.band_coeffs_idx[i].decd_index_re[j];
1740 : }
1741 48819276 : hMdDec->valid_bands[i] |= ( do_diff[i] == 0 && do_repeat[i] == 0 ) ? 1 : 0;
1742 : }
1743 :
1744 5976633 : ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
1745 5976633 : ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
1746 5976633 : if ( *bands_bw != 1 )
1747 : {
1748 276336 : ivas_spar_md_band_upmix(
1749 : hMdDec->spar_md.band_coeffs,
1750 : nB,
1751 : bands_bw,
1752 276336 : hMdDec->valid_bands,
1753 : 1,
1754 : ndec,
1755 : ndm );
1756 : }
1757 :
1758 :
1759 5976633 : return;
1760 : }
1761 :
1762 :
1763 : /*-----------------------------------------------------------------------------------------*
1764 : * Function ivas_decode_arith_bs()
1765 : *
1766 : * Decode bitstream with arith decoder
1767 : *-----------------------------------------------------------------------------------------*/
1768 :
1769 5831508 : static void ivas_decode_arith_bs(
1770 : ivas_spar_md_dec_state_t *hMdDec,
1771 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
1772 : const uint16_t qsi,
1773 : const int16_t nB,
1774 : const int16_t bands_bw,
1775 : int16_t *pDo_diff,
1776 : const int16_t strat,
1777 : const int32_t ivas_total_brate )
1778 : {
1779 : int16_t i, ndm, ndec;
1780 : int16_t j;
1781 : ivas_cell_dim_t pred_cell_dims[IVAS_MAX_NUM_BANDS];
1782 : ivas_cell_dim_t drct_cell_dims[IVAS_MAX_NUM_BANDS];
1783 : ivas_cell_dim_t decd_cell_dims[IVAS_MAX_NUM_BANDS];
1784 : ivas_cell_dim_t decx_cell_dims[IVAS_MAX_NUM_BANDS];
1785 : int16_t symbol_arr_re[IVAS_MAX_INPUT_LEN];
1786 : int16_t symbol_arr_old_re[IVAS_MAX_INPUT_LEN];
1787 : int16_t any_diff;
1788 :
1789 53495760 : for ( i = 0; i < nB; i++ )
1790 : {
1791 47664252 : ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bands_bw * i];
1792 47664252 : ndec = hMdDec->spar_md_cfg.num_decorr_per_band[bands_bw * i];
1793 47664252 : if ( ( ivas_total_brate < IVAS_24k4 ) && ( strat > 3 ) && ( ( ( i % 2 == 1 ) && ( strat % 2 == 0 ) ) || ( ( i % 2 == 0 ) && ( strat % 2 == 1 ) ) ) )
1794 : {
1795 537138 : pred_cell_dims[i].dim1 = 0;
1796 537138 : pred_cell_dims[i].dim2 = 0;
1797 537138 : drct_cell_dims[i].dim1 = 0;
1798 537138 : drct_cell_dims[i].dim2 = 0;
1799 537138 : decd_cell_dims[i].dim1 = 0;
1800 537138 : decd_cell_dims[i].dim2 = 0;
1801 537138 : decx_cell_dims[i].dim1 = 0;
1802 537138 : decx_cell_dims[i].dim2 = 0;
1803 : }
1804 : else
1805 : {
1806 47127114 : pred_cell_dims[i].dim1 = ndm + ndec - 1;
1807 47127114 : if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
1808 : {
1809 3440772 : if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
1810 : {
1811 1146924 : pred_cell_dims[i].dim1 -= ( FOA_CHANNELS - 1 );
1812 : }
1813 : }
1814 47127114 : pred_cell_dims[i].dim2 = 1;
1815 47127114 : drct_cell_dims[i].dim1 = ndec;
1816 47127114 : drct_cell_dims[i].dim2 = ndm - 1;
1817 47127114 : decd_cell_dims[i].dim1 = ndec;
1818 47127114 : decd_cell_dims[i].dim2 = 1;
1819 47127114 : decx_cell_dims[i].dim1 = ( ndec * ( ndec - 1 ) ) >> 1;
1820 47127114 : decx_cell_dims[i].dim2 = 1;
1821 : }
1822 : }
1823 :
1824 5831508 : any_diff = 0;
1825 25207470 : for ( i = 0; i < nB; i++ )
1826 : {
1827 23027169 : if ( pDo_diff[i] != 0 )
1828 : {
1829 3651207 : any_diff = 1;
1830 3651207 : break;
1831 : }
1832 : }
1833 :
1834 5831508 : if ( any_diff == 1 )
1835 : {
1836 3651207 : if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
1837 : {
1838 0 : for ( i = 0; i < nB; i++ )
1839 : {
1840 0 : if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
1841 : {
1842 0 : for ( j = 0; j < pred_cell_dims[i].dim1; j++ )
1843 : {
1844 0 : hMdDec->spar_md_prev.band_coeffs_idx_mapped[i].pred_index_re[j] =
1845 0 : hMdDec->spar_md_prev.band_coeffs_idx_mapped[i].pred_index_re[j + ( FOA_CHANNELS - 1 )];
1846 : }
1847 : }
1848 : }
1849 : }
1850 :
1851 3651207 : ivas_copy_band_coeffs_idx_to_arr( hMdDec->spar_md_prev.band_coeffs_idx_mapped, nB, symbol_arr_old_re, pred_cell_dims, PRED_COEFF );
1852 : }
1853 :
1854 5831508 : ivas_arith_decode_cmplx_cell_array( &hMdDec->arith_coeffs.pred_arith_re[qsi], &hMdDec->arith_coeffs.pred_arith_re_diff[qsi],
1855 : st0, pred_cell_dims, pDo_diff, nB, symbol_arr_re, symbol_arr_old_re );
1856 :
1857 5831508 : ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, pred_cell_dims, PRED_COEFF );
1858 :
1859 5831508 : if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
1860 : {
1861 3727503 : for ( i = 0; i < nB; i++ )
1862 : {
1863 3440772 : if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
1864 : {
1865 4662924 : for ( j = pred_cell_dims[i].dim1 - 1; j >= 0; j-- )
1866 : {
1867 3516000 : hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j + ( FOA_CHANNELS - 1 )] =
1868 3516000 : hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j];
1869 : }
1870 4587696 : for ( j = 0; j < FOA_CHANNELS - 1; j++ )
1871 : {
1872 3440772 : hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j] = 0;
1873 : }
1874 : }
1875 : }
1876 : }
1877 :
1878 5831508 : if ( any_diff == 1 )
1879 : {
1880 3651207 : ivas_copy_band_coeffs_idx_to_arr( hMdDec->spar_md_prev.band_coeffs_idx_mapped, nB, symbol_arr_old_re, drct_cell_dims, DRCT_COEFF );
1881 : }
1882 :
1883 5831508 : ivas_arith_decode_cmplx_cell_array( &hMdDec->arith_coeffs.drct_arith_re[qsi], &hMdDec->arith_coeffs.drct_arith_re_diff[qsi],
1884 : st0, drct_cell_dims, pDo_diff, nB, symbol_arr_re, symbol_arr_old_re );
1885 :
1886 5831508 : ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, drct_cell_dims, DRCT_COEFF );
1887 :
1888 5831508 : if ( any_diff == 1 )
1889 : {
1890 3651207 : ivas_copy_band_coeffs_idx_to_arr( hMdDec->spar_md_prev.band_coeffs_idx_mapped, nB, symbol_arr_old_re, decd_cell_dims, DECD_COEFF );
1891 : }
1892 :
1893 5831508 : ivas_arith_decode_cmplx_cell_array( &hMdDec->arith_coeffs.decd_arith_re[qsi], &hMdDec->arith_coeffs.decd_arith_re_diff[qsi],
1894 : st0, decd_cell_dims, pDo_diff, nB, symbol_arr_re, symbol_arr_old_re );
1895 :
1896 5831508 : ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, decd_cell_dims, DECD_COEFF );
1897 :
1898 5831508 : if ( any_diff == 1 )
1899 : {
1900 3651207 : ivas_copy_band_coeffs_idx_to_arr( hMdDec->spar_md_prev.band_coeffs_idx_mapped, nB, symbol_arr_old_re, decx_cell_dims, DECX_COEFF );
1901 : }
1902 :
1903 5831508 : ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, decx_cell_dims, DECX_COEFF );
1904 :
1905 5831508 : return;
1906 : }
1907 :
1908 :
1909 : /*-----------------------------------------------------------------------------------------*
1910 : * Function ivas_fill_band_coeffs_idx()
1911 : *
1912 : * Copy pred band coeffs to arr
1913 : *-----------------------------------------------------------------------------------------*/
1914 :
1915 23326032 : static void ivas_fill_band_coeffs_idx(
1916 : ivas_band_coeffs_ind_t *pBands_idx,
1917 : const int16_t nB,
1918 : int16_t *pSymbol_re,
1919 : ivas_cell_dim_t *pCell_dims,
1920 : const ivas_coeffs_type_t coeff_type )
1921 : {
1922 : int16_t i, len;
1923 23326032 : int16_t *pPtr_idx = NULL;
1924 :
1925 213983040 : for ( i = 0; i < nB; i++ )
1926 : {
1927 190657008 : switch ( coeff_type )
1928 : {
1929 47664252 : case PRED_COEFF:
1930 : {
1931 47664252 : pPtr_idx = pBands_idx[i].pred_index_re;
1932 47664252 : break;
1933 : }
1934 47664252 : case DRCT_COEFF:
1935 : {
1936 47664252 : pPtr_idx = pBands_idx[i].drct_index_re;
1937 47664252 : break;
1938 : }
1939 47664252 : case DECD_COEFF:
1940 : {
1941 47664252 : pPtr_idx = pBands_idx[i].decd_index_re;
1942 47664252 : break;
1943 : }
1944 47664252 : case DECX_COEFF:
1945 : {
1946 47664252 : break;
1947 : }
1948 :
1949 0 : default:
1950 0 : assert( !"unsupported config!" );
1951 : }
1952 :
1953 190657008 : if ( coeff_type != DECX_COEFF )
1954 : {
1955 142992756 : len = pCell_dims[i].dim1 * pCell_dims[i].dim2;
1956 142992756 : mvs2s( pSymbol_re, pPtr_idx, len );
1957 142992756 : pSymbol_re += len;
1958 : }
1959 : }
1960 :
1961 23326032 : return;
1962 : }
1963 :
1964 :
1965 : /*-----------------------------------------------------------------------------------------*
1966 : * Function ivas_decode_huffman_bs()
1967 : *
1968 : * Decode bitstream with huffman decoder
1969 : *-----------------------------------------------------------------------------------------*/
1970 :
1971 145125 : static void ivas_decode_huffman_bs(
1972 : ivas_spar_md_dec_state_t *hMdDec,
1973 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
1974 : const uint16_t qsi,
1975 : const int16_t nB,
1976 : const int16_t bands_bw )
1977 : {
1978 : int16_t i, j;
1979 : int16_t ndm, ndec;
1980 : int16_t pred_dim, drct_dim, decd_dim, pred_offset;
1981 :
1982 1300149 : for ( i = 0; i < nB; i++ )
1983 : {
1984 1155024 : ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bands_bw * i];
1985 1155024 : ndec = hMdDec->spar_md_cfg.num_decorr_per_band[bands_bw * i];
1986 :
1987 1155024 : pred_dim = ndec + ndm - 1;
1988 1155024 : drct_dim = ndec * ( ndm - 1 );
1989 1155024 : decd_dim = ndec;
1990 1155024 : pred_offset = 0;
1991 :
1992 1155024 : if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
1993 : {
1994 0 : if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
1995 : {
1996 0 : pred_offset = FOA_CHANNELS - 1;
1997 : }
1998 : }
1999 :
2000 4620096 : for ( j = pred_offset; j < pred_dim; j++ )
2001 : {
2002 3465072 : ivas_huffman_decode( &hMdDec->huff_coeffs.pred_huff_re[qsi], st0, &hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j] );
2003 : }
2004 :
2005 1155024 : if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
2006 : {
2007 0 : if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
2008 : {
2009 0 : for ( j = 0; j < pred_offset; j++ )
2010 : {
2011 0 : hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j] = 0;
2012 : }
2013 : }
2014 : }
2015 :
2016 1217184 : for ( j = 0; j < drct_dim; j++ )
2017 : {
2018 62160 : ivas_huffman_decode( &hMdDec->huff_coeffs.drct_huff_re[qsi], st0, &hMdDec->spar_md.band_coeffs_idx[i].drct_index_re[j] );
2019 : }
2020 :
2021 1265952 : for ( j = 0; j < decd_dim; j++ )
2022 : {
2023 110928 : ivas_huffman_decode( &hMdDec->huff_coeffs.decd_huff_re[qsi], st0, &hMdDec->spar_md.band_coeffs_idx[i].decd_index_re[j] );
2024 : }
2025 : }
2026 :
2027 145125 : return;
2028 : }
2029 :
2030 11978958 : static void ivas_spar_plc_get_band_age(
2031 : const int16_t *valid_bands,
2032 : int16_t *base_band_age,
2033 : const int16_t num_bands,
2034 : int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS],
2035 : int16_t valid_band_idx[IVAS_MAX_NUM_BANDS],
2036 : int16_t *all_valid,
2037 : int16_t *b_idx )
2038 : {
2039 : int16_t b, idx;
2040 :
2041 11978958 : set_s( valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
2042 11978958 : set_s( last_valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
2043 11978958 : idx = -1;
2044 11978958 : *all_valid = 1;
2045 132253326 : for ( b = 0; b < num_bands; b++ )
2046 : {
2047 120274368 : if ( valid_bands[b] != 0 )
2048 : {
2049 116906640 : base_band_age[b] = 0; /* reset band age */
2050 116906640 : idx++;
2051 116906640 : valid_band_idx[idx] = b;
2052 : }
2053 : else
2054 : {
2055 3367728 : base_band_age[b] += 1; /* increment the age of invalid bands */
2056 :
2057 3367728 : if ( base_band_age[b] > 3 )
2058 : {
2059 585792 : last_valid_band_idx[b] = idx;
2060 : }
2061 3367728 : *all_valid = 0;
2062 : }
2063 : }
2064 11978958 : *b_idx = idx;
2065 :
2066 11978958 : return;
2067 : }
2068 :
2069 585792 : static void ivas_spar_get_plc_interp_weights(
2070 : int16_t valid_band_idx[IVAS_MAX_NUM_BANDS],
2071 : int16_t last_valid_band_idx,
2072 : int16_t idx,
2073 : int16_t b,
2074 : float *w,
2075 : int16_t *id0,
2076 : int16_t *id1 )
2077 : {
2078 585792 : if ( last_valid_band_idx < 0 ) /* Extrapolation */
2079 : {
2080 138804 : *id1 = valid_band_idx[0];
2081 138804 : *id0 = 0;
2082 138804 : *w = 1;
2083 : }
2084 446988 : else if ( last_valid_band_idx == idx ) /* Extrapolation */
2085 : {
2086 51480 : *id1 = valid_band_idx[last_valid_band_idx];
2087 51480 : *id0 = valid_band_idx[last_valid_band_idx];
2088 51480 : *w = 0;
2089 : }
2090 : else /* Interpolation */
2091 : {
2092 395508 : *id0 = valid_band_idx[last_valid_band_idx];
2093 395508 : *id1 = valid_band_idx[last_valid_band_idx + 1];
2094 395508 : *w = ( (float) ( b - *id0 ) ) / ( *id1 - *id0 );
2095 : }
2096 585792 : return;
2097 : }
2098 :
2099 : /*-----------------------------------------------------------------------------------------*
2100 : * Function ivas_spar_md_fill_invalid_bands()
2101 : *
2102 : * Fill invalid bands in interpolation/extrapolation of valid bands
2103 : * when PLC is to be done with partial time differential coding
2104 : *-----------------------------------------------------------------------------------------*/
2105 5989479 : static void ivas_spar_md_fill_invalid_bands(
2106 : ivas_spar_dec_matrices_t *pSpar_coeffs,
2107 : ivas_spar_dec_matrices_t *pSpar_coeffs_prev,
2108 : const int16_t *valid_bands,
2109 : int16_t *base_band_age,
2110 : const int16_t num_bands,
2111 : const int16_t num_channels,
2112 : const int16_t num_md_sub_frames )
2113 : {
2114 : int16_t i, j, b, all_valid;
2115 5989479 : int16_t valid_band_idx[IVAS_MAX_NUM_BANDS], idx = -1;
2116 : int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS];
2117 5989479 : float w = 0;
2118 5989479 : ivas_spar_plc_get_band_age( valid_bands, base_band_age, num_bands,
2119 : last_valid_band_idx, valid_band_idx, &all_valid, &idx );
2120 5989479 : assert( idx > 0 ); /* some bands should be valid */
2121 :
2122 5989479 : if ( all_valid == 0 )
2123 : {
2124 4511280 : for ( b = 0; b < num_bands; b++ )
2125 : {
2126 : /* check against non zero in if and else if */
2127 4147242 : if ( base_band_age[b] > 3 ) /* old invalid bands */
2128 : {
2129 : int16_t id0, id1;
2130 292896 : ivas_spar_get_plc_interp_weights( valid_band_idx, last_valid_band_idx[b],
2131 : idx, b, &w, &id0, &id1 );
2132 1464480 : for ( i = 0; i < num_channels; i++ )
2133 : {
2134 5857920 : for ( j = 0; j < num_channels; j++ )
2135 : {
2136 4686336 : pSpar_coeffs->C_re[i][j][b] = ( 1 - w ) * pSpar_coeffs->C_re[i][j][id0] + w * pSpar_coeffs->C_re[i][j][id1];
2137 4686336 : pSpar_coeffs->P_re[i][j][b] = ( 1 - w ) * pSpar_coeffs->P_re[i][j][id0] + w * pSpar_coeffs->P_re[i][j][id1];
2138 : }
2139 : }
2140 : }
2141 : else /* young invalid bands */
2142 : {
2143 3854346 : if ( valid_bands[b] == 0 )
2144 : {
2145 6954840 : for ( i = 0; i < num_channels; i++ )
2146 : {
2147 27819360 : for ( j = 0; j < num_channels; j++ )
2148 : {
2149 22255488 : pSpar_coeffs->C_re[i][j][b] = pSpar_coeffs_prev->C_re[i][j][b];
2150 22255488 : pSpar_coeffs->P_re[i][j][b] = pSpar_coeffs_prev->P_re[i][j][b];
2151 : }
2152 : }
2153 : }
2154 : }
2155 :
2156 4147242 : if ( valid_bands[b] == 0 )
2157 : {
2158 : int16_t i_ts;
2159 8419320 : for ( i = 0; i < num_channels; i++ )
2160 : {
2161 33677280 : for ( j = 0; j < num_channels; j++ )
2162 : {
2163 107256384 : for ( i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
2164 : {
2165 80314560 : pSpar_coeffs->C_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = pSpar_coeffs->C_re[i][j][b];
2166 80314560 : pSpar_coeffs->P_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = pSpar_coeffs->P_re[i][j][b];
2167 : }
2168 : }
2169 : }
2170 : }
2171 : }
2172 : }
2173 :
2174 5989479 : return;
2175 : }
2176 :
2177 5989479 : static void ivas_spar_md_fill_invalid_bandcoeffs(
2178 : ivas_band_coeffs_t *pBand_coeffs,
2179 : ivas_band_coeffs_t *pBand_coeffs_prev,
2180 : const int16_t *valid_bands,
2181 : int16_t *base_band_age,
2182 : int16_t *first_valid_frame,
2183 : const int16_t num_bands )
2184 : {
2185 : int16_t j, k, b, all_valid;
2186 5989479 : int16_t valid_band_idx[IVAS_MAX_NUM_BANDS], idx = -1;
2187 : int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS];
2188 5989479 : float w = 0;
2189 :
2190 5989479 : ivas_spar_plc_get_band_age( valid_bands, base_band_age, num_bands,
2191 : last_valid_band_idx, valid_band_idx, &all_valid, &idx );
2192 :
2193 5989479 : assert( idx > 0 ); /* some bands should be valid */
2194 :
2195 5989479 : if ( all_valid == 0 )
2196 : {
2197 3276342 : for ( b = 0; b < num_bands; b++ )
2198 : {
2199 : /* check against non zero in if and else if */
2200 2912304 : if ( ( base_band_age[b] > 3 ) || ( *first_valid_frame == 0 ) ) /* old invalid bands */
2201 292896 : {
2202 : int16_t id0, id1;
2203 292896 : ivas_spar_get_plc_interp_weights( valid_band_idx, last_valid_band_idx[b],
2204 : idx, b, &w, &id0, &id1 );
2205 :
2206 3221856 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
2207 : {
2208 2928960 : pBand_coeffs[b].pred_re[j] = ( 1 - w ) * pBand_coeffs[id0].pred_re[j] + w * pBand_coeffs[id1].pred_re[j];
2209 : }
2210 :
2211 2343168 : for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
2212 : {
2213 8201088 : for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
2214 : {
2215 6150816 : pBand_coeffs[b].C_re[j][k] = ( 1 - w ) * pBand_coeffs[id0].C_re[j][k] + w * pBand_coeffs[id1].C_re[j][k];
2216 : }
2217 : }
2218 :
2219 3221856 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
2220 : {
2221 2928960 : pBand_coeffs[b].P_re[j] = ( 1 - w ) * pBand_coeffs[id0].P_re[j] + w * pBand_coeffs[id1].P_re[j];
2222 : }
2223 : }
2224 : else /* young invalid bands */
2225 : {
2226 2619408 : if ( valid_bands[b] == 0 )
2227 : {
2228 15300648 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
2229 : {
2230 13909680 : pBand_coeffs[b].pred_re[j] = pBand_coeffs_prev[b].pred_re[j];
2231 : }
2232 :
2233 11127744 : for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
2234 : {
2235 38947104 : for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
2236 : {
2237 29210328 : pBand_coeffs[b].C_re[j][k] = pBand_coeffs_prev[b].C_re[j][k];
2238 : }
2239 : }
2240 :
2241 15300648 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
2242 : {
2243 13909680 : pBand_coeffs[b].P_re[j] = pBand_coeffs_prev[b].P_re[j];
2244 : }
2245 : }
2246 : }
2247 : }
2248 : }
2249 : else
2250 : {
2251 5625441 : *first_valid_frame = 1;
2252 : }
2253 :
2254 5989479 : return;
2255 : }
2256 :
2257 :
2258 : /*-----------------------------------------------------------------------------------------*
2259 : * Function ivas_spar_dec_compute_ramp_down_post_matrix()
2260 : *
2261 : *
2262 : *-----------------------------------------------------------------------------------------*/
2263 :
2264 7574382 : static void ivas_spar_dec_compute_ramp_down_post_matrix(
2265 : ivas_spar_md_dec_state_t *hMdDec,
2266 : const int16_t num_bands_out,
2267 : const int16_t bfi,
2268 : const int16_t num_md_sub_frames )
2269 : {
2270 : int16_t num_in_ch, num_out_ch, i, j, b;
2271 :
2272 7574382 : num_in_ch = hMdDec->spar_md_cfg.num_umx_chs;
2273 7574382 : num_out_ch = hMdDec->spar_md_cfg.num_umx_chs;
2274 :
2275 7574382 : if ( bfi == 0 )
2276 : {
2277 6982902 : hMdDec->spar_plc_num_lost_frames = 0;
2278 : }
2279 : else
2280 : {
2281 591480 : if ( hMdDec->td_decorr_flag == 0 )
2282 : {
2283 0 : assert( 0 );
2284 : }
2285 :
2286 591480 : hMdDec->spar_plc_num_lost_frames += 1;
2287 591480 : hMdDec->spar_plc_num_lost_frames = min( hMdDec->spar_plc_num_lost_frames, 100 );
2288 :
2289 591480 : if ( hMdDec->spar_plc_num_lost_frames > ivas_spar_dec_plc_num_frames_keep )
2290 : {
2291 : int16_t num_fade_frames;
2292 : int16_t gain_dB;
2293 : float gain;
2294 : float post_matrix[IVAS_SPAR_MAX_CH];
2295 :
2296 195069 : num_fade_frames = max( hMdDec->spar_plc_num_lost_frames - ivas_spar_dec_plc_num_frames_keep, 0 );
2297 195069 : gain_dB = -min( num_fade_frames, ivas_spar_dec_plc_max_num_frames_ramp_down ) * ivas_spar_dec_plc_per_frame_ramp_down_gain_dB;
2298 195069 : gain = powf( 10, ( ( (float) gain_dB ) / 20 ) );
2299 :
2300 2340828 : for ( i = 0; i < IVAS_SPAR_MAX_CH; i++ )
2301 : {
2302 2145759 : post_matrix[i] = 1 + min( ( (float) num_fade_frames ) / ivas_spar_dec_plc_num_frames_fade_out, 1 ) * ( ivas_spar_dec_plc_spatial_target[i] - 1 );
2303 2145759 : post_matrix[i] *= gain;
2304 : }
2305 :
2306 : /* apply the post matrix */
2307 975291 : for ( int16_t i_ts = 0; i_ts < num_md_sub_frames; i_ts++ )
2308 : {
2309 3901110 : for ( i = 0; i < num_out_ch; i++ )
2310 : {
2311 15604440 : for ( j = 0; j < num_in_ch; j++ )
2312 : {
2313 153964512 : for ( b = 0; b < num_bands_out; b++ )
2314 : {
2315 141480960 : hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] *= post_matrix[i];
2316 : }
2317 : }
2318 : }
2319 : }
2320 : }
2321 : }
2322 :
2323 7574382 : return;
2324 : }
2325 :
2326 :
2327 : /*-----------------------------------------------------------------------------------------*
2328 : * Function ivas_spar_unquant_dtx_indicies()
2329 : *
2330 : * Unquantize SPAR MD DYX indices
2331 : *-----------------------------------------------------------------------------------------*/
2332 :
2333 12846 : static void ivas_spar_unquant_dtx_indicies(
2334 : ivas_spar_md_t *pSpar_md,
2335 : const int16_t nB,
2336 : const int16_t bw,
2337 : int16_t *ndm_per_band )
2338 : {
2339 : int16_t i, b;
2340 : int16_t q_lvl;
2341 : float val;
2342 : int16_t idx;
2343 : float pr_min_max[2];
2344 :
2345 12846 : pr_min_max[0] = pSpar_md->min_max[0];
2346 12846 : pr_min_max[1] = pSpar_md->min_max[1];
2347 :
2348 38538 : for ( b = 0; b < nB; b++ )
2349 : {
2350 102768 : for ( i = 0; i < FOA_CHANNELS - 1; i++ )
2351 : {
2352 77076 : q_lvl = dtx_pr_real_q_levels[ndm_per_band[bw * b] - 1][i];
2353 77076 : idx = pSpar_md->band_coeffs_idx[b].pred_index_re[i];
2354 77076 : ivas_deindex_real_index( &idx, q_lvl, pr_min_max[0], pr_min_max[1], &val, 1 );
2355 77076 : pSpar_md->band_coeffs[b].pred_re[i] = val;
2356 : }
2357 :
2358 90846 : for ( i = 0; i < FOA_CHANNELS - ndm_per_band[bw * b]; i++ )
2359 : {
2360 65154 : q_lvl = dtx_pd_real_q_levels[ndm_per_band[bw * b] - 1][i];
2361 65154 : idx = pSpar_md->band_coeffs_idx[b].decd_index_re[i];
2362 65154 : ivas_deindex_real_index( &idx, q_lvl, dtx_pd_real_min_max[0], dtx_pd_real_min_max[1], &val, 1 );
2363 65154 : pSpar_md->band_coeffs[b].P_re[i] = val;
2364 : }
2365 : }
2366 :
2367 12846 : return;
2368 : }
2369 :
2370 :
2371 : /*-----------------------------------------------------------------------------------------*
2372 : * Function ivas_parse_parameter_bitstream_dtx()
2373 : *
2374 : * parse DTX bitstream parameters
2375 : *-----------------------------------------------------------------------------------------*/
2376 :
2377 12846 : static void ivas_parse_parameter_bitstream_dtx(
2378 : ivas_spar_md_t *pSpar_md,
2379 : Decoder_State *st0, /* i/o: decoder state structure - for bitstream handling*/
2380 : const int16_t bw,
2381 : const int16_t num_bands,
2382 : int16_t *num_dmx_per_band,
2383 : int16_t *num_dec_per_band )
2384 : {
2385 : int16_t i, j, ndec, ndm;
2386 : float val;
2387 : int16_t idx;
2388 : float pr_min_max[2];
2389 : int16_t pr_q_lvls, pr, pd, pd_q_lvls, pr_pd_bits;
2390 : int16_t zero_pad_bits, sid_bits_len;
2391 : int16_t sba_spar_bitlen;
2392 :
2393 12846 : sid_bits_len = st0->next_bit_pos;
2394 12846 : pr_min_max[0] = pSpar_md->min_max[0];
2395 12846 : pr_min_max[1] = pSpar_md->min_max[1];
2396 :
2397 38538 : for ( i = 0; i < num_bands; i++ )
2398 : {
2399 25692 : ndec = num_dec_per_band[bw * i];
2400 25692 : ndm = num_dmx_per_band[bw * i];
2401 :
2402 102768 : for ( j = 0; j < FOA_CHANNELS - 1; j++ )
2403 : {
2404 : int16_t pr_idx_1, pr_idx_2, pd_idx_1, pd_idx_2;
2405 : uint16_t value;
2406 :
2407 77076 : pr_idx_1 = pr_pr_idx_pairs[ndm - 1][j][0];
2408 77076 : pr_idx_2 = pr_pr_idx_pairs[ndm - 1][j][1];
2409 77076 : pd_idx_1 = pr_pd_idx_pairs[ndm - 1][j][0];
2410 77076 : pd_idx_2 = pr_pd_idx_pairs[ndm - 1][j][1];
2411 :
2412 77076 : if ( pr_idx_1 != 0 || pd_idx_1 != 0 || pr_idx_2 != 0 || pd_idx_2 != 0 )
2413 : {
2414 77076 : pr_q_lvls = dtx_pr_real_q_levels[ndm - 1][pd_idx_1 - 1];
2415 :
2416 77076 : if ( ( j + 1 ) > ndec )
2417 : {
2418 11922 : pd_q_lvls = 1;
2419 : }
2420 : else
2421 : {
2422 65154 : pd_q_lvls = dtx_pd_real_q_levels[ndm - 1][pd_idx_2 - 1];
2423 : }
2424 :
2425 77076 : pr_pd_bits = ivas_get_bits_to_encode( pd_q_lvls * pr_q_lvls );
2426 :
2427 77076 : value = get_next_indice( st0, pr_pd_bits );
2428 :
2429 77076 : pr = (int16_t) floor( value / pd_q_lvls );
2430 77076 : pd = value - pr * pd_q_lvls;
2431 77076 : val = dtx_pd_real_min_max[0];
2432 77076 : ivas_quantise_real_values( &val, pd_q_lvls, dtx_pd_real_min_max[0], dtx_pd_real_min_max[1], &idx, &val, 1 );
2433 77076 : pd = pd + idx;
2434 :
2435 77076 : val = pr_min_max[0];
2436 77076 : ivas_quantise_real_values( &val, pr_q_lvls, pr_min_max[0], pr_min_max[1], &idx, &val, 1 );
2437 77076 : pr = pr + idx;
2438 :
2439 77076 : if ( ( j + 1 ) <= ndec )
2440 : {
2441 65154 : pSpar_md->band_coeffs_idx[i].decd_index_re[pd_idx_2 - 1] = pd;
2442 : }
2443 :
2444 77076 : pSpar_md->band_coeffs_idx[i].pred_index_re[pd_idx_1 - 1] = pr;
2445 : }
2446 : }
2447 : }
2448 :
2449 12846 : sid_bits_len = st0->next_bit_pos - sid_bits_len;
2450 12846 : sba_spar_bitlen = ivas_sba_spar_sid_bitlen( num_dmx_per_band[0] );
2451 12846 : zero_pad_bits = sba_spar_bitlen - sid_bits_len;
2452 :
2453 12846 : assert( zero_pad_bits >= 0 );
2454 12846 : if ( num_dmx_per_band[0] == 2 )
2455 : {
2456 5961 : zero_pad_bits -= 1;
2457 : }
2458 :
2459 30729 : for ( j = 0; j < zero_pad_bits; j++ )
2460 : {
2461 17883 : get_next_indice( st0, 1 );
2462 : }
2463 :
2464 12846 : ivas_spar_unquant_dtx_indicies( pSpar_md, num_bands, bw, num_dmx_per_band );
2465 :
2466 12846 : return;
2467 : }
2468 :
2469 :
2470 : /*-----------------------------------------------------------------------------------------*
2471 : * Function ivas_deindex_real_index()
2472 : *
2473 : * Deindex real index
2474 : *-----------------------------------------------------------------------------------------*/
2475 :
2476 146600058 : static ivas_error ivas_deindex_real_index(
2477 : const int16_t *index,
2478 : const int16_t q_levels,
2479 : const float min_value,
2480 : const float max_value,
2481 : float *quant,
2482 : const int16_t dim )
2483 : {
2484 : int16_t i;
2485 : float q_step;
2486 :
2487 146600058 : if ( q_levels == 0 )
2488 : {
2489 0 : return IVAS_ERR_INTERNAL;
2490 : }
2491 :
2492 146600058 : if ( q_levels == 1 )
2493 : {
2494 51446568 : for ( i = 0; i < dim; i++ )
2495 : {
2496 22784976 : quant[i] = 0;
2497 : }
2498 : }
2499 : else
2500 : {
2501 117938466 : q_step = ( max_value - min_value ) / ( q_levels - 1 );
2502 578470992 : for ( i = 0; i < dim; i++ )
2503 : {
2504 460532526 : quant[i] = index[i] * q_step;
2505 : }
2506 : }
2507 :
2508 146600058 : return IVAS_ERR_OK;
2509 : }
2510 :
2511 :
2512 : /*-----------------------------------------------------------------------------------------*
2513 : * Function ivas_spar_to_dirac()
2514 : *
2515 : *
2516 : *-----------------------------------------------------------------------------------------*/
2517 :
2518 5748321 : void ivas_spar_to_dirac(
2519 : Decoder_Struct *st_ivas,
2520 : ivas_spar_md_dec_state_t *hMdDec, /* i/o: SPAR MD decoder handle */
2521 : const int16_t dtx_vad, /* i : DTX frame flag */
2522 : const int16_t num_bands_out, /* i : number of output bands */
2523 : const int16_t bw, /* i : band joining factor */
2524 : const int16_t dyn_active_w_flag )
2525 : {
2526 : DIRAC_DEC_HANDLE hDirAC;
2527 : int16_t start_band, end_band, band, qmf_band_start, qmf_band_end;
2528 : int16_t block, b;
2529 : int16_t *band_grouping;
2530 : float diffuseness[IVAS_MAX_NUM_BANDS];
2531 : int16_t sba_order_internal;
2532 : float azi_dirac[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
2533 : float ele_dirac[IVAS_MAX_NUM_BANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
2534 : int16_t azi[IVAS_MAX_NUM_BANDS];
2535 : int16_t ele[IVAS_MAX_NUM_BANDS];
2536 : float dvx[IVAS_MAX_NUM_BANDS], dvy[IVAS_MAX_NUM_BANDS], dvz[IVAS_MAX_NUM_BANDS];
2537 : float radius;
2538 : float en_ratio, res_pow;
2539 : int16_t num_slots_in_subfr;
2540 : int16_t tmp_write_idx_param_band;
2541 : int16_t tmp_write_idx_band;
2542 : float pred_re_20ms[IVAS_MAX_NUM_BANDS][IVAS_SPAR_MAX_CH - 1];
2543 : int16_t pred_idx;
2544 : int16_t *dirac_to_spar_md_bands;
2545 : int16_t enc_param_start_band;
2546 : int16_t active_w_vlbr;
2547 : int16_t i, num_subframes;
2548 : int16_t active_w;
2549 : SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
2550 :
2551 5748321 : active_w = ( dyn_active_w_flag == 1 ) || ( hMdDec->spar_md_cfg.active_w == 1 );
2552 5748321 : sba_order_internal = min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
2553 5748321 : start_band = 0;
2554 5748321 : end_band = min( num_bands_out, SPAR_DIRAC_SPLIT_START_BAND ) / bw;
2555 :
2556 5748321 : hDirAC = st_ivas->hDirAC;
2557 5748321 : hSpatParamRendCom = st_ivas->hSpatParamRendCom;
2558 :
2559 5748321 : dirac_to_spar_md_bands = st_ivas->hSpar->dirac_to_spar_md_bands;
2560 5748321 : enc_param_start_band = st_ivas->hSpar->enc_param_start_band / bw;
2561 5748321 : active_w_vlbr = ( st_ivas->hDecoderConfig->ivas_total_brate < IVAS_24k4 ) ? 1 : 0;
2562 :
2563 5748321 : if ( hDirAC != NULL && ivas_get_hodirac_flag( st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->sba_analysis_order ) == 0 )
2564 : {
2565 2355552 : band_grouping = hDirAC->band_grouping;
2566 2355552 : num_slots_in_subfr = st_ivas->hDirAC->hConfig->dec_param_estim ? CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES : 1;
2567 :
2568 21199968 : for ( band = start_band; band < end_band; band++ )
2569 : {
2570 : float PR[3], Pd[3], dvnorm, g_pred;
2571 :
2572 18844416 : PR[0] = hMdDec->spar_md.band_coeffs[band].pred_re[2];
2573 18844416 : PR[1] = hMdDec->spar_md.band_coeffs[band].pred_re[0];
2574 18844416 : PR[2] = hMdDec->spar_md.band_coeffs[band].pred_re[1];
2575 18844416 : g_pred = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
2576 18844416 : if ( g_pred <= EPSILON )
2577 : {
2578 610575 : dvx[band] = 1.0f;
2579 610575 : dvy[band] = 0.0f;
2580 610575 : dvz[band] = 0.0f;
2581 : }
2582 : else
2583 : {
2584 18233841 : g_pred = sqrtf( g_pred );
2585 18233841 : dvnorm = 1.0f / g_pred;
2586 18233841 : dvx[band] = PR[0] * dvnorm;
2587 18233841 : dvy[band] = PR[1] * dvnorm;
2588 18233841 : dvz[band] = PR[2] * dvnorm;
2589 : }
2590 :
2591 18844416 : radius = sqrtf( dvx[band] * dvx[band] + dvy[band] * dvy[band] );
2592 18844416 : azi[band] = (int16_t) ( max( -180.0f, min( 180.0f, atan2f( dvy[band], dvx[band] ) / EVS_PI * 180.0f ) ) + 0.5f );
2593 18844416 : ele[band] = (int16_t) ( max( -90.0f, min( 180.0f, atan2f( dvz[band], radius ) / EVS_PI * 180.0f ) ) + 0.5f );
2594 :
2595 18844416 : if ( st_ivas->nchan_transport == 1 )
2596 : {
2597 : float w_en_norm, f_scale;
2598 4952952 : if ( active_w )
2599 : {
2600 4952952 : if ( dtx_vad == 0 )
2601 : {
2602 12024 : f_scale = IVAS_ACTIVEW_DM_F_SCALE_DTX;
2603 : }
2604 : else
2605 : {
2606 4940928 : f_scale = ( active_w_vlbr ) ? IVAS_ACTIVEW_DM_F_SCALE_VLBR : IVAS_ACTIVEW_DM_F_SCALE;
2607 : }
2608 : }
2609 : else
2610 : {
2611 0 : f_scale = 0.0f;
2612 : }
2613 :
2614 4952952 : w_en_norm = ( 1.0f - ( f_scale * g_pred * g_pred ) );
2615 4952952 : w_en_norm *= w_en_norm;
2616 :
2617 4952952 : Pd[0] = hMdDec->spar_md.band_coeffs[band].P_re[1];
2618 4952952 : Pd[1] = hMdDec->spar_md.band_coeffs[band].P_re[0];
2619 4952952 : Pd[2] = hMdDec->spar_md.band_coeffs[band].P_re[2];
2620 4952952 : en_ratio = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
2621 4952952 : res_pow = w_en_norm + en_ratio + ( Pd[0] * Pd[0] + Pd[1] * Pd[1] + Pd[2] * Pd[2] );
2622 4952952 : res_pow *= 0.5f;
2623 4952952 : hMdDec->spar_md.en_ratio_slow[band] = 0.75f * hMdDec->spar_md.en_ratio_slow[band] + 0.25f * en_ratio;
2624 4952952 : hMdDec->spar_md.ref_pow_slow[band] = 0.75f * hMdDec->spar_md.ref_pow_slow[band] + 0.25f * res_pow;
2625 4952952 : en_ratio = sqrtf( hMdDec->spar_md.en_ratio_slow[band] ) / ( hMdDec->spar_md.ref_pow_slow[band] + EPSILON );
2626 : }
2627 : else
2628 : {
2629 13891464 : en_ratio = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
2630 13891464 : hMdDec->spar_md.en_ratio_slow[band] = 0.75f * hMdDec->spar_md.en_ratio_slow[band] + 0.25f * en_ratio;
2631 13891464 : en_ratio = sqrtf( hMdDec->spar_md.en_ratio_slow[band] );
2632 : }
2633 18844416 : diffuseness[band] = 1.0f - en_ratio; /*compute diffuseness*/
2634 18844416 : diffuseness[band] = ( ( diffuseness[band] < 1.0f ) ? ( ( diffuseness[band] < 0.0f ) ? 0.f : diffuseness[band] ) : 1.0f );
2635 : }
2636 :
2637 21199968 : for ( band = start_band; band < end_band; band++ )
2638 : {
2639 : int16_t azi_dith, ele_dith;
2640 18844416 : tmp_write_idx_param_band = hDirAC->spar_to_dirac_write_idx;
2641 :
2642 18844416 : en_ratio = 1.0f - diffuseness[band];
2643 18844416 : masa_sq( 1.0f - en_ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
2644 :
2645 18844416 : qmf_band_start = band_grouping[band];
2646 18844416 : qmf_band_end = band_grouping[band + 1];
2647 :
2648 94222080 : for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
2649 : {
2650 : int16_t ts_start, ts_end, ts;
2651 :
2652 75377664 : ts_start = DirAC_block_grouping[block];
2653 75377664 : ts_end = DirAC_block_grouping[block + 1];
2654 179021952 : for ( b = qmf_band_start; b < qmf_band_end; b++ )
2655 : {
2656 :
2657 103644288 : azi_dith = azi[band];
2658 103644288 : ele_dith = ele[band];
2659 :
2660 103644288 : hSpatParamRendCom->energy_ratio1[block][b] = en_ratio;
2661 103644288 : tmp_write_idx_band = tmp_write_idx_param_band;
2662 :
2663 103644288 : if ( hDirAC->hConfig->dec_param_estim == FALSE )
2664 : {
2665 47060112 : hSpatParamRendCom->elevation[tmp_write_idx_band][b] = ele_dith;
2666 47060112 : hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = azi_dith;
2667 47060112 : hSpatParamRendCom->diffuseness_vector[tmp_write_idx_band][b] = diffuseness[band];
2668 : }
2669 : else
2670 : {
2671 282920880 : for ( ts = ts_start; ts < ts_end; ts++ )
2672 : {
2673 226336704 : hSpatParamRendCom->elevation[tmp_write_idx_band][b] = ele_dith;
2674 226336704 : hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = azi_dith;
2675 226336704 : hSpatParamRendCom->diffuseness_vector[tmp_write_idx_band][b] = diffuseness[band];
2676 226336704 : tmp_write_idx_band = ( tmp_write_idx_band + 1 ) % hSpatParamRendCom->dirac_md_buffer_length;
2677 : }
2678 : }
2679 : }
2680 75377664 : tmp_write_idx_param_band = ( tmp_write_idx_param_band + num_slots_in_subfr ) % hSpatParamRendCom->dirac_md_buffer_length;
2681 : }
2682 : }
2683 :
2684 : /* update buffer write index */
2685 2355552 : if ( hDirAC->hConfig->dec_param_estim == FALSE )
2686 : {
2687 1069548 : hDirAC->spar_to_dirac_write_idx = ( hDirAC->spar_to_dirac_write_idx + MAX_PARAM_SPATIAL_SUBFRAMES ) % hSpatParamRendCom->dirac_md_buffer_length;
2688 : }
2689 : else
2690 : {
2691 1286004 : hDirAC->spar_to_dirac_write_idx = ( hDirAC->spar_to_dirac_write_idx + CLDFB_NO_COL_MAX ) % hSpatParamRendCom->dirac_md_buffer_length;
2692 : }
2693 : }
2694 : else
2695 : {
2696 3392769 : band = end_band;
2697 : }
2698 :
2699 : /*read DirAC metadata, convert DirAC to SPAR*/
2700 27114837 : for ( ; band < num_bands_out / bw; band++ )
2701 : {
2702 : int16_t dirac_band_idx;
2703 :
2704 21366516 : dirac_band_idx = dirac_to_spar_md_bands[band] - enc_param_start_band;
2705 :
2706 21366516 : num_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
2707 21366516 : if ( st_ivas->hQMetaData->useLowerRes )
2708 : {
2709 1100442 : num_subframes = 1;
2710 : }
2711 :
2712 103531254 : for ( block = 0; block < num_subframes; block++ )
2713 : {
2714 82164738 : if ( st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block] < 0.f )
2715 : {
2716 17668911 : st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block] += 360.f;
2717 : }
2718 82164738 : azi_dirac[band][block] = st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block];
2719 82164738 : ele_dirac[band][block] = st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].elevation[block];
2720 : }
2721 :
2722 21366516 : diffuseness[band] = 1.0f - st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].energy_ratio[0];
2723 : }
2724 :
2725 : /* DirAC MD averaged over 4 subframes and converted to SPAR format similar to encoder processing */
2726 5748321 : if ( hMdDec->spar_md_cfg.nchan_transport > 1 )
2727 : {
2728 3919317 : ivas_get_spar_md_from_dirac( azi_dirac, ele_dirac, diffuseness, 1, NULL, &hMdDec->spar_md, &hMdDec->spar_md_cfg, end_band, num_bands_out, ( hMdDec->spar_hoa_md_flag ) ? 1 : sba_order_internal, dtx_vad, NULL, st_ivas->hQMetaData->useLowerRes, active_w_vlbr, dyn_active_w_flag );
2729 :
2730 : /* temporarily copy frame-wise prediction coefficients in DirAC bands*/
2731 15677268 : for ( pred_idx = 0; pred_idx < FOA_CHANNELS - 1; pred_idx++ )
2732 : {
2733 58789755 : for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
2734 : {
2735 47031804 : pred_re_20ms[band][pred_idx] = hMdDec->spar_md.band_coeffs[band].pred_re[pred_idx];
2736 : }
2737 : }
2738 : }
2739 :
2740 : int16_t num_md_sub_frames;
2741 5748321 : num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( sba_order_internal, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->last_active_ivas_total_brate );
2742 5748321 : ivas_get_spar_md_from_dirac( azi_dirac, ele_dirac, diffuseness, num_md_sub_frames, NULL, &hMdDec->spar_md, &hMdDec->spar_md_cfg, end_band, num_bands_out / bw, ( hMdDec->spar_hoa_md_flag ) ? 1 : sba_order_internal, dtx_vad, NULL, st_ivas->hQMetaData->useLowerRes, active_w_vlbr, dyn_active_w_flag );
2743 :
2744 5748321 : if ( st_ivas->hQMetaData->useLowerRes && dtx_vad )
2745 : {
2746 1381680 : for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
2747 : {
2748 1105344 : for ( block = 1; block < num_md_sub_frames; block++ )
2749 : {
2750 0 : for ( i = 0; i < FOA_CHANNELS - 1; i++ ) /* pred coefficient index (index 0, 1, 2 predicts Y, Z, X respectively) */
2751 : {
2752 0 : hMdDec->spar_md.band_coeffs[band + block * IVAS_MAX_NUM_BANDS].pred_re[i] = hMdDec->spar_md.band_coeffs[band].pred_re[i];
2753 : }
2754 0 : for ( i = 0; i < FOA_CHANNELS - 1; i++ ) /* pred coefficient index (index 0, 1, 2 predicts Y, Z, X respectively) */
2755 : {
2756 0 : hMdDec->spar_md.band_coeffs[band + block * IVAS_MAX_NUM_BANDS].P_re[i] = hMdDec->spar_md.band_coeffs[band].P_re[i];
2757 : }
2758 : }
2759 : }
2760 : }
2761 :
2762 : /* expand DirAC TC 20ms MD for residual channels to all subframes*/
2763 27910878 : for ( block = 0; block < num_md_sub_frames; block++ )
2764 : {
2765 110812785 : for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
2766 : {
2767 354600912 : for ( pred_idx = 0; pred_idx < FOA_CHANNELS - 1; pred_idx++ ) /* pred coefficient index (index 0, 1, 2 predicts Y, Z, X respectively) */
2768 : {
2769 265950684 : if ( ivas_is_res_channel( pred_idx + 1, hMdDec->spar_md_cfg.nchan_transport ) )
2770 : {
2771 : /* use 20ms coefficients only for residual channels */
2772 120549072 : hMdDec->spar_md.band_coeffs[band + block * IVAS_MAX_NUM_BANDS].pred_re[pred_idx] = pred_re_20ms[band][pred_idx];
2773 : }
2774 : }
2775 : }
2776 : }
2777 :
2778 27114837 : for ( b = end_band * bw; b < num_bands_out; b++ )
2779 : {
2780 21366516 : hMdDec->valid_bands[b] = 1;
2781 : }
2782 :
2783 5748321 : return;
2784 : }
|