LCOV - code coverage report
Current view: top level - lib_enc - ivas_mc_param_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ b5bd5e0684ad2d9250297e1e7a0ddc986cb7b37e Lines: 511 522 97.9 %
Date: 2025-10-27 07:01:45 Functions: 14 14 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 <math.h>
      34             : #include <assert.h>
      35             : #include "options.h"
      36             : #include "cnst.h"
      37             : #include "rom_enc.h"
      38             : #include "ivas_rom_enc.h"
      39             : #include "rom_com.h"
      40             : #include "prot.h"
      41             : #include "ivas_prot.h"
      42             : #include "ivas_cnst.h"
      43             : #include "ivas_rom_com.h"
      44             : #ifdef DEBUGGING
      45             : #include "debug.h"
      46             : #endif
      47             : #ifdef DEBUG_PLOT
      48             : #include "deb_out.h"
      49             : #endif
      50             : #include "wmc_auto.h"
      51             : 
      52             : 
      53             : /*-------------------------------------------------------------------------
      54             :  * Local function prototypes
      55             :  *------------------------------------------------------------------------*/
      56             : 
      57             : static void ivas_param_mc_dmx( PARAM_MC_ENC_HANDLE hParamMC, float *data_f[], float data_dmx[][L_FRAME48k], const int16_t input_frame, const int16_t nchan_input, const int16_t nchan_transport );
      58             : 
      59             : static void ivas_param_mc_param_est_enc( PARAM_MC_ENC_HANDLE hParamMC, float *data_f[], float Cy_sum[][MAX_LS_CHANNELS][MAX_LS_CHANNELS], float Cx_sum[][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], const int16_t input_frame, const int16_t nchan_input, const int16_t nchan_transport );
      60             : 
      61             : static void ivas_param_mc_parameter_quantizer( const float *x, const int16_t L, const int16_t sz_quantizer, const float *quantizer, int16_t *quant_idx, float *y );
      62             : 
      63             : static void ivas_param_mc_transient_detection( PARAM_MC_ENC_HANDLE hParamMC, TRAN_DET_HANDLE hTranDet, int16_t *bAttackPresent, int16_t *attackIdx );
      64             : 
      65             : static void ivas_param_mc_quantize_iccs( PARAM_MC_ENC_HANDLE hParamMC, float Cy[MAX_LS_CHANNELS][MAX_LS_CHANNELS], const int16_t freq_idx, const int16_t nchan_input, int16_t *ICC_idx_out );
      66             : 
      67             : static void ivas_param_mc_quantize_ilds( PARAM_MC_ENC_HANDLE hParamMC, float Cy[MAX_LS_CHANNELS][MAX_LS_CHANNELS], float Cx[PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], const int16_t freq_idx, const int16_t nchan_input, const int16_t nchan_transport, int16_t *ILD_idx_out, float ILD_q[PARAM_MC_SZ_ILD_MAP] );
      68             : 
      69             : static void ivas_param_mc_write_bs( const PARAM_MC_ENC_HANDLE hParamMC, int16_t *ILD_idx, int16_t *ICC_idx, uint16_t bit_buffer[PARAM_MC_MAX_BITS], int16_t *bit_pos );
      70             : 
      71             : static void ivas_param_mc_dec2bin( const int16_t val, const int16_t N, uint16_t bits[PARAM_MC_MAX_BITS] );
      72             : 
      73             : static void ivas_param_mc_encode_parameter( int16_t *idx_in, HANDLE_IVAS_PARAM_MC_METADATA hMetadataPMC, HANDLE_PARAM_MC_PARAMETER_CODING_INFO hParameterCodingInfo, const int16_t nbands, const int16_t band_step, const int16_t map_size_wo_lfe, const int16_t map_size, uint16_t bit_buffer[PARAM_MC_MAX_BITS], int16_t *bit_pos );
      74             : 
      75             : static void ivas_param_mc_range_encoder( const int16_t *seq_in, const int16_t num_symbols, const uint16_t *cum_freq, const uint16_t *sym_freq, const uint16_t tot_shift, const int16_t max_nb_bits, uint16_t *bit_buffer, int16_t *bit_pos );
      76             : 
      77             : /*-------------------------------------------------------------------------
      78             :  * ivas_param_mc_enc_open()
      79             :  *
      80             :  * Initialize Parametric MC encoder handle
      81             :  *------------------------------------------------------------------------*/
      82             : 
      83         460 : ivas_error ivas_param_mc_enc_open(
      84             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder handle          */
      85             : )
      86             : {
      87             :     int16_t i;
      88             :     IVAS_FB_CFG *fb_cfg;
      89             :     PARAM_MC_ENC_HANDLE hParamMC;
      90             :     uint16_t config_index;
      91             :     MC_LS_SETUP mc_input_setup;
      92             :     int16_t max_bwidth, nchan_inp;
      93             :     int32_t input_Fs, ivas_total_brate;
      94             :     ivas_error error;
      95             : 
      96         460 :     error = IVAS_ERR_OK;
      97             : 
      98             :     /* Sanity Checks */
      99         460 :     if ( ( hParamMC = (PARAM_MC_ENC_HANDLE) malloc( sizeof( PARAM_MC_ENC_DATA ) ) ) == NULL )
     100             :     {
     101           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Paramtric MC\n" ) );
     102             :     }
     103             : 
     104         460 :     mc_input_setup = st_ivas->hEncoderConfig->mc_input_setup;
     105         460 :     max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
     106         460 :     input_Fs = st_ivas->hEncoderConfig->input_Fs;
     107         460 :     nchan_inp = st_ivas->hEncoderConfig->nchan_inp;
     108         460 :     ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
     109             : 
     110             :     /* Preparing Config */
     111         460 :     hParamMC->lfe_index = LFE_CHANNEL;
     112         460 :     st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels( ivas_total_brate, mc_input_setup );
     113             : 
     114             :     /* get configuration index */
     115         460 :     config_index = ivas_param_mc_get_configuration_index( mc_input_setup, ivas_total_brate );
     116             : 
     117             :     /* set core coder dependent on the number of transport channels */
     118         460 :     switch ( st_ivas->nchan_transport )
     119             :     {
     120         106 :         case 4:
     121             :         case 3:
     122         106 :             st_ivas->nCPE = 2;
     123         106 :             st_ivas->nSCE = 0;
     124         106 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     125         106 :             break;
     126         354 :         case 2:
     127         354 :             st_ivas->nCPE = 1;
     128         354 :             st_ivas->nSCE = 0;
     129         354 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     130         354 :             break;
     131             : #ifdef DEBUGGING
     132             :         default:
     133             :             assert( 0 && "Number of transport channels not supported by ParamMC!\n" );
     134             : #endif
     135             :     }
     136             : 
     137             :     /* get dmx factors */
     138         460 :     hParamMC->dmx_factors = ivas_param_mc_conf[config_index].dmx_fac;
     139             : 
     140             :     /* set FB config. */
     141         460 :     if ( ( error = ivas_fb_set_cfg( &fb_cfg, MC_FORMAT, nchan_inp, 0, 0, input_Fs, 0 ) ) != IVAS_ERR_OK )
     142             :     {
     143           0 :         return error;
     144             :     }
     145             : 
     146             :     /* Allocate and initialize FB mixer handle */
     147         460 :     if ( ( error = ivas_FB_mixer_open( &( hParamMC->hFbMixer ), input_Fs, fb_cfg, 0 ) ) != IVAS_ERR_OK )
     148             :     {
     149           0 :         return error;
     150             :     }
     151             : 
     152             :     /* open/init parameter coding */
     153         460 :     ivas_param_mc_metadata_open( mc_input_setup, ivas_total_brate, &hParamMC->hMetadataPMC );
     154             : 
     155             :     /* Band Grouping */
     156         460 :     if ( hParamMC->hMetadataPMC.num_parameter_bands == 20 )
     157             :     {
     158          15 :         mvs2s( param_mc_band_grouping_20, hParamMC->band_grouping, 20 + 1 );
     159             :     }
     160         445 :     else if ( hParamMC->hMetadataPMC.num_parameter_bands == 14 )
     161             :     {
     162         258 :         mvs2s( param_mc_band_grouping_14, hParamMC->band_grouping, 14 + 1 );
     163             :     }
     164         187 :     else if ( hParamMC->hMetadataPMC.num_parameter_bands == 10 )
     165             :     {
     166         187 :         mvs2s( param_mc_band_grouping_10, hParamMC->band_grouping, 10 + 1 );
     167             :     }
     168             :     else
     169             :     {
     170           0 :         assert( 0 && "nbands must be 20, 14, or 10!" );
     171             :     }
     172             : 
     173             :     /* set max parameter band for abs cov */
     174         460 :     i = 0;
     175        4256 :     while ( hParamMC->band_grouping[i] <= PARAM_MC_MAX_BAND_ABS_COV_ENC )
     176             :     {
     177        3796 :         hParamMC->max_param_band_abs_cov = ( i++ );
     178             :     }
     179             : 
     180             :     /* parameter band grouping: 60 band CLDFB to 240 band MDFT resolution */
     181        6702 :     for ( i = 0; i < hParamMC->hMetadataPMC.num_parameter_bands + 1; i++ )
     182             :     {
     183        6242 :         hParamMC->band_grouping[i] *= PARAM_MC_CLDFB_TO_MDFT_FAC;
     184             :     }
     185             : 
     186             :     /* set correct coded band width */
     187         460 :     hParamMC->hMetadataPMC.coded_bwidth = max_bwidth;
     188         460 :     hParamMC->hMetadataPMC.last_coded_bwidth = max_bwidth;
     189         460 :     ivas_param_mc_set_coded_bands( &hParamMC->hMetadataPMC );
     190             : 
     191             :     /* initialize offset for transient detection */
     192         460 :     hParamMC->transient_detector_delay = ( NSUBBLOCKS_SHIFT + 1 ) + NSUBBLOCKS + 1 - (int16_t) ceilf( (float) NS2SA( input_Fs, DELAY_DIRAC_ENC_CMP_NS ) / (float) NS2SA( input_Fs, 2 * DIRAC_SLOT_NS ) );
     193             : 
     194             :     /* Init total/dmx ener factors */
     195         460 :     set_f( hParamMC->ener_fac, 0.0f, PARAM_MC_MAX_PARAMETER_BANDS );
     196             : 
     197             :     /* init previous ILDs */
     198        9660 :     for ( i = 0; i < PARAM_MC_MAX_PARAMETER_BANDS; i++ )
     199             :     {
     200        9200 :         set_zero( hParamMC->prev_ilds[i], PARAM_MC_SZ_ILD_MAP );
     201             :     }
     202             : 
     203         460 :     st_ivas->hParamMC = hParamMC;
     204             : 
     205         460 :     return error;
     206             : }
     207             : 
     208             : 
     209             : /*-------------------------------------------------------------------------
     210             :  * ivas_param_mc_enc_reconfig()
     211             :  *
     212             :  * Reconfigure Parametric MC encoder
     213             :  *------------------------------------------------------------------------*/
     214             : 
     215          79 : ivas_error ivas_param_mc_enc_reconfig(
     216             :     Encoder_Struct *st_ivas /* i/o: IVAS encoder handle          */
     217             : )
     218             : {
     219             :     int16_t i;
     220             :     PARAM_MC_ENC_HANDLE hParamMC;
     221             :     uint16_t config_index;
     222             :     MC_LS_SETUP mc_input_setup;
     223             :     int16_t max_bwidth;
     224             :     int32_t input_Fs, ivas_total_brate;
     225             :     ivas_error error;
     226             : 
     227          79 :     error = IVAS_ERR_OK;
     228             : 
     229          79 :     mc_input_setup = st_ivas->hEncoderConfig->mc_input_setup;
     230          79 :     max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
     231          79 :     input_Fs = st_ivas->hEncoderConfig->input_Fs;
     232          79 :     ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
     233          79 :     hParamMC = st_ivas->hParamMC;
     234             : 
     235             :     /* Preparing Config */
     236          79 :     st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels( ivas_total_brate, mc_input_setup );
     237             : 
     238             :     /* get configuration index */
     239          79 :     config_index = ivas_param_mc_get_configuration_index( mc_input_setup, ivas_total_brate );
     240             : 
     241             :     /* set core coder dependent on the number of transport channels */
     242          79 :     switch ( st_ivas->nchan_transport )
     243             :     {
     244           0 :         case 4:
     245             :         case 3:
     246           0 :             st_ivas->nCPE = 2;
     247           0 :             st_ivas->nSCE = 0;
     248           0 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     249           0 :             break;
     250          79 :         case 2:
     251          79 :             st_ivas->nCPE = 1;
     252          79 :             st_ivas->nSCE = 0;
     253          79 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     254          79 :             break;
     255             : #ifdef DEBUGGING
     256             :         default:
     257             :             assert( 0 && "Number of transport channels not supported by ParamMC!\n" );
     258             : #endif
     259             :     }
     260             : 
     261             :     /* get dmx factors */
     262          79 :     hParamMC->dmx_factors = ivas_param_mc_conf[config_index].dmx_fac;
     263             : 
     264             :     /* open/init parameter coding */
     265          79 :     ivas_param_mc_metadata_open( mc_input_setup, ivas_total_brate, &hParamMC->hMetadataPMC );
     266             : 
     267             :     /* Band Grouping */
     268          79 :     if ( hParamMC->hMetadataPMC.num_parameter_bands == 20 )
     269             :     {
     270           0 :         mvs2s( param_mc_band_grouping_20, hParamMC->band_grouping, 20 + 1 );
     271             :     }
     272          79 :     else if ( hParamMC->hMetadataPMC.num_parameter_bands == 14 )
     273             :     {
     274          41 :         mvs2s( param_mc_band_grouping_14, hParamMC->band_grouping, 14 + 1 );
     275             :     }
     276          38 :     else if ( hParamMC->hMetadataPMC.num_parameter_bands == 10 )
     277             :     {
     278          38 :         mvs2s( param_mc_band_grouping_10, hParamMC->band_grouping, 10 + 1 );
     279             :     }
     280             :     else
     281             :     {
     282           0 :         assert( 0 && "nbands must be 20, 14, or 10!" );
     283             :     }
     284             : 
     285             :     /* set max parameter band for abs cov */
     286          79 :     i = 0;
     287         714 :     while ( hParamMC->band_grouping[i] <= PARAM_MC_MAX_BAND_ABS_COV_ENC )
     288             :     {
     289         635 :         hParamMC->max_param_band_abs_cov = ( i++ );
     290             :     }
     291             : 
     292             :     /* parameter band grouping: 60 band CLDFB to 240 band MDFT resolution */
     293        1112 :     for ( i = 0; i < hParamMC->hMetadataPMC.num_parameter_bands + 1; i++ )
     294             :     {
     295        1033 :         hParamMC->band_grouping[i] *= PARAM_MC_CLDFB_TO_MDFT_FAC;
     296             :     }
     297             : 
     298             :     /* set correct coded band width */
     299          79 :     hParamMC->hMetadataPMC.coded_bwidth = max_bwidth;
     300          79 :     hParamMC->hMetadataPMC.last_coded_bwidth = max_bwidth;
     301          79 :     ivas_param_mc_set_coded_bands( &hParamMC->hMetadataPMC );
     302             : 
     303             :     /* initialize offset for transient detection */
     304          79 :     hParamMC->transient_detector_delay = ( NSUBBLOCKS_SHIFT + 1 ) + NSUBBLOCKS + 1 - (int16_t) ceilf( (float) NS2SA( input_Fs, DELAY_DIRAC_ENC_CMP_NS ) / (float) NS2SA( input_Fs, 2 * DIRAC_SLOT_NS ) );
     305             : 
     306             :     /* Init total/dmx ener factors */
     307          79 :     set_f( hParamMC->ener_fac, 0.0f, PARAM_MC_MAX_PARAMETER_BANDS );
     308             : 
     309             : 
     310          79 :     return error;
     311             : }
     312             : 
     313             : 
     314             : /*-------------------------------------------------------------------------
     315             :  * ivas_param_mc_enc_close()
     316             :  *
     317             :  * Close Parametric MC encoder handle
     318             :  *------------------------------------------------------------------------*/
     319             : 
     320        6513 : void ivas_param_mc_enc_close(
     321             :     PARAM_MC_ENC_HANDLE *hParamMC, /* i/o: Parametric MC encoder handle        */
     322             :     const int32_t sampling_rate )
     323             : {
     324        6513 :     if ( hParamMC == NULL || *hParamMC == NULL )
     325             :     {
     326        6053 :         return;
     327             :     }
     328             : 
     329         460 :     ivas_FB_mixer_close( &( *hParamMC )->hFbMixer, sampling_rate, 0 );
     330             : 
     331         460 :     free( ( *hParamMC ) );
     332         460 :     ( *hParamMC ) = NULL;
     333             : 
     334         460 :     return;
     335             : }
     336             : 
     337             : 
     338             : /*-------------------------------------------------------------------------
     339             :  * ivas_param_mc_enc()
     340             :  *
     341             :  * Parametric MC Encoder main encoding function
     342             :  *------------------------------------------------------------------------*/
     343             : 
     344       20980 : void ivas_param_mc_enc(
     345             :     Encoder_Struct *st_ivas,   /* i/o: IVAS Encoder handle             */
     346             :     BSTR_ENC_HANDLE hMetaData, /* i/o: IVAS Metadata bitstream handle  */
     347             :     float *data_f[],           /* i/o: input/transport MC data         */
     348             :     const int16_t input_frame  /* i  : input frame length              */
     349             : )
     350             : {
     351             :     int16_t k;
     352             :     float Cy_sum[PARAM_MC_MAX_PARAMETER_BANDS][MAX_LS_CHANNELS][MAX_LS_CHANNELS];
     353             :     float Cx_sum[PARAM_MC_MAX_PARAMETER_BANDS][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS];
     354             :     float ILD_q[PARAM_MC_MAX_PARAMETER_BANDS][PARAM_MC_SZ_ILD_MAP];
     355             :     int16_t ILD_idx[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP];
     356             :     int16_t ICC_idx[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ICC_MAP];
     357             :     uint16_t bit_buffer[PARAM_MC_MAX_BITS];
     358             :     int16_t bit_pos;
     359             :     int16_t band_step;
     360             :     float data_dmx[PARAM_MC_MAX_TRANSPORT_CHANS][L_FRAME48k];
     361             :     int16_t ch;
     362             :     int16_t band;
     363             :     PARAM_MC_ENC_HANDLE hParamMC;
     364             :     int16_t nchan_inp;
     365             : 
     366       20980 :     push_wmops( "param_mc_enc" );
     367             : 
     368             :     /* initializations */
     369       20980 :     hParamMC = st_ivas->hParamMC;
     370       20980 :     bit_pos = 0;
     371       20980 :     band_step = 1;
     372       20980 :     nchan_inp = st_ivas->hEncoderConfig->nchan_inp;
     373             : 
     374      440580 :     for ( band = 0; band < PARAM_MC_MAX_PARAMETER_BANDS; band++ )
     375             :     {
     376     7133200 :         for ( ch = 0; ch < MAX_LS_CHANNELS; ch++ )
     377             :         {
     378     6713600 :             set_zero( Cy_sum[band][ch], MAX_LS_CHANNELS );
     379             :         }
     380     1678400 :         for ( ch = 0; ch < PARAM_MC_MAX_TRANSPORT_CHANS; ch++ )
     381             :         {
     382     1258800 :             set_zero( Cx_sum[band][ch], PARAM_MC_MAX_TRANSPORT_CHANS );
     383             :         }
     384             :     }
     385             : 
     386      440580 :     for ( band = 0; band < PARAM_MC_MAX_PARAMETER_BANDS; band++ )
     387             :     {
     388      419600 :         set_zero( ILD_q[band], PARAM_MC_SZ_ILD_MAP );
     389             :     }
     390       20980 :     set_s( ILD_idx, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP );
     391       20980 :     set_s( ICC_idx, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ICC_MAP );
     392             : 
     393             :     /* update parameter frame index */
     394       20980 :     hParamMC->hMetadataPMC.param_frame_idx = ( hParamMC->hMetadataPMC.param_frame_idx + 1 ) % PARAM_MC_PARAMETER_FRAMES;
     395             : 
     396             :     /* DMX generation*/
     397       20980 :     ivas_param_mc_dmx( hParamMC, data_f, data_dmx, input_frame, nchan_inp, st_ivas->nchan_transport );
     398             : 
     399             :     /* Transient Detector */
     400       20980 :     switch ( st_ivas->nchan_transport )
     401             :     {
     402       20980 :         case 2:
     403             :         case 3:
     404             :         case 4:
     405             :         {
     406             :             int16_t bAttackPresent[PARAM_MC_MAX_TRANSPORT_CHANS];
     407             :             int16_t attackIdx[PARAM_MC_MAX_TRANSPORT_CHANS];
     408             : 
     409       20980 :             set_s( attackIdx, -1, PARAM_MC_MAX_TRANSPORT_CHANS );
     410       20980 :             set_s( bAttackPresent, 0, PARAM_MC_MAX_TRANSPORT_CHANS );
     411             : 
     412       71340 :             for ( ch = 0; ch < st_ivas->nchan_transport; ch++ )
     413             :             {
     414       50360 :                 int16_t cpe_idx = ch / 2;
     415             : 
     416       50360 :                 RunTransientDetection( data_dmx[ch], input_frame, st_ivas->hCPE[cpe_idx]->hCoreCoder[ch - cpe_idx * CPE_CHANNELS]->hTranDet );
     417             : 
     418       50360 :                 ivas_param_mc_transient_detection( hParamMC, st_ivas->hCPE[cpe_idx]->hCoreCoder[ch - cpe_idx * CPE_CHANNELS]->hTranDet, &bAttackPresent[ch], &attackIdx[ch] );
     419             :             }
     420             : 
     421             :             /* if more than one attack, use the earlier */
     422       20980 :             hParamMC->hMetadataPMC.bAttackPresent = 0;
     423       20980 :             hParamMC->hMetadataPMC.attackIndex = 16;
     424             : 
     425       71340 :             for ( ch = 0; ch < st_ivas->nchan_transport; ch++ )
     426             :             {
     427       50360 :                 hParamMC->hMetadataPMC.bAttackPresent = max( hParamMC->hMetadataPMC.bAttackPresent, bAttackPresent[ch] );
     428             :             }
     429             : 
     430       20980 :             if ( hParamMC->hMetadataPMC.bAttackPresent )
     431             :             {
     432        6879 :                 for ( ch = 0; ch < st_ivas->nchan_transport; ch++ )
     433             :                 {
     434        4998 :                     hParamMC->hMetadataPMC.attackIndex = min( hParamMC->hMetadataPMC.attackIndex, attackIdx[ch] );
     435             :                 }
     436             :             }
     437             :             else
     438             :             {
     439       19099 :                 hParamMC->hMetadataPMC.attackIndex = 0;
     440             :             }
     441             :         }
     442       20980 :         break;
     443             : #ifdef DEBUGGING
     444             :         default:
     445             :             assert( !"Number of transport channels not valid for ParamMC!" );
     446             : #endif
     447             :     }
     448             : 
     449             :     /* Encoding */
     450             :     /* parameter estimation*/
     451       20980 :     ivas_param_mc_param_est_enc( hParamMC, data_f, Cy_sum, Cx_sum, input_frame, nchan_inp, st_ivas->nchan_transport );
     452             : 
     453       20980 :     band_step = hParamMC->hMetadataPMC.bAttackPresent ? PARAM_MC_TRANSIENT_BAND_STEP : 1;
     454             : 
     455             : 
     456             :     /* ILD parameter quantization */
     457      284763 :     for ( k = 0; k < hParamMC->hMetadataPMC.nbands_coded; k += band_step )
     458             :     {
     459      263783 :         ivas_param_mc_quantize_ilds( hParamMC, Cy_sum[k], Cx_sum[k], k, nchan_inp, st_ivas->nchan_transport, ILD_idx, ILD_q[k] );
     460             :     }
     461             : 
     462             :     /* ICC parameter quantization */
     463      284763 :     for ( k = 0; k < hParamMC->hMetadataPMC.nbands_coded; k += band_step )
     464             :     {
     465      263783 :         ivas_param_mc_quantize_iccs( hParamMC, Cy_sum[k], k, nchan_inp, ICC_idx );
     466             :     }
     467             : 
     468             :     /* time domain DMX generation*/
     469             :     /* just copy data_dmx generated above, contains already the downmix */
     470       71340 :     for ( ch = 0; ch < st_ivas->nchan_transport; ch++ )
     471             :     {
     472       50360 :         mvr2r( data_dmx[ch], data_f[ch], input_frame );
     473             :     }
     474             : 
     475             :     /* we have to run the transient detector on the second channel of the last CPE if we
     476             :        have an odd number of transport channels */
     477       20980 :     if ( st_ivas->nchan_transport > 2 )
     478             :     {
     479       16800 :         for ( ; ch < st_ivas->nCPE * CPE_CHANNELS; ch++ )
     480             :         {
     481        8400 :             int16_t cpe_idx = ch / 2;
     482             : 
     483        8400 :             set_zero( data_f[ch], input_frame );
     484             : 
     485        8400 :             RunTransientDetection( data_f[ch], input_frame, st_ivas->hCPE[cpe_idx]->hCoreCoder[ch - cpe_idx * CPE_CHANNELS]->hTranDet );
     486             :         }
     487             :     }
     488             : 
     489             :     /* write Parametric MC side info bitstream into temporary buffer*/
     490       20980 :     ivas_param_mc_write_bs( hParamMC, ILD_idx, ICC_idx, bit_buffer, &bit_pos );
     491             : 
     492             :     /* push the Parametric MC side info from the temporary buffer into the medatdata bitstream*/
     493       20980 :     push_next_bits( hMetaData, bit_buffer, bit_pos );
     494             : 
     495             :     /* updates */
     496       20980 :     hParamMC->hMetadataPMC.last_coded_bwidth = hParamMC->hMetadataPMC.coded_bwidth;
     497             : 
     498       20980 :     pop_wmops();
     499             : 
     500       20980 :     return;
     501             : }
     502             : 
     503             : 
     504             : /*****************************************************************************************/
     505             : /* local functions                                                                       */
     506             : /*****************************************************************************************/
     507             : 
     508             : /*-------------------------------------------------------------------------
     509             :  * ivas_param_mc_dmx()
     510             :  *
     511             :  * Computes the time domain down mix signal
     512             :  *------------------------------------------------------------------------*/
     513             : 
     514       20980 : static void ivas_param_mc_dmx(
     515             :     PARAM_MC_ENC_HANDLE hParamMC, /* i/o: Parametric MC encoder handle     */
     516             :     float *data_f[],              /* i  : Input frame                      */
     517             :     float data_dmx[][L_FRAME48k], /* o  : Down mixed frame                 */
     518             :     const int16_t input_frame,    /* i  : Input frame length               */
     519             :     const int16_t nchan_input,    /* i  : number of input channels         */
     520             :     const int16_t nchan_transport /* i  : number of transport channels     */
     521             : )
     522             : {
     523             :     int16_t i;
     524             :     const int16_t *idx;
     525             :     int16_t dmx_ch;
     526             :     int16_t inp_ch;
     527             :     const float *p_dmx_fac;
     528             : 
     529       20980 :     idx = Param_MC_index;
     530    19380980 :     for ( i = 0; i < input_frame; i++ )
     531             :     {
     532    19360000 :         p_dmx_fac = hParamMC->dmx_factors;
     533    65904000 :         for ( dmx_ch = 0; dmx_ch < nchan_transport; dmx_ch++ )
     534             :         {
     535    46544000 :             float *dmx_sample = &data_dmx[idx[dmx_ch]][i];
     536    46544000 :             *dmx_sample = 0.0f;
     537   467600000 :             for ( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
     538             :             {
     539   421056000 :                 ( *dmx_sample ) += data_f[idx[inp_ch]][i] * ( *( p_dmx_fac++ ) );
     540             :             }
     541             :         }
     542             :     }
     543             : 
     544       20980 :     return;
     545             : }
     546             : 
     547             : 
     548             : /*-------------------------------------------------------------------------
     549             :  * ivas_param_mc_param_est_enc()
     550             :  *
     551             :  * run the CLDFB analysis on the input signal
     552             :  * estimate the input and down mix covariances
     553             :  *------------------------------------------------------------------------*/
     554             : 
     555       20980 : static void ivas_param_mc_param_est_enc(
     556             :     PARAM_MC_ENC_HANDLE hParamMC,                                               /* i/o: Parametric MC encoder handle               */
     557             :     float *data_f[],                                                            /* i  : Input frame in the time domain             */
     558             :     float Cy_sum[][MAX_LS_CHANNELS][MAX_LS_CHANNELS],                           /* o  : Covariance matrix for the original frame   */
     559             :     float Cx_sum[][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], /* o  : Covariance matrix for the downmixed frame  */
     560             :     const int16_t input_frame,                                                  /* i  : Input frame length                         */
     561             :     const int16_t nchan_input,                                                  /* i  : number of input channels         */
     562             :     const int16_t nchan_transport                                               /* i  : number of transport channels     */
     563             : )
     564             : {
     565             :     int16_t i, cur_cldfb_band, cur_param_band, ch_idx1, ch_idx2, inp_ch;
     566             :     int16_t ts;
     567             :     int16_t l_ts;
     568             :     int16_t num_time_slots;
     569             :     int16_t num_parameter_bands;
     570             :     int16_t brange[2];
     571             :     int16_t band_step;
     572       20980 :     const int16_t *map_ls = Param_MC_index; /* Loudspeakers mapping */
     573             :     int16_t idx_ls;
     574             :     float *pcm_in[MAX_LS_CHANNELS];
     575             :     float slot_frame_f_real[MAX_LS_CHANNELS][DIRAC_NO_FB_BANDS_MAX]; /* Output of the MDFT FB - real part */
     576             :     float slot_frame_f_imag[MAX_LS_CHANNELS][DIRAC_NO_FB_BANDS_MAX]; /* Output of the MDFT FB - imag part */
     577             :     float *p_slot_frame_f_real[MAX_LS_CHANNELS];                     /* Output of the MDFT FB - real part */
     578             :     float *p_slot_frame_f_imag[MAX_LS_CHANNELS];                     /* Output of the MDFT FB - imag part */
     579             : 
     580             :     float dmx_real[PARAM_MC_MAX_TRANSPORT_CHANS]; /* Downmix channel - Real Part */
     581             :     float dmx_imag[PARAM_MC_MAX_TRANSPORT_CHANS]; /* Downmix channel - Imag Part */
     582             :     float a, b, c, d;                             /* Tmp complex values */
     583             :     float Cy_sum_imag[PARAM_MC_MAX_PARAM_BAND_ABS_COV_ENC][MAX_LS_CHANNELS][MAX_LS_CHANNELS];
     584             :     float Cx_sum_imag[PARAM_MC_MAX_PARAM_BAND_ABS_COV_ENC][PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS];
     585             :     float real_part, imag_part;
     586             :     int16_t start_ts;
     587             :     const float *p_dmx_fac;
     588             : 
     589       20980 :     push_wmops( "param_mc_prm_est" );
     590             : 
     591             :     /* initializations */
     592       20980 :     l_ts = input_frame / PARAM_MC_MDFT_NO_SLOTS;
     593       20980 :     num_time_slots = PARAM_MC_MDFT_NO_SLOTS;
     594       20980 :     start_ts = hParamMC->hMetadataPMC.bAttackPresent ? hParamMC->hMetadataPMC.attackIndex : 0;
     595       20980 :     num_parameter_bands = hParamMC->hMetadataPMC.nbands_coded;
     596       20980 :     band_step = 1;
     597             : 
     598      230780 :     for ( cur_param_band = 0; cur_param_band < PARAM_MC_MAX_PARAM_BAND_ABS_COV_ENC; cur_param_band++ )
     599             :     {
     600     3566600 :         for ( ch_idx1 = 0; ch_idx1 < MAX_LS_CHANNELS; ch_idx1++ )
     601             :         {
     602     3356800 :             set_zero( Cy_sum_imag[cur_param_band][ch_idx1], MAX_LS_CHANNELS );
     603             :         }
     604             : 
     605      839200 :         for ( ch_idx1 = 0; ch_idx1 < PARAM_MC_MAX_TRANSPORT_CHANS; ch_idx1++ )
     606             :         {
     607      629400 :             set_zero( Cx_sum_imag[cur_param_band][ch_idx1], PARAM_MC_MAX_TRANSPORT_CHANS );
     608             :         }
     609             :     }
     610             : 
     611             :     /* Copy current frame to memory for delay compensation */
     612      198720 :     for ( i = 0; i < nchan_input; i++ )
     613             :     {
     614      177740 :         idx_ls = map_ls[i];
     615      177740 :         pcm_in[i] = data_f[idx_ls];
     616      177740 :         p_slot_frame_f_real[i] = &slot_frame_f_real[i][0];
     617      177740 :         p_slot_frame_f_imag[i] = &slot_frame_f_imag[i][0];
     618             :     }
     619             : 
     620       24772 :     for ( ts = 0; ts < start_ts; ts++ )
     621             :     {
     622        3792 :         ivas_fb_mixer_update_prior_input( hParamMC->hFbMixer, pcm_in, l_ts, hParamMC->hFbMixer->fb_cfg->num_in_chans );
     623       37132 :         for ( i = 0; i < nchan_input; i++ )
     624             :         {
     625       33340 :             pcm_in[i] += l_ts;
     626             :         }
     627             :     }
     628             : 
     629      185028 :     for ( ts = start_ts; ts < num_time_slots; ts++ )
     630             :     {
     631      164048 :         ivas_fb_mixer_get_windowed_fr( hParamMC->hFbMixer, pcm_in, p_slot_frame_f_real, p_slot_frame_f_imag, l_ts, l_ts, hParamMC->hFbMixer->fb_cfg->num_in_chans );
     632             : 
     633      164048 :         ivas_fb_mixer_update_prior_input( hParamMC->hFbMixer, pcm_in, l_ts, hParamMC->hFbMixer->fb_cfg->num_in_chans );
     634             : 
     635     1552628 :         for ( i = 0; i < nchan_input; i++ )
     636             :         {
     637     1388580 :             pcm_in[i] += l_ts;
     638             :         }
     639             :         /* Computing the downmix */
     640     1441716 :         for ( cur_param_band = 0; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band++ )
     641             :         {
     642     1277668 :             brange[0] = hParamMC->band_grouping[cur_param_band];
     643     1277668 :             brange[1] = hParamMC->band_grouping[cur_param_band + 1];
     644             : 
     645     4558628 :             for ( cur_cldfb_band = brange[0]; cur_cldfb_band < brange[1]; cur_cldfb_band++ )
     646             :             {
     647             :                 /* Cx for DMX */
     648             :                 /* Real Part */
     649     3280960 :                 p_dmx_fac = hParamMC->dmx_factors;
     650             : 
     651    11152920 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
     652             :                 {
     653     7871960 :                     dmx_real[ch_idx1] = 0.0f;
     654     7871960 :                     dmx_imag[ch_idx1] = 0.0f;
     655    78744600 :                     for ( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
     656             :                     {
     657    70872640 :                         dmx_real[ch_idx1] += slot_frame_f_real[inp_ch][cur_cldfb_band] * ( *p_dmx_fac );
     658    70872640 :                         dmx_imag[ch_idx1] += slot_frame_f_imag[inp_ch][cur_cldfb_band] * ( *p_dmx_fac );
     659    70872640 :                         p_dmx_fac++;
     660             :                     }
     661             :                 }
     662             : 
     663             :                 /* Cx for transport channels */
     664    11152920 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
     665             :                 {
     666    27546000 :                     for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
     667             :                     {
     668    19674040 :                         a = dmx_real[ch_idx1];
     669    19674040 :                         b = dmx_imag[ch_idx1];
     670    19674040 :                         c = dmx_real[ch_idx2];
     671    19674040 :                         d = dmx_imag[ch_idx2];
     672             : 
     673             :                         /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
     674    19674040 :                         Cx_sum[cur_param_band][ch_idx1][ch_idx2] += a * c + b * d;
     675    19674040 :                         Cx_sum_imag[cur_param_band][ch_idx1][ch_idx2] += a * d - b * c;
     676             :                     }
     677             :                 }
     678             : 
     679             :                 /* Cy for input channels */
     680    31052560 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
     681             :                 {
     682   171738200 :                     for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
     683             :                     {
     684   143966600 :                         a = slot_frame_f_real[ch_idx1][cur_cldfb_band];
     685   143966600 :                         b = slot_frame_f_imag[ch_idx1][cur_cldfb_band];
     686   143966600 :                         c = slot_frame_f_real[ch_idx2][cur_cldfb_band];
     687   143966600 :                         d = slot_frame_f_imag[ch_idx2][cur_cldfb_band];
     688             : 
     689             :                         /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
     690   143966600 :                         Cy_sum[cur_param_band][ch_idx1][ch_idx2] += a * c + b * d;
     691   143966600 :                         Cy_sum_imag[cur_param_band][ch_idx1][ch_idx2] += a * d - b * c;
     692             :                     }
     693             :                 }
     694             :             }
     695             :         }
     696             : 
     697     1091924 :         for ( ; cur_param_band < num_parameter_bands; cur_param_band++ )
     698             :         {
     699      927876 :             brange[0] = hParamMC->band_grouping[cur_param_band];
     700      927876 :             brange[1] = hParamMC->band_grouping[cur_param_band + 1];
     701             : 
     702    16463516 :             for ( cur_cldfb_band = brange[0]; cur_cldfb_band < brange[1]; cur_cldfb_band++ )
     703             :             {
     704             :                 /* Cx for DMX */
     705             :                 /* Real Part */
     706    15535640 :                 p_dmx_fac = hParamMC->dmx_factors;
     707             : 
     708    52921880 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
     709             :                 {
     710    37386240 :                     dmx_real[ch_idx1] = 0.0f;
     711    37386240 :                     dmx_imag[ch_idx1] = 0.0f;
     712             : 
     713   376231520 :                     for ( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
     714             :                     {
     715   338845280 :                         dmx_real[ch_idx1] += slot_frame_f_real[inp_ch][cur_cldfb_band] * ( *p_dmx_fac );
     716   338845280 :                         dmx_imag[ch_idx1] += slot_frame_f_imag[inp_ch][cur_cldfb_band] * ( *p_dmx_fac );
     717   338845280 :                         p_dmx_fac++;
     718             :                     }
     719             :                 }
     720             : 
     721             :                 /* Cx for transport channels */
     722    52921880 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
     723             :                 {
     724   131103600 :                     for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
     725             :                     {
     726    93717360 :                         a = dmx_real[ch_idx1];
     727    93717360 :                         b = dmx_imag[ch_idx1];
     728    93717360 :                         c = dmx_real[ch_idx2];
     729    93717360 :                         d = dmx_imag[ch_idx2];
     730             : 
     731             :                         /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
     732    93717360 :                         Cx_sum[cur_param_band][ch_idx1][ch_idx2] += a * c + b * d;
     733             :                     }
     734             :                 }
     735             : 
     736             :                 /* Cy for input channels */
     737   147763960 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
     738             :                 {
     739   822256960 :                     for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
     740             :                     {
     741   690028640 :                         a = slot_frame_f_real[ch_idx1][cur_cldfb_band];
     742   690028640 :                         b = slot_frame_f_imag[ch_idx1][cur_cldfb_band];
     743   690028640 :                         c = slot_frame_f_real[ch_idx2][cur_cldfb_band];
     744   690028640 :                         d = slot_frame_f_imag[ch_idx2][cur_cldfb_band];
     745             : 
     746             :                         /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
     747   690028640 :                         Cy_sum[cur_param_band][ch_idx1][ch_idx2] += a * c + b * d;
     748             :                     }
     749             :                 }
     750             :             }
     751             :         }
     752             :     }
     753             : 
     754             :     /* make sure energy and correlation is zero above the relevant LFE bands for LFE
     755             :      * avoids wrong energy in case of band combining at transients */
     756       20980 :     if ( hParamMC->lfe_index >= 0 )
     757             :     {
     758      163360 :         for ( cur_param_band = PARAM_MC_MAX_BAND_LFE; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band++ )
     759             :         {
     760     1360600 :             for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
     761             :             {
     762     1218220 :                 Cy_sum[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0.0f;
     763     1218220 :                 Cy_sum[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0.0f;
     764     1218220 :                 Cy_sum_imag[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0.0f;
     765     1218220 :                 Cy_sum_imag[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0.0f;
     766             :             }
     767             :         }
     768             : 
     769      139620 :         for ( ; cur_param_band < num_parameter_bands; cur_param_band++ )
     770             :         {
     771     1138480 :             for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
     772             :             {
     773     1019840 :                 Cy_sum[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0.0f;
     774     1019840 :                 Cy_sum[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0.0f;
     775             :             }
     776             :         }
     777             :     }
     778             : 
     779       20980 :     if ( !hParamMC->hMetadataPMC.bAttackPresent )
     780             :     {
     781             :         const PARAM_MC_ILD_MAPPING *h_ild_mapping;
     782             :         int16_t ild_attack;
     783       19099 :         ild_attack = 0;
     784       19099 :         h_ild_mapping = hParamMC->hMetadataPMC.ild_mapping_conf;
     785             :         /* create ILDs for to non transmitted parameter bands (only lower half) */
     786      149166 :         for ( cur_param_band = 0; cur_param_band < hParamMC->hMetadataPMC.num_parameter_bands / 2; cur_param_band++ )
     787             :         {
     788             :             float ILD[PARAM_MC_SZ_ILD_MAP];
     789             :             int16_t k;
     790             :             int16_t num_ilds_to_code;
     791             : 
     792      130067 :             if ( cur_param_band >= PARAM_MC_MAX_BAND_LFE )
     793             :             {
     794      110968 :                 num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_wo_lfe;
     795             :             }
     796             :             else
     797             :             {
     798       19099 :                 num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
     799             :             }
     800      130067 :             if ( hParamMC->hMetadataPMC.param_frame_idx != hParamMC->hMetadataPMC.coding_band_mapping[cur_param_band] )
     801             :             {
     802             :                 float Nrg[MAX_LS_CHANNELS];
     803             : 
     804             :                 /* get ICLDs */
     805      624618 :                 for ( k = 0; k < nchan_input; ++k )
     806             :                 {
     807      558622 :                     Nrg[k] = Cy_sum[cur_param_band][k][k];
     808             :                 }
     809      545054 :                 for ( k = 0; k < num_ilds_to_code; ++k )
     810             :                 {
     811      479058 :                     float ref_ener = 0.0f;
     812             :                     int16_t ref_channel_cnt;
     813             :                     int16_t ref_channel_idx;
     814             : 
     815     1003080 :                     for ( ref_channel_cnt = 0; ref_channel_cnt < h_ild_mapping->num_ref_channels[k]; ref_channel_cnt++ )
     816             :                     {
     817      524022 :                         ref_channel_idx = h_ild_mapping->ref_channel_idx[k][ref_channel_cnt];
     818      524022 :                         ref_ener += Cx_sum[cur_param_band][ref_channel_idx][ref_channel_idx];
     819             :                     }
     820      479058 :                     ILD[k] = 10.0f * log10f( ( Nrg[h_ild_mapping->ild_index[k]] + EPSILON ) / ( hParamMC->hMetadataPMC.ild_factors[k] * ref_ener + EPSILON ) );
     821      479058 :                     if ( hParamMC->prev_ilds[cur_param_band][k] - ILD[k] > param_mc_ild_diff_threshold[cur_param_band] )
     822             :                     {
     823       22865 :                         ild_attack++;
     824             :                     }
     825             :                 }
     826             :             }
     827             :         }
     828             :         /* check if the ILDs change too much -> go into transient mode... */
     829       19099 :         if ( ild_attack > PARAM_MC_NUM_ATTACK_ILD_THRESH )
     830             :         {
     831         782 :             hParamMC->hMetadataPMC.bAttackPresent = 1;
     832             :         }
     833             :     }
     834             : 
     835             : 
     836       20980 :     if ( hParamMC->hMetadataPMC.bAttackPresent )
     837             :     {
     838             :         /* combine bands */
     839       13170 :         for ( cur_param_band = 1; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band += 2 )
     840             :         {
     841       39659 :             for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
     842             :             {
     843      111870 :                 for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
     844             :                 {
     845       82718 :                     Cx_sum[cur_param_band - 1][ch_idx1][ch_idx2] += Cx_sum[cur_param_band][ch_idx1][ch_idx2];
     846       82718 :                     Cx_sum_imag[cur_param_band - 1][ch_idx1][ch_idx2] += Cx_sum_imag[cur_param_band][ch_idx1][ch_idx2];
     847             :                 }
     848             :             }
     849             : 
     850      122519 :             for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
     851             :             {
     852      793366 :                 for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
     853             :                 {
     854      681354 :                     Cy_sum[cur_param_band - 1][ch_idx1][ch_idx2] += Cy_sum[cur_param_band][ch_idx1][ch_idx2];
     855      681354 :                     Cy_sum_imag[cur_param_band - 1][ch_idx1][ch_idx2] += Cy_sum_imag[cur_param_band][ch_idx1][ch_idx2];
     856             :                 }
     857             :             }
     858             :         }
     859             : 
     860       10373 :         for ( ; cur_param_band < num_parameter_bands; cur_param_band += 2 )
     861             :         {
     862        7710 :             if ( cur_param_band < num_parameter_bands )
     863             :             {
     864       29240 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
     865             :                 {
     866       82920 :                     for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
     867             :                     {
     868       61390 :                         Cx_sum[cur_param_band - 1][ch_idx1][ch_idx2] += Cx_sum[cur_param_band][ch_idx1][ch_idx2];
     869             :                     }
     870             :                 }
     871             : 
     872       90502 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
     873             :                 {
     874      588560 :                     for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
     875             :                     {
     876      505768 :                         Cy_sum[cur_param_band - 1][ch_idx1][ch_idx2] += Cy_sum[cur_param_band][ch_idx1][ch_idx2];
     877             :                     }
     878             :                 }
     879             :             }
     880             :         }
     881             : 
     882        2663 :         band_step = 2;
     883             :     }
     884             : 
     885             : 
     886             :     /* map complex covariances to real values */
     887      173833 :     for ( cur_param_band = 0; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band += band_step )
     888             :     {
     889             :         /* Cx for transport channels */
     890      518881 :         for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ch_idx1++ )
     891             :         {
     892     1279050 :             for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ch_idx2++ )
     893             :             {
     894      913022 :                 real_part = Cx_sum[cur_param_band][ch_idx1][ch_idx2];
     895      913022 :                 imag_part = Cx_sum_imag[cur_param_band][ch_idx1][ch_idx2];
     896             : 
     897             :                 /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
     898      913022 :                 Cx_sum[cur_param_band][ch_idx1][ch_idx2] = sqrtf( real_part * real_part + imag_part * imag_part );
     899             :             }
     900             :         }
     901             : 
     902             :         /* Cy for transport channels */
     903     1436801 :         for ( ch_idx1 = 0; ch_idx1 < nchan_input; ch_idx1++ )
     904             :         {
     905     7892774 :             for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ch_idx2++ )
     906             :             {
     907     6608826 :                 real_part = Cy_sum[cur_param_band][ch_idx1][ch_idx2];
     908     6608826 :                 imag_part = Cy_sum_imag[cur_param_band][ch_idx1][ch_idx2];
     909     6608826 :                 Cy_sum[cur_param_band][ch_idx1][ch_idx2] = sqrtf( real_part * real_part + imag_part * imag_part );
     910             :             }
     911             :         }
     912             :     }
     913             : 
     914       20980 :     if ( Cy_sum[0][LFE_CHANNEL][LFE_CHANNEL] < PARAM_MC_LFE_ON_THRESH )
     915             :     {
     916       12151 :         hParamMC->hMetadataPMC.lfe_on = 0;
     917             :     }
     918             :     else
     919             :     {
     920        8829 :         hParamMC->hMetadataPMC.lfe_on = 1;
     921             :     }
     922             : 
     923       20980 :     pop_wmops();
     924             : 
     925       20980 :     return;
     926             : }
     927             : 
     928             : 
     929             : /*-------------------------------------------------------------------------
     930             :  * ivas_param_mc_quantize_ilds()
     931             :  *
     932             :  * Quantize the ILD parameters
     933             :  *------------------------------------------------------------------------*/
     934             : 
     935      263783 : static void ivas_param_mc_quantize_ilds(
     936             :     PARAM_MC_ENC_HANDLE hParamMC,                                         /* i/o: Parametric MC encoder handle    */
     937             :     float Cy[MAX_LS_CHANNELS][MAX_LS_CHANNELS],                           /* i  : Covariance matrix of the input  */
     938             :     float Cx[PARAM_MC_MAX_TRANSPORT_CHANS][PARAM_MC_MAX_TRANSPORT_CHANS], /* i  : Covariance matrix of the dmx    */
     939             :     const int16_t freq_idx,                                               /* i  : frequency index being processed */
     940             :     const int16_t nchan_input,                                            /* i  : number of input channels        */
     941             :     const int16_t nchan_transport,                                        /* i  : number of transport channels    */
     942             :     int16_t *ILD_idx_out,                                                 /* o  : ILD indices                     */
     943             :     float ILD_q[PARAM_MC_SZ_ILD_MAP]                                      /* o  : Quanzited ILD matrix            */
     944             : )
     945             : {
     946             :     int16_t k;
     947             :     int16_t Ny;
     948             :     int16_t num_ilds_to_code;
     949             :     int16_t ild_map_size;
     950             :     float Nrg[MAX_LS_CHANNELS];
     951             :     float ILD[PARAM_MC_SZ_ILD_MAP];
     952             :     const PARAM_MC_ILD_MAPPING *h_ild_mapping;
     953             :     float tot_ener, dmx_ener, ener_fac, delta_fac;
     954             :     int16_t ILD_idx[PARAM_MC_SZ_ILD_MAP];
     955             : 
     956      263783 :     push_wmops( "param_mc_prm_q" );
     957             : 
     958             :     /* Initialization */
     959      263783 :     set_zero( Nrg, MAX_LS_CHANNELS );
     960      263783 :     set_zero( ILD, PARAM_MC_SZ_ILD_MAP );
     961             : 
     962      263783 :     Ny = nchan_input;
     963             : 
     964      263783 :     h_ild_mapping = hParamMC->hMetadataPMC.ild_mapping_conf;
     965      263783 :     ild_map_size = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
     966      263783 :     if ( freq_idx >= PARAM_MC_MAX_BAND_LFE )
     967             :     {
     968      242803 :         num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_wo_lfe;
     969             :     }
     970             :     else
     971             :     {
     972       20980 :         num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
     973             :     }
     974             : 
     975             :     /* Downsampling */
     976      263783 :     if ( ( hParamMC->hMetadataPMC.bAttackPresent == 0 ) && ( hParamMC->hMetadataPMC.param_frame_idx != hParamMC->hMetadataPMC.coding_band_mapping[freq_idx] ) )
     977             :     {
     978      122721 :         pop_wmops();
     979             : 
     980      122721 :         return;
     981             :     }
     982             : 
     983             :     /* get ICLDs */
     984     1349416 :     for ( k = 0; k < Ny; ++k )
     985             :     {
     986     1208354 :         Nrg[k] = Cy[k][k];
     987             :     }
     988             : 
     989             :     /* limit ILDs if DMX energy is lower than sum of channel energies */
     990      141062 :     tot_ener = 0.0f;
     991      141062 :     dmx_ener = 0.0f;
     992             : 
     993     1349416 :     for ( k = 0; k < ild_map_size; k++ )
     994             :     {
     995     1208354 :         if ( k != hParamMC->lfe_index || hParamMC->hMetadataPMC.lfe_on )
     996             :         {
     997     1128238 :             tot_ener += Nrg[k];
     998             :         }
     999             :     }
    1000             : 
    1001      483090 :     for ( k = 0; k < nchan_transport; k++ )
    1002             :     {
    1003      342028 :         dmx_ener += Cx[k][k];
    1004             :     }
    1005      141062 :     ener_fac = 10.0f * log10f( ( tot_ener + EPSILON ) / ( dmx_ener + EPSILON ) );
    1006             : 
    1007      141062 :     if ( ener_fac > PARAM_MC_ENER_LIMIT_INTRAFRAME )
    1008             :     {
    1009             :         float limit_fac;
    1010          21 :         limit_fac = powf( 10.0f, ( ( 0.3f * logf( ener_fac - PARAM_MC_ENER_LIMIT_INTRAFRAME + 1.0f ) - ( ener_fac - PARAM_MC_ENER_LIMIT_INTRAFRAME ) ) / 10.0f ) );
    1011          21 :         v_multc( Nrg, limit_fac, Nrg, num_ilds_to_code );
    1012             :     }
    1013             : 
    1014             :     /* limit ILD jumps in non-tranient frames */
    1015      141062 :     tot_ener = 0.0f;
    1016      141062 :     dmx_ener = 0.0f;
    1017             : 
    1018     1349416 :     for ( k = 0; k < ild_map_size; k++ )
    1019             :     {
    1020     1208354 :         if ( k != hParamMC->lfe_index || hParamMC->hMetadataPMC.lfe_on )
    1021             :         {
    1022     1128238 :             tot_ener += Nrg[k];
    1023             :         }
    1024             :     }
    1025             : 
    1026      483090 :     for ( k = 0; k < nchan_transport; k++ )
    1027             :     {
    1028      342028 :         dmx_ener += Cx[k][k];
    1029             :     }
    1030             : 
    1031      141062 :     ener_fac = 10.0f * log10f( ( tot_ener + EPSILON ) / ( dmx_ener + EPSILON ) );
    1032      141062 :     delta_fac = ener_fac - hParamMC->ener_fac[freq_idx];
    1033             : 
    1034      141062 :     if ( !hParamMC->hMetadataPMC.bAttackPresent && ( delta_fac > PARAM_MC_ENER_LIMIT_INTERFRAME ) && ( delta_fac < PARAM_MC_ENER_LIMIT_MAX_DELTA_FAC ) )
    1035             :     {
    1036             :         float limit_fac;
    1037         734 :         limit_fac = powf( 10.0f, ( ( 0.3f * logf( delta_fac - PARAM_MC_ENER_LIMIT_INTERFRAME + 1.0f ) - ( delta_fac - PARAM_MC_ENER_LIMIT_INTERFRAME ) ) / 10.0f ) );
    1038         734 :         v_multc( Nrg, limit_fac, Nrg, num_ilds_to_code );
    1039         734 :         ener_fac += 10.0f * log10f( limit_fac );
    1040             :     }
    1041             : 
    1042      141062 :     hParamMC->ener_fac[freq_idx] = ener_fac;
    1043             : 
    1044             :     /* update also combined bands ener_fac when in transient frame */
    1045      141062 :     if ( hParamMC->hMetadataPMC.bAttackPresent && ( ( freq_idx + 1 ) < hParamMC->hMetadataPMC.nbands_coded ) )
    1046             :     {
    1047       18217 :         hParamMC->ener_fac[freq_idx + 1] = ener_fac;
    1048             :     }
    1049             : 
    1050     1165986 :     for ( k = 0; k < num_ilds_to_code; ++k )
    1051             :     {
    1052     1024924 :         float ref_ener = 0.0f;
    1053             :         int16_t ref_channel_cnt;
    1054             :         int16_t ref_channel_idx;
    1055             : 
    1056     2137642 :         for ( ref_channel_cnt = 0; ref_channel_cnt < h_ild_mapping->num_ref_channels[k]; ref_channel_cnt++ )
    1057             :         {
    1058     1112718 :             ref_channel_idx = h_ild_mapping->ref_channel_idx[k][ref_channel_cnt];
    1059     1112718 :             ref_ener += Cx[ref_channel_idx][ref_channel_idx];
    1060             :         }
    1061     1024924 :         ILD[k] = 10.0f * log10f( ( Nrg[h_ild_mapping->ild_index[k]] + EPSILON ) / ( hParamMC->hMetadataPMC.ild_factors[k] * ref_ener + EPSILON ) );
    1062     1024924 :         hParamMC->prev_ilds[freq_idx][k] = ILD[k];
    1063     1024924 :         if ( hParamMC->hMetadataPMC.bAttackPresent && ( ( freq_idx + 1 ) < hParamMC->hMetadataPMC.nbands_coded ) )
    1064             :         {
    1065      167020 :             hParamMC->prev_ilds[freq_idx + 1][k] = ILD[k];
    1066             :         }
    1067             :     }
    1068             : 
    1069             : 
    1070             :     /* quantize parameters */
    1071      141062 :     ivas_param_mc_parameter_quantizer( ILD, num_ilds_to_code, hParamMC->hMetadataPMC.ild_coding.quantizer_size, hParamMC->hMetadataPMC.ild_coding.quantizer, ILD_idx, ILD_q );
    1072             : 
    1073             :     /* Save current quantized ICLDs */
    1074      141062 :     mvs2s( ILD_idx, ILD_idx_out + freq_idx * ild_map_size, num_ilds_to_code );
    1075             : 
    1076      141062 :     pop_wmops();
    1077             : 
    1078      141062 :     return;
    1079             : }
    1080             : 
    1081             : 
    1082             : /*-------------------------------------------------------------------------
    1083             :  * ivas_param_mc_quantize_iccs()
    1084             :  *
    1085             :  * Quantize the ILD parameters
    1086             :  *------------------------------------------------------------------------*/
    1087             : 
    1088      263783 : static void ivas_param_mc_quantize_iccs(
    1089             :     PARAM_MC_ENC_HANDLE hParamMC,               /* i/o: Parametric MC encoder handle    */
    1090             :     float Cy[MAX_LS_CHANNELS][MAX_LS_CHANNELS], /* i  : Covariance matrix of the input  */
    1091             :     const int16_t freq_idx,                     /* i  : frequency index being processed */
    1092             :     const int16_t nchan_input,                  /* i  : number of input channels        */
    1093             :     int16_t *ICC_idx_out                        /* o  : quantizer indices               */
    1094             : )
    1095             : {
    1096             :     int16_t i, k;
    1097             :     int16_t Ny;
    1098             :     int16_t num_iccs_to_code;
    1099             :     int16_t icc_map_size;
    1100             :     float a;
    1101             :     int16_t tmp_map[2];
    1102             :     float Nrg[MAX_LS_CHANNELS];
    1103             :     float ICC_vect[PARAM_MC_SZ_ICC_MAP];
    1104             :     float ICC_vect_q[PARAM_MC_SZ_ICC_MAP];
    1105             :     int16_t ICC_idx[PARAM_MC_SZ_ICC_MAP];
    1106             : 
    1107             :     /* Initialization */
    1108      263783 :     set_zero( Nrg, MAX_LS_CHANNELS );
    1109      263783 :     set_zero( ICC_vect, PARAM_MC_SZ_ICC_MAP );
    1110      263783 :     set_zero( ICC_vect_q, PARAM_MC_SZ_ICC_MAP );
    1111             : 
    1112      263783 :     Ny = nchan_input;
    1113             : 
    1114             :     /* Downsampling */
    1115      263783 :     if ( ( hParamMC->hMetadataPMC.bAttackPresent == 0 ) && ( hParamMC->hMetadataPMC.param_frame_idx != hParamMC->hMetadataPMC.coding_band_mapping[freq_idx] ) )
    1116             :     {
    1117      122721 :         return;
    1118             :     }
    1119             : 
    1120      141062 :     icc_map_size = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_lfe;
    1121      141062 :     num_iccs_to_code = icc_map_size;
    1122             : 
    1123      141062 :     if ( freq_idx >= PARAM_MC_MAX_BAND_LFE )
    1124             :     {
    1125      128976 :         num_iccs_to_code = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_wo_lfe;
    1126             :     }
    1127             : 
    1128             :     /* Get ICC matrix from Cy */
    1129     1349416 :     for ( k = 0; k < Ny; ++k )
    1130             :     {
    1131     1208354 :         Nrg[k] = Cy[k][k];
    1132     1208354 :         a = 1.0f / ( sqrtf( Nrg[k] ) + EPSILON );
    1133             : 
    1134     7532629 :         for ( i = k; i < Ny; ++i )
    1135             :         {
    1136     6324275 :             Cy[k][i] = Cy[k][i] * a;
    1137             :         }
    1138             : 
    1139     7532629 :         for ( i = 0; i <= k; i++ )
    1140             :         {
    1141     6324275 :             Cy[i][k] = Cy[i][k] * a;
    1142             :         }
    1143             :     }
    1144             : 
    1145             :     /* set ICCs for zero channels to 1 to avoid artifacts in the decoded signal */
    1146     1349416 :     for ( k = 0; k < Ny; ++k )
    1147             :     {
    1148     1208354 :         if ( Nrg[k] == 0.0f )
    1149             :         {
    1150      931348 :             for ( i = k; i < Ny; ++i )
    1151             :             {
    1152      786331 :                 Cy[k][i] = 1.0f;
    1153             :             }
    1154             : 
    1155      798389 :             for ( i = 0; i <= k; ++i )
    1156             :             {
    1157      653372 :                 Cy[i][k] = 1.0f;
    1158             :             }
    1159             :         }
    1160             :     }
    1161             : 
    1162             :     /* Reduce set of parameters and quantize them */
    1163     1079378 :     for ( k = 0; k < num_iccs_to_code; ++k )
    1164             :     {
    1165      938316 :         tmp_map[0] = hParamMC->hMetadataPMC.icc_mapping_conf->icc_mapping[k][0];
    1166      938316 :         tmp_map[1] = hParamMC->hMetadataPMC.icc_mapping_conf->icc_mapping[k][1];
    1167      938316 :         ICC_vect[k] = Cy[tmp_map[0]][tmp_map[1]];
    1168             :     }
    1169             : 
    1170             :     /* Quantization */
    1171      141062 :     ivas_param_mc_parameter_quantizer( ICC_vect, num_iccs_to_code, hParamMC->hMetadataPMC.icc_coding.quantizer_size, hParamMC->hMetadataPMC.icc_coding.quantizer, ICC_idx, ICC_vect_q );
    1172             : 
    1173             :     /* Save current quantized ICCs */
    1174      141062 :     mvs2s( ICC_idx, ICC_idx_out + freq_idx * icc_map_size, num_iccs_to_code );
    1175             : 
    1176      141062 :     return;
    1177             : }
    1178             : 
    1179             : 
    1180             : /*-------------------------------------------------------------------------
    1181             :  * ivas_param_mc_parameter_quantizer()
    1182             :  *
    1183             :  * Parameter Quantization
    1184             :  *------------------------------------------------------------------------*/
    1185             : 
    1186      282124 : static void ivas_param_mc_parameter_quantizer(
    1187             :     const float *x,             /* i  : input sequence      */
    1188             :     const int16_t L,            /* i  : input length        */
    1189             :     const int16_t sz_quantizer, /* i  : quantizer size      */
    1190             :     const float *quantizer,     /* i  : quantizer table     */
    1191             :     int16_t *quant_idx,         /* o  : quant indices       */
    1192             :     float *y                    /* o  : output sequence     */
    1193             : )
    1194             : {
    1195             :     int16_t idx, i;
    1196             :     int16_t idx_min;
    1197             :     float tmp_min;
    1198             : 
    1199      282124 :     set_f( y, 0.0f, L );
    1200      282124 :     idx_min = 0;
    1201             : 
    1202     2245364 :     for ( idx = 0; idx < L; ++idx )
    1203             :     {
    1204     1963240 :         tmp_min = 1000.0f;
    1205    25868552 :         for ( i = 0; i < sz_quantizer; ++i )
    1206             :         {
    1207    23905312 :             if ( fabsf( x[idx] - quantizer[i] ) < tmp_min )
    1208             :             {
    1209    11606008 :                 tmp_min = fabsf( x[idx] - quantizer[i] );
    1210    11606008 :                 idx_min = i;
    1211             :             }
    1212             :         }
    1213             : 
    1214     1963240 :         y[idx] = quantizer[idx_min];
    1215     1963240 :         quant_idx[idx] = idx_min;
    1216             :     }
    1217             : 
    1218      282124 :     return;
    1219             : }
    1220             : 
    1221             : 
    1222             : /*-------------------------------------------------------------------------
    1223             :  * ivas_param_mc_transient_detection()
    1224             :  *
    1225             :  * Detect if the current frame has a transient
    1226             :  *------------------------------------------------------------------------*/
    1227             : 
    1228       50360 : static void ivas_param_mc_transient_detection(
    1229             :     PARAM_MC_ENC_HANDLE hParamMC, /* i  : Parametric MC encoder handle                                      */
    1230             :     TRAN_DET_HANDLE hTranDet,     /* i  : Transient detector handle from core coder for a transport channel */
    1231             :     int16_t *pbIsAttackPresent,   /* o  : Flag for indicating a found transient                             */
    1232             :     int16_t *pAttackIndex         /* o  : Attack position (0 if no attack)                                  */
    1233             : )
    1234             : {
    1235             :     int16_t i;
    1236             :     int16_t bIsAttackPresent, attackIndex;
    1237             :     float *pSubblockNrg;
    1238             :     float *pAccSubblockNrg;
    1239             :     float attackRatioThreshold;
    1240             : 
    1241       50360 :     push_wmops( "param_mc_trn_det" );
    1242             : 
    1243       50360 :     attackRatioThreshold = hTranDet->transientDetector.attackRatioThreshold;
    1244       50360 :     pSubblockNrg = &hTranDet->subblockEnergies.subblockNrg[hParamMC->transient_detector_delay];
    1245       50360 :     pAccSubblockNrg = &hTranDet->subblockEnergies.accSubblockNrg[hParamMC->transient_detector_delay];
    1246             : 
    1247       50360 :     bIsAttackPresent = FALSE;
    1248       50360 :     attackIndex = 16;
    1249             : 
    1250             :     /* Search for the last attack in the subblocks,
    1251             :      * if we had an attack very late in the last frame,
    1252             :      * make the current frame also a transient one... */
    1253       50360 :     if ( ( pSubblockNrg[-1] > pAccSubblockNrg[-1] * attackRatioThreshold ) || ( pSubblockNrg[-2] > pAccSubblockNrg[-2] * attackRatioThreshold ) )
    1254             :     {
    1255          72 :         bIsAttackPresent = TRUE;
    1256          72 :         attackIndex = 0;
    1257             :     }
    1258             : 
    1259      453240 :     for ( i = 0; i < NSUBBLOCKS; i++ )
    1260             :     {
    1261      402880 :         if ( pSubblockNrg[i] > pAccSubblockNrg[i] * attackRatioThreshold )
    1262             :         {
    1263        2410 :             bIsAttackPresent = TRUE;
    1264        2410 :             attackIndex = i;
    1265             :         }
    1266             :     }
    1267             : 
    1268             :     /* avoid post-echos on click sounds (very short transients) due to TNS aliasing */
    1269       50360 :     *pAttackIndex = attackIndex;
    1270       50360 :     *pbIsAttackPresent = bIsAttackPresent;
    1271             : 
    1272       50360 :     pop_wmops();
    1273             : 
    1274       50360 :     return;
    1275             : }
    1276             : 
    1277             : 
    1278             : /*-------------------------------------------------------------------------
    1279             :  * ivas_param_mc_entropy_encoder()
    1280             :  *
    1281             :  * Write the metadata bitstream
    1282             :  *------------------------------------------------------------------------*/
    1283             : 
    1284       20980 : static void ivas_param_mc_write_bs(
    1285             :     const PARAM_MC_ENC_HANDLE hParamMC,     /* i/o: Parametric MC encoder Handle   */
    1286             :     int16_t *ILD_idx,                       /* i  : ILD quantizer indices sequence */
    1287             :     int16_t *ICC_idx,                       /* i  : ICC quantizer indices sequence */
    1288             :     uint16_t bit_buffer[PARAM_MC_MAX_BITS], /* o  : Output bit buffer              */
    1289             :     int16_t *bit_pos                        /* o  : Number of bits used            */
    1290             : )
    1291             : {
    1292             :     int16_t i, pos;
    1293             :     int16_t nbands;
    1294             :     int16_t band_step;
    1295             :     int16_t seq_tmp[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP];
    1296             :     float seq_tmp_uni[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP];
    1297             :     int16_t icc_map_size_wo_lfe;
    1298             :     int16_t icc_map_size;
    1299             :     int16_t ild_map_size_wo_lfe;
    1300             :     int16_t ild_map_size;
    1301             : 
    1302       20980 :     push_wmops( "param_mc_prm_enc" );
    1303             : 
    1304             :     /* Init */
    1305       20980 :     set_zero( seq_tmp_uni, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP );
    1306       20980 :     set_s( seq_tmp, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP );
    1307       20980 :     nbands = hParamMC->hMetadataPMC.nbands_in_param_frame[hParamMC->hMetadataPMC.param_frame_idx];
    1308       20980 :     icc_map_size_wo_lfe = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_wo_lfe;
    1309       20980 :     icc_map_size = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_lfe;
    1310       20980 :     ild_map_size_wo_lfe = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_wo_lfe;
    1311       20980 :     ild_map_size = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
    1312             : 
    1313             :     /*-----------------------------------------------------------------*
    1314             :      * Signaling bits
    1315             :      *-----------------------------------------------------------------*/
    1316             : 
    1317             :     /* reserved bit */
    1318       20980 :     bit_buffer[( *bit_pos )++] = hParamMC->hMetadataPMC.lfe_on;
    1319             : 
    1320             :     /* write coded band width */
    1321       20980 :     i = hParamMC->hMetadataPMC.coded_bwidth;
    1322       62940 :     for ( pos = 0; pos < 2; pos++ )
    1323             :     {
    1324       41960 :         bit_buffer[( *bit_pos )++] = (uint16_t) ( ( i >> pos ) & 1 );
    1325             :     }
    1326             : 
    1327             :     /* write param frame indicator */
    1328       20980 :     bit_buffer[( *bit_pos )++] = hParamMC->hMetadataPMC.param_frame_idx;
    1329             : 
    1330             :     /* write transient frame indicator */
    1331       20980 :     bit_buffer[( *bit_pos )++] = hParamMC->hMetadataPMC.bAttackPresent;
    1332             : 
    1333       20980 :     band_step = 1;
    1334       20980 :     if ( hParamMC->hMetadataPMC.bAttackPresent )
    1335             :     {
    1336        2663 :         band_step = PARAM_MC_TRANSIENT_BAND_STEP;
    1337       10652 :         for ( pos = 2; pos >= 0; --pos )
    1338             :         {
    1339        7989 :             bit_buffer[( *bit_pos )++] = (uint16_t) ( ( hParamMC->hMetadataPMC.attackIndex >> pos ) & 1 );
    1340             :         }
    1341        2663 :         nbands = hParamMC->hMetadataPMC.nbands_coded / band_step + ( ( hParamMC->hMetadataPMC.nbands_coded % band_step ) ? 1 : 0 );
    1342             :     }
    1343             : 
    1344       20980 :     ivas_param_mc_encode_parameter( ICC_idx, &hParamMC->hMetadataPMC, &hParamMC->hMetadataPMC.icc_coding,
    1345             :                                     nbands, band_step, icc_map_size_wo_lfe, icc_map_size, bit_buffer, bit_pos );
    1346             : 
    1347       20980 :     ivas_param_mc_encode_parameter( ILD_idx, &hParamMC->hMetadataPMC, &hParamMC->hMetadataPMC.ild_coding,
    1348             :                                     nbands, band_step, ild_map_size_wo_lfe, ild_map_size, bit_buffer, bit_pos );
    1349       20980 :     pop_wmops();
    1350             : 
    1351       20980 :     return;
    1352             : }
    1353             : 
    1354             : 
    1355             : /*-------------------------------------------------------------------------
    1356             :  * ivas_param_mc_encode_parameter()
    1357             :  *
    1358             :  * (entropy) encode a sequence of parameter indices
    1359             :  *------------------------------------------------------------------------*/
    1360             : 
    1361       41960 : static void ivas_param_mc_encode_parameter(
    1362             :     int16_t *quant_idx,                                         /* i  : indices sequence to encode              */
    1363             :     HANDLE_IVAS_PARAM_MC_METADATA hMetadataPMC,                 /* i  : Parametric MC metadata handle           */
    1364             :     HANDLE_PARAM_MC_PARAMETER_CODING_INFO hParameterCodingInfo, /* i  : parameter quantization and coding info  */
    1365             :     const int16_t nbands,                                       /* i  : number of parameter bands to encode     */
    1366             :     const int16_t band_step,                                    /* i  : parameter band step                     */
    1367             :     const int16_t map_size_wo_lfe,                              /* i  : number of parameters per band (w/o LFE) */
    1368             :     const int16_t map_size,                                     /* i  : number of parameters per band           */
    1369             :     uint16_t bit_buffer[PARAM_MC_MAX_BITS],                     /* o  : Output bit buffer                       */
    1370             :     int16_t *bit_pos                                            /* o  : Number of bits used                     */
    1371             : )
    1372             : {
    1373             :     int16_t sz_seq;
    1374             :     int16_t idx_prev;
    1375             :     int16_t idx_offset;
    1376             :     int16_t bit_cnt_uni;
    1377             :     int16_t bit_cnt_range;
    1378             :     int16_t bit_cnt_range_diff;
    1379             :     int16_t bit_cnt_range_min;
    1380             :     int16_t bit_pos_tmp;
    1381             :     int16_t i, j;
    1382             :     int16_t idx;
    1383             :     int16_t seq_delta[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
    1384             :     int16_t seq[PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_MAX_VAL_MAP_SIZE];
    1385             :     uint16_t tmp_bit_buffer[PARAM_MC_MAX_BITS];
    1386             :     uint16_t tmp_bit_buffer_diff[PARAM_MC_MAX_BITS];
    1387             : 
    1388             :     /* Inits */
    1389       41960 :     sz_seq = nbands * ( map_size_wo_lfe );
    1390             : 
    1391             :     /* Computing Delta Sequence */
    1392       41960 :     idx_prev = hParameterCodingInfo->quantizer_size / 2 + hParameterCodingInfo->quantizer_size % 2 - 1;
    1393       41960 :     idx_offset = hParameterCodingInfo->quantizer_size - 1;
    1394             : 
    1395      326100 :     for ( j = 0; j < map_size_wo_lfe; ++j )
    1396             :     {
    1397      284140 :         int16_t coding_band = 0;
    1398             : 
    1399     3829251 :         for ( i = 0; i < hMetadataPMC->nbands_coded; i += band_step )
    1400             :         {
    1401     3545111 :             if ( hMetadataPMC->bAttackPresent || hMetadataPMC->param_frame_idx == hMetadataPMC->coding_band_mapping[i] )
    1402             :             {
    1403     1933618 :                 idx = quant_idx[i * map_size + j];
    1404     1933618 :                 seq[coding_band + j * nbands] = idx;
    1405     1933618 :                 seq_delta[coding_band + j * nbands] = idx - idx_prev + idx_offset;
    1406     1933618 :                 idx_prev = idx;
    1407     1933618 :                 coding_band++;
    1408             :             }
    1409             :         }
    1410             :     }
    1411             : 
    1412             :     /* LFE */
    1413       41960 :     if ( hMetadataPMC->lfe_on )
    1414             :     {
    1415       35316 :         for ( i = 0; i < PARAM_MC_MAX_BAND_LFE; i += band_step )
    1416             :         {
    1417       17658 :             if ( hMetadataPMC->bAttackPresent || hMetadataPMC->param_frame_idx == hMetadataPMC->coding_band_mapping[i] )
    1418             :             {
    1419             :                 /* LFE ICC/ILDs are always the last ones in coding band 0 */
    1420             :                 int16_t n_lfe_idx, k;
    1421        9560 :                 n_lfe_idx = map_size - map_size_wo_lfe;
    1422       22564 :                 for ( k = 0; k < n_lfe_idx; k++ )
    1423             :                 {
    1424       13004 :                     idx = quant_idx[( i + 1 ) * map_size - n_lfe_idx + k];
    1425       13004 :                     seq[sz_seq] = idx;
    1426       13004 :                     seq_delta[sz_seq] = idx - idx_prev + idx_offset;
    1427       13004 :                     idx_prev = idx;
    1428       13004 :                     sz_seq++;
    1429             :                 }
    1430             :             }
    1431             :         }
    1432             :     }
    1433             : 
    1434             : #ifdef DEBUG_MODE_PARAM_MC
    1435             :     dbgwrite( seq, sizeof( int16_t ), sz_seq, 1, "./res/param_mc_quant_param_idx_enc.dat" );
    1436             : #endif
    1437             : 
    1438       41960 :     bit_cnt_uni = sz_seq * hParameterCodingInfo->uni_bits - 1; /* -1 for the additional diff/direct signaling bit for the range encoder*/
    1439             : 
    1440             :     /* code the direct index sequence */
    1441       41960 :     ivas_param_mc_range_encoder( seq, sz_seq, hParameterCodingInfo->cum_freq, hParameterCodingInfo->sym_freq, PARAM_MC_RANGE_CODER_TOT_SHIFT, bit_cnt_uni, &tmp_bit_buffer[0], &bit_cnt_range );
    1442             : 
    1443             :     /* Coding the delta index sequence */
    1444       41960 :     ivas_param_mc_range_encoder( seq_delta, sz_seq, hParameterCodingInfo->cum_freq_delta, hParameterCodingInfo->sym_freq_delta, PARAM_MC_RANGE_CODER_TOT_SHIFT, bit_cnt_uni, &tmp_bit_buffer_diff[0], &bit_cnt_range_diff );
    1445             : 
    1446       41960 :     bit_cnt_range_min = min( bit_cnt_range, bit_cnt_range_diff );
    1447             : 
    1448             :     /* uniform fallback */
    1449       41960 :     if ( bit_cnt_range_min > bit_cnt_uni )
    1450             :     {
    1451             :         /* Uniform coding is used */
    1452         727 :         bit_buffer[( *bit_pos )++] = 0;
    1453         727 :         bit_pos_tmp = 0;
    1454             : 
    1455       25934 :         for ( i = 0; i < sz_seq; ++i )
    1456             :         {
    1457       25207 :             ivas_param_mc_dec2bin( seq[i], hParameterCodingInfo->uni_bits, &bit_buffer[*( bit_pos ) + bit_pos_tmp] );
    1458       25207 :             bit_pos_tmp += hParameterCodingInfo->uni_bits;
    1459             :         }
    1460         727 :         *bit_pos = *bit_pos + bit_pos_tmp;
    1461             :     }
    1462             :     else
    1463             :     {
    1464             :         /* Range Coding is used */
    1465       41233 :         bit_buffer[( *bit_pos )++] = 1;
    1466       41233 :         if ( bit_cnt_range_diff < bit_cnt_range )
    1467             :         {
    1468       40957 :             bit_buffer[( *bit_pos )++] = 1;
    1469     4398087 :             for ( i = 0; i < bit_cnt_range_diff; i++ )
    1470             :             {
    1471     4357130 :                 bit_buffer[( *bit_pos )++] = tmp_bit_buffer_diff[i];
    1472             :             }
    1473             :         }
    1474             :         else
    1475             :         {
    1476         276 :             bit_buffer[( *bit_pos )++] = 0;
    1477       17568 :             for ( i = 0; i < bit_cnt_range; i++ )
    1478             :             {
    1479       17292 :                 bit_buffer[( *bit_pos )++] = tmp_bit_buffer[i];
    1480             :             }
    1481             :         }
    1482             :     }
    1483             : 
    1484       41960 :     return;
    1485             : }
    1486             : 
    1487             : 
    1488             : /*-------------------------------------------------------------------------
    1489             :  * ivas_param_mc_dec2bin()
    1490             :  *
    1491             :  * Decimal to binary routine
    1492             :  *------------------------------------------------------------------------*/
    1493             : 
    1494       25207 : static void ivas_param_mc_dec2bin(
    1495             :     const int16_t val,                 /* i  : value to encode                       */
    1496             :     const int16_t N,                   /* i  : number of bits for encoding the value */
    1497             :     uint16_t bits[PARAM_MC_MAX_BITS] ) /* o  : encoded bits buffer                   */
    1498             : {
    1499             :     int16_t idx;
    1500             : 
    1501       25207 :     idx = 0;
    1502             :     /* convert value to bitstream, MSB first */
    1503      100828 :     for ( idx = 0; idx < N; idx++ )
    1504             :     {
    1505       75621 :         bits[idx] = ( val >> ( N - 1 - idx ) ) & 1;
    1506             :     }
    1507             : 
    1508       25207 :     return;
    1509             : }
    1510             : 
    1511             : 
    1512             : /*-------------------------------------------------------------------*
    1513             :  * ivas_param_mc_range_encoder()
    1514             :  *
    1515             :  * Parametric MC Range encoder
    1516             :  *-------------------------------------------------------------------*/
    1517             : 
    1518       83920 : static void ivas_param_mc_range_encoder(
    1519             :     const int16_t *seq_in,     /* i  : input sequence                     */
    1520             :     const int16_t num_symbols, /* i  : Number of symbole to encode        */
    1521             :     const uint16_t *cum_freq,  /* i  : cumulated frequencies              */
    1522             :     const uint16_t *sym_freq,  /* i  : symbol frequencies                 */
    1523             :     const uint16_t tot_shift,  /* i  : max cumulative freq as power of 2  */
    1524             :     const int16_t max_nb_bits, /* i  : Maximum number of bits allowed     */
    1525             :     uint16_t *bit_buffer,      /* o  : output bit buffer                  */
    1526             :     int16_t *bit_pos           /* o  : number of bits used                */
    1527             : )
    1528             : {
    1529             :     RangeUniEncState rc_st_enc;
    1530             :     int16_t rc_tot_bits; /* No. of bits returned by range coder */
    1531             :     int16_t i;
    1532             :     uint8_t k, byte;
    1533             :     uint16_t *bits;
    1534             : 
    1535             :     /* Initialize range encoder */
    1536       83920 :     rc_uni_enc_init( &rc_st_enc );
    1537             : 
    1538             :     /* Main loop over the length of the sequence */
    1539     3762282 :     for ( i = 0; i < num_symbols; ++i )
    1540             :     {
    1541     3700755 :         rc_uni_enc_encode_symbol_fastS( &rc_st_enc, (uint16_t) seq_in[i], cum_freq, sym_freq, tot_shift );
    1542             : 
    1543     3700755 :         if ( rc_uni_enc_virtual_finish( &rc_st_enc ) > max_nb_bits )
    1544             :         {
    1545             :             /* we alread have exceeded the maximum number of bits allowed, i.e. the uniform fallback */
    1546       22393 :             *bit_pos = MAX_BITS_PER_FRAME;
    1547       22393 :             return;
    1548             :         }
    1549             :     }
    1550             : 
    1551             :     /* Finish range encoder */
    1552       61527 :     rc_tot_bits = rc_uni_enc_finish( &rc_st_enc ); /* No. of bits consumed by range coder */
    1553             : 
    1554             :     /* Push range coded bits from byte_buffer to bitstream */
    1555             : 
    1556             :     /* 1) Push all complete bytes, one byte at a time */
    1557      985796 :     for ( i = 0; i < ( rc_tot_bits >> 3 ); ++i )
    1558             :     {
    1559             :         /* use rc_st_enc.byte_buffer */
    1560      924269 :         bits = &bit_buffer[i * 8];
    1561             : 
    1562      924269 :         byte = rc_st_enc.byte_buffer[i];
    1563             : 
    1564      924269 :         bits[0] = ( byte >> 7 ) & 1;
    1565      924269 :         bits[1] = ( byte >> 6 ) & 1;
    1566      924269 :         bits[2] = ( byte >> 5 ) & 1;
    1567      924269 :         bits[3] = ( byte >> 4 ) & 1;
    1568      924269 :         bits[4] = ( byte >> 3 ) & 1;
    1569      924269 :         bits[5] = ( byte >> 2 ) & 1;
    1570      924269 :         bits[6] = ( byte >> 1 ) & 1;
    1571      924269 :         bits[7] = byte & 1;
    1572             :     }
    1573             : 
    1574             :     /* 2) Push remaining bits */
    1575       61527 :     if ( ( rc_tot_bits & 7 ) != 0 )
    1576             :     {
    1577       53566 :         uint8_t rem_bits = rc_tot_bits & 7;
    1578             : 
    1579       53566 :         bits = &bit_buffer[i * 8];
    1580       53566 :         byte = rc_st_enc.byte_buffer[i];
    1581             : 
    1582      266795 :         for ( k = 0; k < rem_bits; k++ )
    1583             :         {
    1584      213229 :             bits[k] = ( byte >> ( 7 - k ) ) & 1;
    1585             :         }
    1586             :     }
    1587             : 
    1588             :     /* Update output number of bits */
    1589       61527 :     *bit_pos = rc_tot_bits;
    1590             : 
    1591       61527 :     return;
    1592             : }

Generated by: LCOV version 1.14