LCOV - code coverage report
Current view: top level - lib_rend - ivas_dirac_dec_binaural_functions.c (source / functions) Hit Total Coverage
Test: Coverage on main -- conformance test test_26252.py @ 0c5691e6405a865cd50088c4936e8acb16f658a1 Lines: 1251 1353 92.5 %
Date: 2025-12-18 05:24:35 Functions: 26 26 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 <stdint.h>
      34             : #include "options.h"
      35             : #include <assert.h>
      36             : #include <math.h>
      37             : #include "prot.h"
      38             : #include "ivas_prot.h"
      39             : #include "ivas_prot_rend.h"
      40             : #include "ivas_cnst.h"
      41             : #include "ivas_rom_binauralRenderer.h"
      42             : #include "ivas_rom_rend.h"
      43             : #include "ivas_rom_com.h"
      44             : 
      45             : #ifdef DEBUGGING
      46             : #include "debug.h"
      47             : #endif
      48             : #include "wmc_auto.h"
      49             : 
      50             : 
      51             : /*-------------------------------------------------------------------------
      52             :  * Local constants
      53             :  *------------------------------------------------------------------------*/
      54             : 
      55             : #define CLDFB_HALF_BIN_FREQUENCY_OFFSET         0.5f
      56             : #define IVAS_TDET_DUCK_MULT_FAC_PARA_BIN        ( 2.0f )
      57             : #define IVAS_TDET_DUCK_MULT_FAC_PARA_BIN_LOW_BR ( 3.0f )
      58             : #define SBA_CARDI_TARGET_ENERGY_GAIN            0.5f
      59             : 
      60             : #define STEREO_PREPROCESS_IIR_FACTOR ( 0.9f )
      61             : 
      62             : /* powf(0.95f, 4.0f) for sub-frame smoothing instead of CLDFB slot */
      63             : #define ADAPT_HTPROTO_IIR_FAC 0.81450625f
      64             : 
      65             : #define ADAPT_HTPROTO_ILD_LIM_DB0 1.0f
      66             : #define ADAPT_HTPROTO_ILD_LIM_DB1 4.0f
      67             : #define ADAPT_HTPROTO_ROT_LIM_0   0.4f
      68             : #define ADAPT_HTPROTO_ROT_LIM_1   0.8f
      69             : 
      70             : #define MAX_GAIN_CACHE_SIZE ( ( MASA_MAXIMUM_DIRECTIONS * 3 ) + MAX_NUM_OBJECTS ) /* == different calls to get gains */
      71             : 
      72             : typedef struct hrtfGainCache
      73             : {
      74             :     int16_t azi;
      75             :     int16_t ele;
      76             : 
      77             :     float shVec[HRTF_SH_CHANNELS];
      78             : } PARAMBIN_HRTF_GAIN_CACHE;
      79             : 
      80             : typedef struct parambin_rend_config_data
      81             : {
      82             :     int16_t separateCenterChannelRendering;
      83             :     IVAS_FORMAT ivas_format;
      84             :     MC_MODE mc_mode;
      85             :     int32_t ivas_total_brate;
      86             :     int16_t nchan_transport;
      87             :     float qualityBasedSmFactor;
      88             :     int16_t processReverb;
      89             :     ISM_MODE ism_mode;
      90             : } PARAMBIN_REND_CONFIG, *PARAMBIN_REND_CONFIG_HANDLE;
      91             : 
      92             : 
      93             : /*-------------------------------------------------------------------------
      94             :  * Local function prototypes
      95             :  *------------------------------------------------------------------------*/
      96             : 
      97             : static void ivas_dirac_dec_binaural_internal( Decoder_Struct *st_ivas, COMBINED_ORIENTATION_HANDLE hCombinedOrientationData, float *output_f[], const int16_t nchan_transport, const int16_t subframe );
      98             : 
      99             : static void ivas_dirac_dec_decorrelate_slot( DIRAC_DEC_BIN_HANDLE hDiracDecBin, const int16_t num_freq_bands, const int16_t slot, float inRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], float inIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], float decRe[][CLDFB_NO_CHANNELS_MAX], float decIm[][CLDFB_NO_CHANNELS_MAX] );
     100             : 
     101             : static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices( DIRAC_DEC_BIN_HANDLE hDiracDecBin, const SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, const PARAMBIN_REND_CONFIG_HANDLE hConfig, float inRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], float inIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], const int16_t subframe, float *subFrameTotalEne, float *IIReneLimiter );
     102             : 
     103             : static void ivas_dirac_dec_binaural_formulate_target_covariance_matrices( DIRAC_DEC_BIN_HANDLE hDiracDecBin, const SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, const PARAMBIN_REND_CONFIG_HANDLE hConfig, float Rmat[3][3], const int16_t subframe, const int16_t isHeadtracked, const float *subFrameTotalEne, const float *IIReneLimiter, const MASA_ISM_DATA_HANDLE hMasaIsmData );
     104             : 
     105             : static void ivas_dirac_dec_binaural_determine_processing_matrices( DIRAC_DEC_BIN_HANDLE hDiracDecBin, SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, PARAMBIN_REND_CONFIG_HANDLE hConfig, const int16_t max_band_decorr, float Rmat[3][3], const int16_t subframe, const int16_t isHeadtracked, const int16_t nchanSeparateChannels, const MASA_ISM_DATA_HANDLE hMasaIsmData );
     106             : 
     107             : static void ivas_dirac_dec_binaural_process_output( DIRAC_DEC_BIN_HANDLE hDiracDecBin, SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom, HANDLE_CLDFB_FILTER_BANK cldfbSynDec[MAX_OUTPUT_CHANNELS], float *output_f[], float inRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], float inIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], const int16_t max_band_decorr, const int16_t numInChannels, const int16_t processReverb, const int16_t subframe, float outRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], float outIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], float reverbRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], float reverbIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], float decorrRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], float decorrIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], const uint8_t recompute );
     108             : 
     109             : static void adaptTransportSignalsHeadtracked( COMBINED_ORIENTATION_HANDLE hHeadTrackData, float inRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], float inIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], const int16_t nBins, const int16_t nSlots, float Rmat[3][3] );
     110             : 
     111             : static void ivas_dirac_dec_binaural_check_and_switch_transports_headtracked( COMBINED_ORIENTATION_HANDLE hHeadTrackData, float inRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], float inIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX], const int16_t nBins, const int16_t nSlots, float Rmat[3][3] );
     112             : 
     113             : static void formulate2x2MixingMatrix( float Ein1, float Ein2, float CinRe, float CinIm, float Eout1, float Eout2, float CoutRe, float CoutIm, float Q[BINAURAL_CHANNELS][BINAURAL_CHANNELS], float Mre[BINAURAL_CHANNELS][BINAURAL_CHANNELS], float Mim[BINAURAL_CHANNELS][BINAURAL_CHANNELS], const float regularizationFactor );
     114             : 
     115             : static void hrtfShGetHrtf( const int16_t bin, const int16_t aziDeg, const int16_t eleDeg, float *lRealp, float *lImagp, float *rRealp, float *rImagp, PARAMBIN_HRTF_GAIN_CACHE *gainCache, const int16_t useCachedValue, HRTFS_PARAMBIN_HANDLE hHrtfParambin );
     116             : 
     117             : static void getDirectPartGains( const int16_t bin, int16_t aziDeg, int16_t eleDeg, float *lRealp, float *lImagp, float *rRealp, float *rImagp, const uint8_t stereoMode, float Rmat[3][3], PARAMBIN_HRTF_GAIN_CACHE *gainCache, const int16_t isHeadtracked, HRTFS_PARAMBIN_HANDLE hHrtfParambin );
     118             : 
     119             : static void matrixMul( float Are[BINAURAL_CHANNELS][BINAURAL_CHANNELS], float Aim[BINAURAL_CHANNELS][BINAURAL_CHANNELS], float Bre[BINAURAL_CHANNELS][BINAURAL_CHANNELS], float Bim[BINAURAL_CHANNELS][BINAURAL_CHANNELS], float outRe[BINAURAL_CHANNELS][BINAURAL_CHANNELS], float outIm[BINAURAL_CHANNELS][BINAURAL_CHANNELS] );
     120             : 
     121             : static void matrixTransp2Mul( float Are[BINAURAL_CHANNELS][BINAURAL_CHANNELS], float Aim[BINAURAL_CHANNELS][BINAURAL_CHANNELS], float Bre[BINAURAL_CHANNELS][BINAURAL_CHANNELS], float Bim[BINAURAL_CHANNELS][BINAURAL_CHANNELS], float outRe[BINAURAL_CHANNELS][BINAURAL_CHANNELS], float outIm[BINAURAL_CHANNELS][BINAURAL_CHANNELS] );
     122             : 
     123             : static void ivas_masa_ext_rend_parambin_internal( MASA_EXT_REND_HANDLE hMasaExtRend, COMBINED_ORIENTATION_HANDLE hCombinedOrientationData, float *output_f[], const int16_t subframe, const SPLIT_REND_WRAPPER *hSplitRendWrapper, float Cldfb_Out_Real[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], float Cldfb_Out_Imag[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX] );
     124             : 
     125             : 
     126             : /*-------------------------------------------------------------------------
     127             :  * ivas_dirac_dec_init_binaural_data()
     128             :  *
     129             :  * Initialize parametric binaural renderer
     130             :  *------------------------------------------------------------------------*/
     131             : 
     132        1224 : ivas_error ivas_dirac_dec_init_binaural_data(
     133             :     Decoder_Struct *st_ivas,              /* i/o: IVAS decoder structure                  */
     134             :     HRTFS_PARAMBIN_HANDLE *phHrtfParambin /* i  : HRTF structure for rendering            */
     135             : )
     136             : {
     137             :     DIRAC_DEC_BIN_HANDLE hDiracDecBin;
     138             :     int16_t nBins;
     139             :     int32_t output_Fs;
     140             :     RENDERER_TYPE renderer_type;
     141             :     int16_t j, k, bin;
     142             :     float binCenterFreq, tmpFloat;
     143             :     ivas_error error;
     144             :     float frequency_axis[CLDFB_NO_CHANNELS_MAX];
     145             :     int16_t num_poses, pos_idx;
     146             :     const IVAS_ROOM_ACOUSTICS_CONFIG_DATA *pRoomAcoustics;
     147             : 
     148        1224 :     num_poses = 1;
     149        1224 :     if ( st_ivas->hSplitBinRend != NULL )
     150             :     {
     151         480 :         num_poses = st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses;
     152             :     }
     153             : 
     154        1224 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
     155        1224 :     nBins = st_ivas->hSpatParamRendCom->num_freq_bands;
     156        1224 :     renderer_type = st_ivas->renderer_type;
     157             : 
     158        3920 :     for ( pos_idx = 0; pos_idx < num_poses; pos_idx++ )
     159             :     {
     160        2696 :         hDiracDecBin = st_ivas->hDiracDecBin[pos_idx];
     161             : 
     162        2696 :         if ( hDiracDecBin == NULL )
     163             :         {
     164        2696 :             if ( ( hDiracDecBin = (DIRAC_DEC_BIN_HANDLE) malloc( sizeof( DIRAC_DEC_BIN_DATA ) ) ) == NULL )
     165             :             {
     166           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC binaural handle " );
     167             :             }
     168             : 
     169        2696 :             hDiracDecBin->hTdDecorr = NULL;
     170        2696 :             hDiracDecBin->hReverb = NULL;
     171        2696 :             hDiracDecBin->h_freq_domain_decorr_ap_params = NULL;
     172        2696 :             hDiracDecBin->h_freq_domain_decorr_ap_state = NULL;
     173        2696 :             hDiracDecBin->phHrtfParambin = NULL;
     174             :         }
     175             : 
     176        8088 :         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
     177             :         {
     178       37744 :             for ( k = 0; k < BINAURAL_CHANNELS + MAX_NUM_OBJECTS; k++ )
     179             :             {
     180       32352 :                 set_zero( hDiracDecBin->processMtxRe[j][k], nBins );
     181       32352 :                 set_zero( hDiracDecBin->processMtxIm[j][k], nBins );
     182             :             }
     183             : 
     184       16176 :             for ( k = 0; k < BINAURAL_CHANNELS; k++ )
     185             :             {
     186       10784 :                 set_zero( hDiracDecBin->processMtxDecRe[j][k], nBins );
     187       10784 :                 set_zero( hDiracDecBin->processMtxDecIm[j][k], nBins );
     188             :             }
     189        5392 :             set_zero( hDiracDecBin->ChEnePrev[j], nBins );
     190        5392 :             set_zero( hDiracDecBin->ChEneOutPrev[j], nBins );
     191             :         }
     192        2696 :         set_zero( hDiracDecBin->ChCrossRePrev, nBins );
     193        2696 :         set_zero( hDiracDecBin->ChCrossImPrev, nBins );
     194        2696 :         set_zero( hDiracDecBin->ChCrossReOutPrev, nBins );
     195        2696 :         set_zero( hDiracDecBin->ChCrossImOutPrev, nBins );
     196        2696 :         hDiracDecBin->renderStereoOutputInsteadOfBinaural = 0;
     197             : 
     198             : 
     199      158496 :         for ( bin = 0; bin < nBins; bin++ )
     200             :         {
     201      155800 :             binCenterFreq = ( (float) bin + CLDFB_HALF_BIN_FREQUENCY_OFFSET ) / (float) nBins * ( (float) output_Fs / 2.0f );
     202             :             /* These formulas and values are from Christian Borss's publication for binaural diffuse field coherence */
     203      155800 :             tmpFloat = max( 0.0f, 1.0f - binCenterFreq / 2700.0f );
     204      155800 :             hDiracDecBin->diffuseFieldCoherence[bin] = tmpFloat * sinf( binCenterFreq * EVS_PI / 550.0f ) / ( binCenterFreq * EVS_PI / 550.0f );
     205             :         }
     206             : 
     207       26960 :         for ( bin = 0; bin < BINAURAL_COHERENCE_DIFFERENCE_BINS; bin++ )
     208             :         {
     209       24264 :             hDiracDecBin->diffuseFieldCoherenceX[bin] = hDiracDecBin->diffuseFieldCoherence[bin] + diffuseFieldCoherenceDifferenceX[bin];
     210       24264 :             hDiracDecBin->diffuseFieldCoherenceY[bin] = hDiracDecBin->diffuseFieldCoherence[bin] + diffuseFieldCoherenceDifferenceY[bin];
     211       24264 :             hDiracDecBin->diffuseFieldCoherenceZ[bin] = hDiracDecBin->diffuseFieldCoherence[bin] + diffuseFieldCoherenceDifferenceZ[bin];
     212             :         }
     213             : 
     214        2696 :         if ( renderer_type == RENDERER_BINAURAL_PARAMETRIC ) /* Indication of binaural rendering without room effect */
     215             :         {
     216        2332 :             set_f( hDiracDecBin->earlyPartEneCorrection, 1.0f, CLDFB_NO_CHANNELS_MAX );
     217        2332 :             hDiracDecBin->hReverb = NULL;
     218             :         }
     219         364 :         else if ( renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) /* Indication of binaural rendering with room effect */
     220             :         {
     221         353 :             if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
     222             :             {
     223         238 :                 mvr2r( ( *phHrtfParambin )->parametricEarlyPartEneCorrection, hDiracDecBin->earlyPartEneCorrection, nBins );
     224         238 :                 pRoomAcoustics = NULL;
     225             :             }
     226             :             else
     227             :             {
     228         115 :                 set_f( hDiracDecBin->earlyPartEneCorrection, 1.0f, CLDFB_NO_CHANNELS_MAX );
     229         115 :                 pRoomAcoustics = &( st_ivas->hRenderConfig->roomAcoustics );
     230             :             }
     231             : 
     232             :             /* reconfiguration needed when Reverb. parameters are changed -> close and open the handle again */
     233         353 :             if ( hDiracDecBin->hReverb != NULL && ( ( hDiracDecBin->hReverb->numBins != nBins ) ||
     234           0 :                                                     ( hDiracDecBin->hReverb->blockSize != CLDFB_SLOTS_PER_SUBFRAME ) ) )
     235             :             {
     236           0 :                 ivas_binaural_reverb_close( &( hDiracDecBin->hReverb ) );
     237             :             }
     238             : 
     239         353 :             if ( hDiracDecBin->hReverb == NULL && pos_idx == 0 ) /* open reverb only for the main direction */
     240             :             {
     241         353 :                 if ( ( error = ivas_binaural_reverb_init( &hDiracDecBin->hReverb,
     242             :                                                           st_ivas->hHrtfStatistics,
     243             :                                                           nBins,
     244             :                                                           CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES,
     245             :                                                           pRoomAcoustics,
     246             :                                                           output_Fs,
     247         353 :                                                           ( *phHrtfParambin )->parametricReverberationTimes,
     248         353 :                                                           ( *phHrtfParambin )->parametricReverberationEneCorrections,
     249         353 :                                                           hDiracDecBin->earlyPartEneCorrection ) ) != IVAS_ERR_OK )
     250             :                 {
     251           0 :                     return error;
     252             :                 }
     253             :             }
     254             :         }
     255          11 :         else if ( renderer_type == RENDERER_STEREO_PARAMETRIC )
     256             :         {
     257          11 :             set_f( hDiracDecBin->earlyPartEneCorrection, 1.0f, CLDFB_NO_CHANNELS_MAX );
     258          11 :             hDiracDecBin->hReverb = NULL;
     259          11 :             hDiracDecBin->renderStereoOutputInsteadOfBinaural = 1;
     260             :         }
     261             :         else /* Not valid renderer type for this renderer */
     262             :         {
     263           0 :             assert( false );
     264             :         }
     265             : 
     266        2696 :         hDiracDecBin->hDiffuseDist = NULL; /* Memory is allocated from stack during runtime when needed */
     267             : 
     268        2696 :         if ( hDiracDecBin->hTdDecorr == NULL )
     269             :         {
     270        2696 :             hDiracDecBin->useTdDecorr = 0;
     271             :         }
     272             : 
     273        2696 :         if ( hDiracDecBin->h_freq_domain_decorr_ap_params != NULL )
     274             :         {
     275           0 :             ivas_dirac_dec_decorr_close( &hDiracDecBin->h_freq_domain_decorr_ap_params, &hDiracDecBin->h_freq_domain_decorr_ap_state );
     276             :         }
     277             : 
     278        2696 :         if ( pos_idx == 0 ) /* open decorrelator only for the main direction */
     279             :         {
     280        1224 :             if ( ( error = ivas_td_decorr_reconfig_dec( st_ivas->ivas_format, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->nchan_transport, st_ivas->hDecoderConfig->output_Fs, &( hDiracDecBin->hTdDecorr ), &( hDiracDecBin->useTdDecorr ) ) ) != IVAS_ERR_OK )
     281             :             {
     282           0 :                 return error;
     283             :             }
     284             : 
     285        1224 :             if ( !hDiracDecBin->useTdDecorr && !( st_ivas->ivas_format == ISM_FORMAT && st_ivas->ism_mode == ISM_MODE_PARAM ) )
     286             :             {
     287         509 :                 ivas_dirac_dec_get_frequency_axis( frequency_axis, output_Fs, nBins );
     288             : 
     289         509 :                 if ( ( error = ivas_dirac_dec_decorr_open( &( hDiracDecBin->h_freq_domain_decorr_ap_params ),
     290         509 :                                                            &( hDiracDecBin->h_freq_domain_decorr_ap_state ),
     291             :                                                            nBins,
     292             :                                                            BINAURAL_CHANNELS,
     293             :                                                            BINAURAL_CHANNELS,
     294             :                                                            DIRAC_SYNTHESIS_PSD_LS,
     295             :                                                            frequency_axis,
     296             :                                                            BINAURAL_CHANNELS,
     297             :                                                            output_Fs ) ) != IVAS_ERR_OK )
     298             :                 {
     299           0 :                     return error;
     300             :                 }
     301             :             }
     302             :         }
     303             :         else
     304             :         {
     305        1472 :             hDiracDecBin->useTdDecorr = st_ivas->hDiracDecBin[0]->useTdDecorr; /* copy the flag, but the implementation re-uses the decorrelated signal */
     306             :         }
     307             : 
     308        2696 :         hDiracDecBin->reqularizationFactor = configure_reqularization_factor( st_ivas->ivas_format, st_ivas->hDecoderConfig->ivas_total_brate );
     309             : 
     310        2696 :         hDiracDecBin->phHrtfParambin = phHrtfParambin;
     311             : 
     312        2696 :         st_ivas->hDiracDecBin[pos_idx] = hDiracDecBin;
     313             :     }
     314             : 
     315             :     /* allocate transport channels */
     316        1224 :     if ( st_ivas->hTcBuffer == NULL )
     317             :     {
     318             :         int16_t nchan_to_allocate;
     319             :         int16_t n_samples_granularity;
     320             : 
     321         484 :         nchan_to_allocate = BINAURAL_CHANNELS;
     322         484 :         if ( st_ivas->hDiracDecBin[0] != NULL && st_ivas->hDiracDecBin[0]->useTdDecorr )
     323             :         {
     324         132 :             nchan_to_allocate = 2 * BINAURAL_CHANNELS;
     325             :         }
     326         352 :         else if ( st_ivas->hOutSetup.separateChannelEnabled )
     327             :         {
     328           0 :             nchan_to_allocate++;
     329             :         }
     330         484 :         if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
     331             :         {
     332         225 :             nchan_to_allocate = BINAURAL_CHANNELS + st_ivas->nchan_ism;
     333             :         }
     334             : 
     335         484 :         n_samples_granularity = ivas_jbm_dec_get_render_granularity( st_ivas->renderer_type, ivas_renderer_secondary_select( st_ivas ), output_Fs );
     336             : 
     337         484 :         if ( ( error = ivas_jbm_dec_tc_buffer_open( st_ivas, TC_BUFFER_MODE_RENDERER, ivas_jbm_dec_get_num_tc_channels( st_ivas ), nchan_to_allocate, nchan_to_allocate, n_samples_granularity ) ) != IVAS_ERR_OK )
     338             :         {
     339           0 :             return error;
     340             :         }
     341             :     }
     342        1224 :     return IVAS_ERR_OK;
     343             : }
     344             : 
     345             : 
     346             : /*-------------------------------------------------------------------------
     347             :  * ivas_dirac_dec_close_binaural_data()
     348             :  *
     349             :  * Close parametric binaural renderer
     350             :  *------------------------------------------------------------------------*/
     351             : 
     352        7965 : void ivas_dirac_dec_close_binaural_data(
     353             :     DIRAC_DEC_BIN_HANDLE *hBinaural /* i/o: decoder DirAC binaural data handle  */
     354             : )
     355             : {
     356             :     int16_t pos_idx;
     357             : 
     358        7965 :     if ( hBinaural == NULL || *hBinaural == NULL )
     359             :     {
     360        6554 :         return;
     361             :     }
     362             : 
     363       12699 :     for ( pos_idx = 0; pos_idx < MAX_HEAD_ROT_POSES; pos_idx++ )
     364             :     {
     365       11288 :         if ( hBinaural[pos_idx] != NULL )
     366             :         {
     367        2883 :             if ( hBinaural[pos_idx]->hReverb != NULL )
     368             :             {
     369         472 :                 ivas_binaural_reverb_close( &( hBinaural[pos_idx]->hReverb ) );
     370             :             }
     371             : 
     372        2883 :             ivas_td_decorr_dec_close( &( hBinaural[pos_idx]->hTdDecorr ) );
     373             : 
     374        2883 :             if ( hBinaural[pos_idx]->h_freq_domain_decorr_ap_params != NULL )
     375             :             {
     376         702 :                 ivas_dirac_dec_decorr_close( &( hBinaural[pos_idx]->h_freq_domain_decorr_ap_params ), &( hBinaural[pos_idx]->h_freq_domain_decorr_ap_state ) );
     377             :             }
     378             : 
     379        2883 :             free( hBinaural[pos_idx] );
     380        2883 :             hBinaural[pos_idx] = NULL;
     381             :         }
     382             :     }
     383             : 
     384        1411 :     return;
     385             : }
     386             : 
     387             : /*-------------------------------------------------------------------------
     388             :  * ivas_dirac_dec_binaural_copy_hrtfs()
     389             :  *
     390             :  * Temporary function for copying HRTF data from rom tables if no binary
     391             :  * file was given.
     392             :  *------------------------------------------------------------------------*/
     393             : 
     394        7862 : ivas_error ivas_dirac_dec_binaural_copy_hrtfs(
     395             :     HRTFS_PARAMBIN_HANDLE *hHrtfParambin /* i/o: HRTF structure for rendering */
     396             : )
     397             : {
     398             :     int16_t i, j;
     399             : 
     400        7862 :     if ( hHrtfParambin != NULL && *hHrtfParambin != NULL )
     401             :     {
     402             :         /* Tables already loaded from file */
     403        7099 :         return IVAS_ERR_OK;
     404             :     }
     405             :     else
     406             :     {
     407             :         /* Initialise tables from ROM */
     408             :         HRTFS_PARAMBIN *hrtfParambin;
     409             : 
     410         763 :         if ( ( hrtfParambin = (HRTFS_PARAMBIN *) malloc( sizeof( HRTFS_PARAMBIN ) ) ) == NULL )
     411             :         {
     412           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate memory for parametric binauralizer HRTF tables" );
     413             :         }
     414             : 
     415        2289 :         for ( i = 0; i < BINAURAL_CHANNELS; i++ )
     416             :         {
     417       25942 :             for ( j = 0; j < HRTF_SH_CHANNELS; j++ )
     418             :             {
     419       24416 :                 mvr2r( hrtfShCoeffsRe[i][j], hrtfParambin->hrtfShCoeffsRe[i][j], HRTF_NUM_BINS );
     420       24416 :                 mvr2r( hrtfShCoeffsIm[i][j], hrtfParambin->hrtfShCoeffsIm[i][j], HRTF_NUM_BINS );
     421             :             }
     422             :         }
     423             : 
     424         763 :         mvr2r( parametricReverberationTimes, hrtfParambin->parametricReverberationTimes, CLDFB_NO_CHANNELS_MAX );
     425         763 :         mvr2r( parametricReverberationEneCorrections, hrtfParambin->parametricReverberationEneCorrections, CLDFB_NO_CHANNELS_MAX );
     426         763 :         mvr2r( parametricEarlyPartEneCorrection, hrtfParambin->parametricEarlyPartEneCorrection, CLDFB_NO_CHANNELS_MAX );
     427             : 
     428         763 :         *hHrtfParambin = hrtfParambin;
     429             : 
     430         763 :         return IVAS_ERR_OK;
     431             :     }
     432             : }
     433             : 
     434             : 
     435             : /*-------------------------------------------------------------------------
     436             :  * void ivas_dirac_dec_binaural_render()
     437             :  *
     438             :  *
     439             :  *------------------------------------------------------------------------*/
     440             : 
     441      213167 : void ivas_dirac_dec_binaural_render(
     442             :     Decoder_Struct *st_ivas,       /* i/o: IVAS decoder handle                      */
     443             :     const uint16_t nSamplesAsked,  /* i  : number of CLDFB slots requested          */
     444             :     uint16_t *nSamplesRendered,    /* o  : number of CLDFB slots rendered           */
     445             :     uint16_t *nSamplesAvailable,   /* o  : number of CLDFB slots still to render    */
     446             :     const int16_t nchan_transport, /* i  : number of transport channels             */
     447             :     float *output_f[]              /* o  : rendered time signal                     */
     448             : )
     449             : {
     450             :     int16_t slots_to_render, first_sf, last_sf, subframe_idx;
     451             :     uint16_t slot_size, ch;
     452             :     uint16_t nchan_out;
     453             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
     454             :     float *output_f_local[MAX_OUTPUT_CHANNELS];
     455             : 
     456      213167 :     hSpatParamRendCom = st_ivas->hSpatParamRendCom;
     457      213167 :     nchan_out = BINAURAL_CHANNELS;
     458             : #ifdef DEBUGGING
     459             :     assert( hSpatParamRendCom );
     460             : #endif
     461      639501 :     for ( ch = 0; ch < nchan_out; ch++ )
     462             :     {
     463      426334 :         output_f_local[ch] = output_f[ch];
     464             :     }
     465      213167 :     slot_size = NS2SA( st_ivas->hDecoderConfig->output_Fs, CLDFB_SLOT_NS );
     466             : 
     467             :     /* loop for synthesis, assume we always have to render in multiples of 5ms subframes with spills */
     468      213167 :     slots_to_render = min( hSpatParamRendCom->num_slots - hSpatParamRendCom->slots_rendered, nSamplesAsked / slot_size );
     469      213167 :     *nSamplesRendered = slots_to_render * slot_size;
     470      213167 :     first_sf = hSpatParamRendCom->subframes_rendered;
     471      213167 :     last_sf = first_sf;
     472             : 
     473      984116 :     while ( slots_to_render > 0 )
     474             :     {
     475      770949 :         slots_to_render -= hSpatParamRendCom->subframe_nbslots[last_sf];
     476      770949 :         last_sf++;
     477             :     }
     478             : 
     479             : #ifdef DEBUGGING
     480             :     assert( slots_to_render == 0 );
     481             : #endif
     482      984116 :     for ( subframe_idx = first_sf; subframe_idx < last_sf; subframe_idx++ )
     483             :     {
     484      770949 :         int16_t n_samples_sf = slot_size * hSpatParamRendCom->subframe_nbslots[subframe_idx];
     485      770949 :         ivas_dirac_dec_binaural_internal( st_ivas, st_ivas->hCombinedOrientationData, output_f_local, nchan_transport, subframe_idx );
     486             : 
     487     2312847 :         for ( ch = 0; ch < nchan_out; ch++ )
     488             :         {
     489     1541898 :             output_f_local[ch] += n_samples_sf;
     490             :         }
     491             : 
     492             :         /* update combined orientation access index */
     493      770949 :         ivas_combined_orientation_update_index( st_ivas->hCombinedOrientationData, n_samples_sf );
     494             :     }
     495             : 
     496      213167 :     if ( hSpatParamRendCom->slots_rendered == hSpatParamRendCom->num_slots )
     497             :     {
     498      190712 :         hSpatParamRendCom->dirac_read_idx = ( hSpatParamRendCom->dirac_read_idx + DEFAULT_JBM_SUBFRAMES_5MS ) % hSpatParamRendCom->dirac_md_buffer_length;
     499             :     }
     500             : 
     501      213167 :     *nSamplesAvailable = ( hSpatParamRendCom->num_slots - hSpatParamRendCom->slots_rendered ) * slot_size;
     502             : 
     503      213167 :     return;
     504             : }
     505             : 
     506             : 
     507             : /*-------------------------------------------------------------------------
     508             :  * ivas_dirac_dec_binaural_sba_gain()
     509             :  *
     510             :  * loudness correction for parametric binaural renderer
     511             :  *------------------------------------------------------------------------*/
     512             : 
     513       67408 : void ivas_dirac_dec_binaural_sba_gain(
     514             :     float *output[],              /* i/o: synthesized core-coder transport channels/DirAC output    */
     515             :     const int16_t nchan_remapped, /* i  : num channels after remapping of TCs                       */
     516             :     const int16_t output_frame    /* i  : output frame length                                       */
     517             : )
     518             : {
     519             :     int16_t n;
     520             :     float gain;
     521             : 
     522       67408 :     if ( nchan_remapped == 1 )
     523             :     {
     524       17077 :         gain = 1.4454f;
     525             :     }
     526             :     else
     527             :     {
     528       50331 :         gain = 1.3657f;
     529             :     }
     530             : 
     531      185147 :     for ( n = 0; n < nchan_remapped; n++ )
     532             :     {
     533      117739 :         v_multc( output[n], gain, output[n], output_frame );
     534             :     }
     535             : 
     536       67408 :     return;
     537             : }
     538             : 
     539             : 
     540             : /*-------------------------------------------------------------------------
     541             :  * Local functions
     542             :  *------------------------------------------------------------------------*/
     543             : 
     544      770949 : static void ivas_dirac_dec_binaural_internal(
     545             :     Decoder_Struct *st_ivas,
     546             :     COMBINED_ORIENTATION_HANDLE hCombinedOrientationData,
     547             :     float *output_f[],
     548             :     const int16_t nchan_transport,
     549             :     const int16_t subframe )
     550             : {
     551             :     DIRAC_DEC_BIN_HANDLE hDiracDecBin;
     552             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
     553             :     PARAMBIN_REND_CONFIG config_data;
     554             :     int16_t slot, ch, numInChannels;
     555             :     float Cldfb_RealBuffer_in[6][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
     556             :     float Cldfb_ImagBuffer_in[6][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
     557             :     int16_t nchanSeparateChannels;
     558             :     float Rmat[3][3];
     559             :     int16_t max_band_decorr;
     560             :     DIFFUSE_DISTRIBUTION_DATA diffuseDistData;
     561             :     int16_t nBins, offsetSamples;
     562             :     int16_t i, j;
     563             :     int16_t pos_idx;
     564             :     MULTI_BIN_REND_POSE_DATA *pMultiBinPoseData;
     565             :     float tmp_Cldfb_out_re[BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
     566             :     float tmp_Cldfb_out_im[BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
     567             :     /* these allow re-using the reverb and freq-domain decorrelator signals from ivas_dirac_dec_binaural_process_output() in split rendering for the side renderings */
     568             :     float reverbRe[BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
     569             :     float reverbIm[BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
     570             :     float decorrRe[BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
     571             :     float decorrIm[BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
     572             :     float subFrameTotalEne[CLDFB_NO_CHANNELS_MAX];
     573             :     float IIReneLimiter[CLDFB_NO_CHANNELS_MAX];
     574             : 
     575      770949 :     hDiracDecBin = st_ivas->hDiracDecBin[0];
     576      770949 :     assert( hDiracDecBin );
     577      770949 :     hSpatParamRendCom = st_ivas->hSpatParamRendCom;
     578      770949 :     nBins = hSpatParamRendCom->num_freq_bands;
     579      770949 :     offsetSamples = hSpatParamRendCom->slots_rendered * nBins;
     580             : 
     581             :     /* Setup internal config */
     582      770949 :     config_data.separateCenterChannelRendering = st_ivas->hOutSetup.separateChannelEnabled;
     583      770949 :     config_data.ivas_format = st_ivas->ivas_format;
     584      770949 :     config_data.mc_mode = st_ivas->mc_mode;
     585      770949 :     config_data.ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     586      770949 :     config_data.nchan_transport = st_ivas->nchan_transport;
     587      770949 :     config_data.qualityBasedSmFactor = st_ivas->hMasa != NULL ? st_ivas->hMasa->data.dir_decode_quality : 1.0f;
     588      770949 :     config_data.processReverb = st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ? 1 : 0;
     589      770949 :     if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
     590             :     {
     591      190448 :         config_data.ism_mode = st_ivas->ism_mode;
     592             :     }
     593             :     else
     594             :     {
     595      580501 :         config_data.ism_mode = ISM_MODE_NONE;
     596             :     }
     597             : 
     598             :     /* The input channel number at this processing function (not nchan_transport) */
     599      770949 :     numInChannels = BINAURAL_CHANNELS;
     600      770949 :     if ( config_data.separateCenterChannelRendering || ( st_ivas->ivas_format == MASA_ISM_FORMAT && ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ ) ) )
     601             :     {
     602       84320 :         numInChannels++;
     603             :     }
     604      686629 :     else if ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC && ( st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) )
     605             :     {
     606       13264 :         numInChannels += (uint8_t) st_ivas->nchan_ism;
     607             :     }
     608             : 
     609      770949 :     Rmat[0][0] = 1.0f;
     610      770949 :     Rmat[0][1] = 0.0f;
     611      770949 :     Rmat[0][2] = 0.0f;
     612             : 
     613      770949 :     Rmat[1][0] = 0.0f;
     614      770949 :     Rmat[1][1] = 1.0f;
     615      770949 :     Rmat[1][2] = 0.0f;
     616             : 
     617      770949 :     Rmat[2][0] = 0.0f;
     618      770949 :     Rmat[2][1] = 0.0f;
     619      770949 :     Rmat[2][2] = 1.0f;
     620             : 
     621             :     /* CLDFB Analysis of input */
     622     3824921 :     for ( slot = 0; slot < hSpatParamRendCom->subframe_nbslots[subframe]; slot++ )
     623             :     {
     624     9631739 :         for ( ch = 0; ch < numInChannels; ch++ )
     625             :         {
     626     6577767 :             if ( ch == 0 || nchan_transport == 2 )
     627             :             {
     628     5508213 :                 cldfbAnalysis_ts(
     629     5508213 :                     &( st_ivas->hTcBuffer->tc[ch][nBins * slot + offsetSamples] ),
     630     5508213 :                     Cldfb_RealBuffer_in[ch][slot],
     631     5508213 :                     Cldfb_ImagBuffer_in[ch][slot],
     632             :                     nBins, st_ivas->cldfbAnaDec[ch] );
     633             :             }
     634     1069554 :             else if ( config_data.nchan_transport == 2 ) /* Stereo signal transmitted as mono with DFT stereo */
     635             :             {
     636             :                 /* At mono input duplicate the channel to dual-mono  */
     637      230318 :                 mvr2r( Cldfb_RealBuffer_in[0][slot], Cldfb_RealBuffer_in[1][slot], nBins );
     638      230318 :                 mvr2r( Cldfb_ImagBuffer_in[0][slot], Cldfb_ImagBuffer_in[1][slot], nBins );
     639             :             }
     640             :             else /* when nchan_transport == 1 and ch == 1 */
     641             :             {
     642             :                 /* CNA and HB FD-CNG*/
     643      839236 :                 if ( st_ivas->hSCE[0]->hCoreCoder[0] != NULL && st_ivas->hSCE[0]->hCoreCoder[0]->cng_sba_flag )
     644      623890 :                 {
     645             :                     int16_t numCoreBands, b;
     646             : 
     647      623890 :                     numCoreBands = st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom->numCoreBands;
     648             : 
     649     1247780 :                     generate_masking_noise_dirac( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom,
     650             :                                                   st_ivas->cldfbAnaDec[1],
     651      623890 :                                                   &st_ivas->hTcBuffer->tc[nchan_transport][hSpatParamRendCom->num_freq_bands * hSpatParamRendCom->slots_rendered],
     652      623890 :                                                   Cldfb_RealBuffer_in[2][slot], Cldfb_ImagBuffer_in[2][slot],
     653             :                                                   slot,
     654      623890 :                                                   st_ivas->hSCE[0]->hCoreCoder[0]->cna_dirac_flag && st_ivas->hSCE[0]->hCoreCoder[0]->flag_cna,
     655      623890 :                                                   ( st_ivas->hSCE[0]->hCoreCoder[0]->core_brate == FRAME_NO_DATA || st_ivas->hSCE[0]->hCoreCoder[0]->core_brate == SID_2k40 ) && ( st_ivas->hSCE[0]->hCoreCoder[0]->cng_type == FD_CNG ) && st_ivas->hSCE[0]->hCoreCoder[0]->cng_sba_flag );
     656             : 
     657     1247780 :                     generate_masking_noise_dirac( st_ivas->hSCE[0]->hCoreCoder[0]->hFdCngDec->hFdCngCom,
     658             :                                                   st_ivas->cldfbAnaDec[1], /*nothing will be analyzed, just get cnst*/
     659             :                                                   NULL,
     660      623890 :                                                   Cldfb_RealBuffer_in[1][slot], Cldfb_ImagBuffer_in[1][slot],
     661             :                                                   slot,
     662      623890 :                                                   st_ivas->hSCE[0]->hCoreCoder[0]->cna_dirac_flag && st_ivas->hSCE[0]->hCoreCoder[0]->flag_cna,
     663      623890 :                                                   ( st_ivas->hSCE[0]->hCoreCoder[0]->core_brate == FRAME_NO_DATA || st_ivas->hSCE[0]->hCoreCoder[0]->core_brate == SID_2k40 ) && ( st_ivas->hSCE[0]->hCoreCoder[0]->cng_type == FD_CNG ) && st_ivas->hSCE[0]->hCoreCoder[0]->cng_sba_flag );
     664             : 
     665             :                     /* LB: Copy first channel + LB-CNG to first and second channels with same scaling (dual-mono)*/
     666    12885306 :                     for ( b = 0; b < numCoreBands; b++ )
     667             :                     {
     668    12261416 :                         Cldfb_RealBuffer_in[0][slot][b] = INV_SQRT2 * ( Cldfb_RealBuffer_in[0][slot][b] + Cldfb_RealBuffer_in[2][slot][b] );
     669    12261416 :                         Cldfb_RealBuffer_in[1][slot][b] = Cldfb_RealBuffer_in[0][slot][b];
     670    12261416 :                         Cldfb_ImagBuffer_in[0][slot][b] = INV_SQRT2 * ( Cldfb_ImagBuffer_in[0][slot][b] + Cldfb_ImagBuffer_in[2][slot][b] );
     671    12261416 :                         Cldfb_ImagBuffer_in[1][slot][b] = Cldfb_ImagBuffer_in[0][slot][b];
     672             :                     }
     673             : 
     674             :                     /* HB: Copy first channel to second channel and add HB-CNGs with different scalings*/
     675    24796834 :                     for ( ; b < nBins; b++ )
     676             :                     {
     677    24172944 :                         Cldfb_RealBuffer_in[0][slot][b] *= INV_SQRT2;
     678    24172944 :                         Cldfb_RealBuffer_in[1][slot][b] = Cldfb_RealBuffer_in[0][slot][b] + 0.5f * Cldfb_RealBuffer_in[1][slot][b] + Cldfb_RealBuffer_in[0][slot][b];
     679    24172944 :                         Cldfb_RealBuffer_in[0][slot][b] += 0.5f * Cldfb_RealBuffer_in[2][slot][b];
     680             : 
     681    24172944 :                         Cldfb_ImagBuffer_in[0][slot][b] *= INV_SQRT2;
     682    24172944 :                         Cldfb_ImagBuffer_in[1][slot][b] = Cldfb_ImagBuffer_in[0][slot][b] + 0.5f * Cldfb_ImagBuffer_in[1][slot][b];
     683    24172944 :                         Cldfb_ImagBuffer_in[0][slot][b] += 0.5f * Cldfb_ImagBuffer_in[2][slot][b];
     684             :                     }
     685             :                 }
     686             :                 else
     687             :                 {
     688             :                     /* At mono input duplicate the channel to dual-mono, and apply gain
     689             :                     correction to ensure same overall level as in stereo mode  */
     690      215346 :                     v_multc( Cldfb_RealBuffer_in[0][slot], INV_SQRT_2, Cldfb_RealBuffer_in[0][slot], nBins );
     691      215346 :                     v_multc( Cldfb_ImagBuffer_in[0][slot], INV_SQRT_2, Cldfb_ImagBuffer_in[0][slot], nBins );
     692             : 
     693      215346 :                     mvr2r( Cldfb_RealBuffer_in[0][slot], Cldfb_RealBuffer_in[1][slot], nBins );
     694      215346 :                     mvr2r( Cldfb_ImagBuffer_in[0][slot], Cldfb_ImagBuffer_in[1][slot], nBins );
     695             :                 }
     696             :             }
     697             :         }
     698             : 
     699     3053972 :         if ( hDiracDecBin->useTdDecorr )
     700             :         {
     701     2403666 :             for ( ch = BINAURAL_CHANNELS; ch < ( 2 * BINAURAL_CHANNELS ); ch++ )
     702             :             {
     703     1602444 :                 cldfbAnalysis_ts(
     704     1602444 :                     &( st_ivas->hTcBuffer->tc[ch][nBins * slot + offsetSamples] ),
     705     1602444 :                     Cldfb_RealBuffer_in[ch][slot],
     706     1602444 :                     Cldfb_ImagBuffer_in[ch][slot],
     707             :                     nBins, st_ivas->cldfbAnaDec[ch] );
     708     1602444 :                 if ( config_data.nchan_transport == 1 &&
     709     1141808 :                      ( config_data.ivas_format == SBA_FORMAT || config_data.ivas_format == SBA_ISM_FORMAT ) )
     710             :                 {
     711      546500 :                     v_multc( Cldfb_RealBuffer_in[ch][slot], INV_SQRT_2, Cldfb_RealBuffer_in[ch][slot], nBins );
     712      546500 :                     v_multc( Cldfb_ImagBuffer_in[ch][slot], INV_SQRT_2, Cldfb_ImagBuffer_in[ch][slot], nBins );
     713             :                 }
     714             :             }
     715             :         }
     716             :     }
     717             : 
     718      770949 :     if ( config_data.ivas_format == SBA_FORMAT || config_data.ivas_format == SBA_ISM_FORMAT )
     719             :     {
     720      225366 :         hDiracDecBin->hDiffuseDist = &diffuseDistData;
     721             : 
     722      225366 :         ivas_spar_param_to_masa_param_mapping( st_ivas, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in, subframe );
     723      225366 :         ivas_sba_prototype_renderer( st_ivas, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in, subframe );
     724             :     }
     725             : 
     726      770949 :     if ( ( st_ivas->ivas_format == MASA_ISM_FORMAT && ( st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || ( st_ivas->ism_mode == ISM_MASA_MODE_DISC && ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC ) ) ) ) || st_ivas->ivas_format == ISM_FORMAT )
     727             :     {
     728       97179 :         ivas_omasa_preProcessStereoTransportsForEditedObjects( st_ivas, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in, nBins, subframe );
     729             :     }
     730             : 
     731      770949 :     if ( hCombinedOrientationData )
     732             :     {
     733     1850572 :         for ( i = 0; i < 3; i++ )
     734             :         {
     735     5551716 :             for ( j = 0; j < 3; j++ )
     736             :             {
     737     4163787 :                 Rmat[i][j] = hCombinedOrientationData->Rmat[hCombinedOrientationData->subframe_idx][i][j];
     738             :             }
     739             :         }
     740             : 
     741      462643 :         if ( nchan_transport == 2 )
     742             :         {
     743             :             /* in case of split rendering, determine the prototype rotation based on the main direction and use the same prototypes for the offset directions */
     744      327528 :             adaptTransportSignalsHeadtracked( hCombinedOrientationData, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in, nBins, hSpatParamRendCom->subframe_nbslots[subframe], Rmat );
     745             : 
     746      327528 :             ivas_dirac_dec_binaural_check_and_switch_transports_headtracked( hCombinedOrientationData, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in, nBins, hSpatParamRendCom->subframe_nbslots[subframe], Rmat );
     747             :         }
     748             :     }
     749             : 
     750      770949 :     if ( config_data.ivas_format == ISM_FORMAT )
     751             :     {
     752       45812 :         max_band_decorr = 0;
     753             :     }
     754      725137 :     else if ( hDiracDecBin->useTdDecorr )
     755             :     {
     756      203011 :         max_band_decorr = CLDFB_NO_CHANNELS_MAX;
     757             :     }
     758             :     else
     759             :     {
     760      522126 :         max_band_decorr = hDiracDecBin->h_freq_domain_decorr_ap_params->max_band_decorr;
     761             :     }
     762             : 
     763      770949 :     ivas_dirac_dec_binaural_formulate_input_covariance_matrices( hDiracDecBin, hSpatParamRendCom, &config_data, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in, subframe,
     764             :                                                                  subFrameTotalEne, IIReneLimiter );
     765             : 
     766      770949 :     ivas_dirac_dec_binaural_formulate_target_covariance_matrices( hDiracDecBin, hSpatParamRendCom, &config_data, Rmat, subframe,
     767      770949 :                                                                   hCombinedOrientationData && hCombinedOrientationData->enableCombinedOrientation[hCombinedOrientationData->subframe_idx] > 0, subFrameTotalEne, IIReneLimiter, st_ivas->hMasaIsmData );
     768             : 
     769      770949 :     nchanSeparateChannels = 0;
     770      770949 :     if ( config_data.separateCenterChannelRendering || ( st_ivas->ivas_format == MASA_ISM_FORMAT && ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ ) ) )
     771             :     {
     772       84320 :         nchanSeparateChannels = 1;
     773             :     }
     774      686629 :     else if ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC && ( st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC || st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) )
     775             :     {
     776       13264 :         nchanSeparateChannels = (uint8_t) st_ivas->nchan_ism;
     777             :     }
     778             : 
     779      770949 :     ivas_dirac_dec_binaural_determine_processing_matrices( hDiracDecBin, hSpatParamRendCom, &config_data, max_band_decorr, Rmat, subframe,
     780      770949 :                                                            hCombinedOrientationData && hCombinedOrientationData->enableCombinedOrientation[hCombinedOrientationData->subframe_idx] > 0, nchanSeparateChannels, st_ivas->hMasaIsmData );
     781             : 
     782      770949 :     pMultiBinPoseData = NULL;
     783      770949 :     if ( st_ivas->hSplitBinRend != NULL )
     784             :     {
     785      322746 :         pMultiBinPoseData = &st_ivas->hSplitBinRend->splitrend.multiBinPoseData;
     786             :     }
     787             : 
     788      770949 :     if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
     789             :     {
     790      322746 :         ivas_dirac_dec_binaural_process_output( hDiracDecBin, hSpatParamRendCom, st_ivas->cldfbSynDec, output_f, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in,
     791      322746 :                                                 max_band_decorr, numInChannels, config_data.processReverb, subframe, tmp_Cldfb_out_re, tmp_Cldfb_out_im,
     792             :                                                 reverbRe, reverbIm, decorrRe, decorrIm, 1 );
     793             : 
     794      968238 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
     795             :         {
     796     3191466 :             for ( i = 0; i < hSpatParamRendCom->subframe_nbslots[subframe]; i++ )
     797             :             {
     798     2545974 :                 ivas_CLDFB_RINGBUF_Push( st_ivas->hSplitBinRend->hMultiBinCldfbData[ch], tmp_Cldfb_out_re[ch][i], tmp_Cldfb_out_im[ch][i], CLDFB_NO_CHANNELS_MAX );
     799             :             }
     800             :         }
     801             :     }
     802             :     else
     803             :     {
     804      448203 :         ivas_dirac_dec_binaural_process_output( hDiracDecBin, hSpatParamRendCom, st_ivas->cldfbSynDec, output_f, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in,
     805      448203 :                                                 max_band_decorr, numInChannels, config_data.processReverb, subframe, NULL, NULL,
     806             :                                                 reverbRe, reverbIm, decorrRe, decorrIm, 1 );
     807             :     }
     808             : 
     809      770949 :     hDiracDecBin->hDiffuseDist = NULL;
     810             : 
     811      770949 :     if ( pMultiBinPoseData != NULL && pMultiBinPoseData->num_poses > 1 )
     812             :     {
     813             :         /* quaternion-based rotation from ivas_binRenderer_internal.c:ivas_binRenderer(), but using absolute rotation instead of delta rotations */
     814             :         IVAS_QUATERNION Quaternions_rot, Quaternions_abs, *Quaternions_ref;
     815             :         float Rmat_local[3][3];
     816             : 
     817      228536 :         if ( hCombinedOrientationData )
     818             :         {
     819      228536 :             Quaternions_ref = &hCombinedOrientationData->Quaternions[0];
     820      228536 :             Quaternions_rot.w = -3.0f;                                                                        /* signal to use Euler */
     821      228536 :             Quaternions_abs.w = -3.0f;                                                                        /* signal to use Euler */
     822      228536 :             Quat2EulerDegree( *Quaternions_ref, &Quaternions_abs.z, &Quaternions_abs.y, &Quaternions_abs.x ); /*order in Quat2Euler seems to be reversed ?*/
     823             : 
     824     1218048 :             for ( pos_idx = 1; pos_idx < pMultiBinPoseData->num_poses; pos_idx++ )
     825             :             {
     826      989512 :                 Quaternions_rot.x = Quaternions_abs.x + pMultiBinPoseData->relative_head_poses[pos_idx][0];
     827      989512 :                 Quaternions_rot.y = Quaternions_abs.y + pMultiBinPoseData->relative_head_poses[pos_idx][1];
     828      989512 :                 Quaternions_rot.z = Quaternions_abs.z + pMultiBinPoseData->relative_head_poses[pos_idx][2];
     829             : 
     830      989512 :                 QuatToRotMat( Quaternions_rot, Rmat_local );
     831             : 
     832      989512 :                 hDiracDecBin = st_ivas->hDiracDecBin[pos_idx];
     833      989512 :                 assert( hDiracDecBin != NULL && "No DiracDecBin handle for this position" );
     834      989512 :                 if ( config_data.ivas_format == SBA_FORMAT || config_data.ivas_format == SBA_ISM_FORMAT )
     835             :                 {
     836      209768 :                     hDiracDecBin->hDiffuseDist = &diffuseDistData;
     837             :                 }
     838             : 
     839             :                 /* re-use input covariance for the side renderings */
     840     2968536 :                 for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
     841             :                 {
     842     1979024 :                     mvr2r( st_ivas->hDiracDecBin[0]->ChEne[ch], hDiracDecBin->ChEne[ch], hSpatParamRendCom->num_freq_bands );
     843             :                 }
     844      989512 :                 mvr2r( st_ivas->hDiracDecBin[0]->ChCrossRe, hDiracDecBin->ChCrossRe, hSpatParamRendCom->num_freq_bands );
     845      989512 :                 mvr2r( st_ivas->hDiracDecBin[0]->ChCrossIm, hDiracDecBin->ChCrossIm, hSpatParamRendCom->num_freq_bands );
     846             : 
     847      989512 :                 ivas_dirac_dec_binaural_formulate_target_covariance_matrices( hDiracDecBin, hSpatParamRendCom, &config_data, Rmat_local, subframe, hCombinedOrientationData && hCombinedOrientationData->enableCombinedOrientation[hCombinedOrientationData->subframe_idx] > 0, subFrameTotalEne, IIReneLimiter, st_ivas->hMasaIsmData );
     848             : 
     849      989512 :                 ivas_dirac_dec_binaural_determine_processing_matrices( hDiracDecBin, hSpatParamRendCom, &config_data, max_band_decorr, Rmat_local, subframe, hCombinedOrientationData && hCombinedOrientationData->enableCombinedOrientation[hCombinedOrientationData->subframe_idx] > 0, nchanSeparateChannels, st_ivas->hMasaIsmData );
     850             : 
     851             :                 /* re-use reverb and decorr from main direction for the sides */
     852      989512 :                 ivas_dirac_dec_binaural_process_output( hDiracDecBin, hSpatParamRendCom, st_ivas->cldfbSynDec, output_f, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in,
     853      989512 :                                                         max_band_decorr, numInChannels, config_data.processReverb, subframe, tmp_Cldfb_out_re, tmp_Cldfb_out_im,
     854             :                                                         reverbRe, reverbIm, decorrRe, decorrIm, 0 );
     855             : 
     856             :                 /* copy from temporary buffer to the main split rendering buffer */
     857     2968536 :                 for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
     858             :                 {
     859     9784968 :                     for ( i = 0; i < hSpatParamRendCom->subframe_nbslots[subframe]; i++ )
     860             :                     {
     861     7805944 :                         ivas_CLDFB_RINGBUF_Push( st_ivas->hSplitBinRend->hMultiBinCldfbData[pos_idx * BINAURAL_CHANNELS + ch], tmp_Cldfb_out_re[ch][i], tmp_Cldfb_out_im[ch][i], CLDFB_NO_CHANNELS_MAX );
     862             :                     }
     863             :                 }
     864             : 
     865      989512 :                 hDiracDecBin->hDiffuseDist = NULL;
     866             :             }
     867             :         }
     868             :     }
     869             : 
     870             :     /* update this counter only after the last rendering of split directions */
     871      770949 :     hSpatParamRendCom->slots_rendered += hSpatParamRendCom->subframe_nbslots[subframe];
     872      770949 :     hSpatParamRendCom->subframes_rendered++;
     873             : 
     874      770949 :     return;
     875             : }
     876             : 
     877             : 
     878     2562089 : static void ivas_dirac_dec_decorrelate_slot(
     879             :     DIRAC_DEC_BIN_HANDLE hDiracDecBin,
     880             :     const int16_t num_freq_bands,
     881             :     const int16_t slot,
     882             :     float inRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
     883             :     float inIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
     884             :     float decRe[][CLDFB_NO_CHANNELS_MAX],
     885             :     float decIm[][CLDFB_NO_CHANNELS_MAX] )
     886             : {
     887             :     int16_t offset, ch, bin;
     888             :     float onset_filter[BINAURAL_CHANNELS * CLDFB_NO_CHANNELS_MAX];                     /* 2 ch, 60 bins */
     889             :     float decorrelatedFrameInterleaved[2 * BINAURAL_CHANNELS * CLDFB_NO_CHANNELS_MAX]; /* 2 ch, real + imag, 60 bins */
     890             :     float protoFrameF[2 * BINAURAL_CHANNELS * CLDFB_NO_CHANNELS_MAX];                  /* 2 ch, real + imag, 60 bins */
     891     2562089 :     const int16_t protoIndexDir[BINAURAL_CHANNELS] = { 0, 1 };
     892             : 
     893             :     /* Decorrelation needs interleaved data. Copy left and right signals to proto_frame_f */
     894     7686267 :     for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
     895             :     {
     896     5124178 :         offset = num_freq_bands * BINAURAL_CHANNELS * ch;
     897   279812058 :         for ( bin = 0; bin < num_freq_bands; bin++ )
     898             :         {
     899   274687880 :             protoFrameF[( bin * BINAURAL_CHANNELS ) + offset] = inRe[ch][slot][bin];
     900   274687880 :             protoFrameF[( bin * BINAURAL_CHANNELS ) + offset + 1] = inIm[ch][slot][bin];
     901             :         }
     902             :     }
     903             : 
     904             :     /* Decorrelate proto signal to decorrelatedFrameInterleaved */
     905     2562089 :     ivas_dirac_dec_decorr_process( num_freq_bands,
     906             :                                    BINAURAL_CHANNELS,
     907             :                                    BINAURAL_CHANNELS,
     908             :                                    DIRAC_SYNTHESIS_PSD_LS,
     909             :                                    BINAURAL_CHANNELS,
     910             :                                    protoFrameF,
     911             :                                    BINAURAL_CHANNELS,
     912             :                                    protoIndexDir,
     913             :                                    decorrelatedFrameInterleaved,
     914             :                                    onset_filter,
     915             :                                    hDiracDecBin->h_freq_domain_decorr_ap_params,
     916             :                                    hDiracDecBin->h_freq_domain_decorr_ap_state );
     917             : 
     918             :     /* De-interleave decorrelated signals*/
     919     7686267 :     for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
     920             :     {
     921     5124178 :         offset = num_freq_bands * BINAURAL_CHANNELS * ch;
     922   279812058 :         for ( bin = 0; bin < num_freq_bands; bin++ )
     923             :         {
     924   274687880 :             decRe[ch][bin] = decorrelatedFrameInterleaved[( bin * BINAURAL_CHANNELS ) + offset];
     925   274687880 :             decIm[ch][bin] = decorrelatedFrameInterleaved[( bin * BINAURAL_CHANNELS ) + offset + 1];
     926             :         }
     927             :     }
     928             : 
     929     2562089 :     return;
     930             : }
     931             : 
     932             : 
     933      893302 : static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices(
     934             :     DIRAC_DEC_BIN_HANDLE hDiracDecBin,
     935             :     const SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom,
     936             :     const PARAMBIN_REND_CONFIG_HANDLE hConfig,
     937             :     float inRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
     938             :     float inIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
     939             :     const int16_t subframe,
     940             :     float *subFrameTotalEne,
     941             :     float *IIReneLimiter )
     942             : {
     943             :     int16_t ch, slot, bin;
     944             :     int16_t nBins;
     945             :     float IIReneLimiterFactor;
     946             :     float qualityBasedSmFactor;
     947             :     float lowBitRateEQ[CLDFB_NO_CHANNELS_MAX];
     948             :     uint8_t applyLowBitRateEQ;
     949             :     IVAS_FORMAT ivas_format;
     950             :     int32_t ivas_total_brate;
     951             :     int16_t nchan_transport;
     952             : 
     953      893302 :     ivas_format = hConfig->ivas_format;
     954      893302 :     ivas_total_brate = hConfig->ivas_total_brate;
     955      893302 :     nchan_transport = hConfig->nchan_transport;
     956      893302 :     qualityBasedSmFactor = hConfig->qualityBasedSmFactor;
     957      893302 :     qualityBasedSmFactor *= qualityBasedSmFactor;
     958      893302 :     nBins = hSpatParamRendCom->num_freq_bands; /* Actually bins */
     959             : 
     960      893302 :     set_zero( hDiracDecBin->ChCrossRe, nBins );
     961      893302 :     set_zero( hDiracDecBin->ChCrossIm, nBins );
     962     2679906 :     for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
     963             :     {
     964     1786604 :         set_zero( hDiracDecBin->ChEne[ch], nBins );
     965             :     }
     966             : 
     967             :     /* Determine EQ for low bit rates (13.2 and 16.4 kbps) */
     968      893302 :     applyLowBitRateEQ = 0;
     969      893302 :     if ( ( ivas_format == MASA_FORMAT || ivas_format == MC_FORMAT ) && ivas_total_brate < MASA_STEREO_MIN_BITRATE )
     970             :     {
     971       69907 :         applyLowBitRateEQ = 1;
     972       69907 :         if ( ivas_total_brate == IVAS_16k4 )
     973             :         {
     974      941598 :             for ( bin = 0; bin < LOW_BIT_RATE_BINAURAL_EQ_BINS; bin++ )
     975             :             {
     976      889287 :                 lowBitRateEQ[bin + LOW_BIT_RATE_BINAURAL_EQ_OFFSET] = lowBitRateBinauralEQ[bin] * 0.5f + 0.5f;
     977             :             }
     978             :         }
     979             :         else
     980             :         {
     981      316728 :             for ( bin = 0; bin < LOW_BIT_RATE_BINAURAL_EQ_BINS; bin++ )
     982             :             {
     983      299132 :                 lowBitRateEQ[bin + LOW_BIT_RATE_BINAURAL_EQ_OFFSET] = lowBitRateBinauralEQ[bin];
     984             :             }
     985             :         }
     986             :     }
     987             : 
     988             :     /* Formulate input and target covariance matrices for this subframe */
     989      893302 :     set_zero( subFrameTotalEne, CLDFB_NO_CHANNELS_MAX );
     990             : 
     991             :     /* Calculate input covariance matrix */
     992     4436686 :     for ( slot = 0; slot < hSpatParamRendCom->subframe_nbslots[subframe]; slot++ )
     993             :     {
     994   194501904 :         for ( bin = 0; bin < nBins; bin++ )
     995             :         {
     996   572875560 :             for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
     997             :             {
     998             :                 float instEne;
     999             : 
    1000   381917040 :                 instEne = ( inRe[ch][slot][bin] * inRe[ch][slot][bin] );
    1001   381917040 :                 instEne += ( inIm[ch][slot][bin] * inIm[ch][slot][bin] );
    1002   381917040 :                 hDiracDecBin->ChEne[ch][bin] += instEne;
    1003   381917040 :                 subFrameTotalEne[bin] += instEne;
    1004             :             }
    1005   190958520 :             hDiracDecBin->ChCrossRe[bin] += inRe[0][slot][bin] * inRe[1][slot][bin];
    1006   190958520 :             hDiracDecBin->ChCrossRe[bin] += inIm[0][slot][bin] * inIm[1][slot][bin];
    1007   190958520 :             hDiracDecBin->ChCrossIm[bin] += inRe[0][slot][bin] * inIm[1][slot][bin];
    1008   190958520 :             hDiracDecBin->ChCrossIm[bin] -= inIm[0][slot][bin] * inRe[1][slot][bin];
    1009             :         }
    1010             :     }
    1011             : 
    1012             :     /* Apply EQ at low bit rates */
    1013      893302 :     if ( applyLowBitRateEQ )
    1014             :     {
    1015       69907 :         int16_t lastEqBin = LOW_BIT_RATE_BINAURAL_EQ_OFFSET + LOW_BIT_RATE_BINAURAL_EQ_BINS - 1;
    1016             : 
    1017     1188419 :         for ( bin = LOW_BIT_RATE_BINAURAL_EQ_OFFSET; bin < lastEqBin; bin++ )
    1018             :         {
    1019     1118512 :             subFrameTotalEne[bin] *= lowBitRateEQ[bin];
    1020             :         }
    1021     2042377 :         for ( ; bin < nBins; bin++ )
    1022             :         {
    1023     1972470 :             subFrameTotalEne[bin] *= lowBitRateEQ[lastEqBin];
    1024             :         }
    1025             :     }
    1026             : 
    1027      893302 :     if ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && nchan_transport == 2 )
    1028             :     {
    1029             :         float tempRe, tempIm;
    1030             :         float subFrameSumEne[CLDFB_NO_CHANNELS_MAX];
    1031             : 
    1032      156860 :         v_multc( subFrameTotalEne, SBA_CARDI_TARGET_ENERGY_GAIN, subFrameTotalEne, nBins );
    1033             : 
    1034      156860 :         set_zero( subFrameSumEne, CLDFB_NO_CHANNELS_MAX );
    1035      782625 :         for ( slot = 0; slot < hSpatParamRendCom->subframe_nbslots[subframe]; slot++ )
    1036             :         {
    1037    32281105 :             for ( bin = 0; bin < nBins; bin++ )
    1038             :             {
    1039    31655340 :                 tempRe = inRe[0][slot][bin] + inRe[1][slot][bin];
    1040    31655340 :                 tempIm = inIm[0][slot][bin] + inIm[1][slot][bin];
    1041    31655340 :                 subFrameSumEne[bin] += tempRe * tempRe + tempIm * tempIm;
    1042             :             }
    1043             :         }
    1044             : 
    1045     8095820 :         for ( bin = 0; bin < nBins; bin++ )
    1046             :         {
    1047     7938960 :             subFrameTotalEne[bin] = max( subFrameTotalEne[bin], subFrameSumEne[bin] );
    1048             :         }
    1049             :     }
    1050             : 
    1051             :     /* Temporal IIR-type smoothing of covariance matrices. Also apply encoding quality based smoothing factor. */
    1052      893302 :     if ( ivas_format == MASA_FORMAT && ivas_total_brate < MASA_STEREO_MIN_BITRATE )
    1053             :     {
    1054       68007 :         IIReneLimiterFactor = 16.0f + ( 1.0f - qualityBasedSmFactor );
    1055             :     }
    1056             :     else
    1057             :     {
    1058      825295 :         IIReneLimiterFactor = 8.0f + ( 1.0f - qualityBasedSmFactor );
    1059             :     }
    1060    49058982 :     for ( bin = 0; bin < nBins; bin++ )
    1061             :     {
    1062             :         float eneRatio;
    1063             : 
    1064             :         /* Temporally smooth cov mtx estimates for resulting mixing matrix stability. The design principle is that
    1065             :          * the energy history (IIR) must not be more than double of the current frame energy. This provides more
    1066             :          * robust performance at energy offsets when compared to typical IIR averaging. */
    1067    48165680 :         eneRatio = ( hDiracDecBin->ChEne[0][bin] + hDiracDecBin->ChEne[1][bin] ) / fmaxf( 1e-12f, ( hDiracDecBin->ChEnePrev[0][bin] + hDiracDecBin->ChEnePrev[1][bin] ) );
    1068    48165680 :         IIReneLimiter[bin] = fminf( 1.0f, eneRatio * IIReneLimiterFactor );
    1069             : 
    1070    48165680 :         hDiracDecBin->ChCrossRe[bin] *= qualityBasedSmFactor;
    1071    48165680 :         hDiracDecBin->ChCrossIm[bin] *= qualityBasedSmFactor;
    1072   144497040 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    1073             :         {
    1074    96331360 :             hDiracDecBin->ChEne[ch][bin] *= qualityBasedSmFactor;
    1075             :         }
    1076             : 
    1077    48165680 :         hDiracDecBin->ChCrossRe[bin] += IIReneLimiter[bin] * hDiracDecBin->ChCrossRePrev[bin];
    1078    48165680 :         hDiracDecBin->ChCrossIm[bin] += IIReneLimiter[bin] * hDiracDecBin->ChCrossImPrev[bin];
    1079             : 
    1080   144497040 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    1081             :         {
    1082    96331360 :             hDiracDecBin->ChEne[ch][bin] += IIReneLimiter[bin] * hDiracDecBin->ChEnePrev[ch][bin];
    1083             :         }
    1084             : 
    1085             :         /* Store energy values and coefficients for next round */
    1086    48165680 :         hDiracDecBin->ChCrossRePrev[bin] = hDiracDecBin->ChCrossRe[bin];
    1087    48165680 :         hDiracDecBin->ChCrossImPrev[bin] = hDiracDecBin->ChCrossIm[bin];
    1088   144497040 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    1089             :         {
    1090    96331360 :             hDiracDecBin->ChEnePrev[ch][bin] = hDiracDecBin->ChEne[ch][bin];
    1091             :         }
    1092             :     }
    1093             : 
    1094      893302 :     return;
    1095             : }
    1096             : 
    1097             : 
    1098     1882814 : static void ivas_dirac_dec_binaural_formulate_target_covariance_matrices(
    1099             :     DIRAC_DEC_BIN_HANDLE hDiracDecBin,
    1100             :     const SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom,
    1101             :     const PARAMBIN_REND_CONFIG_HANDLE hConfig,
    1102             :     float Rmat[3][3],
    1103             :     const int16_t subframe,
    1104             :     const int16_t isHeadtracked,
    1105             :     const float *subFrameTotalEne,
    1106             :     const float *IIReneLimiter,
    1107             :     const MASA_ISM_DATA_HANDLE hMasaIsmData )
    1108             : {
    1109             :     int16_t ch, bin;
    1110             :     int16_t separateCenterChannelRendering;
    1111             :     int16_t nBins, idx;
    1112             :     float frameMeanDiffusenessEneWeight[CLDFB_NO_CHANNELS_MAX];
    1113             :     float qualityBasedSmFactor;
    1114             :     int16_t dirac_read_idx;
    1115             :     PARAMBIN_HRTF_GAIN_CACHE gainCache[MAX_GAIN_CACHE_SIZE];
    1116             :     IVAS_FORMAT ivas_format;
    1117             :     MC_MODE mc_mode;
    1118             :     int16_t gainCacheBaseIndex;
    1119             : 
    1120     1882814 :     separateCenterChannelRendering = hConfig->separateCenterChannelRendering;
    1121     1882814 :     ivas_format = hConfig->ivas_format;
    1122     1882814 :     mc_mode = hConfig->mc_mode;
    1123     1882814 :     qualityBasedSmFactor = hConfig->qualityBasedSmFactor;
    1124     1882814 :     qualityBasedSmFactor *= qualityBasedSmFactor;
    1125     1882814 :     nBins = hSpatParamRendCom->num_freq_bands; /* Actually bins */
    1126             : 
    1127     1882814 :     set_zero( hDiracDecBin->ChCrossReOut, nBins );
    1128     1882814 :     set_zero( hDiracDecBin->ChCrossImOut, nBins );
    1129     5648442 :     for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    1130             :     {
    1131     3765628 :         set_zero( hDiracDecBin->ChEneOut[ch], nBins );
    1132             :     }
    1133     1882814 :     set_zero( hDiracDecBin->frameMeanDiffuseness, nBins );
    1134             : 
    1135     1882814 :     set_zero( frameMeanDiffusenessEneWeight, CLDFB_NO_CHANNELS_MAX );
    1136             : 
    1137    20710954 :     for ( idx = 0; idx < MAX_GAIN_CACHE_SIZE; idx++ )
    1138             :     {
    1139    18828140 :         gainCache[idx].azi = -1000; /* Use -1000 as value for uninitialized cache. */
    1140             :     }
    1141             : 
    1142     1882814 :     dirac_read_idx = hSpatParamRendCom->render_to_md_map[subframe];
    1143             : 
    1144             :     /* Determine target covariance matrix containing target binaural properties */
    1145   109419214 :     for ( bin = 0; bin < nBins; bin++ )
    1146             :     {
    1147   107536400 :         float diffuseness = 1.0f; /* ratio1 and ratio2 are subtracted from diffuseness further below */
    1148   107536400 :         float diffusenessValForDecorrelationReduction = 1.0f;
    1149             :         float diffEneValForDecorrelationReduction;
    1150   107536400 :         float surCoh = 0.0f, spreadCoh = 0.0f; /* Default values if spreadSurroundCoherenceApplied == false */
    1151             :         float diffEne, dirEne, meanEnePerCh;
    1152             :         int16_t dirIndex;
    1153             : 
    1154             :         /* When BINAURAL_ROOM is not indicated, hBinaural->earlyPartEneCorrection[bin] values are all 1.0f.
    1155             :          * When BINAURAL_ROOM is indicated, the binaural audio output is based on combined use of the
    1156             :          * HRTF data set and a BRIR-based data set. The HRTF data set is spectrally corrected to match
    1157             :          * the early spectrum of the BRIR data, using the spectral correction data in
    1158             :          * hBinaural->earlyPartEneCorrection[bin], based on the BRIR set. */
    1159   107536400 :         meanEnePerCh = hDiracDecBin->earlyPartEneCorrection[bin] * subFrameTotalEne[bin] / 2.0f;
    1160             : 
    1161             :         /* Determine direct part target covariance matrix (for 1 or 2 directions) */
    1162   262305680 :         for ( dirIndex = 0; dirIndex < hSpatParamRendCom->numSimultaneousDirections; dirIndex++ )
    1163             :         {
    1164             :             int16_t aziDeg, eleDeg;
    1165             :             float lRealp, lImagp, rRealp, rImagp;
    1166             :             float lRealpTmp, lImagpTmp, rRealpTmp, rImagpTmp;
    1167             :             float hrtfEne[BINAURAL_CHANNELS], hrtfCrossRe, hrtfCrossIm, ratio;
    1168   154769280 :             uint8_t isIsmDirection = 0;
    1169             : 
    1170   154769280 :             if ( dirIndex == 0 ) /* For first of the two simultaneous directions */
    1171             :             {
    1172   107536400 :                 aziDeg = hSpatParamRendCom->azimuth[dirac_read_idx][bin];
    1173   107536400 :                 eleDeg = hSpatParamRendCom->elevation[dirac_read_idx][bin];
    1174   107536400 :                 ratio = hSpatParamRendCom->energy_ratio1[dirac_read_idx][bin];
    1175   107536400 :                 spreadCoh = hSpatParamRendCom->spreadCoherence[dirac_read_idx][bin];
    1176   107536400 :                 gainCacheBaseIndex = 0;
    1177             :             }
    1178    47232880 :             else if ( ivas_format != MASA_ISM_FORMAT || ( ivas_format == MASA_ISM_FORMAT && dirIndex < hSpatParamRendCom->numParametricDirections ) ) /* For second of the two simultaneous directions */
    1179             :             {
    1180    24033720 :                 if ( ( ratio = hSpatParamRendCom->energy_ratio2[dirac_read_idx][bin] ) < 0.001 )
    1181             :                 {
    1182             :                     /* This touches only MASA path where second direction always has smaller ratio and
    1183             :                      * for non-2dir it is zero. As the whole direction contribution is multiplied with
    1184             :                      * the ratio, a very small ratio does not contribute any energy to output. Thus,
    1185             :                      * it is better to save complexity. */
    1186    14905168 :                     continue;
    1187             :                 }
    1188     9128552 :                 aziDeg = hSpatParamRendCom->azimuth2[dirac_read_idx][bin];
    1189     9128552 :                 eleDeg = hSpatParamRendCom->elevation2[dirac_read_idx][bin];
    1190     9128552 :                 spreadCoh = hSpatParamRendCom->spreadCoherence2[dirac_read_idx][bin];
    1191     9128552 :                 gainCacheBaseIndex = 3;
    1192             :             }
    1193             :             else /* For object directions of MASA_ISM_FORMAT */
    1194             :             {
    1195    23199160 :                 isIsmDirection = 1;
    1196             :                 uint16_t ismDirIndex;
    1197    23199160 :                 ismDirIndex = dirIndex - hSpatParamRendCom->numParametricDirections;
    1198    23199160 :                 assert( hMasaIsmData != NULL && "hMasaIsmData should not be NULL if we use it" );
    1199             : 
    1200    23199160 :                 if ( hMasaIsmData->ism_dir_is_edited[ismDirIndex] )
    1201             :                 {
    1202     1069440 :                     aziDeg = hMasaIsmData->azimuth_ism_edited[ismDirIndex];
    1203     1069440 :                     eleDeg = hMasaIsmData->elevation_ism_edited[ismDirIndex];
    1204             :                 }
    1205             :                 else
    1206             :                 {
    1207    22129720 :                     aziDeg = hMasaIsmData->azimuth_ism[ismDirIndex][dirac_read_idx];
    1208    22129720 :                     eleDeg = hMasaIsmData->elevation_ism[ismDirIndex][dirac_read_idx];
    1209             :                 }
    1210    23199160 :                 ratio = hMasaIsmData->energy_ratio_ism[ismDirIndex][dirac_read_idx][bin];
    1211    23199160 :                 spreadCoh = 0.0f;
    1212    23199160 :                 gainCacheBaseIndex = 6 + ismDirIndex;
    1213             :             }
    1214             : 
    1215   139864112 :             diffuseness -= ratio; /* diffuseness = 1 - ratio1 - ratio2 */
    1216             : 
    1217   139864112 :             if ( diffuseness < 0.0f )
    1218             :             {
    1219      255636 :                 diffuseness = 0.0f;
    1220             :             }
    1221   139864112 :             if ( isIsmDirection )
    1222             :             {
    1223             :                 /* Objects cause lesser decorrelation reduction, to avoid removing all decorrelation when only objects are present */
    1224    23199160 :                 diffusenessValForDecorrelationReduction -= ratio * 0.5f;
    1225             :             }
    1226             :             else
    1227             :             {
    1228   116664952 :                 diffusenessValForDecorrelationReduction -= ratio;
    1229             :             }
    1230             : 
    1231   139864112 :             if ( separateCenterChannelRendering )
    1232             :             {
    1233             :                 /* In masa + mono rendering mode, the center directions originate from phantom sources, so the
    1234             :                  * spread coherence is increased */
    1235             :                 float aziRad, eleRad, doaVectorX, spatialAngleDeg, altSpreadCoh;
    1236             : 
    1237       15000 :                 aziRad = (float) aziDeg * PI_OVER_180;
    1238       15000 :                 eleRad = (float) eleDeg * PI_OVER_180;
    1239       15000 :                 doaVectorX = cosf( aziRad ) * cosf( eleRad );
    1240       15000 :                 spatialAngleDeg = acosf( doaVectorX ) * _180_OVER_PI;
    1241       15000 :                 altSpreadCoh = 1.0f - ( spatialAngleDeg / 30.0f );
    1242       15000 :                 spreadCoh = max( spreadCoh, altSpreadCoh );
    1243             :             }
    1244             : 
    1245   139864112 :             getDirectPartGains( bin, aziDeg, eleDeg, &lRealp, &lImagp, &rRealp, &rImagp, hDiracDecBin->renderStereoOutputInsteadOfBinaural, Rmat, &gainCache[gainCacheBaseIndex], isHeadtracked, *hDiracDecBin->phHrtfParambin );
    1246             : 
    1247   139864112 :             if ( hDiracDecBin->renderStereoOutputInsteadOfBinaural )
    1248             :             {
    1249             :                 /* Synthesizing spread coherence is not needed for stereo loudspeaker output,
    1250             :                  * as directional sound is reproduced with two loudspeakers in any case */
    1251     3896439 :                 spreadCoh = 0.0f;
    1252             :             }
    1253             : 
    1254   139864112 :             if ( spreadCoh > 0.0f )
    1255             :             {
    1256             :                 float centerMul, sidesMul;
    1257             :                 float hrtfEneCenter, hrtfEneSides, hrtfEneRealized, eneCorrectionFactor;
    1258             :                 float w1, w2, w3, eq;
    1259             : 
    1260    34319801 :                 hrtfEneCenter = ( lRealp * lRealp ) + ( lImagp * lImagp ) + ( rRealp * rRealp ) + ( rImagp * rImagp );
    1261             : 
    1262             :                 /* Spread coherence is synthesized as coherent sources at 30 degree horizontal spacing.
    1263             :                  * The following formulas determine the gains for these sources.
    1264             :                  * spreadCoh = 0: Only panning
    1265             :                  * spreadCoh = 0.5: Three sources coherent panning (e.g. 30 0 -30 deg azi)
    1266             :                  * spreadCoh = 1.0: Two sources coherent panning with gap (as above, but center is silent) */
    1267    34319801 :                 if ( spreadCoh < 0.5f )
    1268             :                 {
    1269             :                     /* 0.0f < spreadCoh < 0.5f */
    1270    31747244 :                     sidesMul = 0.5774f * spreadCoh * 2.0f; /* sqrt(1/3) = 0.5774f */
    1271    31747244 :                     centerMul = 1.0f - ( spreadCoh * 2.0f ) + sidesMul;
    1272             :                 }
    1273             :                 else
    1274             :                 {
    1275             :                     /* 0.5f <= spreadCoh < 1.0f */
    1276     2572557 :                     centerMul = 2.0f - ( 2.0f * spreadCoh );
    1277     2572557 :                     sidesMul = inv_sqrt( centerMul + 2.0f );
    1278     2572557 :                     centerMul *= sidesMul;
    1279             :                 }
    1280             : 
    1281             :                 /* Apply the gain for the center source of the three coherent sources */
    1282    34319801 :                 lRealp *= centerMul;
    1283    34319801 :                 lImagp *= centerMul;
    1284    34319801 :                 rRealp *= centerMul;
    1285    34319801 :                 rImagp *= centerMul;
    1286             : 
    1287             :                 /* Apply the gain for the left source of the three coherent sources */
    1288    34319801 :                 getDirectPartGains( bin, aziDeg + 30, eleDeg, &lRealpTmp, &lImagpTmp, &rRealpTmp, &rImagpTmp, hDiracDecBin->renderStereoOutputInsteadOfBinaural, Rmat, &gainCache[gainCacheBaseIndex + 1], isHeadtracked, *hDiracDecBin->phHrtfParambin );
    1289             : 
    1290    34319801 :                 hrtfEneSides = ( lRealpTmp * lRealpTmp ) + ( lImagpTmp * lImagpTmp ) + ( rRealpTmp * rRealpTmp ) + ( rImagpTmp * rImagpTmp );
    1291    34319801 :                 lRealp += sidesMul * lRealpTmp;
    1292    34319801 :                 lImagp += sidesMul * lImagpTmp;
    1293    34319801 :                 rRealp += sidesMul * rRealpTmp;
    1294    34319801 :                 rImagp += sidesMul * rImagpTmp;
    1295             : 
    1296             :                 /* Apply the gain for the right source of the three coherent sources.
    1297             :                  * -30 degrees to 330 wrapping due to internal functions. */
    1298    34319801 :                 getDirectPartGains( bin, aziDeg + 330, eleDeg, &lRealpTmp, &lImagpTmp, &rRealpTmp, &rImagpTmp, hDiracDecBin->renderStereoOutputInsteadOfBinaural, Rmat, &gainCache[gainCacheBaseIndex + 2], isHeadtracked, *hDiracDecBin->phHrtfParambin );
    1299    34319801 :                 hrtfEneSides += ( lRealpTmp * lRealpTmp ) + ( lImagpTmp * lImagpTmp ) + ( rRealpTmp * rRealpTmp ) + ( rImagpTmp * rImagpTmp );
    1300    34319801 :                 lRealp += sidesMul * lRealpTmp;
    1301    34319801 :                 lImagp += sidesMul * lImagpTmp;
    1302    34319801 :                 rRealp += sidesMul * rRealpTmp;
    1303    34319801 :                 rImagp += sidesMul * rImagpTmp;
    1304             : 
    1305             :                 /* Formulate an eneCorrectionFactor that compensates for the coherent summation of the HRTFs */
    1306    34319801 :                 hrtfEneRealized = ( lRealp * lRealp ) + ( lImagp * lImagp ) + ( rRealp * rRealp ) + ( rImagp * rImagp );
    1307    68639602 :                 eneCorrectionFactor = ( ( hrtfEneSides * sidesMul * sidesMul ) +
    1308    34319801 :                                         ( hrtfEneCenter * centerMul * centerMul ) ) /
    1309    34319801 :                                       max( 1e-12f, hrtfEneRealized );
    1310             : 
    1311             :                 /* Weighting factors to determine appropriate target spectrum for spread coherent sound */
    1312    34319801 :                 if ( spreadCoh < 0.5 )
    1313             :                 {
    1314    31747244 :                     w1 = 1.0f - 2.0f * spreadCoh;
    1315    31747244 :                     w2 = 2.0f * spreadCoh;
    1316    31747244 :                     w3 = 0.0f;
    1317             :                 }
    1318             :                 else
    1319             :                 {
    1320     2572557 :                     w1 = 0.0f;
    1321     2572557 :                     w2 = 2.0f - 2.0f * spreadCoh;
    1322     2572557 :                     w3 = 2.0f * spreadCoh - 1.0f;
    1323             :                 }
    1324             : 
    1325    34319801 :                 if ( ( ivas_format == MC_FORMAT && mc_mode == MC_MODE_MCMASA ) )
    1326             :                 {
    1327      975814 :                     idx = min( bin, MASA_NUM_DEFINED_SUR_SPR_COH_ENE_BINS - 1 );
    1328             : 
    1329             :                     /* Apply the target spectrum to the eneCorrectionFactor */
    1330      975814 :                     if ( separateCenterChannelRendering ) /* spreadCoh mostly originates from phantom sources in separate channel rendering mode */
    1331             :                     {
    1332        7826 :                         eneCorrectionFactor *= w1 * 1.0f + ( w2 + w3 ) * spreadCohEne1[idx];
    1333             :                     }
    1334             :                     else
    1335             :                     {
    1336      967988 :                         eneCorrectionFactor *= w1 * 1.0f + w2 * spreadCohEne05[idx] + w3 * spreadCohEne1[idx];
    1337             :                     }
    1338             :                 }
    1339             : 
    1340             :                 /* Equalize the spread coherent combined HRTFs */
    1341    34319801 :                 eq = min( 4.0f, sqrtf( eneCorrectionFactor ) );
    1342    34319801 :                 lRealp *= eq;
    1343    34319801 :                 lImagp *= eq;
    1344    34319801 :                 rRealp *= eq;
    1345    34319801 :                 rImagp *= eq;
    1346             :             }
    1347             : 
    1348   139864112 :             hrtfEne[0] = ( lRealp * lRealp ) + ( lImagp * lImagp );
    1349   139864112 :             hrtfEne[1] = ( rRealp * rRealp ) + ( rImagp * rImagp );
    1350   139864112 :             hrtfCrossRe = ( lRealp * rRealp ) + ( lImagp * rImagp );
    1351   139864112 :             hrtfCrossIm = ( -lImagp * rRealp ) + ( lRealp * rImagp );
    1352             : 
    1353             :             /* Add direct part (1 or 2) covariance matrix */
    1354   139864112 :             dirEne = ratio * meanEnePerCh;
    1355   139864112 :             hDiracDecBin->ChEneOut[0][bin] += dirEne * hrtfEne[0]; /* Dir ene part*/
    1356   139864112 :             hDiracDecBin->ChEneOut[1][bin] += dirEne * hrtfEne[1];
    1357   139864112 :             hDiracDecBin->ChCrossReOut[bin] += dirEne * hrtfCrossRe; /* Dir cross re */
    1358   139864112 :             hDiracDecBin->ChCrossImOut[bin] += dirEne * hrtfCrossIm; /* Dir cross im */
    1359             :         }
    1360             : 
    1361             :         /* Add diffuse / ambient part covariance matrix */
    1362   107536400 :         diffuseness = max( 0.0f, diffuseness );
    1363   107536400 :         diffEne = diffuseness * meanEnePerCh;
    1364   107536400 :         surCoh = hSpatParamRendCom->surroundingCoherence[dirac_read_idx][bin];
    1365             : 
    1366   107536400 :         diffusenessValForDecorrelationReduction = max( 0.0f, diffusenessValForDecorrelationReduction );
    1367   107536400 :         diffEneValForDecorrelationReduction = diffusenessValForDecorrelationReduction * meanEnePerCh;
    1368             : 
    1369   107536400 :         if ( ( ivas_format == MC_FORMAT && mc_mode == MC_MODE_MCMASA ) )
    1370             :         {
    1371     1713740 :             if ( !hDiracDecBin->renderStereoOutputInsteadOfBinaural )
    1372             :             {
    1373             :                 float spectrumModVal;
    1374             : 
    1375     1713740 :                 idx = min( bin, MASA_NUM_DEFINED_SUR_SPR_COH_ENE_BINS - 1 );
    1376             :                 /* Apply target spectrum that emphasizes low frequencies when the sound is surround coherent */
    1377     1713740 :                 spectrumModVal = ( 1.0f - surCoh ) + surCoh * surCohEne[idx];
    1378     1713740 :                 diffEne *= spectrumModVal;
    1379             : 
    1380             :                 /* Modify also the value for decorrelation reduction */
    1381     1713740 :                 diffEneValForDecorrelationReduction *= spectrumModVal;
    1382             :             }
    1383             :         }
    1384   107536400 :         hDiracDecBin->ChEneOut[0][bin] += diffEne; /* Diff ene part*/
    1385   107536400 :         hDiracDecBin->ChEneOut[1][bin] += diffEne;
    1386             : 
    1387   107536400 :         if ( hDiracDecBin->renderStereoOutputInsteadOfBinaural )
    1388             :         {
    1389             :             /* When rendering stereo, ambience (except for surround coherent sound) has zero ICC. */
    1390     2745700 :             hDiracDecBin->ChCrossReOut[bin] += surCoh * diffEne;
    1391             :         }
    1392             :         else /* When rendering binaural, ambience has frequency dependent ICC. */
    1393             :         {
    1394   104790700 :             if ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && bin < BINAURAL_COHERENCE_DIFFERENCE_BINS )
    1395     3916206 :             {
    1396             :                 float diffuseFieldCoherence;
    1397     3916206 :                 diffuseFieldCoherence = hDiracDecBin->hDiffuseDist->diffuseRatioX[bin] * hDiracDecBin->diffuseFieldCoherenceX[bin] + hDiracDecBin->hDiffuseDist->diffuseRatioY[bin] * hDiracDecBin->diffuseFieldCoherenceY[bin] + hDiracDecBin->hDiffuseDist->diffuseRatioZ[bin] * hDiracDecBin->diffuseFieldCoherenceZ[bin];
    1398     3916206 :                 hDiracDecBin->ChCrossReOut[bin] += ( ( 1.0f - surCoh ) * diffuseFieldCoherence + surCoh ) * diffEne;
    1399             :             }
    1400             :             else
    1401             :             {
    1402   100874494 :                 hDiracDecBin->ChCrossReOut[bin] += ( ( 1.0f - surCoh ) * hDiracDecBin->diffuseFieldCoherence[bin] + surCoh ) * diffEne;
    1403             :             }
    1404             :         }
    1405             : 
    1406             :         /* Store parameters for formulating average diffuseness over frame */
    1407   107536400 :         hDiracDecBin->frameMeanDiffuseness[bin] += diffEneValForDecorrelationReduction;
    1408   107536400 :         frameMeanDiffusenessEneWeight[bin] += meanEnePerCh;
    1409             :     }
    1410             : 
    1411             :     /* Formulate average diffuseness over frame */
    1412   109419214 :     for ( bin = 0; bin < nBins; bin++ )
    1413             :     {
    1414   107536400 :         hDiracDecBin->frameMeanDiffuseness[bin] /= fmaxf( 1e-12f, frameMeanDiffusenessEneWeight[bin] );
    1415             :     }
    1416             : 
    1417   109419214 :     for ( bin = 0; bin < nBins; bin++ )
    1418             :     {
    1419   107536400 :         hDiracDecBin->ChCrossReOut[bin] *= qualityBasedSmFactor;
    1420   107536400 :         hDiracDecBin->ChCrossImOut[bin] *= qualityBasedSmFactor;
    1421             : 
    1422   322609200 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    1423             :         {
    1424   215072800 :             hDiracDecBin->ChEneOut[ch][bin] *= qualityBasedSmFactor;
    1425             :         }
    1426             : 
    1427   107536400 :         hDiracDecBin->ChCrossReOut[bin] += IIReneLimiter[bin] * hDiracDecBin->ChCrossReOutPrev[bin];
    1428   107536400 :         hDiracDecBin->ChCrossImOut[bin] += IIReneLimiter[bin] * hDiracDecBin->ChCrossImOutPrev[bin];
    1429             : 
    1430   322609200 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    1431             :         {
    1432   215072800 :             hDiracDecBin->ChEneOut[ch][bin] += IIReneLimiter[bin] * hDiracDecBin->ChEneOutPrev[ch][bin];
    1433             :         }
    1434             : 
    1435             :         /* Store energy values and coefficients for next round */
    1436   107536400 :         hDiracDecBin->ChCrossReOutPrev[bin] = hDiracDecBin->ChCrossReOut[bin];
    1437   107536400 :         hDiracDecBin->ChCrossImOutPrev[bin] = hDiracDecBin->ChCrossImOut[bin];
    1438             : 
    1439   322609200 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    1440             :         {
    1441   215072800 :             hDiracDecBin->ChEneOutPrev[ch][bin] = hDiracDecBin->ChEneOut[ch][bin];
    1442             :         }
    1443             :     }
    1444             : 
    1445     1882814 :     return;
    1446             : }
    1447             : 
    1448     1882814 : static void ivas_dirac_dec_binaural_determine_processing_matrices(
    1449             :     DIRAC_DEC_BIN_HANDLE hDiracDecBin,
    1450             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom,
    1451             :     PARAMBIN_REND_CONFIG_HANDLE hConfig,
    1452             :     const int16_t max_band_decorr,
    1453             :     float Rmat[3][3],
    1454             :     const int16_t subframe,
    1455             :     const int16_t isHeadtracked,
    1456             :     const int16_t nchanSeparateChannels,
    1457             :     const MASA_ISM_DATA_HANDLE hMasaIsmData )
    1458             : {
    1459             :     int16_t chA, chB, bin;
    1460             :     int16_t separateCenterChannelRendering;
    1461             :     int16_t nBins;
    1462             :     int16_t dirac_read_idx;
    1463             :     PARAMBIN_HRTF_GAIN_CACHE gainCache[MAX_NUM_OBJECTS];
    1464             :     int16_t idx;
    1465             :     ISM_MODE ism_mode;
    1466             :     IVAS_FORMAT ivas_format;
    1467             :     MC_MODE mc_mode;
    1468             :     int32_t ivas_total_brate;
    1469             :     int16_t nchan_transport;
    1470             : 
    1471     1882814 :     ivas_format = hConfig->ivas_format;
    1472     1882814 :     separateCenterChannelRendering = nchanSeparateChannels > 0;
    1473     1882814 :     mc_mode = hConfig->mc_mode;
    1474     1882814 :     ivas_total_brate = hConfig->ivas_total_brate;
    1475     1882814 :     nchan_transport = hConfig->nchan_transport;
    1476     1882814 :     nBins = hSpatParamRendCom->num_freq_bands; /* Actually bins */
    1477             : 
    1478     1882814 :     ism_mode = hConfig->ism_mode;
    1479             : 
    1480     1882814 :     dirac_read_idx = hSpatParamRendCom->render_to_md_map[subframe];
    1481             : 
    1482     9414070 :     for ( idx = 0; idx < MAX_NUM_OBJECTS; idx++ )
    1483             :     {
    1484     7531256 :         gainCache[idx].azi = -1000; /* Use -1000 as value for uninitialized cache. */
    1485             :     }
    1486             : 
    1487   109419214 :     for ( bin = 0; bin < nBins; bin++ )
    1488             :     {
    1489             :         float tmpMtxRe[BINAURAL_CHANNELS][BINAURAL_CHANNELS], tmpMtxIm[BINAURAL_CHANNELS][BINAURAL_CHANNELS], resultMtxRe[BINAURAL_CHANNELS][BINAURAL_CHANNELS], resultMtxIm[BINAURAL_CHANNELS][BINAURAL_CHANNELS], gain;
    1490             :         float CxRe[BINAURAL_CHANNELS][BINAURAL_CHANNELS], CxIm[BINAURAL_CHANNELS][BINAURAL_CHANNELS]; /* Input covariance matrix */
    1491             :         float realizedOutputEne, targetOutputEne, missingOutputEne;
    1492             :         float CrEneL, CrEneR; /* Cr = residual decorrelated sound covariance matrix */
    1493             :         float CrCrossRe, CrCrossIm;
    1494             :         float Mre[BINAURAL_CHANNELS][BINAURAL_CHANNELS], Mim[BINAURAL_CHANNELS][BINAURAL_CHANNELS], MdecRe[BINAURAL_CHANNELS][BINAURAL_CHANNELS], MdecIm[BINAURAL_CHANNELS][BINAURAL_CHANNELS]; /* M = mixing matrix; Mdec = residual decorrelated signal mixing matrix */
    1495   107536400 :         float prototypeMtx[BINAURAL_CHANNELS][BINAURAL_CHANNELS] = { { 1.0f, 0.05f }, { 0.05f, 1.0f } };                                                                                        /* Prototype matrix determines a reference signal in mixing matrix determination */
    1496             : 
    1497   107536400 :         CrEneL = 0.0f;
    1498   107536400 :         CrEneR = 0.0f;
    1499             : 
    1500             :         /* Formulate main processing matrix M */
    1501   107536400 :         formulate2x2MixingMatrix( hDiracDecBin->ChEne[0][bin], hDiracDecBin->ChEne[1][bin],
    1502             :                                   hDiracDecBin->ChCrossRe[bin], hDiracDecBin->ChCrossIm[bin],
    1503             :                                   hDiracDecBin->ChEneOut[0][bin], hDiracDecBin->ChEneOut[1][bin],
    1504             :                                   hDiracDecBin->ChCrossReOut[bin], hDiracDecBin->ChCrossImOut[bin],
    1505             :                                   prototypeMtx, Mre, Mim, hDiracDecBin->reqularizationFactor );
    1506             : 
    1507             :         /* Load estimated covariance matrix to the [2][2] matrix form */
    1508   107536400 :         CxRe[0][0] = hDiracDecBin->ChEne[0][bin];
    1509   107536400 :         CxRe[1][1] = hDiracDecBin->ChEne[1][bin];
    1510   107536400 :         CxRe[1][0] = hDiracDecBin->ChCrossRe[bin];
    1511   107536400 :         CxRe[0][1] = hDiracDecBin->ChCrossRe[bin];
    1512   107536400 :         CxIm[0][0] = 0.0f;
    1513   107536400 :         CxIm[1][1] = 0.0f;
    1514   107536400 :         CxIm[1][0] = hDiracDecBin->ChCrossIm[bin];
    1515   107536400 :         CxIm[0][1] = -hDiracDecBin->ChCrossIm[bin];
    1516             : 
    1517             :         /* Make matrix multiplication M*Cx*M' to determine resulting covariance matrix of processing input with M */
    1518   107536400 :         matrixMul( Mre, Mim, CxRe, CxIm, tmpMtxRe, tmpMtxIm );
    1519   107536400 :         matrixTransp2Mul( tmpMtxRe, tmpMtxIm, Mre, Mim, resultMtxRe, resultMtxIm );
    1520             : 
    1521             :         /* When below the frequency limit where decorrelation is applied, we inject the decorrelated
    1522             :          * residual (or missing) signal component. The procedure is active when there are not enough independent
    1523             :          * signal energy to synthesize a signal with the target covariance matrix from the non-decorrelated signals */
    1524   107536400 :         if ( bin < max_band_decorr )
    1525             :         {
    1526             :             float decorrelationReductionFactor;
    1527             : 
    1528             :             /* Subtract the resulting covariance matrix from the target covariance matrix to determine
    1529             :              * what signal component is missing. The result is the target covariance matrix for the residual signal, i.e.,
    1530             :              * a residual covariance matrix. */
    1531    44241105 :             CrEneL = max( 0.0f, hDiracDecBin->ChEneOut[0][bin] - resultMtxRe[0][0] );
    1532    44241105 :             CrEneR = max( 0.0f, hDiracDecBin->ChEneOut[1][bin] - resultMtxRe[1][1] );
    1533    44241105 :             CrCrossRe = hDiracDecBin->ChCrossReOut[bin] - resultMtxRe[1][0];
    1534    44241105 :             CrCrossIm = hDiracDecBin->ChCrossImOut[bin] - resultMtxIm[1][0];
    1535             : 
    1536             :             /* The amount of the decorrelated sound is further controlled based on the spatial metadata,
    1537             :              * by determining an energy-suppressed residual covariance matrix that is a control parameter
    1538             :              * that guides the processing of the decorrelated sound to a residual signal.
    1539             :              * The procedure improves quality in e.g. double-talk 2-direction rendering situations.*/
    1540    44241105 :             if ( ivas_format == MASA_FORMAT && ivas_total_brate < MASA_STEREO_MIN_BITRATE )
    1541             :             {
    1542    11914820 :                 decorrelationReductionFactor = 1.0f;
    1543             :             }
    1544    32326285 :             else if ( ( ivas_format == MC_FORMAT && mc_mode == MC_MODE_MCMASA ) || ( ivas_format == MASA_FORMAT && nchan_transport == 1 ) )
    1545             :             {
    1546    10442420 :                 decorrelationReductionFactor = sqrtf( fmaxf( 0.0f, hDiracDecBin->frameMeanDiffuseness[bin] ) );
    1547             :             }
    1548    21883865 :             else if ( ( ivas_format == SBA_FORMAT || ivas_format == SBA_ISM_FORMAT ) && nchan_transport == 1 )
    1549             :             {
    1550     3028040 :                 decorrelationReductionFactor = 1.0f;
    1551             :             }
    1552             :             else
    1553             :             {
    1554    18855825 :                 decorrelationReductionFactor = fmaxf( 0.0f, hDiracDecBin->frameMeanDiffuseness[bin] );
    1555             :             }
    1556    44241105 :             CrEneL *= decorrelationReductionFactor;
    1557    44241105 :             CrEneR *= decorrelationReductionFactor;
    1558    44241105 :             CrCrossRe *= decorrelationReductionFactor;
    1559    44241105 :             CrCrossIm *= decorrelationReductionFactor;
    1560             : 
    1561             :             /* Determine a residual mixing matrix Mdec for processing the decorrelated signal to obtain
    1562             :              * the residual signal (that has the residual covariance matrix) */
    1563    44241105 :             formulate2x2MixingMatrix( hDiracDecBin->ChEne[0][bin], hDiracDecBin->ChEne[1][bin],
    1564             :                                       0.0f, 0.0f, /* Decorrelated signal has ideally no cross-terms */
    1565             :                                       CrEneL, CrEneR,
    1566             :                                       CrCrossRe, CrCrossIm,
    1567             :                                       prototypeMtx, MdecRe, MdecIm, 0.2f );
    1568             :         }
    1569             :         else
    1570             :         {
    1571   189885885 :             for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    1572             :             {
    1573   126590590 :                 set_zero( MdecRe[chA], BINAURAL_CHANNELS );
    1574   126590590 :                 set_zero( MdecIm[chA], BINAURAL_CHANNELS );
    1575             :             }
    1576             :         }
    1577             : 
    1578             :         /* The regularizations at determining mixing matrices cause signal energy to be lost to some degree, which is compensated for here */
    1579   107536400 :         realizedOutputEne = CrEneL + CrEneR + resultMtxRe[0][0] + resultMtxRe[1][1];
    1580   107536400 :         targetOutputEne = hDiracDecBin->ChEneOut[0][bin] + hDiracDecBin->ChEneOut[1][bin];
    1581   107536400 :         missingOutputEne = fmaxf( 0.0f, targetOutputEne - realizedOutputEne );
    1582             : 
    1583   107536400 :         gain = sqrtf( ( resultMtxRe[0][0] + resultMtxRe[1][1] + missingOutputEne ) /
    1584   107536400 :                       fmaxf( 1e-12f, resultMtxRe[0][0] + resultMtxRe[1][1] ) );
    1585   107536400 :         gain = fminf( 4.0f, gain );
    1586             : 
    1587   322609200 :         for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    1588             :         {
    1589   645218400 :             for ( chB = 0; chB < BINAURAL_CHANNELS; chB++ )
    1590             :             {
    1591   430145600 :                 Mre[chA][chB] *= gain;
    1592   430145600 :                 Mim[chA][chB] *= gain;
    1593             :             }
    1594             :         }
    1595             : 
    1596             :         /* Store processing matrices */
    1597   322609200 :         for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    1598             :         {
    1599   645218400 :             for ( chB = 0; chB < BINAURAL_CHANNELS; chB++ )
    1600             :             {
    1601   430145600 :                 hDiracDecBin->processMtxRePrev[chA][chB][bin] = hDiracDecBin->processMtxRe[chA][chB][bin];
    1602   430145600 :                 hDiracDecBin->processMtxImPrev[chA][chB][bin] = hDiracDecBin->processMtxIm[chA][chB][bin];
    1603   430145600 :                 hDiracDecBin->processMtxDecRePrev[chA][chB][bin] = hDiracDecBin->processMtxDecRe[chA][chB][bin];
    1604   430145600 :                 hDiracDecBin->processMtxDecImPrev[chA][chB][bin] = hDiracDecBin->processMtxDecIm[chA][chB][bin];
    1605             : 
    1606   430145600 :                 hDiracDecBin->processMtxRe[chA][chB][bin] = Mre[chA][chB];
    1607   430145600 :                 hDiracDecBin->processMtxIm[chA][chB][bin] = Mim[chA][chB];
    1608   430145600 :                 hDiracDecBin->processMtxDecRe[chA][chB][bin] = MdecRe[chA][chB];
    1609   430145600 :                 hDiracDecBin->processMtxDecIm[chA][chB][bin] = MdecIm[chA][chB];
    1610             :             }
    1611             :         }
    1612             : 
    1613   107536400 :         if ( separateCenterChannelRendering )
    1614             :         {
    1615             :             /* The rendering of the separate center channel in masa + mono mode.
    1616             :              * The center channel is processed with a gain factor 0.8414f to match the loudness of different processing paths */
    1617             :             float lRealp, lImagp, rRealp, rImagp;
    1618             :             float gainFactor;
    1619    13306660 :             int16_t aziDeg = 0;
    1620    13306660 :             int16_t eleDeg = 0;
    1621    13306660 :             uint8_t instantChange = 0;
    1622             : 
    1623    13306660 :             if ( ivas_format == MASA_ISM_FORMAT )
    1624             :             {
    1625    13291660 :                 gainFactor = OMASA_TDREND_MATCHING_GAIN * sqrtf( hDiracDecBin->earlyPartEneCorrection[bin] );
    1626             :             }
    1627             :             else
    1628             :             {
    1629       15000 :                 gainFactor = 0.8414f * sqrtf( hDiracDecBin->earlyPartEneCorrection[bin] );
    1630             :             }
    1631             : 
    1632    27851240 :             for ( chB = 0; chB < nchanSeparateChannels; chB++ )
    1633             :             {
    1634    14544580 :                 if ( ivas_format == MASA_ISM_FORMAT )
    1635             :                 {
    1636    14529580 :                     if ( ism_mode == ISM_MASA_MODE_DISC )
    1637             :                     {
    1638     1909280 :                         if ( hMasaIsmData->ism_dir_is_edited[chB] )
    1639             :                         {
    1640           0 :                             aziDeg = hMasaIsmData->azimuth_ism_edited[chB];
    1641           0 :                             eleDeg = hMasaIsmData->elevation_ism_edited[chB];
    1642             :                         }
    1643             :                         else
    1644             :                         {
    1645     1909280 :                             aziDeg = hMasaIsmData->azimuth_ism[chB][dirac_read_idx];
    1646     1909280 :                             eleDeg = hMasaIsmData->elevation_ism[chB][dirac_read_idx];
    1647             :                         }
    1648             :                     }
    1649             :                     else
    1650             :                     {
    1651    12620300 :                         if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && hMasaIsmData->ism_dir_is_edited[hMasaIsmData->idx_separated_ism] )
    1652             :                         {
    1653      318720 :                             aziDeg = hMasaIsmData->azimuth_ism_edited[hMasaIsmData->idx_separated_ism];
    1654      318720 :                             eleDeg = hMasaIsmData->elevation_ism_edited[hMasaIsmData->idx_separated_ism];
    1655             :                         }
    1656             :                         else
    1657             :                         {
    1658    12301580 :                             aziDeg = hMasaIsmData->azimuth_separated_ism[dirac_read_idx];
    1659    12301580 :                             eleDeg = hMasaIsmData->elevation_separated_ism[dirac_read_idx];
    1660             :                         }
    1661    12620300 :                         instantChange = 1;
    1662             :                     }
    1663             :                 }
    1664             : 
    1665    43633740 :                 for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    1666             :                 {
    1667    29089160 :                     hDiracDecBin->processMtxRePrev[chA][chB + 2][bin] = hDiracDecBin->processMtxRe[chA][chB + 2][bin];
    1668    29089160 :                     hDiracDecBin->processMtxImPrev[chA][chB + 2][bin] = hDiracDecBin->processMtxIm[chA][chB + 2][bin];
    1669             :                 }
    1670             : 
    1671    14544580 :                 getDirectPartGains( bin, aziDeg, eleDeg, &lRealp, &lImagp, &rRealp, &rImagp, hDiracDecBin->renderStereoOutputInsteadOfBinaural, Rmat, &gainCache[chB], isHeadtracked, *hDiracDecBin->phHrtfParambin );
    1672             : 
    1673    14544580 :                 hDiracDecBin->processMtxRe[0][chB + 2][bin] = lRealp * gainFactor;
    1674    14544580 :                 hDiracDecBin->processMtxIm[0][chB + 2][bin] = lImagp * gainFactor;
    1675    14544580 :                 hDiracDecBin->processMtxRe[1][chB + 2][bin] = rRealp * gainFactor;
    1676    14544580 :                 hDiracDecBin->processMtxIm[1][chB + 2][bin] = rImagp * gainFactor;
    1677             : 
    1678    14544580 :                 if ( instantChange )
    1679             :                 {
    1680    37860900 :                     for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    1681             :                     {
    1682    25240600 :                         hDiracDecBin->processMtxRePrev[chA][chB + 2][bin] = hDiracDecBin->processMtxRe[chA][chB + 2][bin];
    1683    25240600 :                         hDiracDecBin->processMtxImPrev[chA][chB + 2][bin] = hDiracDecBin->processMtxIm[chA][chB + 2][bin];
    1684             :                     }
    1685             :                 }
    1686             :             }
    1687             :         }
    1688             :     }
    1689             : 
    1690     1882814 :     return;
    1691             : }
    1692             : 
    1693             : 
    1694     1882814 : static void ivas_dirac_dec_binaural_process_output(
    1695             :     DIRAC_DEC_BIN_HANDLE hDiracDecBin,
    1696             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom,
    1697             :     HANDLE_CLDFB_FILTER_BANK cldfbSynDec[MAX_OUTPUT_CHANNELS],
    1698             :     float *output_f[],
    1699             :     float inRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    1700             :     float inIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    1701             :     const int16_t max_band_decorr,
    1702             :     const int16_t numInChannels,
    1703             :     const int16_t processReverb,
    1704             :     const int16_t subframe,
    1705             :     float outRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    1706             :     float outIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    1707             :     float reverbRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    1708             :     float reverbIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    1709             :     float decorrRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    1710             :     float decorrIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    1711             :     const uint8_t recompute )
    1712             : {
    1713             :     int16_t slot, bin, chA, chB;
    1714             :     int16_t nBins;
    1715             :     float outSlotRe[CLDFB_NO_CHANNELS_MAX], outSlotIm[CLDFB_NO_CHANNELS_MAX];
    1716             :     float decSlotRe[BINAURAL_CHANNELS][CLDFB_NO_CHANNELS_MAX], decSlotIm[BINAURAL_CHANNELS][CLDFB_NO_CHANNELS_MAX];
    1717             :     float interpVal;
    1718             :     float *decSlotRePointer;
    1719             :     float *decSlotImPointer;
    1720             :     int16_t offsetSamples;
    1721             :     int16_t nSlots;
    1722             : 
    1723     1882814 :     nBins = hSpatParamRendCom->num_freq_bands;
    1724     1882814 :     offsetSamples = 0;
    1725     1882814 :     nSlots = hSpatParamRendCom->subframe_nbslots[subframe];
    1726             : 
    1727     1882814 :     if ( processReverb )
    1728             :     {
    1729             :         /* Process second / room effect part of binaural output when needed */
    1730      265910 :         if ( recompute == 1 )
    1731             :         {
    1732      265910 :             ivas_binaural_reverb_processSubframe( hDiracDecBin->hReverb, numInChannels, nSlots, inRe, inIm, reverbRe, reverbIm );
    1733             :         }
    1734             :     }
    1735             : 
    1736     1882814 :     interpVal = 0.0f;
    1737     9329170 :     for ( slot = 0; slot < nSlots; slot++ )
    1738             :     {
    1739     7446356 :         interpVal += 1.0f / (float) nSlots;
    1740     7446356 :         if ( !hDiracDecBin->useTdDecorr && max_band_decorr > 0 )
    1741             :         {
    1742     5708253 :             if ( recompute == 1 )
    1743             :             {
    1744     2562089 :                 ivas_dirac_dec_decorrelate_slot( hDiracDecBin, nBins, slot, inRe, inIm, decSlotRe, decSlotIm );
    1745             : 
    1746     7686267 :                 for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    1747             :                 {
    1748     5124178 :                     mvr2r( decSlotRe[chA], decorrRe[chA][slot], CLDFB_NO_CHANNELS_MAX );
    1749     5124178 :                     mvr2r( decSlotIm[chA], decorrIm[chA][slot], CLDFB_NO_CHANNELS_MAX );
    1750             :                 }
    1751             :             }
    1752             :             else
    1753             :             {
    1754     9438492 :                 for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    1755             :                 {
    1756     6292328 :                     mvr2r( decorrRe[chA][slot], decSlotRe[chA], CLDFB_NO_CHANNELS_MAX );
    1757     6292328 :                     mvr2r( decorrIm[chA][slot], decSlotIm[chA], CLDFB_NO_CHANNELS_MAX );
    1758             :                 }
    1759             :             }
    1760             :         }
    1761             : 
    1762    22339068 :         for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    1763             :         {
    1764             :             float *outSlotRePr, *outSlotImPr; /* Pointers needed for function call compatibility */
    1765             : 
    1766    14892712 :             set_zero( outSlotRe, CLDFB_NO_CHANNELS_MAX );
    1767    14892712 :             set_zero( outSlotIm, CLDFB_NO_CHANNELS_MAX );
    1768             : 
    1769             :             /* Processing of the first / HRTF part of the binaural output. */
    1770    46626686 :             for ( chB = 0; chB < numInChannels; chB++ )
    1771             :             {
    1772    31733974 :                 if ( chB < BINAURAL_CHANNELS )
    1773             :                 {
    1774             :                     /* Decorrelator signal for TD decorrelation is stored in two input channels above the two normal inputs.
    1775             :                      * It should be noted that TD decorrelation is used only in cases where numInChannels is 2. If this
    1776             :                      * changes, additional adjustments are required. When using CLDFB decorrelator, we simply assign the
    1777             :                      * pointers to buffers. */
    1778    29785424 :                     if ( hDiracDecBin->useTdDecorr )
    1779             :                     {
    1780     6232120 :                         decSlotRePointer = inRe[chB + 2][slot];
    1781     6232120 :                         decSlotImPointer = inIm[chB + 2][slot];
    1782             :                     }
    1783             :                     else
    1784             :                     {
    1785    23553304 :                         decSlotRePointer = decSlotRe[chB];
    1786    23553304 :                         decSlotImPointer = decSlotIm[chB];
    1787             :                     }
    1788             :                 }
    1789             :                 else
    1790             :                 {
    1791     1948550 :                     decSlotRePointer = NULL; /* below these pointers are used only for chB < 2 */
    1792     1948550 :                     decSlotImPointer = NULL;
    1793             :                 }
    1794             : 
    1795             : 
    1796  1844294734 :                 for ( bin = 0; bin < nBins; bin++ )
    1797             :                 {
    1798             :                     float gain;
    1799             : 
    1800             :                     /* Mixing using the formulated processing matrix M */
    1801  1812560760 :                     gain = ( 1.0f - interpVal ) * hDiracDecBin->processMtxRePrev[chA][chB][bin] +
    1802  1812560760 :                            interpVal * hDiracDecBin->processMtxRe[chA][chB][bin];
    1803  1812560760 :                     outSlotRe[bin] += gain * inRe[chB][slot][bin];
    1804  1812560760 :                     outSlotIm[bin] += gain * inIm[chB][slot][bin];
    1805             : 
    1806  1812560760 :                     gain = ( 1.0f - interpVal ) * hDiracDecBin->processMtxImPrev[chA][chB][bin] +
    1807  1812560760 :                            interpVal * hDiracDecBin->processMtxIm[chA][chB][bin];
    1808  1812560760 :                     outSlotRe[bin] -= gain * inIm[chB][slot][bin];
    1809  1812560760 :                     outSlotIm[bin] += gain * inRe[chB][slot][bin];
    1810             : 
    1811             :                     /* Mixing decorrelated signals using the formulated residual processing matrix Mdec */
    1812  1812560760 :                     if ( bin < max_band_decorr && chB < 2 )
    1813             :                     {
    1814   696330620 :                         gain = ( 1.0f - interpVal ) * hDiracDecBin->processMtxDecRePrev[chA][chB][bin] +
    1815   696330620 :                                interpVal * hDiracDecBin->processMtxDecRe[chA][chB][bin];
    1816   696330620 :                         outSlotRe[bin] += gain * decSlotRePointer[bin];
    1817   696330620 :                         outSlotIm[bin] += gain * decSlotImPointer[bin];
    1818             : 
    1819   696330620 :                         gain = ( 1.0f - interpVal ) * hDiracDecBin->processMtxDecImPrev[chA][chB][bin] +
    1820   696330620 :                                interpVal * hDiracDecBin->processMtxDecIm[chA][chB][bin];
    1821   696330620 :                         outSlotRe[bin] -= gain * decSlotImPointer[bin];
    1822   696330620 :                         outSlotIm[bin] += gain * decSlotRePointer[bin];
    1823             :                     }
    1824             :                 }
    1825             :             }
    1826             : 
    1827    14892712 :             if ( processReverb )
    1828             :             {
    1829             :                 /* Combine second (reverb) part with the first (HRTF) part to obtain binaural output signal with room effect */
    1830     2122902 :                 v_add( outSlotRe, reverbRe[chA][slot], outSlotRe, CLDFB_NO_CHANNELS_MAX );
    1831     2122902 :                 v_add( outSlotIm, reverbIm[chA][slot], outSlotIm, CLDFB_NO_CHANNELS_MAX );
    1832             :             }
    1833             : 
    1834    14892712 :             outSlotRePr = &( outSlotRe[0] );
    1835    14892712 :             outSlotImPr = &( outSlotIm[0] );
    1836             : 
    1837    14892712 :             if ( outRe != NULL && outIm != NULL )
    1838             :             {
    1839             :                 /* provide the data outside in CLDFB domain => mainly for split rendering */
    1840    10351918 :                 mvr2r( outSlotRePr, outRe[chA][slot], CLDFB_NO_CHANNELS_MAX );
    1841    10351918 :                 mvr2r( outSlotImPr, outIm[chA][slot], CLDFB_NO_CHANNELS_MAX );
    1842             :             }
    1843    14892712 :             if ( recompute == 1 )
    1844             :             {
    1845             :                 /* Inverse filter bank */
    1846     7086768 :                 cldfbSynthesis( &outSlotRePr, &outSlotImPr, &( output_f[chA][nBins * slot + offsetSamples] ), nBins, cldfbSynDec[chA] );
    1847             :             }
    1848             :         }
    1849             :     }
    1850             : 
    1851     1882814 :     return;
    1852             : }
    1853             : 
    1854             : 
    1855      367287 : static void adaptTransportSignalsHeadtracked(
    1856             :     COMBINED_ORIENTATION_HANDLE hHeadTrackData,
    1857             :     float inRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    1858             :     float inIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    1859             :     const int16_t nBins,
    1860             :     const int16_t nSlots,
    1861             :     float Rmat[3][3] )
    1862             : {
    1863             :     int16_t slot, ch, bin, louderCh;
    1864             :     float ILD, mono_factor_ILD, mono_factor_rotation, mono_factor, y_val, ene_proc, ene_target;
    1865             :     int16_t max_band;
    1866             :     float eqVal;
    1867             :     int16_t band_idx, bin_lo, bin_hi;
    1868             : 
    1869             :     /* Determine head-orientation-based mono factor.
    1870             :        Rmat[1][1] entry informs how close the ears are aligned according to transport signals. */
    1871      367287 :     y_val = 1.0f - fabsf( Rmat[1][1] );
    1872      367287 :     mono_factor_rotation = ( y_val - ADAPT_HTPROTO_ROT_LIM_0 ) / ( ADAPT_HTPROTO_ROT_LIM_1 - ADAPT_HTPROTO_ROT_LIM_0 );
    1873      367287 :     mono_factor_rotation = fmaxf( 0.0f, fminf( 1.0f, mono_factor_rotation ) );
    1874             : 
    1875             :     /* Adapt transport signals in frequency bands */
    1876             :     /* optimization grouping CLDFB bins into MASA bands (they are readily available in ROM and suitable for the task) AND group CLDFB slots into sub-frames */
    1877             : 
    1878      367287 :     max_band = 0;
    1879     9073550 :     while ( max_band < MASA_FREQUENCY_BANDS && MASA_band_grouping_24[max_band] < nBins )
    1880             :     {
    1881     8706263 :         max_band++;
    1882             :     }
    1883             : 
    1884     9073550 :     for ( band_idx = 0; band_idx < max_band; band_idx++ )
    1885             :     {
    1886             :         float ch_nrg[2]; /* storage for input signal channel energies */
    1887     8706263 :         bin_lo = MASA_band_grouping_24[band_idx];
    1888     8706263 :         bin_hi = min( MASA_band_grouping_24[band_idx + 1], (int16_t) nBins );
    1889    26118789 :         for ( ch = 0; ch < 2; ch++ )
    1890             :         {
    1891    17412526 :             ch_nrg[ch] = 0.0f;
    1892    86605862 :             for ( slot = 0; slot < nSlots; slot++ )
    1893             :             {
    1894   230439576 :                 for ( bin = bin_lo; bin < bin_hi; bin++ )
    1895             :                 {
    1896   161246240 :                     ch_nrg[ch] += ( inRe[ch][slot][bin] * inRe[ch][slot][bin] ) + ( inIm[ch][slot][bin] * inIm[ch][slot][bin] );
    1897             :                 }
    1898             :             }
    1899    17412526 :             hHeadTrackData->chEneIIR[ch][band_idx] *= ADAPT_HTPROTO_IIR_FAC;
    1900    17412526 :             hHeadTrackData->chEneIIR[ch][band_idx] += ( 1.0f - ADAPT_HTPROTO_IIR_FAC ) * ch_nrg[ch];
    1901             :         }
    1902             : 
    1903             :         /* Determine ILD */
    1904     8706263 :         ILD = fabsf( 10.0f * log10f( fmaxf( 1e-12f, hHeadTrackData->chEneIIR[0][band_idx] ) / fmaxf( 1e-12f, hHeadTrackData->chEneIIR[1][band_idx] ) ) );
    1905     8706263 :         if ( hHeadTrackData->chEneIIR[1][band_idx] > hHeadTrackData->chEneIIR[0][band_idx] )
    1906             :         {
    1907     3715660 :             louderCh = 1;
    1908             :         }
    1909             :         else
    1910             :         {
    1911     4990603 :             louderCh = 0;
    1912             :         }
    1913             : 
    1914             :         /* Determine ILD-based mono factor */
    1915     8706263 :         mono_factor_ILD = ( ILD - ADAPT_HTPROTO_ILD_LIM_DB0 ) / ( ADAPT_HTPROTO_ILD_LIM_DB1 - ADAPT_HTPROTO_ILD_LIM_DB0 );
    1916     8706263 :         mono_factor_ILD = fmaxf( 0.0f, fminf( 1.0f, mono_factor_ILD ) );
    1917             : 
    1918             :         /* Combine mono factors */
    1919     8706263 :         mono_factor = mono_factor_ILD * mono_factor_rotation;
    1920             : 
    1921             :         /* Mix original audio and sum signal according to determined mono factor */
    1922    26118789 :         for ( ch = 0; ch < 2; ch++ )
    1923             :         {
    1924    17412526 :             if ( ch != louderCh )
    1925             :             {
    1926     8706263 :                 float band_nrg = 0.0f;
    1927             : 
    1928    43302931 :                 for ( slot = 0; slot < nSlots; slot++ )
    1929             :                 {
    1930   115219788 :                     for ( bin = bin_lo; bin < bin_hi; bin++ )
    1931             :                     {
    1932             :                         /* mono sum signal with the computed weight + rest from the original channel */
    1933    80623120 :                         inRe[ch][slot][bin] = mono_factor * ( inRe[0][slot][bin] + inRe[1][slot][bin] ) + ( 1.0f - mono_factor ) * inRe[ch][slot][bin];
    1934    80623120 :                         inIm[ch][slot][bin] = mono_factor * ( inIm[0][slot][bin] + inIm[1][slot][bin] ) + ( 1.0f - mono_factor ) * inIm[ch][slot][bin];
    1935    80623120 :                         band_nrg += ( inRe[ch][slot][bin] * inRe[ch][slot][bin] ) + ( inIm[ch][slot][bin] * inIm[ch][slot][bin] );
    1936             :                     }
    1937             :                 }
    1938     8706263 :                 hHeadTrackData->procChEneIIR[ch][band_idx] *= ADAPT_HTPROTO_IIR_FAC;
    1939     8706263 :                 hHeadTrackData->procChEneIIR[ch][band_idx] += ( 1.0f - ADAPT_HTPROTO_IIR_FAC ) * band_nrg;
    1940             :             }
    1941             :             else
    1942             :             {
    1943             :                 /* processed signal is input. use the original channel, so no need to compute new signals or signal energy */
    1944     8706263 :                 hHeadTrackData->procChEneIIR[ch][band_idx] *= ADAPT_HTPROTO_IIR_FAC;
    1945     8706263 :                 hHeadTrackData->procChEneIIR[ch][band_idx] += ( 1.0f - ADAPT_HTPROTO_IIR_FAC ) * ch_nrg[ch];
    1946             :             }
    1947             :         }
    1948             : 
    1949             :         /* Equalize */
    1950     8706263 :         ene_target = hHeadTrackData->chEneIIR[0][band_idx] + hHeadTrackData->chEneIIR[1][band_idx];
    1951     8706263 :         ene_proc = hHeadTrackData->procChEneIIR[0][band_idx] + hHeadTrackData->procChEneIIR[1][band_idx];
    1952     8706263 :         eqVal = fminf( 4.0f, sqrtf( ene_target / fmaxf( 1e-12f, ene_proc ) ) );
    1953             : 
    1954    43302931 :         for ( slot = 0; slot < nSlots; slot++ )
    1955             :         {
    1956   103790004 :             for ( ch = 0; ch < 2; ch++ )
    1957             :             {
    1958   230439576 :                 for ( bin = bin_lo; bin < bin_hi; bin++ )
    1959             :                 {
    1960   161246240 :                     inRe[ch][slot][bin] *= eqVal;
    1961   161246240 :                     inIm[ch][slot][bin] *= eqVal;
    1962             :                 }
    1963             :             }
    1964             :         }
    1965             :     }
    1966             : 
    1967      367287 :     return;
    1968             : }
    1969             : 
    1970      367287 : static void ivas_dirac_dec_binaural_check_and_switch_transports_headtracked(
    1971             :     COMBINED_ORIENTATION_HANDLE hHeadTrackData,
    1972             :     float inRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    1973             :     float inIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    1974             :     const int16_t nBins,
    1975             :     const int16_t nSlots,
    1976             :     float Rmat[3][3] )
    1977             : {
    1978             :     int16_t slot, bin, ch;
    1979             :     float tmpVal;
    1980             : 
    1981             :     /* When not currently in prototype signal left-right switching procedure, check if such switching is needed */
    1982      367287 :     if ( hHeadTrackData->lrSwitchedNext == hHeadTrackData->lrSwitchedCurrent )
    1983             :     {
    1984      221767 :         float thresholdDotProduct = 0.17f; /* Corresponds to 10-degree switching threshold */
    1985      221767 :         if ( ( hHeadTrackData->lrSwitchedCurrent == 0 ) && ( Rmat[1][1] < -thresholdDotProduct ) )
    1986             :         {
    1987         810 :             hHeadTrackData->lrSwitchedNext = 1;
    1988             :         }
    1989      221767 :         if ( ( hHeadTrackData->lrSwitchedCurrent == 1 ) && ( Rmat[1][1] > thresholdDotProduct ) )
    1990             :         {
    1991         796 :             hHeadTrackData->lrSwitchedNext = 0;
    1992             :         }
    1993             :     }
    1994             : 
    1995             :     /* When currently in interpolation */
    1996      367287 :     if ( hHeadTrackData->lrSwitchedNext != hHeadTrackData->lrSwitchedCurrent )
    1997             :     {
    1998      728547 :         for ( slot = 0; slot < nSlots; slot++ )
    1999             :         {
    2000             :             float switchOrderFactor, origOrderFactor;
    2001             : 
    2002      581421 :             hHeadTrackData->lrSwitchInterpVal += 0.0025f; /* Corresponds to 0.5 seconds interpolation time */
    2003             : 
    2004      581421 :             if ( hHeadTrackData->lrSwitchInterpVal > 0.999f )
    2005             :             {
    2006             :                 /* Stop interpolation, reset values */
    2007        1295 :                 hHeadTrackData->lrSwitchInterpVal = 0.0f;
    2008        1295 :                 hHeadTrackData->lrSwitchedCurrent = hHeadTrackData->lrSwitchedNext;
    2009             :             }
    2010             : 
    2011             :             /* Gains for determining portion of switched channel order and original channel order */
    2012      581421 :             tmpVal = (float) hHeadTrackData->lrSwitchedNext * hHeadTrackData->lrSwitchInterpVal;
    2013      581421 :             tmpVal += (float) hHeadTrackData->lrSwitchedCurrent * ( 1.0f - hHeadTrackData->lrSwitchInterpVal );
    2014      581421 :             switchOrderFactor = sqrtf( tmpVal );
    2015      581421 :             origOrderFactor = sqrtf( 1.0f - tmpVal );
    2016             : 
    2017    35060201 :             for ( bin = 0; bin < nBins; bin++ )
    2018             :             {
    2019             :                 /* determine original order (1) signals and switched order (2) signals */
    2020             :                 float re1[BINAURAL_CHANNELS], re2[BINAURAL_CHANNELS], im1[BINAURAL_CHANNELS], im2[BINAURAL_CHANNELS];
    2021             : 
    2022   103436340 :                 for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    2023             :                 {
    2024    68957560 :                     re1[ch] = inRe[ch][slot][bin] * origOrderFactor;
    2025    68957560 :                     re2[ch] = inRe[1 - ch][slot][bin] * switchOrderFactor;
    2026    68957560 :                     im1[ch] = inIm[ch][slot][bin] * origOrderFactor;
    2027    68957560 :                     im2[ch] = inIm[1 - ch][slot][bin] * switchOrderFactor;
    2028             :                 }
    2029             : 
    2030   103436340 :                 for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    2031             :                 {
    2032             :                     float eneRef, ene, eq;
    2033             : 
    2034             :                     /* Interpolate / mix original and switched order signals */
    2035    68957560 :                     inRe[ch][slot][bin] = re1[ch] + re2[ch];
    2036    68957560 :                     inIm[ch][slot][bin] = im1[ch] + im2[ch];
    2037             : 
    2038             :                     /* Equalize interpolated signals to preserve energy per bin */
    2039    68957560 :                     eneRef = ( re1[ch] * re1[ch] ) + ( re2[ch] * re2[ch] ) + ( im1[ch] * im1[ch] ) + ( im2[ch] * im2[ch] );
    2040    68957560 :                     ene = ( inRe[ch][slot][bin] * inRe[ch][slot][bin] ) + ( inIm[ch][slot][bin] * inIm[ch][slot][bin] );
    2041    68957560 :                     eq = sqrtf( eneRef / fmaxf( 1e-12f, ene ) );
    2042    68957560 :                     eq = fminf( 4.0f, eq );
    2043    68957560 :                     inRe[ch][slot][bin] *= eq;
    2044    68957560 :                     inIm[ch][slot][bin] *= eq;
    2045             :                 }
    2046             :             }
    2047             :         }
    2048             :     }
    2049             :     else
    2050             :     {
    2051             :         /* If not in interpolation, but in switched prototype situation, then switch left and right channels */
    2052      220161 :         if ( hHeadTrackData->lrSwitchedCurrent == 1 )
    2053             :         {
    2054      340782 :             for ( slot = 0; slot < nSlots; slot++ )
    2055             :             {
    2056    16100124 :                 for ( bin = 0; bin < nBins; bin++ )
    2057             :                 {
    2058    15827920 :                     tmpVal = inRe[0][slot][bin];
    2059    15827920 :                     inRe[0][slot][bin] = inRe[1][slot][bin];
    2060    15827920 :                     inRe[1][slot][bin] = tmpVal;
    2061    15827920 :                     tmpVal = inIm[0][slot][bin];
    2062    15827920 :                     inIm[0][slot][bin] = inIm[1][slot][bin];
    2063    15827920 :                     inIm[1][slot][bin] = tmpVal;
    2064             :                 }
    2065             :             }
    2066             :         }
    2067             :     }
    2068             : 
    2069      367287 :     return;
    2070             : }
    2071             : 
    2072             : 
    2073   303555010 : static void eig2x2(
    2074             :     const float E1,
    2075             :     const float E2,
    2076             :     const float Cre,
    2077             :     const float Cim,
    2078             :     float Ure[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2079             :     float Uim[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2080             :     float D[BINAURAL_CHANNELS] )
    2081             : {
    2082             :     int16_t chA, chB, ch;
    2083             :     float s, normVal, crossSquare, a, pm, add;
    2084             : 
    2085   910665030 :     for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    2086             :     {
    2087  1821330060 :         for ( chB = 0; chB < BINAURAL_CHANNELS; chB++ )
    2088             :         {
    2089  1214220040 :             Ure[chA][chB] = 0.0f;
    2090  1214220040 :             Uim[chA][chB] = 0.0f;
    2091             :         }
    2092             :     }
    2093             : 
    2094   303555010 :     crossSquare = ( Cre * Cre ) + ( Cim * Cim );
    2095   303555010 :     a = ( E1 + E2 ) * ( E1 + E2 ) - 4.0f * ( ( E1 * E2 ) - crossSquare );
    2096   303555010 :     pm = 0.5f * sqrtf( max( 0.0f, a ) );
    2097   303555010 :     add = 0.5f * ( E1 + E2 );
    2098             : 
    2099   303555010 :     D[0] = add + pm;
    2100   303555010 :     D[1] = max( 0.0f, add - pm );
    2101             : 
    2102             :     /* Numeric case, when input is practically zeros */
    2103   303555010 :     if ( D[0] < 1e-12f )
    2104             :     {
    2105     1592297 :         Ure[0][0] = 1.0f;
    2106     1592297 :         Ure[1][1] = 1.0f;
    2107             : 
    2108     1592297 :         return;
    2109             :     }
    2110             : 
    2111             :     /* Numeric case, when input is near an identity matrix with a gain */
    2112   301962713 :     if ( pm < ( 1e-3f * add ) )
    2113             :     {
    2114    24945916 :         Ure[0][0] = 1.0f;
    2115    24945916 :         Ure[1][1] = 1.0f;
    2116             : 
    2117    24945916 :         return;
    2118             :     }
    2119             : 
    2120             :     /* Eigenvectors */
    2121   831050391 :     for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    2122             :     {
    2123   554033594 :         if ( fabsf( E2 - D[ch] ) > fabsf( E1 - D[ch] ) )
    2124             :         {
    2125   241325510 :             s = D[ch] - E2;
    2126   241325510 :             normVal = sqrtf( 1.0f / ( 1e-12f + crossSquare + s * s ) );
    2127             : 
    2128   241325510 :             Ure[0][ch] = s * normVal;
    2129   241325510 :             Ure[1][ch] = Cre * normVal;
    2130   241325510 :             Uim[1][ch] = Cim * normVal;
    2131             :         }
    2132             :         else
    2133             :         {
    2134   312708084 :             s = D[ch] - E1;
    2135   312708084 :             normVal = sqrtf( 1.0f / ( 1e-12f + crossSquare + s * s ) );
    2136             : 
    2137   312708084 :             Ure[1][ch] = s * normVal;
    2138   312708084 :             Ure[0][ch] = Cre * normVal;
    2139   312708084 :             Uim[0][ch] = -Cim * normVal;
    2140             :         }
    2141             :     }
    2142             : 
    2143   277016797 :     return;
    2144             : }
    2145             : 
    2146             : 
    2147   151777505 : static void matrixDiagMul(
    2148             :     float reIn[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2149             :     float imIn[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2150             :     const float D[BINAURAL_CHANNELS],
    2151             :     float reOut[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2152             :     float imOut[BINAURAL_CHANNELS][BINAURAL_CHANNELS] )
    2153             : {
    2154             :     int16_t chA, chB;
    2155             : 
    2156   455332515 :     for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    2157             :     {
    2158   910665030 :         for ( chB = 0; chB < BINAURAL_CHANNELS; chB++ )
    2159             :         {
    2160   607110020 :             reOut[chA][chB] = reIn[chA][chB] * D[chB];
    2161   607110020 :             imOut[chA][chB] = imIn[chA][chB] * D[chB];
    2162             :         }
    2163             :     }
    2164             : 
    2165   151777505 :     return;
    2166             : }
    2167             : 
    2168             : 
    2169   562868915 : static void matrixMul(
    2170             :     float Are[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2171             :     float Aim[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2172             :     float Bre[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2173             :     float Bim[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2174             :     float outRe[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2175             :     float outIm[BINAURAL_CHANNELS][BINAURAL_CHANNELS] )
    2176             : {
    2177             :     int16_t chA, chB;
    2178             : 
    2179  1688606745 :     for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    2180             :     {
    2181  3377213490 :         for ( chB = 0; chB < BINAURAL_CHANNELS; chB++ )
    2182             :         {
    2183  2251475660 :             outRe[chA][chB] = Are[chA][0] * Bre[0][chB] + Are[chA][1] * Bre[1][chB];
    2184  2251475660 :             outRe[chA][chB] -= Aim[chA][0] * Bim[0][chB] + Aim[chA][1] * Bim[1][chB];
    2185  2251475660 :             outIm[chA][chB] = Aim[chA][0] * Bre[0][chB] + Aim[chA][1] * Bre[1][chB];
    2186  2251475660 :             outIm[chA][chB] += Are[chA][0] * Bim[0][chB] + Are[chA][1] * Bim[1][chB];
    2187             :         }
    2188             :     }
    2189             : 
    2190   562868915 :     return;
    2191             : }
    2192             : 
    2193             : 
    2194   151777505 : static void matrixTransp1Mul(
    2195             :     float Are[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2196             :     float Aim[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2197             :     float Bre[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2198             :     float Bim[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2199             :     float outRe[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2200             :     float outIm[BINAURAL_CHANNELS][BINAURAL_CHANNELS] )
    2201             : {
    2202             :     int16_t chA, chB;
    2203             : 
    2204   455332515 :     for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    2205             :     {
    2206   910665030 :         for ( chB = 0; chB < BINAURAL_CHANNELS; chB++ )
    2207             :         {
    2208   607110020 :             outRe[chA][chB] = Are[0][chA] * Bre[0][chB] + Are[1][chA] * Bre[1][chB];
    2209   607110020 :             outRe[chA][chB] -= -Aim[0][chA] * Bim[0][chB] - Aim[1][chA] * Bim[1][chB];
    2210   607110020 :             outIm[chA][chB] = -Aim[0][chA] * Bre[0][chB] - Aim[1][chA] * Bre[1][chB];
    2211   607110020 :             outIm[chA][chB] += Are[0][chA] * Bim[0][chB] + Are[1][chA] * Bim[1][chB];
    2212             :         }
    2213             :     }
    2214             : 
    2215   151777505 :     return;
    2216             : }
    2217             : 
    2218             : 
    2219   411091410 : static void matrixTransp2Mul(
    2220             :     float Are[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2221             :     float Aim[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2222             :     float Bre[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2223             :     float Bim[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2224             :     float outRe[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2225             :     float outIm[BINAURAL_CHANNELS][BINAURAL_CHANNELS] )
    2226             : {
    2227             :     int16_t chA, chB;
    2228             : 
    2229  1233274230 :     for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    2230             :     {
    2231  2466548460 :         for ( chB = 0; chB < BINAURAL_CHANNELS; chB++ )
    2232             :         {
    2233  1644365640 :             outRe[chA][chB] = Are[chA][0] * Bre[chB][0] + Are[chA][1] * Bre[chB][1];
    2234  1644365640 :             outRe[chA][chB] -= Aim[chA][0] * ( -Bim[chB][0] ) + Aim[chA][1] * ( -Bim[chB][1] );
    2235  1644365640 :             outIm[chA][chB] = Aim[chA][0] * Bre[chB][0] + Aim[chA][1] * Bre[chB][1];
    2236  1644365640 :             outIm[chA][chB] += Are[chA][0] * ( -Bim[chB][0] ) + Are[chA][1] * ( -Bim[chB][1] );
    2237             :         }
    2238             :     }
    2239             : 
    2240   411091410 :     return;
    2241             : }
    2242             : 
    2243             : 
    2244   151777505 : static void chol2x2(
    2245             :     const float E1,
    2246             :     const float E2,
    2247             :     const float Cre,
    2248             :     const float Cim,
    2249             :     float outRe[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2250             :     float outIm[BINAURAL_CHANNELS][BINAURAL_CHANNELS] )
    2251             : {
    2252             :     int16_t chA, chB;
    2253             :     float sqrtVal;
    2254             : 
    2255   455332515 :     for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    2256             :     {
    2257   910665030 :         for ( chB = 0; chB < BINAURAL_CHANNELS; chB++ )
    2258             :         {
    2259   607110020 :             outRe[chA][chB] = 0.0f;
    2260   607110020 :             outIm[chA][chB] = 0.0f;
    2261             :         }
    2262             :     }
    2263             : 
    2264   151777505 :     if ( E1 > E2 ) /* Perform Cholesky decomposition according to louder channel first */
    2265             :     {
    2266    78936735 :         outRe[0][0] = sqrtf( E1 );
    2267    78936735 :         outRe[1][0] = Cre / ( 1e-12f + outRe[0][0] );
    2268    78936735 :         outIm[1][0] = Cim / ( 1e-12f + outRe[0][0] );
    2269    78936735 :         sqrtVal = E2 - ( Cre * Cre + Cim * Cim ) / ( 1e-12f + E1 );
    2270    78936735 :         outRe[1][1] = sqrtf( max( 0.0f, sqrtVal ) );
    2271             :     }
    2272             :     else
    2273             :     {
    2274    72840770 :         outRe[1][1] = sqrtf( E2 );
    2275    72840770 :         outRe[0][1] = Cre / ( 1e-12f + outRe[1][1] );
    2276    72840770 :         outIm[0][1] = -Cim / ( 1e-12f + outRe[1][1] );
    2277    72840770 :         sqrtVal = E1 - ( Cre * Cre + Cim * Cim ) / ( 1e-12f + E2 );
    2278    72840770 :         outRe[0][0] = sqrtf( max( 0.0f, sqrtVal ) );
    2279             :     }
    2280             : 
    2281   151777505 :     return;
    2282             : }
    2283             : 
    2284             : 
    2285   151777505 : static void formulate2x2MixingMatrix(
    2286             :     float Ein1,
    2287             :     float Ein2,
    2288             :     float CinRe,
    2289             :     float CinIm,
    2290             :     float Eout1,
    2291             :     float Eout2,
    2292             :     float CoutRe,
    2293             :     float CoutIm,
    2294             :     float Q[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2295             :     float Mre[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2296             :     float Mim[BINAURAL_CHANNELS][BINAURAL_CHANNELS],
    2297             :     const float regularizationFactor )
    2298             : {
    2299             :     /*
    2300             :      This function implements a 2x2 solution for an optimized spatial audio rendering algorithm
    2301             :      Vilkamo, J., Bäckström, T. and Kuntz, A., 2013.
    2302             :      "Optimized covariance domain framework for time–frequency processing of spatial audio."
    2303             :      Journal of the Audio Engineering Society, 61(6), pp.403-411.
    2304             : 
    2305             :      The result of the formulas below are the same as those in the publication, however, some
    2306             :      derivation details differ for as simple as possible 2x2 formulattion
    2307             :      */
    2308             :     int16_t chA, chB;
    2309             :     float maxEne, maxEneDiv;
    2310             :     float KyRe[BINAURAL_CHANNELS][BINAURAL_CHANNELS], KyIm[BINAURAL_CHANNELS][BINAURAL_CHANNELS];
    2311             :     float Uxre[BINAURAL_CHANNELS][BINAURAL_CHANNELS], Uxim[BINAURAL_CHANNELS][BINAURAL_CHANNELS];
    2312             :     float Sx[BINAURAL_CHANNELS], Kxre[BINAURAL_CHANNELS][BINAURAL_CHANNELS], Kxim[BINAURAL_CHANNELS][BINAURAL_CHANNELS];
    2313             :     float tmpRe[BINAURAL_CHANNELS][BINAURAL_CHANNELS], tmpIm[BINAURAL_CHANNELS][BINAURAL_CHANNELS];
    2314             :     float Are[BINAURAL_CHANNELS][BINAURAL_CHANNELS], Aim[BINAURAL_CHANNELS][BINAURAL_CHANNELS];
    2315             :     float Ure[BINAURAL_CHANNELS][BINAURAL_CHANNELS], Uim[BINAURAL_CHANNELS][BINAURAL_CHANNELS];
    2316             :     float D[BINAURAL_CHANNELS];
    2317             :     float div[BINAURAL_CHANNELS];
    2318             :     float Ghat[BINAURAL_CHANNELS];
    2319             :     float GhatQ[BINAURAL_CHANNELS][BINAURAL_CHANNELS];
    2320             :     float Pre[BINAURAL_CHANNELS][BINAURAL_CHANNELS], Pim[BINAURAL_CHANNELS][BINAURAL_CHANNELS];
    2321             : 
    2322             :     /* Normalize energy values */
    2323   151777505 :     maxEne = Ein1;
    2324   151777505 :     maxEne = max( maxEne, Ein2 );
    2325   151777505 :     maxEne = max( maxEne, Eout1 );
    2326   151777505 :     maxEne = max( maxEne, Eout2 );
    2327   151777505 :     maxEneDiv = 1.0f / ( maxEne + 1e-12f );
    2328   151777505 :     Ein1 *= maxEneDiv;
    2329   151777505 :     Ein2 *= maxEneDiv;
    2330   151777505 :     CinRe *= maxEneDiv;
    2331   151777505 :     CinIm *= maxEneDiv;
    2332   151777505 :     Eout1 *= maxEneDiv;
    2333   151777505 :     Eout2 *= maxEneDiv;
    2334   151777505 :     CoutRe *= maxEneDiv;
    2335   151777505 :     CoutIm *= maxEneDiv;
    2336             : 
    2337             :     /* Cholesky decomposition of target / output covariance matrix */
    2338   151777505 :     chol2x2( Eout1, Eout2, CoutRe, CoutIm, KyRe, KyIm );
    2339             : 
    2340             :     /* Eigendecomposition of input covariance matrix */
    2341   151777505 :     eig2x2( Ein1, Ein2, CinRe, CinIm, Uxre, Uxim, Sx );
    2342             : 
    2343             :     /* Eigendecomposition to Kx -- Ux Sx Ux' -> Kx Kx'*/
    2344   151777505 :     Sx[0] = sqrtf( Sx[0] );
    2345   151777505 :     Sx[1] = sqrtf( Sx[1] );
    2346   151777505 :     matrixDiagMul( Uxre, Uxim, Sx, Kxre, Kxim );
    2347             : 
    2348             :     /* Regularize the diagonal Sx for matrix inversion */
    2349   151777505 :     Sx[0] = max( Sx[0], regularizationFactor * Sx[1] );
    2350   151777505 :     Sx[1] = max( Sx[1], regularizationFactor * Sx[0] );
    2351             : 
    2352             :     /* This is equivalent to the prototype signal energy normalization in the publication */
    2353   151777505 :     Ghat[0] = sqrtf( Eout1 / ( 1e-12f + max( Ein1, 0.001f * Ein2 ) ) );
    2354   151777505 :     Ghat[1] = sqrtf( Eout2 / ( 1e-12f + max( Ein2, 0.001f * Ein1 ) ) );
    2355             : 
    2356             :     /* Matrix multiplication, tmp = Ky' * G_hat * Q */
    2357   455332515 :     for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    2358             :     {
    2359   303555010 :         GhatQ[chA][0] = Q[chA][0] * Ghat[chA];
    2360   303555010 :         GhatQ[chA][1] = Q[chA][1] * Ghat[chA];
    2361             :     }
    2362   455332515 :     for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    2363             :     {
    2364   910665030 :         for ( chB = 0; chB < BINAURAL_CHANNELS; chB++ )
    2365             :         {
    2366   607110020 :             tmpRe[chA][chB] = KyRe[0][chA] * GhatQ[0][chB] + KyRe[1][chA] * GhatQ[1][chB];
    2367   607110020 :             tmpIm[chA][chB] = -KyIm[0][chA] * GhatQ[0][chB] - KyIm[1][chA] * GhatQ[1][chB];
    2368             :         }
    2369             :     }
    2370             : 
    2371             :     /* A = Ky' * G_hat * Q * Kx (see publication) */
    2372   151777505 :     matrixMul( tmpRe, tmpIm, Kxre, Kxim, Are, Aim );
    2373             : 
    2374             :     /* Find nearest orthonormal matrix P to A = Ky' * G_hat * Q * Kx
    2375             :       For matrix A that is P = A(A'A)^0.5
    2376             :     */
    2377   151777505 :     matrixTransp1Mul( Are, Aim, Are, Aim, tmpRe, tmpIm );
    2378             : 
    2379   151777505 :     eig2x2( tmpRe[0][0], tmpRe[1][1], tmpRe[1][0], tmpIm[1][0], Ure, Uim, D );
    2380             : 
    2381   151777505 :     div[0] = min( 10000.0f, sqrtf( 1.0f / ( 1e-12f + D[0] ) ) );
    2382   151777505 :     div[1] = min( 10000.0f, sqrtf( 1.0f / ( 1e-12f + D[1] ) ) );
    2383             : 
    2384   151777505 :     matrixMul( Are, Aim, Ure, Uim, tmpRe, tmpIm );
    2385             : 
    2386   455332515 :     for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    2387             :     {
    2388   910665030 :         for ( chB = 0; chB < BINAURAL_CHANNELS; chB++ )
    2389             :         {
    2390   607110020 :             tmpRe[chA][chB] *= div[chB];
    2391   607110020 :             tmpIm[chA][chB] *= div[chB];
    2392             :         }
    2393             :     }
    2394             : 
    2395   151777505 :     matrixTransp2Mul( tmpRe, tmpIm, Ure, Uim, Pre, Pim ); /* Nearest orthonormal matrix P to matrix A formulated */
    2396             : 
    2397             :     /* These are the final formulas of the JAES publication M = Ky P Kx^(-1) */
    2398   455332515 :     for ( chA = 0; chA < BINAURAL_CHANNELS; chA++ )
    2399             :     {
    2400   910665030 :         for ( chB = 0; chB < BINAURAL_CHANNELS; chB++ )
    2401             :         {
    2402   607110020 :             Pre[chA][chB] /= Sx[chB] + 1e-12f;
    2403   607110020 :             Pim[chA][chB] /= Sx[chB] + 1e-12f;
    2404             :         }
    2405             :     }
    2406             : 
    2407   151777505 :     matrixMul( KyRe, KyIm, Pre, Pim, tmpRe, tmpIm );
    2408             : 
    2409   151777505 :     matrixTransp2Mul( tmpRe, tmpIm, Uxre, Uxim, Mre, Mim );
    2410             : 
    2411   151777505 :     return;
    2412             : }
    2413             : 
    2414             : 
    2415   223048294 : static void getDirectPartGains(
    2416             :     const int16_t bin,
    2417             :     int16_t aziDeg,
    2418             :     int16_t eleDeg,
    2419             :     float *lRealp,
    2420             :     float *lImagp,
    2421             :     float *rRealp,
    2422             :     float *rImagp,
    2423             :     const uint8_t renderStereoOutputInsteadOfBinaural,
    2424             :     float Rmat[3][3],
    2425             :     PARAMBIN_HRTF_GAIN_CACHE *gainCache,
    2426             :     const int16_t isHeadtracked,
    2427             :     HRTFS_PARAMBIN_HANDLE hHrtfParambin )
    2428             : {
    2429             :     float aziRad, eleRad;
    2430             :     float y, mappedX, aziRadMapped, A, A2, A3;
    2431             : 
    2432   223048294 :     if ( renderStereoOutputInsteadOfBinaural ) /* In stereo (i.e. non-binaural) rendering mode */
    2433             :     {
    2434     5624639 :         *lImagp = 0.0f;
    2435     5624639 :         *rImagp = 0.0f;
    2436     5624639 :         if ( aziDeg == gainCache->azi && eleDeg == gainCache->ele )
    2437             :         {
    2438     4902287 :             *lRealp = gainCache->shVec[0]; /* Reused memory */
    2439     4902287 :             *rRealp = gainCache->shVec[1]; /* Reused memory */
    2440             :         }
    2441             :         else
    2442             :         {
    2443             :             /* Convert azi and ele to an azi value of the cone of confusion */
    2444      722352 :             aziRad = (float) aziDeg * PI_OVER_180;
    2445      722352 :             eleRad = (float) eleDeg * PI_OVER_180;
    2446      722352 :             y = ( sinf( aziRad ) * cosf( eleRad ) );
    2447      722352 :             mappedX = sqrtf( max( 0.0f, 1.0f - ( y * y ) ) );
    2448      722352 :             aziRadMapped = atan2f( y, mappedX );
    2449             : 
    2450             :             /* Determine the real valued amplitude panning gains */
    2451      722352 :             if ( aziRadMapped >= LS_ANGLE_RAD_30_DEG )
    2452             :             { /* Left side */
    2453      247306 :                 *lRealp = 1.0f;
    2454      247306 :                 *rRealp = 0.0f;
    2455             :             }
    2456      475046 :             else if ( aziRadMapped <= -LS_ANGLE_RAD_30_DEG )
    2457             :             { /* Right side */
    2458      260369 :                 *lRealp = 0.0f;
    2459      260369 :                 *rRealp = 1.0f;
    2460             :             }
    2461             :             else /* Tangent panning law */
    2462             :             {
    2463      214677 :                 A = tanf( aziRadMapped ) * INV_TAN_LS_ANGLE_RAD_30_DEG;
    2464      214677 :                 A2 = ( A - 1.0f ) / max( 0.001f, A + 1.0f );
    2465      214677 :                 A3 = 1.0f / ( A2 * A2 + 1.0f );
    2466      214677 :                 *lRealp = sqrtf( A3 );
    2467      214677 :                 *rRealp = sqrtf( 1.0f - A3 );
    2468             :             }
    2469             :             /* Scaling to have the same expected gain as for the HRTF rendering */
    2470      722352 :             *lRealp *= SQRT2;
    2471      722352 :             *rRealp *= SQRT2;
    2472             : 
    2473             :             /* Store to gain cache */
    2474      722352 :             gainCache->azi = aziDeg;
    2475      722352 :             gainCache->ele = eleDeg;
    2476      722352 :             gainCache->shVec[0] = *lRealp; /* Reuse memory */
    2477      722352 :             gainCache->shVec[1] = *rRealp; /* Reuse memory */
    2478             :         }
    2479             :     }
    2480             :     else /* In regular binaural rendering mode */
    2481             :     {
    2482   217423655 :         if ( aziDeg == gainCache->azi && eleDeg == gainCache->ele )
    2483             :         {
    2484   165964735 :             hrtfShGetHrtf( bin, aziDeg, eleDeg, lRealp, lImagp, rRealp, rImagp, gainCache, TRUE, hHrtfParambin );
    2485             :         }
    2486             :         else
    2487             :         {
    2488    51458920 :             gainCache->azi = aziDeg;
    2489    51458920 :             gainCache->ele = eleDeg;
    2490    51458920 :             if ( isHeadtracked )
    2491             :             {
    2492    42626197 :                 rotateAziEle( (float) aziDeg, (float) eleDeg, &aziDeg, &eleDeg, Rmat, 0 );
    2493             :             }
    2494    51458920 :             hrtfShGetHrtf( bin, aziDeg, eleDeg, lRealp, lImagp, rRealp, rImagp, gainCache, FALSE, hHrtfParambin );
    2495             :         }
    2496             :     }
    2497             : 
    2498   223048294 :     return;
    2499             : }
    2500             : 
    2501             : 
    2502   217423655 : static void hrtfShGetHrtf(
    2503             :     const int16_t bin,
    2504             :     const int16_t aziDeg,
    2505             :     const int16_t eleDeg,
    2506             :     float *lRealp,
    2507             :     float *lImagp,
    2508             :     float *rRealp,
    2509             :     float *rImagp,
    2510             :     PARAMBIN_HRTF_GAIN_CACHE *gainCache,
    2511             :     const int16_t useCachedValue,
    2512             :     HRTFS_PARAMBIN_HANDLE hHrtfParambin )
    2513             : {
    2514             :     int16_t k;
    2515             :     float( *hrtfShCoeffsReInt )[16][60];
    2516             :     float( *hrtfShCoeffsImInt )[16][60];
    2517   217423655 :     hrtfShCoeffsReInt = hHrtfParambin->hrtfShCoeffsRe;
    2518   217423655 :     hrtfShCoeffsImInt = hHrtfParambin->hrtfShCoeffsIm;
    2519             : 
    2520   217423655 :     *lRealp = 0.0f;
    2521   217423655 :     *lImagp = 0.0f;
    2522   217423655 :     *rRealp = 0.0f;
    2523   217423655 :     *rImagp = 0.0f;
    2524             : 
    2525   217423655 :     if ( useCachedValue )
    2526             :     {
    2527             :         float *shVec;
    2528   165964735 :         shVec = gainCache->shVec;
    2529             : 
    2530  2821400495 :         for ( k = 0; k < HRTF_SH_CHANNELS; k++ )
    2531             :         {
    2532  2655435760 :             *lRealp += hrtfShCoeffsReInt[0][k][bin] * shVec[k];
    2533  2655435760 :             *lImagp += hrtfShCoeffsImInt[0][k][bin] * shVec[k];
    2534  2655435760 :             *rRealp += hrtfShCoeffsReInt[1][k][bin] * shVec[k];
    2535  2655435760 :             *rImagp += hrtfShCoeffsImInt[1][k][bin] * shVec[k];
    2536             :         }
    2537             :     }
    2538             :     else
    2539             :     {
    2540             :         float shVec[HRTF_SH_CHANNELS];
    2541             : 
    2542    51458920 :         ivas_dirac_dec_get_response( aziDeg,
    2543             :                                      eleDeg,
    2544             :                                      shVec,
    2545             :                                      HRTF_SH_ORDER );
    2546             : 
    2547   874801640 :         for ( k = 0; k < HRTF_SH_CHANNELS; k++ )
    2548             :         {
    2549   823342720 :             *lRealp += hrtfShCoeffsReInt[0][k][bin] * shVec[k];
    2550   823342720 :             *lImagp += hrtfShCoeffsImInt[0][k][bin] * shVec[k];
    2551   823342720 :             *rRealp += hrtfShCoeffsReInt[1][k][bin] * shVec[k];
    2552   823342720 :             *rImagp += hrtfShCoeffsImInt[1][k][bin] * shVec[k];
    2553             : 
    2554   823342720 :             gainCache->shVec[k] = shVec[k];
    2555             :         }
    2556             :     }
    2557             : 
    2558   217423655 :     return;
    2559             : }
    2560             : 
    2561             : 
    2562             : /*-------------------------------------------------------------------------
    2563             :  * configure_reqularization_factor()
    2564             :  *
    2565             :  * Configure regularization factor for the mixing matrix generation of the
    2566             :  * parametric binauralizer using IVAS codec format and current bitrate.
    2567             :  *------------------------------------------------------------------------*/
    2568             : 
    2569             : /*! r: Configured reqularization factor value */
    2570       16029 : float configure_reqularization_factor(
    2571             :     const IVAS_FORMAT ivas_format, /* i  : IVAS format          */
    2572             :     const int32_t ivas_total_brate /* i  : IVAS total bitrate  */
    2573             : )
    2574             : {
    2575             :     float reqularizationFactor;
    2576       16029 :     reqularizationFactor = 1.0f; /* Default value */
    2577             : 
    2578       16029 :     if ( ivas_format == MASA_FORMAT )
    2579             :     {
    2580        6384 :         if ( ivas_total_brate >= IVAS_160k )
    2581             :         {
    2582         229 :             reqularizationFactor = 0.4f;
    2583             :         }
    2584        6155 :         else if ( ivas_total_brate == IVAS_128k )
    2585             :         {
    2586         376 :             reqularizationFactor = 0.5f;
    2587             :         }
    2588        5779 :         else if ( ivas_total_brate == IVAS_96k )
    2589             :         {
    2590         112 :             reqularizationFactor = 0.6f;
    2591             :         }
    2592        5667 :         else if ( ivas_total_brate >= IVAS_64k )
    2593             :         {
    2594         235 :             reqularizationFactor = 0.8f;
    2595             :         }
    2596             :         else
    2597             :         {
    2598        5432 :             reqularizationFactor = 1.0f;
    2599             :         }
    2600             :     }
    2601             : 
    2602       16029 :     if ( ivas_format == MC_FORMAT ) /* This is always McMASA for parametric binauralizer. */
    2603             :     {
    2604         362 :         if ( ivas_total_brate >= IVAS_96k )
    2605             :         {
    2606           2 :             reqularizationFactor = 0.4f;
    2607             :         }
    2608         360 :         else if ( ivas_total_brate >= IVAS_80k )
    2609             :         {
    2610           4 :             reqularizationFactor = 0.5f;
    2611             :         }
    2612         356 :         else if ( ivas_total_brate >= IVAS_64k )
    2613             :         {
    2614          10 :             reqularizationFactor = 0.7f;
    2615             :         }
    2616         346 :         else if ( ivas_total_brate >= IVAS_48k )
    2617             :         {
    2618          15 :             reqularizationFactor = 0.8f;
    2619             :         }
    2620             :         else
    2621             :         {
    2622         331 :             reqularizationFactor = 1.0f;
    2623             :         }
    2624             :     }
    2625             : 
    2626             :     /* For SBA and parametric ISM, currently in default value of 1.0f. */
    2627             : 
    2628       16029 :     return reqularizationFactor;
    2629             : }
    2630             : 
    2631             : 
    2632             : /*-------------------------------------------------------------------*
    2633             :  * ivas_omasa_preProcessStereoTransportsForEditedObjects()
    2634             :  *
    2635             :  *
    2636             :  *-------------------------------------------------------------------*/
    2637             : 
    2638      106139 : void ivas_omasa_preProcessStereoTransportsForEditedObjects(
    2639             :     Decoder_Struct *st_ivas,
    2640             :     float inRe[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    2641             :     float inIm[][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX],
    2642             :     const int16_t nBins,
    2643             :     const int16_t subframe )
    2644             : {
    2645             :     int16_t bin, ch, inCh, outCh, ismDirIndex, slot, band_idx, bin_lo, bin_hi, max_band, n_ism;
    2646             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    2647             :     MASA_ISM_DATA_HANDLE hMasaIsmData;
    2648             :     PARAM_ISM_DEC_HANDLE hParamIsmDec;
    2649             :     uint8_t enableCentering;
    2650             :     int16_t dirac_read_idx;
    2651             :     int16_t nSlots;
    2652             :     float panGainsOut[4][2];
    2653             :     float panGainsIn[4][2];
    2654             :     float panEnesOut[4][2];
    2655             :     float panEnesIn[4][2];
    2656             :     uint8_t ismGainEdited[4];
    2657             :     uint8_t ismDirEdited[4];
    2658             :     uint8_t masaGainEdited;
    2659             :     uint8_t masaIsmMode;
    2660             : 
    2661      106139 :     hSpatParamRendCom = st_ivas->hSpatParamRendCom;
    2662      106139 :     hMasaIsmData = st_ivas->hMasaIsmData;
    2663      106139 :     hParamIsmDec = st_ivas->hParamIsmDec;
    2664             : 
    2665      106139 :     if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    2666             :     {
    2667       60327 :         masaIsmMode = 1u;
    2668             :     }
    2669             :     else
    2670             :     {
    2671       45812 :         masaIsmMode = 0u;
    2672             :     }
    2673             : 
    2674      106139 :     if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_FOA || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_HOA2 || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_HOA3 )
    2675             :     {
    2676        6040 :         enableCentering = 0;
    2677             :     }
    2678             :     else
    2679             :     {
    2680      100099 :         enableCentering = 1;
    2681             :     }
    2682             : 
    2683      106139 :     if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC || st_ivas->ivas_format == ISM_FORMAT )
    2684             :     {
    2685       59076 :         n_ism = st_ivas->nchan_ism;
    2686             :     }
    2687             :     else
    2688             :     {
    2689       47063 :         n_ism = hSpatParamRendCom->numIsmDirections;
    2690             :     }
    2691             : 
    2692      486223 :     for ( ismDirIndex = 0; ismDirIndex < n_ism; ismDirIndex++ )
    2693             :     {
    2694      380084 :         ismGainEdited[ismDirIndex] = hMasaIsmData->ism_gain_is_edited[ismDirIndex];
    2695      380084 :         ismDirEdited[ismDirIndex] = hMasaIsmData->ism_dir_is_edited[ismDirIndex];
    2696             :     }
    2697      106139 :     masaGainEdited = hMasaIsmData->masa_gain_is_edited;
    2698             : 
    2699             :     /* Bypass processing until first object is moved or gained */
    2700      106139 :     if ( hMasaIsmData->objectsEdited == 0 )
    2701             :     {
    2702      399427 :         for ( ismDirIndex = 0; ismDirIndex < n_ism; ismDirIndex++ )
    2703             :         {
    2704      311178 :             if ( ismDirEdited[ismDirIndex] )
    2705             :             {
    2706         609 :                 hMasaIsmData->objectsEdited = 1;
    2707             :             }
    2708             : 
    2709      311178 :             if ( ismGainEdited[ismDirIndex] )
    2710             :             {
    2711         574 :                 hMasaIsmData->objectsEdited = 1;
    2712             :             }
    2713             :         }
    2714             : 
    2715       88249 :         if ( masaGainEdited )
    2716             :         {
    2717           2 :             hMasaIsmData->objectsEdited = 1;
    2718             :         }
    2719             : 
    2720       88249 :         if ( hMasaIsmData->objectsEdited == 0 )
    2721             :         {
    2722             :             /* No objects have moved so far */
    2723       88096 :             return;
    2724             :         }
    2725             :     }
    2726             : 
    2727             :     /* OMASA gaining for discrete OMASA mode with stereo_param/bin_room_param renderer */
    2728       18043 :     if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || st_ivas->renderer_type == RENDERER_STEREO_PARAMETRIC ) && st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    2729             :     {
    2730             :         /* ISM gaining */
    2731           0 :         for ( ch = 0; ch < n_ism; ch++ )
    2732             :         {
    2733           0 :             if ( st_ivas->hMasaIsmData->ism_gain_is_edited[ch] )
    2734             :             {
    2735           0 :                 for ( slot = 0; slot < hSpatParamRendCom->subframe_nbslots[subframe]; slot++ )
    2736             :                 {
    2737             :                     float gain;
    2738           0 :                     gain = st_ivas->hMasaIsmData->gain_ism_edited[ch];
    2739           0 :                     v_multc( inRe[ch + 2][slot], gain, inRe[ch + 2][slot], nBins );
    2740           0 :                     v_multc( inIm[ch + 2][slot], gain, inIm[ch + 2][slot], nBins );
    2741             :                 }
    2742             :             }
    2743             :         }
    2744             : 
    2745             :         /* MASA gaining */
    2746           0 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    2747             :         {
    2748           0 :             if ( masaGainEdited )
    2749             :             {
    2750           0 :                 for ( slot = 0; slot < hSpatParamRendCom->subframe_nbslots[subframe]; slot++ )
    2751             :                 {
    2752             :                     float gain_masa;
    2753           0 :                     gain_masa = st_ivas->hMasaIsmData->gain_masa_edited;
    2754           0 :                     v_multc( inRe[ch][slot], gain_masa, inRe[ch][slot], nBins );
    2755           0 :                     v_multc( inIm[ch][slot], gain_masa, inIm[ch][slot], nBins );
    2756             :                 }
    2757             :             }
    2758             :         }
    2759             :     }
    2760       18043 :     else if ( st_ivas->ivas_format == ISM_FORMAT && st_ivas->hISMDTX.dtx_flag ) /* If dtx on, perform just the smoothing of the processing gains */
    2761           0 :     {
    2762             :         float totalTargetEne;
    2763             :         float ismPreprocMtxNew[2][2];
    2764             :         float ismPreprocMtxIncrement[2][2];
    2765             :         float nSlotDiv;
    2766             :         float outSlotRe[2][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    2767             :         float outSlotIm[2][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    2768             :         float eqVal;
    2769             : 
    2770           0 :         nSlots = hSpatParamRendCom->subframe_nbslots[subframe];
    2771           0 :         nSlotDiv = 1.0f / ( (float) nSlots );
    2772             : 
    2773             :         /* Use diagonal mixing matrix as the instant mixing matrix, to slowly fade away the editing during dtx */
    2774           0 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    2775             :         {
    2776           0 :             ismPreprocMtxNew[ch][ch] = 1.0f;
    2777           0 :             ismPreprocMtxNew[1 - ch][ch] = 0.0f;
    2778             :         }
    2779             : 
    2780             :         /* Determine the highest band */
    2781           0 :         max_band = 0;
    2782           0 :         while ( max_band < MAX_PARAM_ISM_NBANDS && hParamIsmDec->hParamIsm->band_grouping[max_band] < nBins )
    2783             :         {
    2784           0 :             max_band++;
    2785             :         }
    2786             : 
    2787             :         /* Init out array */
    2788           0 :         for ( slot = 0; slot < nSlots; slot++ )
    2789             :         {
    2790           0 :             for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    2791             :             {
    2792           0 :                 set_zero( outSlotRe[ch][slot], CLDFB_NO_CHANNELS_MAX );
    2793           0 :                 set_zero( outSlotIm[ch][slot], CLDFB_NO_CHANNELS_MAX );
    2794             :             }
    2795             :         }
    2796             : 
    2797             :         /* Perform the processing in frequency bands */
    2798           0 :         for ( band_idx = 0; band_idx < max_band; band_idx++ )
    2799             :         {
    2800           0 :             bin_lo = hParamIsmDec->hParamIsm->band_grouping[band_idx];
    2801           0 :             bin_hi = min( hParamIsmDec->hParamIsm->band_grouping[band_idx + 1], nBins );
    2802             : 
    2803             :             /* Determine transport energies */
    2804           0 :             totalTargetEne = 0.0f;
    2805           0 :             for ( slot = 0; slot < nSlots; slot++ )
    2806             :             {
    2807           0 :                 for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    2808             :                 {
    2809           0 :                     for ( bin = bin_lo; bin < bin_hi; bin++ )
    2810             :                     {
    2811           0 :                         totalTargetEne += inRe[ch][slot][bin] * inRe[ch][slot][bin];
    2812           0 :                         totalTargetEne += inIm[ch][slot][bin] * inIm[ch][slot][bin];
    2813             :                     }
    2814             :                 }
    2815             :             }
    2816             : 
    2817             :             /* Get increment value for temporal interpolation */
    2818           0 :             for ( inCh = 0; inCh < BINAURAL_CHANNELS; inCh++ )
    2819             :             {
    2820           0 :                 for ( outCh = 0; outCh < BINAURAL_CHANNELS; outCh++ )
    2821             :                 {
    2822           0 :                     ismPreprocMtxIncrement[outCh][inCh] = ( ismPreprocMtxNew[outCh][inCh] - hMasaIsmData->ismPreprocMatrix[outCh][inCh][band_idx] ) * nSlotDiv;
    2823             :                 }
    2824             :             }
    2825             : 
    2826             :             /* Mix signals */
    2827           0 :             hMasaIsmData->preprocEneTarget[band_idx] *= STEREO_PREPROCESS_IIR_FACTOR;
    2828           0 :             hMasaIsmData->preprocEneRealized[band_idx] *= STEREO_PREPROCESS_IIR_FACTOR;
    2829           0 :             hMasaIsmData->preprocEneTarget[band_idx] += totalTargetEne;
    2830             : 
    2831           0 :             for ( outCh = 0; outCh < BINAURAL_CHANNELS; outCh++ )
    2832             :             {
    2833           0 :                 for ( slot = 0; slot < nSlots; slot++ )
    2834             :                 {
    2835           0 :                     for ( inCh = 0; inCh < BINAURAL_CHANNELS; inCh++ )
    2836             :                     {
    2837           0 :                         hMasaIsmData->ismPreprocMatrix[outCh][inCh][band_idx] += ismPreprocMtxIncrement[outCh][inCh];
    2838           0 :                         for ( bin = bin_lo; bin < bin_hi; bin++ )
    2839             :                         {
    2840           0 :                             outSlotRe[outCh][slot][bin] += inRe[inCh][slot][bin] * hMasaIsmData->ismPreprocMatrix[outCh][inCh][band_idx];
    2841           0 :                             outSlotIm[outCh][slot][bin] += inIm[inCh][slot][bin] * hMasaIsmData->ismPreprocMatrix[outCh][inCh][band_idx];
    2842             :                         }
    2843             :                     }
    2844             : 
    2845           0 :                     for ( bin = bin_lo; bin < bin_hi; bin++ )
    2846             :                     {
    2847           0 :                         hMasaIsmData->preprocEneRealized[band_idx] += ( outSlotRe[outCh][slot][bin] * outSlotRe[outCh][slot][bin] ) + ( outSlotIm[outCh][slot][bin] * outSlotIm[outCh][slot][bin] );
    2848             :                     }
    2849             :                 }
    2850             :             }
    2851             : 
    2852           0 :             eqVal = fminf( 4.0f, sqrtf( hMasaIsmData->preprocEneTarget[band_idx] / fmaxf( 1e-12f, hMasaIsmData->preprocEneRealized[band_idx] ) ) );
    2853             : 
    2854           0 :             for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    2855             :             {
    2856           0 :                 for ( slot = 0; slot < nSlots; slot++ )
    2857             :                 {
    2858           0 :                     for ( bin = bin_lo; bin < bin_hi; bin++ )
    2859             :                     {
    2860           0 :                         inRe[ch][slot][bin] = outSlotRe[ch][slot][bin] * eqVal;
    2861           0 :                         inIm[ch][slot][bin] = outSlotIm[ch][slot][bin] * eqVal;
    2862             :                     }
    2863             :                 }
    2864             :             }
    2865             :         }
    2866             :     }
    2867             :     else /* Other processing modes */
    2868             :     {
    2869             :         float subframeEne;
    2870             :         float subframeEneCh[2];
    2871             :         float normEnes[2];
    2872             :         float ratioAccOrig;
    2873             :         float ratioAccNew;
    2874             :         float ratio;
    2875             :         float ismEneThis;
    2876             :         float ismTargetEneThis;
    2877             :         float ismTargetEneThisCh[2];
    2878             :         float totalTargetEneCh[2];
    2879             :         float totalTargetEne;
    2880             :         float masaTargetEneThisCh[2];
    2881             :         float ismPreprocMtxNew[2][2];
    2882             :         float ismPreprocMtxIncrement[2][2];
    2883             :         float eneMove[2];
    2884             :         float enePreserve[2];
    2885             :         float ismRatioAcc;
    2886             :         float remainderNormEne;
    2887             :         float centeringFactor;
    2888             :         float eneMoveThis;
    2889             :         float enePreserveThis;
    2890             :         float normVal;
    2891             :         float eqVal;
    2892             :         float outSlotRe[2][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    2893             :         float outSlotIm[2][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    2894             :         float newRatios[6];
    2895             :         float gainIsmThis;
    2896             :         float gainMasaPow2;
    2897             :         float nSlotDiv;
    2898             :         float tempDivisor;
    2899             :         float masaEneThisCh[2];
    2900             :         float ratioAccNewDivisor;
    2901             : 
    2902       18043 :         gainMasaPow2 = 1.0f;
    2903       18043 :         if ( masaGainEdited )
    2904             :         {
    2905        5264 :             gainMasaPow2 = hMasaIsmData->gain_masa_edited;
    2906        5264 :             gainMasaPow2 *= gainMasaPow2;
    2907             :         }
    2908             : 
    2909       18043 :         nSlots = hSpatParamRendCom->subframe_nbslots[subframe];
    2910       18043 :         dirac_read_idx = hSpatParamRendCom->render_to_md_map[subframe];
    2911       18043 :         nSlotDiv = 1.0f / ( (float) nSlots );
    2912             : 
    2913             :         /* Determine panning gains and energies for each object */
    2914       87559 :         for ( ismDirIndex = 0; ismDirIndex < n_ism; ismDirIndex++ )
    2915             :         {
    2916             :             /* Get input and output panning gains */
    2917       69516 :             ivas_get_stereo_panning_gains( hMasaIsmData->azimuth_ism[ismDirIndex][dirac_read_idx],
    2918       69516 :                                            hMasaIsmData->elevation_ism[ismDirIndex][dirac_read_idx],
    2919       69516 :                                            panGainsIn[ismDirIndex] );
    2920             : 
    2921       69516 :             if ( ismDirEdited[ismDirIndex] )
    2922             :             {
    2923       68684 :                 ivas_get_stereo_panning_gains( hMasaIsmData->azimuth_ism_edited[ismDirIndex],
    2924       68684 :                                                hMasaIsmData->elevation_ism_edited[ismDirIndex],
    2925       68684 :                                                panGainsOut[ismDirIndex] );
    2926             :             }
    2927             :             else
    2928             :             {
    2929             :                 /* When not edited, input and output pan gains are the same */
    2930        2496 :                 for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    2931             :                 {
    2932        1664 :                     panGainsOut[ismDirIndex][ch] = panGainsIn[ismDirIndex][ch];
    2933             :                 }
    2934             :             }
    2935             : 
    2936             :             /* Determine pan enes */
    2937      208548 :             for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    2938             :             {
    2939      139032 :                 panEnesOut[ismDirIndex][ch] = panGainsOut[ismDirIndex][ch] * panGainsOut[ismDirIndex][ch];
    2940      139032 :                 panEnesIn[ismDirIndex][ch] = panGainsIn[ismDirIndex][ch] * panGainsIn[ismDirIndex][ch];
    2941             :             }
    2942             :         }
    2943             : 
    2944             :         /* Determine the highest band */
    2945       18043 :         max_band = 0;
    2946       18043 :         if ( masaIsmMode )
    2947             :         {
    2948      133200 :             while ( max_band < MASA_FREQUENCY_BANDS && MASA_band_grouping_24[max_band] < nBins )
    2949             :             {
    2950      127872 :                 max_band++;
    2951             :             }
    2952             :         }
    2953             :         else
    2954             :         {
    2955      152580 :             while ( max_band < MAX_PARAM_ISM_NBANDS && hParamIsmDec->hParamIsm->band_grouping[max_band] < nBins )
    2956             :             {
    2957      139865 :                 max_band++;
    2958             :             }
    2959             :         }
    2960             : 
    2961             :         /* Init out array */
    2962       87339 :         for ( slot = 0; slot < nSlots; slot++ )
    2963             :         {
    2964      207888 :             for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    2965             :             {
    2966      138592 :                 set_zero( outSlotRe[ch][slot], CLDFB_NO_CHANNELS_MAX );
    2967      138592 :                 set_zero( outSlotIm[ch][slot], CLDFB_NO_CHANNELS_MAX );
    2968             :             }
    2969             :         }
    2970             : 
    2971             :         /* Perform the processing in frequency bands */
    2972      285780 :         for ( band_idx = 0; band_idx < max_band; band_idx++ )
    2973             :         {
    2974      267737 :             ratioAccOrig = 0.0f;
    2975      267737 :             ratioAccNew = 0.0f;
    2976      267737 :             ismRatioAcc = 0.0f;
    2977             : 
    2978      267737 :             set_zero( subframeEneCh, 2 );
    2979      267737 :             set_zero( ismPreprocMtxNew[0], 2 );
    2980      267737 :             set_zero( ismPreprocMtxNew[1], 2 );
    2981      267737 :             set_zero( eneMove, 2 );
    2982      267737 :             set_zero( enePreserve, 2 );
    2983      267737 :             if ( masaIsmMode )
    2984             :             {
    2985      127872 :                 bin_lo = MASA_band_grouping_24[band_idx];
    2986      127872 :                 bin_hi = min( MASA_band_grouping_24[band_idx + 1], nBins );
    2987             :             }
    2988             :             else
    2989             :             {
    2990      139865 :                 bin_lo = hParamIsmDec->hParamIsm->band_grouping[band_idx];
    2991      139865 :                 bin_hi = min( hParamIsmDec->hParamIsm->band_grouping[band_idx + 1], nBins );
    2992             :             }
    2993             : 
    2994             :             /* Determine transport normalized energies and subframe energy */
    2995     1307049 :             for ( slot = 0; slot < nSlots; slot++ )
    2996             :             {
    2997     3117936 :                 for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    2998             :                 {
    2999    10394144 :                     for ( bin = bin_lo; bin < bin_hi; bin++ )
    3000             :                     {
    3001     8315520 :                         subframeEneCh[ch] += inRe[ch][slot][bin] * inRe[ch][slot][bin];
    3002     8315520 :                         subframeEneCh[ch] += inIm[ch][slot][bin] * inIm[ch][slot][bin];
    3003             :                     }
    3004             :                 }
    3005             :             }
    3006      267737 :             subframeEne = subframeEneCh[0] + subframeEneCh[1];
    3007      267737 :             totalTargetEneCh[0] = subframeEneCh[0];
    3008      267737 :             totalTargetEneCh[1] = subframeEneCh[1];
    3009      267737 :             masaEneThisCh[0] = subframeEneCh[0];
    3010      267737 :             masaEneThisCh[1] = subframeEneCh[1];
    3011             : 
    3012             :             /* Gain editing */
    3013             :             /* For each object, estimate new target energy per channel based on the applied gain */
    3014     1274941 :             for ( ismDirIndex = 0; ismDirIndex < n_ism; ismDirIndex++ )
    3015             :             {
    3016     1007204 :                 ratio = hMasaIsmData->energy_ratio_ism[ismDirIndex][dirac_read_idx][bin_lo];
    3017     1007204 :                 ratioAccOrig += ratio;
    3018             : 
    3019             :                 /* Calculate MASA energy as a residual of original channel energies subtracted with ISM energies */
    3020     3021612 :                 for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3021             :                 {
    3022     2014408 :                     masaEneThisCh[ch] -= panEnesIn[ismDirIndex][ch] * ratio * subframeEne;
    3023             :                 }
    3024             : 
    3025             :                 /* Calculate target energy, gained ratio for accumulation, and transports gains, if ism gain is edited */
    3026     1007204 :                 if ( ismGainEdited[ismDirIndex] )
    3027             :                 {
    3028      929222 :                     gainIsmThis = hMasaIsmData->gain_ism_edited[ismDirIndex];
    3029             :                     /* ISM original energy */
    3030      929222 :                     ismEneThis = ratio * subframeEne;
    3031             : 
    3032             :                     /* ISM target energy */
    3033      929222 :                     ismTargetEneThis = ( gainIsmThis * gainIsmThis ) * ismEneThis;
    3034             : 
    3035      929222 :                     ratio *= gainIsmThis * gainIsmThis;
    3036             : 
    3037             :                     /* Determine panning energies and channel target energies */
    3038     2787666 :                     for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3039             :                     {
    3040     1858444 :                         ismTargetEneThisCh[ch] = panEnesIn[ismDirIndex][ch] * ismTargetEneThis; /* Ism target energy per channel */
    3041     1858444 :                         totalTargetEneCh[ch] -= panEnesIn[ismDirIndex][ch] * ismEneThis;        /* Reduce original ism energy */
    3042     1858444 :                         totalTargetEneCh[ch] += ismTargetEneThisCh[ch];                         /* Add ism target energy per channel */
    3043             :                     }
    3044             : 
    3045             :                     /* If separated ism edited, apply gain directly to the separated ism */
    3046      929222 :                     if ( ismDirIndex == hMasaIsmData->idx_separated_ism )
    3047             :                     {
    3048             :                         /* Separated object gaining is done elsewhere with DIRAC renderer */
    3049      123168 :                         if ( st_ivas->renderer_type != RENDERER_DIRAC )
    3050             :                         {
    3051             :                             /* Gain transport channel of separated ism */
    3052      615840 :                             for ( slot = 0; slot < nSlots; slot++ )
    3053             :                             {
    3054     1724352 :                                 for ( bin = bin_lo; bin < bin_hi; bin++ )
    3055             :                                 {
    3056     1231680 :                                     inRe[2][slot][bin] = gainIsmThis * inRe[2][slot][bin];
    3057     1231680 :                                     inIm[2][slot][bin] = gainIsmThis * inIm[2][slot][bin];
    3058             :                                 }
    3059             :                             }
    3060             :                         }
    3061             :                     }
    3062             :                 }
    3063     1007204 :                 ratioAccNew += ratio;
    3064             :             }
    3065             : 
    3066      267737 :             if ( masaIsmMode )
    3067             :             {
    3068             :                 /* MASA original ratios */
    3069      127872 :                 ratio = hSpatParamRendCom->energy_ratio1[dirac_read_idx][bin_lo] + hSpatParamRendCom->energy_ratio2[dirac_read_idx][bin_lo];
    3070      127872 :                 ratioAccOrig += ratio;
    3071             : 
    3072             :                 /* Estimate MASA original energies and determine adjusted target energies and gained ratio for accumulation, if a gain is applied to MASA */
    3073      127872 :                 if ( masaGainEdited )
    3074             :                 {
    3075             :                     /* Gained MASA ratio */
    3076      126336 :                     ratio *= gainMasaPow2;
    3077             : 
    3078             :                     /* Calculate MASA target energies and add to total target energy estimation */
    3079      379008 :                     for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3080             :                     {
    3081      252672 :                         masaEneThisCh[ch] = fmaxf( masaEneThisCh[ch], 0.0f );       /* MASA original energy per channel */
    3082      252672 :                         masaTargetEneThisCh[ch] = gainMasaPow2 * masaEneThisCh[ch]; /* MASA target energy per channel */
    3083      252672 :                         totalTargetEneCh[ch] -= masaEneThisCh[ch];                  /* Reduce original energy per channel */
    3084      252672 :                         totalTargetEneCh[ch] += masaTargetEneThisCh[ch];            /* Add target energy per channel */
    3085             :                     }
    3086             :                 }
    3087             : 
    3088      127872 :                 ratioAccNew += ratio;
    3089             :             }
    3090             : 
    3091             :             /* Limit target energies to non-negative values */
    3092      803211 :             for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3093             :             {
    3094      535474 :                 totalTargetEneCh[ch] = max( totalTargetEneCh[ch], 0.0f );
    3095             :             }
    3096             : 
    3097             :             /* due to rounding, the sum may exceed 1.0f ever so slightly, so clip it */
    3098      267737 :             ratioAccOrig = min( ratioAccOrig, 1.0f );
    3099      267737 :             if ( masaGainEdited )
    3100             :             {
    3101      126336 :                 ratioAccNew += gainMasaPow2 * ( 1.0f - ratioAccOrig );
    3102             :             }
    3103             :             else
    3104             :             {
    3105      141401 :                 ratioAccNew += ( 1.0f - ratioAccOrig );
    3106             :             }
    3107             : 
    3108      267737 :             ratioAccNewDivisor = 1.0f / fmaxf( 1e-12f, ratioAccNew ); /* New target total energy ratio divider */
    3109             : 
    3110             :             /* Determine and process object energy ratios based on gaining */
    3111     1274941 :             for ( ismDirIndex = 0; ismDirIndex < n_ism; ismDirIndex++ )
    3112             :             {
    3113     1007204 :                 newRatios[ismDirIndex + 2] = hMasaIsmData->energy_ratio_ism[ismDirIndex][dirac_read_idx][bin_lo]; /* Determine original object energy ratio */
    3114     1007204 :                 if ( ismGainEdited[ismDirIndex] )
    3115             :                 {
    3116      929222 :                     gainIsmThis = hMasaIsmData->gain_ism_edited[ismDirIndex];
    3117      929222 :                     newRatios[ismDirIndex + 2] *= ( gainIsmThis * gainIsmThis ); /* Gain original object energy ratio, if edited */
    3118             :                 }
    3119     1007204 :                 newRatios[ismDirIndex + 2] *= ratioAccNewDivisor; /* Divide with new target total ratio */
    3120             :             }
    3121             : 
    3122             :             /* Determine and process MASA energy ratios based on gaining */
    3123      267737 :             newRatios[0] = hSpatParamRendCom->energy_ratio1[dirac_read_idx][bin_lo];
    3124      267737 :             newRatios[1] = hSpatParamRendCom->energy_ratio2[dirac_read_idx][bin_lo];
    3125      267737 :             if ( masaGainEdited )
    3126             :             {
    3127      126336 :                 newRatios[0] *= gainMasaPow2;
    3128      126336 :                 newRatios[1] *= gainMasaPow2;
    3129             :             }
    3130      267737 :             newRatios[0] *= ratioAccNewDivisor;
    3131      267737 :             newRatios[1] *= ratioAccNewDivisor;
    3132             : 
    3133             :             /* Set adjusted energy ratios */
    3134     1350317 :             for ( bin = bin_lo; bin < bin_hi; bin++ )
    3135             :             {
    3136     5253540 :                 for ( ismDirIndex = 0; ismDirIndex < n_ism; ismDirIndex++ )
    3137             :                 {
    3138     4170960 :                     hMasaIsmData->energy_ratio_ism[ismDirIndex][dirac_read_idx][bin] = newRatios[ismDirIndex + 2];
    3139             :                 }
    3140     1082580 :                 hSpatParamRendCom->energy_ratio1[dirac_read_idx][bin] = newRatios[0];
    3141     1082580 :                 hSpatParamRendCom->energy_ratio2[dirac_read_idx][bin] = newRatios[1];
    3142             :             }
    3143             : 
    3144             :             /* Direction editing */
    3145             :             /* Determine new energetic values after gaining */
    3146      267737 :             totalTargetEne = totalTargetEneCh[0] + totalTargetEneCh[1];
    3147      267737 :             tempDivisor = 1.0f / fmaxf( 1e-12f, totalTargetEne );
    3148      267737 :             normEnes[0] = totalTargetEneCh[0] * tempDivisor;
    3149      267737 :             normEnes[1] = totalTargetEneCh[1] * tempDivisor;
    3150             : 
    3151             :             /* For each ismDir, determine moved and preserve energy ratio per channel */
    3152     1274941 :             for ( ismDirIndex = 0; ismDirIndex < n_ism; ismDirIndex++ )
    3153             :             {
    3154     1007204 :                 ratio = hMasaIsmData->energy_ratio_ism[ismDirIndex][dirac_read_idx][bin_lo];
    3155             : 
    3156     1007204 :                 ismRatioAcc += ratio;
    3157             : 
    3158     1007204 :                 if ( enableCentering )
    3159             :                 {
    3160     1007204 :                     centeringFactor = fmaxf( 0.0f, 2.0f * fabsf( panEnesIn[ismDirIndex][0] - panEnesOut[ismDirIndex][0] ) - 1.0f );
    3161     3021612 :                     for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3162             :                     {
    3163     2014408 :                         panEnesOut[ismDirIndex][ch] *= ( 1.0f - centeringFactor );
    3164     2014408 :                         panEnesOut[ismDirIndex][ch] += 0.5f * centeringFactor;
    3165             :                     }
    3166             :                 }
    3167             : 
    3168     3021612 :                 for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3169             :                 {
    3170     2014408 :                     eneMoveThis = fmaxf( 0.0f, panEnesIn[ismDirIndex][ch] - panEnesOut[ismDirIndex][ch] );
    3171     2014408 :                     enePreserveThis = panEnesIn[ismDirIndex][ch] - eneMoveThis;
    3172             : 
    3173     2014408 :                     eneMove[ch] += ratio * eneMoveThis;
    3174     2014408 :                     enePreserve[ch] += ratio * enePreserveThis;
    3175             : 
    3176             :                     /* Subtract object parts from normEnes */
    3177     2014408 :                     normEnes[ch] -= panEnesIn[ismDirIndex][ch] * ratio;
    3178             :                 }
    3179             :             }
    3180             : 
    3181             :             /* Any remaining (non-object) energy is set to be preserved at both channels */
    3182      267737 :             remainderNormEne = fmaxf( 0.0f, ( 1.0f - ismRatioAcc ) - normEnes[0] - normEnes[1] );
    3183             : 
    3184             :             /* Normalize */
    3185      803211 :             for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3186             :             {
    3187      535474 :                 enePreserve[ch] += fmaxf( 0.0f, normEnes[ch] + remainderNormEne / 2.0f );
    3188      535474 :                 normVal = 1.0f / fmaxf( EPSILON, eneMove[ch] + enePreserve[ch] );
    3189      535474 :                 normVal *= fminf( 10.0f, totalTargetEneCh[ch] / fmaxf( EPSILON, subframeEneCh[ch] ) );
    3190      535474 :                 eneMove[ch] *= normVal;
    3191      535474 :                 enePreserve[ch] *= normVal;
    3192             :             }
    3193             : 
    3194             :             /* Temporally average target energy */
    3195      267737 :             hMasaIsmData->eneOrigIIR[band_idx] *= STEREO_PREPROCESS_IIR_FACTOR;
    3196      267737 :             hMasaIsmData->eneOrigIIR[band_idx] += totalTargetEne;
    3197             : 
    3198             :             /* Temporally average energy moving and preserving, and generate the transport signal preprocessing matrix for
    3199             :              * gaining objects and moving objects between left and right */
    3200      803211 :             for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3201             :             {
    3202      535474 :                 hMasaIsmData->eneMoveIIR[ch][band_idx] *= STEREO_PREPROCESS_IIR_FACTOR;
    3203      535474 :                 hMasaIsmData->eneMoveIIR[ch][band_idx] += eneMove[ch] * totalTargetEne;
    3204      535474 :                 hMasaIsmData->enePreserveIIR[ch][band_idx] *= STEREO_PREPROCESS_IIR_FACTOR;
    3205      535474 :                 hMasaIsmData->enePreserveIIR[ch][band_idx] += enePreserve[ch] * totalTargetEne;
    3206      535474 :                 normVal = 1.0f / fmaxf( EPSILON, hMasaIsmData->eneOrigIIR[band_idx] );
    3207      535474 :                 ismPreprocMtxNew[ch][ch] = fminf( 4.0f, sqrtf( hMasaIsmData->enePreserveIIR[ch][band_idx] * normVal ) );
    3208      535474 :                 ismPreprocMtxNew[1 - ch][ch] = fminf( 4.0f, sqrtf( hMasaIsmData->eneMoveIIR[ch][band_idx] * normVal ) );
    3209             :             }
    3210             : 
    3211             :             /* Get increment value for temporal interpolation */
    3212      803211 :             for ( inCh = 0; inCh < BINAURAL_CHANNELS; inCh++ )
    3213             :             {
    3214     1606422 :                 for ( outCh = 0; outCh < BINAURAL_CHANNELS; outCh++ )
    3215             :                 {
    3216     1070948 :                     ismPreprocMtxIncrement[outCh][inCh] = ( ismPreprocMtxNew[outCh][inCh] - hMasaIsmData->ismPreprocMatrix[outCh][inCh][band_idx] ) * nSlotDiv;
    3217             :                 }
    3218             :             }
    3219             : 
    3220             :             /* Mix signals */
    3221      267737 :             hMasaIsmData->preprocEneTarget[band_idx] *= STEREO_PREPROCESS_IIR_FACTOR;
    3222      267737 :             hMasaIsmData->preprocEneRealized[band_idx] *= STEREO_PREPROCESS_IIR_FACTOR;
    3223      267737 :             hMasaIsmData->preprocEneTarget[band_idx] += totalTargetEne;
    3224             : 
    3225      803211 :             for ( outCh = 0; outCh < BINAURAL_CHANNELS; outCh++ )
    3226             :             {
    3227     2614098 :                 for ( slot = 0; slot < nSlots; slot++ )
    3228             :                 {
    3229     6235872 :                     for ( inCh = 0; inCh < BINAURAL_CHANNELS; inCh++ )
    3230             :                     {
    3231     4157248 :                         hMasaIsmData->ismPreprocMatrix[outCh][inCh][band_idx] += ismPreprocMtxIncrement[outCh][inCh];
    3232    20788288 :                         for ( bin = bin_lo; bin < bin_hi; bin++ )
    3233             :                         {
    3234    16631040 :                             outSlotRe[outCh][slot][bin] += inRe[inCh][slot][bin] * hMasaIsmData->ismPreprocMatrix[outCh][inCh][band_idx];
    3235    16631040 :                             outSlotIm[outCh][slot][bin] += inIm[inCh][slot][bin] * hMasaIsmData->ismPreprocMatrix[outCh][inCh][band_idx];
    3236             :                         }
    3237             :                     }
    3238             : 
    3239    10394144 :                     for ( bin = bin_lo; bin < bin_hi; bin++ )
    3240             :                     {
    3241     8315520 :                         hMasaIsmData->preprocEneRealized[band_idx] += ( outSlotRe[outCh][slot][bin] * outSlotRe[outCh][slot][bin] ) + ( outSlotIm[outCh][slot][bin] * outSlotIm[outCh][slot][bin] );
    3242             :                     }
    3243             :                 }
    3244             :             }
    3245             : 
    3246      267737 :             eqVal = fminf( 4.0f, sqrtf( hMasaIsmData->preprocEneTarget[band_idx] / fmaxf( 1e-12f, hMasaIsmData->preprocEneRealized[band_idx] ) ) );
    3247             : 
    3248      803211 :             for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3249             :             {
    3250     2614098 :                 for ( slot = 0; slot < nSlots; slot++ )
    3251             :                 {
    3252    10394144 :                     for ( bin = bin_lo; bin < bin_hi; bin++ )
    3253             :                     {
    3254     8315520 :                         inRe[ch][slot][bin] = outSlotRe[ch][slot][bin] * eqVal;
    3255     8315520 :                         inIm[ch][slot][bin] = outSlotIm[ch][slot][bin] * eqVal;
    3256             :                     }
    3257             :                 }
    3258             :             }
    3259             :         }
    3260             : 
    3261       18043 :         if ( !masaIsmMode )
    3262             :         {
    3263             :             int16_t obj_idx1, obj_idx2;
    3264             : 
    3265      152580 :             for ( band_idx = 0; band_idx < hParamIsmDec->hParamIsm->nbands; band_idx++ )
    3266             :             {
    3267      139865 :                 bin_lo = hParamIsmDec->hParamIsm->band_grouping[band_idx];
    3268      139865 :                 bin_hi = hParamIsmDec->hParamIsm->band_grouping[band_idx + 1];
    3269             : 
    3270      139865 :                 obj_idx1 = hParamIsmDec->hParamIsm->obj_indices[band_idx][0][0];
    3271      139865 :                 obj_idx2 = hParamIsmDec->hParamIsm->obj_indices[band_idx][0][1];
    3272             : 
    3273      902765 :                 for ( bin = bin_lo; bin < bin_hi; bin++ )
    3274             :                 {
    3275      762900 :                     hSpatParamRendCom->energy_ratio1[dirac_read_idx][bin] = hMasaIsmData->energy_ratio_ism[obj_idx1][dirac_read_idx][bin];
    3276      762900 :                     hSpatParamRendCom->energy_ratio2[dirac_read_idx][bin] = hMasaIsmData->energy_ratio_ism[obj_idx2][dirac_read_idx][bin];
    3277             :                 }
    3278             :             }
    3279             :         }
    3280             :     }
    3281             : 
    3282       18043 :     return;
    3283             : }
    3284             : 
    3285             : 
    3286      122353 : static void ivas_masa_ext_rend_parambin_internal(
    3287             :     MASA_EXT_REND_HANDLE hMasaExtRend,
    3288             :     COMBINED_ORIENTATION_HANDLE hCombinedOrientationData,
    3289             :     float *output_f[],
    3290             :     const int16_t subframe,
    3291             :     const SPLIT_REND_WRAPPER *hSplitRendWrapper,
    3292             :     float Cldfb_Out_Real[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX],
    3293             :     float Cldfb_Out_Imag[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX] )
    3294             : {
    3295             :     DIRAC_DEC_BIN_HANDLE hDiracDecBin;
    3296             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    3297             :     PARAMBIN_REND_CONFIG config_data;
    3298             :     int16_t slot, ch, numInChannels;
    3299             :     float Cldfb_RealBuffer_in[6][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    3300             :     float Cldfb_ImagBuffer_in[6][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    3301             :     float Rmat[3][3];
    3302             :     int16_t max_band_decorr;
    3303             :     int16_t nBins;
    3304             :     int16_t i, j;
    3305             :     int16_t nchan_transport;
    3306             : 
    3307             :     int16_t pos_idx;
    3308             :     const MULTI_BIN_REND_POSE_DATA *pMultiBinPoseData;
    3309             :     float tmp_Cldfb_out_re[BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    3310             :     float tmp_Cldfb_out_im[BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    3311             : 
    3312             :     /* these allow re-using the reverb and freq-domain decorrelator signals from ivas_dirac_dec_binaural_process_output() in split rendering for the side renderings */
    3313             :     float reverbRe[BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    3314             :     float reverbIm[BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    3315             :     float decorrRe[BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    3316             :     float decorrIm[BINAURAL_CHANNELS][CLDFB_SLOTS_PER_SUBFRAME][CLDFB_NO_CHANNELS_MAX];
    3317             :     float subFrameTotalEne[CLDFB_NO_CHANNELS_MAX];
    3318             :     float IIReneLimiter[CLDFB_NO_CHANNELS_MAX];
    3319             : 
    3320      122353 :     hDiracDecBin = hMasaExtRend->hDiracDecBin[0];
    3321      122353 :     assert( hDiracDecBin );
    3322      122353 :     hSpatParamRendCom = hMasaExtRend->hSpatParamRendCom;
    3323      122353 :     nBins = hSpatParamRendCom->num_freq_bands;
    3324             : 
    3325             :     /* Setup internal config. MASA EXT renderer is quite strict. */
    3326      122353 :     config_data.separateCenterChannelRendering = 0;
    3327      122353 :     config_data.ivas_format = MASA_FORMAT;
    3328      122353 :     config_data.mc_mode = MC_MODE_NONE;
    3329      122353 :     config_data.ivas_total_brate = IVAS_512k; /* Maximum bitrate set for external renderer */
    3330      122353 :     config_data.nchan_transport = hMasaExtRend->nchan_input;
    3331      122353 :     config_data.qualityBasedSmFactor = 1.0f;
    3332      122353 :     config_data.processReverb = hMasaExtRend->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ? 1 : 0;
    3333      122353 :     config_data.ism_mode = ISM_MODE_NONE;
    3334             : 
    3335             :     /* Set nchan_transport to number of transport channels in MASA input */
    3336      122353 :     nchan_transport = hMasaExtRend->nchan_input;
    3337             : 
    3338             :     /* The input channel number at this processing function (not nchan_transport) */
    3339      122353 :     numInChannels = BINAURAL_CHANNELS;
    3340             : 
    3341      122353 :     Rmat[0][0] = 1.0f;
    3342      122353 :     Rmat[0][1] = 0.0f;
    3343      122353 :     Rmat[0][2] = 0.0f;
    3344             : 
    3345      122353 :     Rmat[1][0] = 0.0f;
    3346      122353 :     Rmat[1][1] = 1.0f;
    3347      122353 :     Rmat[1][2] = 0.0f;
    3348             : 
    3349      122353 :     Rmat[2][0] = 0.0f;
    3350      122353 :     Rmat[2][1] = 0.0f;
    3351      122353 :     Rmat[2][2] = 1.0f;
    3352             : 
    3353             :     /* CLDFB Analysis of input */
    3354      611765 :     for ( slot = 0; slot < hSpatParamRendCom->subframe_nbslots[subframe]; slot++ )
    3355             :     {
    3356     1468236 :         for ( ch = 0; ch < numInChannels; ch++ )
    3357             :         {
    3358      978824 :             if ( ch == 0 || nchan_transport == 2 )
    3359             :             {
    3360      778688 :                 cldfbAnalysis_ts(
    3361      778688 :                     &( output_f[ch][nBins * slot] ),
    3362      778688 :                     Cldfb_RealBuffer_in[ch][slot],
    3363      778688 :                     Cldfb_ImagBuffer_in[ch][slot],
    3364             :                     nBins, hMasaExtRend->cldfbAnaRend[ch] );
    3365             :             }
    3366             :             else /* when nchan_transport == 1 and ch == 1 */
    3367             :             {
    3368             :                 /* At mono input duplicate the channel to dual-mono, and apply gain
    3369             :                 correction to ensure same overall level as in stereo mode  */
    3370      200136 :                 v_multc( Cldfb_RealBuffer_in[0][slot], INV_SQRT_2, Cldfb_RealBuffer_in[0][slot], nBins );
    3371      200136 :                 v_multc( Cldfb_ImagBuffer_in[0][slot], INV_SQRT_2, Cldfb_ImagBuffer_in[0][slot], nBins );
    3372             : 
    3373      200136 :                 mvr2r( Cldfb_RealBuffer_in[0][slot], Cldfb_RealBuffer_in[1][slot], nBins );
    3374      200136 :                 mvr2r( Cldfb_ImagBuffer_in[0][slot], Cldfb_ImagBuffer_in[1][slot], nBins );
    3375             :             }
    3376             :         }
    3377             :     }
    3378             : 
    3379      122353 :     if ( hCombinedOrientationData )
    3380             :     {
    3381      281940 :         for ( i = 0; i < 3; i++ )
    3382             :         {
    3383      845820 :             for ( j = 0; j < 3; j++ )
    3384             :             {
    3385      634365 :                 Rmat[i][j] = hCombinedOrientationData->Rmat[hCombinedOrientationData->subframe_idx][i][j];
    3386             :             }
    3387             :         }
    3388             : 
    3389       70485 :         if ( nchan_transport == 2 )
    3390             :         {
    3391             :             /* in case of split rendering, determine the prototype rotation based on the main direction and use the same prototypes for the offset directions */
    3392       39759 :             adaptTransportSignalsHeadtracked( hCombinedOrientationData, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in, nBins, hSpatParamRendCom->subframe_nbslots[subframe], Rmat );
    3393             : 
    3394       39759 :             ivas_dirac_dec_binaural_check_and_switch_transports_headtracked( hCombinedOrientationData, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in, nBins, hSpatParamRendCom->subframe_nbslots[subframe], Rmat );
    3395             :         }
    3396             :     }
    3397             : 
    3398             : 
    3399             :     /* Always using CLDFB decorrelation in MASA EXT renderer */
    3400      122353 :     max_band_decorr = hDiracDecBin->h_freq_domain_decorr_ap_params->max_band_decorr;
    3401             : 
    3402             : 
    3403      122353 :     ivas_dirac_dec_binaural_formulate_input_covariance_matrices( hDiracDecBin, hSpatParamRendCom, &config_data, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in, subframe,
    3404             :                                                                  subFrameTotalEne, IIReneLimiter );
    3405             : 
    3406      122353 :     ivas_dirac_dec_binaural_formulate_target_covariance_matrices( hDiracDecBin, hSpatParamRendCom, &config_data, Rmat, subframe,
    3407      122353 :                                                                   hCombinedOrientationData && hCombinedOrientationData->enableCombinedOrientation[hCombinedOrientationData->subframe_idx] > 0,
    3408             :                                                                   subFrameTotalEne, IIReneLimiter, NULL );
    3409             : 
    3410      122353 :     ivas_dirac_dec_binaural_determine_processing_matrices( hDiracDecBin, hSpatParamRendCom, &config_data, max_band_decorr, Rmat, subframe,
    3411      122353 :                                                            hCombinedOrientationData && hCombinedOrientationData->enableCombinedOrientation[hCombinedOrientationData->subframe_idx] > 0,
    3412             :                                                            0, NULL );
    3413             : 
    3414      122353 :     pMultiBinPoseData = NULL;
    3415      122353 :     if ( hSplitRendWrapper != NULL )
    3416             :     {
    3417           0 :         pMultiBinPoseData = &( hSplitRendWrapper->multiBinPoseData );
    3418             : 
    3419           0 :         ivas_dirac_dec_binaural_process_output( hDiracDecBin, hSpatParamRendCom, hMasaExtRend->cldfbSynRend, output_f, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in,
    3420           0 :                                                 max_band_decorr, numInChannels, config_data.processReverb, subframe, tmp_Cldfb_out_re, tmp_Cldfb_out_im,
    3421             :                                                 reverbRe, reverbIm, decorrRe, decorrIm, 1 );
    3422             : 
    3423           0 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3424             :         {
    3425           0 :             for ( i = 0; i < CLDFB_SLOTS_PER_SUBFRAME; i++ )
    3426             :             {
    3427           0 :                 mvr2r( tmp_Cldfb_out_re[ch][i], Cldfb_Out_Real[ch][subframe * CLDFB_SLOTS_PER_SUBFRAME + i], CLDFB_NO_CHANNELS_MAX );
    3428           0 :                 mvr2r( tmp_Cldfb_out_im[ch][i], Cldfb_Out_Imag[ch][subframe * CLDFB_SLOTS_PER_SUBFRAME + i], CLDFB_NO_CHANNELS_MAX );
    3429             :             }
    3430             :         }
    3431             :     }
    3432             :     else
    3433             :     {
    3434      122353 :         ivas_dirac_dec_binaural_process_output( hDiracDecBin, hSpatParamRendCom, hMasaExtRend->cldfbSynRend, output_f, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in,
    3435      122353 :                                                 max_band_decorr, numInChannels, config_data.processReverb, subframe, NULL, NULL,
    3436             :                                                 reverbRe, reverbIm, decorrRe, decorrIm, 1 );
    3437             :     }
    3438             : 
    3439      122353 :     hDiracDecBin->hDiffuseDist = NULL;
    3440             : 
    3441      122353 :     if ( pMultiBinPoseData != NULL && pMultiBinPoseData->num_poses > 1 )
    3442             :     {
    3443             :         /* quaternion-based rotation from ivas_binRenderer_internal.c:ivas_binRenderer(), but using absolute rotation instead of delta rotations */
    3444             :         IVAS_QUATERNION Quaternions_rot, Quaternions_abs, *Quaternions_ref;
    3445             :         float Rmat_local[3][3];
    3446             : 
    3447           0 :         if ( hCombinedOrientationData )
    3448             :         {
    3449           0 :             Quaternions_ref = &hCombinedOrientationData->Quaternions[0];
    3450           0 :             Quaternions_rot.w = -3.0f;                                                                        /* signal to use Euler */
    3451           0 :             Quaternions_abs.w = -3.0f;                                                                        /* signal to use Euler */
    3452           0 :             Quat2EulerDegree( *Quaternions_ref, &Quaternions_abs.z, &Quaternions_abs.y, &Quaternions_abs.x ); /*order in Quat2Euler seems to be reversed ?*/
    3453             : 
    3454           0 :             for ( pos_idx = 1; pos_idx < pMultiBinPoseData->num_poses; pos_idx++ )
    3455             :             {
    3456           0 :                 Quaternions_rot.x = Quaternions_abs.x + pMultiBinPoseData->relative_head_poses[pos_idx][0];
    3457           0 :                 Quaternions_rot.y = Quaternions_abs.y + pMultiBinPoseData->relative_head_poses[pos_idx][1];
    3458           0 :                 Quaternions_rot.z = Quaternions_abs.z + pMultiBinPoseData->relative_head_poses[pos_idx][2];
    3459             : 
    3460           0 :                 QuatToRotMat( Quaternions_rot, Rmat_local );
    3461             : 
    3462           0 :                 hDiracDecBin = hMasaExtRend->hDiracDecBin[pos_idx];
    3463           0 :                 assert( hDiracDecBin != NULL && "No DiracDecBin handle for this position" );
    3464             : 
    3465             :                 /* re-use input covariance for the side renderings */
    3466           0 :                 for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3467             :                 {
    3468           0 :                     mvr2r( hMasaExtRend->hDiracDecBin[0]->ChEne[ch], hDiracDecBin->ChEne[ch], hSpatParamRendCom->num_freq_bands );
    3469             :                 }
    3470           0 :                 mvr2r( hMasaExtRend->hDiracDecBin[0]->ChCrossRe, hDiracDecBin->ChCrossRe, hSpatParamRendCom->num_freq_bands );
    3471           0 :                 mvr2r( hMasaExtRend->hDiracDecBin[0]->ChCrossIm, hDiracDecBin->ChCrossIm, hSpatParamRendCom->num_freq_bands );
    3472             : 
    3473           0 :                 ivas_dirac_dec_binaural_formulate_target_covariance_matrices( hDiracDecBin, hSpatParamRendCom, &config_data, Rmat_local, subframe,
    3474           0 :                                                                               hCombinedOrientationData && hCombinedOrientationData->enableCombinedOrientation[hCombinedOrientationData->subframe_idx] > 0,
    3475             :                                                                               subFrameTotalEne, IIReneLimiter, NULL );
    3476             : 
    3477           0 :                 ivas_dirac_dec_binaural_determine_processing_matrices( hDiracDecBin, hSpatParamRendCom, &config_data, max_band_decorr, Rmat_local, subframe,
    3478           0 :                                                                        hCombinedOrientationData && hCombinedOrientationData->enableCombinedOrientation[hCombinedOrientationData->subframe_idx] > 0,
    3479             :                                                                        0, NULL );
    3480             : 
    3481             :                 /* re-use reverb and decorr from main direction for the sides */
    3482           0 :                 ivas_dirac_dec_binaural_process_output( hDiracDecBin, hSpatParamRendCom, hMasaExtRend->cldfbSynRend, output_f, Cldfb_RealBuffer_in, Cldfb_ImagBuffer_in,
    3483           0 :                                                         max_band_decorr, numInChannels, config_data.processReverb, subframe, tmp_Cldfb_out_re, tmp_Cldfb_out_im,
    3484             :                                                         reverbRe, reverbIm, decorrRe, decorrIm, 0 );
    3485             : 
    3486             :                 /* copy from temporary buffer to the main split rendering buffer */
    3487           0 :                 for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3488             :                 {
    3489           0 :                     for ( i = 0; i < CLDFB_SLOTS_PER_SUBFRAME; i++ )
    3490             :                     {
    3491           0 :                         mvr2r( tmp_Cldfb_out_re[ch][i], Cldfb_Out_Real[pos_idx * BINAURAL_CHANNELS + ch][subframe * CLDFB_SLOTS_PER_SUBFRAME + i], CLDFB_NO_CHANNELS_MAX );
    3492           0 :                         mvr2r( tmp_Cldfb_out_im[ch][i], Cldfb_Out_Imag[pos_idx * BINAURAL_CHANNELS + ch][subframe * CLDFB_SLOTS_PER_SUBFRAME + i], CLDFB_NO_CHANNELS_MAX );
    3493             :                     }
    3494             :                 }
    3495             : 
    3496           0 :                 hDiracDecBin->hDiffuseDist = NULL;
    3497             :             }
    3498             :         }
    3499             :     }
    3500             : 
    3501             :     /* update this counter only after the last rendering of split directions */
    3502      122353 :     hSpatParamRendCom->slots_rendered += hSpatParamRendCom->subframe_nbslots[subframe];
    3503      122353 :     hSpatParamRendCom->subframes_rendered++;
    3504             : 
    3505      122353 :     return;
    3506             : }
    3507             : 
    3508             : 
    3509       59887 : void ivas_masa_ext_rend_parambin_render(
    3510             :     MASA_EXT_REND_HANDLE hMasaExtRend,                               /* i/o: MASA ext rend structure                                  */
    3511             :     COMBINED_ORIENTATION_HANDLE hCombinedOrientationData,            /* i  : combined orientation handle                              */
    3512             :     float *output_f[],                                               /* i/o: synthesized core-coder transport channels/DirAC output   */
    3513             :     const int16_t num_subframes,                                     /* i  : number of subframes to render                            */
    3514             :     const SPLIT_REND_WRAPPER *hSplitRendWrapper,                     /* i  : split rendering orientation data                         */
    3515             :     float Cldfb_Out_Real[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], /* o  : rendered orientations for split rend. real part of cldfb */
    3516             :     float Cldfb_Out_Imag[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]  /* o  : rendered orientations for split rend. imag part of cldfb */
    3517             : )
    3518             : {
    3519             :     int16_t subframe;
    3520             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    3521             :     float *p_output[BINAURAL_CHANNELS];
    3522             :     int16_t ch;
    3523             : 
    3524       59887 :     hSpatParamRendCom = hMasaExtRend->hSpatParamRendCom;
    3525             : 
    3526      179661 :     for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3527             :     {
    3528      119774 :         p_output[ch] = output_f[ch];
    3529             :     }
    3530             : 
    3531       59887 :     hSpatParamRendCom->subframes_rendered = hSpatParamRendCom->dirac_read_idx;
    3532             : 
    3533      182240 :     for ( subframe = 0; subframe < num_subframes; subframe++ )
    3534             :     {
    3535      122353 :         int16_t n_samples_sf = hSpatParamRendCom->slot_size * CLDFB_SLOTS_PER_SUBFRAME;
    3536      122353 :         hSpatParamRendCom->slots_rendered = 0;
    3537             : 
    3538      122353 :         ivas_masa_ext_rend_parambin_internal( hMasaExtRend, hCombinedOrientationData, p_output, hSpatParamRendCom->dirac_read_idx, hSplitRendWrapper, Cldfb_Out_Real, Cldfb_Out_Imag );
    3539             : 
    3540      367059 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    3541             :         {
    3542      244706 :             p_output[ch] += n_samples_sf;
    3543             :         }
    3544             : 
    3545      122353 :         ivas_combined_orientation_update_index( hCombinedOrientationData, n_samples_sf );
    3546             : 
    3547      122353 :         hSpatParamRendCom->dirac_read_idx = ( hSpatParamRendCom->dirac_read_idx + 1 ) % hSpatParamRendCom->dirac_md_buffer_length;
    3548             :     }
    3549             : 
    3550       59887 :     return;
    3551             : }

Generated by: LCOV version 1.14