Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : #include <assert.h>
34 : #include "options.h"
35 : #include <stdint.h>
36 : #include "cnst.h"
37 : #include "rom_enc.h"
38 : #include "rom_com.h"
39 : #include "prot.h"
40 : #include "ivas_prot.h"
41 : #include "ivas_prot_rend.h"
42 : #include "ivas_cnst.h"
43 : #include "ivas_rom_com.h"
44 : #include "ivas_rom_dec.h"
45 : #include "math.h"
46 : #ifdef DEBUGGING
47 : #include "debug.h"
48 : #endif
49 : #ifdef DEBUG_PLOT
50 : #include "deb_out.h"
51 : #endif
52 : #include "wmc_auto.h"
53 : #include "rom_dec.h"
54 :
55 : /*-----------------------------------------------------------------------*
56 : * Local constants
57 : *-----------------------------------------------------------------------*/
58 :
59 : #define PARAM_MC_LOCAL_SZ_LFE_MAP 5
60 :
61 : /*-----------------------------------------------------------------------*
62 : * Local typedefs
63 : *-----------------------------------------------------------------------*/
64 :
65 : typedef struct parameter_band_mapping_struct
66 : {
67 : int16_t n_source_bands[20];
68 : int16_t source_band_idx[20][4];
69 : float source_band_factor[20][4];
70 :
71 : } PARAM_MC_PARAMETER_BAND_MAPPING;
72 :
73 : /*-----------------------------------------------------------------------*
74 : * Local function prototypes
75 : *-----------------------------------------------------------------------*/
76 :
77 : static void ivas_param_mc_dec_init( PARAM_MC_DEC_HANDLE hParamMC, const int16_t nchan_in, const int16_t nchan_out );
78 :
79 : static void param_mc_protoSignalComputation( float *RealBuffer, float *ImagBuffer, float *proto_frame_f, const PARAM_MC_DIFF_PROTO_INFO *diff_proto_info, const int16_t num_freq_bands );
80 :
81 : static void ivas_param_mc_dec_copy_diffuse_proto( PARAM_MC_DEC_HANDLE hParamMC, float Cldfb_buffer_real[MAX_LS_CHANNELS][PARAM_MC_MAX_NSLOTS_IN_SUBFRAME][CLDFB_NO_CHANNELS_MAX], float Cldfb_buffer_imag[MAX_LS_CHANNELS][PARAM_MC_MAX_NSLOTS_IN_SUBFRAME][CLDFB_NO_CHANNELS_MAX], const int16_t nY, const int16_t slot_idx );
82 :
83 : static int16_t ivas_param_mc_range_decoder_LC( uint16_t *bit_buffer, int16_t *x, int16_t *BER_detect, const int16_t sz_seq, const int16_t sz_alphabet, const uint16_t *cft, const uint16_t *sft, const int16_t tot_shift, const int16_t nbbits );
84 :
85 : static int16_t ivas_param_mc_uniform_decoder( float *seq, const int16_t sz_seq, const float *alphabet, const int16_t N, uint16_t bit_buffer[PARAM_MC_MAX_BITS] );
86 :
87 : static void ivas_param_mc_dequantize_cov( PARAM_MC_DEC_HANDLE hDirAC, float *ild_q, float *icc_q, const int16_t param_band_index, const int16_t nY_int, const PARAM_MC_SYNTHESIS_CONF synth_conf, const int16_t nY, const int16_t nX, float *Cx_state, float *Cproto, float *Cy_state );
88 :
89 : static void ivas_param_mc_get_mixing_matrices( PARAM_MC_DEC_HANDLE hParamMC, IVAS_OUTPUT_SETUP *hSynthesisOutputSetup, float Cx_in[PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS], const int16_t param_band_idx, float *mixing_matrix[], float *mixing_matrix_res[], const int16_t nY_int, const PARAM_MC_SYNTHESIS_CONF synth_conf, const int16_t nX, const int16_t nY );
90 :
91 : static void param_mc_update_mixing_matrices( PARAM_MC_DEC_HANDLE hParamMC, float *mixing_matrix[], float *mixing_matrix_res[], const uint16_t nX, const uint16_t nY );
92 :
93 : static void ivas_param_mc_dec_compute_interpolator( const uint16_t bAttackPresent, const uint16_t attackPos, const uint16_t interp_length, float *interpolator );
94 :
95 : static void param_mc_set_num_synth_bands( const int32_t output_Fs, PARAM_MC_DEC_HANDLE hParamMC );
96 :
97 : static ivas_error param_mc_get_diff_proto_info( const float *proto_mtx, const uint16_t nchan_transport, const uint16_t nchan_out_cov, PARAM_MC_DIFF_PROTO_INFO *p_diff_proto_info );
98 :
99 : static void ivas_param_mc_get_param_band_mapping( const int16_t n_target_bands, const int16_t *target_band_grouping, const int16_t n_source_bands, const int16_t *source_band_grouping, PARAM_MC_PARAMETER_BAND_MAPPING *parameter_band_mapping );
100 :
101 : static void ivas_param_mc_bs_decode_parameter_values( uint16_t bit_buffer[], int16_t *bit_pos, const int16_t max_bits, int16_t *BER_detect, HANDLE_IVAS_PARAM_MC_METADATA hMetadataPMC, HANDLE_PARAM_MC_PARAMETER_CODING_INFO hParamCodingInfo, const int16_t map_size_wo_lfe, const int16_t map_size, const int16_t num_lfe_bands, const int16_t band_step, const int16_t num_param_bands, float *value_buffer );
102 :
103 : /*-------------------------------------------------------------------------
104 : * ivas_param_mc_dec_open()
105 : *
106 : * Open Parametric MC decoder handle
107 : *-------------------------------------------------------------------------*/
108 :
109 10560 : ivas_error ivas_param_mc_dec_open(
110 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
111 : )
112 : {
113 : int16_t k, nchan_transport;
114 : PARAM_MC_DEC_HANDLE hParamMC;
115 : int16_t nchan_out_transport;
116 : int16_t nchan_out_cov;
117 : float proto_matrix[MAX_LS_CHANNELS * PARAM_MC_MAX_TRANSPORT_CHANS];
118 : float proto_mtx_norm;
119 : int16_t max_param_band_residual;
120 : uint16_t config_index;
121 : MC_LS_SETUP mc_ls_setup;
122 : float frequency_axis[CLDFB_NO_CHANNELS_MAX];
123 : AUDIO_CONFIG output_config;
124 : int32_t output_Fs, ivas_total_brate;
125 : ivas_error error;
126 :
127 10560 : error = IVAS_ERR_OK;
128 :
129 : /*-----------------------------------------------------------------*
130 : * prepare library opening
131 : *-----------------------------------------------------------------*/
132 :
133 10560 : if ( ( hParamMC = (PARAM_MC_DEC_HANDLE) malloc( sizeof( PARAM_MC_DEC_DATA ) ) ) == NULL )
134 : {
135 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
136 : }
137 :
138 10560 : if ( ( hParamMC->hMetadataPMC = (HANDLE_IVAS_PARAM_MC_METADATA) malloc( sizeof( IVAS_PARAM_MC_METADATA ) ) ) == NULL )
139 : {
140 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC metadata \n" ) );
141 : }
142 :
143 10560 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
144 10560 : output_config = st_ivas->hDecoderConfig->output_config;
145 10560 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
146 10560 : mc_ls_setup = ivas_mc_map_output_config_to_mc_ls_setup( st_ivas->transport_config );
147 10560 : nchan_out_transport = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
148 10560 : hParamMC->hoa_encoder = NULL;
149 :
150 : /* determine the synthesis config */
151 10560 : if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM || st_ivas->renderer_type == RENDERER_BINAURAL_OBJECTS_TD || st_ivas->transport_config == output_config || output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
152 : {
153 9006 : hParamMC->synthesis_conf = PARAM_MC_SYNTH_DIRECT;
154 : }
155 1554 : else if ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO )
156 : {
157 246 : hParamMC->synthesis_conf = PARAM_MC_SYNTH_MONO_STEREO;
158 : }
159 1308 : else if ( st_ivas->transport_config != output_config )
160 : {
161 1308 : if ( ( output_config != IVAS_AUDIO_CONFIG_LS_CUSTOM && nchan_out_transport > audioCfg2channels( output_config ) ) || ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM && nchan_out_transport > st_ivas->hOutSetup.nchan_out_woLFE ) )
162 : {
163 3 : hParamMC->synthesis_conf = PARAM_MC_SYNTH_LS_CONV_COV;
164 : /* need to reset the intern config */
165 3 : st_ivas->intern_config = output_config;
166 3 : ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->intern_config );
167 3 : if ( output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
168 : {
169 0 : st_ivas->hIntSetup.nchan_out_woLFE = st_ivas->hLsSetupCustom->num_spk;
170 0 : st_ivas->hIntSetup.ls_azimuth = st_ivas->hLsSetupCustom->ls_azimuth;
171 0 : st_ivas->hIntSetup.ls_elevation = st_ivas->hLsSetupCustom->ls_elevation;
172 : }
173 : }
174 : else
175 : {
176 1305 : hParamMC->synthesis_conf = PARAM_MC_SYNTH_LS_CONV_CLDFB;
177 : }
178 : }
179 :
180 10560 : hParamMC->ls_conv_dmx_matrix = NULL;
181 :
182 10560 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
183 : {
184 249 : nchan_out_cov = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
185 : }
186 : else
187 : {
188 10311 : nchan_out_cov = nchan_out_transport;
189 : }
190 :
191 10560 : st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels( ivas_total_brate, mc_ls_setup );
192 10560 : config_index = ivas_param_mc_get_configuration_index( mc_ls_setup, ivas_total_brate );
193 10560 : nchan_transport = st_ivas->nchan_transport;
194 :
195 10560 : switch ( nchan_transport )
196 : {
197 3036 : case 4:
198 : case 3:
199 3036 : st_ivas->nCPE = 2;
200 3036 : st_ivas->nSCE = 0;
201 3036 : st_ivas->element_mode_init = IVAS_CPE_MDCT;
202 3036 : break;
203 7524 : case 2:
204 7524 : st_ivas->nCPE = 1;
205 7524 : st_ivas->nSCE = 0;
206 7524 : st_ivas->element_mode_init = IVAS_CPE_MDCT;
207 :
208 7524 : break;
209 : #ifdef DEBUGGING
210 : default:
211 : assert( 0 && "Number of TC not supported for Parametric MC!" );
212 : #endif
213 : }
214 :
215 : /*-----------------------------------------------------------------*
216 : * set input parameters
217 : *-----------------------------------------------------------------*/
218 :
219 10560 : hParamMC->slot_size = (int16_t) ( output_Fs / FRAMES_PER_SEC ) / CLDFB_NO_COL_MAX;
220 10560 : set_s( hParamMC->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
221 10560 : set_s( hParamMC->subframe_nbslots, PARAM_MC_MAX_NSLOTS_IN_SUBFRAME, DEFAULT_JBM_SUBFRAMES_5MS );
222 10560 : hParamMC->nb_subframes = DEFAULT_JBM_SUBFRAMES_5MS;
223 :
224 10560 : hParamMC->num_freq_bands = (int16_t) ( output_Fs * INV_CLDFB_BANDWIDTH + 0.5f );
225 10560 : hParamMC->max_band_energy_compensation = hParamMC->num_freq_bands;
226 10560 : ivas_param_mc_metadata_open( mc_ls_setup, ivas_total_brate, hParamMC->hMetadataPMC );
227 :
228 : /* init arrays for quantized parameters */
229 10560 : if ( ( hParamMC->icc_q = (float *) malloc( hParamMC->hMetadataPMC->num_parameter_bands * hParamMC->hMetadataPMC->icc_mapping_conf->icc_map_size_lfe * sizeof( float ) ) ) == NULL )
230 : {
231 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
232 : }
233 10560 : if ( ( hParamMC->icld_q = (float *) malloc( hParamMC->hMetadataPMC->num_parameter_bands * hParamMC->hMetadataPMC->ild_mapping_conf->ild_map_size_lfe * sizeof( float ) ) ) == NULL )
234 : {
235 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
236 : }
237 10560 : set_f( hParamMC->icld_q, PARAM_MC_DEFAULT_MIN_ILD, hParamMC->hMetadataPMC->num_parameter_bands * hParamMC->hMetadataPMC->ild_mapping_conf->ild_map_size_lfe );
238 10560 : set_f( hParamMC->icc_q, 0.0f, hParamMC->hMetadataPMC->num_parameter_bands * hParamMC->hMetadataPMC->icc_mapping_conf->icc_map_size_lfe );
239 :
240 10560 : param_mc_set_num_synth_bands( output_Fs, hParamMC );
241 :
242 : /* Band Grouping */
243 10560 : if ( hParamMC->hMetadataPMC->num_parameter_bands == 20 )
244 : {
245 915 : mvs2s( param_mc_band_grouping_20, hParamMC->band_grouping, 20 + 1 );
246 : }
247 9645 : else if ( hParamMC->hMetadataPMC->num_parameter_bands == 14 )
248 : {
249 5499 : mvs2s( param_mc_band_grouping_14, hParamMC->band_grouping, 14 + 1 );
250 : }
251 4146 : else if ( hParamMC->hMetadataPMC->num_parameter_bands == 10 )
252 : {
253 4146 : mvs2s( param_mc_band_grouping_10, hParamMC->band_grouping, 10 + 1 );
254 : }
255 : else
256 : {
257 0 : assert( 0 && "nbands must be 20, 14, or 10!" );
258 : }
259 :
260 : /* set max parameter band for abs cov */
261 10560 : k = 0;
262 99138 : while ( hParamMC->band_grouping[k] <= PARAM_MC_MAX_BAND_ABS_COV_DEC )
263 : {
264 88578 : hParamMC->max_param_band_abs_cov = ( k++ );
265 : }
266 :
267 : /*-----------------------------------------------------------------*
268 : * open sub-modules
269 : *-----------------------------------------------------------------*/
270 :
271 : /* prototype signal computation */
272 :
273 10560 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_CLDFB || hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
274 : {
275 1554 : if ( ( error = ivas_ls_setup_conversion_open( st_ivas ) ) != IVAS_ERR_OK )
276 : {
277 0 : return error;
278 : }
279 :
280 : /* convert the ls conv dmx matrix into column order matrix format (nchan_out_cldfb x nchan_out) */
281 1554 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
282 : {
283 249 : if ( ( hParamMC->ls_conv_dmx_matrix = (float *) malloc( nchan_out_transport * nchan_out_cov * sizeof( float ) ) ) == NULL )
284 : {
285 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
286 : }
287 :
288 3225 : for ( k = 0; k < nchan_out_transport; k++ )
289 : {
290 2976 : mvr2r( st_ivas->hLsSetUpConversion->dmxMtx[k], &hParamMC->ls_conv_dmx_matrix[k * nchan_out_cov], nchan_out_cov );
291 : }
292 :
293 : /* convert ParamMC parameter bands to SFB */
294 249 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
295 : {
296 246 : st_ivas->hLsSetUpConversion->sfbCnt = hParamMC->num_param_bands_synth;
297 3690 : for ( k = 0; k <= hParamMC->num_param_bands_synth; k++ )
298 : {
299 3444 : st_ivas->hLsSetUpConversion->sfbOffset[k] = PARAM_MC_BAND_TO_MDCT_BAND_RATIO * hParamMC->band_grouping[k];
300 : }
301 : }
302 : else
303 : {
304 : /* close the ls conversion handle immediately, it was only needed to get the DMX matrix in case of DMX in the covariance domain */
305 3 : ivas_ls_setup_conversion_close( &st_ivas->hLsSetUpConversion );
306 : }
307 : }
308 : }
309 :
310 10560 : if ( ( hParamMC->proto_matrix_int = (float *) malloc( nchan_out_transport * nchan_transport * sizeof( float ) ) ) == NULL )
311 : {
312 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
313 : }
314 10560 : mvr2r( ivas_param_mc_conf[config_index].dmx_fac, hParamMC->proto_matrix_int, nchan_transport * nchan_out_transport );
315 :
316 10560 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
317 : {
318 249 : matrix_product( hParamMC->ls_conv_dmx_matrix, nchan_out_cov, nchan_out_transport, 0, ivas_param_mc_conf[config_index].dmx_fac, nchan_out_transport, nchan_transport, 0, proto_matrix );
319 :
320 249 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
321 : {
322 246 : proto_mtx_norm = 1.f;
323 1722 : for ( k = 0; k < nchan_transport * nchan_out_cov; k++ )
324 : {
325 1476 : proto_mtx_norm = max( fabsf( proto_mtx_norm ), fabsf( proto_matrix[k] ) );
326 : }
327 246 : proto_mtx_norm = 1.f / proto_mtx_norm;
328 :
329 : /* transfer flattened proto_matrix to 2D in hLsSetupConversion->dmxMtx */
330 984 : for ( k = 0; k < nchan_transport; k++ )
331 : {
332 2214 : for ( int16_t i = 0; i < nchan_out_cov; i++ )
333 : {
334 1476 : st_ivas->hLsSetUpConversion->dmxMtx[k][i] = proto_matrix[k * nchan_out_cov + i] * proto_mtx_norm;
335 : }
336 : }
337 : }
338 : }
339 : else
340 : {
341 10311 : mvr2r( ivas_param_mc_conf[config_index].dmx_fac, proto_matrix, nchan_out_transport * nchan_transport );
342 : }
343 :
344 10560 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
345 : {
346 246 : hParamMC->num_outputs_diff = 0;
347 246 : hParamMC->diff_proto_info = NULL;
348 246 : hParamMC->h_output_synthesis_params.use_onset_filters = 0;
349 246 : hParamMC->max_band_decorr = 0;
350 246 : hParamMC->h_freq_domain_decorr_ap_params = NULL;
351 246 : hParamMC->h_freq_domain_decorr_ap_state = NULL;
352 : }
353 : else
354 : {
355 10314 : hParamMC->num_outputs_diff = nchan_out_cov;
356 10314 : if ( ( hParamMC->diff_proto_info = (PARAM_MC_DIFF_PROTO_INFO *) malloc( sizeof( PARAM_MC_DIFF_PROTO_INFO ) ) ) == NULL )
357 : {
358 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
359 : }
360 :
361 10314 : if ( ( error = param_mc_get_diff_proto_info( proto_matrix, nchan_transport, nchan_out_cov, hParamMC->diff_proto_info ) ) != IVAS_ERR_OK )
362 : {
363 0 : return error;
364 : }
365 :
366 : /* decorrelation */
367 10314 : hParamMC->h_freq_domain_decorr_ap_params = NULL;
368 10314 : hParamMC->h_freq_domain_decorr_ap_state = NULL;
369 :
370 10314 : ivas_dirac_dec_get_frequency_axis( frequency_axis, output_Fs, hParamMC->num_freq_bands );
371 :
372 10314 : if ( ( error = ivas_dirac_dec_decorr_open( &( hParamMC->h_freq_domain_decorr_ap_params ), &( hParamMC->h_freq_domain_decorr_ap_state ), hParamMC->num_freq_bands, hParamMC->num_outputs_diff, hParamMC->diff_proto_info->num_protos_diff,
373 : DIRAC_SYNTHESIS_COV_MC_LS, frequency_axis, nchan_transport, output_Fs ) ) != IVAS_ERR_OK )
374 : {
375 0 : return error;
376 : }
377 :
378 10314 : hParamMC->h_output_synthesis_params.use_onset_filters = 0;
379 10314 : hParamMC->max_band_decorr = hParamMC->h_freq_domain_decorr_ap_params->max_band_decorr;
380 : }
381 10560 : hParamMC->max_band_energy_compensation = hParamMC->band_grouping[hParamMC->hMetadataPMC->nbands_coded];
382 10560 : max_param_band_residual = 0;
383 :
384 43545 : for ( k = hParamMC->hMetadataPMC->num_parameter_bands; k >= 0; k-- )
385 : {
386 43545 : if ( hParamMC->band_grouping[k] <= hParamMC->max_band_decorr )
387 : {
388 10560 : max_param_band_residual = k;
389 10560 : assert( hParamMC->band_grouping[k] == hParamMC->max_band_decorr );
390 10560 : break;
391 : }
392 : }
393 :
394 : /* output synthesis */
395 10560 : if ( ( error = ivas_dirac_dec_output_synthesis_cov_open( &( hParamMC->h_output_synthesis_params ), &( hParamMC->h_output_synthesis_cov_state ), hParamMC->max_band_decorr, PARAM_MC_MAX_NSLOTS,
396 10560 : hParamMC->hMetadataPMC->num_parameter_bands, max_param_band_residual, nchan_transport, nchan_out_cov, proto_matrix ) ) != IVAS_ERR_OK )
397 : {
398 0 : return error;
399 : }
400 :
401 10560 : ivas_param_mc_dec_compute_interpolator( 0, 0, DEFAULT_JBM_CLDFB_TIMESLOTS, hParamMC->h_output_synthesis_params.interpolator );
402 :
403 : /* Head or external rotation */
404 : #ifdef IVAS_RTPDUMP
405 10560 : if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ) && ( st_ivas->hDecoderConfig->Opt_Headrotation || st_ivas->hDecoderConfig->Opt_ExternalOrientation || st_ivas->hCombinedOrientationData ) )
406 : #else
407 : if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ) && ( st_ivas->hDecoderConfig->Opt_Headrotation || st_ivas->hDecoderConfig->Opt_ExternalOrientation ) )
408 : #endif
409 : {
410 1230 : if ( ( hParamMC->hoa_encoder = (float *) malloc( st_ivas->hTransSetup.nchan_out_woLFE * MAX_INTERN_CHANNELS * sizeof( float ) ) ) == NULL )
411 : {
412 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
413 : }
414 1230 : compute_hoa_encoder_mtx( st_ivas->hTransSetup.ls_azimuth, st_ivas->hTransSetup.ls_elevation, hParamMC->hoa_encoder, st_ivas->hTransSetup.nchan_out_woLFE, HEAD_ROTATION_HOA_ORDER );
415 : }
416 :
417 : /*-----------------------------------------------------------------*
418 : * memory allocation
419 : *-----------------------------------------------------------------*/
420 :
421 10560 : if ( hParamMC->max_band_decorr > 0 )
422 : {
423 10314 : if ( ( hParamMC->proto_frame_f = (float *) malloc( 2 * hParamMC->diff_proto_info->num_protos_diff * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
424 : {
425 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
426 : }
427 :
428 10314 : if ( ( hParamMC->proto_frame_dec_f = (float *) malloc( 2 * nchan_out_cov * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
429 : {
430 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
431 : }
432 : }
433 : else
434 : {
435 246 : hParamMC->proto_frame_f = NULL;
436 246 : hParamMC->proto_frame_dec_f = NULL;
437 : }
438 :
439 10560 : ivas_param_mc_dec_init( hParamMC, nchan_transport, nchan_out_cov );
440 :
441 10560 : if ( hParamMC->synthesis_conf != PARAM_MC_SYNTH_MONO_STEREO )
442 : {
443 : int16_t n_cldfb_slots;
444 : int16_t granularity;
445 :
446 10314 : n_cldfb_slots = DEFAULT_JBM_CLDFB_TIMESLOTS;
447 10314 : if ( st_ivas->hDecoderConfig->Opt_tsm )
448 : {
449 759 : n_cldfb_slots = MAX_JBM_CLDFB_TIMESLOTS;
450 : }
451 :
452 10314 : if ( ( hParamMC->Cldfb_RealBuffer_tc = (float *) malloc( n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
453 : {
454 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC JBM\n" ) );
455 : }
456 10314 : set_zero( hParamMC->Cldfb_RealBuffer_tc, n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands );
457 :
458 10314 : if ( ( hParamMC->Cldfb_ImagBuffer_tc = (float *) malloc( n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
459 : {
460 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC JBM\n" ) );
461 : }
462 10314 : set_zero( hParamMC->Cldfb_ImagBuffer_tc, n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands );
463 :
464 10314 : if ( st_ivas->hTcBuffer == NULL )
465 : {
466 30 : granularity = ivas_jbm_dec_get_render_granularity( st_ivas->renderer_type, RENDERER_DISABLE, output_Fs );
467 :
468 30 : if ( ( error = ivas_jbm_dec_tc_buffer_open( st_ivas, TC_BUFFER_MODE_RENDERER, nchan_transport, nchan_transport, 0, granularity ) ) != IVAS_ERR_OK )
469 : {
470 0 : return error;
471 : }
472 : }
473 : }
474 : else
475 : {
476 246 : hParamMC->Cldfb_RealBuffer_tc = NULL;
477 246 : hParamMC->Cldfb_ImagBuffer_tc = NULL;
478 : }
479 :
480 10560 : hParamMC->subframes_rendered = 0;
481 10560 : hParamMC->slots_rendered = 0;
482 :
483 10560 : st_ivas->hParamMC = hParamMC;
484 :
485 10560 : return error;
486 : }
487 :
488 :
489 : /*-------------------------------------------------------------------------
490 : * ivas_param_mc_get_param_band_mapping()
491 : *
492 : *
493 : *-------------------------------------------------------------------------*/
494 :
495 2487 : static void ivas_param_mc_get_param_band_mapping(
496 : const int16_t n_target_bands,
497 : const int16_t *target_band_grouping,
498 : const int16_t n_source_bands,
499 : const int16_t *source_band_grouping,
500 : PARAM_MC_PARAMETER_BAND_MAPPING *parameter_band_mapping )
501 : {
502 : int16_t target_band_idx;
503 2487 : int16_t source_band_idx = 0;
504 : int16_t source_band_cnt_total;
505 :
506 35541 : for ( target_band_idx = 0; target_band_idx < n_target_bands; target_band_idx++ )
507 : {
508 33054 : int16_t upper = target_band_grouping[target_band_idx + 1];
509 33054 : int16_t lower = target_band_grouping[target_band_idx];
510 33054 : int16_t source_band_in_target_band_cnt = 0;
511 33054 : float norm_fac = 1.0f;
512 33054 : source_band_cnt_total = 0;
513 204642 : for ( source_band_idx = 0; source_band_idx < n_source_bands; source_band_idx++ )
514 : {
515 : /* find lowest corresponding source band*/
516 204642 : if ( source_band_grouping[source_band_idx] <= lower && source_band_grouping[source_band_idx + 1] >= lower )
517 : {
518 : do
519 : {
520 83010 : int16_t source_bands_in_target_band = min( source_band_grouping[source_band_idx + 1], upper ) - max( source_band_grouping[source_band_idx], lower );
521 83010 : if ( source_bands_in_target_band )
522 : {
523 43188 : source_band_cnt_total += source_bands_in_target_band;
524 43188 : parameter_band_mapping->source_band_idx[target_band_idx][source_band_in_target_band_cnt] = source_band_idx;
525 43188 : parameter_band_mapping->source_band_factor[target_band_idx][source_band_in_target_band_cnt++] = (float) source_bands_in_target_band;
526 : }
527 83010 : source_band_idx++;
528 83010 : } while ( source_band_grouping[source_band_idx] <= upper && source_band_idx < n_source_bands );
529 33054 : break;
530 : }
531 : }
532 33054 : norm_fac = 1.0f / ( (float) source_band_cnt_total );
533 76242 : for ( source_band_idx = 0; source_band_idx < source_band_in_target_band_cnt; source_band_idx++ )
534 : {
535 :
536 43188 : parameter_band_mapping->source_band_factor[target_band_idx][source_band_idx] *= norm_fac;
537 : }
538 33054 : parameter_band_mapping->n_source_bands[target_band_idx] = source_band_in_target_band_cnt;
539 : }
540 :
541 2487 : return;
542 : }
543 :
544 :
545 : /*-------------------------------------------------------------------------
546 : * ivas_param_mc_dec_reconfig()
547 : *
548 : * Reconfiguration of ParamMC decoder
549 : *-------------------------------------------------------------------------*/
550 :
551 2487 : ivas_error ivas_param_mc_dec_reconfig(
552 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
553 : )
554 : {
555 : int16_t k, nchan_transport;
556 : PARAM_MC_DEC_HANDLE hParamMC;
557 : int16_t nchan_out_transport;
558 : int16_t nchan_out_cov;
559 : float proto_matrix[MAX_LS_CHANNELS * PARAM_MC_MAX_TRANSPORT_CHANS];
560 : float proto_mtx_norm;
561 : int16_t max_param_band_residual;
562 : uint16_t config_index;
563 : MC_LS_SETUP mc_ls_setup;
564 : float frequency_axis[CLDFB_NO_CHANNELS_MAX];
565 : int32_t output_Fs, ivas_total_brate;
566 : ivas_error error;
567 : int16_t nchan_transport_old;
568 : int16_t num_param_bands_old;
569 : PARAM_MC_PARAMETER_BAND_MAPPING parameter_band_mapping;
570 : int16_t band_grouping_old[20 + 1];
571 :
572 2487 : error = IVAS_ERR_OK;
573 2487 : hParamMC = st_ivas->hParamMC;
574 :
575 : /* save important config information from the previous state */
576 2487 : nchan_transport_old = st_ivas->nchan_transport;
577 2487 : num_param_bands_old = hParamMC->hMetadataPMC->num_parameter_bands;
578 :
579 : /*-----------------------------------------------------------------*
580 : * prepare library opening
581 : *-----------------------------------------------------------------*/
582 :
583 2487 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
584 2487 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
585 2487 : mc_ls_setup = ivas_mc_map_output_config_to_mc_ls_setup( st_ivas->transport_config );
586 2487 : nchan_out_transport = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
587 :
588 2487 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
589 : {
590 0 : nchan_out_cov = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
591 : }
592 : else
593 : {
594 2487 : nchan_out_cov = nchan_out_transport;
595 : }
596 :
597 2487 : st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels( ivas_total_brate, mc_ls_setup );
598 2487 : config_index = ivas_param_mc_get_configuration_index( mc_ls_setup, ivas_total_brate );
599 2487 : nchan_transport = st_ivas->nchan_transport;
600 :
601 2487 : switch ( nchan_transport )
602 : {
603 321 : case 4:
604 : case 3:
605 321 : st_ivas->nCPE = 2;
606 321 : st_ivas->nSCE = 0;
607 321 : st_ivas->element_mode_init = IVAS_CPE_MDCT;
608 321 : break;
609 2166 : case 2:
610 2166 : st_ivas->nCPE = 1;
611 2166 : st_ivas->nSCE = 0;
612 2166 : st_ivas->element_mode_init = IVAS_CPE_MDCT;
613 :
614 2166 : break;
615 : #ifdef DEBUGGING
616 : default:
617 : assert( 0 && "Number of TC not supported for Parametric MC!" );
618 : #endif
619 : }
620 :
621 : /*-----------------------------------------------------------------*
622 : * set input parameters
623 : *-----------------------------------------------------------------*/
624 :
625 2487 : hParamMC->num_freq_bands = (int16_t) ( output_Fs * INV_CLDFB_BANDWIDTH + 0.5f );
626 2487 : hParamMC->max_band_energy_compensation = hParamMC->num_freq_bands;
627 :
628 2487 : mvs2s( hParamMC->band_grouping, band_grouping_old, hParamMC->hMetadataPMC->num_parameter_bands + 1 );
629 :
630 2487 : ivas_param_mc_metadata_open( mc_ls_setup, ivas_total_brate, hParamMC->hMetadataPMC );
631 :
632 : /* Band Grouping */
633 2487 : if ( hParamMC->hMetadataPMC->num_parameter_bands == 20 )
634 : {
635 306 : mvs2s( param_mc_band_grouping_20, hParamMC->band_grouping, 20 + 1 );
636 : }
637 2181 : else if ( hParamMC->hMetadataPMC->num_parameter_bands == 14 )
638 : {
639 1281 : mvs2s( param_mc_band_grouping_14, hParamMC->band_grouping, 14 + 1 );
640 : }
641 900 : else if ( hParamMC->hMetadataPMC->num_parameter_bands == 10 )
642 : {
643 900 : mvs2s( param_mc_band_grouping_10, hParamMC->band_grouping, 10 + 1 );
644 : }
645 : else
646 : {
647 0 : assert( 0 && "nbands must be 20, 14, or 10!" );
648 : }
649 :
650 2487 : ivas_param_mc_get_param_band_mapping( hParamMC->hMetadataPMC->num_parameter_bands, hParamMC->band_grouping, num_param_bands_old, band_grouping_old, ¶meter_band_mapping );
651 :
652 2487 : if ( nchan_transport_old != nchan_transport || num_param_bands_old != hParamMC->hMetadataPMC->num_parameter_bands )
653 : {
654 2343 : float *ild_q_old = hParamMC->icld_q;
655 2343 : float *icc_q_old = hParamMC->icc_q;
656 :
657 :
658 : /* init arrays for the quantized parameters */
659 2343 : if ( ( hParamMC->icc_q = (float *) malloc( hParamMC->hMetadataPMC->num_parameter_bands * hParamMC->hMetadataPMC->icc_mapping_conf->icc_map_size_lfe * sizeof( float ) ) ) == NULL )
660 : {
661 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
662 : }
663 2343 : if ( ( hParamMC->icld_q = (float *) malloc( hParamMC->hMetadataPMC->num_parameter_bands * hParamMC->hMetadataPMC->ild_mapping_conf->ild_map_size_lfe * sizeof( float ) ) ) == NULL )
664 : {
665 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
666 : }
667 2343 : set_f( hParamMC->icld_q, PARAM_MC_DEFAULT_MIN_ILD, hParamMC->hMetadataPMC->num_parameter_bands * hParamMC->hMetadataPMC->ild_mapping_conf->ild_map_size_lfe );
668 2343 : set_f( hParamMC->icc_q, 0.0f, hParamMC->hMetadataPMC->num_parameter_bands * hParamMC->hMetadataPMC->icc_mapping_conf->icc_map_size_lfe );
669 :
670 : /* map old to new parameter banding, only for same number of TCs, needs some more thought for a changing number of TCs */
671 2343 : if ( num_param_bands_old != hParamMC->hMetadataPMC->num_parameter_bands && nchan_transport_old == nchan_transport )
672 : {
673 : int16_t new_param_band_idx, param_idx, source_param_idx;
674 : int16_t num_param_lfe;
675 1764 : float *p_icc_new = hParamMC->icc_q;
676 1764 : float *p_ild_new = hParamMC->icld_q;
677 :
678 : /* ICC */
679 1764 : num_param_lfe = hParamMC->hMetadataPMC->icc_mapping_conf->icc_map_size_lfe;
680 23274 : for ( new_param_band_idx = 0; new_param_band_idx < hParamMC->hMetadataPMC->num_parameter_bands; new_param_band_idx++ )
681 : {
682 139764 : for ( param_idx = 0; param_idx < num_param_lfe; param_idx++ )
683 : {
684 118254 : *p_icc_new = 0.0f;
685 274506 : for ( source_param_idx = 0; source_param_idx < parameter_band_mapping.n_source_bands[new_param_band_idx]; source_param_idx++ )
686 : {
687 156252 : *p_icc_new += icc_q_old[parameter_band_mapping.source_band_idx[new_param_band_idx][source_param_idx] * num_param_lfe + param_idx] * parameter_band_mapping.source_band_factor[new_param_band_idx][source_param_idx];
688 : }
689 118254 : p_icc_new++;
690 : }
691 : }
692 :
693 : /* ILD */
694 1764 : num_param_lfe = hParamMC->hMetadataPMC->ild_mapping_conf->ild_map_size_lfe;
695 23274 : for ( new_param_band_idx = 0; new_param_band_idx < hParamMC->hMetadataPMC->num_parameter_bands; new_param_band_idx++ )
696 : {
697 161274 : for ( param_idx = 0; param_idx < num_param_lfe; param_idx++ )
698 : {
699 139764 : *p_ild_new = 0.0f;
700 324492 : for ( source_param_idx = 0; source_param_idx < parameter_band_mapping.n_source_bands[new_param_band_idx]; source_param_idx++ )
701 : {
702 184728 : *p_ild_new += powf( 10.0f, ild_q_old[parameter_band_mapping.source_band_idx[new_param_band_idx][source_param_idx] * num_param_lfe + param_idx] / 10.0f ) * parameter_band_mapping.source_band_factor[new_param_band_idx][source_param_idx];
703 : }
704 139764 : *p_ild_new = 10.0f * log10f( *p_ild_new );
705 139764 : p_ild_new++;
706 : }
707 : }
708 : }
709 2343 : free( ild_q_old );
710 2343 : free( icc_q_old );
711 : }
712 :
713 2487 : param_mc_set_num_synth_bands( output_Fs, hParamMC );
714 :
715 : /* set max parameter band for abs cov */
716 2487 : k = 0;
717 23682 : while ( hParamMC->band_grouping[k] <= PARAM_MC_MAX_BAND_ABS_COV_DEC )
718 : {
719 21195 : hParamMC->max_param_band_abs_cov = ( k++ );
720 : }
721 :
722 : /*-----------------------------------------------------------------*
723 : * open sub-modules
724 : *-----------------------------------------------------------------*/
725 :
726 : /* prototype signal computation */
727 :
728 2487 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
729 : {
730 0 : if ( nchan_transport_old != nchan_transport )
731 : {
732 0 : if ( st_ivas->hLsSetUpConversion != NULL )
733 : {
734 0 : ivas_ls_setup_conversion_close( &st_ivas->hLsSetUpConversion );
735 : }
736 :
737 0 : if ( ( error = ivas_ls_setup_conversion_open( st_ivas ) ) != IVAS_ERR_OK )
738 : {
739 0 : return error;
740 : }
741 :
742 : /* convert the ls conv dmx matrix into column order matrix format (nchan_out_cldfb x nchan_out) */
743 0 : free( hParamMC->ls_conv_dmx_matrix );
744 0 : if ( ( hParamMC->ls_conv_dmx_matrix = (float *) malloc( nchan_out_transport * nchan_out_cov * sizeof( float ) ) ) == NULL )
745 : {
746 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
747 : }
748 0 : for ( k = 0; k < nchan_out_transport; k++ )
749 : {
750 0 : mvr2r( st_ivas->hLsSetUpConversion->dmxMtx[k], &hParamMC->ls_conv_dmx_matrix[k * nchan_out_cov], nchan_out_cov );
751 : }
752 : }
753 : /* convert ParamMC parameter bands to SFB */
754 :
755 0 : st_ivas->hLsSetUpConversion->sfbCnt = hParamMC->num_param_bands_synth;
756 0 : for ( k = 0; k <= hParamMC->num_param_bands_synth; k++ )
757 : {
758 0 : st_ivas->hLsSetUpConversion->sfbOffset[k] = PARAM_MC_BAND_TO_MDCT_BAND_RATIO * hParamMC->band_grouping[k];
759 : }
760 0 : for ( ; k < MAX_SFB + 2; k++ )
761 : {
762 0 : st_ivas->hLsSetUpConversion->sfbOffset[k] = 0;
763 : }
764 : }
765 :
766 2487 : if ( nchan_transport_old != nchan_transport )
767 : {
768 579 : free( hParamMC->proto_matrix_int );
769 579 : if ( ( hParamMC->proto_matrix_int = (float *) malloc( nchan_out_transport * nchan_transport * sizeof( float ) ) ) == NULL )
770 : {
771 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
772 : }
773 579 : mvr2r( ivas_param_mc_conf[config_index].dmx_fac, hParamMC->proto_matrix_int, nchan_transport * nchan_out_transport );
774 : }
775 :
776 2487 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
777 : {
778 0 : matrix_product( hParamMC->ls_conv_dmx_matrix, nchan_out_cov, nchan_out_transport, 0,
779 : ivas_param_mc_conf[config_index].dmx_fac, nchan_out_transport, nchan_transport, 0,
780 : proto_matrix );
781 :
782 0 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
783 : {
784 0 : proto_mtx_norm = 1.f;
785 0 : for ( k = 0; k < nchan_transport * nchan_out_cov; k++ )
786 : {
787 0 : proto_mtx_norm = max( fabsf( proto_mtx_norm ), fabsf( proto_matrix[k] ) );
788 : }
789 0 : proto_mtx_norm = 1.f / proto_mtx_norm;
790 :
791 : /* transfer flattened proto_matrix to 2D in hLsSetupConversion->dmxMtx */
792 0 : for ( k = 0; k < nchan_transport; k++ )
793 : {
794 0 : for ( int16_t i = 0; i < nchan_out_cov; i++ )
795 : {
796 0 : st_ivas->hLsSetUpConversion->dmxMtx[k][i] = proto_matrix[k * nchan_out_cov + i] * proto_mtx_norm;
797 : }
798 : }
799 : }
800 : }
801 : else
802 : {
803 2487 : mvr2r( ivas_param_mc_conf[config_index].dmx_fac, proto_matrix, nchan_out_transport * nchan_transport );
804 : }
805 :
806 2487 : if ( nchan_transport_old != nchan_transport && hParamMC->synthesis_conf != PARAM_MC_SYNTH_MONO_STEREO )
807 : {
808 : int16_t i;
809 : int16_t len;
810 :
811 : /* close decorrelator */
812 579 : ivas_dirac_dec_decorr_close( &hParamMC->h_freq_domain_decorr_ap_params, &hParamMC->h_freq_domain_decorr_ap_state );
813 :
814 : /* deallocate diffuse prototype info */
815 579 : if ( hParamMC->diff_proto_info )
816 : {
817 2316 : for ( i = 0; i < hParamMC->diff_proto_info->num_protos_diff; i++ )
818 : {
819 1737 : free( hParamMC->diff_proto_info->source_chan_idx[i] );
820 1737 : hParamMC->diff_proto_info->source_chan_idx[i] = NULL;
821 :
822 1737 : free( hParamMC->diff_proto_info->proto_fac[i] );
823 1737 : hParamMC->diff_proto_info->proto_fac[i] = NULL;
824 : }
825 :
826 579 : free( hParamMC->diff_proto_info->source_chan_idx );
827 579 : hParamMC->diff_proto_info->source_chan_idx = NULL;
828 :
829 579 : free( hParamMC->diff_proto_info->proto_fac );
830 579 : hParamMC->diff_proto_info->proto_fac = NULL;
831 :
832 579 : free( hParamMC->diff_proto_info->proto_index_diff );
833 579 : hParamMC->diff_proto_info->proto_index_diff = NULL;
834 :
835 579 : free( hParamMC->diff_proto_info->num_source_chan_diff );
836 579 : hParamMC->diff_proto_info->num_source_chan_diff = NULL;
837 :
838 579 : free( hParamMC->diff_proto_info );
839 579 : hParamMC->diff_proto_info = NULL;
840 : }
841 :
842 579 : hParamMC->num_outputs_diff = nchan_out_cov;
843 579 : if ( ( hParamMC->diff_proto_info = (PARAM_MC_DIFF_PROTO_INFO *) malloc( sizeof( PARAM_MC_DIFF_PROTO_INFO ) ) ) == NULL )
844 : {
845 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
846 : }
847 :
848 579 : if ( ( param_mc_get_diff_proto_info( proto_matrix, nchan_transport, nchan_out_cov, hParamMC->diff_proto_info ) ) != IVAS_ERR_OK )
849 : {
850 0 : return error;
851 : }
852 :
853 : /* decorrelation */
854 579 : hParamMC->h_freq_domain_decorr_ap_params = NULL;
855 579 : hParamMC->h_freq_domain_decorr_ap_state = NULL;
856 :
857 579 : ivas_dirac_dec_get_frequency_axis( frequency_axis, output_Fs, hParamMC->num_freq_bands );
858 :
859 579 : if ( ( error = ivas_dirac_dec_decorr_open( &( hParamMC->h_freq_domain_decorr_ap_params ), &( hParamMC->h_freq_domain_decorr_ap_state ), hParamMC->num_freq_bands, hParamMC->num_outputs_diff,
860 579 : hParamMC->diff_proto_info->num_protos_diff, DIRAC_SYNTHESIS_COV_MC_LS, frequency_axis, nchan_transport, output_Fs ) ) != IVAS_ERR_OK )
861 : {
862 0 : return error;
863 : }
864 :
865 579 : hParamMC->h_output_synthesis_params.use_onset_filters = 0;
866 579 : hParamMC->max_band_decorr = hParamMC->h_freq_domain_decorr_ap_params->max_band_decorr;
867 : /* init decorrelation */
868 579 : if ( hParamMC->max_band_decorr > 0 )
869 : {
870 :
871 579 : len = hParamMC->diff_proto_info->num_protos_diff * hParamMC->h_freq_domain_decorr_ap_params->h_onset_detection_power_params.max_band_decorr;
872 :
873 : /* init onsetDetectionPower */
874 579 : set_zero( hParamMC->h_freq_domain_decorr_ap_state->h_onset_detection_power_state.onset_detector_1, len );
875 579 : set_zero( hParamMC->h_freq_domain_decorr_ap_state->h_onset_detection_power_state.onset_detector_2, len );
876 : }
877 : }
878 2487 : hParamMC->max_band_energy_compensation = hParamMC->band_grouping[hParamMC->hMetadataPMC->nbands_coded];
879 2487 : max_param_band_residual = 0;
880 :
881 9966 : for ( k = hParamMC->hMetadataPMC->num_parameter_bands; k >= 0; k-- )
882 : {
883 9966 : if ( hParamMC->band_grouping[k] <= hParamMC->max_band_decorr )
884 : {
885 2487 : max_param_band_residual = k;
886 2487 : assert( hParamMC->band_grouping[k] == hParamMC->max_band_decorr );
887 2487 : break;
888 : }
889 : }
890 :
891 2487 : if ( nchan_transport_old != nchan_transport || num_param_bands_old != hParamMC->hMetadataPMC->num_parameter_bands )
892 : {
893 2343 : DIRAC_OUTPUT_SYNTHESIS_COV_STATE cov_state_old = hParamMC->h_output_synthesis_cov_state;
894 2343 : DIRAC_OUTPUT_SYNTHESIS_PARAMS params_old = hParamMC->h_output_synthesis_params;
895 : float tmp_buf[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
896 :
897 2343 : set_zero( tmp_buf, MAX_LS_CHANNELS * MAX_LS_CHANNELS );
898 :
899 : /* output synthesis */
900 2343 : if ( ( error = ivas_dirac_dec_output_synthesis_cov_open( &( hParamMC->h_output_synthesis_params ), &( hParamMC->h_output_synthesis_cov_state ), hParamMC->max_band_decorr, PARAM_MC_MAX_NSLOTS,
901 2343 : hParamMC->hMetadataPMC->num_parameter_bands, max_param_band_residual, nchan_transport, nchan_out_cov, proto_matrix ) ) != IVAS_ERR_OK )
902 : {
903 0 : return error;
904 : }
905 :
906 2343 : ivas_param_mc_dec_compute_interpolator( 0, 0, DEFAULT_JBM_CLDFB_TIMESLOTS, hParamMC->h_output_synthesis_params.interpolator );
907 :
908 2343 : ivas_dirac_dec_output_synthesis_cov_init( &( hParamMC->h_output_synthesis_cov_state ), nchan_transport, nchan_out_cov, hParamMC->hMetadataPMC->num_parameter_bands, max_param_band_residual );
909 :
910 : /* map old to new parameter banding, only for same number of TCs, needs some more thought for changing number of TCs */
911 2343 : if ( num_param_bands_old != hParamMC->hMetadataPMC->num_parameter_bands && nchan_transport_old == nchan_transport )
912 : {
913 : int16_t new_param_band_idx, source_param_idx;
914 :
915 :
916 23274 : for ( new_param_band_idx = 0; new_param_band_idx < hParamMC->hMetadataPMC->num_parameter_bands; new_param_band_idx++ )
917 : {
918 49986 : for ( source_param_idx = 0; source_param_idx < parameter_band_mapping.n_source_bands[new_param_band_idx]; source_param_idx++ )
919 : {
920 : /* Cx */
921 28476 : v_multc( cov_state_old.cx_old[parameter_band_mapping.source_band_idx[new_param_band_idx][source_param_idx]], parameter_band_mapping.source_band_factor[new_param_band_idx][source_param_idx], tmp_buf, nchan_transport_old * nchan_transport_old );
922 28476 : v_add( tmp_buf, hParamMC->h_output_synthesis_cov_state.cx_old[new_param_band_idx], hParamMC->h_output_synthesis_cov_state.cx_old[new_param_band_idx], nchan_transport_old * nchan_transport_old );
923 : /* Cy */
924 28476 : v_multc( cov_state_old.cy_old[parameter_band_mapping.source_band_idx[new_param_band_idx][source_param_idx]], parameter_band_mapping.source_band_factor[new_param_band_idx][source_param_idx], tmp_buf, nchan_out_cov * nchan_out_cov );
925 28476 : v_add( tmp_buf, hParamMC->h_output_synthesis_cov_state.cy_old[new_param_band_idx], hParamMC->h_output_synthesis_cov_state.cy_old[new_param_band_idx], nchan_out_cov * nchan_out_cov );
926 : /* mixing matrix*/
927 28476 : v_multc( cov_state_old.mixing_matrix_old[parameter_band_mapping.source_band_idx[new_param_band_idx][source_param_idx]], parameter_band_mapping.source_band_factor[new_param_band_idx][source_param_idx], tmp_buf, nchan_transport_old * nchan_out_cov );
928 28476 : v_add( tmp_buf, hParamMC->h_output_synthesis_cov_state.mixing_matrix_old[new_param_band_idx], hParamMC->h_output_synthesis_cov_state.mixing_matrix_old[new_param_band_idx], nchan_transport_old * nchan_out_cov );
929 : }
930 : }
931 18747 : for ( new_param_band_idx = 0; new_param_band_idx < max_param_band_residual; new_param_band_idx++ )
932 : {
933 40023 : for ( source_param_idx = 0; source_param_idx < parameter_band_mapping.n_source_bands[new_param_band_idx]; source_param_idx++ )
934 : {
935 : /* residual mixing matrix*/
936 23040 : v_multc( cov_state_old.mixing_matrix_res_old[parameter_band_mapping.source_band_idx[new_param_band_idx][source_param_idx]], parameter_band_mapping.source_band_factor[new_param_band_idx][source_param_idx], tmp_buf, nchan_out_cov * nchan_out_cov );
937 23040 : v_add( tmp_buf, hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_old[new_param_band_idx], hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_old[new_param_band_idx], nchan_out_cov * nchan_out_cov );
938 : }
939 : }
940 : }
941 :
942 2343 : ivas_dirac_dec_output_synthesis_cov_close( ¶ms_old, &cov_state_old );
943 : }
944 :
945 : /*-----------------------------------------------------------------*
946 : * memory allocation
947 : *-----------------------------------------------------------------*/
948 :
949 2487 : if ( hParamMC->max_band_decorr > 0 && nchan_transport_old != nchan_transport )
950 : {
951 579 : free( hParamMC->proto_frame_f );
952 579 : if ( ( hParamMC->proto_frame_f = (float *) malloc( 2 * hParamMC->diff_proto_info->num_protos_diff * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
953 : {
954 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
955 : }
956 579 : set_zero( hParamMC->proto_frame_f, 2 * hParamMC->diff_proto_info->num_protos_diff * hParamMC->num_freq_bands );
957 : }
958 :
959 :
960 2487 : if ( nchan_transport_old != nchan_transport )
961 : {
962 579 : if ( hParamMC->synthesis_conf != PARAM_MC_SYNTH_MONO_STEREO )
963 : {
964 : int16_t n_cldfb_slots;
965 :
966 579 : if ( hParamMC->Cldfb_RealBuffer_tc != NULL )
967 : {
968 579 : free( hParamMC->Cldfb_RealBuffer_tc );
969 579 : hParamMC->Cldfb_RealBuffer_tc = NULL;
970 : }
971 579 : if ( hParamMC->Cldfb_ImagBuffer_tc != NULL )
972 : {
973 579 : free( hParamMC->Cldfb_ImagBuffer_tc );
974 579 : hParamMC->Cldfb_ImagBuffer_tc = NULL;
975 : }
976 :
977 579 : n_cldfb_slots = DEFAULT_JBM_CLDFB_TIMESLOTS;
978 579 : if ( st_ivas->hDecoderConfig->Opt_tsm )
979 : {
980 123 : n_cldfb_slots = MAX_JBM_CLDFB_TIMESLOTS;
981 : }
982 579 : if ( ( hParamMC->Cldfb_RealBuffer_tc = (float *) malloc( n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
983 : {
984 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC JBM\n" ) );
985 : }
986 579 : set_zero( hParamMC->Cldfb_RealBuffer_tc, n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands );
987 :
988 579 : if ( ( hParamMC->Cldfb_ImagBuffer_tc = (float *) malloc( n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
989 : {
990 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC JBM\n" ) );
991 : }
992 579 : set_zero( hParamMC->Cldfb_ImagBuffer_tc, n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands );
993 : }
994 : else
995 : {
996 0 : if ( hParamMC->Cldfb_RealBuffer_tc != NULL )
997 : {
998 0 : free( hParamMC->Cldfb_RealBuffer_tc );
999 0 : hParamMC->Cldfb_RealBuffer_tc = NULL;
1000 : }
1001 0 : if ( hParamMC->Cldfb_ImagBuffer_tc != NULL )
1002 : {
1003 0 : free( hParamMC->Cldfb_ImagBuffer_tc );
1004 0 : hParamMC->Cldfb_ImagBuffer_tc = NULL;
1005 : }
1006 : }
1007 : }
1008 2487 : return error;
1009 : }
1010 :
1011 :
1012 : /*-------------------------------------------------------------------------
1013 : * param_mc_get_num_cldfb_syntheses()
1014 : *
1015 : * calculate the necessary number of CLDFB synthesis instances
1016 : *------------------------------------------------------------------------*/
1017 :
1018 : /*! r: number of cldfb synthesis instances */
1019 12558 : int16_t param_mc_get_num_cldfb_syntheses(
1020 : Decoder_Struct *st_ivas /* i : Parametric MC handle */
1021 : )
1022 : {
1023 : int16_t num_cldfb_syntheses;
1024 :
1025 12558 : num_cldfb_syntheses = 0;
1026 :
1027 : /* sanity check*/
1028 12558 : if ( st_ivas->hParamMC == NULL )
1029 : {
1030 0 : assert( 0 && "ParamMC handle does not exist!\n" );
1031 : }
1032 :
1033 12558 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1034 : {
1035 0 : num_cldfb_syntheses = 2;
1036 : }
1037 12558 : else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
1038 : {
1039 4242 : num_cldfb_syntheses = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
1040 : }
1041 8316 : else if ( st_ivas->renderer_type == RENDERER_MC_PARAMMC )
1042 : {
1043 8316 : num_cldfb_syntheses = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
1044 : }
1045 : #ifdef DEBUGGING
1046 : else
1047 : {
1048 : assert( 0 && "Renderer settings not valid for ParamMC!\n" );
1049 : }
1050 : #endif
1051 :
1052 12558 : return num_cldfb_syntheses;
1053 : }
1054 :
1055 :
1056 : /*-------------------------------------------------------------------------
1057 : * ivas_param_mc_dec_close()
1058 : *
1059 : * Close Parametric MC memories
1060 : *------------------------------------------------------------------------*/
1061 :
1062 12457 : void ivas_param_mc_dec_close(
1063 : PARAM_MC_DEC_HANDLE *hParamMC_out /* i/o: Parametric MC decoder handle */
1064 : )
1065 : {
1066 : uint16_t i;
1067 : PARAM_MC_DEC_HANDLE hParamMC;
1068 :
1069 12457 : if ( hParamMC_out == NULL || *hParamMC_out == NULL )
1070 : {
1071 1897 : return;
1072 : }
1073 :
1074 10560 : hParamMC = *hParamMC_out;
1075 :
1076 : /* close sub-modules */
1077 10560 : ivas_dirac_dec_output_synthesis_cov_close( &hParamMC->h_output_synthesis_params, &hParamMC->h_output_synthesis_cov_state );
1078 :
1079 10560 : if ( hParamMC->h_freq_domain_decorr_ap_params != NULL || hParamMC->h_freq_domain_decorr_ap_state != NULL )
1080 : {
1081 10314 : ivas_dirac_dec_decorr_close( &hParamMC->h_freq_domain_decorr_ap_params, &hParamMC->h_freq_domain_decorr_ap_state );
1082 : }
1083 :
1084 : /* parameter decoding */
1085 10560 : if ( hParamMC->hMetadataPMC != NULL )
1086 : {
1087 10560 : free( hParamMC->hMetadataPMC );
1088 10560 : hParamMC->hMetadataPMC = NULL;
1089 : }
1090 :
1091 10560 : if ( hParamMC->icc_q != NULL )
1092 : {
1093 10560 : free( hParamMC->icc_q );
1094 10560 : hParamMC->icc_q = NULL;
1095 : }
1096 :
1097 10560 : if ( hParamMC->icld_q != NULL )
1098 : {
1099 10560 : free( hParamMC->icld_q );
1100 10560 : hParamMC->icld_q = NULL;
1101 : }
1102 :
1103 : /* diffuse prototype info */
1104 10560 : if ( hParamMC->diff_proto_info )
1105 : {
1106 41262 : for ( i = 0; i < hParamMC->diff_proto_info->num_protos_diff; i++ )
1107 : {
1108 30948 : free( hParamMC->diff_proto_info->source_chan_idx[i] );
1109 30948 : hParamMC->diff_proto_info->source_chan_idx[i] = NULL;
1110 :
1111 30948 : free( hParamMC->diff_proto_info->proto_fac[i] );
1112 30948 : hParamMC->diff_proto_info->proto_fac[i] = NULL;
1113 : }
1114 :
1115 10314 : free( hParamMC->diff_proto_info->source_chan_idx );
1116 10314 : hParamMC->diff_proto_info->source_chan_idx = NULL;
1117 :
1118 10314 : free( hParamMC->diff_proto_info->proto_fac );
1119 10314 : hParamMC->diff_proto_info->proto_fac = NULL;
1120 :
1121 10314 : free( hParamMC->diff_proto_info->proto_index_diff );
1122 10314 : hParamMC->diff_proto_info->proto_index_diff = NULL;
1123 :
1124 10314 : free( hParamMC->diff_proto_info->num_source_chan_diff );
1125 10314 : hParamMC->diff_proto_info->num_source_chan_diff = NULL;
1126 :
1127 10314 : free( hParamMC->diff_proto_info );
1128 10314 : hParamMC->diff_proto_info = NULL;
1129 : }
1130 :
1131 : /* States */
1132 : /* free prototype signal buffers */
1133 10560 : if ( hParamMC->proto_frame_f != NULL )
1134 : {
1135 10314 : free( hParamMC->proto_frame_f );
1136 10314 : hParamMC->proto_frame_f = NULL;
1137 : }
1138 :
1139 10560 : if ( hParamMC->proto_frame_dec_f != NULL )
1140 : {
1141 10314 : free( hParamMC->proto_frame_dec_f );
1142 10314 : hParamMC->proto_frame_dec_f = NULL;
1143 : }
1144 :
1145 10560 : if ( hParamMC->ls_conv_dmx_matrix != NULL )
1146 : {
1147 249 : free( hParamMC->ls_conv_dmx_matrix );
1148 249 : hParamMC->ls_conv_dmx_matrix = NULL;
1149 : }
1150 :
1151 10560 : if ( hParamMC->proto_matrix_int != NULL )
1152 : {
1153 10560 : free( hParamMC->proto_matrix_int );
1154 10560 : hParamMC->proto_matrix_int = NULL;
1155 : }
1156 :
1157 10560 : if ( hParamMC->hoa_encoder != NULL )
1158 : {
1159 1230 : free( hParamMC->hoa_encoder );
1160 1230 : hParamMC->hoa_encoder = NULL;
1161 : }
1162 :
1163 10560 : if ( hParamMC->Cldfb_RealBuffer_tc != NULL )
1164 : {
1165 10314 : free( hParamMC->Cldfb_RealBuffer_tc );
1166 10314 : hParamMC->Cldfb_RealBuffer_tc = NULL;
1167 : }
1168 10560 : if ( hParamMC->Cldfb_ImagBuffer_tc != NULL )
1169 : {
1170 10314 : free( hParamMC->Cldfb_ImagBuffer_tc );
1171 10314 : hParamMC->Cldfb_ImagBuffer_tc = NULL;
1172 : }
1173 :
1174 10560 : free( *hParamMC_out );
1175 10560 : *hParamMC_out = NULL;
1176 :
1177 10560 : return;
1178 : }
1179 :
1180 :
1181 : /*-------------------------------------------------------------------------
1182 : * ivas_param_mc_dec_read_BS()
1183 : *
1184 : * Read the Parametric MC metadata
1185 : *------------------------------------------------------------------------*/
1186 :
1187 338124 : void ivas_param_mc_dec_read_BS(
1188 : const int32_t ivas_total_brate, /* i : IVAS total bitrate */
1189 : Decoder_State *st, /* i/o: decoder state structure */
1190 : PARAM_MC_DEC_HANDLE hParamMC, /* i/o: decoder ParamMC handle */
1191 : int16_t *nb_bits /* o : number of bits written */
1192 : )
1193 : {
1194 : int16_t param_frame_idx;
1195 : int16_t band_step;
1196 : uint16_t bit_buffer[PARAM_MC_MAX_BITS];
1197 : int16_t bits_to_copy;
1198 : int16_t bit_pos;
1199 : int16_t num_lfe_bands;
1200 : int16_t num_param_bands;
1201 : int16_t metadata_bit_pos;
1202 : int16_t i, j, k;
1203 : int16_t icc_map_size;
1204 : int16_t icc_map_size_wo_lfe;
1205 : int16_t ild_map_size;
1206 : int16_t ild_map_size_wo_lfe;
1207 : HANDLE_IVAS_PARAM_MC_METADATA hMetadataPMC;
1208 :
1209 338124 : push_wmops( "param_mc_read_bs" );
1210 :
1211 : /* Inits */
1212 338124 : *nb_bits = 0;
1213 338124 : hMetadataPMC = hParamMC->hMetadataPMC;
1214 338124 : icc_map_size = hMetadataPMC->icc_mapping_conf->icc_map_size_lfe;
1215 338124 : icc_map_size_wo_lfe = hMetadataPMC->icc_mapping_conf->icc_map_size_wo_lfe;
1216 338124 : ild_map_size = hMetadataPMC->ild_mapping_conf->ild_map_size_lfe;
1217 338124 : ild_map_size_wo_lfe = hMetadataPMC->ild_mapping_conf->ild_map_size_wo_lfe;
1218 :
1219 338124 : if ( !st->bfi )
1220 : {
1221 335469 : metadata_bit_pos = (int16_t) ( ivas_total_brate / FRAMES_PER_SEC - 1 );
1222 335469 : bits_to_copy = (int16_t) min( ivas_total_brate / FRAMES_PER_SEC, PARAM_MC_MAX_BITS );
1223 :
1224 : /* copy and reverse metadata */
1225 340355949 : for ( bit_pos = 0; bit_pos < bits_to_copy; bit_pos++ )
1226 : {
1227 340020480 : bit_buffer[bit_pos] = st->bit_stream[metadata_bit_pos--];
1228 : }
1229 :
1230 335469 : bit_pos = 0;
1231 :
1232 : /* read reserved bit */
1233 335469 : hMetadataPMC->lfe_on = bit_buffer[bit_pos++];
1234 :
1235 : /* get coded bwidth */
1236 : {
1237 : int16_t pos;
1238 335469 : int16_t bw = 0;
1239 1006407 : for ( pos = 0; pos < 2; pos++ )
1240 : {
1241 670938 : bw += ( bit_buffer[bit_pos++] << pos );
1242 : }
1243 335469 : hMetadataPMC->coded_bwidth = bw;
1244 : }
1245 :
1246 : /* set tables if coded band width differs from last frame */
1247 335469 : if ( hMetadataPMC->coded_bwidth != hMetadataPMC->last_coded_bwidth )
1248 : {
1249 2022 : ivas_param_mc_set_coded_bands( hMetadataPMC );
1250 2022 : param_mc_set_num_synth_bands( st->output_Fs, hParamMC );
1251 2022 : hParamMC->max_band_energy_compensation = hParamMC->band_grouping[hParamMC->num_param_bands_synth];
1252 : }
1253 :
1254 335469 : param_frame_idx = bit_buffer[bit_pos++];
1255 335469 : hMetadataPMC->param_frame_idx = param_frame_idx;
1256 335469 : num_param_bands = hMetadataPMC->nbands_in_param_frame[param_frame_idx];
1257 :
1258 335469 : hMetadataPMC->bAttackPresent = bit_buffer[bit_pos++];
1259 335469 : hMetadataPMC->attackIndex = 0;
1260 335469 : band_step = 1;
1261 335469 : num_lfe_bands = 0;
1262 :
1263 335469 : if ( hMetadataPMC->bAttackPresent )
1264 : {
1265 66684 : for ( i = 2; i >= 0; i-- )
1266 : {
1267 50013 : hMetadataPMC->attackIndex += ( bit_buffer[bit_pos++] << i );
1268 : }
1269 :
1270 16671 : band_step = PARAM_MC_TRANSIENT_BAND_STEP;
1271 16671 : num_lfe_bands = PARAM_MC_MAX_BAND_LFE / band_step + ( PARAM_MC_MAX_BAND_LFE % band_step ) ? 1 : 0;
1272 16671 : num_param_bands = hMetadataPMC->nbands_coded / band_step + ( ( hMetadataPMC->nbands_coded % band_step ) ? 1 : 0 );
1273 : }
1274 : else
1275 : {
1276 637596 : for ( j = 0; j < PARAM_MC_MAX_BAND_LFE; j += band_step )
1277 : {
1278 318798 : if ( param_frame_idx == hMetadataPMC->coding_band_mapping[j] )
1279 : {
1280 : /* LFE ICC is always the last ICC in coding band 0 */
1281 159711 : num_lfe_bands++;
1282 : }
1283 : }
1284 : }
1285 :
1286 335469 : if ( !hMetadataPMC->lfe_on )
1287 : {
1288 3123 : num_lfe_bands = 0;
1289 : }
1290 :
1291 335469 : ivas_param_mc_bs_decode_parameter_values( bit_buffer, &bit_pos, bits_to_copy, &st->BER_detect, hMetadataPMC, &hMetadataPMC->icc_coding,
1292 : icc_map_size_wo_lfe, icc_map_size, num_lfe_bands, band_step, num_param_bands, hParamMC->icc_q );
1293 335469 : if ( !st->BER_detect )
1294 : {
1295 335469 : ivas_param_mc_bs_decode_parameter_values( bit_buffer, &bit_pos, bits_to_copy, &st->BER_detect, hMetadataPMC, &hMetadataPMC->ild_coding,
1296 : ild_map_size_wo_lfe, ild_map_size, num_lfe_bands, band_step, num_param_bands, hParamMC->icld_q );
1297 : }
1298 : /* set LFE ILD and ICC to zero above PARAM_MC_MAX_BAND_LFE for attack frames */
1299 335469 : if ( hMetadataPMC->bAttackPresent )
1300 : {
1301 33276 : for ( k = PARAM_MC_MAX_BAND_LFE; k < band_step * num_lfe_bands; k++ )
1302 : {
1303 16605 : hParamMC->icc_q[( k + 1 ) * icc_map_size - 1] = 1.0f;
1304 16605 : hParamMC->icld_q[( k + 1 ) * ild_map_size - 1] = PARAM_MC_DEFAULT_MIN_ILD;
1305 : }
1306 : }
1307 :
1308 335469 : *nb_bits = bit_pos;
1309 :
1310 : } /* if ( !st->bfi ) */
1311 :
1312 338124 : if ( st->bfi )
1313 : {
1314 : /* for PLC, use the saved ILDs and ICCs from the past and set the transient flag and transient position to zero */
1315 2655 : hMetadataPMC->bAttackPresent = 0;
1316 2655 : hMetadataPMC->attackIndex = 0;
1317 : }
1318 :
1319 338124 : pop_wmops();
1320 :
1321 338124 : return;
1322 : }
1323 :
1324 :
1325 : /*-------------------------------------------------------------------------
1326 : * ivas_param_mc_dec_digest_tc()
1327 : *
1328 : *
1329 : *------------------------------------------------------------------------*/
1330 :
1331 335436 : void ivas_param_mc_dec_digest_tc(
1332 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1333 : const uint8_t nCldfbSlots, /* i : number of CLDFB slots in transport channels */
1334 : float *p_data_f[] /* i/o: synthesized core-coder transport channels/DirAC output */
1335 : )
1336 : {
1337 : int16_t ch, num_freq_bands, slot_idx, cldfb_ch, n_ch_cldfb;
1338 : float *cldfb_real_buffer, *cldfb_imag_buffer;
1339 :
1340 : /* Initialization */
1341 335436 : num_freq_bands = st_ivas->hParamMC->num_freq_bands;
1342 335436 : n_ch_cldfb = st_ivas->hTcBuffer->nchan_transport_jbm - st_ivas->hTcBuffer->nchan_buffer_full;
1343 :
1344 335436 : cldfb_real_buffer = st_ivas->hParamMC->Cldfb_RealBuffer_tc;
1345 335436 : cldfb_imag_buffer = st_ivas->hParamMC->Cldfb_ImagBuffer_tc;
1346 :
1347 : /* CLDFB Analysis */
1348 1031673 : for ( ch = 0, cldfb_ch = 0; cldfb_ch < n_ch_cldfb; cldfb_ch++, ch++ )
1349 : {
1350 11836032 : for ( slot_idx = 0; slot_idx < nCldfbSlots; slot_idx++ )
1351 : {
1352 11139795 : cldfbAnalysis_ts( &( p_data_f[ch][num_freq_bands * slot_idx] ),
1353 11139795 : &cldfb_real_buffer[slot_idx * num_freq_bands * n_ch_cldfb + cldfb_ch * num_freq_bands],
1354 11139795 : &cldfb_imag_buffer[slot_idx * num_freq_bands * n_ch_cldfb + cldfb_ch * num_freq_bands],
1355 : num_freq_bands, st_ivas->cldfbAnaDec[cldfb_ch] );
1356 : }
1357 : }
1358 :
1359 335436 : return;
1360 : }
1361 :
1362 :
1363 : /*-------------------------------------------------------------------------
1364 : * ivas_param_mc_dec_prepare_renderer()
1365 : *
1366 : *
1367 : *------------------------------------------------------------------------*/
1368 :
1369 335436 : void ivas_param_mc_dec_prepare_renderer(
1370 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1371 : const uint8_t nCldfbSlots /* i : number of CLDFB slots in transport channels */
1372 : )
1373 : {
1374 : PARAM_MC_DEC_HANDLE hParamMC;
1375 : int16_t i;
1376 : int16_t is_next_band, skip_next_band;
1377 : int16_t slot_idx, param_band_idx;
1378 : int16_t nchan_transport, nchan_out_transport, nchan_out_cldfb;
1379 : int16_t nchan_out_cov;
1380 : /*CLDFB*/
1381 : float *pCx, *pCx_imag;
1382 : float cx[PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS];
1383 : float cx_imag[PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS];
1384 : float cx_next_band[PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS];
1385 : float cx_imag_next_band[PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS];
1386 : float real_part, imag_part;
1387 : /* format converter */
1388 : int16_t channel_active[MAX_OUTPUT_CHANNELS];
1389 : IVAS_OUTPUT_SETUP *hSynthesisOutputSetup;
1390 :
1391 335436 : hParamMC = st_ivas->hParamMC;
1392 335436 : assert( hParamMC );
1393 :
1394 335436 : push_wmops( "param_mc_dec_digest_tc" );
1395 :
1396 335436 : set_s( channel_active, 0, MAX_LS_CHANNELS );
1397 335436 : nchan_transport = st_ivas->nchan_transport;
1398 335436 : nchan_out_transport = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
1399 :
1400 335436 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1401 : {
1402 243309 : nchan_out_cldfb = BINAURAL_CHANNELS;
1403 243309 : set_s( channel_active, 1, nchan_out_cldfb );
1404 243309 : nchan_out_cov = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
1405 243309 : hSynthesisOutputSetup = &st_ivas->hTransSetup;
1406 : }
1407 92127 : else if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_CLDFB )
1408 : {
1409 8145 : nchan_out_cov = nchan_out_transport;
1410 8145 : nchan_out_cldfb = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
1411 8145 : hSynthesisOutputSetup = &st_ivas->hTransSetup;
1412 : }
1413 83982 : else if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
1414 : {
1415 23928 : nchan_out_cov = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
1416 23928 : nchan_out_cldfb = nchan_out_cov;
1417 23928 : set_s( channel_active, 1, nchan_out_cov );
1418 23928 : hSynthesisOutputSetup = &st_ivas->hOutSetup;
1419 : }
1420 : else
1421 : {
1422 60054 : nchan_out_cov = nchan_out_transport;
1423 60054 : nchan_out_cldfb = nchan_out_transport;
1424 60054 : set_s( channel_active, 1, nchan_out_cov );
1425 60054 : hSynthesisOutputSetup = &st_ivas->hTransSetup;
1426 : }
1427 :
1428 : /* adapt transient position */
1429 335436 : if ( hParamMC->hMetadataPMC->bAttackPresent )
1430 : {
1431 16374 : hParamMC->hMetadataPMC->attackIndex = (int16_t) max( 0, hParamMC->hMetadataPMC->attackIndex + ( ( nCldfbSlots - DEFAULT_JBM_CLDFB_TIMESLOTS ) / 2 ) );
1432 : }
1433 : /* adapt subframes */
1434 335436 : hParamMC->num_slots = nCldfbSlots;
1435 335436 : hParamMC->slots_rendered = 0;
1436 335436 : hParamMC->subframes_rendered = 0;
1437 335436 : ivas_jbm_dec_get_adapted_subframes( nCldfbSlots, hParamMC->subframe_nbslots, &hParamMC->nb_subframes );
1438 335436 : st_ivas->hTcBuffer->nb_subframes = hParamMC->nb_subframes;
1439 335436 : mvs2s( hParamMC->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, hParamMC->nb_subframes );
1440 :
1441 335436 : ivas_param_mc_dec_compute_interpolator( hParamMC->hMetadataPMC->bAttackPresent, hParamMC->hMetadataPMC->attackIndex, nCldfbSlots, hParamMC->h_output_synthesis_params.interpolator );
1442 :
1443 : /* loop over two bands at a time */
1444 2572074 : for ( param_band_idx = 0; param_band_idx < hParamMC->num_param_bands_synth; param_band_idx += 2 )
1445 : {
1446 : /* don't process next band if it exceeds the limit */
1447 2236638 : skip_next_band = ( ( param_band_idx + 1 ) == hParamMC->num_param_bands_synth ) ? 1 : 0;
1448 :
1449 2236638 : set_zero( cx, PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS );
1450 2236638 : set_zero( cx_imag, PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS );
1451 2236638 : set_zero( cx_next_band, PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS );
1452 2236638 : set_zero( cx_imag_next_band, PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS );
1453 :
1454 : /* slot loop for gathering the input data */
1455 38022834 : for ( slot_idx = 0; slot_idx < nCldfbSlots; slot_idx++ )
1456 : {
1457 35786196 : if ( slot_idx >= 2 * hParamMC->hMetadataPMC->attackIndex )
1458 : {
1459 105221610 : for ( is_next_band = 0; is_next_band < 2; is_next_band++ )
1460 : {
1461 70147740 : if ( is_next_band && skip_next_band )
1462 : {
1463 842757 : continue;
1464 : }
1465 :
1466 69304983 : ivas_dirac_dec_output_synthesis_cov_param_mc_collect_slot( &hParamMC->Cldfb_RealBuffer_tc[slot_idx * hParamMC->num_freq_bands * nchan_transport],
1467 69304983 : &hParamMC->Cldfb_ImagBuffer_tc[slot_idx * hParamMC->num_freq_bands * nchan_transport],
1468 : is_next_band ? cx_next_band : cx,
1469 : is_next_band ? cx_imag_next_band : cx_imag,
1470 69304983 : param_band_idx + is_next_band,
1471 : hParamMC,
1472 : nchan_transport );
1473 : }
1474 : }
1475 : }
1476 :
1477 : /* map from complex input covariance to real values */
1478 6709914 : for ( is_next_band = 0; is_next_band < 2; is_next_band++ )
1479 : {
1480 4473276 : if ( is_next_band && skip_next_band )
1481 : {
1482 54189 : continue;
1483 : }
1484 : /* Cx for transport channels */
1485 4419087 : pCx = is_next_band ? &cx_next_band[0] : &cx[0];
1486 4419087 : pCx_imag = is_next_band ? &cx_imag_next_band[0] : &cx_imag[0];
1487 24084465 : for ( i = 0; i < nchan_transport * nchan_transport; i++ )
1488 : {
1489 19665378 : real_part = pCx[i];
1490 19665378 : imag_part = pCx_imag[i];
1491 :
1492 : /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
1493 19665378 : if ( param_band_idx < hParamMC->max_param_band_abs_cov )
1494 : {
1495 11542350 : pCx[i] = sqrtf( real_part * real_part + imag_part * imag_part );
1496 : }
1497 : else
1498 : {
1499 8123028 : pCx[i] = real_part;
1500 : }
1501 : }
1502 : }
1503 :
1504 : /* we have to do it similar to the encoder in case of attacks (i.e. accumulate two bands) to ensure correct DMX of the target covariance*/
1505 2236638 : if ( hParamMC->hMetadataPMC->bAttackPresent && ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO ) )
1506 : {
1507 4956 : v_add( cx, cx_next_band, cx, nchan_transport * nchan_transport );
1508 4956 : mvr2r( cx, cx_next_band, nchan_transport * nchan_transport );
1509 : }
1510 :
1511 :
1512 6709914 : for ( is_next_band = 0; is_next_band < 2; is_next_band++ )
1513 : {
1514 4473276 : if ( is_next_band && skip_next_band )
1515 : {
1516 54189 : continue;
1517 : }
1518 :
1519 : /* generate mixing matrices */
1520 4419087 : ivas_param_mc_get_mixing_matrices( hParamMC,
1521 : hSynthesisOutputSetup,
1522 : is_next_band ? cx_next_band : cx,
1523 4419087 : param_band_idx + is_next_band,
1524 4419087 : hParamMC->h_output_synthesis_cov_state.mixing_matrix,
1525 4419087 : hParamMC->h_output_synthesis_cov_state.mixing_matrix_res,
1526 : nchan_out_transport,
1527 : hParamMC->synthesis_conf,
1528 : nchan_transport,
1529 : nchan_out_cov );
1530 : }
1531 : }
1532 :
1533 335436 : pop_wmops();
1534 :
1535 335436 : return;
1536 : }
1537 :
1538 :
1539 : /*-------------------------------------------------------------------------
1540 : * ivas_param_mc_dec_render()
1541 : *
1542 : * Parametric MC rendering process
1543 : *------------------------------------------------------------------------*/
1544 :
1545 759717 : void ivas_param_mc_dec_render(
1546 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1547 : const uint16_t nSamplesAsked, /* i : number of CLDFB slots requested */
1548 : uint16_t *nSamplesRendered, /* o : number of CLDFB slots rendered */
1549 : uint16_t *nSamplesAvailableNext, /* o : number of CLDFB slots still to render */
1550 : float *output_f[] /* o : rendered time signal */
1551 : )
1552 : {
1553 : PARAM_MC_DEC_HANDLE hParamMC;
1554 : int16_t i, ch;
1555 : int16_t subframe_idx;
1556 : int16_t slot_idx, slot_idx_start, slot_idx_start_cldfb_synth, first_sf, last_sf, slots_to_render;
1557 : int16_t nchan_transport, nchan_out_transport, nchan_out_cldfb;
1558 : int16_t nchan_out_cov;
1559 : /*CLDFB*/
1560 : float Cldfb_RealBuffer[MAX_INTERN_CHANNELS][PARAM_MC_MAX_NSLOTS_IN_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
1561 : float Cldfb_ImagBuffer[MAX_INTERN_CHANNELS][PARAM_MC_MAX_NSLOTS_IN_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
1562 : float Cldfb_RealBuffer_Binaural[MAX_HEAD_ROT_POSES][BINAURAL_CHANNELS][PARAM_MC_MAX_NSLOTS_IN_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
1563 : float Cldfb_ImagBuffer_Binaural[MAX_HEAD_ROT_POSES][BINAURAL_CHANNELS][PARAM_MC_MAX_NSLOTS_IN_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
1564 : /*Decorrelator*/
1565 : float onset_filter[MAX_LS_CHANNELS * CLDFB_NO_CHANNELS_MAX];
1566 : /* format converter */
1567 : int16_t channel_active[MAX_OUTPUT_CHANNELS];
1568 : uint16_t nband_synth, nbands_to_zero;
1569 : uint16_t nchan_out_init;
1570 : uint32_t output_Fs;
1571 :
1572 759717 : hParamMC = st_ivas->hParamMC;
1573 759717 : assert( hParamMC );
1574 :
1575 759717 : push_wmops( "param_mc_dec_render" );
1576 :
1577 759717 : set_s( channel_active, 0, MAX_LS_CHANNELS );
1578 759717 : nchan_transport = st_ivas->nchan_transport;
1579 759717 : nchan_out_transport = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
1580 759717 : nchan_out_init = nchan_out_transport;
1581 759717 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
1582 :
1583 759717 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1584 : {
1585 578154 : nchan_out_cldfb = BINAURAL_CHANNELS;
1586 578154 : set_s( channel_active, 1, nchan_out_cldfb );
1587 578154 : if ( st_ivas->hCombinedOrientationData )
1588 : {
1589 390288 : nchan_out_init = MAX_INTERN_CHANNELS;
1590 : }
1591 578154 : nchan_out_cov = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
1592 : }
1593 181563 : else if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_CLDFB )
1594 : {
1595 19005 : nchan_out_cov = nchan_out_transport;
1596 19005 : nchan_out_cldfb = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
1597 : }
1598 162558 : else if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
1599 : {
1600 55832 : nchan_out_cov = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
1601 55832 : nchan_out_cldfb = nchan_out_cov;
1602 55832 : set_s( channel_active, 1, nchan_out_cov );
1603 : }
1604 : else
1605 : {
1606 106726 : nchan_out_cov = nchan_out_transport;
1607 106726 : nchan_out_cldfb = nchan_out_transport;
1608 106726 : set_s( channel_active, 1, nchan_out_cov );
1609 : }
1610 :
1611 : /* set everything to zero that will not be decoded */
1612 759717 : nband_synth = hParamMC->band_grouping[hParamMC->num_param_bands_synth];
1613 759717 : nbands_to_zero = hParamMC->num_freq_bands - nband_synth;
1614 9498483 : for ( ch = 0; ch < nchan_out_init; ch++ )
1615 : {
1616 43693830 : for ( slot_idx = 0; slot_idx < JBM_CLDFB_SLOTS_IN_SUBFRAME; slot_idx++ )
1617 : {
1618 34955064 : set_zero( &( Cldfb_RealBuffer[ch][slot_idx][nband_synth] ), nbands_to_zero );
1619 34955064 : set_zero( &( Cldfb_ImagBuffer[ch][slot_idx][nband_synth] ), nbands_to_zero );
1620 : }
1621 : }
1622 :
1623 : /* loop for synthesis, assume we always have to render in multiples of 5ms subframes with spills */
1624 759717 : slots_to_render = min( hParamMC->num_slots - hParamMC->slots_rendered, nSamplesAsked / NS2SA( output_Fs, CLDFB_SLOT_NS ) );
1625 759717 : *nSamplesRendered = slots_to_render * NS2SA( output_Fs, CLDFB_SLOT_NS );
1626 759717 : first_sf = hParamMC->subframes_rendered;
1627 759717 : last_sf = first_sf;
1628 2111895 : while ( slots_to_render > 0 )
1629 : {
1630 1352178 : slots_to_render -= hParamMC->subframe_nbslots[last_sf];
1631 1352178 : last_sf++;
1632 : }
1633 : #ifdef DEBUGGING
1634 : assert( slots_to_render == 0 );
1635 : #endif
1636 759717 : if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
1637 : {
1638 67165 : for ( subframe_idx = first_sf; subframe_idx < last_sf; subframe_idx++ )
1639 : {
1640 42420 : slots_to_render += hParamMC->subframe_nbslots[subframe_idx];
1641 : }
1642 : }
1643 759717 : slot_idx_start = hParamMC->slots_rendered;
1644 759717 : slot_idx_start_cldfb_synth = 0;
1645 2111895 : for ( subframe_idx = first_sf; subframe_idx < last_sf; subframe_idx++ )
1646 : {
1647 6719160 : for ( slot_idx = 0; slot_idx < hParamMC->subframe_nbslots[subframe_idx]; slot_idx++, hParamMC->slots_rendered++ )
1648 : {
1649 :
1650 5366982 : if ( hParamMC->max_band_decorr > 0 )
1651 : {
1652 : /*-----------------------------------------------------------------*
1653 : * protoype signal computation
1654 : *-----------------------------------------------------------------*/
1655 :
1656 5366982 : param_mc_protoSignalComputation( &hParamMC->Cldfb_RealBuffer_tc[hParamMC->slots_rendered * nchan_transport * hParamMC->num_freq_bands],
1657 5366982 : &hParamMC->Cldfb_ImagBuffer_tc[hParamMC->slots_rendered * nchan_transport * hParamMC->num_freq_bands],
1658 5366982 : hParamMC->proto_frame_f, hParamMC->diff_proto_info,
1659 5366982 : hParamMC->num_freq_bands );
1660 :
1661 : /*-----------------------------------------------------------------*
1662 : * frequency domain decorrelation
1663 : *-----------------------------------------------------------------*/
1664 :
1665 : /* decorrelate prototype frame */
1666 5366982 : ivas_dirac_dec_decorr_process( hParamMC->num_freq_bands,
1667 5366982 : hParamMC->num_outputs_diff,
1668 5366982 : hParamMC->diff_proto_info->num_protos_diff,
1669 : DIRAC_SYNTHESIS_COV_MC_LS,
1670 : nchan_transport,
1671 5366982 : hParamMC->proto_frame_f,
1672 5366982 : hParamMC->diff_proto_info->num_protos_diff,
1673 5366982 : hParamMC->diff_proto_info->proto_index_diff,
1674 : hParamMC->proto_frame_dec_f,
1675 : onset_filter,
1676 : hParamMC->h_freq_domain_decorr_ap_params,
1677 : hParamMC->h_freq_domain_decorr_ap_state );
1678 :
1679 : /* copy decorrelated frame directly to output CLDFB buffer, acts also as intermediate */
1680 : /* memory for the decorrelated signal */
1681 5366982 : ivas_param_mc_dec_copy_diffuse_proto( hParamMC, Cldfb_RealBuffer, Cldfb_ImagBuffer, nchan_out_cov, slot_idx );
1682 : }
1683 :
1684 : /*-----------------------------------------------------------------*
1685 : * output synthesis
1686 : *-----------------------------------------------------------------*/
1687 :
1688 5366982 : ivas_dirac_dec_output_synthesis_cov_param_mc_synthesise_slot( &hParamMC->Cldfb_RealBuffer_tc[hParamMC->slots_rendered * nchan_transport * hParamMC->num_freq_bands],
1689 5366982 : &hParamMC->Cldfb_ImagBuffer_tc[hParamMC->slots_rendered * nchan_transport * hParamMC->num_freq_bands],
1690 : Cldfb_RealBuffer, Cldfb_ImagBuffer,
1691 5366982 : hParamMC->h_output_synthesis_cov_state.mixing_matrix, hParamMC->h_output_synthesis_cov_state.mixing_matrix_res, slot_idx, slot_idx + slot_idx_start,
1692 : nchan_transport, nchan_out_cov, hParamMC );
1693 :
1694 5366982 : if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ) )
1695 : {
1696 3892950 : if (
1697 3892950 : st_ivas->hCombinedOrientationData && st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
1698 : {
1699 1823862 : ivas_param_mc_mc2sba_cldfb( st_ivas->hTransSetup, hParamMC->hoa_encoder, slot_idx, Cldfb_RealBuffer, Cldfb_ImagBuffer, nband_synth, GAIN_LFE );
1700 : }
1701 : else
1702 : {
1703 : /* remove LFE */
1704 : uint16_t idx_out;
1705 : uint16_t idx_lfe;
1706 : IVAS_OUTPUT_SETUP hLsSetup;
1707 :
1708 2069088 : hLsSetup = st_ivas->hTransSetup;
1709 :
1710 : /* If LFE should be rendered, add it to other channels before removing */
1711 2069088 : if ( st_ivas->hBinRenderer->render_lfe )
1712 : {
1713 4138176 : for ( idx_lfe = 0; idx_lfe < hLsSetup.num_lfe; idx_lfe++ )
1714 : {
1715 : /* Copy just the first band of LFE*/
1716 2069088 : v_multc( Cldfb_RealBuffer[hLsSetup.index_lfe[idx_lfe]][slot_idx], ( GAIN_LFE / hLsSetup.nchan_out_woLFE ), Cldfb_RealBuffer[hLsSetup.index_lfe[idx_lfe]][slot_idx], 1 );
1717 2069088 : v_multc( Cldfb_ImagBuffer[hLsSetup.index_lfe[idx_lfe]][slot_idx], ( GAIN_LFE / hLsSetup.nchan_out_woLFE ), Cldfb_ImagBuffer[hLsSetup.index_lfe[idx_lfe]][slot_idx], 1 );
1718 :
1719 14922336 : for ( ch = 0; ch < ( hLsSetup.nchan_out_woLFE + hLsSetup.num_lfe ); ch++ )
1720 : {
1721 12853248 : if ( hLsSetup.index_lfe[idx_lfe] != ch )
1722 : {
1723 10784160 : v_add( Cldfb_RealBuffer[ch][slot_idx], Cldfb_RealBuffer[hLsSetup.index_lfe[idx_lfe]][slot_idx], Cldfb_RealBuffer[ch][slot_idx], 1 );
1724 10784160 : v_add( Cldfb_ImagBuffer[ch][slot_idx], Cldfb_ImagBuffer[hLsSetup.index_lfe[idx_lfe]][slot_idx], Cldfb_ImagBuffer[ch][slot_idx], 1 );
1725 : }
1726 : }
1727 : }
1728 : }
1729 :
1730 2069088 : idx_out = 0;
1731 2069088 : idx_lfe = 0;
1732 :
1733 14922336 : for ( ch = 0; ch < ( hLsSetup.nchan_out_woLFE + hLsSetup.num_lfe ); ch++ )
1734 : {
1735 12853248 : if ( ( hLsSetup.num_lfe > 0 ) && ( hLsSetup.index_lfe[idx_lfe] == ch ) )
1736 : {
1737 2069088 : if ( idx_lfe < ( hLsSetup.num_lfe - 1 ) )
1738 : {
1739 0 : idx_lfe++;
1740 : }
1741 : }
1742 10784160 : else if ( ch != idx_out )
1743 : {
1744 4576896 : mvr2r( Cldfb_RealBuffer[ch][slot_idx], Cldfb_RealBuffer[idx_out][slot_idx], nband_synth );
1745 4576896 : mvr2r( Cldfb_ImagBuffer[ch][slot_idx], Cldfb_ImagBuffer[idx_out][slot_idx], nband_synth );
1746 4576896 : idx_out++;
1747 : }
1748 : else
1749 : {
1750 6207264 : idx_out++;
1751 : }
1752 : }
1753 : }
1754 : }
1755 : }
1756 :
1757 1352178 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1758 : {
1759 983670 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
1760 : {
1761 0 : if ( st_ivas->hSplitBinRend->hCldfbDataOut != NULL )
1762 : {
1763 0 : for ( slot_idx = 0; slot_idx < MAX_PARAM_SPATIAL_SUBFRAMES; slot_idx++ )
1764 : {
1765 0 : for ( ch = 0; ch < ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ); ch++ )
1766 : {
1767 0 : mvr2r( Cldfb_RealBuffer[ch][slot_idx], st_ivas->hSplitBinRend->hCldfbDataOut->Cldfb_RealBuffer[ch][slot_idx_start + slot_idx], hParamMC->num_freq_bands );
1768 0 : mvr2r( Cldfb_ImagBuffer[ch][slot_idx], st_ivas->hSplitBinRend->hCldfbDataOut->Cldfb_ImagBuffer[ch][slot_idx_start + slot_idx], hParamMC->num_freq_bands );
1769 : }
1770 : }
1771 0 : st_ivas->hSplitBinRend->hCldfbDataOut->config = st_ivas->hIntSetup.output_config;
1772 : }
1773 : }
1774 :
1775 983670 : ivas_binRenderer( st_ivas->hBinRenderer,
1776 0 : ( st_ivas->hSplitBinRend == NULL ) ? NULL : &st_ivas->hSplitBinRend->splitrend.multiBinPoseData,
1777 : #ifdef SPLIT_REND_WITH_HEAD_ROT_DEBUG
1778 : NULL,
1779 : #endif
1780 : st_ivas->hCombinedOrientationData,
1781 983670 : hParamMC->subframe_nbslots[subframe_idx],
1782 : Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural, Cldfb_RealBuffer, Cldfb_ImagBuffer );
1783 :
1784 983670 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
1785 : {
1786 : int16_t pos_idx;
1787 0 : for ( pos_idx = 0; pos_idx < st_ivas->hBinRenderer->numPoses; pos_idx++ )
1788 : {
1789 0 : for ( slot_idx = 0; slot_idx < hParamMC->subframe_nbslots[subframe_idx]; slot_idx++ )
1790 : {
1791 0 : for ( ch = 0; ch < nchan_out_cldfb; ch++ )
1792 : {
1793 : #ifdef FIX_1119_SPLIT_RENDERING_VOIP
1794 0 : ivas_CLDFB_RINGBUF_Push(
1795 0 : st_ivas->hSplitBinRend->hMultiBinCldfbData[pos_idx * BINAURAL_CHANNELS + ch],
1796 0 : Cldfb_RealBuffer_Binaural[pos_idx][ch][slot_idx],
1797 0 : Cldfb_ImagBuffer_Binaural[pos_idx][ch][slot_idx],
1798 0 : hParamMC->num_freq_bands );
1799 : #else
1800 : mvr2r( Cldfb_RealBuffer_Binaural[pos_idx][ch][slot_idx], st_ivas->hSplitBinRend->hMultiBinCldfbData->Cldfb_RealBuffer_Binaural[( pos_idx * BINAURAL_CHANNELS ) + ch][slot_idx_start + slot_idx], hParamMC->num_freq_bands );
1801 : mvr2r( Cldfb_ImagBuffer_Binaural[pos_idx][ch][slot_idx], st_ivas->hSplitBinRend->hMultiBinCldfbData->Cldfb_ImagBuffer_Binaural[( pos_idx * BINAURAL_CHANNELS ) + ch][slot_idx_start + slot_idx], hParamMC->num_freq_bands );
1802 : #endif
1803 : }
1804 : }
1805 : }
1806 : }
1807 :
1808 : /* update combined orientation access index */
1809 983670 : ivas_combined_orientation_update_index( st_ivas->hCombinedOrientationData, hParamMC->num_freq_bands * hParamMC->subframe_nbslots[subframe_idx] );
1810 : }
1811 368508 : else if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_CLDFB )
1812 : {
1813 : /* format conversion*/
1814 32580 : ivas_lssetupconversion_process_param_mc( st_ivas, hParamMC->subframe_nbslots[subframe_idx], Cldfb_RealBuffer, Cldfb_ImagBuffer, channel_active );
1815 : }
1816 :
1817 : /* CLDFB synthesis */
1818 6055086 : for ( ch = 0; ch < nchan_out_cldfb; ch++ )
1819 : {
1820 : float *RealBuffer[16];
1821 : float *ImagBuffer[16];
1822 :
1823 4702908 : if ( channel_active[ch] )
1824 : {
1825 : /* open CLDFB buffer up to CLDFB_NO_CHANNELS_MAX bands for 48kHz */
1826 22453680 : for ( i = 0; i < hParamMC->subframe_nbslots[subframe_idx]; i++ )
1827 : {
1828 17946252 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1829 : {
1830 7785900 : RealBuffer[i] = Cldfb_RealBuffer_Binaural[0][ch][i];
1831 7785900 : ImagBuffer[i] = Cldfb_ImagBuffer_Binaural[0][ch][i];
1832 : }
1833 : else
1834 : {
1835 10160352 : RealBuffer[i] = Cldfb_RealBuffer[ch][i];
1836 10160352 : ImagBuffer[i] = Cldfb_ImagBuffer[ch][i];
1837 : }
1838 : }
1839 :
1840 4507428 : cldfbSynthesis( RealBuffer, ImagBuffer, &( output_f[ch][slot_idx_start_cldfb_synth * hParamMC->num_freq_bands] ),
1841 4507428 : hParamMC->num_freq_bands * hParamMC->subframe_nbslots[subframe_idx], st_ivas->cldfbSynDec[ch] );
1842 : }
1843 : else
1844 : {
1845 195480 : set_f( &( output_f[ch][slot_idx_start_cldfb_synth * hParamMC->num_freq_bands] ), 0.0f, hParamMC->num_freq_bands * hParamMC->subframe_nbslots[subframe_idx] );
1846 : }
1847 : }
1848 1352178 : slot_idx_start += hParamMC->subframe_nbslots[subframe_idx];
1849 1352178 : slot_idx_start_cldfb_synth += hParamMC->subframe_nbslots[subframe_idx];
1850 : }
1851 :
1852 759717 : if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
1853 : {
1854 24745 : ivas_mc2sba( st_ivas->hIntSetup, output_f, output_f, hParamMC->num_freq_bands * slots_to_render, st_ivas->hOutSetup.ambisonics_order, 0.f );
1855 : }
1856 :
1857 : /* update */
1858 759717 : if ( hParamMC->slots_rendered == hParamMC->num_slots )
1859 : {
1860 335436 : hParamMC->hMetadataPMC->last_coded_bwidth = hParamMC->hMetadataPMC->coded_bwidth;
1861 335436 : param_mc_update_mixing_matrices( hParamMC, hParamMC->h_output_synthesis_cov_state.mixing_matrix, hParamMC->h_output_synthesis_cov_state.mixing_matrix_res, nchan_transport, nchan_out_cov );
1862 : }
1863 759717 : hParamMC->subframes_rendered = last_sf;
1864 759717 : *nSamplesAvailableNext = ( hParamMC->num_slots - hParamMC->slots_rendered ) * NS2SA( output_Fs, CLDFB_SLOT_NS );
1865 759717 : pop_wmops();
1866 :
1867 759717 : return;
1868 : }
1869 :
1870 :
1871 : /*-------------------------------------------------------------------------
1872 : * param_mc_dec_init()
1873 : *
1874 : * Parametric MC decoding initialization
1875 : *------------------------------------------------------------------------*/
1876 :
1877 10560 : static void ivas_param_mc_dec_init(
1878 : PARAM_MC_DEC_HANDLE hParamMC, /* i/o: decoder DirAC handle */
1879 : const int16_t nchan_transport, /* i : number of input (transport) channels */
1880 : const int16_t nchan_cov ) /* i : number of cov synthesis channels */
1881 : {
1882 : int16_t k;
1883 : uint16_t max_param_band_residual;
1884 : int16_t len;
1885 :
1886 : /*-----------------------------------------------------------------*
1887 : * init sub-modules
1888 : *-----------------------------------------------------------------*/
1889 :
1890 : /* decorrelation */
1891 10560 : if ( hParamMC->max_band_decorr > 0 )
1892 : {
1893 10314 : len = hParamMC->diff_proto_info->num_protos_diff * hParamMC->h_freq_domain_decorr_ap_params->h_onset_detection_power_params.max_band_decorr;
1894 :
1895 : /* init onsetDetectionPower */
1896 10314 : set_zero( hParamMC->h_freq_domain_decorr_ap_state->h_onset_detection_power_state.onset_detector_1, len );
1897 10314 : set_zero( hParamMC->h_freq_domain_decorr_ap_state->h_onset_detection_power_state.onset_detector_2, len );
1898 : }
1899 :
1900 10560 : max_param_band_residual = 0;
1901 :
1902 : /* output synthesis */
1903 43545 : for ( k = hParamMC->hMetadataPMC->num_parameter_bands; k >= 0; k-- )
1904 : {
1905 43545 : if ( hParamMC->band_grouping[k] <= hParamMC->max_band_decorr )
1906 : {
1907 10560 : max_param_band_residual = k;
1908 : #ifdef DEBUGGING
1909 : assert( hParamMC->band_grouping[k] == hParamMC->max_band_decorr );
1910 : #endif
1911 10560 : break;
1912 : }
1913 : }
1914 :
1915 10560 : ivas_dirac_dec_output_synthesis_cov_init( &( hParamMC->h_output_synthesis_cov_state ), nchan_transport, nchan_cov, hParamMC->hMetadataPMC->num_parameter_bands, max_param_band_residual );
1916 :
1917 : /*-----------------------------------------------------------------*
1918 : * init proto frames
1919 : *-----------------------------------------------------------------*/
1920 :
1921 10560 : if ( hParamMC->max_band_decorr > 0 )
1922 : {
1923 10314 : set_zero( hParamMC->proto_frame_f, 2 * hParamMC->diff_proto_info->num_protos_diff * hParamMC->num_freq_bands );
1924 10314 : set_zero( hParamMC->proto_frame_dec_f, 2 * nchan_cov * hParamMC->num_freq_bands );
1925 : }
1926 :
1927 10560 : return;
1928 : }
1929 :
1930 :
1931 : /*-------------------------------------------------------------------------
1932 : * Local functions
1933 : *-------------------------------------------------------------------------*/
1934 :
1935 :
1936 : /*-------------------------------------------------------------------------
1937 : * ivas_param_mc_dec_compute_diffuse_proto()
1938 : *
1939 : * Compute prototypes for decorrelation
1940 : *------------------------------------------------------------------------*/
1941 :
1942 5366982 : static void param_mc_protoSignalComputation(
1943 : float *RealBuffer, /* i : CLDFB samples of the transport channels (real part) */
1944 : float *ImagBuffer, /* i : CLDFB samples of the transport channels (imaginary part) */
1945 : float *proto_frame_f, /* o : interleaved complex prototype CLDFB samples */
1946 : const PARAM_MC_DIFF_PROTO_INFO *diff_proto_info, /* i : prototype generation information */
1947 : const int16_t num_freq_bands /* i : number of frequency bands for the prototypes */
1948 : )
1949 : {
1950 : int16_t band;
1951 : int16_t proto_ch_idx, source_ch_cnt;
1952 : float *p_proto_frame;
1953 : float *p_real_buffer;
1954 : float *p_imag_buffer;
1955 :
1956 5366982 : set_zero( proto_frame_f, 2 * num_freq_bands * diff_proto_info->num_protos_diff );
1957 :
1958 22233624 : for ( proto_ch_idx = 0; proto_ch_idx < diff_proto_info->num_protos_diff; proto_ch_idx++ )
1959 : {
1960 16866642 : int16_t num_source_ch = diff_proto_info->num_source_chan_diff[proto_ch_idx];
1961 :
1962 38694435 : for ( source_ch_cnt = 0; source_ch_cnt < num_source_ch; source_ch_cnt++ )
1963 : {
1964 21827793 : float fac = diff_proto_info->proto_fac[proto_ch_idx][source_ch_cnt];
1965 21827793 : int16_t source_ch_idx = diff_proto_info->source_chan_idx[proto_ch_idx][source_ch_cnt];
1966 :
1967 21827793 : p_proto_frame = &proto_frame_f[proto_ch_idx * num_freq_bands * 2];
1968 21827793 : p_real_buffer = &RealBuffer[source_ch_idx * num_freq_bands];
1969 21827793 : p_imag_buffer = &ImagBuffer[source_ch_idx * num_freq_bands];
1970 :
1971 1228886493 : for ( band = 0; band < num_freq_bands; band++ )
1972 : {
1973 1207058700 : *( p_proto_frame++ ) += fac * ( *( p_real_buffer++ ) );
1974 1207058700 : *( p_proto_frame++ ) += fac * ( *( p_imag_buffer++ ) );
1975 : }
1976 : }
1977 : }
1978 :
1979 5366982 : return;
1980 : }
1981 :
1982 :
1983 : /*-------------------------------------------------------------------------
1984 : * ivas_param_mc_dec_compute_diffuse_proto()
1985 : *
1986 : * Transfer decorrelated signals back from the decorrelator buffer to
1987 : * the buffers used in the final synthesis
1988 : *------------------------------------------------------------------------*/
1989 :
1990 5366982 : static void ivas_param_mc_dec_copy_diffuse_proto(
1991 : PARAM_MC_DEC_HANDLE hParamMC, /* i : Parametric MC handle */
1992 : float Cldfb_buffer_real[MAX_LS_CHANNELS][PARAM_MC_MAX_NSLOTS_IN_SUBFRAME][CLDFB_NO_CHANNELS_MAX], /* o : CLDFB buffer used in the final synthesis (real part) */
1993 : float Cldfb_buffer_imag[MAX_LS_CHANNELS][PARAM_MC_MAX_NSLOTS_IN_SUBFRAME][CLDFB_NO_CHANNELS_MAX], /* o : CLDFB buffer used in the final synthesis (imaginary part) */
1994 : const int16_t nY, /* i : number of decorrelated channels */
1995 : const int16_t slot_idx /* i : current time slot index */
1996 : )
1997 : {
1998 : int16_t k, l;
1999 : int16_t num_freq_bands, num_freq_bands_diff;
2000 : float *p_proto_diff;
2001 :
2002 5366982 : num_freq_bands = hParamMC->num_freq_bands;
2003 5366982 : num_freq_bands_diff = hParamMC->h_output_synthesis_params.max_band_decorr;
2004 :
2005 40790514 : for ( k = 0; k < nY; k++ )
2006 : {
2007 35423532 : p_proto_diff = hParamMC->proto_frame_dec_f + k * 2 * num_freq_bands;
2008 743894172 : for ( l = 0; l < num_freq_bands_diff; l++ )
2009 : {
2010 708470640 : Cldfb_buffer_real[k][slot_idx][l] = *( p_proto_diff++ );
2011 708470640 : Cldfb_buffer_imag[k][slot_idx][l] = *( p_proto_diff++ );
2012 : }
2013 : }
2014 :
2015 5366982 : return;
2016 : }
2017 :
2018 :
2019 : /*-------------------------------------------------------------------------
2020 : * ivas_param_mc_bin2dec()
2021 : *
2022 : * decode a number of bits to an integer
2023 : *------------------------------------------------------------------------*/
2024 : /* r : decoded integer */
2025 1317174 : static int16_t ivas_param_mc_bin2dec(
2026 : uint16_t bits[PARAM_MC_MAX_BITS], /* i : bit buffer */
2027 : const int16_t N /* i : number of bits to decode */
2028 : )
2029 : {
2030 : int16_t i;
2031 : int16_t out;
2032 :
2033 1317174 : assert( N <= 16 );
2034 1317174 : out = 0;
2035 5268804 : for ( i = 0; i < N; i++ )
2036 : {
2037 3951630 : out += ( bits[i] << ( N - 1 - i ) );
2038 : }
2039 :
2040 1317174 : return out;
2041 : }
2042 :
2043 :
2044 : /*-------------------------------------------------------------------------
2045 : * ivas_param_mc_uniform_decoder()
2046 : *
2047 : * decode a uniformily coded sequence of float values
2048 : *------------------------------------------------------------------------*/
2049 :
2050 41478 : static int16_t ivas_param_mc_uniform_decoder(
2051 : float *seq, /* o : decoded sequence of float values */
2052 : const int16_t sz_seq, /* i : number of values to decode */
2053 : const float *alphabet, /* i : codebook */
2054 : const int16_t N, /* i : number of bits per coded index */
2055 : uint16_t bit_buffer[PARAM_MC_MAX_BITS] /* i : bit buffer to decode */
2056 : )
2057 : {
2058 : int16_t i;
2059 : int16_t idx;
2060 : int16_t n_bits;
2061 :
2062 41478 : n_bits = 0;
2063 41478 : assert( N * sz_seq < PARAM_MC_MAX_BITS );
2064 :
2065 1358652 : for ( i = 0; i < sz_seq; ++i )
2066 : {
2067 1317174 : idx = ivas_param_mc_bin2dec( &bit_buffer[i * N], N );
2068 1317174 : seq[i] = alphabet[idx];
2069 : }
2070 :
2071 41478 : n_bits = N * sz_seq;
2072 :
2073 41478 : return n_bits;
2074 : }
2075 :
2076 :
2077 : /*-------------------------------------------------------------------------
2078 : * ivas_param_mc_range_decoder_LC()
2079 : *
2080 : * decode a sequency of inidices coded with a range coder
2081 : *------------------------------------------------------------------------*/
2082 :
2083 629460 : static int16_t ivas_param_mc_range_decoder_LC(
2084 : uint16_t *bit_buffer, /* i : bit buffer to read from */
2085 : int16_t *x, /* o : decoded indices */
2086 : int16_t *BER_detect, /* o : flag for indicating a bit error */
2087 : const int16_t sz_seq, /* i : size of the sequence to be decoded */
2088 : const int16_t sz_alphabet, /* i : size of the alphabet */
2089 : const uint16_t *cft, /* i : cumulative frequency table */
2090 : const uint16_t *sft, /* i : symbol frequency table */
2091 : const int16_t tot_shift, /* i : total frequency as a power of 2 */
2092 : const int16_t nbbits /* i : maximum bit budget */
2093 : )
2094 : {
2095 : RangeUniDecState rc_st_dec; /* State of the range decoder */
2096 : int16_t cur_bit_pos;
2097 : int16_t k;
2098 : int16_t r;
2099 :
2100 : /* Start Decoding */
2101 : /* Initialize range decoder */
2102 629460 : cur_bit_pos = 0;
2103 629460 : rc_uni_dec_init( &rc_st_dec, bit_buffer, nbbits - 32 ); /* (nbbits + 30) entries are read by the decoder */
2104 :
2105 : /* Main Loop through the indices */
2106 23689905 : for ( k = 0; k < sz_seq; k++ )
2107 : {
2108 23060445 : r = rc_uni_dec_read_symbol_fastS( &rc_st_dec, cft, sft, sz_alphabet, tot_shift ); /*Alphabet size = 17 (2^4 = 16 MSB symbols + 1 ESC symbol) */
2109 : /* r is the symbol read, the possible values are {0,1,....alphabet_size - 1} */
2110 :
2111 : /* Update bitstream pointer */
2112 23060445 : cur_bit_pos = rc_uni_dec_virtual_finish( &rc_st_dec );
2113 :
2114 : /* Confirm that there is no overflow */
2115 23060445 : if ( cur_bit_pos > nbbits )
2116 : {
2117 0 : *BER_detect |= 1;
2118 : }
2119 :
2120 23060445 : x[k] = r;
2121 : }
2122 :
2123 : /* We don't need to finish because virtual_finish() already does the same */
2124 : /*st->next_bit_pos = rc_uni_dec_finish(&rc_st_dec);*/
2125 :
2126 : /* Check for bitstream errors */
2127 629460 : if ( rc_st_dec.bit_error_detected != 0 )
2128 : {
2129 0 : *BER_detect |= 1;
2130 : }
2131 :
2132 629460 : return cur_bit_pos;
2133 : }
2134 :
2135 :
2136 : /*-------------------------------------------------------------------------
2137 : * param_mc_compute_interpolator()
2138 : *
2139 : * compute the interpolator used in the final synthesis
2140 : *------------------------------------------------------------------------*/
2141 :
2142 348339 : static void ivas_param_mc_dec_compute_interpolator(
2143 : const uint16_t bAttackPresent, /* i : flag indicating if we have a transient in the current frame */
2144 : const uint16_t attackPos, /* i : position of the transient */
2145 : const uint16_t interp_length, /* i : number of interpolation values to be calculated */
2146 : float *interpolator /* o : interpolator */
2147 : )
2148 : {
2149 : int16_t idx;
2150 :
2151 348339 : if ( bAttackPresent )
2152 : {
2153 125886 : for ( idx = 0; idx < 2 * attackPos; idx++ )
2154 : {
2155 109512 : interpolator[idx] = 0.0f;
2156 : }
2157 168846 : for ( ; idx < interp_length; idx++ )
2158 : {
2159 152472 : interpolator[idx] = 1.0f;
2160 : }
2161 : }
2162 : else
2163 : {
2164 331965 : ivas_jbm_dec_get_adapted_linear_interpolator( DEFAULT_JBM_CLDFB_TIMESLOTS, interp_length, interpolator );
2165 : }
2166 :
2167 348339 : return;
2168 : }
2169 :
2170 :
2171 : /*-------------------------------------------------------------------------
2172 : * remove_lfe_from_cy()
2173 : *
2174 : * remove all LFE related values from a covariance matrix
2175 : *------------------------------------------------------------------------*/
2176 :
2177 4086777 : static void remove_lfe_from_cy(
2178 : const int16_t nY, /* i : dimension of the covariance matrix */
2179 : int16_t lfe_indices[PARAM_MC_LOCAL_SZ_LFE_MAP], /* i : LFE index */
2180 : int16_t num_lfe, /* i : number of LFEs */
2181 : float *cy, /* i : covariance matrix */
2182 : float *cy_woLFE ) /* o : covariance matrix with LFE removed */
2183 : {
2184 : int16_t ch_idx1, ch_idx2;
2185 : int16_t lfe_idx1, lfe_idx2;
2186 : float *ptrCy;
2187 : float *ptrCy_out;
2188 :
2189 4086777 : ptrCy = cy;
2190 4086777 : ptrCy_out = cy_woLFE;
2191 :
2192 12260331 : for ( lfe_idx1 = 0; lfe_idx1 < num_lfe + 1; lfe_idx1++ )
2193 : {
2194 31175163 : for ( ch_idx1 = lfe_indices[lfe_idx1] + 1; ch_idx1 < lfe_indices[lfe_idx1 + 1]; ch_idx1++ )
2195 : {
2196 69004827 : for ( lfe_idx2 = 0; lfe_idx2 < num_lfe + 1; lfe_idx2++ )
2197 : {
2198 182516691 : for ( ch_idx2 = lfe_indices[lfe_idx2] + 1; ch_idx2 < lfe_indices[lfe_idx2 + 1]; ch_idx2++ )
2199 : {
2200 136513473 : *( ptrCy_out++ ) = *( ptrCy++ );
2201 : }
2202 46003218 : ptrCy++;
2203 : }
2204 23001609 : ptrCy--;
2205 : }
2206 8173554 : ptrCy += nY;
2207 : }
2208 :
2209 4086777 : return;
2210 : }
2211 :
2212 :
2213 : /*-------------------------------------------------------------------------
2214 : * ivas_param_mc_get_mixing_matrices()
2215 : *
2216 : * calculate the direct and residual mixing matrices
2217 : * using the covariance method
2218 : *------------------------------------------------------------------------*/
2219 :
2220 4419087 : static void ivas_param_mc_get_mixing_matrices(
2221 : PARAM_MC_DEC_HANDLE hParamMC, /* i : Parametric MC handle */
2222 : IVAS_OUTPUT_SETUP *hSynthesisOutputSetup,
2223 : float Cx_in[PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS], /* i : input covariance for all parameter bands */
2224 : const int16_t param_band_idx, /* i : parameter band index */
2225 : float *mixing_matrix[], /* o : direct mixing matrices for all parameter bands */
2226 : float *mixing_matrix_res[], /* o : residual mixing matrices for all parameter bands */
2227 : const int16_t nY_intern, /* i : number of channels in the transported format */
2228 : const PARAM_MC_SYNTHESIS_CONF synth_config, /* i : Parametric MC synthesis config */
2229 : const int16_t nX, /* i : number of transport channels */
2230 : const int16_t nY_cov /* i : number of covariance synthesis output channels */
2231 : )
2232 : {
2233 : float Cx[PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS];
2234 : float Cy[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2235 : float Cy_diag[MAX_LS_CHANNELS];
2236 : float Cr[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2237 : float Cproto_diag[MAX_LS_CHANNELS];
2238 : float Cproto[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2239 : float mat_mult_buffer1[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2240 : float *Cx_state;
2241 : float *Cx_old_state;
2242 : float Cy_state[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2243 : float *Cy_old_state;
2244 : int16_t nY_band;
2245 : float proto_matrix_noLFE[PARAM_MC_MAX_TRANSPORT_CHANS * MAX_LS_CHANNELS];
2246 : float *proto_matrix_ptr;
2247 : int16_t num_lfe_bands;
2248 : int16_t brange[2];
2249 : uint16_t i;
2250 : int16_t ch_idx1, ch_idx2, lfe_idx1, lfe_idx2;
2251 : float *ptrMM;
2252 : float *ptrMM_out;
2253 : float Cy_full[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2254 : float mixing_matrix_local[MAX_LS_CHANNELS * PARAM_MC_MAX_TRANSPORT_CHANS];
2255 : float mixing_matrix_res_local[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2256 : int16_t remove_lfe;
2257 : int16_t lfe_indices[PARAM_MC_LOCAL_SZ_LFE_MAP];
2258 :
2259 4419087 : set_zero( Cproto, MAX_LS_CHANNELS * MAX_LS_CHANNELS );
2260 4419087 : set_zero( mat_mult_buffer1, MAX_LS_CHANNELS * MAX_LS_CHANNELS );
2261 4419087 : set_zero( proto_matrix_noLFE, PARAM_MC_MAX_TRANSPORT_CHANS * MAX_LS_CHANNELS );
2262 :
2263 4419087 : nY_band = nY_cov;
2264 4419087 : num_lfe_bands = 0;
2265 4419087 : remove_lfe = 0;
2266 :
2267 4419087 : set_s( lfe_indices, -1, PARAM_MC_LOCAL_SZ_LFE_MAP );
2268 4419087 : if ( hSynthesisOutputSetup->num_lfe )
2269 : {
2270 : float *proto_matrix_ptr_in;
2271 : #ifdef DEBUGGING
2272 : assert( ( nY_cov == ( hSynthesisOutputSetup->nchan_out_woLFE + hSynthesisOutputSetup->num_lfe ) ) && "Number of channels do not match!" );
2273 : #endif
2274 8838174 : for ( lfe_idx1 = 0; lfe_idx1 < hSynthesisOutputSetup->num_lfe; lfe_idx1++ )
2275 : {
2276 4419087 : lfe_indices[lfe_idx1 + 1] = hSynthesisOutputSetup->index_lfe[lfe_idx1];
2277 : }
2278 4419087 : lfe_indices[hSynthesisOutputSetup->num_lfe + 1] = nY_cov;
2279 4419087 : proto_matrix_ptr = &proto_matrix_noLFE[0];
2280 4419087 : proto_matrix_ptr_in = &hParamMC->h_output_synthesis_params.proto_matrix[0];
2281 4419087 : set_zero( proto_matrix_noLFE, PARAM_MC_MAX_TRANSPORT_CHANS * MAX_LS_CHANNELS );
2282 :
2283 13655067 : for ( ch_idx1 = 0; ch_idx1 < nX; ch_idx1++ )
2284 : {
2285 27707940 : for ( lfe_idx1 = 0; lfe_idx1 < hSynthesisOutputSetup->num_lfe + 1; lfe_idx1++ )
2286 : {
2287 71652624 : for ( ch_idx2 = lfe_indices[lfe_idx1] + 1; ch_idx2 < lfe_indices[lfe_idx1 + 1]; ch_idx2++ )
2288 : {
2289 53180664 : *( proto_matrix_ptr++ ) = *( proto_matrix_ptr_in++ );
2290 : }
2291 18471960 : proto_matrix_ptr_in++;
2292 : }
2293 9235980 : proto_matrix_ptr_in--;
2294 : }
2295 : }
2296 :
2297 4419087 : if ( hParamMC->hMetadataPMC->lfe_on )
2298 : {
2299 4376832 : num_lfe_bands = PARAM_MC_MAX_BAND_LFE;
2300 : }
2301 :
2302 4419087 : if ( hSynthesisOutputSetup->num_lfe > 0 && param_band_idx >= num_lfe_bands )
2303 : {
2304 4086777 : remove_lfe = 1;
2305 4086777 : nY_band = nY_cov - hSynthesisOutputSetup->num_lfe;
2306 4086777 : proto_matrix_ptr = proto_matrix_noLFE;
2307 : }
2308 : else
2309 : {
2310 332310 : proto_matrix_ptr = hParamMC->h_output_synthesis_params.proto_matrix;
2311 : }
2312 :
2313 4419087 : brange[0] = hParamMC->band_grouping[param_band_idx];
2314 4419087 : brange[1] = hParamMC->band_grouping[param_band_idx + 1];
2315 :
2316 4419087 : Cx_state = Cx_in;
2317 4419087 : Cx_old_state = hParamMC->h_output_synthesis_cov_state.cx_old[param_band_idx];
2318 4419087 : Cy_old_state = hParamMC->h_output_synthesis_cov_state.cy_old[param_band_idx];
2319 :
2320 : /* Getting mixing mtx */
2321 : /* estimate target cov from input cov and proto_matrix */
2322 4419087 : matrix_product( hParamMC->proto_matrix_int, nY_intern, nX, 0, Cx_state, nX, nX, 0, mat_mult_buffer1 );
2323 :
2324 4419087 : matrix_product( mat_mult_buffer1, nY_intern, nX, 0, hParamMC->proto_matrix_int, nY_intern, nX, 1, Cproto );
2325 :
2326 34370361 : for ( ch_idx1 = 0; ch_idx1 < nY_intern; ch_idx1++ )
2327 : {
2328 29951274 : if ( Cproto[ch_idx1 + ch_idx1 * nY_intern] < 0.0f )
2329 : {
2330 0 : Cproto[ch_idx1 + ch_idx1 * nY_intern] = 0.0f;
2331 : }
2332 : }
2333 :
2334 4419087 : ivas_param_mc_dequantize_cov( hParamMC,
2335 4419087 : hParamMC->icld_q + param_band_idx * hParamMC->hMetadataPMC->ild_mapping_conf->ild_map_size_lfe,
2336 4419087 : hParamMC->icc_q + param_band_idx * hParamMC->hMetadataPMC->icc_mapping_conf->icc_map_size_lfe,
2337 : param_band_idx, nY_cov,
2338 : synth_config,
2339 : nY_intern,
2340 : nX, Cx_state, Cproto, Cy_state );
2341 :
2342 : /* Smoothing: Sum over two buffers */
2343 4419087 : if ( hParamMC->hMetadataPMC->bAttackPresent )
2344 : {
2345 : /* no smoothing on attacks */
2346 210936 : mvr2r( Cx_state, Cx, nX * nX );
2347 210936 : mvr2r( Cy_state, Cy_full, nY_cov * nY_cov );
2348 : }
2349 : else
2350 : {
2351 : /* smoothing gains are now identical to one, simply add up */
2352 4208151 : v_add( Cx_state, Cx_old_state, Cx, nX * nX );
2353 4208151 : v_add( Cy_state, Cy_old_state, Cy_full, nY_cov * nY_cov );
2354 : }
2355 :
2356 : /* cov buffer update */
2357 4419087 : mvr2r( Cx_state, Cx_old_state, nX * nX );
2358 4419087 : mvr2r( Cy_state, Cy_old_state, nY_cov * nY_cov );
2359 :
2360 : /* remove LFE if necessary */
2361 4419087 : if ( remove_lfe )
2362 : {
2363 4086777 : remove_lfe_from_cy( nY_cov, lfe_indices, hSynthesisOutputSetup->num_lfe, Cy_full, Cy );
2364 : }
2365 : else
2366 : {
2367 332310 : mvr2r( Cy_full, Cy, nY_band * nY_band );
2368 : }
2369 :
2370 4419087 : matrix_product( proto_matrix_ptr, nY_band, nX, 0, Cx, nX, nX, 0, mat_mult_buffer1 );
2371 :
2372 4419087 : matrix_product_diag( mat_mult_buffer1, nY_band, nX, 0, proto_matrix_ptr, nY_band, nX, 1, Cproto_diag );
2373 :
2374 : /* make sure we have no negative entries in Cproto_diag due to rounding errors */
2375 29613600 : for ( ch_idx1 = 0; ch_idx1 < nY_band; ch_idx1++ )
2376 : {
2377 25194513 : if ( Cproto_diag[ch_idx1] < 0.0f )
2378 : {
2379 0 : Cproto_diag[ch_idx1] = 0.0f;
2380 : }
2381 : }
2382 :
2383 : /* Computing the mixing matrices */
2384 :
2385 : /* bands with decorr */
2386 4419087 : if ( brange[0] < hParamMC->h_output_synthesis_params.max_band_decorr )
2387 : {
2388 3561699 : computeMixingMatrices( nX, nY_band, Cx, Cy, proto_matrix_ptr, 0, PARAM_MC_REG_SX, PARAM_MC_REG_GHAT, mixing_matrix_local, Cr );
2389 :
2390 : /* Compute mixing matrix for residual */
2391 3561699 : computeMixingMatricesResidual( nY_band, Cproto_diag, Cr, PARAM_MC_REG_SX, PARAM_MC_REG_GHAT, mixing_matrix_res_local );
2392 :
2393 3561699 : if ( remove_lfe )
2394 : {
2395 3229389 : set_zero( mixing_matrix_res[param_band_idx], nY_cov * nY_cov );
2396 :
2397 3229389 : ptrMM = mixing_matrix_res_local;
2398 3229389 : ptrMM_out = mixing_matrix_res[param_band_idx];
2399 9688167 : for ( lfe_idx1 = 0; lfe_idx1 < hSynthesisOutputSetup->num_lfe + 1; lfe_idx1++ )
2400 : {
2401 24654891 : for ( ch_idx1 = lfe_indices[lfe_idx1] + 1; ch_idx1 < lfe_indices[lfe_idx1 + 1]; ch_idx1++ )
2402 : {
2403 54588339 : for ( lfe_idx2 = 0; lfe_idx2 < hSynthesisOutputSetup->num_lfe + 1; lfe_idx2++ )
2404 : {
2405 144444855 : for ( ch_idx2 = lfe_indices[lfe_idx2] + 1; ch_idx2 < lfe_indices[lfe_idx2 + 1]; ch_idx2++ )
2406 : {
2407 108052629 : *( ptrMM_out++ ) = *( ptrMM++ );
2408 : }
2409 36392226 : ptrMM_out++;
2410 : }
2411 18196113 : ptrMM_out--;
2412 : }
2413 6458778 : ptrMM_out += nY_cov;
2414 : }
2415 : }
2416 : else
2417 : {
2418 332310 : mvr2r( mixing_matrix_res_local, mixing_matrix_res[param_band_idx], nY_cov * nY_cov );
2419 : }
2420 : }
2421 857388 : else if ( brange[0] < hParamMC->max_band_energy_compensation )
2422 : {
2423 : /* Compute mixing matrices (energy compensation only) */
2424 857388 : computeMixingMatrices( nX, nY_band, Cx, Cy, proto_matrix_ptr, 1, PARAM_MC_REG_SX, PARAM_MC_REG_GHAT, mixing_matrix_local, Cr );
2425 : }
2426 : else
2427 : {
2428 : /*if neither decorrelation nor energy compensation is applied*/
2429 0 : for ( i = 0; i < nY_band; i++ )
2430 : {
2431 0 : Cy_diag[i] = Cy[i + nY_band * i];
2432 0 : Cy_diag[i] = sqrtf( Cy_diag[i] / ( Cproto_diag[i] + EPSILON ) );
2433 : }
2434 :
2435 0 : diag_matrix_product( Cy_diag, nY_band, proto_matrix_ptr, nY_band, nX, 0, mixing_matrix_local );
2436 : }
2437 4419087 : if ( remove_lfe )
2438 : {
2439 4086777 : set_zero( mixing_matrix[param_band_idx], nX * nY_cov );
2440 :
2441 4086777 : ptrMM = mixing_matrix_local;
2442 4086777 : ptrMM_out = mixing_matrix[param_band_idx];
2443 12633216 : for ( ch_idx1 = 0; ch_idx1 < nX; ch_idx1++ )
2444 : {
2445 25639317 : for ( lfe_idx1 = 0; lfe_idx1 < hSynthesisOutputSetup->num_lfe + 1; lfe_idx1++ )
2446 : {
2447 66331335 : for ( ch_idx2 = lfe_indices[lfe_idx1] + 1; ch_idx2 < lfe_indices[lfe_idx1 + 1]; ch_idx2++ )
2448 : {
2449 49238457 : *( ptrMM_out++ ) = *( ptrMM++ );
2450 : }
2451 17092878 : ptrMM_out++;
2452 : }
2453 8546439 : ptrMM_out--;
2454 : }
2455 : }
2456 : else
2457 : {
2458 332310 : mvr2r( mixing_matrix_local, mixing_matrix[param_band_idx], nY_cov * nX );
2459 : }
2460 :
2461 4419087 : return;
2462 : }
2463 :
2464 :
2465 : /*-------------------------------------------------------------------------
2466 : * param_mc_update_mixing_matrices()
2467 : *
2468 : * update mixing matrix buffers
2469 : *------------------------------------------------------------------------*/
2470 :
2471 335436 : static void param_mc_update_mixing_matrices(
2472 : PARAM_MC_DEC_HANDLE hParamMC, /* i/o: Parametric MC handle */
2473 : float *mixing_matrix[], /* i : direct mixing matrices for the frame just processed */
2474 : float *mixing_matrix_res[], /* i : residual mixing matrices for the frame just processed */
2475 : const uint16_t nX, /* i : number of transport channels */
2476 : const uint16_t nY ) /* i : number of synthesis channels */
2477 : {
2478 : uint16_t param_band_idx;
2479 :
2480 4871934 : for ( param_band_idx = 0; param_band_idx < hParamMC->hMetadataPMC->nbands_coded; param_band_idx++ )
2481 : {
2482 : int16_t brange[2];
2483 :
2484 4536498 : brange[0] = hParamMC->band_grouping[param_band_idx];
2485 4536498 : brange[1] = hParamMC->band_grouping[param_band_idx + 1];
2486 :
2487 4536498 : mvr2r( mixing_matrix[param_band_idx], hParamMC->h_output_synthesis_cov_state.mixing_matrix_old[param_band_idx], nX * nY );
2488 :
2489 4536498 : if ( brange[0] < hParamMC->h_output_synthesis_params.max_band_decorr )
2490 : {
2491 3561699 : mvr2r( mixing_matrix_res[param_band_idx], hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_old[param_band_idx], nY * nY );
2492 : }
2493 : }
2494 :
2495 335436 : return;
2496 : }
2497 :
2498 :
2499 : /*-------------------------------------------------------------------------
2500 : * ivas_param_mc_dequantize_cov()
2501 : *
2502 : * generate the target covariance matrix
2503 : *------------------------------------------------------------------------*/
2504 :
2505 4419087 : static void ivas_param_mc_dequantize_cov(
2506 : PARAM_MC_DEC_HANDLE hParamMC, /* i : Parametric MC handle */
2507 : float *ild_q, /* i : sequence of dequantized ILD values */
2508 : float *icc_q, /* i : sequence of dequantized ICC values */
2509 : const int16_t param_band_index, /* i : current parameter band */
2510 : const int16_t nY_cov, /* i : number of output channels in the covariance synthesis */
2511 : const PARAM_MC_SYNTHESIS_CONF synth_conf, /* i : Parametric MC synthesis configuration */
2512 : const int16_t nY_int, /* i : number of channels in the transported format */
2513 : const int16_t nX, /* i : number of transport channels */
2514 : float *Cx_state, /* i : transport channel covariance matrix */
2515 : float *Cproto, /* i : prototype matrix */
2516 : float *Cy_state /* o : target covariance matrix */
2517 : )
2518 : {
2519 : float Nrqq[MAX_OUTPUT_CHANNELS];
2520 : float a[MAX_OUTPUT_CHANNELS];
2521 : int16_t k;
2522 : int16_t l;
2523 : float *Cyp;
2524 : float ap;
2525 : const PARAM_MC_ILD_MAPPING *h_ild_mapping;
2526 : float Cy_state_int[MAX_OUTPUT_CHANNELS * MAX_OUTPUT_CHANNELS];
2527 :
2528 4419087 : set_zero( Nrqq, MAX_OUTPUT_CHANNELS );
2529 4419087 : h_ild_mapping = hParamMC->hMetadataPMC->ild_mapping_conf;
2530 :
2531 : /*get back Nrg*/
2532 34370361 : for ( k = 0; k < nY_int; k++ )
2533 : {
2534 29951274 : float ref_ener = 0.0f;
2535 : int16_t ref_channel_cnt;
2536 : int16_t ref_channel_idx;
2537 :
2538 67945110 : for ( ref_channel_cnt = 0; ref_channel_cnt < h_ild_mapping->num_ref_channels[k]; ref_channel_cnt++ )
2539 : {
2540 37993836 : ref_channel_idx = h_ild_mapping->ref_channel_idx[k][ref_channel_cnt];
2541 37993836 : ref_ener += Cx_state[ref_channel_idx + ref_channel_idx * nX];
2542 : }
2543 29951274 : Nrqq[h_ild_mapping->ild_index[k]] = powf( 10.0f, ild_q[k] / 10.0f ) * hParamMC->hMetadataPMC->ild_factors[k] * ref_ener;
2544 : }
2545 :
2546 : /* estimate ICCs from estimated Cproto */
2547 34370361 : for ( k = 0; k < nY_int; k++ )
2548 : {
2549 29951274 : a[k] = 1.f / ( sqrtf( Cproto[k + nY_int * k] ) + EPSILON );
2550 :
2551 29951274 : v_multc( Cproto + k * nY_int, a[k], Cy_state_int + k * nY_int, nY_int );
2552 : }
2553 :
2554 34370361 : for ( k = 0; k < nY_int; k++ )
2555 : {
2556 29951274 : Cyp = Cy_state_int + k;
2557 29951274 : ap = a[k];
2558 240948870 : for ( l = 0; l < nY_int; l++ )
2559 : {
2560 210997596 : ( *Cyp ) *= ap;
2561 210997596 : Cyp += nY_int;
2562 : }
2563 : }
2564 :
2565 : /* replace some estimated ICCs with transmitted values */
2566 29951274 : for ( k = 0; k < hParamMC->hMetadataPMC->icc_mapping_conf->icc_map_size_lfe; k++ )
2567 : {
2568 25532187 : Cy_state_int[hParamMC->hMetadataPMC->icc_mapping_conf->icc_mapping[k][0] + nY_int * hParamMC->hMetadataPMC->icc_mapping_conf->icc_mapping[k][1]] = icc_q[k];
2569 25532187 : Cy_state_int[hParamMC->hMetadataPMC->icc_mapping_conf->icc_mapping[k][1] + nY_int * hParamMC->hMetadataPMC->icc_mapping_conf->icc_mapping[k][0]] = icc_q[k];
2570 : }
2571 :
2572 4419087 : if ( param_band_index >= PARAM_MC_MAX_BAND_LFE || !hParamMC->hMetadataPMC->lfe_on )
2573 : {
2574 31797675 : for ( k = 0; k < nY_int; k++ )
2575 : {
2576 27710898 : Cy_state_int[k + 3 * nY_int] = 1.0f;
2577 27710898 : Cy_state_int[3 + k * nY_int] = 1.0f;
2578 : }
2579 4086777 : Nrqq[3] = 0.0f;
2580 : }
2581 :
2582 : /* Generate back Covariance Mtx */
2583 34370361 : for ( k = 0; k < nY_int; k++ )
2584 : {
2585 29951274 : a[k] = sqrtf( Nrqq[k] );
2586 29951274 : v_multc( Cy_state_int + k * nY_int, a[k], Cy_state_int + k * nY_int, nY_int );
2587 : }
2588 :
2589 34370361 : for ( k = 0; k < nY_int; k++ )
2590 : {
2591 29951274 : Cyp = Cy_state_int + k;
2592 29951274 : ap = a[k];
2593 :
2594 240948870 : for ( l = 0; l < nY_int; l++ )
2595 : {
2596 210997596 : ( *Cyp ) *= ap;
2597 210997596 : Cyp += nY_int;
2598 : }
2599 : }
2600 :
2601 4419087 : if ( synth_conf == PARAM_MC_SYNTH_LS_CONV_COV )
2602 : {
2603 : /* Cy = dmx*Cy*dmx' */
2604 : float mat_mult_buffer1[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2605 : float target_ch_ener[MAX_LS_CHANNELS];
2606 : float dmx_ch_ener[MAX_LS_CHANNELS];
2607 :
2608 334992 : set_zero( target_ch_ener, MAX_LS_CHANNELS );
2609 334992 : set_zero( dmx_ch_ener, MAX_LS_CHANNELS );
2610 :
2611 334992 : matrix_product( hParamMC->ls_conv_dmx_matrix, nY_cov, nY_int, 0,
2612 : Cy_state_int, nY_int, nY_int, 0,
2613 : mat_mult_buffer1 );
2614 :
2615 334992 : matrix_product( mat_mult_buffer1, nY_cov, nY_int, 0,
2616 334992 : hParamMC->ls_conv_dmx_matrix, nY_cov, nY_int, 1,
2617 : Cy_state );
2618 :
2619 2344944 : for ( k = 0; k < nY_cov; k++ )
2620 : {
2621 18089568 : for ( l = 0; l < nY_int; l++ )
2622 : {
2623 16079616 : target_ch_ener[k] += hParamMC->ls_conv_dmx_matrix[k + l * nY_cov] * Nrqq[l];
2624 : }
2625 :
2626 2009952 : dmx_ch_ener[k] = Cy_state[k + nY_cov * k];
2627 :
2628 2009952 : if ( dmx_ch_ener[k] < 0.0f )
2629 : {
2630 0 : Cy_state[k + nY_cov * k] *= -1.0f;
2631 0 : dmx_ch_ener[k] *= -1.0f;
2632 : }
2633 :
2634 2009952 : target_ch_ener[k] = sqrtf( target_ch_ener[k] / ( dmx_ch_ener[k] + EPSILON ) );
2635 :
2636 2009952 : v_multc( Cy_state + k * nY_cov, target_ch_ener[k], Cy_state + k * nY_cov, nY_cov );
2637 :
2638 2009952 : Cyp = Cy_state + k;
2639 2009952 : ap = target_ch_ener[k];
2640 14069664 : for ( l = 0; l < nY_cov; l++ )
2641 : {
2642 12059712 : ( *Cyp ) *= ap;
2643 12059712 : Cyp += nY_cov;
2644 : }
2645 : }
2646 : }
2647 : else
2648 : {
2649 4084095 : mvr2r( Cy_state_int, Cy_state, nY_int * nY_int );
2650 : }
2651 :
2652 4419087 : return;
2653 : }
2654 :
2655 :
2656 : /*-------------------------------------------------------------------------*
2657 : * param_mc_set_num_synth_bands()
2658 : *
2659 : * set the number of frequency bands to be synthesized
2660 : *-------------------------------------------------------------------------*/
2661 :
2662 15069 : static void param_mc_set_num_synth_bands(
2663 : const int32_t output_Fs, /* i : output sampling frequency */
2664 : PARAM_MC_DEC_HANDLE hParamMC /* i/o: Parametric MC handle */
2665 : )
2666 : {
2667 : uint16_t max_param_band_synth;
2668 : const int16_t *param_mc_bands_coded;
2669 :
2670 15069 : switch ( hParamMC->hMetadataPMC->num_parameter_bands )
2671 : {
2672 1221 : case 20:
2673 1221 : param_mc_bands_coded = param_mc_bands_coded_20;
2674 1221 : break;
2675 6177 : case 10:
2676 6177 : param_mc_bands_coded = param_mc_bands_coded_10;
2677 6177 : break;
2678 7671 : case 14:
2679 : default:
2680 7671 : param_mc_bands_coded = param_mc_bands_coded_14;
2681 7671 : break;
2682 : }
2683 :
2684 15069 : switch ( output_Fs )
2685 : {
2686 0 : case 8000:
2687 0 : max_param_band_synth = param_mc_bands_coded[NB];
2688 0 : break;
2689 3063 : case 16000:
2690 3063 : max_param_band_synth = param_mc_bands_coded[WB];
2691 3063 : break;
2692 1326 : case 32000:
2693 1326 : max_param_band_synth = param_mc_bands_coded[SWB];
2694 1326 : break;
2695 10680 : case 48000:
2696 : default:
2697 10680 : max_param_band_synth = param_mc_bands_coded[FB];
2698 10680 : break;
2699 : }
2700 :
2701 15069 : hParamMC->num_param_bands_synth = min( hParamMC->hMetadataPMC->nbands_coded, max_param_band_synth );
2702 :
2703 15069 : return;
2704 : }
2705 :
2706 :
2707 : /*-------------------------------------------------------------------------*
2708 : * param_mc_get_diff_proto_info()
2709 : *
2710 : * calculated the diffuse prototype information
2711 : *-------------------------------------------------------------------------*/
2712 :
2713 10893 : static ivas_error param_mc_get_diff_proto_info(
2714 : const float *proto_mtx, /* i : protoype matrix for the synthesis */
2715 : const uint16_t nchan_transport, /* i : number of transport channels */
2716 : const uint16_t nchan_out_cov, /* i : number if output channels of the covariance synthesis */
2717 : PARAM_MC_DIFF_PROTO_INFO *p_diff_proto_info /* o : generated diffuse prototype info */
2718 : )
2719 : {
2720 : float proto_fac[MAX_LS_CHANNELS * PARAM_MC_MAX_TRANSPORT_CHANS];
2721 : uint16_t cur_out_ch;
2722 : uint16_t cur_diff_proto;
2723 : uint16_t cur_transport_ch;
2724 : uint16_t max_num_src_chan;
2725 :
2726 : /* Initializations */
2727 10893 : max_num_src_chan = 0;
2728 10893 : set_zero( proto_fac, MAX_LS_CHANNELS * PARAM_MC_MAX_TRANSPORT_CHANS );
2729 10893 : if ( ( p_diff_proto_info->proto_index_diff = (int16_t *) malloc( nchan_out_cov * sizeof( int16_t ) ) ) == NULL )
2730 : {
2731 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
2732 : }
2733 10893 : set_s( p_diff_proto_info->proto_index_diff, 0, nchan_out_cov );
2734 :
2735 10893 : if ( ( p_diff_proto_info->num_source_chan_diff = (int16_t *) malloc( nchan_out_cov * sizeof( int16_t ) ) ) == NULL )
2736 : {
2737 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
2738 : }
2739 10893 : set_s( p_diff_proto_info->num_source_chan_diff, 0, nchan_out_cov );
2740 :
2741 : /* we have at least one prototype, copy the first one */
2742 10893 : p_diff_proto_info->num_protos_diff = 1;
2743 10893 : mvr2r_inc( proto_mtx, nchan_out_cov, proto_fac, nchan_out_cov, nchan_transport );
2744 10893 : p_diff_proto_info->proto_index_diff[0] = 0;
2745 :
2746 : /* search for distinct prototypes */
2747 83196 : for ( cur_out_ch = 1; cur_out_ch < nchan_out_cov; cur_out_ch++ )
2748 : {
2749 72303 : uint16_t found = 0;
2750 :
2751 146598 : for ( cur_diff_proto = 0; cur_diff_proto < p_diff_proto_info->num_protos_diff; cur_diff_proto++ )
2752 : {
2753 124806 : float diff = 0;
2754 124806 : float *proto_fac_ptr = proto_fac + cur_diff_proto;
2755 124806 : const float *proto_mtx_ptr = proto_mtx + cur_out_ch;
2756 :
2757 422559 : for ( cur_transport_ch = 0; cur_transport_ch < nchan_transport; cur_transport_ch++ )
2758 : {
2759 297753 : diff += fabsf( *proto_fac_ptr - *proto_mtx_ptr );
2760 297753 : proto_fac_ptr += nchan_out_cov;
2761 297753 : proto_mtx_ptr += nchan_out_cov;
2762 : }
2763 :
2764 : /* we already have this prototype, save the index */
2765 124806 : if ( diff < 0.1f )
2766 : {
2767 50511 : found = 1;
2768 50511 : p_diff_proto_info->proto_index_diff[cur_out_ch] = cur_diff_proto;
2769 50511 : break;
2770 : }
2771 : }
2772 :
2773 : /* new distinct prototype, add it */
2774 72303 : if ( found == 0 )
2775 : {
2776 21792 : const float *proto_mtx_ptr = proto_mtx + cur_out_ch;
2777 : int16_t cur_num_src_chan;
2778 :
2779 21792 : cur_num_src_chan = 0;
2780 71526 : for ( cur_transport_ch = 0; cur_transport_ch < nchan_transport; cur_transport_ch++ )
2781 : {
2782 49734 : if ( *proto_mtx_ptr > EPSILON )
2783 : {
2784 29610 : cur_num_src_chan++;
2785 : }
2786 49734 : proto_mtx_ptr += nchan_out_cov;
2787 : }
2788 :
2789 21792 : mvr2r_inc( proto_mtx + cur_out_ch, nchan_out_cov, proto_fac + p_diff_proto_info->num_protos_diff, nchan_out_cov, nchan_transport );
2790 :
2791 21792 : p_diff_proto_info->proto_index_diff[cur_out_ch] = p_diff_proto_info->num_protos_diff;
2792 21792 : p_diff_proto_info->num_protos_diff++;
2793 21792 : max_num_src_chan = max( max_num_src_chan, cur_num_src_chan );
2794 : }
2795 : }
2796 :
2797 : /* set up the prototype info struct */
2798 10893 : if ( ( p_diff_proto_info->source_chan_idx = (int16_t **) malloc( p_diff_proto_info->num_protos_diff * sizeof( int16_t * ) ) ) == NULL )
2799 : {
2800 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
2801 : }
2802 10893 : if ( ( p_diff_proto_info->proto_fac = (float **) malloc( p_diff_proto_info->num_protos_diff * sizeof( float * ) ) ) == NULL )
2803 : {
2804 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
2805 : }
2806 :
2807 43578 : for ( cur_diff_proto = 0; cur_diff_proto < p_diff_proto_info->num_protos_diff; cur_diff_proto++ )
2808 : {
2809 : float *proto_fac_ptr;
2810 :
2811 32685 : if ( ( p_diff_proto_info->source_chan_idx[cur_diff_proto] = (int16_t *) malloc( max_num_src_chan * sizeof( int16_t ) ) ) == NULL )
2812 : {
2813 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
2814 : }
2815 32685 : if ( ( p_diff_proto_info->proto_fac[cur_diff_proto] = (float *) malloc( max_num_src_chan * sizeof( float ) ) ) == NULL )
2816 : {
2817 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
2818 : }
2819 :
2820 32685 : proto_fac_ptr = proto_fac + cur_diff_proto;
2821 107280 : for ( cur_transport_ch = 0; cur_transport_ch < nchan_transport; cur_transport_ch++ )
2822 : {
2823 74595 : if ( *proto_fac_ptr > EPSILON )
2824 : {
2825 40503 : p_diff_proto_info->source_chan_idx[cur_diff_proto][p_diff_proto_info->num_source_chan_diff[cur_diff_proto]] = cur_transport_ch;
2826 40503 : p_diff_proto_info->proto_fac[cur_diff_proto][p_diff_proto_info->num_source_chan_diff[cur_diff_proto]] = *proto_fac_ptr;
2827 40503 : p_diff_proto_info->num_source_chan_diff[cur_diff_proto]++;
2828 : }
2829 74595 : proto_fac_ptr += nchan_out_cov;
2830 : }
2831 : }
2832 :
2833 10893 : return IVAS_ERR_OK;
2834 : }
2835 :
2836 :
2837 : /*-------------------------------------------------------------------------*
2838 : * ivas_param_mc_bs_decode_parameter_values()
2839 : *
2840 : * reads and decodes a sequence of Parametric MC parameters from the bitstream
2841 : *-------------------------------------------------------------------------*/
2842 :
2843 670938 : static void ivas_param_mc_bs_decode_parameter_values(
2844 : uint16_t bit_buffer[], /* i : bitstream buffer */
2845 : int16_t *bit_pos, /* i/o: current bitstream buffer position */
2846 : const int16_t max_bits, /* i : maximum available bits in the buffer */
2847 : int16_t *BER_detect, /* i/o: bit error detection flag */
2848 : HANDLE_IVAS_PARAM_MC_METADATA hMetadataPMC, /* i : Parametric MC metadata information */
2849 : HANDLE_PARAM_MC_PARAMETER_CODING_INFO hParamCodingInfo, /* i : Parametric MC parameter quantization and coding tables */
2850 : const int16_t map_size_wo_lfe, /* i : number of parameters per band (w/o LFEs) */
2851 : const int16_t map_size, /* i : number of parameters per band (total) */
2852 : const int16_t num_lfe_bands, /* i : number of parameter bands with coded LFE */
2853 : const int16_t band_step, /* i : parameter band step */
2854 : const int16_t num_param_bands, /* i : number of parameter bands to decode */
2855 : float *value_buffer /* o : output buffer for decoded parameter values */
2856 : )
2857 : {
2858 : int16_t range_coding;
2859 : int16_t sz_seq;
2860 : int16_t delta_coding;
2861 : int16_t delta_idx[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
2862 : int16_t idx[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
2863 : int16_t idx_prev;
2864 : int16_t idx_offset;
2865 : int16_t sz_alphabet;
2866 : int16_t i, j, k;
2867 : float dequant_seq[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
2868 : float dequant_ordered[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
2869 : int16_t n_lfe_idx;
2870 :
2871 670938 : range_coding = bit_buffer[( *bit_pos )++];
2872 :
2873 : /* Decoding the sequence */
2874 670938 : n_lfe_idx = map_size - map_size_wo_lfe;
2875 670938 : sz_seq = num_param_bands * ( map_size_wo_lfe ) + num_lfe_bands * n_lfe_idx;
2876 :
2877 670938 : set_s( idx, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE );
2878 670938 : set_zero( dequant_ordered, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE );
2879 670938 : set_zero( dequant_seq, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE );
2880 :
2881 670938 : if ( range_coding )
2882 : {
2883 629460 : delta_coding = bit_buffer[( *bit_pos )++];
2884 :
2885 629460 : if ( delta_coding )
2886 : {
2887 612519 : idx_prev = hParamCodingInfo->quantizer_size / 2 + hParamCodingInfo->quantizer_size % 2 - 1;
2888 612519 : sz_alphabet = 2 * hParamCodingInfo->quantizer_size - 1;
2889 612519 : idx_offset = hParamCodingInfo->quantizer_size - 1;
2890 :
2891 : /* read range coded delta ICC indices */
2892 1225038 : *bit_pos += ivas_param_mc_range_decoder_LC( &bit_buffer[*bit_pos], delta_idx, BER_detect, sz_seq, sz_alphabet,
2893 612519 : hParamCodingInfo->cum_freq_delta, hParamCodingInfo->sym_freq_delta, PARAM_MC_RANGE_CODER_TOT_SHIFT, max_bits - *bit_pos );
2894 :
2895 : /* delta index to absolute index */
2896 23036709 : for ( j = 0; j < sz_seq; j++ )
2897 : {
2898 22424190 : idx[j] = idx_prev + delta_idx[j] - idx_offset;
2899 22424190 : idx_prev = idx[j];
2900 : }
2901 : }
2902 : else
2903 : {
2904 : /* read range coded absolute ICC indices */
2905 16941 : sz_alphabet = hParamCodingInfo->quantizer_size;
2906 16941 : *bit_pos += ivas_param_mc_range_decoder_LC( &bit_buffer[*bit_pos], idx, BER_detect, sz_seq, sz_alphabet,
2907 16941 : hParamCodingInfo->cum_freq, hParamCodingInfo->sym_freq, PARAM_MC_RANGE_CODER_TOT_SHIFT, max_bits - *bit_pos );
2908 : }
2909 :
2910 : /* dequantize */
2911 23689905 : for ( j = 0; j < sz_seq; j++ )
2912 : {
2913 23060445 : dequant_seq[j] = hParamCodingInfo->quantizer[idx[j]];
2914 : }
2915 : }
2916 : else
2917 : {
2918 41478 : set_zero( dequant_seq, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE );
2919 :
2920 : /* read uniformly coded ICCs */
2921 41478 : *bit_pos += ivas_param_mc_uniform_decoder( dequant_seq, sz_seq, hParamCodingInfo->quantizer, hParamCodingInfo->uni_bits, &bit_buffer[*bit_pos] );
2922 : }
2923 :
2924 : /* reorder from sequential to parameter-band-wise */
2925 670938 : k = 0;
2926 4187787 : for ( j = 0; j < map_size_wo_lfe; ++j )
2927 : {
2928 27530190 : for ( i = 0; i < num_param_bands; ++i )
2929 : {
2930 24013341 : dequant_ordered[j + i * map_size] = dequant_seq[k++];
2931 : }
2932 : }
2933 :
2934 1020420 : for ( i = 0; i < num_lfe_bands; i++ )
2935 : {
2936 713760 : for ( j = 0; j < n_lfe_idx; j++ )
2937 : {
2938 364278 : dequant_ordered[map_size - n_lfe_idx + j + i * map_size] = dequant_seq[k++];
2939 : }
2940 : }
2941 :
2942 670938 : if ( !( *BER_detect ) )
2943 : {
2944 670938 : j = 0;
2945 9534162 : for ( k = 0; k < hMetadataPMC->nbands_coded; k += band_step )
2946 : {
2947 8863224 : if ( hMetadataPMC->bAttackPresent || hMetadataPMC->param_frame_idx == hMetadataPMC->coding_band_mapping[k] )
2948 : {
2949 4546956 : mvr2r( dequant_ordered + j * map_size, value_buffer + k * map_size, map_size );
2950 4546956 : j++;
2951 : }
2952 8863224 : if ( hMetadataPMC->bAttackPresent && k + 1 < hMetadataPMC->nbands_coded )
2953 : {
2954 223524 : mvr2r( value_buffer + k * map_size, value_buffer + ( k + 1 ) * map_size, map_size );
2955 : }
2956 : }
2957 : }
2958 :
2959 670938 : return;
2960 : }
|