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 2178 : 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 2178 : 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 2178 : 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 2178 : 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 12734 : for ( i = 0; i < num_channels; i++ )
116 : {
117 10556 : 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 70366 : for ( j = 0; j < num_channels; j++ )
122 : {
123 59810 : 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 2178 : 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 12734 : for ( i = 0; i < num_channels; i++ )
135 : {
136 10556 : 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 70366 : for ( j = 0; j < num_channels; j++ )
141 : {
142 59810 : 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 2178 : 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 12734 : for ( i = 0; i < num_channels; i++ )
154 : {
155 10556 : 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 70366 : for ( j = 0; j < num_channels; j++ )
160 : {
161 59810 : 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 2178 : 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 12734 : for ( i = 0; i < num_channels; i++ )
173 : {
174 10556 : 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 70366 : for ( j = 0; j < num_channels; j++ )
179 : {
180 59810 : 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 2178 : 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 12734 : for ( i = 0; i < num_channels; i++ )
192 : {
193 10556 : 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 70366 : for ( j = 0; j < num_channels; j++ )
198 : {
199 59810 : 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 2178 : 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 12734 : for ( i = 0; i < num_channels; i++ )
211 : {
212 10556 : 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 70366 : for ( j = 0; j < num_channels; j++ )
217 : {
218 59810 : 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 2178 : 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 12734 : for ( i = 0; i < num_channels; i++ )
230 : {
231 10556 : 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 70366 : for ( j = 0; j < num_channels; j++ )
236 : {
237 59810 : 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 12734 : for ( i = 0; i < num_channels; i++ )
244 : {
245 70366 : for ( j = 0; j < num_channels; j++ )
246 : {
247 777530 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
248 : {
249 717720 : hMdDec->spar_coeffs_prev.C_re[i][j][k] = 0.0f;
250 717720 : hMdDec->spar_coeffs_prev.P_re[i][j][k] = 0.0f;
251 717720 : hMdDec->spar_coeffs_tar.C_re[i][j][k] = 0.0f;
252 717720 : hMdDec->spar_coeffs_tar.P_re[i][j][k] = 0.0f;
253 : }
254 : }
255 : }
256 :
257 2178 : 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 2373924 : 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 2373924 : num_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
277 2373924 : if ( sba_order > SBA_FOA_ORDER )
278 : {
279 553506 : if ( ivas_total_brate >= IVAS_512k )
280 : {
281 192256 : num_subframes = 1;
282 : }
283 : }
284 :
285 2373924 : 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 133281 : num_subframes = 1;
289 : }
290 :
291 2373924 : 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 2127 : 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 2127 : error = IVAS_ERR_OK;
315 :
316 2127 : 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 2127 : num_md_sub_frames = ivas_get_spar_dec_md_num_subframes( sba_order, hDecoderConfig->ivas_total_brate, last_active_ivas_total_brate );
322 :
323 2127 : 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 2127 : 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 2127 : hMdDec->table_idx = ivas_get_spar_table_idx( hDecoderConfig->ivas_total_brate, sba_order, SPAR_CONFIG_BW, NULL, NULL );
342 : }
343 :
344 2127 : if ( ( error = ivas_spar_md_dec_init( hMdDec, hDecoderConfig, num_channels, sba_order ) ) != IVAS_ERR_OK )
345 : {
346 0 : return error;
347 : }
348 :
349 2127 : *hMdDec_out = hMdDec;
350 :
351 2127 : return error;
352 : }
353 :
354 :
355 : /*-------------------------------------------------------------------------
356 : * ivas_spar_md_dec_matrix_close()
357 : *
358 : * Deallocate SPAR MD decoder matrices
359 : *------------------------------------------------------------------------*/
360 :
361 2178 : 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 2178 : if ( hMdDecoder->spar_md.band_coeffs != NULL )
369 : {
370 2178 : free( hMdDecoder->spar_md.band_coeffs );
371 2178 : hMdDecoder->spar_md.band_coeffs = NULL;
372 : }
373 2178 : if ( hMdDecoder->band_coeffs_prev != NULL )
374 : {
375 2178 : free( hMdDecoder->band_coeffs_prev );
376 2178 : hMdDecoder->band_coeffs_prev = NULL;
377 : }
378 :
379 2178 : if ( hMdDecoder->mixer_mat != NULL )
380 : {
381 12734 : for ( i = 0; i < num_channels; i++ )
382 : {
383 70366 : for ( j = 0; j < num_channels; j++ )
384 : {
385 59810 : free( hMdDecoder->mixer_mat[i][j] );
386 : }
387 10556 : free( hMdDecoder->mixer_mat[i] );
388 : }
389 2178 : free( hMdDecoder->mixer_mat );
390 : }
391 :
392 2178 : if ( hMdDecoder->spar_coeffs.C_re != NULL )
393 : {
394 12734 : for ( i = 0; i < num_channels; i++ )
395 : {
396 70366 : for ( j = 0; j < num_channels; j++ )
397 : {
398 59810 : free( hMdDecoder->spar_coeffs.C_re[i][j] );
399 : }
400 10556 : free( hMdDecoder->spar_coeffs.C_re[i] );
401 : }
402 2178 : free( hMdDecoder->spar_coeffs.C_re );
403 : }
404 :
405 2178 : if ( hMdDecoder->spar_coeffs.P_re != NULL )
406 : {
407 12734 : for ( i = 0; i < num_channels; i++ )
408 : {
409 70366 : for ( j = 0; j < num_channels; j++ )
410 : {
411 59810 : free( hMdDecoder->spar_coeffs.P_re[i][j] );
412 : }
413 10556 : free( hMdDecoder->spar_coeffs.P_re[i] );
414 : }
415 2178 : free( hMdDecoder->spar_coeffs.P_re );
416 : }
417 :
418 2178 : if ( hMdDecoder->spar_coeffs_prev.C_re != NULL )
419 : {
420 12734 : for ( i = 0; i < num_channels; i++ )
421 : {
422 70366 : for ( j = 0; j < num_channels; j++ )
423 : {
424 59810 : free( hMdDecoder->spar_coeffs_prev.C_re[i][j] );
425 : }
426 10556 : free( hMdDecoder->spar_coeffs_prev.C_re[i] );
427 : }
428 2178 : free( hMdDecoder->spar_coeffs_prev.C_re );
429 : }
430 :
431 2178 : if ( hMdDecoder->spar_coeffs_prev.P_re != NULL )
432 : {
433 12734 : for ( i = 0; i < num_channels; i++ )
434 : {
435 70366 : for ( j = 0; j < num_channels; j++ )
436 : {
437 59810 : free( hMdDecoder->spar_coeffs_prev.P_re[i][j] );
438 : }
439 10556 : free( hMdDecoder->spar_coeffs_prev.P_re[i] );
440 : }
441 2178 : free( hMdDecoder->spar_coeffs_prev.P_re );
442 : }
443 :
444 2178 : if ( hMdDecoder->spar_coeffs_tar.C_re != NULL )
445 : {
446 12734 : for ( i = 0; i < num_channels; i++ )
447 : {
448 70366 : for ( j = 0; j < num_channels; j++ )
449 : {
450 59810 : free( hMdDecoder->spar_coeffs_tar.C_re[i][j] );
451 : }
452 10556 : free( hMdDecoder->spar_coeffs_tar.C_re[i] );
453 : }
454 2178 : free( hMdDecoder->spar_coeffs_tar.C_re );
455 : }
456 :
457 2178 : if ( hMdDecoder->spar_coeffs_tar.P_re != NULL )
458 : {
459 12734 : for ( i = 0; i < num_channels; i++ )
460 : {
461 70366 : for ( j = 0; j < num_channels; j++ )
462 : {
463 59810 : free( hMdDecoder->spar_coeffs_tar.P_re[i][j] );
464 : }
465 10556 : free( hMdDecoder->spar_coeffs_tar.P_re[i] );
466 : }
467 2178 : free( hMdDecoder->spar_coeffs_tar.P_re );
468 : }
469 :
470 2178 : return;
471 : }
472 :
473 :
474 : /*-------------------------------------------------------------------------
475 : * ivas_spar_md_dec_close()
476 : *
477 : * Deallocate SPAR MD decoder handle
478 : *------------------------------------------------------------------------*/
479 :
480 2127 : 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 2127 : hMdDecoder = *hMdDec;
488 2127 : num_channels = hMdDecoder->spar_md_cfg.num_umx_chs;
489 :
490 2127 : ivas_spar_md_dec_matrix_close( hMdDecoder, num_channels );
491 :
492 2127 : free( *hMdDec );
493 2127 : *hMdDec = NULL;
494 :
495 2127 : return;
496 : }
497 :
498 :
499 : /*-----------------------------------------------------------------------------------------*
500 : * Function ivas_spar_md_dec_init()
501 : *
502 : * SPAR MD decoder initialization
503 : *-----------------------------------------------------------------------------------------*/
504 :
505 2483 : 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 2483 : 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 2483 : ivas_sba_get_spar_hoa_ch_ind( num_channels, hDecoderConfig->ivas_total_brate, hMdDec->HOA_md_ind );
520 :
521 2483 : 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 2483 : 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 2483 : nchan_transport = hMdDec->spar_md_cfg.nchan_transport;
526 :
527 : /* get FB coefficients */
528 32279 : for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
529 : {
530 29796 : pFC[i] = ivas_fb_fcs_12band_1ms[i] * hDecoderConfig->output_Fs * 0.5f;
531 : }
532 :
533 2483 : if ( ( error = ivas_spar_set_dec_config( hMdDec, nchan_transport, pFC ) ) != IVAS_ERR_OK )
534 : {
535 0 : return error;
536 : }
537 :
538 2483 : 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 2483 : PR_minmax[0] = hMdDec->spar_md_cfg.quant_strat[0].PR.min;
545 2483 : PR_minmax[1] = hMdDec->spar_md_cfg.quant_strat[0].PR.max;
546 2483 : ivas_spar_quant_dtx_init( &hMdDec->spar_md, PR_minmax );
547 :
548 2483 : ivas_spar_arith_coeffs_com_init( &hMdDec->arith_coeffs, &hMdDec->spar_md_cfg, hMdDec->table_idx, DEC );
549 2483 : ivas_spar_huff_coeffs_com_init( &hMdDec->huff_coeffs, &hMdDec->spar_md_cfg, hMdDec->table_idx, DEC );
550 :
551 2483 : hMdDec->spar_md_cfg.prev_quant_idx = -1;
552 :
553 : /* initialize PLC state */
554 2483 : set_s( hMdDec->valid_bands, 0, IVAS_MAX_NUM_BANDS );
555 2483 : set_s( hMdDec->base_band_age, 0, IVAS_MAX_NUM_BANDS );
556 2483 : set_s( hMdDec->base_band_coeffs_age, 0, IVAS_MAX_NUM_BANDS );
557 2483 : hMdDec->spar_plc_num_lost_frames = 0;
558 2483 : hMdDec->spar_plc_enable_fadeout_flag = 1;
559 2483 : hMdDec->dtx_md_smoothing_cntr = 1;
560 :
561 2483 : ivas_clear_band_coeffs( hMdDec->spar_md.band_coeffs, IVAS_MAX_NUM_BANDS );
562 2483 : ivas_clear_band_coeffs( hMdDec->band_coeffs_prev, IVAS_MAX_NUM_BANDS );
563 2483 : ivas_clear_band_coeff_idx( hMdDec->spar_md.band_coeffs_idx, IVAS_MAX_NUM_BANDS );
564 2483 : ivas_clear_band_coeff_idx( hMdDec->spar_md_prev.band_coeffs_idx, IVAS_MAX_NUM_BANDS );
565 2483 : ivas_clear_band_coeff_idx( hMdDec->spar_md_prev.band_coeffs_idx_mapped, IVAS_MAX_NUM_BANDS );
566 :
567 2483 : hMdDec->spar_md.dtx_vad = 0;
568 2483 : hMdDec->td_decorr_flag = 1;
569 :
570 2483 : set_f( hMdDec->spar_md.en_ratio_slow, 0.0f, IVAS_MAX_NUM_BANDS );
571 2483 : set_f( hMdDec->spar_md.ref_pow_slow, 0.0f, IVAS_MAX_NUM_BANDS );
572 :
573 2483 : set_zero( hMdDec->smooth_fac, IVAS_MAX_NUM_BANDS );
574 32279 : for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
575 : {
576 29796 : set_zero( hMdDec->smooth_buf[i], 2 * SBA_DIRAC_NRG_SMOOTH_LONG + 1 );
577 : }
578 :
579 29796 : for ( i = 0; i < IVAS_SPAR_MAX_CH; i++ )
580 : {
581 327756 : for ( j = 0; j < IVAS_SPAR_MAX_CH; j++ )
582 : {
583 300443 : set_zero( hMdDec->mixer_mat_prev2[i][j], IVAS_MAX_NUM_BANDS );
584 : }
585 : }
586 2483 : hMdDec->first_valid_frame = 1;
587 :
588 2483 : hMdDec->spar_md.res_ind = 0;
589 2483 : hMdDec->spar_md.prior_dyn_active_w_flag = 0;
590 :
591 2483 : 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 2483 : 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 8512 : for ( i = 0; i < nchan_transport; i++ )
609 : {
610 6029 : hMdDec->spar_md_cfg.max_freq_per_chan[i] = ivas_spar_br_table_consts[hMdDec->table_idx].fpcs;
611 : }
612 :
613 2483 : 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 2483 : switch ( nchan )
616 : {
617 2104 : case 4: /* FOA_CHANNELS */
618 2104 : hMdDec->num_decorr = IVAS_TD_DECORR_OUT_3CH;
619 2104 : break;
620 31 : case 9: /* IVAS_HOA_2_CH */
621 31 : hMdDec->num_decorr = IVAS_TD_DECORR_OUT_5CH;
622 31 : break;
623 60 : case 6: /* IVAS_HOA_2_CH */
624 60 : hMdDec->num_decorr = IVAS_TD_DECORR_OUT_2CH;
625 60 : break;
626 149 : case 8: /* IVAS_HOA_3_CH */
627 149 : hMdDec->num_decorr = IVAS_TD_DECORR_OUT_4CH;
628 149 : break;
629 : }
630 :
631 2483 : hMdDec->spar_md_cfg.num_umx_chs = nchan;
632 :
633 2483 : dmx_ch = 0;
634 32279 : for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ )
635 : {
636 29796 : dmx_ch = 0;
637 102144 : for ( j = 0; j < nchan_transport; j++ )
638 : {
639 72348 : if ( pFC[i] < hMdDec->spar_md_cfg.max_freq_per_chan[j] )
640 : {
641 72348 : dmx_ch += 1;
642 : }
643 : }
644 :
645 29796 : hMdDec->spar_md_cfg.num_dmx_chans_per_band[i] = hMdDec->spar_md_cfg.nchan_transport;
646 29796 : hMdDec->spar_md_cfg.num_decorr_per_band[i] = nchan - hMdDec->spar_md_cfg.nchan_transport;
647 : }
648 :
649 2483 : hMdDec->spar_md_cfg.nchan_transport = dmx_ch;
650 :
651 2483 : return IVAS_ERR_OK;
652 : }
653 :
654 :
655 : /*-----------------------------------------------------------------------------------------*
656 : * Function ivas_dec_mono_sba_handling()
657 : *
658 : *
659 : *-----------------------------------------------------------------------------------------*/
660 :
661 232400 : 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 232400 : mono_flag = 1;
668 :
669 1180723 : for ( b = 0; b < st_ivas->hQMetaData->q_direction[0].cfg.nbands; b++ )
670 : {
671 4741615 : for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; ++block )
672 : {
673 3793292 : float azimuth = st_ivas->hQMetaData->q_direction[0].band_data[b].azimuth[block];
674 3793292 : float elevation = st_ivas->hQMetaData->q_direction[0].band_data[b].azimuth[block];
675 3793292 : float energy_ratio = st_ivas->hQMetaData->q_direction[0].band_data[0].energy_ratio[block];
676 3793292 : if (
677 726219 : ( azimuth != 0.0f ) ||
678 726219 : ( elevation != 0.0f ) ||
679 : ( energy_ratio > 0.15f ) ) /* 0.15f is just above the lowest quantised value. */
680 : {
681 3415074 : mono_flag = 0;
682 : }
683 : }
684 : }
685 :
686 : /* Combine the SPAR prediction coefs flag with the azimuth, elevation and energy ratio flag.*/
687 232400 : mono_flag = mono_flag && ivas_spar_chk_zero_coefs( st_ivas );
688 :
689 232400 : if ( mono_flag )
690 : {
691 : /* Set Energy Ratio values to be zero */
692 340 : for ( b = 0; b < st_ivas->hQMetaData->q_direction[0].cfg.nbands; b++ )
693 : {
694 255 : set_zero( st_ivas->hQMetaData->q_direction[0].band_data[b].energy_ratio, MAX_PARAM_SPATIAL_SUBFRAMES );
695 : }
696 85 : if ( st_ivas->hDirAC != NULL )
697 : {
698 448 : for ( block = 0; block < st_ivas->hSpatParamRendCom->dirac_md_buffer_length; ++block )
699 : {
700 : /* Set directional Energy Ratio values to be zero */
701 384 : set_zero( st_ivas->hSpatParamRendCom->energy_ratio1[block], st_ivas->hSpatParamRendCom->num_freq_bands );
702 384 : 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 384 : set_f( st_ivas->hSpatParamRendCom->diffuseness_vector[block], 1.0f, st_ivas->hSpatParamRendCom->num_freq_bands );
708 : }
709 : }
710 : }
711 :
712 232400 : return;
713 : }
714 :
715 :
716 : /*-----------------------------------------------------------------------------------------*
717 : * Function ivas_spar_md_dec_process()
718 : *
719 : * SPAR Meta Data decoder process
720 : *-----------------------------------------------------------------------------------------*/
721 :
722 232400 : 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 232400 : hMdDec = st_ivas->hSpar->hMdDec;
737 :
738 232400 : active_w_vlbr = ( st_ivas->hDecoderConfig->ivas_total_brate < IVAS_24k4 ) ? 1 : 0;
739 :
740 232400 : num_md_chs = ivas_sba_get_nchan_metadata( sba_order, st_ivas->hDecoderConfig->ivas_total_brate );
741 :
742 232400 : 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 232400 : if ( hMdDec->spar_md_cfg.nchan_transport > 1 && hMdDec->spar_md_cfg.nchan_transport <= 3 )
745 : {
746 89961 : hMdDec->spar_md.res_ind = 0;
747 89961 : dyn_active_w_flag = get_next_indice( st0, 1 );
748 89961 : if ( dyn_active_w_flag == 1 )
749 : {
750 127 : if ( hMdDec->spar_md_cfg.nchan_transport == 2 )
751 : {
752 127 : hMdDec->spar_md.res_ind = get_next_indice( st0, 1 );
753 127 : 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 142439 : dyn_active_w_flag = 0;
764 142439 : if ( hMdDec->spar_md_cfg.nchan_transport == FOA_CHANNELS )
765 : {
766 102876 : get_next_indice( st0, 1 );
767 : }
768 : }
769 :
770 232400 : ivas_spar_dec_parse_md_bs( hMdDec, st0, &nB, &bw, &dtx_vad, st_ivas->hDecoderConfig->ivas_total_brate,
771 232400 : st_ivas->hQMetaData->sba_inactive_mode );
772 :
773 232400 : assert( nB == hMdDec->spar_md.num_bands );
774 232400 : assert( bw == 1 );
775 232400 : ivas_spar_md_fill_invalid_bandcoeffs(
776 : hMdDec->spar_md.band_coeffs,
777 : hMdDec->band_coeffs_prev,
778 232400 : &hMdDec->valid_bands[0],
779 : &hMdDec->base_band_coeffs_age[0],
780 : &hMdDec->first_valid_frame,
781 : nB );
782 :
783 232400 : ivas_dec_mono_sba_handling( st_ivas );
784 :
785 : /* SPAR to DirAC conversion */
786 232400 : if ( hMdDec->spar_hoa_dirac2spar_md_flag == 1 )
787 : {
788 205905 : 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 232400 : 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 814340 : for ( i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
829 : {
830 5483016 : for ( b = 0; b < hMdDec->spar_md.num_bands; b++ )
831 : {
832 53911836 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
833 : {
834 49010760 : 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 39208608 : for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
838 : {
839 137230128 : for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
840 : {
841 102922596 : 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 53911836 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
846 : {
847 49010760 : 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 232400 : 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 232400 : 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 232400 : hMdDec->dtx_md_smoothing_cntr = 1;
869 :
870 232400 : return;
871 : }
872 :
873 :
874 : /*-----------------------------------------------------------------------------------------*
875 : * Function ivas_spar_chk_zero_coefs()
876 : *
877 : * Check for zeroed SPAR coefficients
878 : *-----------------------------------------------------------------------------------------*/
879 :
880 776 : 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 776 : int16_t mono = 1;
887 : int16_t ndec, ndm;
888 :
889 776 : hMdDec = st_ivas->hSpar->hMdDec;
890 776 : ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
891 776 : ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
892 :
893 6984 : for ( b = 0; b < min( hMdDec->spar_md.num_bands, SPAR_DIRAC_SPLIT_START_BAND ); b++ )
894 : {
895 24912 : for ( j = 0; j < ndm + ndec - 1; j++ )
896 : {
897 18704 : if ( hMdDec->spar_md.band_coeffs[b].pred_re[j] != 0.0f )
898 : {
899 9009 : mono = 0;
900 : }
901 : }
902 22424 : for ( j = 0; j < ndec; j++ )
903 : {
904 19144 : for ( k = 0; k < ndm - 1; k++ )
905 : {
906 2928 : if ( hMdDec->spar_md.band_coeffs[b].C_re[j][k] != 0.0f )
907 : {
908 716 : mono = 0;
909 : }
910 : }
911 : }
912 22424 : for ( j = 0; j < ndec; j++ )
913 : {
914 16216 : if ( hMdDec->spar_md.band_coeffs[b].P_re[j] != 0.0f )
915 : {
916 14581 : mono = 0;
917 : }
918 : }
919 : }
920 :
921 776 : 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 1327 : 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 1327 : ramp = (float) hMdDec->dtx_md_smoothing_cntr / IVAS_DEFAULT_DTX_CNG_RAMP;
941 :
942 17251 : for ( b = 0; b < num_bands_out; b++ )
943 : {
944 15924 : dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[b];
945 :
946 63696 : for ( j = 1; j < FOA_CHANNELS; j++ )
947 : {
948 170496 : for ( k = dmx_ch; k < FOA_CHANNELS; k++ )
949 : {
950 122724 : prev = hMdDec->spar_coeffs_prev.P_re[j][k][b];
951 122724 : tar = hMdDec->spar_coeffs_tar.P_re[j][k][b];
952 122724 : new_val = prev + ( ramp * ( tar - prev ) );
953 122724 : hMdDec->spar_coeffs.P_re[j][k][b] = new_val;
954 : }
955 : }
956 :
957 79620 : for ( j = 0; j < FOA_CHANNELS; j++ )
958 : {
959 154848 : for ( k = 0; k < dmx_ch; k++ )
960 : {
961 91152 : prev = hMdDec->spar_coeffs_prev.C_re[j][k][b];
962 91152 : tar = hMdDec->spar_coeffs_tar.C_re[j][k][b];
963 91152 : new_val = prev + ( ramp * ( tar - prev ) );
964 91152 : hMdDec->spar_coeffs.C_re[j][k][b] = new_val;
965 : }
966 : }
967 : }
968 :
969 : /* expand MD to all time slots */
970 5158 : for ( int16_t i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
971 : {
972 49803 : for ( b = 0; b < num_bands_out; b++ )
973 : {
974 45972 : dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[b];
975 :
976 183888 : for ( j = 1; j < FOA_CHANNELS; j++ )
977 : {
978 489888 : for ( k = dmx_ch; k < FOA_CHANNELS; k++ )
979 :
980 : {
981 351972 : 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 229860 : for ( j = 0; j < FOA_CHANNELS; j++ )
986 : {
987 450144 : for ( k = 0; k < dmx_ch; k++ )
988 : {
989 266256 : 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 1327 : hMdDec->dtx_md_smoothing_cntr = min( hMdDec->dtx_md_smoothing_cntr + 1, IVAS_DEFAULT_DTX_CNG_RAMP );
996 :
997 1327 : 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 76 : 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 76 : num_channels = hMdDec->spar_md_cfg.num_umx_chs;
1017 :
1018 380 : for ( i = 0; i < num_channels; i++ )
1019 : {
1020 1520 : for ( j = 0; j < num_channels; j++ )
1021 : {
1022 15808 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1023 : {
1024 14592 : hMdDec->spar_coeffs_prev.C_re[i][j][k] = hMdDec->spar_coeffs_tar.C_re[i][j][k];
1025 : }
1026 : }
1027 : }
1028 :
1029 380 : for ( i = 0; i < num_channels; i++ )
1030 : {
1031 1520 : for ( j = 0; j < num_channels; j++ )
1032 : {
1033 15808 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1034 : {
1035 14592 : hMdDec->spar_coeffs_prev.P_re[i][j][k] = hMdDec->spar_coeffs_tar.P_re[i][j][k];
1036 : }
1037 : }
1038 : }
1039 :
1040 380 : for ( i = 0; i < num_channels; i++ )
1041 : {
1042 1520 : for ( j = 0; j < num_channels; j++ )
1043 : {
1044 15808 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1045 : {
1046 14592 : hMdDec->spar_coeffs_tar.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
1047 : }
1048 : }
1049 : }
1050 :
1051 380 : for ( i = 0; i < num_channels; i++ )
1052 : {
1053 1520 : for ( j = 0; j < num_channels; j++ )
1054 : {
1055 15808 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1056 : {
1057 14592 : hMdDec->spar_coeffs_tar.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
1058 : }
1059 : }
1060 : }
1061 :
1062 76 : ivas_spar_smooth_md_dtx( hMdDec, num_bands_out, num_md_sub_frames );
1063 :
1064 76 : return;
1065 : }
1066 :
1067 :
1068 : /*-----------------------------------------------------------------------------------------*
1069 : * Function ivas_spar_update_md_hist()
1070 : *
1071 : * Update previous and target MD
1072 : *-----------------------------------------------------------------------------------------*/
1073 :
1074 232324 : 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 232324 : num_channels = hMdDec->spar_md_cfg.num_umx_chs;
1081 :
1082 1412779 : for ( i = 0; i < num_channels; i++ )
1083 : {
1084 8407702 : for ( j = 0; j < num_channels; j++ )
1085 : {
1086 93954211 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1087 : {
1088 86726964 : hMdDec->spar_coeffs_prev.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
1089 : }
1090 : }
1091 : }
1092 :
1093 1412779 : for ( i = 0; i < num_channels; i++ )
1094 : {
1095 8407702 : for ( j = 0; j < num_channels; j++ )
1096 : {
1097 93954211 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1098 : {
1099 86726964 : hMdDec->spar_coeffs_prev.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
1100 : }
1101 : }
1102 : }
1103 :
1104 1412779 : for ( i = 0; i < num_channels; i++ )
1105 : {
1106 8407702 : for ( j = 0; j < num_channels; j++ )
1107 : {
1108 93954211 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1109 : {
1110 86726964 : hMdDec->spar_coeffs_tar.C_re[i][j][k] = hMdDec->spar_coeffs.C_re[i][j][k];
1111 : }
1112 : }
1113 : }
1114 :
1115 1412779 : for ( i = 0; i < num_channels; i++ )
1116 : {
1117 8407702 : for ( j = 0; j < num_channels; j++ )
1118 : {
1119 93954211 : for ( k = 0; k < IVAS_MAX_NUM_BANDS; k++ )
1120 : {
1121 86726964 : hMdDec->spar_coeffs_tar.P_re[i][j][k] = hMdDec->spar_coeffs.P_re[i][j][k];
1122 : }
1123 : }
1124 : }
1125 :
1126 232324 : return;
1127 : }
1128 :
1129 :
1130 : /*-----------------------------------------------------------------------------------------*
1131 : * Function ivas_get_spar_matrices()
1132 : *
1133 : * Get SPAR matrices
1134 : *-----------------------------------------------------------------------------------------*/
1135 :
1136 232400 : 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 232400 : num_bands = num_bands_out;
1154 232400 : order = remix_order_set[hMdDec->spar_md_cfg.remix_unmix_order];
1155 :
1156 232400 : split_band = SPAR_DIRAC_SPLIT_START_BAND;
1157 232400 : 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 232400 : if ( bw == IVAS_RED_BAND_FACT )
1173 : {
1174 0 : num_bands = num_bands >> 1;
1175 : }
1176 :
1177 232400 : active_w = ( dyn_active_w_flag == 1 ) || ( hMdDec->spar_md_cfg.active_w == 1 );
1178 232400 : 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 1046740 : for ( i_ts = 0; i_ts < n_ts; i_ts++ )
1181 : {
1182 4533903 : for ( i = 0; i < numch_out; i++ )
1183 : {
1184 22722570 : for ( j = 0; j < numch_out; j++ )
1185 : {
1186 19003007 : set_zero( &hMdDec->spar_coeffs.C_re[i][j][i_ts * IVAS_MAX_NUM_BANDS], IVAS_MAX_NUM_BANDS );
1187 19003007 : set_zero( &hMdDec->spar_coeffs.P_re[i][j][i_ts * IVAS_MAX_NUM_BANDS], IVAS_MAX_NUM_BANDS );
1188 : }
1189 : }
1190 814340 : num_bands = min( num_bands, nB );
1191 :
1192 10491320 : 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 9676980 : dmx_ch = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bw * b];
1199 :
1200 53915336 : for ( j = 0; j < numch_out; j++ )
1201 : {
1202 44238356 : set_zero( tmp_C1_re[j], numch_out );
1203 44238356 : set_zero( tmp_C2_re[j], numch_out );
1204 44238356 : set_zero( tmp_dm_re[j], numch_out );
1205 :
1206 44238356 : tmp_C1_re[j][j] = 1.0f;
1207 44238356 : tmp_C2_re[j][j] = 1.0f;
1208 44238356 : tmp_dm_re[j][j] = 1.0f;
1209 : }
1210 :
1211 44238356 : for ( j = 1; j < numch_out; j++ )
1212 : {
1213 34561376 : tmp_C1_re[j][0] = hMdDec->spar_md.band_coeffs[b + i_ts * IVAS_MAX_NUM_BANDS].pred_re[j - 1];
1214 : }
1215 :
1216 9676980 : if ( active_w == 1 )
1217 : {
1218 5767968 : for ( j = 1; j < numch_out; j++ )
1219 : {
1220 4325976 : 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 1441992 : IVAS_RMULT_FLOAT( tmp_C2_re[0][1], tmp_C1_re[1][0], re );
1224 1441992 : tmp_dm_re[0][0] = 1 + re;
1225 :
1226 1441992 : IVAS_RMULT_FLOAT( tmp_C2_re[0][2], tmp_C1_re[2][0], re );
1227 1441992 : tmp_dm_re[0][0] += re;
1228 :
1229 1441992 : IVAS_RMULT_FLOAT( tmp_C2_re[0][3], tmp_C1_re[3][0], re );
1230 1441992 : tmp_dm_re[0][0] += re;
1231 :
1232 1441992 : if ( dyn_active_w_flag == 1 )
1233 : {
1234 6096 : tmp_dm_re[0][0] *= IVAS_SPAR_DYN_ACTIVEW_THRESH;
1235 : }
1236 :
1237 1441992 : tmp_dm_re[0][1] = tmp_C2_re[0][1];
1238 :
1239 1441992 : tmp_dm_re[0][2] = tmp_C2_re[0][2];
1240 :
1241 1441992 : tmp_dm_re[0][3] = tmp_C2_re[0][3];
1242 :
1243 1441992 : tmp_dm_re[1][0] = tmp_C1_re[1][0];
1244 :
1245 1441992 : tmp_dm_re[2][0] = tmp_C1_re[2][0];
1246 :
1247 1441992 : tmp_dm_re[3][0] = tmp_C1_re[3][0];
1248 :
1249 1441992 : if ( hMdDec->spar_md_cfg.remix_unmix_order != 3 )
1250 : {
1251 1441992 : ivas_mat_col_rearrange( tmp_dm_re, order, i_ts, hMdDec->mixer_mat, b, numch_out );
1252 : }
1253 : }
1254 : else
1255 : {
1256 8234988 : if ( hMdDec->spar_md_cfg.remix_unmix_order != 3 )
1257 : {
1258 8234988 : ivas_mat_col_rearrange( tmp_C1_re, order, i_ts, hMdDec->mixer_mat, b, numch_out );
1259 : }
1260 : }
1261 :
1262 9676980 : 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 53915336 : for ( j = 0; j < numch_out; j++ )
1268 : {
1269 44238356 : set_zero( tmpC_re[j], numch_out );
1270 44238356 : set_zero( tmpP_re[j], numch_out );
1271 : }
1272 :
1273 53915336 : for ( j = 0; j < numch_out; j++ )
1274 : {
1275 44238356 : set_zero( tmpC_re[j], numch_out );
1276 : }
1277 :
1278 37124836 : for ( k = 0; k < dmx_ch; k++ )
1279 : {
1280 27447856 : tmpC_re[k][k] = 1;
1281 : }
1282 :
1283 26467480 : for ( j = dmx_ch; j < numch_out; j++ )
1284 : {
1285 41938992 : for ( k = 1; k < dmx_ch; k++ )
1286 : {
1287 25148492 : 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 26467480 : for ( j = dmx_ch; j < numch_out; j++ )
1292 : {
1293 69292824 : for ( k = dmx_ch; k < numch_out; k++ )
1294 : {
1295 52502324 : if ( ( j - dmx_ch ) == ( k - dmx_ch ) )
1296 : {
1297 16790500 : 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 35711824 : tmpP_re[j][k] = 0;
1302 : }
1303 : }
1304 : }
1305 :
1306 44238356 : for ( j = 1; j < numch_out; j++ )
1307 : {
1308 112212192 : for ( k = dmx_ch; k < numch_out; k++ )
1309 : {
1310 620394976 : for ( m = 0; m < numch_out; m++ )
1311 : {
1312 542744160 : IVAS_RMULT_FLOAT( hMdDec->mixer_mat[j][m][b + i_ts * IVAS_MAX_NUM_BANDS], tmpP_re[m][k], re );
1313 542744160 : hMdDec->spar_coeffs.P_re[j][k][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] += re;
1314 : }
1315 : }
1316 : }
1317 :
1318 53915336 : for ( j = 0; j < numch_out; j++ )
1319 : {
1320 176151524 : for ( k = 0; k < dmx_ch; k++ )
1321 : {
1322 857170080 : for ( m = 0; m < numch_out; m++ )
1323 : {
1324 725256912 : IVAS_RMULT_FLOAT( hMdDec->mixer_mat[j][m][b + i_ts * IVAS_MAX_NUM_BANDS], tmpC_re[m][k], re );
1325 725256912 : hMdDec->spar_coeffs.C_re[j][k][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] += re;
1326 : }
1327 : }
1328 : }
1329 :
1330 9676980 : hMdDec->spar_coeffs.C_re[0][0][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] =
1331 9676980 : max( 0, hMdDec->spar_coeffs.C_re[0][0][( b * bw ) + i_ts * IVAS_MAX_NUM_BANDS] );
1332 : }
1333 : }
1334 :
1335 : /* band mixing */
1336 814340 : 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 232400 : return;
1361 : }
1362 :
1363 :
1364 : /*-----------------------------------------------------------------------------------------*
1365 : * Function ivas_mat_col_rearrange()
1366 : *
1367 : * reorders the input matrix based on order
1368 : *-----------------------------------------------------------------------------------------*/
1369 :
1370 9676980 : 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 53915336 : for ( i = 0; i < num_ch; i++ )
1381 : {
1382 44238356 : idx = order[i];
1383 :
1384 270592840 : for ( j = 0; j < num_ch; j++ )
1385 : {
1386 226354484 : mixer_mat[j][i][bands + i_ts * IVAS_MAX_NUM_BANDS] = in_re[j][idx];
1387 : }
1388 : }
1389 :
1390 9676980 : return;
1391 : }
1392 :
1393 :
1394 : /*-----------------------------------------------------------------------------------------*
1395 : * Function ivas_spar_dec_gen_umx_mat()
1396 : *
1397 : * generates upmix matrix
1398 : *-----------------------------------------------------------------------------------------*/
1399 :
1400 295403 : 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 295403 : num_out_ch = hMdDec->spar_md_cfg.num_umx_chs;
1410 :
1411 1345870 : for ( i_ts = 0; i_ts < num_md_sub_frames; i_ts++ )
1412 : {
1413 1050467 : if ( hMdDec->td_decorr_flag == 1 )
1414 : {
1415 5712715 : for ( i = 0; i < num_out_ch; i++ )
1416 : {
1417 17393764 : for ( j = 0; j < nchan_transport; j++ )
1418 : {
1419 164536188 : for ( b = 0; b < num_bands_out; b++ )
1420 : {
1421 151804672 : 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 5712715 : for ( i = 0; i < num_out_ch; i++ )
1427 : {
1428 14716332 : for ( j = nchan_transport; j < num_out_ch; j++ )
1429 : {
1430 129732740 : for ( b = 0; b < num_bands_out; b++ )
1431 : {
1432 119678656 : 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 295403 : ivas_spar_dec_compute_ramp_down_post_matrix( hMdDec, num_bands_out, bfi, num_md_sub_frames );
1473 :
1474 295403 : return;
1475 : }
1476 :
1477 12501 : 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 12501 : bw_fact = *bands_bw / bw_final;
1489 61271 : for ( i = *nB - 1; i >= 0; i-- )
1490 : {
1491 :
1492 148778 : for ( b = bw_fact - 1; b >= 0; b-- )
1493 : {
1494 100008 : idx = i * bw_fact + b;
1495 400032 : for ( ii = 0; ii < ndec + ndm - 1; ii++ )
1496 : {
1497 300024 : band_coeffs[idx].pred_re[ii] = band_coeffs[i].pred_re[ii];
1498 : }
1499 397088 : for ( ii = 0; ii < ndec; ii++ )
1500 : {
1501 302968 : for ( jj = 0; jj < ndm - 1; jj++ )
1502 : {
1503 5888 : band_coeffs[idx].C_re[ii][jj] = band_coeffs[i].C_re[ii][jj];
1504 : }
1505 : }
1506 397088 : for ( jj = 0; jj < ndec; jj++ )
1507 : {
1508 297080 : band_coeffs[idx].P_re[jj] = band_coeffs[i].P_re[jj];
1509 : }
1510 100008 : valid_bands[idx] = valid_bands[i];
1511 : }
1512 : }
1513 12501 : *nB = ( *nB ) * ( *bands_bw ) / bw_final;
1514 12501 : *bands_bw = bw_final;
1515 :
1516 12501 : return;
1517 : }
1518 :
1519 : /*-----------------------------------------------------------------------------------------*
1520 : * Function ivas_spar_dec_parse_md_bs()
1521 : *
1522 : * Parse SPAR MD bitstream
1523 : *-----------------------------------------------------------------------------------------*/
1524 :
1525 232400 : 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 232400 : *dtx_vad = 1;
1543 232400 : *bands_bw = 1;
1544 232400 : qsi = 0;
1545 232400 : num_bands = hMdDec->spar_md.num_bands;
1546 :
1547 232400 : if ( ivas_total_brate > IVAS_SID_5k2 )
1548 : {
1549 232186 : if ( hMdDec->spar_md_cfg.quant_strat_bits > 0 )
1550 : {
1551 232186 : 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 102876 : qsi = get_next_indice( st0, 1 );
1555 102876 : if ( qsi == 1 )
1556 : {
1557 0 : *dtx_vad = 0;
1558 : }
1559 : }
1560 : else
1561 : {
1562 129310 : if ( sba_inactive_mode == 1 )
1563 : {
1564 403 : *dtx_vad = 0;
1565 403 : qsi = hMdDec->spar_md_cfg.quant_strat_bits + 1;
1566 : }
1567 : else
1568 : {
1569 128907 : 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 214 : *dtx_vad = 0;
1581 : }
1582 :
1583 232400 : hMdDec->dtx_vad = *dtx_vad;
1584 :
1585 232400 : if ( *dtx_vad == 0 )
1586 : {
1587 617 : *nB = SPAR_DTX_BANDS;
1588 617 : *bands_bw = num_bands / *nB;
1589 :
1590 1851 : for ( i = 0; i < *nB; i++ )
1591 : {
1592 13574 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
1593 : {
1594 12340 : hMdDec->spar_md.band_coeffs[i].pred_re[j] = 0;
1595 12340 : hMdDec->spar_md.band_coeffs[i].P_re[j] = 0;
1596 : }
1597 1234 : hMdDec->valid_bands[i] = 1;
1598 : }
1599 :
1600 5553 : for ( i = 0; i < num_bands; i++ )
1601 : {
1602 39488 : for ( j = 0; j < ( IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS ); j++ )
1603 : {
1604 138208 : for ( k = 0; k < ( IVAS_SPAR_MAX_DMX_CHS - 1 ); k++ )
1605 : {
1606 103656 : hMdDec->spar_md.band_coeffs[i].C_re[j][k] = 0;
1607 : }
1608 : }
1609 : }
1610 :
1611 617 : 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 617 : if ( *bands_bw != 1 )
1614 : {
1615 617 : ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
1616 617 : ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
1617 617 : ivas_spar_md_band_upmix(
1618 : hMdDec->spar_md.band_coeffs,
1619 : nB,
1620 : bands_bw,
1621 617 : hMdDec->valid_bands,
1622 : 1,
1623 : ndec,
1624 : ndm );
1625 : }
1626 :
1627 617 : return;
1628 : }
1629 :
1630 231783 : qs = hMdDec->spar_md_cfg.quant_strat[qsi];
1631 :
1632 231783 : strat = get_next_indice( st0, 3 );
1633 :
1634 231783 : no_ec = 0;
1635 :
1636 231783 : if ( strat < 2 )
1637 : {
1638 116083 : *bands_bw = strat + 1;
1639 116083 : *nB = num_bands / *bands_bw;
1640 1231211 : for ( i = 0; i < *nB; i++ )
1641 : {
1642 1115128 : do_diff[i] = 0;
1643 1115128 : do_repeat[i] = 0;
1644 : }
1645 : }
1646 115700 : else if ( strat < 4 )
1647 : {
1648 10354 : *bands_bw = strat - 1;
1649 10354 : *nB = num_bands / *bands_bw;
1650 92810 : for ( i = 0; i < *nB; i++ )
1651 : {
1652 82456 : do_diff[i] = 0;
1653 82456 : do_repeat[i] = 0;
1654 : }
1655 10354 : no_ec = 1;
1656 : }
1657 105346 : else if ( ivas_total_brate < IVAS_24k4 )
1658 : {
1659 11448 : *bands_bw = 2;
1660 11448 : *nB = num_bands / *bands_bw;
1661 :
1662 57240 : for ( i = 0; i < *nB; i++ )
1663 : {
1664 45792 : do_diff[i] = 0;
1665 45792 : do_repeat[i] = ( ( strat % 2 ) == ( ( i + 1 ) % 2 ) );
1666 : }
1667 : }
1668 : else
1669 : {
1670 93898 : *bands_bw = 1;
1671 93898 : *nB = num_bands;
1672 :
1673 845082 : for ( i = 0; i < *nB; i++ )
1674 : {
1675 751184 : do_diff[i] = ( ( ( i + 1 ) & 3 ) != strat - 4 );
1676 751184 : do_repeat[i] = 0;
1677 : }
1678 93898 : if ( hMdDec->spar_md_cfg.prev_quant_idx >= 0 )
1679 : {
1680 93876 : 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 231783 : hMdDec->spar_md_cfg.prev_quant_idx = qsi;
1687 :
1688 231783 : if ( no_ec == 0 )
1689 : {
1690 221429 : ivas_decode_arith_bs( hMdDec, st0, qsi, *nB, *bands_bw, do_diff, strat, ivas_total_brate );
1691 : }
1692 : else
1693 : {
1694 10354 : ivas_decode_huffman_bs( hMdDec, st0, qsi, *nB, *bands_bw );
1695 : }
1696 :
1697 2226343 : for ( i = 0; i < *nB; i++ )
1698 : {
1699 1994560 : ndec = hMdDec->spar_md_cfg.num_decorr_per_band[( *bands_bw ) * i];
1700 1994560 : ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[( *bands_bw ) * i];
1701 :
1702 1994560 : 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 1994560 : j = 0;
1705 6972412 : for ( ii = 0; ii < ndec; ii++ )
1706 : {
1707 15453064 : for ( jj = 0; jj < ndm - 1; jj++ )
1708 : {
1709 10475212 : quant[j] = hMdDec->spar_md.band_coeffs[i].C_re[ii][jj];
1710 10475212 : j++;
1711 : }
1712 : }
1713 :
1714 1994560 : 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 1994560 : j = 0;
1717 6972412 : for ( ii = 0; ii < ndec; ii++ )
1718 : {
1719 15453064 : for ( jj = 0; jj < ndm - 1; jj++ )
1720 : {
1721 10475212 : hMdDec->spar_md.band_coeffs[i].C_re[ii][jj] = quant[j];
1722 10475212 : j++;
1723 : }
1724 : }
1725 :
1726 1994560 : 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 10992148 : for ( j = 0; j < ndm + ndec - 1; j++ )
1730 : {
1731 8997588 : 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 12469772 : for ( j = 0; j < ndec * ( ndm - 1 ); j++ )
1734 : {
1735 10475212 : 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 6972412 : for ( j = 0; j < ndec; j++ )
1738 : {
1739 4977852 : 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 1994560 : hMdDec->valid_bands[i] |= ( do_diff[i] == 0 && do_repeat[i] == 0 ) ? 1 : 0;
1742 : }
1743 :
1744 231783 : ndec = hMdDec->spar_md_cfg.num_decorr_per_band[0];
1745 231783 : ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[0];
1746 231783 : if ( *bands_bw != 1 )
1747 : {
1748 11884 : ivas_spar_md_band_upmix(
1749 : hMdDec->spar_md.band_coeffs,
1750 : nB,
1751 : bands_bw,
1752 11884 : hMdDec->valid_bands,
1753 : 1,
1754 : ndec,
1755 : ndm );
1756 : }
1757 :
1758 :
1759 231783 : return;
1760 : }
1761 :
1762 :
1763 : /*-----------------------------------------------------------------------------------------*
1764 : * Function ivas_decode_arith_bs()
1765 : *
1766 : * Decode bitstream with arith decoder
1767 : *-----------------------------------------------------------------------------------------*/
1768 :
1769 221429 : 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 2133533 : for ( i = 0; i < nB; i++ )
1790 : {
1791 1912104 : ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bands_bw * i];
1792 1912104 : ndec = hMdDec->spar_md_cfg.num_decorr_per_band[bands_bw * i];
1793 1912104 : if ( ( ivas_total_brate < IVAS_24k4 ) && ( strat > 3 ) && ( ( ( i % 2 == 1 ) && ( strat % 2 == 0 ) ) || ( ( i % 2 == 0 ) && ( strat % 2 == 1 ) ) ) )
1794 : {
1795 22896 : pred_cell_dims[i].dim1 = 0;
1796 22896 : pred_cell_dims[i].dim2 = 0;
1797 22896 : drct_cell_dims[i].dim1 = 0;
1798 22896 : drct_cell_dims[i].dim2 = 0;
1799 22896 : decd_cell_dims[i].dim1 = 0;
1800 22896 : decd_cell_dims[i].dim2 = 0;
1801 22896 : decx_cell_dims[i].dim1 = 0;
1802 22896 : decx_cell_dims[i].dim2 = 0;
1803 : }
1804 : else
1805 : {
1806 1889208 : pred_cell_dims[i].dim1 = ndm + ndec - 1;
1807 1889208 : if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
1808 : {
1809 245556 : if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
1810 : {
1811 81852 : pred_cell_dims[i].dim1 -= ( FOA_CHANNELS - 1 );
1812 : }
1813 : }
1814 1889208 : pred_cell_dims[i].dim2 = 1;
1815 1889208 : drct_cell_dims[i].dim1 = ndec;
1816 1889208 : drct_cell_dims[i].dim2 = ndm - 1;
1817 1889208 : decd_cell_dims[i].dim1 = ndec;
1818 1889208 : decd_cell_dims[i].dim2 = 1;
1819 1889208 : decx_cell_dims[i].dim1 = ( ndec * ( ndec - 1 ) ) >> 1;
1820 1889208 : decx_cell_dims[i].dim2 = 1;
1821 : }
1822 : }
1823 :
1824 221429 : any_diff = 0;
1825 1405999 : for ( i = 0; i < nB; i++ )
1826 : {
1827 1278468 : if ( pDo_diff[i] != 0 )
1828 : {
1829 93898 : any_diff = 1;
1830 93898 : break;
1831 : }
1832 : }
1833 :
1834 221429 : if ( any_diff == 1 )
1835 : {
1836 93898 : 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 93898 : 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 221429 : 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 221429 : ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, pred_cell_dims, PRED_COEFF );
1858 :
1859 221429 : if ( hMdDec->spar_hoa_md_flag && hMdDec->spar_hoa_dirac2spar_md_flag )
1860 : {
1861 266019 : for ( i = 0; i < nB; i++ )
1862 : {
1863 245556 : if ( i >= SPAR_DIRAC_SPLIT_START_BAND )
1864 : {
1865 363244 : for ( j = pred_cell_dims[i].dim1 - 1; j >= 0; j-- )
1866 : {
1867 281392 : hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j + ( FOA_CHANNELS - 1 )] =
1868 281392 : hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j];
1869 : }
1870 327408 : for ( j = 0; j < FOA_CHANNELS - 1; j++ )
1871 : {
1872 245556 : hMdDec->spar_md.band_coeffs_idx[i].pred_index_re[j] = 0;
1873 : }
1874 : }
1875 : }
1876 : }
1877 :
1878 221429 : if ( any_diff == 1 )
1879 : {
1880 93898 : 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 221429 : 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 221429 : ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, drct_cell_dims, DRCT_COEFF );
1887 :
1888 221429 : if ( any_diff == 1 )
1889 : {
1890 93898 : 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 221429 : 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 221429 : ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, decd_cell_dims, DECD_COEFF );
1897 :
1898 221429 : if ( any_diff == 1 )
1899 : {
1900 93898 : 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 221429 : ivas_fill_band_coeffs_idx( hMdDec->spar_md.band_coeffs_idx, nB, symbol_arr_re, decx_cell_dims, DECX_COEFF );
1904 :
1905 221429 : return;
1906 : }
1907 :
1908 :
1909 : /*-----------------------------------------------------------------------------------------*
1910 : * Function ivas_fill_band_coeffs_idx()
1911 : *
1912 : * Copy pred band coeffs to arr
1913 : *-----------------------------------------------------------------------------------------*/
1914 :
1915 885716 : 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 885716 : int16_t *pPtr_idx = NULL;
1924 :
1925 8534132 : for ( i = 0; i < nB; i++ )
1926 : {
1927 7648416 : switch ( coeff_type )
1928 : {
1929 1912104 : case PRED_COEFF:
1930 : {
1931 1912104 : pPtr_idx = pBands_idx[i].pred_index_re;
1932 1912104 : break;
1933 : }
1934 1912104 : case DRCT_COEFF:
1935 : {
1936 1912104 : pPtr_idx = pBands_idx[i].drct_index_re;
1937 1912104 : break;
1938 : }
1939 1912104 : case DECD_COEFF:
1940 : {
1941 1912104 : pPtr_idx = pBands_idx[i].decd_index_re;
1942 1912104 : break;
1943 : }
1944 1912104 : case DECX_COEFF:
1945 : {
1946 1912104 : break;
1947 : }
1948 :
1949 0 : default:
1950 0 : assert( !"unsupported config!" );
1951 : }
1952 :
1953 7648416 : if ( coeff_type != DECX_COEFF )
1954 : {
1955 5736312 : len = pCell_dims[i].dim1 * pCell_dims[i].dim2;
1956 5736312 : mvs2s( pSymbol_re, pPtr_idx, len );
1957 5736312 : pSymbol_re += len;
1958 : }
1959 : }
1960 :
1961 885716 : return;
1962 : }
1963 :
1964 :
1965 : /*-----------------------------------------------------------------------------------------*
1966 : * Function ivas_decode_huffman_bs()
1967 : *
1968 : * Decode bitstream with huffman decoder
1969 : *-----------------------------------------------------------------------------------------*/
1970 :
1971 10354 : 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 92810 : for ( i = 0; i < nB; i++ )
1983 : {
1984 82456 : ndm = hMdDec->spar_md_cfg.num_dmx_chans_per_band[bands_bw * i];
1985 82456 : ndec = hMdDec->spar_md_cfg.num_decorr_per_band[bands_bw * i];
1986 :
1987 82456 : pred_dim = ndec + ndm - 1;
1988 82456 : drct_dim = ndec * ( ndm - 1 );
1989 82456 : decd_dim = ndec;
1990 82456 : pred_offset = 0;
1991 :
1992 82456 : 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 329824 : for ( j = pred_offset; j < pred_dim; j++ )
2001 : {
2002 247368 : 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 82456 : 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 84904 : for ( j = 0; j < drct_dim; j++ )
2017 : {
2018 2448 : 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 86784 : for ( j = 0; j < decd_dim; j++ )
2022 : {
2023 4328 : 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 10354 : return;
2028 : }
2029 :
2030 464800 : 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 464800 : set_s( valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
2042 464800 : set_s( last_valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
2043 464800 : idx = -1;
2044 464800 : *all_valid = 1;
2045 5276524 : for ( b = 0; b < num_bands; b++ )
2046 : {
2047 4811724 : if ( valid_bands[b] != 0 )
2048 : {
2049 4763920 : base_band_age[b] = 0; /* reset band age */
2050 4763920 : idx++;
2051 4763920 : valid_band_idx[idx] = b;
2052 : }
2053 : else
2054 : {
2055 47804 : base_band_age[b] += 1; /* increment the age of invalid bands */
2056 :
2057 47804 : if ( base_band_age[b] > 3 )
2058 : {
2059 5476 : last_valid_band_idx[b] = idx;
2060 : }
2061 47804 : *all_valid = 0;
2062 : }
2063 : }
2064 464800 : *b_idx = idx;
2065 :
2066 464800 : return;
2067 : }
2068 :
2069 5476 : 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 5476 : if ( last_valid_band_idx < 0 ) /* Extrapolation */
2079 : {
2080 1550 : *id1 = valid_band_idx[0];
2081 1550 : *id0 = 0;
2082 1550 : *w = 1;
2083 : }
2084 3926 : else if ( last_valid_band_idx == idx ) /* Extrapolation */
2085 : {
2086 430 : *id1 = valid_band_idx[last_valid_band_idx];
2087 430 : *id0 = valid_band_idx[last_valid_band_idx];
2088 430 : *w = 0;
2089 : }
2090 : else /* Interpolation */
2091 : {
2092 3496 : *id0 = valid_band_idx[last_valid_band_idx];
2093 3496 : *id1 = valid_band_idx[last_valid_band_idx + 1];
2094 3496 : *w = ( (float) ( b - *id0 ) ) / ( *id1 - *id0 );
2095 : }
2096 5476 : 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 232400 : 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 232400 : int16_t valid_band_idx[IVAS_MAX_NUM_BANDS], idx = -1;
2116 : int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS];
2117 232400 : float w = 0;
2118 232400 : 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 232400 : assert( idx > 0 ); /* some bands should be valid */
2121 :
2122 232400 : if ( all_valid == 0 )
2123 : {
2124 68530 : for ( b = 0; b < num_bands; b++ )
2125 : {
2126 : /* check against non zero in if and else if */
2127 63086 : if ( base_band_age[b] > 3 ) /* old invalid bands */
2128 : {
2129 : int16_t id0, id1;
2130 2738 : ivas_spar_get_plc_interp_weights( valid_band_idx, last_valid_band_idx[b],
2131 : idx, b, &w, &id0, &id1 );
2132 13690 : for ( i = 0; i < num_channels; i++ )
2133 : {
2134 54760 : for ( j = 0; j < num_channels; j++ )
2135 : {
2136 43808 : 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 43808 : 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 60348 : if ( valid_bands[b] == 0 )
2144 : {
2145 105820 : for ( i = 0; i < num_channels; i++ )
2146 : {
2147 423280 : for ( j = 0; j < num_channels; j++ )
2148 : {
2149 338624 : pSpar_coeffs->C_re[i][j][b] = pSpar_coeffs_prev->C_re[i][j][b];
2150 338624 : pSpar_coeffs->P_re[i][j][b] = pSpar_coeffs_prev->P_re[i][j][b];
2151 : }
2152 : }
2153 : }
2154 : }
2155 :
2156 63086 : if ( valid_bands[b] == 0 )
2157 : {
2158 : int16_t i_ts;
2159 119510 : for ( i = 0; i < num_channels; i++ )
2160 : {
2161 478040 : for ( j = 0; j < num_channels; j++ )
2162 : {
2163 1508608 : for ( i_ts = 1; i_ts < num_md_sub_frames; i_ts++ )
2164 : {
2165 1126176 : pSpar_coeffs->C_re[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] = pSpar_coeffs->C_re[i][j][b];
2166 1126176 : 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 232400 : return;
2175 : }
2176 :
2177 232400 : 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 232400 : int16_t valid_band_idx[IVAS_MAX_NUM_BANDS], idx = -1;
2187 : int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS];
2188 232400 : float w = 0;
2189 :
2190 232400 : 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 232400 : assert( idx > 0 ); /* some bands should be valid */
2194 :
2195 232400 : if ( all_valid == 0 )
2196 : {
2197 48996 : for ( b = 0; b < num_bands; b++ )
2198 : {
2199 : /* check against non zero in if and else if */
2200 43552 : if ( ( base_band_age[b] > 3 ) || ( *first_valid_frame == 0 ) ) /* old invalid bands */
2201 2738 : {
2202 : int16_t id0, id1;
2203 2738 : ivas_spar_get_plc_interp_weights( valid_band_idx, last_valid_band_idx[b],
2204 : idx, b, &w, &id0, &id1 );
2205 :
2206 30118 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
2207 : {
2208 27380 : pBand_coeffs[b].pred_re[j] = ( 1 - w ) * pBand_coeffs[id0].pred_re[j] + w * pBand_coeffs[id1].pred_re[j];
2209 : }
2210 :
2211 21904 : for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
2212 : {
2213 76664 : for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
2214 : {
2215 57498 : 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 30118 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
2220 : {
2221 27380 : 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 40814 : if ( valid_bands[b] == 0 )
2227 : {
2228 232804 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
2229 : {
2230 211640 : pBand_coeffs[b].pred_re[j] = pBand_coeffs_prev[b].pred_re[j];
2231 : }
2232 :
2233 169312 : for ( j = 0; j < IVAS_SPAR_MAX_CH - IVAS_SPAR_MAX_DMX_CHS; j++ )
2234 : {
2235 592592 : for ( k = 0; k < IVAS_SPAR_MAX_DMX_CHS - 1; k++ )
2236 : {
2237 444444 : pBand_coeffs[b].C_re[j][k] = pBand_coeffs_prev[b].C_re[j][k];
2238 : }
2239 : }
2240 :
2241 232804 : for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
2242 : {
2243 211640 : pBand_coeffs[b].P_re[j] = pBand_coeffs_prev[b].P_re[j];
2244 : }
2245 : }
2246 : }
2247 : }
2248 : }
2249 : else
2250 : {
2251 226956 : *first_valid_frame = 1;
2252 : }
2253 :
2254 232400 : return;
2255 : }
2256 :
2257 :
2258 : /*-----------------------------------------------------------------------------------------*
2259 : * Function ivas_spar_dec_compute_ramp_down_post_matrix()
2260 : *
2261 : *
2262 : *-----------------------------------------------------------------------------------------*/
2263 :
2264 295403 : 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 295403 : num_in_ch = hMdDec->spar_md_cfg.num_umx_chs;
2273 295403 : num_out_ch = hMdDec->spar_md_cfg.num_umx_chs;
2274 :
2275 295403 : if ( bfi == 0 )
2276 : {
2277 285344 : hMdDec->spar_plc_num_lost_frames = 0;
2278 : }
2279 : else
2280 : {
2281 10059 : if ( hMdDec->td_decorr_flag == 0 )
2282 : {
2283 0 : assert( 0 );
2284 : }
2285 :
2286 10059 : hMdDec->spar_plc_num_lost_frames += 1;
2287 10059 : hMdDec->spar_plc_num_lost_frames = min( hMdDec->spar_plc_num_lost_frames, 100 );
2288 :
2289 10059 : 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 2038 : num_fade_frames = max( hMdDec->spar_plc_num_lost_frames - ivas_spar_dec_plc_num_frames_keep, 0 );
2297 2038 : 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 2038 : gain = powf( 10, ( ( (float) gain_dB ) / 20 ) );
2299 :
2300 24456 : for ( i = 0; i < IVAS_SPAR_MAX_CH; i++ )
2301 : {
2302 22418 : 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 22418 : post_matrix[i] *= gain;
2304 : }
2305 :
2306 : /* apply the post matrix */
2307 10190 : for ( int16_t i_ts = 0; i_ts < num_md_sub_frames; i_ts++ )
2308 : {
2309 40760 : for ( i = 0; i < num_out_ch; i++ )
2310 : {
2311 163040 : for ( j = 0; j < num_in_ch; j++ )
2312 : {
2313 1611648 : for ( b = 0; b < num_bands_out; b++ )
2314 : {
2315 1481216 : hMdDec->mixer_mat[i][j][b + i_ts * IVAS_MAX_NUM_BANDS] *= post_matrix[i];
2316 : }
2317 : }
2318 : }
2319 : }
2320 : }
2321 : }
2322 :
2323 295403 : return;
2324 : }
2325 :
2326 :
2327 : /*-----------------------------------------------------------------------------------------*
2328 : * Function ivas_spar_unquant_dtx_indicies()
2329 : *
2330 : * Unquantize SPAR MD DYX indices
2331 : *-----------------------------------------------------------------------------------------*/
2332 :
2333 617 : 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 617 : pr_min_max[0] = pSpar_md->min_max[0];
2346 617 : pr_min_max[1] = pSpar_md->min_max[1];
2347 :
2348 1851 : for ( b = 0; b < nB; b++ )
2349 : {
2350 4936 : for ( i = 0; i < FOA_CHANNELS - 1; i++ )
2351 : {
2352 3702 : q_lvl = dtx_pr_real_q_levels[ndm_per_band[bw * b] - 1][i];
2353 3702 : idx = pSpar_md->band_coeffs_idx[b].pred_index_re[i];
2354 3702 : ivas_deindex_real_index( &idx, q_lvl, pr_min_max[0], pr_min_max[1], &val, 1 );
2355 3702 : pSpar_md->band_coeffs[b].pred_re[i] = val;
2356 : }
2357 :
2358 4200 : for ( i = 0; i < FOA_CHANNELS - ndm_per_band[bw * b]; i++ )
2359 : {
2360 2966 : q_lvl = dtx_pd_real_q_levels[ndm_per_band[bw * b] - 1][i];
2361 2966 : idx = pSpar_md->band_coeffs_idx[b].decd_index_re[i];
2362 2966 : ivas_deindex_real_index( &idx, q_lvl, dtx_pd_real_min_max[0], dtx_pd_real_min_max[1], &val, 1 );
2363 2966 : pSpar_md->band_coeffs[b].P_re[i] = val;
2364 : }
2365 : }
2366 :
2367 617 : return;
2368 : }
2369 :
2370 :
2371 : /*-----------------------------------------------------------------------------------------*
2372 : * Function ivas_parse_parameter_bitstream_dtx()
2373 : *
2374 : * parse DTX bitstream parameters
2375 : *-----------------------------------------------------------------------------------------*/
2376 :
2377 617 : 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 617 : sid_bits_len = st0->next_bit_pos;
2394 617 : pr_min_max[0] = pSpar_md->min_max[0];
2395 617 : pr_min_max[1] = pSpar_md->min_max[1];
2396 :
2397 1851 : for ( i = 0; i < num_bands; i++ )
2398 : {
2399 1234 : ndec = num_dec_per_band[bw * i];
2400 1234 : ndm = num_dmx_per_band[bw * i];
2401 :
2402 4936 : 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 3702 : pr_idx_1 = pr_pr_idx_pairs[ndm - 1][j][0];
2408 3702 : pr_idx_2 = pr_pr_idx_pairs[ndm - 1][j][1];
2409 3702 : pd_idx_1 = pr_pd_idx_pairs[ndm - 1][j][0];
2410 3702 : pd_idx_2 = pr_pd_idx_pairs[ndm - 1][j][1];
2411 :
2412 3702 : if ( pr_idx_1 != 0 || pd_idx_1 != 0 || pr_idx_2 != 0 || pd_idx_2 != 0 )
2413 : {
2414 3702 : pr_q_lvls = dtx_pr_real_q_levels[ndm - 1][pd_idx_1 - 1];
2415 :
2416 3702 : if ( ( j + 1 ) > ndec )
2417 : {
2418 736 : pd_q_lvls = 1;
2419 : }
2420 : else
2421 : {
2422 2966 : pd_q_lvls = dtx_pd_real_q_levels[ndm - 1][pd_idx_2 - 1];
2423 : }
2424 :
2425 3702 : pr_pd_bits = ivas_get_bits_to_encode( pd_q_lvls * pr_q_lvls );
2426 :
2427 3702 : value = get_next_indice( st0, pr_pd_bits );
2428 :
2429 3702 : pr = (int16_t) floor( value / pd_q_lvls );
2430 3702 : pd = value - pr * pd_q_lvls;
2431 3702 : val = dtx_pd_real_min_max[0];
2432 3702 : ivas_quantise_real_values( &val, pd_q_lvls, dtx_pd_real_min_max[0], dtx_pd_real_min_max[1], &idx, &val, 1 );
2433 3702 : pd = pd + idx;
2434 :
2435 3702 : val = pr_min_max[0];
2436 3702 : ivas_quantise_real_values( &val, pr_q_lvls, pr_min_max[0], pr_min_max[1], &idx, &val, 1 );
2437 3702 : pr = pr + idx;
2438 :
2439 3702 : if ( ( j + 1 ) <= ndec )
2440 : {
2441 2966 : pSpar_md->band_coeffs_idx[i].decd_index_re[pd_idx_2 - 1] = pd;
2442 : }
2443 :
2444 3702 : pSpar_md->band_coeffs_idx[i].pred_index_re[pd_idx_1 - 1] = pr;
2445 : }
2446 : }
2447 : }
2448 :
2449 617 : sid_bits_len = st0->next_bit_pos - sid_bits_len;
2450 617 : sba_spar_bitlen = ivas_sba_spar_sid_bitlen( num_dmx_per_band[0] );
2451 617 : zero_pad_bits = sba_spar_bitlen - sid_bits_len;
2452 :
2453 617 : assert( zero_pad_bits >= 0 );
2454 617 : if ( num_dmx_per_band[0] == 2 )
2455 : {
2456 368 : zero_pad_bits -= 1;
2457 : }
2458 :
2459 1721 : for ( j = 0; j < zero_pad_bits; j++ )
2460 : {
2461 1104 : get_next_indice( st0, 1 );
2462 : }
2463 :
2464 617 : ivas_spar_unquant_dtx_indicies( pSpar_md, num_bands, bw, num_dmx_per_band );
2465 :
2466 617 : return;
2467 : }
2468 :
2469 :
2470 : /*-----------------------------------------------------------------------------------------*
2471 : * Function ivas_deindex_real_index()
2472 : *
2473 : * Deindex real index
2474 : *-----------------------------------------------------------------------------------------*/
2475 :
2476 5990348 : 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 5990348 : if ( q_levels == 0 )
2488 : {
2489 0 : return IVAS_ERR_INTERNAL;
2490 : }
2491 :
2492 5990348 : if ( q_levels == 1 )
2493 : {
2494 2503696 : for ( i = 0; i < dim; i++ )
2495 : {
2496 1342032 : quant[i] = 0;
2497 : }
2498 : }
2499 : else
2500 : {
2501 4828684 : q_step = ( max_value - min_value ) / ( q_levels - 1 );
2502 31963708 : for ( i = 0; i < dim; i++ )
2503 : {
2504 27135024 : quant[i] = index[i] * q_step;
2505 : }
2506 : }
2507 :
2508 5990348 : return IVAS_ERR_OK;
2509 : }
2510 :
2511 :
2512 : /*-----------------------------------------------------------------------------------------*
2513 : * Function ivas_spar_to_dirac()
2514 : *
2515 : *
2516 : *-----------------------------------------------------------------------------------------*/
2517 :
2518 205905 : 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 205905 : active_w = ( dyn_active_w_flag == 1 ) || ( hMdDec->spar_md_cfg.active_w == 1 );
2552 205905 : sba_order_internal = min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER );
2553 205905 : start_band = 0;
2554 205905 : end_band = min( num_bands_out, SPAR_DIRAC_SPLIT_START_BAND ) / bw;
2555 :
2556 205905 : hDirAC = st_ivas->hDirAC;
2557 205905 : hSpatParamRendCom = st_ivas->hSpatParamRendCom;
2558 :
2559 205905 : dirac_to_spar_md_bands = st_ivas->hSpar->dirac_to_spar_md_bands;
2560 205905 : enc_param_start_band = st_ivas->hSpar->enc_param_start_band / bw;
2561 205905 : active_w_vlbr = ( st_ivas->hDecoderConfig->ivas_total_brate < IVAS_24k4 ) ? 1 : 0;
2562 :
2563 205905 : if ( hDirAC != NULL && ivas_get_hodirac_flag( st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->sba_analysis_order ) == 0 )
2564 : {
2565 151733 : band_grouping = hDirAC->band_grouping;
2566 151733 : num_slots_in_subfr = st_ivas->hDirAC->hConfig->dec_param_estim ? CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES : 1;
2567 :
2568 1365597 : for ( band = start_band; band < end_band; band++ )
2569 : {
2570 : float PR[3], Pd[3], dvnorm, g_pred;
2571 :
2572 1213864 : PR[0] = hMdDec->spar_md.band_coeffs[band].pred_re[2];
2573 1213864 : PR[1] = hMdDec->spar_md.band_coeffs[band].pred_re[0];
2574 1213864 : PR[2] = hMdDec->spar_md.band_coeffs[band].pred_re[1];
2575 1213864 : g_pred = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
2576 1213864 : if ( g_pred <= EPSILON )
2577 : {
2578 39548 : dvx[band] = 1.0f;
2579 39548 : dvy[band] = 0.0f;
2580 39548 : dvz[band] = 0.0f;
2581 : }
2582 : else
2583 : {
2584 1174316 : g_pred = sqrtf( g_pred );
2585 1174316 : dvnorm = 1.0f / g_pred;
2586 1174316 : dvx[band] = PR[0] * dvnorm;
2587 1174316 : dvy[band] = PR[1] * dvnorm;
2588 1174316 : dvz[band] = PR[2] * dvnorm;
2589 : }
2590 :
2591 1213864 : radius = sqrtf( dvx[band] * dvx[band] + dvy[band] * dvy[band] );
2592 1213864 : azi[band] = (int16_t) ( max( -180.0f, min( 180.0f, atan2f( dvy[band], dvx[band] ) / EVS_PI * 180.0f ) ) + 0.5f );
2593 1213864 : ele[band] = (int16_t) ( max( -90.0f, min( 180.0f, atan2f( dvz[band], radius ) / EVS_PI * 180.0f ) ) + 0.5f );
2594 :
2595 1213864 : if ( st_ivas->nchan_transport == 1 )
2596 : {
2597 : float w_en_norm, f_scale;
2598 178672 : if ( active_w )
2599 : {
2600 178672 : if ( dtx_vad == 0 )
2601 : {
2602 840 : f_scale = IVAS_ACTIVEW_DM_F_SCALE_DTX;
2603 : }
2604 : else
2605 : {
2606 177832 : 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 178672 : w_en_norm = ( 1.0f - ( f_scale * g_pred * g_pred ) );
2615 178672 : w_en_norm *= w_en_norm;
2616 :
2617 178672 : Pd[0] = hMdDec->spar_md.band_coeffs[band].P_re[1];
2618 178672 : Pd[1] = hMdDec->spar_md.band_coeffs[band].P_re[0];
2619 178672 : Pd[2] = hMdDec->spar_md.band_coeffs[band].P_re[2];
2620 178672 : en_ratio = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
2621 178672 : res_pow = w_en_norm + en_ratio + ( Pd[0] * Pd[0] + Pd[1] * Pd[1] + Pd[2] * Pd[2] );
2622 178672 : res_pow *= 0.5f;
2623 178672 : hMdDec->spar_md.en_ratio_slow[band] = 0.75f * hMdDec->spar_md.en_ratio_slow[band] + 0.25f * en_ratio;
2624 178672 : hMdDec->spar_md.ref_pow_slow[band] = 0.75f * hMdDec->spar_md.ref_pow_slow[band] + 0.25f * res_pow;
2625 178672 : en_ratio = sqrtf( hMdDec->spar_md.en_ratio_slow[band] ) / ( hMdDec->spar_md.ref_pow_slow[band] + EPSILON );
2626 : }
2627 : else
2628 : {
2629 1035192 : en_ratio = PR[0] * PR[0] + PR[1] * PR[1] + PR[2] * PR[2];
2630 1035192 : hMdDec->spar_md.en_ratio_slow[band] = 0.75f * hMdDec->spar_md.en_ratio_slow[band] + 0.25f * en_ratio;
2631 1035192 : en_ratio = sqrtf( hMdDec->spar_md.en_ratio_slow[band] );
2632 : }
2633 1213864 : diffuseness[band] = 1.0f - en_ratio; /*compute diffuseness*/
2634 1213864 : diffuseness[band] = ( ( diffuseness[band] < 1.0f ) ? ( ( diffuseness[band] < 0.0f ) ? 0.f : diffuseness[band] ) : 1.0f );
2635 : }
2636 :
2637 1365597 : for ( band = start_band; band < end_band; band++ )
2638 : {
2639 : int16_t azi_dith, ele_dith;
2640 1213864 : tmp_write_idx_param_band = hDirAC->spar_to_dirac_write_idx;
2641 :
2642 1213864 : en_ratio = 1.0f - diffuseness[band];
2643 1213864 : masa_sq( 1.0f - en_ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
2644 :
2645 1213864 : qmf_band_start = band_grouping[band];
2646 1213864 : qmf_band_end = band_grouping[band + 1];
2647 :
2648 6069320 : for ( block = 0; block < MAX_PARAM_SPATIAL_SUBFRAMES; block++ )
2649 : {
2650 : int16_t ts_start, ts_end, ts;
2651 :
2652 4855456 : ts_start = DirAC_block_grouping[block];
2653 4855456 : ts_end = DirAC_block_grouping[block + 1];
2654 11531708 : for ( b = qmf_band_start; b < qmf_band_end; b++ )
2655 : {
2656 :
2657 6676252 : azi_dith = azi[band];
2658 6676252 : ele_dith = ele[band];
2659 :
2660 6676252 : hSpatParamRendCom->energy_ratio1[block][b] = en_ratio;
2661 6676252 : tmp_write_idx_band = tmp_write_idx_param_band;
2662 :
2663 6676252 : if ( hDirAC->hConfig->dec_param_estim == FALSE )
2664 : {
2665 2426556 : hSpatParamRendCom->elevation[tmp_write_idx_band][b] = ele_dith;
2666 2426556 : hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = azi_dith;
2667 2426556 : hSpatParamRendCom->diffuseness_vector[tmp_write_idx_band][b] = diffuseness[band];
2668 : }
2669 : else
2670 : {
2671 21248480 : for ( ts = ts_start; ts < ts_end; ts++ )
2672 : {
2673 16998784 : hSpatParamRendCom->elevation[tmp_write_idx_band][b] = ele_dith;
2674 16998784 : hSpatParamRendCom->azimuth[tmp_write_idx_band][b] = azi_dith;
2675 16998784 : hSpatParamRendCom->diffuseness_vector[tmp_write_idx_band][b] = diffuseness[band];
2676 16998784 : tmp_write_idx_band = ( tmp_write_idx_band + 1 ) % hSpatParamRendCom->dirac_md_buffer_length;
2677 : }
2678 : }
2679 : }
2680 4855456 : 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 151733 : if ( hDirAC->hConfig->dec_param_estim == FALSE )
2686 : {
2687 55149 : 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 96584 : 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 54172 : band = end_band;
2697 : }
2698 :
2699 : /*read DirAC metadata, convert DirAC to SPAR*/
2700 1005417 : for ( ; band < num_bands_out / bw; band++ )
2701 : {
2702 : int16_t dirac_band_idx;
2703 :
2704 799512 : dirac_band_idx = dirac_to_spar_md_bands[band] - enc_param_start_band;
2705 :
2706 799512 : num_subframes = MAX_PARAM_SPATIAL_SUBFRAMES;
2707 799512 : if ( st_ivas->hQMetaData->useLowerRes )
2708 : {
2709 47256 : num_subframes = 1;
2710 : }
2711 :
2712 3855792 : for ( block = 0; block < num_subframes; block++ )
2713 : {
2714 3056280 : if ( st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block] < 0.f )
2715 : {
2716 305853 : st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block] += 360.f;
2717 : }
2718 3056280 : azi_dirac[band][block] = st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].azimuth[block];
2719 3056280 : ele_dirac[band][block] = st_ivas->hQMetaData->q_direction->band_data[dirac_band_idx].elevation[block];
2720 : }
2721 :
2722 799512 : 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 205905 : if ( hMdDec->spar_md_cfg.nchan_transport > 1 )
2727 : {
2728 166342 : 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 665368 : for ( pred_idx = 0; pred_idx < FOA_CHANNELS - 1; pred_idx++ )
2732 : {
2733 2495130 : for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
2734 : {
2735 1996104 : 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 205905 : 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 205905 : 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 205905 : if ( st_ivas->hQMetaData->useLowerRes && dtx_vad )
2745 : {
2746 59420 : for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
2747 : {
2748 47536 : 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 993750 : for ( block = 0; block < num_md_sub_frames; block++ )
2764 : {
2765 3939225 : for ( band = SPAR_DIRAC_SPLIT_START_BAND; band < IVAS_MAX_NUM_BANDS; band++ )
2766 : {
2767 12605520 : 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 9454140 : if ( ivas_is_res_channel( pred_idx + 1, hMdDec->spar_md_cfg.nchan_transport ) )
2770 : {
2771 : /* use 20ms coefficients only for residual channels */
2772 5646608 : 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 1005417 : for ( b = end_band * bw; b < num_bands_out; b++ )
2779 : {
2780 799512 : hMdDec->valid_bands[b] = 1;
2781 : }
2782 :
2783 205905 : return;
2784 : }
|