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 13330 : 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 13330 : error = IVAS_ERR_OK;
128 :
129 : /*-----------------------------------------------------------------*
130 : * prepare library opening
131 : *-----------------------------------------------------------------*/
132 :
133 13330 : 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 13330 : 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 13330 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
144 13330 : output_config = st_ivas->hDecoderConfig->output_config;
145 13330 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
146 13330 : mc_ls_setup = ivas_mc_map_output_config_to_mc_ls_setup( st_ivas->transport_config );
147 13330 : nchan_out_transport = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
148 13330 : hParamMC->hoa_encoder = NULL;
149 :
150 : /* determine the synthesis config */
151 13330 : 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 11044 : hParamMC->synthesis_conf = PARAM_MC_SYNTH_DIRECT;
154 : }
155 2286 : else if ( output_config == IVAS_AUDIO_CONFIG_MONO || output_config == IVAS_AUDIO_CONFIG_STEREO )
156 : {
157 422 : hParamMC->synthesis_conf = PARAM_MC_SYNTH_MONO_STEREO;
158 : }
159 1864 : else if ( st_ivas->transport_config != output_config )
160 : {
161 1864 : 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 115 : hParamMC->synthesis_conf = PARAM_MC_SYNTH_LS_CONV_COV;
164 : /* need to reset the intern config */
165 115 : st_ivas->intern_config = output_config;
166 115 : ivas_output_init( &( st_ivas->hIntSetup ), st_ivas->intern_config );
167 115 : 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 1749 : hParamMC->synthesis_conf = PARAM_MC_SYNTH_LS_CONV_CLDFB;
177 : }
178 : }
179 :
180 13330 : hParamMC->ls_conv_dmx_matrix = NULL;
181 :
182 13330 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
183 : {
184 537 : nchan_out_cov = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
185 : }
186 : else
187 : {
188 12793 : nchan_out_cov = nchan_out_transport;
189 : }
190 :
191 13330 : st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels( ivas_total_brate, mc_ls_setup );
192 13330 : config_index = ivas_param_mc_get_configuration_index( mc_ls_setup, ivas_total_brate );
193 13330 : nchan_transport = st_ivas->nchan_transport;
194 :
195 13330 : switch ( nchan_transport )
196 : {
197 3683 : case 4:
198 : case 3:
199 3683 : st_ivas->nCPE = 2;
200 3683 : st_ivas->nSCE = 0;
201 3683 : st_ivas->element_mode_init = IVAS_CPE_MDCT;
202 3683 : break;
203 9647 : case 2:
204 9647 : st_ivas->nCPE = 1;
205 9647 : st_ivas->nSCE = 0;
206 9647 : st_ivas->element_mode_init = IVAS_CPE_MDCT;
207 :
208 9647 : 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 13330 : hParamMC->slot_size = (int16_t) ( output_Fs / FRAMES_PER_SEC ) / CLDFB_NO_COL_MAX;
220 13330 : set_s( hParamMC->subframe_nbslots, 0, MAX_JBM_SUBFRAMES_5MS );
221 13330 : set_s( hParamMC->subframe_nbslots, PARAM_MC_MAX_NSLOTS_IN_SUBFRAME, DEFAULT_JBM_SUBFRAMES_5MS );
222 13330 : hParamMC->nb_subframes = DEFAULT_JBM_SUBFRAMES_5MS;
223 :
224 13330 : hParamMC->num_freq_bands = (int16_t) ( output_Fs * INV_CLDFB_BANDWIDTH + 0.5f );
225 13330 : hParamMC->max_band_energy_compensation = hParamMC->num_freq_bands;
226 13330 : ivas_param_mc_metadata_open( mc_ls_setup, ivas_total_brate, hParamMC->hMetadataPMC );
227 :
228 : /* init arrays for quantized parameters */
229 13330 : 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 13330 : 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 13330 : 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 13330 : set_f( hParamMC->icc_q, 0.0f, hParamMC->hMetadataPMC->num_parameter_bands * hParamMC->hMetadataPMC->icc_mapping_conf->icc_map_size_lfe );
239 :
240 13330 : param_mc_set_num_synth_bands( output_Fs, hParamMC );
241 :
242 : /* Band Grouping */
243 13330 : if ( hParamMC->hMetadataPMC->num_parameter_bands == 20 )
244 : {
245 1212 : mvs2s( param_mc_band_grouping_20, hParamMC->band_grouping, 20 + 1 );
246 : }
247 12118 : else if ( hParamMC->hMetadataPMC->num_parameter_bands == 14 )
248 : {
249 6989 : mvs2s( param_mc_band_grouping_14, hParamMC->band_grouping, 14 + 1 );
250 : }
251 5129 : else if ( hParamMC->hMetadataPMC->num_parameter_bands == 10 )
252 : {
253 5129 : 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 13330 : k = 0;
262 125466 : while ( hParamMC->band_grouping[k] <= PARAM_MC_MAX_BAND_ABS_COV_DEC )
263 : {
264 112136 : hParamMC->max_param_band_abs_cov = ( k++ );
265 : }
266 :
267 : /*-----------------------------------------------------------------*
268 : * open sub-modules
269 : *-----------------------------------------------------------------*/
270 :
271 : /* prototype signal computation */
272 :
273 13330 : 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 2286 : 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 2286 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
282 : {
283 537 : 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 6041 : for ( k = 0; k < nchan_out_transport; k++ )
289 : {
290 5504 : 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 537 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
295 : {
296 422 : st_ivas->hLsSetUpConversion->sfbCnt = hParamMC->num_param_bands_synth;
297 6310 : for ( k = 0; k <= hParamMC->num_param_bands_synth; k++ )
298 : {
299 5888 : 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 115 : ivas_ls_setup_conversion_close( &st_ivas->hLsSetUpConversion );
306 : }
307 : }
308 : }
309 :
310 13330 : 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 13330 : mvr2r( ivas_param_mc_conf[config_index].dmx_fac, hParamMC->proto_matrix_int, nchan_transport * nchan_out_transport );
315 :
316 13330 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
317 : {
318 537 : 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 537 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
321 : {
322 422 : proto_mtx_norm = 1.f;
323 2540 : for ( k = 0; k < nchan_transport * nchan_out_cov; k++ )
324 : {
325 2118 : proto_mtx_norm = max( fabsf( proto_mtx_norm ), fabsf( proto_matrix[k] ) );
326 : }
327 422 : proto_mtx_norm = 1.f / proto_mtx_norm;
328 :
329 : /* transfer flattened proto_matrix to 2D in hLsSetupConversion->dmxMtx */
330 1580 : for ( k = 0; k < nchan_transport; k++ )
331 : {
332 3276 : for ( int16_t i = 0; i < nchan_out_cov; i++ )
333 : {
334 2118 : st_ivas->hLsSetUpConversion->dmxMtx[k][i] = proto_matrix[k * nchan_out_cov + i] * proto_mtx_norm;
335 : }
336 : }
337 : }
338 : }
339 : else
340 : {
341 12793 : mvr2r( ivas_param_mc_conf[config_index].dmx_fac, proto_matrix, nchan_out_transport * nchan_transport );
342 : }
343 :
344 13330 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
345 : {
346 422 : hParamMC->num_outputs_diff = 0;
347 422 : hParamMC->diff_proto_info = NULL;
348 422 : hParamMC->h_output_synthesis_params.use_onset_filters = 0;
349 422 : hParamMC->max_band_decorr = 0;
350 422 : hParamMC->h_freq_domain_decorr_ap_params = NULL;
351 422 : hParamMC->h_freq_domain_decorr_ap_state = NULL;
352 : }
353 : else
354 : {
355 12908 : hParamMC->num_outputs_diff = nchan_out_cov;
356 12908 : 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 12908 : 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 12908 : hParamMC->h_freq_domain_decorr_ap_params = NULL;
368 12908 : hParamMC->h_freq_domain_decorr_ap_state = NULL;
369 :
370 12908 : ivas_dirac_dec_get_frequency_axis( frequency_axis, output_Fs, hParamMC->num_freq_bands );
371 :
372 12908 : 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 12908 : hParamMC->h_output_synthesis_params.use_onset_filters = 0;
379 12908 : hParamMC->max_band_decorr = hParamMC->h_freq_domain_decorr_ap_params->max_band_decorr;
380 : }
381 13330 : hParamMC->max_band_energy_compensation = hParamMC->band_grouping[hParamMC->hMetadataPMC->nbands_coded];
382 13330 : max_param_band_residual = 0;
383 :
384 56469 : for ( k = hParamMC->hMetadataPMC->num_parameter_bands; k >= 0; k-- )
385 : {
386 56469 : if ( hParamMC->band_grouping[k] <= hParamMC->max_band_decorr )
387 : {
388 13330 : max_param_band_residual = k;
389 13330 : assert( hParamMC->band_grouping[k] == hParamMC->max_band_decorr );
390 13330 : break;
391 : }
392 : }
393 :
394 : /* output synthesis */
395 13330 : 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 13330 : 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 13330 : 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 13330 : 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 ) )
405 : {
406 1384 : if ( ( hParamMC->hoa_encoder = (float *) malloc( st_ivas->hTransSetup.nchan_out_woLFE * MAX_INTERN_CHANNELS * sizeof( float ) ) ) == NULL )
407 : {
408 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
409 : }
410 1384 : 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 );
411 : }
412 :
413 : /*-----------------------------------------------------------------*
414 : * memory allocation
415 : *-----------------------------------------------------------------*/
416 :
417 13330 : if ( hParamMC->max_band_decorr > 0 )
418 : {
419 12908 : if ( ( hParamMC->proto_frame_f = (float *) malloc( 2 * hParamMC->diff_proto_info->num_protos_diff * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
420 : {
421 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
422 : }
423 :
424 12908 : if ( ( hParamMC->proto_frame_dec_f = (float *) malloc( 2 * nchan_out_cov * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
425 : {
426 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
427 : }
428 : }
429 : else
430 : {
431 422 : hParamMC->proto_frame_f = NULL;
432 422 : hParamMC->proto_frame_dec_f = NULL;
433 : }
434 :
435 13330 : ivas_param_mc_dec_init( hParamMC, nchan_transport, nchan_out_cov );
436 :
437 13330 : if ( hParamMC->synthesis_conf != PARAM_MC_SYNTH_MONO_STEREO )
438 : {
439 : int16_t n_cldfb_slots;
440 : int16_t granularity;
441 :
442 12908 : n_cldfb_slots = DEFAULT_JBM_CLDFB_TIMESLOTS;
443 12908 : if ( st_ivas->hDecoderConfig->Opt_tsm )
444 : {
445 1549 : n_cldfb_slots = MAX_JBM_CLDFB_TIMESLOTS;
446 : }
447 :
448 12908 : if ( ( hParamMC->Cldfb_RealBuffer_tc = (float *) malloc( n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
449 : {
450 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC JBM\n" ) );
451 : }
452 12908 : set_zero( hParamMC->Cldfb_RealBuffer_tc, n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands );
453 :
454 12908 : if ( ( hParamMC->Cldfb_ImagBuffer_tc = (float *) malloc( n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
455 : {
456 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC JBM\n" ) );
457 : }
458 12908 : set_zero( hParamMC->Cldfb_ImagBuffer_tc, n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands );
459 :
460 12908 : if ( st_ivas->hTcBuffer == NULL )
461 : {
462 1340 : granularity = ivas_jbm_dec_get_render_granularity( st_ivas->renderer_type, RENDERER_DISABLE, output_Fs );
463 :
464 1340 : if ( ( error = ivas_jbm_dec_tc_buffer_open( st_ivas, TC_BUFFER_MODE_RENDERER, nchan_transport, nchan_transport, 0, granularity ) ) != IVAS_ERR_OK )
465 : {
466 0 : return error;
467 : }
468 : }
469 : }
470 : else
471 : {
472 422 : hParamMC->Cldfb_RealBuffer_tc = NULL;
473 422 : hParamMC->Cldfb_ImagBuffer_tc = NULL;
474 : }
475 :
476 13330 : hParamMC->subframes_rendered = 0;
477 13330 : hParamMC->slots_rendered = 0;
478 :
479 13330 : st_ivas->hParamMC = hParamMC;
480 :
481 13330 : return error;
482 : }
483 :
484 :
485 : /*-------------------------------------------------------------------------
486 : * ivas_param_mc_get_param_band_mapping()
487 : *
488 : *
489 : *-------------------------------------------------------------------------*/
490 :
491 2803 : static void ivas_param_mc_get_param_band_mapping(
492 : const int16_t n_target_bands,
493 : const int16_t *target_band_grouping,
494 : const int16_t n_source_bands,
495 : const int16_t *source_band_grouping,
496 : PARAM_MC_PARAMETER_BAND_MAPPING *parameter_band_mapping )
497 : {
498 : int16_t target_band_idx;
499 2803 : int16_t source_band_idx = 0;
500 : int16_t source_band_cnt_total;
501 :
502 39673 : for ( target_band_idx = 0; target_band_idx < n_target_bands; target_band_idx++ )
503 : {
504 36870 : int16_t upper = target_band_grouping[target_band_idx + 1];
505 36870 : int16_t lower = target_band_grouping[target_band_idx];
506 36870 : int16_t source_band_in_target_band_cnt = 0;
507 36870 : float norm_fac = 1.0f;
508 36870 : source_band_cnt_total = 0;
509 226442 : for ( source_band_idx = 0; source_band_idx < n_source_bands; source_band_idx++ )
510 : {
511 : /* find lowest corresponding source band*/
512 226442 : if ( source_band_grouping[source_band_idx] <= lower && source_band_grouping[source_band_idx + 1] >= lower )
513 : {
514 : do
515 : {
516 92714 : int16_t source_bands_in_target_band = min( source_band_grouping[source_band_idx + 1], upper ) - max( source_band_grouping[source_band_idx], lower );
517 92714 : if ( source_bands_in_target_band )
518 : {
519 48276 : source_band_cnt_total += source_bands_in_target_band;
520 48276 : parameter_band_mapping->source_band_idx[target_band_idx][source_band_in_target_band_cnt] = source_band_idx;
521 48276 : parameter_band_mapping->source_band_factor[target_band_idx][source_band_in_target_band_cnt++] = (float) source_bands_in_target_band;
522 : }
523 92714 : source_band_idx++;
524 92714 : } while ( source_band_grouping[source_band_idx] <= upper && source_band_idx < n_source_bands );
525 36870 : break;
526 : }
527 : }
528 36870 : norm_fac = 1.0f / ( (float) source_band_cnt_total );
529 85146 : for ( source_band_idx = 0; source_band_idx < source_band_in_target_band_cnt; source_band_idx++ )
530 : {
531 :
532 48276 : parameter_band_mapping->source_band_factor[target_band_idx][source_band_idx] *= norm_fac;
533 : }
534 36870 : parameter_band_mapping->n_source_bands[target_band_idx] = source_band_in_target_band_cnt;
535 : }
536 :
537 2803 : return;
538 : }
539 :
540 :
541 : /*-------------------------------------------------------------------------
542 : * ivas_param_mc_dec_reconfig()
543 : *
544 : * Reconfiguration of ParamMC decoder
545 : *-------------------------------------------------------------------------*/
546 :
547 2803 : ivas_error ivas_param_mc_dec_reconfig(
548 : Decoder_Struct *st_ivas /* i/o: IVAS decoder structure */
549 : )
550 : {
551 : int16_t k, nchan_transport;
552 : PARAM_MC_DEC_HANDLE hParamMC;
553 : int16_t nchan_out_transport;
554 : int16_t nchan_out_cov;
555 : float proto_matrix[MAX_LS_CHANNELS * PARAM_MC_MAX_TRANSPORT_CHANS];
556 : float proto_mtx_norm;
557 : int16_t max_param_band_residual;
558 : uint16_t config_index;
559 : MC_LS_SETUP mc_ls_setup;
560 : float frequency_axis[CLDFB_NO_CHANNELS_MAX];
561 : int32_t output_Fs, ivas_total_brate;
562 : ivas_error error;
563 : int16_t nchan_transport_old;
564 : int16_t num_param_bands_old;
565 : PARAM_MC_PARAMETER_BAND_MAPPING parameter_band_mapping;
566 : int16_t band_grouping_old[20 + 1];
567 :
568 2803 : error = IVAS_ERR_OK;
569 2803 : hParamMC = st_ivas->hParamMC;
570 :
571 : /* save important config information from the previous state */
572 2803 : nchan_transport_old = st_ivas->nchan_transport;
573 2803 : num_param_bands_old = hParamMC->hMetadataPMC->num_parameter_bands;
574 :
575 : /*-----------------------------------------------------------------*
576 : * prepare library opening
577 : *-----------------------------------------------------------------*/
578 :
579 2803 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
580 2803 : ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
581 2803 : mc_ls_setup = ivas_mc_map_output_config_to_mc_ls_setup( st_ivas->transport_config );
582 2803 : nchan_out_transport = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
583 :
584 2803 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
585 : {
586 0 : nchan_out_cov = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
587 : }
588 : else
589 : {
590 2803 : nchan_out_cov = nchan_out_transport;
591 : }
592 :
593 2803 : st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels( ivas_total_brate, mc_ls_setup );
594 2803 : config_index = ivas_param_mc_get_configuration_index( mc_ls_setup, ivas_total_brate );
595 2803 : nchan_transport = st_ivas->nchan_transport;
596 :
597 2803 : switch ( nchan_transport )
598 : {
599 321 : case 4:
600 : case 3:
601 321 : st_ivas->nCPE = 2;
602 321 : st_ivas->nSCE = 0;
603 321 : st_ivas->element_mode_init = IVAS_CPE_MDCT;
604 321 : break;
605 2482 : case 2:
606 2482 : st_ivas->nCPE = 1;
607 2482 : st_ivas->nSCE = 0;
608 2482 : st_ivas->element_mode_init = IVAS_CPE_MDCT;
609 :
610 2482 : break;
611 : #ifdef DEBUGGING
612 : default:
613 : assert( 0 && "Number of TC not supported for Parametric MC!" );
614 : #endif
615 : }
616 :
617 : /*-----------------------------------------------------------------*
618 : * set input parameters
619 : *-----------------------------------------------------------------*/
620 :
621 2803 : hParamMC->num_freq_bands = (int16_t) ( output_Fs * INV_CLDFB_BANDWIDTH + 0.5f );
622 2803 : hParamMC->max_band_energy_compensation = hParamMC->num_freq_bands;
623 :
624 2803 : mvs2s( hParamMC->band_grouping, band_grouping_old, hParamMC->hMetadataPMC->num_parameter_bands + 1 );
625 :
626 2803 : ivas_param_mc_metadata_open( mc_ls_setup, ivas_total_brate, hParamMC->hMetadataPMC );
627 :
628 : /* Band Grouping */
629 2803 : if ( hParamMC->hMetadataPMC->num_parameter_bands == 20 )
630 : {
631 306 : mvs2s( param_mc_band_grouping_20, hParamMC->band_grouping, 20 + 1 );
632 : }
633 2497 : else if ( hParamMC->hMetadataPMC->num_parameter_bands == 14 )
634 : {
635 1445 : mvs2s( param_mc_band_grouping_14, hParamMC->band_grouping, 14 + 1 );
636 : }
637 1052 : else if ( hParamMC->hMetadataPMC->num_parameter_bands == 10 )
638 : {
639 1052 : mvs2s( param_mc_band_grouping_10, hParamMC->band_grouping, 10 + 1 );
640 : }
641 : else
642 : {
643 0 : assert( 0 && "nbands must be 20, 14, or 10!" );
644 : }
645 :
646 2803 : 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 );
647 :
648 2803 : if ( nchan_transport_old != nchan_transport || num_param_bands_old != hParamMC->hMetadataPMC->num_parameter_bands )
649 : {
650 2647 : float *ild_q_old = hParamMC->icld_q;
651 2647 : float *icc_q_old = hParamMC->icc_q;
652 :
653 : /* init arrays for the quantized parameters */
654 2647 : if ( ( hParamMC->icc_q = (float *) malloc( hParamMC->hMetadataPMC->num_parameter_bands * hParamMC->hMetadataPMC->icc_mapping_conf->icc_map_size_lfe * sizeof( float ) ) ) == NULL )
655 : {
656 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
657 : }
658 2647 : if ( ( hParamMC->icld_q = (float *) malloc( hParamMC->hMetadataPMC->num_parameter_bands * hParamMC->hMetadataPMC->ild_mapping_conf->ild_map_size_lfe * sizeof( float ) ) ) == NULL )
659 : {
660 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
661 : }
662 2647 : set_f( hParamMC->icld_q, PARAM_MC_DEFAULT_MIN_ILD, hParamMC->hMetadataPMC->num_parameter_bands * hParamMC->hMetadataPMC->ild_mapping_conf->ild_map_size_lfe );
663 2647 : set_f( hParamMC->icc_q, 0.0f, hParamMC->hMetadataPMC->num_parameter_bands * hParamMC->hMetadataPMC->icc_mapping_conf->icc_map_size_lfe );
664 :
665 : /* map old to new parameter banding, only for same number of TCs, needs some more thought for a changing number of TCs */
666 2647 : if ( num_param_bands_old != hParamMC->hMetadataPMC->num_parameter_bands && nchan_transport_old == nchan_transport )
667 : {
668 : int16_t new_param_band_idx, param_idx, source_param_idx;
669 : int16_t num_param_lfe;
670 2060 : float *p_icc_new = hParamMC->icc_q;
671 2060 : float *p_ild_new = hParamMC->icld_q;
672 :
673 : /* ICC */
674 2060 : num_param_lfe = hParamMC->hMetadataPMC->icc_mapping_conf->icc_map_size_lfe;
675 27106 : for ( new_param_band_idx = 0; new_param_band_idx < hParamMC->hMetadataPMC->num_parameter_bands; new_param_band_idx++ )
676 : {
677 161140 : for ( param_idx = 0; param_idx < num_param_lfe; param_idx++ )
678 : {
679 136094 : *p_icc_new = 0.0f;
680 316282 : for ( source_param_idx = 0; source_param_idx < parameter_band_mapping.n_source_bands[new_param_band_idx]; source_param_idx++ )
681 : {
682 180188 : *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];
683 : }
684 136094 : p_icc_new++;
685 : }
686 : }
687 :
688 : /* ILD */
689 2060 : num_param_lfe = hParamMC->hMetadataPMC->ild_mapping_conf->ild_map_size_lfe;
690 27106 : for ( new_param_band_idx = 0; new_param_band_idx < hParamMC->hMetadataPMC->num_parameter_bands; new_param_band_idx++ )
691 : {
692 186186 : for ( param_idx = 0; param_idx < num_param_lfe; param_idx++ )
693 : {
694 161140 : *p_ild_new = 0.0f;
695 374540 : for ( source_param_idx = 0; source_param_idx < parameter_band_mapping.n_source_bands[new_param_band_idx]; source_param_idx++ )
696 : {
697 213400 : *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];
698 : }
699 161140 : *p_ild_new = 10.0f * log10f( *p_ild_new );
700 161140 : p_ild_new++;
701 : }
702 : }
703 : }
704 2647 : free( ild_q_old );
705 2647 : free( icc_q_old );
706 : }
707 :
708 2803 : param_mc_set_num_synth_bands( output_Fs, hParamMC );
709 :
710 : /* set max parameter band for abs cov */
711 2803 : k = 0;
712 26538 : while ( hParamMC->band_grouping[k] <= PARAM_MC_MAX_BAND_ABS_COV_DEC )
713 : {
714 23735 : hParamMC->max_param_band_abs_cov = ( k++ );
715 : }
716 :
717 : /*-----------------------------------------------------------------*
718 : * open sub-modules
719 : *-----------------------------------------------------------------*/
720 :
721 : /* prototype signal computation */
722 2803 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
723 : {
724 0 : if ( nchan_transport_old != nchan_transport )
725 : {
726 0 : if ( st_ivas->hLsSetUpConversion != NULL )
727 : {
728 0 : ivas_ls_setup_conversion_close( &st_ivas->hLsSetUpConversion );
729 : }
730 :
731 0 : if ( ( error = ivas_ls_setup_conversion_open( st_ivas ) ) != IVAS_ERR_OK )
732 : {
733 0 : return error;
734 : }
735 :
736 : /* convert the ls conv dmx matrix into column order matrix format (nchan_out_cldfb x nchan_out) */
737 0 : free( hParamMC->ls_conv_dmx_matrix );
738 0 : if ( ( hParamMC->ls_conv_dmx_matrix = (float *) malloc( nchan_out_transport * nchan_out_cov * sizeof( float ) ) ) == NULL )
739 : {
740 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
741 : }
742 0 : for ( k = 0; k < nchan_out_transport; k++ )
743 : {
744 0 : mvr2r( st_ivas->hLsSetUpConversion->dmxMtx[k], &hParamMC->ls_conv_dmx_matrix[k * nchan_out_cov], nchan_out_cov );
745 : }
746 : }
747 :
748 : /* convert ParamMC parameter bands to SFB */
749 0 : st_ivas->hLsSetUpConversion->sfbCnt = hParamMC->num_param_bands_synth;
750 0 : for ( k = 0; k <= hParamMC->num_param_bands_synth; k++ )
751 : {
752 0 : st_ivas->hLsSetUpConversion->sfbOffset[k] = PARAM_MC_BAND_TO_MDCT_BAND_RATIO * hParamMC->band_grouping[k];
753 : }
754 0 : for ( ; k < MAX_SFB + 2; k++ )
755 : {
756 0 : st_ivas->hLsSetUpConversion->sfbOffset[k] = 0;
757 : }
758 : }
759 :
760 2803 : if ( nchan_transport_old != nchan_transport )
761 : {
762 587 : free( hParamMC->proto_matrix_int );
763 587 : if ( ( hParamMC->proto_matrix_int = (float *) malloc( nchan_out_transport * nchan_transport * sizeof( float ) ) ) == NULL )
764 : {
765 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
766 : }
767 587 : mvr2r( ivas_param_mc_conf[config_index].dmx_fac, hParamMC->proto_matrix_int, nchan_transport * nchan_out_transport );
768 : }
769 :
770 2803 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
771 : {
772 0 : matrix_product( hParamMC->ls_conv_dmx_matrix, nchan_out_cov, nchan_out_transport, 0,
773 : ivas_param_mc_conf[config_index].dmx_fac, nchan_out_transport, nchan_transport, 0,
774 : proto_matrix );
775 :
776 0 : if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
777 : {
778 0 : proto_mtx_norm = 1.f;
779 0 : for ( k = 0; k < nchan_transport * nchan_out_cov; k++ )
780 : {
781 0 : proto_mtx_norm = max( fabsf( proto_mtx_norm ), fabsf( proto_matrix[k] ) );
782 : }
783 0 : proto_mtx_norm = 1.f / proto_mtx_norm;
784 :
785 : /* transfer flattened proto_matrix to 2D in hLsSetupConversion->dmxMtx */
786 0 : for ( k = 0; k < nchan_transport; k++ )
787 : {
788 0 : for ( int16_t i = 0; i < nchan_out_cov; i++ )
789 : {
790 0 : st_ivas->hLsSetUpConversion->dmxMtx[k][i] = proto_matrix[k * nchan_out_cov + i] * proto_mtx_norm;
791 : }
792 : }
793 : }
794 : }
795 : else
796 : {
797 2803 : mvr2r( ivas_param_mc_conf[config_index].dmx_fac, proto_matrix, nchan_out_transport * nchan_transport );
798 : }
799 :
800 2803 : if ( nchan_transport_old != nchan_transport && hParamMC->synthesis_conf != PARAM_MC_SYNTH_MONO_STEREO )
801 : {
802 : int16_t i;
803 : int16_t len;
804 :
805 : /* close decorrelator */
806 587 : ivas_dirac_dec_decorr_close( &hParamMC->h_freq_domain_decorr_ap_params, &hParamMC->h_freq_domain_decorr_ap_state );
807 :
808 : /* deallocate diffuse prototype info */
809 587 : if ( hParamMC->diff_proto_info )
810 : {
811 2348 : for ( i = 0; i < hParamMC->diff_proto_info->num_protos_diff; i++ )
812 : {
813 1761 : free( hParamMC->diff_proto_info->source_chan_idx[i] );
814 1761 : hParamMC->diff_proto_info->source_chan_idx[i] = NULL;
815 :
816 1761 : free( hParamMC->diff_proto_info->proto_fac[i] );
817 1761 : hParamMC->diff_proto_info->proto_fac[i] = NULL;
818 : }
819 :
820 587 : free( hParamMC->diff_proto_info->source_chan_idx );
821 587 : hParamMC->diff_proto_info->source_chan_idx = NULL;
822 :
823 587 : free( hParamMC->diff_proto_info->proto_fac );
824 587 : hParamMC->diff_proto_info->proto_fac = NULL;
825 :
826 587 : free( hParamMC->diff_proto_info->proto_index_diff );
827 587 : hParamMC->diff_proto_info->proto_index_diff = NULL;
828 :
829 587 : free( hParamMC->diff_proto_info->num_source_chan_diff );
830 587 : hParamMC->diff_proto_info->num_source_chan_diff = NULL;
831 :
832 587 : free( hParamMC->diff_proto_info );
833 587 : hParamMC->diff_proto_info = NULL;
834 : }
835 :
836 587 : hParamMC->num_outputs_diff = nchan_out_cov;
837 587 : if ( ( hParamMC->diff_proto_info = (PARAM_MC_DIFF_PROTO_INFO *) malloc( sizeof( PARAM_MC_DIFF_PROTO_INFO ) ) ) == NULL )
838 : {
839 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
840 : }
841 :
842 587 : if ( ( param_mc_get_diff_proto_info( proto_matrix, nchan_transport, nchan_out_cov, hParamMC->diff_proto_info ) ) != IVAS_ERR_OK )
843 : {
844 0 : return error;
845 : }
846 :
847 : /* decorrelation */
848 587 : hParamMC->h_freq_domain_decorr_ap_params = NULL;
849 587 : hParamMC->h_freq_domain_decorr_ap_state = NULL;
850 :
851 587 : ivas_dirac_dec_get_frequency_axis( frequency_axis, output_Fs, hParamMC->num_freq_bands );
852 :
853 587 : 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,
854 587 : hParamMC->diff_proto_info->num_protos_diff, DIRAC_SYNTHESIS_COV_MC_LS, frequency_axis, nchan_transport, output_Fs ) ) != IVAS_ERR_OK )
855 : {
856 0 : return error;
857 : }
858 :
859 587 : hParamMC->h_output_synthesis_params.use_onset_filters = 0;
860 587 : hParamMC->max_band_decorr = hParamMC->h_freq_domain_decorr_ap_params->max_band_decorr;
861 : /* init decorrelation */
862 587 : if ( hParamMC->max_band_decorr > 0 )
863 : {
864 :
865 587 : len = hParamMC->diff_proto_info->num_protos_diff * hParamMC->h_freq_domain_decorr_ap_params->h_onset_detection_power_params.max_band_decorr;
866 :
867 : /* init onsetDetectionPower */
868 587 : set_zero( hParamMC->h_freq_domain_decorr_ap_state->h_onset_detection_power_state.onset_detector_1, len );
869 587 : set_zero( hParamMC->h_freq_domain_decorr_ap_state->h_onset_detection_power_state.onset_detector_2, len );
870 : }
871 : }
872 2803 : hParamMC->max_band_energy_compensation = hParamMC->band_grouping[hParamMC->hMetadataPMC->nbands_coded];
873 2803 : max_param_band_residual = 0;
874 :
875 11078 : for ( k = hParamMC->hMetadataPMC->num_parameter_bands; k >= 0; k-- )
876 : {
877 11078 : if ( hParamMC->band_grouping[k] <= hParamMC->max_band_decorr )
878 : {
879 2803 : max_param_band_residual = k;
880 2803 : assert( hParamMC->band_grouping[k] == hParamMC->max_band_decorr );
881 2803 : break;
882 : }
883 : }
884 :
885 2803 : if ( nchan_transport_old != nchan_transport || num_param_bands_old != hParamMC->hMetadataPMC->num_parameter_bands )
886 : {
887 2647 : DIRAC_OUTPUT_SYNTHESIS_COV_STATE cov_state_old = hParamMC->h_output_synthesis_cov_state;
888 2647 : DIRAC_OUTPUT_SYNTHESIS_PARAMS params_old = hParamMC->h_output_synthesis_params;
889 : float tmp_buf[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
890 :
891 2647 : set_zero( tmp_buf, MAX_LS_CHANNELS * MAX_LS_CHANNELS );
892 :
893 : /* output synthesis */
894 2647 : 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,
895 2647 : hParamMC->hMetadataPMC->num_parameter_bands, max_param_band_residual, nchan_transport, nchan_out_cov, proto_matrix ) ) != IVAS_ERR_OK )
896 : {
897 0 : return error;
898 : }
899 :
900 2647 : ivas_param_mc_dec_compute_interpolator( 0, 0, DEFAULT_JBM_CLDFB_TIMESLOTS, hParamMC->h_output_synthesis_params.interpolator );
901 :
902 2647 : 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 );
903 :
904 : /* map old to new parameter banding, only for same number of TCs, needs some more thought for changing number of TCs */
905 2647 : if ( num_param_bands_old != hParamMC->hMetadataPMC->num_parameter_bands && nchan_transport_old == nchan_transport )
906 : {
907 : int16_t new_param_band_idx, source_param_idx;
908 :
909 :
910 27106 : for ( new_param_band_idx = 0; new_param_band_idx < hParamMC->hMetadataPMC->num_parameter_bands; new_param_band_idx++ )
911 : {
912 58258 : for ( source_param_idx = 0; source_param_idx < parameter_band_mapping.n_source_bands[new_param_band_idx]; source_param_idx++ )
913 : {
914 : /* Cx */
915 33212 : 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 );
916 33212 : 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 );
917 : /* Cy */
918 33212 : 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 );
919 33212 : 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 );
920 : /* mixing matrix*/
921 33212 : 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 );
922 33212 : 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 );
923 : }
924 : }
925 21843 : for ( new_param_band_idx = 0; new_param_band_idx < max_param_band_residual; new_param_band_idx++ )
926 : {
927 46671 : for ( source_param_idx = 0; source_param_idx < parameter_band_mapping.n_source_bands[new_param_band_idx]; source_param_idx++ )
928 : {
929 : /* residual mixing matrix*/
930 26888 : 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 );
931 26888 : 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 );
932 : }
933 : }
934 : }
935 :
936 2647 : ivas_dirac_dec_output_synthesis_cov_close( ¶ms_old, &cov_state_old );
937 : }
938 :
939 : /*-----------------------------------------------------------------*
940 : * memory allocation
941 : *-----------------------------------------------------------------*/
942 :
943 2803 : if ( hParamMC->max_band_decorr > 0 && nchan_transport_old != nchan_transport )
944 : {
945 587 : free( hParamMC->proto_frame_f );
946 587 : if ( ( hParamMC->proto_frame_f = (float *) malloc( 2 * hParamMC->diff_proto_info->num_protos_diff * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
947 : {
948 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
949 : }
950 587 : set_zero( hParamMC->proto_frame_f, 2 * hParamMC->diff_proto_info->num_protos_diff * hParamMC->num_freq_bands );
951 : }
952 :
953 2803 : if ( nchan_transport_old != nchan_transport )
954 : {
955 587 : if ( hParamMC->synthesis_conf != PARAM_MC_SYNTH_MONO_STEREO )
956 : {
957 : int16_t n_cldfb_slots;
958 :
959 587 : if ( hParamMC->Cldfb_RealBuffer_tc != NULL )
960 : {
961 587 : free( hParamMC->Cldfb_RealBuffer_tc );
962 587 : hParamMC->Cldfb_RealBuffer_tc = NULL;
963 : }
964 587 : if ( hParamMC->Cldfb_ImagBuffer_tc != NULL )
965 : {
966 587 : free( hParamMC->Cldfb_ImagBuffer_tc );
967 587 : hParamMC->Cldfb_ImagBuffer_tc = NULL;
968 : }
969 :
970 587 : n_cldfb_slots = DEFAULT_JBM_CLDFB_TIMESLOTS;
971 587 : if ( st_ivas->hDecoderConfig->Opt_tsm )
972 : {
973 123 : n_cldfb_slots = MAX_JBM_CLDFB_TIMESLOTS;
974 : }
975 587 : if ( ( hParamMC->Cldfb_RealBuffer_tc = (float *) malloc( n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
976 : {
977 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC JBM\n" ) );
978 : }
979 587 : set_zero( hParamMC->Cldfb_RealBuffer_tc, n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands );
980 :
981 587 : if ( ( hParamMC->Cldfb_ImagBuffer_tc = (float *) malloc( n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands * sizeof( float ) ) ) == NULL )
982 : {
983 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC JBM\n" ) );
984 : }
985 587 : set_zero( hParamMC->Cldfb_ImagBuffer_tc, n_cldfb_slots * nchan_transport * hParamMC->num_freq_bands );
986 : }
987 : else
988 : {
989 0 : if ( hParamMC->Cldfb_RealBuffer_tc != NULL )
990 : {
991 0 : free( hParamMC->Cldfb_RealBuffer_tc );
992 0 : hParamMC->Cldfb_RealBuffer_tc = NULL;
993 : }
994 0 : if ( hParamMC->Cldfb_ImagBuffer_tc != NULL )
995 : {
996 0 : free( hParamMC->Cldfb_ImagBuffer_tc );
997 0 : hParamMC->Cldfb_ImagBuffer_tc = NULL;
998 : }
999 : }
1000 : }
1001 :
1002 2803 : return error;
1003 : }
1004 :
1005 :
1006 : /*-------------------------------------------------------------------------
1007 : * param_mc_get_num_cldfb_syntheses()
1008 : *
1009 : * calculate the necessary number of CLDFB synthesis instances
1010 : *------------------------------------------------------------------------*/
1011 :
1012 : /*! r: number of cldfb synthesis instances */
1013 14602 : int16_t param_mc_get_num_cldfb_syntheses(
1014 : Decoder_Struct *st_ivas /* i : Parametric MC handle */
1015 : )
1016 : {
1017 : int16_t num_cldfb_syntheses;
1018 :
1019 14602 : num_cldfb_syntheses = 0;
1020 :
1021 : /* sanity check*/
1022 14602 : if ( st_ivas->hParamMC == NULL )
1023 : {
1024 0 : assert( 0 && "ParamMC handle does not exist!\n" );
1025 : }
1026 :
1027 14602 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1028 : {
1029 0 : num_cldfb_syntheses = 2;
1030 : }
1031 14602 : else if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
1032 : {
1033 5072 : num_cldfb_syntheses = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
1034 : }
1035 9530 : else if ( st_ivas->renderer_type == RENDERER_MC_PARAMMC )
1036 : {
1037 9530 : num_cldfb_syntheses = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
1038 : }
1039 : #ifdef DEBUGGING
1040 : else
1041 : {
1042 : assert( 0 && "Renderer settings not valid for ParamMC!\n" );
1043 : }
1044 : #endif
1045 :
1046 14602 : return num_cldfb_syntheses;
1047 : }
1048 :
1049 :
1050 : /*-------------------------------------------------------------------------
1051 : * ivas_param_mc_dec_close()
1052 : *
1053 : * Close Parametric MC memories
1054 : *------------------------------------------------------------------------*/
1055 :
1056 101480 : void ivas_param_mc_dec_close(
1057 : PARAM_MC_DEC_HANDLE *hParamMC_out /* i/o: Parametric MC decoder handle */
1058 : )
1059 : {
1060 : uint16_t i;
1061 : PARAM_MC_DEC_HANDLE hParamMC;
1062 :
1063 101480 : if ( hParamMC_out == NULL || *hParamMC_out == NULL )
1064 : {
1065 88150 : return;
1066 : }
1067 :
1068 13330 : hParamMC = *hParamMC_out;
1069 :
1070 : /* close sub-modules */
1071 13330 : ivas_dirac_dec_output_synthesis_cov_close( &hParamMC->h_output_synthesis_params, &hParamMC->h_output_synthesis_cov_state );
1072 :
1073 13330 : if ( hParamMC->h_freq_domain_decorr_ap_params != NULL || hParamMC->h_freq_domain_decorr_ap_state != NULL )
1074 : {
1075 12908 : ivas_dirac_dec_decorr_close( &hParamMC->h_freq_domain_decorr_ap_params, &hParamMC->h_freq_domain_decorr_ap_state );
1076 : }
1077 :
1078 : /* parameter decoding */
1079 13330 : if ( hParamMC->hMetadataPMC != NULL )
1080 : {
1081 13330 : free( hParamMC->hMetadataPMC );
1082 13330 : hParamMC->hMetadataPMC = NULL;
1083 : }
1084 :
1085 13330 : if ( hParamMC->icc_q != NULL )
1086 : {
1087 13330 : free( hParamMC->icc_q );
1088 13330 : hParamMC->icc_q = NULL;
1089 : }
1090 :
1091 13330 : if ( hParamMC->icld_q != NULL )
1092 : {
1093 13330 : free( hParamMC->icld_q );
1094 13330 : hParamMC->icld_q = NULL;
1095 : }
1096 :
1097 : /* diffuse prototype info */
1098 13330 : if ( hParamMC->diff_proto_info )
1099 : {
1100 51862 : for ( i = 0; i < hParamMC->diff_proto_info->num_protos_diff; i++ )
1101 : {
1102 38954 : free( hParamMC->diff_proto_info->source_chan_idx[i] );
1103 38954 : hParamMC->diff_proto_info->source_chan_idx[i] = NULL;
1104 :
1105 38954 : free( hParamMC->diff_proto_info->proto_fac[i] );
1106 38954 : hParamMC->diff_proto_info->proto_fac[i] = NULL;
1107 : }
1108 :
1109 12908 : free( hParamMC->diff_proto_info->source_chan_idx );
1110 12908 : hParamMC->diff_proto_info->source_chan_idx = NULL;
1111 :
1112 12908 : free( hParamMC->diff_proto_info->proto_fac );
1113 12908 : hParamMC->diff_proto_info->proto_fac = NULL;
1114 :
1115 12908 : free( hParamMC->diff_proto_info->proto_index_diff );
1116 12908 : hParamMC->diff_proto_info->proto_index_diff = NULL;
1117 :
1118 12908 : free( hParamMC->diff_proto_info->num_source_chan_diff );
1119 12908 : hParamMC->diff_proto_info->num_source_chan_diff = NULL;
1120 :
1121 12908 : free( hParamMC->diff_proto_info );
1122 12908 : hParamMC->diff_proto_info = NULL;
1123 : }
1124 :
1125 : /* States */
1126 : /* free prototype signal buffers */
1127 13330 : if ( hParamMC->proto_frame_f != NULL )
1128 : {
1129 12908 : free( hParamMC->proto_frame_f );
1130 12908 : hParamMC->proto_frame_f = NULL;
1131 : }
1132 :
1133 13330 : if ( hParamMC->proto_frame_dec_f != NULL )
1134 : {
1135 12908 : free( hParamMC->proto_frame_dec_f );
1136 12908 : hParamMC->proto_frame_dec_f = NULL;
1137 : }
1138 :
1139 13330 : if ( hParamMC->ls_conv_dmx_matrix != NULL )
1140 : {
1141 537 : free( hParamMC->ls_conv_dmx_matrix );
1142 537 : hParamMC->ls_conv_dmx_matrix = NULL;
1143 : }
1144 :
1145 13330 : if ( hParamMC->proto_matrix_int != NULL )
1146 : {
1147 13330 : free( hParamMC->proto_matrix_int );
1148 13330 : hParamMC->proto_matrix_int = NULL;
1149 : }
1150 :
1151 13330 : if ( hParamMC->hoa_encoder != NULL )
1152 : {
1153 1384 : free( hParamMC->hoa_encoder );
1154 1384 : hParamMC->hoa_encoder = NULL;
1155 : }
1156 :
1157 13330 : if ( hParamMC->Cldfb_RealBuffer_tc != NULL )
1158 : {
1159 12908 : free( hParamMC->Cldfb_RealBuffer_tc );
1160 12908 : hParamMC->Cldfb_RealBuffer_tc = NULL;
1161 : }
1162 13330 : if ( hParamMC->Cldfb_ImagBuffer_tc != NULL )
1163 : {
1164 12908 : free( hParamMC->Cldfb_ImagBuffer_tc );
1165 12908 : hParamMC->Cldfb_ImagBuffer_tc = NULL;
1166 : }
1167 :
1168 13330 : free( *hParamMC_out );
1169 13330 : *hParamMC_out = NULL;
1170 :
1171 13330 : return;
1172 : }
1173 :
1174 :
1175 : /*-------------------------------------------------------------------------
1176 : * ivas_param_mc_dec_read_BS()
1177 : *
1178 : * Read the Parametric MC metadata
1179 : *------------------------------------------------------------------------*/
1180 :
1181 459633 : void ivas_param_mc_dec_read_BS(
1182 : const int32_t ivas_total_brate, /* i : IVAS total bitrate */
1183 : Decoder_State *st, /* i/o: decoder state structure */
1184 : PARAM_MC_DEC_HANDLE hParamMC, /* i/o: decoder ParamMC handle */
1185 : int16_t *nb_bits /* o : number of bits written */
1186 : )
1187 : {
1188 : int16_t param_frame_idx;
1189 : int16_t band_step;
1190 : uint16_t bit_buffer[PARAM_MC_MAX_BITS];
1191 : int16_t bits_to_copy;
1192 : int16_t bit_pos;
1193 : int16_t num_lfe_bands;
1194 : int16_t num_param_bands;
1195 : int16_t metadata_bit_pos;
1196 : int16_t i, j, k;
1197 : int16_t icc_map_size;
1198 : int16_t icc_map_size_wo_lfe;
1199 : int16_t ild_map_size;
1200 : int16_t ild_map_size_wo_lfe;
1201 : HANDLE_IVAS_PARAM_MC_METADATA hMetadataPMC;
1202 :
1203 459633 : push_wmops( "param_mc_read_bs" );
1204 :
1205 : /* Inits */
1206 459633 : *nb_bits = 0;
1207 459633 : hMetadataPMC = hParamMC->hMetadataPMC;
1208 459633 : icc_map_size = hMetadataPMC->icc_mapping_conf->icc_map_size_lfe;
1209 459633 : icc_map_size_wo_lfe = hMetadataPMC->icc_mapping_conf->icc_map_size_wo_lfe;
1210 459633 : ild_map_size = hMetadataPMC->ild_mapping_conf->ild_map_size_lfe;
1211 459633 : ild_map_size_wo_lfe = hMetadataPMC->ild_mapping_conf->ild_map_size_wo_lfe;
1212 :
1213 459633 : if ( !st->bfi )
1214 : {
1215 449799 : metadata_bit_pos = (int16_t) ( ivas_total_brate / FRAMES_PER_SEC - 1 );
1216 449799 : bits_to_copy = (int16_t) min( ivas_total_brate / FRAMES_PER_SEC, PARAM_MC_MAX_BITS );
1217 :
1218 : /* copy and reverse metadata */
1219 456180615 : for ( bit_pos = 0; bit_pos < bits_to_copy; bit_pos++ )
1220 : {
1221 455730816 : bit_buffer[bit_pos] = st->bit_stream[metadata_bit_pos--];
1222 : }
1223 :
1224 449799 : bit_pos = 0;
1225 :
1226 : /* read reserved bit */
1227 449799 : hMetadataPMC->lfe_on = bit_buffer[bit_pos++];
1228 :
1229 : /* get coded bwidth */
1230 : {
1231 : int16_t pos;
1232 449799 : int16_t bw = 0;
1233 1349397 : for ( pos = 0; pos < 2; pos++ )
1234 : {
1235 899598 : bw += ( bit_buffer[bit_pos++] << pos );
1236 : }
1237 449799 : hMetadataPMC->coded_bwidth = bw;
1238 : }
1239 :
1240 : /* set tables if coded band width differs from last frame */
1241 449799 : if ( hMetadataPMC->coded_bwidth != hMetadataPMC->last_coded_bwidth )
1242 : {
1243 8050 : ivas_param_mc_set_coded_bands( hMetadataPMC );
1244 8050 : param_mc_set_num_synth_bands( st->output_Fs, hParamMC );
1245 8050 : hParamMC->max_band_energy_compensation = hParamMC->band_grouping[hParamMC->num_param_bands_synth];
1246 : }
1247 :
1248 449799 : param_frame_idx = bit_buffer[bit_pos++];
1249 449799 : hMetadataPMC->param_frame_idx = param_frame_idx;
1250 449799 : num_param_bands = hMetadataPMC->nbands_in_param_frame[param_frame_idx];
1251 :
1252 449799 : hMetadataPMC->bAttackPresent = bit_buffer[bit_pos++];
1253 449799 : hMetadataPMC->attackIndex = 0;
1254 449799 : band_step = 1;
1255 449799 : num_lfe_bands = 0;
1256 :
1257 449799 : if ( hMetadataPMC->bAttackPresent )
1258 : {
1259 104844 : for ( i = 2; i >= 0; i-- )
1260 : {
1261 78633 : hMetadataPMC->attackIndex += ( bit_buffer[bit_pos++] << i );
1262 : }
1263 :
1264 26211 : band_step = PARAM_MC_TRANSIENT_BAND_STEP;
1265 26211 : num_lfe_bands = PARAM_MC_MAX_BAND_LFE / band_step + ( PARAM_MC_MAX_BAND_LFE % band_step ) ? 1 : 0;
1266 26211 : num_param_bands = hMetadataPMC->nbands_coded / band_step + ( ( hMetadataPMC->nbands_coded % band_step ) ? 1 : 0 );
1267 : }
1268 : else
1269 : {
1270 847176 : for ( j = 0; j < PARAM_MC_MAX_BAND_LFE; j += band_step )
1271 : {
1272 423588 : if ( param_frame_idx == hMetadataPMC->coding_band_mapping[j] )
1273 : {
1274 : /* LFE ICC is always the last ICC in coding band 0 */
1275 211785 : num_lfe_bands++;
1276 : }
1277 : }
1278 : }
1279 :
1280 449799 : if ( !hMetadataPMC->lfe_on )
1281 : {
1282 55627 : num_lfe_bands = 0;
1283 : }
1284 :
1285 449799 : ivas_param_mc_bs_decode_parameter_values( bit_buffer, &bit_pos, bits_to_copy, &st->BER_detect, hMetadataPMC, &hMetadataPMC->icc_coding,
1286 : icc_map_size_wo_lfe, icc_map_size, num_lfe_bands, band_step, num_param_bands, hParamMC->icc_q );
1287 449799 : if ( !st->BER_detect )
1288 : {
1289 449799 : ivas_param_mc_bs_decode_parameter_values( bit_buffer, &bit_pos, bits_to_copy, &st->BER_detect, hMetadataPMC, &hMetadataPMC->ild_coding,
1290 : ild_map_size_wo_lfe, ild_map_size, num_lfe_bands, band_step, num_param_bands, hParamMC->icld_q );
1291 : }
1292 : /* set LFE ILD and ICC to zero above PARAM_MC_MAX_BAND_LFE for attack frames */
1293 449799 : if ( hMetadataPMC->bAttackPresent )
1294 : {
1295 49243 : for ( k = PARAM_MC_MAX_BAND_LFE; k < band_step * num_lfe_bands; k++ )
1296 : {
1297 23032 : hParamMC->icc_q[( k + 1 ) * icc_map_size - 1] = 1.0f;
1298 23032 : hParamMC->icld_q[( k + 1 ) * ild_map_size - 1] = PARAM_MC_DEFAULT_MIN_ILD;
1299 : }
1300 : }
1301 :
1302 449799 : *nb_bits = bit_pos;
1303 :
1304 : } /* if ( !st->bfi ) */
1305 :
1306 459633 : if ( st->bfi )
1307 : {
1308 : /* for PLC, use the saved ILDs and ICCs from the past and set the transient flag and transient position to zero */
1309 9834 : hMetadataPMC->bAttackPresent = 0;
1310 9834 : hMetadataPMC->attackIndex = 0;
1311 : }
1312 :
1313 459633 : pop_wmops();
1314 :
1315 459633 : return;
1316 : }
1317 :
1318 :
1319 : /*-------------------------------------------------------------------------
1320 : * ivas_param_mc_dec_digest_tc()
1321 : *
1322 : *
1323 : *------------------------------------------------------------------------*/
1324 :
1325 448549 : void ivas_param_mc_dec_digest_tc(
1326 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1327 : const uint8_t nCldfbSlots, /* i : number of CLDFB slots in transport channels */
1328 : float *p_data_f[] /* i/o: synthesized core-coder transport channels/DirAC output */
1329 : )
1330 : {
1331 : int16_t ch, num_freq_bands, slot_idx, cldfb_ch, n_ch_cldfb;
1332 : float *cldfb_real_buffer, *cldfb_imag_buffer;
1333 :
1334 : /* Initialization */
1335 448549 : num_freq_bands = st_ivas->hParamMC->num_freq_bands;
1336 448549 : n_ch_cldfb = st_ivas->hTcBuffer->nchan_transport_jbm - st_ivas->hTcBuffer->nchan_buffer_full;
1337 :
1338 448549 : cldfb_real_buffer = st_ivas->hParamMC->Cldfb_RealBuffer_tc;
1339 448549 : cldfb_imag_buffer = st_ivas->hParamMC->Cldfb_ImagBuffer_tc;
1340 :
1341 : /* CLDFB Analysis */
1342 1404567 : for ( ch = 0, cldfb_ch = 0; cldfb_ch < n_ch_cldfb; cldfb_ch++, ch++ )
1343 : {
1344 16254832 : for ( slot_idx = 0; slot_idx < nCldfbSlots; slot_idx++ )
1345 : {
1346 15298814 : cldfbAnalysis_ts( &( p_data_f[ch][num_freq_bands * slot_idx] ),
1347 15298814 : &cldfb_real_buffer[slot_idx * num_freq_bands * n_ch_cldfb + cldfb_ch * num_freq_bands],
1348 15298814 : &cldfb_imag_buffer[slot_idx * num_freq_bands * n_ch_cldfb + cldfb_ch * num_freq_bands],
1349 : num_freq_bands, st_ivas->cldfbAnaDec[cldfb_ch] );
1350 : }
1351 : }
1352 :
1353 448549 : return;
1354 : }
1355 :
1356 :
1357 : /*-------------------------------------------------------------------------
1358 : * ivas_param_mc_dec_prepare_renderer()
1359 : *
1360 : *
1361 : *------------------------------------------------------------------------*/
1362 :
1363 448549 : void ivas_param_mc_dec_prepare_renderer(
1364 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1365 : const uint8_t nCldfbSlots /* i : number of CLDFB slots in transport channels */
1366 : )
1367 : {
1368 : PARAM_MC_DEC_HANDLE hParamMC;
1369 : int16_t i;
1370 : int16_t is_next_band, skip_next_band;
1371 : int16_t slot_idx, param_band_idx;
1372 : int16_t nchan_transport, nchan_out_transport, nchan_out_cldfb;
1373 : int16_t nchan_out_cov;
1374 : /*CLDFB*/
1375 : float *pCx, *pCx_imag;
1376 : float cx[PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS];
1377 : float cx_imag[PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS];
1378 : float cx_next_band[PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS];
1379 : float cx_imag_next_band[PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS];
1380 : float real_part, imag_part;
1381 : /* format converter */
1382 : int16_t channel_active[MAX_OUTPUT_CHANNELS];
1383 : IVAS_OUTPUT_SETUP *hSynthesisOutputSetup;
1384 :
1385 448549 : hParamMC = st_ivas->hParamMC;
1386 448549 : assert( hParamMC );
1387 :
1388 448549 : set_s( channel_active, 0, MAX_LS_CHANNELS );
1389 448549 : nchan_transport = st_ivas->nchan_transport;
1390 448549 : nchan_out_transport = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
1391 :
1392 448549 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1393 : {
1394 309044 : nchan_out_cldfb = BINAURAL_CHANNELS;
1395 309044 : set_s( channel_active, 1, nchan_out_cldfb );
1396 309044 : nchan_out_cov = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
1397 309044 : hSynthesisOutputSetup = &st_ivas->hTransSetup;
1398 : }
1399 139505 : else if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_CLDFB )
1400 : {
1401 20791 : nchan_out_cov = nchan_out_transport;
1402 20791 : nchan_out_cldfb = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
1403 20791 : hSynthesisOutputSetup = &st_ivas->hTransSetup;
1404 : }
1405 118714 : else if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
1406 : {
1407 29874 : nchan_out_cov = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
1408 29874 : nchan_out_cldfb = nchan_out_cov;
1409 29874 : set_s( channel_active, 1, nchan_out_cov );
1410 29874 : hSynthesisOutputSetup = &st_ivas->hOutSetup;
1411 : }
1412 : else
1413 : {
1414 88840 : nchan_out_cov = nchan_out_transport;
1415 88840 : nchan_out_cldfb = nchan_out_transport;
1416 88840 : set_s( channel_active, 1, nchan_out_cov );
1417 88840 : hSynthesisOutputSetup = &st_ivas->hTransSetup;
1418 : }
1419 :
1420 : /* adapt transient position */
1421 448549 : if ( hParamMC->hMetadataPMC->bAttackPresent )
1422 : {
1423 25266 : hParamMC->hMetadataPMC->attackIndex = (int16_t) max( 0, hParamMC->hMetadataPMC->attackIndex + ( ( nCldfbSlots - DEFAULT_JBM_CLDFB_TIMESLOTS ) / 2 ) );
1424 : }
1425 : /* adapt subframes */
1426 448549 : hParamMC->num_slots = nCldfbSlots;
1427 448549 : hParamMC->slots_rendered = 0;
1428 448549 : hParamMC->subframes_rendered = 0;
1429 448549 : ivas_jbm_dec_get_adapted_subframes( nCldfbSlots, hParamMC->subframe_nbslots, &hParamMC->nb_subframes );
1430 448549 : st_ivas->hTcBuffer->nb_subframes = hParamMC->nb_subframes;
1431 448549 : mvs2s( hParamMC->subframe_nbslots, st_ivas->hTcBuffer->subframe_nbslots, hParamMC->nb_subframes );
1432 :
1433 448549 : ivas_param_mc_dec_compute_interpolator( hParamMC->hMetadataPMC->bAttackPresent, hParamMC->hMetadataPMC->attackIndex, nCldfbSlots, hParamMC->h_output_synthesis_params.interpolator );
1434 :
1435 : /* loop over two bands at a time */
1436 3438970 : for ( param_band_idx = 0; param_band_idx < hParamMC->num_param_bands_synth; param_band_idx += 2 )
1437 : {
1438 : /* don't process next band if it exceeds the limit */
1439 2990421 : skip_next_band = ( ( param_band_idx + 1 ) == hParamMC->num_param_bands_synth ) ? 1 : 0;
1440 :
1441 2990421 : set_zero( cx, PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS );
1442 2990421 : set_zero( cx_imag, PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS );
1443 2990421 : set_zero( cx_next_band, PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS );
1444 2990421 : set_zero( cx_imag_next_band, PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS );
1445 :
1446 : /* slot loop for gathering the input data */
1447 50843036 : for ( slot_idx = 0; slot_idx < nCldfbSlots; slot_idx++ )
1448 : {
1449 47852615 : if ( slot_idx >= 2 * hParamMC->hMetadataPMC->attackIndex )
1450 : {
1451 140669391 : for ( is_next_band = 0; is_next_band < 2; is_next_band++ )
1452 : {
1453 93779594 : if ( is_next_band && skip_next_band )
1454 : {
1455 1351119 : continue;
1456 : }
1457 :
1458 92428475 : ivas_dirac_dec_output_synthesis_cov_param_mc_collect_slot( &hParamMC->Cldfb_RealBuffer_tc[slot_idx * hParamMC->num_freq_bands * nchan_transport],
1459 92428475 : &hParamMC->Cldfb_ImagBuffer_tc[slot_idx * hParamMC->num_freq_bands * nchan_transport],
1460 : is_next_band ? cx_next_band : cx,
1461 : is_next_band ? cx_imag_next_band : cx_imag,
1462 92428475 : param_band_idx + is_next_band,
1463 : hParamMC,
1464 : nchan_transport );
1465 : }
1466 : }
1467 : }
1468 :
1469 : /* map from complex input covariance to real values */
1470 8971263 : for ( is_next_band = 0; is_next_band < 2; is_next_band++ )
1471 : {
1472 5980842 : if ( is_next_band && skip_next_band )
1473 : {
1474 86579 : continue;
1475 : }
1476 : /* Cx for transport channels */
1477 5894263 : pCx = is_next_band ? &cx_next_band[0] : &cx[0];
1478 5894263 : pCx_imag = is_next_band ? &cx_imag_next_band[0] : &cx_imag[0];
1479 33967375 : for ( i = 0; i < nchan_transport * nchan_transport; i++ )
1480 : {
1481 28073112 : real_part = pCx[i];
1482 28073112 : imag_part = pCx_imag[i];
1483 :
1484 : /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
1485 28073112 : if ( param_band_idx < hParamMC->max_param_band_abs_cov )
1486 : {
1487 16559474 : pCx[i] = sqrtf( real_part * real_part + imag_part * imag_part );
1488 : }
1489 : else
1490 : {
1491 11513638 : pCx[i] = real_part;
1492 : }
1493 : }
1494 : }
1495 :
1496 : /* 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*/
1497 2990421 : if ( hParamMC->hMetadataPMC->bAttackPresent && ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO ) )
1498 : {
1499 9019 : v_add( cx, cx_next_band, cx, nchan_transport * nchan_transport );
1500 9019 : mvr2r( cx, cx_next_band, nchan_transport * nchan_transport );
1501 : }
1502 :
1503 8971263 : for ( is_next_band = 0; is_next_band < 2; is_next_band++ )
1504 : {
1505 5980842 : if ( is_next_band && skip_next_band )
1506 : {
1507 86579 : continue;
1508 : }
1509 :
1510 : /* generate mixing matrices */
1511 5894263 : ivas_param_mc_get_mixing_matrices( hParamMC,
1512 : hSynthesisOutputSetup,
1513 : is_next_band ? cx_next_band : cx,
1514 5894263 : param_band_idx + is_next_band,
1515 5894263 : hParamMC->h_output_synthesis_cov_state.mixing_matrix,
1516 5894263 : hParamMC->h_output_synthesis_cov_state.mixing_matrix_res,
1517 : nchan_out_transport,
1518 : hParamMC->synthesis_conf,
1519 : nchan_transport,
1520 : nchan_out_cov );
1521 : }
1522 : }
1523 :
1524 448549 : return;
1525 : }
1526 :
1527 :
1528 : /*-------------------------------------------------------------------------
1529 : * ivas_param_mc_dec_render()
1530 : *
1531 : * Parametric MC rendering process
1532 : *------------------------------------------------------------------------*/
1533 :
1534 915179 : void ivas_param_mc_dec_render(
1535 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
1536 : const uint16_t nSamplesAsked, /* i : number of CLDFB slots requested */
1537 : uint16_t *nSamplesRendered, /* o : number of CLDFB slots rendered */
1538 : uint16_t *nSamplesAvailableNext, /* o : number of CLDFB slots still to render */
1539 : float *output_f[] /* o : rendered time signal */
1540 : )
1541 : {
1542 : PARAM_MC_DEC_HANDLE hParamMC;
1543 : int16_t i, ch;
1544 : int16_t subframe_idx;
1545 : int16_t slot_idx, slot_idx_start, slot_idx_start_cldfb_synth, first_sf, last_sf, slots_to_render;
1546 : int16_t nchan_transport, nchan_out_transport, nchan_out_cldfb;
1547 : int16_t nchan_out_cov;
1548 : /*CLDFB*/
1549 : float Cldfb_RealBuffer[MAX_INTERN_CHANNELS][PARAM_MC_MAX_NSLOTS_IN_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
1550 : float Cldfb_ImagBuffer[MAX_INTERN_CHANNELS][PARAM_MC_MAX_NSLOTS_IN_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
1551 : float Cldfb_RealBuffer_Binaural[MAX_HEAD_ROT_POSES][BINAURAL_CHANNELS][PARAM_MC_MAX_NSLOTS_IN_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
1552 : float Cldfb_ImagBuffer_Binaural[MAX_HEAD_ROT_POSES][BINAURAL_CHANNELS][PARAM_MC_MAX_NSLOTS_IN_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
1553 : /*Decorrelator*/
1554 : float onset_filter[MAX_LS_CHANNELS * CLDFB_NO_CHANNELS_MAX];
1555 : /* format converter */
1556 : int16_t channel_active[MAX_OUTPUT_CHANNELS];
1557 : uint16_t nband_synth, nbands_to_zero;
1558 : uint16_t nchan_out_init;
1559 : uint32_t output_Fs;
1560 :
1561 915179 : hParamMC = st_ivas->hParamMC;
1562 915179 : assert( hParamMC );
1563 :
1564 915179 : push_wmops( "param_mc_dec_render" );
1565 :
1566 915179 : set_s( channel_active, 0, MAX_LS_CHANNELS );
1567 915179 : nchan_transport = st_ivas->nchan_transport;
1568 915179 : nchan_out_transport = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
1569 915179 : nchan_out_init = nchan_out_transport;
1570 915179 : output_Fs = st_ivas->hDecoderConfig->output_Fs;
1571 :
1572 915179 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1573 : {
1574 678842 : nchan_out_cldfb = BINAURAL_CHANNELS;
1575 678842 : set_s( channel_active, 1, nchan_out_cldfb );
1576 678842 : if ( st_ivas->hCombinedOrientationData )
1577 : {
1578 444990 : nchan_out_init = MAX_INTERN_CHANNELS;
1579 : }
1580 678842 : nchan_out_cov = st_ivas->hTransSetup.nchan_out_woLFE + st_ivas->hTransSetup.num_lfe;
1581 : }
1582 236337 : else if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_CLDFB )
1583 : {
1584 33011 : nchan_out_cov = nchan_out_transport;
1585 33011 : nchan_out_cldfb = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
1586 : }
1587 203326 : else if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_COV || hParamMC->synthesis_conf == PARAM_MC_SYNTH_MONO_STEREO )
1588 : {
1589 62378 : nchan_out_cov = st_ivas->hOutSetup.nchan_out_woLFE + st_ivas->hOutSetup.num_lfe;
1590 62378 : nchan_out_cldfb = nchan_out_cov;
1591 62378 : set_s( channel_active, 1, nchan_out_cov );
1592 : }
1593 : else
1594 : {
1595 140948 : nchan_out_cov = nchan_out_transport;
1596 140948 : nchan_out_cldfb = nchan_out_transport;
1597 140948 : set_s( channel_active, 1, nchan_out_cov );
1598 : }
1599 :
1600 : /* set everything to zero that will not be decoded */
1601 915179 : nband_synth = hParamMC->band_grouping[hParamMC->num_param_bands_synth];
1602 915179 : nbands_to_zero = hParamMC->num_freq_bands - nband_synth;
1603 11266397 : for ( ch = 0; ch < nchan_out_init; ch++ )
1604 : {
1605 51756090 : for ( slot_idx = 0; slot_idx < JBM_CLDFB_SLOTS_IN_SUBFRAME; slot_idx++ )
1606 : {
1607 41404872 : set_zero( &( Cldfb_RealBuffer[ch][slot_idx][nband_synth] ), nbands_to_zero );
1608 41404872 : set_zero( &( Cldfb_ImagBuffer[ch][slot_idx][nband_synth] ), nbands_to_zero );
1609 : }
1610 : }
1611 :
1612 : /* loop for synthesis, assume we always have to render in multiples of 5ms subframes with spills */
1613 915179 : slots_to_render = min( hParamMC->num_slots - hParamMC->slots_rendered, nSamplesAsked / NS2SA( output_Fs, CLDFB_SLOT_NS ) );
1614 915179 : *nSamplesRendered = slots_to_render * NS2SA( output_Fs, CLDFB_SLOT_NS );
1615 915179 : first_sf = hParamMC->subframes_rendered;
1616 915179 : last_sf = first_sf;
1617 2720579 : while ( slots_to_render > 0 )
1618 : {
1619 1805400 : slots_to_render -= hParamMC->subframe_nbslots[last_sf];
1620 1805400 : last_sf++;
1621 : }
1622 : #ifdef DEBUGGING
1623 : assert( slots_to_render == 0 );
1624 : #endif
1625 915179 : if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
1626 : {
1627 139675 : for ( subframe_idx = first_sf; subframe_idx < last_sf; subframe_idx++ )
1628 : {
1629 99276 : slots_to_render += hParamMC->subframe_nbslots[subframe_idx];
1630 : }
1631 : }
1632 915179 : slot_idx_start = hParamMC->slots_rendered;
1633 915179 : slot_idx_start_cldfb_synth = 0;
1634 2720579 : for ( subframe_idx = first_sf; subframe_idx < last_sf; subframe_idx++ )
1635 : {
1636 8983035 : for ( slot_idx = 0; slot_idx < hParamMC->subframe_nbslots[subframe_idx]; slot_idx++, hParamMC->slots_rendered++ )
1637 : {
1638 :
1639 7177635 : if ( hParamMC->max_band_decorr > 0 )
1640 : {
1641 : /*-----------------------------------------------------------------*
1642 : * protoype signal computation
1643 : *-----------------------------------------------------------------*/
1644 :
1645 7177635 : param_mc_protoSignalComputation( &hParamMC->Cldfb_RealBuffer_tc[hParamMC->slots_rendered * nchan_transport * hParamMC->num_freq_bands],
1646 7177635 : &hParamMC->Cldfb_ImagBuffer_tc[hParamMC->slots_rendered * nchan_transport * hParamMC->num_freq_bands],
1647 7177635 : hParamMC->proto_frame_f, hParamMC->diff_proto_info,
1648 7177635 : hParamMC->num_freq_bands );
1649 :
1650 : /*-----------------------------------------------------------------*
1651 : * frequency domain decorrelation
1652 : *-----------------------------------------------------------------*/
1653 :
1654 : /* decorrelate prototype frame */
1655 7177635 : ivas_dirac_dec_decorr_process( hParamMC->num_freq_bands,
1656 7177635 : hParamMC->num_outputs_diff,
1657 7177635 : hParamMC->diff_proto_info->num_protos_diff,
1658 : DIRAC_SYNTHESIS_COV_MC_LS,
1659 : nchan_transport,
1660 7177635 : hParamMC->proto_frame_f,
1661 7177635 : hParamMC->diff_proto_info->num_protos_diff,
1662 7177635 : hParamMC->diff_proto_info->proto_index_diff,
1663 : hParamMC->proto_frame_dec_f,
1664 : onset_filter,
1665 : hParamMC->h_freq_domain_decorr_ap_params,
1666 : hParamMC->h_freq_domain_decorr_ap_state );
1667 :
1668 : /* copy decorrelated frame directly to output CLDFB buffer, acts also as intermediate */
1669 : /* memory for the decorrelated signal */
1670 7177635 : ivas_param_mc_dec_copy_diffuse_proto( hParamMC, Cldfb_RealBuffer, Cldfb_ImagBuffer, nchan_out_cov, slot_idx );
1671 : }
1672 :
1673 : /*-----------------------------------------------------------------*
1674 : * output synthesis
1675 : *-----------------------------------------------------------------*/
1676 :
1677 7177635 : ivas_dirac_dec_output_synthesis_cov_param_mc_synthesise_slot( &hParamMC->Cldfb_RealBuffer_tc[hParamMC->slots_rendered * nchan_transport * hParamMC->num_freq_bands],
1678 7177635 : &hParamMC->Cldfb_ImagBuffer_tc[hParamMC->slots_rendered * nchan_transport * hParamMC->num_freq_bands],
1679 : Cldfb_RealBuffer, Cldfb_ImagBuffer,
1680 7177635 : hParamMC->h_output_synthesis_cov_state.mixing_matrix, hParamMC->h_output_synthesis_cov_state.mixing_matrix_res, slot_idx, slot_idx + slot_idx_start,
1681 : nchan_transport, nchan_out_cov, hParamMC );
1682 :
1683 7177635 : if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ) )
1684 : {
1685 4945555 : if (
1686 4945555 : st_ivas->hCombinedOrientationData && st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV )
1687 : {
1688 2227987 : ivas_param_mc_mc2sba_cldfb( st_ivas->hTransSetup, hParamMC->hoa_encoder, slot_idx, Cldfb_RealBuffer, Cldfb_ImagBuffer, nband_synth, GAIN_LFE );
1689 : }
1690 : else
1691 : {
1692 : /* remove LFE */
1693 : uint16_t idx_out;
1694 : uint16_t idx_lfe;
1695 : IVAS_OUTPUT_SETUP hLsSetup;
1696 :
1697 2717568 : hLsSetup = st_ivas->hTransSetup;
1698 :
1699 : /* If LFE should be rendered, add it to other channels before removing */
1700 2717568 : if ( st_ivas->hBinRenderer->render_lfe )
1701 : {
1702 5435136 : for ( idx_lfe = 0; idx_lfe < hLsSetup.num_lfe; idx_lfe++ )
1703 : {
1704 : /* Copy just the first band of LFE*/
1705 2717568 : 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 );
1706 2717568 : 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 );
1707 :
1708 20126496 : for ( ch = 0; ch < ( hLsSetup.nchan_out_woLFE + hLsSetup.num_lfe ); ch++ )
1709 : {
1710 17408928 : if ( hLsSetup.index_lfe[idx_lfe] != ch )
1711 : {
1712 14691360 : v_add( Cldfb_RealBuffer[ch][slot_idx], Cldfb_RealBuffer[hLsSetup.index_lfe[idx_lfe]][slot_idx], Cldfb_RealBuffer[ch][slot_idx], 1 );
1713 14691360 : v_add( Cldfb_ImagBuffer[ch][slot_idx], Cldfb_ImagBuffer[hLsSetup.index_lfe[idx_lfe]][slot_idx], Cldfb_ImagBuffer[ch][slot_idx], 1 );
1714 : }
1715 : }
1716 : }
1717 : }
1718 :
1719 2717568 : idx_out = 0;
1720 2717568 : idx_lfe = 0;
1721 :
1722 20126496 : for ( ch = 0; ch < ( hLsSetup.nchan_out_woLFE + hLsSetup.num_lfe ); ch++ )
1723 : {
1724 17408928 : if ( ( hLsSetup.num_lfe > 0 ) && ( hLsSetup.index_lfe[idx_lfe] == ch ) )
1725 : {
1726 2717568 : if ( idx_lfe < ( hLsSetup.num_lfe - 1 ) )
1727 : {
1728 0 : idx_lfe++;
1729 : }
1730 : }
1731 14691360 : else if ( ch != idx_out )
1732 : {
1733 6538656 : mvr2r( Cldfb_RealBuffer[ch][slot_idx], Cldfb_RealBuffer[idx_out][slot_idx], nband_synth );
1734 6538656 : mvr2r( Cldfb_ImagBuffer[ch][slot_idx], Cldfb_ImagBuffer[idx_out][slot_idx], nband_synth );
1735 6538656 : idx_out++;
1736 : }
1737 : else
1738 : {
1739 8152704 : idx_out++;
1740 : }
1741 : }
1742 : }
1743 : }
1744 : }
1745 :
1746 1805400 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1747 : {
1748 1247380 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
1749 : {
1750 49946 : if ( st_ivas->hSplitBinRend->hCldfbDataOut != NULL )
1751 : {
1752 73450 : for ( slot_idx = 0; slot_idx < MAX_PARAM_SPATIAL_SUBFRAMES; slot_idx++ )
1753 : {
1754 998920 : for ( ch = 0; ch < ( st_ivas->hIntSetup.nchan_out_woLFE + st_ivas->hIntSetup.num_lfe ); ch++ )
1755 : {
1756 940160 : mvr2r( Cldfb_RealBuffer[ch][slot_idx], st_ivas->hSplitBinRend->hCldfbDataOut->Cldfb_RealBuffer[ch][slot_idx_start + slot_idx], hParamMC->num_freq_bands );
1757 940160 : mvr2r( Cldfb_ImagBuffer[ch][slot_idx], st_ivas->hSplitBinRend->hCldfbDataOut->Cldfb_ImagBuffer[ch][slot_idx_start + slot_idx], hParamMC->num_freq_bands );
1758 : }
1759 : }
1760 14690 : st_ivas->hSplitBinRend->hCldfbDataOut->config = st_ivas->hIntSetup.output_config;
1761 : }
1762 : }
1763 :
1764 1247380 : ivas_binRenderer( st_ivas->hBinRenderer,
1765 49946 : ( st_ivas->hSplitBinRend == NULL ) ? NULL : &st_ivas->hSplitBinRend->splitrend.multiBinPoseData,
1766 : #ifdef SPLIT_REND_WITH_HEAD_ROT_DEBUG
1767 : NULL,
1768 : #endif
1769 : st_ivas->hCombinedOrientationData,
1770 1247380 : hParamMC->subframe_nbslots[subframe_idx],
1771 : Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural, Cldfb_RealBuffer, Cldfb_ImagBuffer );
1772 :
1773 1247380 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
1774 : {
1775 : int16_t pos_idx;
1776 252668 : for ( pos_idx = 0; pos_idx < st_ivas->hBinRenderer->numPoses; pos_idx++ )
1777 : {
1778 1006503 : for ( slot_idx = 0; slot_idx < hParamMC->subframe_nbslots[subframe_idx]; slot_idx++ )
1779 : {
1780 2411343 : for ( ch = 0; ch < nchan_out_cldfb; ch++ )
1781 : {
1782 1607562 : ivas_CLDFB_RINGBUF_Push(
1783 1607562 : st_ivas->hSplitBinRend->hMultiBinCldfbData[pos_idx * BINAURAL_CHANNELS + ch],
1784 1607562 : Cldfb_RealBuffer_Binaural[pos_idx][ch][slot_idx],
1785 1607562 : Cldfb_ImagBuffer_Binaural[pos_idx][ch][slot_idx],
1786 1607562 : hParamMC->num_freq_bands );
1787 : }
1788 : }
1789 : }
1790 : }
1791 :
1792 : /* update combined orientation access index */
1793 1247380 : ivas_combined_orientation_update_index( st_ivas->hCombinedOrientationData, hParamMC->num_freq_bands * hParamMC->subframe_nbslots[subframe_idx] );
1794 : }
1795 558020 : else if ( hParamMC->synthesis_conf == PARAM_MC_SYNTH_LS_CONV_CLDFB )
1796 : {
1797 : /* format conversion*/
1798 83164 : ivas_lssetupconversion_process_param_mc( st_ivas, hParamMC->subframe_nbslots[subframe_idx], Cldfb_RealBuffer, Cldfb_ImagBuffer, channel_active );
1799 : }
1800 :
1801 : /* CLDFB synthesis */
1802 8610128 : for ( ch = 0; ch < nchan_out_cldfb; ch++ )
1803 : {
1804 : float *RealBuffer[16];
1805 : float *ImagBuffer[16];
1806 :
1807 6804728 : if ( channel_active[ch] )
1808 : {
1809 : /* open CLDFB buffer up to CLDFB_NO_CHANNELS_MAX bands for 48kHz */
1810 32094190 : for ( i = 0; i < hParamMC->subframe_nbslots[subframe_idx]; i++ )
1811 : {
1812 25657766 : if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV || st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM )
1813 : {
1814 9891110 : RealBuffer[i] = Cldfb_RealBuffer_Binaural[0][ch][i];
1815 9891110 : ImagBuffer[i] = Cldfb_ImagBuffer_Binaural[0][ch][i];
1816 : }
1817 : else
1818 : {
1819 15766656 : RealBuffer[i] = Cldfb_RealBuffer[ch][i];
1820 15766656 : ImagBuffer[i] = Cldfb_ImagBuffer[ch][i];
1821 : }
1822 : }
1823 :
1824 6436424 : cldfbSynthesis( RealBuffer, ImagBuffer, &( output_f[ch][slot_idx_start_cldfb_synth * hParamMC->num_freq_bands] ),
1825 6436424 : hParamMC->num_freq_bands * hParamMC->subframe_nbslots[subframe_idx], st_ivas->cldfbSynDec[ch] );
1826 : }
1827 : else
1828 : {
1829 368304 : 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] );
1830 : }
1831 : }
1832 1805400 : slot_idx_start += hParamMC->subframe_nbslots[subframe_idx];
1833 1805400 : slot_idx_start_cldfb_synth += hParamMC->subframe_nbslots[subframe_idx];
1834 : }
1835 :
1836 915179 : if ( st_ivas->renderer_type == RENDERER_SBA_LINEAR_ENC )
1837 : {
1838 40399 : ivas_mc2sba( st_ivas->hIntSetup, output_f, output_f, hParamMC->num_freq_bands * slots_to_render, st_ivas->hOutSetup.ambisonics_order, 0.f );
1839 : }
1840 :
1841 : /* update */
1842 915179 : if ( hParamMC->slots_rendered == hParamMC->num_slots )
1843 : {
1844 448549 : hParamMC->hMetadataPMC->last_coded_bwidth = hParamMC->hMetadataPMC->coded_bwidth;
1845 448549 : 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 );
1846 : }
1847 915179 : hParamMC->subframes_rendered = last_sf;
1848 915179 : *nSamplesAvailableNext = ( hParamMC->num_slots - hParamMC->slots_rendered ) * NS2SA( output_Fs, CLDFB_SLOT_NS );
1849 915179 : pop_wmops();
1850 :
1851 915179 : return;
1852 : }
1853 :
1854 :
1855 : /*-------------------------------------------------------------------------
1856 : * param_mc_dec_init()
1857 : *
1858 : * Parametric MC decoding initialization
1859 : *------------------------------------------------------------------------*/
1860 :
1861 13330 : static void ivas_param_mc_dec_init(
1862 : PARAM_MC_DEC_HANDLE hParamMC, /* i/o: decoder DirAC handle */
1863 : const int16_t nchan_transport, /* i : number of input (transport) channels */
1864 : const int16_t nchan_cov ) /* i : number of cov synthesis channels */
1865 : {
1866 : int16_t k;
1867 : uint16_t max_param_band_residual;
1868 : int16_t len;
1869 :
1870 : /*-----------------------------------------------------------------*
1871 : * init sub-modules
1872 : *-----------------------------------------------------------------*/
1873 :
1874 : /* decorrelation */
1875 13330 : if ( hParamMC->max_band_decorr > 0 )
1876 : {
1877 12908 : len = hParamMC->diff_proto_info->num_protos_diff * hParamMC->h_freq_domain_decorr_ap_params->h_onset_detection_power_params.max_band_decorr;
1878 :
1879 : /* init onsetDetectionPower */
1880 12908 : set_zero( hParamMC->h_freq_domain_decorr_ap_state->h_onset_detection_power_state.onset_detector_1, len );
1881 12908 : set_zero( hParamMC->h_freq_domain_decorr_ap_state->h_onset_detection_power_state.onset_detector_2, len );
1882 : }
1883 :
1884 13330 : max_param_band_residual = 0;
1885 :
1886 : /* output synthesis */
1887 56469 : for ( k = hParamMC->hMetadataPMC->num_parameter_bands; k >= 0; k-- )
1888 : {
1889 56469 : if ( hParamMC->band_grouping[k] <= hParamMC->max_band_decorr )
1890 : {
1891 13330 : max_param_band_residual = k;
1892 : #ifdef DEBUGGING
1893 : assert( hParamMC->band_grouping[k] == hParamMC->max_band_decorr );
1894 : #endif
1895 13330 : break;
1896 : }
1897 : }
1898 :
1899 13330 : 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 );
1900 :
1901 : /*-----------------------------------------------------------------*
1902 : * init proto frames
1903 : *-----------------------------------------------------------------*/
1904 :
1905 13330 : if ( hParamMC->max_band_decorr > 0 )
1906 : {
1907 12908 : set_zero( hParamMC->proto_frame_f, 2 * hParamMC->diff_proto_info->num_protos_diff * hParamMC->num_freq_bands );
1908 12908 : set_zero( hParamMC->proto_frame_dec_f, 2 * nchan_cov * hParamMC->num_freq_bands );
1909 : }
1910 :
1911 13330 : return;
1912 : }
1913 :
1914 :
1915 : /*-------------------------------------------------------------------------
1916 : * Local functions
1917 : *-------------------------------------------------------------------------*/
1918 :
1919 :
1920 : /*-------------------------------------------------------------------------
1921 : * ivas_param_mc_dec_compute_diffuse_proto()
1922 : *
1923 : * Compute prototypes for decorrelation
1924 : *------------------------------------------------------------------------*/
1925 :
1926 7177635 : static void param_mc_protoSignalComputation(
1927 : float *RealBuffer, /* i : CLDFB samples of the transport channels (real part) */
1928 : float *ImagBuffer, /* i : CLDFB samples of the transport channels (imaginary part) */
1929 : float *proto_frame_f, /* o : interleaved complex prototype CLDFB samples */
1930 : const PARAM_MC_DIFF_PROTO_INFO *diff_proto_info, /* i : prototype generation information */
1931 : const int16_t num_freq_bands /* i : number of frequency bands for the prototypes */
1932 : )
1933 : {
1934 : int16_t band;
1935 : int16_t proto_ch_idx, source_ch_cnt;
1936 : float *p_proto_frame;
1937 : float *p_real_buffer;
1938 : float *p_imag_buffer;
1939 :
1940 7177635 : set_zero( proto_frame_f, 2 * num_freq_bands * diff_proto_info->num_protos_diff );
1941 :
1942 29666508 : for ( proto_ch_idx = 0; proto_ch_idx < diff_proto_info->num_protos_diff; proto_ch_idx++ )
1943 : {
1944 22488873 : int16_t num_source_ch = diff_proto_info->num_source_chan_diff[proto_ch_idx];
1945 :
1946 51202333 : for ( source_ch_cnt = 0; source_ch_cnt < num_source_ch; source_ch_cnt++ )
1947 : {
1948 28713460 : float fac = diff_proto_info->proto_fac[proto_ch_idx][source_ch_cnt];
1949 28713460 : int16_t source_ch_idx = diff_proto_info->source_chan_idx[proto_ch_idx][source_ch_cnt];
1950 :
1951 28713460 : p_proto_frame = &proto_frame_f[proto_ch_idx * num_freq_bands * 2];
1952 28713460 : p_real_buffer = &RealBuffer[source_ch_idx * num_freq_bands];
1953 28713460 : p_imag_buffer = &ImagBuffer[source_ch_idx * num_freq_bands];
1954 :
1955 1570481780 : for ( band = 0; band < num_freq_bands; band++ )
1956 : {
1957 1541768320 : *( p_proto_frame++ ) += fac * ( *( p_real_buffer++ ) );
1958 1541768320 : *( p_proto_frame++ ) += fac * ( *( p_imag_buffer++ ) );
1959 : }
1960 : }
1961 : }
1962 :
1963 7177635 : return;
1964 : }
1965 :
1966 :
1967 : /*-------------------------------------------------------------------------
1968 : * ivas_param_mc_dec_compute_diffuse_proto()
1969 : *
1970 : * Transfer decorrelated signals back from the decorrelator buffer to
1971 : * the buffers used in the final synthesis
1972 : *------------------------------------------------------------------------*/
1973 :
1974 7177635 : static void ivas_param_mc_dec_copy_diffuse_proto(
1975 : PARAM_MC_DEC_HANDLE hParamMC, /* i : Parametric MC handle */
1976 : 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) */
1977 : 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) */
1978 : const int16_t nY, /* i : number of decorrelated channels */
1979 : const int16_t slot_idx /* i : current time slot index */
1980 : )
1981 : {
1982 : int16_t k, l;
1983 : int16_t num_freq_bands, num_freq_bands_diff;
1984 : float *p_proto_diff;
1985 :
1986 7177635 : num_freq_bands = hParamMC->num_freq_bands;
1987 7177635 : num_freq_bands_diff = hParamMC->h_output_synthesis_params.max_band_decorr;
1988 :
1989 56518331 : for ( k = 0; k < nY; k++ )
1990 : {
1991 49340696 : p_proto_diff = hParamMC->proto_frame_dec_f + k * 2 * num_freq_bands;
1992 1036154616 : for ( l = 0; l < num_freq_bands_diff; l++ )
1993 : {
1994 986813920 : Cldfb_buffer_real[k][slot_idx][l] = *( p_proto_diff++ );
1995 986813920 : Cldfb_buffer_imag[k][slot_idx][l] = *( p_proto_diff++ );
1996 : }
1997 : }
1998 :
1999 7177635 : return;
2000 : }
2001 :
2002 :
2003 : /*-------------------------------------------------------------------------
2004 : * ivas_param_mc_bin2dec()
2005 : *
2006 : * decode a number of bits to an integer
2007 : *------------------------------------------------------------------------*/
2008 : /* r : decoded integer */
2009 1431936 : static int16_t ivas_param_mc_bin2dec(
2010 : uint16_t bits[PARAM_MC_MAX_BITS], /* i : bit buffer */
2011 : const int16_t N /* i : number of bits to decode */
2012 : )
2013 : {
2014 : int16_t i;
2015 : int16_t out;
2016 :
2017 1431936 : assert( N <= 16 );
2018 1431936 : out = 0;
2019 5727852 : for ( i = 0; i < N; i++ )
2020 : {
2021 4295916 : out += ( bits[i] << ( N - 1 - i ) );
2022 : }
2023 :
2024 1431936 : return out;
2025 : }
2026 :
2027 :
2028 : /*-------------------------------------------------------------------------
2029 : * ivas_param_mc_uniform_decoder()
2030 : *
2031 : * decode a uniformily coded sequence of float values
2032 : *------------------------------------------------------------------------*/
2033 :
2034 45565 : static int16_t ivas_param_mc_uniform_decoder(
2035 : float *seq, /* o : decoded sequence of float values */
2036 : const int16_t sz_seq, /* i : number of values to decode */
2037 : const float *alphabet, /* i : codebook */
2038 : const int16_t N, /* i : number of bits per coded index */
2039 : uint16_t bit_buffer[PARAM_MC_MAX_BITS] /* i : bit buffer to decode */
2040 : )
2041 : {
2042 : int16_t i;
2043 : int16_t idx;
2044 : int16_t n_bits;
2045 :
2046 45565 : n_bits = 0;
2047 45565 : assert( N * sz_seq < PARAM_MC_MAX_BITS );
2048 :
2049 1477501 : for ( i = 0; i < sz_seq; ++i )
2050 : {
2051 1431936 : idx = ivas_param_mc_bin2dec( &bit_buffer[i * N], N );
2052 1431936 : seq[i] = alphabet[idx];
2053 : }
2054 :
2055 45565 : n_bits = N * sz_seq;
2056 :
2057 45565 : return n_bits;
2058 : }
2059 :
2060 :
2061 : /*-------------------------------------------------------------------------
2062 : * ivas_param_mc_range_decoder_LC()
2063 : *
2064 : * decode a sequency of inidices coded with a range coder
2065 : *------------------------------------------------------------------------*/
2066 :
2067 854033 : static int16_t ivas_param_mc_range_decoder_LC(
2068 : uint16_t *bit_buffer, /* i : bit buffer to read from */
2069 : int16_t *x, /* o : decoded indices */
2070 : int16_t *BER_detect, /* o : flag for indicating a bit error */
2071 : const int16_t sz_seq, /* i : size of the sequence to be decoded */
2072 : const int16_t sz_alphabet, /* i : size of the alphabet */
2073 : const uint16_t *cft, /* i : cumulative frequency table */
2074 : const uint16_t *sft, /* i : symbol frequency table */
2075 : const int16_t tot_shift, /* i : total frequency as a power of 2 */
2076 : const int16_t nbbits /* i : maximum bit budget */
2077 : )
2078 : {
2079 : RangeUniDecState rc_st_dec; /* State of the range decoder */
2080 : int16_t cur_bit_pos;
2081 : int16_t k;
2082 : int16_t r;
2083 :
2084 : /* Start Decoding */
2085 : /* Initialize range decoder */
2086 854033 : cur_bit_pos = 0;
2087 854033 : rc_uni_dec_init( &rc_st_dec, bit_buffer, nbbits - 32 ); /* (nbbits + 30) entries are read by the decoder */
2088 :
2089 : /* Main Loop through the indices */
2090 33223573 : for ( k = 0; k < sz_seq; k++ )
2091 : {
2092 32369540 : 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) */
2093 : /* r is the symbol read, the possible values are {0,1,....alphabet_size - 1} */
2094 :
2095 : /* Update bitstream pointer */
2096 32369540 : cur_bit_pos = rc_uni_dec_virtual_finish( &rc_st_dec );
2097 :
2098 : /* Confirm that there is no overflow */
2099 32369540 : if ( cur_bit_pos > nbbits )
2100 : {
2101 0 : *BER_detect |= 1;
2102 : }
2103 :
2104 32369540 : x[k] = r;
2105 : }
2106 :
2107 : /* We don't need to finish because virtual_finish() already does the same */
2108 : /*st->next_bit_pos = rc_uni_dec_finish(&rc_st_dec);*/
2109 :
2110 : /* Check for bitstream errors */
2111 854033 : if ( rc_st_dec.bit_error_detected != 0 )
2112 : {
2113 0 : *BER_detect |= 1;
2114 : }
2115 :
2116 854033 : return cur_bit_pos;
2117 : }
2118 :
2119 :
2120 : /*-------------------------------------------------------------------------
2121 : * param_mc_compute_interpolator()
2122 : *
2123 : * compute the interpolator used in the final synthesis
2124 : *------------------------------------------------------------------------*/
2125 :
2126 464526 : static void ivas_param_mc_dec_compute_interpolator(
2127 : const uint16_t bAttackPresent, /* i : flag indicating if we have a transient in the current frame */
2128 : const uint16_t attackPos, /* i : position of the transient */
2129 : const uint16_t interp_length, /* i : number of interpolation values to be calculated */
2130 : float *interpolator /* o : interpolator */
2131 : )
2132 : {
2133 : int16_t idx;
2134 :
2135 464526 : if ( bAttackPresent )
2136 : {
2137 172044 : for ( idx = 0; idx < 2 * attackPos; idx++ )
2138 : {
2139 146778 : interpolator[idx] = 0.0f;
2140 : }
2141 282829 : for ( ; idx < interp_length; idx++ )
2142 : {
2143 257563 : interpolator[idx] = 1.0f;
2144 : }
2145 : }
2146 : else
2147 : {
2148 439260 : ivas_jbm_dec_get_adapted_linear_interpolator( DEFAULT_JBM_CLDFB_TIMESLOTS, interp_length, interpolator );
2149 : }
2150 :
2151 464526 : return;
2152 : }
2153 :
2154 :
2155 : /*-------------------------------------------------------------------------
2156 : * remove_lfe_from_cy()
2157 : *
2158 : * remove all LFE related values from a covariance matrix
2159 : *------------------------------------------------------------------------*/
2160 :
2161 5501295 : static void remove_lfe_from_cy(
2162 : const int16_t nY, /* i : dimension of the covariance matrix */
2163 : int16_t lfe_indices[PARAM_MC_LOCAL_SZ_LFE_MAP], /* i : LFE index */
2164 : int16_t num_lfe, /* i : number of LFEs */
2165 : float *cy, /* i : covariance matrix */
2166 : float *cy_woLFE ) /* o : covariance matrix with LFE removed */
2167 : {
2168 : int16_t ch_idx1, ch_idx2;
2169 : int16_t lfe_idx1, lfe_idx2;
2170 : float *ptrCy;
2171 : float *ptrCy_out;
2172 :
2173 5501295 : ptrCy = cy;
2174 5501295 : ptrCy_out = cy_woLFE;
2175 :
2176 16503885 : for ( lfe_idx1 = 0; lfe_idx1 < num_lfe + 1; lfe_idx1++ )
2177 : {
2178 43500969 : for ( ch_idx1 = lfe_indices[lfe_idx1] + 1; ch_idx1 < lfe_indices[lfe_idx1 + 1]; ch_idx1++ )
2179 : {
2180 97495137 : for ( lfe_idx2 = 0; lfe_idx2 < num_lfe + 1; lfe_idx2++ )
2181 : {
2182 271601941 : for ( ch_idx2 = lfe_indices[lfe_idx2] + 1; ch_idx2 < lfe_indices[lfe_idx2 + 1]; ch_idx2++ )
2183 : {
2184 206605183 : *( ptrCy_out++ ) = *( ptrCy++ );
2185 : }
2186 64996758 : ptrCy++;
2187 : }
2188 32498379 : ptrCy--;
2189 : }
2190 11002590 : ptrCy += nY;
2191 : }
2192 :
2193 5501295 : return;
2194 : }
2195 :
2196 :
2197 : /*-------------------------------------------------------------------------
2198 : * ivas_param_mc_get_mixing_matrices()
2199 : *
2200 : * calculate the direct and residual mixing matrices
2201 : * using the covariance method
2202 : *------------------------------------------------------------------------*/
2203 :
2204 5894263 : static void ivas_param_mc_get_mixing_matrices(
2205 : PARAM_MC_DEC_HANDLE hParamMC, /* i : Parametric MC handle */
2206 : IVAS_OUTPUT_SETUP *hSynthesisOutputSetup,
2207 : float Cx_in[PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS], /* i : input covariance for all parameter bands */
2208 : const int16_t param_band_idx, /* i : parameter band index */
2209 : float *mixing_matrix[], /* o : direct mixing matrices for all parameter bands */
2210 : float *mixing_matrix_res[], /* o : residual mixing matrices for all parameter bands */
2211 : const int16_t nY_intern, /* i : number of channels in the transported format */
2212 : const PARAM_MC_SYNTHESIS_CONF synth_config, /* i : Parametric MC synthesis config */
2213 : const int16_t nX, /* i : number of transport channels */
2214 : const int16_t nY_cov /* i : number of covariance synthesis output channels */
2215 : )
2216 : {
2217 : float Cx[PARAM_MC_MAX_TRANSPORT_CHANS * PARAM_MC_MAX_TRANSPORT_CHANS];
2218 : float Cy[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2219 : float Cy_diag[MAX_LS_CHANNELS];
2220 : float Cr[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2221 : float Cproto_diag[MAX_LS_CHANNELS];
2222 : float Cproto[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2223 : float mat_mult_buffer1[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2224 : float *Cx_state;
2225 : float *Cx_old_state;
2226 : float Cy_state[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2227 : float *Cy_old_state;
2228 : int16_t nY_band;
2229 : float proto_matrix_noLFE[PARAM_MC_MAX_TRANSPORT_CHANS * MAX_LS_CHANNELS];
2230 : float *proto_matrix_ptr;
2231 : int16_t num_lfe_bands;
2232 : int16_t brange[2];
2233 : uint16_t i;
2234 : int16_t ch_idx1, ch_idx2, lfe_idx1, lfe_idx2;
2235 : float *ptrMM;
2236 : float *ptrMM_out;
2237 : float Cy_full[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2238 : float mixing_matrix_local[MAX_LS_CHANNELS * PARAM_MC_MAX_TRANSPORT_CHANS];
2239 : float mixing_matrix_res_local[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2240 : int16_t remove_lfe;
2241 : int16_t lfe_indices[PARAM_MC_LOCAL_SZ_LFE_MAP];
2242 :
2243 5894263 : set_zero( Cproto, MAX_LS_CHANNELS * MAX_LS_CHANNELS );
2244 5894263 : set_zero( mat_mult_buffer1, MAX_LS_CHANNELS * MAX_LS_CHANNELS );
2245 5894263 : set_zero( proto_matrix_noLFE, PARAM_MC_MAX_TRANSPORT_CHANS * MAX_LS_CHANNELS );
2246 :
2247 5894263 : nY_band = nY_cov;
2248 5894263 : num_lfe_bands = 0;
2249 5894263 : remove_lfe = 0;
2250 :
2251 5894263 : set_s( lfe_indices, -1, PARAM_MC_LOCAL_SZ_LFE_MAP );
2252 5894263 : if ( hSynthesisOutputSetup->num_lfe )
2253 : {
2254 : float *proto_matrix_ptr_in;
2255 : #ifdef DEBUGGING
2256 : assert( ( nY_cov == ( hSynthesisOutputSetup->nchan_out_woLFE + hSynthesisOutputSetup->num_lfe ) ) && "Number of channels do not match!" );
2257 : #endif
2258 11788526 : for ( lfe_idx1 = 0; lfe_idx1 < hSynthesisOutputSetup->num_lfe; lfe_idx1++ )
2259 : {
2260 5894263 : lfe_indices[lfe_idx1 + 1] = hSynthesisOutputSetup->index_lfe[lfe_idx1];
2261 : }
2262 5894263 : lfe_indices[hSynthesisOutputSetup->num_lfe + 1] = nY_cov;
2263 5894263 : proto_matrix_ptr = &proto_matrix_noLFE[0];
2264 5894263 : proto_matrix_ptr_in = &hParamMC->h_output_synthesis_params.proto_matrix[0];
2265 5894263 : set_zero( proto_matrix_noLFE, PARAM_MC_MAX_TRANSPORT_CHANS * MAX_LS_CHANNELS );
2266 :
2267 18582001 : for ( ch_idx1 = 0; ch_idx1 < nX; ch_idx1++ )
2268 : {
2269 38063214 : for ( lfe_idx1 = 0; lfe_idx1 < hSynthesisOutputSetup->num_lfe + 1; lfe_idx1++ )
2270 : {
2271 103047438 : for ( ch_idx2 = lfe_indices[lfe_idx1] + 1; ch_idx2 < lfe_indices[lfe_idx1 + 1]; ch_idx2++ )
2272 : {
2273 77671962 : *( proto_matrix_ptr++ ) = *( proto_matrix_ptr_in++ );
2274 : }
2275 25375476 : proto_matrix_ptr_in++;
2276 : }
2277 12687738 : proto_matrix_ptr_in--;
2278 : }
2279 : }
2280 :
2281 5894263 : if ( hParamMC->hMetadataPMC->lfe_on )
2282 : {
2283 5190180 : num_lfe_bands = PARAM_MC_MAX_BAND_LFE;
2284 : }
2285 :
2286 5894263 : if ( hSynthesisOutputSetup->num_lfe > 0 && param_band_idx >= num_lfe_bands )
2287 : {
2288 5501295 : remove_lfe = 1;
2289 5501295 : nY_band = nY_cov - hSynthesisOutputSetup->num_lfe;
2290 5501295 : proto_matrix_ptr = proto_matrix_noLFE;
2291 : }
2292 : else
2293 : {
2294 392968 : proto_matrix_ptr = hParamMC->h_output_synthesis_params.proto_matrix;
2295 : }
2296 :
2297 5894263 : brange[0] = hParamMC->band_grouping[param_band_idx];
2298 5894263 : brange[1] = hParamMC->band_grouping[param_band_idx + 1];
2299 :
2300 5894263 : Cx_state = Cx_in;
2301 5894263 : Cx_old_state = hParamMC->h_output_synthesis_cov_state.cx_old[param_band_idx];
2302 5894263 : Cy_old_state = hParamMC->h_output_synthesis_cov_state.cy_old[param_band_idx];
2303 :
2304 : /* Getting mixing mtx */
2305 : /* estimate target cov from input cov and proto_matrix */
2306 5894263 : matrix_product( hParamMC->proto_matrix_int, nY_intern, nX, 0, Cx_state, nX, nX, 0, mat_mult_buffer1 );
2307 :
2308 5894263 : matrix_product( mat_mult_buffer1, nY_intern, nX, 0, hParamMC->proto_matrix_int, nY_intern, nX, 1, Cproto );
2309 :
2310 47508597 : for ( ch_idx1 = 0; ch_idx1 < nY_intern; ch_idx1++ )
2311 : {
2312 41614334 : if ( Cproto[ch_idx1 + ch_idx1 * nY_intern] < 0.0f )
2313 : {
2314 0 : Cproto[ch_idx1 + ch_idx1 * nY_intern] = 0.0f;
2315 : }
2316 : }
2317 :
2318 5894263 : ivas_param_mc_dequantize_cov( hParamMC,
2319 5894263 : hParamMC->icld_q + param_band_idx * hParamMC->hMetadataPMC->ild_mapping_conf->ild_map_size_lfe,
2320 5894263 : hParamMC->icc_q + param_band_idx * hParamMC->hMetadataPMC->icc_mapping_conf->icc_map_size_lfe,
2321 : param_band_idx, nY_cov,
2322 : synth_config,
2323 : nY_intern,
2324 : nX, Cx_state, Cproto, Cy_state );
2325 :
2326 : /* Smoothing: Sum over two buffers */
2327 5894263 : if ( hParamMC->hMetadataPMC->bAttackPresent )
2328 : {
2329 : /* no smoothing on attacks */
2330 330940 : mvr2r( Cx_state, Cx, nX * nX );
2331 330940 : mvr2r( Cy_state, Cy_full, nY_cov * nY_cov );
2332 : }
2333 : else
2334 : {
2335 : /* smoothing gains are now identical to one, simply add up */
2336 5563323 : v_add( Cx_state, Cx_old_state, Cx, nX * nX );
2337 5563323 : v_add( Cy_state, Cy_old_state, Cy_full, nY_cov * nY_cov );
2338 : }
2339 :
2340 : /* cov buffer update */
2341 5894263 : mvr2r( Cx_state, Cx_old_state, nX * nX );
2342 5894263 : mvr2r( Cy_state, Cy_old_state, nY_cov * nY_cov );
2343 :
2344 : /* remove LFE if necessary */
2345 5894263 : if ( remove_lfe )
2346 : {
2347 5501295 : remove_lfe_from_cy( nY_cov, lfe_indices, hSynthesisOutputSetup->num_lfe, Cy_full, Cy );
2348 : }
2349 : else
2350 : {
2351 392968 : mvr2r( Cy_full, Cy, nY_band * nY_band );
2352 : }
2353 :
2354 5894263 : matrix_product( proto_matrix_ptr, nY_band, nX, 0, Cx, nX, nX, 0, mat_mult_buffer1 );
2355 :
2356 5894263 : matrix_product_diag( mat_mult_buffer1, nY_band, nX, 0, proto_matrix_ptr, nY_band, nX, 1, Cproto_diag );
2357 :
2358 : /* make sure we have no negative entries in Cproto_diag due to rounding errors */
2359 41127666 : for ( ch_idx1 = 0; ch_idx1 < nY_band; ch_idx1++ )
2360 : {
2361 35233403 : if ( Cproto_diag[ch_idx1] < 0.0f )
2362 : {
2363 0 : Cproto_diag[ch_idx1] = 0.0f;
2364 : }
2365 : }
2366 :
2367 : /* Computing the mixing matrices */
2368 :
2369 : /* bands with decorr */
2370 5894263 : if ( brange[0] < hParamMC->h_output_synthesis_params.max_band_decorr )
2371 : {
2372 4779950 : computeMixingMatrices( nX, nY_band, Cx, Cy, proto_matrix_ptr, 0, PARAM_MC_REG_SX, PARAM_MC_REG_GHAT, mixing_matrix_local, Cr );
2373 :
2374 : /* Compute mixing matrix for residual */
2375 4779950 : computeMixingMatricesResidual( nY_band, Cproto_diag, Cr, PARAM_MC_REG_SX, PARAM_MC_REG_GHAT, mixing_matrix_res_local );
2376 :
2377 4779950 : if ( remove_lfe )
2378 : {
2379 4386982 : set_zero( mixing_matrix_res[param_band_idx], nY_cov * nY_cov );
2380 :
2381 4386982 : ptrMM = mixing_matrix_res_local;
2382 4386982 : ptrMM_out = mixing_matrix_res[param_band_idx];
2383 13160946 : for ( lfe_idx1 = 0; lfe_idx1 < hSynthesisOutputSetup->num_lfe + 1; lfe_idx1++ )
2384 : {
2385 34715484 : for ( ch_idx1 = lfe_indices[lfe_idx1] + 1; ch_idx1 < lfe_indices[lfe_idx1 + 1]; ch_idx1++ )
2386 : {
2387 77824560 : for ( lfe_idx2 = 0; lfe_idx2 < hSynthesisOutputSetup->num_lfe + 1; lfe_idx2++ )
2388 : {
2389 216820798 : for ( ch_idx2 = lfe_indices[lfe_idx2] + 1; ch_idx2 < lfe_indices[lfe_idx2 + 1]; ch_idx2++ )
2390 : {
2391 164937758 : *( ptrMM_out++ ) = *( ptrMM++ );
2392 : }
2393 51883040 : ptrMM_out++;
2394 : }
2395 25941520 : ptrMM_out--;
2396 : }
2397 8773964 : ptrMM_out += nY_cov;
2398 : }
2399 : }
2400 : else
2401 : {
2402 392968 : mvr2r( mixing_matrix_res_local, mixing_matrix_res[param_band_idx], nY_cov * nY_cov );
2403 : }
2404 : }
2405 1114313 : else if ( brange[0] < hParamMC->max_band_energy_compensation )
2406 : {
2407 : /* Compute mixing matrices (energy compensation only) */
2408 1114313 : computeMixingMatrices( nX, nY_band, Cx, Cy, proto_matrix_ptr, 1, PARAM_MC_REG_SX, PARAM_MC_REG_GHAT, mixing_matrix_local, Cr );
2409 : }
2410 : else
2411 : {
2412 : /*if neither decorrelation nor energy compensation is applied*/
2413 0 : for ( i = 0; i < nY_band; i++ )
2414 : {
2415 0 : Cy_diag[i] = Cy[i + nY_band * i];
2416 0 : Cy_diag[i] = sqrtf( Cy_diag[i] / ( Cproto_diag[i] + EPSILON ) );
2417 : }
2418 :
2419 0 : diag_matrix_product( Cy_diag, nY_band, proto_matrix_ptr, nY_band, nX, 0, mixing_matrix_local );
2420 : }
2421 :
2422 5894263 : if ( remove_lfe )
2423 : {
2424 5501295 : set_zero( mixing_matrix[param_band_idx], nX * nY_cov );
2425 :
2426 5501295 : ptrMM = mixing_matrix_local;
2427 5501295 : ptrMM_out = mixing_matrix[param_band_idx];
2428 17346678 : for ( ch_idx1 = 0; ch_idx1 < nX; ch_idx1++ )
2429 : {
2430 35536149 : for ( lfe_idx1 = 0; lfe_idx1 < hSynthesisOutputSetup->num_lfe + 1; lfe_idx1++ )
2431 : {
2432 96171659 : for ( ch_idx2 = lfe_indices[lfe_idx1] + 1; ch_idx2 < lfe_indices[lfe_idx1 + 1]; ch_idx2++ )
2433 : {
2434 72480893 : *( ptrMM_out++ ) = *( ptrMM++ );
2435 : }
2436 23690766 : ptrMM_out++;
2437 : }
2438 11845383 : ptrMM_out--;
2439 : }
2440 : }
2441 : else
2442 : {
2443 392968 : mvr2r( mixing_matrix_local, mixing_matrix[param_band_idx], nY_cov * nX );
2444 : }
2445 :
2446 5894263 : return;
2447 : }
2448 :
2449 :
2450 : /*-------------------------------------------------------------------------
2451 : * param_mc_update_mixing_matrices()
2452 : *
2453 : * update mixing matrix buffers
2454 : *------------------------------------------------------------------------*/
2455 :
2456 448549 : static void param_mc_update_mixing_matrices(
2457 : PARAM_MC_DEC_HANDLE hParamMC, /* i/o: Parametric MC handle */
2458 : float *mixing_matrix[], /* i : direct mixing matrices for the frame just processed */
2459 : float *mixing_matrix_res[], /* i : residual mixing matrices for the frame just processed */
2460 : const uint16_t nX, /* i : number of transport channels */
2461 : const uint16_t nY ) /* i : number of synthesis channels */
2462 : {
2463 : uint16_t param_band_idx;
2464 :
2465 6468823 : for ( param_band_idx = 0; param_band_idx < hParamMC->hMetadataPMC->nbands_coded; param_band_idx++ )
2466 : {
2467 : int16_t brange[2];
2468 :
2469 6020274 : brange[0] = hParamMC->band_grouping[param_band_idx];
2470 6020274 : brange[1] = hParamMC->band_grouping[param_band_idx + 1];
2471 :
2472 6020274 : mvr2r( mixing_matrix[param_band_idx], hParamMC->h_output_synthesis_cov_state.mixing_matrix_old[param_band_idx], nX * nY );
2473 :
2474 6020274 : if ( brange[0] < hParamMC->h_output_synthesis_params.max_band_decorr )
2475 : {
2476 4779950 : mvr2r( mixing_matrix_res[param_band_idx], hParamMC->h_output_synthesis_cov_state.mixing_matrix_res_old[param_band_idx], nY * nY );
2477 : }
2478 : }
2479 :
2480 448549 : return;
2481 : }
2482 :
2483 :
2484 : /*-------------------------------------------------------------------------
2485 : * ivas_param_mc_dequantize_cov()
2486 : *
2487 : * generate the target covariance matrix
2488 : *------------------------------------------------------------------------*/
2489 :
2490 5894263 : static void ivas_param_mc_dequantize_cov(
2491 : PARAM_MC_DEC_HANDLE hParamMC, /* i : Parametric MC handle */
2492 : float *ild_q, /* i : sequence of dequantized ILD values */
2493 : float *icc_q, /* i : sequence of dequantized ICC values */
2494 : const int16_t param_band_index, /* i : current parameter band */
2495 : const int16_t nY_cov, /* i : number of output channels in the covariance synthesis */
2496 : const PARAM_MC_SYNTHESIS_CONF synth_conf, /* i : Parametric MC synthesis configuration */
2497 : const int16_t nY_int, /* i : number of channels in the transported format */
2498 : const int16_t nX, /* i : number of transport channels */
2499 : float *Cx_state, /* i : transport channel covariance matrix */
2500 : float *Cproto, /* i : prototype matrix */
2501 : float *Cy_state /* o : target covariance matrix */
2502 : )
2503 : {
2504 : float Nrqq[MAX_OUTPUT_CHANNELS];
2505 : float a[MAX_OUTPUT_CHANNELS];
2506 : int16_t k;
2507 : int16_t l;
2508 : float *Cyp;
2509 : float ap;
2510 : const PARAM_MC_ILD_MAPPING *h_ild_mapping;
2511 : float Cy_state_int[MAX_OUTPUT_CHANNELS * MAX_OUTPUT_CHANNELS];
2512 :
2513 5894263 : set_zero( Nrqq, MAX_OUTPUT_CHANNELS );
2514 5894263 : h_ild_mapping = hParamMC->hMetadataPMC->ild_mapping_conf;
2515 :
2516 : /*get back Nrg*/
2517 47508597 : for ( k = 0; k < nY_int; k++ )
2518 : {
2519 41614334 : float ref_ener = 0.0f;
2520 : int16_t ref_channel_cnt;
2521 : int16_t ref_channel_idx;
2522 :
2523 93218770 : for ( ref_channel_cnt = 0; ref_channel_cnt < h_ild_mapping->num_ref_channels[k]; ref_channel_cnt++ )
2524 : {
2525 51604436 : ref_channel_idx = h_ild_mapping->ref_channel_idx[k][ref_channel_cnt];
2526 51604436 : ref_ener += Cx_state[ref_channel_idx + ref_channel_idx * nX];
2527 : }
2528 41614334 : Nrqq[h_ild_mapping->ild_index[k]] = powf( 10.0f, ild_q[k] / 10.0f ) * hParamMC->hMetadataPMC->ild_factors[k] * ref_ener;
2529 : }
2530 :
2531 : /* estimate ICCs from estimated Cproto */
2532 47508597 : for ( k = 0; k < nY_int; k++ )
2533 : {
2534 41614334 : a[k] = 1.f / ( sqrtf( Cproto[k + nY_int * k] ) + EPSILON );
2535 :
2536 41614334 : v_multc( Cproto + k * nY_int, a[k], Cy_state_int + k * nY_int, nY_int );
2537 : }
2538 :
2539 47508597 : for ( k = 0; k < nY_int; k++ )
2540 : {
2541 41614334 : Cyp = Cy_state_int + k;
2542 41614334 : ap = a[k];
2543 351653378 : for ( l = 0; l < nY_int; l++ )
2544 : {
2545 310039044 : ( *Cyp ) *= ap;
2546 310039044 : Cyp += nY_int;
2547 : }
2548 : }
2549 :
2550 : /* replace some estimated ICCs with transmitted values */
2551 41614334 : for ( k = 0; k < hParamMC->hMetadataPMC->icc_mapping_conf->icc_map_size_lfe; k++ )
2552 : {
2553 35720071 : 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];
2554 35720071 : 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];
2555 : }
2556 :
2557 5894263 : if ( param_band_index >= PARAM_MC_MAX_BAND_LFE || !hParamMC->hMetadataPMC->lfe_on )
2558 : {
2559 44317677 : for ( k = 0; k < nY_int; k++ )
2560 : {
2561 38816382 : Cy_state_int[k + 3 * nY_int] = 1.0f;
2562 38816382 : Cy_state_int[3 + k * nY_int] = 1.0f;
2563 : }
2564 5501295 : Nrqq[3] = 0.0f;
2565 : }
2566 :
2567 : /* Generate back Covariance Mtx */
2568 47508597 : for ( k = 0; k < nY_int; k++ )
2569 : {
2570 41614334 : a[k] = sqrtf( Nrqq[k] );
2571 41614334 : v_multc( Cy_state_int + k * nY_int, a[k], Cy_state_int + k * nY_int, nY_int );
2572 : }
2573 :
2574 47508597 : for ( k = 0; k < nY_int; k++ )
2575 : {
2576 41614334 : Cyp = Cy_state_int + k;
2577 41614334 : ap = a[k];
2578 :
2579 351653378 : for ( l = 0; l < nY_int; l++ )
2580 : {
2581 310039044 : ( *Cyp ) *= ap;
2582 310039044 : Cyp += nY_int;
2583 : }
2584 : }
2585 :
2586 5894263 : if ( synth_conf == PARAM_MC_SYNTH_LS_CONV_COV )
2587 : {
2588 : /* Cy = dmx*Cy*dmx' */
2589 : float mat_mult_buffer1[MAX_LS_CHANNELS * MAX_LS_CHANNELS];
2590 : float target_ch_ener[MAX_LS_CHANNELS];
2591 : float dmx_ch_ener[MAX_LS_CHANNELS];
2592 :
2593 415860 : set_zero( target_ch_ener, MAX_LS_CHANNELS );
2594 415860 : set_zero( dmx_ch_ener, MAX_LS_CHANNELS );
2595 :
2596 415860 : matrix_product( hParamMC->ls_conv_dmx_matrix, nY_cov, nY_int, 0,
2597 : Cy_state_int, nY_int, nY_int, 0,
2598 : mat_mult_buffer1 );
2599 :
2600 415860 : matrix_product( mat_mult_buffer1, nY_cov, nY_int, 0,
2601 415860 : hParamMC->ls_conv_dmx_matrix, nY_cov, nY_int, 1,
2602 : Cy_state );
2603 :
2604 2976756 : for ( k = 0; k < nY_cov; k++ )
2605 : {
2606 23921640 : for ( l = 0; l < nY_int; l++ )
2607 : {
2608 21360744 : target_ch_ener[k] += hParamMC->ls_conv_dmx_matrix[k + l * nY_cov] * Nrqq[l];
2609 : }
2610 :
2611 2560896 : dmx_ch_ener[k] = Cy_state[k + nY_cov * k];
2612 :
2613 2560896 : if ( dmx_ch_ener[k] < 0.0f )
2614 : {
2615 0 : Cy_state[k + nY_cov * k] *= -1.0f;
2616 0 : dmx_ch_ener[k] *= -1.0f;
2617 : }
2618 :
2619 2560896 : target_ch_ener[k] = sqrtf( target_ch_ener[k] / ( dmx_ch_ener[k] + EPSILON ) );
2620 :
2621 2560896 : v_multc( Cy_state + k * nY_cov, target_ch_ener[k], Cy_state + k * nY_cov, nY_cov );
2622 :
2623 2560896 : Cyp = Cy_state + k;
2624 2560896 : ap = target_ch_ener[k];
2625 18482256 : for ( l = 0; l < nY_cov; l++ )
2626 : {
2627 15921360 : ( *Cyp ) *= ap;
2628 15921360 : Cyp += nY_cov;
2629 : }
2630 : }
2631 : }
2632 : else
2633 : {
2634 5478403 : mvr2r( Cy_state_int, Cy_state, nY_int * nY_int );
2635 : }
2636 :
2637 5894263 : return;
2638 : }
2639 :
2640 :
2641 : /*-------------------------------------------------------------------------*
2642 : * param_mc_set_num_synth_bands()
2643 : *
2644 : * set the number of frequency bands to be synthesized
2645 : *-------------------------------------------------------------------------*/
2646 :
2647 24183 : static void param_mc_set_num_synth_bands(
2648 : const int32_t output_Fs, /* i : output sampling frequency */
2649 : PARAM_MC_DEC_HANDLE hParamMC /* i/o: Parametric MC handle */
2650 : )
2651 : {
2652 : uint16_t max_param_band_synth;
2653 : const int16_t *param_mc_bands_coded;
2654 :
2655 24183 : switch ( hParamMC->hMetadataPMC->num_parameter_bands )
2656 : {
2657 2736 : case 20:
2658 2736 : param_mc_bands_coded = param_mc_bands_coded_20;
2659 2736 : break;
2660 8706 : case 10:
2661 8706 : param_mc_bands_coded = param_mc_bands_coded_10;
2662 8706 : break;
2663 12741 : case 14:
2664 : default:
2665 12741 : param_mc_bands_coded = param_mc_bands_coded_14;
2666 12741 : break;
2667 : }
2668 :
2669 24183 : switch ( output_Fs )
2670 : {
2671 0 : case 8000:
2672 0 : max_param_band_synth = param_mc_bands_coded[NB];
2673 0 : break;
2674 6647 : case 16000:
2675 6647 : max_param_band_synth = param_mc_bands_coded[WB];
2676 6647 : break;
2677 4630 : case 32000:
2678 4630 : max_param_band_synth = param_mc_bands_coded[SWB];
2679 4630 : break;
2680 12906 : case 48000:
2681 : default:
2682 12906 : max_param_band_synth = param_mc_bands_coded[FB];
2683 12906 : break;
2684 : }
2685 :
2686 24183 : hParamMC->num_param_bands_synth = min( hParamMC->hMetadataPMC->nbands_coded, max_param_band_synth );
2687 :
2688 24183 : return;
2689 : }
2690 :
2691 :
2692 : /*-------------------------------------------------------------------------*
2693 : * param_mc_get_diff_proto_info()
2694 : *
2695 : * calculated the diffuse prototype information
2696 : *-------------------------------------------------------------------------*/
2697 :
2698 13495 : static ivas_error param_mc_get_diff_proto_info(
2699 : const float *proto_mtx, /* i : protoype matrix for the synthesis */
2700 : const uint16_t nchan_transport, /* i : number of transport channels */
2701 : const uint16_t nchan_out_cov, /* i : number if output channels of the covariance synthesis */
2702 : PARAM_MC_DIFF_PROTO_INFO *p_diff_proto_info /* o : generated diffuse prototype info */
2703 : )
2704 : {
2705 : float proto_fac[MAX_LS_CHANNELS * PARAM_MC_MAX_TRANSPORT_CHANS];
2706 : uint16_t cur_out_ch;
2707 : uint16_t cur_diff_proto;
2708 : uint16_t cur_transport_ch;
2709 : uint16_t max_num_src_chan;
2710 :
2711 : /* Initializations */
2712 13495 : max_num_src_chan = 0;
2713 13495 : set_zero( proto_fac, MAX_LS_CHANNELS * PARAM_MC_MAX_TRANSPORT_CHANS );
2714 13495 : if ( ( p_diff_proto_info->proto_index_diff = (int16_t *) malloc( nchan_out_cov * sizeof( int16_t ) ) ) == NULL )
2715 : {
2716 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
2717 : }
2718 13495 : set_s( p_diff_proto_info->proto_index_diff, 0, nchan_out_cov );
2719 :
2720 13495 : if ( ( p_diff_proto_info->num_source_chan_diff = (int16_t *) malloc( nchan_out_cov * sizeof( int16_t ) ) ) == NULL )
2721 : {
2722 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
2723 : }
2724 13495 : set_s( p_diff_proto_info->num_source_chan_diff, 0, nchan_out_cov );
2725 :
2726 : /* we have at least one prototype, copy the first one */
2727 13495 : p_diff_proto_info->num_protos_diff = 1;
2728 13495 : mvr2r_inc( proto_mtx, nchan_out_cov, proto_fac, nchan_out_cov, nchan_transport );
2729 13495 : p_diff_proto_info->proto_index_diff[0] = 0;
2730 :
2731 : /* search for distinct prototypes */
2732 102098 : for ( cur_out_ch = 1; cur_out_ch < nchan_out_cov; cur_out_ch++ )
2733 : {
2734 88603 : uint16_t found = 0;
2735 :
2736 180983 : for ( cur_diff_proto = 0; cur_diff_proto < p_diff_proto_info->num_protos_diff; cur_diff_proto++ )
2737 : {
2738 153763 : float diff = 0;
2739 153763 : float *proto_fac_ptr = proto_fac + cur_diff_proto;
2740 153763 : const float *proto_mtx_ptr = proto_mtx + cur_out_ch;
2741 :
2742 518406 : for ( cur_transport_ch = 0; cur_transport_ch < nchan_transport; cur_transport_ch++ )
2743 : {
2744 364643 : diff += fabsf( *proto_fac_ptr - *proto_mtx_ptr );
2745 364643 : proto_fac_ptr += nchan_out_cov;
2746 364643 : proto_mtx_ptr += nchan_out_cov;
2747 : }
2748 :
2749 : /* we already have this prototype, save the index */
2750 153763 : if ( diff < 0.1f )
2751 : {
2752 61383 : found = 1;
2753 61383 : p_diff_proto_info->proto_index_diff[cur_out_ch] = cur_diff_proto;
2754 61383 : break;
2755 : }
2756 : }
2757 :
2758 : /* new distinct prototype, add it */
2759 88603 : if ( found == 0 )
2760 : {
2761 27220 : const float *proto_mtx_ptr = proto_mtx + cur_out_ch;
2762 : int16_t cur_num_src_chan;
2763 :
2764 27220 : cur_num_src_chan = 0;
2765 89112 : for ( cur_transport_ch = 0; cur_transport_ch < nchan_transport; cur_transport_ch++ )
2766 : {
2767 61892 : if ( *proto_mtx_ptr > EPSILON )
2768 : {
2769 37049 : cur_num_src_chan++;
2770 : }
2771 61892 : proto_mtx_ptr += nchan_out_cov;
2772 : }
2773 :
2774 27220 : mvr2r_inc( proto_mtx + cur_out_ch, nchan_out_cov, proto_fac + p_diff_proto_info->num_protos_diff, nchan_out_cov, nchan_transport );
2775 :
2776 27220 : p_diff_proto_info->proto_index_diff[cur_out_ch] = p_diff_proto_info->num_protos_diff;
2777 27220 : p_diff_proto_info->num_protos_diff++;
2778 27220 : max_num_src_chan = max( max_num_src_chan, cur_num_src_chan );
2779 : }
2780 : }
2781 :
2782 : /* set up the prototype info struct */
2783 13495 : if ( ( p_diff_proto_info->source_chan_idx = (int16_t **) malloc( p_diff_proto_info->num_protos_diff * sizeof( int16_t * ) ) ) == NULL )
2784 : {
2785 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
2786 : }
2787 13495 : if ( ( p_diff_proto_info->proto_fac = (float **) malloc( p_diff_proto_info->num_protos_diff * sizeof( float * ) ) ) == NULL )
2788 : {
2789 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
2790 : }
2791 :
2792 54210 : for ( cur_diff_proto = 0; cur_diff_proto < p_diff_proto_info->num_protos_diff; cur_diff_proto++ )
2793 : {
2794 : float *proto_fac_ptr;
2795 :
2796 40715 : if ( ( p_diff_proto_info->source_chan_idx[cur_diff_proto] = (int16_t *) malloc( max_num_src_chan * sizeof( int16_t ) ) ) == NULL )
2797 : {
2798 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
2799 : }
2800 40715 : if ( ( p_diff_proto_info->proto_fac[cur_diff_proto] = (float *) malloc( max_num_src_chan * sizeof( float ) ) ) == NULL )
2801 : {
2802 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Parametric MC\n" ) );
2803 : }
2804 :
2805 40715 : proto_fac_ptr = proto_fac + cur_diff_proto;
2806 133251 : for ( cur_transport_ch = 0; cur_transport_ch < nchan_transport; cur_transport_ch++ )
2807 : {
2808 92536 : if ( *proto_fac_ptr > EPSILON )
2809 : {
2810 50544 : p_diff_proto_info->source_chan_idx[cur_diff_proto][p_diff_proto_info->num_source_chan_diff[cur_diff_proto]] = cur_transport_ch;
2811 50544 : p_diff_proto_info->proto_fac[cur_diff_proto][p_diff_proto_info->num_source_chan_diff[cur_diff_proto]] = *proto_fac_ptr;
2812 50544 : p_diff_proto_info->num_source_chan_diff[cur_diff_proto]++;
2813 : }
2814 92536 : proto_fac_ptr += nchan_out_cov;
2815 : }
2816 : }
2817 :
2818 13495 : return IVAS_ERR_OK;
2819 : }
2820 :
2821 :
2822 : /*-------------------------------------------------------------------------*
2823 : * ivas_param_mc_bs_decode_parameter_values()
2824 : *
2825 : * reads and decodes a sequence of Parametric MC parameters from the bitstream
2826 : *-------------------------------------------------------------------------*/
2827 :
2828 899598 : static void ivas_param_mc_bs_decode_parameter_values(
2829 : uint16_t bit_buffer[], /* i : bitstream buffer */
2830 : int16_t *bit_pos, /* i/o: current bitstream buffer position */
2831 : const int16_t max_bits, /* i : maximum available bits in the buffer */
2832 : int16_t *BER_detect, /* i/o: bit error detection flag */
2833 : HANDLE_IVAS_PARAM_MC_METADATA hMetadataPMC, /* i : Parametric MC metadata information */
2834 : HANDLE_PARAM_MC_PARAMETER_CODING_INFO hParamCodingInfo, /* i : Parametric MC parameter quantization and coding tables */
2835 : const int16_t map_size_wo_lfe, /* i : number of parameters per band (w/o LFEs) */
2836 : const int16_t map_size, /* i : number of parameters per band (total) */
2837 : const int16_t num_lfe_bands, /* i : number of parameter bands with coded LFE */
2838 : const int16_t band_step, /* i : parameter band step */
2839 : const int16_t num_param_bands, /* i : number of parameter bands to decode */
2840 : float *value_buffer /* o : output buffer for decoded parameter values */
2841 : )
2842 : {
2843 : int16_t range_coding;
2844 : int16_t sz_seq;
2845 : int16_t delta_coding;
2846 : int16_t delta_idx[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
2847 : int16_t idx[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
2848 : int16_t idx_prev;
2849 : int16_t idx_offset;
2850 : int16_t sz_alphabet;
2851 : int16_t i, j, k;
2852 : float dequant_seq[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
2853 : float dequant_ordered[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
2854 : int16_t n_lfe_idx;
2855 :
2856 899598 : range_coding = bit_buffer[( *bit_pos )++];
2857 :
2858 : /* Decoding the sequence */
2859 899598 : n_lfe_idx = map_size - map_size_wo_lfe;
2860 899598 : sz_seq = num_param_bands * ( map_size_wo_lfe ) + num_lfe_bands * n_lfe_idx;
2861 :
2862 899598 : set_s( idx, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE );
2863 899598 : set_zero( dequant_ordered, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE );
2864 899598 : set_zero( dequant_seq, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE );
2865 :
2866 899598 : if ( range_coding )
2867 : {
2868 854033 : delta_coding = bit_buffer[( *bit_pos )++];
2869 :
2870 854033 : if ( delta_coding )
2871 : {
2872 835760 : idx_prev = hParamCodingInfo->quantizer_size / 2 + hParamCodingInfo->quantizer_size % 2 - 1;
2873 835760 : sz_alphabet = 2 * hParamCodingInfo->quantizer_size - 1;
2874 835760 : idx_offset = hParamCodingInfo->quantizer_size - 1;
2875 :
2876 : /* read range coded delta ICC indices */
2877 1671520 : *bit_pos += ivas_param_mc_range_decoder_LC( &bit_buffer[*bit_pos], delta_idx, BER_detect, sz_seq, sz_alphabet,
2878 835760 : hParamCodingInfo->cum_freq_delta, hParamCodingInfo->sym_freq_delta, PARAM_MC_RANGE_CODER_TOT_SHIFT, max_bits - *bit_pos );
2879 :
2880 : /* delta index to absolute index */
2881 32533148 : for ( j = 0; j < sz_seq; j++ )
2882 : {
2883 31697388 : idx[j] = idx_prev + delta_idx[j] - idx_offset;
2884 31697388 : idx_prev = idx[j];
2885 : }
2886 : }
2887 : else
2888 : {
2889 : /* read range coded absolute ICC indices */
2890 18273 : sz_alphabet = hParamCodingInfo->quantizer_size;
2891 18273 : *bit_pos += ivas_param_mc_range_decoder_LC( &bit_buffer[*bit_pos], idx, BER_detect, sz_seq, sz_alphabet,
2892 18273 : hParamCodingInfo->cum_freq, hParamCodingInfo->sym_freq, PARAM_MC_RANGE_CODER_TOT_SHIFT, max_bits - *bit_pos );
2893 : }
2894 :
2895 : /* dequantize */
2896 33223573 : for ( j = 0; j < sz_seq; j++ )
2897 : {
2898 32369540 : dequant_seq[j] = hParamCodingInfo->quantizer[idx[j]];
2899 : }
2900 : }
2901 : else
2902 : {
2903 45565 : set_zero( dequant_seq, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE );
2904 :
2905 : /* read uniformly coded ICCs */
2906 45565 : *bit_pos += ivas_param_mc_uniform_decoder( dequant_seq, sz_seq, hParamCodingInfo->quantizer, hParamCodingInfo->uni_bits, &bit_buffer[*bit_pos] );
2907 : }
2908 :
2909 : /* reorder from sequential to parameter-band-wise */
2910 899598 : k = 0;
2911 5827597 : for ( j = 0; j < map_size_wo_lfe; ++j )
2912 : {
2913 38280702 : for ( i = 0; i < num_param_bands; ++i )
2914 : {
2915 33352703 : dequant_ordered[j + i * map_size] = dequant_seq[k++];
2916 : }
2917 : }
2918 :
2919 1316218 : for ( i = 0; i < num_lfe_bands; i++ )
2920 : {
2921 865393 : for ( j = 0; j < n_lfe_idx; j++ )
2922 : {
2923 448773 : dequant_ordered[map_size - n_lfe_idx + j + i * map_size] = dequant_seq[k++];
2924 : }
2925 : }
2926 :
2927 899598 : if ( !( *BER_detect ) )
2928 : {
2929 899598 : j = 0;
2930 12634830 : for ( k = 0; k < hMetadataPMC->nbands_coded; k += band_step )
2931 : {
2932 11735232 : if ( hMetadataPMC->bAttackPresent || hMetadataPMC->param_frame_idx == hMetadataPMC->coding_band_mapping[k] )
2933 : {
2934 6047046 : mvr2r( dequant_ordered + j * map_size, value_buffer + k * map_size, map_size );
2935 6047046 : j++;
2936 : }
2937 11735232 : if ( hMetadataPMC->bAttackPresent && k + 1 < hMetadataPMC->nbands_coded )
2938 : {
2939 351000 : mvr2r( value_buffer + k * map_size, value_buffer + ( k + 1 ) * map_size, map_size );
2940 : }
2941 : }
2942 : }
2943 :
2944 899598 : return;
2945 : }
|