LCOV - code coverage report
Current view: top level - lib_dec - ivas_mc_param_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 984 1069 92.0 %
Date: 2025-11-01 08:50:45 Functions: 23 23 100.0 %

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

Generated by: LCOV version 1.14