LCOV - code coverage report
Current view: top level - lib_enc - ivas_mc_param_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 517 522 99.0 %
Date: 2025-05-28 04:28:20 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_CICP_CHANNELS][MAX_CICP_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_CICP_CHANNELS][MAX_CICP_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_CICP_CHANNELS][MAX_CICP_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        3844 : 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        3844 :     error = IVAS_ERR_OK;
      97             : 
      98             :     /* Sanity Checks */
      99        3844 :     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        3844 :     mc_input_setup = st_ivas->hEncoderConfig->mc_input_setup;
     105        3844 :     max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
     106        3844 :     input_Fs = st_ivas->hEncoderConfig->input_Fs;
     107        3844 :     nchan_inp = st_ivas->hEncoderConfig->nchan_inp;
     108        3844 :     ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
     109             : 
     110             :     /* Preparing Config */
     111        3844 :     hParamMC->lfe_index = LFE_CHANNEL;
     112        3844 :     st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels( ivas_total_brate, mc_input_setup );
     113             : 
     114             :     /* get configuration index */
     115        3844 :     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        3844 :     switch ( st_ivas->nchan_transport )
     119             :     {
     120        1045 :         case 4:
     121             :         case 3:
     122        1045 :             st_ivas->nCPE = 2;
     123        1045 :             st_ivas->nSCE = 0;
     124        1045 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     125        1045 :             break;
     126        2799 :         case 2:
     127        2799 :             st_ivas->nCPE = 1;
     128        2799 :             st_ivas->nSCE = 0;
     129        2799 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     130        2799 :             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        3844 :     hParamMC->dmx_factors = ivas_param_mc_conf[config_index].dmx_fac;
     139             : 
     140             :     /* set FB config. */
     141        3844 :     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        3844 :     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        3844 :     ivas_param_mc_metadata_open( mc_input_setup, ivas_total_brate, &hParamMC->hMetadataPMC );
     154             : 
     155             :     /* Band Grouping */
     156        3844 :     if ( hParamMC->hMetadataPMC.num_parameter_bands == 20 )
     157             :     {
     158         317 :         mvs2s( param_mc_band_grouping_20, hParamMC->band_grouping, 20 + 1 );
     159             :     }
     160        3527 :     else if ( hParamMC->hMetadataPMC.num_parameter_bands == 14 )
     161             :     {
     162        1994 :         mvs2s( param_mc_band_grouping_14, hParamMC->band_grouping, 14 + 1 );
     163             :     }
     164        1533 :     else if ( hParamMC->hMetadataPMC.num_parameter_bands == 10 )
     165             :     {
     166        1533 :         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        3844 :     i = 0;
     175       36008 :     while ( hParamMC->band_grouping[i] <= PARAM_MC_MAX_BAND_ABS_COV_ENC )
     176             :     {
     177       32164 :         hParamMC->max_param_band_abs_cov = ( i++ );
     178             :     }
     179             : 
     180             :     /* parameter band grouping: 60 band CLDFB to 240 band MDFT resolution */
     181       57274 :     for ( i = 0; i < hParamMC->hMetadataPMC.num_parameter_bands + 1; i++ )
     182             :     {
     183       53430 :         hParamMC->band_grouping[i] *= PARAM_MC_CLDFB_TO_MDFT_FAC;
     184             :     }
     185             : 
     186             :     /* set correct coded band width */
     187        3844 :     hParamMC->hMetadataPMC.coded_bwidth = max_bwidth;
     188        3844 :     hParamMC->hMetadataPMC.last_coded_bwidth = max_bwidth;
     189        3844 :     ivas_param_mc_set_coded_bands( &hParamMC->hMetadataPMC );
     190             : 
     191             :     /* initialize offset for transient detection */
     192        3844 :     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        3844 :     set_f( hParamMC->ener_fac, 0.0f, PARAM_MC_MAX_PARAMETER_BANDS );
     196             : 
     197             :     /* init previous ILDs */
     198       80724 :     for ( i = 0; i < PARAM_MC_MAX_PARAMETER_BANDS; i++ )
     199             :     {
     200       76880 :         set_zero( hParamMC->prev_ilds[i], PARAM_MC_SZ_ILD_MAP );
     201             :     }
     202             : 
     203        3844 :     st_ivas->hParamMC = hParamMC;
     204             : 
     205        3844 :     return error;
     206             : }
     207             : 
     208             : 
     209             : /*-------------------------------------------------------------------------
     210             :  * ivas_param_mc_enc_reconfig()
     211             :  *
     212             :  * Reconfigure Parametric MC encoder
     213             :  *------------------------------------------------------------------------*/
     214             : 
     215         894 : 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         894 :     error = IVAS_ERR_OK;
     228             : 
     229         894 :     mc_input_setup = st_ivas->hEncoderConfig->mc_input_setup;
     230         894 :     max_bwidth = st_ivas->hEncoderConfig->max_bwidth;
     231         894 :     input_Fs = st_ivas->hEncoderConfig->input_Fs;
     232         894 :     ivas_total_brate = st_ivas->hEncoderConfig->ivas_total_brate;
     233         894 :     hParamMC = st_ivas->hParamMC;
     234             : 
     235             :     /* Preparing Config */
     236         894 :     st_ivas->nchan_transport = ivas_param_mc_getNumTransportChannels( ivas_total_brate, mc_input_setup );
     237             : 
     238             :     /* get configuration index */
     239         894 :     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         894 :     switch ( st_ivas->nchan_transport )
     243             :     {
     244         107 :         case 4:
     245             :         case 3:
     246         107 :             st_ivas->nCPE = 2;
     247         107 :             st_ivas->nSCE = 0;
     248         107 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     249         107 :             break;
     250         787 :         case 2:
     251         787 :             st_ivas->nCPE = 1;
     252         787 :             st_ivas->nSCE = 0;
     253         787 :             st_ivas->hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
     254         787 :             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         894 :     hParamMC->dmx_factors = ivas_param_mc_conf[config_index].dmx_fac;
     263             : 
     264             :     /* open/init parameter coding */
     265         894 :     ivas_param_mc_metadata_open( mc_input_setup, ivas_total_brate, &hParamMC->hMetadataPMC );
     266             : 
     267             :     /* Band Grouping */
     268         894 :     if ( hParamMC->hMetadataPMC.num_parameter_bands == 20 )
     269             :     {
     270         102 :         mvs2s( param_mc_band_grouping_20, hParamMC->band_grouping, 20 + 1 );
     271             :     }
     272         792 :     else if ( hParamMC->hMetadataPMC.num_parameter_bands == 14 )
     273             :     {
     274         461 :         mvs2s( param_mc_band_grouping_14, hParamMC->band_grouping, 14 + 1 );
     275             :     }
     276         331 :     else if ( hParamMC->hMetadataPMC.num_parameter_bands == 10 )
     277             :     {
     278         331 :         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         894 :     i = 0;
     287        8482 :     while ( hParamMC->band_grouping[i] <= PARAM_MC_MAX_BAND_ABS_COV_ENC )
     288             :     {
     289        7588 :         hParamMC->max_param_band_abs_cov = ( i++ );
     290             :     }
     291             : 
     292             :     /* parameter band grouping: 60 band CLDFB to 240 band MDFT resolution */
     293       13592 :     for ( i = 0; i < hParamMC->hMetadataPMC.num_parameter_bands + 1; i++ )
     294             :     {
     295       12698 :         hParamMC->band_grouping[i] *= PARAM_MC_CLDFB_TO_MDFT_FAC;
     296             :     }
     297             : 
     298             :     /* set correct coded band width */
     299         894 :     hParamMC->hMetadataPMC.coded_bwidth = max_bwidth;
     300         894 :     hParamMC->hMetadataPMC.last_coded_bwidth = max_bwidth;
     301         894 :     ivas_param_mc_set_coded_bands( &hParamMC->hMetadataPMC );
     302             : 
     303             :     /* initialize offset for transient detection */
     304         894 :     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         894 :     set_f( hParamMC->ener_fac, 0.0f, PARAM_MC_MAX_PARAMETER_BANDS );
     308             : 
     309             : 
     310         894 :     return error;
     311             : }
     312             : 
     313             : 
     314             : /*-------------------------------------------------------------------------
     315             :  * ivas_param_mc_enc_close()
     316             :  *
     317             :  * Close Parametric MC encoder handle
     318             :  *------------------------------------------------------------------------*/
     319             : 
     320       16625 : 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       16625 :     if ( hParamMC == NULL || *hParamMC == NULL )
     325             :     {
     326       12781 :         return;
     327             :     }
     328             : 
     329        3844 :     ivas_FB_mixer_close( &( *hParamMC )->hFbMixer, sampling_rate, 0 );
     330             : 
     331        3844 :     free( ( *hParamMC ) );
     332        3844 :     ( *hParamMC ) = NULL;
     333             : 
     334        3844 :     return;
     335             : }
     336             : 
     337             : 
     338             : /*-------------------------------------------------------------------------
     339             :  * ivas_param_mc_enc()
     340             :  *
     341             :  * Parametric MC Encoder main encoding function
     342             :  *------------------------------------------------------------------------*/
     343             : 
     344      125548 : 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_CICP_CHANNELS][MAX_CICP_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      125548 :     push_wmops( "param_mc_enc" );
     367             : 
     368             :     /* initializations */
     369      125548 :     hParamMC = st_ivas->hParamMC;
     370      125548 :     bit_pos = 0;
     371      125548 :     band_step = 1;
     372      125548 :     nchan_inp = st_ivas->hEncoderConfig->nchan_inp;
     373             : 
     374     2636508 :     for ( band = 0; band < PARAM_MC_MAX_PARAMETER_BANDS; band++ )
     375             :     {
     376    42686320 :         for ( ch = 0; ch < MAX_CICP_CHANNELS; ch++ )
     377             :         {
     378    40175360 :             set_zero( Cy_sum[band][ch], MAX_CICP_CHANNELS );
     379             :         }
     380    10043840 :         for ( ch = 0; ch < PARAM_MC_MAX_TRANSPORT_CHANS; ch++ )
     381             :         {
     382     7532880 :             set_zero( Cx_sum[band][ch], PARAM_MC_MAX_TRANSPORT_CHANS );
     383             :         }
     384             :     }
     385             : 
     386     2636508 :     for ( band = 0; band < PARAM_MC_MAX_PARAMETER_BANDS; band++ )
     387             :     {
     388     2510960 :         set_zero( ILD_q[band], PARAM_MC_SZ_ILD_MAP );
     389             :     }
     390      125548 :     set_s( ILD_idx, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP );
     391      125548 :     set_s( ICC_idx, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ICC_MAP );
     392             : 
     393             :     /* update parameter frame index */
     394      125548 :     hParamMC->hMetadataPMC.param_frame_idx = ( hParamMC->hMetadataPMC.param_frame_idx + 1 ) % PARAM_MC_PARAMETER_FRAMES;
     395             : 
     396             :     /* DMX generation*/
     397      125548 :     ivas_param_mc_dmx( hParamMC, data_f, data_dmx, input_frame, nchan_inp, st_ivas->nchan_transport );
     398             : 
     399             :     /* Transient Detector */
     400      125548 :     switch ( st_ivas->nchan_transport )
     401             :     {
     402      125548 :         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      125548 :             set_s( attackIdx, -1, PARAM_MC_MAX_TRANSPORT_CHANS );
     410      125548 :             set_s( bAttackPresent, 0, PARAM_MC_MAX_TRANSPORT_CHANS );
     411             : 
     412      387024 :             for ( ch = 0; ch < st_ivas->nchan_transport; ch++ )
     413             :             {
     414      261476 :                 int16_t cpe_idx = ch / 2;
     415             : 
     416      261476 :                 RunTransientDetection( data_dmx[ch], input_frame, st_ivas->hCPE[cpe_idx]->hCoreCoder[ch - cpe_idx * CPE_CHANNELS]->hTranDet );
     417             : 
     418      261476 :                 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      125548 :             hParamMC->hMetadataPMC.bAttackPresent = 0;
     423      125548 :             hParamMC->hMetadataPMC.attackIndex = 16;
     424             : 
     425      387024 :             for ( ch = 0; ch < st_ivas->nchan_transport; ch++ )
     426             :             {
     427      261476 :                 hParamMC->hMetadataPMC.bAttackPresent = max( hParamMC->hMetadataPMC.bAttackPresent, bAttackPresent[ch] );
     428             :             }
     429             : 
     430      125548 :             if ( hParamMC->hMetadataPMC.bAttackPresent )
     431             :             {
     432       19989 :                 for ( ch = 0; ch < st_ivas->nchan_transport; ch++ )
     433             :                 {
     434       13788 :                     hParamMC->hMetadataPMC.attackIndex = min( hParamMC->hMetadataPMC.attackIndex, attackIdx[ch] );
     435             :                 }
     436             :             }
     437             :             else
     438             :             {
     439      119347 :                 hParamMC->hMetadataPMC.attackIndex = 0;
     440             :             }
     441             :         }
     442      125548 :         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      125548 :     ivas_param_mc_param_est_enc( hParamMC, data_f, Cy_sum, Cx_sum, input_frame, nchan_inp, st_ivas->nchan_transport );
     452             : 
     453      125548 :     band_step = hParamMC->hMetadataPMC.bAttackPresent ? PARAM_MC_TRANSIENT_BAND_STEP : 1;
     454             : 
     455             : 
     456             :     /* ILD parameter quantization */
     457     1776667 :     for ( k = 0; k < hParamMC->hMetadataPMC.nbands_coded; k += band_step )
     458             :     {
     459     1651119 :         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     1776667 :     for ( k = 0; k < hParamMC->hMetadataPMC.nbands_coded; k += band_step )
     464             :     {
     465     1651119 :         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      387024 :     for ( ch = 0; ch < st_ivas->nchan_transport; ch++ )
     471             :     {
     472      261476 :         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      125548 :     if ( st_ivas->nchan_transport > 2 )
     478             :     {
     479       20760 :         for ( ; ch < st_ivas->nCPE * CPE_CHANNELS; ch++ )
     480             :         {
     481       10380 :             int16_t cpe_idx = ch / 2;
     482             : 
     483       10380 :             set_zero( data_f[ch], input_frame );
     484             : 
     485       10380 :             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      125548 :     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      125548 :     push_next_bits( hMetaData, bit_buffer, bit_pos );
     494             : 
     495             :     /* updates */
     496      125548 :     hParamMC->hMetadataPMC.last_coded_bwidth = hParamMC->hMetadataPMC.coded_bwidth;
     497             : 
     498      125548 :     pop_wmops();
     499             : 
     500      125548 :     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      125548 : 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      125548 :     idx = Param_MC_index;
     530   119002028 :     for ( i = 0; i < input_frame; i++ )
     531             :     {
     532   118876480 :         p_dmx_fac = hParamMC->dmx_factors;
     533   366354240 :         for ( dmx_ch = 0; dmx_ch < nchan_transport; dmx_ch++ )
     534             :         {
     535   247477760 :             float *dmx_sample = &data_dmx[idx[dmx_ch]][i];
     536   247477760 :             *dmx_sample = 0.0f;
     537  1953632000 :             for ( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
     538             :             {
     539  1706154240 :                 ( *dmx_sample ) += data_f[idx[inp_ch]][i] * ( *( p_dmx_fac++ ) );
     540             :             }
     541             :         }
     542             :     }
     543             : 
     544      125548 :     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      125548 : 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_CICP_CHANNELS][MAX_CICP_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      125548 :     const int16_t *map_ls = Param_MC_index; /* Loudspeakers mapping */
     573             :     int16_t idx_ls;
     574             :     float *pcm_in[MAX_CICP_CHANNELS];
     575             :     float slot_frame_f_real[MAX_CICP_CHANNELS][DIRAC_NO_FB_BANDS_MAX]; /* Output of the MDFT FB - real part */
     576             :     float slot_frame_f_imag[MAX_CICP_CHANNELS][DIRAC_NO_FB_BANDS_MAX]; /* Output of the MDFT FB - imag part */
     577             :     float *p_slot_frame_f_real[MAX_CICP_CHANNELS];                     /* Output of the MDFT FB - real part */
     578             :     float *p_slot_frame_f_imag[MAX_CICP_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_CICP_CHANNELS][MAX_CICP_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      125548 :     push_wmops( "param_mc_prm_est" );
     590             : 
     591             :     /* initializations */
     592      125548 :     l_ts = input_frame / PARAM_MC_MDFT_NO_SLOTS;
     593      125548 :     num_time_slots = PARAM_MC_MDFT_NO_SLOTS;
     594      125548 :     start_ts = hParamMC->hMetadataPMC.bAttackPresent ? hParamMC->hMetadataPMC.attackIndex : 0;
     595      125548 :     num_parameter_bands = hParamMC->hMetadataPMC.nbands_coded;
     596      125548 :     band_step = 1;
     597             : 
     598     1381028 :     for ( cur_param_band = 0; cur_param_band < PARAM_MC_MAX_PARAM_BAND_ABS_COV_ENC; cur_param_band++ )
     599             :     {
     600    21343160 :         for ( ch_idx1 = 0; ch_idx1 < MAX_CICP_CHANNELS; ch_idx1++ )
     601             :         {
     602    20087680 :             set_zero( Cy_sum_imag[cur_param_band][ch_idx1], MAX_CICP_CHANNELS );
     603             :         }
     604             : 
     605     5021920 :         for ( ch_idx1 = 0; ch_idx1 < PARAM_MC_MAX_TRANSPORT_CHANS; ch_idx1++ )
     606             :         {
     607     3766440 :             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      974920 :     for ( i = 0; i < nchan_input; i++ )
     613             :     {
     614      849372 :         idx_ls = map_ls[i];
     615      849372 :         pcm_in[i] = data_f[idx_ls];
     616      849372 :         p_slot_frame_f_real[i] = &slot_frame_f_real[i][0];
     617      849372 :         p_slot_frame_f_imag[i] = &slot_frame_f_imag[i][0];
     618             :     }
     619             : 
     620      146448 :     for ( ts = 0; ts < start_ts; ts++ )
     621             :     {
     622       20900 :         ivas_fb_mixer_update_prior_input( hParamMC->hFbMixer, pcm_in, l_ts, hParamMC->hFbMixer->fb_cfg->num_in_chans );
     623      177432 :         for ( i = 0; i < nchan_input; i++ )
     624             :         {
     625      156532 :             pcm_in[i] += l_ts;
     626             :         }
     627             :     }
     628             : 
     629     1109032 :     for ( ts = start_ts; ts < num_time_slots; ts++ )
     630             :     {
     631      983484 :         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      983484 :         ivas_fb_mixer_update_prior_input( hParamMC->hFbMixer, pcm_in, l_ts, hParamMC->hFbMixer->fb_cfg->num_in_chans );
     634             : 
     635     7621928 :         for ( i = 0; i < nchan_input; i++ )
     636             :         {
     637     6638444 :             pcm_in[i] += l_ts;
     638             :         }
     639             :         /* Computing the downmix */
     640     8598102 :         for ( cur_param_band = 0; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band++ )
     641             :         {
     642     7614618 :             brange[0] = hParamMC->band_grouping[cur_param_band];
     643     7614618 :             brange[1] = hParamMC->band_grouping[cur_param_band + 1];
     644             : 
     645    27284298 :             for ( cur_cldfb_band = brange[0]; cur_cldfb_band < brange[1]; cur_cldfb_band++ )
     646             :             {
     647             :                 /* Cx for DMX */
     648             :                 /* Real Part */
     649    19669680 :                 p_dmx_fac = hParamMC->dmx_factors;
     650             : 
     651    60570200 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
     652             :                 {
     653    40900520 :                     dmx_real[ch_idx1] = 0.0f;
     654    40900520 :                     dmx_imag[ch_idx1] = 0.0f;
     655   322115520 :                     for ( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
     656             :                     {
     657   281215000 :                         dmx_real[ch_idx1] += slot_frame_f_real[inp_ch][cur_cldfb_band] * ( *p_dmx_fac );
     658   281215000 :                         dmx_imag[ch_idx1] += slot_frame_f_imag[inp_ch][cur_cldfb_band] * ( *p_dmx_fac );
     659   281215000 :                         p_dmx_fac++;
     660             :                     }
     661             :                 }
     662             : 
     663             :                 /* Cx for transport channels */
     664    60570200 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
     665             :                 {
     666   127385040 :                     for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
     667             :                     {
     668    86484520 :                         a = dmx_real[ch_idx1];
     669    86484520 :                         b = dmx_imag[ch_idx1];
     670    86484520 :                         c = dmx_real[ch_idx2];
     671    86484520 :                         d = dmx_imag[ch_idx2];
     672             : 
     673             :                         /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
     674    86484520 :                         Cx_sum[cur_param_band][ch_idx1][ch_idx2] += a * c + b * d;
     675    86484520 :                         Cx_sum_imag[cur_param_band][ch_idx1][ch_idx2] += a * d - b * c;
     676             :                     }
     677             :                 }
     678             : 
     679             :                 /* Cy for input channels */
     680   152438560 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
     681             :                 {
     682   665549320 :                     for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
     683             :                     {
     684   532780440 :                         a = slot_frame_f_real[ch_idx1][cur_cldfb_band];
     685   532780440 :                         b = slot_frame_f_imag[ch_idx1][cur_cldfb_band];
     686   532780440 :                         c = slot_frame_f_real[ch_idx2][cur_cldfb_band];
     687   532780440 :                         d = slot_frame_f_imag[ch_idx2][cur_cldfb_band];
     688             : 
     689             :                         /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
     690   532780440 :                         Cy_sum[cur_param_band][ch_idx1][ch_idx2] += a * c + b * d;
     691   532780440 :                         Cy_sum_imag[cur_param_band][ch_idx1][ch_idx2] += a * d - b * c;
     692             :                     }
     693             :                 }
     694             :             }
     695             :         }
     696             : 
     697     6635495 :         for ( ; cur_param_band < num_parameter_bands; cur_param_band++ )
     698             :         {
     699     5652011 :             brange[0] = hParamMC->band_grouping[cur_param_band];
     700     5652011 :             brange[1] = hParamMC->band_grouping[cur_param_band + 1];
     701             : 
     702   101637491 :             for ( cur_cldfb_band = brange[0]; cur_cldfb_band < brange[1]; cur_cldfb_band++ )
     703             :             {
     704             :                 /* Cx for DMX */
     705             :                 /* Real Part */
     706    95985480 :                 p_dmx_fac = hParamMC->dmx_factors;
     707             : 
     708   295527000 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
     709             :                 {
     710   199541520 :                     dmx_real[ch_idx1] = 0.0f;
     711   199541520 :                     dmx_imag[ch_idx1] = 0.0f;
     712             : 
     713  1572173480 :                     for ( inp_ch = 0; inp_ch < nchan_input; inp_ch++ )
     714             :                     {
     715  1372631960 :                         dmx_real[ch_idx1] += slot_frame_f_real[inp_ch][cur_cldfb_band] * ( *p_dmx_fac );
     716  1372631960 :                         dmx_imag[ch_idx1] += slot_frame_f_imag[inp_ch][cur_cldfb_band] * ( *p_dmx_fac );
     717  1372631960 :                         p_dmx_fac++;
     718             :                     }
     719             :                 }
     720             : 
     721             :                 /* Cx for transport channels */
     722   295527000 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
     723             :                 {
     724   621336240 :                     for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
     725             :                     {
     726   421794720 :                         a = dmx_real[ch_idx1];
     727   421794720 :                         b = dmx_imag[ch_idx1];
     728   421794720 :                         c = dmx_real[ch_idx2];
     729   421794720 :                         d = dmx_imag[ch_idx2];
     730             : 
     731             :                         /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
     732   421794720 :                         Cx_sum[cur_param_band][ch_idx1][ch_idx2] += a * c + b * d;
     733             :                     }
     734             :                 }
     735             : 
     736             :                 /* Cy for input channels */
     737   744237640 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
     738             :                 {
     739  3250981040 :                     for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
     740             :                     {
     741  2602728880 :                         a = slot_frame_f_real[ch_idx1][cur_cldfb_band];
     742  2602728880 :                         b = slot_frame_f_imag[ch_idx1][cur_cldfb_band];
     743  2602728880 :                         c = slot_frame_f_real[ch_idx2][cur_cldfb_band];
     744  2602728880 :                         d = slot_frame_f_imag[ch_idx2][cur_cldfb_band];
     745             : 
     746             :                         /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
     747  2602728880 :                         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      125548 :     if ( hParamMC->lfe_index >= 0 )
     757             :     {
     758      971840 :         for ( cur_param_band = PARAM_MC_MAX_BAND_LFE; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band++ )
     759             :         {
     760     6604576 :             for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
     761             :             {
     762     5758284 :                 Cy_sum[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0.0f;
     763     5758284 :                 Cy_sum[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0.0f;
     764     5758284 :                 Cy_sum_imag[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0.0f;
     765     5758284 :                 Cy_sum_imag[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0.0f;
     766             :             }
     767             :         }
     768             : 
     769      846983 :         for ( ; cur_param_band < num_parameter_bands; cur_param_band++ )
     770             :         {
     771     5650173 :             for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
     772             :             {
     773     4928738 :                 Cy_sum[cur_param_band][hParamMC->lfe_index][ch_idx1] = 0.0f;
     774     4928738 :                 Cy_sum[cur_param_band][ch_idx1][hParamMC->lfe_index] = 0.0f;
     775             :             }
     776             :         }
     777             :     }
     778             : 
     779      125548 :     if ( !hParamMC->hMetadataPMC.bAttackPresent )
     780             :     {
     781             :         const PARAM_MC_ILD_MAPPING *h_ild_mapping;
     782             :         int16_t ild_attack;
     783      119347 :         ild_attack = 0;
     784      119347 :         h_ild_mapping = hParamMC->hMetadataPMC.ild_mapping_conf;
     785             :         /* create ILDs for to non transmitted parameter bands (only lower half) */
     786      928636 :         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      809289 :             if ( cur_param_band >= PARAM_MC_MAX_BAND_LFE )
     793             :             {
     794      689942 :                 num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_wo_lfe;
     795             :             }
     796             :             else
     797             :             {
     798      119347 :                 num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
     799             :             }
     800      809289 :             if ( hParamMC->hMetadataPMC.param_frame_idx != hParamMC->hMetadataPMC.coding_band_mapping[cur_param_band] )
     801             :             {
     802             :                 float Nrg[MAX_CICP_CHANNELS];
     803             : 
     804             :                 /* get ICLDs */
     805     3155658 :                 for ( k = 0; k < nchan_input; ++k )
     806             :                 {
     807     2750988 :                     Nrg[k] = Cy_sum[cur_param_band][k][k];
     808             :                 }
     809     2775284 :                 for ( k = 0; k < num_ilds_to_code; ++k )
     810             :                 {
     811     2370614 :                     float ref_ener = 0.0f;
     812             :                     int16_t ref_channel_cnt;
     813             :                     int16_t ref_channel_idx;
     814             : 
     815     5160716 :                     for ( ref_channel_cnt = 0; ref_channel_cnt < h_ild_mapping->num_ref_channels[k]; ref_channel_cnt++ )
     816             :                     {
     817     2790102 :                         ref_channel_idx = h_ild_mapping->ref_channel_idx[k][ref_channel_cnt];
     818     2790102 :                         ref_ener += Cx_sum[cur_param_band][ref_channel_idx][ref_channel_idx];
     819             :                     }
     820     2370614 :                     ILD[k] = 10.0f * log10f( ( Nrg[h_ild_mapping->ild_index[k]] + EPSILON ) / ( hParamMC->hMetadataPMC.ild_factors[k] * ref_ener + EPSILON ) );
     821     2370614 :                     if ( hParamMC->prev_ilds[cur_param_band][k] - ILD[k] > param_mc_ild_diff_threshold[cur_param_band] )
     822             :                     {
     823        3928 :                         ild_attack++;
     824             :                     }
     825             :                 }
     826             :             }
     827             :         }
     828             :         /* check if the ILDs change too much -> go into transient mode... */
     829      119347 :         if ( ild_attack > PARAM_MC_NUM_ATTACK_ILD_THRESH )
     830             :         {
     831         120 :             hParamMC->hMetadataPMC.bAttackPresent = 1;
     832             :         }
     833             :     }
     834             : 
     835             : 
     836      125548 :     if ( hParamMC->hMetadataPMC.bAttackPresent )
     837             :     {
     838             :         /* combine bands */
     839       30627 :         for ( cur_param_band = 1; cur_param_band < hParamMC->max_param_band_abs_cov; cur_param_band += 2 )
     840             :         {
     841       79000 :             for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
     842             :             {
     843      182328 :                 for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
     844             :                 {
     845      127634 :                     Cx_sum[cur_param_band - 1][ch_idx1][ch_idx2] += Cx_sum[cur_param_band][ch_idx1][ch_idx2];
     846      127634 :                     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      206620 :             for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
     851             :             {
     852     1006261 :                 for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
     853             :                 {
     854      823947 :                     Cy_sum[cur_param_band - 1][ch_idx1][ch_idx2] += Cy_sum[cur_param_band][ch_idx1][ch_idx2];
     855      823947 :                     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       24171 :         for ( ; cur_param_band < num_parameter_bands; cur_param_band += 2 )
     861             :         {
     862       17850 :             if ( cur_param_band < num_parameter_bands )
     863             :             {
     864       58698 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ++ch_idx1 )
     865             :                 {
     866      137988 :                     for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ++ch_idx2 )
     867             :                     {
     868       97140 :                         Cx_sum[cur_param_band - 1][ch_idx1][ch_idx2] += Cx_sum[cur_param_band][ch_idx1][ch_idx2];
     869             :                     }
     870             :                 }
     871             : 
     872      153286 :                 for ( ch_idx1 = 0; ch_idx1 < nchan_input; ++ch_idx1 )
     873             :                 {
     874      752842 :                     for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ++ch_idx2 )
     875             :                     {
     876      617406 :                         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        6321 :         band_step = 2;
     883             :     }
     884             : 
     885             : 
     886             :     /* map complex covariances to real values */
     887     1073082 :     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     2929560 :         for ( ch_idx1 = 0; ch_idx1 < nchan_transport; ch_idx1++ )
     891             :         {
     892     6206952 :             for ( ch_idx2 = 0; ch_idx2 < nchan_transport; ch_idx2++ )
     893             :             {
     894     4224926 :                 real_part = Cx_sum[cur_param_band][ch_idx1][ch_idx2];
     895     4224926 :                 imag_part = Cx_sum_imag[cur_param_band][ch_idx1][ch_idx2];
     896             : 
     897             :                 /* (a-ib)(c+id) = ac + bd + i(ad-bc) */
     898     4224926 :                 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     7372876 :         for ( ch_idx1 = 0; ch_idx1 < nchan_input; ch_idx1++ )
     904             :         {
     905    32331375 :             for ( ch_idx2 = ch_idx1; ch_idx2 < nchan_input; ch_idx2++ )
     906             :             {
     907    25906033 :                 real_part = Cy_sum[cur_param_band][ch_idx1][ch_idx2];
     908    25906033 :                 imag_part = Cy_sum_imag[cur_param_band][ch_idx1][ch_idx2];
     909    25906033 :                 Cy_sum[cur_param_band][ch_idx1][ch_idx2] = sqrtf( real_part * real_part + imag_part * imag_part );
     910             :             }
     911             :         }
     912             :     }
     913             : 
     914      125548 :     if ( Cy_sum[0][LFE_CHANNEL][LFE_CHANNEL] < PARAM_MC_LFE_ON_THRESH )
     915             :     {
     916       10682 :         hParamMC->hMetadataPMC.lfe_on = 0;
     917             :     }
     918             :     else
     919             :     {
     920      114866 :         hParamMC->hMetadataPMC.lfe_on = 1;
     921             :     }
     922             : 
     923      125548 :     pop_wmops();
     924             : 
     925      125548 :     return;
     926             : }
     927             : 
     928             : 
     929             : /*-------------------------------------------------------------------------
     930             :  * ivas_param_mc_quantize_ilds()
     931             :  *
     932             :  * Quantize the ILD parameters
     933             :  *------------------------------------------------------------------------*/
     934             : 
     935     1651119 : static void ivas_param_mc_quantize_ilds(
     936             :     PARAM_MC_ENC_HANDLE hParamMC,                                         /* i/o: Parametric MC encoder handle    */
     937             :     float Cy[MAX_CICP_CHANNELS][MAX_CICP_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_CICP_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     1651119 :     push_wmops( "param_mc_prm_q" );
     957             : 
     958             :     /* Initialization */
     959     1651119 :     set_zero( Nrg, MAX_CICP_CHANNELS );
     960     1651119 :     set_zero( ILD, PARAM_MC_SZ_ILD_MAP );
     961             : 
     962     1651119 :     Ny = nchan_input;
     963             : 
     964     1651119 :     h_ild_mapping = hParamMC->hMetadataPMC.ild_mapping_conf;
     965     1651119 :     ild_map_size = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
     966     1651119 :     if ( freq_idx >= PARAM_MC_MAX_BAND_LFE )
     967             :     {
     968     1525571 :         num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_wo_lfe;
     969             :     }
     970             :     else
     971             :     {
     972      125548 :         num_ilds_to_code = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
     973             :     }
     974             : 
     975             :     /* Downsampling */
     976     1651119 :     if ( ( hParamMC->hMetadataPMC.bAttackPresent == 0 ) && ( hParamMC->hMetadataPMC.param_frame_idx != hParamMC->hMetadataPMC.coding_band_mapping[freq_idx] ) )
     977             :     {
     978      803826 :         pop_wmops();
     979             : 
     980      803826 :         return;
     981             :     }
     982             : 
     983             :     /* get ICLDs */
     984     6620165 :     for ( k = 0; k < Ny; ++k )
     985             :     {
     986     5772872 :         Nrg[k] = Cy[k][k];
     987             :     }
     988             : 
     989             :     /* limit ILDs if DMX energy is lower than sum of channel energies */
     990      847293 :     tot_ener = 0.0f;
     991      847293 :     dmx_ener = 0.0f;
     992             : 
     993     6620165 :     for ( k = 0; k < ild_map_size; k++ )
     994             :     {
     995     5772872 :         if ( k != hParamMC->lfe_index || hParamMC->hMetadataPMC.lfe_on )
     996             :         {
     997     5702543 :             tot_ener += Nrg[k];
     998             :         }
     999             :     }
    1000             : 
    1001     2629037 :     for ( k = 0; k < nchan_transport; k++ )
    1002             :     {
    1003     1781744 :         dmx_ener += Cx[k][k];
    1004             :     }
    1005      847293 :     ener_fac = 10.0f * log10f( ( tot_ener + EPSILON ) / ( dmx_ener + EPSILON ) );
    1006             : 
    1007      847293 :     if ( ener_fac > PARAM_MC_ENER_LIMIT_INTRAFRAME )
    1008             :     {
    1009             :         float limit_fac;
    1010        8700 :         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        8700 :         v_multc( Nrg, limit_fac, Nrg, num_ilds_to_code );
    1012             :     }
    1013             : 
    1014             :     /* limit ILD jumps in non-tranient frames */
    1015      847293 :     tot_ener = 0.0f;
    1016      847293 :     dmx_ener = 0.0f;
    1017             : 
    1018     6620165 :     for ( k = 0; k < ild_map_size; k++ )
    1019             :     {
    1020     5772872 :         if ( k != hParamMC->lfe_index || hParamMC->hMetadataPMC.lfe_on )
    1021             :         {
    1022     5702543 :             tot_ener += Nrg[k];
    1023             :         }
    1024             :     }
    1025             : 
    1026     2629037 :     for ( k = 0; k < nchan_transport; k++ )
    1027             :     {
    1028     1781744 :         dmx_ener += Cx[k][k];
    1029             :     }
    1030             : 
    1031      847293 :     ener_fac = 10.0f * log10f( ( tot_ener + EPSILON ) / ( dmx_ener + EPSILON ) );
    1032      847293 :     delta_fac = ener_fac - hParamMC->ener_fac[freq_idx];
    1033             : 
    1034      847293 :     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       15115 :         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       15115 :         v_multc( Nrg, limit_fac, Nrg, num_ilds_to_code );
    1039       15115 :         ener_fac += 10.0f * log10f( limit_fac );
    1040             :     }
    1041             : 
    1042      847293 :     hParamMC->ener_fac[freq_idx] = ener_fac;
    1043             : 
    1044             :     /* update also combined bands ener_fac when in transient frame */
    1045      847293 :     if ( hParamMC->hMetadataPMC.bAttackPresent && ( ( freq_idx + 1 ) < hParamMC->hMetadataPMC.nbands_coded ) )
    1046             :     {
    1047       42156 :         hParamMC->ener_fac[freq_idx + 1] = ener_fac;
    1048             :     }
    1049             : 
    1050     5757442 :     for ( k = 0; k < num_ilds_to_code; ++k )
    1051             :     {
    1052     4910149 :         float ref_ener = 0.0f;
    1053             :         int16_t ref_channel_cnt;
    1054             :         int16_t ref_channel_idx;
    1055             : 
    1056    10640875 :         for ( ref_channel_cnt = 0; ref_channel_cnt < h_ild_mapping->num_ref_channels[k]; ref_channel_cnt++ )
    1057             :         {
    1058     5730726 :             ref_channel_idx = h_ild_mapping->ref_channel_idx[k][ref_channel_cnt];
    1059     5730726 :             ref_ener += Cx[ref_channel_idx][ref_channel_idx];
    1060             :         }
    1061     4910149 :         ILD[k] = 10.0f * log10f( ( Nrg[h_ild_mapping->ild_index[k]] + EPSILON ) / ( hParamMC->hMetadataPMC.ild_factors[k] * ref_ener + EPSILON ) );
    1062     4910149 :         hParamMC->prev_ilds[freq_idx][k] = ILD[k];
    1063     4910149 :         if ( hParamMC->hMetadataPMC.bAttackPresent && ( ( freq_idx + 1 ) < hParamMC->hMetadataPMC.nbands_coded ) )
    1064             :         {
    1065      272073 :             hParamMC->prev_ilds[freq_idx + 1][k] = ILD[k];
    1066             :         }
    1067             :     }
    1068             : 
    1069             : 
    1070             :     /* quantize parameters */
    1071      847293 :     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      847293 :     mvs2s( ILD_idx, ILD_idx_out + freq_idx * ild_map_size, num_ilds_to_code );
    1075             : 
    1076      847293 :     pop_wmops();
    1077             : 
    1078      847293 :     return;
    1079             : }
    1080             : 
    1081             : 
    1082             : /*-------------------------------------------------------------------------
    1083             :  * ivas_param_mc_quantize_iccs()
    1084             :  *
    1085             :  * Quantize the ILD parameters
    1086             :  *------------------------------------------------------------------------*/
    1087             : 
    1088     1651119 : static void ivas_param_mc_quantize_iccs(
    1089             :     PARAM_MC_ENC_HANDLE hParamMC,                   /* i/o: Parametric MC encoder handle    */
    1090             :     float Cy[MAX_CICP_CHANNELS][MAX_CICP_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_CICP_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     1651119 :     set_zero( Nrg, MAX_CICP_CHANNELS );
    1109     1651119 :     set_zero( ICC_vect, PARAM_MC_SZ_ICC_MAP );
    1110     1651119 :     set_zero( ICC_vect_q, PARAM_MC_SZ_ICC_MAP );
    1111             : 
    1112     1651119 :     Ny = nchan_input;
    1113             : 
    1114             :     /* Downsampling */
    1115     1651119 :     if ( ( hParamMC->hMetadataPMC.bAttackPresent == 0 ) && ( hParamMC->hMetadataPMC.param_frame_idx != hParamMC->hMetadataPMC.coding_band_mapping[freq_idx] ) )
    1116             :     {
    1117      803826 :         return;
    1118             :     }
    1119             : 
    1120      847293 :     icc_map_size = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_lfe;
    1121      847293 :     num_iccs_to_code = icc_map_size;
    1122             : 
    1123      847293 :     if ( freq_idx >= PARAM_MC_MAX_BAND_LFE )
    1124             :     {
    1125      781208 :         num_iccs_to_code = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_wo_lfe;
    1126             :     }
    1127             : 
    1128             :     /* Get ICC matrix from Cy */
    1129     6620165 :     for ( k = 0; k < Ny; ++k )
    1130             :     {
    1131     5772872 :         Nrg[k] = Cy[k][k];
    1132     5772872 :         a = 1.0f / ( sqrtf( Nrg[k] ) + EPSILON );
    1133             : 
    1134    29170672 :         for ( i = k; i < Ny; ++i )
    1135             :         {
    1136    23397800 :             Cy[k][i] = Cy[k][i] * a;
    1137             :         }
    1138             : 
    1139    29170672 :         for ( i = 0; i <= k; i++ )
    1140             :         {
    1141    23397800 :             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     6620165 :     for ( k = 0; k < Ny; ++k )
    1147             :     {
    1148     5772872 :         if ( Nrg[k] == 0.0f )
    1149             :         {
    1150     3762786 :             for ( i = k; i < Ny; ++i )
    1151             :             {
    1152     2981551 :                 Cy[k][i] = 1.0f;
    1153             :             }
    1154             : 
    1155     3906175 :             for ( i = 0; i <= k; ++i )
    1156             :             {
    1157     3124940 :                 Cy[i][k] = 1.0f;
    1158             :             }
    1159             :         }
    1160             :     }
    1161             : 
    1162             :     /* Reduce set of parameters and quantize them */
    1163     4991664 :     for ( k = 0; k < num_iccs_to_code; ++k )
    1164             :     {
    1165     4144371 :         tmp_map[0] = hParamMC->hMetadataPMC.icc_mapping_conf->icc_mapping[k][0];
    1166     4144371 :         tmp_map[1] = hParamMC->hMetadataPMC.icc_mapping_conf->icc_mapping[k][1];
    1167     4144371 :         ICC_vect[k] = Cy[tmp_map[0]][tmp_map[1]];
    1168             :     }
    1169             : 
    1170             :     /* Quantization */
    1171      847293 :     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      847293 :     mvs2s( ICC_idx, ICC_idx_out + freq_idx * icc_map_size, num_iccs_to_code );
    1175             : 
    1176      847293 :     return;
    1177             : }
    1178             : 
    1179             : 
    1180             : /*-------------------------------------------------------------------------
    1181             :  * ivas_param_mc_parameter_quantizer()
    1182             :  *
    1183             :  * Parameter Quantization
    1184             :  *------------------------------------------------------------------------*/
    1185             : 
    1186     1694586 : 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     1694586 :     set_f( y, 0.0f, L );
    1200     1694586 :     idx_min = 0;
    1201             : 
    1202    10749106 :     for ( idx = 0; idx < L; ++idx )
    1203             :     {
    1204     9054520 :         tmp_min = 1000.0f;
    1205   120771872 :         for ( i = 0; i < sz_quantizer; ++i )
    1206             :         {
    1207   111717352 :             if ( fabsf( x[idx] - quantizer[i] ) < tmp_min )
    1208             :             {
    1209    58331854 :                 tmp_min = fabsf( x[idx] - quantizer[i] );
    1210    58331854 :                 idx_min = i;
    1211             :             }
    1212             :         }
    1213             : 
    1214     9054520 :         y[idx] = quantizer[idx_min];
    1215     9054520 :         quant_idx[idx] = idx_min;
    1216             :     }
    1217             : 
    1218     1694586 :     return;
    1219             : }
    1220             : 
    1221             : 
    1222             : /*-------------------------------------------------------------------------
    1223             :  * ivas_param_mc_transient_detection()
    1224             :  *
    1225             :  * Detect if the current frame has a transient
    1226             :  *------------------------------------------------------------------------*/
    1227             : 
    1228      261476 : 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      261476 :     push_wmops( "param_mc_trn_det" );
    1242             : 
    1243      261476 :     attackRatioThreshold = hTranDet->transientDetector.attackRatioThreshold;
    1244      261476 :     pSubblockNrg = &hTranDet->subblockEnergies.subblockNrg[hParamMC->transient_detector_delay];
    1245      261476 :     pAccSubblockNrg = &hTranDet->subblockEnergies.accSubblockNrg[hParamMC->transient_detector_delay];
    1246             : 
    1247      261476 :     bIsAttackPresent = FALSE;
    1248      261476 :     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      261476 :     if ( ( pSubblockNrg[-1] > pAccSubblockNrg[-1] * attackRatioThreshold ) || ( pSubblockNrg[-2] > pAccSubblockNrg[-2] * attackRatioThreshold ) )
    1254             :     {
    1255        1049 :         bIsAttackPresent = TRUE;
    1256        1049 :         attackIndex = 0;
    1257             :     }
    1258             : 
    1259     2353284 :     for ( i = 0; i < NSUBBLOCKS; i++ )
    1260             :     {
    1261     2091808 :         if ( pSubblockNrg[i] > pAccSubblockNrg[i] * attackRatioThreshold )
    1262             :         {
    1263       10168 :             bIsAttackPresent = TRUE;
    1264       10168 :             attackIndex = i;
    1265             :         }
    1266             :     }
    1267             : 
    1268             :     /* avoid post-echos on click sounds (very short transients) due to TNS aliasing */
    1269      261476 :     *pAttackIndex = attackIndex;
    1270      261476 :     *pbIsAttackPresent = bIsAttackPresent;
    1271             : 
    1272      261476 :     pop_wmops();
    1273             : 
    1274      261476 :     return;
    1275             : }
    1276             : 
    1277             : 
    1278             : /*-------------------------------------------------------------------------
    1279             :  * ivas_param_mc_entropy_encoder()
    1280             :  *
    1281             :  * Write the metadata bitstream
    1282             :  *------------------------------------------------------------------------*/
    1283             : 
    1284      125548 : 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      125548 :     push_wmops( "param_mc_prm_enc" );
    1303             : 
    1304             :     /* Init */
    1305      125548 :     set_zero( seq_tmp_uni, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP );
    1306      125548 :     set_s( seq_tmp, 0, PARAM_MC_MAX_PARAMETER_BANDS * PARAM_MC_SZ_ILD_MAP );
    1307      125548 :     nbands = hParamMC->hMetadataPMC.nbands_in_param_frame[hParamMC->hMetadataPMC.param_frame_idx];
    1308      125548 :     icc_map_size_wo_lfe = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_wo_lfe;
    1309      125548 :     icc_map_size = hParamMC->hMetadataPMC.icc_mapping_conf->icc_map_size_lfe;
    1310      125548 :     ild_map_size_wo_lfe = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_wo_lfe;
    1311      125548 :     ild_map_size = hParamMC->hMetadataPMC.ild_mapping_conf->ild_map_size_lfe;
    1312             : 
    1313             :     /*-----------------------------------------------------------------*
    1314             :      * Signaling bits
    1315             :      *-----------------------------------------------------------------*/
    1316             : 
    1317             :     /* reserved bit */
    1318      125548 :     bit_buffer[( *bit_pos )++] = hParamMC->hMetadataPMC.lfe_on;
    1319             : 
    1320             :     /* write coded band width */
    1321      125548 :     i = hParamMC->hMetadataPMC.coded_bwidth;
    1322      376644 :     for ( pos = 0; pos < 2; pos++ )
    1323             :     {
    1324      251096 :         bit_buffer[( *bit_pos )++] = (uint16_t) ( ( i >> pos ) & 1 );
    1325             :     }
    1326             : 
    1327             :     /* write param frame indicator */
    1328      125548 :     bit_buffer[( *bit_pos )++] = hParamMC->hMetadataPMC.param_frame_idx;
    1329             : 
    1330             :     /* write transient frame indicator */
    1331      125548 :     bit_buffer[( *bit_pos )++] = hParamMC->hMetadataPMC.bAttackPresent;
    1332             : 
    1333      125548 :     band_step = 1;
    1334      125548 :     if ( hParamMC->hMetadataPMC.bAttackPresent )
    1335             :     {
    1336        6321 :         band_step = PARAM_MC_TRANSIENT_BAND_STEP;
    1337       25284 :         for ( pos = 2; pos >= 0; --pos )
    1338             :         {
    1339       18963 :             bit_buffer[( *bit_pos )++] = (uint16_t) ( ( hParamMC->hMetadataPMC.attackIndex >> pos ) & 1 );
    1340             :         }
    1341        6321 :         nbands = hParamMC->hMetadataPMC.nbands_coded / band_step + ( ( hParamMC->hMetadataPMC.nbands_coded % band_step ) ? 1 : 0 );
    1342             :     }
    1343             : 
    1344      125548 :     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      125548 :     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      125548 :     pop_wmops();
    1350             : 
    1351      125548 :     return;
    1352             : }
    1353             : 
    1354             : 
    1355             : /*-------------------------------------------------------------------------
    1356             :  * ivas_param_mc_encode_parameter()
    1357             :  *
    1358             :  * (entropy) encode a sequence of parameter indices
    1359             :  *------------------------------------------------------------------------*/
    1360             : 
    1361      251096 : 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      251096 :     sz_seq = nbands * ( map_size_wo_lfe );
    1390             : 
    1391             :     /* Computing Delta Sequence */
    1392      251096 :     idx_prev = hParameterCodingInfo->quantizer_size / 2 + hParameterCodingInfo->quantizer_size % 2 - 1;
    1393      251096 :     idx_offset = hParameterCodingInfo->quantizer_size - 1;
    1394             : 
    1395     1562816 :     for ( j = 0; j < map_size_wo_lfe; ++j )
    1396             :     {
    1397     1311720 :         int16_t coding_band = 0;
    1398             : 
    1399    18633161 :         for ( i = 0; i < hMetadataPMC->nbands_coded; i += band_step )
    1400             :         {
    1401    17321441 :             if ( hMetadataPMC->bAttackPresent || hMetadataPMC->param_frame_idx == hMetadataPMC->coding_band_mapping[i] )
    1402             :             {
    1403     8916707 :                 idx = quant_idx[i * map_size + j];
    1404     8916707 :                 seq[coding_band + j * nbands] = idx;
    1405     8916707 :                 seq_delta[coding_band + j * nbands] = idx - idx_prev + idx_offset;
    1406     8916707 :                 idx_prev = idx;
    1407     8916707 :                 coding_band++;
    1408             :             }
    1409             :         }
    1410             :     }
    1411             : 
    1412             :     /* LFE */
    1413      251096 :     if ( hMetadataPMC->lfe_on )
    1414             :     {
    1415      459464 :         for ( i = 0; i < PARAM_MC_MAX_BAND_LFE; i += band_step )
    1416             :         {
    1417      229732 :             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      121018 :                 n_lfe_idx = map_size - map_size_wo_lfe;
    1422      247604 :                 for ( k = 0; k < n_lfe_idx; k++ )
    1423             :                 {
    1424      126586 :                     idx = quant_idx[( i + 1 ) * map_size - n_lfe_idx + k];
    1425      126586 :                     seq[sz_seq] = idx;
    1426      126586 :                     seq_delta[sz_seq] = idx - idx_prev + idx_offset;
    1427      126586 :                     idx_prev = idx;
    1428      126586 :                     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      251096 :     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      251096 :     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      251096 :     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      251096 :     bit_cnt_range_min = min( bit_cnt_range, bit_cnt_range_diff );
    1447             : 
    1448             :     /* uniform fallback */
    1449      251096 :     if ( bit_cnt_range_min > bit_cnt_uni )
    1450             :     {
    1451             :         /* Uniform coding is used */
    1452       14530 :         bit_buffer[( *bit_pos )++] = 0;
    1453       14530 :         bit_pos_tmp = 0;
    1454             : 
    1455      472192 :         for ( i = 0; i < sz_seq; ++i )
    1456             :         {
    1457      457662 :             ivas_param_mc_dec2bin( seq[i], hParameterCodingInfo->uni_bits, &bit_buffer[*( bit_pos ) + bit_pos_tmp] );
    1458      457662 :             bit_pos_tmp += hParameterCodingInfo->uni_bits;
    1459             :         }
    1460       14530 :         *bit_pos = *bit_pos + bit_pos_tmp;
    1461             :     }
    1462             :     else
    1463             :     {
    1464             :         /* Range Coding is used */
    1465      236566 :         bit_buffer[( *bit_pos )++] = 1;
    1466      236566 :         if ( bit_cnt_range_diff < bit_cnt_range )
    1467             :         {
    1468      230591 :             bit_buffer[( *bit_pos )++] = 1;
    1469    20079666 :             for ( i = 0; i < bit_cnt_range_diff; i++ )
    1470             :             {
    1471    19849075 :                 bit_buffer[( *bit_pos )++] = tmp_bit_buffer_diff[i];
    1472             :             }
    1473             :         }
    1474             :         else
    1475             :         {
    1476        5975 :             bit_buffer[( *bit_pos )++] = 0;
    1477      551055 :             for ( i = 0; i < bit_cnt_range; i++ )
    1478             :             {
    1479      545080 :                 bit_buffer[( *bit_pos )++] = tmp_bit_buffer[i];
    1480             :             }
    1481             :         }
    1482             :     }
    1483             : 
    1484      251096 :     return;
    1485             : }
    1486             : 
    1487             : 
    1488             : /*-------------------------------------------------------------------------
    1489             :  * ivas_param_mc_dec2bin()
    1490             :  *
    1491             :  * Decimal to binary routine
    1492             :  *------------------------------------------------------------------------*/
    1493             : 
    1494      457662 : 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      457662 :     idx = 0;
    1502             :     /* convert value to bitstream, MSB first */
    1503     1830684 :     for ( idx = 0; idx < N; idx++ )
    1504             :     {
    1505     1373022 :         bits[idx] = ( val >> ( N - 1 - idx ) ) & 1;
    1506             :     }
    1507             : 
    1508      457662 :     return;
    1509             : }
    1510             : 
    1511             : 
    1512             : /*-------------------------------------------------------------------*
    1513             :  * ivas_param_mc_range_encoder()
    1514             :  *
    1515             :  * Parametric MC Range encoder
    1516             :  *-------------------------------------------------------------------*/
    1517             : 
    1518      502192 : 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      502192 :     rc_uni_enc_init( &rc_st_enc );
    1537             : 
    1538             :     /* Main loop over the length of the sequence */
    1539    17747330 :     for ( i = 0; i < num_symbols; ++i )
    1540             :     {
    1541    17371027 :         rc_uni_enc_encode_symbol_fastS( &rc_st_enc, (uint16_t) seq_in[i], cum_freq, sym_freq, tot_shift );
    1542             : 
    1543    17371027 :         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      125889 :             *bit_pos = MAX_BITS_PER_FRAME;
    1547      125889 :             return;
    1548             :         }
    1549             :     }
    1550             : 
    1551             :     /* Finish range encoder */
    1552      376303 :     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     5011361 :     for ( i = 0; i < ( rc_tot_bits >> 3 ); ++i )
    1558             :     {
    1559             :         /* use rc_st_enc.byte_buffer */
    1560     4635058 :         bits = &bit_buffer[i * 8];
    1561             : 
    1562     4635058 :         byte = rc_st_enc.byte_buffer[i];
    1563             : 
    1564     4635058 :         bits[0] = ( byte >> 7 ) & 1;
    1565     4635058 :         bits[1] = ( byte >> 6 ) & 1;
    1566     4635058 :         bits[2] = ( byte >> 5 ) & 1;
    1567     4635058 :         bits[3] = ( byte >> 4 ) & 1;
    1568     4635058 :         bits[4] = ( byte >> 3 ) & 1;
    1569     4635058 :         bits[5] = ( byte >> 2 ) & 1;
    1570     4635058 :         bits[6] = ( byte >> 1 ) & 1;
    1571     4635058 :         bits[7] = byte & 1;
    1572             :     }
    1573             : 
    1574             :     /* 2) Push remaining bits */
    1575      376303 :     if ( ( rc_tot_bits & 7 ) != 0 )
    1576             :     {
    1577      330285 :         uint8_t rem_bits = rc_tot_bits & 7;
    1578             : 
    1579      330285 :         bits = &bit_buffer[i * 8];
    1580      330285 :         byte = rc_st_enc.byte_buffer[i];
    1581             : 
    1582     1645609 :         for ( k = 0; k < rem_bits; k++ )
    1583             :         {
    1584     1315324 :             bits[k] = ( byte >> ( 7 - k ) ) & 1;
    1585             :         }
    1586             :     }
    1587             : 
    1588             :     /* Update output number of bits */
    1589      376303 :     *bit_pos = rc_tot_bits;
    1590             : 
    1591      376303 :     return;
    1592             : }

Generated by: LCOV version 1.14