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

Generated by: LCOV version 1.14