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