LCOV - code coverage report
Current view: top level - lib_rend - lib_rend.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ fec202a8f89be4a2f278a9fc377bfb58b58fab11 Lines: 2269 3252 69.8 %
Date: 2025-09-12 08:48:38 Functions: 149 185 80.5 %

          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 "options.h"
      34             : #include "lib_rend.h"
      35             : #include "prot.h"
      36             : #include "ivas_prot.h"
      37             : #include "ivas_prot_rend.h"
      38             : #include "isar_prot.h"
      39             : #include "lib_isar_pre_rend.h"
      40             : #include "ivas_cnst.h"
      41             : #include "ivas_rom_com.h"
      42             : #include "ivas_rom_rend.h"
      43             : #include <assert.h>
      44             : #include <math.h>
      45             : #include <stdbool.h>
      46             : #include "wmc_auto.h"
      47             : 
      48             : 
      49             : /*-------------------------------------------------------------------*
      50             :  * Local constants
      51             :  *-------------------------------------------------------------------*/
      52             : 
      53             : /* Maximum buffer length (total) in samples. */
      54             : #define MAX_BUFFER_LENGTH     ( L_FRAME_MAX * MAX_INPUT_CHANNELS )
      55             : #define MAX_BIN_DELAY_SAMPLES 150 /* Maximum supported rendering latency for binaural IRs */
      56             : 
      57             : /*-------------------------------------------------------------------*
      58             :  * Local types
      59             :  *-------------------------------------------------------------------*/
      60             : 
      61             : typedef float pan_vector[MAX_OUTPUT_CHANNELS];
      62             : typedef float pan_matrix[MAX_INPUT_CHANNELS][MAX_OUTPUT_CHANNELS];
      63             : typedef float rotation_gains[MAX_INPUT_CHANNELS][MAX_INPUT_CHANNELS];
      64             : typedef float rotation_matrix[3][3];
      65             : 
      66             : /* EFAP wrapper to simplify writing panning gains to a vector that includes LFE channels */
      67             : typedef struct
      68             : {
      69             :     EFAP_HANDLE hEfap;
      70             :     AUDIO_CONFIG speakerConfig;
      71             :     const LSSETUP_CUSTOM_STRUCT *pCustomLsSetup; /* Pointer to main custom LS struct from renderer handle - doesn't need freeing */
      72             : } EFAP_WRAPPER;
      73             : 
      74             : /* Lightweight helper struct that gathers all information required for rendering
      75             :  * any config to any other config. Used to simplify signatures of rendering functions.
      76             :  *
      77             :  * This struct should store ONLY CONST POINTERS to data existing elsewhere.
      78             :  * Storing pointers instead of data itself ensures that no additional updates
      79             :  * are required when any of these are changed in the renderer. Making the pointers
      80             :  * const ensures that this data is only read, but not modified by the rendering functions. */
      81             : typedef struct
      82             : {
      83             :     const int32_t *pOutSampleRate;
      84             :     const int32_t *pMaxGlobalDelayNs;
      85             :     const AUDIO_CONFIG *pOutConfig;
      86             :     const LSSETUP_CUSTOM_STRUCT *pCustomLsOut;
      87             :     const EFAP_WRAPPER *pEfapOutWrapper;
      88             :     const IVAS_REND_HeadRotData *pHeadRotData;
      89             :     const RENDER_CONFIG_HANDLE *hhRendererConfig;
      90             :     const int16_t *pSplitRendBFI;
      91             :     const SPLIT_REND_WRAPPER *pSplitRendWrapper;
      92             :     const COMBINED_ORIENTATION_HANDLE *pCombinedOrientationData;
      93             : } rendering_context;
      94             : 
      95             : /* Common base for input structs */
      96             : typedef struct
      97             : {
      98             :     AUDIO_CONFIG inConfig;
      99             :     IVAS_REND_InputId id;
     100             :     IVAS_REND_AudioBuffer inputBuffer;
     101             :     TD_RINGBUF_HANDLE delayBuffer;
     102             :     float gain; /* Linear, not in dB */
     103             :     rendering_context ctx;
     104             :     int32_t numNewSamplesPerChannel; /* Used to keep track how much new audio was fed before rendering current frame */
     105             :     int32_t delayNumSamples;
     106             : } input_base;
     107             : 
     108             : typedef struct
     109             : {
     110             :     input_base base;
     111             :     IVAS_ISM_METADATA currentPos;
     112             :     IVAS_ISM_METADATA previousPos;
     113             :     TDREND_WRAPPER tdRendWrapper;
     114             :     CREND_WRAPPER_HANDLE crendWrapper;
     115             :     REVERB_HANDLE hReverb;
     116             :     rotation_matrix rot_mat_prev;
     117             :     pan_vector prev_pan_gains;
     118             :     int8_t firstFrameRendered;
     119             :     TDREND_WRAPPER splitTdRendWrappers[MAX_HEAD_ROT_POSES - 1]; /* Additional TD Rend instances used for split rendering */
     120             :     float *bufferData;
     121             :     int16_t nonDiegeticPan;
     122             :     float nonDiegeticPanGain;
     123             :     OMASA_ANA_HANDLE hOMasa;
     124             :     uint16_t total_num_objects;
     125             : #ifdef NONBE_1377_REND_DIRATT_CONF
     126             :     int16_t object_id;
     127             : #endif
     128             :     float ism_metadata_delay_ms;
     129             : } input_ism;
     130             : 
     131             : typedef struct
     132             : {
     133             :     int16_t numLfeChannels;
     134             :     bool pan_lfe;
     135             :     float lfeInputGain;
     136             :     float lfeOutputAzimuth;
     137             :     float lfeOutputElevation;
     138             :     IVAS_REND_LfePanMtx lfePanMtx;
     139             : } lfe_routing;
     140             : 
     141             : typedef struct
     142             : {
     143             :     input_base base;
     144             : 
     145             :     /* Full panning matrix. 1st index is input channel, 2nd index is output channel.
     146             :        All LFE channels should be included, both for inputs and outputs */
     147             :     pan_matrix panGains;
     148             : 
     149             :     LSSETUP_CUSTOM_STRUCT customLsInput;
     150             :     EFAP_WRAPPER efapInWrapper;
     151             :     TDREND_WRAPPER tdRendWrapper;
     152             :     CREND_WRAPPER_HANDLE crendWrapper;
     153             :     TDREND_WRAPPER splitTdRendWrappers[MAX_HEAD_ROT_POSES - 1]; /* Additional TD Rend instances used for split rendering */
     154             :     REVERB_HANDLE hReverb;
     155             :     rotation_gains rot_gains_prev[MAX_HEAD_ROT_POSES];
     156             :     int16_t nonDiegeticPan;
     157             :     float nonDiegeticPanGain;
     158             :     lfe_routing lfeRouting;
     159             :     float *bufferData;
     160             :     int16_t binauralDelaySmp;
     161             :     float *lfeDelayBuffer;
     162             :     MCMASA_ANA_HANDLE hMcMasa;
     163             : } input_mc;
     164             : 
     165             : typedef struct
     166             : {
     167             :     input_base base;
     168             :     pan_matrix hoaDecMtx;
     169             :     CLDFB_REND_WRAPPER cldfbRendWrapper;
     170             :     CREND_WRAPPER_HANDLE crendWrapper;
     171             :     rotation_gains rot_gains_prev[MAX_HEAD_ROT_POSES];
     172             :     float *bufferData;
     173             :     DIRAC_ANA_HANDLE hDirAC;
     174             : } input_sba;
     175             : 
     176             : typedef struct
     177             : {
     178             :     input_base base;
     179             :     MASA_METADATA_FRAME masaMetadata;
     180             :     bool metadataHasBeenFed;
     181             :     float *bufferData;
     182             :     MASA_EXT_REND_HANDLE hMasaExtRend;
     183             :     MASA_PREREND_HANDLE hMasaPrerend;
     184             : } input_masa;
     185             : 
     186             : typedef struct hrtf_handles
     187             : {
     188             :     IVAS_DEC_HRTF_CREND_HANDLE hSetOfHRTF;
     189             :     IVAS_DEC_HRTF_FASTCONV_HANDLE hHrtfFastConv;
     190             :     IVAS_DEC_HRTF_PARAMBIN_HANDLE hHrtfParambin;
     191             :     IVAS_DEC_HRTF_HANDLE hHrtfTD;
     192             :     IVAS_DEC_HRTF_STATISTICS_HANDLE hHrtfStatistics;
     193             : } hrtf_handles;
     194             : 
     195             : struct IVAS_REND
     196             : {
     197             :     int32_t sampleRateOut;
     198             :     int32_t maxGlobalDelayNs;
     199             : 
     200             :     IVAS_LIMITER_HANDLE hLimiter;
     201             : #ifdef DEBUGGING
     202             :     int32_t numClipping; /* Counter of clipped output samples */
     203             : #endif
     204             : 
     205             :     input_ism inputsIsm[RENDERER_MAX_ISM_INPUTS];
     206             :     input_mc inputsMc[RENDERER_MAX_MC_INPUTS];
     207             :     input_sba inputsSba[RENDERER_MAX_SBA_INPUTS];
     208             :     input_masa inputsMasa[RENDERER_MAX_MASA_INPUTS];
     209             : 
     210             :     AUDIO_CONFIG inputConfig;
     211             :     AUDIO_CONFIG outputConfig;
     212             :     EFAP_WRAPPER efapOutWrapper;
     213             :     IVAS_LSSETUP_CUSTOM_STRUCT customLsOut;
     214             : 
     215             :     int16_t splitRendBFI;
     216             :     SPLIT_REND_WRAPPER *splitRendWrapper;
     217             :     IVAS_REND_AudioBuffer splitRendEncBuffer;
     218             : 
     219             :     IVAS_REND_HeadRotData headRotData;
     220             : 
     221             :     EXTERNAL_ORIENTATION_HANDLE hExternalOrientationData;
     222             :     COMBINED_ORIENTATION_HANDLE hCombinedOrientationData;
     223             : 
     224             :     int8_t rendererConfigEnabled;
     225             :     RENDER_CONFIG_DATA *hRendererConfig; /* Renderer config pointer */
     226             : 
     227             :     int16_t num_subframes;
     228             :     hrtf_handles hHrtfs;
     229             : };
     230             : 
     231             : 
     232             : /*-------------------------------------------------------------------*
     233             :  * Local function prototypes
     234             :  *-------------------------------------------------------------------*/
     235             : 
     236             : static ivas_error initMasaExtRenderer( input_masa *inputMasa, const AUDIO_CONFIG outConfig, const RENDER_CONFIG_DATA *hRendCfg, hrtf_handles *hHrtfs );
     237             : 
     238             : static void freeMasaExtRenderer( MASA_EXT_REND_HANDLE *hMasaExtRendOut );
     239             : 
     240             : static void renderSbaToMultiBinauralCldfb( input_sba *sbaInput, float Cldfb_Out_Real[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], float Cldfb_Out_Imag[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX], const int16_t low_res_pre_rend_rot, const int16_t num_subframes );
     241             : 
     242             : static ivas_error renderSbaToMultiBinaural( input_sba *sbaInput, const AUDIO_CONFIG outConfig, float out[][L_FRAME48k] );
     243             : 
     244             : /*-------------------------------------------------------------------*
     245             :  * Local functions
     246             :  *-------------------------------------------------------------------*/
     247             : 
     248       20786 : static ivas_error allocateInputBaseBufferData(
     249             :     float **data,
     250             :     const int16_t data_size )
     251             : {
     252       20786 :     *data = (float *) malloc( data_size * sizeof( float ) );
     253       20786 :     if ( *data == NULL )
     254             :     {
     255           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for input base buffer data" );
     256             :     }
     257             : 
     258       20786 :     return IVAS_ERR_OK;
     259             : }
     260             : 
     261       58674 : static void freeInputBaseBufferData(
     262             :     float **data )
     263             : {
     264       58674 :     if ( *data != NULL )
     265             :     {
     266       20786 :         free( *data );
     267       20786 :         *data = NULL;
     268             :     }
     269             : 
     270       58674 :     return;
     271             : }
     272             : 
     273    36222516 : static int16_t latencyNsToSamples(
     274             :     int32_t sampleRate,
     275             :     int32_t latency_ns )
     276             : {
     277    36222516 :     return (int16_t) roundf( (float) ( latency_ns ) * ( sampleRate / 1000000000.f ) );
     278             : }
     279             : 
     280        2230 : static ivas_error allocateMcLfeDelayBuffer(
     281             :     float **lfeDelayBuffer,
     282             :     const int16_t data_size )
     283             : {
     284        2230 :     *lfeDelayBuffer = (float *) malloc( data_size * sizeof( float ) );
     285        2230 :     if ( *lfeDelayBuffer == NULL )
     286             :     {
     287           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for LFE delay buffer" );
     288             :     }
     289             : 
     290        2230 :     return IVAS_ERR_OK;
     291             : }
     292             : 
     293        8382 : static void freeMcLfeDelayBuffer(
     294             :     float **lfeDelayBuffer )
     295             : {
     296        8382 :     if ( *lfeDelayBuffer != NULL )
     297             :     {
     298        2230 :         free( *lfeDelayBuffer );
     299        2230 :         *lfeDelayBuffer = NULL;
     300             :     }
     301             : 
     302        8382 :     return;
     303             : }
     304             : 
     305        3330 : static IVAS_QUATERNION quaternionInit(
     306             :     void )
     307             : {
     308             :     IVAS_QUATERNION q;
     309        3330 :     q.w = 1.0f;
     310        3330 :     q.x = q.y = q.z = 0.0f;
     311        3330 :     return q;
     312             : }
     313             : 
     314  4015587158 : static float *getSmplPtr(
     315             :     IVAS_REND_AudioBuffer buffer,
     316             :     const uint32_t chnlIdx,
     317             :     const uint32_t smplIdx )
     318             : {
     319  4015587158 :     return buffer.data + chnlIdx * buffer.config.numSamplesPerChannel + smplIdx;
     320             : }
     321             : 
     322           0 : static void convertBitsBufferToInternalBitsBuff(
     323             :     const IVAS_REND_BitstreamBuffer outBits,
     324             :     ISAR_SPLIT_REND_BITS_HANDLE hBits )
     325             : {
     326           0 :     hBits->bits_buf = outBits.bits;
     327           0 :     hBits->bits_read = outBits.config.bitsRead;
     328           0 :     hBits->bits_written = outBits.config.bitsWritten;
     329           0 :     hBits->buf_len = outBits.config.bufLenInBytes;
     330           0 :     hBits->codec = outBits.config.codec;
     331           0 :     hBits->pose_correction = outBits.config.poseCorrection;
     332           0 :     hBits->codec_frame_size_ms = outBits.config.codec_frame_size_ms;
     333             : 
     334           0 :     return;
     335             : }
     336             : 
     337           0 : static void convertInternalBitsBuffToBitsBuffer(
     338             :     IVAS_REND_BitstreamBuffer *hOutBits,
     339             :     const ISAR_SPLIT_REND_BITS_DATA bits )
     340             : {
     341           0 :     hOutBits->bits = bits.bits_buf;
     342           0 :     hOutBits->config.bitsRead = bits.bits_read;
     343           0 :     hOutBits->config.bitsWritten = bits.bits_written;
     344           0 :     hOutBits->config.bufLenInBytes = bits.buf_len;
     345           0 :     hOutBits->config.codec = bits.codec;
     346           0 :     hOutBits->config.poseCorrection = bits.pose_correction;
     347           0 :     hOutBits->config.codec_frame_size_ms = bits.codec_frame_size_ms;
     348             : 
     349           0 :     return;
     350             : }
     351             : 
     352           0 : static void copyBufferToCLDFBarray(
     353             :     const IVAS_REND_AudioBuffer buffer,
     354             :     float re[MAX_OUTPUT_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX],
     355             :     float im[MAX_OUTPUT_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX] )
     356             : {
     357             :     uint32_t smplIdx, slotIdx;
     358             :     uint32_t numCldfbSamples, num_bands;
     359             :     uint32_t chnlIdx;
     360             :     const float *readPtr;
     361             : 
     362           0 :     assert( ( buffer.config.is_cldfb == 1 ) && "for time domain input call copyBufferTo2dArray()" );
     363           0 :     readPtr = buffer.data;
     364           0 :     numCldfbSamples = ( (uint32_t) buffer.config.numSamplesPerChannel ) >> 1;
     365           0 :     num_bands = numCldfbSamples / CLDFB_NO_COL_MAX;
     366           0 :     for ( chnlIdx = 0; chnlIdx < (uint32_t) buffer.config.numChannels; ++chnlIdx )
     367             :     {
     368           0 :         for ( slotIdx = 0; slotIdx < CLDFB_NO_COL_MAX; ++slotIdx )
     369             :         {
     370           0 :             for ( smplIdx = 0; smplIdx < num_bands; ++smplIdx )
     371             :             {
     372           0 :                 re[chnlIdx][slotIdx][smplIdx] = *readPtr++;
     373             :             }
     374           0 :             for ( smplIdx = 0; smplIdx < num_bands; ++smplIdx )
     375             :             {
     376           0 :                 im[chnlIdx][slotIdx][smplIdx] = *readPtr++;
     377             :             }
     378             :         }
     379             :     }
     380             : 
     381           0 :     return;
     382             : }
     383             : 
     384           0 : static void accumulateCLDFBArrayToBuffer(
     385             :     float re[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX],
     386             :     float im[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX],
     387             :     const IVAS_REND_AudioBuffer *buffer )
     388             : {
     389             :     uint32_t smplIdx, slotIdx;
     390             :     uint32_t numCldfbSamples, num_bands;
     391             :     uint32_t chnlIdx;
     392             :     float *writePtr;
     393             : 
     394           0 :     assert( ( buffer->config.is_cldfb == 1 ) && "for time domain input call copyBufferTo2dArray()" );
     395           0 :     writePtr = buffer->data;
     396           0 :     numCldfbSamples = ( (uint32_t) buffer->config.numSamplesPerChannel ) >> 1;
     397           0 :     num_bands = numCldfbSamples / CLDFB_NO_COL_MAX;
     398           0 :     for ( chnlIdx = 0; chnlIdx < (uint32_t) buffer->config.numChannels; ++chnlIdx )
     399             :     {
     400           0 :         for ( slotIdx = 0; slotIdx < CLDFB_NO_COL_MAX; ++slotIdx )
     401             :         {
     402           0 :             for ( smplIdx = 0; smplIdx < num_bands; ++smplIdx )
     403             :             {
     404           0 :                 *writePtr++ += re[chnlIdx][slotIdx][smplIdx];
     405             :             }
     406           0 :             for ( smplIdx = 0; smplIdx < num_bands; ++smplIdx )
     407             :             {
     408           0 :                 *writePtr++ += im[chnlIdx][slotIdx][smplIdx];
     409             :             }
     410             :         }
     411             :     }
     412             : 
     413           0 :     return;
     414             : }
     415             : 
     416    13206068 : static void copyBufferTo2dArray(
     417             :     const IVAS_REND_AudioBuffer buffer,
     418             :     float array[][L_FRAME48k] )
     419             : {
     420             :     uint32_t smplIdx;
     421             :     uint32_t chnlIdx;
     422             :     const float *readPtr;
     423             : 
     424    13206068 :     assert( ( buffer.config.is_cldfb == 0 ) && "for CLDFB input call copyBufferToCLDFBarray()" );
     425    13206068 :     readPtr = buffer.data;
     426             : 
     427    89777654 :     for ( chnlIdx = 0; chnlIdx < (uint32_t) buffer.config.numChannels; ++chnlIdx )
     428             :     {
     429 19739479746 :         for ( smplIdx = 0; smplIdx < (uint32_t) buffer.config.numSamplesPerChannel; ++smplIdx )
     430             :         {
     431 19662908160 :             array[chnlIdx][smplIdx] = *readPtr++;
     432             :         }
     433             :     }
     434             : 
     435    13206068 :     return;
     436             : }
     437             : 
     438    13206068 : static void accumulate2dArrayToBuffer(
     439             :     float array[][L_FRAME48k],
     440             :     const IVAS_REND_AudioBuffer *buffer )
     441             : {
     442             :     int16_t smplIdx, chnlIdx;
     443             :     float *writePtr;
     444             : 
     445    13206068 :     writePtr = buffer->data;
     446    43524284 :     for ( chnlIdx = 0; chnlIdx < buffer->config.numChannels; ++chnlIdx )
     447             :     {
     448  7988814216 :         for ( smplIdx = 0; smplIdx < buffer->config.numSamplesPerChannel; ++smplIdx )
     449             :         {
     450  7958496000 :             *writePtr++ += array[chnlIdx][smplIdx];
     451             :         }
     452             :     }
     453             : 
     454    13206068 :     return;
     455             : }
     456             : 
     457             : /*-------------------------------------------------------------------*
     458             :  * limitRendererOutput()
     459             :  *
     460             :  * In-place saturation control for multichannel buffers with adaptive release time
     461             :  *-------------------------------------------------------------------*/
     462             : 
     463             : #ifndef DISABLE_LIMITER
     464             : /*! r: number of clipped output samples */
     465    13740500 : static int32_t limitRendererOutput(
     466             :     IVAS_LIMITER_HANDLE hLimiter, /* i/o: limiter struct handle                                           */
     467             :     float *output,                /* i/o: I/O buffer                                                      */
     468             :     const int16_t output_frame,   /* i  : number of samples per channel in the buffer                     */
     469             :     const float threshold         /* i  : signal amplitude above which limiting starts to be applied      */
     470             : )
     471             : {
     472             :     int16_t i;
     473             :     float **channels;
     474             :     int16_t num_channels;
     475    13740500 :     int32_t numClipping = 0;
     476             : 
     477             :     /* return early if given bad parameters */
     478    13740500 :     if ( hLimiter == NULL || output == NULL || output_frame <= 0 )
     479             :     {
     480           0 :         return 0;
     481             :     }
     482             : 
     483    13740500 :     channels = hLimiter->channel_ptrs;
     484    13740500 :     num_channels = hLimiter->num_channels;
     485             : 
     486    93694162 :     for ( i = 0; i < num_channels; ++i )
     487             :     {
     488    79953662 :         channels[i] = output + i * output_frame;
     489             :     }
     490             : 
     491    13740500 :     limiter_process( hLimiter, output_frame, threshold, 0, NULL );
     492             : 
     493             :     /* Apply clipping to buffer in case the limiter let through some samples > 1.0f */
     494 22890553940 :     for ( i = 0; i < output_frame * num_channels; ++i )
     495             :     {
     496             : #ifdef DEBUGGING
     497             :         if ( output[i] < INT16_MIN || output[i] > INT16_MAX )
     498             :         {
     499             :             ++numClipping;
     500             :         }
     501             : #endif
     502             : 
     503 22876813440 :         output[i] = min( max( INT16_MIN, output[i] ), INT16_MAX );
     504             :     }
     505             : 
     506    13740500 :     return numClipping;
     507             : }
     508             : #endif
     509             : 
     510             : /*-------------------------------------------------------------------*
     511             :  * validateOutputAudioConfig()
     512             :  *
     513             :  *
     514             :  *-------------------------------------------------------------------*/
     515             : 
     516        8382 : static ivas_error validateOutputAudioConfig(
     517             :     const AUDIO_CONFIG outConfig )
     518             : {
     519        8382 :     switch ( outConfig )
     520             :     {
     521        8382 :         case IVAS_AUDIO_CONFIG_MONO:
     522             :         case IVAS_AUDIO_CONFIG_STEREO:
     523             :         case IVAS_AUDIO_CONFIG_5_1:
     524             :         case IVAS_AUDIO_CONFIG_7_1:
     525             :         case IVAS_AUDIO_CONFIG_5_1_2:
     526             :         case IVAS_AUDIO_CONFIG_5_1_4:
     527             :         case IVAS_AUDIO_CONFIG_7_1_4:
     528             :         case IVAS_AUDIO_CONFIG_LS_CUSTOM:
     529             :         case IVAS_AUDIO_CONFIG_FOA:
     530             :         case IVAS_AUDIO_CONFIG_HOA2:
     531             :         case IVAS_AUDIO_CONFIG_HOA3:
     532             :         case IVAS_AUDIO_CONFIG_BINAURAL:
     533             :         case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED:
     534             :         case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM:
     535             :         case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR:
     536             :         case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB:
     537             :         case IVAS_AUDIO_CONFIG_MASA1:
     538             :         case IVAS_AUDIO_CONFIG_MASA2:
     539        8382 :             return IVAS_ERR_OK;
     540           0 :         default:
     541           0 :             break;
     542             :     }
     543             : 
     544           0 :     return IVAS_ERR_INVALID_OUTPUT_FORMAT;
     545             : }
     546             : 
     547             : 
     548             : /*-------------------------------------------------------------------*
     549             :  * getAudioConfigType()
     550             :  *
     551             :  *
     552             :  *-------------------------------------------------------------------*/
     553             : 
     554   137074533 : IVAS_REND_AudioConfigType getAudioConfigType(
     555             :     const AUDIO_CONFIG config )
     556             : {
     557             :     IVAS_REND_AudioConfigType type;
     558             : 
     559   137074533 :     switch ( config )
     560             :     {
     561    64232098 :         case IVAS_AUDIO_CONFIG_MONO:
     562             :         case IVAS_AUDIO_CONFIG_STEREO:
     563             :         case IVAS_AUDIO_CONFIG_5_1:
     564             :         case IVAS_AUDIO_CONFIG_7_1:
     565             :         case IVAS_AUDIO_CONFIG_5_1_2:
     566             :         case IVAS_AUDIO_CONFIG_5_1_4:
     567             :         case IVAS_AUDIO_CONFIG_7_1_4:
     568             :         case IVAS_AUDIO_CONFIG_LS_CUSTOM:
     569    64232098 :             type = IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED;
     570    64232098 :             break;
     571    25296899 :         case IVAS_AUDIO_CONFIG_FOA:
     572             :         case IVAS_AUDIO_CONFIG_HOA2:
     573             :         case IVAS_AUDIO_CONFIG_HOA3:
     574    25296899 :             type = IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS;
     575    25296899 :             break;
     576     6002202 :         case IVAS_AUDIO_CONFIG_OBA:
     577     6002202 :             type = IVAS_REND_AUDIO_CONFIG_TYPE_OBJECT_BASED;
     578     6002202 :             break;
     579    41032014 :         case IVAS_AUDIO_CONFIG_BINAURAL:
     580             :         case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR:
     581             :         case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB:
     582             :         case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM:
     583             :         case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED:
     584    41032014 :             type = IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL;
     585    41032014 :             break;
     586      511320 :         case IVAS_AUDIO_CONFIG_MASA1:
     587             :         case IVAS_AUDIO_CONFIG_MASA2:
     588      511320 :             type = IVAS_REND_AUDIO_CONFIG_TYPE_MASA;
     589      511320 :             break;
     590           0 :         default:
     591           0 :             type = IVAS_REND_AUDIO_CONFIG_TYPE_UNKNOWN;
     592           0 :             break;
     593             :     }
     594             : 
     595   137074533 :     return type;
     596             : }
     597             : 
     598             : 
     599             : /*-------------------------------------------------------------------*
     600             :  * validateOutputSampleRate()
     601             :  *
     602             :  *
     603             :  *-------------------------------------------------------------------*/
     604             : 
     605        8382 : static ivas_error validateOutputSampleRate(
     606             :     const int32_t sampleRate,
     607             :     const AUDIO_CONFIG outConfig )
     608             : {
     609        8382 :     if ( getAudioConfigType( outConfig ) != IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL )
     610             :     {
     611             :         /* If no binaural rendering, any sampling rate is supported */
     612        5718 :         return IVAS_ERR_OK;
     613             :     }
     614        2664 :     else if ( ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || outConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) && sampleRate != 48000 )
     615             :     {
     616           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_SAMPLING_RATE, "Error: Only 48kHz output sampling rate is supported for split rendering." );
     617             :     }
     618             :     else
     619             :     {
     620             :         /* Otherwise rendering to binaural, support the same set as IVAS decoder */
     621        2664 :         switch ( sampleRate )
     622             :         {
     623        2664 :             case 16000:
     624             :             case 32000:
     625             :             case 48000:
     626        2664 :                 return IVAS_ERR_OK;
     627             :         }
     628             : 
     629           0 :         return IVAS_ERR_INVALID_SAMPLING_RATE;
     630             :     }
     631             : }
     632             : 
     633             : 
     634             : /*-------------------------------------------------------------------*
     635             :  * getAudioConfigNumChannels()
     636             :  *
     637             :  *
     638             :  *-------------------------------------------------------------------*/
     639             : 
     640   104354731 : ivas_error getAudioConfigNumChannels(
     641             :     const AUDIO_CONFIG config,
     642             :     int16_t *numChannels )
     643             : {
     644   104354731 :     switch ( config )
     645             :     {
     646    27691352 :         case IVAS_AUDIO_CONFIG_MONO:
     647             :         case IVAS_AUDIO_CONFIG_OBA:
     648             :         case IVAS_AUDIO_CONFIG_MASA1:
     649    27691352 :             *numChannels = 1;
     650    27691352 :             break;
     651    23579570 :         case IVAS_AUDIO_CONFIG_STEREO:
     652             :         case IVAS_AUDIO_CONFIG_BINAURAL:
     653             :         case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED:
     654             :         case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM:
     655             :         case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR:
     656             :         case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB:
     657             :         case IVAS_AUDIO_CONFIG_MASA2:
     658    23579570 :             *numChannels = 2;
     659    23579570 :             break;
     660     8925824 :         case IVAS_AUDIO_CONFIG_FOA:
     661     8925824 :             *numChannels = 4;
     662     8925824 :             break;
     663     2697285 :         case IVAS_AUDIO_CONFIG_5_1:
     664     2697285 :             *numChannels = 6;
     665     2697285 :             break;
     666     2610721 :         case IVAS_AUDIO_CONFIG_7_1:
     667             :         case IVAS_AUDIO_CONFIG_5_1_2:
     668     2610721 :             *numChannels = 8;
     669     2610721 :             break;
     670     8888252 :         case IVAS_AUDIO_CONFIG_HOA2:
     671     8888252 :             *numChannels = 9;
     672     8888252 :             break;
     673     1362896 :         case IVAS_AUDIO_CONFIG_5_1_4:
     674     1362896 :             *numChannels = 10;
     675     1362896 :             break;
     676    19706530 :         case IVAS_AUDIO_CONFIG_7_1_4:
     677    19706530 :             *numChannels = 12;
     678    19706530 :             break;
     679     8892301 :         case IVAS_AUDIO_CONFIG_HOA3:
     680     8892301 :             *numChannels = 16;
     681     8892301 :             break;
     682           0 :         default:
     683           0 :             return IVAS_ERR_NUM_CHANNELS_UNKNOWN;
     684             :     }
     685             : 
     686   104354731 :     return IVAS_ERR_OK;
     687             : }
     688             : 
     689             : 
     690             : /*-------------------------------------------------------------------*
     691             :  * Local functions
     692             :  *-------------------------------------------------------------------*/
     693             : 
     694        9084 : static ivas_error initLimiter(
     695             :     IVAS_LIMITER_HANDLE *phLimiter,
     696             :     const int16_t numChannels,
     697             :     const int32_t sampleRate )
     698             : {
     699             :     ivas_error error;
     700             : 
     701             :     /* If re-initializing with unchanged values, return early */
     702        9084 :     if ( *phLimiter != NULL && ( *phLimiter )->num_channels == numChannels && ( *phLimiter )->sampling_rate == sampleRate )
     703             :     {
     704           0 :         return IVAS_ERR_OK;
     705             :     }
     706             : 
     707             :     /* Support re-init: close if already allocated */
     708        9084 :     if ( *phLimiter != NULL )
     709             :     {
     710         702 :         ivas_limiter_close( phLimiter );
     711             :     }
     712             : 
     713        9084 :     if ( ( error = ivas_limiter_open( phLimiter, numChannels, sampleRate ) ) != IVAS_ERR_OK )
     714             :     {
     715           0 :         return error;
     716             :     }
     717             : 
     718        9084 :     return IVAS_ERR_OK;
     719             : }
     720             : 
     721             : 
     722       10612 : static LSSETUP_CUSTOM_STRUCT defaultCustomLs(
     723             :     void )
     724             : {
     725             :     LSSETUP_CUSTOM_STRUCT ls;
     726             : 
     727             :     /* Set mono by default. This simplifies initialization,
     728             :        since output config is never in an undefined state. */
     729       10612 :     ls.is_planar_setup = 1;
     730       10612 :     ls.num_spk = 1;
     731       10612 :     set_zero( ls.ls_azimuth, MAX_OUTPUT_CHANNELS );
     732       10612 :     set_zero( ls.ls_elevation, MAX_OUTPUT_CHANNELS );
     733       10612 :     ls.num_lfe = 0;
     734       10612 :     set_s( ls.lfe_idx, 0, MAX_OUTPUT_CHANNELS );
     735       10612 :     ls.separate_ch_found = 0;
     736       10612 :     set_f( ls.separate_ch_gains, 0, MAX_OUTPUT_CHANNELS );
     737             : 
     738       10612 :     return ls;
     739             : }
     740             : 
     741             : 
     742       81114 : static ivas_error getSpeakerAzimuths(
     743             :     AUDIO_CONFIG config,
     744             :     const float **azimuths )
     745             : {
     746       81114 :     switch ( config )
     747             :     {
     748         618 :         case IVAS_AUDIO_CONFIG_MONO:
     749         618 :             *azimuths = ls_azimuth_CICP1;
     750         618 :             break;
     751         642 :         case IVAS_AUDIO_CONFIG_STEREO:
     752         642 :             *azimuths = ls_azimuth_CICP2;
     753         642 :             break;
     754       30594 :         case IVAS_AUDIO_CONFIG_5_1:
     755       30594 :             *azimuths = ls_azimuth_CICP6;
     756       30594 :             break;
     757        5094 :         case IVAS_AUDIO_CONFIG_7_1:
     758        5094 :             *azimuths = ls_azimuth_CICP12;
     759        5094 :             break;
     760       14118 :         case IVAS_AUDIO_CONFIG_5_1_2:
     761       14118 :             *azimuths = ls_azimuth_CICP14;
     762       14118 :             break;
     763       14154 :         case IVAS_AUDIO_CONFIG_5_1_4:
     764       14154 :             *azimuths = ls_azimuth_CICP16;
     765       14154 :             break;
     766       15894 :         case IVAS_AUDIO_CONFIG_7_1_4:
     767       15894 :             *azimuths = ls_azimuth_CICP19;
     768       15894 :             break;
     769           0 :         default:
     770           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Unexpected audio config" );
     771             :     }
     772             : 
     773       81114 :     return IVAS_ERR_OK;
     774             : }
     775             : 
     776             : 
     777       81114 : static ivas_error getSpeakerElevations(
     778             :     AUDIO_CONFIG config,
     779             :     const float **elevations )
     780             : {
     781       81114 :     switch ( config )
     782             :     {
     783         618 :         case IVAS_AUDIO_CONFIG_MONO:
     784         618 :             *elevations = ls_elevation_CICP1;
     785         618 :             break;
     786         642 :         case IVAS_AUDIO_CONFIG_STEREO:
     787         642 :             *elevations = ls_elevation_CICP2;
     788         642 :             break;
     789       30594 :         case IVAS_AUDIO_CONFIG_5_1:
     790       30594 :             *elevations = ls_elevation_CICP6;
     791       30594 :             break;
     792        5094 :         case IVAS_AUDIO_CONFIG_7_1:
     793        5094 :             *elevations = ls_elevation_CICP12;
     794        5094 :             break;
     795       14118 :         case IVAS_AUDIO_CONFIG_5_1_2:
     796       14118 :             *elevations = ls_elevation_CICP14;
     797       14118 :             break;
     798       14154 :         case IVAS_AUDIO_CONFIG_5_1_4:
     799       14154 :             *elevations = ls_elevation_CICP16;
     800       14154 :             break;
     801       15894 :         case IVAS_AUDIO_CONFIG_7_1_4:
     802       15894 :             *elevations = ls_elevation_CICP19;
     803       15894 :             break;
     804           0 :         default:
     805           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Unexpected audio config" );
     806             :     }
     807             : 
     808       81114 :     return IVAS_ERR_OK;
     809             : }
     810             : 
     811             : 
     812     5833578 : static ivas_error getAmbisonicsOrder(
     813             :     AUDIO_CONFIG config,
     814             :     int16_t *order )
     815             : {
     816     5833578 :     switch ( config )
     817             :     {
     818     1944582 :         case IVAS_AUDIO_CONFIG_FOA:
     819     1944582 :             *order = 1;
     820     1944582 :             break;
     821     1944498 :         case IVAS_AUDIO_CONFIG_HOA2:
     822     1944498 :             *order = 2;
     823     1944498 :             break;
     824     1944498 :         case IVAS_AUDIO_CONFIG_HOA3:
     825     1944498 :             *order = 3;
     826     1944498 :             break;
     827           0 :         default:
     828           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Unsupported audio config" );
     829             :     }
     830             : 
     831     5833578 :     return IVAS_ERR_OK;
     832             : }
     833             : 
     834             : 
     835           0 : static int16_t getNumLfeChannels(
     836             :     input_mc *inputMc )
     837             : {
     838           0 :     switch ( inputMc->base.inConfig )
     839             :     {
     840           0 :         case IVAS_AUDIO_CONFIG_5_1:
     841             :         case IVAS_AUDIO_CONFIG_7_1:
     842             :         case IVAS_AUDIO_CONFIG_5_1_2:
     843             :         case IVAS_AUDIO_CONFIG_5_1_4:
     844             :         case IVAS_AUDIO_CONFIG_7_1_4:
     845           0 :             return 1;
     846           0 :         case IVAS_AUDIO_CONFIG_LS_CUSTOM:
     847           0 :             return inputMc->customLsInput.num_lfe;
     848           0 :         default:
     849           0 :             break;
     850             :     }
     851             : 
     852           0 :     return 0;
     853             : }
     854             : 
     855        5982 : static ivas_error getNumNonLfeChannelsInSpeakerLayout(
     856             :     AUDIO_CONFIG config,
     857             :     int16_t *numNonLfeChannels )
     858             : {
     859        5982 :     switch ( config )
     860             :     {
     861         618 :         case IVAS_AUDIO_CONFIG_MONO:
     862         618 :             *numNonLfeChannels = 1;
     863         618 :             break;
     864         642 :         case IVAS_AUDIO_CONFIG_STEREO:
     865         642 :             *numNonLfeChannels = 2;
     866         642 :             break;
     867         582 :         case IVAS_AUDIO_CONFIG_5_1:
     868         582 :             *numNonLfeChannels = 5;
     869         582 :             break;
     870        1164 :         case IVAS_AUDIO_CONFIG_5_1_2:
     871             :         case IVAS_AUDIO_CONFIG_7_1:
     872        1164 :             *numNonLfeChannels = 7;
     873        1164 :             break;
     874         618 :         case IVAS_AUDIO_CONFIG_5_1_4:
     875         618 :             *numNonLfeChannels = 9;
     876         618 :             break;
     877        2358 :         case IVAS_AUDIO_CONFIG_7_1_4:
     878        2358 :             *numNonLfeChannels = 11;
     879        2358 :             break;
     880           0 :         default:
     881           0 :             return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Unexpected audio config" );
     882             :     }
     883             : 
     884        5982 :     return IVAS_ERR_OK;
     885             : }
     886             : 
     887      153228 : static ivas_error getMcConfigValues(
     888             :     AUDIO_CONFIG inConfig,
     889             :     const LSSETUP_CUSTOM_STRUCT *pInCustomLs,
     890             :     const float **azimuth,
     891             :     const float **elevation,
     892             :     int16_t *lfe_idx,
     893             :     int16_t *is_planar )
     894             : {
     895             :     int16_t i;
     896             :     ivas_error error;
     897             : 
     898      153228 :     *lfe_idx = -1;
     899      153228 :     *is_planar = 1;
     900      153228 :     switch ( inConfig )
     901             :     {
     902       78096 :         case IVAS_AUDIO_CONFIG_LS_CUSTOM:
     903       78096 :             *azimuth = (const float *) &pInCustomLs->ls_azimuth;
     904       78096 :             *elevation = (const float *) &pInCustomLs->ls_elevation;
     905       78096 :             if ( pInCustomLs->num_lfe > 0 )
     906             :             {
     907           0 :                 *lfe_idx = pInCustomLs->lfe_idx[0];
     908             :             }
     909      594576 :             for ( i = 0; i < pInCustomLs->num_spk; i++ )
     910             :             {
     911      594576 :                 if ( pInCustomLs->ls_elevation[i] != 0 )
     912             :                 {
     913       78096 :                     *is_planar = 0;
     914       78096 :                     break;
     915             :                 }
     916             :             }
     917       78096 :             break;
     918           0 :         case IVAS_AUDIO_CONFIG_MONO:
     919             :         case IVAS_AUDIO_CONFIG_STEREO:
     920           0 :             if ( ( error = getSpeakerAzimuths( inConfig, azimuth ) ) != IVAS_ERR_OK )
     921             :             {
     922           0 :                 return error;
     923             :             }
     924           0 :             if ( ( error = getSpeakerElevations( inConfig, elevation ) ) != IVAS_ERR_OK )
     925             :             {
     926           0 :                 return error;
     927             :             }
     928           0 :             break;
     929       75132 :         case IVAS_AUDIO_CONFIG_5_1:
     930             :         case IVAS_AUDIO_CONFIG_7_1:
     931             :         case IVAS_AUDIO_CONFIG_5_1_2:
     932             :         case IVAS_AUDIO_CONFIG_5_1_4:
     933             :         case IVAS_AUDIO_CONFIG_7_1_4:
     934       75132 :             if ( ( error = getSpeakerAzimuths( inConfig, azimuth ) ) != IVAS_ERR_OK )
     935             :             {
     936           0 :                 return error;
     937             :             }
     938       75132 :             if ( ( error = getSpeakerElevations( inConfig, elevation ) ) != IVAS_ERR_OK )
     939             :             {
     940           0 :                 return error;
     941             :             }
     942       75132 :             *lfe_idx = LFE_CHANNEL;
     943       75132 :             *is_planar = ( inConfig == IVAS_AUDIO_CONFIG_5_1 || inConfig == IVAS_AUDIO_CONFIG_7_1 ) ? 1 : 0;
     944       75132 :             break;
     945           0 :         default:
     946           0 :             assert( !"Invalid speaker config" );
     947             :             return IVAS_ERR_WRONG_PARAMS;
     948             :     }
     949             : 
     950      153228 :     return IVAS_ERR_OK;
     951             : }
     952             : 
     953             : 
     954        9894 : static ivas_error initEfap(
     955             :     EFAP_WRAPPER *pEfapWrapper,
     956             :     AUDIO_CONFIG outConfig,
     957             :     const LSSETUP_CUSTOM_STRUCT *pCustomLsOut )
     958             : {
     959             :     ivas_error error;
     960             :     const float *azimuths;
     961             :     const float *elevations;
     962             :     int16_t numNonLfeChannels;
     963             : 
     964        9894 :     if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR || outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
     965             :     {
     966        1776 :         pEfapWrapper->speakerConfig = IVAS_AUDIO_CONFIG_7_1_4;
     967             :     }
     968             :     else
     969             :     {
     970        8118 :         pEfapWrapper->speakerConfig = outConfig;
     971             :     }
     972        9894 :     pEfapWrapper->pCustomLsSetup = pCustomLsOut;
     973             : 
     974             :     /* If re-initializing, free existing EFAP handle. */
     975        9894 :     if ( pEfapWrapper->hEfap != NULL )
     976             :     {
     977         702 :         efap_free_data( &pEfapWrapper->hEfap );
     978             :     }
     979             : 
     980             :     /* Only initialize EFAP handle if output config is channel-based */
     981        9894 :     if ( getAudioConfigType( pEfapWrapper->speakerConfig ) != IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED )
     982             :     {
     983        2400 :         pEfapWrapper->hEfap = NULL;
     984        2400 :         return IVAS_ERR_OK;
     985             :     }
     986             : 
     987        7494 :     if ( outConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM )
     988             :     {
     989        2034 :         if ( ( error = efap_init_data( &pEfapWrapper->hEfap, pCustomLsOut->ls_azimuth, pCustomLsOut->ls_elevation, pCustomLsOut->num_spk, EFAP_MODE_EFAP ) ) != IVAS_ERR_OK )
     990             :         {
     991           0 :             return error;
     992             :         }
     993             :     }
     994             :     else
     995             :     {
     996        5460 :         if ( ( error = getSpeakerAzimuths( pEfapWrapper->speakerConfig, &azimuths ) ) != IVAS_ERR_OK )
     997             :         {
     998           0 :             return error;
     999             :         }
    1000             : 
    1001        5460 :         if ( ( error = getSpeakerElevations( pEfapWrapper->speakerConfig, &elevations ) ) != IVAS_ERR_OK )
    1002             :         {
    1003           0 :             return error;
    1004             :         }
    1005             : 
    1006        5460 :         if ( ( error = getNumNonLfeChannelsInSpeakerLayout( pEfapWrapper->speakerConfig, &numNonLfeChannels ) ) != IVAS_ERR_OK )
    1007             :         {
    1008           0 :             return error;
    1009             :         }
    1010             : 
    1011        5460 :         if ( ( error = efap_init_data( &pEfapWrapper->hEfap, azimuths, elevations, numNonLfeChannels, EFAP_MODE_EFAP ) ) != IVAS_ERR_OK )
    1012             :         {
    1013           0 :             return error;
    1014             :         }
    1015             :     }
    1016             : 
    1017        7494 :     return IVAS_ERR_OK;
    1018             : }
    1019             : 
    1020             : 
    1021     5403330 : static ivas_error getEfapGains(
    1022             :     EFAP_WRAPPER efapWrapper,
    1023             :     const float azi,
    1024             :     const float ele,
    1025             :     pan_vector panGains )
    1026             : {
    1027             :     pan_vector tmpPanGains; /* tmp pan gain buffer without LFE channels */
    1028             :     float *readPtr;
    1029             :     int16_t i;
    1030             :     int16_t lfeCount;
    1031             :     int16_t numChannels;
    1032             :     ivas_error error;
    1033             : 
    1034             :     /* EFAP returns an array of gains only for non-LFE speakers */
    1035     5403330 :     efap_determine_gains( efapWrapper.hEfap, tmpPanGains, azi, ele, EFAP_MODE_EFAP );
    1036             : 
    1037             :     /* Now copy to buffer that includes LFE channels */
    1038     5403330 :     if ( efapWrapper.speakerConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    1039             :     {
    1040      350226 :         numChannels = efapWrapper.pCustomLsSetup->num_spk + efapWrapper.pCustomLsSetup->num_lfe;
    1041      350226 :         readPtr = tmpPanGains;
    1042             : 
    1043     4552746 :         for ( i = 0, lfeCount = 0; i < numChannels; ++i )
    1044             :         {
    1045     4202520 :             if ( lfeCount < efapWrapper.pCustomLsSetup->num_lfe && i == efapWrapper.pCustomLsSetup->lfe_idx[lfeCount] )
    1046             :             {
    1047           0 :                 panGains[i] = 0.0f;
    1048           0 :                 ++lfeCount;
    1049             :             }
    1050             :             else
    1051             :             {
    1052     4202520 :                 panGains[i] = *readPtr;
    1053     4202520 :                 ++readPtr;
    1054             :             }
    1055             :         }
    1056             :     }
    1057             :     else
    1058             :     {
    1059     5053104 :         if ( ( error = getAudioConfigNumChannels( efapWrapper.speakerConfig, &numChannels ) ) != IVAS_ERR_OK )
    1060             :         {
    1061           0 :             return error;
    1062             :         }
    1063             : 
    1064     5053104 :         readPtr = tmpPanGains;
    1065             : 
    1066    59388420 :         for ( i = 0; i < numChannels; ++i )
    1067             :         {
    1068    54335316 :             if ( i == LFE_CHANNEL )
    1069             :             {
    1070     4819404 :                 panGains[i] = 0.0f;
    1071             :             }
    1072             :             else
    1073             :             {
    1074    49515912 :                 panGains[i] = *readPtr;
    1075    49515912 :                 ++readPtr;
    1076             :             }
    1077             :         }
    1078             :     }
    1079             : 
    1080     5403330 :     return IVAS_ERR_OK;
    1081             : }
    1082             : 
    1083             : 
    1084        1332 : static ivas_error initHeadRotation(
    1085             :     IVAS_REND_HANDLE hIvasRend )
    1086             : {
    1087             :     int16_t i, crossfade_len;
    1088             :     float tmp;
    1089             :     ivas_error error;
    1090             : 
    1091             :     /* Head rotation is enabled by default */
    1092        1332 :     hIvasRend->headRotData.headRotEnabled = 1;
    1093             : 
    1094             :     /* Initialize 5ms crossfade */
    1095        1332 :     crossfade_len = L_FRAME48k / MAX_PARAM_SPATIAL_SUBFRAMES;
    1096        1332 :     tmp = 1.f / ( crossfade_len - 1 );
    1097      321012 :     for ( i = 0; i < crossfade_len; i++ )
    1098             :     {
    1099      319680 :         hIvasRend->headRotData.crossfade[i] = i * tmp;
    1100             :     }
    1101             : 
    1102             :     /* Initialize with unit quaternions */
    1103        4662 :     for ( i = 0; i < hIvasRend->num_subframes; ++i )
    1104             :     {
    1105        3330 :         hIvasRend->headRotData.headPositions[i] = quaternionInit();
    1106             :     }
    1107             : 
    1108        1332 :     hIvasRend->headRotData.sr_pose_pred_axis = DEFAULT_AXIS;
    1109             : 
    1110        1332 :     if ( ( hIvasRend->headRotData.hOrientationTracker = (ivas_orient_trk_state_t *) malloc( sizeof( ivas_orient_trk_state_t ) ) ) == NULL )
    1111             :     {
    1112           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Orientation tracking" );
    1113             :     }
    1114             : 
    1115        1332 :     if ( ( error = ivas_orient_trk_Init( hIvasRend->headRotData.hOrientationTracker ) ) != IVAS_ERR_OK )
    1116             :     {
    1117           0 :         return error;
    1118             :     }
    1119             : 
    1120        1332 :     return IVAS_ERR_OK;
    1121             : }
    1122             : 
    1123        8382 : static void closeHeadRotation(
    1124             :     IVAS_REND_HANDLE hIvasRend )
    1125             : {
    1126        8382 :     if ( hIvasRend != NULL && hIvasRend->headRotData.headRotEnabled && hIvasRend->headRotData.hOrientationTracker != NULL )
    1127             :     {
    1128        1332 :         free( hIvasRend->headRotData.hOrientationTracker );
    1129             :     }
    1130             : 
    1131        8382 :     return;
    1132             : }
    1133             : 
    1134             : 
    1135       13146 : static void initRotMatrix(
    1136             :     rotation_matrix rot_mat )
    1137             : {
    1138             :     int16_t i;
    1139             : 
    1140             :     /* Initialize rotation matrices */
    1141       52584 :     for ( i = 0; i < 3; i++ )
    1142             :     {
    1143       39438 :         set_zero( rot_mat[i], 3 );
    1144       39438 :         rot_mat[i][i] = 1.f;
    1145             :     }
    1146             : 
    1147       13146 :     return;
    1148             : }
    1149             : 
    1150             : 
    1151       44032 : static void initRotGains(
    1152             :     rotation_gains rot_gains )
    1153             : {
    1154             :     int16_t i;
    1155             : 
    1156             :     /* Set gains to passthrough */
    1157      748544 :     for ( i = 0; i < MAX_INPUT_CHANNELS; i++ )
    1158             :     {
    1159      704512 :         set_zero( rot_gains[i], MAX_INPUT_CHANNELS );
    1160      704512 :         rot_gains[i][i] = 1.f;
    1161             :     }
    1162             : 
    1163       44032 :     return;
    1164             : }
    1165             : 
    1166             : 
    1167      138134 : static void initRendInputBase(
    1168             :     input_base *inputBase,
    1169             :     const AUDIO_CONFIG inConfig,
    1170             :     const IVAS_REND_InputId id,
    1171             :     const rendering_context rendCtx,
    1172             :     float *dataBuf,
    1173             :     const int16_t dataBufSize )
    1174             : {
    1175      138134 :     inputBase->inConfig = inConfig;
    1176      138134 :     inputBase->id = id;
    1177      138134 :     inputBase->gain = 1.0f;
    1178      138134 :     inputBase->ctx = rendCtx;
    1179      138134 :     inputBase->numNewSamplesPerChannel = 0;
    1180      138134 :     inputBase->delayNumSamples = 0;
    1181      138134 :     inputBase->delayBuffer = NULL;
    1182             : 
    1183      138134 :     inputBase->inputBuffer.config.numSamplesPerChannel = 0;
    1184      138134 :     inputBase->inputBuffer.config.numChannels = 0;
    1185      138134 :     inputBase->inputBuffer.data = dataBuf;
    1186      138134 :     if ( inputBase->inputBuffer.data != NULL )
    1187             :     {
    1188       20786 :         set_zero( inputBase->inputBuffer.data, dataBufSize );
    1189             :     }
    1190             : 
    1191      138134 :     return;
    1192             : }
    1193             : 
    1194             : 
    1195     5970612 : static IVAS_ISM_METADATA defaultObjectPosition(
    1196             :     void )
    1197             : {
    1198             :     IVAS_ISM_METADATA pos;
    1199             : 
    1200     5970612 :     pos.azimuth = 0.0f;
    1201     5970612 :     pos.elevation = 0.0f;
    1202     5970612 :     pos.radius = 1.0f;
    1203     5970612 :     pos.spread = 0.0f;
    1204     5970612 :     pos.gainFactor = 1.0f;
    1205     5970612 :     pos.yaw = 0.0f;
    1206     5970612 :     pos.pitch = 0.0f;
    1207     5970612 :     pos.non_diegetic_flag = 0;
    1208             : 
    1209     5970612 :     return pos;
    1210             : }
    1211             : 
    1212             : 
    1213    18832860 : static int8_t checkObjectPositionChanged(
    1214             :     IVAS_ISM_METADATA *currentPos,
    1215             :     IVAS_ISM_METADATA *previousPos )
    1216             : {
    1217    34836354 :     return !( fabs( currentPos->azimuth - previousPos->azimuth ) < EPSILON &&
    1218    16003494 :               fabs( currentPos->elevation - previousPos->elevation ) < EPSILON );
    1219             : }
    1220             : 
    1221             : 
    1222       58674 : static rendering_context getRendCtx(
    1223             :     IVAS_REND_HANDLE hIvasRend )
    1224             : {
    1225             :     rendering_context ctx;
    1226             : 
    1227             :     /* Note: when refactoring this, always take the ADDRESS of a member of the
    1228             :      * renderer struct, so that the context stores a POINTER to the member, even
    1229             :      * if the member is a pointer or handle itself. */
    1230       58674 :     ctx.pMaxGlobalDelayNs = &hIvasRend->maxGlobalDelayNs;
    1231       58674 :     ctx.pOutConfig = &hIvasRend->outputConfig;
    1232       58674 :     ctx.pOutSampleRate = &hIvasRend->sampleRateOut;
    1233       58674 :     ctx.pCustomLsOut = &hIvasRend->customLsOut;
    1234       58674 :     ctx.pEfapOutWrapper = &hIvasRend->efapOutWrapper;
    1235       58674 :     ctx.pHeadRotData = &hIvasRend->headRotData;
    1236       58674 :     ctx.hhRendererConfig = &hIvasRend->hRendererConfig;
    1237       58674 :     ctx.pSplitRendBFI = &hIvasRend->splitRendBFI;
    1238       58674 :     ctx.pSplitRendWrapper = hIvasRend->splitRendWrapper;
    1239       58674 :     ctx.pCombinedOrientationData = &hIvasRend->hCombinedOrientationData;
    1240             : 
    1241       58674 :     return ctx;
    1242             : }
    1243             : 
    1244             : 
    1245      123008 : static TDREND_WRAPPER defaultTdRendWrapper(
    1246             :     void )
    1247             : {
    1248             :     TDREND_WRAPPER w;
    1249             : 
    1250      123008 :     w.binaural_latency_ns = 0;
    1251      123008 :     w.hBinRendererTd = NULL;
    1252      123008 :     w.hHrtfTD = NULL;
    1253             : 
    1254      123008 :     return w;
    1255             : }
    1256             : 
    1257             : 
    1258       20786 : static bool isIoConfigPairSupported(
    1259             :     const AUDIO_CONFIG inConfig,
    1260             :     const AUDIO_CONFIG outConfig )
    1261             : {
    1262             :     /* Rendering mono or stereo to binaural is not supported */
    1263       20786 :     if ( ( inConfig == IVAS_AUDIO_CONFIG_MONO || inConfig == IVAS_AUDIO_CONFIG_STEREO ) && getAudioConfigType( outConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL )
    1264             :     {
    1265           0 :         return false;
    1266             :     }
    1267             : 
    1268             :     /* If not returned so far, config pair is supported */
    1269       20786 :     return true;
    1270             : }
    1271             : 
    1272             : 
    1273       49912 : static int32_t getRendInputDelayIsm(
    1274             :     const input_ism *inputIsm,
    1275             :     bool splitPreRendCldfb )
    1276             : {
    1277             :     int32_t latency_ns;
    1278       49912 :     latency_ns = 0;
    1279             :     (void) ( splitPreRendCldfb ); /* unused */
    1280             : 
    1281             :     /* set the rendering delay in InputBase */
    1282       49912 :     latency_ns = max( latency_ns,
    1283             :                       inputIsm->tdRendWrapper.binaural_latency_ns );
    1284       49912 :     if ( inputIsm->crendWrapper != NULL )
    1285             :     {
    1286        5520 :         latency_ns = max( latency_ns,
    1287             :                           inputIsm->crendWrapper->binaural_latency_ns );
    1288             :     }
    1289             : 
    1290       49912 :     return latency_ns;
    1291             : }
    1292             : 
    1293             : 
    1294       13146 : static void setRendInputDelayIsm(
    1295             :     void *input,
    1296             :     bool splitPreRendCldfb )
    1297             : {
    1298             :     input_ism *inputIsm;
    1299       13146 :     inputIsm = (input_ism *) input;
    1300             : 
    1301       13146 :     inputIsm->base.delayNumSamples = latencyNsToSamples( *inputIsm->base.ctx.pOutSampleRate,
    1302             :                                                          getRendInputDelayIsm( inputIsm, splitPreRendCldfb ) );
    1303       13146 : }
    1304             : 
    1305             : 
    1306        5072 : static int32_t getRendInputDelayMc(
    1307             :     const input_mc *inputMc,
    1308             :     bool splitPreRendCldfb )
    1309             : {
    1310             :     int32_t latency_ns;
    1311        5072 :     latency_ns = 0;
    1312             :     (void) ( splitPreRendCldfb ); /* unused */
    1313             : 
    1314        5072 :     latency_ns = max( latency_ns,
    1315             :                       inputMc->tdRendWrapper.binaural_latency_ns );
    1316        5072 :     if ( inputMc->crendWrapper != NULL )
    1317             :     {
    1318         912 :         latency_ns = max( latency_ns,
    1319             :                           inputMc->crendWrapper->binaural_latency_ns );
    1320             :     }
    1321             : 
    1322        5072 :     return latency_ns;
    1323             : }
    1324             : 
    1325             : 
    1326        2230 : static void setRendInputDelayMc(
    1327             :     void *input,
    1328             :     bool splitPreRendCldfb )
    1329             : {
    1330             :     input_mc *inputMc;
    1331        2230 :     inputMc = (input_mc *) input;
    1332             : 
    1333        2230 :     inputMc->base.delayNumSamples = latencyNsToSamples( *inputMc->base.ctx.pOutSampleRate,
    1334             :                                                         getRendInputDelayMc( inputMc, splitPreRendCldfb ) );
    1335        2230 : }
    1336             : 
    1337             : 
    1338        6552 : static int32_t getRendInputDelaySba(
    1339             :     const input_sba *inputSba,
    1340             :     bool splitPreRendCldfb )
    1341             : {
    1342             :     int32_t latency_ns;
    1343        6552 :     latency_ns = 0;
    1344             : 
    1345        6552 :     if ( inputSba->cldfbRendWrapper.hCldfbRend != NULL )
    1346             :     {
    1347           0 :         latency_ns = max( latency_ns,
    1348             :                           inputSba->cldfbRendWrapper.binaural_latency_ns +
    1349             :                               ( splitPreRendCldfb ? 0 : IVAS_FB_DEC_DELAY_NS ) );
    1350             :     }
    1351        6552 :     if ( inputSba->crendWrapper != NULL )
    1352             :     {
    1353        2160 :         latency_ns = max( latency_ns,
    1354             :                           inputSba->crendWrapper->binaural_latency_ns );
    1355             :     }
    1356             : 
    1357        6552 :     return latency_ns;
    1358             : }
    1359             : 
    1360             : 
    1361        3274 : static void setRendInputDelaySba(
    1362             :     void *input,
    1363             :     bool splitPreRendCldfb )
    1364             : {
    1365             :     input_sba *inputSba;
    1366        3274 :     inputSba = (input_sba *) input;
    1367             : 
    1368        3274 :     inputSba->base.delayNumSamples = latencyNsToSamples( *inputSba->base.ctx.pOutSampleRate,
    1369             :                                                          getRendInputDelaySba( inputSba, splitPreRendCldfb ) );
    1370        3274 : }
    1371             : 
    1372             : 
    1373        4272 : static int32_t getRendInputDelayMasa(
    1374             :     const input_masa *inputMasa,
    1375             :     bool splitPreRendCldfb )
    1376             : {
    1377             :     int32_t latency_ns;
    1378             : 
    1379        4272 :     latency_ns = 0;
    1380             : 
    1381        8424 :     if ( ( inputMasa->base.inConfig == IVAS_AUDIO_CONFIG_MASA1 && *inputMasa->base.ctx.pOutConfig == IVAS_AUDIO_CONFIG_MONO ) ||
    1382        4152 :          ( getAudioConfigType( *inputMasa->base.ctx.pOutConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_MASA ) )
    1383             :     {
    1384         192 :         return 0; /* no delay */
    1385             :     }
    1386             :     else
    1387             :     {
    1388             :         /* no delay applied for split rendering */
    1389        4080 :         latency_ns = max( latency_ns,
    1390             :                           (int32_t) ( ( splitPreRendCldfb ? 0 : (float) IVAS_FB_DEC_DELAY_NS + 0.5f ) ) );
    1391             :     }
    1392        4080 :     return latency_ns;
    1393             : }
    1394             : 
    1395             : 
    1396        2136 : static void setRendInputDelayMasa(
    1397             :     void *input,
    1398             :     bool splitPreRendCldfb )
    1399             : {
    1400             :     input_masa *inputMasa;
    1401        2136 :     inputMasa = (input_masa *) input;
    1402             : 
    1403        2136 :     inputMasa->base.delayNumSamples = latencyNsToSamples( *inputMasa->base.ctx.pOutSampleRate,
    1404             :                                                           getRendInputDelayMasa( inputMasa, splitPreRendCldfb ) );
    1405        2136 : }
    1406             : 
    1407             : 
    1408       20786 : static int32_t getMaxGlobalDelayNs( IVAS_REND_CONST_HANDLE hIvasRend )
    1409             : {
    1410             :     int16_t i;
    1411             :     int32_t latency_ns;
    1412             :     int32_t max_latency_ns;
    1413             :     bool splitPreRendCldfb;
    1414             : 
    1415       20786 :     max_latency_ns = 0;
    1416             :     /*assumes that input has been added which means codec has been set to either lcld or lc3plus (even if render config specified default)*/
    1417       20786 :     if ( hIvasRend->hRendererConfig != NULL )
    1418             :     {
    1419        6696 :         splitPreRendCldfb = ( hIvasRend->hRendererConfig->split_rend_config.codec == ISAR_SPLIT_REND_CODEC_LCLD );
    1420             :     }
    1421             :     else
    1422             :     {
    1423       14090 :         splitPreRendCldfb = false;
    1424             :     }
    1425             : 
    1426             :     /* Compute the maximum delay across all inputs */
    1427      103930 :     for ( i = 0; i < RENDERER_MAX_ISM_INPUTS; i++ )
    1428             :     {
    1429       83144 :         if ( hIvasRend->inputsIsm[i].base.inConfig != IVAS_AUDIO_CONFIG_INVALID )
    1430             :         {
    1431       36766 :             latency_ns = getRendInputDelayIsm( &hIvasRend->inputsIsm[i], splitPreRendCldfb );
    1432       36766 :             max_latency_ns = max( max_latency_ns, latency_ns );
    1433             :         }
    1434             :     }
    1435             : 
    1436       41572 :     for ( i = 0; i < RENDERER_MAX_MC_INPUTS; i++ )
    1437             :     {
    1438       20786 :         if ( hIvasRend->inputsMc[i].base.inConfig != IVAS_AUDIO_CONFIG_INVALID )
    1439             :         {
    1440        2842 :             latency_ns = getRendInputDelayMc( &hIvasRend->inputsMc[i], splitPreRendCldfb );
    1441        2842 :             max_latency_ns = max( max_latency_ns, latency_ns );
    1442             :         }
    1443             :     }
    1444             : 
    1445       41572 :     for ( i = 0; i < RENDERER_MAX_SBA_INPUTS; i++ )
    1446             :     {
    1447       20786 :         if ( hIvasRend->inputsSba[i].base.inConfig != IVAS_AUDIO_CONFIG_INVALID )
    1448             :         {
    1449        3278 :             latency_ns = getRendInputDelaySba( &hIvasRend->inputsSba[i], splitPreRendCldfb );
    1450        3278 :             max_latency_ns = max( max_latency_ns, latency_ns );
    1451             :         }
    1452             :     }
    1453             : 
    1454       41572 :     for ( i = 0; i < RENDERER_MAX_MASA_INPUTS; i++ )
    1455             :     {
    1456       20786 :         if ( hIvasRend->inputsMasa[i].base.inConfig != IVAS_AUDIO_CONFIG_INVALID )
    1457             :         {
    1458        2136 :             latency_ns = getRendInputDelayMasa( &hIvasRend->inputsMasa[i], splitPreRendCldfb );
    1459        2136 :             max_latency_ns = max( max_latency_ns, latency_ns );
    1460             :         }
    1461             :     }
    1462             : 
    1463       20786 :     return max_latency_ns;
    1464             : }
    1465             : 
    1466             : 
    1467       20786 : static void setMaxGlobalDelayNs( IVAS_REND_HANDLE hIvasRend )
    1468             : {
    1469       20786 :     hIvasRend->maxGlobalDelayNs = getMaxGlobalDelayNs( (IVAS_REND_CONST_HANDLE) hIvasRend );
    1470       20786 : }
    1471             : 
    1472    35521204 : static ivas_error alignInputDelay(
    1473             :     input_base *inputBase,
    1474             :     const IVAS_REND_ReadOnlyAudioBuffer inputAudio,
    1475             :     const int32_t maxGlobalDelayNs,
    1476             :     const int32_t sampleRateOut,
    1477             :     const int16_t cldfb2tdSampleFact,
    1478             :     const bool flushInputs )
    1479             : {
    1480             :     ivas_error error;
    1481             :     input_ism *inputIsm;
    1482             :     int16_t maxGlobalDelaySamples;
    1483             :     int32_t numSamplesToPush, numSamplesToPop;
    1484             :     uint32_t ringBufferSize, preDelay;
    1485             : 
    1486    35521204 :     maxGlobalDelaySamples = latencyNsToSamples( sampleRateOut, maxGlobalDelayNs );
    1487    35521204 :     maxGlobalDelaySamples *= cldfb2tdSampleFact;
    1488             : 
    1489             :     /* check if we need to open the delay buffer */
    1490    35521204 :     if ( inputBase->delayBuffer == NULL )
    1491             :     {
    1492             :         /* buffer has to accomodate maxGlobalDelaySamples + 2 * frameSize */
    1493    30432972 :         ringBufferSize = maxGlobalDelaySamples + 2 * inputAudio.config.numSamplesPerChannel;
    1494             : 
    1495             :         /* pre delay for this input is maximum delay - input delay */
    1496    30432972 :         preDelay = maxGlobalDelaySamples - inputBase->delayNumSamples * cldfb2tdSampleFact;
    1497             : 
    1498    30432972 :         if ( preDelay > 0 )
    1499             :         {
    1500        5584 :             if ( ( error = ivas_TD_RINGBUF_Open( &inputBase->delayBuffer, ringBufferSize, inputAudio.config.numChannels ) ) != IVAS_ERR_OK )
    1501             :             {
    1502           0 :                 return error;
    1503             :             }
    1504             : 
    1505             :             /* for the first frame we need to push zeros to align the input delay to the global delay
    1506             :              * and then push a frame of actual data */
    1507        5584 :             ivas_TD_RINGBUF_PushZeros( inputBase->delayBuffer, preDelay );
    1508             : 
    1509             :             /* for ISM inputs, ensure the metadata sync delay is updated */
    1510        5584 :             if ( getAudioConfigType( inputBase->inConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_OBJECT_BASED )
    1511             :             {
    1512        4080 :                 inputIsm = (input_ism *) inputBase;
    1513        4080 :                 inputIsm->ism_metadata_delay_ms = maxGlobalDelayNs / 1e6f;
    1514             :             }
    1515             :         }
    1516             :     }
    1517             : 
    1518    35521204 :     if ( inputBase->delayBuffer != NULL )
    1519             :     {
    1520             :         /* push in the new input data and pop to retrieve a complete input frame
    1521             :          * if we are flushing the inputs, we don't push in any new data */
    1522     5093816 :         numSamplesToPush = flushInputs ? 0 : inputAudio.config.numSamplesPerChannel;
    1523     5093816 :         numSamplesToPop = flushInputs ? ivas_TD_RINGBUF_Size( inputBase->delayBuffer ) : (uint32_t) inputAudio.config.numSamplesPerChannel;
    1524             : 
    1525     5093816 :         ivas_TD_RINGBUF_Push( inputBase->delayBuffer, inputAudio.data, numSamplesToPush );
    1526     5093816 :         ivas_TD_RINGBUF_Pop( inputBase->delayBuffer, inputBase->inputBuffer.data, numSamplesToPop );
    1527             :     }
    1528             :     else
    1529             :     {
    1530             :         /* delay buffer isn't open - we don't need it */
    1531    30427388 :         mvr2r( inputAudio.data,
    1532             :                inputBase->inputBuffer.data,
    1533    30427388 :                inputAudio.config.numSamplesPerChannel * inputAudio.config.numChannels );
    1534             :     }
    1535             : 
    1536    35521204 :     return IVAS_ERR_OK;
    1537             : }
    1538             : 
    1539          36 : static ivas_error initIsmMasaRendering(
    1540             :     input_ism *inputIsm,
    1541             :     const int32_t inSampleRate )
    1542             : {
    1543             :     ivas_error error;
    1544             : 
    1545          36 :     if ( inputIsm->tdRendWrapper.hBinRendererTd != NULL )
    1546             :     {
    1547           0 :         ivas_td_binaural_close( &inputIsm->tdRendWrapper.hBinRendererTd );
    1548             :     }
    1549             : 
    1550          36 :     ivas_rend_closeCrend( &inputIsm->crendWrapper );
    1551             : 
    1552          36 :     ivas_reverb_close( &inputIsm->hReverb );
    1553             : 
    1554          36 :     if ( ( error = ivas_omasa_ana_open( &inputIsm->hOMasa, inSampleRate, inputIsm->total_num_objects ) ) != IVAS_ERR_OK )
    1555             :     {
    1556           0 :         return error;
    1557             :     }
    1558             : 
    1559          36 :     return IVAS_ERR_OK;
    1560             : }
    1561             : 
    1562             : 
    1563       13146 : static ivas_error setRendInputActiveIsm(
    1564             :     void *input,
    1565             :     const AUDIO_CONFIG inConfig,
    1566             :     const IVAS_REND_InputId id,
    1567             :     RENDER_CONFIG_DATA *hRendCfg,
    1568             :     hrtf_handles *hrtfs )
    1569             : {
    1570             :     ivas_error error;
    1571             :     rendering_context rendCtx;
    1572             :     AUDIO_CONFIG outConfig;
    1573             :     input_ism *inputIsm;
    1574             :     int16_t i;
    1575             : 
    1576       13146 :     inputIsm = (input_ism *) input;
    1577       13146 :     rendCtx = inputIsm->base.ctx;
    1578       13146 :     outConfig = *rendCtx.pOutConfig;
    1579             : 
    1580       13146 :     if ( !isIoConfigPairSupported( inConfig, outConfig ) )
    1581             :     {
    1582           0 :         return IVAS_ERR_IO_CONFIG_PAIR_NOT_SUPPORTED;
    1583             :     }
    1584             : 
    1585       13146 :     if ( ( error = allocateInputBaseBufferData( &inputIsm->bufferData, MAX_BUFFER_LENGTH ) ) != IVAS_ERR_OK )
    1586             :     {
    1587           0 :         return error;
    1588             :     }
    1589       13146 :     initRendInputBase( &inputIsm->base, inConfig, id, rendCtx, inputIsm->bufferData, MAX_BUFFER_LENGTH );
    1590             : 
    1591       13146 :     inputIsm->firstFrameRendered = FALSE;
    1592             : 
    1593       13146 :     inputIsm->currentPos = defaultObjectPosition();
    1594       13146 :     inputIsm->previousPos = defaultObjectPosition();
    1595       13146 :     inputIsm->crendWrapper = NULL;
    1596       13146 :     inputIsm->hReverb = NULL;
    1597       13146 :     inputIsm->tdRendWrapper = defaultTdRendWrapper();
    1598       13146 :     initRotMatrix( inputIsm->rot_mat_prev );
    1599       13146 :     set_zero( inputIsm->prev_pan_gains, MAX_OUTPUT_CHANNELS );
    1600             : 
    1601      105168 :     for ( i = 0; i < (int16_t) ( sizeof( inputIsm->splitTdRendWrappers ) / sizeof( *inputIsm->splitTdRendWrappers ) ); ++i )
    1602             :     {
    1603       92022 :         inputIsm->splitTdRendWrappers[i] = defaultTdRendWrapper();
    1604       92022 :         inputIsm->splitTdRendWrappers[i].hHrtfTD = &hrtfs->hHrtfTD;
    1605             :     }
    1606             : 
    1607       13146 :     inputIsm->hOMasa = NULL;
    1608             : 
    1609       13146 :     error = IVAS_ERR_OK;
    1610       13146 :     inputIsm->tdRendWrapper.hHrtfTD = &hrtfs->hHrtfTD;
    1611             : 
    1612       13146 :     if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL || outConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || outConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    1613             :     {
    1614             : #ifdef NONBE_1377_REND_DIRATT_CONF
    1615        1440 :         if ( ( error = ivas_td_binaural_open_ext( &inputIsm->tdRendWrapper, inConfig, hRendCfg, NULL, *rendCtx.pOutSampleRate, inputIsm->object_id ) ) != IVAS_ERR_OK )
    1616             : #else
    1617             :         if ( ( error = ivas_td_binaural_open_ext( &inputIsm->tdRendWrapper, inConfig, hRendCfg, NULL, *rendCtx.pOutSampleRate ) ) != IVAS_ERR_OK )
    1618             : #endif
    1619             :         {
    1620           0 :             return error;
    1621             :         }
    1622             : 
    1623             :         /* Open TD renderer wrappers */
    1624       11520 :         for ( i = 0; i < MAX_HEAD_ROT_POSES - 1; ++i )
    1625             :         {
    1626             : #ifdef NONBE_1377_REND_DIRATT_CONF
    1627       10080 :             if ( ( error = ivas_td_binaural_open_ext( &inputIsm->splitTdRendWrappers[i], inConfig, hRendCfg, NULL, *inputIsm->base.ctx.pOutSampleRate, inputIsm->object_id ) ) != IVAS_ERR_OK )
    1628             : #else
    1629             :             if ( ( error = ivas_td_binaural_open_ext( &inputIsm->splitTdRendWrappers[i], inConfig, hRendCfg, NULL, *inputIsm->base.ctx.pOutSampleRate ) ) != IVAS_ERR_OK )
    1630             : #endif
    1631             :             {
    1632           0 :                 return error;
    1633             :             }
    1634             : 
    1635             :             /* Assert same delay as main TD renderer */
    1636       10080 :             assert( inputIsm->splitTdRendWrappers[i].binaural_latency_ns == inputIsm->tdRendWrapper.binaural_latency_ns );
    1637             :         }
    1638             :     }
    1639       11706 :     else if ( outConfig == IVAS_AUDIO_CONFIG_MASA1 || outConfig == IVAS_AUDIO_CONFIG_MASA2 )
    1640             :     {
    1641          36 :         if ( ( error = initIsmMasaRendering( inputIsm, *rendCtx.pOutSampleRate ) ) != IVAS_ERR_OK )
    1642             :         {
    1643           0 :             return error;
    1644             :         }
    1645             :     }
    1646             :     else
    1647             :     {
    1648             : #ifdef NONBE_1377_REND_DIRATT_CONF
    1649       11670 :         if ( ( error = ivas_td_binaural_open_ext( &inputIsm->tdRendWrapper, inConfig, hRendCfg, NULL, *rendCtx.pOutSampleRate, inputIsm->object_id ) ) != IVAS_ERR_OK )
    1650             : #else
    1651             :         if ( ( error = ivas_td_binaural_open_ext( &inputIsm->tdRendWrapper, inConfig, hRendCfg, NULL, *rendCtx.pOutSampleRate ) ) != IVAS_ERR_OK )
    1652             : #endif
    1653             :         {
    1654           0 :             return error;
    1655             :         }
    1656             : 
    1657       11670 :         if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    1658             :         {
    1659        1440 :             if ( ( error = ivas_reverb_open( &( inputIsm->hReverb ), hrtfs->hHrtfStatistics, hRendCfg, *rendCtx.pOutSampleRate ) ) != IVAS_ERR_OK )
    1660             :             {
    1661           0 :                 return error;
    1662             :             }
    1663             :         }
    1664       10230 :         else if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
    1665             :         {
    1666        1440 :             if ( ( error = ivas_rend_openCrend( &inputIsm->crendWrapper, IVAS_AUDIO_CONFIG_7_1_4, outConfig, hRendCfg, hrtfs->hSetOfHRTF, hrtfs->hHrtfStatistics, *rendCtx.pOutSampleRate, 1, rendCtx.pSplitRendWrapper != NULL ? rendCtx.pSplitRendWrapper->multiBinPoseData.num_poses : 1 ) ) != IVAS_ERR_OK )
    1667             :             {
    1668           0 :                 return error;
    1669             :             }
    1670             :         }
    1671             :     }
    1672             : 
    1673       13146 :     return IVAS_ERR_OK;
    1674             : }
    1675             : 
    1676             : 
    1677       33528 : static void clearInputIsm(
    1678             :     input_ism *inputIsm )
    1679             : {
    1680             :     rendering_context rendCtx;
    1681             :     int16_t i;
    1682             : 
    1683       33528 :     rendCtx = inputIsm->base.ctx;
    1684             : 
    1685       33528 :     freeInputBaseBufferData( &inputIsm->base.inputBuffer.data );
    1686       33528 :     ivas_TD_RINGBUF_Close( &inputIsm->base.delayBuffer );
    1687             : 
    1688       33528 :     initRendInputBase( &inputIsm->base, IVAS_AUDIO_CONFIG_INVALID, 0, rendCtx, NULL, 0 );
    1689             : 
    1690             :     /* Free input's internal handles */
    1691       33528 :     ivas_rend_closeCrend( &inputIsm->crendWrapper );
    1692             : 
    1693       33528 :     ivas_reverb_close( &inputIsm->hReverb );
    1694             : 
    1695       33528 :     if ( inputIsm->tdRendWrapper.hBinRendererTd != NULL )
    1696             :     {
    1697       13110 :         ivas_td_binaural_close( &inputIsm->tdRendWrapper.hBinRendererTd );
    1698             :     }
    1699             : 
    1700      268224 :     for ( i = 0; i < (int16_t) ( sizeof( inputIsm->splitTdRendWrappers ) / sizeof( *inputIsm->splitTdRendWrappers ) ); ++i )
    1701             :     {
    1702      234696 :         ivas_td_binaural_close( &inputIsm->splitTdRendWrappers[i].hBinRendererTd );
    1703             :     }
    1704             : 
    1705       33528 :     ivas_omasa_ana_close( &( inputIsm->hOMasa ) );
    1706             : 
    1707       33528 :     return;
    1708             : }
    1709             : 
    1710             : 
    1711         372 : static void copyLsConversionMatrixToPanMatrix(
    1712             :     const LS_CONVERSION_MATRIX *lsConvMatrix,
    1713             :     pan_matrix panMatrix )
    1714             : {
    1715             :     int16_t i;
    1716             :     int16_t inCh, outCh;
    1717             :     int16_t numNonZeroGains;
    1718             :     int16_t numColumns;
    1719             : 
    1720             :     /* Index 0 is special and describes the following values */
    1721         372 :     numNonZeroGains = lsConvMatrix[0].index;
    1722         372 :     numColumns = (int16_t) lsConvMatrix[0].value;
    1723             : 
    1724        3924 :     for ( i = 1; i < numNonZeroGains + 1; ++i )
    1725             :     {
    1726        3552 :         inCh = lsConvMatrix[i].index / numColumns;
    1727        3552 :         outCh = lsConvMatrix[i].index % numColumns;
    1728             : 
    1729        3552 :         panMatrix[inCh][outCh] = lsConvMatrix[i].value;
    1730             :     }
    1731             : 
    1732         372 :     return;
    1733             : }
    1734             : 
    1735             : 
    1736       11638 : static void setZeroPanMatrix(
    1737             :     pan_matrix panMatrix )
    1738             : {
    1739             :     int16_t i;
    1740             : 
    1741      197846 :     for ( i = 0; i < MAX_INPUT_CHANNELS; ++i )
    1742             :     {
    1743      186208 :         set_zero( panMatrix[i], MAX_OUTPUT_CHANNELS );
    1744             :     }
    1745             : 
    1746       11638 :     return;
    1747             : }
    1748             : 
    1749             : 
    1750             : /* Note: this only sets non-zero elements, call setZeroPanMatrix() to init first. */
    1751         978 : static void fillIdentityPanMatrix(
    1752             :     pan_matrix panMatrix )
    1753             : {
    1754             :     int16_t i;
    1755             : 
    1756       16626 :     for ( i = 0; i < min( MAX_INPUT_CHANNELS, MAX_OUTPUT_CHANNELS ); ++i )
    1757             :     {
    1758       15648 :         panMatrix[i][i] = 1.0f;
    1759             :     }
    1760             : 
    1761         978 :     return;
    1762             : }
    1763             : 
    1764             : 
    1765         174 : static ivas_error initMcPanGainsWithIdentMatrix(
    1766             :     input_mc *inputMc )
    1767             : {
    1768         174 :     fillIdentityPanMatrix( inputMc->panGains );
    1769             : 
    1770         174 :     return IVAS_ERR_OK;
    1771             : }
    1772             : 
    1773             : 
    1774         600 : static ivas_error initMcPanGainsWithConversionMapping(
    1775             :     input_mc *inputMc,
    1776             :     const AUDIO_CONFIG outConfig )
    1777             : {
    1778             :     AUDIO_CONFIG ivasConfigIn, ivasConfigOut;
    1779             :     int16_t i;
    1780             : 
    1781         600 :     ivasConfigIn = inputMc->base.inConfig;
    1782         600 :     ivasConfigOut = outConfig;
    1783             : 
    1784             :     /* Find conversion mapping for current I/O config pair.
    1785             :      * Stay with default panning matrix if conversion_matrix is NULL */
    1786       15480 :     for ( i = 0; i < LS_SETUP_CONVERSION_NUM_MAPPINGS; ++i )
    1787             :     {
    1788       15480 :         if ( ls_conversion_mapping[i].input_config == ivasConfigIn && ls_conversion_mapping[i].output_config == ivasConfigOut )
    1789             :         {
    1790             :             /* Mapping found with valid matrix - copy */
    1791         600 :             if ( ls_conversion_mapping[i].conversion_matrix != NULL )
    1792             :             {
    1793         372 :                 copyLsConversionMatrixToPanMatrix( ls_conversion_mapping[i].conversion_matrix, inputMc->panGains );
    1794             :             }
    1795             :             /* Mapping found with NULL matrix - use identity matrix */
    1796             :             else
    1797             :             {
    1798         228 :                 fillIdentityPanMatrix( inputMc->panGains );
    1799             :             }
    1800             : 
    1801         600 :             return IVAS_ERR_OK;
    1802             :         }
    1803             :     }
    1804             : 
    1805           0 :     return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Missing multichannel conversion mapping" );
    1806             : }
    1807             : 
    1808             : 
    1809        1080 : static ivas_error initMcPanGainsWithEfap(
    1810             :     input_mc *inputMc,
    1811             :     const AUDIO_CONFIG outConfig )
    1812             : {
    1813             :     int16_t i;
    1814             :     int16_t numNonLfeInChannels;
    1815             :     int16_t inLfeChIdx, outChIdx;
    1816             :     const float *spkAzi, *spkEle;
    1817             :     ivas_error error;
    1818             : 
    1819        1080 :     if ( inputMc->base.inConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM )
    1820             :     {
    1821         198 :         if ( ( error = getNumNonLfeChannelsInSpeakerLayout( inputMc->base.inConfig, &numNonLfeInChannels ) ) != IVAS_ERR_OK )
    1822             :         {
    1823           0 :             return error;
    1824             :         }
    1825             : 
    1826         198 :         if ( ( error = getSpeakerAzimuths( inputMc->base.inConfig, &spkAzi ) ) != IVAS_ERR_OK )
    1827             :         {
    1828           0 :             return error;
    1829             :         }
    1830             : 
    1831         198 :         if ( ( error = getSpeakerElevations( inputMc->base.inConfig, &spkEle ) ) != IVAS_ERR_OK )
    1832             :         {
    1833           0 :             return error;
    1834             :         }
    1835             : 
    1836         198 :         inLfeChIdx = LFE_CHANNEL;
    1837             :     }
    1838             :     else
    1839             :     {
    1840         882 :         numNonLfeInChannels = inputMc->customLsInput.num_spk;
    1841         882 :         spkAzi = inputMc->customLsInput.ls_azimuth;
    1842         882 :         spkEle = inputMc->customLsInput.ls_elevation;
    1843         882 :         inLfeChIdx = -1;
    1844         882 :         if ( inputMc->customLsInput.num_lfe > 0 )
    1845             :         {
    1846           0 :             inLfeChIdx = inputMc->customLsInput.lfe_idx[0];
    1847             :         }
    1848             :     }
    1849             : 
    1850        7542 :     for ( i = 0, outChIdx = 0; i < numNonLfeInChannels; ++i, ++outChIdx )
    1851             :     {
    1852        6462 :         if ( i == inLfeChIdx )
    1853             :         {
    1854          90 :             ++outChIdx;
    1855             :         }
    1856             : 
    1857        6462 :         if ( ( error = getEfapGains( *inputMc->base.ctx.pEfapOutWrapper, spkAzi[i], spkEle[i], inputMc->panGains[outChIdx] ) ) != IVAS_ERR_OK )
    1858             :         {
    1859           0 :             return error;
    1860             :         }
    1861             :     }
    1862             : 
    1863        1080 :     if ( outConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM && inLfeChIdx >= 0 )
    1864             :     {
    1865          72 :         inputMc->panGains[inLfeChIdx][LFE_CHANNEL] = 1;
    1866             :     }
    1867        1008 :     else if ( inputMc->base.ctx.pCustomLsOut->num_lfe > 0 && inLfeChIdx >= 0 )
    1868             :     {
    1869           0 :         inputMc->panGains[inLfeChIdx][inputMc->base.ctx.pCustomLsOut->lfe_idx[0]] = 1;
    1870             :     }
    1871             : 
    1872        1080 :     return IVAS_ERR_OK;
    1873             : }
    1874             : 
    1875             : 
    1876    46268116 : static ivas_error getRendInputNumChannels(
    1877             :     const void *rendInput,
    1878             :     int16_t *numInChannels )
    1879             : {
    1880             :     /* Using a void pointer for this function to be reusable for any input type (input_ism, input_mc, input_sba).
    1881             :         Assumptions:        - input_base is always the first member in the input struct    */
    1882             : 
    1883             :     ivas_error error;
    1884             :     const input_base *pInputBase;
    1885             :     const input_mc *pInputMc;
    1886             : 
    1887    46268116 :     pInputBase = (const input_base *) rendInput;
    1888             : 
    1889    46268116 :     if ( pInputBase->inConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    1890             :     {
    1891     1297110 :         pInputMc = (const input_mc *) rendInput;
    1892     1297110 :         *numInChannels = pInputMc->customLsInput.num_spk + pInputMc->customLsInput.num_lfe;
    1893             :     }
    1894             :     else
    1895             :     {
    1896    44971006 :         if ( ( error = getAudioConfigNumChannels( pInputBase->inConfig, numInChannels ) ) != IVAS_ERR_OK )
    1897             :         {
    1898           0 :             return error;
    1899             :         }
    1900             :     }
    1901             : 
    1902    46268116 :     return IVAS_ERR_OK;
    1903             : }
    1904             : 
    1905             : 
    1906          96 : static ivas_error initMcPanGainsWithMonoOut(
    1907             :     input_mc *inputMc )
    1908             : {
    1909             :     int16_t i;
    1910             :     int16_t numInChannels;
    1911             :     int16_t readIdx;
    1912             :     int16_t writeIdx;
    1913             :     bool skipSideSpeakers;
    1914             :     ivas_error error;
    1915             : 
    1916          96 :     if ( ( error = getRendInputNumChannels( inputMc, &numInChannels ) ) != IVAS_ERR_OK )
    1917             :     {
    1918           0 :         return error;
    1919             :     }
    1920             : 
    1921          96 :     if ( inputMc->base.inConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    1922             :     {
    1923           0 :         for ( i = 0; i < numInChannels; ++i )
    1924             :         {
    1925             :             /* It's OK to also set gain 1 for LFE input channels here.
    1926             :              * Correct LFE handling will be applied within updateMcPanGains() */
    1927           0 :             inputMc->panGains[i][0] = 1.f;
    1928             :         }
    1929             :     }
    1930          96 :     else if ( inputMc->base.inConfig == IVAS_AUDIO_CONFIG_STEREO )
    1931             :     {
    1932             :         /* Special case for STEREO to MONO: Passive downmix (L+R)/2 */
    1933          24 :         inputMc->panGains[0][0] = 0.5;
    1934          24 :         inputMc->panGains[1][0] = 0.5;
    1935             :     }
    1936             :     else
    1937             :     {
    1938             :         /* ls_conversion_cicpX_stereo contains gains for side speakers.
    1939             :          * These should be skipped with 5.1+X inputs. */
    1940          72 :         skipSideSpeakers = false;
    1941          72 :         if ( inputMc->base.inConfig == IVAS_AUDIO_CONFIG_5_1_2 || inputMc->base.inConfig == IVAS_AUDIO_CONFIG_5_1_4 )
    1942             :         {
    1943          36 :             skipSideSpeakers = true;
    1944             :         }
    1945         720 :         for ( readIdx = 0, writeIdx = 0; writeIdx < numInChannels; ++readIdx, ++writeIdx )
    1946             :         {
    1947         648 :             if ( skipSideSpeakers && readIdx == 4 )
    1948             :             {
    1949             :                 /* Skip gains for side speakers in lookup table */
    1950          36 :                 readIdx += 2;
    1951             :             }
    1952             : 
    1953         648 :             inputMc->panGains[writeIdx][0] = ls_conversion_cicpX_mono[readIdx][0];
    1954             :         }
    1955             :     }
    1956             : 
    1957          96 :     return IVAS_ERR_OK;
    1958             : }
    1959             : 
    1960             : 
    1961          72 : static ivas_error initMcPanGainsWithStereoLookup(
    1962             :     input_mc *inputMc )
    1963             : {
    1964             :     int16_t readIdx;
    1965             :     int16_t writeIdx;
    1966             :     bool skipSideSpeakers;
    1967             :     int16_t numInChannels;
    1968             :     ivas_error error;
    1969             : 
    1970             :     /* Special case - MONO input.
    1971             :      * Use gains for center CICP speaker and return early. */
    1972          72 :     if ( inputMc->base.inConfig == IVAS_AUDIO_CONFIG_MONO )
    1973             :     {
    1974           0 :         inputMc->panGains[0][0] = ls_conversion_cicpX_stereo[2][0];
    1975           0 :         inputMc->panGains[0][1] = ls_conversion_cicpX_stereo[2][1];
    1976           0 :         return IVAS_ERR_OK;
    1977             :     }
    1978             : 
    1979             :     /* ls_conversion_cicpX_stereo contains gains for side speakers.
    1980             :      * These should be skipped with 5.1+X inputs. */
    1981          72 :     skipSideSpeakers = false;
    1982          72 :     if ( inputMc->base.inConfig == IVAS_AUDIO_CONFIG_5_1_2 || inputMc->base.inConfig == IVAS_AUDIO_CONFIG_5_1_4 )
    1983             :     {
    1984          36 :         skipSideSpeakers = true;
    1985             :     }
    1986             : 
    1987          72 :     if ( ( error = getRendInputNumChannels( inputMc, &numInChannels ) ) != IVAS_ERR_OK )
    1988             :     {
    1989           0 :         return error;
    1990             :     }
    1991             : 
    1992         720 :     for ( readIdx = 0, writeIdx = 0; writeIdx < numInChannels; ++readIdx, ++writeIdx )
    1993             :     {
    1994         648 :         if ( skipSideSpeakers && readIdx == 4 )
    1995             :         {
    1996             :             /* Skip gains for side speakers in lookup table */
    1997          36 :             readIdx += 2;
    1998             :         }
    1999             : 
    2000         648 :         inputMc->panGains[writeIdx][0] = ls_conversion_cicpX_stereo[readIdx][0];
    2001         648 :         inputMc->panGains[writeIdx][1] = ls_conversion_cicpX_stereo[readIdx][1];
    2002             :     }
    2003             : 
    2004          72 :     return IVAS_ERR_OK;
    2005             : }
    2006             : 
    2007             : 
    2008             : /* Returns 1 (true) if configs A and B are equal, otherwise returns 0 (false).
    2009             :  * If both configs are custom LS layouts, layout details are compared to determine equality. */
    2010        2052 : static bool configsAreEqual(
    2011             :     const AUDIO_CONFIG configA,
    2012             :     const LSSETUP_CUSTOM_STRUCT customLsA,
    2013             :     const AUDIO_CONFIG configB,
    2014             :     const LSSETUP_CUSTOM_STRUCT customLsB )
    2015             : {
    2016             :     int16_t i;
    2017             : 
    2018             :     /* Both input and output are custom LS - compare structs */
    2019        2052 :     if ( configA == IVAS_AUDIO_CONFIG_LS_CUSTOM && configB == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    2020             :     {
    2021         108 :         if ( customLsA.num_spk != customLsB.num_spk )
    2022             :         {
    2023          90 :             return false;
    2024             :         }
    2025             : 
    2026          18 :         if ( customLsA.num_lfe != customLsB.num_lfe )
    2027             :         {
    2028           0 :             return false;
    2029             :         }
    2030             : 
    2031          18 :         if ( customLsA.is_planar_setup != customLsB.is_planar_setup )
    2032             :         {
    2033           0 :             return false;
    2034             :         }
    2035             : 
    2036         234 :         for ( i = 0; i < customLsA.num_spk; ++i )
    2037             :         {
    2038             :             /* Compare to nearest degree (hence the int16_t cast) */
    2039         216 :             if ( (int16_t) customLsA.ls_azimuth[i] != (int16_t) customLsB.ls_azimuth[i] ||
    2040         216 :                  (int16_t) customLsA.ls_elevation[i] != (int16_t) customLsB.ls_elevation[i] )
    2041             :             {
    2042           0 :                 return false;
    2043             :             }
    2044             :         }
    2045          18 :         for ( i = 0; i < customLsA.num_lfe; ++i )
    2046             :         {
    2047           0 :             if ( customLsA.lfe_idx[i] != customLsB.lfe_idx[i] )
    2048             :             {
    2049           0 :                 return false;
    2050             :             }
    2051             :         }
    2052             : 
    2053          18 :         return true;
    2054             :     }
    2055             : 
    2056             :     /* Otherwise it's enough to compare config enums */
    2057        1944 :     return configA == configB;
    2058             : }
    2059             : 
    2060             : 
    2061        2052 : static ivas_error updateLfePanGainsForMcOut(
    2062             :     input_mc *inputMc,
    2063             :     const AUDIO_CONFIG outConfig )
    2064             : {
    2065             :     int16_t i, numLfeIn, numOutChannels;
    2066             :     ivas_error error;
    2067        2052 :     error = IVAS_ERR_OK;
    2068             : 
    2069             :     /* If panning is not required, simply return */
    2070        2052 :     if ( !inputMc->lfeRouting.pan_lfe )
    2071             :     {
    2072        2052 :         return error;
    2073             :     }
    2074             : 
    2075           0 :     numLfeIn = getNumLfeChannels( inputMc );
    2076             : 
    2077           0 :     if ( outConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    2078             :     {
    2079           0 :         numOutChannels = inputMc->base.ctx.pCustomLsOut->num_spk + inputMc->base.ctx.pCustomLsOut->num_lfe;
    2080             :     }
    2081             :     else
    2082             :     {
    2083           0 :         if ( ( error = getAudioConfigNumChannels( outConfig, &numOutChannels ) ) != IVAS_ERR_OK )
    2084             :         {
    2085           0 :             return error;
    2086             :         }
    2087             :     }
    2088             : 
    2089           0 :     for ( i = 0; i < numLfeIn; i++ )
    2090             :     {
    2091             :         /* panning gains */
    2092           0 :         if ( ( error = getEfapGains( *inputMc->base.ctx.pEfapOutWrapper, inputMc->lfeRouting.lfeOutputAzimuth, inputMc->lfeRouting.lfeOutputElevation, inputMc->lfeRouting.lfePanMtx[i] ) ) != IVAS_ERR_OK )
    2093             :         {
    2094           0 :             return error;
    2095             :         }
    2096             : 
    2097             :         /* linear input gain */
    2098           0 :         v_multc( inputMc->lfeRouting.lfePanMtx[i], inputMc->lfeRouting.lfeInputGain, inputMc->lfeRouting.lfePanMtx[i], numOutChannels );
    2099             :     }
    2100             : 
    2101           0 :     return error;
    2102             : }
    2103             : 
    2104             : 
    2105         540 : static ivas_error updateLfePanGainsForAmbiOut(
    2106             :     input_mc *inputMc,
    2107             :     const AUDIO_CONFIG outConfig )
    2108             : {
    2109             :     int16_t i;
    2110             :     int16_t numLfeIn, outAmbiOrder;
    2111             :     ivas_error error;
    2112         540 :     error = IVAS_ERR_OK;
    2113             : 
    2114             :     /* If panning is not required, simply return */
    2115         540 :     if ( !inputMc->lfeRouting.pan_lfe )
    2116             :     {
    2117         540 :         return error;
    2118             :     }
    2119             : 
    2120           0 :     if ( ( error = getAmbisonicsOrder( outConfig, &outAmbiOrder ) ) != IVAS_ERR_OK )
    2121             :     {
    2122           0 :         return error;
    2123             :     }
    2124             : 
    2125           0 :     numLfeIn = getNumLfeChannels( inputMc );
    2126             : 
    2127           0 :     for ( i = 0; i < numLfeIn; i++ )
    2128             :     {
    2129             :         /* panning gains */
    2130           0 :         ivas_dirac_dec_get_response( (int16_t) inputMc->lfeRouting.lfeOutputAzimuth, (int16_t) inputMc->lfeRouting.lfeOutputElevation, inputMc->lfeRouting.lfePanMtx[i], outAmbiOrder );
    2131             : 
    2132             :         /* linear input gain */
    2133           0 :         v_multc( inputMc->lfeRouting.lfePanMtx[i], inputMc->lfeRouting.lfeInputGain, inputMc->lfeRouting.lfePanMtx[i], RENDERER_MAX_OUTPUT_CHANNELS );
    2134             :     }
    2135             : 
    2136           0 :     return error;
    2137             : }
    2138             : 
    2139             : 
    2140        2052 : static ivas_error updateMcPanGainsForMcOut(
    2141             :     input_mc *inputMc,
    2142             :     const AUDIO_CONFIG outConfig )
    2143             : {
    2144             :     ivas_error error;
    2145             : 
    2146             :     /* "if" conditions below realize the following mapping:
    2147             : 
    2148             :     If in == out, use identity matrix, otherwise follow the table:
    2149             :     +-----------+-------------+---------------+-----------+--------------------+
    2150             :     |  in\out   |   MONO      |   STEREO      | custom LS |       other        |
    2151             :     +-----------+-------------+---------------+-----------+--------------------+
    2152             :     | MONO      | mono out    | EFAP          | EFAP      | EFAP               |
    2153             :     | custom LS | mono out    | EFAP          | EFAP      | EFAP               |
    2154             :     | other     | mono lookup | stereo lookup | EFAP      | conversion mapping |
    2155             :     +-----------+-------------+---------------+-----------+--------------------+
    2156             :     */
    2157             : 
    2158        2052 :     if ( configsAreEqual( inputMc->base.inConfig, inputMc->customLsInput, outConfig, *inputMc->base.ctx.pCustomLsOut ) )
    2159             :     {
    2160         174 :         error = initMcPanGainsWithIdentMatrix( inputMc );
    2161             :     }
    2162        1878 :     else if ( outConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM ||
    2163        1662 :               inputMc->base.inConfig == IVAS_AUDIO_CONFIG_MONO ||
    2164        1560 :               inputMc->base.inConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    2165             :     {
    2166        1110 :         if ( ( inputMc->base.inConfig == IVAS_AUDIO_CONFIG_MONO ) && ( inputMc->nonDiegeticPan ) )
    2167             :         {
    2168          30 :             inputMc->panGains[0][0] = ( inputMc->nonDiegeticPanGain + 1.f ) * 0.5f;
    2169          30 :             inputMc->panGains[0][1] = 1.f - inputMc->panGains[0][0];
    2170          30 :             error = IVAS_ERR_OK;
    2171             :         }
    2172             :         else
    2173             :         {
    2174        1080 :             error = initMcPanGainsWithEfap( inputMc, outConfig );
    2175             :         }
    2176             :     }
    2177         768 :     else if ( outConfig == IVAS_AUDIO_CONFIG_MONO )
    2178             :     {
    2179          96 :         error = initMcPanGainsWithMonoOut( inputMc );
    2180             :     }
    2181         672 :     else if ( outConfig == IVAS_AUDIO_CONFIG_STEREO )
    2182             :     {
    2183          72 :         error = initMcPanGainsWithStereoLookup( inputMc );
    2184             :     }
    2185             :     else /* default */
    2186             :     {
    2187         600 :         error = initMcPanGainsWithConversionMapping( inputMc, outConfig );
    2188             :     }
    2189             : 
    2190             :     /* check for errors from above block */
    2191        2052 :     if ( error != IVAS_ERR_OK )
    2192             :     {
    2193           0 :         return error;
    2194             :     }
    2195             : 
    2196             :     /* update LFE panning */
    2197        2052 :     error = updateLfePanGainsForMcOut( inputMc, outConfig );
    2198             : 
    2199        2052 :     return error;
    2200             : }
    2201             : 
    2202             : 
    2203         540 : static ivas_error updateMcPanGainsForAmbiOut(
    2204             :     input_mc *inputMc,
    2205             :     const AUDIO_CONFIG outConfig )
    2206             : {
    2207             :     int16_t ch_in, ch_out, lfeIdx;
    2208             :     int16_t numNonLfeInChannels, outAmbiOrder;
    2209             :     const float *spkAzi, *spkEle;
    2210             :     ivas_error error;
    2211             : 
    2212         540 :     if ( ( error = getAmbisonicsOrder( outConfig, &outAmbiOrder ) ) != IVAS_ERR_OK )
    2213             :     {
    2214           0 :         return error;
    2215             :     }
    2216             : 
    2217         540 :     if ( inputMc->base.inConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM )
    2218             :     {
    2219         324 :         if ( ( error = getNumNonLfeChannelsInSpeakerLayout( inputMc->base.inConfig, &numNonLfeInChannels ) ) != IVAS_ERR_OK )
    2220             :         {
    2221           0 :             return error;
    2222             :         }
    2223             : 
    2224         324 :         if ( ( error = getSpeakerAzimuths( inputMc->base.inConfig, &spkAzi ) ) != IVAS_ERR_OK )
    2225             :         {
    2226           0 :             return error;
    2227             :         }
    2228             : 
    2229         324 :         if ( ( error = getSpeakerElevations( inputMc->base.inConfig, &spkEle ) ) != IVAS_ERR_OK )
    2230             :         {
    2231           0 :             return error;
    2232             :         }
    2233             : 
    2234        2232 :         for ( ch_in = 0, ch_out = 0; ch_in < numNonLfeInChannels; ++ch_in, ++ch_out )
    2235             :         {
    2236        1908 :             if ( ch_in == LFE_CHANNEL )
    2237             :             {
    2238         216 :                 ++ch_out;
    2239             :             }
    2240        1908 :             ivas_dirac_dec_get_response( (int16_t) spkAzi[ch_in], (int16_t) spkEle[ch_in], inputMc->panGains[ch_out], outAmbiOrder );
    2241             :         }
    2242             :     }
    2243             :     else
    2244             :     {
    2245         216 :         numNonLfeInChannels = inputMc->customLsInput.num_spk;
    2246         216 :         spkAzi = inputMc->customLsInput.ls_azimuth;
    2247         216 :         spkEle = inputMc->customLsInput.ls_elevation;
    2248             : 
    2249        1620 :         for ( ch_in = 0, ch_out = 0; ch_in < numNonLfeInChannels; ++ch_in, ++ch_out )
    2250             :         {
    2251        1404 :             for ( lfeIdx = 0; lfeIdx < inputMc->customLsInput.num_lfe; ++lfeIdx )
    2252             :             {
    2253           0 :                 if ( inputMc->customLsInput.lfe_idx[lfeIdx] == ch_in )
    2254             :                 {
    2255           0 :                     ++ch_out;
    2256           0 :                     break;
    2257             :                 }
    2258             :             }
    2259             : 
    2260        1404 :             ivas_dirac_dec_get_response( (int16_t) spkAzi[ch_in], (int16_t) spkEle[ch_in], inputMc->panGains[ch_out], outAmbiOrder );
    2261             :         }
    2262             :     }
    2263             : 
    2264             :     /* update LFE panning */
    2265         540 :     if ( ( error = updateLfePanGainsForAmbiOut( inputMc, outConfig ) ) != IVAS_ERR_OK )
    2266             :     {
    2267           0 :         return error;
    2268             :     }
    2269             : 
    2270         540 :     return IVAS_ERR_OK;
    2271             : }
    2272             : 
    2273             : 
    2274        2860 : static ivas_error updateMcPanGains(
    2275             :     input_mc *inputMc,
    2276             :     const AUDIO_CONFIG outConfig )
    2277             : {
    2278             :     int16_t i;
    2279             :     ivas_error error;
    2280             : 
    2281             :     /* Reset to all zeros - some functions below only write non-zero elements. */
    2282        2860 :     setZeroPanMatrix( inputMc->panGains );
    2283             : 
    2284        2860 :     error = IVAS_ERR_OK;
    2285        2860 :     switch ( getAudioConfigType( outConfig ) )
    2286             :     {
    2287        1524 :         case IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED:
    2288        1524 :             error = updateMcPanGainsForMcOut( inputMc, outConfig );
    2289        1524 :             break;
    2290         540 :         case IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS:
    2291         540 :             error = updateMcPanGainsForAmbiOut( inputMc, outConfig );
    2292         540 :             break;
    2293         792 :         case IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL:
    2294             :             switch ( outConfig )
    2295             :             {
    2296         264 :                 case IVAS_AUDIO_CONFIG_BINAURAL:
    2297             :                 case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED:
    2298             :                 case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM:
    2299         264 :                     break; /* Do nothing */
    2300         528 :                 case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR:
    2301             :                 case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB:
    2302             :                     /* Prepare rendering to intermediate format */
    2303         528 :                     error = updateMcPanGainsForMcOut( inputMc, IVAS_AUDIO_CONFIG_7_1_4 );
    2304         528 :                     break;
    2305           0 :                 default:
    2306           0 :                     return IVAS_ERR_INVALID_OUTPUT_FORMAT;
    2307             :             }
    2308         792 :             break;
    2309           4 :         case IVAS_REND_AUDIO_CONFIG_TYPE_MASA:
    2310           4 :             break; /* Do nothing */
    2311           0 :         default:
    2312           0 :             return IVAS_ERR_INVALID_OUTPUT_FORMAT;
    2313             :     }
    2314             :     /* Check error here to keep switch statement more compact */
    2315        2860 :     if ( error != IVAS_ERR_OK )
    2316             :     {
    2317           0 :         return error;
    2318             :     }
    2319             : 
    2320             :     /* Copy LFE routing to pan gains array */
    2321        2860 :     if ( inputMc->base.inConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    2322             :     {
    2323        1260 :         for ( i = 0; i < inputMc->customLsInput.num_lfe; ++i )
    2324             :         {
    2325           0 :             mvr2r( inputMc->lfeRouting.lfePanMtx[i], inputMc->panGains[inputMc->customLsInput.lfe_idx[i]], RENDERER_MAX_OUTPUT_CHANNELS );
    2326             :         }
    2327             :     }
    2328             :     else
    2329             :     {
    2330             :         /* For code simplicity, always copy LFE gains. If config has no LFE, gains will be zero anyway. */
    2331        1600 :         mvr2r( inputMc->lfeRouting.lfePanMtx[0], inputMc->panGains[LFE_CHANNEL], RENDERER_MAX_OUTPUT_CHANNELS );
    2332             :     }
    2333             : 
    2334        2860 :     return IVAS_ERR_OK;
    2335             : }
    2336             : 
    2337             : 
    2338      680526 : static ivas_error initMcBinauralRendering(
    2339             :     input_mc *inputMc,
    2340             :     const AUDIO_CONFIG inConfig,
    2341             :     const AUDIO_CONFIG outConfig,
    2342             :     RENDER_CONFIG_DATA *hRendCfg,
    2343             :     IVAS_DEC_HRTF_CREND_HANDLE hMixconv,
    2344             :     HRTFS_STATISTICS_HANDLE hHrtfStatistics,
    2345             :     uint8_t reconfigureFlag )
    2346             : {
    2347             :     ivas_error error;
    2348             :     int16_t i;
    2349             :     int32_t binauralDelayNs;
    2350             :     int32_t outSampleRate;
    2351             :     int8_t useTDRend;
    2352             : 
    2353             :     /* Allocate TD binaural renderer for custom loudspeaker layouts (regardless of headrotation)
    2354             :       or planar MC layouts with headrotation, CREND for the rest */
    2355      680526 :     useTDRend = FALSE;
    2356      680526 :     if ( outConfig != IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
    2357             :     {
    2358      453684 :         if ( inConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM && outConfig != IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    2359             :         {
    2360      101712 :             useTDRend = TRUE;
    2361             :         }
    2362      351972 :         else if ( ( inConfig == IVAS_AUDIO_CONFIG_5_1 || inConfig == IVAS_AUDIO_CONFIG_7_1 ) &&
    2363      179664 :                   ( inputMc->base.ctx.pHeadRotData->headRotEnabled ) )
    2364             :         {
    2365      110568 :             useTDRend = TRUE;
    2366             :         }
    2367             :     }
    2368             : 
    2369             :     /* if TD renderer was open and we need to use CREND, close it */
    2370      680526 :     if ( !reconfigureFlag || ( !useTDRend && inputMc->tdRendWrapper.hBinRendererTd != NULL ) )
    2371             :     {
    2372         792 :         ivas_td_binaural_close( &inputMc->tdRendWrapper.hBinRendererTd );
    2373             :     }
    2374             : 
    2375      680526 :     if ( !reconfigureFlag || !useTDRend )
    2376             :     {
    2377     3747504 :         for ( i = 0; i < MAX_HEAD_ROT_POSES - 1; ++i )
    2378             :         {
    2379     3279066 :             if ( inputMc->splitTdRendWrappers[i].hBinRendererTd != NULL )
    2380             :             {
    2381           0 :                 ivas_td_binaural_close( &inputMc->splitTdRendWrappers[i].hBinRendererTd );
    2382             :             }
    2383             :         }
    2384             :     }
    2385             : 
    2386             :     /* if we need to use TD renderer and CREND was open, close it */
    2387      680526 :     if ( useTDRend )
    2388             :     {
    2389      212280 :         ivas_rend_closeCrend( &inputMc->crendWrapper );
    2390             :     }
    2391             : 
    2392      680526 :     if ( !reconfigureFlag || ( !useTDRend && outConfig != IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB && inputMc->hReverb != NULL ) )
    2393             :     {
    2394         792 :         ivas_reverb_close( &inputMc->hReverb );
    2395             :     }
    2396             : 
    2397      680526 :     if ( !reconfigureFlag || ( inConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM && !inputMc->base.ctx.pHeadRotData->headRotEnabled ) )
    2398             :     {
    2399      117936 :         if ( inputMc->efapInWrapper.hEfap != NULL )
    2400             :         {
    2401         216 :             efap_free_data( &inputMc->efapInWrapper.hEfap );
    2402             :         }
    2403             :     }
    2404             : 
    2405      680526 :     outSampleRate = *inputMc->base.ctx.pOutSampleRate;
    2406             : 
    2407      680526 :     if ( useTDRend && inputMc->tdRendWrapper.hBinRendererTd == NULL )
    2408             :     {
    2409             : #ifdef NONBE_1377_REND_DIRATT_CONF
    2410         192 :         if ( ( error = ivas_td_binaural_open_ext( &inputMc->tdRendWrapper, inConfig, hRendCfg, &inputMc->customLsInput, outSampleRate, 0 ) ) != IVAS_ERR_OK )
    2411             : #else
    2412             :         if ( ( error = ivas_td_binaural_open_ext( &inputMc->tdRendWrapper, inConfig, hRendCfg, &inputMc->customLsInput, outSampleRate ) ) != IVAS_ERR_OK )
    2413             : #endif
    2414             :         {
    2415           0 :             return error;
    2416             :         }
    2417             : 
    2418         192 :         if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || outConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    2419             :         {
    2420             :             /* Open TD renderer wrappers */
    2421           0 :             for ( i = 0; i < MAX_HEAD_ROT_POSES - 1; ++i )
    2422             :             {
    2423             : #ifdef NONBE_1377_REND_DIRATT_CONF
    2424           0 :                 if ( ( error = ivas_td_binaural_open_ext( &inputMc->splitTdRendWrappers[i], inConfig, hRendCfg, &inputMc->customLsInput, outSampleRate, 0 ) ) != IVAS_ERR_OK )
    2425             : #else
    2426             :                 if ( ( error = ivas_td_binaural_open_ext( &inputMc->splitTdRendWrappers[i], inConfig, hRendCfg, &inputMc->customLsInput, outSampleRate ) ) != IVAS_ERR_OK )
    2427             : #endif
    2428             :                 {
    2429           0 :                     return error;
    2430             :                 }
    2431             : 
    2432             :                 /* Assert same delay as main TD renderer */
    2433           0 :                 assert( inputMc->splitTdRendWrappers[i].binaural_latency_ns == inputMc->tdRendWrapper.binaural_latency_ns );
    2434             :             }
    2435             :         }
    2436             : 
    2437         192 :         if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB && inputMc->hReverb == NULL )
    2438             :         {
    2439          24 :             if ( ( error = ivas_reverb_open( &( inputMc->hReverb ), hHrtfStatistics, hRendCfg, outSampleRate ) ) != IVAS_ERR_OK )
    2440             :             {
    2441           0 :                 return error;
    2442             :             }
    2443             :         }
    2444             :     }
    2445      680334 :     else if ( !useTDRend && inputMc->crendWrapper == NULL )
    2446             :     {
    2447             :         /* open CREND */
    2448         912 :         if ( ( error = ivas_rend_openCrend( &inputMc->crendWrapper, ( inConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM ) ? IVAS_AUDIO_CONFIG_7_1_4 : inConfig, outConfig, hRendCfg, hMixconv, hHrtfStatistics,
    2449         456 :                                             outSampleRate, 1, ( ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) || ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ) ) ? inputMc->base.ctx.pSplitRendWrapper->multiBinPoseData.num_poses : 1 ) ) != IVAS_ERR_OK )
    2450             :         {
    2451           0 :             return error;
    2452             :         }
    2453             :     }
    2454             : 
    2455             :     /* Initialise the EFAP handle for rotation on input layout */
    2456      680526 :     if ( inConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM && inputMc->base.ctx.pHeadRotData->headRotEnabled && inputMc->efapInWrapper.hEfap == NULL )
    2457             :     {
    2458         180 :         if ( ( error = initEfap( &inputMc->efapInWrapper, inConfig, NULL ) ) != IVAS_ERR_OK )
    2459             :         {
    2460           0 :             return error;
    2461             :         }
    2462             :     }
    2463             : 
    2464             :     /* determine binaural delay ( used for aligning LFE to output signal ) */
    2465      680526 :     binauralDelayNs = max( ( inputMc->crendWrapper != NULL ) ? inputMc->crendWrapper->binaural_latency_ns : 0, inputMc->tdRendWrapper.binaural_latency_ns );
    2466      680526 :     inputMc->binauralDelaySmp = latencyNsToSamples( *inputMc->base.ctx.pOutSampleRate, binauralDelayNs );
    2467             : 
    2468      680526 :     if ( inputMc->binauralDelaySmp > MAX_BIN_DELAY_SAMPLES )
    2469             :     {
    2470           0 :         return IVAS_ERROR( IVAS_ERR_WRONG_PARAMS, "Invalid delay for LFE binaural rendering!)" );
    2471             :     }
    2472             : 
    2473      680526 :     return IVAS_ERR_OK;
    2474             : }
    2475             : 
    2476             : 
    2477           4 : static ivas_error initMcMasaRendering(
    2478             :     input_mc *inputMc,
    2479             :     const AUDIO_CONFIG inConfig,
    2480             :     const int32_t inSampleRate )
    2481             : {
    2482             :     ivas_error error;
    2483             : 
    2484           4 :     if ( inputMc->tdRendWrapper.hBinRendererTd != NULL )
    2485             :     {
    2486           0 :         ivas_td_binaural_close( &inputMc->tdRendWrapper.hBinRendererTd );
    2487             :     }
    2488             : 
    2489           4 :     ivas_rend_closeCrend( &inputMc->crendWrapper );
    2490             : 
    2491           4 :     ivas_reverb_close( &inputMc->hReverb );
    2492             : 
    2493           4 :     if ( inputMc->efapInWrapper.hEfap != NULL )
    2494             :     {
    2495           0 :         efap_free_data( &inputMc->efapInWrapper.hEfap );
    2496             :     }
    2497             : 
    2498           4 :     if ( ( error = ivas_mcmasa_ana_open( &inputMc->hMcMasa, inConfig, inSampleRate ) ) != IVAS_ERR_OK )
    2499             :     {
    2500           0 :         return error;
    2501             :     }
    2502             : 
    2503           4 :     return IVAS_ERR_OK;
    2504             : }
    2505             : 
    2506             : 
    2507        2860 : static lfe_routing defaultLfeRouting(
    2508             :     const AUDIO_CONFIG inConfig,
    2509             :     const LSSETUP_CUSTOM_STRUCT customLsIn,
    2510             :     const AUDIO_CONFIG outConfig,
    2511             :     const LSSETUP_CUSTOM_STRUCT customLsOut )
    2512             : {
    2513             :     int16_t i;
    2514             :     lfe_routing routing;
    2515             : 
    2516             :     /* Set all output gains to zero, then route each input LFE consecutively to the next available output LFE. */
    2517             : 
    2518       14300 :     for ( i = 0; i < RENDERER_MAX_INPUT_LFE_CHANNELS; ++i )
    2519             :     {
    2520       11440 :         set_zero( routing.lfePanMtx[i], RENDERER_MAX_OUTPUT_CHANNELS );
    2521             :     }
    2522             : 
    2523        2860 :     routing.pan_lfe = false;
    2524        2860 :     routing.lfeInputGain = 1.0f;
    2525             : 
    2526        2860 :     switch ( inConfig )
    2527             :     {
    2528        1174 :         case IVAS_AUDIO_CONFIG_5_1:
    2529             :         case IVAS_AUDIO_CONFIG_5_1_2:
    2530             :         case IVAS_AUDIO_CONFIG_5_1_4:
    2531             :         case IVAS_AUDIO_CONFIG_7_1:
    2532             :         case IVAS_AUDIO_CONFIG_7_1_4:
    2533        1174 :             routing.numLfeChannels = 1;
    2534        1174 :             break;
    2535        1260 :         case IVAS_AUDIO_CONFIG_LS_CUSTOM:
    2536        1260 :             routing.numLfeChannels = customLsIn.num_lfe;
    2537        1260 :             break;
    2538         426 :         default:
    2539         426 :             routing.numLfeChannels = 0;
    2540             :     }
    2541             : 
    2542        2860 :     switch ( outConfig )
    2543             :     {
    2544         900 :         case IVAS_AUDIO_CONFIG_5_1:
    2545             :         case IVAS_AUDIO_CONFIG_5_1_2:
    2546             :         case IVAS_AUDIO_CONFIG_5_1_4:
    2547             :         case IVAS_AUDIO_CONFIG_7_1:
    2548             :         case IVAS_AUDIO_CONFIG_7_1_4:
    2549         900 :             routing.lfePanMtx[0][LFE_CHANNEL] = 1.0f;
    2550         900 :             break;
    2551         234 :         case IVAS_AUDIO_CONFIG_LS_CUSTOM:
    2552         234 :             for ( i = 0; i < routing.numLfeChannels && i < customLsOut.num_lfe; ++i )
    2553             :             {
    2554           0 :                 routing.lfePanMtx[i][customLsOut.lfe_idx[i]] = 1.0f;
    2555             :             }
    2556         234 :             break;
    2557        1726 :         default:
    2558             :             /* Do nothing */
    2559        1726 :             break;
    2560             :     }
    2561             : 
    2562        2860 :     return routing;
    2563             : }
    2564             : 
    2565             : 
    2566        2230 : static ivas_error setRendInputActiveMc(
    2567             :     void *input,
    2568             :     const AUDIO_CONFIG inConfig,
    2569             :     const IVAS_REND_InputId id,
    2570             :     RENDER_CONFIG_DATA *hRendCfg,
    2571             :     hrtf_handles *hrtfs )
    2572             : {
    2573             :     int16_t i;
    2574             :     ivas_error error;
    2575             :     rendering_context rendCtx;
    2576             :     AUDIO_CONFIG outConfig;
    2577             :     input_mc *inputMc;
    2578             :     int16_t pos_idx;
    2579             : 
    2580        2230 :     inputMc = (input_mc *) input;
    2581        2230 :     rendCtx = inputMc->base.ctx;
    2582        2230 :     outConfig = *rendCtx.pOutConfig;
    2583             : 
    2584        2230 :     if ( !isIoConfigPairSupported( inConfig, outConfig ) )
    2585             :     {
    2586           0 :         return IVAS_ERR_IO_CONFIG_PAIR_NOT_SUPPORTED;
    2587             :     }
    2588             : 
    2589        2230 :     if ( ( error = allocateMcLfeDelayBuffer( &inputMc->lfeDelayBuffer, MAX_BIN_DELAY_SAMPLES ) ) != IVAS_ERR_OK )
    2590             :     {
    2591           0 :         return error;
    2592             :     }
    2593             : 
    2594        2230 :     if ( ( error = allocateInputBaseBufferData( &inputMc->bufferData, MAX_BUFFER_LENGTH ) ) != IVAS_ERR_OK )
    2595             :     {
    2596           0 :         return error;
    2597             :     }
    2598        2230 :     initRendInputBase( &inputMc->base, inConfig, id, rendCtx, inputMc->bufferData, MAX_BUFFER_LENGTH );
    2599             : 
    2600        2230 :     setZeroPanMatrix( inputMc->panGains );
    2601        2230 :     inputMc->customLsInput = defaultCustomLs();
    2602        2230 :     inputMc->tdRendWrapper = defaultTdRendWrapper();
    2603             : 
    2604        2230 :     if ( hrtfs->hHrtfTD )
    2605             :     {
    2606           0 :         inputMc->tdRendWrapper.binaural_latency_ns = (int32_t) ( hrtfs->hHrtfTD->latency_s * 1000000000.f );
    2607             :     }
    2608        2230 :     inputMc->tdRendWrapper.hHrtfTD = &hrtfs->hHrtfTD;
    2609        2230 :     inputMc->crendWrapper = NULL;
    2610        2230 :     inputMc->hReverb = NULL;
    2611        2230 :     inputMc->hMcMasa = NULL;
    2612             : 
    2613       20070 :     for ( pos_idx = 0; pos_idx < MAX_HEAD_ROT_POSES; pos_idx++ )
    2614             :     {
    2615       17840 :         initRotGains( inputMc->rot_gains_prev[pos_idx] );
    2616             :     }
    2617        2230 :     inputMc->lfeRouting = defaultLfeRouting( inConfig, inputMc->customLsInput, outConfig, *inputMc->base.ctx.pCustomLsOut );
    2618        2230 :     set_zero( inputMc->lfeDelayBuffer, MAX_BIN_DELAY_SAMPLES );
    2619        2230 :     inputMc->binauralDelaySmp = 0;
    2620             : 
    2621       17840 :     for ( i = 0; i < (int16_t) ( sizeof( inputMc->splitTdRendWrappers ) / sizeof( *inputMc->splitTdRendWrappers ) ); ++i )
    2622             :     {
    2623       15610 :         inputMc->splitTdRendWrappers[i] = defaultTdRendWrapper();
    2624       15610 :         if ( hrtfs->hHrtfTD )
    2625             :         {
    2626           0 :             inputMc->splitTdRendWrappers[i].binaural_latency_ns = (int32_t) ( hrtfs->hHrtfTD->latency_s * 1000000000.f );
    2627             :         }
    2628       15610 :         inputMc->splitTdRendWrappers[i].hHrtfTD = &hrtfs->hHrtfTD;
    2629             :     }
    2630             : 
    2631        2230 :     if ( getAudioConfigType( outConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL )
    2632             :     {
    2633         576 :         if ( ( error = initMcBinauralRendering( inputMc, inConfig, outConfig, hRendCfg, hrtfs->hSetOfHRTF, hrtfs->hHrtfStatistics, FALSE ) ) != IVAS_ERR_OK )
    2634             :         {
    2635           0 :             return error;
    2636             :         }
    2637             :     }
    2638             : 
    2639        2230 :     if ( outConfig == IVAS_AUDIO_CONFIG_MASA1 || outConfig == IVAS_AUDIO_CONFIG_MASA2 )
    2640             :     {
    2641           4 :         if ( ( error = initMcMasaRendering( inputMc, inConfig, *rendCtx.pOutSampleRate ) ) != IVAS_ERR_OK )
    2642             :         {
    2643           0 :             return error;
    2644             :         }
    2645             :     }
    2646             : 
    2647        2230 :     if ( ( error = updateMcPanGains( inputMc, outConfig ) ) != IVAS_ERR_OK )
    2648             :     {
    2649           0 :         return error;
    2650             :     }
    2651             : 
    2652        2230 :     return IVAS_ERR_OK;
    2653             : }
    2654             : 
    2655             : 
    2656        8382 : static void clearInputMc(
    2657             :     input_mc *inputMc )
    2658             : {
    2659             :     int16_t i;
    2660             :     rendering_context rendCtx;
    2661             : 
    2662        8382 :     rendCtx = inputMc->base.ctx;
    2663             : 
    2664        8382 :     freeMcLfeDelayBuffer( &inputMc->lfeDelayBuffer );
    2665        8382 :     freeInputBaseBufferData( &inputMc->bufferData );
    2666        8382 :     ivas_TD_RINGBUF_Close( &inputMc->base.delayBuffer );
    2667             : 
    2668        8382 :     initRendInputBase( &inputMc->base, IVAS_AUDIO_CONFIG_INVALID, 0, rendCtx, NULL, 0 );
    2669             : 
    2670             :     /* Free input's internal handles */
    2671        8382 :     if ( inputMc->efapInWrapper.hEfap != NULL )
    2672             :     {
    2673         594 :         efap_free_data( &inputMc->efapInWrapper.hEfap );
    2674             :     }
    2675             : 
    2676        8382 :     ivas_rend_closeCrend( &inputMc->crendWrapper );
    2677             : 
    2678        8382 :     ivas_reverb_close( &inputMc->hReverb );
    2679             : 
    2680        8382 :     if ( inputMc->tdRendWrapper.hBinRendererTd != NULL )
    2681             :     {
    2682         120 :         ivas_td_binaural_close( &inputMc->tdRendWrapper.hBinRendererTd );
    2683             :     }
    2684             : 
    2685       67056 :     for ( i = 0; i < (int16_t) ( sizeof( inputMc->splitTdRendWrappers ) / sizeof( *inputMc->splitTdRendWrappers ) ); ++i )
    2686             :     {
    2687       58674 :         if ( inputMc->splitTdRendWrappers[i].hBinRendererTd != NULL )
    2688             :         {
    2689           0 :             ivas_td_binaural_close( &inputMc->splitTdRendWrappers[i].hBinRendererTd );
    2690             :         }
    2691             :     }
    2692             : 
    2693        8382 :     ivas_mcmasa_ana_close( &( inputMc->hMcMasa ) );
    2694             : 
    2695        8382 :     return;
    2696             : }
    2697             : 
    2698             : 
    2699        1974 : static ivas_error initSbaPanGainsForMcOut(
    2700             :     input_sba *inputSba,
    2701             :     const AUDIO_CONFIG outConfig,
    2702             :     const LSSETUP_CUSTOM_STRUCT *outSetupCustom )
    2703             : {
    2704             :     int16_t ambiOrderIn;
    2705             :     int16_t chInIdx, chOutIdx;
    2706             :     float *tmpDecMtx, *readPtr;
    2707             :     IVAS_OUTPUT_SETUP hOutSetup;
    2708             :     ivas_error error;
    2709             : 
    2710        1974 :     if ( ( error = getAmbisonicsOrder( inputSba->base.inConfig, &ambiOrderIn ) ) != IVAS_ERR_OK )
    2711             :     {
    2712           0 :         return error;
    2713             :     }
    2714             : 
    2715        1974 :     if ( getAudioConfigType( outConfig ) != IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED )
    2716             :     {
    2717           0 :         assert( !"Invalid configuration" );
    2718             :         return IVAS_ERR_WRONG_PARAMS;
    2719             :     }
    2720             : 
    2721        1974 :     switch ( outConfig )
    2722             :     {
    2723         192 :         case IVAS_AUDIO_CONFIG_MONO:
    2724         192 :             hOutSetup.ls_azimuth = ls_azimuth_CICP1;
    2725         192 :             hOutSetup.ls_elevation = ls_elevation_CICP1;
    2726         192 :             ivas_output_init( &hOutSetup, outConfig );
    2727         192 :             break;
    2728        1512 :         case IVAS_AUDIO_CONFIG_STEREO:
    2729             :         case IVAS_AUDIO_CONFIG_5_1:
    2730             :         case IVAS_AUDIO_CONFIG_7_1:
    2731             :         case IVAS_AUDIO_CONFIG_5_1_2:
    2732             :         case IVAS_AUDIO_CONFIG_5_1_4:
    2733             :         case IVAS_AUDIO_CONFIG_7_1_4:
    2734        1512 :             ivas_output_init( &hOutSetup, outConfig );
    2735        1512 :             break;
    2736         270 :         case IVAS_AUDIO_CONFIG_LS_CUSTOM:
    2737         270 :             ivas_ls_custom_setup( &hOutSetup, outSetupCustom );
    2738         270 :             break;
    2739           0 :         default:
    2740           0 :             assert( !"Invalid speaker config" );
    2741             :             return IVAS_ERR_WRONG_PARAMS;
    2742             :     }
    2743             : 
    2744             :     /* obtain and copy over HOA decoding matrix */
    2745        1974 :     tmpDecMtx = NULL;
    2746        1974 :     if ( ( error = ivas_sba_get_hoa_dec_matrix( hOutSetup, &tmpDecMtx, ambiOrderIn ) ) != IVAS_ERR_OK )
    2747             :     {
    2748           0 :         return error;
    2749             :     }
    2750             : 
    2751        1974 :     readPtr = &tmpDecMtx[0];
    2752       18558 :     for ( chOutIdx = 0; chOutIdx < hOutSetup.nchan_out_woLFE + hOutSetup.num_lfe; ++chOutIdx )
    2753             :     {
    2754      281928 :         for ( chInIdx = 0; chInIdx < SBA_NHARM_HOA3; ++chInIdx )
    2755             :         {
    2756      265344 :             if ( hOutSetup.num_lfe > 0 && chOutIdx == hOutSetup.index_lfe[0] )
    2757             :             {
    2758       21120 :                 continue; /* nothing to be rendered to LFE */
    2759             :             }
    2760      244224 :             inputSba->hoaDecMtx[chInIdx][chOutIdx] = *readPtr++;
    2761             :         }
    2762             :     }
    2763             : 
    2764        1974 :     free( tmpDecMtx );
    2765             : 
    2766        1974 :     return IVAS_ERR_OK;
    2767             : }
    2768             : 
    2769             : 
    2770         576 : static ivas_error initSbaPanGainsForSbaOut(
    2771             :     input_sba *inputSba,
    2772             :     const AUDIO_CONFIG outConfig )
    2773             : {
    2774             :     ivas_error error;
    2775         576 :     error = IVAS_ERR_OK;
    2776             : 
    2777         576 :     if ( getAudioConfigType( outConfig ) != IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS )
    2778             :     {
    2779           0 :         assert( !"Invalid configuration" );
    2780             :         return IVAS_ERR_WRONG_PARAMS;
    2781             :     }
    2782             : 
    2783         576 :     fillIdentityPanMatrix( inputSba->hoaDecMtx );
    2784             : 
    2785         576 :     return error;
    2786             : }
    2787             : 
    2788             : 
    2789        3274 : static ivas_error updateSbaPanGains(
    2790             :     input_sba *inputSba,
    2791             :     const AUDIO_CONFIG outConfig,
    2792             :     RENDER_CONFIG_DATA *hRendCfg,
    2793             :     IVAS_DEC_HRTF_CREND_HANDLE hMixconv,
    2794             :     IVAS_DEC_HRTF_STATISTICS_HANDLE hHrtfStatistics )
    2795             : {
    2796             :     ivas_error error;
    2797             :     AUDIO_CONFIG inConfig;
    2798             :     rendering_context rendCtx;
    2799             : 
    2800             :     /* Reset to all zeros - some functions below only write non-zero elements. */
    2801        3274 :     setZeroPanMatrix( inputSba->hoaDecMtx );
    2802             : 
    2803        3274 :     inConfig = inputSba->base.inConfig;
    2804        3274 :     rendCtx = inputSba->base.ctx;
    2805             : 
    2806        3274 :     switch ( getAudioConfigType( outConfig ) )
    2807             :     {
    2808        1614 :         case IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED:
    2809        1614 :             error = initSbaPanGainsForMcOut( inputSba, outConfig, inputSba->base.ctx.pCustomLsOut );
    2810        1614 :             break;
    2811         576 :         case IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS:
    2812         576 :             error = initSbaPanGainsForSbaOut( inputSba, outConfig );
    2813         576 :             break;
    2814        1080 :         case IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL:
    2815             :             switch ( outConfig )
    2816             :             {
    2817           0 :                 case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED:
    2818             :                 case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM:
    2819             :                 {
    2820           0 :                     if ( hRendCfg->split_rend_config.rendererSelection == IVAS_BIN_RENDERER_TYPE_FASTCONV )
    2821             :                     {
    2822           0 :                         assert( *rendCtx.pOutSampleRate == 48000 && "split binaural fast conv mode is currently supported with 48k sampling rate only" );
    2823           0 :                         if ( ( error = ivas_rend_openCldfbRend( &inputSba->cldfbRendWrapper, inConfig, outConfig, &rendCtx.pSplitRendWrapper->multiBinPoseData, *rendCtx.pOutSampleRate ) ) != IVAS_ERR_OK )
    2824             :                         {
    2825           0 :                             return error;
    2826             :                         }
    2827             :                     }
    2828             :                     else
    2829             :                     {
    2830           0 :                         assert( ( *rendCtx.pOutSampleRate == 48000 ) && "split binaural crend mode is currently supported with 48k sampling rate only" );
    2831           0 :                         if ( ( error = ivas_rend_openMultiBinCrend( &inputSba->crendWrapper, inConfig, outConfig, &rendCtx.pSplitRendWrapper->multiBinPoseData, *rendCtx.pOutSampleRate ) ) != IVAS_ERR_OK )
    2832             :                         {
    2833           0 :                             return error;
    2834             :                         }
    2835             :                     }
    2836           0 :                     break;
    2837             :                 }
    2838         720 :                 case IVAS_AUDIO_CONFIG_BINAURAL:
    2839             :                 case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB:
    2840         720 :                     if ( hRendCfg->split_rend_config.rendererSelection == IVAS_BIN_RENDERER_TYPE_FASTCONV )
    2841             :                     {
    2842           0 :                         if ( ( error = ivas_rend_openCldfbRend( &inputSba->cldfbRendWrapper, inConfig, outConfig, &rendCtx.pSplitRendWrapper->multiBinPoseData, *rendCtx.pOutSampleRate ) ) != IVAS_ERR_OK )
    2843             :                         {
    2844           0 :                             return error;
    2845             :                         }
    2846             :                     }
    2847             :                     else
    2848             :                     {
    2849         720 :                         if ( ( error = ivas_rend_openCrend( &inputSba->crendWrapper, inConfig, outConfig, hRendCfg, hMixconv, hHrtfStatistics, *rendCtx.pOutSampleRate, 1, rendCtx.pSplitRendWrapper != NULL ? rendCtx.pSplitRendWrapper->multiBinPoseData.num_poses : 1 ) ) != IVAS_ERR_OK )
    2850             :                         {
    2851           0 :                             return error;
    2852             :                         }
    2853             :                     }
    2854         720 :                     break;
    2855         360 :                 case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR:
    2856         360 :                     if ( ( error = initSbaPanGainsForMcOut( inputSba, IVAS_AUDIO_CONFIG_7_1_4, NULL ) ) != IVAS_ERR_OK )
    2857             :                     {
    2858           0 :                         return error;
    2859             :                     }
    2860             : 
    2861         360 :                     if ( ( error = ivas_rend_openCrend( &inputSba->crendWrapper, IVAS_AUDIO_CONFIG_7_1_4, outConfig, hRendCfg, hMixconv, hHrtfStatistics, *rendCtx.pOutSampleRate, 1, rendCtx.pSplitRendWrapper != NULL ? rendCtx.pSplitRendWrapper->multiBinPoseData.num_poses : 1 ) ) != IVAS_ERR_OK )
    2862             :                     {
    2863           0 :                         return error;
    2864             :                     }
    2865         360 :                     break;
    2866           0 :                 default:
    2867           0 :                     return IVAS_ERR_INVALID_OUTPUT_FORMAT;
    2868             :             }
    2869        1080 :             break;
    2870           4 :         case IVAS_REND_AUDIO_CONFIG_TYPE_MASA:
    2871           4 :             error = IVAS_ERR_OK;
    2872           4 :             break; /* Do nothing */
    2873           0 :         default:
    2874           0 :             return IVAS_ERR_INVALID_OUTPUT_FORMAT;
    2875             :     }
    2876             : 
    2877             :     /* Check error here to keep switch statement more compact */
    2878        3274 :     if ( error != IVAS_ERR_OK )
    2879             :     {
    2880           0 :         return error;
    2881             :     }
    2882             : 
    2883        3274 :     return IVAS_ERR_OK;
    2884             : }
    2885             : 
    2886             : 
    2887           4 : static ivas_error initSbaMasaRendering(
    2888             :     input_sba *inputSba,
    2889             :     int32_t inSampleRate )
    2890             : {
    2891             :     ivas_error error;
    2892             : 
    2893           4 :     ivas_rend_closeCrend( &inputSba->crendWrapper );
    2894             : 
    2895           4 :     if ( ( error = ivas_dirac_ana_open( &inputSba->hDirAC, inSampleRate ) ) != IVAS_ERR_OK )
    2896             :     {
    2897           0 :         return error;
    2898             :     }
    2899             : 
    2900           4 :     return IVAS_ERR_OK;
    2901             : }
    2902             : 
    2903             : 
    2904        3274 : static ivas_error setRendInputActiveSba(
    2905             :     void *input,
    2906             :     const AUDIO_CONFIG inConfig,
    2907             :     const IVAS_REND_InputId id,
    2908             :     RENDER_CONFIG_DATA *hRendCfg,
    2909             :     hrtf_handles *hrtfs )
    2910             : {
    2911             :     ivas_error error;
    2912             :     rendering_context rendCtx;
    2913             :     AUDIO_CONFIG outConfig;
    2914             :     input_sba *inputSba;
    2915             :     int16_t pos_idx;
    2916             : 
    2917        3274 :     inputSba = (input_sba *) input;
    2918        3274 :     rendCtx = inputSba->base.ctx;
    2919        3274 :     outConfig = *rendCtx.pOutConfig;
    2920             : 
    2921        3274 :     if ( !isIoConfigPairSupported( inConfig, outConfig ) )
    2922             :     {
    2923           0 :         return IVAS_ERR_IO_CONFIG_PAIR_NOT_SUPPORTED;
    2924             :     }
    2925             : 
    2926        3274 :     if ( ( error = allocateInputBaseBufferData( &inputSba->bufferData, MAX_CLDFB_BUFFER_LENGTH ) ) != IVAS_ERR_OK )
    2927             :     {
    2928           0 :         return error;
    2929             :     }
    2930             : 
    2931        3274 :     initRendInputBase( &inputSba->base, inConfig, id, rendCtx, inputSba->bufferData, MAX_CLDFB_BUFFER_LENGTH );
    2932             : 
    2933        3274 :     setZeroPanMatrix( inputSba->hoaDecMtx );
    2934             : 
    2935        3274 :     inputSba->crendWrapper = NULL;
    2936       29466 :     for ( pos_idx = 0; pos_idx < MAX_HEAD_ROT_POSES; pos_idx++ )
    2937             :     {
    2938       26192 :         initRotGains( inputSba->rot_gains_prev[pos_idx] );
    2939             :     }
    2940        3274 :     inputSba->cldfbRendWrapper.hHrtfFastConv = hrtfs->hHrtfFastConv;
    2941             : 
    2942        3274 :     if ( outConfig == IVAS_AUDIO_CONFIG_MASA1 || outConfig == IVAS_AUDIO_CONFIG_MASA2 )
    2943             :     {
    2944           4 :         if ( ( error = initSbaMasaRendering( inputSba, *rendCtx.pOutSampleRate ) ) != IVAS_ERR_OK )
    2945             :         {
    2946           0 :             return error;
    2947             :         }
    2948             :     }
    2949             : 
    2950        3274 :     if ( ( error = updateSbaPanGains( inputSba, outConfig, hRendCfg, hrtfs->hSetOfHRTF, hrtfs->hHrtfStatistics ) ) != IVAS_ERR_OK )
    2951             :     {
    2952           0 :         return error;
    2953             :     }
    2954             : 
    2955        3274 :     return IVAS_ERR_OK;
    2956             : }
    2957             : 
    2958             : 
    2959        8382 : static void clearInputSba(
    2960             :     input_sba *inputSba )
    2961             : {
    2962             :     rendering_context rendCtx;
    2963             : 
    2964        8382 :     rendCtx = inputSba->base.ctx;
    2965             : 
    2966        8382 :     freeInputBaseBufferData( &inputSba->bufferData );
    2967        8382 :     ivas_TD_RINGBUF_Close( &inputSba->base.delayBuffer );
    2968             : 
    2969        8382 :     initRendInputBase( &inputSba->base, IVAS_AUDIO_CONFIG_INVALID, 0, rendCtx, NULL, 0 );
    2970             : 
    2971             :     /* Free input's internal handles */
    2972        8382 :     ivas_rend_closeCrend( &inputSba->crendWrapper );
    2973             : 
    2974        8382 :     if ( inputSba->cldfbRendWrapper.hCldfbRend != NULL )
    2975             :     {
    2976           0 :         ivas_rend_closeCldfbRend( &inputSba->cldfbRendWrapper );
    2977             :     }
    2978             : 
    2979        8382 :     ivas_dirac_ana_close( &( inputSba->hDirAC ) );
    2980             : 
    2981        8382 :     return;
    2982             : }
    2983             : 
    2984             : 
    2985        2136 : static ivas_error setRendInputActiveMasa(
    2986             :     void *input,
    2987             :     const AUDIO_CONFIG inConfig,
    2988             :     const IVAS_REND_InputId id,
    2989             :     RENDER_CONFIG_DATA *hRendCfg,
    2990             :     hrtf_handles *hrtfs )
    2991             : {
    2992             :     ivas_error error;
    2993             :     rendering_context rendCtx;
    2994             :     AUDIO_CONFIG outConfig;
    2995             :     input_masa *inputMasa;
    2996             :     int16_t numInChannels;
    2997             : 
    2998        2136 :     inputMasa = (input_masa *) input;
    2999        2136 :     rendCtx = inputMasa->base.ctx;
    3000        2136 :     outConfig = *rendCtx.pOutConfig;
    3001             : 
    3002        2136 :     if ( !isIoConfigPairSupported( inConfig, outConfig ) )
    3003             :     {
    3004           0 :         return IVAS_ERR_IO_CONFIG_PAIR_NOT_SUPPORTED;
    3005             :     }
    3006             : 
    3007        2136 :     if ( ( error = allocateInputBaseBufferData( &inputMasa->bufferData, MAX_BUFFER_LENGTH ) ) != IVAS_ERR_OK )
    3008             :     {
    3009           0 :         return error;
    3010             :     }
    3011        2136 :     initRendInputBase( &inputMasa->base, inConfig, id, rendCtx, inputMasa->bufferData, MAX_BUFFER_LENGTH );
    3012             : 
    3013        2136 :     if ( ( error = getAudioConfigNumChannels( inConfig, &numInChannels ) ) != IVAS_ERR_OK )
    3014             :     {
    3015           0 :         return error;
    3016             :     }
    3017             : 
    3018        2136 :     if ( getAudioConfigType( outConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_MASA )
    3019             :     {
    3020          36 :         inputMasa->metadataHasBeenFed = false;
    3021          36 :         if ( ( error = masaPrerendOpen( &inputMasa->hMasaPrerend, inputMasa->base.inConfig == IVAS_AUDIO_CONFIG_MASA1 ? 1 : 2, *( inputMasa->base.ctx.pOutSampleRate ) ) ) != IVAS_ERR_OK )
    3022             :         {
    3023           0 :             return error;
    3024             :         }
    3025             :     }
    3026             :     else
    3027             :     {
    3028        2100 :         if ( ( error = initMasaExtRenderer( inputMasa, outConfig, hRendCfg, hrtfs ) ) != IVAS_ERR_OK )
    3029             :         {
    3030           0 :             return error;
    3031             :         }
    3032        2100 :         inputMasa->metadataHasBeenFed = false;
    3033             :     }
    3034             : 
    3035        2136 :     return IVAS_ERR_OK;
    3036             : }
    3037             : 
    3038             : 
    3039        8382 : static void clearInputMasa(
    3040             :     input_masa *inputMasa )
    3041             : {
    3042             :     rendering_context rendCtx;
    3043             : 
    3044        8382 :     rendCtx = inputMasa->base.ctx;
    3045             : 
    3046        8382 :     freeInputBaseBufferData( &inputMasa->bufferData );
    3047        8382 :     ivas_TD_RINGBUF_Close( &inputMasa->base.delayBuffer );
    3048             : 
    3049        8382 :     masaPrerendClose( &inputMasa->hMasaPrerend );
    3050        8382 :     freeMasaExtRenderer( &inputMasa->hMasaExtRend );
    3051             : 
    3052        8382 :     initRendInputBase( &inputMasa->base, IVAS_AUDIO_CONFIG_INVALID, 0, rendCtx, NULL, 0 );
    3053             : 
    3054        8382 :     return;
    3055             : }
    3056             : 
    3057             : 
    3058             : /*-------------------------------------------------------------------------
    3059             :  * IVAS_REND_Open()
    3060             :  *
    3061             :  *
    3062             :  *------------------------------------------------------------------------*/
    3063             : 
    3064        8382 : ivas_error IVAS_REND_Open(
    3065             :     IVAS_REND_HANDLE *phIvasRend,          /* i/o: Pointer to renderer handle                          */
    3066             :     const int32_t outputSampleRate,        /* i  : output sampling rate                                */
    3067             :     const IVAS_AUDIO_CONFIG outConfig,     /* i  : output audio config                                 */
    3068             :     const bool asHrtfBinary,               /* i  : load hrtf binary file                               */
    3069             :     const int16_t nonDiegeticPan,          /* i  : non-diegetic object flag                            */
    3070             :     const float nonDiegeticPanGain,        /* i  : non-diegetic panning gain                           */
    3071             :     const int16_t Opt_Headrotation,        /* i  : indicates whether head-rotation is used             */
    3072             :     const int16_t Opt_ExternalOrientation, /* i  : indicates whether external orientations are used    */
    3073             :     const int16_t num_subframes            /* i  : number of subframes                                 */
    3074             : )
    3075             : {
    3076             :     int16_t i;
    3077             :     int16_t j;
    3078             :     IVAS_REND_HANDLE hIvasRend;
    3079             :     ivas_error error;
    3080             :     int16_t numOutChannels;
    3081             : 
    3082             :     /* Validate function arguments */
    3083        8382 :     if ( phIvasRend == NULL )
    3084             :     {
    3085           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3086             :     }
    3087             : 
    3088        8382 :     if ( ( error = validateOutputAudioConfig( outConfig ) ) != IVAS_ERR_OK )
    3089             :     {
    3090           0 :         return error;
    3091             :     }
    3092             : 
    3093        8382 :     if ( ( error = validateOutputSampleRate( outputSampleRate, outConfig ) ) != IVAS_ERR_OK )
    3094             :     {
    3095           0 :         return error;
    3096             :     }
    3097             : 
    3098        8382 :     *phIvasRend = (IVAS_REND_HANDLE) malloc( sizeof( struct IVAS_REND ) );
    3099        8382 :     if ( *phIvasRend == NULL )
    3100             :     {
    3101           0 :         return IVAS_ERR_FAILED_ALLOC;
    3102             :     }
    3103             : 
    3104        8382 :     hIvasRend = *phIvasRend;
    3105        8382 :     hIvasRend->sampleRateOut = outputSampleRate;
    3106        8382 :     hIvasRend->outputConfig = outConfig;
    3107        8382 :     hIvasRend->customLsOut = defaultCustomLs();
    3108        8382 :     hIvasRend->hLimiter = NULL;
    3109        8382 :     hIvasRend->efapOutWrapper.hEfap = NULL;
    3110        8382 :     hIvasRend->efapOutWrapper.pCustomLsSetup = NULL;
    3111             : #ifdef DEBUGGING
    3112             :     hIvasRend->numClipping = 0;
    3113             : #endif
    3114        8382 :     hIvasRend->num_subframes = num_subframes;
    3115             : 
    3116             :     /* Initialize limiter */
    3117        8382 :     if ( ( error = IVAS_REND_GetNumOutChannels( hIvasRend, &numOutChannels ) ) != IVAS_ERR_OK )
    3118             :     {
    3119           0 :         return error;
    3120             :     }
    3121             : 
    3122        8382 :     if ( ( error = initLimiter( &hIvasRend->hLimiter, numOutChannels, outputSampleRate ) ) != IVAS_ERR_OK )
    3123             :     {
    3124           0 :         return error;
    3125             :     }
    3126             : 
    3127             :     /* Initialize headrotation data */
    3128        8382 :     hIvasRend->headRotData.headRotEnabled = 0;
    3129        8382 :     if ( Opt_Headrotation )
    3130             :     {
    3131        1332 :         if ( ( error = initHeadRotation( hIvasRend ) ) != IVAS_ERR_OK )
    3132             :         {
    3133           0 :             return error;
    3134             :         }
    3135             :     }
    3136             : 
    3137             :     /* Initialize external orientation data */
    3138        8382 :     hIvasRend->hExternalOrientationData = NULL;
    3139        8382 :     if ( Opt_ExternalOrientation )
    3140             :     {
    3141           0 :         if ( ( error = ivas_external_orientation_open( &( hIvasRend->hExternalOrientationData ), num_subframes ) ) != IVAS_ERR_OK )
    3142             :         {
    3143           0 :             return error;
    3144             :         }
    3145             :     }
    3146             : 
    3147             :     /* Initilize combined orientation data */
    3148        8382 :     hIvasRend->hCombinedOrientationData = NULL;
    3149        8382 :     if ( Opt_Headrotation || Opt_ExternalOrientation )
    3150             :     {
    3151        1332 :         if ( ( error = ivas_combined_orientation_open( &( hIvasRend->hCombinedOrientationData ), outputSampleRate, num_subframes ) ) != IVAS_ERR_OK )
    3152             :         {
    3153           0 :             return error;
    3154             :         }
    3155             :     }
    3156             : 
    3157             :     /* Initialize EFAP */
    3158        8382 :     if ( ( error = initEfap( &hIvasRend->efapOutWrapper, outConfig, &hIvasRend->customLsOut ) ) != IVAS_ERR_OK )
    3159             :     {
    3160           0 :         return error;
    3161             :     }
    3162             : 
    3163             :     /* Initialize inputs */
    3164             : 
    3165        8382 :     hIvasRend->splitRendWrapper = NULL;
    3166        8382 :     if ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    3167             :     {
    3168           0 :         if ( ( hIvasRend->splitRendWrapper = (SPLIT_REND_WRAPPER *) malloc( sizeof( SPLIT_REND_WRAPPER ) ) ) == NULL )
    3169             :         {
    3170           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IVAS renderer handle" );
    3171             :         }
    3172             : 
    3173           0 :         isar_init_split_rend_handles( hIvasRend->splitRendWrapper );
    3174             :     }
    3175        8382 :     hIvasRend->splitRendEncBuffer.data = NULL;
    3176        8382 :     hIvasRend->splitRendEncBuffer.config.is_cldfb = 0;
    3177        8382 :     hIvasRend->splitRendEncBuffer.config.numChannels = 0;
    3178        8382 :     hIvasRend->splitRendEncBuffer.config.numSamplesPerChannel = 0;
    3179             : 
    3180       41910 :     for ( i = 0; i < RENDERER_MAX_ISM_INPUTS; ++i )
    3181             :     {
    3182       33528 :         initRendInputBase( &hIvasRend->inputsIsm[i].base, IVAS_AUDIO_CONFIG_INVALID, 0, getRendCtx( hIvasRend ), NULL, 0 );
    3183             : 
    3184       33528 :         hIvasRend->inputsIsm[i].crendWrapper = NULL;
    3185       33528 :         hIvasRend->inputsIsm[i].hReverb = NULL;
    3186       33528 :         hIvasRend->inputsIsm[i].tdRendWrapper.hBinRendererTd = NULL;
    3187      268224 :         for ( j = 0; j < (int16_t) ( sizeof( hIvasRend->inputsIsm[i].splitTdRendWrappers ) / sizeof( *hIvasRend->inputsIsm[i].splitTdRendWrappers ) ); ++j )
    3188             :         {
    3189      234696 :             hIvasRend->inputsIsm[i].splitTdRendWrappers[j].hBinRendererTd = NULL;
    3190      234696 :             hIvasRend->inputsIsm[i].splitTdRendWrappers[j].hHrtfTD = NULL;
    3191             :         }
    3192       33528 :         hIvasRend->inputsIsm[i].bufferData = NULL;
    3193       33528 :         hIvasRend->inputsIsm[i].nonDiegeticPan = nonDiegeticPan;
    3194       33528 :         hIvasRend->inputsIsm[i].nonDiegeticPanGain = nonDiegeticPanGain;
    3195       33528 :         hIvasRend->inputsIsm[i].hOMasa = NULL;
    3196             :     }
    3197             : 
    3198       16764 :     for ( i = 0; i < RENDERER_MAX_MC_INPUTS; ++i )
    3199             :     {
    3200        8382 :         initRendInputBase( &hIvasRend->inputsMc[i].base, IVAS_AUDIO_CONFIG_INVALID, 0, getRendCtx( hIvasRend ), NULL, 0 );
    3201             : 
    3202        8382 :         hIvasRend->inputsMc[i].efapInWrapper.hEfap = NULL;
    3203        8382 :         hIvasRend->inputsMc[i].crendWrapper = NULL;
    3204        8382 :         hIvasRend->inputsMc[i].hReverb = NULL;
    3205        8382 :         hIvasRend->inputsMc[i].tdRendWrapper.hBinRendererTd = NULL;
    3206        8382 :         hIvasRend->inputsMc[i].bufferData = NULL;
    3207        8382 :         hIvasRend->inputsMc[i].lfeDelayBuffer = NULL;
    3208        8382 :         hIvasRend->inputsMc[i].nonDiegeticPan = nonDiegeticPan;
    3209        8382 :         hIvasRend->inputsMc[i].nonDiegeticPanGain = nonDiegeticPanGain;
    3210        8382 :         hIvasRend->inputsMc[i].hMcMasa = NULL;
    3211       67056 :         for ( j = 0; j < (int16_t) ( sizeof( hIvasRend->inputsMc[i].splitTdRendWrappers ) / sizeof( *hIvasRend->inputsMc[i].splitTdRendWrappers ) ); ++j )
    3212             :         {
    3213       58674 :             hIvasRend->inputsMc[i].splitTdRendWrappers[j].hBinRendererTd = NULL;
    3214       58674 :             hIvasRend->inputsMc[i].splitTdRendWrappers[j].hHrtfTD = NULL;
    3215             :         }
    3216             :     }
    3217             : 
    3218       16764 :     for ( i = 0; i < RENDERER_MAX_SBA_INPUTS; ++i )
    3219             :     {
    3220        8382 :         initRendInputBase( &hIvasRend->inputsSba[i].base, IVAS_AUDIO_CONFIG_INVALID, 0, getRendCtx( hIvasRend ), NULL, 0 );
    3221             : 
    3222        8382 :         hIvasRend->inputsSba[i].crendWrapper = NULL;
    3223        8382 :         hIvasRend->inputsSba[i].cldfbRendWrapper.hCldfbRend = NULL;
    3224        8382 :         hIvasRend->inputsSba[i].cldfbRendWrapper.hHrtfFastConv = NULL;
    3225        8382 :         hIvasRend->inputsSba[i].bufferData = NULL;
    3226        8382 :         hIvasRend->inputsSba[i].hDirAC = NULL;
    3227             :     }
    3228             : 
    3229       16764 :     for ( i = 0; i < RENDERER_MAX_MASA_INPUTS; ++i )
    3230             :     {
    3231        8382 :         initRendInputBase( &hIvasRend->inputsMasa[i].base, IVAS_AUDIO_CONFIG_INVALID, 0, getRendCtx( hIvasRend ), NULL, 0 );
    3232             : 
    3233        8382 :         hIvasRend->inputsMasa[i].metadataHasBeenFed = false;
    3234        8382 :         hIvasRend->inputsMasa[i].bufferData = NULL;
    3235        8382 :         hIvasRend->inputsMasa[i].hMasaPrerend = NULL;
    3236        8382 :         hIvasRend->inputsMasa[i].hMasaExtRend = NULL;
    3237             :     }
    3238             : 
    3239             : 
    3240        8382 :     hIvasRend->hHrtfs.hHrtfFastConv = NULL;
    3241        8382 :     hIvasRend->hHrtfs.hHrtfParambin = NULL;
    3242        8382 :     hIvasRend->hHrtfs.hHrtfTD = NULL;
    3243        8382 :     hIvasRend->hHrtfs.hSetOfHRTF = NULL;
    3244        8382 :     hIvasRend->hHrtfs.hHrtfStatistics = NULL;
    3245        8382 :     if ( asHrtfBinary )
    3246             :     {
    3247           0 :         if ( ( error = ivas_HRTF_binary_open( &( hIvasRend->hHrtfs.hHrtfTD ) ) ) != IVAS_ERR_OK )
    3248             :         {
    3249           0 :             return error;
    3250             :         }
    3251           0 :         if ( ( error = ivas_HRTF_CRend_binary_open( &( hIvasRend->hHrtfs.hSetOfHRTF ) ) ) != IVAS_ERR_OK )
    3252             :         {
    3253           0 :             return error;
    3254             :         }
    3255           0 :         if ( ( error = ivas_HRTF_fastconv_binary_open( &( hIvasRend->hHrtfs.hHrtfFastConv ) ) ) != IVAS_ERR_OK )
    3256             :         {
    3257           0 :             return error;
    3258             :         }
    3259           0 :         if ( ( error = ivas_HRTF_parambin_binary_open( &( hIvasRend->hHrtfs.hHrtfParambin ) ) ) != IVAS_ERR_OK )
    3260             :         {
    3261           0 :             return error;
    3262             :         }
    3263           0 :         if ( ( error = ivas_HRTF_statistics_binary_open( &( hIvasRend->hHrtfs.hHrtfStatistics ) ) ) != IVAS_ERR_OK )
    3264             :         {
    3265           0 :             return error;
    3266             :         }
    3267             :     }
    3268             : 
    3269        8382 :     if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    3270             :     {
    3271         888 :         if ( ( error = ivas_HRTF_statistics_init( &( hIvasRend->hHrtfs.hHrtfStatistics ), hIvasRend->sampleRateOut ) ) != IVAS_ERR_OK )
    3272             :         {
    3273           0 :             return error;
    3274             :         }
    3275             :     }
    3276             : 
    3277        8382 :     return IVAS_ERR_OK;
    3278             : }
    3279             : 
    3280             : 
    3281        1332 : static LSSETUP_CUSTOM_STRUCT makeCustomLsSetup(
    3282             :     const IVAS_CUSTOM_LS_DATA rendCustomLsLayout )
    3283             : {
    3284             :     int16_t i;
    3285             :     LSSETUP_CUSTOM_STRUCT customLs;
    3286             : 
    3287             :     /* Copy layout description */
    3288        1332 :     customLs.num_spk = rendCustomLsLayout.num_spk;
    3289        1332 :     mvr2r( rendCustomLsLayout.azimuth, customLs.ls_azimuth, rendCustomLsLayout.num_spk );
    3290        1332 :     mvr2r( rendCustomLsLayout.elevation, customLs.ls_elevation, rendCustomLsLayout.num_spk );
    3291             : 
    3292        1332 :     customLs.is_planar_setup = 1;
    3293        6660 :     for ( i = 0; i < rendCustomLsLayout.num_spk; ++i )
    3294             :     {
    3295        6660 :         if ( fabsf( rendCustomLsLayout.elevation[i] ) > EPSILON )
    3296             :         {
    3297        1332 :             customLs.is_planar_setup = 0;
    3298        1332 :             break;
    3299             :         }
    3300             :     }
    3301             : 
    3302        1332 :     customLs.num_lfe = rendCustomLsLayout.num_lfe;
    3303        1332 :     mvs2s( rendCustomLsLayout.lfe_idx, customLs.lfe_idx, rendCustomLsLayout.num_lfe );
    3304             : 
    3305        1332 :     return customLs;
    3306             : }
    3307             : 
    3308             : 
    3309        1332 : static ivas_error validateCustomLsLayout(
    3310             :     const IVAS_CUSTOM_LS_DATA layout )
    3311             : {
    3312             :     int16_t i;
    3313             : 
    3314             :     /* Negative number of speakers or LFEs makes no sense */
    3315        1332 :     if ( layout.num_spk < 0 || layout.num_lfe < 0 )
    3316             :     {
    3317           0 :         return IVAS_ERR_INVALID_CUSTOM_LS_LAYOUT;
    3318             :     }
    3319             : 
    3320             :     /* There must be at least one speaker or LFE in the layout */
    3321        1332 :     if ( layout.num_spk + layout.num_lfe <= 0 )
    3322             :     {
    3323           0 :         return IVAS_ERR_INVALID_CUSTOM_LS_LAYOUT;
    3324             :     }
    3325             : 
    3326             :     /* LFE indices must be positive */
    3327        1332 :     for ( i = 0; i < layout.num_lfe; ++i )
    3328             :     {
    3329           0 :         if ( layout.lfe_idx[i] < 0 )
    3330             :         {
    3331           0 :             return IVAS_ERR_INVALID_CUSTOM_LS_LAYOUT;
    3332             :         }
    3333             :     }
    3334             : 
    3335        1332 :     return IVAS_ERR_OK;
    3336             : }
    3337             : 
    3338             : 
    3339             : /*-------------------------------------------------------------------*
    3340             :  * IVAS_REND_ConfigureCustomOutputLoudspeakerLayout()
    3341             :  *
    3342             :  *
    3343             :  *-------------------------------------------------------------------*/
    3344             : 
    3345         702 : ivas_error IVAS_REND_ConfigureCustomOutputLoudspeakerLayout(
    3346             :     IVAS_REND_HANDLE hIvasRend,
    3347             :     const IVAS_CUSTOM_LS_DATA layout )
    3348             : {
    3349             :     int16_t i, numOutChannels;
    3350             :     ivas_error error;
    3351             :     input_mc *inputMc;
    3352             :     input_sba *inputSba;
    3353             : 
    3354             :     /* Validate function arguments */
    3355         702 :     if ( hIvasRend == NULL )
    3356             :     {
    3357           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3358             :     }
    3359             : 
    3360         702 :     if ( hIvasRend->outputConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM )
    3361             :     {
    3362             :         /* Specifying details of custom speaker layout only makes sense if output config is set to custom speaker layout */
    3363           0 :         return IVAS_ERR_INVALID_OUTPUT_FORMAT;
    3364             :     }
    3365             : 
    3366         702 :     if ( ( error = validateCustomLsLayout( layout ) ) != IVAS_ERR_OK )
    3367             :     {
    3368           0 :         return error;
    3369             :     }
    3370             : 
    3371         702 :     hIvasRend->customLsOut = makeCustomLsSetup( layout );
    3372             : 
    3373             :     /* Re-initialize limiter - number of output channels may have changed */
    3374         702 :     if ( ( error = IVAS_REND_GetNumOutChannels( hIvasRend, &numOutChannels ) ) != IVAS_ERR_OK )
    3375             :     {
    3376           0 :         return error;
    3377             :     }
    3378             : 
    3379         702 :     if ( ( error = initLimiter( &hIvasRend->hLimiter, numOutChannels, hIvasRend->sampleRateOut ) ) != IVAS_ERR_OK )
    3380             :     {
    3381           0 :         return error;
    3382             :     }
    3383             : 
    3384             :     /* Re-initialize EFAP - output layout has changed or has been fully defined for the first time */
    3385         702 :     if ( ( error = initEfap( &hIvasRend->efapOutWrapper, hIvasRend->outputConfig, &hIvasRend->customLsOut ) ) != IVAS_ERR_OK )
    3386             :     {
    3387           0 :         return error;
    3388             :     }
    3389             : 
    3390             :     /* Re-initialize panning gains for each active MC input, This includes re-initializing
    3391             :      * LFE handling for the new output layout, which means custom LFE handling is overwritten,
    3392             :      * if previously set for any MC input. */
    3393        1404 :     for ( i = 0; i < RENDERER_MAX_MC_INPUTS; ++i )
    3394             :     {
    3395         702 :         inputMc = &hIvasRend->inputsMc[i];
    3396         702 :         if ( inputMc->base.inConfig == IVAS_AUDIO_CONFIG_INVALID )
    3397             :         {
    3398             :             /* Input inactive, skip. */
    3399         702 :             continue;
    3400             :         }
    3401             : 
    3402           0 :         inputMc->lfeRouting = defaultLfeRouting( inputMc->base.inConfig, inputMc->customLsInput, hIvasRend->outputConfig, *inputMc->base.ctx.pCustomLsOut );
    3403             : 
    3404           0 :         if ( ( error = updateMcPanGains( inputMc, hIvasRend->outputConfig ) ) != IVAS_ERR_OK )
    3405             :         {
    3406           0 :             return error;
    3407             :         }
    3408             :     }
    3409             : 
    3410             :     /* Re-initialize panning gains for each active SBA input */
    3411        1404 :     for ( i = 0; i < RENDERER_MAX_SBA_INPUTS; ++i )
    3412             :     {
    3413         702 :         inputSba = &hIvasRend->inputsSba[i];
    3414             : 
    3415         702 :         if ( inputSba->base.inConfig == IVAS_AUDIO_CONFIG_INVALID )
    3416             :         {
    3417             :             /* Input inactive, skip. */
    3418         702 :             continue;
    3419             :         }
    3420             : 
    3421           0 :         if ( ( error = updateSbaPanGains( inputSba, hIvasRend->outputConfig, hIvasRend->hRendererConfig, NULL, NULL ) ) != IVAS_ERR_OK )
    3422             :         {
    3423           0 :             return error;
    3424             :         }
    3425             :     }
    3426             : 
    3427         702 :     return IVAS_ERR_OK;
    3428             : }
    3429             : 
    3430             : 
    3431             : /*-------------------------------------------------------------------*
    3432             :  * IVAS_REND_GetNumOutChannels()
    3433             :  *
    3434             :  *
    3435             :  *-------------------------------------------------------------------*/
    3436             : 
    3437    13757966 : ivas_error IVAS_REND_GetNumOutChannels(
    3438             :     IVAS_REND_CONST_HANDLE hIvasRend,
    3439             :     int16_t *numOutChannels )
    3440             : {
    3441             :     ivas_error error;
    3442             : 
    3443             :     /* Validate function arguments */
    3444    13757966 :     if ( hIvasRend == NULL || numOutChannels == NULL )
    3445             :     {
    3446           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3447             :     }
    3448             : 
    3449             :     /* Handle special cases where additional info is needed from the renderer, otherwise use getAudioConfigNumChannels() */
    3450    13757966 :     switch ( hIvasRend->outputConfig )
    3451             :     {
    3452      508524 :         case IVAS_AUDIO_CONFIG_LS_CUSTOM:
    3453      508524 :             *numOutChannels = hIvasRend->customLsOut.num_spk + hIvasRend->customLsOut.num_lfe;
    3454      508524 :             break;
    3455    13249442 :         default:
    3456    13249442 :             if ( ( error = getAudioConfigNumChannels( hIvasRend->outputConfig, numOutChannels ) ) != IVAS_ERR_OK )
    3457             :             {
    3458           0 :                 return error;
    3459             :             }
    3460    13249442 :             break;
    3461             :     }
    3462             : 
    3463    13757966 :     return IVAS_ERR_OK;
    3464             : }
    3465             : 
    3466             : 
    3467       20786 : static IVAS_REND_InputId makeInputId(
    3468             :     AUDIO_CONFIG config,
    3469             :     const int32_t inputIndex )
    3470             : {
    3471             :     /* Put config type in second byte (from LSB), put index + 1 in first byte
    3472             :      *
    3473             :      * Index is incremented here so that a valid ID can never be 0. */
    3474       20786 :     return (IVAS_REND_InputId) ( ( ( (uint32_t) getAudioConfigType( config ) ) << 8 ) | ( inputIndex + 1 ) );
    3475             : }
    3476             : 
    3477             : 
    3478    60778830 : static ivas_error getInputById(
    3479             :     IVAS_REND_HANDLE hIvasRend,
    3480             :     IVAS_REND_InputId inputId,
    3481             :     void **ppInput )
    3482             : {
    3483             :     int32_t inputIndex;
    3484             :     IVAS_REND_AudioConfigType configType;
    3485             :     input_base *pInputBase;
    3486             : 
    3487             :     /* Reverse makeInputId() */
    3488    60778830 :     inputIndex = ( inputId & 0xFF ) - 1;
    3489    60778830 :     configType = ( inputId & 0xFF00 ) >> 8;
    3490             : 
    3491             :     /* Validate values derived from input ID */
    3492    60778830 :     if ( inputIndex < 0 )
    3493             :     {
    3494           0 :         return IVAS_ERR_INVALID_INPUT_ID;
    3495             :     }
    3496    60778830 :     switch ( configType )
    3497             :     {
    3498    49595802 :         case IVAS_REND_AUDIO_CONFIG_TYPE_OBJECT_BASED:
    3499    49595802 :             if ( inputIndex > RENDERER_MAX_ISM_INPUTS )
    3500             :             {
    3501           0 :                 return IVAS_ERR_INVALID_INPUT_ID;
    3502             :             }
    3503    49595802 :             pInputBase = &hIvasRend->inputsIsm[inputIndex].base;
    3504    49595802 :             break;
    3505     2178058 :         case IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED:
    3506     2178058 :             if ( inputIndex > RENDERER_MAX_MC_INPUTS )
    3507             :             {
    3508           0 :                 return IVAS_ERR_INVALID_INPUT_ID;
    3509             :             }
    3510     2178058 :             pInputBase = &hIvasRend->inputsMc[inputIndex].base;
    3511     2178058 :             break;
    3512     7498298 :         case IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS:
    3513     7498298 :             if ( inputIndex > RENDERER_MAX_SBA_INPUTS )
    3514             :             {
    3515           0 :                 return IVAS_ERR_INVALID_INPUT_ID;
    3516             :             }
    3517     7498298 :             pInputBase = &hIvasRend->inputsSba[inputIndex].base;
    3518     7498298 :             break;
    3519     1506672 :         case IVAS_REND_AUDIO_CONFIG_TYPE_MASA:
    3520     1506672 :             if ( inputIndex > RENDERER_MAX_MASA_INPUTS )
    3521             :             {
    3522           0 :                 return IVAS_ERR_INVALID_INPUT_ID;
    3523             :             }
    3524     1506672 :             pInputBase = &hIvasRend->inputsMasa[inputIndex].base;
    3525     1506672 :             break;
    3526           0 :         default:
    3527           0 :             return IVAS_ERR_INVALID_INPUT_ID;
    3528             :     }
    3529             : 
    3530             :     /* Ensure input ID matches and that input is active */
    3531    60778830 :     if ( pInputBase->id != inputId || pInputBase->inConfig == IVAS_AUDIO_CONFIG_INVALID )
    3532             :     {
    3533           0 :         return IVAS_ERR_INVALID_INPUT_ID;
    3534             :     }
    3535             : 
    3536             :     /* Validation done, set value via output parameter */
    3537    60778830 :     *ppInput = pInputBase;
    3538             : 
    3539    60778830 :     return IVAS_ERR_OK;
    3540             : }
    3541             : 
    3542             : 
    3543    10746744 : static ivas_error getConstInputById(
    3544             :     IVAS_REND_CONST_HANDLE hIvasRend,
    3545             :     const IVAS_REND_InputId inputId,
    3546             :     const void **ppInput )
    3547             : {
    3548             :     int32_t inputIndex;
    3549             :     IVAS_REND_AudioConfigType configType;
    3550             :     const input_base *pInputBase;
    3551             : 
    3552             :     /* Reverse makeInputId() */
    3553    10746744 :     inputIndex = ( inputId & 0xFF ) - 1;
    3554    10746744 :     configType = ( inputId & 0xFF00 ) >> 8;
    3555             : 
    3556             :     /* Validate values derived from input ID */
    3557    10746744 :     if ( inputIndex < 0 )
    3558             :     {
    3559           0 :         return IVAS_ERR_INVALID_INPUT_ID;
    3560             :     }
    3561    10746744 :     switch ( configType )
    3562             :     {
    3563       13146 :         case IVAS_REND_AUDIO_CONFIG_TYPE_OBJECT_BASED:
    3564       13146 :             if ( inputIndex > RENDERER_MAX_ISM_INPUTS )
    3565             :             {
    3566           0 :                 return IVAS_ERR_INVALID_INPUT_ID;
    3567             :             }
    3568       13146 :             pInputBase = &hIvasRend->inputsIsm[inputIndex].base;
    3569       13146 :             break;
    3570     2177428 :         case IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED:
    3571     2177428 :             if ( inputIndex > RENDERER_MAX_MC_INPUTS )
    3572             :             {
    3573           0 :                 return IVAS_ERR_INVALID_INPUT_ID;
    3574             :             }
    3575     2177428 :             pInputBase = &hIvasRend->inputsMc[inputIndex].base;
    3576     2177428 :             break;
    3577     7498298 :         case IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS:
    3578     7498298 :             if ( inputIndex > RENDERER_MAX_SBA_INPUTS )
    3579             :             {
    3580           0 :                 return IVAS_ERR_INVALID_INPUT_ID;
    3581             :             }
    3582     7498298 :             pInputBase = &hIvasRend->inputsSba[inputIndex].base;
    3583     7498298 :             break;
    3584     1057872 :         case IVAS_REND_AUDIO_CONFIG_TYPE_MASA:
    3585     1057872 :             if ( inputIndex > RENDERER_MAX_MASA_INPUTS )
    3586             :             {
    3587           0 :                 return IVAS_ERR_INVALID_INPUT_ID;
    3588             :             }
    3589     1057872 :             pInputBase = &hIvasRend->inputsMasa[inputIndex].base;
    3590     1057872 :             break;
    3591           0 :         default:
    3592           0 :             return IVAS_ERR_INVALID_INPUT_ID;
    3593             :     }
    3594             : 
    3595             :     /* Ensure input ID matches and that input is active */
    3596    10746744 :     if ( pInputBase->id != inputId || pInputBase->inConfig == IVAS_AUDIO_CONFIG_INVALID )
    3597             :     {
    3598           0 :         return IVAS_ERR_INVALID_INPUT_ID;
    3599             :     }
    3600             : 
    3601             :     /* Validation done, set value via output parameter */
    3602    10746744 :     *ppInput = pInputBase;
    3603             : 
    3604    10746744 :     return IVAS_ERR_OK;
    3605             : }
    3606             : 
    3607             : 
    3608       75198 : static void *getInputByIndex(
    3609             :     void *inputsArray,
    3610             :     const size_t index,
    3611             :     const IVAS_REND_AudioConfigType inputType )
    3612             : {
    3613       75198 :     switch ( inputType )
    3614             :     {
    3615        6690 :         case IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED:
    3616        6690 :             return (input_mc *) inputsArray + index;
    3617        9822 :         case IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS:
    3618        9822 :             return (input_sba *) inputsArray + index;
    3619       52278 :         case IVAS_REND_AUDIO_CONFIG_TYPE_OBJECT_BASED:
    3620       52278 :             return (input_ism *) inputsArray + index;
    3621        6408 :         case IVAS_REND_AUDIO_CONFIG_TYPE_MASA:
    3622        6408 :             return (input_masa *) inputsArray + index;
    3623           0 :         default:
    3624           0 :             break;
    3625             :     }
    3626             : 
    3627             :     /* this should be unreachable */
    3628           0 :     assert( 0 );
    3629             : 
    3630             :     /* include a final return to make the linter happy and avoid problems with wmc_tool (see #1355) */
    3631             :     return NULL;
    3632             : }
    3633             : 
    3634             : 
    3635       20786 : static ivas_error findFreeInputSlot(
    3636             :     void *inputs,
    3637             :     const IVAS_REND_AudioConfigType inputType,
    3638             :     const int32_t maxInputs,
    3639             :     int32_t *inputIndex )
    3640             : {
    3641             :     /* Using a void pointer and a separately provided type is a hack for this function
    3642             :        to be reusable for arrays of any input type (input_ism, input_mc, input_sba, input_masa).
    3643             :         Assumptions:
    3644             :             - input_base is always the first member in the input struct
    3645             :             - memory alignments of original input type and input_base are the same
    3646             :     */
    3647             :     int32_t i;
    3648             :     bool canAddInput;
    3649             :     const input_base *pInputBase;
    3650             : 
    3651       20786 :     canAddInput = false;
    3652             : 
    3653             :     /* Find first unused input in array */
    3654       33626 :     for ( i = 0; i < maxInputs; ++i )
    3655             :     {
    3656       33626 :         pInputBase = (const input_base *) getInputByIndex( inputs, i, inputType );
    3657             : 
    3658       33626 :         if ( pInputBase->inConfig == IVAS_AUDIO_CONFIG_INVALID )
    3659             :         {
    3660       20786 :             *inputIndex = i;
    3661       20786 :             canAddInput = true;
    3662       20786 :             break;
    3663             :         }
    3664             :     }
    3665             : 
    3666       20786 :     if ( !canAddInput )
    3667             :     {
    3668           0 :         return IVAS_ERR_TOO_MANY_INPUTS;
    3669             :     }
    3670             : 
    3671       20786 :     return IVAS_ERR_OK;
    3672             : }
    3673             : 
    3674             : 
    3675             : /*-------------------------------------------------------------------------
    3676             :  * Function getCldfbRendFlag()
    3677             :  *
    3678             :  *
    3679             :  *------------------------------------------------------------------------*/
    3680             : 
    3681        6696 : static int16_t getCldfbRendFlag(
    3682             :     IVAS_REND_HANDLE hIvasRend, /* i  : Renderer handle               */
    3683             :     const IVAS_REND_AudioConfigType new_configType )
    3684             : {
    3685             :     int16_t i;
    3686        6696 :     int16_t numMasaInputs = 0, numSbaInputs = 0;
    3687             :     int16_t isCldfbRend;
    3688             : 
    3689        6696 :     isCldfbRend = 0;
    3690             :     /* This function is called during three different phases of renderer processing:
    3691             :      * - IVAS_REND_AddInput()
    3692             :      * - IVAS_REND_FeedRenderConfig()
    3693             :      * - IVAS_REND_GetSplitBinauralBitstream()
    3694             :      * Only the last case can assume all inputs are present for the current frame to be rendered */
    3695        6696 :     if ( hIvasRend->hRendererConfig != NULL )
    3696             :     {
    3697       13392 :         for ( i = 0; i < RENDERER_MAX_MASA_INPUTS; ++i )
    3698             :         {
    3699        6696 :             numMasaInputs += ( hIvasRend->inputsMasa[i].base.inConfig == IVAS_AUDIO_CONFIG_INVALID && new_configType != IVAS_REND_AUDIO_CONFIG_TYPE_MASA ) ? 0 : 1;
    3700             :         }
    3701       13392 :         for ( i = 0; i < RENDERER_MAX_SBA_INPUTS; ++i )
    3702             :         {
    3703        6696 :             numSbaInputs += ( hIvasRend->inputsSba[i].base.inConfig == IVAS_AUDIO_CONFIG_INVALID && new_configType != IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS ) ? 0 : 1;
    3704             :         }
    3705        6696 :         if ( ( numMasaInputs > 0 ) || ( numSbaInputs > 0 && hIvasRend->hRendererConfig->split_rend_config.rendererSelection == IVAS_BIN_RENDERER_TYPE_FASTCONV ) )
    3706             :         {
    3707         720 :             isCldfbRend = 1;
    3708             :         }
    3709             :     }
    3710             : 
    3711        6696 :     return isCldfbRend;
    3712             : }
    3713             : 
    3714             : /*-------------------------------------------------------------------------
    3715             :  * Function isar_pre_rend_init()
    3716             :  *
    3717             :  *
    3718             :  *------------------------------------------------------------------------*/
    3719             : 
    3720        6696 : static ivas_error isar_pre_rend_init(
    3721             :     SPLIT_REND_WRAPPER *pSplitRendWrapper,
    3722             :     IVAS_REND_AudioBuffer *pSplitRendEncBuffer,
    3723             :     ISAR_SPLIT_REND_CONFIG_DATA *pSplit_rend_config,
    3724             :     IVAS_REND_HeadRotData headRotData,
    3725             :     const int32_t outputSampleRate,
    3726             :     const AUDIO_CONFIG outConfig,
    3727             :     const int16_t cldfb_in_flag,
    3728             :     const int16_t num_subframes )
    3729             : {
    3730             :     bool realloc;
    3731             :     ivas_error error;
    3732             :     IVAS_REND_AudioBufferConfig bufConfig;
    3733             : 
    3734        6696 :     realloc = false;
    3735             : 
    3736             :     /* only perform init if split rendering output */
    3737        6696 :     if ( outConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED && outConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    3738             :     {
    3739        6696 :         return IVAS_ERR_OK;
    3740             :     }
    3741             : 
    3742             :     /* these functions should only be called once during initial allocation */
    3743           0 :     if ( pSplitRendEncBuffer->data == NULL )
    3744             :     {
    3745           0 :         if ( pSplit_rend_config->poseCorrectionMode == ISAR_SPLIT_REND_POSE_CORRECTION_MODE_CLDFB )
    3746             :         {
    3747           0 :             ISAR_PRE_REND_GetMultiBinPoseData( pSplit_rend_config, &pSplitRendWrapper->multiBinPoseData, headRotData.sr_pose_pred_axis );
    3748             :         }
    3749           0 :         else if ( pSplit_rend_config->poseCorrectionMode == ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE )
    3750             :         {
    3751           0 :             isar_renderSplitUpdateNoCorrectionPoseData( pSplit_rend_config, &pSplitRendWrapper->multiBinPoseData );
    3752             :         }
    3753             : 
    3754           0 :         if ( ( error = ISAR_PRE_REND_open( pSplitRendWrapper, pSplit_rend_config, outputSampleRate, cldfb_in_flag, outConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM, num_subframes, 0 ) ) != IVAS_ERR_OK )
    3755             :         {
    3756           0 :             return error;
    3757             :         }
    3758             :     }
    3759             : 
    3760             :     /* We may need to change the allocated buffer size if a new input is added.
    3761             :      * If the cldfb_in_flag is different from what was previously allocated for the buffer, change the size */
    3762           0 :     if ( pSplitRendEncBuffer->data != NULL && ( cldfb_in_flag != pSplitRendEncBuffer->config.is_cldfb ) )
    3763             :     {
    3764           0 :         realloc = true;
    3765             :     }
    3766             : 
    3767           0 :     if ( pSplitRendEncBuffer->data == NULL || realloc )
    3768             :     {
    3769             :         /* set buffer config */
    3770           0 :         bufConfig.is_cldfb = cldfb_in_flag;
    3771           0 :         bufConfig.numSamplesPerChannel = cldfb_in_flag ? MAX_CLDFB_BUFFER_LENGTH_PER_CHANNEL : L_FRAME_MAX;
    3772           0 :         bufConfig.numChannels = BINAURAL_CHANNELS * pSplitRendWrapper->multiBinPoseData.num_poses;
    3773           0 :         pSplitRendEncBuffer->config = bufConfig;
    3774             : 
    3775             :         /* allocate memory */
    3776           0 :         if ( realloc )
    3777             :         {
    3778           0 :             free( pSplitRendEncBuffer->data );
    3779             :         }
    3780             : 
    3781           0 :         if ( ( pSplitRendEncBuffer->data = malloc( bufConfig.numChannels * bufConfig.numSamplesPerChannel * sizeof( float ) ) ) == NULL )
    3782             :         {
    3783           0 :             return IVAS_ERR_FAILED_ALLOC;
    3784             :         }
    3785             :     }
    3786             : 
    3787           0 :     return IVAS_ERR_OK;
    3788             : }
    3789             : 
    3790             : 
    3791             : /*-------------------------------------------------------------------*
    3792             :  * IVAS_REND_AddInput()
    3793             :  *
    3794             :  *
    3795             :  *-------------------------------------------------------------------*/
    3796             : 
    3797       20786 : ivas_error IVAS_REND_AddInput(
    3798             :     IVAS_REND_HANDLE hIvasRend,  /* i/o: Renderer handle               */
    3799             :     const AUDIO_CONFIG inConfig, /* i  : audio config for a new input  */
    3800             :     IVAS_REND_InputId *inputId   /* o  : ID of the new input           */
    3801             : )
    3802             : {
    3803             :     ivas_error error;
    3804             :     int32_t maxNumInputsOfType;
    3805             :     void *inputsArray;
    3806             :     IVAS_REND_AudioConfigType inputType;
    3807             :     ivas_error ( *activateInput )( void *, AUDIO_CONFIG, IVAS_REND_InputId, RENDER_CONFIG_DATA *, hrtf_handles * );
    3808             :     void ( *setInputDelay )( void *, bool );
    3809             :     int32_t inputIndex;
    3810             :     bool splitPreRendCldfb;
    3811             : 
    3812       20786 :     splitPreRendCldfb = false;
    3813             : 
    3814             :     /* Validate function arguments */
    3815       20786 :     if ( hIvasRend == NULL || inputId == NULL )
    3816             :     {
    3817           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3818             :     }
    3819             : 
    3820       20786 :     if ( hIvasRend->hRendererConfig != NULL )
    3821             :     {
    3822             :         int16_t cldfb_in_flag;
    3823        6696 :         cldfb_in_flag = getCldfbRendFlag( hIvasRend, getAudioConfigType( inConfig ) );
    3824             : 
    3825        6696 :         if ( ( error = isar_pre_rend_init( hIvasRend->splitRendWrapper,
    3826             :                                            &hIvasRend->splitRendEncBuffer,
    3827        6696 :                                            &hIvasRend->hRendererConfig->split_rend_config,
    3828             :                                            hIvasRend->headRotData,
    3829             :                                            hIvasRend->sampleRateOut,
    3830             :                                            hIvasRend->outputConfig,
    3831             :                                            cldfb_in_flag,
    3832        6696 :                                            hIvasRend->num_subframes ) ) != IVAS_ERR_OK )
    3833             :         {
    3834           0 :             return error;
    3835             :         }
    3836             : 
    3837             :         /*assumes that input has been added which means codec has been set to either lcld or lc3plus (even if render config specified default)*/
    3838        6696 :         splitPreRendCldfb = ( hIvasRend->hRendererConfig->split_rend_config.codec == ISAR_SPLIT_REND_CODEC_LCLD );
    3839             :     }
    3840             : 
    3841       20786 :     inputType = getAudioConfigType( inConfig );
    3842       20786 :     switch ( inputType )
    3843             :     {
    3844       13146 :         case IVAS_REND_AUDIO_CONFIG_TYPE_OBJECT_BASED:
    3845       13146 :             maxNumInputsOfType = RENDERER_MAX_ISM_INPUTS;
    3846       13146 :             inputsArray = hIvasRend->inputsIsm;
    3847       13146 :             activateInput = setRendInputActiveIsm;
    3848       13146 :             setInputDelay = setRendInputDelayIsm;
    3849       13146 :             break;
    3850        2230 :         case IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED:
    3851        2230 :             maxNumInputsOfType = RENDERER_MAX_MC_INPUTS;
    3852        2230 :             inputsArray = hIvasRend->inputsMc;
    3853        2230 :             activateInput = setRendInputActiveMc;
    3854        2230 :             setInputDelay = setRendInputDelayMc;
    3855        2230 :             break;
    3856        3274 :         case IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS:
    3857        3274 :             maxNumInputsOfType = RENDERER_MAX_SBA_INPUTS;
    3858        3274 :             inputsArray = hIvasRend->inputsSba;
    3859        3274 :             activateInput = setRendInputActiveSba;
    3860        3274 :             setInputDelay = setRendInputDelaySba;
    3861        3274 :             break;
    3862        2136 :         case IVAS_REND_AUDIO_CONFIG_TYPE_MASA:
    3863        2136 :             maxNumInputsOfType = RENDERER_MAX_MASA_INPUTS;
    3864        2136 :             inputsArray = hIvasRend->inputsMasa;
    3865        2136 :             activateInput = setRendInputActiveMasa;
    3866        2136 :             setInputDelay = setRendInputDelayMasa;
    3867        2136 :             break;
    3868           0 :         default:
    3869           0 :             return IVAS_ERR_INVALID_INPUT_FORMAT;
    3870             :     }
    3871             : 
    3872             :     /* Find first free input in array corresponding to input type */
    3873       20786 :     if ( ( error = findFreeInputSlot( inputsArray, inputType, maxNumInputsOfType, &inputIndex ) ) != IVAS_ERR_OK )
    3874             :     {
    3875           0 :         return error;
    3876             :     }
    3877             : 
    3878       20786 :     *inputId = makeInputId( inConfig, inputIndex );
    3879       20786 :     if ( ( error = activateInput( getInputByIndex( inputsArray, inputIndex, inputType ), inConfig, *inputId, hIvasRend->hRendererConfig, &hIvasRend->hHrtfs ) ) != IVAS_ERR_OK )
    3880             :     {
    3881           0 :         return error;
    3882             :     }
    3883             : 
    3884       20786 :     setInputDelay( getInputByIndex( inputsArray, inputIndex, inputType ), splitPreRendCldfb );
    3885             : 
    3886             :     /* set global maximum delay after adding an input */
    3887       20786 :     setMaxGlobalDelayNs( hIvasRend );
    3888             : 
    3889       20786 :     return IVAS_ERR_OK;
    3890             : }
    3891             : 
    3892             : 
    3893             : /*-------------------------------------------------------------------*
    3894             :  * IVAS_REND_ConfigureCustomInputLoudspeakerLayout()
    3895             :  *
    3896             :  *
    3897             :  * Note: this will reset any custom LFE routing set for the input
    3898             :  *-------------------------------------------------------------------*/
    3899             : 
    3900         630 : ivas_error IVAS_REND_ConfigureCustomInputLoudspeakerLayout(
    3901             :     IVAS_REND_HANDLE hIvasRend,      /* i/o: Renderer handle                        */
    3902             :     const IVAS_REND_InputId inputId, /* i  : ID of the input                        */
    3903             :     const IVAS_CUSTOM_LS_DATA layout /* i  : custom loudspeaker layout for input    */
    3904             : )
    3905             : {
    3906             :     input_mc *inputMc;
    3907             :     ivas_error error;
    3908             : 
    3909             :     /* Validate function arguments */
    3910         630 :     if ( hIvasRend == NULL )
    3911             :     {
    3912           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3913             :     }
    3914             : 
    3915         630 :     if ( ( error = validateCustomLsLayout( layout ) ) != IVAS_ERR_OK )
    3916             :     {
    3917           0 :         return error;
    3918             :     }
    3919             : 
    3920         630 :     if ( ( error = getInputById( hIvasRend, inputId, (void **) &inputMc ) ) != IVAS_ERR_OK )
    3921             :     {
    3922           0 :         return error;
    3923             :     }
    3924             : 
    3925         630 :     if ( inputMc->base.inConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM )
    3926             :     {
    3927             :         /* Specifying details of custom speaker layout only makes sense if input config is set to custom speaker layout */
    3928           0 :         return IVAS_ERR_INVALID_INPUT_FORMAT;
    3929             :     }
    3930             : 
    3931             :     /* Re-initialize panning gains for the MC input, This includes re-initializing LFE handling
    3932             :      * for the new input layout, which means custom LFE handling is overwritten, if previously
    3933             :      * set for the MC input. */
    3934         630 :     inputMc->customLsInput = makeCustomLsSetup( layout );
    3935             : 
    3936         630 :     inputMc->lfeRouting = defaultLfeRouting( inputMc->base.inConfig, inputMc->customLsInput, hIvasRend->outputConfig, *inputMc->base.ctx.pCustomLsOut );
    3937             : 
    3938         630 :     if ( ( error = initEfap( &inputMc->efapInWrapper, inputMc->base.inConfig, &inputMc->customLsInput ) ) != IVAS_ERR_OK )
    3939             :     {
    3940           0 :         return error;
    3941             :     }
    3942             : 
    3943         630 :     if ( getAudioConfigType( hIvasRend->outputConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL )
    3944             :     {
    3945         216 :         if ( ( error = initMcBinauralRendering( inputMc,
    3946         216 :                                                 inputMc->base.inConfig,
    3947             :                                                 hIvasRend->outputConfig,
    3948             :                                                 hIvasRend->hRendererConfig,
    3949             :                                                 hIvasRend->hHrtfs.hSetOfHRTF,
    3950             :                                                 hIvasRend->hHrtfs.hHrtfStatistics,
    3951             :                                                 FALSE ) ) != IVAS_ERR_OK )
    3952             :         {
    3953           0 :             return error;
    3954             :         }
    3955             :     }
    3956             : 
    3957         630 :     if ( ( error = updateMcPanGains( inputMc, hIvasRend->outputConfig ) ) != IVAS_ERR_OK )
    3958             :     {
    3959           0 :         return error;
    3960             :     }
    3961             : 
    3962         630 :     return IVAS_ERR_OK;
    3963             : }
    3964             : 
    3965             : #ifdef NONBE_1377_REND_DIRATT_CONF
    3966             : /*-------------------------------------------------------------------*
    3967             :  * IVAS_REND_SetObjectIDs()
    3968             :  *
    3969             :  *
    3970             :  *-------------------------------------------------------------------*/
    3971             : 
    3972        8382 : ivas_error IVAS_REND_SetObjectIDs(
    3973             :     IVAS_REND_HANDLE hIvasRend /* i/o: Renderer handle            */
    3974             : )
    3975             : {
    3976             :     int16_t i;
    3977             : 
    3978             :     /* Validate function arguments */
    3979        8382 :     if ( hIvasRend == NULL )
    3980             :     {
    3981           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3982             :     }
    3983             : 
    3984       41910 :     for ( i = 0; i < RENDERER_MAX_ISM_INPUTS; i++ )
    3985             :     {
    3986       33528 :         hIvasRend->inputsIsm[i].object_id = i;
    3987             :     }
    3988             : 
    3989        8382 :     return IVAS_ERR_OK;
    3990             : }
    3991             : #endif
    3992             : 
    3993             : 
    3994             : /*-------------------------------------------------------------------*
    3995             :  * IVAS_REND_SetInputGain()
    3996             :  *
    3997             :  *
    3998             :  *-------------------------------------------------------------------*/
    3999             : 
    4000       20786 : ivas_error IVAS_REND_SetInputGain(
    4001             :     IVAS_REND_HANDLE hIvasRend,      /* i/o: Renderer handle            */
    4002             :     const IVAS_REND_InputId inputId, /* i  : ID of the input            */
    4003             :     const float gain                 /* i  : linear gain (not in dB)    */
    4004             : )
    4005             : {
    4006             :     input_base *inputBase;
    4007             :     ivas_error error;
    4008             : 
    4009             :     /* Validate function arguments */
    4010       20786 :     if ( hIvasRend == NULL )
    4011             :     {
    4012           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4013             :     }
    4014             : 
    4015       20786 :     if ( ( error = getInputById( hIvasRend, inputId, (void **) &inputBase ) ) != IVAS_ERR_OK )
    4016             :     {
    4017           0 :         return error;
    4018             :     }
    4019             : 
    4020       20786 :     inputBase->gain = gain;
    4021             : 
    4022       20786 :     return IVAS_ERR_OK;
    4023             : }
    4024             : 
    4025             : 
    4026             : /*-------------------------------------------------------------------*
    4027             :  * IVAS_REND_SetInputLfeMtx()
    4028             :  *
    4029             :  *
    4030             :  *-------------------------------------------------------------------*/
    4031             : 
    4032           0 : ivas_error IVAS_REND_SetInputLfeMtx(
    4033             :     IVAS_REND_HANDLE hIvasRend,          /* i/o: Renderer handle        */
    4034             :     const IVAS_REND_InputId inputId,     /* i  : ID of the input        */
    4035             :     const IVAS_REND_LfePanMtx *lfePanMtx /* i  : LFE panning matrix     */
    4036             : )
    4037             : {
    4038             :     int16_t i;
    4039             :     input_base *pInputBase;
    4040             :     input_mc *pInputMc;
    4041             :     ivas_error error;
    4042             : 
    4043             :     /* Validate function arguments */
    4044           0 :     if ( hIvasRend == NULL )
    4045             :     {
    4046           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4047             :     }
    4048             : 
    4049           0 :     if ( ( error = getInputById( hIvasRend, inputId, (void **) &pInputBase ) ) != IVAS_ERR_OK )
    4050             :     {
    4051           0 :         return error;
    4052             :     }
    4053             : 
    4054           0 :     if ( getAudioConfigType( pInputBase->inConfig ) != IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED )
    4055             :     {
    4056             :         /* Custom LFE panning matrix only makes sense with channel-based input */
    4057           0 :         return IVAS_ERR_INVALID_INPUT_FORMAT;
    4058             :     }
    4059           0 :     pInputMc = (input_mc *) pInputBase;
    4060             : 
    4061             :     /* copy LFE panning matrix */
    4062           0 :     for ( i = 0; i < RENDERER_MAX_INPUT_LFE_CHANNELS; i++ )
    4063             :     {
    4064           0 :         mvr2r( ( *lfePanMtx )[i], pInputMc->lfeRouting.lfePanMtx[i], RENDERER_MAX_OUTPUT_CHANNELS );
    4065             :     }
    4066             : 
    4067           0 :     if ( ( error = updateMcPanGains( pInputMc, hIvasRend->outputConfig ) ) != IVAS_ERR_OK )
    4068             :     {
    4069           0 :         return error;
    4070             :     }
    4071             : 
    4072           0 :     return IVAS_ERR_OK;
    4073             : }
    4074             : 
    4075             : 
    4076             : /*-------------------------------------------------------------------*
    4077             :  * IVAS_REND_SetInputLfePos()
    4078             :  *
    4079             :  *
    4080             :  *-------------------------------------------------------------------*/
    4081             : 
    4082           0 : ivas_error IVAS_REND_SetInputLfePos(
    4083             :     IVAS_REND_HANDLE hIvasRend,      /* i/o: Renderer handle                                     */
    4084             :     const IVAS_REND_InputId inputId, /* i  : ID of the input                                     */
    4085             :     const float inputGain,           /* i  : Input gain to be applied to the LFE channel(s)      */
    4086             :     const float outputAzimuth,       /* i  : Output azimuth position                             */
    4087             :     const float outputElevation      /* i  : Output elevation position                           */
    4088             : )
    4089             : {
    4090             :     input_base *pInputBase;
    4091             :     input_mc *pInputMc;
    4092             :     ivas_error error;
    4093             : 
    4094             :     /* Validate function arguments */
    4095           0 :     if ( hIvasRend == NULL )
    4096             :     {
    4097           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4098             :     }
    4099             : 
    4100           0 :     if ( ( error = getInputById( hIvasRend, inputId, (void **) &pInputBase ) ) != IVAS_ERR_OK )
    4101             :     {
    4102           0 :         return error;
    4103             :     }
    4104             : 
    4105           0 :     if ( getAudioConfigType( pInputBase->inConfig ) != IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED )
    4106             :     {
    4107             :         /* Custom LFE routing only makes sense with channel-based input */
    4108           0 :         return IVAS_ERR_INVALID_INPUT_FORMAT;
    4109             :     }
    4110           0 :     pInputMc = (input_mc *) pInputBase;
    4111             : 
    4112           0 :     pInputMc->lfeRouting.pan_lfe = true;
    4113           0 :     pInputMc->lfeRouting.lfeInputGain = inputGain;
    4114           0 :     pInputMc->lfeRouting.lfeOutputAzimuth = outputAzimuth;
    4115           0 :     pInputMc->lfeRouting.lfeOutputElevation = outputElevation;
    4116             : 
    4117           0 :     if ( ( error = updateMcPanGains( pInputMc, hIvasRend->outputConfig ) ) != IVAS_ERR_OK )
    4118             :     {
    4119           0 :         return error;
    4120             :     }
    4121             : 
    4122           0 :     return IVAS_ERR_OK;
    4123             : }
    4124             : 
    4125             : 
    4126             : /*-------------------------------------------------------------------*
    4127             :  * IVAS_REND_RemoveInput()
    4128             :  *
    4129             :  *
    4130             :  *-------------------------------------------------------------------*/
    4131             : /* ToDo; unused function */
    4132           0 : ivas_error IVAS_REND_RemoveInput(
    4133             :     IVAS_REND_HANDLE hIvasRend,     /* i/o: Renderer handle     */
    4134             :     const IVAS_REND_InputId inputId /* i  : ID of the input     */
    4135             : )
    4136             : {
    4137             :     ivas_error error;
    4138             :     input_base *inputBase;
    4139             : 
    4140             :     /* Validate function arguments */
    4141           0 :     if ( hIvasRend == NULL )
    4142             :     {
    4143           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4144             :     }
    4145             : 
    4146           0 :     if ( ( error = getInputById( hIvasRend, inputId, (void **) &inputBase ) ) != IVAS_ERR_OK )
    4147             :     {
    4148           0 :         return error;
    4149             :     }
    4150             : 
    4151           0 :     switch ( getAudioConfigType( inputBase->inConfig ) )
    4152             :     {
    4153           0 :         case IVAS_REND_AUDIO_CONFIG_TYPE_OBJECT_BASED:
    4154           0 :             clearInputIsm( (input_ism *) inputBase );
    4155           0 :             break;
    4156           0 :         case IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED:
    4157           0 :             clearInputMc( (input_mc *) inputBase );
    4158           0 :             break;
    4159           0 :         case IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS:
    4160           0 :             clearInputSba( (input_sba *) inputBase );
    4161           0 :             break;
    4162           0 :         case IVAS_REND_AUDIO_CONFIG_TYPE_MASA:
    4163           0 :             clearInputMasa( (input_masa *) inputBase );
    4164           0 :             break;
    4165           0 :         default:
    4166           0 :             return IVAS_ERR_INVALID_INPUT_FORMAT;
    4167             :     }
    4168             : 
    4169             :     /* set global maximum delay after removing an input */
    4170           0 :     setMaxGlobalDelayNs( hIvasRend );
    4171             : 
    4172           0 :     return IVAS_ERR_OK;
    4173             : }
    4174             : 
    4175             : 
    4176             : /*-------------------------------------------------------------------*
    4177             :  * IVAS_REND_GetInputNumChannels()
    4178             :  *
    4179             :  *
    4180             :  *-------------------------------------------------------------------*/
    4181             : 
    4182    10746744 : ivas_error IVAS_REND_GetInputNumChannels(
    4183             :     IVAS_REND_CONST_HANDLE hIvasRend, /* i  : Renderer handle                   */
    4184             :     const IVAS_REND_InputId inputId,  /* i  : ID of the input                   */
    4185             :     int16_t *numChannels              /* o  : number of channels of the input   */
    4186             : )
    4187             : {
    4188             :     ivas_error error;
    4189             :     const input_base *pInput;
    4190             : 
    4191             :     /* Validate function arguments */
    4192    10746744 :     if ( hIvasRend == NULL || numChannels == NULL )
    4193             :     {
    4194           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4195             :     }
    4196             : 
    4197    10746744 :     if ( ( error = getConstInputById( hIvasRend, inputId, (const void **) &pInput ) ) != IVAS_ERR_OK )
    4198             :     {
    4199           0 :         return error;
    4200             :     }
    4201             : 
    4202    10746744 :     if ( ( error = getRendInputNumChannels( pInput, numChannels ) ) != IVAS_ERR_OK )
    4203             :     {
    4204           0 :         return error;
    4205             :     }
    4206             : 
    4207    10746744 :     return IVAS_ERR_OK;
    4208             : }
    4209             : 
    4210             : 
    4211             : /*-------------------------------------------------------------------*
    4212             :  * IVAS_REND_GetNumAllObjects()
    4213             :  *
    4214             :  *
    4215             :  *-------------------------------------------------------------------*/
    4216             : 
    4217       13146 : ivas_error IVAS_REND_GetNumAllObjects(
    4218             :     IVAS_REND_CONST_HANDLE hIvasRend, /* i  : Renderer handle       */
    4219             :     int16_t *numChannels              /* o  : number of all objects */
    4220             : )
    4221             : {
    4222       13146 :     if ( hIvasRend == NULL || numChannels == NULL )
    4223             :     {
    4224           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4225             :     }
    4226             : 
    4227       13146 :     if ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_MASA1 || hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_MASA2 )
    4228             :     {
    4229          36 :         *numChannels = (int16_t) hIvasRend->inputsIsm[0].total_num_objects;
    4230             :     }
    4231             : 
    4232       13146 :     return IVAS_ERR_OK;
    4233             : }
    4234             : 
    4235             : 
    4236             : /*-------------------------------------------------------------------*
    4237             :  * IVAS_REND_GetDelay()
    4238             :  *
    4239             :  *
    4240             :  *-------------------------------------------------------------------*/
    4241             : 
    4242           0 : ivas_error IVAS_REND_GetDelay(
    4243             :     IVAS_REND_CONST_HANDLE hIvasRend, /* i  : Renderer state                                                    */
    4244             :     int16_t *nSamples,                /* o  : Renderer delay in samples                                         */
    4245             :     int32_t *timeScale                /* o  : Time scale of the delay, equal to renderer output sampling rate   */
    4246             : )
    4247             : {
    4248             :     int32_t max_latency_ns;
    4249             : 
    4250             :     /* Validate function arguments */
    4251           0 :     if ( hIvasRend == NULL || nSamples == NULL || timeScale == NULL )
    4252             :     {
    4253           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4254             :     }
    4255             : 
    4256           0 :     *nSamples = 0;
    4257           0 :     *timeScale = hIvasRend->sampleRateOut;
    4258             : 
    4259           0 :     max_latency_ns = getMaxGlobalDelayNs( hIvasRend );
    4260             : 
    4261           0 :     *nSamples = latencyNsToSamples( hIvasRend->sampleRateOut, max_latency_ns );
    4262             : 
    4263           0 :     return IVAS_ERR_OK;
    4264             : }
    4265             : 
    4266             : /*-------------------------------------------------------------------*
    4267             :  * IVAS_REND_FeedInputAudio()
    4268             :  *
    4269             :  *
    4270             :  *-------------------------------------------------------------------*/
    4271             : 
    4272    35521204 : ivas_error IVAS_REND_FeedInputAudio(
    4273             :     IVAS_REND_HANDLE hIvasRend,                     /* i/o: Renderer handle          */
    4274             :     const IVAS_REND_InputId inputId,                /* i  : ID of the input          */
    4275             :     const IVAS_REND_ReadOnlyAudioBuffer inputAudio, /* i  : buffer with input audio  */
    4276             :     const bool flushInputs                          /* i  : flush input audio        */
    4277             : )
    4278             : {
    4279             :     ivas_error error;
    4280             :     input_base *inputBase;
    4281             :     int16_t numInputChannels;
    4282             :     int16_t cldfb2tdSampleFact;
    4283             : 
    4284             :     /* Validate function arguments */
    4285    35521204 :     if ( hIvasRend == NULL || inputAudio.data == NULL )
    4286             :     {
    4287           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4288             :     }
    4289             : 
    4290    35521204 :     cldfb2tdSampleFact = ( inputAudio.config.is_cldfb ) ? 2 : 1;
    4291             : 
    4292    35521204 :     if ( inputAudio.config.numSamplesPerChannel <= 0 || ( L_FRAME_MAX < inputAudio.config.numSamplesPerChannel && inputAudio.config.is_cldfb == 0 ) ||
    4293    35521204 :          ( ( L_FRAME_MAX * cldfb2tdSampleFact ) < inputAudio.config.numSamplesPerChannel && inputAudio.config.is_cldfb == 1 ) )
    4294             :     {
    4295           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_BUFFER_SIZE, "Buffer size outside of supported range" );
    4296             :     }
    4297             : 
    4298    35521204 :     if ( inputAudio.config.numChannels <= 0 || MAX_INPUT_CHANNELS < inputAudio.config.numChannels )
    4299             :     {
    4300           0 :         return IVAS_ERR_WRONG_NUM_CHANNELS;
    4301             :     }
    4302             : 
    4303    35521204 :     if ( getAudioConfigType( hIvasRend->outputConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL &&
    4304    12520008 :          hIvasRend->outputConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED &&
    4305    12520008 :          hIvasRend->outputConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM &&
    4306    12520008 :          ( inputAudio.config.numSamplesPerChannel * 1000 / cldfb2tdSampleFact ) != ( BINAURAL_RENDERING_FRAME_SIZE_MS * hIvasRend->num_subframes ) * hIvasRend->sampleRateOut )
    4307             :     {
    4308           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_BUFFER_SIZE, "Binaural rendering requires specific frame size" );
    4309             :     }
    4310             : 
    4311    35521204 :     if ( ( error = getInputById( hIvasRend, inputId, (void **) &inputBase ) ) != IVAS_ERR_OK )
    4312             :     {
    4313           0 :         return error;
    4314             :     }
    4315             : 
    4316    35521204 :     if ( ( error = getRendInputNumChannels( inputBase, &numInputChannels ) ) != IVAS_ERR_OK )
    4317             :     {
    4318           0 :         return error;
    4319             :     }
    4320             : 
    4321    35521204 :     if ( ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_MASA1 || hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_MASA2 ) && inputBase->inConfig == IVAS_AUDIO_CONFIG_OBA )
    4322             :     {
    4323        7836 :         numInputChannels = (int16_t) hIvasRend->inputsIsm[0].total_num_objects;
    4324             :     }
    4325             : 
    4326    35521204 :     if ( numInputChannels != inputAudio.config.numChannels )
    4327             :     {
    4328           0 :         return IVAS_ERR_WRONG_NUM_CHANNELS;
    4329             :     }
    4330             : 
    4331    35521204 :     inputBase->inputBuffer.config = inputAudio.config;
    4332    35521204 :     if ( ( error = alignInputDelay(
    4333             :                inputBase,
    4334             :                inputAudio,
    4335             :                hIvasRend->maxGlobalDelayNs,
    4336             :                hIvasRend->sampleRateOut,
    4337             :                cldfb2tdSampleFact,
    4338             :                flushInputs ) ) != IVAS_ERR_OK )
    4339             :     {
    4340           0 :         return error;
    4341             :     }
    4342    35521204 :     inputBase->numNewSamplesPerChannel = inputAudio.config.numSamplesPerChannel / cldfb2tdSampleFact;
    4343             : 
    4344    35521204 :     return IVAS_ERR_OK;
    4345             : }
    4346             : 
    4347             : 
    4348             : /*-------------------------------------------------------------------*
    4349             :  * IVAS_REND_FeedInputObjectMetadata()
    4350             :  *
    4351             :  *
    4352             :  *-------------------------------------------------------------------*/
    4353             : 
    4354    24787410 : ivas_error IVAS_REND_FeedInputObjectMetadata(
    4355             :     IVAS_REND_HANDLE hIvasRend,            /* i/o: Renderer handle          */
    4356             :     const IVAS_REND_InputId inputId,       /* i  : ID of the input          */
    4357             :     const IVAS_ISM_METADATA objectPosition /* i  : object position struct   */
    4358             : )
    4359             : {
    4360             :     input_base *inputBase;
    4361             :     input_ism *inputIsm;
    4362             :     ivas_error error;
    4363             : 
    4364             :     /* Validate function arguments */
    4365    24787410 :     if ( hIvasRend == NULL )
    4366             :     {
    4367           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4368             :     }
    4369             : 
    4370    24787410 :     if ( ( error = getInputById( hIvasRend, inputId, (void **) &inputBase ) ) != IVAS_ERR_OK )
    4371             :     {
    4372           0 :         return error;
    4373             :     }
    4374             : 
    4375    24787410 :     if ( inputBase->inConfig != IVAS_AUDIO_CONFIG_OBA )
    4376             :     {
    4377             :         /* Object metadata should only be fed for object inputs */
    4378           0 :         return IVAS_ERR_METADATA_NOT_EXPECTED;
    4379             :     }
    4380             : 
    4381    24787410 :     inputIsm = (input_ism *) inputBase;
    4382    24787410 :     inputIsm->previousPos = inputIsm->currentPos;
    4383    24787410 :     inputIsm->currentPos = objectPosition;
    4384             : 
    4385    24787410 :     return IVAS_ERR_OK;
    4386             : }
    4387             : 
    4388             : 
    4389             : /*-------------------------------------------------------------------*
    4390             :  * IVAS_REND_FeedInputObjectMetadataToOMasa()
    4391             :  *
    4392             :  *
    4393             :  *-------------------------------------------------------------------*/
    4394             : 
    4395       20496 : ivas_error IVAS_REND_FeedInputObjectMetadataToOMasa(
    4396             :     IVAS_REND_HANDLE hIvasRend,            /* i/o: Renderer handle          */
    4397             :     const int16_t inputIndex,              /* i  : Index of the input       */
    4398             :     const IVAS_ISM_METADATA objectPosition /* i  : object position struct   */
    4399             : )
    4400             : {
    4401             :     /* Validate function arguments */
    4402       20496 :     if ( hIvasRend == NULL )
    4403             :     {
    4404           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4405             :     }
    4406             : 
    4407             :     /* Set position to OMasa struct */
    4408       20496 :     hIvasRend->inputsIsm->hOMasa->ism_azimuth[inputIndex] = objectPosition.azimuth;
    4409       20496 :     hIvasRend->inputsIsm->hOMasa->ism_elevation[inputIndex] = objectPosition.elevation;
    4410             : 
    4411       20496 :     return IVAS_ERR_OK;
    4412             : }
    4413             : 
    4414             : 
    4415             : /*-------------------------------------------------------------------*
    4416             :  * IVAS_REND_FeedInputMasaMetadata()
    4417             :  *
    4418             :  *
    4419             :  *-------------------------------------------------------------------*/
    4420             : 
    4421      448800 : ivas_error IVAS_REND_FeedInputMasaMetadata(
    4422             :     IVAS_REND_HANDLE hIvasRend,            /* i/o: Renderer handle      */
    4423             :     const IVAS_REND_InputId inputId,       /* i  : ID of the input      */
    4424             :     IVAS_MASA_METADATA_HANDLE masaMetadata /* i  : MASA metadata frame  */
    4425             : )
    4426             : {
    4427             :     ivas_error error;
    4428             :     input_base *inputBase;
    4429             :     input_masa *inputMasa;
    4430             : 
    4431             :     /* Validate function arguments */
    4432      448800 :     if ( hIvasRend == NULL )
    4433             :     {
    4434           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4435             :     }
    4436             : 
    4437      448800 :     if ( ( error = getInputById( hIvasRend, inputId, (void **) &inputBase ) ) != IVAS_ERR_OK )
    4438             :     {
    4439           0 :         return error;
    4440             :     }
    4441             : 
    4442      448800 :     if ( getAudioConfigType( inputBase->inConfig ) != IVAS_REND_AUDIO_CONFIG_TYPE_MASA )
    4443             :     {
    4444             :         /* MASA metadata should only be fed for MASA inputs */
    4445           0 :         return IVAS_ERR_METADATA_NOT_EXPECTED;
    4446             :     }
    4447             : 
    4448      448800 :     inputMasa = (input_masa *) inputBase;
    4449      448800 :     inputMasa->masaMetadata = *masaMetadata;
    4450      448800 :     inputMasa->metadataHasBeenFed = true;
    4451             : 
    4452      448800 :     return IVAS_ERR_OK;
    4453             : }
    4454             : 
    4455             : 
    4456             : /*-------------------------------------------------------------------*
    4457             :  * IVAS_REND_InitConfig()
    4458             :  *
    4459             :  *
    4460             :  *-------------------------------------------------------------------*/
    4461             : 
    4462        8382 : ivas_error IVAS_REND_InitConfig(
    4463             :     IVAS_REND_HANDLE hIvasRend,       /* i/o: Renderer handle     */
    4464             :     const AUDIO_CONFIG outAudioConfig /* i  : output audioConfig  */
    4465             : )
    4466             : {
    4467             :     ivas_error error;
    4468             :     bool rendererConfigEnabled;
    4469             : 
    4470        8382 :     rendererConfigEnabled = ( getAudioConfigType( outAudioConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL );
    4471             : 
    4472        8382 :     if ( rendererConfigEnabled )
    4473             :     {
    4474        2664 :         hIvasRend->rendererConfigEnabled = 1;
    4475             :     }
    4476             :     else
    4477             :     {
    4478        5718 :         hIvasRend->rendererConfigEnabled = 0;
    4479             :     }
    4480             : 
    4481        8382 :     if ( rendererConfigEnabled )
    4482             :     {
    4483        2664 :         if ( ( error = ivas_render_config_open( &( hIvasRend->hRendererConfig ) ) ) != IVAS_ERR_OK )
    4484             :         {
    4485           0 :             return error;
    4486             :         }
    4487        2664 :         if ( ( error = ivas_render_config_init_from_rom( &hIvasRend->hRendererConfig ) ) != IVAS_ERR_OK )
    4488             :         {
    4489           0 :             return error;
    4490             :         }
    4491             :     }
    4492             :     else
    4493             :     {
    4494        5718 :         hIvasRend->hRendererConfig = NULL;
    4495             :     }
    4496             : 
    4497        8382 :     return IVAS_ERR_OK;
    4498             : }
    4499             : 
    4500             : 
    4501             : /*-------------------------------------------------------------------*
    4502             :  * IVAS_REND_GetRenderConfig()
    4503             :  *
    4504             :  *
    4505             :  *-------------------------------------------------------------------*/
    4506             : 
    4507           0 : ivas_error IVAS_REND_GetRenderConfig(
    4508             :     IVAS_REND_HANDLE hIvasRend,            /* i/o: IVAS renderer handle        */
    4509             :     const IVAS_RENDER_CONFIG_HANDLE hRCout /* o  : Render configuration handle */
    4510             : )
    4511             : {
    4512             :     RENDER_CONFIG_HANDLE hRCin;
    4513             : 
    4514           0 :     if ( hIvasRend == NULL || hIvasRend->hRendererConfig == NULL || hRCout == NULL )
    4515             :     {
    4516           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4517             :     }
    4518             : 
    4519           0 :     hRCin = hIvasRend->hRendererConfig;
    4520             : #ifdef DEBUGGING
    4521             :     switch ( hRCin->renderer_type_override )
    4522             :     {
    4523             :         case IVAS_RENDER_TYPE_OVERRIDE_CREND:
    4524             :             hRCout->renderer_type_override = IVAS_RENDER_TYPE_OVERRIDE_CREND;
    4525             :             break;
    4526             :         case IVAS_RENDER_TYPE_OVERRIDE_FASTCONV:
    4527             :             hRCout->renderer_type_override = IVAS_RENDER_TYPE_OVERRIDE_FASTCONV;
    4528             :             break;
    4529             :         default:
    4530             :             hRCout->renderer_type_override = IVAS_RENDER_TYPE_OVERRIDE_NONE;
    4531             :             break;
    4532             :     }
    4533             : #endif
    4534           0 :     hRCout->roomAcoustics.nBands = hRCin->roomAcoustics.nBands;
    4535           0 :     hRCout->roomAcoustics.acousticPreDelay = hRCin->roomAcoustics.acousticPreDelay;
    4536           0 :     hRCout->roomAcoustics.inputPreDelay = hRCin->roomAcoustics.inputPreDelay;
    4537           0 :     mvr2r( hRCin->directivity, hRCout->directivity, 3 * MAX_NUM_OBJECTS );
    4538             : 
    4539           0 :     mvr2r( hRCin->roomAcoustics.pFc_input, hRCout->roomAcoustics.pFc_input, CLDFB_NO_CHANNELS_MAX );
    4540           0 :     mvr2r( hRCin->roomAcoustics.pAcoustic_rt60, hRCout->roomAcoustics.pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
    4541           0 :     mvr2r( hRCin->roomAcoustics.pAcoustic_dsr, hRCout->roomAcoustics.pAcoustic_dsr, CLDFB_NO_CHANNELS_MAX );
    4542             : 
    4543           0 :     hRCout->split_rend_config.splitRendBitRate = ISAR_MAX_SPLIT_REND_BITRATE;
    4544           0 :     hRCout->split_rend_config.dof = 3;
    4545           0 :     hRCout->split_rend_config.hq_mode = 0;
    4546           0 :     hRCout->split_rend_config.codec_delay_ms = 0;
    4547           0 :     hRCout->split_rend_config.isar_frame_size_ms = 20;
    4548           0 :     hRCout->split_rend_config.codec_frame_size_ms = 0; /* 0 means "use default for selected codec" */
    4549           0 :     hRCout->split_rend_config.codec = ISAR_SPLIT_REND_CODEC_DEFAULT;
    4550           0 :     hRCout->split_rend_config.poseCorrectionMode = ISAR_SPLIT_REND_POSE_CORRECTION_MODE_CLDFB;
    4551           0 :     hRCout->split_rend_config.rendererSelection = hRCin->split_rend_config.rendererSelection;
    4552           0 :     hRCout->split_rend_config.lc3plus_highres = 0;
    4553             : 
    4554           0 :     hRCout->roomAcoustics.use_er = hRCin->roomAcoustics.use_er;
    4555           0 :     hRCout->roomAcoustics.lowComplexity = hRCin->roomAcoustics.lowComplexity;
    4556             : 
    4557           0 :     mvr2r( hRCin->distAtt, hRCout->distAtt, 3 );
    4558             : 
    4559           0 :     return IVAS_ERR_OK;
    4560             : }
    4561             : 
    4562             : 
    4563             : /*-------------------------------------------------------------------*
    4564             :  * IVAS_REND_FeedRenderConfig()
    4565             :  *
    4566             :  *
    4567             :  *-------------------------------------------------------------------*/
    4568             : 
    4569           0 : ivas_error IVAS_REND_FeedRenderConfig(
    4570             :     IVAS_REND_HANDLE hIvasRend,                /* i/o: IVAS renderer handle        */
    4571             :     const IVAS_RENDER_CONFIG_DATA renderConfig /* i  : Render configuration struct */
    4572             : )
    4573             : {
    4574             :     RENDER_CONFIG_HANDLE hRenderConfig;
    4575             :     uint16_t i;
    4576             :     input_ism *pIsmInput;
    4577             :     input_masa *pMasaInput;
    4578             :     input_mc *pMcInput;
    4579             :     input_sba *pSbaInput;
    4580             :     ivas_error error;
    4581             : 
    4582           0 :     if ( hIvasRend == NULL || hIvasRend->hRendererConfig == NULL )
    4583             :     {
    4584           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4585             :     }
    4586           0 :     hRenderConfig = hIvasRend->hRendererConfig;
    4587             : #ifdef DEBUGGING
    4588             :     hRenderConfig->renderer_type_override = renderConfig.renderer_type_override;
    4589             : #endif
    4590           0 :     hRenderConfig->roomAcoustics.nBands = renderConfig.roomAcoustics.nBands;
    4591           0 :     hRenderConfig->roomAcoustics.acousticPreDelay = renderConfig.roomAcoustics.acousticPreDelay;
    4592           0 :     hRenderConfig->roomAcoustics.inputPreDelay = renderConfig.roomAcoustics.inputPreDelay;
    4593           0 :     mvr2r( renderConfig.roomAcoustics.pFc_input, hRenderConfig->roomAcoustics.pFc_input, CLDFB_NO_CHANNELS_MAX );
    4594           0 :     mvr2r( renderConfig.roomAcoustics.pAcoustic_rt60, hRenderConfig->roomAcoustics.pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
    4595           0 :     mvr2r( renderConfig.roomAcoustics.pAcoustic_dsr, hRenderConfig->roomAcoustics.pAcoustic_dsr, CLDFB_NO_CHANNELS_MAX );
    4596           0 :     mvr2r( renderConfig.directivity, hRenderConfig->directivity, 3 * MAX_NUM_OBJECTS );
    4597           0 :     mvr2r( renderConfig.distAtt, hRenderConfig->distAtt, 3 );
    4598             : 
    4599           0 :     hRenderConfig->roomAcoustics.use_er = 0;
    4600           0 :     if ( renderConfig.roomAcoustics.use_er == 1 )
    4601             :     {
    4602           0 :         hRenderConfig->roomAcoustics.use_er = renderConfig.roomAcoustics.use_er;
    4603           0 :         hRenderConfig->roomAcoustics.lowComplexity = renderConfig.roomAcoustics.lowComplexity;
    4604           0 :         hRenderConfig->roomAcoustics.dimensions = renderConfig.roomAcoustics.dimensions;
    4605           0 :         hRenderConfig->roomAcoustics.ListenerOrigin = renderConfig.roomAcoustics.ListenerOrigin;
    4606             : 
    4607           0 :         mvr2r( renderConfig.roomAcoustics.AbsCoeff, hRenderConfig->roomAcoustics.AbsCoeff, IVAS_ROOM_ABS_COEFF );
    4608             :     }
    4609             : 
    4610             :     /* Re-initialize reverb instance if already available */
    4611             :     /* ISM inputs */
    4612           0 :     for ( i = 0, pIsmInput = hIvasRend->inputsIsm; i < RENDERER_MAX_ISM_INPUTS; ++i, ++pIsmInput )
    4613             :     {
    4614           0 :         if ( pIsmInput->base.inConfig == IVAS_AUDIO_CONFIG_INVALID )
    4615             :         {
    4616             :             /* Skip inactive inputs */
    4617           0 :             continue;
    4618             :         }
    4619           0 :         if ( pIsmInput->hReverb != NULL )
    4620             :         {
    4621           0 :             if ( ( error = ivas_reverb_open( &pIsmInput->hReverb, hIvasRend->hHrtfs.hHrtfStatistics, hRenderConfig, *pIsmInput->base.ctx.pOutSampleRate ) ) != IVAS_ERR_OK )
    4622             :             {
    4623           0 :                 return error;
    4624             :             }
    4625             :         }
    4626           0 :         if ( pIsmInput->crendWrapper != NULL && pIsmInput->crendWrapper->hCrend[0] != NULL )
    4627             :         {
    4628           0 :             if ( ( error = ivas_reverb_open( &pIsmInput->crendWrapper->hCrend[0]->hReverb, hIvasRend->hHrtfs.hHrtfStatistics, hRenderConfig, *pIsmInput->base.ctx.pOutSampleRate ) ) != IVAS_ERR_OK )
    4629             :             {
    4630           0 :                 return error;
    4631             :             }
    4632             :         }
    4633             :     }
    4634             : 
    4635             :     /* MASA inputs */
    4636           0 :     for ( i = 0, pMasaInput = hIvasRend->inputsMasa; i < RENDERER_MAX_MASA_INPUTS; ++i, ++pMasaInput )
    4637             :     {
    4638           0 :         if ( pMasaInput->base.inConfig == IVAS_AUDIO_CONFIG_INVALID )
    4639             :         {
    4640             :             /* Skip inactive inputs */
    4641           0 :             continue;
    4642             :         }
    4643             : 
    4644           0 :         if ( pMasaInput->hMasaExtRend != NULL )
    4645             :         {
    4646           0 :             if ( pMasaInput->hMasaExtRend->hDiracDecBin[0] != NULL && pMasaInput->hMasaExtRend->hDiracDecBin[0]->hReverb != NULL )
    4647             :             {
    4648           0 :                 ivas_binaural_reverb_close( &pMasaInput->hMasaExtRend->hDiracDecBin[0]->hReverb );
    4649             : 
    4650           0 :                 if ( ( error = ivas_binaural_reverb_init( &pMasaInput->hMasaExtRend->hDiracDecBin[0]->hReverb,
    4651             :                                                           hIvasRend->hHrtfs.hHrtfStatistics,
    4652           0 :                                                           pMasaInput->hMasaExtRend->hSpatParamRendCom->num_freq_bands,
    4653             :                                                           CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES,
    4654           0 :                                                           &( hRenderConfig->roomAcoustics ),
    4655           0 :                                                           *pMasaInput->base.ctx.pOutSampleRate,
    4656             :                                                           NULL,
    4657             :                                                           NULL,
    4658             :                                                           NULL ) ) != IVAS_ERR_OK )
    4659             :                 {
    4660           0 :                     return error;
    4661             :                 }
    4662             :             }
    4663             : 
    4664           0 :             if ( pMasaInput->hMasaExtRend->hReverb != NULL )
    4665             :             {
    4666           0 :                 ivas_binaural_reverb_close( &pMasaInput->hMasaExtRend->hReverb );
    4667             : 
    4668           0 :                 if ( ( error = ivas_binaural_reverb_init( &pMasaInput->hMasaExtRend->hReverb,
    4669             :                                                           hIvasRend->hHrtfs.hHrtfStatistics,
    4670           0 :                                                           pMasaInput->hMasaExtRend->hSpatParamRendCom->num_freq_bands,
    4671             :                                                           CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES,
    4672           0 :                                                           &( hRenderConfig->roomAcoustics ),
    4673           0 :                                                           *pMasaInput->base.ctx.pOutSampleRate,
    4674             :                                                           NULL,
    4675             :                                                           NULL,
    4676             :                                                           NULL ) ) != IVAS_ERR_OK )
    4677             :                 {
    4678           0 :                     return error;
    4679             :                 }
    4680             :             }
    4681             :         }
    4682             :     }
    4683             : 
    4684             :     /* Multi-channel inputs */
    4685           0 :     for ( i = 0, pMcInput = hIvasRend->inputsMc; i < RENDERER_MAX_MC_INPUTS; ++i, ++pMcInput )
    4686             :     {
    4687           0 :         if ( pMcInput->base.inConfig == IVAS_AUDIO_CONFIG_INVALID )
    4688             :         {
    4689             :             /* Skip inactive inputs */
    4690           0 :             continue;
    4691             :         }
    4692             : 
    4693           0 :         if ( pMcInput->hReverb != NULL )
    4694             :         {
    4695           0 :             if ( ( error = ivas_reverb_open( &pMcInput->hReverb, hIvasRend->hHrtfs.hHrtfStatistics, hRenderConfig, *pMcInput->base.ctx.pOutSampleRate ) ) != IVAS_ERR_OK )
    4696             :             {
    4697           0 :                 return error;
    4698             :             }
    4699             :         }
    4700             : 
    4701           0 :         if ( pMcInput->crendWrapper != NULL && pMcInput->crendWrapper->hCrend[0] && pMcInput->crendWrapper->hCrend[0]->hReverb != NULL )
    4702             :         {
    4703           0 :             if ( ( error = ivas_reverb_open( &pMcInput->crendWrapper->hCrend[0]->hReverb, hIvasRend->hHrtfs.hHrtfStatistics, hRenderConfig, *pMcInput->base.ctx.pOutSampleRate ) ) != IVAS_ERR_OK )
    4704             :             {
    4705           0 :                 return error;
    4706             :             }
    4707             :         }
    4708             :     }
    4709             : 
    4710             :     /* SBA inputs */
    4711           0 :     for ( i = 0, pSbaInput = hIvasRend->inputsSba; i < RENDERER_MAX_SBA_INPUTS; ++i, ++pSbaInput )
    4712             :     {
    4713           0 :         if ( pSbaInput->base.inConfig == IVAS_AUDIO_CONFIG_INVALID )
    4714             :         {
    4715             :             /* Skip inactive inputs */
    4716           0 :             continue;
    4717             :         }
    4718             : 
    4719           0 :         if ( pSbaInput->crendWrapper != NULL && pSbaInput->crendWrapper->hCrend[0] != NULL && pSbaInput->crendWrapper->hCrend[0]->hReverb != NULL )
    4720             :         {
    4721           0 :             if ( ( error = ivas_reverb_open( &pSbaInput->crendWrapper->hCrend[0]->hReverb, hIvasRend->hHrtfs.hHrtfStatistics, hRenderConfig, *pSbaInput->base.ctx.pOutSampleRate ) ) != IVAS_ERR_OK )
    4722             :             {
    4723           0 :                 return error;
    4724             :             }
    4725             :         }
    4726             :     }
    4727             : 
    4728           0 :     hRenderConfig->split_rend_config = renderConfig.split_rend_config;
    4729             :     /* Overwrite any pose correction settings if 0 DOF (no pose correction) was selected */
    4730           0 :     if ( hRenderConfig->split_rend_config.dof == 0 )
    4731             :     {
    4732           0 :         hRenderConfig->split_rend_config.poseCorrectionMode = ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE;
    4733             :     }
    4734             : 
    4735           0 :     hRenderConfig->split_rend_config.codec = renderConfig.split_rend_config.codec;
    4736             : 
    4737           0 :     if ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    4738             :     {
    4739           0 :         if ( ( error = isar_split_rend_validate_config( &hRenderConfig->split_rend_config, ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0 ) ) != IVAS_ERR_OK )
    4740             :         {
    4741           0 :             return error;
    4742             :         }
    4743             :     }
    4744             : 
    4745             :     /* Must re-initialize split rendering config in case renderer config is updated after adding renderer inputs */
    4746             :     /* if its not initialized yet then no need to re-initialize, initialization will happen while adding inputs*/
    4747           0 :     if ( hIvasRend->splitRendEncBuffer.data != NULL && hIvasRend->hRendererConfig != NULL )
    4748             :     {
    4749             :         int16_t cldfb_in_flag;
    4750           0 :         cldfb_in_flag = getCldfbRendFlag( hIvasRend, IVAS_REND_AUDIO_CONFIG_TYPE_UNKNOWN );
    4751             : 
    4752           0 :         if ( hIvasRend->splitRendWrapper != NULL )
    4753             :         {
    4754           0 :             ISAR_PRE_REND_close( hIvasRend->splitRendWrapper, &hIvasRend->splitRendEncBuffer );
    4755           0 :             free( hIvasRend->splitRendWrapper );
    4756           0 :             hIvasRend->splitRendWrapper = NULL;
    4757             :         }
    4758             : 
    4759           0 :         if ( ( error = isar_pre_rend_init( hIvasRend->splitRendWrapper, &hIvasRend->splitRendEncBuffer, &hIvasRend->hRendererConfig->split_rend_config, hIvasRend->headRotData, hIvasRend->sampleRateOut, hIvasRend->outputConfig, cldfb_in_flag, hIvasRend->num_subframes ) ) != IVAS_ERR_OK )
    4760             :         {
    4761           0 :             return error;
    4762             :         }
    4763             :     }
    4764             : 
    4765           0 :     return IVAS_ERR_OK;
    4766             : }
    4767             : 
    4768             : 
    4769             : /*-------------------------------------------------------------------*
    4770             :  * IVAS_REND_SetHeadRotation()
    4771             :  *
    4772             :  *
    4773             :  *-------------------------------------------------------------------*/
    4774             : 
    4775     3748770 : ivas_error IVAS_REND_SetHeadRotation(
    4776             :     IVAS_REND_HANDLE hIvasRend,              /* i/o: Renderer handle                            */
    4777             :     const IVAS_QUATERNION headRot,           /* i  : head orientations for next rendering call  */
    4778             :     const IVAS_VECTOR3 Pos,                  /* i  : listener positions for next rendering call */
    4779             :     const ISAR_SPLIT_REND_ROT_AXIS rot_axis, /* i  : external control for rotation axis for split rendering                  */
    4780             :     const int16_t sf_idx                     /* i  : subframe index                             */
    4781             : )
    4782             : {
    4783             :     int16_t i;
    4784             :     IVAS_QUATERNION rotQuat;
    4785             :     ivas_error error;
    4786             : 
    4787             :     /* Validate function arguments */
    4788     3748770 :     if ( hIvasRend == NULL )
    4789             :     {
    4790           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4791             :     }
    4792             : 
    4793     3748770 :     if ( getAudioConfigType( hIvasRend->outputConfig ) != IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL )
    4794             :     {
    4795             :         /* Head rotation can be set only with binaural output */
    4796           0 :         return IVAS_ERR_INVALID_OUTPUT_FORMAT;
    4797             :     }
    4798             : 
    4799     3748770 :     hIvasRend->headRotData.headRotEnabled = 1;
    4800             : 
    4801             :     /* reconfigure binaural rendering to allocate the necessary renderers and free unused ones */
    4802     7497540 :     for ( i = 0; i < RENDERER_MAX_MC_INPUTS; ++i )
    4803             :     {
    4804     3748770 :         if ( hIvasRend->inputsMc[i].base.inConfig != IVAS_AUDIO_CONFIG_INVALID )
    4805             :         {
    4806      418410 :             if ( ( error = initMcBinauralRendering( &hIvasRend->inputsMc[i],
    4807             :                                                     hIvasRend->inputsMc[i].base.inConfig,
    4808             :                                                     hIvasRend->outputConfig,
    4809             :                                                     hIvasRend->hRendererConfig,
    4810             :                                                     hIvasRend->hHrtfs.hSetOfHRTF,
    4811             :                                                     hIvasRend->hHrtfs.hHrtfStatistics,
    4812             :                                                     TRUE ) ) != IVAS_ERR_OK )
    4813             :             {
    4814           0 :                 return error;
    4815             :             }
    4816             :         }
    4817             :     }
    4818             : 
    4819             :     /* check for Euler angle signaling */
    4820     3748770 :     if ( headRot.w == -3.0f )
    4821             :     {
    4822           0 :         Euler2Quat( deg2rad( headRot.x ), deg2rad( headRot.y ), deg2rad( headRot.z ), &rotQuat );
    4823             :     }
    4824             :     else
    4825             :     {
    4826     3748770 :         rotQuat = headRot;
    4827             :     }
    4828             : 
    4829     3748770 :     if ( ( error = ivas_orient_trk_Process( hIvasRend->headRotData.hOrientationTracker, rotQuat, FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES, &hIvasRend->headRotData.headPositions[sf_idx] ) ) != IVAS_ERR_OK )
    4830             :     {
    4831           0 :         return error;
    4832             :     }
    4833             : 
    4834     3748770 :     hIvasRend->headRotData.Pos[sf_idx] = Pos;
    4835             : 
    4836     3748770 :     hIvasRend->headRotData.sr_pose_pred_axis = rot_axis;
    4837             : 
    4838     3748770 :     return IVAS_ERR_OK;
    4839             : }
    4840             : 
    4841             : 
    4842             : /*-------------------------------------------------------------------*
    4843             :  * IVAS_REND_DisableHeadRotation()
    4844             :  *
    4845             :  *
    4846             :  *-------------------------------------------------------------------*/
    4847             : 
    4848    11398592 : ivas_error IVAS_REND_DisableHeadRotation(
    4849             :     IVAS_REND_HANDLE hIvasRend /* i/o: Renderer handle  */
    4850             : )
    4851             : {
    4852             :     int16_t i;
    4853             :     ivas_error error;
    4854             : 
    4855             :     /* Validate function arguments */
    4856    11398592 :     if ( hIvasRend == NULL )
    4857             :     {
    4858           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4859             :     }
    4860             : 
    4861    11398592 :     hIvasRend->headRotData.headRotEnabled = 0;
    4862             : 
    4863             :     /* reconfigure binaural rendering to allocate the necessary renderers and free unused ones */
    4864    11398592 :     if ( getAudioConfigType( hIvasRend->outputConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL )
    4865             :     {
    4866     4683816 :         for ( i = 0; i < RENDERER_MAX_MC_INPUTS; ++i )
    4867             :         {
    4868     2341908 :             if ( hIvasRend->inputsMc[i].base.inConfig != IVAS_AUDIO_CONFIG_INVALID )
    4869             :             {
    4870      261324 :                 if ( ( error = initMcBinauralRendering( &hIvasRend->inputsMc[i],
    4871             :                                                         hIvasRend->inputsMc[i].base.inConfig,
    4872             :                                                         hIvasRend->outputConfig,
    4873             :                                                         hIvasRend->hRendererConfig,
    4874             :                                                         hIvasRend->hHrtfs.hSetOfHRTF,
    4875             :                                                         hIvasRend->hHrtfs.hHrtfStatistics,
    4876             :                                                         TRUE ) ) != IVAS_ERR_OK )
    4877             :                 {
    4878           0 :                     return error;
    4879             :                 }
    4880             :             }
    4881             :         }
    4882             :     }
    4883             : 
    4884    11398592 :     return IVAS_ERR_OK;
    4885             : }
    4886             : 
    4887             : 
    4888             : /*-------------------------------------------------------------------*
    4889             :  * IVAS_REND_SetSplitRendBFI()
    4890             :  *
    4891             :  *
    4892             :  *-------------------------------------------------------------------*/
    4893             : 
    4894           0 : ivas_error IVAS_REND_SetSplitRendBFI(
    4895             :     IVAS_REND_HANDLE hIvasRend, /* i/o: Renderer handle         */
    4896             :     const int16_t bfi           /* i  : bad frame indicator     */
    4897             : )
    4898             : {
    4899           0 :     hIvasRend->splitRendBFI = bfi;
    4900             : 
    4901           0 :     return IVAS_ERR_OK;
    4902             : }
    4903             : 
    4904             : 
    4905             : /*-------------------------------------------------------------------*
    4906             :  * IVAS_REND_SetOrientationTrackingMode()
    4907             :  *
    4908             :  *
    4909             :  *-------------------------------------------------------------------*/
    4910             : 
    4911        8382 : ivas_error IVAS_REND_SetOrientationTrackingMode(
    4912             :     IVAS_REND_HANDLE hIvasRend,                       /* i/o: Renderer handle                */
    4913             :     const IVAS_HEAD_ORIENT_TRK_T orientation_tracking /* i  : Head orientation tracking type */
    4914             : )
    4915             : {
    4916        8382 :     if ( hIvasRend->headRotData.headRotEnabled == 0 )
    4917             :     {
    4918        7050 :         return IVAS_ERR_OK;
    4919             :     }
    4920             : 
    4921        1332 :     return ivas_orient_trk_SetTrackingType( hIvasRend->headRotData.hOrientationTracker, orientation_tracking );
    4922             : }
    4923             : 
    4924             : 
    4925             : /*-------------------------------------------------------------------*
    4926             :  * IVAS_REND_SetReferenceRotation()
    4927             :  *
    4928             :  *
    4929             :  *-------------------------------------------------------------------*/
    4930             : 
    4931           0 : ivas_error IVAS_REND_SetReferenceRotation(
    4932             :     IVAS_REND_HANDLE hIvasRend,  /* i/o: Renderer handle        */
    4933             :     const IVAS_QUATERNION refRot /* i  : Reference rotation     */
    4934             : )
    4935             : {
    4936             :     ivas_error error;
    4937             : 
    4938             :     /* Validate function arguments */
    4939           0 :     if ( hIvasRend == NULL )
    4940             :     {
    4941           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4942             :     }
    4943             : 
    4944           0 :     if ( ( error = ivas_orient_trk_SetReferenceRotation( hIvasRend->headRotData.hOrientationTracker, refRot ) ) != IVAS_ERR_OK )
    4945             :     {
    4946           0 :         return error;
    4947             :     }
    4948             : 
    4949           0 :     return IVAS_ERR_OK;
    4950             : }
    4951             : 
    4952             : 
    4953             : /*-------------------------------------------------------------------*
    4954             :  * IVAS_REND_GetMainOrientation()
    4955             :  *
    4956             :  *
    4957             :  *-------------------------------------------------------------------*/
    4958             : // ToDo: not used
    4959           0 : ivas_error IVAS_REND_GetMainOrientation(
    4960             :     IVAS_REND_HANDLE hIvasRend,   /* i/o: Renderer handle                           */
    4961             :     IVAS_QUATERNION *pOrientation /* i/o: Quaternion pointer for main orientation   */
    4962             : )
    4963             : {
    4964             :     ivas_error error;
    4965             : 
    4966           0 :     if ( hIvasRend == NULL || pOrientation == NULL )
    4967             :     {
    4968           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4969             :     }
    4970             : 
    4971           0 :     if ( ( error = ivas_orient_trk_GetMainOrientation( hIvasRend->headRotData.hOrientationTracker, pOrientation ) ) != IVAS_ERR_OK )
    4972             :     {
    4973           0 :         return error;
    4974             :     }
    4975             : 
    4976           0 :     return IVAS_ERR_OK;
    4977             : }
    4978             : 
    4979             : 
    4980             : /*-------------------------------------------------------------------*
    4981             :  * IVAS_REND_GetTrackedRotation()
    4982             :  *
    4983             :  *
    4984             :  *-------------------------------------------------------------------*/
    4985             : // ToDo: not used
    4986           0 : ivas_error IVAS_REND_GetTrackedRotation(
    4987             :     IVAS_REND_HANDLE hIvasRend, /* i/o: Renderer handle                             */
    4988             :     IVAS_QUATERNION *pRotation  /* i/o: Quaternion pointer processed rotation       */
    4989             : )
    4990             : {
    4991             :     ivas_error error;
    4992             : 
    4993           0 :     if ( hIvasRend == NULL || pRotation == NULL )
    4994             :     {
    4995           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4996             :     }
    4997             : 
    4998           0 :     if ( ( error = ivas_orient_trk_GetTrackedRotation( hIvasRend->headRotData.hOrientationTracker, pRotation ) ) != IVAS_ERR_OK )
    4999             :     {
    5000           0 :         return error;
    5001             :     }
    5002             : 
    5003           0 :     return IVAS_ERR_OK;
    5004             : }
    5005             : 
    5006             : 
    5007             : /*---------------------------------------------------------------------*
    5008             :  * IVAS_REND_SetReferenceVector( )
    5009             :  *
    5010             :  * Sets a reference vector spanning from listenerPos to refPos. Only
    5011             :  * available in OTR_TRACKING_REF_VEC and OTR_TRACKING_REF_VEC_LEV modes.
    5012             :  *---------------------------------------------------------------------*/
    5013             : 
    5014           0 : ivas_error IVAS_REND_SetReferenceVector(
    5015             :     IVAS_REND_HANDLE hIvasRend,     /* i/o: Renderer handle             */
    5016             :     const IVAS_VECTOR3 listenerPos, /* i  : Listener position           */
    5017             :     const IVAS_VECTOR3 refPos       /* i  : Reference position          */
    5018             : )
    5019             : {
    5020           0 :     if ( hIvasRend == NULL || hIvasRend->headRotData.hOrientationTracker == NULL )
    5021             :     {
    5022           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    5023             :     }
    5024             : 
    5025           0 :     return ivas_orient_trk_SetReferenceVector( hIvasRend->headRotData.hOrientationTracker, listenerPos, refPos );
    5026             : }
    5027             : 
    5028             : 
    5029             : /*---------------------------------------------------------------------*
    5030             :  * IVAS_REND_SetExternalOrientation()
    5031             :  *
    5032             :  *
    5033             :  *---------------------------------------------------------------------*/
    5034             : 
    5035           0 : ivas_error IVAS_REND_SetExternalOrientation(
    5036             :     IVAS_REND_HANDLE hIvasRend,           /* i/o: Renderer handle                                                         */
    5037             :     IVAS_QUATERNION *orientation,         /* i  : external orientation data                                               */
    5038             :     int8_t enableHeadRotation,            /* i  : flag to enable head rotation for this frame                             */
    5039             :     int8_t enableExternalOrientation,     /* i  : flag to enable external orientation for this frame                      */
    5040             :     int8_t enableRotationInterpolation,   /* i  : flag to interpolate rotations from current and previous frames          */
    5041             :     int16_t numFramesToTargetOrientation, /* i  : number of frames until target orientation is reached                    */
    5042             :     const int16_t sf_idx                  /* i  : subframe index                                                          */
    5043             : )
    5044             : {
    5045             :     /* Validate function arguments */
    5046           0 :     if ( hIvasRend == NULL || hIvasRend->hExternalOrientationData == NULL )
    5047             :     {
    5048           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    5049             :     }
    5050             : 
    5051           0 :     if ( orientation == NULL )
    5052             :     {
    5053           0 :         hIvasRend->hExternalOrientationData->enableExternalOrientation[sf_idx] = 0;
    5054             :     }
    5055             :     else
    5056             :     {
    5057           0 :         QuaternionInverse( *orientation, &hIvasRend->hExternalOrientationData->Quaternions[sf_idx] );
    5058             : 
    5059           0 :         hIvasRend->hExternalOrientationData->enableHeadRotation[sf_idx] = enableHeadRotation;
    5060           0 :         hIvasRend->hExternalOrientationData->enableExternalOrientation[sf_idx] = enableExternalOrientation;
    5061           0 :         hIvasRend->hExternalOrientationData->enableRotationInterpolation[sf_idx] = enableRotationInterpolation;
    5062           0 :         hIvasRend->hExternalOrientationData->numFramesToTargetOrientation[sf_idx] = numFramesToTargetOrientation;
    5063             :     }
    5064             : 
    5065           0 :     return IVAS_ERR_OK;
    5066             : }
    5067             : 
    5068             : 
    5069             : /*---------------------------------------------------------------------*
    5070             :  * IVAS_REND_CombineHeadAndExternalOrientation()
    5071             :  *
    5072             :  *
    5073             :  *---------------------------------------------------------------------*/
    5074             : 
    5075    13740500 : ivas_error IVAS_REND_CombineHeadAndExternalOrientation(
    5076             :     IVAS_REND_HANDLE hIvasRend /* i/o: Renderer handle      */
    5077             : )
    5078             : {
    5079    13740500 :     if ( hIvasRend == NULL )
    5080             :     {
    5081           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    5082             :     }
    5083             : 
    5084    13740500 :     return combine_external_and_head_orientations_rend( &hIvasRend->headRotData, hIvasRend->hExternalOrientationData, hIvasRend->hCombinedOrientationData );
    5085             : }
    5086             : 
    5087             : 
    5088             : /*---------------------------------------------------------------------*
    5089             :  * IVAS_REND_GetCombinedOrientation()
    5090             :  *
    5091             :  *
    5092             :  *---------------------------------------------------------------------*/
    5093             : // ToDo: not used
    5094           0 : ivas_error IVAS_REND_GetCombinedOrientation(
    5095             :     IVAS_REND_HANDLE hIvasRend,   /* i/o: Renderer handle                                */
    5096             :     IVAS_QUATERNION *pOrientation /* i/o: Quaternion pointer processed orientation       */
    5097             : )
    5098             : {
    5099             :     int16_t i;
    5100             : 
    5101           0 :     if ( hIvasRend == NULL || pOrientation == NULL )
    5102             :     {
    5103           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    5104             :     }
    5105             : 
    5106           0 :     if ( hIvasRend->hCombinedOrientationData != NULL )
    5107             :     {
    5108           0 :         for ( i = 0; i < hIvasRend->hCombinedOrientationData->num_subframes; ++i )
    5109             :         {
    5110           0 :             pOrientation[i] = hIvasRend->hCombinedOrientationData->Quaternions[i];
    5111             :         }
    5112             :     }
    5113             : 
    5114           0 :     return IVAS_ERR_OK;
    5115             : }
    5116             : 
    5117             : 
    5118             : /*-------------------------------------------------------------------*
    5119             :  * Local functions
    5120             :  *-------------------------------------------------------------------*/
    5121             : 
    5122             : /* Take one channel from input buffer and copy it to each channel
    5123             :    in output buffer, with different gain applied per output channel.
    5124             :    This function takes 2 gain vectors - one for the beginning and one
    5125             :    for the end of the buffer. Gain values are lineraly interpolated
    5126             :    for all samples in between. */
    5127    87074682 : static void renderBufferChannelLerp(
    5128             :     const IVAS_REND_AudioBuffer inAudio,
    5129             :     const int32_t inChannelIdx,
    5130             :     const float *const gainsCurrent,
    5131             :     const float *const gainsPrev,
    5132             :     IVAS_REND_AudioBuffer outAudio )
    5133             : {
    5134             :     const float *inSmpl;
    5135             :     float *outSmpl;
    5136             :     float fadeIn;
    5137             :     float fadeOut;
    5138             :     int32_t i;
    5139             :     const float *lastInSmpl;
    5140             :     int16_t outChnlIdx;
    5141             :     float currentGain;
    5142             :     float previousGain;
    5143             : 
    5144             :     /* Pointer to behind last input sample */
    5145    87074682 :     lastInSmpl = getSmplPtr( inAudio, inChannelIdx, inAudio.config.numSamplesPerChannel );
    5146             : 
    5147   827969814 :     for ( outChnlIdx = 0; outChnlIdx < outAudio.config.numChannels; ++outChnlIdx )
    5148             :     {
    5149   740895132 :         currentGain = gainsCurrent[outChnlIdx];
    5150   740895132 :         previousGain = gainsPrev == NULL ? 0.f : gainsPrev[outChnlIdx];
    5151             : 
    5152             :         /* Process current output channel only if applying non-zero gains */
    5153   740895132 :         if ( fabsf( currentGain ) > EPSILON || ( gainsPrev != NULL && fabsf( previousGain ) > EPSILON ) )
    5154             :         {
    5155             :             /* Reset input pointer to the beginning of input channel */
    5156   427864978 :             inSmpl = getSmplPtr( inAudio, inChannelIdx, 0 );
    5157             : 
    5158             :             /* Set output pointer to first output channel sample */
    5159   427864978 :             outSmpl = getSmplPtr( outAudio, outChnlIdx, 0 );
    5160             : 
    5161   427864978 :             if ( gainsPrev == NULL || fabsf( previousGain - currentGain ) <= EPSILON )
    5162             :             {
    5163             :                 /* If no interpolation from previous frame, apply current gain */
    5164             :                 do
    5165             :                 {
    5166 >11644*10^7 :                     *outSmpl += currentGain * ( *inSmpl );
    5167 >11644*10^7 :                     ++outSmpl;
    5168 >11644*10^7 :                     ++inSmpl;
    5169             : 
    5170 >11644*10^7 :                 } while ( inSmpl != lastInSmpl );
    5171             :             }
    5172             :             else
    5173             :             {
    5174    16178802 :                 i = 0;
    5175             : 
    5176             :                 /* Otherwise use weighted average between previous and current gain */
    5177             :                 do
    5178             :                 {
    5179  6777159360 :                     fadeIn = (float) i / ( outAudio.config.numSamplesPerChannel - 1 );
    5180  6777159360 :                     fadeOut = 1.0f - fadeIn;
    5181             : 
    5182  6777159360 :                     *outSmpl += ( fadeIn * currentGain + fadeOut * previousGain ) * ( *inSmpl );
    5183  6777159360 :                     ++outSmpl;
    5184  6777159360 :                     ++inSmpl;
    5185  6777159360 :                     ++i;
    5186  6777159360 :                 } while ( inSmpl != lastInSmpl );
    5187             :             }
    5188             :         }
    5189             :     }
    5190             : 
    5191    87074682 :     return;
    5192             : }
    5193             : 
    5194             : 
    5195             : /* Take one channel from input buffer and copy it to each channel
    5196             :    in output buffer, with different gain applied per output channel */
    5197    68231592 : static void renderBufferChannel(
    5198             :     const IVAS_REND_AudioBuffer inAudio,
    5199             :     const int32_t inChannelIdx,
    5200             :     const float *const outputGains,
    5201             :     IVAS_REND_AudioBuffer outAudio )
    5202             : {
    5203    68231592 :     renderBufferChannelLerp( inAudio, inChannelIdx, outputGains, NULL, outAudio );
    5204             : 
    5205    68231592 :     return;
    5206             : }
    5207             : 
    5208             : 
    5209     1504308 : static ivas_error chooseCrossfade(
    5210             :     const IVAS_REND_HeadRotData *headRotData,
    5211             :     const float **pCrossfade )
    5212             : {
    5213     1504308 :     *pCrossfade = headRotData->crossfade;
    5214             : 
    5215     1504308 :     return IVAS_ERR_OK;
    5216             : }
    5217             : 
    5218      153228 : static ivas_error rotateFrameMc(
    5219             :     IVAS_REND_AudioBuffer inAudio,                               /* i  : Input Audio buffer               */
    5220             :     AUDIO_CONFIG inConfig,                                       /* i  : Input Audio config               */
    5221             :     const LSSETUP_CUSTOM_STRUCT *pInCustomLs,                    /* i  : Input Custom LS setup            */
    5222             :     const IVAS_REND_HeadRotData *headRotData,                    /* i  : Head rotation data               */
    5223             :     const COMBINED_ORIENTATION_HANDLE *hCombinedOrientationData, /* i  : Combined head and external orientations */
    5224             :     rotation_gains gains_prev,                                   /* i/o: Previous frame rotation gains    */
    5225             :     const EFAP_HANDLE hEFAPdata,                                 /* i  : EFAP structure                   */
    5226             :     IVAS_REND_AudioBuffer outAudio                               /* o  : Output Audio buffer              */
    5227             : )
    5228             : {
    5229             :     int16_t i;
    5230             :     int16_t j;
    5231             :     const float *crossfade;
    5232             :     int16_t num_subframes;
    5233             :     int16_t subframe_idx, subframe_len;
    5234             :     int16_t azimuth, elevation;
    5235             :     int16_t is_planar_setup, lfe_idx;
    5236             :     int16_t nchan;
    5237             :     int16_t ch_in, ch_out;
    5238             :     int16_t ch_in_woLFE, ch_out_woLFE;
    5239             :     float *readPtr, *writePtr;
    5240             :     const float *ls_azimuth, *ls_elevation;
    5241             :     rotation_matrix Rmat;
    5242             :     rotation_gains gains;
    5243             :     float tmp_gains[MAX_INPUT_CHANNELS];
    5244             :     ivas_error error;
    5245             : 
    5246      153228 :     push_wmops( "rotateFrameMc" );
    5247      153228 :     if ( ( error = chooseCrossfade( headRotData, &crossfade ) ) != IVAS_ERR_OK )
    5248             :     {
    5249           0 :         return error;
    5250             :     }
    5251      153228 :     num_subframes = ( hCombinedOrientationData != NULL ) ? ( *hCombinedOrientationData )->num_subframes : MAX_PARAM_SPATIAL_SUBFRAMES;
    5252             : 
    5253      153228 :     if ( inConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM )
    5254             :     {
    5255       75132 :         if ( ( error = getAudioConfigNumChannels( inConfig, &nchan ) ) != IVAS_ERR_OK )
    5256             :         {
    5257           0 :             return error;
    5258             :         }
    5259             :     }
    5260             :     else
    5261             :     {
    5262       78096 :         nchan = pInCustomLs->num_spk + pInCustomLs->num_lfe;
    5263             :     }
    5264             : 
    5265      153228 :     if ( ( error = getMcConfigValues( inConfig, pInCustomLs, &ls_azimuth, &ls_elevation, &lfe_idx, &is_planar_setup ) ) != IVAS_ERR_OK )
    5266             :     {
    5267           0 :         return error;
    5268             :     }
    5269             : 
    5270             :     /* initialize gains to passthrough */
    5271     1916724 :     for ( ch_in = 0; ch_in < nchan; ch_in++ )
    5272             :     {
    5273     1763496 :         set_zero( gains[ch_in], nchan );
    5274     1763496 :         gains[ch_in][ch_in] = 1.f;
    5275             :     }
    5276             : 
    5277             :     /* subframe loop */
    5278             : 
    5279      153228 :     subframe_len = inAudio.config.numSamplesPerChannel / num_subframes;
    5280      398598 :     for ( subframe_idx = 0; subframe_idx < num_subframes; subframe_idx++ )
    5281             :     {
    5282      981480 :         for ( i = 0; i < 3; i++ )
    5283             :         {
    5284      736110 :             if ( hCombinedOrientationData != NULL )
    5285             :             {
    5286     2944440 :                 for ( j = 0; j < 3; j++ )
    5287             :                 {
    5288     2208330 :                     Rmat[i][j] = ( *hCombinedOrientationData )->Rmat[subframe_idx][i][j];
    5289             :                 }
    5290             :             }
    5291             :             else
    5292             :             {
    5293             :                 /* Set to identity */
    5294           0 :                 set_zero( Rmat[i], 3 );
    5295           0 :                 Rmat[i][i] = 1.0f;
    5296             :             }
    5297             :         }
    5298             : 
    5299     3069210 :         for ( ch_in = 0; ch_in < nchan; ch_in++ )
    5300             :         {
    5301             :             /* skip LFE */
    5302     2823840 :             if ( ch_in == lfe_idx )
    5303             :             {
    5304      120330 :                 continue;
    5305             :             }
    5306             : 
    5307             :             /* input channel index without LFE */
    5308     2703510 :             ch_in_woLFE = ( ( lfe_idx > 0 ) && ( ch_in >= lfe_idx ) ) ? ch_in - 1 : ch_in;
    5309             : 
    5310             :             /* gains for current subframe rotation */
    5311     2703510 :             rotateAziEle( ls_azimuth[ch_in_woLFE], ls_elevation[ch_in_woLFE], &azimuth, &elevation, Rmat, is_planar_setup );
    5312             : 
    5313     2703510 :             if ( hEFAPdata != NULL && ( ls_azimuth[ch_in_woLFE] != azimuth || ls_elevation[ch_in_woLFE] != elevation ) )
    5314             :             {
    5315      870270 :                 efap_determine_gains( hEFAPdata, tmp_gains, azimuth, elevation, EFAP_MODE_EFAP );
    5316             : 
    5317     8687430 :                 for ( ch_out = 0; ch_out < nchan; ch_out++ )
    5318             :                 {
    5319             :                     /* skip LFE */
    5320     7817160 :                     if ( ch_out == lfe_idx )
    5321             :                     {
    5322      870270 :                         continue;
    5323             :                     }
    5324             : 
    5325             :                     /* output channel index without LFE */
    5326     6946890 :                     ch_out_woLFE = ( ( lfe_idx > 0 ) && ( ch_out >= lfe_idx ) ) ? ch_out - 1 : ch_out;
    5327             : 
    5328     6946890 :                     gains[ch_in][ch_out] = tmp_gains[ch_out_woLFE];
    5329             :                 }
    5330             :             }
    5331             :         }
    5332             : 
    5333             :         /* apply panning gains by mtx multiplication */
    5334     3069210 :         for ( ch_out = 0; ch_out < nchan; ch_out++ )
    5335             :         {
    5336    39310560 :             for ( ch_in = 0; ch_in < nchan; ch_in++ )
    5337             :             {
    5338    36486720 :                 writePtr = getSmplPtr( outAudio, ch_out, subframe_idx * subframe_len );
    5339    36486720 :                 readPtr = getSmplPtr( inAudio, ch_in, subframe_idx * subframe_len );
    5340             :                 /* crossfade with previous rotation gains */
    5341  5874361920 :                 for ( i = 0; i < subframe_len; i++ )
    5342             :                 {
    5343  5837875200 :                     *writePtr++ +=
    5344  5837875200 :                         ( *readPtr ) * ( ( 1 - crossfade[i] ) * gains_prev[ch_in][ch_out] ) +
    5345  5837875200 :                         ( *readPtr ) * ( crossfade[i] * gains[ch_in][ch_out] );
    5346  5837875200 :                     readPtr++;
    5347             :                 }
    5348             :             }
    5349             :         }
    5350             : 
    5351             :         /* move gains to gains_prev */
    5352     3069210 :         for ( i = 0; i < nchan; i++ )
    5353             :         {
    5354     2823840 :             mvr2r( gains[i], gains_prev[i], nchan );
    5355             :         }
    5356             :     }
    5357             : 
    5358      153228 :     pop_wmops();
    5359      153228 :     return IVAS_ERR_OK;
    5360             : }
    5361             : 
    5362             : 
    5363     1351080 : static ivas_error rotateFrameSba(
    5364             :     IVAS_REND_AudioBuffer inAudio,                               /* i  : Input Audio buffer               */
    5365             :     const AUDIO_CONFIG inConfig,                                 /* i  : Input Audio config               */
    5366             :     const IVAS_REND_HeadRotData *headRotData,                    /* i  : Head rotation data               */
    5367             :     const COMBINED_ORIENTATION_HANDLE *hCombinedOrientationData, /* i  : Combined head and external orientations */
    5368             :     rotation_gains gains_prev,                                   /* i/o: Previous frame rotation gains    */
    5369             :     IVAS_REND_AudioBuffer outAudio                               /* o  : Output Audio buffer              */
    5370             : )
    5371             : {
    5372             :     int16_t i, l, n, m;
    5373             :     int16_t m1, m2;
    5374             :     int16_t shd_rot_max_order;
    5375             :     const float *crossfade;
    5376             :     int16_t num_subframes;
    5377             :     int16_t subframe_idx, subframe_len;
    5378             : 
    5379             :     float *writePtr;
    5380             :     rotation_matrix Rmat;
    5381             :     float tmpRot[2 * HEADROT_ORDER + 1];
    5382             :     rotation_gains gains;
    5383             :     ivas_error error;
    5384             :     int16_t idx;
    5385             :     float val, cf, oneminuscf;
    5386             : 
    5387     1351080 :     push_wmops( "rotateFrameSba" );
    5388             : 
    5389     1351080 :     if ( ( error = chooseCrossfade( headRotData, &crossfade ) ) != IVAS_ERR_OK )
    5390             :     {
    5391           0 :         return error;
    5392             :     }
    5393     1351080 :     num_subframes = ( hCombinedOrientationData != NULL ) ? ( *hCombinedOrientationData )->num_subframes : MAX_PARAM_SPATIAL_SUBFRAMES;
    5394             : 
    5395     1351080 :     if ( ( error = getAmbisonicsOrder( inConfig, &shd_rot_max_order ) ) != IVAS_ERR_OK )
    5396             :     {
    5397           0 :         return error;
    5398             :     }
    5399             : 
    5400     1351080 :     subframe_len = inAudio.config.numSamplesPerChannel / num_subframes;
    5401     3513780 :     for ( subframe_idx = 0; subframe_idx < num_subframes; subframe_idx++ )
    5402             :     {
    5403             :         /* initialize rotation matrices with zeros */
    5404    36765900 :         for ( i = 0; i < HEADROT_SHMAT_DIM; i++ )
    5405             :         {
    5406    34603200 :             set_zero( gains[i], HEADROT_SHMAT_DIM );
    5407             :         }
    5408             : 
    5409     8650800 :         for ( i = 0; i < 3; i++ )
    5410             :         {
    5411     6488100 :             if ( hCombinedOrientationData != NULL )
    5412             :             {
    5413    25952400 :                 for ( l = 0; l < 3; l++ )
    5414             :                 {
    5415    19464300 :                     Rmat[i][l] = ( *hCombinedOrientationData )->Rmat[subframe_idx][i][l];
    5416             :                 }
    5417             :             }
    5418             :             else
    5419             :             {
    5420             :                 /* Set to identity */
    5421           0 :                 set_zero( Rmat[i], 3 );
    5422           0 :                 Rmat[i][i] = 1.0f;
    5423             :             }
    5424             :         }
    5425             : 
    5426             :         /* calculate ambisonics rotation matrices for the previous and current frames */
    5427     2162700 :         SHrotmatgen( gains, Rmat, shd_rot_max_order );
    5428             : 
    5429   348194700 :         for ( i = 0; i < subframe_len; i++ )
    5430             :         {
    5431   346032000 :             idx = subframe_idx * subframe_len + i;
    5432   346032000 :             cf = crossfade[i];
    5433   346032000 :             oneminuscf = 1 - cf;
    5434             :             /*    As the rotation matrix becomes block diagonal in a SH basis, we can*/
    5435             :             /*      apply each angular-momentum block individually to save complexity. */
    5436             : 
    5437             :             /*    loop over l blocks */
    5438   346032000 :             m1 = 1;
    5439   346032000 :             m2 = 4;
    5440  1038096000 :             for ( l = 1; l <= shd_rot_max_order; l++ )
    5441             :             {
    5442             :                 /* compute mtx-vector product for this l  */
    5443  3691008000 :                 for ( n = m1; n < m2; n++ )
    5444             :                 {
    5445  2998944000 :                     tmpRot[n - m1] = 0.f;
    5446             : 
    5447 17532288000 :                     for ( m = m1; m < m2; m++ )
    5448             :                     {
    5449 14533344000 :                         val = inAudio.data[m * inAudio.config.numSamplesPerChannel + idx];
    5450             :                         /* crossfade with previous rotation gains */
    5451 14533344000 :                         tmpRot[n - m1] += ( cf * gains[n][m] * val + oneminuscf * gains_prev[n][m] * val );
    5452             :                     }
    5453             :                 }
    5454             :                 /* write back the result */
    5455  3691008000 :                 for ( n = m1; n < m2; n++ )
    5456             :                 {
    5457  2998944000 :                     writePtr = getSmplPtr( outAudio, n, idx );
    5458  2998944000 :                     ( *writePtr ) = tmpRot[n - m1];
    5459             :                 }
    5460   692064000 :                 m1 = m2;
    5461   692064000 :                 m2 += 2 * ( l + 1 ) + 1;
    5462             :             }
    5463             :         }
    5464             : 
    5465             :         /* move SHrotmat to SHrotmat_prev */
    5466    36765900 :         for ( i = 0; i < HEADROT_SHMAT_DIM; i++ )
    5467             :         {
    5468    34603200 :             mvr2r( gains[i], gains_prev[i], HEADROT_SHMAT_DIM );
    5469             :         }
    5470             :     }
    5471             : 
    5472     1351080 :     pop_wmops();
    5473             : 
    5474     1351080 :     return IVAS_ERR_OK;
    5475             : }
    5476             : 
    5477             : 
    5478     2972160 : static ivas_error renderIsmToBinaural(
    5479             :     const input_ism *ismInput,
    5480             :     IVAS_REND_AudioBuffer outAudio )
    5481             : {
    5482             :     float tmpTDRendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    5483             :     ivas_error error;
    5484             :     int16_t ism_md_subframe_update_ext;
    5485             : 
    5486     2972160 :     push_wmops( "renderIsmToBinaural" );
    5487             :     /* Metadata Delay to sync with audio delay converted from ms to 5ms (1000/50/4) subframe index */
    5488     2972160 :     ism_md_subframe_update_ext = (int16_t) roundf( ismInput->ism_metadata_delay_ms / ( 1000.f / FRAMES_PER_SEC / MAX_PARAM_SPATIAL_SUBFRAMES ) );
    5489     2972160 :     copyBufferTo2dArray( ismInput->base.inputBuffer, tmpTDRendBuffer );
    5490             : 
    5491     2972160 :     if ( ( error = ivas_td_binaural_renderer_ext( &ismInput->tdRendWrapper, ismInput->base.inConfig, NULL, ismInput->base.ctx.pCombinedOrientationData, &ismInput->currentPos, ismInput->hReverb, ism_md_subframe_update_ext,
    5492     2972160 :                                                   *ismInput->base.ctx.pOutSampleRate, outAudio.config.numSamplesPerChannel, tmpTDRendBuffer ) ) != IVAS_ERR_OK )
    5493             :     {
    5494           0 :         return error;
    5495             :     }
    5496             : 
    5497     2972160 :     accumulate2dArrayToBuffer( tmpTDRendBuffer, &outAudio );
    5498             : 
    5499     2972160 :     pop_wmops();
    5500             : 
    5501     2972160 :     return IVAS_ERR_OK;
    5502             : }
    5503             : 
    5504             : 
    5505           0 : static int16_t getNumSubframesInBuffer(
    5506             :     const IVAS_REND_AudioBuffer *buffer,
    5507             :     const int32_t sampleRate )
    5508             : {
    5509             :     int16_t cldfb2tdSampleFact;
    5510             : 
    5511           0 :     cldfb2tdSampleFact = buffer->config.is_cldfb ? 2 : 1;
    5512             : 
    5513             : #ifdef DEBUGGING
    5514             :     assert( buffer->config.numSamplesPerChannel % ( sampleRate / FRAMES_PER_SEC / MAX_PARAM_SPATIAL_SUBFRAMES * cldfb2tdSampleFact ) == 0 );
    5515             : #endif
    5516             : 
    5517           0 :     return (int16_t) ( buffer->config.numSamplesPerChannel / ( sampleRate / FRAMES_PER_SEC / MAX_PARAM_SPATIAL_SUBFRAMES * cldfb2tdSampleFact ) );
    5518             : }
    5519             : 
    5520             : 
    5521     2972160 : static ivas_error renderIsmToBinauralRoom(
    5522             :     input_ism *ismInput,
    5523             :     IVAS_REND_AudioBuffer outAudio )
    5524             : {
    5525             :     int16_t position_changed;
    5526             :     int16_t i, j;
    5527             :     int16_t azi_rot, ele_rot;
    5528             :     int16_t subframe_idx;
    5529             :     int16_t tmp;
    5530             :     rotation_matrix Rmat;
    5531             :     float tmpRendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    5532             :     ivas_error error;
    5533             :     pan_vector currentPanGains;
    5534             :     IVAS_REND_AudioBuffer tmpMcBuffer;
    5535             :     IVAS_ISM_METADATA rotatedPosPrev;
    5536             :     IVAS_ISM_METADATA rotatedPos;
    5537             :     const COMBINED_ORIENTATION_HANDLE *hCombinedOrientationData;
    5538             :     int8_t combinedOrientationEnabled;
    5539             :     float *p_tmpRendBuffer[MAX_OUTPUT_CHANNELS];
    5540             : 
    5541    50526720 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    5542             :     {
    5543    47554560 :         p_tmpRendBuffer[i] = tmpRendBuffer[i];
    5544             :     }
    5545             : 
    5546     2972160 :     push_wmops( "renderIsmToBinauralRoom" );
    5547             : 
    5548     2972160 :     rotatedPosPrev = defaultObjectPosition();
    5549     2972160 :     rotatedPos = defaultObjectPosition();
    5550             : 
    5551     2972160 :     hCombinedOrientationData = ismInput->base.ctx.pCombinedOrientationData;
    5552     2972160 :     combinedOrientationEnabled = 0;
    5553     2972160 :     if ( *hCombinedOrientationData != NULL )
    5554             :     {
    5555     1486080 :         for ( subframe_idx = 0; subframe_idx < ( *hCombinedOrientationData )->num_subframes; subframe_idx++ )
    5556             :         {
    5557             : 
    5558     1486080 :             if ( ( *hCombinedOrientationData )->enableCombinedOrientation[subframe_idx] != 0 )
    5559             :             {
    5560     1486080 :                 combinedOrientationEnabled = 1;
    5561     1486080 :                 break;
    5562             :             }
    5563             :         }
    5564             :     }
    5565             : 
    5566     2972160 :     if ( combinedOrientationEnabled )
    5567             :     {
    5568     2972160 :         for ( subframe_idx = 0; subframe_idx < 1; subframe_idx++ )
    5569             :         {
    5570     5944320 :             for ( i = 0; i < 3; i++ )
    5571             :             {
    5572     4458240 :                 if ( hCombinedOrientationData != NULL && ( *hCombinedOrientationData )->enableCombinedOrientation[subframe_idx] )
    5573             :                 {
    5574    17832960 :                     for ( j = 0; j < 3; j++ )
    5575             :                     {
    5576    13374720 :                         Rmat[i][j] = ( *hCombinedOrientationData )->Rmat[subframe_idx][i][j];
    5577             :                     }
    5578             :                 }
    5579             :                 else
    5580             :                 {
    5581             :                     /* Set to identity */
    5582           0 :                     set_zero( Rmat[i], 3 );
    5583           0 :                     Rmat[i][i] = 1.0f;
    5584             :                 }
    5585             :             }
    5586             :         }
    5587             :     }
    5588             : 
    5589             :     /* get previous position */
    5590     2972160 :     if ( combinedOrientationEnabled )
    5591             :     {
    5592     1486080 :         rotateAziEle( ismInput->previousPos.azimuth, ismInput->previousPos.elevation, &azi_rot, &ele_rot, ismInput->rot_mat_prev, 0 );
    5593     1486080 :         rotatedPosPrev.azimuth = (float) azi_rot;
    5594     1486080 :         rotatedPosPrev.elevation = (float) ele_rot;
    5595             :     }
    5596             :     else
    5597             :     {
    5598     1486080 :         rotatedPosPrev.azimuth = ismInput->previousPos.azimuth;
    5599     1486080 :         rotatedPosPrev.elevation = ismInput->previousPos.elevation;
    5600             :     }
    5601             : 
    5602             :     /* get current position */
    5603     2972160 :     if ( combinedOrientationEnabled )
    5604             :     {
    5605     1486080 :         rotateAziEle( ismInput->currentPos.azimuth, ismInput->currentPos.elevation, &azi_rot, &ele_rot, Rmat, 0 );
    5606     1486080 :         rotatedPos.azimuth = (float) azi_rot;
    5607     1486080 :         rotatedPos.elevation = (float) ele_rot;
    5608             :     }
    5609             :     else
    5610             :     {
    5611     1486080 :         rotatedPos.azimuth = ismInput->currentPos.azimuth;
    5612     1486080 :         rotatedPos.elevation = ismInput->currentPos.elevation;
    5613             :     }
    5614             : 
    5615     2972160 :     position_changed = !ismInput->firstFrameRendered || checkObjectPositionChanged( &rotatedPos, &rotatedPosPrev );
    5616             : 
    5617             :     /* set previous gains if this is the first frame */
    5618     2972160 :     if ( ( error = getEfapGains( *ismInput->base.ctx.pEfapOutWrapper, rotatedPosPrev.azimuth, rotatedPosPrev.elevation, ismInput->prev_pan_gains ) ) != IVAS_ERR_OK )
    5619             :     {
    5620           0 :         return error;
    5621             :     }
    5622             : 
    5623             :     /* compute gains only if position changed */
    5624     2972160 :     if ( position_changed )
    5625             :     {
    5626      679212 :         if ( ( error = getEfapGains( *ismInput->base.ctx.pEfapOutWrapper,
    5627             :                                      rotatedPos.azimuth,
    5628             :                                      rotatedPos.elevation,
    5629             :                                      currentPanGains ) ) != IVAS_ERR_OK )
    5630             :         {
    5631           0 :             return error;
    5632             :         }
    5633             :     }
    5634             : 
    5635             :     /* intermediate rendering to 7_1_4 */
    5636     2972160 :     tmpMcBuffer = ismInput->base.inputBuffer;
    5637             : 
    5638     2972160 :     if ( ( error = getAudioConfigNumChannels( IVAS_AUDIO_CONFIG_7_1_4, &tmp ) ) != IVAS_ERR_OK )
    5639             :     {
    5640           0 :         return error;
    5641             :     }
    5642             : 
    5643     2972160 :     tmpMcBuffer.config.numChannels = tmp;
    5644     2972160 :     tmpMcBuffer.data = malloc( tmpMcBuffer.config.numSamplesPerChannel * tmpMcBuffer.config.numChannels * sizeof( float ) );
    5645     2972160 :     set_zero( tmpMcBuffer.data, tmpMcBuffer.config.numSamplesPerChannel * tmpMcBuffer.config.numChannels );
    5646             : 
    5647     2972160 :     renderBufferChannelLerp( ismInput->base.inputBuffer, 0,
    5648             :                              position_changed ? currentPanGains : ismInput->prev_pan_gains,
    5649             :                              position_changed ? ismInput->prev_pan_gains : NULL,
    5650             :                              tmpMcBuffer );
    5651             : 
    5652     2972160 :     copyBufferTo2dArray( tmpMcBuffer, tmpRendBuffer );
    5653             : 
    5654             :     /* save gains for next frame */
    5655    11888640 :     for ( i = 0; i < 3; i++ )
    5656             :     {
    5657     8916480 :         mvr2r( Rmat[i], ismInput->rot_mat_prev[i], 3 );
    5658             :     }
    5659             : 
    5660     2972160 :     if ( position_changed )
    5661             :     {
    5662      679212 :         mvr2r( currentPanGains, ismInput->prev_pan_gains, MAX_OUTPUT_CHANNELS );
    5663             :     }
    5664             : 
    5665             :     /* render 7_1_4 with BRIRs */
    5666     2972160 :     if ( ( error = ivas_rend_crendProcessSubframe( ismInput->crendWrapper, IVAS_AUDIO_CONFIG_7_1_4, IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR, NULL, NULL,
    5667     2972160 :                                                    NULL, NULL, NULL, p_tmpRendBuffer, p_tmpRendBuffer, ismInput->base.inputBuffer.config.numSamplesPerChannel, *ismInput->base.ctx.pOutSampleRate, 0 ) ) != IVAS_ERR_OK )
    5668             :     {
    5669           0 :         return error;
    5670             :     }
    5671             : 
    5672     2972160 :     accumulate2dArrayToBuffer( tmpRendBuffer, &outAudio );
    5673             : 
    5674     2972160 :     free( tmpMcBuffer.data );
    5675     2972160 :     pop_wmops();
    5676             : 
    5677     2972160 :     return IVAS_ERR_OK;
    5678             : }
    5679             : 
    5680             : 
    5681     2972160 : static ivas_error renderIsmToBinauralReverb(
    5682             :     input_ism *ismInput,
    5683             :     IVAS_REND_AudioBuffer outAudio )
    5684             : {
    5685             :     float tmpRendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    5686             :     ivas_error error;
    5687             :     int16_t ism_md_subframe_update_ext;
    5688             : 
    5689     2972160 :     push_wmops( "renderIsmToBinauralRoom" );
    5690             : 
    5691             :     /* Metadata Delay to sync with audio delay converted from ms to 5ms (1000/50/4) subframe index */
    5692     2972160 :     ism_md_subframe_update_ext = (int16_t) roundf( ismInput->ism_metadata_delay_ms / ( 1000.f / FRAMES_PER_SEC / MAX_PARAM_SPATIAL_SUBFRAMES ) );
    5693             : 
    5694     2972160 :     copyBufferTo2dArray( ismInput->base.inputBuffer, tmpRendBuffer );
    5695             : 
    5696     2972160 :     if ( ( error = ivas_td_binaural_renderer_ext( &ismInput->tdRendWrapper, ismInput->base.inConfig, NULL, ismInput->base.ctx.pCombinedOrientationData, &ismInput->currentPos, ismInput->hReverb,
    5697     2972160 :                                                   ism_md_subframe_update_ext, *ismInput->base.ctx.pOutSampleRate, outAudio.config.numSamplesPerChannel, tmpRendBuffer ) ) != IVAS_ERR_OK )
    5698             :     {
    5699           0 :         return error;
    5700             :     }
    5701             : 
    5702     2972160 :     accumulate2dArrayToBuffer( tmpRendBuffer, &outAudio );
    5703     2972160 :     pop_wmops();
    5704             : 
    5705     2972160 :     return IVAS_ERR_OK;
    5706             : }
    5707             : 
    5708             : 
    5709    11390946 : static ivas_error renderIsmToMc(
    5710             :     input_ism *ismInput,
    5711             :     const IVAS_REND_AudioBuffer outAudio )
    5712             : {
    5713             :     int8_t position_changed;
    5714             :     pan_vector currentPanGains;
    5715             :     ivas_error error;
    5716             : 
    5717    11390946 :     push_wmops( "renderIsmToMc" );
    5718             : 
    5719    11390946 :     position_changed = !ismInput->firstFrameRendered || checkObjectPositionChanged( &ismInput->currentPos, &ismInput->previousPos );
    5720    11390946 :     if ( *ismInput->base.ctx.pOutConfig == IVAS_AUDIO_CONFIG_STEREO )
    5721             :     {
    5722     1538358 :         if ( ismInput->nonDiegeticPan )
    5723             :         {
    5724       45030 :             ismInput->prev_pan_gains[0] = currentPanGains[0] = ( ismInput->nonDiegeticPanGain + 1.f ) * 0.5f;
    5725       45030 :             ismInput->prev_pan_gains[1] = currentPanGains[1] = 1.f - currentPanGains[0];
    5726             :         }
    5727             :         else
    5728             :         {
    5729     1493328 :             set_zero( currentPanGains, MAX_OUTPUT_CHANNELS );
    5730             : 
    5731     1493328 :             ivas_ism_get_stereo_gains( ismInput->currentPos.azimuth, ismInput->currentPos.elevation, &currentPanGains[0], &currentPanGains[1] );
    5732             : 
    5733     1493328 :             set_zero( ismInput->prev_pan_gains, MAX_OUTPUT_CHANNELS );
    5734             : 
    5735     1493328 :             ivas_ism_get_stereo_gains( ismInput->previousPos.azimuth, ismInput->previousPos.elevation, &ismInput->prev_pan_gains[0], &ismInput->prev_pan_gains[1] );
    5736             :         }
    5737             :     }
    5738             :     else
    5739             :     {
    5740             :         /* compute gains only if position changed */
    5741     9852588 :         if ( position_changed )
    5742             :         {
    5743             :             // TODO tmu review when #215 is resolved
    5744     1739808 :             if ( ( error = getEfapGains( *ismInput->base.ctx.pEfapOutWrapper,
    5745     1739808 :                                          (int16_t) floorf( ismInput->currentPos.azimuth + 0.5f ),
    5746     1739808 :                                          (int16_t) floorf( ismInput->currentPos.elevation + 0.5f ),
    5747             :                                          currentPanGains ) ) != IVAS_ERR_OK )
    5748             :             {
    5749           0 :                 return error;
    5750             :             }
    5751             :         }
    5752             : 
    5753             :         /* set previous gains if this is the first frame */
    5754     9852588 :         if ( !ismInput->firstFrameRendered )
    5755             :         {
    5756             :             // TODO tmu review when #215 is resolved
    5757        5688 :             if ( ( error = getEfapGains( *ismInput->base.ctx.pEfapOutWrapper,
    5758        5688 :                                          (int16_t) floorf( ismInput->previousPos.azimuth + 0.5f ),
    5759        5688 :                                          (int16_t) floorf( ismInput->previousPos.elevation + 0.5f ),
    5760        5688 :                                          ismInput->prev_pan_gains ) ) != IVAS_ERR_OK )
    5761             :             {
    5762           0 :                 return error;
    5763             :             }
    5764             :         }
    5765             :     }
    5766             : 
    5767             :     /* Assume num channels in audio buffer to be 1.
    5768             :      * This should have been validated in IVAS_REND_FeedInputAudio() */
    5769    11390946 :     renderBufferChannelLerp( ismInput->base.inputBuffer, 0,
    5770             :                              position_changed ? currentPanGains : ismInput->prev_pan_gains,
    5771             :                              position_changed ? ismInput->prev_pan_gains : NULL,
    5772             :                              outAudio );
    5773             : 
    5774    11390946 :     if ( position_changed )
    5775             :     {
    5776     2016072 :         mvr2r( currentPanGains, ismInput->prev_pan_gains, MAX_OUTPUT_CHANNELS );
    5777             :     }
    5778             : 
    5779    11390946 :     pop_wmops();
    5780             : 
    5781    11390946 :     return IVAS_ERR_OK;
    5782             : }
    5783             : 
    5784             : 
    5785     4479984 : static ivas_error renderIsmToSba(
    5786             :     input_ism *ismInput,
    5787             :     const AUDIO_CONFIG outConfig,
    5788             :     const IVAS_REND_AudioBuffer outAudio )
    5789             : {
    5790             :     int8_t position_changed;
    5791             :     int16_t ambiOrderOut;
    5792             :     int16_t numOutChannels;
    5793             :     pan_vector currentPanGains;
    5794             :     ivas_error error;
    5795     4479984 :     error = IVAS_ERR_OK;
    5796             : 
    5797     4479984 :     push_wmops( "renderIsmToSba" );
    5798             : 
    5799     4479984 :     if ( ( error = getAudioConfigNumChannels( outConfig, &numOutChannels ) ) != IVAS_ERR_OK )
    5800             :     {
    5801           0 :         return error;
    5802             :     }
    5803             : 
    5804     4479984 :     if ( ( error = getAmbisonicsOrder( outConfig, &ambiOrderOut ) ) != IVAS_ERR_OK )
    5805             :     {
    5806           0 :         return error;
    5807             :     }
    5808             : 
    5809     4479984 :     position_changed = !ismInput->firstFrameRendered || checkObjectPositionChanged( &ismInput->currentPos, &ismInput->previousPos );
    5810             : 
    5811             :     /* set previous gains if this is the first frame */
    5812     4479984 :     if ( !ismInput->firstFrameRendered )
    5813             :     {
    5814             :         // TODO tmu review when #215 is resolved
    5815        2304 :         ivas_dirac_dec_get_response( (int16_t) floorf( ismInput->previousPos.azimuth + 0.5f ),
    5816        2304 :                                      (int16_t) floorf( ismInput->previousPos.elevation + 0.5f ),
    5817        2304 :                                      ismInput->prev_pan_gains,
    5818             :                                      ambiOrderOut );
    5819             :     }
    5820             : 
    5821             :     /* compute gains only if position changed */
    5822     4479984 :     if ( position_changed )
    5823             :     {
    5824             :         // TODO tmu review when #215 is resolved
    5825      695952 :         ivas_dirac_dec_get_response( (int16_t) floorf( ismInput->currentPos.azimuth + 0.5f ),
    5826      695952 :                                      (int16_t) floorf( ismInput->currentPos.elevation + 0.5f ),
    5827             :                                      currentPanGains,
    5828             :                                      ambiOrderOut );
    5829             :     }
    5830             : 
    5831             :     /* Assume num channels in audio buffer to be 1.
    5832             :      * This should have been validated in IVAS_REND_FeedInputAudio() */
    5833     4479984 :     renderBufferChannelLerp( ismInput->base.inputBuffer, 0,
    5834             :                              position_changed ? currentPanGains : ismInput->prev_pan_gains,
    5835             :                              position_changed ? ismInput->prev_pan_gains : NULL,
    5836             :                              outAudio );
    5837             : 
    5838     4479984 :     if ( position_changed )
    5839             :     {
    5840      695952 :         mvr2r( currentPanGains, ismInput->prev_pan_gains, MAX_OUTPUT_CHANNELS );
    5841             :     }
    5842     4479984 :     pop_wmops();
    5843             : 
    5844     4479984 :     return error;
    5845             : }
    5846             : 
    5847             : 
    5848           0 : static ivas_error renderIsmToSplitBinaural(
    5849             :     input_ism *ismInput,
    5850             :     const IVAS_REND_AudioBuffer outAudio )
    5851             : {
    5852             :     ivas_error error;
    5853             :     float tmpProcessing[MAX_NUM_OBJECTS][L_FRAME48k];
    5854             :     int16_t pos_idx;
    5855             :     const MULTI_BIN_REND_POSE_DATA *pMultiBinPoseData;
    5856             :     const SPLIT_REND_WRAPPER *pSplitRendWrapper;
    5857             :     IVAS_QUATERNION originalHeadRot[MAX_PARAM_SPATIAL_SUBFRAMES];
    5858             :     int16_t i, ch, slot_idx, num_bands;
    5859             :     float tmpBinaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][L_FRAME48k];
    5860             :     float tmpBinaural_CldfbRe[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    5861             :     float tmpBinaural_CldfbIm[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    5862           0 :     int16_t output_frame = ismInput->base.inputBuffer.config.numSamplesPerChannel;
    5863             :     COMBINED_ORIENTATION_HANDLE pCombinedOrientationData;
    5864             :     int16_t ism_md_subframe_update_ext;
    5865             : 
    5866           0 :     push_wmops( "renderIsmToSplitBinaural" );
    5867             : 
    5868           0 :     pSplitRendWrapper = ismInput->base.ctx.pSplitRendWrapper;
    5869           0 :     pMultiBinPoseData = &pSplitRendWrapper->multiBinPoseData;
    5870             : 
    5871             :     /* Metadata Delay to sync with audio delay converted from ms to 5ms (1000/50/4) subframe index */
    5872           0 :     ism_md_subframe_update_ext = (int16_t) roundf( ismInput->ism_metadata_delay_ms / ( 1000.f / FRAMES_PER_SEC / MAX_PARAM_SPATIAL_SUBFRAMES ) );
    5873             : 
    5874           0 :     pCombinedOrientationData = *ismInput->base.ctx.pCombinedOrientationData;
    5875             : 
    5876           0 :     if ( pMultiBinPoseData->poseCorrectionMode == ISAR_SPLIT_REND_POSE_CORRECTION_MODE_CLDFB )
    5877             :     {
    5878           0 :         for ( i = 1; i < pCombinedOrientationData->num_subframes; ++i )
    5879             :         {
    5880           0 :             pCombinedOrientationData->Quaternions[i] = pCombinedOrientationData->Quaternions[0];
    5881             :         }
    5882             :     }
    5883             : 
    5884             :     /* Save current head positions */
    5885           0 :     for ( i = 0; i < pCombinedOrientationData->num_subframes; ++i )
    5886             :     {
    5887           0 :         originalHeadRot[i] = pCombinedOrientationData->Quaternions[i];
    5888             :     }
    5889             : 
    5890             :     /* Copy input audio to a processing buffer. */
    5891           0 :     copyBufferTo2dArray( ismInput->base.inputBuffer, tmpProcessing );
    5892             : 
    5893           0 :     for ( pos_idx = 0; pos_idx < pMultiBinPoseData->num_poses; pos_idx++ )
    5894             :     {
    5895             :         /* Update head positions */
    5896           0 :         if ( pos_idx != 0 )
    5897             :         {
    5898           0 :             for ( i = 0; i < pCombinedOrientationData->num_subframes; ++i )
    5899             :             {
    5900           0 :                 if ( originalHeadRot[i].w == -3.0f )
    5901             :                 {
    5902           0 :                     pCombinedOrientationData->Quaternions[i].w = -3.0f;
    5903           0 :                     pCombinedOrientationData->Quaternions[i].x = originalHeadRot[i].x + pMultiBinPoseData->relative_head_poses[pos_idx][0];
    5904           0 :                     pCombinedOrientationData->Quaternions[i].y = originalHeadRot[i].y + pMultiBinPoseData->relative_head_poses[pos_idx][1];
    5905           0 :                     pCombinedOrientationData->Quaternions[i].z = originalHeadRot[i].z + pMultiBinPoseData->relative_head_poses[pos_idx][2];
    5906             :                 }
    5907             :                 else
    5908             :                 {
    5909           0 :                     pCombinedOrientationData->Quaternions[i].w = -3.0f;
    5910           0 :                     Quat2EulerDegree( originalHeadRot[i],
    5911           0 :                                       &pCombinedOrientationData->Quaternions[i].z,
    5912           0 :                                       &pCombinedOrientationData->Quaternions[i].y,
    5913           0 :                                       &pCombinedOrientationData->Quaternions[i].x );
    5914           0 :                     pCombinedOrientationData->Quaternions[i].x += pMultiBinPoseData->relative_head_poses[pos_idx][0];
    5915           0 :                     pCombinedOrientationData->Quaternions[i].y += pMultiBinPoseData->relative_head_poses[pos_idx][1];
    5916           0 :                     pCombinedOrientationData->Quaternions[i].z += pMultiBinPoseData->relative_head_poses[pos_idx][2];
    5917             :                 }
    5918             :             }
    5919             :         }
    5920             : 
    5921             :         /* Render */
    5922           0 :         if ( ( error = ivas_td_binaural_renderer_ext( ( pos_idx == 0 ) ? &ismInput->tdRendWrapper : &ismInput->splitTdRendWrappers[pos_idx - 1], ismInput->base.inConfig, NULL, ismInput->base.ctx.pCombinedOrientationData, &ismInput->currentPos,
    5923           0 :                                                       NULL, ism_md_subframe_update_ext, *ismInput->base.ctx.pOutSampleRate, output_frame, tmpProcessing ) ) != IVAS_ERR_OK )
    5924             :         {
    5925           0 :             return error;
    5926             :         }
    5927             : 
    5928           0 :         if ( outAudio.config.is_cldfb )
    5929             :         {
    5930             :             /* Perform CLDFB analysis on rendered audio, since the output buffer is CLDFB domain */
    5931           0 :             num_bands = (int16_t) ( ( BINAURAL_MAXBANDS * *ismInput->base.ctx.pOutSampleRate ) / 48000 );
    5932           0 :             for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    5933             :             {
    5934           0 :                 for ( slot_idx = 0; slot_idx < IVAS_CLDFB_NO_COL_MAX; slot_idx++ )
    5935             :                 {
    5936           0 :                     cldfbAnalysis_ts( &tmpProcessing[ch][num_bands * slot_idx],
    5937           0 :                                       &tmpBinaural_CldfbRe[BINAURAL_CHANNELS * pos_idx + ch][slot_idx][0],
    5938           0 :                                       &tmpBinaural_CldfbIm[BINAURAL_CHANNELS * pos_idx + ch][slot_idx][0],
    5939             :                                       num_bands,
    5940           0 :                                       ismInput->base.ctx.pSplitRendWrapper->hCldfbHandles->cldfbAna[pos_idx + ch] );
    5941             :                 }
    5942             :             }
    5943             :         }
    5944             :         else
    5945             :         {
    5946             :             /* Copy rendered audio to tmp storage buffer. Copying directly to output would
    5947             :              * overwrite original audio, which is still needed for rendering next head pose. */
    5948           0 :             mvr2r( tmpProcessing[0], tmpBinaural[BINAURAL_CHANNELS * pos_idx], output_frame );
    5949           0 :             mvr2r( tmpProcessing[1], tmpBinaural[BINAURAL_CHANNELS * pos_idx + 1], output_frame );
    5950             :         }
    5951             : 
    5952             :         /* Overwrite processing buffer with original input audio again */
    5953           0 :         copyBufferTo2dArray( ismInput->base.inputBuffer, tmpProcessing );
    5954             :     }
    5955             : 
    5956             :     /* Restore original head rotation */
    5957           0 :     for ( i = 0; i < pCombinedOrientationData->num_subframes; ++i )
    5958             :     {
    5959           0 :         pCombinedOrientationData->Quaternions[i] = originalHeadRot[i];
    5960             :     }
    5961             : 
    5962           0 :     if ( outAudio.config.is_cldfb )
    5963             :     {
    5964           0 :         accumulateCLDFBArrayToBuffer( tmpBinaural_CldfbRe, tmpBinaural_CldfbIm, &outAudio );
    5965             :     }
    5966             :     else
    5967             :     {
    5968           0 :         accumulate2dArrayToBuffer( tmpBinaural, &outAudio );
    5969             :     }
    5970           0 :     pop_wmops();
    5971             : 
    5972             :     /* Encoding to split rendering bitstream done at a higher level */
    5973           0 :     return IVAS_ERR_OK;
    5974             : }
    5975             : 
    5976             : 
    5977        7836 : static void renderIsmToMasa(
    5978             :     input_ism *ismInput,
    5979             :     IVAS_REND_AudioBuffer outAudio )
    5980             : {
    5981             :     float tmpRendBuffer[MAX_NUM_OBJECTS][L_FRAME48k];
    5982             : 
    5983        7836 :     push_wmops( "renderIsmToMasa" );
    5984             : 
    5985        7836 :     copyBufferTo2dArray( ismInput->base.inputBuffer, tmpRendBuffer );
    5986             : 
    5987        7836 :     ivas_omasa_ana( ismInput->hOMasa, tmpRendBuffer, ismInput->base.inputBuffer.config.numSamplesPerChannel, outAudio.config.numChannels, ismInput->base.inputBuffer.config.numChannels );
    5988             : 
    5989        7836 :     accumulate2dArrayToBuffer( tmpRendBuffer, &outAudio );
    5990             : 
    5991        7836 :     pop_wmops();
    5992             : 
    5993        7836 :     return;
    5994             : }
    5995             : 
    5996             : 
    5997    24795246 : static ivas_error renderInputIsm(
    5998             :     input_ism *ismInput,
    5999             :     const AUDIO_CONFIG outConfig,
    6000             :     const IVAS_REND_AudioBuffer outAudio )
    6001             : {
    6002             :     ivas_error error;
    6003             :     IVAS_REND_AudioBuffer inAudio;
    6004             :     int16_t cldfb2tdSampleFact;
    6005             : 
    6006    24795246 :     error = IVAS_ERR_OK;
    6007    24795246 :     inAudio = ismInput->base.inputBuffer;
    6008             : 
    6009    24795246 :     cldfb2tdSampleFact = outAudio.config.is_cldfb ? 2 : 1;
    6010    24795246 :     if ( ismInput->base.numNewSamplesPerChannel * cldfb2tdSampleFact != outAudio.config.numSamplesPerChannel )
    6011             :     {
    6012           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_BUFFER_SIZE, "Mismatch between the number of input samples vs number of requested output samples - currently not allowed" );
    6013             :     }
    6014    24795246 :     ismInput->base.numNewSamplesPerChannel = 0;
    6015             : 
    6016             :     /* Apply input gain to new audio */
    6017    24795246 :     v_multc( inAudio.data, ismInput->base.gain, inAudio.data, inAudio.config.numSamplesPerChannel * inAudio.config.numChannels );
    6018             : 
    6019             :     /* set combined orientation subframe info to start info */
    6020    24795246 :     ivas_combined_orientation_set_to_start_index( *ismInput->base.ctx.pCombinedOrientationData );
    6021             : 
    6022    24795246 :     switch ( getAudioConfigType( outConfig ) )
    6023             :     {
    6024    11390946 :         case IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED:
    6025    11390946 :             error = renderIsmToMc( ismInput, outAudio );
    6026    11390946 :             break;
    6027     4479984 :         case IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS:
    6028     4479984 :             error = renderIsmToSba( ismInput, outConfig, outAudio );
    6029     4479984 :             break;
    6030     8916480 :         case IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL:
    6031             :             switch ( outConfig )
    6032             :             {
    6033     2972160 :                 case IVAS_AUDIO_CONFIG_BINAURAL:
    6034     2972160 :                     error = renderIsmToBinaural( ismInput, outAudio );
    6035     2972160 :                     break;
    6036     2972160 :                 case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR:
    6037     2972160 :                     error = renderIsmToBinauralRoom( ismInput, outAudio );
    6038     2972160 :                     break;
    6039           0 :                 case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED:
    6040             :                 case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM:
    6041           0 :                     error = renderIsmToSplitBinaural( ismInput, outAudio );
    6042           0 :                     break;
    6043     2972160 :                 case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB:
    6044     2972160 :                     error = renderIsmToBinauralReverb( ismInput, outAudio );
    6045     2972160 :                     break;
    6046           0 :                 default:
    6047           0 :                     return IVAS_ERR_INVALID_OUTPUT_FORMAT;
    6048             :             }
    6049     8916480 :             break;
    6050        7836 :         case IVAS_REND_AUDIO_CONFIG_TYPE_MASA:
    6051        7836 :             renderIsmToMasa( ismInput, outAudio );
    6052        7836 :             break;
    6053           0 :         default:
    6054           0 :             return IVAS_ERR_INVALID_OUTPUT_FORMAT;
    6055             :     }
    6056             : 
    6057             :     /* Check error here to keep switch statement more compact */
    6058    24795246 :     if ( error != IVAS_ERR_OK )
    6059             :     {
    6060           0 :         return error;
    6061             :     }
    6062             : 
    6063    24795246 :     ismInput->firstFrameRendered = TRUE;
    6064             : 
    6065    24795246 :     return error;
    6066             : }
    6067             : 
    6068             : 
    6069    13740500 : static ivas_error renderActiveInputsIsm(
    6070             :     IVAS_REND_HANDLE hIvasRend,
    6071             :     IVAS_REND_AudioBuffer outAudio )
    6072             : {
    6073             :     int16_t i;
    6074             :     input_ism *pCurrentInput;
    6075             :     ivas_error error;
    6076             : 
    6077    68702500 :     for ( i = 0, pCurrentInput = hIvasRend->inputsIsm; i < RENDERER_MAX_ISM_INPUTS; ++i, ++pCurrentInput )
    6078             :     {
    6079    54962000 :         if ( pCurrentInput->base.inConfig == IVAS_AUDIO_CONFIG_INVALID )
    6080             :         {
    6081             :             /* Skip inactive inputs */
    6082    30166754 :             continue;
    6083             :         }
    6084             : 
    6085    24795246 :         if ( ( error = renderInputIsm( pCurrentInput, hIvasRend->outputConfig, outAudio ) ) != IVAS_ERR_OK )
    6086             :         {
    6087           0 :             return error;
    6088             :         }
    6089             :     }
    6090             : 
    6091    13740500 :     return IVAS_ERR_OK;
    6092             : }
    6093             : 
    6094             : 
    6095      522648 : static ivas_error renderLfeToBinaural(
    6096             :     const input_mc *mcInput,
    6097             :     const AUDIO_CONFIG outConfig,
    6098             :     IVAS_REND_AudioBuffer outAudio )
    6099             : {
    6100             :     int16_t lfe_idx;
    6101             :     int16_t pose_idx, num_poses;
    6102             :     float gain;
    6103             :     int16_t ear_idx;
    6104             :     float tmpLfeBuffer[L_FRAME_MAX];
    6105             :     int16_t frame_size, num_cpy_smpl_cur_frame, num_cpy_smpl_prev_frame;
    6106             :     const float *lfeInput;
    6107             :     float *writePtr;
    6108             : 
    6109      522648 :     assert( ( getAudioConfigType( outConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL ) && "Must be binaural output" );
    6110             : 
    6111      522648 :     push_wmops( "renderLfeToBinaural" );
    6112             : 
    6113      522648 :     gain = GAIN_LFE;
    6114             : 
    6115      522648 :     if ( mcInput->base.inConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM )
    6116             :     {
    6117      288360 :         lfe_idx = LFE_CHANNEL;
    6118             :     }
    6119      234288 :     else if ( mcInput->customLsInput.num_lfe > 0 )
    6120             :     {
    6121           0 :         lfe_idx = mcInput->customLsInput.lfe_idx[0];
    6122             :     }
    6123             :     else
    6124             :     {
    6125             :         /* no LFE to render */
    6126      234288 :         return IVAS_ERR_OK;
    6127             :     }
    6128             : 
    6129             :     /* --- Prepare LFE signal to be added to binaural output --- */
    6130      288360 :     lfeInput = getSmplPtr( mcInput->base.inputBuffer, lfe_idx, 0 );
    6131      288360 :     frame_size = mcInput->base.inputBuffer.config.numSamplesPerChannel;
    6132      288360 :     num_cpy_smpl_prev_frame = mcInput->binauralDelaySmp;
    6133      288360 :     num_cpy_smpl_cur_frame = frame_size - num_cpy_smpl_prev_frame;
    6134             : 
    6135      288360 :     assert( mcInput->binauralDelaySmp <= MAX_BIN_DELAY_SAMPLES );
    6136             : 
    6137             :     /* Get delayed LFE signal from previous frame, apply gain and save in tmp buffer */
    6138      288360 :     v_multc( mcInput->lfeDelayBuffer, gain, tmpLfeBuffer, num_cpy_smpl_prev_frame );
    6139             : 
    6140             :     /* Continue filling tmp buffer, now with LFE signal from current frame */
    6141      288360 :     v_multc( lfeInput, gain, tmpLfeBuffer + num_cpy_smpl_prev_frame, num_cpy_smpl_cur_frame );
    6142             : 
    6143             :     /* Save remaining LFE samples of current frame for next frame */
    6144      288360 :     mvr2r( lfeInput + num_cpy_smpl_cur_frame, mcInput->lfeDelayBuffer, num_cpy_smpl_prev_frame );
    6145             : 
    6146             :     /* Copy LFE to left and right binaural channels for all poses */
    6147      288360 :     if ( mcInput->base.ctx.pSplitRendWrapper != NULL )
    6148             :     {
    6149           0 :         num_poses = mcInput->base.ctx.pSplitRendWrapper->multiBinPoseData.num_poses;
    6150             :     }
    6151             :     else
    6152             :     {
    6153      288360 :         num_poses = 1;
    6154             :     }
    6155             : 
    6156      576720 :     for ( pose_idx = 0; pose_idx < num_poses; ++pose_idx )
    6157             :     {
    6158      865080 :         for ( ear_idx = 0; ear_idx < BINAURAL_CHANNELS; ++ear_idx )
    6159             :         {
    6160      576720 :             writePtr = getSmplPtr( outAudio, pose_idx * BINAURAL_CHANNELS + ear_idx, 0 );
    6161      576720 :             v_add( writePtr, tmpLfeBuffer, writePtr, frame_size );
    6162             :         }
    6163             :     }
    6164             : 
    6165      288360 :     pop_wmops();
    6166             : 
    6167      288360 :     return IVAS_ERR_OK;
    6168             : }
    6169             : 
    6170             : 
    6171      174216 : static ivas_error renderMcToBinaural(
    6172             :     input_mc *mcInput,
    6173             :     const AUDIO_CONFIG outConfig,
    6174             :     IVAS_REND_AudioBuffer outAudio )
    6175             : {
    6176             :     float tmpRendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    6177             :     AUDIO_CONFIG inConfig;
    6178             :     ivas_error error;
    6179             :     IVAS_REND_AudioBuffer tmpRotBuffer;
    6180             :     const COMBINED_ORIENTATION_HANDLE *hCombinedOrientationData;
    6181             :     int8_t combinedOrientationEnabled;
    6182             :     int16_t subframe_idx;
    6183             :     float *p_tmpRendBuffer[MAX_OUTPUT_CHANNELS];
    6184             :     int16_t i;
    6185             : 
    6186     2961672 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    6187             :     {
    6188     2787456 :         p_tmpRendBuffer[i] = tmpRendBuffer[i];
    6189             :     }
    6190             : 
    6191      174216 :     push_wmops( "renderMcToBinaural" );
    6192      174216 :     inConfig = mcInput->base.inConfig;
    6193             : 
    6194      174216 :     hCombinedOrientationData = mcInput->base.ctx.pCombinedOrientationData;
    6195      174216 :     combinedOrientationEnabled = 0;
    6196      174216 :     if ( *hCombinedOrientationData != NULL )
    6197             :     {
    6198       87108 :         for ( subframe_idx = 0; subframe_idx < ( *hCombinedOrientationData )->num_subframes; subframe_idx++ )
    6199             :         {
    6200       87108 :             if ( ( *hCombinedOrientationData )->enableCombinedOrientation[subframe_idx] != 0 )
    6201             :             {
    6202       87108 :                 combinedOrientationEnabled = 1;
    6203       87108 :                 break;
    6204             :             }
    6205             :         }
    6206             :     }
    6207             : 
    6208      174216 :     if ( ( inConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM ) || ( combinedOrientationEnabled && ( inConfig == IVAS_AUDIO_CONFIG_5_1 || inConfig == IVAS_AUDIO_CONFIG_7_1 ) ) )
    6209             :     {
    6210      112620 :         copyBufferTo2dArray( mcInput->base.inputBuffer, tmpRendBuffer );
    6211             : 
    6212      112620 :         if ( ( error = ivas_td_binaural_renderer_ext( &mcInput->tdRendWrapper, mcInput->base.inConfig, &mcInput->customLsInput, mcInput->base.ctx.pCombinedOrientationData, NULL, mcInput->hReverb,
    6213      112620 :                                                       0, *mcInput->base.ctx.pOutSampleRate, mcInput->base.inputBuffer.config.numSamplesPerChannel, tmpRendBuffer ) ) != IVAS_ERR_OK )
    6214             :         {
    6215           0 :             return error;
    6216             :         }
    6217             :     }
    6218             :     else
    6219             :     {
    6220             :         /* apply rotation */
    6221       61596 :         if ( combinedOrientationEnabled )
    6222             :         {
    6223       13536 :             tmpRotBuffer = mcInput->base.inputBuffer;
    6224       13536 :             tmpRotBuffer.data = malloc( tmpRotBuffer.config.numSamplesPerChannel * tmpRotBuffer.config.numChannels * sizeof( float ) );
    6225       13536 :             set_zero( tmpRotBuffer.data, tmpRotBuffer.config.numSamplesPerChannel * tmpRotBuffer.config.numChannels );
    6226             : 
    6227       13536 :             if ( ( error = rotateFrameMc( mcInput->base.inputBuffer, mcInput->base.inConfig, &mcInput->customLsInput, mcInput->base.ctx.pHeadRotData, mcInput->base.ctx.pCombinedOrientationData, mcInput->rot_gains_prev[0], mcInput->efapInWrapper.hEfap, tmpRotBuffer ) ) != IVAS_ERR_OK )
    6228             :             {
    6229           0 :                 return error;
    6230             :             }
    6231             : 
    6232       13536 :             copyBufferTo2dArray( tmpRotBuffer, tmpRendBuffer );
    6233       13536 :             free( tmpRotBuffer.data );
    6234             :         }
    6235             :         else
    6236             :         {
    6237       48060 :             copyBufferTo2dArray( mcInput->base.inputBuffer, tmpRendBuffer );
    6238             :         }
    6239             : 
    6240             :         /* call CREND */
    6241       61596 :         if ( ( error = ivas_rend_crendProcessSubframe( mcInput->crendWrapper, mcInput->base.inConfig, outConfig, NULL, NULL,
    6242       61596 :                                                        NULL, NULL, NULL, p_tmpRendBuffer, p_tmpRendBuffer, mcInput->base.inputBuffer.config.numSamplesPerChannel, *mcInput->base.ctx.pOutSampleRate, 0 ) ) != IVAS_ERR_OK )
    6243             :         {
    6244           0 :             return error;
    6245             :         }
    6246             :     }
    6247             : 
    6248      174216 :     accumulate2dArrayToBuffer( tmpRendBuffer, &outAudio );
    6249             : 
    6250      174216 :     if ( ( error = renderLfeToBinaural( mcInput, outConfig, outAudio ) ) != IVAS_ERR_OK )
    6251             :     {
    6252           0 :         return error;
    6253             :     }
    6254             : 
    6255      174216 :     pop_wmops();
    6256      174216 :     return IVAS_ERR_OK;
    6257             : }
    6258             : 
    6259             : 
    6260      192240 : static ivas_error renderMcToBinauralRoom(
    6261             :     input_mc *mcInput,
    6262             :     const AUDIO_CONFIG outConfig,
    6263             :     IVAS_REND_AudioBuffer outAudio )
    6264             : {
    6265             :     float tmpRendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    6266             :     AUDIO_CONFIG inConfig;
    6267             :     ivas_error error;
    6268             :     IVAS_REND_AudioBuffer tmpRotBuffer;
    6269             :     float *p_tmpRendBuffer[MAX_OUTPUT_CHANNELS];
    6270             :     int16_t i;
    6271             :     const COMBINED_ORIENTATION_HANDLE *hCombinedOrientationData;
    6272             :     int8_t combinedOrientationEnabled;
    6273             :     int16_t subframe_idx;
    6274             : 
    6275     3268080 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    6276             :     {
    6277     3075840 :         p_tmpRendBuffer[i] = tmpRendBuffer[i];
    6278             :     }
    6279             : 
    6280      192240 :     push_wmops( "renderMcToBinauralRoom" );
    6281      192240 :     inConfig = mcInput->base.inConfig;
    6282             : 
    6283      192240 :     hCombinedOrientationData = mcInput->base.ctx.pCombinedOrientationData;
    6284      192240 :     combinedOrientationEnabled = 0;
    6285      192240 :     if ( *hCombinedOrientationData != NULL )
    6286             :     {
    6287       96120 :         for ( subframe_idx = 0; subframe_idx < ( *hCombinedOrientationData )->num_subframes; subframe_idx++ )
    6288             :         {
    6289       96120 :             if ( ( *hCombinedOrientationData )->enableCombinedOrientation[subframe_idx] != 0 )
    6290             :             {
    6291       96120 :                 combinedOrientationEnabled = 1;
    6292       96120 :                 break;
    6293             :             }
    6294             :         }
    6295             :     }
    6296             : 
    6297      192240 :     if ( ( mcInput->hReverb != NULL && outConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) && ( ( inConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM ) || ( combinedOrientationEnabled && ( inConfig == IVAS_AUDIO_CONFIG_5_1 || inConfig == IVAS_AUDIO_CONFIG_7_1 ) ) ) )
    6298             :     {
    6299       34524 :         copyBufferTo2dArray( mcInput->base.inputBuffer, tmpRendBuffer );
    6300             : 
    6301       34524 :         if ( ( error = ivas_td_binaural_renderer_ext( &mcInput->tdRendWrapper, mcInput->base.inConfig, &mcInput->customLsInput, mcInput->base.ctx.pCombinedOrientationData, NULL, mcInput->hReverb,
    6302       34524 :                                                       0, *mcInput->base.ctx.pOutSampleRate, mcInput->base.inputBuffer.config.numSamplesPerChannel, tmpRendBuffer ) ) != IVAS_ERR_OK )
    6303             :         {
    6304           0 :             return error;
    6305             :         }
    6306             :     }
    6307             :     else
    6308             :     {
    6309             :         /* apply rotation */
    6310      157716 :         if ( combinedOrientationEnabled )
    6311             :         {
    6312       61596 :             tmpRotBuffer = mcInput->base.inputBuffer;
    6313       61596 :             tmpRotBuffer.data = malloc( tmpRotBuffer.config.numSamplesPerChannel * tmpRotBuffer.config.numChannels * sizeof( float ) );
    6314       61596 :             set_zero( tmpRotBuffer.data, tmpRotBuffer.config.numSamplesPerChannel * tmpRotBuffer.config.numChannels );
    6315             : 
    6316       61596 :             if ( ( error = rotateFrameMc( mcInput->base.inputBuffer, mcInput->base.inConfig, &mcInput->customLsInput, mcInput->base.ctx.pHeadRotData, mcInput->base.ctx.pCombinedOrientationData, mcInput->rot_gains_prev[0], mcInput->efapInWrapper.hEfap, tmpRotBuffer ) ) != IVAS_ERR_OK )
    6317             :             {
    6318           0 :                 return error;
    6319             :             }
    6320             : 
    6321       61596 :             copyBufferTo2dArray( tmpRotBuffer, tmpRendBuffer );
    6322       61596 :             free( tmpRotBuffer.data );
    6323             :         }
    6324             :         else
    6325             :         {
    6326       96120 :             copyBufferTo2dArray( mcInput->base.inputBuffer, tmpRendBuffer );
    6327             :         }
    6328             : 
    6329             :         /* call CREND */
    6330      157716 :         if ( ( error = ivas_rend_crendProcessSubframe( mcInput->crendWrapper, mcInput->base.inConfig, outConfig, NULL, NULL,
    6331      157716 :                                                        NULL, NULL, NULL, p_tmpRendBuffer, p_tmpRendBuffer, mcInput->base.inputBuffer.config.numSamplesPerChannel, *mcInput->base.ctx.pOutSampleRate, 0 ) ) != IVAS_ERR_OK )
    6332             :         {
    6333           0 :             return error;
    6334             :         }
    6335             :     }
    6336             : 
    6337      192240 :     accumulate2dArrayToBuffer( tmpRendBuffer, &outAudio );
    6338             : 
    6339      192240 :     if ( ( error = renderLfeToBinaural( mcInput, outConfig, outAudio ) ) != IVAS_ERR_OK )
    6340             :     {
    6341           0 :         return error;
    6342             :     }
    6343             : 
    6344      192240 :     pop_wmops();
    6345      192240 :     return IVAS_ERR_OK;
    6346             : }
    6347             : 
    6348             : 
    6349      156192 : static ivas_error renderMcCustomLsToBinauralRoom(
    6350             :     input_mc *mcInput,
    6351             :     const AUDIO_CONFIG outConfig,
    6352             :     IVAS_REND_AudioBuffer outAudio )
    6353             : {
    6354             :     int16_t i;
    6355             :     int16_t tmp;
    6356             :     float tmpCrendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    6357             :     ivas_error error;
    6358             :     IVAS_REND_AudioBuffer tmpRotBuffer;
    6359             :     IVAS_REND_AudioBuffer tmpMcBuffer;
    6360             :     IVAS_REND_AudioBuffer *tmpBufPtr;
    6361             :     float *p_tmpCrendBuffer[MAX_OUTPUT_CHANNELS];
    6362             :     const COMBINED_ORIENTATION_HANDLE *hCombinedOrientationData;
    6363             :     int8_t combinedOrientationEnabled;
    6364             :     int16_t subframe_idx;
    6365             : 
    6366      156192 :     push_wmops( "renderMcCustomLsToBinauralRoom" );
    6367      156192 :     tmpRotBuffer = outAudio; /* avoid compilation warning */
    6368             : 
    6369     2655264 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    6370             :     {
    6371     2499072 :         p_tmpCrendBuffer[i] = tmpCrendBuffer[i];
    6372             :     }
    6373             : 
    6374      156192 :     hCombinedOrientationData = mcInput->base.ctx.pCombinedOrientationData;
    6375      156192 :     combinedOrientationEnabled = 0;
    6376      156192 :     if ( *hCombinedOrientationData != NULL )
    6377             :     {
    6378       78096 :         for ( subframe_idx = 0; subframe_idx < ( *hCombinedOrientationData )->num_subframes; subframe_idx++ )
    6379             :         {
    6380       78096 :             if ( ( *hCombinedOrientationData )->enableCombinedOrientation[subframe_idx] != 0 )
    6381             :             {
    6382       78096 :                 combinedOrientationEnabled = 1;
    6383       78096 :                 break;
    6384             :             }
    6385             :         }
    6386             :     }
    6387             : 
    6388             :     /* apply rotation */
    6389      156192 :     if ( combinedOrientationEnabled )
    6390             :     {
    6391       78096 :         tmpRotBuffer = mcInput->base.inputBuffer;
    6392       78096 :         tmpRotBuffer.data = malloc( tmpRotBuffer.config.numSamplesPerChannel * tmpRotBuffer.config.numChannels * sizeof( float ) );
    6393       78096 :         set_zero( tmpRotBuffer.data, tmpRotBuffer.config.numSamplesPerChannel * tmpRotBuffer.config.numChannels );
    6394             : 
    6395       78096 :         if ( ( error = rotateFrameMc( mcInput->base.inputBuffer, mcInput->base.inConfig, &mcInput->customLsInput, mcInput->base.ctx.pHeadRotData, mcInput->base.ctx.pCombinedOrientationData, mcInput->rot_gains_prev[0], mcInput->efapInWrapper.hEfap, tmpRotBuffer ) ) != IVAS_ERR_OK )
    6396             :         {
    6397           0 :             return error;
    6398             :         }
    6399             :     }
    6400             : 
    6401             :     /* intermediate conversion to 7_1_4 */
    6402      156192 :     tmpMcBuffer = mcInput->base.inputBuffer;
    6403             : 
    6404      156192 :     if ( ( error = getAudioConfigNumChannels( IVAS_AUDIO_CONFIG_7_1_4, &tmp ) ) != IVAS_ERR_OK )
    6405             :     {
    6406           0 :         return error;
    6407             :     }
    6408             : 
    6409      156192 :     tmpMcBuffer.config.numChannels = tmp;
    6410      156192 :     tmpMcBuffer.data = malloc( tmpMcBuffer.config.numSamplesPerChannel * tmpMcBuffer.config.numChannels * sizeof( float ) );
    6411      156192 :     set_zero( tmpMcBuffer.data, tmpMcBuffer.config.numSamplesPerChannel * tmpMcBuffer.config.numChannels );
    6412             : 
    6413      156192 :     tmpBufPtr = ( combinedOrientationEnabled ) ? &tmpRotBuffer : &mcInput->base.inputBuffer;
    6414     2438688 :     for ( i = 0; i < mcInput->base.inputBuffer.config.numChannels; i++ )
    6415             :     {
    6416     2282496 :         renderBufferChannel( *tmpBufPtr, i, mcInput->panGains[i], tmpMcBuffer );
    6417             :     }
    6418      156192 :     copyBufferTo2dArray( tmpMcBuffer, tmpCrendBuffer );
    6419             : 
    6420             :     /* call CREND */
    6421      156192 :     if ( ( error = ivas_rend_crendProcessSubframe( mcInput->crendWrapper, IVAS_AUDIO_CONFIG_7_1_4, outConfig, NULL, NULL,
    6422      156192 :                                                    NULL, NULL, NULL, p_tmpCrendBuffer, p_tmpCrendBuffer, mcInput->base.inputBuffer.config.numSamplesPerChannel, *mcInput->base.ctx.pOutSampleRate, 0 ) ) != IVAS_ERR_OK )
    6423             :     {
    6424           0 :         return error;
    6425             :     }
    6426             : 
    6427      156192 :     accumulate2dArrayToBuffer( tmpCrendBuffer, &outAudio );
    6428             : 
    6429      156192 :     if ( ( error = renderLfeToBinaural( mcInput, outConfig, outAudio ) ) != IVAS_ERR_OK )
    6430             :     {
    6431           0 :         return error;
    6432             :     }
    6433             : 
    6434      156192 :     if ( combinedOrientationEnabled )
    6435             :     {
    6436       78096 :         free( tmpRotBuffer.data );
    6437             :     }
    6438      156192 :     free( tmpMcBuffer.data );
    6439             : 
    6440      156192 :     pop_wmops();
    6441      156192 :     return IVAS_ERR_OK;
    6442             : }
    6443             : 
    6444             : 
    6445     1192580 : static void renderMcToMc(
    6446             :     const input_mc *mcInput,
    6447             :     IVAS_REND_AudioBuffer outAudio )
    6448             : {
    6449             :     int16_t i;
    6450             :     IVAS_REND_AudioBuffer inAudio;
    6451             : 
    6452     1192580 :     push_wmops( "renderMcToMc" );
    6453     1192580 :     inAudio = mcInput->base.inputBuffer;
    6454             : 
    6455     9098810 :     for ( i = 0; i < inAudio.config.numChannels; ++i )
    6456             :     {
    6457     7906230 :         renderBufferChannel( inAudio, i, mcInput->panGains[i], outAudio );
    6458             :     }
    6459             : 
    6460     1192580 :     pop_wmops();
    6461     1192580 :     return;
    6462             : }
    6463             : 
    6464             : 
    6465      459366 : static void renderMcToSba(
    6466             :     const input_mc *mcInput,
    6467             :     IVAS_REND_AudioBuffer outAudio )
    6468             : {
    6469             :     int16_t i;
    6470             :     IVAS_REND_AudioBuffer inAudio;
    6471             : 
    6472      459366 :     push_wmops( "renderMcToSba" );
    6473      459366 :     inAudio = mcInput->base.inputBuffer;
    6474             : 
    6475     3572772 :     for ( i = 0; i < inAudio.config.numChannels; ++i )
    6476             :     {
    6477     3113406 :         renderBufferChannel( inAudio, i, mcInput->panGains[i], outAudio );
    6478             :     }
    6479             : 
    6480      459366 :     pop_wmops();
    6481      459366 :     return;
    6482             : }
    6483             : 
    6484             : 
    6485         604 : static void renderMcToMasa(
    6486             :     input_mc *mcInput,
    6487             :     IVAS_REND_AudioBuffer outAudio )
    6488             : {
    6489             :     float tmpRendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    6490             : 
    6491         604 :     push_wmops( "renderMcToMasa" );
    6492         604 :     copyBufferTo2dArray( mcInput->base.inputBuffer, tmpRendBuffer );
    6493             : 
    6494         604 :     ivas_mcmasa_ana( mcInput->hMcMasa, tmpRendBuffer, mcInput->base.inputBuffer.config.numSamplesPerChannel, outAudio.config.numChannels, mcInput->base.inputBuffer.config.numChannels );
    6495             : 
    6496         604 :     accumulate2dArrayToBuffer( tmpRendBuffer, &outAudio );
    6497             : 
    6498         604 :     pop_wmops();
    6499         604 :     return;
    6500             : }
    6501             : 
    6502             : 
    6503           0 : static ivas_error renderMcToSplitBinaural(
    6504             :     input_mc *mcInput,
    6505             :     const AUDIO_CONFIG outConfig,
    6506             :     IVAS_REND_AudioBuffer outAudio )
    6507             : {
    6508             :     int16_t i, j, pos_idx;
    6509             :     int16_t sf;
    6510             :     int16_t output_frame;
    6511             :     ivas_error error;
    6512             :     const MULTI_BIN_REND_POSE_DATA *pMultiBinPoseData;
    6513             :     const SPLIT_REND_WRAPPER *pSplitRendWrapper;
    6514             :     float tmpRendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    6515             :     float *p_tmpRendBuffer[MAX_OUTPUT_CHANNELS];
    6516             :     float tmpSplitBinauralBuffer[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][L_FRAME48k];
    6517             :     AUDIO_CONFIG inConfig;
    6518             :     IVAS_REND_AudioBuffer tmpRotBuffer;
    6519             :     COMBINED_ORIENTATION_DATA combinedOrientationDataLocal;
    6520             :     COMBINED_ORIENTATION_HANDLE pCombinedOrientationDataLocal;
    6521             : 
    6522           0 :     push_wmops( "renderMcToSplitBinaural" );
    6523           0 :     inConfig = mcInput->base.inConfig;
    6524           0 :     output_frame = mcInput->base.inputBuffer.config.numSamplesPerChannel;
    6525             : 
    6526           0 :     pSplitRendWrapper = mcInput->base.ctx.pSplitRendWrapper;
    6527           0 :     pMultiBinPoseData = &pSplitRendWrapper->multiBinPoseData;
    6528             : 
    6529           0 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; ++i )
    6530             :     {
    6531           0 :         p_tmpRendBuffer[i] = tmpRendBuffer[i];
    6532             :     }
    6533             : 
    6534             :     /* save current head positions */
    6535           0 :     pCombinedOrientationDataLocal = *mcInput->base.ctx.pCombinedOrientationData;
    6536           0 :     combinedOrientationDataLocal = *pCombinedOrientationDataLocal;
    6537           0 :     if ( pMultiBinPoseData->poseCorrectionMode == ISAR_SPLIT_REND_POSE_CORRECTION_MODE_CLDFB )
    6538             :     {
    6539           0 :         for ( sf = 1; sf < combinedOrientationDataLocal.num_subframes; ++sf )
    6540             :         {
    6541           0 :             combinedOrientationDataLocal.Quaternions[sf] = combinedOrientationDataLocal.Quaternions[0];
    6542           0 :             for ( i = 0; i < 3; i++ )
    6543             :             {
    6544           0 :                 for ( j = 0; j < 3; j++ )
    6545             :                 {
    6546           0 :                     combinedOrientationDataLocal.Rmat[sf][i][j] = combinedOrientationDataLocal.Rmat[0][i][j];
    6547             :                 }
    6548             :             }
    6549             :         }
    6550             :     }
    6551             : 
    6552             :     /* temporary buffer for rotation in source format for CREND */
    6553           0 :     tmpRotBuffer = mcInput->base.inputBuffer;
    6554           0 :     if ( inConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM && inConfig != IVAS_AUDIO_CONFIG_5_1 && inConfig != IVAS_AUDIO_CONFIG_7_1 )
    6555             :     {
    6556           0 :         tmpRotBuffer.data = malloc( tmpRotBuffer.config.numSamplesPerChannel * tmpRotBuffer.config.numChannels * sizeof( float ) );
    6557             :     }
    6558             : 
    6559           0 :     for ( pos_idx = 0; pos_idx < pMultiBinPoseData->num_poses; pos_idx++ )
    6560             :     {
    6561             :         /* Update head positions */
    6562             :         IVAS_QUATERNION Quaternions_orig[MAX_PARAM_SPATIAL_SUBFRAMES], Quaternions_abs;
    6563           0 :         for ( i = 0; i < combinedOrientationDataLocal.num_subframes; i++ )
    6564             :         {
    6565           0 :             Quaternions_orig[i] = combinedOrientationDataLocal.Quaternions[i];
    6566           0 :             Quaternions_abs.w = -3.0f;
    6567           0 :             Quat2EulerDegree( combinedOrientationDataLocal.Quaternions[i], &Quaternions_abs.z, &Quaternions_abs.y, &Quaternions_abs.x ); /*order in Quat2Euler seems to be reversed ?*/
    6568             : 
    6569           0 :             Quaternions_abs.x += pMultiBinPoseData->relative_head_poses[pos_idx][0];
    6570           0 :             Quaternions_abs.y += pMultiBinPoseData->relative_head_poses[pos_idx][1];
    6571           0 :             Quaternions_abs.z += pMultiBinPoseData->relative_head_poses[pos_idx][2];
    6572           0 :             combinedOrientationDataLocal.Quaternions[i] = Quaternions_abs;
    6573           0 :             QuatToRotMat( combinedOrientationDataLocal.Quaternions[i], combinedOrientationDataLocal.Rmat[i] );
    6574             :         }
    6575             : 
    6576           0 :         if ( inConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM || inConfig == IVAS_AUDIO_CONFIG_5_1 || inConfig == IVAS_AUDIO_CONFIG_7_1 )
    6577             :         {
    6578             :             /* tdrend processing overview:
    6579             :              *  1. copy from inputBuffer to tmpRendBuffer
    6580             :              *  2. td_binaural_renderer_ext: inplace processing in tmpRendBuffer
    6581             :              *  3. copy from tmpRendBuffer to tmpSplitBinBuffer
    6582             :              *  4. LFE mixing
    6583             :              *  5. tmpSplitBinBuffer accumulated to outBuffer */
    6584             : 
    6585             :             /* copy input to tdrend input/output buffer */
    6586           0 :             copyBufferTo2dArray( mcInput->base.inputBuffer, tmpRendBuffer );
    6587             : 
    6588             :             /* perform rotation in source format to tmpRotBuffer */
    6589           0 :             pCombinedOrientationDataLocal = &combinedOrientationDataLocal;
    6590             : 
    6591             :             /* Render */
    6592           0 :             if ( ( error = ivas_td_binaural_renderer_ext( ( pos_idx == 0 ) ? &mcInput->tdRendWrapper : &mcInput->splitTdRendWrappers[pos_idx - 1], mcInput->base.inConfig, &mcInput->customLsInput, &pCombinedOrientationDataLocal, NULL, mcInput->hReverb, 0, /* Ism Audio Metadata Delay Sync in ms for External Renderer */ *mcInput->base.ctx.pOutSampleRate, mcInput->base.inputBuffer.config.numSamplesPerChannel, tmpRendBuffer ) ) != IVAS_ERR_OK )
    6593             :             {
    6594           0 :                 return error;
    6595             :             }
    6596             : 
    6597             :             /* Copy rendered audio to tmp storage buffer. Copying directly to output would
    6598             :              * overwrite original audio, which is still needed for rendering next head pose. */
    6599           0 :             mvr2r( tmpRendBuffer[0], tmpSplitBinauralBuffer[2 * pos_idx], output_frame );
    6600           0 :             mvr2r( tmpRendBuffer[1], tmpSplitBinauralBuffer[2 * pos_idx + 1], output_frame );
    6601             :         }
    6602             :         else
    6603             :         {
    6604             :             /* crend processing overview:
    6605             :              *  1. rotateFrameMc: inputBuffer to tmpRotBuffer
    6606             :              *  2. crend_process: tmpRotBuffer to tmpRendBuffer
    6607             :              *  3. copy from tmpRendBuffer to tmpSplitBinBuffer
    6608             :              *  4. LFE mixing
    6609             :              *  5. tmpSplitBinBuffer accumulated to outBuffer */
    6610             : 
    6611             :             /* copy input for in-place rotation */
    6612           0 :             set_zero( tmpRotBuffer.data, tmpRotBuffer.config.numSamplesPerChannel * tmpRotBuffer.config.numChannels );
    6613             : 
    6614             :             /* perform rotation in source format to tmpRotBuffer */
    6615           0 :             pCombinedOrientationDataLocal = &combinedOrientationDataLocal;
    6616           0 :             if ( ( error = rotateFrameMc( mcInput->base.inputBuffer, mcInput->base.inConfig, &mcInput->customLsInput, mcInput->base.ctx.pHeadRotData, &pCombinedOrientationDataLocal, mcInput->rot_gains_prev[pos_idx], mcInput->efapInWrapper.hEfap, tmpRotBuffer ) ) != IVAS_ERR_OK )
    6617             :             {
    6618           0 :                 return error;
    6619             :             }
    6620             : 
    6621           0 :             copyBufferTo2dArray( tmpRotBuffer, tmpRendBuffer );
    6622             : 
    6623             :             /* call CREND (rotation already performed) */
    6624           0 :             if ( ( error = ivas_rend_crendProcessSubframe( mcInput->crendWrapper, mcInput->base.inConfig, outConfig, NULL, NULL,
    6625           0 :                                                            NULL, NULL, NULL, p_tmpRendBuffer, p_tmpRendBuffer, mcInput->base.inputBuffer.config.numSamplesPerChannel, *mcInput->base.ctx.pOutSampleRate, pos_idx ) ) != IVAS_ERR_OK )
    6626             :             {
    6627           0 :                 return error;
    6628             :             }
    6629             : 
    6630             :             /* Copy rendererd audio to tmp storage buffer, Copying directly to output would
    6631             :              * overwrite original audio, which is still needed for rendering next head pose. */
    6632           0 :             mvr2r( tmpRendBuffer[0], tmpSplitBinauralBuffer[2 * pos_idx], output_frame );
    6633           0 :             mvr2r( tmpRendBuffer[1], tmpSplitBinauralBuffer[2 * pos_idx + 1], output_frame );
    6634             :         }
    6635             : 
    6636             :         /* restore original headrotation data */
    6637           0 :         for ( i = 0; i < combinedOrientationDataLocal.num_subframes; i++ )
    6638             :         {
    6639           0 :             combinedOrientationDataLocal.Quaternions[i] = Quaternions_orig[i];
    6640             :         }
    6641             :     }
    6642             : 
    6643           0 :     if ( inConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM && inConfig != IVAS_AUDIO_CONFIG_5_1 && inConfig != IVAS_AUDIO_CONFIG_7_1 )
    6644             :     {
    6645             :         /* free temporary buffer for rotation in source format for CREND */
    6646           0 :         free( tmpRotBuffer.data );
    6647             :     }
    6648             : 
    6649           0 :     accumulate2dArrayToBuffer( tmpSplitBinauralBuffer, &outAudio );
    6650             : 
    6651           0 :     if ( ( error = renderLfeToBinaural( mcInput, outConfig, outAudio ) ) != IVAS_ERR_OK )
    6652             :     {
    6653           0 :         return error;
    6654             :     }
    6655             : 
    6656           0 :     pop_wmops();
    6657           0 :     return IVAS_ERR_OK;
    6658             : }
    6659             : 
    6660             : 
    6661     2175198 : static ivas_error renderInputMc(
    6662             :     input_mc *mcInput,
    6663             :     const AUDIO_CONFIG outConfig,
    6664             :     IVAS_REND_AudioBuffer outAudio )
    6665             : {
    6666             :     ivas_error error;
    6667             :     IVAS_REND_AudioBuffer inAudio;
    6668             : 
    6669     2175198 :     error = IVAS_ERR_OK;
    6670             : 
    6671     2175198 :     inAudio = mcInput->base.inputBuffer;
    6672             : 
    6673     2175198 :     if ( mcInput->base.numNewSamplesPerChannel != outAudio.config.numSamplesPerChannel )
    6674             :     {
    6675           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_BUFFER_SIZE, "Mismatch between the number of input samples vs number of requested output samples - currently not allowed" );
    6676             :     }
    6677     2175198 :     mcInput->base.numNewSamplesPerChannel = 0;
    6678             : 
    6679             :     /* Apply input gain to new audio */
    6680     2175198 :     v_multc( inAudio.data, mcInput->base.gain, inAudio.data, inAudio.config.numSamplesPerChannel * inAudio.config.numChannels );
    6681             : 
    6682             :     /* set combined orientation subframe info to start info */
    6683     2175198 :     ivas_combined_orientation_set_to_start_index( *( mcInput->base.ctx.pCombinedOrientationData ) );
    6684             : 
    6685     2175198 :     switch ( getAudioConfigType( outConfig ) )
    6686             :     {
    6687     1192580 :         case IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED:
    6688     1192580 :             renderMcToMc( mcInput, outAudio );
    6689     1192580 :             break;
    6690      459366 :         case IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS:
    6691      459366 :             renderMcToSba( mcInput, outAudio );
    6692      459366 :             break;
    6693      522648 :         case IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL:
    6694             :             switch ( outConfig )
    6695             :             {
    6696      174216 :                 case IVAS_AUDIO_CONFIG_BINAURAL:
    6697      174216 :                     error = renderMcToBinaural( mcInput, outConfig, outAudio );
    6698      174216 :                     break;
    6699      348432 :                 case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR:
    6700             :                 case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB:
    6701      348432 :                     if ( mcInput->base.inConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    6702             :                     {
    6703      156192 :                         error = renderMcCustomLsToBinauralRoom( mcInput, outConfig, outAudio );
    6704             :                     }
    6705             :                     else
    6706             :                     {
    6707      192240 :                         error = renderMcToBinauralRoom( mcInput, outConfig, outAudio );
    6708             :                     }
    6709      348432 :                     break;
    6710           0 :                 case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED:
    6711             :                 case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM:
    6712           0 :                     error = renderMcToSplitBinaural( mcInput, outConfig, outAudio );
    6713           0 :                     break;
    6714           0 :                 default:
    6715           0 :                     return IVAS_ERR_INVALID_OUTPUT_FORMAT;
    6716             :             }
    6717      522648 :             break;
    6718         604 :         case IVAS_REND_AUDIO_CONFIG_TYPE_MASA:
    6719         604 :             renderMcToMasa( mcInput, outAudio );
    6720         604 :             break;
    6721           0 :         default:
    6722           0 :             return IVAS_ERR_INVALID_OUTPUT_FORMAT;
    6723             :     }
    6724             : 
    6725     2175198 :     return error;
    6726             : }
    6727             : 
    6728             : 
    6729    13740500 : static ivas_error renderActiveInputsMc(
    6730             :     IVAS_REND_HANDLE hIvasRend,
    6731             :     IVAS_REND_AudioBuffer outAudio )
    6732             : {
    6733             :     int16_t i;
    6734             :     input_mc *pCurrentInput;
    6735             :     ivas_error error;
    6736             : 
    6737    27481000 :     for ( i = 0, pCurrentInput = hIvasRend->inputsMc; i < RENDERER_MAX_MC_INPUTS; ++i, ++pCurrentInput )
    6738             :     {
    6739    13740500 :         if ( pCurrentInput->base.inConfig == IVAS_AUDIO_CONFIG_INVALID )
    6740             :         {
    6741             :             /* Skip inactive inputs */
    6742    11565302 :             continue;
    6743             :         }
    6744             : 
    6745     2175198 :         if ( ( error = renderInputMc( pCurrentInput, hIvasRend->outputConfig, outAudio ) ) != IVAS_ERR_OK )
    6746             :         {
    6747           0 :             return error;
    6748             :         }
    6749             :     }
    6750             : 
    6751    13740500 :     return IVAS_ERR_OK;
    6752             : }
    6753             : 
    6754             : 
    6755     3435744 : static void renderSbaToMc(
    6756             :     const input_sba *sbaInput,
    6757             :     IVAS_REND_AudioBuffer outAudio )
    6758             : {
    6759             :     int16_t i;
    6760             :     IVAS_REND_AudioBuffer inAudio;
    6761             : 
    6762     3435744 :     push_wmops( "renderSbaToMc" );
    6763     3435744 :     inAudio = sbaInput->base.inputBuffer;
    6764             : 
    6765    36576060 :     for ( i = 0; i < inAudio.config.numChannels; ++i )
    6766             :     {
    6767    33140316 :         renderBufferChannel( inAudio, i, sbaInput->hoaDecMtx[i], outAudio );
    6768             :     }
    6769             : 
    6770     3435744 :     pop_wmops();
    6771     3435744 :     return;
    6772             : }
    6773             : 
    6774             : 
    6775     1356516 : static void renderSbaToSba(
    6776             :     const input_sba *sbaInput,
    6777             :     IVAS_REND_AudioBuffer outAudio )
    6778             : {
    6779             :     int16_t i;
    6780             :     IVAS_REND_AudioBuffer inAudio;
    6781             : 
    6782     1356516 :     push_wmops( "renderSbaToSba" );
    6783     1356516 :     inAudio = sbaInput->base.inputBuffer;
    6784             : 
    6785    14438700 :     for ( i = 0; i < inAudio.config.numChannels; ++i )
    6786             :     {
    6787    13082184 :         renderBufferChannel( inAudio, i, sbaInput->hoaDecMtx[i], outAudio );
    6788             :     }
    6789             : 
    6790     1356516 :     pop_wmops();
    6791     1356516 :     return;
    6792             : }
    6793             : 
    6794             : 
    6795           0 : static ivas_error renderSbaToMultiBinaural(
    6796             :     input_sba *sbaInput,
    6797             :     const AUDIO_CONFIG outConfig,
    6798             :     float out[][L_FRAME48k] )
    6799             : {
    6800             :     float tmpCrendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    6801             :     float *p_tmpCrendBuffer[MAX_OUTPUT_CHANNELS];
    6802             :     int16_t sf;
    6803             :     int16_t i, j, pos_idx;
    6804             :     COMBINED_ORIENTATION_DATA combinedOrientationDataLocal;
    6805             :     COMBINED_ORIENTATION_HANDLE pCombinedOrientationDataLocal;
    6806             :     ivas_error error;
    6807             :     IVAS_REND_AudioBuffer tmpRotBuffer;
    6808             :     const MULTI_BIN_REND_POSE_DATA *pMultiBinPoseData;
    6809             : 
    6810           0 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    6811             :     {
    6812           0 :         p_tmpCrendBuffer[i] = tmpCrendBuffer[i];
    6813             :     }
    6814           0 :     push_wmops( "renderSbaToMultiBinaural" );
    6815           0 :     pMultiBinPoseData = &sbaInput->base.ctx.pSplitRendWrapper->multiBinPoseData;
    6816             : 
    6817           0 :     pCombinedOrientationDataLocal = *sbaInput->base.ctx.pCombinedOrientationData;
    6818           0 :     combinedOrientationDataLocal = *pCombinedOrientationDataLocal;
    6819           0 :     if ( pMultiBinPoseData->poseCorrectionMode == ISAR_SPLIT_REND_POSE_CORRECTION_MODE_CLDFB )
    6820             :     {
    6821           0 :         for ( sf = 1; sf < combinedOrientationDataLocal.num_subframes; sf++ )
    6822             :         {
    6823           0 :             combinedOrientationDataLocal.Quaternions[sf] = combinedOrientationDataLocal.Quaternions[0];
    6824           0 :             for ( i = 0; i < 3; i++ )
    6825             :             {
    6826           0 :                 for ( j = 0; j < 3; j++ )
    6827             :                 {
    6828           0 :                     combinedOrientationDataLocal.Rmat[sf][i][j] = combinedOrientationDataLocal.Rmat[0][i][j];
    6829             :                 }
    6830             :             }
    6831             :         }
    6832             :     }
    6833             : 
    6834           0 :     tmpRotBuffer = sbaInput->base.inputBuffer;
    6835           0 :     tmpRotBuffer.data = malloc( tmpRotBuffer.config.numSamplesPerChannel * tmpRotBuffer.config.numChannels * sizeof( float ) );
    6836             : 
    6837           0 :     for ( pos_idx = 0; pos_idx < pMultiBinPoseData->num_poses; pos_idx++ )
    6838             :     {
    6839             :         IVAS_QUATERNION Quaternions_orig[MAX_PARAM_SPATIAL_SUBFRAMES], Quaternions_abs;
    6840           0 :         for ( i = 0; i < combinedOrientationDataLocal.num_subframes; i++ )
    6841             :         {
    6842           0 :             Quaternions_orig[i] = combinedOrientationDataLocal.Quaternions[i];
    6843           0 :             Quaternions_abs.w = -3.0f;
    6844           0 :             Quat2EulerDegree( combinedOrientationDataLocal.Quaternions[i], &Quaternions_abs.z, &Quaternions_abs.y, &Quaternions_abs.x ); /*order in Quat2Euler seems to be reversed ?*/
    6845             : 
    6846           0 :             Quaternions_abs.x += pMultiBinPoseData->relative_head_poses[pos_idx][0];
    6847           0 :             Quaternions_abs.y += pMultiBinPoseData->relative_head_poses[pos_idx][1];
    6848           0 :             Quaternions_abs.z += pMultiBinPoseData->relative_head_poses[pos_idx][2];
    6849           0 :             combinedOrientationDataLocal.Quaternions[i] = Quaternions_abs;
    6850           0 :             QuatToRotMat( combinedOrientationDataLocal.Quaternions[i], combinedOrientationDataLocal.Rmat[i] );
    6851             :         }
    6852             : 
    6853             : 
    6854             :         /* copy input for in-place rotation */
    6855           0 :         mvr2r( sbaInput->base.inputBuffer.data, tmpRotBuffer.data, tmpRotBuffer.config.numChannels * tmpRotBuffer.config.numSamplesPerChannel );
    6856             : 
    6857           0 :         pCombinedOrientationDataLocal = &combinedOrientationDataLocal;
    6858             : 
    6859           0 :         if ( ( error = rotateFrameSba( sbaInput->base.inputBuffer, sbaInput->base.inConfig, sbaInput->base.ctx.pHeadRotData, &pCombinedOrientationDataLocal, sbaInput->rot_gains_prev[pos_idx], tmpRotBuffer ) ) != IVAS_ERR_OK )
    6860             :         {
    6861           0 :             return error;
    6862             :         }
    6863             : 
    6864           0 :         copyBufferTo2dArray( tmpRotBuffer, tmpCrendBuffer );
    6865             : 
    6866           0 :         assert( sbaInput->crendWrapper->hCrend[0]->hReverb == NULL );
    6867             : 
    6868             :         /* call CREND */
    6869           0 :         if ( ( error = ivas_rend_crendProcessSubframe( sbaInput->crendWrapper, sbaInput->base.inConfig, outConfig, NULL, NULL,
    6870           0 :                                                        NULL, NULL, NULL, p_tmpCrendBuffer, p_tmpCrendBuffer, sbaInput->base.inputBuffer.config.numSamplesPerChannel, *sbaInput->base.ctx.pOutSampleRate, pos_idx ) ) != IVAS_ERR_OK )
    6871             :         {
    6872           0 :             return error;
    6873             :         }
    6874             : 
    6875           0 :         for ( i = 0; i < combinedOrientationDataLocal.num_subframes; i++ )
    6876             :         {
    6877           0 :             combinedOrientationDataLocal.Quaternions[i] = Quaternions_orig[i];
    6878             :         }
    6879             : 
    6880             : 
    6881             :         /* move to output */
    6882           0 :         for ( i = 0; i < BINAURAL_CHANNELS; i++ )
    6883             :         {
    6884           0 :             mvr2r( tmpCrendBuffer[i], out[pos_idx * BINAURAL_CHANNELS + i], tmpRotBuffer.config.numSamplesPerChannel );
    6885             :         }
    6886             :     }
    6887             : 
    6888           0 :     free( tmpRotBuffer.data );
    6889             : 
    6890           0 :     pop_wmops();
    6891           0 :     return IVAS_ERR_OK;
    6892             : }
    6893             : 
    6894             : 
    6895           0 : static void renderSbaToMultiBinauralCldfb(
    6896             :     input_sba *sbaInput,
    6897             :     float Cldfb_Out_Real[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX],
    6898             :     float Cldfb_Out_Imag[][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX],
    6899             :     const int16_t low_res_pre_rend_rot,
    6900             :     const int16_t num_subframes )
    6901             : {
    6902             :     float Cldfb_RealBuffer[MAX_OUTPUT_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    6903             :     float Cldfb_ImagBuffer[MAX_OUTPUT_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    6904             : 
    6905           0 :     copyBufferToCLDFBarray( sbaInput->base.inputBuffer, Cldfb_RealBuffer, Cldfb_ImagBuffer );
    6906             : 
    6907           0 :     ivas_rend_CldfbMultiBinRendProcess( sbaInput->cldfbRendWrapper.hCldfbRend, sbaInput->base.ctx.pCombinedOrientationData, &sbaInput->base.ctx.pSplitRendWrapper->multiBinPoseData,
    6908             :                                         Cldfb_RealBuffer, Cldfb_ImagBuffer, Cldfb_Out_Real, Cldfb_Out_Imag, low_res_pre_rend_rot, num_subframes );
    6909             : 
    6910           0 :     return;
    6911             : }
    6912             : 
    6913             : 
    6914           0 : static ivas_error renderSbaToSplitBinaural(
    6915             :     input_sba *sbaInput,
    6916             :     const AUDIO_CONFIG outConfig,
    6917             :     IVAS_REND_AudioBuffer outAudio )
    6918             : {
    6919             :     float tmpCrendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    6920             :     ivas_error error;
    6921             :     float Cldfb_RealBuffer_Binaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    6922             :     float Cldfb_ImagBuffer_Binaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    6923             : 
    6924           0 :     push_wmops( "renderSbaToSplitBinaural" );
    6925             : 
    6926           0 :     if ( sbaInput->base.ctx.hhRendererConfig[0]->split_rend_config.rendererSelection == IVAS_BIN_RENDERER_TYPE_FASTCONV )
    6927             :     {
    6928           0 :         renderSbaToMultiBinauralCldfb( sbaInput, Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural, 1,
    6929           0 :                                        getNumSubframesInBuffer( &outAudio, *sbaInput->base.ctx.pOutSampleRate ) );
    6930             : 
    6931           0 :         accumulateCLDFBArrayToBuffer( Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural, &outAudio );
    6932             :     }
    6933             :     else
    6934             :     {
    6935           0 :         if ( ( error = renderSbaToMultiBinaural( sbaInput, outConfig, tmpCrendBuffer ) ) != IVAS_ERR_OK )
    6936             :         {
    6937           0 :             return error;
    6938             :         }
    6939             : 
    6940           0 :         accumulate2dArrayToBuffer( tmpCrendBuffer, &outAudio );
    6941             :     }
    6942             : 
    6943           0 :     pop_wmops();
    6944           0 :     return IVAS_ERR_OK;
    6945             : }
    6946             : 
    6947             : 
    6948     1801440 : static ivas_error renderSbaToBinaural(
    6949             :     input_sba *sbaInput,
    6950             :     const AUDIO_CONFIG outConfig,
    6951             :     IVAS_REND_AudioBuffer outAudio )
    6952             : {
    6953             :     float tmpCrendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    6954             :     ivas_error error;
    6955             :     IVAS_REND_AudioBuffer tmpRotBuffer;
    6956             :     float *p_tmpCrendBuffer[MAX_OUTPUT_CHANNELS];
    6957             :     int16_t i;
    6958             :     const COMBINED_ORIENTATION_HANDLE *hCombinedOrientationData;
    6959             :     int8_t combinedOrientationEnabled;
    6960             :     int16_t subframe_idx;
    6961             : 
    6962     1801440 :     push_wmops( "renderSbaToBinaural" );
    6963     1801440 :     if ( sbaInput->base.ctx.hhRendererConfig[0]->split_rend_config.rendererSelection == IVAS_BIN_RENDERER_TYPE_FASTCONV )
    6964             :     {
    6965             :         float Cldfb_RealBuffer_Binaural[BINAURAL_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    6966             :         float Cldfb_ImagBuffer_Binaural[BINAURAL_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    6967             : 
    6968           0 :         renderSbaToMultiBinauralCldfb( sbaInput, Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural, 0,
    6969           0 :                                        getNumSubframesInBuffer( &outAudio, *sbaInput->base.ctx.pOutSampleRate ) );
    6970             : 
    6971           0 :         accumulateCLDFBArrayToBuffer( Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural, &outAudio );
    6972             :     }
    6973             :     else
    6974             :     {
    6975    30624480 :         for ( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    6976             :         {
    6977    28823040 :             p_tmpCrendBuffer[i] = tmpCrendBuffer[i];
    6978             :         }
    6979             : 
    6980     1801440 :         hCombinedOrientationData = sbaInput->base.ctx.pCombinedOrientationData;
    6981     1801440 :         combinedOrientationEnabled = 0;
    6982     1801440 :         if ( *hCombinedOrientationData != NULL )
    6983             :         {
    6984      900720 :             for ( subframe_idx = 0; subframe_idx < ( *hCombinedOrientationData )->num_subframes; subframe_idx++ )
    6985             :             {
    6986      900720 :                 if ( ( *hCombinedOrientationData )->enableCombinedOrientation[subframe_idx] != 0 )
    6987             :                 {
    6988      900720 :                     combinedOrientationEnabled = 1;
    6989      900720 :                     break;
    6990             :                 }
    6991             :             }
    6992             :         }
    6993             : 
    6994             :         /* apply rotation */
    6995     1801440 :         if ( combinedOrientationEnabled )
    6996             :         {
    6997      900720 :             tmpRotBuffer = sbaInput->base.inputBuffer;
    6998      900720 :             tmpRotBuffer.data = malloc( tmpRotBuffer.config.numSamplesPerChannel * tmpRotBuffer.config.numChannels * sizeof( float ) );
    6999             : 
    7000             :             /* copy input for in-place rotation */
    7001      900720 :             mvr2r( sbaInput->base.inputBuffer.data, tmpRotBuffer.data, tmpRotBuffer.config.numChannels * tmpRotBuffer.config.numSamplesPerChannel );
    7002             : 
    7003      900720 :             if ( ( error = rotateFrameSba( sbaInput->base.inputBuffer, sbaInput->base.inConfig, sbaInput->base.ctx.pHeadRotData,
    7004      900720 :                                            sbaInput->base.ctx.pCombinedOrientationData, sbaInput->rot_gains_prev[0], tmpRotBuffer ) ) != IVAS_ERR_OK )
    7005             :             {
    7006           0 :                 return error;
    7007             :             }
    7008             : 
    7009      900720 :             copyBufferTo2dArray( tmpRotBuffer, tmpCrendBuffer );
    7010      900720 :             free( tmpRotBuffer.data );
    7011             :         }
    7012             :         else
    7013             :         {
    7014      900720 :             copyBufferTo2dArray( sbaInput->base.inputBuffer, tmpCrendBuffer );
    7015             :         }
    7016             : 
    7017             :         /* call CREND */
    7018     1801440 :         if ( ( error = ivas_rend_crendProcessSubframe( sbaInput->crendWrapper, sbaInput->base.inConfig, outConfig, NULL, NULL,
    7019     1801440 :                                                        NULL, NULL, NULL, p_tmpCrendBuffer, p_tmpCrendBuffer, sbaInput->base.inputBuffer.config.numSamplesPerChannel, *sbaInput->base.ctx.pOutSampleRate, 0 ) ) != IVAS_ERR_OK )
    7020             :         {
    7021           0 :             return error;
    7022             :         }
    7023             : 
    7024     1801440 :         accumulate2dArrayToBuffer( tmpCrendBuffer, &outAudio );
    7025             :     }
    7026             : 
    7027     1801440 :     pop_wmops();
    7028     1801440 :     return IVAS_ERR_OK;
    7029             : }
    7030             : 
    7031             : 
    7032      900720 : static ivas_error renderSbaToBinauralRoom(
    7033             :     input_sba *sbaInput,
    7034             :     const AUDIO_CONFIG outConfig,
    7035             :     IVAS_REND_AudioBuffer outAudio )
    7036             : {
    7037             :     int16_t i;
    7038             :     int16_t tmp;
    7039             :     float tmpCrendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    7040             :     ivas_error error;
    7041             :     IVAS_REND_AudioBuffer tmpRotBuffer;
    7042             :     IVAS_REND_AudioBuffer tmpMcBuffer;
    7043             :     IVAS_REND_AudioBuffer *tmpBufPtr;
    7044             :     float *p_tmpCrendBuffer[MAX_OUTPUT_CHANNELS];
    7045             :     const COMBINED_ORIENTATION_HANDLE *hCombinedOrientationData;
    7046             :     int8_t combinedOrientationEnabled;
    7047             :     int16_t subframe_idx;
    7048             : 
    7049      900720 :     tmpRotBuffer = outAudio; /* avoid compilation warning */
    7050      900720 :     push_wmops( "renderSbaToBinauralRoom" );
    7051             : 
    7052    15312240 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    7053             :     {
    7054    14411520 :         p_tmpCrendBuffer[i] = tmpCrendBuffer[i];
    7055             :     }
    7056             : 
    7057      900720 :     hCombinedOrientationData = sbaInput->base.ctx.pCombinedOrientationData;
    7058      900720 :     combinedOrientationEnabled = 0;
    7059      900720 :     if ( *hCombinedOrientationData != NULL )
    7060             :     {
    7061      450360 :         for ( subframe_idx = 0; subframe_idx < ( *hCombinedOrientationData )->num_subframes; subframe_idx++ )
    7062             :         {
    7063      450360 :             if ( ( *hCombinedOrientationData )->enableCombinedOrientation[subframe_idx] != 0 )
    7064             :             {
    7065      450360 :                 combinedOrientationEnabled = 1;
    7066      450360 :                 break;
    7067             :             }
    7068             :         }
    7069             :     }
    7070             : 
    7071             :     /* apply rotation */
    7072      900720 :     if ( combinedOrientationEnabled )
    7073             :     {
    7074      450360 :         tmpRotBuffer = sbaInput->base.inputBuffer;
    7075      450360 :         tmpRotBuffer.data = malloc( tmpRotBuffer.config.numSamplesPerChannel * tmpRotBuffer.config.numChannels * sizeof( float ) );
    7076             : 
    7077             :         /* copy input for in-place rotation */
    7078      450360 :         mvr2r( sbaInput->base.inputBuffer.data, tmpRotBuffer.data, tmpRotBuffer.config.numChannels * tmpRotBuffer.config.numSamplesPerChannel );
    7079             : 
    7080      450360 :         if ( ( error = rotateFrameSba( sbaInput->base.inputBuffer, sbaInput->base.inConfig, sbaInput->base.ctx.pHeadRotData,
    7081      450360 :                                        sbaInput->base.ctx.pCombinedOrientationData, sbaInput->rot_gains_prev[0], tmpRotBuffer ) ) != IVAS_ERR_OK )
    7082             :         {
    7083           0 :             return error;
    7084             :         }
    7085             :     }
    7086             : 
    7087             :     /* intermediate rendering to 7_1_4 */
    7088      900720 :     tmpMcBuffer = sbaInput->base.inputBuffer;
    7089             : 
    7090      900720 :     if ( ( error = getAudioConfigNumChannels( IVAS_AUDIO_CONFIG_7_1_4, &tmp ) ) != IVAS_ERR_OK )
    7091             :     {
    7092           0 :         return error;
    7093             :     }
    7094             : 
    7095      900720 :     tmpMcBuffer.config.numChannels = tmp;
    7096      900720 :     tmpMcBuffer.data = malloc( tmpMcBuffer.config.numSamplesPerChannel * tmpMcBuffer.config.numChannels * sizeof( float ) );
    7097      900720 :     set_zero( tmpMcBuffer.data, tmpMcBuffer.config.numChannels * tmpMcBuffer.config.numSamplesPerChannel );
    7098             : 
    7099      900720 :     tmpBufPtr = ( combinedOrientationEnabled ) ? &tmpRotBuffer : &sbaInput->base.inputBuffer;
    7100     9607680 :     for ( i = 0; i < sbaInput->base.inputBuffer.config.numChannels; i++ )
    7101             :     {
    7102     8706960 :         renderBufferChannel( *tmpBufPtr, i, sbaInput->hoaDecMtx[i], tmpMcBuffer );
    7103             :     }
    7104             : 
    7105      900720 :     copyBufferTo2dArray( tmpMcBuffer, tmpCrendBuffer );
    7106             : 
    7107             :     /* call CREND */
    7108      900720 :     if ( ( error = ivas_rend_crendProcessSubframe( sbaInput->crendWrapper, IVAS_AUDIO_CONFIG_7_1_4, outConfig, NULL, NULL,
    7109      900720 :                                                    NULL, NULL, NULL, p_tmpCrendBuffer, p_tmpCrendBuffer, sbaInput->base.inputBuffer.config.numSamplesPerChannel, *sbaInput->base.ctx.pOutSampleRate, 0 ) ) != IVAS_ERR_OK )
    7110             :     {
    7111           0 :         return error;
    7112             :     }
    7113             : 
    7114      900720 :     accumulate2dArrayToBuffer( tmpCrendBuffer, &outAudio );
    7115             : 
    7116      900720 :     if ( combinedOrientationEnabled )
    7117             :     {
    7118      450360 :         free( tmpRotBuffer.data );
    7119             :     }
    7120      900720 :     free( tmpMcBuffer.data );
    7121             : 
    7122      900720 :     pop_wmops();
    7123      900720 :     return IVAS_ERR_OK;
    7124             : }
    7125             : 
    7126             : 
    7127         604 : static void renderSbaToMasa(
    7128             :     input_sba *sbaInput,
    7129             :     IVAS_REND_AudioBuffer outAudio )
    7130             : {
    7131             :     float tmpRendBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    7132             : 
    7133         604 :     push_wmops( "renderMcToMasa" );
    7134         604 :     copyBufferTo2dArray( sbaInput->base.inputBuffer, tmpRendBuffer );
    7135         604 :     ivas_dirac_ana( sbaInput->hDirAC, tmpRendBuffer, sbaInput->base.inputBuffer.config.numSamplesPerChannel, outAudio.config.numChannels );
    7136         604 :     accumulate2dArrayToBuffer( tmpRendBuffer, &outAudio );
    7137             : 
    7138         604 :     pop_wmops();
    7139         604 :     return;
    7140             : }
    7141             : 
    7142             : 
    7143     7495024 : static ivas_error renderInputSba(
    7144             :     input_sba *sbaInput,
    7145             :     const AUDIO_CONFIG outConfig,
    7146             :     IVAS_REND_AudioBuffer outAudio )
    7147             : {
    7148             :     ivas_error error;
    7149             :     IVAS_REND_AudioBuffer inAudio;
    7150             :     int16_t cldfb2tdSampleFact;
    7151             : 
    7152     7495024 :     error = IVAS_ERR_OK;
    7153     7495024 :     inAudio = sbaInput->base.inputBuffer;
    7154             : 
    7155     7495024 :     cldfb2tdSampleFact = outAudio.config.is_cldfb ? 2 : 1;
    7156     7495024 :     if ( ( sbaInput->base.numNewSamplesPerChannel * cldfb2tdSampleFact != outAudio.config.numSamplesPerChannel ) &&
    7157           0 :          ( outConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ) && ( outConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
    7158             :     {
    7159           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_BUFFER_SIZE, "Mismatch between the number of input samples vs number of requested output samples - currently not allowed" );
    7160             :     }
    7161     7495024 :     sbaInput->base.numNewSamplesPerChannel = 0;
    7162             : 
    7163             :     /* Apply input gain to new audio */
    7164     7495024 :     v_multc( inAudio.data, sbaInput->base.gain, inAudio.data, inAudio.config.numSamplesPerChannel * inAudio.config.numChannels );
    7165             : 
    7166             :     /* set combined orientation subframe info to start info */
    7167     7495024 :     ivas_combined_orientation_set_to_start_index( *( sbaInput->base.ctx.pCombinedOrientationData ) );
    7168             : 
    7169     7495024 :     switch ( getAudioConfigType( outConfig ) )
    7170             :     {
    7171     3435744 :         case IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED:
    7172     3435744 :             renderSbaToMc( sbaInput, outAudio );
    7173     3435744 :             break;
    7174     1356516 :         case IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS:
    7175     1356516 :             renderSbaToSba( sbaInput, outAudio );
    7176     1356516 :             break;
    7177     2702160 :         case IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL:
    7178             :             switch ( outConfig )
    7179             :             {
    7180           0 :                 case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED:
    7181             :                 case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM:
    7182           0 :                     error = renderSbaToSplitBinaural( sbaInput, outConfig, outAudio );
    7183           0 :                     break;
    7184     1801440 :                 case IVAS_AUDIO_CONFIG_BINAURAL:
    7185             :                 case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB:
    7186     1801440 :                     error = renderSbaToBinaural( sbaInput, outConfig, outAudio );
    7187     1801440 :                     break;
    7188      900720 :                 case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR:
    7189      900720 :                     error = renderSbaToBinauralRoom( sbaInput, outConfig, outAudio );
    7190      900720 :                     break;
    7191           0 :                 default:
    7192           0 :                     return IVAS_ERR_INVALID_OUTPUT_FORMAT;
    7193             :             }
    7194     2702160 :             break;
    7195         604 :         case IVAS_REND_AUDIO_CONFIG_TYPE_MASA:
    7196         604 :             renderSbaToMasa( sbaInput, outAudio );
    7197         604 :             break;
    7198           0 :         default:
    7199           0 :             return IVAS_ERR_INVALID_OUTPUT_FORMAT;
    7200             :     }
    7201             : 
    7202     7495024 :     return error;
    7203             : }
    7204             : 
    7205             : 
    7206    13740500 : static ivas_error renderActiveInputsSba(
    7207             :     IVAS_REND_HANDLE hIvasRend,
    7208             :     IVAS_REND_AudioBuffer outAudio )
    7209             : {
    7210             :     int16_t i;
    7211             :     input_sba *pCurrentInput;
    7212             :     ivas_error error;
    7213             : 
    7214    27481000 :     for ( i = 0, pCurrentInput = hIvasRend->inputsSba; i < RENDERER_MAX_SBA_INPUTS; ++i, ++pCurrentInput )
    7215             :     {
    7216    13740500 :         if ( pCurrentInput->base.inConfig == IVAS_AUDIO_CONFIG_INVALID )
    7217             :         {
    7218             :             /* Skip inactive inputs */
    7219     6245476 :             continue;
    7220             :         }
    7221             : 
    7222     7495024 :         if ( ( error = renderInputSba( pCurrentInput, hIvasRend->outputConfig, outAudio ) ) != IVAS_ERR_OK )
    7223             :         {
    7224           0 :             return error;
    7225             :         }
    7226             :     }
    7227             : 
    7228    13740500 :     return IVAS_ERR_OK;
    7229             : }
    7230             : 
    7231             : 
    7232     1016340 : static void copyMasaMetadataToDiracRenderer(
    7233             :     MASA_METADATA_FRAME *meta,
    7234             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom,
    7235             :     const int16_t maxBin )
    7236             : {
    7237             :     int16_t band, sf, bin;
    7238             :     int16_t meta_write_index;
    7239             : 
    7240     1016340 :     hSpatParamRendCom->numParametricDirections = meta->descriptive_meta.numberOfDirections + 1;
    7241     1016340 :     hSpatParamRendCom->numSimultaneousDirections = meta->descriptive_meta.numberOfDirections + 1;
    7242             : 
    7243     5081700 :     for ( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
    7244             :     {
    7245     4065360 :         meta_write_index = ( hSpatParamRendCom->dirac_bs_md_write_idx + sf ) % hSpatParamRendCom->dirac_md_buffer_length;
    7246             : 
    7247   101634000 :         for ( band = 0; band < MASA_MAXIMUM_CODING_SUBBANDS; band++ )
    7248             :         {
    7249   260183040 :             for ( bin = MASA_band_grouping_24[band]; bin < MASA_band_grouping_24[band + 1] && bin < maxBin; bin++ )
    7250             :             {
    7251   162614400 :                 hSpatParamRendCom->azimuth[meta_write_index][bin] = (int16_t) meta->directional_meta[0].azimuth[sf][band];
    7252   162614400 :                 hSpatParamRendCom->elevation[meta_write_index][bin] = (int16_t) meta->directional_meta[0].elevation[sf][band];
    7253   162614400 :                 hSpatParamRendCom->energy_ratio1[meta_write_index][bin] = meta->directional_meta[0].energy_ratio[sf][band];
    7254   162614400 :                 hSpatParamRendCom->diffuseness_vector[meta_write_index][bin] = 1.0f - meta->directional_meta[0].energy_ratio[sf][band];
    7255   162614400 :                 hSpatParamRendCom->spreadCoherence[meta_write_index][bin] = meta->directional_meta[0].spread_coherence[sf][band];
    7256   162614400 :                 hSpatParamRendCom->surroundingCoherence[meta_write_index][bin] = meta->common_meta.surround_coherence[sf][band];
    7257             : 
    7258   162614400 :                 if ( hSpatParamRendCom->numSimultaneousDirections == 2 )
    7259             :                 {
    7260    80948160 :                     hSpatParamRendCom->azimuth2[meta_write_index][bin] = (int16_t) meta->directional_meta[1].azimuth[sf][band];
    7261    80948160 :                     hSpatParamRendCom->elevation2[meta_write_index][bin] = (int16_t) meta->directional_meta[1].elevation[sf][band];
    7262    80948160 :                     hSpatParamRendCom->energy_ratio2[meta_write_index][bin] = meta->directional_meta[1].energy_ratio[sf][band];
    7263    80948160 :                     hSpatParamRendCom->diffuseness_vector[meta_write_index][bin] -= meta->directional_meta[1].energy_ratio[sf][band];
    7264    80948160 :                     hSpatParamRendCom->spreadCoherence2[meta_write_index][bin] = meta->directional_meta[1].spread_coherence[sf][band];
    7265             :                 }
    7266             :             }
    7267             :         }
    7268             :     }
    7269             : 
    7270     1016340 :     hSpatParamRendCom->dirac_bs_md_write_idx = ( hSpatParamRendCom->dirac_bs_md_write_idx + MAX_PARAM_SPATIAL_SUBFRAMES ) % hSpatParamRendCom->dirac_md_buffer_length;
    7271             : 
    7272     1016340 :     return;
    7273             : }
    7274             : 
    7275             : 
    7276        7836 : static void renderMasaToMasa(
    7277             :     input_masa *masaInput,
    7278             :     IVAS_REND_AudioBuffer outAudio )
    7279             : {
    7280             :     int16_t sf, band, dir, numDirs;
    7281             :     float ratioSum;
    7282             :     MASA_DECODER_EXT_OUT_META_HANDLE outMeta;
    7283             :     MASA_METADATA_FRAME *inMeta;
    7284             :     float tmpBuffer[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    7285             :     int16_t ts, i, j, l_ts;
    7286             :     float Chan_RealBuffer[MASA_MAX_TRANSPORT_CHANNELS][CLDFB_NO_CHANNELS_MAX];
    7287             :     float Chan_ImagBuffer[MASA_MAX_TRANSPORT_CHANNELS][CLDFB_NO_CHANNELS_MAX];
    7288             :     int16_t band_m_idx, block_m_idx;
    7289             :     int16_t mrange[2];
    7290             :     int16_t brange[2];
    7291             :     int16_t numAnalysisChannels;
    7292             : 
    7293        7836 :     copyBufferTo2dArray( masaInput->base.inputBuffer, tmpBuffer );
    7294             : 
    7295             :     /* Calculate energy */
    7296        7836 :     l_ts = masaInput->base.inputBuffer.config.numSamplesPerChannel / CLDFB_NO_COL_MAX;
    7297        7836 :     numAnalysisChannels = masaInput->hMasaPrerend->num_Cldfb_instances;
    7298             : 
    7299             :     /* do processing over all CLDFB time slots */
    7300       39180 :     for ( block_m_idx = 0; block_m_idx < MAX_PARAM_SPATIAL_SUBFRAMES; block_m_idx++ )
    7301             :     {
    7302       31344 :         mrange[0] = DirAC_block_grouping[block_m_idx];
    7303       31344 :         mrange[1] = DirAC_block_grouping[block_m_idx + 1];
    7304             : 
    7305       31344 :         set_zero( masaInput->hMasaPrerend->energy[block_m_idx], MASA_FREQUENCY_BANDS );
    7306             : 
    7307      156720 :         for ( ts = mrange[0]; ts < mrange[1]; ts++ )
    7308             :         {
    7309      318272 :             for ( i = 0; i < numAnalysisChannels; i++ )
    7310             :             {
    7311      192896 :                 cldfbAnalysis_ts( &( tmpBuffer[i][l_ts * ts] ), Chan_RealBuffer[i], Chan_ImagBuffer[i], l_ts, masaInput->hMasaPrerend->cldfbAnaEnc[i] );
    7312             :             }
    7313             : 
    7314             :             /* Compute channel energy for metadata processing */
    7315     3134400 :             for ( band_m_idx = 0; band_m_idx < MASA_FREQUENCY_BANDS; band_m_idx++ )
    7316             :             {
    7317     3009024 :                 brange[0] = MASA_band_grouping_24[band_m_idx];
    7318     3009024 :                 brange[1] = MASA_band_grouping_24[band_m_idx + 1];
    7319    10531584 :                 for ( j = brange[0]; j < brange[1]; j++ )
    7320             :                 {
    7321    19096320 :                     for ( i = 0; i < numAnalysisChannels; i++ )
    7322             :                     {
    7323    11573760 :                         masaInput->hMasaPrerend->energy[block_m_idx][band_m_idx] += Chan_RealBuffer[0][j] * Chan_RealBuffer[0][j] + Chan_ImagBuffer[0][j] * Chan_ImagBuffer[0][j];
    7324             :                     }
    7325             :                 }
    7326             :             }
    7327             :         }
    7328             :     }
    7329             : 
    7330             :     /* Copy audio channels if mismatch in number of transports */
    7331        7836 :     if ( masaInput->base.inputBuffer.config.numChannels == 1 && outAudio.config.numChannels == 2 )
    7332             :     {
    7333        1808 :         mvr2r( tmpBuffer[0], tmpBuffer[1], masaInput->base.inputBuffer.config.numSamplesPerChannel );
    7334             :     }
    7335        6028 :     else if ( masaInput->base.inputBuffer.config.numChannels == 2 && outAudio.config.numChannels == 1 )
    7336             :     {
    7337        2110 :         v_add( tmpBuffer[0], tmpBuffer[1], tmpBuffer[0], masaInput->base.inputBuffer.config.numSamplesPerChannel );
    7338             :     }
    7339             : 
    7340             :     /* Copy metadata */
    7341        7836 :     outMeta = masaInput->hMasaPrerend->hMasaOut;
    7342        7836 :     inMeta = &masaInput->masaMetadata;
    7343        7836 :     numDirs = inMeta->descriptive_meta.numberOfDirections + 1;
    7344             : 
    7345       39180 :     for ( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
    7346             :     {
    7347      783600 :         for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    7348             :         {
    7349             :             /* Remainder is always set to zero and energy removal is compensated in following steps
    7350             :              * to other ratios. */
    7351      752256 :             inMeta->common_meta.remainder_to_total_ratio[sf][band] = 0.0f;
    7352             : 
    7353      752256 :             ratioSum = 0;
    7354     1909632 :             for ( dir = 0; dir < numDirs; dir++ )
    7355             :             {
    7356     1157376 :                 ratioSum += inMeta->directional_meta[dir].energy_ratio[sf][band];
    7357             :             }
    7358      752256 :             ratioSum += inMeta->common_meta.diffuse_to_total_ratio[sf][band];
    7359             : 
    7360      752256 :             if ( ratioSum == 0.0f )
    7361             :             {
    7362           0 :                 for ( dir = 0; dir < numDirs; dir++ )
    7363             :                 {
    7364           0 :                     inMeta->directional_meta[dir].energy_ratio[sf][band] = 0.0f;
    7365             :                 }
    7366           0 :                 inMeta->common_meta.diffuse_to_total_ratio[sf][band] = 1.0f;
    7367             :             }
    7368      752256 :             else if ( ratioSum != 1.0f )
    7369             :             {
    7370       56772 :                 for ( dir = 0; dir < numDirs; dir++ )
    7371             :                 {
    7372       37848 :                     inMeta->directional_meta[dir].energy_ratio[sf][band] /= ratioSum;
    7373             :                 }
    7374       18924 :                 inMeta->common_meta.diffuse_to_total_ratio[sf][band] /= ratioSum;
    7375             :             }
    7376             :         }
    7377             :     }
    7378             : 
    7379       39180 :     for ( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
    7380             :     {
    7381      783600 :         for ( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
    7382             :         {
    7383      752256 :             outMeta->diffuseToTotalRatio[sf][band] = UINT8_MAX;
    7384     1909632 :             for ( dir = 0; dir < numDirs; dir++ )
    7385             :             {
    7386     1157376 :                 outMeta->directionIndex[dir][sf][band] = index_theta_phi_16( &inMeta->directional_meta[dir].elevation[sf][band], &inMeta->directional_meta[dir].azimuth[sf][band], masaInput->hMasaPrerend->sph_grid16 );
    7387     1157376 :                 outMeta->directToTotalRatio[dir][sf][band] = (uint8_t) floorf( inMeta->directional_meta[dir].energy_ratio[sf][band] * UINT8_MAX );
    7388     1157376 :                 outMeta->diffuseToTotalRatio[sf][band] -= outMeta->directToTotalRatio[dir][sf][band];
    7389     1157376 :                 outMeta->spreadCoherence[dir][sf][band] = (uint8_t) floorf( inMeta->directional_meta[dir].spread_coherence[sf][band] * UINT8_MAX );
    7390             :             }
    7391      752256 :             outMeta->surroundCoherence[sf][band] = (uint8_t) floorf( inMeta->common_meta.surround_coherence[sf][band] * UINT8_MAX );
    7392             :         }
    7393             :     }
    7394             : 
    7395        7836 :     copy_masa_descriptive_meta( &( outMeta->descriptiveMeta ), &( inMeta->descriptive_meta ) );
    7396             : 
    7397        7836 :     accumulate2dArrayToBuffer( tmpBuffer, &outAudio );
    7398             : 
    7399        7836 :     return;
    7400             : }
    7401             : 
    7402             : 
    7403     1055736 : static ivas_error renderInputMasa(
    7404             :     input_masa *masaInput,
    7405             :     const AUDIO_CONFIG outConfig,
    7406             :     IVAS_REND_AudioBuffer outAudio )
    7407             : {
    7408             :     IVAS_REND_AudioBuffer inAudio;
    7409             :     int16_t ch;
    7410             :     int16_t maxBin;
    7411             :     float *tmpBuffer[MAX_OUTPUT_CHANNELS];
    7412             :     float tmpBuffer_buff[MAX_OUTPUT_CHANNELS][L_FRAME48k];
    7413             :     int16_t cldfb2tdSampleFact;
    7414             :     float Cldfb_RealBuffer_Binaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    7415             :     float Cldfb_ImagBuffer_Binaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    7416             : 
    7417     1055736 :     if ( !masaInput->metadataHasBeenFed )
    7418             :     {
    7419           0 :         return IVAS_ERR_MISSING_METADATA;
    7420             :     }
    7421             : 
    7422     1055736 :     inAudio = masaInput->base.inputBuffer;
    7423     1055736 :     cldfb2tdSampleFact = outAudio.config.is_cldfb ? 2 : 1;
    7424     1055736 :     if ( ( masaInput->base.numNewSamplesPerChannel * cldfb2tdSampleFact != outAudio.config.numSamplesPerChannel ) &&
    7425           0 :          ( outConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ) && ( outConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
    7426             :     {
    7427           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_BUFFER_SIZE, "Mismatch between the number of input samples vs number of requested output samples - currently not allowed" );
    7428             :     }
    7429     1055736 :     masaInput->base.numNewSamplesPerChannel = 0;
    7430             : 
    7431             :     /* Apply input gain to new audio */
    7432     1055736 :     v_multc( inAudio.data, masaInput->base.gain, inAudio.data, inAudio.config.numSamplesPerChannel * inAudio.config.numChannels );
    7433             : 
    7434     1055736 :     maxBin = (int16_t) ( *masaInput->base.ctx.pOutSampleRate * INV_CLDFB_BANDWIDTH );
    7435             : 
    7436             :     /* set combined orientation subframe info to start info */
    7437     1055736 :     ivas_combined_orientation_set_to_start_index( *( masaInput->base.ctx.pCombinedOrientationData ) );
    7438             : 
    7439     1055736 :     if ( getAudioConfigType( outConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_MASA )
    7440             :     {
    7441             :         /* MASA prerendering path for MASA -> MASA */
    7442        7836 :         renderMasaToMasa( masaInput, outAudio );
    7443             :     }
    7444             :     else
    7445             :     {
    7446             :         /* MASA external renderer -> other formats */
    7447             :         int16_t num_subframes;
    7448    17814300 :         for ( ch = 0; ch < MAX_OUTPUT_CHANNELS; ch++ )
    7449             :         {
    7450    16766400 :             tmpBuffer[ch] = tmpBuffer_buff[ch];
    7451             :         }
    7452             : 
    7453     1047900 :         copyBufferTo2dArray( masaInput->base.inputBuffer, tmpBuffer_buff );
    7454             : 
    7455     1047900 :         num_subframes = (int16_t) ( masaInput->base.inputBuffer.config.numSamplesPerChannel / ( *masaInput->base.ctx.pOutSampleRate / ( IVAS_NUM_FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES ) ) );
    7456             : 
    7457     1047900 :         if ( outConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM || outConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED )
    7458           0 :         {
    7459             :             /* split rendering. use the combined of the first subframe in all subframes */
    7460             :             int16_t sf, i, j;
    7461             :             COMBINED_ORIENTATION_HANDLE pCombinedOrientationData;
    7462           0 :             pCombinedOrientationData = *masaInput->base.ctx.pCombinedOrientationData;
    7463           0 :             for ( sf = 1; sf < pCombinedOrientationData->num_subframes; sf++ )
    7464             :             {
    7465           0 :                 pCombinedOrientationData->Quaternions[sf] = pCombinedOrientationData->Quaternions[0];
    7466           0 :                 for ( i = 0; i < 3; i++ )
    7467             :                 {
    7468           0 :                     for ( j = 0; j < 3; j++ )
    7469             :                     {
    7470           0 :                         pCombinedOrientationData->Rmat[sf][i][j] = pCombinedOrientationData->Rmat[0][i][j];
    7471             :                     }
    7472             :                 }
    7473             :             }
    7474             : 
    7475           0 :             copyMasaMetadataToDiracRenderer( &masaInput->masaMetadata, masaInput->hMasaExtRend->hSpatParamRendCom, maxBin );
    7476             : 
    7477           0 :             ivas_masa_ext_rend_parambin_render( masaInput->hMasaExtRend, *masaInput->base.ctx.pCombinedOrientationData, tmpBuffer, num_subframes, masaInput->base.ctx.pSplitRendWrapper, Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural );
    7478             : 
    7479           0 :             accumulateCLDFBArrayToBuffer( Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural, &outAudio );
    7480             :         }
    7481             :         else
    7482             :         {
    7483             :             /* non-split path */
    7484     1047900 :             switch ( masaInput->hMasaExtRend->renderer_type )
    7485             :             {
    7486      574500 :                 case RENDERER_DIRAC:
    7487      574500 :                     copyMasaMetadataToDiracRenderer( &masaInput->masaMetadata, masaInput->hMasaExtRend->hSpatParamRendCom, maxBin );
    7488      574500 :                     ivas_masa_ext_dirac_render( masaInput->hMasaExtRend, tmpBuffer, num_subframes );
    7489      574500 :                     break;
    7490      441840 :                 case RENDERER_STEREO_PARAMETRIC:
    7491             :                 case RENDERER_BINAURAL_PARAMETRIC:
    7492             :                 case RENDERER_BINAURAL_PARAMETRIC_ROOM:
    7493      441840 :                     copyMasaMetadataToDiracRenderer( &masaInput->masaMetadata, masaInput->hMasaExtRend->hSpatParamRendCom, maxBin );
    7494      441840 :                     ivas_masa_ext_rend_parambin_render( masaInput->hMasaExtRend, *masaInput->base.ctx.pCombinedOrientationData, tmpBuffer, num_subframes, NULL, NULL, NULL );
    7495      441840 :                     break;
    7496       31560 :                 case RENDERER_DISABLE:
    7497       31560 :                     break; /* This happens for 1TC MASA to MONO where we just copy input transport to output */
    7498           0 :                 default:
    7499           0 :                     return ( IVAS_ERROR( IVAS_ERR_IO_CONFIG_PAIR_NOT_SUPPORTED, "Wrong output config for MASA input in external renderer\n" ) );
    7500             :             }
    7501             : 
    7502     1047900 :             accumulate2dArrayToBuffer( tmpBuffer_buff, &outAudio );
    7503             :         }
    7504             :     }
    7505             : 
    7506     1055736 :     return IVAS_ERR_OK;
    7507             : }
    7508             : 
    7509             : 
    7510    13740500 : static ivas_error renderActiveInputsMasa(
    7511             :     IVAS_REND_HANDLE hIvasRend,
    7512             :     IVAS_REND_AudioBuffer outAudio )
    7513             : {
    7514             :     int16_t i;
    7515             :     input_masa *pCurrentInput;
    7516             :     ivas_error error;
    7517             : 
    7518    27481000 :     for ( i = 0, pCurrentInput = hIvasRend->inputsMasa; i < RENDERER_MAX_MASA_INPUTS; ++i, ++pCurrentInput )
    7519             :     {
    7520    13740500 :         if ( pCurrentInput->base.inConfig == IVAS_AUDIO_CONFIG_INVALID )
    7521             :         {
    7522             :             /* Skip inactive inputs */
    7523    12684764 :             continue;
    7524             :         }
    7525             : 
    7526     1055736 :         if ( ( error = renderInputMasa( pCurrentInput, hIvasRend->outputConfig, outAudio ) ) != IVAS_ERR_OK )
    7527             :         {
    7528           0 :             return error;
    7529             :         }
    7530             :     }
    7531             : 
    7532    13740500 :     return IVAS_ERR_OK;
    7533             : }
    7534             : 
    7535             : 
    7536             : /*---------------------------------------------------------------------*
    7537             :  * IVAS_REND_GetMasaMetadata( )
    7538             :  *
    7539             :  * Get metadata of the estimated MASA frame
    7540             :  *---------------------------------------------------------------------*/
    7541             : 
    7542           0 : ivas_error IVAS_REND_GetMasaMetadata(
    7543             :     IVAS_REND_HANDLE hIvasRend,                        /* i/o: IVAS renderer handle                                                    */
    7544             :     MASA_DECODER_EXT_OUT_META_HANDLE *hMasaExtOutMeta, /* o  : pointer to handle, which will be set to point to analyzed MASA metadata */
    7545             :     const IVAS_REND_AudioConfigType inputType          /* i  : Input type                                                              */
    7546             : )
    7547             : {
    7548           0 :     if ( hIvasRend == NULL )
    7549             :     {
    7550           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    7551             :     }
    7552             : 
    7553             :     /* Get the metadata handle */
    7554           0 :     if ( inputType == IVAS_REND_AUDIO_CONFIG_TYPE_OBJECT_BASED )
    7555             :     {
    7556           0 :         *hMasaExtOutMeta = hIvasRend->inputsIsm->hOMasa->hMasaOut;
    7557             :     }
    7558           0 :     else if ( inputType == IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED )
    7559             :     {
    7560           0 :         *hMasaExtOutMeta = hIvasRend->inputsMc->hMcMasa->hMasaOut;
    7561             :     }
    7562           0 :     else if ( inputType == IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS )
    7563             :     {
    7564           0 :         *hMasaExtOutMeta = hIvasRend->inputsSba->hDirAC->hMasaOut;
    7565             :     }
    7566             :     else
    7567             :     {
    7568           0 :         return IVAS_ERR_NOT_SUPPORTED_OPTION;
    7569             :     }
    7570             : 
    7571           0 :     return IVAS_ERR_OK;
    7572             : }
    7573             : 
    7574             : 
    7575             : /*---------------------------------------------------------------------*
    7576             :  * IVAS_REND_MergeMasaMetadata( )
    7577             :  *
    7578             :  * Merge MASA metadata from two formats
    7579             :  *---------------------------------------------------------------------*/
    7580             : 
    7581        9000 : ivas_error IVAS_REND_MergeMasaMetadata(
    7582             :     IVAS_REND_HANDLE hIvasRend,                        /* i/o: IVAS renderer handle                                             */
    7583             :     MASA_DECODER_EXT_OUT_META_HANDLE *hMasaExtOutMeta, /* o  : pointer to handle, which will be set to point to merged metadata */
    7584             :     const IVAS_REND_AudioConfigType inputType1,        /* i  : Input type 1                                                     */
    7585             :     const IVAS_REND_AudioConfigType inputType2         /* i  : Input type 2                                                     */
    7586             : )
    7587             : {
    7588             :     MASA_DECODER_EXT_OUT_META_HANDLE inMeta2;
    7589             :     float( *inEne1 )[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS];
    7590             :     float( *inEne2 )[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS];
    7591             : 
    7592        9000 :     if ( hIvasRend == NULL )
    7593             :     {
    7594           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    7595             :     }
    7596             : 
    7597             :     /* Input1 metadata and energy */
    7598        9000 :     if ( inputType1 == IVAS_REND_AUDIO_CONFIG_TYPE_OBJECT_BASED )
    7599             :     {
    7600           0 :         *hMasaExtOutMeta = hIvasRend->inputsIsm->hOMasa->hMasaOut;
    7601           0 :         inEne1 = &( hIvasRend->inputsIsm->hOMasa->energy );
    7602             :     }
    7603        9000 :     else if ( inputType1 == IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED )
    7604             :     {
    7605           0 :         *hMasaExtOutMeta = hIvasRend->inputsMc->hMcMasa->hMasaOut;
    7606           0 :         inEne1 = &( hIvasRend->inputsMc->hMcMasa->energy );
    7607             :     }
    7608        9000 :     else if ( inputType1 == IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS )
    7609             :     {
    7610        1800 :         *hMasaExtOutMeta = hIvasRend->inputsSba->hDirAC->hMasaOut;
    7611        1800 :         inEne1 = &( hIvasRend->inputsSba->hDirAC->energy );
    7612             :     }
    7613        7200 :     else if ( inputType1 == IVAS_REND_AUDIO_CONFIG_TYPE_MASA )
    7614             :     {
    7615        7200 :         *hMasaExtOutMeta = hIvasRend->inputsMasa->hMasaPrerend->hMasaOut;
    7616        7200 :         inEne1 = &( hIvasRend->inputsMasa->hMasaPrerend->energy );
    7617             :     }
    7618             :     else
    7619             :     {
    7620           0 :         return IVAS_ERR_NOT_SUPPORTED_OPTION;
    7621             :     }
    7622             : 
    7623             :     /* Input2 metadata and energy */
    7624        9000 :     if ( inputType2 == IVAS_REND_AUDIO_CONFIG_TYPE_OBJECT_BASED )
    7625             :     {
    7626        7800 :         inMeta2 = hIvasRend->inputsIsm->hOMasa->hMasaOut;
    7627        7800 :         inEne2 = &( hIvasRend->inputsIsm->hOMasa->energy );
    7628             :     }
    7629        1200 :     else if ( inputType2 == IVAS_REND_AUDIO_CONFIG_TYPE_CHANNEL_BASED )
    7630             :     {
    7631         600 :         inMeta2 = hIvasRend->inputsMc->hMcMasa->hMasaOut;
    7632         600 :         inEne2 = &( hIvasRend->inputsMc->hMcMasa->energy );
    7633             :     }
    7634         600 :     else if ( inputType2 == IVAS_REND_AUDIO_CONFIG_TYPE_AMBISONICS )
    7635             :     {
    7636           0 :         inMeta2 = hIvasRend->inputsSba->hDirAC->hMasaOut;
    7637           0 :         inEne2 = &( hIvasRend->inputsSba->hDirAC->energy );
    7638             :     }
    7639         600 :     else if ( inputType2 == IVAS_REND_AUDIO_CONFIG_TYPE_MASA )
    7640             :     {
    7641         600 :         inMeta2 = hIvasRend->inputsMasa->hMasaPrerend->hMasaOut;
    7642         600 :         inEne2 = &( hIvasRend->inputsMasa->hMasaPrerend->energy );
    7643             :     }
    7644             :     else
    7645             :     {
    7646           0 :         return IVAS_ERR_NOT_SUPPORTED_OPTION;
    7647             :     }
    7648             : 
    7649             :     /* Merge metadata */
    7650        9000 :     ivas_prerend_merge_masa_metadata( *hMasaExtOutMeta, *hMasaExtOutMeta, inputType1, *inEne1, inMeta2, inputType2, *inEne2 );
    7651        9000 :     ( *hMasaExtOutMeta )->descriptiveMeta.numberOfChannels = hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_MASA1 ? 0u : 1u;
    7652             : 
    7653        9000 :     return IVAS_ERR_OK;
    7654             : }
    7655             : 
    7656             : 
    7657             : /*---------------------------------------------------------------------*
    7658             :  * IVAS_REND_SetTotalNumberOfObjects( )
    7659             :  *
    7660             :  * Set the total number of objects to the first object data
    7661             :  *---------------------------------------------------------------------*/
    7662             : 
    7663        5346 : ivas_error IVAS_REND_SetTotalNumberOfObjects(
    7664             :     IVAS_REND_HANDLE hIvasRend,      /* i/o: IVAS renderer handle    */
    7665             :     const uint16_t total_num_objects /* i  : total number of objects */
    7666             : )
    7667             : {
    7668        5346 :     if ( hIvasRend == NULL )
    7669             :     {
    7670           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    7671             :     }
    7672             : 
    7673        5346 :     hIvasRend->inputsIsm[0].total_num_objects = total_num_objects;
    7674             : 
    7675        5346 :     return IVAS_ERR_OK;
    7676             : }
    7677             : 
    7678             : 
    7679             : /*---------------------------------------------------------------------*
    7680             :  * IVAS_REND_SetIsmMetadataDelay( )
    7681             :  *
    7682             :  * Set the Metadata Delay in ms in order to sync with audio delay
    7683             :  *---------------------------------------------------------------------*/
    7684             : 
    7685        5346 : ivas_error IVAS_REND_SetIsmMetadataDelay(
    7686             :     IVAS_REND_HANDLE hIvasRend, /* i/o: IVAS renderer handle    */
    7687             :     const float sync_md_delay   /* i  : ISM Metadata Delay in ms to sync with audio delay   */
    7688             : )
    7689             : {
    7690             :     int16_t i;
    7691             : 
    7692        5346 :     if ( hIvasRend == NULL )
    7693             :     {
    7694           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    7695             :     }
    7696             : 
    7697       26730 :     for ( i = 0; i < RENDERER_MAX_ISM_INPUTS; ++i )
    7698             :     {
    7699       21384 :         hIvasRend->inputsIsm[i].ism_metadata_delay_ms = sync_md_delay;
    7700             :     }
    7701             : 
    7702        5346 :     return IVAS_ERR_OK;
    7703             : }
    7704             : 
    7705             : 
    7706             : /*-------------------------------------------------------------------*
    7707             :  * getSamplesInternal()
    7708             :  *
    7709             :  *
    7710             :  *-------------------------------------------------------------------*/
    7711             : 
    7712    13740500 : static ivas_error getSamplesInternal(
    7713             :     IVAS_REND_HANDLE hIvasRend,    /* i/o: Renderer handle          */
    7714             :     IVAS_REND_AudioBuffer outAudio /* i/o: buffer for output audio  */
    7715             : )
    7716             : {
    7717             :     ivas_error error;
    7718             :     int16_t numOutChannels;
    7719             :     int16_t cldfb2tdSampleFact;
    7720             : 
    7721             :     /* Validate function arguments */
    7722    13740500 :     if ( hIvasRend == NULL || outAudio.data == NULL )
    7723             :     {
    7724           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    7725             :     }
    7726             : 
    7727    13740500 :     cldfb2tdSampleFact = ( outAudio.config.is_cldfb ) ? 2 : 1;
    7728             : 
    7729    13740500 :     if ( outAudio.config.numSamplesPerChannel <= 0 || ( L_FRAME_MAX < outAudio.config.numSamplesPerChannel && outAudio.config.is_cldfb == 0 ) ||
    7730    13740500 :          ( ( L_FRAME_MAX * cldfb2tdSampleFact ) < outAudio.config.numSamplesPerChannel && outAudio.config.is_cldfb == 1 ) )
    7731             :     {
    7732           0 :         return IVAS_ERR_INVALID_BUFFER_SIZE;
    7733             :     }
    7734             : 
    7735    13740500 :     if ( outAudio.config.numChannels <= 0 || MAX_OUTPUT_CHANNELS < outAudio.config.numChannels )
    7736             :     {
    7737           0 :         return IVAS_ERR_WRONG_NUM_CHANNELS;
    7738             :     }
    7739             : 
    7740    13740500 :     if ( getAudioConfigType( hIvasRend->outputConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_BINAURAL &&
    7741     4683816 :          hIvasRend->outputConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED &&
    7742     4683816 :          hIvasRend->outputConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM &&
    7743     4683816 :          ( outAudio.config.numSamplesPerChannel * 1000 / cldfb2tdSampleFact ) != ( hIvasRend->num_subframes * BINAURAL_RENDERING_FRAME_SIZE_MS ) * hIvasRend->sampleRateOut )
    7744             :     {
    7745           0 :         return IVAS_ERROR( IVAS_ERR_INVALID_BUFFER_SIZE, "Binaural rendering requires specific frame size" );
    7746             :     }
    7747             : 
    7748             :     /* Check that there is allowed configuration for MASA format output */
    7749    13740500 :     if ( getAudioConfigType( hIvasRend->outputConfig ) == IVAS_REND_AUDIO_CONFIG_TYPE_MASA )
    7750             :     {
    7751             :         int16_t i;
    7752        7836 :         int16_t numMasaInputs = 0;
    7753        7836 :         int16_t numOtherInputs = 0;
    7754             : 
    7755       15672 :         for ( i = 0; i < RENDERER_MAX_MASA_INPUTS; i++ )
    7756             :         {
    7757        7836 :             numMasaInputs += hIvasRend->inputsMasa[i].base.inConfig == IVAS_AUDIO_CONFIG_INVALID ? 0 : 1;
    7758             :         }
    7759             : 
    7760       15672 :         for ( i = 0; i < RENDERER_MAX_MC_INPUTS; i++ )
    7761             :         {
    7762        7836 :             numOtherInputs += hIvasRend->inputsMc[i].base.inConfig == IVAS_AUDIO_CONFIG_INVALID ? 0 : 1;
    7763             :         }
    7764             : 
    7765       15672 :         for ( i = 0; i < RENDERER_MAX_SBA_INPUTS; i++ )
    7766             :         {
    7767        7836 :             numOtherInputs += hIvasRend->inputsSba[i].base.inConfig == IVAS_AUDIO_CONFIG_INVALID ? 0 : 1;
    7768             :         }
    7769             : 
    7770             :         /* For ISM, we check only first as all ISMs are handled together via OMASA when merging to MASA. */
    7771        7836 :         numOtherInputs += hIvasRend->inputsIsm[0].base.inConfig == IVAS_AUDIO_CONFIG_INVALID ? 0 : 1;
    7772             : 
    7773        7836 :         if ( numMasaInputs == 0 || numOtherInputs == 0 )
    7774             :         {
    7775           0 :             return IVAS_ERR_IO_CONFIG_PAIR_NOT_SUPPORTED;
    7776             :         }
    7777             :     }
    7778             : 
    7779    13740500 :     if ( ( error = IVAS_REND_GetNumOutChannels( hIvasRend, &numOutChannels ) ) != IVAS_ERR_OK )
    7780             :     {
    7781           0 :         return error;
    7782             :     }
    7783             : 
    7784    13740500 :     if ( numOutChannels != outAudio.config.numChannels &&
    7785           0 :          hIvasRend->outputConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED &&
    7786           0 :          hIvasRend->outputConfig != IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    7787             :     {
    7788           0 :         return IVAS_ERR_WRONG_NUM_CHANNELS;
    7789             :     }
    7790             : 
    7791             :     /* Clear output buffer */
    7792    13740500 :     set_zero( outAudio.data, outAudio.config.numChannels * outAudio.config.numSamplesPerChannel );
    7793             : 
    7794    13740500 :     if ( ( error = renderActiveInputsIsm( hIvasRend, outAudio ) ) != IVAS_ERR_OK )
    7795             :     {
    7796           0 :         return error;
    7797             :     }
    7798             : 
    7799    13740500 :     if ( ( error = renderActiveInputsMc( hIvasRend, outAudio ) ) != IVAS_ERR_OK )
    7800             :     {
    7801           0 :         return error;
    7802             :     }
    7803             : 
    7804    13740500 :     if ( ( error = renderActiveInputsSba( hIvasRend, outAudio ) ) != IVAS_ERR_OK )
    7805             :     {
    7806           0 :         return error;
    7807             :     }
    7808             : 
    7809    13740500 :     if ( ( error = renderActiveInputsMasa( hIvasRend, outAudio ) ) != IVAS_ERR_OK )
    7810             :     {
    7811           0 :         return error;
    7812             :     }
    7813             : 
    7814    13740500 :     return IVAS_ERR_OK;
    7815             : }
    7816             : 
    7817             : 
    7818             : /*-------------------------------------------------------------------*
    7819             :  * IVAS_REND_GetSamples()
    7820             :  *
    7821             :  *
    7822             :  *-------------------------------------------------------------------*/
    7823             : 
    7824    13740500 : ivas_error IVAS_REND_GetSamples(
    7825             :     IVAS_REND_HANDLE hIvasRend,    /* i/o: Renderer handle          */
    7826             :     IVAS_REND_AudioBuffer outAudio /* i/o: buffer for output audio  */
    7827             : )
    7828             : {
    7829             :     ivas_error error;
    7830             : 
    7831    13740500 :     if ( ( error = getSamplesInternal( hIvasRend, outAudio ) ) != IVAS_ERR_OK )
    7832             :     {
    7833           0 :         return error;
    7834             :     }
    7835             : 
    7836    13740500 :     if ( outAudio.config.is_cldfb == 0 )
    7837             :     {
    7838             : #ifndef DISABLE_LIMITER
    7839             : #ifdef DEBUGGING
    7840             :         hIvasRend->numClipping +=
    7841             : #endif
    7842    13740500 :             limitRendererOutput( hIvasRend->hLimiter, outAudio.data, outAudio.config.numSamplesPerChannel, IVAS_LIMITER_THRESHOLD );
    7843             : #endif
    7844             :     }
    7845             : 
    7846             :     /* update global cominbed orientation start index */
    7847    13740500 :     ivas_combined_orientation_update_start_index( hIvasRend->hCombinedOrientationData, outAudio.config.numSamplesPerChannel );
    7848             : 
    7849    13740500 :     return IVAS_ERR_OK;
    7850             : }
    7851             : 
    7852             : 
    7853             : /*-------------------------------------------------------------------*
    7854             :  * IVAS_REND_GetSplitBinauralBitstream()
    7855             :  *
    7856             :  *
    7857             :  *-------------------------------------------------------------------*/
    7858             : 
    7859           0 : ivas_error IVAS_REND_GetSplitBinauralBitstream(
    7860             :     IVAS_REND_HANDLE hIvasRend,      /* i/o: Renderer handle             */
    7861             :     IVAS_REND_AudioBuffer outAudio,  /* i/o: buffer for output audio     */
    7862             :     IVAS_REND_BitstreamBuffer *hBits /* o  : buffer for output bitstream */
    7863             : )
    7864             : {
    7865             :     ivas_error error;
    7866             :     int16_t ch;
    7867             :     int16_t cldfb_in_flag;
    7868             :     int16_t i, ro_md_flag;
    7869             :     int16_t num_poses_orig;
    7870             :     float *tmpBinaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS], tmpBinaural_buff[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][L_FRAME48k];
    7871             :     float Cldfb_ImagBuffer_Binaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    7872             :     float Cldfb_RealBuffer_Binaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX];
    7873             :     IVAS_REND_AudioBufferConfig *pSplitEncBufConfig;
    7874             :     ISAR_SPLIT_REND_CONFIG_HANDLE pSplitRendConfig;
    7875             :     ISAR_SPLIT_REND_BITS_DATA bits;
    7876             : 
    7877           0 :     for ( ch = 0; ch < MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS; ch++ )
    7878             :     {
    7879           0 :         tmpBinaural[ch] = tmpBinaural_buff[ch];
    7880             :     }
    7881             : 
    7882           0 :     cldfb_in_flag = getCldfbRendFlag( hIvasRend, IVAS_REND_AUDIO_CONFIG_TYPE_UNKNOWN );
    7883           0 :     pSplitEncBufConfig = &hIvasRend->splitRendEncBuffer.config;
    7884           0 :     pSplitRendConfig = &hIvasRend->hRendererConfig->split_rend_config;
    7885             : 
    7886             :     /* 0 DoF / No pose correction retains frame size */
    7887           0 :     pSplitEncBufConfig->is_cldfb = cldfb_in_flag;
    7888           0 :     if ( pSplitRendConfig->dof == 0 || pSplitRendConfig->poseCorrectionMode == ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE )
    7889             :     {
    7890           0 :         pSplitEncBufConfig->numSamplesPerChannel = outAudio.config.numSamplesPerChannel;
    7891             :     }
    7892             :     /* Pose correction requires 20ms */
    7893             :     else
    7894             :     {
    7895           0 :         pSplitEncBufConfig->numSamplesPerChannel = (int16_t) ( hIvasRend->sampleRateOut / FRAMES_PER_SEC );
    7896             :     }
    7897           0 :     pSplitEncBufConfig->numSamplesPerChannel *= cldfb_in_flag ? 2 : 1;
    7898             : 
    7899           0 :     num_poses_orig = hIvasRend->splitRendWrapper->multiBinPoseData.num_poses;
    7900           0 :     ISAR_PRE_REND_GetMultiBinPoseData( pSplitRendConfig,
    7901           0 :                                        &hIvasRend->splitRendWrapper->multiBinPoseData,
    7902             :                                        hIvasRend->headRotData.sr_pose_pred_axis );
    7903           0 :     assert( num_poses_orig == hIvasRend->splitRendWrapper->multiBinPoseData.num_poses && "number of poses should not change dynamically" );
    7904             : 
    7905             :     /* hIvasRend->splitRendEncBuffer contains multi-pose data for BINAURAL_SPLIT_CODED output
    7906             :        outAudio used later for main pose BINAURAL_SPLIT_PCM output */
    7907           0 :     if ( ( error = getSamplesInternal( hIvasRend, hIvasRend->splitRendEncBuffer ) ) != IVAS_ERR_OK )
    7908             :     {
    7909           0 :         return error;
    7910             :     }
    7911             : 
    7912             :     /* copy outputs */
    7913           0 :     if ( hIvasRend->splitRendEncBuffer.config.is_cldfb == 1 )
    7914             :     {
    7915           0 :         cldfb_in_flag = 1;
    7916           0 :         copyBufferToCLDFBarray( hIvasRend->splitRendEncBuffer, Cldfb_RealBuffer_Binaural, Cldfb_ImagBuffer_Binaural );
    7917             :     }
    7918             :     else
    7919             :     {
    7920           0 :         cldfb_in_flag = 0;
    7921           0 :         copyBufferTo2dArray( hIvasRend->splitRendEncBuffer, tmpBinaural_buff );
    7922             :     }
    7923             : 
    7924             :     /* Encode split rendering bitstream */
    7925           0 :     convertBitsBufferToInternalBitsBuff( *hBits, &bits );
    7926             : 
    7927           0 :     ro_md_flag = 0;
    7928           0 :     for ( i = 0; i < RENDERER_MAX_ISM_INPUTS; ++i )
    7929             :     {
    7930           0 :         if ( hIvasRend->inputsIsm[i].base.inConfig != IVAS_AUDIO_CONFIG_INVALID )
    7931             :         {
    7932           0 :             ro_md_flag = 1;
    7933           0 :             break;
    7934             :         }
    7935             :     }
    7936             : 
    7937           0 :     if ( ( error = ISAR_PRE_REND_MultiBinToSplitBinaural( hIvasRend->splitRendWrapper,
    7938             :                                                           hIvasRend->headRotData.headPositions[0],
    7939             :                                                           pSplitRendConfig->splitRendBitRate,
    7940             :                                                           pSplitRendConfig->codec,
    7941           0 :                                                           pSplitRendConfig->isar_frame_size_ms,
    7942           0 :                                                           pSplitRendConfig->codec_frame_size_ms,
    7943             :                                                           &bits,
    7944             :                                                           Cldfb_RealBuffer_Binaural,
    7945             :                                                           Cldfb_ImagBuffer_Binaural,
    7946           0 :                                                           ( const int16_t )( ( BINAURAL_MAXBANDS * hIvasRend->sampleRateOut ) / 48000 ),
    7947             :                                                           tmpBinaural,
    7948             :                                                           1,
    7949             :                                                           cldfb_in_flag,
    7950           0 :                                                           ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0,
    7951             :                                                           ro_md_flag ) ) != IVAS_ERR_OK )
    7952             :     {
    7953           0 :         return error;
    7954             :     }
    7955             : 
    7956           0 :     convertInternalBitsBuffToBitsBuffer( hBits, bits );
    7957             : 
    7958             :     /* copy over first pose data to outAudio */
    7959           0 :     if ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    7960             :     {
    7961             :         /* set outAudio to zero - getSamplesInternal only cleared splitRendEncBuffer */
    7962           0 :         set_zero( outAudio.data, outAudio.config.numChannels * outAudio.config.numSamplesPerChannel );
    7963           0 :         accumulate2dArrayToBuffer( tmpBinaural_buff, &outAudio );
    7964             :     }
    7965             : 
    7966           0 :     if ( outAudio.config.is_cldfb == 0 )
    7967             :     {
    7968             : #ifndef DISABLE_LIMITER
    7969             : #ifdef DEBUGGING
    7970             :         hIvasRend->numClipping +=
    7971             : #endif
    7972           0 :             limitRendererOutput( hIvasRend->hLimiter, outAudio.data, outAudio.config.numSamplesPerChannel, IVAS_LIMITER_THRESHOLD );
    7973             : #endif
    7974             :     }
    7975             : 
    7976             :     /* update global cominbed orientation start index */
    7977           0 :     ivas_combined_orientation_update_start_index( hIvasRend->hCombinedOrientationData, outAudio.config.numSamplesPerChannel );
    7978             : 
    7979           0 :     return IVAS_ERR_OK;
    7980             : }
    7981             : 
    7982             : 
    7983             : /*-------------------------------------------------------------------*
    7984             :  * IVAS_REND_GetSplitRendBitstreamHeader()
    7985             :  *
    7986             :  *
    7987             :  *-------------------------------------------------------------------*/
    7988             : 
    7989           0 : ivas_error IVAS_REND_GetSplitRendBitstreamHeader(
    7990             :     IVAS_REND_HANDLE hIvasRend,                           /* i/o: IVAS renderer handle                    */
    7991             :     ISAR_SPLIT_REND_CODEC *pCodec,                        /* o  : pointer to codec setting                */
    7992             :     ISAR_SPLIT_REND_POSE_CORRECTION_MODE *poseCorrection, /* o  : pointer to pose correction mode         */
    7993             :     int16_t *pCodec_frame_size_ms,                        /* o  : pointer to codec frame size setting     */
    7994             :     int16_t *pIsar_frame_size_ms                          /* o  : pointer to ISAR frame size setting      */
    7995             : )
    7996             : {
    7997           0 :     if ( hIvasRend == NULL || hIvasRend->hRendererConfig == NULL )
    7998             :     {
    7999           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    8000             :     }
    8001             : 
    8002           0 :     *pCodec = hIvasRend->hRendererConfig->split_rend_config.codec;
    8003           0 :     *pCodec_frame_size_ms = hIvasRend->hRendererConfig->split_rend_config.codec_frame_size_ms;
    8004           0 :     *pIsar_frame_size_ms = hIvasRend->hRendererConfig->split_rend_config.isar_frame_size_ms;
    8005           0 :     *poseCorrection = hIvasRend->hRendererConfig->split_rend_config.poseCorrectionMode;
    8006             : 
    8007           0 :     return IVAS_ERR_OK;
    8008             : }
    8009             : 
    8010             : 
    8011             : /*-------------------------------------------------------------------*
    8012             :  * IVAS_REND_Close()
    8013             :  *
    8014             :  *
    8015             :  *-------------------------------------------------------------------*/
    8016             : 
    8017        8382 : void IVAS_REND_Close(
    8018             :     IVAS_REND_HANDLE *phIvasRend /* i/o: Pointer to renderer handle */
    8019             : )
    8020             : {
    8021             :     uint16_t i;
    8022             :     IVAS_REND_HANDLE hIvasRend;
    8023             : 
    8024             :     /* Validate function arguments */
    8025        8382 :     if ( phIvasRend == NULL || *phIvasRend == NULL )
    8026             :     {
    8027           0 :         return;
    8028             :     }
    8029        8382 :     hIvasRend = *phIvasRend;
    8030             : 
    8031        8382 :     if ( hIvasRend->efapOutWrapper.hEfap != NULL )
    8032             :     {
    8033        5982 :         efap_free_data( &hIvasRend->efapOutWrapper.hEfap );
    8034             :     }
    8035             : 
    8036             :     /* clear inputs */
    8037       41910 :     for ( i = 0; i < RENDERER_MAX_ISM_INPUTS; ++i )
    8038             :     {
    8039       33528 :         clearInputIsm( &hIvasRend->inputsIsm[i] );
    8040             :     }
    8041       16764 :     for ( i = 0; i < RENDERER_MAX_MC_INPUTS; ++i )
    8042             :     {
    8043        8382 :         clearInputMc( &hIvasRend->inputsMc[i] );
    8044             :     }
    8045       16764 :     for ( i = 0; i < RENDERER_MAX_SBA_INPUTS; ++i )
    8046             :     {
    8047        8382 :         clearInputSba( &hIvasRend->inputsSba[i] );
    8048        8382 :         hIvasRend->hHrtfs.hHrtfFastConv = NULL;
    8049             :     }
    8050       16764 :     for ( i = 0; i < RENDERER_MAX_MASA_INPUTS; ++i )
    8051             :     {
    8052        8382 :         clearInputMasa( &hIvasRend->inputsMasa[i] );
    8053             :     }
    8054             : 
    8055             :     /* clear Config. Renderer */
    8056        8382 :     ivas_render_config_close( &( hIvasRend->hRendererConfig ) );
    8057             : 
    8058        8382 :     ivas_limiter_close( &hIvasRend->hLimiter );
    8059             : 
    8060             :     /* Split binaural rendering */
    8061        8382 :     if ( hIvasRend->splitRendWrapper != NULL )
    8062             :     {
    8063           0 :         ISAR_PRE_REND_close( hIvasRend->splitRendWrapper, &hIvasRend->splitRendEncBuffer );
    8064           0 :         free( hIvasRend->splitRendWrapper );
    8065           0 :         hIvasRend->splitRendWrapper = NULL;
    8066             :     }
    8067             : 
    8068        8382 :     closeHeadRotation( hIvasRend );
    8069             : 
    8070        8382 :     ivas_external_orientation_close( &hIvasRend->hExternalOrientationData );
    8071        8382 :     ivas_combined_orientation_close( &hIvasRend->hCombinedOrientationData );
    8072             : 
    8073             :     /* Fastconv HRTF memories */
    8074        8382 :     ivas_binaural_hrtf_close( &hIvasRend->hHrtfs.hHrtfFastConv );
    8075             : 
    8076             :     /* Parametric binauralizer HRTF filters */
    8077        8382 :     ivas_HRTF_binary_close( &( hIvasRend->hHrtfs.hHrtfTD ) );
    8078        8382 :     ivas_HRTF_CRend_binary_close( &( hIvasRend->hHrtfs.hSetOfHRTF ) );
    8079        8382 :     ivas_HRTF_fastconv_binary_close( &( hIvasRend->hHrtfs.hHrtfFastConv ) );
    8080        8382 :     ivas_HRTF_parambin_binary_close( &( hIvasRend->hHrtfs.hHrtfParambin ) );
    8081        8382 :     ivas_HRTF_statistics_close( &( hIvasRend->hHrtfs.hHrtfStatistics ) );
    8082             : 
    8083        8382 :     free( hIvasRend );
    8084        8382 :     *phIvasRend = NULL;
    8085             : 
    8086        8382 :     return;
    8087             : }
    8088             : 
    8089             : 
    8090             : /*-------------------------------------------------------------------*
    8091             :  * IVAS_REND_openCldfb()
    8092             :  *
    8093             :  *
    8094             :  *-------------------------------------------------------------------*/
    8095             : 
    8096           0 : ivas_error IVAS_REND_openCldfb(
    8097             :     IVAS_CLDFB_FILTER_BANK_HANDLE cldfbAna[RENDERER_MAX_INPUT_CHANNELS],
    8098             :     IVAS_CLDFB_FILTER_BANK_HANDLE cldfbSyn[RENDERER_MAX_OUTPUT_CHANNELS],
    8099             :     const int16_t num_in_chs,
    8100             :     const int16_t num_out_chs,
    8101             :     const int32_t output_Fs )
    8102             : {
    8103             :     int16_t n;
    8104             :     ivas_error error;
    8105             : 
    8106           0 :     for ( n = 0; n < num_in_chs; n++ )
    8107             :     {
    8108           0 :         if ( ( error = openCldfb( &( cldfbAna[n] ), CLDFB_ANALYSIS, output_Fs, CLDFB_PROTOTYPE_5_00MS ) ) != IVAS_ERR_OK )
    8109             :         {
    8110           0 :             return error;
    8111             :         }
    8112             :     }
    8113           0 :     for ( ; n < RENDERER_MAX_INPUT_CHANNELS; n++ )
    8114             :     {
    8115           0 :         cldfbAna[n] = NULL;
    8116             :     }
    8117             : 
    8118           0 :     for ( n = 0; n < num_out_chs; n++ )
    8119             :     {
    8120           0 :         if ( ( error = openCldfb( &( cldfbSyn[n] ), CLDFB_SYNTHESIS, output_Fs, CLDFB_PROTOTYPE_5_00MS ) ) != IVAS_ERR_OK )
    8121             :         {
    8122           0 :             return error;
    8123             :         }
    8124             :     }
    8125           0 :     for ( ; n < RENDERER_MAX_OUTPUT_CHANNELS; n++ )
    8126             :     {
    8127           0 :         cldfbSyn[n] = NULL;
    8128             :     }
    8129             : 
    8130           0 :     return IVAS_ERR_OK;
    8131             : }
    8132             : 
    8133             : 
    8134             : /*-------------------------------------------------------------------*
    8135             :  * IVAS_REND_closeCldfb()
    8136             :  *
    8137             :  *
    8138             :  *-------------------------------------------------------------------*/
    8139             : 
    8140           0 : void IVAS_REND_closeCldfb(
    8141             :     IVAS_CLDFB_FILTER_BANK_HANDLE cldfbAna[RENDERER_MAX_INPUT_CHANNELS],
    8142             :     IVAS_CLDFB_FILTER_BANK_HANDLE cldfbSyn[RENDERER_MAX_OUTPUT_CHANNELS] )
    8143             : {
    8144             :     int16_t n;
    8145             : 
    8146           0 :     for ( n = 0; n < RENDERER_MAX_INPUT_CHANNELS; n++ )
    8147             :     {
    8148           0 :         if ( cldfbAna[n] != NULL )
    8149             :         {
    8150           0 :             deleteCldfb( &( cldfbAna[n] ) );
    8151           0 :             cldfbAna[n] = NULL;
    8152             :         }
    8153             :     }
    8154             : 
    8155           0 :     for ( n = 0; n < RENDERER_MAX_OUTPUT_CHANNELS; n++ )
    8156             :     {
    8157           0 :         if ( cldfbSyn[n] != NULL )
    8158             :         {
    8159           0 :             deleteCldfb( &( cldfbSyn[n] ) );
    8160           0 :             cldfbSyn[n] = NULL;
    8161             :         }
    8162             :     }
    8163             : 
    8164           0 :     return;
    8165             : }
    8166             : 
    8167             : 
    8168             : /*-------------------------------------------------------------------*
    8169             :  * IVAS_REND_cldfbSynthesis_wrapper()
    8170             :  *
    8171             :  *
    8172             :  *-------------------------------------------------------------------*/
    8173             : 
    8174           0 : void IVAS_REND_cldfbAnalysis_ts_wrapper(
    8175             :     const float *timeIn,                          /* i  : time buffer         */
    8176             :     float realBuffer[IVAS_CLDFB_NO_CHANNELS_MAX], /* o  : real value buffer   */
    8177             :     float imagBuffer[IVAS_CLDFB_NO_CHANNELS_MAX], /* o  : imag value buffer   */
    8178             :     const int16_t samplesToProcess,               /* i  : samples to process  */
    8179             :     IVAS_CLDFB_FILTER_BANK_HANDLE h_cldfb         /* i  : filterbank state    */
    8180             : )
    8181             : {
    8182           0 :     cldfbAnalysis_ts( timeIn, realBuffer, imagBuffer, samplesToProcess, h_cldfb );
    8183             : 
    8184           0 :     return;
    8185             : }
    8186             : 
    8187             : 
    8188             : /*-------------------------------------------------------------------*
    8189             :  * IVAS_REND_cldfbSynthesis_wrapper()
    8190             :  *
    8191             :  *
    8192             :  *-------------------------------------------------------------------*/
    8193             : 
    8194           0 : void IVAS_REND_cldfbSynthesis_wrapper(
    8195             :     float **realBuffer,                   /* i  : real values                 */
    8196             :     float **imagBuffer,                   /* i  : imag values                 */
    8197             :     float *timeOut,                       /* o  : output time domain samples  */
    8198             :     const int16_t samplesToProcess,       /* i  : number of processed samples */
    8199             :     IVAS_CLDFB_FILTER_BANK_HANDLE h_cldfb /* i  : filter bank state           */
    8200             : )
    8201             : {
    8202           0 :     cldfbSynthesis( realBuffer, imagBuffer, timeOut, samplesToProcess, h_cldfb );
    8203             : 
    8204           0 :     return;
    8205             : }
    8206             : 
    8207             : 
    8208             : #ifdef DEBUGGING
    8209             : /*-------------------------------------------------------------------*
    8210             :  * IVAS_REND_GetNoCLipping()
    8211             :  *
    8212             :  *
    8213             :  *-------------------------------------------------------------------*/
    8214             : 
    8215             : int32_t IVAS_REND_GetNoCLipping(
    8216             :     IVAS_REND_CONST_HANDLE hIvasRend )
    8217             : {
    8218             :     return hIvasRend->numClipping;
    8219             : }
    8220             : 
    8221             : int32_t IVAS_REND_GetCntFramesLimited(
    8222             :     IVAS_REND_CONST_HANDLE hIvasRend )
    8223             : {
    8224             :     if ( hIvasRend->hLimiter == NULL )
    8225             :     {
    8226             :         return 0;
    8227             :     }
    8228             : 
    8229             :     return hIvasRend->hLimiter->cnt_frames_limited;
    8230             : }
    8231             : #endif
    8232             : 
    8233             : 
    8234             : /*---------------------------------------------------------------------*
    8235             :  * IVAS_REND_GetHrtfHandle( )
    8236             :  *
    8237             :  *
    8238             :  *---------------------------------------------------------------------*/
    8239             : 
    8240           0 : ivas_error IVAS_REND_GetHrtfHandle(
    8241             :     IVAS_REND_HANDLE hIvasRend,    /* i/o: IVAS renderer handle     */
    8242             :     IVAS_DEC_HRTF_HANDLE **hHrtfTD /* o  : HRTF handle              */
    8243             : )
    8244             : {
    8245           0 :     if ( hIvasRend == NULL || hIvasRend->hHrtfs.hHrtfTD == NULL )
    8246             :     {
    8247           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    8248             :     }
    8249             : 
    8250           0 :     *hHrtfTD = &hIvasRend->hHrtfs.hHrtfTD;
    8251             : 
    8252           0 :     return IVAS_ERR_OK;
    8253             : }
    8254             : 
    8255             : 
    8256             : /*---------------------------------------------------------------------*
    8257             :  * IVAS_REND_GetHrtfCRendHandle( )
    8258             :  *
    8259             :  *
    8260             :  *---------------------------------------------------------------------*/
    8261             : 
    8262           0 : ivas_error IVAS_REND_GetHrtfCRendHandle(
    8263             :     IVAS_REND_HANDLE hIvasRend,             /* i/o: IVAS renderer handle     */
    8264             :     IVAS_DEC_HRTF_CREND_HANDLE **hSetOfHRTF /* o  : Set of HRTF handle       */
    8265             : )
    8266             : {
    8267           0 :     if ( hIvasRend == NULL || hIvasRend->hHrtfs.hSetOfHRTF == NULL )
    8268             :     {
    8269           0 :         return IVAS_ERR_WRONG_PARAMS;
    8270             :     }
    8271             : 
    8272           0 :     *hSetOfHRTF = &hIvasRend->hHrtfs.hSetOfHRTF;
    8273             : 
    8274           0 :     return IVAS_ERR_OK;
    8275             : }
    8276             : 
    8277             : 
    8278             : /*---------------------------------------------------------------------*
    8279             :  * IVAS_REND_GetHrtfFastConvHandle( )
    8280             :  *
    8281             :  *
    8282             :  *---------------------------------------------------------------------*/
    8283             : 
    8284           0 : ivas_error IVAS_REND_GetHrtfFastConvHandle(
    8285             :     IVAS_REND_HANDLE hIvasRend,                   /* i/o: IVAS renderer handle   */
    8286             :     IVAS_DEC_HRTF_FASTCONV_HANDLE **hHrtfFastConv /* o  : FASTCONV HRTF handle   */
    8287             : )
    8288             : {
    8289           0 :     if ( hIvasRend == NULL || hIvasRend->hHrtfs.hHrtfFastConv == NULL )
    8290             :     {
    8291           0 :         return IVAS_ERR_WRONG_PARAMS;
    8292             :     }
    8293             : 
    8294           0 :     *hHrtfFastConv = &hIvasRend->hHrtfs.hHrtfFastConv;
    8295             : 
    8296           0 :     return IVAS_ERR_OK;
    8297             : }
    8298             : 
    8299             : 
    8300             : /*---------------------------------------------------------------------*
    8301             :  * IVAS_REND_GetHrtfParamBinHandle( )
    8302             :  *
    8303             :  *
    8304             :  *---------------------------------------------------------------------*/
    8305             : 
    8306           0 : ivas_error IVAS_REND_GetHrtfParamBinHandle(
    8307             :     IVAS_REND_HANDLE hIvasRend,                   /* i/o: IVAS renderer handle                */
    8308             :     IVAS_DEC_HRTF_PARAMBIN_HANDLE **hHrtfParambin /* o  : Parametric binauralizer HRTF handle */
    8309             : )
    8310             : {
    8311           0 :     if ( hIvasRend == NULL || hIvasRend->hHrtfs.hHrtfParambin == NULL )
    8312             :     {
    8313           0 :         return IVAS_ERR_WRONG_PARAMS;
    8314             :     }
    8315             : 
    8316           0 :     *hHrtfParambin = &hIvasRend->hHrtfs.hHrtfParambin;
    8317             : 
    8318           0 :     return IVAS_ERR_OK;
    8319             : }
    8320             : 
    8321             : /*---------------------------------------------------------------------*
    8322             :  * IVAS_REND_GetHrtfStatisticsHandle( )
    8323             :  *
    8324             :  *
    8325             :  *---------------------------------------------------------------------*/
    8326             : 
    8327           0 : ivas_error IVAS_REND_GetHrtfStatisticsHandle(
    8328             :     IVAS_REND_HANDLE hIvasRend,                       /* i/o: IVAS renderer handle    */
    8329             :     IVAS_DEC_HRTF_STATISTICS_HANDLE **hHrtfStatistics /* o  : HRTF statistics handle  */
    8330             : )
    8331             : {
    8332           0 :     if ( hIvasRend == NULL || hIvasRend->hHrtfs.hHrtfStatistics == NULL )
    8333             :     {
    8334           0 :         return IVAS_ERR_WRONG_PARAMS;
    8335             :     }
    8336             : 
    8337           0 :     *hHrtfStatistics = &hIvasRend->hHrtfs.hHrtfStatistics;
    8338             : 
    8339           0 :     return IVAS_ERR_OK;
    8340             : }
    8341             : 
    8342        1200 : static ivas_error ivas_masa_ext_rend_dirac_rend_init(
    8343             :     input_masa *inputMasa )
    8344             : {
    8345             :     int16_t nchan_out_woLFE;
    8346             :     int16_t nchan_transport;
    8347             :     uint16_t i, j, k;
    8348             :     float ls_azimuth[MAX_OUTPUT_CHANNELS];
    8349             :     float ls_elevation[MAX_OUTPUT_CHANNELS];
    8350             :     int32_t output_Fs;
    8351             :     ivas_error error;
    8352             :     DIRAC_REND_HANDLE hDirACRend;
    8353             :     SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom;
    8354             : 
    8355        1200 :     error = IVAS_ERR_OK;
    8356             : 
    8357        1200 :     hDirACRend = NULL;
    8358        1200 :     output_Fs = *( inputMasa->base.ctx.pOutSampleRate );
    8359             : 
    8360        1200 :     hSpatParamRendCom = inputMasa->hMasaExtRend->hSpatParamRendCom;
    8361             : 
    8362             :     /*-----------------------------------------------------------------*
    8363             :      * prepare library opening
    8364             :      *-----------------------------------------------------------------*/
    8365             : 
    8366        1200 :     if ( ( hDirACRend = (DIRAC_REND_HANDLE) malloc( sizeof( DIRAC_REND_DATA ) ) ) == NULL )
    8367             :     {
    8368           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC renderer\n" ) );
    8369             :     }
    8370             : 
    8371        1200 :     nchan_transport = inputMasa->base.inConfig == IVAS_AUDIO_CONFIG_MASA2 ? 2 : 1;
    8372             : 
    8373             :     /*-----------------------------------------------------------------*
    8374             :      * output setup: for parametric binaural renderer, use output setup, otherwise internal setup
    8375             :      *-----------------------------------------------------------------*/
    8376             : 
    8377        1200 :     ivas_output_init( &hDirACRend->hOutSetup, *inputMasa->base.ctx.pOutConfig );
    8378             : 
    8379        1200 :     if ( hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    8380             :     {
    8381             :         /* Copy from ivas_ls_custom_setup */
    8382         180 :         hDirACRend->hOutSetup.nchan_out_woLFE = inputMasa->base.ctx.pCustomLsOut->num_spk;
    8383         180 :         hDirACRend->hOutSetup.ls_azimuth = inputMasa->base.ctx.pCustomLsOut->ls_azimuth;
    8384         180 :         hDirACRend->hOutSetup.ls_elevation = inputMasa->base.ctx.pCustomLsOut->ls_elevation;
    8385             : 
    8386         180 :         hDirACRend->hOutSetup.num_lfe = inputMasa->base.ctx.pCustomLsOut->num_lfe;
    8387         180 :         hDirACRend->hOutSetup.index_lfe[0] = inputMasa->base.ctx.pCustomLsOut->lfe_idx[0];
    8388             : 
    8389         180 :         hDirACRend->hOutSetup.is_loudspeaker_setup = TRUE;
    8390         180 :         hDirACRend->hOutSetup.is_planar_setup = (int8_t) inputMasa->base.ctx.pCustomLsOut->is_planar_setup;
    8391             :     }
    8392             : 
    8393        1200 :     nchan_out_woLFE = hDirACRend->hOutSetup.nchan_out_woLFE;
    8394             : 
    8395        1200 :     if ( hDirACRend->hOutSetup.ls_azimuth != NULL && hDirACRend->hOutSetup.ls_elevation != NULL )
    8396             :     {
    8397         780 :         mvr2r( hDirACRend->hOutSetup.ls_azimuth, ls_azimuth, nchan_out_woLFE );
    8398         780 :         mvr2r( hDirACRend->hOutSetup.ls_elevation, ls_elevation, nchan_out_woLFE );
    8399             :     }
    8400             : 
    8401        1200 :     if ( hDirACRend->hOutSetup.ambisonics_order == -1 )
    8402             :     {
    8403         840 :         hDirACRend->hOutSetup.ambisonics_order = SBA_HOA3_ORDER; /* Order 3 is used by default in DirAC for SHD processing */
    8404         840 :         if ( hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_MONO || hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_STEREO )
    8405             :         {
    8406          60 :             hDirACRend->hOutSetup.ambisonics_order = SBA_FOA_ORDER;
    8407             :         }
    8408             :     }
    8409         360 :     else if ( hDirACRend->hOutSetup.ambisonics_order >= SBA_FOA_ORDER )
    8410             :     {
    8411         360 :         mvr2r( ls_azimuth_4d4, ls_azimuth, DIRAC_HOA_RENDERING_NUM_VIRT_DECORR_LS );
    8412         360 :         mvr2r( ls_elevation_4d4, ls_elevation, DIRAC_HOA_RENDERING_NUM_VIRT_DECORR_LS );
    8413             :     }
    8414             : 
    8415             :     /*-----------------------------------------------------------------*
    8416             :      * set input parameters
    8417             :      *-----------------------------------------------------------------*/
    8418             : 
    8419        1200 :     if ( hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_MONO )
    8420             :     {
    8421          60 :         hDirACRend->synthesisConf = DIRAC_SYNTHESIS_MONO;
    8422          60 :         hDirACRend->panningConf = DIRAC_PANNING_HOA3;
    8423          60 :         nchan_out_woLFE = 1;
    8424             :     }
    8425        1140 :     else if ( hDirACRend->hOutSetup.is_loudspeaker_setup )
    8426             :     {
    8427         780 :         hDirACRend->synthesisConf = DIRAC_SYNTHESIS_PSD_LS;
    8428         780 :         hDirACRend->panningConf = DIRAC_PANNING_VBAP;
    8429             :     }
    8430         360 :     else if ( !hDirACRend->hOutSetup.is_loudspeaker_setup && nchan_transport > 1 )
    8431             :     {
    8432         180 :         hDirACRend->synthesisConf = DIRAC_SYNTHESIS_PSD_SHD;
    8433         180 :         hDirACRend->panningConf = DIRAC_PANNING_HOA3;
    8434             :     }
    8435             :     else
    8436             :     {
    8437         180 :         hDirACRend->synthesisConf = DIRAC_SYNTHESIS_GAIN_SHD;
    8438         180 :         hDirACRend->panningConf = DIRAC_PANNING_HOA3;
    8439             :     }
    8440             : 
    8441        1200 :     if ( ( hDirACRend->frequency_axis = (float *) malloc( hSpatParamRendCom->num_freq_bands * sizeof( float ) ) ) == NULL )
    8442             :     {
    8443           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
    8444             :     }
    8445        1200 :     set_f( hDirACRend->frequency_axis, 0.0f, hSpatParamRendCom->num_freq_bands );
    8446             : 
    8447        1200 :     ivas_dirac_dec_get_frequency_axis( hDirACRend->frequency_axis, output_Fs, hSpatParamRendCom->num_freq_bands );
    8448             : 
    8449        1200 :     if ( hDirACRend->panningConf == DIRAC_PANNING_HOA3 && nchan_transport == 2 )
    8450             :     {
    8451         240 :         if ( ( hDirACRend->masa_stereo_type_detect = (MASA_STEREO_TYPE_DETECT *) malloc( sizeof( MASA_STEREO_TYPE_DETECT ) ) ) == NULL )
    8452             :         {
    8453           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
    8454             :         }
    8455         240 :         ivas_masa_init_stereotype_detection( hDirACRend->masa_stereo_type_detect );
    8456             :     }
    8457             :     else
    8458             :     {
    8459         960 :         hDirACRend->masa_stereo_type_detect = NULL;
    8460             :     }
    8461             : 
    8462        1200 :     hSpatParamRendCom->numIsmDirections = 0;
    8463             : 
    8464             :     /*-----------------------------------------------------------------*
    8465             :      * (re)configure sub-modules
    8466             :      *-----------------------------------------------------------------*/
    8467             : 
    8468             :     /* prototype signal computation */
    8469             :     /* allocate output setup related arrays */
    8470        1200 :     if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_LS )
    8471             :     {
    8472             :         /* Directional and diffuses components in output LS format */
    8473         780 :         hDirACRend->num_outputs_diff = nchan_out_woLFE;
    8474         780 :         hDirACRend->num_outputs_dir = nchan_out_woLFE;
    8475             :     }
    8476         420 :     else if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    8477             :     {
    8478             :         /* Directional and diffuses components in SHD */
    8479             :         /* Diffuseness components up to 1st order */
    8480         180 :         hDirACRend->num_outputs_diff = ( min( hDirACRend->hOutSetup.ambisonics_order, 1 ) + 1 ) * ( min( hDirACRend->hOutSetup.ambisonics_order, 1 ) + 1 );
    8481         180 :         hDirACRend->num_outputs_dir = ivas_sba_get_nchan( hDirACRend->hOutSetup.ambisonics_order, 0 );
    8482             :     }
    8483         240 :     else if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_SHD )
    8484             :     {
    8485         180 :         hDirACRend->num_outputs_diff = DIRAC_HOA_RENDERING_NUM_VIRT_DECORR_LS;
    8486         180 :         hDirACRend->num_outputs_dir = nchan_out_woLFE;
    8487             :     }
    8488          60 :     else if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_MONO )
    8489             :     {
    8490          60 :         hDirACRend->num_outputs_diff = 1; /* There is one output channel in mono */
    8491          60 :         hDirACRend->num_outputs_dir = 2;  /* Two channels are pre-rendered for stereo type detection */
    8492             :     }
    8493             :     else
    8494             :     {
    8495           0 :         assert( 0 && "DirAC: not existing synthesis methods!" );
    8496             :     }
    8497             : 
    8498        1200 :     if ( ( hDirACRend->proto_index_dir = (int16_t *) malloc( sizeof( int16_t ) * hDirACRend->num_outputs_dir ) ) == NULL )
    8499             :     {
    8500           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
    8501             :     }
    8502             : 
    8503        1200 :     if ( ( hDirACRend->proto_index_diff = (int16_t *) malloc( sizeof( int16_t ) * hDirACRend->num_outputs_diff ) ) == NULL )
    8504             :     {
    8505           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
    8506             :     }
    8507             : 
    8508        1200 :     set_s( hDirACRend->proto_index_dir, 0, hDirACRend->num_outputs_dir );
    8509        1200 :     set_s( hDirACRend->proto_index_diff, 0, hDirACRend->num_outputs_diff );
    8510             : 
    8511        1200 :     hDirACRend->sba_map_tc = sba_map_tc;
    8512             : 
    8513        1200 :     if ( nchan_transport == 1 )
    8514             :     {
    8515         570 :         hDirACRend->num_protos_ambi = 1;
    8516         570 :         hDirACRend->num_protos_dir = 1;
    8517         570 :         hDirACRend->num_protos_diff = 1;
    8518             :     }
    8519         630 :     else if ( nchan_transport == 2 )
    8520             :     {
    8521         630 :         if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    8522             :         {
    8523           0 :             hDirACRend->num_protos_ambi = 2;
    8524           0 :             hDirACRend->num_protos_diff = 1;
    8525           0 :             hDirACRend->num_protos_dir = 2;
    8526           0 :             hDirACRend->proto_index_dir[1] = 1;
    8527             :         }
    8528         630 :         else if ( hDirACRend->hOutSetup.output_config == IVAS_AUDIO_CONFIG_MONO )
    8529             :         {
    8530             :             /* Following the foa rendering for code compatibility */
    8531          60 :             hDirACRend->num_protos_ambi = 2;
    8532          60 :             hDirACRend->num_protos_dir = 2;
    8533          60 :             hDirACRend->num_protos_diff = 3;
    8534          60 :             hDirACRend->proto_index_dir[0] = 0;
    8535          60 :             hDirACRend->proto_index_diff[0] = 0;
    8536             :         }
    8537             :         else
    8538             :         {
    8539         570 :             hDirACRend->num_protos_ambi = 2;
    8540         570 :             hDirACRend->num_protos_diff = 3;
    8541             : 
    8542        5430 :             for ( k = 0; k < hDirACRend->num_outputs_diff; k++ )
    8543             :             {
    8544        4860 :                 if ( ls_azimuth[k] > 0.0f )
    8545             :                 {
    8546        2280 :                     hDirACRend->proto_index_diff[k] = 1;
    8547             :                 }
    8548        2580 :                 else if ( ls_azimuth[k] < 0.0f )
    8549             :                 {
    8550        2190 :                     hDirACRend->proto_index_diff[k] = 2;
    8551             :                 }
    8552             :                 else
    8553             :                 {
    8554         390 :                     hDirACRend->proto_index_diff[k] = 0;
    8555             :                 }
    8556             :             }
    8557             : 
    8558         570 :             if ( hDirACRend->hOutSetup.is_loudspeaker_setup )
    8559             :             {
    8560         390 :                 hDirACRend->num_protos_dir = 3;
    8561         390 :                 mvs2s( hDirACRend->proto_index_diff, hDirACRend->proto_index_dir, nchan_out_woLFE );
    8562             :             }
    8563             :             else
    8564             :             {
    8565         180 :                 hDirACRend->num_protos_dir = 2;
    8566         180 :                 hDirACRend->proto_index_dir[1] = 1;
    8567             :             }
    8568             :         }
    8569             :     }
    8570             : 
    8571             :     /* direct/diffuse responses */
    8572        1200 :     if ( ( hDirACRend->diffuse_response_function = (float *) malloc( sizeof( float ) * hDirACRend->num_outputs_dir ) ) == NULL )
    8573             :     {
    8574           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
    8575             :     }
    8576             : 
    8577        1200 :     if ( ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_LS ) || ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_SHD ) || ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_MONO ) )
    8578             :     {
    8579        1020 :         initDiffuseResponses( hDirACRend->diffuse_response_function, nchan_out_woLFE, hDirACRend->hOutSetup.output_config,
    8580        1020 :                               hDirACRend->hOutSetup, hDirACRend->hOutSetup.ambisonics_order, MASA_FORMAT, &hDirACRend->num_ele_spk_no_diffuse_rendering, IVAS_AUDIO_CONFIG_INVALID );
    8581             :     }
    8582             :     else
    8583             :     {
    8584         180 :         initDiffuseResponses( hDirACRend->diffuse_response_function, hDirACRend->num_outputs_dir, IVAS_AUDIO_CONFIG_FOA,
    8585         180 :                               hDirACRend->hOutSetup, hDirACRend->hOutSetup.ambisonics_order, MASA_FORMAT, &hDirACRend->num_ele_spk_no_diffuse_rendering, IVAS_AUDIO_CONFIG_INVALID );
    8586             :     }
    8587             : 
    8588        1200 :     hDirACRend->hoa_encoder = NULL;
    8589        1200 :     if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_SHD )
    8590             :     {
    8591         180 :         if ( ( hDirACRend->hoa_encoder = (float *) malloc( nchan_out_woLFE * hDirACRend->num_outputs_diff * sizeof( float ) ) ) == NULL )
    8592             :         {
    8593           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
    8594             :         }
    8595             : 
    8596         180 :         set_f( hDirACRend->hoa_encoder, 0.0f, nchan_out_woLFE * hDirACRend->num_outputs_diff );
    8597         180 :         compute_hoa_encoder_mtx( ls_azimuth, ls_elevation, hDirACRend->hoa_encoder, hDirACRend->num_outputs_diff, hDirACRend->hOutSetup.ambisonics_order );
    8598             :     }
    8599             : 
    8600             :     /* VBAP */
    8601        1200 :     inputMasa->hMasaExtRend->hVBAPdata = NULL;
    8602             : 
    8603        1200 :     if ( hDirACRend->panningConf == DIRAC_PANNING_VBAP )
    8604             :     {
    8605         780 :         if ( ( error = vbap_init_data( &( inputMasa->hMasaExtRend->hVBAPdata ), ls_azimuth, ls_elevation, nchan_out_woLFE, MASA_FORMAT ) ) != IVAS_ERR_OK )
    8606             :         {
    8607           0 :             return error;
    8608             :         }
    8609             :     }
    8610             : 
    8611             :     /* HOA panning/dec */
    8612        1200 :     hDirACRend->hoa_decoder = NULL;
    8613        1200 :     if ( hDirACRend->panningConf == DIRAC_PANNING_HOA3 )
    8614             :     {
    8615         420 :         if ( hDirACRend->hOutSetup.is_loudspeaker_setup )
    8616             :         {
    8617          60 :             if ( ( error = ivas_sba_get_hoa_dec_matrix( hDirACRend->hOutSetup, &inputMasa->hMasaExtRend->hoa_dec_mtx, hDirACRend->hOutSetup.ambisonics_order ) ) != IVAS_ERR_OK )
    8618             :             {
    8619           0 :                 return error;
    8620             :             }
    8621             : 
    8622          60 :             hDirACRend->hoa_decoder = inputMasa->hMasaExtRend->hoa_dec_mtx;
    8623             :         }
    8624             :     }
    8625             : 
    8626             :     /* decorrelation */
    8627        1200 :     hDirACRend->proto_signal_decorr_on = 1;
    8628        1200 :     if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_MONO )
    8629             :     {
    8630          60 :         hDirACRend->proto_signal_decorr_on = 0;
    8631             :     }
    8632             : 
    8633        1200 :     if ( hDirACRend->proto_signal_decorr_on )
    8634             :     {
    8635        1140 :         if ( ( error = ivas_dirac_dec_decorr_open( &( hDirACRend->h_freq_domain_decorr_ap_params ),
    8636        1140 :                                                    &( hDirACRend->h_freq_domain_decorr_ap_state ),
    8637        1140 :                                                    hSpatParamRendCom->num_freq_bands,
    8638        1140 :                                                    hDirACRend->num_outputs_diff,
    8639        1140 :                                                    hDirACRend->num_protos_diff,
    8640             :                                                    hDirACRend->synthesisConf,
    8641             :                                                    hDirACRend->frequency_axis,
    8642             :                                                    nchan_transport,
    8643             :                                                    output_Fs ) ) != IVAS_ERR_OK )
    8644             :         {
    8645           0 :             return error;
    8646             :         }
    8647             :     }
    8648             : 
    8649             :     /* output synthesis */
    8650        1200 :     if ( ( ivas_dirac_dec_output_synthesis_open( hSpatParamRendCom, hDirACRend, RENDERER_DIRAC, nchan_transport, output_Fs, 0 ) ) != IVAS_ERR_OK )
    8651             :     {
    8652           0 :         return error;
    8653             :     }
    8654        1200 :     hDirACRend->h_output_synthesis_psd_params.use_onset_filters = hDirACRend->proto_signal_decorr_on;
    8655             : 
    8656        1200 :     if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_PSD_SHD || hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    8657             :     {
    8658         360 :         hDirACRend->h_output_synthesis_psd_params.use_onset_filters = 0;
    8659             :     }
    8660             : 
    8661             :     /*-----------------------------------------------------------------*
    8662             :      * memory allocation
    8663             :      *-----------------------------------------------------------------*/
    8664             : 
    8665        1200 :     if ( hDirACRend->synthesisConf == DIRAC_SYNTHESIS_GAIN_SHD )
    8666             :     {
    8667         180 :         hDirACRend->proto_frame_f = NULL;
    8668             :     }
    8669             :     else
    8670             :     {
    8671        1020 :         if ( ( hDirACRend->proto_frame_f = (float *) malloc( sizeof( float ) * 2 * hDirACRend->num_protos_diff * hSpatParamRendCom->num_freq_bands ) ) == NULL )
    8672             :         {
    8673           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC\n" ) );
    8674             :         }
    8675             :     }
    8676             : 
    8677             : 
    8678        1200 :     hDirACRend->buffer_energy = NULL;
    8679             : 
    8680        4800 :     for ( i = 0; i < DIRAC_NUM_DIMS; i++ )
    8681             :     {
    8682      118800 :         for ( j = 0; j < DIRAC_NO_COL_AVG_DIFF; j++ )
    8683             :         {
    8684      115200 :             hDirACRend->buffer_intensity_real[i][j] = NULL;
    8685             :         }
    8686             :     }
    8687             : 
    8688             :     /* output synthesis */
    8689        1200 :     ivas_dirac_dec_output_synthesis_init( hSpatParamRendCom, hDirACRend, nchan_out_woLFE, 0 );
    8690             : 
    8691             :     /* Allocate stack memory */
    8692        1200 :     if ( ( error = ivas_dirac_alloc_mem( hDirACRend, RENDERER_DIRAC, hSpatParamRendCom->num_freq_bands, &( hDirACRend->stack_mem ), 0 ) ) != IVAS_ERR_OK )
    8693             :     {
    8694           0 :         return error;
    8695             :     }
    8696             : 
    8697        1200 :     inputMasa->hMasaExtRend->hDirACRend = hDirACRend;
    8698             : 
    8699        1200 :     return error;
    8700             : }
    8701             : 
    8702         840 : static ivas_error ivas_masa_ext_rend_parambin_init(
    8703             :     input_masa *inputMasa,                  /* i/o: MASA external renderer structure        */
    8704             :     const RENDER_CONFIG_DATA *hRendCfg,     /* i  : Renderer configuration data handle      */
    8705             :     HRTFS_STATISTICS_HANDLE hHrtfStatistics /* i  : HRTF statistics                         */
    8706             : )
    8707             : {
    8708             :     DIRAC_DEC_BIN_HANDLE hDiracDecBin;
    8709             :     HRTFS_PARAMBIN_HANDLE *phHrtfParambin;
    8710             :     int16_t nBins;
    8711             :     int32_t output_Fs;
    8712             :     RENDERER_TYPE renderer_type;
    8713             :     int16_t j, k, bin;
    8714             :     int16_t num_poses;
    8715             :     float binCenterFreq, tmpFloat;
    8716             :     ivas_error error;
    8717             :     float frequency_axis[CLDFB_NO_CHANNELS_MAX];
    8718             :     int16_t pos_idx;
    8719             :     const IVAS_ROOM_ACOUSTICS_CONFIG_DATA *pRoomAcoustics;
    8720             : 
    8721         840 :     error = IVAS_ERR_OK;
    8722             : 
    8723         840 :     phHrtfParambin = inputMasa->hMasaExtRend->hHrtfParambin;
    8724             : 
    8725             :     /* Set common variables and defaults */
    8726         840 :     output_Fs = *( inputMasa->base.ctx.pOutSampleRate );
    8727         840 :     nBins = inputMasa->hMasaExtRend->hSpatParamRendCom->num_freq_bands;
    8728         840 :     renderer_type = inputMasa->hMasaExtRend->renderer_type;
    8729             : 
    8730         840 :     num_poses = 1;
    8731         840 :     if ( inputMasa->base.ctx.pSplitRendWrapper != NULL )
    8732             :     {
    8733           0 :         num_poses = inputMasa->base.ctx.pSplitRendWrapper->multiBinPoseData.num_poses;
    8734             :     }
    8735             : 
    8736        1680 :     for ( pos_idx = 0; pos_idx < num_poses; pos_idx++ )
    8737             :     {
    8738         840 :         hDiracDecBin = inputMasa->hMasaExtRend->hDiracDecBin[pos_idx];
    8739             : 
    8740             :         /* Init assumes that no reconfiguration is required in external renderer. Instead, free and rebuild whole rendering. */
    8741         840 :         if ( ( hDiracDecBin = (DIRAC_DEC_BIN_HANDLE) malloc( sizeof( DIRAC_DEC_BIN_DATA ) ) ) == NULL )
    8742             :         {
    8743           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DirAC binaural handle " );
    8744             :         }
    8745             : 
    8746         840 :         hDiracDecBin->hTdDecorr = NULL;
    8747         840 :         hDiracDecBin->hReverb = NULL;
    8748         840 :         hDiracDecBin->h_freq_domain_decorr_ap_params = NULL;
    8749         840 :         hDiracDecBin->h_freq_domain_decorr_ap_state = NULL;
    8750         840 :         hDiracDecBin->hDiffuseDist = NULL; /* Not used in external renderer */
    8751         840 :         hDiracDecBin->useTdDecorr = 0;     /* Always use frequency domain decorrelator in external renderer */
    8752             : 
    8753        2520 :         for ( j = 0; j < BINAURAL_CHANNELS; j++ )
    8754             :         {
    8755       11760 :             for ( k = 0; k < BINAURAL_CHANNELS + MAX_NUM_OBJECTS; k++ )
    8756             :             {
    8757       10080 :                 set_zero( hDiracDecBin->processMtxRe[j][k], nBins );
    8758       10080 :                 set_zero( hDiracDecBin->processMtxIm[j][k], nBins );
    8759             :             }
    8760             : 
    8761        5040 :             for ( k = 0; k < BINAURAL_CHANNELS; k++ )
    8762             :             {
    8763        3360 :                 set_zero( hDiracDecBin->processMtxDecRe[j][k], nBins );
    8764        3360 :                 set_zero( hDiracDecBin->processMtxDecIm[j][k], nBins );
    8765             :             }
    8766        1680 :             set_zero( hDiracDecBin->ChEnePrev[j], nBins );
    8767        1680 :             set_zero( hDiracDecBin->ChEneOutPrev[j], nBins );
    8768             :         }
    8769         840 :         set_zero( hDiracDecBin->ChCrossRePrev, nBins );
    8770         840 :         set_zero( hDiracDecBin->ChCrossImPrev, nBins );
    8771         840 :         set_zero( hDiracDecBin->ChCrossReOutPrev, nBins );
    8772         840 :         set_zero( hDiracDecBin->ChCrossImOutPrev, nBins );
    8773         840 :         hDiracDecBin->renderStereoOutputInsteadOfBinaural = 0;
    8774             : 
    8775       34440 :         for ( bin = 0; bin < nBins; bin++ )
    8776             :         {
    8777       33600 :             binCenterFreq = ( (float) bin + 0.5f ) / (float) nBins * ( (float) output_Fs / 2.0f );
    8778             :             /* These formulas and values are from Christian Borss's publication for binaural diffuse field coherence */
    8779       33600 :             tmpFloat = max( 0.0f, 1.0f - binCenterFreq / 2700.0f );
    8780       33600 :             hDiracDecBin->diffuseFieldCoherence[bin] = tmpFloat * sinf( binCenterFreq * EVS_PI / 550.0f ) / ( binCenterFreq * EVS_PI / 550.0f );
    8781             :         }
    8782             : 
    8783             :         /* No SPAR in external renderer so set directive diffuse field coherence tables to zero */
    8784         840 :         set_zero( hDiracDecBin->diffuseFieldCoherenceX, BINAURAL_COHERENCE_DIFFERENCE_BINS );
    8785         840 :         set_zero( hDiracDecBin->diffuseFieldCoherenceY, BINAURAL_COHERENCE_DIFFERENCE_BINS );
    8786         840 :         set_zero( hDiracDecBin->diffuseFieldCoherenceZ, BINAURAL_COHERENCE_DIFFERENCE_BINS );
    8787             : 
    8788         840 :         if ( renderer_type == RENDERER_BINAURAL_PARAMETRIC ) /* Indication of binaural rendering without room effect */
    8789             :         {
    8790         240 :             set_f( hDiracDecBin->earlyPartEneCorrection, 1.0f, CLDFB_NO_CHANNELS_MAX );
    8791         240 :             hDiracDecBin->hReverb = NULL;
    8792             :         }
    8793         600 :         else if ( renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM ) /* Indication of binaural rendering with room effect */
    8794             :         {
    8795         480 :             if ( *inputMasa->base.ctx.pOutConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
    8796             :             {
    8797         240 :                 mvr2r( ( *phHrtfParambin )->parametricEarlyPartEneCorrection, hDiracDecBin->earlyPartEneCorrection, nBins );
    8798         240 :                 pRoomAcoustics = NULL;
    8799             :             }
    8800             :             else
    8801             :             {
    8802         240 :                 set_f( hDiracDecBin->earlyPartEneCorrection, 1.0f, CLDFB_NO_CHANNELS_MAX );
    8803         240 :                 pRoomAcoustics = &( hRendCfg->roomAcoustics );
    8804             :             }
    8805             : 
    8806         480 :             if ( hDiracDecBin->hReverb == NULL && pos_idx == 0 ) /* open reverb only for the main direction */
    8807             :             {
    8808         480 :                 if ( ( error = ivas_binaural_reverb_init( &hDiracDecBin->hReverb,
    8809             :                                                           hHrtfStatistics,
    8810             :                                                           nBins,
    8811             :                                                           CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES,
    8812             :                                                           pRoomAcoustics,
    8813             :                                                           output_Fs,
    8814         480 :                                                           ( *phHrtfParambin )->parametricReverberationTimes,
    8815         480 :                                                           ( *phHrtfParambin )->parametricReverberationEneCorrections,
    8816         480 :                                                           hDiracDecBin->earlyPartEneCorrection ) ) != IVAS_ERR_OK )
    8817             :                 {
    8818           0 :                     return error;
    8819             :                 }
    8820             :             }
    8821             :         }
    8822         120 :         else if ( renderer_type == RENDERER_STEREO_PARAMETRIC )
    8823             :         {
    8824         120 :             set_f( hDiracDecBin->earlyPartEneCorrection, 1.0f, CLDFB_NO_CHANNELS_MAX );
    8825         120 :             hDiracDecBin->hReverb = NULL;
    8826         120 :             hDiracDecBin->renderStereoOutputInsteadOfBinaural = 1;
    8827             :         }
    8828             :         else /* Not valid renderer type for this renderer */
    8829             :         {
    8830           0 :             assert( false );
    8831             :         }
    8832             : 
    8833         840 :         if ( pos_idx == 0 ) /* open decorrelator only for the main direction */
    8834             :         {
    8835             :             /* Always open frequency domain decorrelator */
    8836         840 :             ivas_dirac_dec_get_frequency_axis( frequency_axis, output_Fs, nBins );
    8837         840 :             if ( ( error = ivas_dirac_dec_decorr_open( &( hDiracDecBin->h_freq_domain_decorr_ap_params ),
    8838         840 :                                                        &( hDiracDecBin->h_freq_domain_decorr_ap_state ),
    8839             :                                                        nBins,
    8840             :                                                        BINAURAL_CHANNELS,
    8841             :                                                        BINAURAL_CHANNELS,
    8842             :                                                        DIRAC_SYNTHESIS_PSD_LS,
    8843             :                                                        frequency_axis,
    8844             :                                                        BINAURAL_CHANNELS,
    8845             :                                                        output_Fs ) ) != IVAS_ERR_OK )
    8846             :             {
    8847           0 :                 return error;
    8848             :             }
    8849             :         }
    8850             : 
    8851             :         /* External renderer uses constant regularization factor */
    8852         840 :         hDiracDecBin->reqularizationFactor = 0.4f;
    8853             : 
    8854         840 :         hDiracDecBin->phHrtfParambin = phHrtfParambin;
    8855             : 
    8856         840 :         inputMasa->hMasaExtRend->hDiracDecBin[pos_idx] = hDiracDecBin;
    8857             :     }
    8858             : 
    8859         840 :     return error;
    8860             : }
    8861             : 
    8862        2100 : static ivas_error initMasaExtRenderer(
    8863             :     input_masa *inputMasa,
    8864             :     const AUDIO_CONFIG outConfig,
    8865             :     const RENDER_CONFIG_DATA *hRendCfg,
    8866             :     hrtf_handles *hrtfs )
    8867             : {
    8868             :     int16_t i;
    8869             :     ivas_error error;
    8870             :     MASA_EXT_REND_HANDLE hMasaExtRend;
    8871             : 
    8872        2100 :     error = IVAS_ERR_OK;
    8873             : 
    8874        2100 :     if ( ( hMasaExtRend = (MASA_EXT_REND_HANDLE) malloc( sizeof( MASA_EXT_REND_DATA ) ) ) == NULL )
    8875             :     {
    8876           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MASA external renderer structure\n" ) );
    8877             :     }
    8878             : 
    8879        2100 :     inputMasa->hMasaExtRend = hMasaExtRend;
    8880             : 
    8881             :     /* Default init */
    8882        2100 :     hMasaExtRend->renderer_type = RENDERER_DISABLE;
    8883        2100 :     hMasaExtRend->hDirACRend = NULL;
    8884        2100 :     hMasaExtRend->hSpatParamRendCom = NULL;
    8885       18900 :     for ( i = 0; i < MAX_HEAD_ROT_POSES; i++ )
    8886             :     {
    8887       16800 :         hMasaExtRend->hDiracDecBin[i] = NULL;
    8888             :     }
    8889        2100 :     hMasaExtRend->hReverb = NULL;
    8890        2100 :     hMasaExtRend->hHrtfParambin = &hrtfs->hHrtfParambin;
    8891        2100 :     hMasaExtRend->hVBAPdata = NULL;
    8892        2100 :     hMasaExtRend->hoa_dec_mtx = NULL;
    8893             : 
    8894        2100 :     if ( ( error = getAudioConfigNumChannels( inputMasa->base.inConfig, &hMasaExtRend->nchan_input ) ) != IVAS_ERR_OK )
    8895             :     {
    8896           0 :         return error;
    8897             :     }
    8898             : 
    8899        2100 :     if ( outConfig == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    8900             :     {
    8901         180 :         hMasaExtRend->nchan_output = inputMasa->base.ctx.pCustomLsOut->num_spk + inputMasa->base.ctx.pCustomLsOut->num_lfe;
    8902             :     }
    8903        1920 :     else if ( ( error = getAudioConfigNumChannels( outConfig, &hMasaExtRend->nchan_output ) ) != IVAS_ERR_OK )
    8904             :     {
    8905           0 :         return error;
    8906             :     }
    8907             : 
    8908        2100 :     switch ( outConfig )
    8909             :     {
    8910         120 :         case IVAS_AUDIO_CONFIG_MONO:
    8911         120 :             if ( inputMasa->base.inConfig == IVAS_AUDIO_CONFIG_MASA2 )
    8912             :             {
    8913          60 :                 hMasaExtRend->renderer_type = RENDERER_DIRAC;
    8914             :             }
    8915             :             else
    8916             :             {
    8917             :                 /* 1TC MASA to mono does not need rendering. */
    8918          60 :                 hMasaExtRend->renderer_type = RENDERER_DISABLE;
    8919             :             }
    8920         120 :             break;
    8921             : 
    8922         120 :         case IVAS_AUDIO_CONFIG_STEREO:
    8923         120 :             hMasaExtRend->renderer_type = RENDERER_STEREO_PARAMETRIC;
    8924         120 :             break;
    8925             : 
    8926        1140 :         case IVAS_AUDIO_CONFIG_5_1:
    8927             :         case IVAS_AUDIO_CONFIG_7_1:
    8928             :         case IVAS_AUDIO_CONFIG_5_1_2:
    8929             :         case IVAS_AUDIO_CONFIG_5_1_4:
    8930             :         case IVAS_AUDIO_CONFIG_7_1_4:
    8931             :         case IVAS_AUDIO_CONFIG_LS_CUSTOM:
    8932             :         case IVAS_AUDIO_CONFIG_FOA:
    8933             :         case IVAS_AUDIO_CONFIG_HOA2:
    8934             :         case IVAS_AUDIO_CONFIG_HOA3:
    8935        1140 :             hMasaExtRend->renderer_type = RENDERER_DIRAC;
    8936        1140 :             break;
    8937             : 
    8938         240 :         case IVAS_AUDIO_CONFIG_BINAURAL:
    8939             :         case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM:
    8940             :         case IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED:
    8941         240 :             hMasaExtRend->renderer_type = RENDERER_BINAURAL_PARAMETRIC;
    8942         240 :             break;
    8943             : 
    8944         480 :         case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR:
    8945             :         case IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB:
    8946         480 :             hMasaExtRend->renderer_type = RENDERER_BINAURAL_PARAMETRIC_ROOM;
    8947         480 :             break;
    8948             : 
    8949           0 :         default:
    8950           0 :             return ( IVAS_ERROR( IVAS_ERR_IO_CONFIG_PAIR_NOT_SUPPORTED, "Wrong output config for MASA input in external renderer\n" ) );
    8951             :     }
    8952             : 
    8953        2100 :     if ( hMasaExtRend->renderer_type != RENDERER_DISABLE )
    8954             :     {
    8955             :         int16_t subframe;
    8956             : 
    8957        2040 :         if ( ( error = ivas_spat_hSpatParamRendCom_config( &hMasaExtRend->hSpatParamRendCom, DIRAC_OPEN, 0, MASA_FORMAT, MC_MODE_NONE, *( inputMasa->base.ctx.pOutSampleRate ), 0, 1 ) ) != IVAS_ERR_OK )
    8958             :         {
    8959           0 :             return error;
    8960             :         }
    8961             : 
    8962             :         /* Simple population of the metadata index map as no adaptation is present */
    8963        2040 :         set_s( hMasaExtRend->hSpatParamRendCom->render_to_md_map, 0, MAX_JBM_SUBFRAMES_5MS * JBM_CLDFB_SLOTS_IN_SUBFRAME );
    8964       10200 :         for ( subframe = 0; subframe < MAX_PARAM_SPATIAL_SUBFRAMES; subframe++ )
    8965             :         {
    8966        8160 :             hMasaExtRend->hSpatParamRendCom->render_to_md_map[subframe] = subframe;
    8967             :         }
    8968        2040 :         hMasaExtRend->hSpatParamRendCom->subframes_rendered = 0;
    8969             :     }
    8970             : 
    8971        2100 :     if ( hMasaExtRend->renderer_type == RENDERER_DIRAC )
    8972             :     {
    8973        1200 :         if ( ( error = ivas_masa_ext_rend_dirac_rend_init( inputMasa ) ) != IVAS_ERR_OK )
    8974             :         {
    8975           0 :             return error;
    8976             :         }
    8977             :     }
    8978             : 
    8979        2100 :     if ( hMasaExtRend->renderer_type == RENDERER_BINAURAL_PARAMETRIC || hMasaExtRend->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM || hMasaExtRend->renderer_type == RENDERER_STEREO_PARAMETRIC )
    8980             :     {
    8981         840 :         if ( hMasaExtRend->renderer_type != RENDERER_STEREO_PARAMETRIC )
    8982             :         {
    8983         720 :             if ( ( error = ivas_dirac_dec_binaural_copy_hrtfs( inputMasa->hMasaExtRend->hHrtfParambin ) ) != IVAS_ERR_OK )
    8984             :             {
    8985           0 :                 return error;
    8986             :             }
    8987             :         }
    8988             : 
    8989         840 :         if ( ( error = ivas_masa_ext_rend_parambin_init( inputMasa, hRendCfg, hrtfs->hHrtfStatistics ) ) != IVAS_ERR_OK )
    8990             :         {
    8991           0 :             return error;
    8992             :         }
    8993             :     }
    8994             : 
    8995             :     /* Init CLDFB for analysis & synthesis if renderer is used. Otherwise, NULL. */
    8996        6300 :     for ( i = 0; i < MASA_MAX_TRANSPORT_CHANNELS; i++ )
    8997             :     {
    8998        4200 :         hMasaExtRend->cldfbAnaRend[i] = NULL;
    8999             :     }
    9000             : 
    9001       35700 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    9002             :     {
    9003       33600 :         hMasaExtRend->cldfbSynRend[i] = NULL;
    9004             :     }
    9005             : 
    9006        2100 :     if ( hMasaExtRend->renderer_type != RENDERER_DISABLE )
    9007             :     {
    9008        5130 :         for ( i = 0; i < hMasaExtRend->nchan_input; i++ )
    9009             :         {
    9010        3090 :             if ( ( error = openCldfb( &( hMasaExtRend->cldfbAnaRend[i] ), CLDFB_ANALYSIS, *inputMasa->base.ctx.pOutSampleRate, CLDFB_PROTOTYPE_5_00MS ) ) != IVAS_ERR_OK )
    9011             :             {
    9012           0 :                 return error;
    9013             :             }
    9014             :         }
    9015             : 
    9016       14700 :         for ( i = 0; i < hMasaExtRend->nchan_output; i++ )
    9017             :         {
    9018       12660 :             if ( ( error = openCldfb( &( hMasaExtRend->cldfbSynRend[i] ), CLDFB_SYNTHESIS, *inputMasa->base.ctx.pOutSampleRate, CLDFB_PROTOTYPE_5_00MS ) ) != IVAS_ERR_OK )
    9019             :             {
    9020           0 :                 return error;
    9021             :             }
    9022             :         }
    9023             :     }
    9024             : 
    9025        2100 :     inputMasa->hMasaExtRend = hMasaExtRend;
    9026             : 
    9027        2100 :     return IVAS_ERR_OK;
    9028             : }
    9029             : 
    9030             : 
    9031        8382 : static void freeMasaExtRenderer(
    9032             :     MASA_EXT_REND_HANDLE *hMasaExtRendOut )
    9033             : {
    9034             :     MASA_EXT_REND_HANDLE hMasaExtRend;
    9035             :     int16_t i;
    9036             : 
    9037        8382 :     if ( hMasaExtRendOut == NULL || *hMasaExtRendOut == NULL )
    9038             :     {
    9039        6282 :         return;
    9040             :     }
    9041             : 
    9042        2100 :     hMasaExtRend = *hMasaExtRendOut;
    9043             : 
    9044        2100 :     if ( hMasaExtRend->hDirACRend != NULL )
    9045             :     {
    9046        1200 :         ivas_dirac_rend_close( &hMasaExtRend->hDirACRend );
    9047             :     }
    9048             : 
    9049        2100 :     if ( hMasaExtRend->hSpatParamRendCom != NULL )
    9050             :     {
    9051        2040 :         ivas_spat_hSpatParamRendCom_close( &hMasaExtRend->hSpatParamRendCom );
    9052             :     }
    9053             : 
    9054       18900 :     for ( i = 0; i < MAX_HEAD_ROT_POSES; i++ )
    9055             :     {
    9056       16800 :         if ( hMasaExtRend->hDiracDecBin[i] != NULL )
    9057             :         {
    9058         840 :             ivas_dirac_dec_close_binaural_data( &hMasaExtRend->hDiracDecBin[i] );
    9059             :         }
    9060             :     }
    9061             : 
    9062             : 
    9063        2100 :     if ( hMasaExtRend->hReverb != NULL )
    9064             :     {
    9065           0 :         ivas_binaural_reverb_close( &hMasaExtRend->hReverb );
    9066             :     }
    9067             : 
    9068        2100 :     if ( hMasaExtRend->hHrtfParambin != NULL )
    9069             :     {
    9070        2100 :         ivas_HRTF_parambin_binary_close( hMasaExtRend->hHrtfParambin );
    9071             :     }
    9072             : 
    9073        2100 :     if ( hMasaExtRend->hVBAPdata != NULL )
    9074             :     {
    9075         780 :         vbap_free_data( &hMasaExtRend->hVBAPdata );
    9076             :     }
    9077             : 
    9078        2100 :     if ( hMasaExtRend->hoa_dec_mtx != NULL )
    9079             :     {
    9080          60 :         free( hMasaExtRend->hoa_dec_mtx );
    9081             :     }
    9082             : 
    9083        6300 :     for ( i = 0; i < MASA_MAX_TRANSPORT_CHANNELS; i++ )
    9084             :     {
    9085        4200 :         if ( hMasaExtRend->cldfbAnaRend[i] != NULL )
    9086             :         {
    9087        3090 :             deleteCldfb( &hMasaExtRend->cldfbAnaRend[i] );
    9088             :         }
    9089             :     }
    9090             : 
    9091       35700 :     for ( i = 0; i < MAX_OUTPUT_CHANNELS; i++ )
    9092             :     {
    9093       33600 :         if ( hMasaExtRend->cldfbSynRend[i] != NULL )
    9094             :         {
    9095       12660 :             deleteCldfb( &hMasaExtRend->cldfbSynRend[i] );
    9096             :         }
    9097             :     }
    9098             : 
    9099        2100 :     free( hMasaExtRend );
    9100        2100 :     *hMasaExtRendOut = NULL;
    9101             : 
    9102        2100 :     return;
    9103             : }
    9104             : 
    9105             : 
    9106        8382 : static ivas_error printConfigInfo_rend(
    9107             :     IVAS_REND_HANDLE hIvasRend /* i  : IVAS renderer handle     */
    9108             : )
    9109             : {
    9110             :     ivas_error error;
    9111             :     char config_str[50];
    9112             : 
    9113             :     /*-----------------------------------------------------------------*
    9114             :      * Print output audio configuration
    9115             :      *-----------------------------------------------------------------*/
    9116             : 
    9117        8382 :     if ( ( error = get_channel_config( hIvasRend->outputConfig, &config_str[0] ) ) != IVAS_ERR_OK )
    9118             :     {
    9119           0 :         return error;
    9120             :     }
    9121             : 
    9122        8382 :     fprintf( stdout, "Output configuration:   %s\n", config_str );
    9123             : 
    9124             :     /*-----------------------------------------------------------------*
    9125             :      * Print renderer configurations
    9126             :      *-----------------------------------------------------------------*/
    9127             : 
    9128        8382 :     fprintf( stdout, "Output sampling rate:   %d Hz\n", hIvasRend->sampleRateOut );
    9129             : 
    9130        8382 :     if ( hIvasRend->headRotData.headRotEnabled == 1 )
    9131             :     {
    9132        1332 :         fprintf( stdout, "Head-tracking:          ON\n" );
    9133             :     }
    9134             : 
    9135        8382 :     if ( hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL ||
    9136        7494 :          hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR ||
    9137        6606 :          hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ||
    9138        5718 :          hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ||
    9139        5718 :          hIvasRend->outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    9140             :     {
    9141        2664 :         fprintf( stdout, "Render framesize:       %dms\n", hIvasRend->num_subframes * 5 );
    9142             :     }
    9143             : 
    9144        8382 :     return IVAS_ERR_OK;
    9145             : }
    9146             : 
    9147             : 
    9148             : /*---------------------------------------------------------------------*
    9149             :  * IVAS_REND_PrintInputConfig()
    9150             :  *
    9151             :  *
    9152             :  *---------------------------------------------------------------------*/
    9153             : 
    9154       12986 : void IVAS_REND_PrintInputConfig(
    9155             :     const IVAS_AUDIO_CONFIG inputConfig /* i  : input audio configuration    */
    9156             : )
    9157             : {
    9158             :     char config_str[50];
    9159             : 
    9160       12986 :     get_channel_config( inputConfig, &config_str[0] );
    9161       12986 :     fprintf( stdout, "Input configuration:    %s\n", config_str );
    9162             : 
    9163       12986 :     return;
    9164             : }
    9165             : 
    9166             : 
    9167             : /*---------------------------------------------------------------------*
    9168             :  * IVAS_REND_PrintConfig()
    9169             :  *
    9170             :  *
    9171             :  *---------------------------------------------------------------------*/
    9172             : 
    9173        8382 : ivas_error IVAS_REND_PrintConfig(
    9174             :     IVAS_REND_HANDLE hIvasRend /* i  : IVAS renderer handle    */
    9175             : )
    9176             : {
    9177        8382 :     if ( hIvasRend == NULL )
    9178             :     {
    9179           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    9180             :     }
    9181             : 
    9182        8382 :     return printConfigInfo_rend( hIvasRend );
    9183             : }
    9184             : 
    9185             : 
    9186             : /*---------------------------------------------------------------------*
    9187             :  * IVAS_REND_PrintDisclaimer()
    9188             :  *
    9189             :  * Print IVAS disclaimer to console
    9190             :  *---------------------------------------------------------------------*/
    9191             : 
    9192        8382 : void IVAS_REND_PrintDisclaimer( void )
    9193             : {
    9194        8382 :     print_disclaimer( stderr );
    9195             : 
    9196        8382 :     return;
    9197             : }

Generated by: LCOV version 1.14