LCOV - code coverage report
Current view: top level - lib_dec - lib_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 867217ee32c8e8cd2cf5aae69e60c58e00160b49 Lines: 1430 1859 76.9 %
Date: 2025-12-15 05:59:49 Functions: 85 95 89.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 "lib_dec.h"
      34             : #include "ivas_cnst.h"
      35             : #include "ivas_prot.h"
      36             : #include "ivas_prot_rend.h"
      37             : #include "isar_prot.h"
      38             : #include "ivas_rom_com.h"
      39             : #include "lib_isar_pre_rend.h"
      40             : #include "prot.h"
      41             : #include "jbm_jb4sb.h"
      42             : #include "jbm_pcmdsp_apa.h"
      43             : #include "jbm_pcmdsp_fifo.h"
      44             : #include <math.h>
      45             : #include <assert.h>
      46             : #ifdef DEBUGGING
      47             : #include "debug.h"
      48             : #include <string.h>
      49             : #endif
      50             : #include "wmc_auto.h"
      51             : 
      52             : /*---------------------------------------------------------------------*
      53             :  * Local structs
      54             :  *---------------------------------------------------------------------*/
      55             : 
      56             : struct IVAS_DEC_VOIP
      57             : {
      58             :     JB4_HANDLE hJBM;
      59             :     uint16_t lastDecodedWasActive;
      60             :     JB4_DATAUNIT_HANDLE hCurrentDataUnit; /* Points to the currently processed data unit */
      61             :     uint16_t *bs_conversion_buf;          /* Buffer for bitstream conversion from packed to serial */
      62             :     int16_t nSamplesRendered20ms;         /* how many samples have been rendered since the last 20ms render border*/
      63             : #ifdef SUPPORT_JBM_TRACEFILE
      64             :     IVAS_JBM_TRACE_DATA JbmTraceData;
      65             : #endif
      66             : };
      67             : 
      68             : typedef struct IVAS_DEC_VOIP IVAS_DEC_VOIP;
      69             : 
      70             : struct IVAS_DEC
      71             : {
      72             :     IVAS_DEC_MODE mode;
      73             :     uint16_t nSamplesFrame;
      74             :     Decoder_Struct *st_ivas;
      75             :     IVAS_DEC_VOIP *hVoIP;
      76             :     bool hasBeenFedFirstGoodFrame; /* False on init. Gets set to true after first good frame has been fed into the decoder, but not decoded yet.  */
      77             :     bool hasDecodedFirstGoodFrame; /* False on init. Gets set to true after first good frame has been decoded -> all bitstream information is known from that point on */
      78             :     bool isInitialized;
      79             : 
      80             :     int16_t bitstreamformat; /* Bitstream format flag (G.192/MIME/VOIP_G192_RTP/VOIP_RTPDUMP) */
      81             : #ifdef DEBUGGING
      82             :     bool Opt_VOIP; /* flag indicating VOIP mode with JBM */
      83             : #endif
      84             :     int16_t tsm_scale; /* scale for TSM operation */
      85             :     int16_t tsm_max_scaling;
      86             :     int16_t timeScalingDone; /* have we done already one TSM in a 20ms frame? */
      87             :     float tsm_quality;
      88             :     PCMDSP_APA_HANDLE hTimeScaler;
      89             :     bool needNewFrame;
      90             :     bool hasBeenFedFrame;
      91             :     bool updateOrientation;
      92             :     uint16_t nSamplesAvailableNext;
      93             :     int16_t nTransportChannelsOld;
      94             :     int16_t amrwb_rfc4867_flag; /* MIME from rfc4867 is used */
      95             :     int16_t sdp_hf_only;        /* RTP payload format parameter: only Header-Full format without zero padding for size collision avoidance */
      96             :     int16_t prev_ft_speech;     /* RXDTX handler: previous frametype flag for  G.192 format AMRWB SID_FIRST detection */
      97             :     int16_t CNG;                /* RXDTX handler: CNG=1, nonCNG=0 */
      98             : 
      99             :     uint16_t nSamplesFlushed;
     100             :     void *flushbuffer;
     101             :     IVAS_DEC_PCM_TYPE pcmType;
     102             :     bool hasBeenPreparedRendering;
     103             : };
     104             : 
     105             : 
     106             : /*---------------------------------------------------------------------*
     107             :  * Local function declarations
     108             :  *---------------------------------------------------------------------*/
     109             : 
     110             : static void ivas_destroy_handle_VoIP( IVAS_DEC_VOIP *hVoIP );
     111             : #ifdef SUPPORT_JBM_TRACEFILE
     112             : static void store_JbmData( IVAS_DEC_VOIP *hVoIP, JB4_DATAUNIT_HANDLE dataUnit, const uint32_t systemTimestamp_ms, const uint16_t extBufferedSamples, const int32_t output_Fs );
     113             : #endif
     114             : static ivas_error evs_dec_main( Decoder_Struct *st_ivas );
     115             : static ivas_error input_format_API_to_internal( IVAS_DEC_INPUT_FORMAT input_format, int16_t *bitstream_format_internal, int16_t *sdp_hf_only, const bool is_voip_enabled );
     116             : static void init_decoder_config( DECODER_CONFIG_HANDLE hDecoderConfig );
     117             : static ivas_error ivas_dec_setup_all( IVAS_DEC_HANDLE hIvasDec, uint8_t *nTransportChannels, ISAR_SPLIT_REND_BITS_DATA *splitRendBits );
     118             : static ivas_error apa_setup( IVAS_DEC_HANDLE hIvasDec, const bool isInitialized_voip, const uint16_t nTransportChannels );
     119             : static PCM_RESOLUTION pcm_type_API_to_internal( const IVAS_DEC_PCM_TYPE pcmType );
     120             : static void *pcm_buffer_offset( void *buffer, const IVAS_DEC_PCM_TYPE pcmType, const int32_t offset );
     121             : static ivas_error set_pcm_buffer_to_zero( void *buffer, const IVAS_DEC_PCM_TYPE pcmType, const int16_t nZeroSamples );
     122             : static ivas_error isar_set_split_rend_setup( ISAR_DEC_SPLIT_REND_WRAPPER *hSplitBinRend, const ISAR_SPLIT_REND_CONFIG_DATA *hSplitBinConfig, const COMBINED_ORIENTATION_HANDLE hCombinedOrientationData, ISAR_SPLIT_REND_BITS_DATA *splitRendBits );
     123             : static ivas_error ivas_dec_reconfig_split_rend( Decoder_Struct *st_ivas );
     124             : static ivas_error ivas_dec_init_split_rend( Decoder_Struct *st_ivas );
     125             : static ivas_error ivas_create_handle_isar( ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE *hSplitBinRend_out );
     126             : static void ivas_destroy_handle_isar( ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE *hSplitBinRend_out );
     127             : static int16_t get_render_frame_size_ms( IVAS_RENDER_FRAMESIZE render_framesize );
     128             : static int16_t get_render_frame_size_samples( const DECODER_CONFIG_HANDLE hDecoderConfig );
     129             : static int16_t ivas_dec_split_rend_cldfb_in( const RENDERER_TYPE renderer_type );
     130             : static void update_voip_rendered20ms( IVAS_DEC_HANDLE hIvasDec, const int16_t nSamplesRendered );
     131             : 
     132             : 
     133             : /*---------------------------------------------------------------------*
     134             :  * IVAS_DEC_Open()
     135             :  *
     136             :  * Open IVAS decoder
     137             :  *---------------------------------------------------------------------*/
     138             : 
     139             : /* may return an error but may still have allocated memory - thus run Close also in case of error to release memory */
     140       86054 : ivas_error IVAS_DEC_Open(
     141             :     IVAS_DEC_HANDLE *phIvasDec, /* i/o: pointer to an IVAS decoder handle to be opened  */
     142             :     const IVAS_DEC_MODE mode    /* i  : compatibility mode (EVS or IVAS)                */
     143             : )
     144             : {
     145             :     IVAS_DEC_HANDLE hIvasDec;
     146             :     Decoder_Struct *st_ivas;
     147             : 
     148       86054 :     if ( phIvasDec == NULL )
     149             :     {
     150           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     151             :     }
     152             : 
     153             :     /*-----------------------------------------------------------------*
     154             :      * Allocate and initialize IVAS application decoder handle
     155             :      *-----------------------------------------------------------------*/
     156             : 
     157       86054 :     if ( ( *phIvasDec = (IVAS_DEC_HANDLE) malloc( sizeof( struct IVAS_DEC ) ) ) == NULL )
     158             :     {
     159           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IVAS decoder handle" );
     160             :     }
     161             : 
     162       86054 :     hIvasDec = *phIvasDec;
     163       86054 :     hIvasDec->hVoIP = NULL;
     164       86054 :     hIvasDec->hTimeScaler = NULL;
     165       86054 :     hIvasDec->tsm_scale = 100;
     166       86054 :     hIvasDec->tsm_max_scaling = 0;
     167       86054 :     hIvasDec->tsm_quality = 1.0f;
     168       86054 :     hIvasDec->timeScalingDone = 0;
     169       86054 :     hIvasDec->needNewFrame = false;
     170       86054 :     hIvasDec->nTransportChannelsOld = 0;
     171       86054 :     hIvasDec->nSamplesAvailableNext = 0;
     172       86054 :     hIvasDec->nSamplesFrame = 0;
     173       86054 :     hIvasDec->hasBeenFedFrame = false;
     174       86054 :     hIvasDec->hasBeenFedFirstGoodFrame = false;
     175       86054 :     hIvasDec->hasDecodedFirstGoodFrame = false;
     176       86054 :     hIvasDec->isInitialized = false;
     177       86054 :     hIvasDec->updateOrientation = false;
     178       86054 :     hIvasDec->flushbuffer = NULL;
     179       86054 :     hIvasDec->pcmType = IVAS_DEC_PCM_INVALID;
     180       86054 :     hIvasDec->nSamplesFlushed = 0;
     181       86054 :     hIvasDec->hasBeenPreparedRendering = false;
     182             : 
     183       86054 :     hIvasDec->mode = mode;
     184             : 
     185       86054 :     hIvasDec->bitstreamformat = G192;
     186             : #ifdef DEBUGGING
     187             :     hIvasDec->Opt_VOIP = 0;
     188             : #endif
     189       86054 :     hIvasDec->amrwb_rfc4867_flag = -1;
     190       86054 :     hIvasDec->prev_ft_speech = 1; /* RXDTX handler previous frametype flag for G.192 format AMRWB SID_FIRST detection */
     191       86054 :     hIvasDec->CNG = 0;            /* RXDTX handler CNG = 1, no CNG = 0*/
     192             : 
     193             :     /*-----------------------------------------------------------------*
     194             :      * Initialize IVAS-codec decoder state
     195             :      *-----------------------------------------------------------------*/
     196             : 
     197       86054 :     if ( ( hIvasDec->st_ivas = (Decoder_Struct *) malloc( sizeof( Decoder_Struct ) ) ) == NULL )
     198             :     {
     199           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IVAS decoder structure" );
     200             :     }
     201             : 
     202       86054 :     if ( ( hIvasDec->st_ivas->hDecoderConfig = (DECODER_CONFIG_HANDLE) malloc( sizeof( DECODER_CONFIG ) ) ) == NULL )
     203             :     {
     204           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for Decoder config structure" );
     205             :     }
     206             : 
     207             :     /*-----------------------------------------------------------------*
     208             :      * Initialize IVAS-codec decoder state
     209             :      *-----------------------------------------------------------------*/
     210             : 
     211       86054 :     st_ivas = hIvasDec->st_ivas;
     212             : 
     213             :     /* initialize Decoder Config. handle */
     214       86054 :     init_decoder_config( hIvasDec->st_ivas->hDecoderConfig );
     215             : 
     216             :     /* initialize pointers to handles to NULL */
     217       86054 :     ivas_initialize_handles_dec( st_ivas );
     218             : 
     219       86054 :     st_ivas->restartNeeded = 0;
     220             : 
     221             :     /* set high-level parameters */
     222       86054 :     if ( mode == IVAS_DEC_MODE_EVS )
     223             :     {
     224         495 :         st_ivas->codec_mode = 0; /* unknown before first frame */
     225         495 :         st_ivas->element_mode_init = EVS_MONO;
     226         495 :         st_ivas->ivas_format = MONO_FORMAT;
     227         495 :         st_ivas->transport_config = IVAS_AUDIO_CONFIG_INVALID;
     228         495 :         st_ivas->intern_config = IVAS_AUDIO_CONFIG_INVALID;
     229         495 :         st_ivas->writeFECoffset = 0;
     230         495 :         hIvasDec->hasDecodedFirstGoodFrame = true; /* Functionality to suppress output for initial lost frames is disabled in EVS operation */
     231             : 
     232         495 :         return IVAS_ERR_OK;
     233             :     }
     234       85559 :     else if ( mode == IVAS_DEC_MODE_IVAS )
     235             :     {
     236       85559 :         st_ivas->codec_mode = 0; /* unknown before first frame */
     237       85559 :         st_ivas->element_mode_init = -1;
     238       85559 :         st_ivas->ivas_format = UNDEFINED_FORMAT;
     239       85559 :         st_ivas->transport_config = IVAS_AUDIO_CONFIG_INVALID;
     240       85559 :         st_ivas->intern_config = IVAS_AUDIO_CONFIG_INVALID;
     241       85559 :         st_ivas->renderer_type = RENDERER_DISABLE;
     242       85559 :         st_ivas->ini_frame = 0;
     243       85559 :         st_ivas->ini_active_frame = 0;
     244       85559 :         st_ivas->writeFECoffset = 0;
     245             : 
     246       85559 :         st_ivas->ism_mode = ISM_MODE_NONE;
     247       85559 :         st_ivas->mc_mode = MC_MODE_NONE;
     248             : 
     249       85559 :         st_ivas->sba_order = 0;
     250       85559 :         st_ivas->sba_planar = 0;
     251       85559 :         st_ivas->sba_analysis_order = 0;
     252             : 
     253       85559 :         return IVAS_ERR_OK;
     254             :     }
     255             : 
     256           0 :     return IVAS_ERR_WRONG_PARAMS;
     257             : }
     258             : 
     259             : 
     260             : /*-------------------------------------------------------------------------*
     261             :  * isar_set_split_rend_setup()
     262             :  *
     263             :  * Setup IVAS split rendering
     264             :  *-------------------------------------------------------------------------*/
     265             : 
     266      286028 : static ivas_error isar_set_split_rend_setup(
     267             :     ISAR_DEC_SPLIT_REND_WRAPPER *hSplitBinRend,
     268             :     const ISAR_SPLIT_REND_CONFIG_DATA *hSplitBinConfig,
     269             :     const COMBINED_ORIENTATION_HANDLE hCombinedOrientationData,
     270             :     ISAR_SPLIT_REND_BITS_DATA *splitRendBits /* o  : output split rendering bits    */
     271             : )
     272             : {
     273      286028 :     splitRendBits->bits_read = 0;
     274      286028 :     splitRendBits->bits_written = 0;
     275      286028 :     splitRendBits->buf_len = ISAR_MAX_SPLIT_REND_BITS_BUFFER_SIZE_IN_BYTES;
     276      286028 :     splitRendBits->codec = ISAR_SPLIT_REND_CODEC_DEFAULT;
     277      286028 :     splitRendBits->pose_correction = ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE;
     278      286028 :     splitRendBits->codec_frame_size_ms = 0;
     279      286028 :     splitRendBits->isar_frame_size_ms = 0;
     280      286028 :     splitRendBits->lc3plus_highres = 0;
     281             : 
     282      286028 :     ISAR_PRE_REND_GetMultiBinPoseData( hSplitBinConfig, &hSplitBinRend->splitrend.multiBinPoseData, ( hCombinedOrientationData != NULL ) ? hCombinedOrientationData->sr_pose_pred_axis : DEFAULT_AXIS );
     283             : 
     284      286028 :     if ( hCombinedOrientationData != NULL )
     285             :     {
     286      286028 :         isar_set_split_rend_ht_setup( &hSplitBinRend->splitrend, hCombinedOrientationData->Quaternions, hCombinedOrientationData->Rmat );
     287             :     }
     288             : 
     289      286028 :     return IVAS_ERR_OK;
     290             : }
     291             : 
     292             : /*---------------------------------------------------------------------*
     293             :  * init_decoder_config()
     294             :  *
     295             :  * Initialize Decoder Config. handle
     296             :  *---------------------------------------------------------------------*/
     297             : 
     298       86054 : static void init_decoder_config(
     299             :     DECODER_CONFIG_HANDLE hDecoderConfig /* i/o: configuration structure */
     300             : )
     301             : {
     302       86054 :     hDecoderConfig->Opt_AMR_WB = 0;
     303       86054 :     hDecoderConfig->nchan_out = 1;
     304       86054 :     hDecoderConfig->output_config = IVAS_AUDIO_CONFIG_INVALID;
     305       86054 :     hDecoderConfig->Opt_LsCustom = 0;
     306       86054 :     hDecoderConfig->Opt_HRTF_binary = 0;
     307       86054 :     hDecoderConfig->Opt_Headrotation = 0;
     308       86054 :     hDecoderConfig->Opt_RendConfigCustom = 0;
     309       86054 :     hDecoderConfig->room_size = IVAS_ROOM_SIZE_AUTO;
     310       86054 :     hDecoderConfig->orientation_tracking = IVAS_HEAD_ORIENT_TRK_NONE;
     311       86054 :     hDecoderConfig->Opt_non_diegetic_pan = 0;
     312       86054 :     hDecoderConfig->non_diegetic_pan_gain = 0;
     313       86054 :     hDecoderConfig->Opt_tsm = 0;
     314       86054 :     hDecoderConfig->Opt_delay_comp = 0;
     315       86054 :     hDecoderConfig->Opt_ExternalOrientation = 0;
     316       86054 :     hDecoderConfig->Opt_dpid_on = 0;
     317       86054 :     hDecoderConfig->Opt_aeid_on = 0;
     318       86054 :     hDecoderConfig->Opt_ObjEdit_on = 0;
     319             : 
     320       86054 :     return;
     321             : }
     322             : 
     323             : 
     324             : /*---------------------------------------------------------------------*
     325             :  * IVAS_DEC_Close( )
     326             :  *
     327             :  * Deallocate IVAS decoder memory handles
     328             :  *---------------------------------------------------------------------*/
     329             : 
     330       86054 : void IVAS_DEC_Close(
     331             :     IVAS_DEC_HANDLE *phIvasDec /* i/o: pointer to IVAS decoder handle   */
     332             : )
     333             : {
     334             :     /* Free all memory */
     335       86054 :     if ( phIvasDec == NULL || *phIvasDec == NULL )
     336             :     {
     337           0 :         return;
     338             :     }
     339             : 
     340       86054 :     if ( ( *phIvasDec )->hVoIP )
     341             :     {
     342       38234 :         ivas_destroy_handle_VoIP( ( *phIvasDec )->hVoIP );
     343       38234 :         ( *phIvasDec )->hVoIP = NULL;
     344             :     }
     345             : 
     346       86054 :     if ( ( *phIvasDec )->st_ivas )
     347             :     {
     348             :         /* destroy Split binaural renderer (ISAR) handle */
     349       86054 :         ivas_destroy_handle_isar( &( *phIvasDec )->st_ivas->hSplitBinRend );
     350             : 
     351             :         /* destroy IVAS decoder handles */
     352       86054 :         ivas_destroy_dec( ( *phIvasDec )->st_ivas );
     353       86054 :         ( *phIvasDec )->st_ivas = NULL;
     354             :     }
     355             : 
     356       86054 :     apa_exit( &( *phIvasDec )->hTimeScaler );
     357             : 
     358       86054 :     if ( ( *phIvasDec )->flushbuffer != NULL )
     359             :     {
     360       39274 :         free( ( *phIvasDec )->flushbuffer );
     361             :     }
     362             : 
     363       86054 :     free( *phIvasDec );
     364       86054 :     *phIvasDec = NULL;
     365       86054 :     phIvasDec = NULL;
     366             : 
     367       86054 :     return;
     368             : }
     369             : 
     370             : 
     371             : /*---------------------------------------------------------------------*
     372             :  * mapIvasFormat( )
     373             :  *
     374             :  *
     375             :  *---------------------------------------------------------------------*/
     376             : 
     377        5618 : static IVAS_DEC_BS_FORMAT mapIvasFormat(
     378             :     const IVAS_FORMAT ivas_format )
     379             : {
     380        5618 :     switch ( ivas_format )
     381             :     {
     382           0 :         case MONO_FORMAT:
     383           0 :             return IVAS_DEC_BS_MONO;
     384         186 :         case STEREO_FORMAT:
     385         186 :             return IVAS_DEC_BS_STEREO;
     386        1069 :         case ISM_FORMAT:
     387        1069 :             return IVAS_DEC_BS_OBJ;
     388         508 :         case MC_FORMAT:
     389         508 :             return IVAS_DEC_BS_MC;
     390         787 :         case SBA_FORMAT:
     391         787 :             return IVAS_DEC_BS_SBA;
     392        2027 :         case SBA_ISM_FORMAT:
     393        2027 :             return IVAS_DEC_BS_SBA_ISM;
     394         535 :         case MASA_FORMAT:
     395         535 :             return IVAS_DEC_BS_MASA;
     396         506 :         case MASA_ISM_FORMAT:
     397         506 :             return IVAS_DEC_BS_MASA_ISM;
     398           0 :         default:
     399           0 :             break;
     400             :     }
     401             : 
     402           0 :     return IVAS_DEC_BS_UNKOWN;
     403             : }
     404             : 
     405             : 
     406             : /*---------------------------------------------------------------------*
     407             :  * create_flush_buffer()
     408             :  *
     409             :  * Create flush buffer - needed for binaural outputs with TSM or in VoIP mode
     410             :  *---------------------------------------------------------------------*/
     411             : 
     412       38234 : static ivas_error create_flush_buffer(
     413             :     IVAS_DEC_HANDLE hIvasDec /* i/o: IVAS decoder handle            */
     414             : )
     415             : {
     416       38234 :     hIvasDec->flushbuffer = (void *) malloc( CPE_CHANNELS * hIvasDec->nSamplesFrame / IVAS_MAX_PARAM_SPATIAL_SUBFRAMES * sizeof( int16_t ) );
     417       38234 :     if ( hIvasDec->flushbuffer == NULL )
     418             :     {
     419           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate JBM flush buffer" );
     420             :     }
     421             : 
     422       38234 :     hIvasDec->pcmType = IVAS_DEC_PCM_INT16;
     423       38234 :     set_s( (int16_t *) hIvasDec->flushbuffer, 0, CPE_CHANNELS * hIvasDec->nSamplesFrame / IVAS_MAX_PARAM_SPATIAL_SUBFRAMES );
     424             : 
     425       38234 :     return IVAS_ERR_OK;
     426             : }
     427             : 
     428             : 
     429             : /*---------------------------------------------------------------------*
     430             :  * IVAS_DEC_Configure( )
     431             :  *
     432             :  * Decoder configuration
     433             :  * legacy behavior: if no output format set, then it's EVS mono
     434             :  *---------------------------------------------------------------------*/
     435             : 
     436       86054 : ivas_error IVAS_DEC_Configure(
     437             :     IVAS_DEC_HANDLE hIvasDec,                          /* i/o: IVAS decoder handle                        */
     438             :     const uint32_t sampleRate,                         /* i  : output sampling frequency                  */
     439             :     const AUDIO_CONFIG outputConfig,                   /* i  : output configuration                       */
     440             :     const IVAS_RENDER_FRAMESIZE renderFramesize,       /* i  : rendering frame size                       */
     441             :     const bool customLsOutputEnabled,                  /* i  : enable custom loudspeaker setup handle     */
     442             :     const bool hrtfReaderEnabled,                      /* i  : enable HRTF binary file input              */
     443             :     const bool enableHeadRotation,                     /* i  : enable head rotation for binaural output   */
     444             :     const bool enableExternalOrientation,              /* i  : enable external orientations               */
     445             :     const IVAS_HEAD_ORIENT_TRK_T orientation_tracking, /* i  : head orientation tracking type             */
     446             :     const bool renderConfigEnabled,                    /* i  : enable Renderer config. file for binaural output */
     447             :     const IVAS_ROOM_SIZE_T roomSize,                   /* i  : room size selector for reverb */
     448             :     const bool non_diegetic_pan_enabled,               /* i  : enabled diegetic panning                         */
     449             :     const float non_diegetic_pan_gain,                 /* i  : non diegetic panning gain                        */
     450             :     const bool dpidEnabled,                            /* i  : enable directivity pattern option                */
     451             :     const uint16_t acousticEnvironmentId,              /* i  : Acoustic environment ID                          */
     452             :     const bool objEditEnabled,                         /* i  : enable object editing                            */
     453             :     const bool delayCompensationEnabled                /* i  : enable delay compensation                        */
     454             : )
     455             : {
     456             :     Decoder_Struct *st_ivas;
     457             :     DECODER_CONFIG_HANDLE hDecoderConfig;
     458             :     ivas_error error;
     459             : 
     460       86054 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
     461             :     {
     462           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     463             :     }
     464             : 
     465       86054 :     if ( sampleRate != 8000 && sampleRate != 16000 && sampleRate != 32000 && sampleRate != 48000 )
     466             :     {
     467           0 :         return IVAS_ERR_WRONG_PARAMS;
     468             :     }
     469             : 
     470       86054 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS &&
     471         495 :          ( outputConfig == IVAS_AUDIO_CONFIG_INVALID ||
     472         495 :            outputConfig == IVAS_AUDIO_CONFIG_ISM1 ||
     473         495 :            outputConfig == IVAS_AUDIO_CONFIG_ISM2 ||
     474         495 :            outputConfig == IVAS_AUDIO_CONFIG_ISM3 ||
     475         495 :            outputConfig == IVAS_AUDIO_CONFIG_ISM4 ||
     476         495 :            outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ||
     477         495 :            outputConfig == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ||
     478         495 :            outputConfig == IVAS_AUDIO_CONFIG_MASA1 ||
     479             :            outputConfig == IVAS_AUDIO_CONFIG_MASA2 ) )
     480             :     {
     481           0 :         return IVAS_ERR_WRONG_MODE;
     482             :     }
     483             : 
     484       86054 :     st_ivas = hIvasDec->st_ivas;
     485             : 
     486       86054 :     hDecoderConfig = st_ivas->hDecoderConfig;
     487             : 
     488       86054 :     hDecoderConfig->output_config = outputConfig;
     489       86054 :     if ( hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_INVALID )
     490             :     {
     491           0 :         return IVAS_ERR_WRONG_PARAMS;
     492             :     }
     493             : 
     494       86054 :     hDecoderConfig->output_Fs = sampleRate;
     495             : 
     496       86054 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
     497             :     {
     498         495 :         st_ivas->element_mode_init = EVS_MONO;
     499         495 :         hDecoderConfig->nchan_out = 1;
     500             :     }
     501             : 
     502       86054 :     if ( outputConfig != IVAS_AUDIO_CONFIG_EXTERNAL && outputConfig != IVAS_AUDIO_CONFIG_LS_CUSTOM )
     503             :     {
     504       80424 :         hDecoderConfig->nchan_out = audioCfg2channels( hDecoderConfig->output_config );
     505             :     }
     506             : 
     507       86054 :     hDecoderConfig->Opt_LsCustom = (int16_t) customLsOutputEnabled;
     508       86054 :     hDecoderConfig->Opt_Headrotation = (int16_t) enableHeadRotation;
     509       86054 :     hDecoderConfig->orientation_tracking = orientation_tracking;
     510       86054 :     hDecoderConfig->Opt_HRTF_binary = (int16_t) hrtfReaderEnabled;
     511       86054 :     hDecoderConfig->Opt_RendConfigCustom = (int16_t) renderConfigEnabled;
     512       86054 :     hDecoderConfig->room_size = roomSize;
     513       86054 :     hDecoderConfig->Opt_non_diegetic_pan = (int16_t) non_diegetic_pan_enabled;
     514       86054 :     hDecoderConfig->non_diegetic_pan_gain = non_diegetic_pan_gain;
     515       86054 :     hDecoderConfig->Opt_delay_comp = (int16_t) delayCompensationEnabled;
     516       86054 :     hDecoderConfig->Opt_ExternalOrientation = enableExternalOrientation;
     517       86054 :     hDecoderConfig->Opt_dpid_on = (int16_t) dpidEnabled;
     518       86054 :     hDecoderConfig->Opt_aeid_on = acousticEnvironmentId != 65535 ? TRUE : FALSE;
     519       86054 :     hDecoderConfig->Opt_ObjEdit_on = (int16_t) objEditEnabled;
     520             : 
     521       86054 :     if ( renderFramesize == IVAS_RENDER_FRAMESIZE_UNKNOWN )
     522             :     {
     523           0 :         return IVAS_ERR_WRONG_PARAMS;
     524             :     }
     525       86054 :     if ( outputConfig == IVAS_AUDIO_CONFIG_EXTERNAL )
     526             :     {
     527        5621 :         hDecoderConfig->render_framesize = IVAS_RENDER_FRAMESIZE_20MS;
     528             :     }
     529             :     else
     530             :     {
     531       80433 :         hDecoderConfig->render_framesize = renderFramesize;
     532             :     }
     533             : 
     534             :     /* Set decoder parameters to initial values */
     535       86054 :     if ( ( error = ivas_init_decoder_front( st_ivas ) ) != IVAS_ERR_OK )
     536             :     {
     537           0 :         return error;
     538             :     }
     539             : 
     540             :     /* create ISAR handle */
     541       86054 :     if ( hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
     542             :     {
     543        2088 :         if ( ( error = ivas_create_handle_isar( &st_ivas->hSplitBinRend ) ) != IVAS_ERR_OK )
     544             :         {
     545           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for ISAR handle" );
     546             :         }
     547             :     }
     548             : 
     549       86054 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
     550             :     {
     551         495 :         hIvasDec->st_ivas->ivas_format = MONO_FORMAT;
     552             :     }
     553             : 
     554       86054 :     hIvasDec->nSamplesFrame = (uint16_t) ( hDecoderConfig->output_Fs / FRAMES_PER_SEC );
     555             : 
     556       86054 :     return IVAS_ERR_OK;
     557             : }
     558             : 
     559             : 
     560             : /*---------------------------------------------------------------------*
     561             :  * IVAS_DEC_EnableSplitRendering( )
     562             :  *
     563             :  * Update IVAS decoder config. if Split rendering is enabled
     564             :  *---------------------------------------------------------------------*/
     565             : 
     566        2088 : ivas_error IVAS_DEC_EnableSplitRendering(
     567             :     IVAS_DEC_HANDLE hIvasDec /* i/o: IVAS decoder handle    */
     568             : )
     569             : {
     570             :     DECODER_CONFIG_HANDLE hDecoderConfig;
     571             : 
     572        2088 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
     573             :     {
     574           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     575             :     }
     576             : 
     577        2088 :     hDecoderConfig = hIvasDec->st_ivas->hDecoderConfig;
     578             : 
     579        2088 :     hDecoderConfig->Opt_Headrotation = 1;
     580        2088 :     hDecoderConfig->render_framesize = IVAS_RENDER_FRAMESIZE_20MS;
     581             : 
     582        2088 :     return IVAS_ERR_OK;
     583             : }
     584             : 
     585             : 
     586             : /*---------------------------------------------------------------------*
     587             :  * get_render_framesize_ms( )
     588             :  *
     589             :  * Get render framesize in ms
     590             :  *---------------------------------------------------------------------*/
     591             : 
     592       64186 : static int16_t get_render_frame_size_ms(
     593             :     const IVAS_RENDER_FRAMESIZE render_framesize )
     594             : {
     595       64186 :     return (int16_t) ( render_framesize * ( 1000 / ( FRAMES_PER_SEC * IVAS_MAX_PARAM_SPATIAL_SUBFRAMES ) ) );
     596             : }
     597             : 
     598             : 
     599             : /*---------------------------------------------------------------------*
     600             :  * IVAS_DEC_SetRenderFramesize( )
     601             :  *
     602             :  * Set render framesize
     603             :  *---------------------------------------------------------------------*/
     604             : 
     605        2080 : ivas_error IVAS_DEC_SetRenderFramesize(
     606             :     IVAS_DEC_HANDLE hIvasDec,                    /* i/o: IVAS decoder handle   */
     607             :     const IVAS_RENDER_FRAMESIZE render_framesize /* i  : render framesize      */
     608             : )
     609             : {
     610        2080 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hDecoderConfig == NULL )
     611             :     {
     612           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     613             :     }
     614             : 
     615        2080 :     hIvasDec->st_ivas->hDecoderConfig->render_framesize = render_framesize;
     616             : 
     617        2080 :     if ( hIvasDec->st_ivas->hExtOrientationData != NULL )
     618             :     {
     619           0 :         hIvasDec->st_ivas->hExtOrientationData->num_subframes = (int16_t) render_framesize;
     620             :     }
     621             : 
     622        2080 :     if ( hIvasDec->st_ivas->hCombinedOrientationData != NULL )
     623             :     {
     624        2080 :         hIvasDec->st_ivas->hCombinedOrientationData->num_subframes = (int16_t) render_framesize;
     625             :     }
     626             : 
     627        2080 :     return IVAS_ERR_OK;
     628             : }
     629             : 
     630             : 
     631             : /*---------------------------------------------------------------------*
     632             :  * IVAS_DEC_GetGetRenderFramesize( )
     633             :  *
     634             :  * Get render framesize
     635             :  *---------------------------------------------------------------------*/
     636             : 
     637       88142 : ivas_error IVAS_DEC_GetRenderFramesize(
     638             :     IVAS_DEC_HANDLE hIvasDec,               /* i/o: IVAS decoder handle   */
     639             :     IVAS_RENDER_FRAMESIZE *render_framesize /* o  : render framesize      */
     640             : )
     641             : {
     642       88142 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || render_framesize == NULL )
     643             :     {
     644           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     645             :     }
     646             : 
     647       88142 :     *render_framesize = hIvasDec->st_ivas->hDecoderConfig->render_framesize;
     648             : 
     649       88142 :     return IVAS_ERR_OK;
     650             : }
     651             : 
     652             : 
     653             : /*---------------------------------------------------------------------*
     654             :  * get_render_frame_size_samples( )
     655             :  *
     656             :  *
     657             :  *---------------------------------------------------------------------*/
     658             : 
     659      536667 : static int16_t get_render_frame_size_samples(
     660             :     const DECODER_CONFIG_HANDLE hDecoderConfig /* i  : configuration structure */
     661             : )
     662             : {
     663      536667 :     return (int16_t) ( hDecoderConfig->output_Fs * hDecoderConfig->render_framesize / ( FRAMES_PER_SEC * IVAS_MAX_PARAM_SPATIAL_SUBFRAMES ) );
     664             : }
     665             : 
     666             : 
     667             : /*---------------------------------------------------------------------*
     668             :  * IVAS_DEC_GetGetRenderFramesizeSamples( )
     669             :  *
     670             :  * Get render framesize in samples
     671             :  *---------------------------------------------------------------------*/
     672             : 
     673      536203 : ivas_error IVAS_DEC_GetRenderFramesizeSamples(
     674             :     IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle          */
     675             :     int16_t *render_framesize /* o  : render framesize in samples  */
     676             : )
     677             : {
     678      536203 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || render_framesize == NULL )
     679             :     {
     680           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     681             :     }
     682             : 
     683      536203 :     *render_framesize = get_render_frame_size_samples( hIvasDec->st_ivas->hDecoderConfig );
     684             : 
     685      536203 :     return IVAS_ERR_OK;
     686             : }
     687             : 
     688             : 
     689             : /*---------------------------------------------------------------------*
     690             :  * IVAS_DEC_GetGetRenderFramesizeMs( )
     691             :  *
     692             :  * Get render framesize in milliseconds
     693             :  *---------------------------------------------------------------------*/
     694             : 
     695       38213 : ivas_error IVAS_DEC_GetRenderFramesizeMs(
     696             :     IVAS_DEC_HANDLE hIvasDec,  /* i/o: IVAS decoder handle          */
     697             :     uint32_t *render_framesize /* o  : render framesize in samples  */
     698             : )
     699             : {
     700       38213 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || render_framesize == NULL )
     701             :     {
     702           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     703             :     }
     704             : 
     705       38213 :     *render_framesize = get_render_frame_size_ms( hIvasDec->st_ivas->hDecoderConfig->render_framesize );
     706             : 
     707       38213 :     return IVAS_ERR_OK;
     708             : }
     709             : 
     710             : 
     711             : /*---------------------------------------------------------------------*
     712             :  * IVAS_DEC_GetGetReferencesUpdateFrequency( )
     713             :  *
     714             :  * Get update frequency of the reference vector/orientation
     715             :  *---------------------------------------------------------------------*/
     716             : 
     717       88121 : ivas_error IVAS_DEC_GetReferencesUpdateFrequency(
     718             :     IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle          */
     719             :     int16_t *update_frequency /* o  : update frequency             */
     720             : )
     721             : {
     722       88121 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || update_frequency == NULL )
     723             :     {
     724           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     725             :     }
     726             : 
     727       88121 :     *update_frequency = (int16_t) ( IVAS_MAX_PARAM_SPATIAL_SUBFRAMES / hIvasDec->st_ivas->hDecoderConfig->render_framesize );
     728             : 
     729       88121 :     return IVAS_ERR_OK;
     730             : }
     731             : 
     732             : /*---------------------------------------------------------------------*
     733             :  * IVAS_DEC_GetGetNumOrientationSubframes( )
     734             :  *
     735             :  * Get the number of subframes for head/external orientation per render frame
     736             :  *---------------------------------------------------------------------*/
     737             : 
     738     8331517 : ivas_error IVAS_DEC_GetNumOrientationSubframes(
     739             :     IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle   */
     740             :     int16_t *num_subframes    /* o  : render framesize      */
     741             : )
     742             : {
     743     8331517 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || num_subframes == NULL )
     744             :     {
     745           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     746             :     }
     747             : 
     748     8331517 :     *num_subframes = (int16_t) hIvasDec->st_ivas->hDecoderConfig->render_framesize;
     749             : 
     750     8331517 :     return IVAS_ERR_OK;
     751             : }
     752             : 
     753             : 
     754             : /*---------------------------------------------------------------------*
     755             :  * IVAS_DEC_EnableVoIP( )
     756             :  *
     757             :  * Intitialize JBM
     758             :  * jbmSafetyMargin: allowed delay reserve in addition to network jitter
     759             :  * to reduce late-loss, default: 60 [milliseconds]
     760             :  *---------------------------------------------------------------------*/
     761             : 
     762       38234 : ivas_error IVAS_DEC_EnableVoIP(
     763             :     IVAS_DEC_HANDLE hIvasDec,               /* i/o: IVAS decoder handle                             */
     764             :     const int16_t jbmSafetyMargin,          /* i  : allowed delay reserve for JBM, in milliseconds  */
     765             :     const IVAS_DEC_INPUT_FORMAT inputFormat /* i  : format of the input bitstream                   */
     766             : )
     767             : {
     768             :     DECODER_CONFIG_HANDLE hDecoderConfig;
     769             :     ivas_error error;
     770             : 
     771       38234 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
     772             :     {
     773           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     774             :     }
     775             : 
     776       38234 :     hDecoderConfig = hIvasDec->st_ivas->hDecoderConfig;
     777             : 
     778             : #ifdef DEBUGGING
     779             :     hIvasDec->Opt_VOIP = 1;
     780             : #endif
     781       38234 :     hDecoderConfig->Opt_tsm = 1;
     782             : 
     783       38234 :     if ( hDecoderConfig->output_config != IVAS_AUDIO_CONFIG_EXTERNAL )
     784             :     {
     785       35547 :         hDecoderConfig->nchan_out = audioCfg2channels( hDecoderConfig->output_config );
     786             :     }
     787             : #ifdef VARIABLE_SPEED_DECODING
     788             :     else
     789             :     {
     790             :         hDecoderConfig->nchan_out = 1;
     791             :     }
     792             : #endif
     793             : 
     794       38234 :     if ( ( error = input_format_API_to_internal( inputFormat, &hIvasDec->bitstreamformat, &hIvasDec->sdp_hf_only, true ) ) != IVAS_ERR_OK )
     795             :     {
     796           0 :         return error;
     797             :     }
     798             : 
     799       38234 :     if ( ( hIvasDec->hVoIP = malloc( sizeof( IVAS_DEC_VOIP ) ) ) == NULL )
     800             :     {
     801           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate VoIP handle" );
     802             :     }
     803             : 
     804       38234 :     hIvasDec->hVoIP->lastDecodedWasActive = 0;
     805       38234 :     hIvasDec->hVoIP->hCurrentDataUnit = NULL;
     806       38234 :     hIvasDec->hVoIP->nSamplesRendered20ms = 0;
     807             : 
     808             : #define WMC_TOOL_SKIP
     809             :     /* Bitstream conversion is not counted towards complexity and memory usage */
     810       38234 :     hIvasDec->hVoIP->bs_conversion_buf = malloc( sizeof( uint16_t ) * ( MAX_BITS_PER_FRAME + 4 * 8 ) );
     811             : #undef WMC_TOOL_SKIP
     812             : 
     813       38234 :     if ( hIvasDec->hVoIP->bs_conversion_buf == NULL )
     814             :     {
     815           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate VoIP handle" );
     816             :     }
     817             : 
     818             :     /* initialize JBM */
     819       38234 :     if ( ( error = JB4_Create( &hIvasDec->hVoIP->hJBM ) != IVAS_ERR_OK ) != IVAS_ERR_OK )
     820             :     {
     821           0 :         return error;
     822             :     }
     823             : 
     824       38234 :     if ( JB4_Init( hIvasDec->hVoIP->hJBM, jbmSafetyMargin ) != 0 )
     825             :     {
     826           0 :         return IVAS_ERR_FAILED_ALLOC;
     827             :     }
     828             : 
     829             : #ifdef NONBE_1122_KEEP_EVS_MODE_UNCHANGED
     830       38234 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
     831             :     {
     832         246 :         JB4_TMP_SetEvsCompatFlag( hIvasDec->hVoIP->hJBM );
     833             :     }
     834             : #endif
     835             : 
     836             :     /* init flush buffer (needed for binaural outputs) */
     837       38234 :     if ( ( error = create_flush_buffer( hIvasDec ) ) != IVAS_ERR_OK )
     838             :     {
     839           0 :         fprintf( stderr, "\nError in create_flush_buffer , code: %d\n", error );
     840           0 :         return error;
     841             :     }
     842             : 
     843       38234 :     return IVAS_ERR_OK;
     844             : }
     845             : 
     846             : 
     847             : /*---------------------------------------------------------------------*
     848             :  * IVAS_DEC_FeedFrame_Serial( )
     849             :  *
     850             :  *
     851             :  *---------------------------------------------------------------------*/
     852             : 
     853     5978995 : ivas_error IVAS_DEC_FeedFrame_Serial(
     854             :     IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle                   */
     855             :     uint16_t *serial,         /* i  : buffer containing serial input bitstream. Each bit should be stored as a single uint16_t value  */
     856             :     const uint16_t num_bits,  /* i  : number of bits in input bitstream     */
     857             :     int16_t bfi               /* i  : bad frame indicator flag              */
     858             : )
     859             : {
     860             :     ivas_error error;
     861             : 
     862     5978995 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
     863             :     {
     864           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
     865             :     }
     866             : 
     867     5978995 :     if ( !hIvasDec->isInitialized )
     868             :     {
     869             :         /* Once first frame is fed, finish initialization in EVS Mono.
     870             :          * In IVAS mode, initialization is done in ivas_dec(). */
     871       86273 :         if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
     872             :         {
     873         495 :             hIvasDec->st_ivas->hDecoderConfig->ivas_total_brate = ACELP_8k00;
     874         495 :             hIvasDec->st_ivas->transport_config = IVAS_AUDIO_CONFIG_MONO;
     875             : 
     876         495 :             if ( ( error = ivas_init_decoder( hIvasDec->st_ivas ) ) != IVAS_ERR_OK )
     877             :             {
     878           0 :                 return error;
     879             :             }
     880             : 
     881         495 :             if ( hIvasDec->hVoIP != NULL && hIvasDec->hVoIP->hCurrentDataUnit != NULL )
     882             :             {
     883           0 :                 DEC_CORE_HANDLE st = hIvasDec->st_ivas->hSCE[0]->hCoreCoder[0];
     884           0 :                 st->ini_frame = 0;
     885           0 :                 st->prev_use_partial_copy = 0;
     886           0 :                 hIvasDec->st_ivas->hDecoderConfig->ivas_total_brate = hIvasDec->hVoIP->hCurrentDataUnit->dataSize * FRAMES_PER_SEC;
     887             :             }
     888             : 
     889         495 :             hIvasDec->isInitialized = true;
     890             :         }
     891             :     }
     892             : 
     893     5978995 :     if ( !bfi ) /* TODO(mcjbm): Is this ok for bfi == 2 (partial frame)? Is there enough info to fully configure decoder? */
     894             :     {
     895     5518823 :         hIvasDec->hasBeenFedFirstGoodFrame = true;
     896             :     }
     897             : 
     898             :     /* Update redundant frame information in EVS (pre- read indices) */
     899     5978995 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS && hIvasDec->hVoIP != NULL && hIvasDec->hVoIP->hCurrentDataUnit != NULL )
     900             :     {
     901       17910 :         DEC_CORE_HANDLE st = hIvasDec->st_ivas->hSCE[0]->hCoreCoder[0];
     902       17910 :         st->bit_stream = serial;
     903             : 
     904       17910 :         if ( hIvasDec->hVoIP->hCurrentDataUnit->partial_frame || st->prev_use_partial_copy )
     905             :         {
     906         288 :             st->next_coder_type = hIvasDec->hVoIP->hCurrentDataUnit->nextCoderType;
     907             :         }
     908             :         else
     909             :         {
     910       17622 :             st->next_coder_type = INACTIVE;
     911             :         }
     912             : 
     913       17910 :         if ( hIvasDec->hVoIP->hCurrentDataUnit->partial_frame == 1 && bfi != 1 )
     914             :         {
     915         184 :             bfi = 2;
     916             :         }
     917             :     }
     918             : 
     919     5978995 :     if ( ( error = read_indices( hIvasDec->st_ivas, serial, num_bits, &hIvasDec->prev_ft_speech, &hIvasDec->CNG, bfi ) ) != IVAS_ERR_OK )
     920             :     {
     921           0 :         return error;
     922             :     }
     923             : 
     924             :     /* Update redundant frame information in EVS (post- read indices) */
     925     5978995 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS &&
     926       41962 :          hIvasDec->hVoIP != NULL &&
     927       20662 :          hIvasDec->hVoIP->hCurrentDataUnit != NULL &&
     928       17910 :          hIvasDec->hVoIP->hCurrentDataUnit->partial_frame != 0 )
     929             :     {
     930         192 :         DEC_CORE_HANDLE st = hIvasDec->st_ivas->hSCE[0]->hCoreCoder[0];
     931         192 :         st->codec_mode = MODE2;
     932         192 :         st->use_partial_copy = 1;
     933             :     }
     934             : 
     935     5978995 :     hIvasDec->needNewFrame = false;
     936     5978995 :     hIvasDec->hasBeenFedFrame = true;
     937     5978995 :     hIvasDec->nSamplesAvailableNext = hIvasDec->nSamplesFrame;
     938             : 
     939     5978995 :     return IVAS_ERR_OK;
     940             : }
     941             : 
     942             : 
     943             : /*---------------------------------------------------------------------*
     944             :  * renderer_type_to_mode()
     945             :  *
     946             :  * Convert (codec library) renderer type to (API public) binaural renderer mode
     947             :  *---------------------------------------------------------------------*/
     948             : 
     949             : /*! r: binaural renderer mode (API type) */
     950    10966082 : static IVAS_BIN_RENDERER_TYPE renderer_type_to_mode(
     951             :     const RENDERER_TYPE renderer_type /* i  : renderer type (codec library type) */
     952             : )
     953             : {
     954             :     IVAS_BIN_RENDERER_TYPE binaural_renderer;
     955             : 
     956    10966082 :     switch ( renderer_type )
     957             :     {
     958      567485 :         case RENDERER_BINAURAL_OBJECTS_TD:
     959      567485 :             binaural_renderer = IVAS_BIN_RENDERER_TYPE_TDREND;
     960      567485 :             break;
     961      184101 :         case RENDERER_BINAURAL_MIXER_CONV:
     962             :         case RENDERER_BINAURAL_MIXER_CONV_ROOM:
     963      184101 :             binaural_renderer = IVAS_BIN_RENDERER_TYPE_CREND;
     964      184101 :             break;
     965      362146 :         case RENDERER_BINAURAL_FASTCONV:
     966      362146 :             binaural_renderer = IVAS_BIN_RENDERER_TYPE_FASTCONV;
     967      362146 :             break;
     968      130374 :         case RENDERER_BINAURAL_FASTCONV_ROOM:
     969      130374 :             binaural_renderer = IVAS_BIN_RENDERER_TYPE_FASTCONV;
     970      130374 :             break;
     971      979059 :         case RENDERER_BINAURAL_PARAMETRIC:
     972             :         case RENDERER_BINAURAL_PARAMETRIC_ROOM:
     973      979059 :             binaural_renderer = IVAS_BIN_RENDERER_TYPE_PARAMBIN;
     974      979059 :             break;
     975     8742917 :         default:
     976     8742917 :             binaural_renderer = IVAS_BIN_RENDERER_TYPE_NONE;
     977     8742917 :             break;
     978             :     }
     979             : 
     980    10966082 :     return binaural_renderer;
     981             : }
     982             : 
     983             : 
     984             : /*---------------------------------------------------------------------*
     985             :  * IVAS_DEC_ReadFormat( )
     986             :  *
     987             :  * Read main parameters from the bitstream to set-up the decoder:
     988             :  * - IVAS format
     989             :  * - IVAS format specific signaling
     990             :  * - compensate for renderer granularity change in JBM
     991             :  *---------------------------------------------------------------------*/
     992             : 
     993     5550788 : ivas_error IVAS_DEC_ReadFormat(
     994             :     IVAS_DEC_HANDLE hIvasDec,                      /* i/o: IVAS decoder handle                      */
     995             :     IVAS_BIN_RENDERER_TYPE *binaural_renderer,     /* o  : binaural renderer type                   */
     996             :     IVAS_BIN_RENDERER_TYPE *binaural_renderer_sec, /* o  : secondary binaural renderer type         */
     997             :     IVAS_AUDIO_CONFIG *hrtf_set_audio_cfg          /* o  : HRTF set audio config.                   */
     998             : )
     999             : {
    1000             :     ivas_error error;
    1001             :     Decoder_Struct *st_ivas;
    1002             :     ISM_MODE ism_mode_old;
    1003             :     MC_MODE mc_mode_old;
    1004             :     int16_t nchan_transport_old;
    1005             :     AUDIO_CONFIG intern_config_old, transport_config_old, output_config;
    1006             :     RENDERER_TYPE renderer_type_old, renderer_type_sec_new, renderer_type_sec_old;
    1007             : 
    1008     5550788 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    1009             :     {
    1010           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1011             :     }
    1012             : 
    1013     5550788 :     st_ivas = hIvasDec->st_ivas;
    1014     5550788 :     ism_mode_old = st_ivas->ism_mode;
    1015     5550788 :     mc_mode_old = st_ivas->mc_mode;
    1016     5550788 :     nchan_transport_old = st_ivas->nchan_transport;
    1017     5550788 :     intern_config_old = st_ivas->intern_config;
    1018     5550788 :     transport_config_old = st_ivas->transport_config;
    1019     5550788 :     renderer_type_old = st_ivas->renderer_type;
    1020     5550788 :     renderer_type_sec_old = ivas_renderer_secondary_select( st_ivas );
    1021             : 
    1022     5550788 :     output_config = st_ivas->hDecoderConfig->output_config;
    1023             : 
    1024     5550788 :     if ( st_ivas->ivas_format == MONO_FORMAT )
    1025             :     {
    1026       36754 :         return IVAS_ERR_OK;
    1027             :     }
    1028             : 
    1029     5514034 :     if ( st_ivas->bfi == 0 )
    1030             :     {
    1031     5483041 :         if ( ( error = ivas_dec_get_format( st_ivas ) ) != IVAS_ERR_OK )
    1032             :         {
    1033           0 :             return error;
    1034             :         }
    1035             : 
    1036     5483041 :         if ( st_ivas->restartNeeded == 1 )
    1037             :         {
    1038           0 :             return IVAS_ERR_OK;
    1039             :         }
    1040             : 
    1041             :         /* Select binaural renderer */
    1042     5483041 :         ivas_renderer_select( st_ivas );
    1043     5483041 :         *binaural_renderer = renderer_type_to_mode( st_ivas->renderer_type );
    1044             : 
    1045             :         /* Select secondary binaural renderer (used in combined formats) */
    1046     5483041 :         renderer_type_sec_new = ivas_renderer_secondary_select( st_ivas );
    1047     5483041 :         *binaural_renderer_sec = renderer_type_to_mode( renderer_type_sec_new );
    1048             : 
    1049             :         /* select HRTF audio configuration to load the right HRTF set for the external binary file */
    1050     5483041 :         *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_INVALID;
    1051     5483041 :         if ( *binaural_renderer == IVAS_BIN_RENDERER_TYPE_FASTCONV )
    1052             :         {
    1053      492520 :             if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
    1054             :             {
    1055             :                 /* SHD HRIRs */
    1056      432158 :                 *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_HOA3;
    1057             : 
    1058      432158 :                 if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR )
    1059             :                 {
    1060             :                     /* BRIRs */
    1061      117054 :                     *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
    1062             :                 }
    1063             :             }
    1064       60362 :             else if ( st_ivas->ivas_format == MC_FORMAT )
    1065             :             {
    1066             :                 /* HRIRs */
    1067       60362 :                 *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
    1068             : 
    1069       60362 :                 if ( ( st_ivas->hDecoderConfig->Opt_Headrotation ) &&
    1070       30950 :                      !( st_ivas->mc_mode == MC_MODE_PARAMUPMIX && output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR ) )
    1071             :                 {
    1072             :                     /* SHD HRIRs for low complexity rotation */
    1073       30950 :                     *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_HOA3;
    1074             :                 }
    1075             :             }
    1076             :         }
    1077     4990521 :         else if ( *binaural_renderer == IVAS_BIN_RENDERER_TYPE_CREND )
    1078             :         {
    1079      184101 :             if ( st_ivas->ivas_format == ISM_FORMAT )
    1080             :             {
    1081             :                 /* BRIRs */
    1082       96264 :                 *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
    1083             :             }
    1084       87837 :             else if ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCT )
    1085             :             {
    1086             :                 /* BRIRs */
    1087       87837 :                 *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_7_1_4;
    1088             : 
    1089       87837 :                 if ( st_ivas->hDecoderConfig->Opt_Headrotation && ( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB ) )
    1090             :                 {
    1091             :                     /* SHD HRIRs for low complexity rotation */
    1092         627 :                     *hrtf_set_audio_cfg = IVAS_AUDIO_CONFIG_HOA3;
    1093             :                 }
    1094             :             }
    1095             :         }
    1096             : 
    1097             :         /* JBM: compensate when binaural renderer granularity changes (happens in bitrate switching) */
    1098     5483041 :         if ( st_ivas->ini_active_frame > 0 && st_ivas->hDecoderConfig->Opt_tsm &&
    1099     1658694 :              ( ( renderer_type_old != st_ivas->renderer_type ) ||
    1100             :                ( renderer_type_sec_old != renderer_type_sec_new ) ) )
    1101             :         {
    1102        6755 :             int16_t tc_granularity_new = ivas_jbm_dec_get_render_granularity( st_ivas->renderer_type, renderer_type_sec_new, st_ivas->hDecoderConfig->output_Fs );
    1103             : 
    1104        6755 :             st_ivas->nchan_transport = nchan_transport_old;
    1105             : 
    1106        6755 :             if ( st_ivas->hTcBuffer == NULL )
    1107             :             {
    1108           0 :                 return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1109             :             }
    1110             : 
    1111             :             /* when granularity goes down, render what still fits in the new granularity */
    1112        6755 :             if ( tc_granularity_new < st_ivas->hTcBuffer->n_samples_granularity )
    1113             :             {
    1114        1836 :                 if ( ( error = ivas_jbm_dec_flush_renderer( st_ivas, tc_granularity_new, renderer_type_old, intern_config_old, &st_ivas->hIntSetup, mc_mode_old, ism_mode_old, &hIvasDec->nSamplesFlushed, pcm_type_API_to_internal( hIvasDec->pcmType ), hIvasDec->flushbuffer ) ) != IVAS_ERR_OK )
    1115             :                 {
    1116           0 :                     return error;
    1117             :                 }
    1118             :             }
    1119             :         }
    1120             :     }
    1121             : 
    1122     5514034 :     st_ivas->ism_mode = ism_mode_old;
    1123     5514034 :     st_ivas->mc_mode = mc_mode_old;
    1124     5514034 :     st_ivas->nchan_transport = nchan_transport_old;
    1125     5514034 :     st_ivas->intern_config = intern_config_old;
    1126     5514034 :     st_ivas->transport_config = transport_config_old;
    1127     5514034 :     st_ivas->renderer_type = renderer_type_old;
    1128             : 
    1129     5514034 :     return IVAS_ERR_OK;
    1130             : }
    1131             : 
    1132             : 
    1133             : /*---------------------------------------------------------------------*
    1134             :  * IVAS_DEC_GetSamplesDecoder( )
    1135             :  *
    1136             :  * Main function to run setup, decode transport channels, do TSM and feed to renderer.
    1137             :  *---------------------------------------------------------------------*/
    1138             : 
    1139     5976243 : ivas_error IVAS_DEC_GetSamplesDecoder(
    1140             :     IVAS_DEC_HANDLE hIvasDec,                /* i/o: IVAS decoder handle            */
    1141             :     ISAR_SPLIT_REND_BITS_DATA *splitRendBits /* o  : output split rendering bits    */
    1142             : )
    1143             : {
    1144             :     ivas_error error;
    1145             :     Decoder_Struct *st_ivas;
    1146             :     uint16_t nTimeScalerOutSamples;
    1147             :     uint8_t nTransportChannels;
    1148             :     int16_t nResidualSamples, nSamplesTcsScaled;
    1149             :     bool isInitialized_voip;
    1150             : 
    1151     5976243 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    1152             :     {
    1153           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1154             :     }
    1155             : 
    1156     5976243 :     if ( !hIvasDec->hasBeenFedFirstGoodFrame && !hIvasDec->isInitialized ) /* note: 'isInitialized' is related to EVS decoder */
    1157             :     {
    1158         240 :         return IVAS_ERR_OK;
    1159             :     }
    1160             : 
    1161     5976003 :     st_ivas = hIvasDec->st_ivas;
    1162             : 
    1163     5976003 :     isInitialized_voip = hIvasDec->hTimeScaler != NULL;
    1164             : 
    1165     5976003 :     if ( !hIvasDec->isInitialized || hIvasDec->hasBeenFedFrame ) /* wait for the first good frame */
    1166             :     {
    1167             :         /*-----------------------------------------------------------------*
    1168             :          * Setup all decoder parts (IVAS decoder, ISAR)
    1169             :          *-----------------------------------------------------------------*/
    1170             : 
    1171     5976003 :         if ( ( error = ivas_dec_setup_all( hIvasDec, &nTransportChannels, splitRendBits ) ) != IVAS_ERR_OK )
    1172             :         {
    1173           0 :             return error;
    1174             :         }
    1175             : 
    1176             :         /*-----------------------------------------------------------------*
    1177             :          * IVAS decoder: decode transport channels and metadata
    1178             :          *-----------------------------------------------------------------*/
    1179             : 
    1180     5976003 :         if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
    1181             :         {
    1182       39210 :             if ( ( error = evs_dec_main( st_ivas ) ) != IVAS_ERR_OK )
    1183             :             {
    1184           0 :                 return error;
    1185             :             }
    1186             :         }
    1187     5936793 :         else if ( hIvasDec->mode == IVAS_DEC_MODE_IVAS )
    1188             :         {
    1189     5936793 :             if ( ( error = ivas_jbm_dec_tc( st_ivas ) ) != IVAS_ERR_OK )
    1190             :             {
    1191           0 :                 return error;
    1192             :             }
    1193             : 
    1194     5936793 :             hIvasDec->isInitialized = true; /* Initialization done in ivas_dec() */
    1195             :         }
    1196             : 
    1197     5976003 :         if ( hIvasDec->hasBeenFedFirstGoodFrame )
    1198             :         {
    1199     5976003 :             hIvasDec->hasDecodedFirstGoodFrame = true;
    1200             :         }
    1201             : 
    1202             :         /*-----------------------------------------------------------------*
    1203             :          * JBM
    1204             :          *-----------------------------------------------------------------*/
    1205             : 
    1206     5976003 :         if ( st_ivas->hDecoderConfig->Opt_tsm )
    1207             :         {
    1208     2137570 :             if ( nTransportChannels != hIvasDec->nTransportChannelsOld )
    1209             :             {
    1210       53413 :                 if ( ( error = apa_setup( hIvasDec, isInitialized_voip, nTransportChannels ) ) != IVAS_ERR_OK )
    1211             :                 {
    1212           0 :                     return error;
    1213             :                 }
    1214             :             }
    1215             : 
    1216     2137570 :             if ( apa_set_scale( hIvasDec->hTimeScaler, hIvasDec->tsm_scale ) != 0 )
    1217             :             {
    1218           0 :                 return IVAS_ERR_UNKNOWN;
    1219             :             }
    1220             : 
    1221             :             /* convert deinterleaved decoded TC audio channels buffer to an interleaved one */
    1222     2137570 :             ivas_buffer_deinterleaved_to_interleaved( st_ivas->p_output_f, nTransportChannels, hIvasDec->nSamplesFrame, st_ivas->hTcBuffer->tc_buffer );
    1223             : 
    1224             :             /* time-scale modification */
    1225     2137570 :             if ( apa_exec( hIvasDec->hTimeScaler, st_ivas->hTcBuffer->tc_buffer, hIvasDec->nSamplesFrame * nTransportChannels, (uint16_t) hIvasDec->tsm_max_scaling, st_ivas->hTcBuffer->tc_buffer, &nTimeScalerOutSamples ) != 0 )
    1226             :             {
    1227           0 :                 return IVAS_ERR_UNKNOWN;
    1228             :             }
    1229             : 
    1230     2137570 :             assert( nTimeScalerOutSamples <= APA_BUF );
    1231     2137570 :             nSamplesTcsScaled = nTimeScalerOutSamples / nTransportChannels;
    1232     2137570 :             hIvasDec->timeScalingDone = 1;
    1233             : 
    1234             :             /* convert interleaved time-scaled TC audio channels buffer to deinterleaved one */
    1235     2137570 :             ivas_buffer_interleaved_to_deinterleaved( st_ivas->hTcBuffer->tc_buffer, nTransportChannels, nSamplesTcsScaled, NS2SA( st_ivas->hDecoderConfig->output_Fs, MAX_JBM_L_FRAME_NS ) );
    1236             :         }
    1237             :         else
    1238             :         {
    1239     3838433 :             nSamplesTcsScaled = hIvasDec->nSamplesFrame;
    1240             :         }
    1241             : 
    1242             :         /*-----------------------------------------------------------------*
    1243             :          * Feed decoded transport channels samples to the renderer
    1244             :          *-----------------------------------------------------------------*/
    1245             : 
    1246     5976003 :         ivas_jbm_dec_feed_tc_to_renderer( st_ivas, nSamplesTcsScaled, &nResidualSamples );
    1247             : 
    1248     5976003 :         if ( st_ivas->hDecoderConfig->Opt_tsm )
    1249             :         {
    1250             :             /* feed residual samples to TSM for the next call */
    1251     2137570 :             if ( apa_set_renderer_residual_samples( hIvasDec->hTimeScaler, (uint16_t) nResidualSamples ) != 0 )
    1252             :             {
    1253           0 :                 return IVAS_ERR_UNKNOWN;
    1254             :             }
    1255             :         }
    1256             : 
    1257     5976003 :         hIvasDec->hasBeenFedFrame = false;
    1258             :     }
    1259             : 
    1260     5976003 :     hIvasDec->hasBeenPreparedRendering = false;
    1261             : 
    1262             :     /*-----------------------------------------------------------------*
    1263             :      * Set editable metadata
    1264             :      *-----------------------------------------------------------------*/
    1265             : 
    1266     5976003 :     if ( st_ivas->hIsmMetaData[0] )
    1267             :     {
    1268     2487091 :         if ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
    1269             :         {
    1270     2467510 :             if ( st_ivas->ism_mode == ISM_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    1271             :             {
    1272             :                 int16_t obj;
    1273     2242505 :                 ISM_METADATA_HANDLE *hIsmMetaData = st_ivas->hIsmMetaData;
    1274     7962078 :                 for ( obj = 0; obj < st_ivas->nchan_ism; obj++ )
    1275             :                 {
    1276     5719573 :                     hIsmMetaData[obj]->edited_azimuth = hIsmMetaData[obj]->azimuth;
    1277     5719573 :                     hIsmMetaData[obj]->edited_elevation = hIsmMetaData[obj]->elevation;
    1278     5719573 :                     hIsmMetaData[obj]->edited_yaw = hIsmMetaData[obj]->yaw;
    1279     5719573 :                     hIsmMetaData[obj]->edited_pitch = hIsmMetaData[obj]->pitch;
    1280     5719573 :                     hIsmMetaData[obj]->edited_radius = hIsmMetaData[obj]->radius;
    1281     5719573 :                     hIsmMetaData[obj]->edited_gain = 1.0f;
    1282             :                 }
    1283             : 
    1284     2242505 :                 if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC )
    1285             :                 {
    1286      764386 :                     st_ivas->hSbaIsmData->gain_bed = 1.0f;
    1287             :                 }
    1288             :             }
    1289             :         }
    1290             :     }
    1291             : 
    1292     5976003 :     if ( st_ivas->hParamIsmDec != NULL )
    1293             :     {
    1294      150380 :         if ( st_ivas->ism_mode == ISM_MODE_PARAM )
    1295             :         {
    1296      150380 :             int16_t obj = 0;
    1297      150380 :             PARAM_ISM_DEC_HANDLE hParamIsmDec = st_ivas->hParamIsmDec;
    1298      685963 :             for ( obj = 0; obj < st_ivas->nchan_ism; obj++ )
    1299             :             {
    1300      535583 :                 hParamIsmDec->edited_azimuth_values[obj] = hParamIsmDec->azimuth_values[obj];
    1301      535583 :                 hParamIsmDec->edited_elevation_values[obj] = hParamIsmDec->elevation_values[obj];
    1302             :             }
    1303             :         }
    1304             :     }
    1305             : 
    1306     5976003 :     return IVAS_ERR_OK;
    1307             : }
    1308             : 
    1309             : 
    1310             : /*---------------------------------------------------------------------*
    1311             :  * IVAS_DEC_GetEditableParameters( )
    1312             :  *
    1313             :  * Get editable metadata parameters
    1314             :  *---------------------------------------------------------------------*/
    1315             : 
    1316       48081 : ivas_error IVAS_DEC_GetEditableParameters(
    1317             :     IVAS_DEC_HANDLE hIvasDec,                         /* i/o: IVAS decoder handle              */
    1318             :     IVAS_EDITABLE_PARAMETERS *hIvasEditableParameters /* o  : object editing parameters handle */
    1319             : )
    1320             : {
    1321             :     int16_t dirac_read_idx, obj;
    1322             :     Decoder_Struct *st_ivas;
    1323             :     ISM_MODE ism_mode;
    1324             : 
    1325       48081 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasEditableParameters == NULL )
    1326             :     {
    1327           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1328             :     }
    1329             : 
    1330       48081 :     if ( !hIvasDec->hasBeenFedFirstGoodFrame )
    1331             :     {
    1332           3 :         hIvasEditableParameters->num_obj = 0;
    1333             : 
    1334           3 :         return IVAS_ERR_OK;
    1335             :     }
    1336             : 
    1337       48078 :     st_ivas = hIvasDec->st_ivas;
    1338       48078 :     ism_mode = st_ivas->ism_mode;
    1339             : 
    1340       48078 :     if ( !( st_ivas->ivas_format == ISM_FORMAT ||
    1341       21036 :             st_ivas->ivas_format == SBA_ISM_FORMAT ||
    1342       12024 :             st_ivas->ivas_format == MASA_ISM_FORMAT ||
    1343        1002 :             ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->nchan_ism > 0 ) ) )
    1344             :     {
    1345           0 :         return IVAS_ERROR( IVAS_ERR_OBJECTS_EDITING_NOT_SUPPORTED, "Object editing is not supported in this operation mode." );
    1346             :     }
    1347             : 
    1348       48078 :     hIvasEditableParameters->gain_bed = 1.0f;
    1349       48078 :     hIvasEditableParameters->num_obj = st_ivas->nchan_ism;
    1350       48078 :     if ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
    1351             :     {
    1352       36054 :         if ( ism_mode == ISM_MODE_DISC || ism_mode == ISM_SBA_MODE_DISC )
    1353             :         {
    1354       94953 :             for ( obj = 0; obj < hIvasEditableParameters->num_obj; obj++ )
    1355             :             {
    1356       74217 :                 hIvasEditableParameters->ism_metadata[obj].azimuth = st_ivas->hIsmMetaData[obj]->azimuth;
    1357       74217 :                 hIvasEditableParameters->ism_metadata[obj].elevation = st_ivas->hIsmMetaData[obj]->elevation;
    1358       74217 :                 hIvasEditableParameters->ism_metadata[obj].yaw = st_ivas->hIsmMetaData[obj]->yaw;
    1359       74217 :                 hIvasEditableParameters->ism_metadata[obj].pitch = st_ivas->hIsmMetaData[obj]->pitch;
    1360       74217 :                 hIvasEditableParameters->ism_metadata[obj].radius = st_ivas->hIsmMetaData[obj]->radius;
    1361       74217 :                 hIvasEditableParameters->ism_metadata[obj].gain = st_ivas->hIsmMetaData[obj]->edited_gain;
    1362       74217 :                 hIvasEditableParameters->ism_metadata[obj].non_diegetic_flag = st_ivas->hIsmMetaData[obj]->non_diegetic_flag;
    1363             :             }
    1364             : 
    1365       20736 :             if ( ism_mode == ISM_SBA_MODE_DISC )
    1366             :             {
    1367        7212 :                 hIvasEditableParameters->gain_bed = 1.0f;
    1368             :             }
    1369             :         }
    1370       15318 :         else if ( ism_mode == ISM_MODE_PARAM )
    1371             :         {
    1372       63075 :             for ( obj = 0; obj < hIvasEditableParameters->num_obj; obj++ )
    1373             :             {
    1374       49557 :                 hIvasEditableParameters->ism_metadata[obj].azimuth = st_ivas->hParamIsmDec->azimuth_values[obj];
    1375       49557 :                 hIvasEditableParameters->ism_metadata[obj].elevation = st_ivas->hParamIsmDec->elevation_values[obj];
    1376       49557 :                 hIvasEditableParameters->ism_metadata[obj].yaw = 0.0f;
    1377       49557 :                 hIvasEditableParameters->ism_metadata[obj].pitch = 0.0f;
    1378       49557 :                 hIvasEditableParameters->ism_metadata[obj].radius = 0.0f;
    1379       49557 :                 hIvasEditableParameters->ism_metadata[obj].gain = 1.0f;
    1380       49557 :                 hIvasEditableParameters->ism_metadata[obj].non_diegetic_flag = 0;
    1381             :             }
    1382             :         }
    1383        1800 :         else if ( ism_mode == ISM_MODE_NONE )
    1384             :         {
    1385        1800 :             hIvasEditableParameters->num_obj = 0;
    1386             :         }
    1387             : #ifdef DEBUGGING
    1388             :         else
    1389             :         {
    1390             :             assert( 0 && "This should never happen!" );
    1391             :         }
    1392             : #endif
    1393             :     }
    1394       12024 :     else if ( st_ivas->ivas_format == MASA_ISM_FORMAT || ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->nchan_ism > 0 ) )
    1395             :     {
    1396             :         /* object editing possible only in two highest OMASA modes */
    1397       12024 :         if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC )
    1398             :         {
    1399       27102 :             for ( obj = 0; obj < hIvasEditableParameters->num_obj; obj++ )
    1400             :             {
    1401       20076 :                 hIvasEditableParameters->ism_metadata[obj].azimuth = st_ivas->hIsmMetaData[obj]->azimuth;
    1402       20076 :                 hIvasEditableParameters->ism_metadata[obj].elevation = st_ivas->hIsmMetaData[obj]->elevation;
    1403       20076 :                 hIvasEditableParameters->ism_metadata[obj].yaw = st_ivas->hIsmMetaData[obj]->yaw;
    1404       20076 :                 hIvasEditableParameters->ism_metadata[obj].pitch = st_ivas->hIsmMetaData[obj]->pitch;
    1405       20076 :                 hIvasEditableParameters->ism_metadata[obj].radius = st_ivas->hIsmMetaData[obj]->radius;
    1406             : 
    1407             :                 /* reset the otherwise unused "gain" field for the object */
    1408       20076 :                 st_ivas->hIsmMetaData[obj]->edited_gain = 1.0f;
    1409       20076 :                 hIvasEditableParameters->ism_metadata[obj].gain = st_ivas->hIsmMetaData[obj]->edited_gain;
    1410       20076 :                 hIvasEditableParameters->ism_metadata[obj].non_diegetic_flag = st_ivas->hIsmMetaData[obj]->non_diegetic_flag;
    1411             :             }
    1412             :         }
    1413        4998 :         else if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
    1414             :         {
    1415             :             /* Handle MONO output */
    1416        3996 :             if ( st_ivas->renderer_type != RENDERER_MONO_DOWNMIX )
    1417             :             {
    1418        3996 :                 dirac_read_idx = st_ivas->hSpatParamRendCom->render_to_md_map[st_ivas->hSpatParamRendCom->subframes_rendered];
    1419             :             }
    1420             :             else
    1421             :             {
    1422           0 :                 dirac_read_idx = 0;
    1423             :             }
    1424             : 
    1425       17988 :             for ( obj = 0; obj < hIvasEditableParameters->num_obj; obj++ )
    1426             :             {
    1427       13992 :                 hIvasEditableParameters->ism_metadata[obj].azimuth = st_ivas->hMasaIsmData->azimuth_ism[obj][dirac_read_idx];
    1428       13992 :                 hIvasEditableParameters->ism_metadata[obj].elevation = st_ivas->hMasaIsmData->elevation_ism[obj][dirac_read_idx];
    1429             : 
    1430       13992 :                 hIvasEditableParameters->ism_metadata[obj].yaw = st_ivas->hIsmMetaData[obj]->yaw;
    1431       13992 :                 hIvasEditableParameters->ism_metadata[obj].pitch = st_ivas->hIsmMetaData[obj]->pitch;
    1432       13992 :                 hIvasEditableParameters->ism_metadata[obj].radius = st_ivas->hIsmMetaData[obj]->radius;
    1433             : 
    1434             :                 /* reset the otherwise unused "gain" field for the object */
    1435       13992 :                 st_ivas->hIsmMetaData[obj]->edited_gain = 1.0f;
    1436       13992 :                 hIvasEditableParameters->ism_metadata[obj].gain = st_ivas->hIsmMetaData[obj]->edited_gain;
    1437       13992 :                 hIvasEditableParameters->ism_metadata[obj].non_diegetic_flag = st_ivas->hIsmMetaData[obj]->non_diegetic_flag;
    1438             :             }
    1439             :         }
    1440        1002 :         else if ( ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || ism_mode == ISM_MODE_NONE )
    1441             :         {
    1442        1002 :             hIvasEditableParameters->num_obj = 0;
    1443             :         }
    1444             : #ifdef DEBUGGING
    1445             :         else
    1446             :         {
    1447             :             assert( 0 && "This should never happen!" );
    1448             :         }
    1449             : #endif
    1450             :     }
    1451             : 
    1452       48078 :     return IVAS_ERR_OK;
    1453             : }
    1454             : 
    1455             : 
    1456             : /*---------------------------------------------------------------------*
    1457             :  * IVAS_DEC_SetEditableParameters( )
    1458             :  *
    1459             :  * Set editable metadata parameters
    1460             :  *---------------------------------------------------------------------*/
    1461             : 
    1462       48081 : ivas_error IVAS_DEC_SetEditableParameters(
    1463             :     IVAS_DEC_HANDLE hIvasDec,                        /* i/o: IVAS decoder handle              */
    1464             :     IVAS_EDITABLE_PARAMETERS hIvasEditableParameters /* i  : object editing parameters handle */
    1465             : )
    1466             : {
    1467             :     int16_t dirac_read_idx, obj;
    1468             :     Decoder_Struct *st_ivas;
    1469             :     ISM_MODE ism_mode;
    1470             : 
    1471       48081 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    1472             :     {
    1473           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1474             :     }
    1475             : 
    1476       48081 :     if ( !hIvasDec->hasBeenFedFirstGoodFrame )
    1477             :     {
    1478           3 :         return IVAS_ERR_OK;
    1479             :     }
    1480             : 
    1481       48078 :     st_ivas = hIvasDec->st_ivas;
    1482       48078 :     ism_mode = st_ivas->ism_mode;
    1483             : 
    1484       48078 :     if ( !( st_ivas->ivas_format == ISM_FORMAT ||
    1485       21036 :             st_ivas->ivas_format == SBA_ISM_FORMAT ||
    1486       12024 :             st_ivas->ivas_format == MASA_ISM_FORMAT ||
    1487        1002 :             ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->nchan_ism > 0 ) ) )
    1488             :     {
    1489           0 :         return IVAS_ERROR( IVAS_ERR_OBJECTS_EDITING_NOT_SUPPORTED, "Object editing no supported in this operation mode." );
    1490             :     }
    1491             : 
    1492       48078 :     if ( st_ivas->ivas_format == ISM_FORMAT || st_ivas->ivas_format == SBA_ISM_FORMAT )
    1493             :     {
    1494       36054 :         if ( ism_mode == ISM_MODE_DISC || ism_mode == ISM_SBA_MODE_DISC )
    1495             :         {
    1496             : #ifdef DEBUGGING
    1497             :             assert( hIvasEditableParameters.num_obj == st_ivas->nchan_ism );
    1498             : #endif
    1499       94953 :             for ( obj = 0; obj < hIvasEditableParameters.num_obj; obj++ )
    1500             :             {
    1501       74217 :                 st_ivas->hIsmMetaData[obj]->edited_azimuth = hIvasEditableParameters.ism_metadata[obj].azimuth;
    1502       74217 :                 st_ivas->hIsmMetaData[obj]->edited_elevation = hIvasEditableParameters.ism_metadata[obj].elevation;
    1503       74217 :                 st_ivas->hIsmMetaData[obj]->edited_radius = hIvasEditableParameters.ism_metadata[obj].radius;
    1504       74217 :                 st_ivas->hIsmMetaData[obj]->edited_yaw = hIvasEditableParameters.ism_metadata[obj].yaw;
    1505       74217 :                 st_ivas->hIsmMetaData[obj]->edited_pitch = hIvasEditableParameters.ism_metadata[obj].pitch;
    1506       74217 :                 if ( hIvasEditableParameters.ism_metadata[obj].gain > EDIT_GAIN_MAX )
    1507             :                 {
    1508           0 :                     st_ivas->hIsmMetaData[obj]->edited_gain = EDIT_GAIN_MAX;
    1509             :                 }
    1510             :                 else
    1511             :                 {
    1512       74217 :                     st_ivas->hIsmMetaData[obj]->edited_gain = hIvasEditableParameters.ism_metadata[obj].gain;
    1513             :                 }
    1514       74217 :                 st_ivas->hIsmMetaData[obj]->non_diegetic_flag = hIvasEditableParameters.ism_metadata[obj].non_diegetic_flag;
    1515             :             }
    1516             : 
    1517       20736 :             if ( ism_mode == ISM_SBA_MODE_DISC )
    1518             :             {
    1519        7212 :                 if ( hIvasEditableParameters.gain_bed > EDIT_GAIN_MAX )
    1520             :                 {
    1521           0 :                     st_ivas->hSbaIsmData->gain_bed = EDIT_GAIN_MAX;
    1522             :                 }
    1523             :                 else
    1524             :                 {
    1525        7212 :                     st_ivas->hSbaIsmData->gain_bed = hIvasEditableParameters.gain_bed;
    1526             :                 }
    1527             :             }
    1528             :         }
    1529       15318 :         else if ( ism_mode == ISM_MODE_PARAM )
    1530             :         {
    1531             : #ifdef DEBUGGING
    1532             :             assert( hIvasEditableParameters.num_obj == st_ivas->nchan_ism );
    1533             : #endif
    1534       63075 :             for ( obj = 0; obj < hIvasEditableParameters.num_obj; obj++ )
    1535             :             {
    1536       49557 :                 st_ivas->hParamIsmDec->edited_azimuth_values[obj] = hIvasEditableParameters.ism_metadata[obj].azimuth;
    1537       49557 :                 st_ivas->hParamIsmDec->edited_elevation_values[obj] = hIvasEditableParameters.ism_metadata[obj].elevation;
    1538             : 
    1539       49557 :                 if ( st_ivas->hMasaIsmData != NULL )
    1540             :                 {
    1541             :                     /* Limit gain edit to a range of +12dB to -24dB with parametric ISM mode */
    1542       36012 :                     if ( hIvasEditableParameters.ism_metadata[obj].gain > EDIT_GAIN_MAX )
    1543             :                     {
    1544           0 :                         st_ivas->hMasaIsmData->gain_ism_edited[obj] = EDIT_GAIN_MAX;
    1545             :                     }
    1546       36012 :                     else if ( hIvasEditableParameters.ism_metadata[obj].gain < EDIT_GAIN_MIN )
    1547             :                     {
    1548           0 :                         st_ivas->hMasaIsmData->gain_ism_edited[obj] = EDIT_GAIN_MIN;
    1549             :                     }
    1550             :                     else
    1551             :                     {
    1552       36012 :                         st_ivas->hMasaIsmData->gain_ism_edited[obj] = hIvasEditableParameters.ism_metadata[obj].gain;
    1553             :                     }
    1554             : 
    1555             :                     /* Detect direction editing in Param-ISM mode */
    1556       36012 :                     if ( fabsf( st_ivas->hParamIsmDec->azimuth_values[obj] - hIvasEditableParameters.ism_metadata[obj].azimuth ) > OMASA_AZI_EDIT_THR ||
    1557         192 :                          fabsf( st_ivas->hParamIsmDec->elevation_values[obj] - hIvasEditableParameters.ism_metadata[obj].elevation ) > OMASA_ELE_EDIT_THR )
    1558             :                     {
    1559       35988 :                         st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 1u;
    1560             :                     }
    1561             :                     else
    1562             :                     {
    1563          24 :                         st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 0u;
    1564             :                     }
    1565             : 
    1566             :                     /* Detect gain editing in Param-ISM mode */
    1567       36012 :                     if ( fabsf( 1.0f - hIvasEditableParameters.ism_metadata[obj].gain ) > OMASA_GAIN_EDIT_THR )
    1568             :                     {
    1569       34158 :                         st_ivas->hMasaIsmData->ism_gain_is_edited[obj] = 1u;
    1570             :                     }
    1571             :                     else
    1572             :                     {
    1573        1854 :                         st_ivas->hMasaIsmData->ism_gain_is_edited[obj] = 0u;
    1574             :                     }
    1575             :                 }
    1576             :             }
    1577             : 
    1578       13518 :             if ( st_ivas->hMasaIsmData != NULL )
    1579             :             {
    1580             :                 /* MASA is not present with the ISM format */
    1581        9003 :                 st_ivas->hMasaIsmData->masa_gain_is_edited = 0u;
    1582             :             }
    1583             :         }
    1584        1800 :         else if ( ism_mode == ISM_MODE_NONE )
    1585             :         {
    1586        1800 :             if ( hIvasEditableParameters.num_obj != 0 )
    1587             :             {
    1588           0 :                 return IVAS_ERR_OBJECTS_EDITING_NOT_SUPPORTED;
    1589             :             }
    1590             :         }
    1591             : #ifdef DEBUGGING
    1592             :         else
    1593             :         {
    1594             :             assert( 0 && "This should never happen!" );
    1595             :         }
    1596             : #endif
    1597             :     }
    1598       12024 :     else if ( st_ivas->ivas_format == MASA_ISM_FORMAT && ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || ism_mode == ISM_MASA_MODE_DISC ) )
    1599             :     {
    1600             :         int16_t id_th;
    1601             :         float threshold_azi, threshold_ele;
    1602             : 
    1603             : #ifdef DEBUGGING
    1604             :         assert( hIvasEditableParameters.num_obj == st_ivas->nchan_ism );
    1605             : #endif
    1606       45090 :         for ( obj = 0; obj < hIvasEditableParameters.num_obj; obj++ )
    1607             :         {
    1608       34068 :             if ( st_ivas->hMasaIsmData != NULL )
    1609             :             {
    1610             :                 /* copy relevant fields also to OMASA structs, but only if the value has been changed. original values are in st_ivas->hIsmMetaData */
    1611             :                 /* first, need to convert float values to ints used internally */
    1612             :                 int16_t new_azi, new_ele;
    1613       34068 :                 if ( hIvasEditableParameters.ism_metadata[obj].azimuth > 0.0f )
    1614             :                 {
    1615       22839 :                     new_azi = (int16_t) ( hIvasEditableParameters.ism_metadata[obj].azimuth + 0.5f );
    1616             :                 }
    1617             :                 else
    1618             :                 {
    1619       11229 :                     new_azi = (int16_t) ( hIvasEditableParameters.ism_metadata[obj].azimuth - 0.5f );
    1620             :                 }
    1621             : 
    1622       34068 :                 if ( hIvasEditableParameters.ism_metadata[obj].elevation > 0.0f )
    1623             :                 {
    1624       11301 :                     new_ele = (int16_t) ( hIvasEditableParameters.ism_metadata[obj].elevation + 0.5f );
    1625             :                 }
    1626             :                 else
    1627             :                 {
    1628       22767 :                     new_ele = (int16_t) ( hIvasEditableParameters.ism_metadata[obj].elevation - 0.5f );
    1629             :                 }
    1630             : 
    1631       34068 :                 if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
    1632             :                 {
    1633             :                     /* Handle MONO output */
    1634       13992 :                     if ( st_ivas->renderer_type != RENDERER_MONO_DOWNMIX )
    1635             :                     {
    1636       13992 :                         dirac_read_idx = st_ivas->hSpatParamRendCom->render_to_md_map[st_ivas->hSpatParamRendCom->subframes_rendered];
    1637             :                     }
    1638             :                     else
    1639             :                     {
    1640           0 :                         dirac_read_idx = 0;
    1641             :                     }
    1642             : 
    1643             :                     /* determine thresholds for detecting object metadata edit for direction based on quantization resolution of the spatial direction parameters.
    1644             :                      * these depend from the number of bits used to transmit the directions,
    1645             :                      * which in turn depends from the object priority and importance:
    1646             :                      * importance -> priority -> number of bits -> elevation resolution -> elevation ring index -> azimuth resolution.
    1647             :                      * leading to elevation_resolution -> elevation threshold and azimuth resolution -> azimuth threshold */
    1648       13992 :                     id_th = (int16_t) ( fabsf( (float) st_ivas->hMasaIsmData->elevation_ism[obj][dirac_read_idx] ) / delta_theta_masa[st_ivas->hMasaIsmData->bits_ism[obj] - 3] + 0.5f );
    1649       13992 :                     threshold_azi = 360.0f / (float) no_phi_masa[st_ivas->hMasaIsmData->bits_ism[obj] - 1][id_th];
    1650       13992 :                     threshold_ele = delta_theta_masa[st_ivas->hMasaIsmData->bits_ism[obj] - 3];
    1651             : 
    1652       13992 :                     if ( ( (float) abs( new_azi - st_ivas->hMasaIsmData->azimuth_ism[obj][dirac_read_idx] ) > threshold_azi ) ||
    1653        1341 :                          ( (float) abs( new_ele - st_ivas->hMasaIsmData->elevation_ism[obj][dirac_read_idx] ) > threshold_ele ) )
    1654             :                     {
    1655             :                         /* at least one of the threshold is exceeded, so use new direction value and set editing detection flag */
    1656       13368 :                         st_ivas->hMasaIsmData->azimuth_ism_edited[obj] = new_azi;
    1657       13368 :                         st_ivas->hMasaIsmData->elevation_ism_edited[obj] = new_ele;
    1658             : 
    1659       13368 :                         st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 1u;
    1660             :                     }
    1661             :                     else
    1662             :                     {
    1663         624 :                         st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 0u;
    1664             :                     }
    1665             :                 }
    1666             :                 else
    1667             :                 {
    1668             :                     /* detect editing in ISM_MASA_MODE_DISC mode */
    1669       20076 :                     if ( fabsf( st_ivas->hIsmMetaData[obj]->azimuth - hIvasEditableParameters.ism_metadata[obj].azimuth ) > OMASA_AZI_EDIT_THR ||
    1670          78 :                          fabsf( st_ivas->hIsmMetaData[obj]->elevation - hIvasEditableParameters.ism_metadata[obj].elevation ) > OMASA_ELE_EDIT_THR )
    1671             :                     {
    1672       20070 :                         st_ivas->hMasaIsmData->azimuth_ism_edited[obj] = new_azi;
    1673       20070 :                         st_ivas->hMasaIsmData->elevation_ism_edited[obj] = new_ele;
    1674             : 
    1675       20070 :                         st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 1u;
    1676             :                     }
    1677             :                     else
    1678             :                     {
    1679           6 :                         st_ivas->hMasaIsmData->ism_dir_is_edited[obj] = 0u;
    1680             :                     }
    1681             :                 }
    1682             : 
    1683             :                 /* compare pre-edit gain and the edited one to detect editing */
    1684       34068 :                 if ( fabsf( st_ivas->hIsmMetaData[obj]->edited_gain - hIvasEditableParameters.ism_metadata[obj].gain ) > OMASA_GAIN_EDIT_THR )
    1685             :                 {
    1686       31503 :                     st_ivas->hMasaIsmData->ism_gain_is_edited[obj] = 1u;
    1687             : 
    1688             :                     /* Limit masa gain edit to a range of +12dB to -infdB with discrete OMASA mode and +12dB to -24dB with parametric OMASA mode  */
    1689       31503 :                     if ( hIvasEditableParameters.ism_metadata[obj].gain > EDIT_GAIN_MAX )
    1690             :                     {
    1691           0 :                         st_ivas->hMasaIsmData->gain_ism_edited[obj] = EDIT_GAIN_MAX;
    1692             :                     }
    1693       31503 :                     else if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && hIvasEditableParameters.ism_metadata[obj].gain < EDIT_GAIN_MIN )
    1694             :                     {
    1695           0 :                         st_ivas->hMasaIsmData->gain_ism_edited[obj] = EDIT_GAIN_MIN;
    1696             :                     }
    1697             :                     else
    1698             :                     {
    1699       31503 :                         st_ivas->hMasaIsmData->gain_ism_edited[obj] = hIvasEditableParameters.ism_metadata[obj].gain;
    1700             :                     }
    1701             :                 }
    1702             :                 else
    1703             :                 {
    1704        2565 :                     st_ivas->hMasaIsmData->ism_gain_is_edited[obj] = 0u;
    1705             :                 }
    1706             :             }
    1707             : 
    1708             :             /* Copy edited values to hIsmMetaData struct */
    1709       34068 :             if ( st_ivas->hIsmMetaData[obj] != NULL )
    1710             :             {
    1711       34068 :                 st_ivas->hIsmMetaData[obj]->edited_azimuth = hIvasEditableParameters.ism_metadata[obj].azimuth;
    1712       34068 :                 st_ivas->hIsmMetaData[obj]->edited_elevation = hIvasEditableParameters.ism_metadata[obj].elevation;
    1713             : 
    1714       34068 :                 if ( ism_mode == ISM_MASA_MODE_DISC )
    1715             :                 {
    1716       20076 :                     st_ivas->hIsmMetaData[obj]->edited_yaw = hIvasEditableParameters.ism_metadata[obj].yaw;
    1717       20076 :                     st_ivas->hIsmMetaData[obj]->edited_pitch = hIvasEditableParameters.ism_metadata[obj].pitch;
    1718       20076 :                     st_ivas->hIsmMetaData[obj]->edited_radius = hIvasEditableParameters.ism_metadata[obj].radius;
    1719             :                 }
    1720             : 
    1721       34068 :                 if ( hIvasEditableParameters.ism_metadata[obj].gain > EDIT_GAIN_MAX )
    1722             :                 {
    1723           0 :                     st_ivas->hIsmMetaData[obj]->edited_gain = EDIT_GAIN_MAX;
    1724             :                 }
    1725       34068 :                 else if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && hIvasEditableParameters.ism_metadata[obj].gain < EDIT_GAIN_MIN )
    1726             :                 {
    1727           0 :                     st_ivas->hIsmMetaData[obj]->edited_gain = EDIT_GAIN_MIN;
    1728             :                 }
    1729             :                 else
    1730             :                 {
    1731       34068 :                     st_ivas->hIsmMetaData[obj]->edited_gain = hIvasEditableParameters.ism_metadata[obj].gain;
    1732             :                 }
    1733             : 
    1734       34068 :                 st_ivas->hIsmMetaData[obj]->non_diegetic_flag = hIvasEditableParameters.ism_metadata[obj].non_diegetic_flag;
    1735             :             }
    1736             :         }
    1737             : 
    1738       11022 :         if ( fabsf( hIvasEditableParameters.gain_bed - 1.0f ) > OMASA_GAIN_EDIT_THR )
    1739             :         {
    1740             :             /* Limit masa gain edit to a range of +12dB to -infdB with discrete OMASA mode and +12dB to -24dB with parametric OMASA mode */
    1741       10599 :             if ( hIvasEditableParameters.gain_bed > EDIT_GAIN_MAX )
    1742             :             {
    1743           0 :                 st_ivas->hMasaIsmData->gain_masa_edited = EDIT_GAIN_MAX;
    1744             :             }
    1745       10599 :             else if ( ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ && hIvasEditableParameters.gain_bed < EDIT_GAIN_MIN )
    1746             :             {
    1747           0 :                 st_ivas->hMasaIsmData->gain_masa_edited = EDIT_GAIN_MIN;
    1748             :             }
    1749             :             else
    1750             :             {
    1751       10599 :                 st_ivas->hMasaIsmData->gain_masa_edited = hIvasEditableParameters.gain_bed;
    1752             :             }
    1753       10599 :             st_ivas->hMasaIsmData->masa_gain_is_edited = 1u;
    1754             :         }
    1755             :         else
    1756             :         {
    1757         423 :             st_ivas->hMasaIsmData->masa_gain_is_edited = 0u;
    1758             :         }
    1759             :     }
    1760             : 
    1761       48078 :     return IVAS_ERR_OK;
    1762             : }
    1763             : 
    1764             : 
    1765             : /*---------------------------------------------------------------------*
    1766             :  * IVAS_DEC_PrepareRenderer( )
    1767             :  *
    1768             :  * prepare IVAS renderer
    1769             :  *---------------------------------------------------------------------*/
    1770             : 
    1771     5976243 : ivas_error IVAS_DEC_PrepareRenderer(
    1772             :     IVAS_DEC_HANDLE hIvasDec /* i/o: IVAS decoder handle    */
    1773             : )
    1774             : {
    1775     5976243 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    1776             :     {
    1777           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1778             :     }
    1779             : 
    1780     5976243 :     if ( hIvasDec->hasBeenFedFirstGoodFrame || hIvasDec->isInitialized ) /* note: 'isInitialized' is related to EVS decoder */
    1781             :     {
    1782     5976003 :         ivas_dec_prepare_renderer( hIvasDec->st_ivas );
    1783             :     }
    1784             : 
    1785     5976243 :     hIvasDec->hasBeenPreparedRendering = true;
    1786             : 
    1787     5976243 :     return IVAS_ERR_OK;
    1788             : }
    1789             : 
    1790             : 
    1791             : /*---------------------------------------------------------------------*
    1792             :  * IVAS_DEC_GetSamplesRenderer( )
    1793             :  *
    1794             :  * Main function to render the decoded data to output data
    1795             :  *---------------------------------------------------------------------*/
    1796             : 
    1797     7939113 : ivas_error IVAS_DEC_GetSamplesRenderer(
    1798             :     IVAS_DEC_HANDLE hIvasDec,        /* i/o: IVAS decoder handle                                                     */
    1799             :     const int16_t nSamplesAsked,     /* i  : number of samples wanted by the caller                                  */
    1800             :     const IVAS_DEC_PCM_TYPE pcmType, /* i  : type for the decoded PCM resolution                                     */
    1801             :     void *pcmBuf,                    /* o  : output synthesis signal                                                 */
    1802             :     int16_t *nOutSamples,            /* o  : number of samples per channel written to output buffer                  */
    1803             :     bool *needNewFrame               /* o  : indication that the decoder needs a new frame                           */
    1804             : )
    1805             : {
    1806             :     ivas_error error;
    1807             :     uint16_t nSamplesRendered, nSamplesRendered_loop;
    1808             :     uint8_t nOutChannels;
    1809             :     Decoder_Struct *st_ivas;
    1810             : 
    1811     7939113 :     nSamplesRendered = 0;
    1812     7939113 :     nOutChannels = 0;
    1813     7939113 :     nSamplesRendered_loop = 0;
    1814             : 
    1815     7939113 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    1816             :     {
    1817           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1818             :     }
    1819             : 
    1820             :     /* the rendering needs to be prepared at this point */
    1821     7939113 :     if ( hIvasDec->hasBeenPreparedRendering == false )
    1822             :     {
    1823           0 :         return IVAS_ERR_UNKNOWN;
    1824             :     }
    1825             : 
    1826     7939113 :     st_ivas = hIvasDec->st_ivas;
    1827             : 
    1828     7939113 :     if ( hIvasDec->updateOrientation )
    1829             :     {
    1830             :         /*----------------------------------------------------------------*
    1831             :          * Combine orientations
    1832             :          *----------------------------------------------------------------*/
    1833             : 
    1834      861781 :         if ( ( error = combine_external_and_head_orientations_dec( st_ivas->hHeadTrackData, st_ivas->hExtOrientationData, st_ivas->hCombinedOrientationData ) ) != IVAS_ERR_OK )
    1835             :         {
    1836           0 :             return error;
    1837             :         }
    1838             : 
    1839             :         /*----------------------------------------------------------------*
    1840             :          * Binaural split rendering setup
    1841             :          *----------------------------------------------------------------*/
    1842             : 
    1843      861781 :         if ( st_ivas->hCombinedOrientationData != NULL && ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
    1844             :         {
    1845      301823 :             isar_set_split_rend_ht_setup( &st_ivas->hSplitBinRend->splitrend, st_ivas->hCombinedOrientationData->Quaternions, st_ivas->hCombinedOrientationData->Rmat );
    1846             :         }
    1847             : 
    1848      861781 :         hIvasDec->updateOrientation = false;
    1849             :     }
    1850             : 
    1851     7939113 :     if ( !hIvasDec->hasBeenFedFrame && hIvasDec->nSamplesAvailableNext == 0 )
    1852             :     {
    1853             :         /* no frame was fed, do nothing but ask for a frame */
    1854           0 :         *needNewFrame = true;
    1855           0 :         *nOutSamples = 0;
    1856           0 :         hIvasDec->needNewFrame = true;
    1857           0 :         return IVAS_ERR_OK;
    1858             :     }
    1859             : 
    1860             :     /* check if we are still at the beginning with bad frames, put out zeroes, keep track of subframes */
    1861     7939113 :     if ( !hIvasDec->isInitialized && st_ivas->bfi )
    1862             :     {
    1863         544 :         hIvasDec->hasBeenFedFrame = false;
    1864         544 :         set_s( pcmBuf, 0, st_ivas->hDecoderConfig->nchan_out * nSamplesAsked );
    1865         544 :         nSamplesRendered = nSamplesAsked;
    1866         544 :         hIvasDec->nSamplesAvailableNext -= nSamplesAsked;
    1867             :     }
    1868             :     else
    1869             :     {
    1870     7938569 :         nOutChannels = (uint8_t) st_ivas->hDecoderConfig->nchan_out;
    1871     7938569 :         hIvasDec->hasBeenFedFrame = false;
    1872             : 
    1873             :         /* check for possible flushed samples from a rate switch */
    1874     7938569 :         if ( hIvasDec->nSamplesFlushed > 0 )
    1875             :         {
    1876             : #ifdef DEBUGGING
    1877             :             assert( hIvasDec->pcmType == pcmType );
    1878             : #endif
    1879             :             /* note: offset (rendered samples) is always 0 */
    1880         615 :             if ( pcmType == IVAS_DEC_PCM_INT16 )
    1881             :             {
    1882         615 :                 mvs2s( (int16_t *) hIvasDec->flushbuffer, pcmBuf, hIvasDec->nSamplesFlushed * nOutChannels );
    1883             :             }
    1884           0 :             else if ( pcmType == IVAS_DEC_PCM_FLOAT )
    1885             :             {
    1886           0 :                 mvr2r( (float *) hIvasDec->flushbuffer, pcmBuf, hIvasDec->nSamplesFlushed * nOutChannels );
    1887             :             }
    1888             : #ifdef DEBUGGING
    1889             :             else
    1890             :             {
    1891             :                 assert( 0 && "wrong PCM type for the flush buffer!" );
    1892             :             }
    1893             : #endif
    1894         615 :             nSamplesRendered = hIvasDec->nSamplesFlushed;
    1895         615 :             hIvasDec->nSamplesFlushed = 0;
    1896             :         }
    1897             : 
    1898             :         /* render IVAS frames directly to the output buffer */
    1899     7938569 :         if ( ( error = ivas_jbm_dec_render( st_ivas, nSamplesAsked - nSamplesRendered, &nSamplesRendered_loop, &hIvasDec->nSamplesAvailableNext, pcm_type_API_to_internal( pcmType ), pcm_buffer_offset( pcmBuf, pcmType, nSamplesRendered * nOutChannels ) ) ) != IVAS_ERR_OK )
    1900             :         {
    1901           0 :             return error;
    1902             :         }
    1903             : 
    1904     7938569 :         nSamplesRendered += nSamplesRendered_loop;
    1905             :     }
    1906             : 
    1907     7939113 :     if ( hIvasDec->nSamplesAvailableNext == 0 )
    1908             :     {
    1909     5975670 :         *needNewFrame = true;
    1910     5975670 :         hIvasDec->needNewFrame = true;
    1911             :     }
    1912             :     else
    1913             :     {
    1914     1963443 :         *needNewFrame = false;
    1915             :     }
    1916             : 
    1917     7939113 :     *nOutSamples = nSamplesRendered;
    1918             : 
    1919     7939113 :     return IVAS_ERR_OK;
    1920             : }
    1921             : 
    1922             : 
    1923             : /*---------------------------------------------------------------------*
    1924             :  * isar_get_frame_size( )
    1925             :  *
    1926             :  *
    1927             :  *---------------------------------------------------------------------*/
    1928             : 
    1929      147250 : static int16_t isar_get_frame_size(
    1930             :     Decoder_Struct *st_ivas /* i  : IVAS decoder handle */
    1931             : )
    1932             : {
    1933             :     int32_t output_Fs;
    1934             :     int16_t nSamplesPerChannel;
    1935             : 
    1936      147250 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
    1937             : 
    1938      147250 :     if ( st_ivas->hDecoderConfig->render_framesize != IVAS_RENDER_FRAMESIZE_20MS &&
    1939       12400 :          ( st_ivas->hRenderConfig->split_rend_config.poseCorrectionMode == ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE ||
    1940           0 :            st_ivas->hRenderConfig->split_rend_config.dof == 0 ) )
    1941             :     {
    1942       12400 :         nSamplesPerChannel = (int16_t) ( output_Fs / FRAMES_PER_SEC / MAX_PARAM_SPATIAL_SUBFRAMES );
    1943       12400 :         nSamplesPerChannel *= (int16_t) st_ivas->hDecoderConfig->render_framesize;
    1944             :     }
    1945             :     else
    1946             :     {
    1947      134850 :         nSamplesPerChannel = (int16_t) ( output_Fs / FRAMES_PER_SEC );
    1948             :     }
    1949             : 
    1950      147250 :     return nSamplesPerChannel;
    1951             : }
    1952             : 
    1953             : 
    1954             : /*---------------------------------------------------------------------*
    1955             :  * isar_render_poses( )
    1956             :  *
    1957             :  *
    1958             :  *---------------------------------------------------------------------*/
    1959             : 
    1960      347454 : static ivas_error isar_render_poses(
    1961             :     IVAS_DEC_HANDLE hIvasDec,    /* i/o: IVAS decoder handle                                        */
    1962             :     const int16_t nSamplesAsked, /* i  : number of samples wanted by the caller                     */
    1963             :     int16_t *nOutSamples,        /* o  : number of samples per channel written to output buffer     */
    1964             :     bool *needNewFrame           /* o  : indication that the decoder needs a new frame              */
    1965             : )
    1966             : {
    1967             :     float pcmBuf[BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES * L_FRAME48k];
    1968             :     Decoder_Struct *st_ivas;
    1969             :     ivas_error error;
    1970             :     int16_t numPoses;
    1971             : 
    1972      347454 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    1973             :     {
    1974           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    1975             :     }
    1976             : 
    1977      347454 :     *needNewFrame = false;
    1978             : 
    1979      347454 :     st_ivas = hIvasDec->st_ivas;
    1980             : 
    1981      347454 :     numPoses = st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses;
    1982             : 
    1983             :     /* init flush buffer for rate switch if not already initizalized */
    1984      347454 :     if ( hIvasDec->flushbuffer == NULL )
    1985             :     {
    1986        1040 :         hIvasDec->flushbuffer = (void *) malloc( numPoses * BINAURAL_CHANNELS * hIvasDec->nSamplesFrame / IVAS_MAX_PARAM_SPATIAL_SUBFRAMES * sizeof( float ) );
    1987        1040 :         if ( hIvasDec->flushbuffer == NULL )
    1988             :         {
    1989           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Could not allocate flush buffer" );
    1990             :         }
    1991        1040 :         hIvasDec->pcmType = IVAS_DEC_PCM_FLOAT;
    1992        1040 :         set_zero( (float *) hIvasDec->flushbuffer, numPoses * BINAURAL_CHANNELS * hIvasDec->nSamplesFrame / IVAS_MAX_PARAM_SPATIAL_SUBFRAMES );
    1993             :     }
    1994             : 
    1995             :     /* render */
    1996      347454 :     if ( ( error = IVAS_DEC_GetSamplesRenderer( hIvasDec, nSamplesAsked, IVAS_DEC_PCM_FLOAT, pcmBuf, nOutSamples, needNewFrame ) ) != IVAS_ERR_OK )
    1997             :     {
    1998           0 :         return error;
    1999             :     }
    2000             : 
    2001      347454 :     if ( !hIvasDec->hasBeenFedFirstGoodFrame )
    2002             :     {
    2003           0 :         return IVAS_ERR_OK;
    2004             :     }
    2005             : 
    2006      347454 :     if ( !ivas_dec_split_rend_cldfb_in( st_ivas->renderer_type ) )
    2007             :     {
    2008       92606 :         ivas_TD_RINGBUF_PushInterleaved( st_ivas->hSplitBinRend->hMultiBinTdData, pcmBuf, *nOutSamples );
    2009             :     }
    2010             : 
    2011      347454 :     return error;
    2012             : }
    2013             : 
    2014             : 
    2015             : /*---------------------------------------------------------------------*
    2016             :  * isar_generate_metadata_and_bitstream( )
    2017             :  *
    2018             :  *
    2019             :  *---------------------------------------------------------------------*/
    2020             : 
    2021      301823 : static ivas_error isar_generate_metadata_and_bitstream(
    2022             :     Decoder_Struct *st_ivas,                 /* i/o: IVAS decoder handle                */
    2023             :     float **p_head_pose_buf,                 /* i/o: PCM buffer with head-pose data     */
    2024             :     int16_t nSamples,                        /* i  : duration of audio (in samples per channel) for which metadata should be generated */
    2025             :     ISAR_SPLIT_REND_BITS_DATA *splitRendBits /* o  : output split rendering bits        */
    2026             : )
    2027             : {
    2028             :     ivas_error error;
    2029             :     ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE hSplitBinRend;
    2030             :     int16_t max_band;
    2031             :     int16_t pcm_out_flag;
    2032             :     int16_t cldfb_in_flag;
    2033             :     int16_t ro_md_flag;
    2034             :     IVAS_QUATERNION Quaternion;
    2035             :     int16_t i, j, num_poses, num_cldfb_slots, n_samples_in_cldfb_slot;
    2036             :     float *p_Cldfb_RealBuffer_Binaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX];
    2037             :     float *p_Cldfb_ImagBuffer_Binaural[MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS][CLDFB_NO_COL_MAX];
    2038             : 
    2039      301823 :     hSplitBinRend = st_ivas->hSplitBinRend;
    2040             : 
    2041      301823 :     max_band = (int16_t) ( ( BINAURAL_MAXBANDS * st_ivas->hDecoderConfig->output_Fs ) / 48000 );
    2042      301823 :     pcm_out_flag = ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0;
    2043      301823 :     cldfb_in_flag = ivas_dec_split_rend_cldfb_in( st_ivas->renderer_type );
    2044             : 
    2045      301823 :     if ( cldfb_in_flag )
    2046             :     {
    2047      215292 :         n_samples_in_cldfb_slot = NS2SA( st_ivas->hDecoderConfig->output_Fs, CLDFB_SLOT_NS );
    2048      215292 :         assert( nSamples % n_samples_in_cldfb_slot == 0 );
    2049      215292 :         num_cldfb_slots = nSamples / n_samples_in_cldfb_slot;
    2050             : 
    2051      215292 :         num_poses = hSplitBinRend->splitrend.multiBinPoseData.num_poses;
    2052             : 
    2053     1848352 :         for ( i = 0; i < BINAURAL_CHANNELS * num_poses; ++i )
    2054             :         {
    2055    27522356 :             for ( j = 0; j < num_cldfb_slots; ++j )
    2056             :             {
    2057             :                 /* Save pointers to first CLDFB column in the ring buffer. Allows us to save
    2058             :                  * significant amounts of memory by not copying CLDFB values into a separate buffer. */
    2059    25889296 :                 ivas_CLDFB_RINGBUF_GetByIdx( hSplitBinRend->hMultiBinCldfbData[i], &p_Cldfb_RealBuffer_Binaural[i][j], &p_Cldfb_ImagBuffer_Binaural[i][j], 0 );
    2060             : 
    2061             :                 /* Pop the CLDFB column we just saved pointers to. This is fine as long as we use
    2062             :                  * the saved columns only before any new columns are pushed to the buffer - the new
    2063             :                  * columns could potentially overwrite the old columns we wanted to use.
    2064             :                  * This requirement is fulfilled in this case. */
    2065    25889296 :                 ivas_CLDFB_RINGBUF_Pop( hSplitBinRend->hMultiBinCldfbData[i], NULL, NULL, CLDFB_NO_CHANNELS_MAX );
    2066             :             }
    2067             :         }
    2068             :     }
    2069             :     else
    2070             :     {
    2071       86531 :         ivas_TD_RINGBUF_PopChannels( st_ivas->hSplitBinRend->hMultiBinTdData, p_head_pose_buf, nSamples );
    2072             :     }
    2073             : 
    2074             : 
    2075      301823 :     if ( st_ivas->hBinRendererTd != NULL )
    2076             :     {
    2077      152972 :         ro_md_flag = 1;
    2078             :     }
    2079             :     else
    2080             :     {
    2081      148851 :         ro_md_flag = 0;
    2082             :     }
    2083             : 
    2084      301823 :     if ( st_ivas->hHeadTrackData != NULL )
    2085             :     {
    2086      301823 :         Quaternion = st_ivas->hHeadTrackData->Quaternions[0];
    2087             :     }
    2088             :     else
    2089             :     {
    2090           0 :         Quaternion.w = -3.0f;
    2091           0 :         Quaternion.x = 0.0f;
    2092           0 :         Quaternion.y = 0.0f;
    2093           0 :         Quaternion.z = 0.0f;
    2094             :     }
    2095             : 
    2096      301823 :     if ( ( error = ISAR_PRE_REND_MultiBinToSplitBinaural( &hSplitBinRend->splitrend,
    2097             :                                                           Quaternion,
    2098      301823 :                                                           st_ivas->hRenderConfig->split_rend_config.splitRendBitRate,
    2099      301823 :                                                           st_ivas->hRenderConfig->split_rend_config.codec,
    2100      301823 :                                                           st_ivas->hRenderConfig->split_rend_config.isar_frame_size_ms,
    2101      301823 :                                                           st_ivas->hRenderConfig->split_rend_config.codec_frame_size_ms,
    2102             :                                                           splitRendBits,
    2103             :                                                           p_Cldfb_RealBuffer_Binaural,
    2104             :                                                           p_Cldfb_ImagBuffer_Binaural,
    2105             :                                                           max_band, p_head_pose_buf, 1, cldfb_in_flag, pcm_out_flag, ro_md_flag ) ) != IVAS_ERR_OK )
    2106             :     {
    2107           0 :         return error;
    2108             :     }
    2109             : 
    2110      301823 :     return IVAS_ERR_OK;
    2111             : }
    2112             : 
    2113             : 
    2114             : /*---------------------------------------------------------------------*
    2115             :  * IVAS_DEC_GetSplitBinauralBitstream( )
    2116             :  *
    2117             :  * Get split-rendering bitstream
    2118             :  *---------------------------------------------------------------------*/
    2119             : 
    2120      147250 : ivas_error IVAS_DEC_GetSplitBinauralBitstream(
    2121             :     IVAS_DEC_HANDLE hIvasDec,                 /* i/o: IVAS decoder handle                                       */
    2122             :     void *pcmBuf_out,                         /* o  : output synthesis signal for BINAURAL_SPLIT_PCM            */
    2123             :     ISAR_SPLIT_REND_BITS_DATA *splitRendBits, /* o  : output split rendering bits                               */
    2124             :     int16_t *nOutSamples,                     /* o  : number of samples per channel written to output buffer    */
    2125             :     bool *needNewFrame                        /* o  : indication that the decoder needs a new frame             */
    2126             : )
    2127             : {
    2128             :     Decoder_Struct *st_ivas;
    2129             :     ivas_error error;
    2130             :     float head_pose_buf[BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES][L_FRAME48k];
    2131             :     float *p_head_pose_buf[BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES];
    2132             :     int16_t i;
    2133             :     int16_t pcm_out_flag;
    2134             :     int16_t numSamplesPerChannelToOutput;
    2135             : 
    2136      147250 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2137             :     {
    2138           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2139             :     }
    2140             : 
    2141      147250 :     st_ivas = hIvasDec->st_ivas;
    2142             : 
    2143      147250 :     if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig ) == 0 )
    2144             :     {
    2145           0 :         return IVAS_ERR_WRONG_PARAMS;
    2146             :     }
    2147             : 
    2148      147250 :     pcm_out_flag = ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0;
    2149      147250 :     numSamplesPerChannelToOutput = isar_get_frame_size( st_ivas );
    2150             : 
    2151      147250 :     if ( ( error = isar_render_poses( hIvasDec, numSamplesPerChannelToOutput, nOutSamples, needNewFrame ) ) != IVAS_ERR_OK )
    2152             :     {
    2153           0 :         return error;
    2154             :     }
    2155             : 
    2156      147250 :     if ( !hIvasDec->hasBeenFedFirstGoodFrame )
    2157             :     {
    2158           0 :         return IVAS_ERR_OK;
    2159             :     }
    2160             : 
    2161     2503250 :     for ( i = 0; i < BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES; ++i )
    2162             :     {
    2163     2356000 :         p_head_pose_buf[i] = head_pose_buf[i];
    2164             :     }
    2165             : 
    2166      147250 :     if ( ( error = isar_generate_metadata_and_bitstream( st_ivas, p_head_pose_buf, *nOutSamples, splitRendBits ) ) != IVAS_ERR_OK )
    2167             :     {
    2168           0 :         return error;
    2169             :     }
    2170             : 
    2171             :     /* convert to int16 with limiting for BINAURAL_SPLIT_PCM */
    2172      147250 :     if ( pcm_out_flag )
    2173             :     {
    2174        3650 :         if ( st_ivas->hDecoderConfig->render_framesize == IVAS_RENDER_FRAMESIZE_5MS )
    2175             :         {
    2176             : #ifndef DISABLE_LIMITER
    2177           0 :             ivas_limiter_dec( st_ivas->hLimiter, p_head_pose_buf, st_ivas->hDecoderConfig->nchan_out, numSamplesPerChannelToOutput, st_ivas->BER_detect );
    2178             : #endif
    2179             :         }
    2180             :         else
    2181             :         {
    2182        3650 :             ivas_limiter_dec( st_ivas->hLimiter, p_head_pose_buf, st_ivas->hDecoderConfig->nchan_out, numSamplesPerChannelToOutput, st_ivas->BER_detect );
    2183             :         }
    2184             : 
    2185             : #ifdef DEBUGGING
    2186             :         st_ivas->noClipping +=
    2187             : #endif
    2188        3650 :             ivas_syn_output( p_head_pose_buf, numSamplesPerChannelToOutput, st_ivas->hDecoderConfig->nchan_out, (int16_t *) pcmBuf_out );
    2189             :     }
    2190             : 
    2191      147250 :     return IVAS_ERR_OK;
    2192             : }
    2193             : 
    2194             : 
    2195             : /*---------------------------------------------------------------------*
    2196             :  * ivas_dec_setup_all()
    2197             :  *
    2198             :  * Set-up all decoder parts: IVAS decoder, ISAR
    2199             :  *---------------------------------------------------------------------*/
    2200             : 
    2201     5976003 : static ivas_error ivas_dec_setup_all(
    2202             :     IVAS_DEC_HANDLE hIvasDec,                /* i/o: IVAS decoder handle                        */
    2203             :     uint8_t *nTransportChannels,             /* o  : number of decoded transport PCM channels   */
    2204             :     ISAR_SPLIT_REND_BITS_DATA *splitRendBits /* o  : output split rendering bits                */
    2205             : )
    2206             : {
    2207             :     ivas_error error;
    2208             : 
    2209     5976003 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
    2210             :     {
    2211             : #ifdef FIX_1435_MOVE_STEREO_PANNING
    2212       39210 :         *nTransportChannels = 1;
    2213             : #else
    2214             :         if ( hIvasDec->st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX )
    2215             :         {
    2216             :             *nTransportChannels = MAX_OUTPUT_CHANNELS_IN_DIEGETIC_PAN;
    2217             :         }
    2218             :         else
    2219             :         {
    2220             :             *nTransportChannels = 1;
    2221             :         }
    2222             : #endif
    2223             :     }
    2224             :     else
    2225             :     {
    2226             :         Decoder_Struct *st_ivas;
    2227             : 
    2228     5936793 :         st_ivas = hIvasDec->st_ivas;
    2229             : 
    2230             :         /* Setup IVAS split rendering */
    2231     5936793 :         if ( splitRendBits != NULL )
    2232             :         {
    2233      286028 :             if ( ( error = isar_set_split_rend_setup( st_ivas->hSplitBinRend, &st_ivas->hRenderConfig->split_rend_config, st_ivas->hCombinedOrientationData, splitRendBits ) ) != IVAS_ERR_OK )
    2234             :             {
    2235           0 :                 return error;
    2236             :             }
    2237             :         }
    2238             : 
    2239             :         /*----------------------------------------------------------------*
    2240             :          * IVAS decoder setup
    2241             :          * - read IVAS format signaling
    2242             :          * - read IVAS format specific signaling
    2243             :          * - initialize decoder in the first frame based on IVAS format and number of transport channels
    2244             :          * - reconfigure the decoder when the number of TC or IVAS total bitrate change
    2245             :          *----------------------------------------------------------------*/
    2246             : 
    2247     5936793 :         if ( st_ivas->bfi == 0 )
    2248             :         {
    2249     5487604 :             if ( ( error = ivas_dec_setup( st_ivas ) ) != IVAS_ERR_OK )
    2250             :             {
    2251           0 :                 return error;
    2252             :             }
    2253             :         }
    2254             : 
    2255     5936793 :         *nTransportChannels = (uint8_t) st_ivas->hTcBuffer->nchan_transport_jbm;
    2256             : 
    2257             :         /*-----------------------------------------------------------------*
    2258             :          * ISAR:
    2259             :          * - initialize ISAR handle at the first frame
    2260             :          * - reconfigure the ISAR handle in case of bitrate switching (renderer might change)
    2261             :          *-----------------------------------------------------------------*/
    2262             : 
    2263     5936793 :         if ( st_ivas->ini_frame == 0 && splitRendBits != NULL )
    2264             :         {
    2265        2088 :             if ( ( error = ivas_dec_init_split_rend( st_ivas ) ) != IVAS_ERR_OK )
    2266             :             {
    2267           0 :                 return error;
    2268             :             }
    2269             :         }
    2270             : 
    2271     5936793 :         if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    2272             :         {
    2273      286028 :             if ( ( error = ivas_dec_reconfig_split_rend( st_ivas ) ) != IVAS_ERR_OK )
    2274             :             {
    2275           0 :                 return error;
    2276             :             }
    2277             :         }
    2278             :     }
    2279             : 
    2280     5976003 :     return IVAS_ERR_OK;
    2281             : }
    2282             : 
    2283             : 
    2284             : /*---------------------------------------------------------------------*
    2285             :  * IVAS_DEC_GetNumObjects( )
    2286             :  *
    2287             :  * Returns the number of objects available in the decoded bitstream
    2288             :  *---------------------------------------------------------------------*/
    2289             : 
    2290      272896 : ivas_error IVAS_DEC_GetNumObjects(
    2291             :     IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle                                         */
    2292             :     uint16_t *numObjects      /* o  : number of objects for which the decoder has been configured */
    2293             : )
    2294             : {
    2295             :     int16_t is_masa_ism;
    2296      272896 :     is_masa_ism = 0;
    2297             : 
    2298      272896 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2299             :     {
    2300           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2301             :     }
    2302             : 
    2303      272896 :     if ( hIvasDec->st_ivas->hMasa != NULL )
    2304             :     {
    2305       46265 :         if ( hIvasDec->st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
    2306             :         {
    2307       46265 :             is_masa_ism = 1;
    2308             :         }
    2309             :     }
    2310             : 
    2311      272896 :     if ( hIvasDec->st_ivas->ivas_format == ISM_FORMAT || hIvasDec->st_ivas->ivas_format == SBA_ISM_FORMAT || hIvasDec->st_ivas->ivas_format == MASA_ISM_FORMAT || is_masa_ism )
    2312             :     {
    2313      272896 :         *numObjects = hIvasDec->st_ivas->nchan_ism;
    2314             :     }
    2315             :     else
    2316             :     {
    2317           0 :         *numObjects = 0;
    2318             :     }
    2319             : 
    2320      272896 :     return IVAS_ERR_OK;
    2321             : }
    2322             : 
    2323             : 
    2324             : /*---------------------------------------------------------------------*
    2325             :  * IVAS_DEC_GetFormat( )
    2326             :  *
    2327             :  * Returns the format of currently decoded bitstream.
    2328             :  * Note: bitstream format is only known after the first (good) frame has been decoded.
    2329             :  *---------------------------------------------------------------------*/
    2330             : 
    2331        5618 : ivas_error IVAS_DEC_GetFormat(
    2332             :     IVAS_DEC_HANDLE hIvasDec,  /* i/o: IVAS decoder handle                               */
    2333             :     IVAS_DEC_BS_FORMAT *format /* o  : format detected from bitstream fed to the decoder */
    2334             : )
    2335             : {
    2336        5618 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2337             :     {
    2338           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2339             :     }
    2340             : 
    2341        5618 :     if ( hIvasDec->hasDecodedFirstGoodFrame )
    2342             :     {
    2343        5618 :         *format = mapIvasFormat( hIvasDec->st_ivas->ivas_format );
    2344             :     }
    2345             :     else
    2346             :     {
    2347           0 :         *format = IVAS_DEC_BS_UNKOWN;
    2348             :     }
    2349             : 
    2350        5618 :     if ( *format == IVAS_DEC_BS_MASA && hIvasDec->st_ivas->hMasa != NULL )
    2351             :     {
    2352         535 :         if ( hIvasDec->st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
    2353             :         {
    2354         180 :             *format = IVAS_DEC_BS_MASA_ISM;
    2355             :         }
    2356             :     }
    2357             : 
    2358        5618 :     return IVAS_ERR_OK;
    2359             : }
    2360             : 
    2361             : 
    2362             : /*---------------------------------------------------------------------*
    2363             :  * getOutputBufferSize()
    2364             :  *
    2365             :  * Get size of output buffer in samples
    2366             :  *---------------------------------------------------------------------*/
    2367             : 
    2368      172066 : static int16_t getOutputBufferSize(
    2369             :     const Decoder_Struct *st_ivas /* i  : IVAS decoder handle */
    2370             : )
    2371             : {
    2372      172066 :     if ( st_ivas->hDecoderConfig == NULL )
    2373             :     {
    2374           0 :         return -1;
    2375             :     }
    2376             : 
    2377      172066 :     if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_EXTERNAL )
    2378             :     {
    2379       11236 :         return (int16_t) ( st_ivas->hDecoderConfig->output_Fs * ( MAX_OUTPUT_CHANNELS + MAX_NUM_OBJECTS ) / FRAMES_PER_SEC );
    2380             :     }
    2381      160830 :     else if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_LS_CUSTOM )
    2382             :     {
    2383          18 :         if ( st_ivas->hLsSetupCustom == NULL )
    2384             :         {
    2385           0 :             return -1;
    2386             :         }
    2387             : 
    2388          18 :         return (int16_t) ( st_ivas->hDecoderConfig->output_Fs * ( st_ivas->hLsSetupCustom->num_spk + st_ivas->hLsSetupCustom->num_lfe ) / FRAMES_PER_SEC );
    2389             :     }
    2390             :     else
    2391             :     {
    2392      160812 :         return (int16_t) ( st_ivas->hDecoderConfig->output_Fs * st_ivas->hDecoderConfig->nchan_out / FRAMES_PER_SEC );
    2393             :     }
    2394             : }
    2395             : 
    2396             : 
    2397             : /*---------------------------------------------------------------------*
    2398             :  * IVAS_DEC_GetOutputBufferSize()
    2399             :  *
    2400             :  * Returns size of output buffer in samples
    2401             :  *---------------------------------------------------------------------*/
    2402             : 
    2403      172066 : ivas_error IVAS_DEC_GetOutputBufferSize(
    2404             :     const IVAS_DEC_HANDLE hIvasDec, /* i  : IVAS decoder handle                                                                     */
    2405             :     int16_t *outputBufferSize       /* o  : total number of samples expected in the output buffer for current decoder configuration */
    2406             : )
    2407             : {
    2408      172066 :     if ( outputBufferSize == NULL || hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2409             :     {
    2410           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2411             :     }
    2412             : 
    2413      172066 :     *outputBufferSize = getOutputBufferSize( hIvasDec->st_ivas );
    2414             : 
    2415      172066 :     if ( *outputBufferSize == -1 )
    2416             :     {
    2417           0 :         return IVAS_ERR_INVALID_OUTPUT_BUFFER_SIZE;
    2418             :     }
    2419             :     else
    2420             :     {
    2421      172066 :         return IVAS_ERR_OK;
    2422             :     }
    2423             : }
    2424             : 
    2425             : 
    2426             : /*---------------------------------------------------------------------*
    2427             :  * IVAS_DEC_GetNumOutputChannels( )
    2428             :  *
    2429             :  * Returns number of output channels
    2430             :  *---------------------------------------------------------------------*/
    2431             : 
    2432       86033 : ivas_error IVAS_DEC_GetNumOutputChannels(
    2433             :     IVAS_DEC_HANDLE hIvasDec,  /* i/o: IVAS decoder handle             */
    2434             :     int16_t *numOutputChannels /* o  : number of PCM output channels   */
    2435             : )
    2436             : {
    2437       86033 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2438             :     {
    2439           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2440             :     }
    2441             : 
    2442       86033 :     if ( hIvasDec->hasDecodedFirstGoodFrame )
    2443             :     {
    2444       86033 :         *numOutputChannels = hIvasDec->st_ivas->hDecoderConfig->nchan_out;
    2445             :     }
    2446             :     else
    2447             :     {
    2448           0 :         *numOutputChannels = 0;
    2449             :     }
    2450             : 
    2451       86033 :     return IVAS_ERR_OK;
    2452             : }
    2453             : 
    2454             : 
    2455             : /*---------------------------------------------------------------------*
    2456             :  * IVAS_DEC_GetObjectMetadata( )
    2457             :  *
    2458             :  * Get metadata of one object decoded in the most recent frame
    2459             :  *---------------------------------------------------------------------*/
    2460             : 
    2461      764033 : ivas_error IVAS_DEC_GetObjectMetadata(
    2462             :     IVAS_DEC_HANDLE hIvasDec,    /* i/o: IVAS decoder handle                                                                */
    2463             :     IVAS_ISM_METADATA *metadata, /* o  : struct where metadata decoded in most recently decoded frame will be written       */
    2464             :     const uint16_t zero_flag,    /* i  : if this flag is enabled, this function outputs a zero-initialized metadata struct  */
    2465             :     const uint16_t objectIdx     /* i  : index of the queried object                                                        */
    2466             : )
    2467             : {
    2468             :     Decoder_Struct *st_ivas;
    2469             :     ISM_METADATA_HANDLE hIsmMeta;
    2470             :     int16_t is_masa_ism;
    2471      764033 :     is_masa_ism = 0;
    2472             : 
    2473      764033 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2474             :     {
    2475           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2476             :     }
    2477             : 
    2478      764033 :     st_ivas = hIvasDec->st_ivas;
    2479      764033 :     if ( hIvasDec->st_ivas->hMasa != NULL )
    2480             :     {
    2481      132162 :         if ( hIvasDec->st_ivas->hMasa->config.input_ivas_format == MASA_ISM_FORMAT )
    2482             :         {
    2483      132162 :             is_masa_ism = 1;
    2484             :         }
    2485             :     }
    2486      764033 :     if ( st_ivas->ivas_format != ISM_FORMAT && st_ivas->ivas_format != MASA_ISM_FORMAT && st_ivas->ivas_format != SBA_ISM_FORMAT && is_masa_ism == 0 )
    2487             :     {
    2488           0 :         return IVAS_ERR_WRONG_MODE;
    2489             :     }
    2490             : 
    2491      764033 :     if ( objectIdx >= st_ivas->nchan_ism )
    2492             :     {
    2493           0 :         return IVAS_ERR_INVALID_INDEX;
    2494             :     }
    2495             : 
    2496      764033 :     hIsmMeta = st_ivas->hIsmMetaData[objectIdx];
    2497             : 
    2498      764033 :     if ( hIsmMeta == NULL || zero_flag || ( st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ ) )
    2499             :     {
    2500      236489 :         metadata->azimuth = 0.f;
    2501      236489 :         metadata->elevation = 0.f;
    2502      236489 :         metadata->radius = 1.f;
    2503      236489 :         metadata->spread = 0.f;
    2504      236489 :         metadata->gainFactor = 1.f;
    2505      236489 :         metadata->yaw = 0.f;
    2506      236489 :         metadata->pitch = 0.f;
    2507      236489 :         metadata->non_diegetic_flag = 0;
    2508             :     }
    2509             :     else
    2510             :     {
    2511      527544 :         if ( st_ivas->ism_mode == ISM_MODE_DISC )
    2512             :         {
    2513      285112 :             metadata->azimuth = hIsmMeta->edited_azimuth;
    2514      285112 :             metadata->elevation = hIsmMeta->edited_elevation;
    2515      285112 :             metadata->radius = hIsmMeta->edited_radius;
    2516             : 
    2517      285112 :             metadata->yaw = hIsmMeta->edited_yaw;
    2518      285112 :             metadata->pitch = hIsmMeta->edited_pitch;
    2519      285112 :             metadata->spread = 0.f;
    2520             : 
    2521      285112 :             metadata->gainFactor = hIsmMeta->edited_gain;
    2522      285112 :             metadata->non_diegetic_flag = hIsmMeta->non_diegetic_flag;
    2523             :         }
    2524      242432 :         else if ( st_ivas->ism_mode == ISM_MODE_PARAM )
    2525             :         {
    2526       21692 :             metadata->azimuth = st_ivas->hParamIsmDec->edited_azimuth_values[objectIdx];
    2527       21692 :             metadata->elevation = st_ivas->hParamIsmDec->edited_elevation_values[objectIdx];
    2528       21692 :             metadata->radius = hIsmMeta->radius;
    2529       21692 :             metadata->yaw = hIsmMeta->yaw;
    2530       21692 :             metadata->pitch = hIsmMeta->pitch;
    2531       21692 :             metadata->spread = 0.f;
    2532       21692 :             metadata->gainFactor = 1.f;
    2533       21692 :             metadata->non_diegetic_flag = hIsmMeta->non_diegetic_flag;
    2534             :         }
    2535      220740 :         else if ( st_ivas->ism_mode == ISM_SBA_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_DISC || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ )
    2536             :         {
    2537      216660 :             metadata->azimuth = st_ivas->hIsmMetaData[objectIdx]->edited_azimuth;
    2538      216660 :             metadata->elevation = st_ivas->hIsmMetaData[objectIdx]->edited_elevation;
    2539      216660 :             metadata->radius = st_ivas->hIsmMetaData[objectIdx]->edited_radius;
    2540      216660 :             metadata->yaw = st_ivas->hIsmMetaData[objectIdx]->edited_yaw;
    2541      216660 :             metadata->pitch = st_ivas->hIsmMetaData[objectIdx]->edited_pitch;
    2542      216660 :             metadata->spread = 0.f;
    2543      216660 :             metadata->gainFactor = st_ivas->hIsmMetaData[objectIdx]->edited_gain;
    2544      216660 :             metadata->non_diegetic_flag = st_ivas->hIsmMetaData[objectIdx]->non_diegetic_flag;
    2545             :         }
    2546             :         else
    2547             :         {
    2548        4080 :             metadata->azimuth = hIsmMeta->azimuth;
    2549        4080 :             metadata->elevation = hIsmMeta->elevation;
    2550        4080 :             metadata->radius = hIsmMeta->radius;
    2551        4080 :             metadata->yaw = hIsmMeta->yaw;
    2552        4080 :             metadata->pitch = hIsmMeta->pitch;
    2553        4080 :             metadata->spread = 0.f;
    2554        4080 :             metadata->gainFactor = 1.f;
    2555        4080 :             metadata->non_diegetic_flag = hIsmMeta->non_diegetic_flag;
    2556             :         }
    2557             :     }
    2558             : 
    2559      764033 :     return IVAS_ERR_OK;
    2560             : }
    2561             : 
    2562             : 
    2563             : /*---------------------------------------------------------------------*
    2564             :  * IVAS_DEC_GetMasaMetadata( )
    2565             :  *
    2566             :  * Get metadata of the most recently decoded MASA frame
    2567             :  *---------------------------------------------------------------------*/
    2568             : 
    2569      108516 : ivas_error IVAS_DEC_GetMasaMetadata(
    2570             :     IVAS_DEC_HANDLE hIvasDec,                          /* i/o: IVAS decoder handle              */
    2571             :     MASA_DECODER_EXT_OUT_META_HANDLE *hMasaExtOutMeta, /* o  : pointer to handle, which will be set to point to metadata from the most recently decoded frame */
    2572             :     const uint8_t getFromJbmBuffer                     /* i  : get metadata from a JBM buffer   */
    2573             : )
    2574             : {
    2575      108516 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2576             :     {
    2577           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2578             :     }
    2579             : 
    2580      108516 :     if ( hIvasDec->st_ivas->ivas_format != MASA_FORMAT && hIvasDec->st_ivas->ivas_format != MASA_ISM_FORMAT )
    2581             :     {
    2582           0 :         return IVAS_ERR_WRONG_MODE;
    2583             :     }
    2584             : 
    2585      108516 :     if ( getFromJbmBuffer )
    2586             :     {
    2587       33600 :         ivas_jbm_masa_sf_to_sf_map( hIvasDec->st_ivas );
    2588             :     }
    2589             : 
    2590      108516 :     *hMasaExtOutMeta = hIvasDec->st_ivas->hMasa->data.extOutMeta;
    2591             : 
    2592      108516 :     return IVAS_ERR_OK;
    2593             : }
    2594             : 
    2595             : 
    2596             : /*---------------------------------------------------------------------*
    2597             :  * IVAS_DEC_FeedHeadTrackData( )
    2598             :  *
    2599             :  * Feed the decoder with the head tracking data
    2600             :  *---------------------------------------------------------------------*/
    2601             : 
    2602     2164907 : ivas_error IVAS_DEC_FeedHeadTrackData(
    2603             :     IVAS_DEC_HANDLE hIvasDec,               /* i/o: IVAS decoder handle                                     */
    2604             :     IVAS_QUATERNION orientation,            /* i  : head-tracking data, listener orientation                */
    2605             :     IVAS_VECTOR3 Pos,                       /* i  : listener position                                       */
    2606             :     const int16_t subframe_idx,             /* i  : subframe index                                          */
    2607             :     const ISAR_SPLIT_REND_ROT_AXIS rot_axis /* i  : external control for rotation axis for split rendering  */
    2608             : )
    2609             : {
    2610             :     HEAD_TRACK_DATA_HANDLE hHeadTrackData;
    2611             :     ivas_error error;
    2612             : 
    2613     2164907 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2614             :     {
    2615           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2616             :     }
    2617             : 
    2618     2164907 :     hHeadTrackData = hIvasDec->st_ivas->hHeadTrackData;
    2619             : 
    2620     2164907 :     if ( hHeadTrackData == NULL )
    2621             :     {
    2622           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2623             :     }
    2624             : 
    2625             :     /* Move head-tracking data to the decoder handle */
    2626             :     /* check for Euler angle signaling */
    2627     2164907 :     if ( orientation.w == -3.0f )
    2628             :     {
    2629      962066 :         Euler2Quat( deg2rad( orientation.x ), deg2rad( orientation.y ), deg2rad( orientation.z ), &orientation );
    2630             :     }
    2631             : 
    2632     2164907 :     if ( ( error = ivas_orient_trk_Process( hHeadTrackData->OrientationTracker, orientation, FRAMES_PER_SEC * MAX_PARAM_SPATIAL_SUBFRAMES, &hHeadTrackData->Quaternions[subframe_idx] ) ) != IVAS_ERR_OK )
    2633             :     {
    2634           0 :         return error;
    2635             :     }
    2636             : 
    2637     2164907 :     hHeadTrackData->Pos[subframe_idx].x = Pos.x;
    2638     2164907 :     hHeadTrackData->Pos[subframe_idx].y = Pos.y;
    2639     2164907 :     hHeadTrackData->Pos[subframe_idx].z = Pos.z;
    2640             : 
    2641     2164907 :     hHeadTrackData->sr_pose_pred_axis = rot_axis;
    2642     2164907 :     hIvasDec->updateOrientation = true;
    2643             : 
    2644     2164907 :     return IVAS_ERR_OK;
    2645             : }
    2646             : 
    2647             : 
    2648             : /*---------------------------------------------------------------------*
    2649             :  * IVAS_DEC_FeedRefRotData( )
    2650             :  *
    2651             :  * Feed the decoder with the reference rotation data
    2652             :  *---------------------------------------------------------------------*/
    2653             : 
    2654           0 : ivas_error IVAS_DEC_FeedRefRotData(
    2655             :     IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle     */
    2656             :     IVAS_QUATERNION rotation  /* i  : reference rotation data */
    2657             : )
    2658             : {
    2659             :     ivas_orient_trk_state_t *pOtr;
    2660             : 
    2661           0 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHeadTrackData == NULL || hIvasDec->st_ivas->hHeadTrackData->OrientationTracker == NULL )
    2662             :     {
    2663           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2664             :     }
    2665             : 
    2666           0 :     pOtr = hIvasDec->st_ivas->hHeadTrackData->OrientationTracker;
    2667             : 
    2668           0 :     pOtr->refRot.w = rotation.w;
    2669           0 :     pOtr->refRot.x = rotation.x;
    2670           0 :     pOtr->refRot.z = rotation.z;
    2671           0 :     pOtr->refRot.y = rotation.y;
    2672             : 
    2673           0 :     hIvasDec->updateOrientation = true;
    2674             : 
    2675           0 :     return IVAS_ERR_OK;
    2676             : }
    2677             : 
    2678             : 
    2679             : /*---------------------------------------------------------------------*
    2680             :  * IVAS_DEC_FeedRefVectorData( )
    2681             :  *
    2682             :  * Feed the decoder with a reference vector spanning from listenerPos
    2683             :  * to refPos. Only available in OTR_TRACKING_REF_POS and
    2684             :  * OTR_TRACKING_REF_POS_LEV modes.
    2685             :  *---------------------------------------------------------------------*/
    2686             : 
    2687       12012 : ivas_error IVAS_DEC_FeedRefVectorData(
    2688             :     IVAS_DEC_HANDLE hIvasDec,       /* i/o: IVAS decoder handle     */
    2689             :     const IVAS_VECTOR3 listenerPos, /* i  : Listener position       */
    2690             :     const IVAS_VECTOR3 refPos       /* i  : Reference position      */
    2691             : )
    2692             : {
    2693             :     ivas_orient_trk_state_t *pOtr;
    2694             : 
    2695       12012 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHeadTrackData == NULL || hIvasDec->st_ivas->hHeadTrackData->OrientationTracker == NULL )
    2696             :     {
    2697           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2698             :     }
    2699             : 
    2700       12012 :     pOtr = hIvasDec->st_ivas->hHeadTrackData->OrientationTracker;
    2701             : 
    2702       12012 :     hIvasDec->updateOrientation = true;
    2703             : 
    2704       12012 :     return ivas_orient_trk_SetReferenceVector( pOtr, listenerPos, refPos );
    2705             : }
    2706             : 
    2707             : 
    2708             : /*---------------------------------------------------------------------*
    2709             :  * IVAS_DEC_FeedExternalOrientationData( )
    2710             :  *
    2711             :  * Feed the decoder with the external orientation data
    2712             :  *---------------------------------------------------------------------*/
    2713             : 
    2714      397417 : ivas_error IVAS_DEC_FeedExternalOrientationData(
    2715             :     IVAS_DEC_HANDLE hIvasDec,             /* i/o: IVAS decoder handle                                                     */
    2716             :     IVAS_QUATERNION orientation,          /* i  : external orientation data                                               */
    2717             :     int8_t enableHeadRotation,            /* i  : flag to enable head rotation for this frame                             */
    2718             :     int8_t enableExternalOrientation,     /* i  : flag to enable external orientation for this frame                      */
    2719             :     int8_t enableRotationInterpolation,   /* i  : flag to interpolate rotations from current and previous frames          */
    2720             :     int16_t numFramesToTargetOrientation, /* i  : number of frames until target orientation is reached                    */
    2721             :     const int16_t subframe_idx            /* i  : subframe index                                                          */
    2722             : )
    2723             : {
    2724             :     EXTERNAL_ORIENTATION_HANDLE hExternalOrientationData;
    2725             : 
    2726      397417 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2727             :     {
    2728           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2729             :     }
    2730             : 
    2731      397417 :     hExternalOrientationData = hIvasDec->st_ivas->hExtOrientationData;
    2732             : 
    2733      397417 :     if ( hExternalOrientationData == NULL )
    2734             :     {
    2735           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2736             :     }
    2737             : 
    2738             :     /* Move external orientation data to the decoder handle (invert orientations) */
    2739      397417 :     QuaternionInverse( orientation, &hExternalOrientationData->Quaternions[subframe_idx] );
    2740             : 
    2741      397417 :     hExternalOrientationData->enableHeadRotation[subframe_idx] = enableHeadRotation;
    2742      397417 :     hExternalOrientationData->enableExternalOrientation[subframe_idx] = enableExternalOrientation;
    2743      397417 :     hExternalOrientationData->enableRotationInterpolation[subframe_idx] = enableRotationInterpolation;
    2744      397417 :     hExternalOrientationData->numFramesToTargetOrientation[subframe_idx] = numFramesToTargetOrientation;
    2745             : 
    2746      397417 :     hIvasDec->updateOrientation = true;
    2747             : 
    2748      397417 :     return IVAS_ERR_OK;
    2749             : }
    2750             : 
    2751             : 
    2752             : /*---------------------------------------------------------------------*
    2753             :  * IVAS_DEC_FeedCustomLsData( )
    2754             :  *
    2755             :  * Feed the decoder with the Custom loudspeaker data
    2756             :  *---------------------------------------------------------------------*/
    2757             : 
    2758             : /*! r: error code */
    2759           9 : ivas_error IVAS_DEC_FeedCustomLsData(
    2760             :     IVAS_DEC_HANDLE hIvasDec,               /* i/o: IVAS decoder handle             */
    2761             :     const IVAS_CUSTOM_LS_DATA hLsCustomData /* i  : Custom loudspeaker setup data   */
    2762             : )
    2763             : {
    2764             :     int16_t i, is_planar;
    2765             :     IVAS_LSSETUP_CUSTOM_HANDLE hLsSetupCustom;
    2766             : 
    2767           9 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2768             :     {
    2769           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2770             :     }
    2771             : 
    2772           9 :     hLsSetupCustom = hIvasDec->st_ivas->hLsSetupCustom;
    2773             : 
    2774           9 :     if ( hLsSetupCustom == NULL )
    2775             :     {
    2776           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2777             :     }
    2778             : 
    2779             :     /* Move Custom LS layout data to the decoder handle */
    2780             : 
    2781             :     /* Loudspeaker azimuths and elevations */
    2782           9 :     hLsSetupCustom->num_spk = hLsCustomData.num_spk;
    2783             : 
    2784           9 :     mvr2r( hLsCustomData.azimuth, hLsSetupCustom->ls_azimuth, hLsCustomData.num_spk );
    2785           9 :     mvr2r( hLsCustomData.elevation, hLsSetupCustom->ls_elevation, hLsCustomData.num_spk );
    2786             : 
    2787             :     /* Set planar flag */
    2788           9 :     is_planar = 1;
    2789         153 :     for ( i = 0; i < hLsCustomData.num_spk; i++ )
    2790             :     {
    2791         144 :         if ( is_planar && hLsSetupCustom->ls_elevation[i] != 0.0f )
    2792             :         {
    2793           9 :             is_planar = 0;
    2794             :         }
    2795             :     }
    2796           9 :     hLsSetupCustom->is_planar_setup = is_planar;
    2797             : 
    2798             :     /* Loudspeaker LFE */
    2799           9 :     hLsSetupCustom->num_lfe = hLsCustomData.num_lfe;
    2800           9 :     mvs2s( hLsCustomData.lfe_idx, hLsSetupCustom->lfe_idx, hLsCustomData.num_lfe );
    2801             : 
    2802           9 :     return IVAS_ERR_OK;
    2803             : }
    2804             : 
    2805             : 
    2806             : /*---------------------------------------------------------------------*
    2807             :  * IVAS_DEC_GetHrtfTDrendHandle( )
    2808             :  *
    2809             :  * Get TD binaural renderer handle
    2810             :  *---------------------------------------------------------------------*/
    2811             : 
    2812        2540 : ivas_error IVAS_DEC_GetHrtfTDrendHandle(
    2813             :     IVAS_DEC_HANDLE hIvasDec,         /* i/o: IVAS decoder handle      */
    2814             :     IVAS_DEC_HRTF_TD_HANDLE **hHrtfTD /* o  : HRTF handle              */
    2815             : )
    2816             : {
    2817        2540 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfTD == NULL )
    2818             :     {
    2819           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2820             :     }
    2821             : 
    2822        2540 :     *hHrtfTD = &hIvasDec->st_ivas->hHrtfTD;
    2823             : 
    2824        2540 :     return IVAS_ERR_OK;
    2825             : }
    2826             : 
    2827             : 
    2828             : /*---------------------------------------------------------------------*
    2829             :  * IVAS_DEC_GetHrtfCRendHandle( )
    2830             :  *
    2831             :  * Get Crend binaural renderer handle
    2832             :  *---------------------------------------------------------------------*/
    2833             : 
    2834         853 : ivas_error IVAS_DEC_GetHrtfCRendHandle(
    2835             :     IVAS_DEC_HANDLE hIvasDec,               /* i/o: IVAS decoder handle      */
    2836             :     IVAS_DEC_HRTF_CREND_HANDLE **hHrtfCrend /* o  : Crend HRTF handle        */
    2837             : )
    2838             : {
    2839         853 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfCrend == NULL )
    2840             :     {
    2841           0 :         return IVAS_ERR_WRONG_PARAMS;
    2842             :     }
    2843             : 
    2844         853 :     *hHrtfCrend = &hIvasDec->st_ivas->hHrtfCrend;
    2845             : 
    2846         853 :     return IVAS_ERR_OK;
    2847             : }
    2848             : 
    2849             : 
    2850             : /*---------------------------------------------------------------------*
    2851             :  * IVAS_DEC_GetHrtfFastConvHandle( )
    2852             :  *
    2853             :  * Get FastConv binaural renderer handle
    2854             :  *---------------------------------------------------------------------*/
    2855             : 
    2856        2298 : ivas_error IVAS_DEC_GetHrtfFastConvHandle(
    2857             :     IVAS_DEC_HANDLE hIvasDec,                     /* i/o: IVAS decoder handle    */
    2858             :     IVAS_DEC_HRTF_FASTCONV_HANDLE **hHrtfFastConv /* o  : FASTCONV HRTF handle   */
    2859             : )
    2860             : {
    2861        2298 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfFastConv == NULL )
    2862             :     {
    2863           0 :         return IVAS_ERR_WRONG_PARAMS;
    2864             :     }
    2865             : 
    2866        2298 :     *hHrtfFastConv = &hIvasDec->st_ivas->hHrtfFastConv;
    2867             : 
    2868        2298 :     return IVAS_ERR_OK;
    2869             : }
    2870             : 
    2871             : 
    2872             : /*---------------------------------------------------------------------*
    2873             :  * IVAS_DEC_GetHrtfParamBinHandle( )
    2874             :  *
    2875             :  * Get Parametric binaural renderer handle
    2876             :  *---------------------------------------------------------------------*/
    2877             : 
    2878        5691 : ivas_error IVAS_DEC_GetHrtfParamBinHandle(
    2879             :     IVAS_DEC_HANDLE hIvasDec,                     /* i/o: IVAS decoder handle                 */
    2880             :     IVAS_DEC_HRTF_PARAMBIN_HANDLE **hHrtfParambin /* o  : Parametric binauralizer HRTF handle */
    2881             : )
    2882             : {
    2883        5691 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfParambin == NULL )
    2884             :     {
    2885           0 :         return IVAS_ERR_WRONG_PARAMS;
    2886             :     }
    2887             : 
    2888        5691 :     *hHrtfParambin = &hIvasDec->st_ivas->hHrtfParambin;
    2889             : 
    2890        5691 :     return IVAS_ERR_OK;
    2891             : }
    2892             : 
    2893             : /*---------------------------------------------------------------------*
    2894             :  * IVAS_DEC_GetHrtfStatisticsHandle( )
    2895             :  *
    2896             :  * Get HRTF statistics (room effect) binaural renderer handle
    2897             :  *---------------------------------------------------------------------*/
    2898             : 
    2899         801 : ivas_error IVAS_DEC_GetHrtfStatisticsHandle(
    2900             :     IVAS_DEC_HANDLE hIvasDec,                         /* i/o: IVAS decoder handle       */
    2901             :     IVAS_DEC_HRTF_STATISTICS_HANDLE **hHrtfStatistics /* o  : HRTF statistics handle    */
    2902             : )
    2903             : {
    2904         801 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hHrtfStatistics == NULL )
    2905             :     {
    2906           0 :         return IVAS_ERR_WRONG_PARAMS;
    2907             :     }
    2908             : 
    2909         801 :     *hHrtfStatistics = &hIvasDec->st_ivas->hHrtfStatistics;
    2910             : 
    2911         801 :     return IVAS_ERR_OK;
    2912             : }
    2913             : 
    2914             : 
    2915             : /*---------------------------------------------------------------------*
    2916             :  * IVAS_DEC_HRTF_binary_open( )
    2917             :  *
    2918             :  * Allocate HRTF binary handles
    2919             :  *---------------------------------------------------------------------*/
    2920             : 
    2921       19626 : ivas_error IVAS_DEC_HRTF_binary_open(
    2922             :     IVAS_DEC_HANDLE hIvasDec,                      /* i/o: IVAS decoder handle      */
    2923             :     const IVAS_BIN_RENDERER_TYPE binaural_renderer /* i  : binaural renderer type   */
    2924             : )
    2925             : {
    2926             :     ivas_error error;
    2927             :     Decoder_Struct *st_ivas;
    2928             : 
    2929       19626 :     st_ivas = hIvasDec->st_ivas;
    2930             : 
    2931       19626 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    2932             :     {
    2933           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    2934             :     }
    2935             : 
    2936       19626 :     if ( st_ivas->hDecoderConfig->Opt_HRTF_binary )
    2937             :     {
    2938             :         /* TD binaural renderer */
    2939       19626 :         if ( ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_TDREND || binaural_renderer == IVAS_BIN_RENDERER_TYPE_DEFAULT ) && st_ivas->hHrtfTD == NULL )
    2940             :         {
    2941        2540 :             if ( ( error = ivas_HRTF_binary_open( &( st_ivas->hHrtfTD ) ) ) != IVAS_ERR_OK )
    2942             :             {
    2943           0 :                 return error;
    2944             :             }
    2945             :         }
    2946             : 
    2947             :         /* Crend binaural renderer */
    2948       19626 :         if ( ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_CREND || binaural_renderer == IVAS_BIN_RENDERER_TYPE_DEFAULT ) && st_ivas->hHrtfCrend == NULL )
    2949             :         {
    2950         853 :             if ( ( error = ivas_HRTF_CRend_binary_open( &( st_ivas->hHrtfCrend ) ) ) != IVAS_ERR_OK )
    2951             :             {
    2952           0 :                 return error;
    2953             :             }
    2954             :         }
    2955             : 
    2956             :         /* FastConv binaural renderer */
    2957       19626 :         if ( ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_FASTCONV || binaural_renderer == IVAS_BIN_RENDERER_TYPE_DEFAULT ) && st_ivas->hHrtfFastConv == NULL )
    2958             :         {
    2959        2283 :             if ( ( error = ivas_HRTF_fastconv_binary_open( &st_ivas->hHrtfFastConv ) ) != IVAS_ERR_OK )
    2960             :             {
    2961           0 :                 return error;
    2962             :             }
    2963             :         }
    2964             : 
    2965             :         /* Parametric binaural renderer */
    2966       19626 :         if ( ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_PARAMBIN || binaural_renderer == IVAS_BIN_RENDERER_TYPE_DEFAULT ) && st_ivas->hHrtfParambin == NULL )
    2967             :         {
    2968        4896 :             if ( ( error = ivas_HRTF_parambin_binary_open( &st_ivas->hHrtfParambin ) ) != IVAS_ERR_OK )
    2969             :             {
    2970           0 :                 return error;
    2971             :             }
    2972             :         }
    2973             : 
    2974       19626 :         if ( st_ivas->hHrtfStatistics == NULL && ( binaural_renderer == IVAS_BIN_RENDERER_TYPE_FASTCONV || binaural_renderer == IVAS_BIN_RENDERER_TYPE_CREND ) && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB )
    2975             :         {
    2976         696 :             if ( ( error = ivas_HRTF_statistics_binary_open( &st_ivas->hHrtfStatistics ) ) != IVAS_ERR_OK )
    2977             :             {
    2978           0 :                 return error;
    2979             :             }
    2980             :         }
    2981             :     }
    2982             : 
    2983       19626 :     return IVAS_ERR_OK;
    2984             : }
    2985             : 
    2986             : 
    2987             : /*---------------------------------------------------------------------*
    2988             :  * IVAS_DEC_HRTF_binary_close( )
    2989             :  *
    2990             :  * Deallocate HRTF binary handles
    2991             :  *---------------------------------------------------------------------*/
    2992             : 
    2993        9813 : ivas_error IVAS_DEC_HRTF_binary_close(
    2994             :     IVAS_DEC_HANDLE hIvasDec,                          /* i/o: IVAS decoder handle              */
    2995             :     const IVAS_BIN_RENDERER_TYPE binaural_renderer_old /* i  : previous binaural renderer type  */
    2996             : )
    2997             : {
    2998             :     Decoder_Struct *st_ivas;
    2999             : 
    3000        9813 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    3001             :     {
    3002           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3003             :     }
    3004             : 
    3005        9813 :     st_ivas = hIvasDec->st_ivas;
    3006             : 
    3007        9813 :     if ( st_ivas->hDecoderConfig->Opt_HRTF_binary && st_ivas->ini_frame > 0 )
    3008             :     {
    3009        2013 :         if ( !( binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_TDREND || binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_DEFAULT ) )
    3010             :         {
    3011        1980 :             ivas_HRTF_binary_close( &st_ivas->hHrtfTD );
    3012             :         }
    3013             : 
    3014        2013 :         if ( !( binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_CREND || binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_DEFAULT ) )
    3015             :         {
    3016             :             /* CRend binaural renderer handle */
    3017        1887 :             ivas_HRTF_CRend_binary_close( &st_ivas->hHrtfCrend );
    3018             :         }
    3019             : 
    3020        2013 :         if ( !( binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_FASTCONV || binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_DEFAULT ) )
    3021             :         {
    3022             :             /* Fastconv HRTF memories */
    3023        1548 :             ivas_binaural_hrtf_close( &st_ivas->hHrtfFastConv );
    3024             : 
    3025             :             /* Fastconv HRTF filters */
    3026        1548 :             ivas_HRTF_fastconv_binary_close( &st_ivas->hHrtfFastConv );
    3027             :         }
    3028             : 
    3029        2013 :         if ( !( binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_PARAMBIN || binaural_renderer_old == IVAS_BIN_RENDERER_TYPE_DEFAULT ) )
    3030             :         {
    3031             : 
    3032             :             /* Parametric binauralizer HRTF filters */
    3033         624 :             ivas_HRTF_parambin_binary_close( &st_ivas->hHrtfParambin );
    3034             :         }
    3035             :     }
    3036             : 
    3037        9813 :     return IVAS_ERR_OK;
    3038             : }
    3039             : 
    3040             : 
    3041             : /*---------------------------------------------------------------------*
    3042             :  * IVAS_DEC_AddAcousticEnvironment( )
    3043             :  *
    3044             :  * Adds acoustic environment configuration
    3045             :  *---------------------------------------------------------------------*/
    3046             : 
    3047         129 : ivas_error IVAS_DEC_AddAcousticEnvironment(
    3048             :     IVAS_DEC_HANDLE hIvasDec,                                 /* i/o: IVAS decoder handle         */
    3049             :     const IVAS_ROOM_ACOUSTICS_CONFIG_DATA roomAcousticsConfig /* i  : Room acoustic configuration */
    3050             : )
    3051             : {
    3052             :     uint16_t n;
    3053             :     Decoder_Struct *st_ivas;
    3054         129 :     IVAS_ROOM_ACOUSTICS_CONFIG_DATA *pAE = NULL;
    3055             : 
    3056         129 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || ( hIvasDec->st_ivas->acousticEnvironmentsCount > 0 && hIvasDec->st_ivas->pAcousticEnvironments == NULL ) )
    3057             :     {
    3058           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3059             :     }
    3060             : 
    3061         129 :     st_ivas = hIvasDec->st_ivas;
    3062             : 
    3063             :     /* Check if already there */
    3064         192 :     for ( n = 0; n < st_ivas->acousticEnvironmentsCount; n++ )
    3065             :     {
    3066          63 :         if ( st_ivas->pAcousticEnvironments[n].aeID == roomAcousticsConfig.aeID )
    3067             :         {
    3068           0 :             pAE = &st_ivas->pAcousticEnvironments[n];
    3069           0 :             break;
    3070             :         }
    3071             :     }
    3072             : 
    3073             :     /* If not found */
    3074         129 :     if ( pAE == NULL )
    3075             :     {
    3076         129 :         IVAS_ROOM_ACOUSTICS_CONFIG_DATA *ppAE = malloc( ( st_ivas->acousticEnvironmentsCount + 1 ) * sizeof( IVAS_ROOM_ACOUSTICS_CONFIG_DATA ) );
    3077             : 
    3078         129 :         if ( ppAE == NULL )
    3079             :         {
    3080           0 :             return IVAS_ERR_FAILED_ALLOC;
    3081             :         }
    3082             : 
    3083         192 :         for ( n = 0; n < st_ivas->acousticEnvironmentsCount; n++ )
    3084             :         {
    3085          63 :             pAE = &ppAE[n];
    3086          63 :             pAE->aeID = st_ivas->pAcousticEnvironments[n].aeID;
    3087          63 :             pAE->nBands = st_ivas->pAcousticEnvironments[n].nBands;
    3088          63 :             pAE->acousticPreDelay = st_ivas->pAcousticEnvironments[n].acousticPreDelay;
    3089          63 :             pAE->inputPreDelay = st_ivas->pAcousticEnvironments[n].inputPreDelay;
    3090             : 
    3091          63 :             mvr2r( st_ivas->pAcousticEnvironments[n].pFc_input, pAE->pFc_input, CLDFB_NO_CHANNELS_MAX );
    3092          63 :             mvr2r( st_ivas->pAcousticEnvironments[n].pAcoustic_rt60, pAE->pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
    3093          63 :             mvr2r( st_ivas->pAcousticEnvironments[n].pAcoustic_dsr, pAE->pAcoustic_dsr, CLDFB_NO_CHANNELS_MAX );
    3094             : 
    3095          63 :             pAE->use_er = st_ivas->pAcousticEnvironments[n].use_er;
    3096             : 
    3097          63 :             if ( pAE->use_er == 1 )
    3098             :             {
    3099          21 :                 pAE->lowComplexity = st_ivas->pAcousticEnvironments[n].lowComplexity;
    3100          21 :                 pAE->dimensions.x = st_ivas->pAcousticEnvironments[n].dimensions.x;
    3101          21 :                 pAE->dimensions.y = st_ivas->pAcousticEnvironments[n].dimensions.y;
    3102          21 :                 pAE->dimensions.z = st_ivas->pAcousticEnvironments[n].dimensions.z;
    3103          21 :                 pAE->ListenerOrigin.x = st_ivas->pAcousticEnvironments[n].ListenerOrigin.x;
    3104          21 :                 pAE->ListenerOrigin.y = st_ivas->pAcousticEnvironments[n].ListenerOrigin.y;
    3105          21 :                 pAE->ListenerOrigin.z = st_ivas->pAcousticEnvironments[n].ListenerOrigin.z;
    3106             : 
    3107          21 :                 mvr2r( st_ivas->pAcousticEnvironments[n].AbsCoeff, pAE->AbsCoeff, IVAS_ROOM_ABS_COEFF );
    3108             :             }
    3109             :         }
    3110             : 
    3111         129 :         free( st_ivas->pAcousticEnvironments );
    3112         129 :         st_ivas->pAcousticEnvironments = ppAE;
    3113         129 :         n = st_ivas->acousticEnvironmentsCount++;
    3114         129 :         pAE = &st_ivas->pAcousticEnvironments[n];
    3115             :     }
    3116             : 
    3117         129 :     pAE->aeID = roomAcousticsConfig.aeID;
    3118         129 :     pAE->nBands = roomAcousticsConfig.nBands;
    3119         129 :     pAE->acousticPreDelay = roomAcousticsConfig.acousticPreDelay;
    3120         129 :     pAE->inputPreDelay = roomAcousticsConfig.inputPreDelay;
    3121             : 
    3122         129 :     mvr2r( roomAcousticsConfig.pFc_input, pAE->pFc_input, CLDFB_NO_CHANNELS_MAX );
    3123         129 :     mvr2r( roomAcousticsConfig.pAcoustic_rt60, pAE->pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
    3124         129 :     mvr2r( roomAcousticsConfig.pAcoustic_dsr, pAE->pAcoustic_dsr, CLDFB_NO_CHANNELS_MAX );
    3125             : 
    3126         129 :     pAE->use_er = roomAcousticsConfig.use_er;
    3127             : 
    3128         129 :     if ( pAE->use_er == 1 )
    3129             :     {
    3130          54 :         pAE->lowComplexity = roomAcousticsConfig.lowComplexity;
    3131          54 :         pAE->dimensions.x = roomAcousticsConfig.dimensions.x;
    3132          54 :         pAE->dimensions.y = roomAcousticsConfig.dimensions.y;
    3133          54 :         pAE->dimensions.z = roomAcousticsConfig.dimensions.z;
    3134          54 :         pAE->ListenerOrigin.x = roomAcousticsConfig.ListenerOrigin.x;
    3135          54 :         pAE->ListenerOrigin.y = roomAcousticsConfig.ListenerOrigin.y;
    3136          54 :         pAE->ListenerOrigin.z = roomAcousticsConfig.ListenerOrigin.z;
    3137             : 
    3138          54 :         mvr2r( roomAcousticsConfig.AbsCoeff, pAE->AbsCoeff, IVAS_ROOM_ABS_COEFF );
    3139             :     }
    3140             : 
    3141         129 :     return IVAS_ERR_OK;
    3142             : }
    3143             : 
    3144             : 
    3145             : /*---------------------------------------------------------------------*
    3146             :  * IVAS_DEC_GetAcousticEnvironment( )
    3147             :  *
    3148             :  * Gets acoustic environment configuration with a given ID
    3149             :  *---------------------------------------------------------------------*/
    3150             : 
    3151          87 : ivas_error IVAS_DEC_GetAcousticEnvironment(
    3152             :     IVAS_DEC_HANDLE hIvasDec,               /* i/o: IVAS decoder handle                     */
    3153             :     uint16_t aeID,                          /* i  : Acoustic environment ID                 */
    3154             :     IVAS_ROOM_ACOUSTICS_CONFIG_DATA *pAcEnv /* o  : Room acoustic environment data pointer  */
    3155             : )
    3156             : {
    3157             :     uint16_t n, m;
    3158          87 :     uint16_t found = 0;
    3159             :     Decoder_Struct *st_ivas;
    3160             : 
    3161          87 :     if ( hIvasDec == NULL || pAcEnv == NULL )
    3162             :     {
    3163           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3164             :     }
    3165             : 
    3166          87 :     st_ivas = hIvasDec->st_ivas;
    3167             : 
    3168             :     /* In case of default AE ID, select the first one available */
    3169          87 :     if ( aeID == IVAS_DEFAULT_AEID && st_ivas->acousticEnvironmentsCount > 0 )
    3170             :     {
    3171          75 :         aeID = st_ivas->pAcousticEnvironments[0].aeID;
    3172             :     }
    3173             : 
    3174         216 :     for ( n = 0; n < st_ivas->acousticEnvironmentsCount; n++ )
    3175             :     {
    3176         129 :         IVAS_ROOM_ACOUSTICS_CONFIG_DATA ae = st_ivas->pAcousticEnvironments[n];
    3177             : 
    3178         129 :         if ( aeID == ae.aeID )
    3179             :         {
    3180          87 :             found = 1;
    3181          87 :             pAcEnv->aeID = aeID;
    3182          87 :             pAcEnv->nBands = ae.nBands;
    3183          87 :             pAcEnv->inputPreDelay = ae.inputPreDelay;
    3184        2637 :             for ( m = 0; m < pAcEnv->nBands; m++ )
    3185             :             {
    3186        2550 :                 pAcEnv->pFc_input[m] = ae.pFc_input[m];
    3187        2550 :                 pAcEnv->pAcoustic_rt60[m] = ae.pAcoustic_rt60[m];
    3188        2550 :                 pAcEnv->pAcoustic_dsr[m] = ae.pAcoustic_dsr[m];
    3189             :             }
    3190             : 
    3191             :             /* If ER are allocated then propagate parameters */
    3192          87 :             pAcEnv->use_er = ae.use_er;
    3193          87 :             if ( ae.use_er != 0 )
    3194             :             {
    3195          21 :                 pAcEnv->lowComplexity = ae.lowComplexity;
    3196             : 
    3197          21 :                 pAcEnv->dimensions.x = ae.dimensions.x;
    3198          21 :                 pAcEnv->dimensions.y = ae.dimensions.y;
    3199          21 :                 pAcEnv->dimensions.z = ae.dimensions.z;
    3200             : 
    3201          21 :                 pAcEnv->ListenerOrigin.x = ae.ListenerOrigin.x;
    3202          21 :                 pAcEnv->ListenerOrigin.y = ae.ListenerOrigin.y;
    3203          21 :                 pAcEnv->ListenerOrigin.z = ae.ListenerOrigin.z;
    3204             : 
    3205         147 :                 for ( m = 0; m < IVAS_ROOM_ABS_COEFF; m++ )
    3206             :                 {
    3207         126 :                     pAcEnv->AbsCoeff[m] = ae.AbsCoeff[m];
    3208             :                 }
    3209             :             }
    3210             :         }
    3211             :     }
    3212             : 
    3213          87 :     return found ? IVAS_ERR_OK : IVAS_ERR_ACOUSTIC_ENVIRONMENT_MISSING;
    3214             : }
    3215             : 
    3216             : 
    3217             : /*---------------------------------------------------------------------*
    3218             :  * copyRendererConfigStruct( )
    3219             :  *
    3220             :  *
    3221             :  *---------------------------------------------------------------------*/
    3222             : 
    3223        3312 : static ivas_error copyRendererConfigStruct(
    3224             :     RENDER_CONFIG_HANDLE hRCin,
    3225             :     IVAS_RENDER_CONFIG_HANDLE hRCout )
    3226             : {
    3227        3312 :     if ( hRCin == NULL || hRCout == NULL )
    3228             :     {
    3229           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3230             :     }
    3231             : 
    3232        3312 :     hRCout->roomAcoustics.aeID = hRCin->roomAcoustics.aeID;
    3233        3312 :     hRCout->roomAcoustics.nBands = hRCin->roomAcoustics.nBands;
    3234        3312 :     hRCout->roomAcoustics.acousticPreDelay = hRCin->roomAcoustics.acousticPreDelay;
    3235        3312 :     hRCout->roomAcoustics.inputPreDelay = hRCin->roomAcoustics.inputPreDelay;
    3236             : 
    3237        3312 :     mvr2r( hRCin->roomAcoustics.pFc_input, hRCout->roomAcoustics.pFc_input, CLDFB_NO_CHANNELS_MAX );
    3238        3312 :     mvr2r( hRCin->roomAcoustics.pAcoustic_rt60, hRCout->roomAcoustics.pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
    3239        3312 :     mvr2r( hRCin->roomAcoustics.pAcoustic_dsr, hRCout->roomAcoustics.pAcoustic_dsr, CLDFB_NO_CHANNELS_MAX );
    3240        3312 :     mvr2r( hRCin->directivity, hRCout->directivity, 3 * MAX_NUM_OBJECTS );
    3241        3312 :     mvr2r( hRCin->distAtt, hRCout->distAtt, 3 );
    3242             : 
    3243        3312 :     hRCout->split_rend_config = hRCin->split_rend_config;
    3244             : 
    3245        3312 :     hRCout->roomAcoustics.use_er = hRCin->roomAcoustics.use_er;
    3246        3312 :     hRCout->roomAcoustics.lowComplexity = hRCin->roomAcoustics.lowComplexity;
    3247             : 
    3248        3312 :     return IVAS_ERR_OK;
    3249             : }
    3250             : 
    3251             : 
    3252             : /*---------------------------------------------------------------------*
    3253             :  * IVAS_DEC_GetRenderConfig( )
    3254             :  *
    3255             :  * Return renderer configuration parameters handle
    3256             :  *---------------------------------------------------------------------*/
    3257             : 
    3258        3312 : ivas_error IVAS_DEC_GetRenderConfig(
    3259             :     IVAS_DEC_HANDLE hIvasDec,              /* i/o: IVAS decoder handle         */
    3260             :     const IVAS_RENDER_CONFIG_HANDLE hRCout /* o  : Render configuration handle */
    3261             : )
    3262             : {
    3263        3312 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hRenderConfig == NULL || hRCout == NULL )
    3264             :     {
    3265           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3266             :     }
    3267             : 
    3268        3312 :     return copyRendererConfigStruct( hIvasDec->st_ivas->hRenderConfig, hRCout );
    3269             : }
    3270             : 
    3271             : 
    3272             : /*---------------------------------------------------------------------*
    3273             :  * IVAS_DEC_GetDefaultRenderConfig( )
    3274             :  *
    3275             :  * Return default renderer configuration parameters
    3276             :  *---------------------------------------------------------------------*/
    3277             : 
    3278             : /*! r: error code*/
    3279           0 : ivas_error IVAS_DEC_GetDefaultRenderConfig(
    3280             :     IVAS_RENDER_CONFIG_HANDLE hRCout /* o  : Render config handle   */
    3281             : )
    3282             : {
    3283             :     RENDER_CONFIG_DATA RCin;
    3284           0 :     RENDER_CONFIG_HANDLE hRCin = &RCin;
    3285             :     ivas_error error;
    3286             : 
    3287           0 :     if ( ( error = ivas_render_config_init_from_rom( &hRCin ) ) != IVAS_ERR_OK )
    3288             :     {
    3289           0 :         return error;
    3290             :     }
    3291             : 
    3292           0 :     return copyRendererConfigStruct( hRCin, hRCout );
    3293             : }
    3294             : 
    3295             : 
    3296             : /*---------------------------------------------------------------------*
    3297             :  * IVAS_DEC_FeedRenderConfig( )
    3298             :  *
    3299             :  * Set renderer configuration (acoustic environment) parameters
    3300             :  *---------------------------------------------------------------------*/
    3301             : 
    3302        2209 : ivas_error IVAS_DEC_FeedRenderConfig(
    3303             :     IVAS_DEC_HANDLE hIvasDec,                  /* i/o: IVAS decoder handle         */
    3304             :     const IVAS_RENDER_CONFIG_DATA renderConfig /* i  : Render configuration struct */
    3305             : )
    3306             : {
    3307             :     RENDER_CONFIG_HANDLE hRenderConfig;
    3308             :     Decoder_Struct *st_ivas;
    3309             :     ivas_error error;
    3310             : 
    3311        2209 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hRenderConfig == NULL )
    3312             :     {
    3313           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3314             :     }
    3315             : 
    3316        2209 :     hRenderConfig = hIvasDec->st_ivas->hRenderConfig;
    3317        2209 :     st_ivas = hIvasDec->st_ivas;
    3318             : 
    3319        2209 :     hRenderConfig->roomAcoustics.aeID = renderConfig.roomAcoustics.aeID;
    3320        2209 :     hRenderConfig->roomAcoustics.nBands = renderConfig.roomAcoustics.nBands;
    3321        2209 :     hRenderConfig->roomAcoustics.acousticPreDelay = renderConfig.roomAcoustics.acousticPreDelay;
    3322        2209 :     hRenderConfig->roomAcoustics.inputPreDelay = renderConfig.roomAcoustics.inputPreDelay;
    3323             : 
    3324        2209 :     hRenderConfig->roomAcoustics.use_er = 0;
    3325        2209 :     if ( renderConfig.roomAcoustics.use_er == 1 )
    3326             :     {
    3327          42 :         hRenderConfig->roomAcoustics.use_er = renderConfig.roomAcoustics.use_er;
    3328          42 :         hRenderConfig->roomAcoustics.lowComplexity = renderConfig.roomAcoustics.lowComplexity;
    3329          42 :         hRenderConfig->roomAcoustics.dimensions = renderConfig.roomAcoustics.dimensions;
    3330          42 :         hRenderConfig->roomAcoustics.ListenerOrigin = renderConfig.roomAcoustics.ListenerOrigin;
    3331             : 
    3332          42 :         mvr2r( renderConfig.roomAcoustics.AbsCoeff, hRenderConfig->roomAcoustics.AbsCoeff, IVAS_ROOM_ABS_COEFF );
    3333             :     }
    3334             : 
    3335        2209 :     mvr2r( renderConfig.roomAcoustics.pFc_input, hRenderConfig->roomAcoustics.pFc_input, CLDFB_NO_CHANNELS_MAX );
    3336        2209 :     mvr2r( renderConfig.roomAcoustics.pAcoustic_rt60, hRenderConfig->roomAcoustics.pAcoustic_rt60, CLDFB_NO_CHANNELS_MAX );
    3337        2209 :     mvr2r( renderConfig.roomAcoustics.pAcoustic_dsr, hRenderConfig->roomAcoustics.pAcoustic_dsr, CLDFB_NO_CHANNELS_MAX );
    3338             : 
    3339             :     /* Re-initialize reverb instance if already available */
    3340             : 
    3341             :     /* TD renderer Jot reverberator */
    3342        2209 :     if ( st_ivas->hReverb != NULL )
    3343             :     {
    3344           0 :         if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
    3345             :         {
    3346           0 :             return error;
    3347             :         }
    3348             :     }
    3349             : 
    3350             :     /* CREND Jot reverberator */
    3351        2209 :     if ( st_ivas->hCrendWrapper != NULL && st_ivas->hCrendWrapper->hCrend[0] != NULL && st_ivas->hCrendWrapper->hCrend[0]->hReverb != NULL )
    3352             :     {
    3353          12 :         if ( ( error = ivas_reverb_open( &st_ivas->hCrendWrapper->hCrend[0]->hReverb, st_ivas->hHrtfStatistics, hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
    3354             :         {
    3355           0 :             return error;
    3356             :         }
    3357             :     }
    3358             : 
    3359             :     /* Parametric renderer reverberator */
    3360        2209 :     if ( st_ivas->hDiracDecBin[0] != NULL && st_ivas->hDiracDecBin[0]->hReverb != NULL )
    3361             :     {
    3362          12 :         ivas_binaural_reverb_close( &( st_ivas->hDiracDecBin[0]->hReverb ) );
    3363             : 
    3364          12 :         if ( ( error = ivas_binaural_reverb_init( &( st_ivas->hDiracDecBin[0]->hReverb ), st_ivas->hHrtfStatistics, st_ivas->hSpatParamRendCom->num_freq_bands, CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES,
    3365          12 :                                                   &( hRenderConfig->roomAcoustics ), st_ivas->hDecoderConfig->output_Fs, NULL, NULL, NULL ) ) != IVAS_ERR_OK )
    3366             :         {
    3367           0 :             return error;
    3368             :         }
    3369             :     }
    3370             : 
    3371             :     /* FastConv renderer reverberator */
    3372        2209 :     if ( st_ivas->hBinRenderer != NULL && st_ivas->hBinRenderer->hReverb != NULL )
    3373             :     {
    3374           9 :         ivas_binaural_reverb_close( &( st_ivas->hBinRenderer->hReverb ) );
    3375             : 
    3376           9 :         if ( ( error = ivas_binaural_reverb_init( &( st_ivas->hBinRenderer->hReverb ), st_ivas->hHrtfStatistics, st_ivas->hBinRenderer->conv_band, st_ivas->hBinRenderer->timeSlots,
    3377           9 :                                                   &( hRenderConfig->roomAcoustics ), st_ivas->hDecoderConfig->output_Fs, NULL, NULL, NULL ) ) != IVAS_ERR_OK )
    3378             :         {
    3379           0 :             return error;
    3380             :         }
    3381             :     }
    3382             : 
    3383        2209 :     mvr2r( renderConfig.directivity, hRenderConfig->directivity, 3 * MAX_NUM_OBJECTS );
    3384        2209 :     mvr2r( renderConfig.distAtt, hRenderConfig->distAtt, 3 );
    3385             : 
    3386        2209 :     hRenderConfig->split_rend_config = renderConfig.split_rend_config;
    3387             : 
    3388             :     /* Overwrite any pose correction settings if 0 DOF (no pose correction) was selected */
    3389        2209 :     if ( hRenderConfig->split_rend_config.dof == 0 )
    3390             :     {
    3391         676 :         hRenderConfig->split_rend_config.poseCorrectionMode = ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE;
    3392             :     }
    3393             : 
    3394        2209 :     if ( is_split_rendering_enabled( st_ivas->hDecoderConfig, hRenderConfig ) )
    3395             :     {
    3396        2080 :         if ( ( error = isar_split_rend_validate_config( &hRenderConfig->split_rend_config, ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0 ) ) != IVAS_ERR_OK )
    3397             :         {
    3398           0 :             return error;
    3399             :         }
    3400             :     }
    3401             : 
    3402        2209 :     return IVAS_ERR_OK;
    3403             : }
    3404             : 
    3405             : 
    3406             : /*---------------------------------------------------------------------*
    3407             :  * feedAcousticEnvPI( )
    3408             :  *
    3409             :  * Set acoustic environment from the PI data
    3410             :  *---------------------------------------------------------------------*/
    3411             : 
    3412           0 : static ivas_error feedAcousticEnvPI(
    3413             :     IVAS_DEC_HANDLE hIvasDec,                   /* i/o: IVAS decoder handle         */
    3414             :     const IVAS_PIDATA_ACOUSTIC_ENV hAcoustEnvPI /* i  : Render configuration struct */
    3415             : )
    3416             : {
    3417             :     RENDER_CONFIG_HANDLE hRenderConfig;
    3418             :     Decoder_Struct *st_ivas;
    3419             :     ivas_error error;
    3420             : 
    3421           0 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hRenderConfig == NULL )
    3422             :     {
    3423           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3424             :     }
    3425             : 
    3426           0 :     hRenderConfig = hIvasDec->st_ivas->hRenderConfig;
    3427           0 :     st_ivas = hIvasDec->st_ivas;
    3428             : 
    3429             :     /* Ignore if AE ID already in use */
    3430           0 :     if ( hRenderConfig->roomAcoustics.aeID == hAcoustEnvPI.aeid )
    3431             :     {
    3432           0 :         return IVAS_ERR_OK;
    3433             :     }
    3434             : 
    3435             :     /* Attempt to load the one already available */
    3436           0 :     if ( ( error = IVAS_DEC_GetAcousticEnvironment( hIvasDec, hAcoustEnvPI.aeid, &hRenderConfig->roomAcoustics ) ) == IVAS_ERR_ACOUSTIC_ENVIRONMENT_MISSING )
    3437             :     {
    3438             :         /* Add the new compact room environment */
    3439             :         IVAS_ROOM_ACOUSTICS_CONFIG_DATA acEnv;
    3440             : 
    3441           0 :         acEnv.aeID = hAcoustEnvPI.aeid;
    3442           0 :         acEnv.nBands = IVAS_PI_AE_NUM_BANDS;
    3443           0 :         acEnv.pFc_input[IVAS_PI_AE_LOW] = IVAS_PI_AE_LOW_FREQ;
    3444           0 :         acEnv.pFc_input[IVAS_PI_AE_MID] = IVAS_PI_AE_MID_FREQ;
    3445           0 :         acEnv.pFc_input[IVAS_PI_AE_HIGH] = IVAS_PI_AE_HIGH_FREQ;
    3446           0 :         acEnv.pAcoustic_rt60[IVAS_PI_AE_LOW] = hAcoustEnvPI.rt60[IVAS_PI_AE_LOW];
    3447           0 :         acEnv.pAcoustic_rt60[IVAS_PI_AE_MID] = hAcoustEnvPI.rt60[IVAS_PI_AE_MID];
    3448           0 :         acEnv.pAcoustic_rt60[IVAS_PI_AE_HIGH] = hAcoustEnvPI.rt60[IVAS_PI_AE_HIGH];
    3449           0 :         acEnv.inputPreDelay = (float) ( 0.1 * hRenderConfig->roomAcoustics.pAcoustic_rt60[IVAS_PI_AE_MID] );
    3450           0 :         acEnv.pAcoustic_dsr[IVAS_PI_AE_LOW] = powf( 10.0f, hAcoustEnvPI.dsr[IVAS_PI_AE_LOW] / 10.0f );
    3451           0 :         acEnv.pAcoustic_dsr[IVAS_PI_AE_MID] = powf( 10.0f, hAcoustEnvPI.dsr[IVAS_PI_AE_MID] / 10.0f );
    3452           0 :         acEnv.pAcoustic_dsr[IVAS_PI_AE_HIGH] = powf( 10.0f, hAcoustEnvPI.dsr[IVAS_PI_AE_HIGH] / 10.0f );
    3453             : 
    3454           0 :         acEnv.use_er = hAcoustEnvPI.availEarlyReflections;
    3455             : 
    3456           0 :         if ( hAcoustEnvPI.availEarlyReflections )
    3457             :         {
    3458           0 :             hRenderConfig->roomAcoustics.dimensions.x = hAcoustEnvPI.roomDimensions.x;
    3459           0 :             hRenderConfig->roomAcoustics.dimensions.y = hAcoustEnvPI.roomDimensions.y;
    3460           0 :             hRenderConfig->roomAcoustics.dimensions.z = hAcoustEnvPI.roomDimensions.z;
    3461           0 :             mvr2r( hAcoustEnvPI.absorbCoeffs, hRenderConfig->roomAcoustics.AbsCoeff, IVAS_ROOM_ABS_COEFF );
    3462             :         }
    3463             : 
    3464           0 :         if ( ( error = IVAS_DEC_AddAcousticEnvironment( hIvasDec, acEnv ) ) != IVAS_ERR_OK )
    3465             :         {
    3466           0 :             return error;
    3467             :         }
    3468             : 
    3469           0 :         if ( ( error = IVAS_DEC_GetAcousticEnvironment( hIvasDec, hAcoustEnvPI.aeid, &hRenderConfig->roomAcoustics ) ) != IVAS_ERR_OK )
    3470             :         {
    3471           0 :             return error;
    3472             :         }
    3473             :     }
    3474             : 
    3475             :     /* Re-initialize reverb instance if already available */
    3476             : 
    3477             :     /* TD renderer Jot reverberator */
    3478           0 :     if ( st_ivas->hReverb != NULL )
    3479             :     {
    3480           0 :         if ( ( error = ivas_reverb_open( &st_ivas->hReverb, st_ivas->hHrtfStatistics, hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
    3481             :         {
    3482           0 :             return error;
    3483             :         }
    3484             :     }
    3485             : 
    3486             :     /* CREND Jot reverberator */
    3487           0 :     if ( st_ivas->hCrendWrapper != NULL && st_ivas->hCrendWrapper->hCrend[0] != NULL && st_ivas->hCrendWrapper->hCrend[0]->hReverb != NULL )
    3488             :     {
    3489           0 :         if ( ( error = ivas_reverb_open( &st_ivas->hCrendWrapper->hCrend[0]->hReverb, st_ivas->hHrtfStatistics, hRenderConfig, st_ivas->hDecoderConfig->output_Fs ) ) != IVAS_ERR_OK )
    3490             :         {
    3491           0 :             return error;
    3492             :         }
    3493             :     }
    3494             : 
    3495             :     /* Parametric renderer reverberator */
    3496           0 :     if ( st_ivas->hDiracDecBin[0] != NULL && st_ivas->hDiracDecBin[0]->hReverb != NULL )
    3497             :     {
    3498           0 :         ivas_binaural_reverb_close( &( st_ivas->hDiracDecBin[0]->hReverb ) );
    3499             : 
    3500           0 :         if ( ( error = ivas_binaural_reverb_init( &( st_ivas->hDiracDecBin[0]->hReverb ), st_ivas->hHrtfStatistics, st_ivas->hSpatParamRendCom->num_freq_bands, CLDFB_NO_COL_MAX / MAX_PARAM_SPATIAL_SUBFRAMES,
    3501           0 :                                                   &( hRenderConfig->roomAcoustics ), st_ivas->hDecoderConfig->output_Fs, NULL, NULL, NULL ) ) != IVAS_ERR_OK )
    3502             :         {
    3503           0 :             return error;
    3504             :         }
    3505             :     }
    3506             : 
    3507             :     /* FastConv renderer reverberator */
    3508           0 :     if ( st_ivas->hBinRenderer != NULL && st_ivas->hBinRenderer->hReverb != NULL )
    3509             :     {
    3510           0 :         ivas_binaural_reverb_close( &( st_ivas->hBinRenderer->hReverb ) );
    3511             : 
    3512           0 :         if ( ( error = ivas_binaural_reverb_init( &( st_ivas->hBinRenderer->hReverb ), st_ivas->hHrtfStatistics, st_ivas->hBinRenderer->conv_band, st_ivas->hBinRenderer->timeSlots,
    3513           0 :                                                   &( hRenderConfig->roomAcoustics ), st_ivas->hDecoderConfig->output_Fs, NULL, NULL, NULL ) ) != IVAS_ERR_OK )
    3514             :         {
    3515           0 :             return error;
    3516             :         }
    3517             :     }
    3518             : 
    3519           0 :     return IVAS_ERR_OK;
    3520             : }
    3521             : 
    3522             : 
    3523             : /*---------------------------------------------------------------------*
    3524             :  * IVAS_DEC_GetDelay( )
    3525             :  *
    3526             :  * Return IVAS decoder delay in nanoseconds
    3527             :  *---------------------------------------------------------------------*/
    3528             : 
    3529      196637 : ivas_error IVAS_DEC_GetDelay(
    3530             :     IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle                                            */
    3531             :     int16_t *nSamples,        /* o  : decoder delay in samples                                       */
    3532             :     int32_t *timeScale        /* o  : time scale of the delay, equal to decoder output sampling rate */
    3533             : )
    3534             : {
    3535             :     Decoder_Struct *st_ivas;
    3536             :     DECODER_CONFIG_HANDLE hDecoderConfig;
    3537             : 
    3538      196637 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || nSamples == NULL || timeScale == NULL )
    3539             :     {
    3540           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3541             :     }
    3542             : 
    3543      196637 :     if ( !hIvasDec->hasDecodedFirstGoodFrame )
    3544             :     {
    3545             :         /* Delay depends on IVAS format, which is unknown until first frame has been decoded */
    3546           0 :         return IVAS_ERR_WAITING_FOR_BITSTREAM;
    3547             :     }
    3548             : 
    3549      196637 :     st_ivas = hIvasDec->st_ivas;
    3550      196637 :     hDecoderConfig = st_ivas->hDecoderConfig;
    3551             : 
    3552      196637 :     nSamples[1] = NS2SA( hDecoderConfig->output_Fs, get_delay( DEC, hDecoderConfig->output_Fs, st_ivas->ivas_format, st_ivas->cldfbSynDec[0], hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ) );
    3553             : 
    3554      196637 :     nSamples[2] = (int16_t) roundf( (float) st_ivas->binaural_latency_ns * hDecoderConfig->output_Fs / 1000000000.f );
    3555      196637 :     nSamples[0] = nSamples[1] + nSamples[2];
    3556             : 
    3557      196637 :     if ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT )
    3558             :     {
    3559             :         /* note: in MASA, all delay is compensated at the decoder by default, so subtract the encoder delay for print-out */
    3560      125633 :         nSamples[1] -= NS2SA( hDecoderConfig->output_Fs, IVAS_ENC_DELAY_NS );
    3561             :     }
    3562             : 
    3563      196637 :     *timeScale = hDecoderConfig->output_Fs;
    3564             : 
    3565      196637 :     return IVAS_ERR_OK;
    3566             : }
    3567             : 
    3568             : 
    3569             : /*---------------------------------------------------------------------*
    3570             :  * IVAS_DEC_HasDecodedFirstGoodFrame( )
    3571             :  *
    3572             :  * Return flag indicating if the decoder has decoded a good frame
    3573             :  *---------------------------------------------------------------------*/
    3574             : 
    3575      502471 : ivas_error IVAS_DEC_HasDecodedFirstGoodFrame(
    3576             :     IVAS_DEC_HANDLE hIvasDec,      /* i/o: IVAS decoder handle                                                             */
    3577             :     bool *hasDecodedFirstGoodFrame /* o  : flag indicating if the decoder has decoded a good frame since it was configured */
    3578             : )
    3579             : {
    3580      502471 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hasDecodedFirstGoodFrame == NULL )
    3581             :     {
    3582           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3583             :     }
    3584             : 
    3585      502471 :     *hasDecodedFirstGoodFrame = hIvasDec->hasDecodedFirstGoodFrame;
    3586             : 
    3587      502471 :     return IVAS_ERR_OK;
    3588             : }
    3589             : 
    3590             : 
    3591             : /*---------------------------------------------------------------------*
    3592             :  * isSidFrame( )
    3593             :  *
    3594             :  * Check if a frame contains a SID
    3595             :  *---------------------------------------------------------------------*/
    3596             : 
    3597     1713086 : static bool isSidFrame(
    3598             :     const uint16_t size )
    3599             : {
    3600     1713086 :     if ( size == SID_1k75 / FRAMES_PER_SEC )
    3601             :     {
    3602           0 :         return true; /* AMR-WB SID */
    3603             :     }
    3604     1713086 :     else if ( size == SID_2k40 / FRAMES_PER_SEC )
    3605             :     {
    3606           0 :         return true; /* EVS SID */
    3607             :     }
    3608     1713086 :     else if ( size == IVAS_SID_5k2 / FRAMES_PER_SEC )
    3609             :     {
    3610        1192 :         return true; /* IVAS SID */
    3611             :     }
    3612             : 
    3613     1711894 :     return false;
    3614             : }
    3615             : 
    3616             : 
    3617             : /*---------------------------------------------------------------------*
    3618             :  * bsCompactToSerial( )
    3619             :  *
    3620             :  * Bitstream conversion to Byte format
    3621             :  *---------------------------------------------------------------------*/
    3622             : 
    3623     1712115 : static void bsCompactToSerial(
    3624             :     const uint8_t *compact,
    3625             :     uint16_t *serial,
    3626             :     const uint16_t num_bits )
    3627             : {
    3628             : /* Bitstream conversion is not counted towards complexity and memory usage */
    3629             : #define WMC_TOOL_SKIP
    3630             :     uint32_t i;
    3631     1712115 :     uint8_t byte = 0;
    3632     1712115 :     const uint8_t mask = 0x80;
    3633             : 
    3634  4858944691 :     for ( i = 0; i < num_bits; ++i )
    3635             :     {
    3636  4857232576 :         if ( i % 8 == 0 )
    3637             :         {
    3638   607154773 :             byte = compact[i / 8];
    3639             :         }
    3640             : 
    3641  4857232576 :         serial[i] = ( byte & mask ) >> 7;
    3642             : 
    3643  4857232576 :         byte <<= 1;
    3644             :     }
    3645             : 
    3646             :     /* Add 4 padding bytes required by core coder */
    3647    56499795 :     for ( i = 0; i < 4 * 8; ++i )
    3648             :     {
    3649    54787680 :         serial[num_bits + i] = 0;
    3650             :     }
    3651             : #undef WMC_TOOL_SKIP
    3652     1712115 : }
    3653             : 
    3654             : 
    3655             : /*---------------------------------------------------------------------*
    3656             :  * IVAS_DEC_VoIP_FeedFrame( )
    3657             :  *
    3658             :  * Feed RTP packet into internal jitter buffer
    3659             :  *---------------------------------------------------------------------*/
    3660             : 
    3661     1713086 : ivas_error IVAS_DEC_VoIP_FeedFrame(
    3662             :     IVAS_DEC_HANDLE hIvasDec,         /* i/o: IVAS decoder handle                             */
    3663             :     uint8_t *au,                      /* i  : buffer containing input access unit             */
    3664             :     const uint16_t auSize,            /* i  : size of the access unit                         */
    3665             :     const uint16_t rtpSequenceNumber, /* i  : RTP sequence number (16 bits)                   */
    3666             :     const uint32_t rtpTimeStamp,      /* i  : RTP timestamp (32 bits)                         */
    3667             :     const uint32_t rcvTime_ms,        /* i  : receive time of the RTP packet in milliseconds  */
    3668             :     const bool qBit                   /* i  : Q bit for AMR-WB IO                             */
    3669             : )
    3670             : {
    3671             :     JB4_DATAUNIT_HANDLE dataUnit;
    3672             :     int16_t partialCopyFrameType, partialCopyOffset;
    3673             :     int16_t result;
    3674             : 
    3675     1713086 :     if ( hIvasDec == NULL || hIvasDec->hVoIP == NULL || au == NULL )
    3676             :     {
    3677           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3678             :     }
    3679             : 
    3680     1713086 :     if ( auSize == 0 )
    3681             :     {
    3682           0 :         return IVAS_ERR_OK; /* ignore empty/NO_DATA frame - shouldn't be transmitted in RTP */
    3683             :     }
    3684     1713086 :     if ( ( auSize + 7 ) / 8 > MAX_AU_SIZE )
    3685             :     {
    3686           0 :         return IVAS_ERR_INVALID_BITSTREAM;
    3687             :     }
    3688             : 
    3689     1713086 :     partialCopyFrameType = 0;
    3690     1713086 :     partialCopyOffset = 0;
    3691             : 
    3692     1713086 :     if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
    3693             :     {
    3694             :         /* check if frame contains a partial copy and get its offset */
    3695       15270 :         evs_dec_previewFrame( au, auSize, &partialCopyFrameType, &partialCopyOffset );
    3696             :     }
    3697             : 
    3698             :     /* create data unit for primary copy in the frame */
    3699     1713086 :     dataUnit = JB4_AllocDataUnit( hIvasDec->hVoIP->hJBM );
    3700     1713086 :     mvc2c( au, dataUnit->data, (int16_t) ( ( auSize + 7 ) / 8 ) );
    3701     1713086 :     dataUnit->dataSize = auSize;
    3702     1713086 :     dataUnit->duration = 20;
    3703     1713086 :     dataUnit->sequenceNumber = rtpSequenceNumber;
    3704     1713086 :     dataUnit->silenceIndicator = isSidFrame( dataUnit->dataSize );
    3705     1713086 :     dataUnit->timeScale = 1000;
    3706     1713086 :     dataUnit->rcvTime = rcvTime_ms;
    3707     1713086 :     dataUnit->timeStamp = rtpTimeStamp;
    3708     1713086 :     dataUnit->partial_frame = 0;
    3709     1713086 :     dataUnit->partialCopyOffset = partialCopyOffset;
    3710     1713086 :     dataUnit->qBit = qBit;
    3711             : 
    3712             :     /* add the frame to the JBM */
    3713     1713086 :     result = JB4_PushDataUnit( hIvasDec->hVoIP->hJBM, dataUnit, rcvTime_ms );
    3714     1713086 :     if ( result != 0 )
    3715             :     {
    3716           0 :         return IVAS_ERR_UNKNOWN;
    3717             :     }
    3718             : 
    3719     1713086 :     if ( partialCopyFrameType != RF_NO_DATA && partialCopyOffset != 0 )
    3720             :     {
    3721             :         /* create data unit for partial copy in the frame */
    3722        1041 :         dataUnit = JB4_AllocDataUnit( hIvasDec->hVoIP->hJBM );
    3723        1041 :         mvc2c( au, dataUnit->data, (int16_t) ( ( auSize + 7 ) / 8 ) );
    3724        1041 :         dataUnit->dataSize = auSize;
    3725        1041 :         dataUnit->duration = 20;
    3726        1041 :         dataUnit->sequenceNumber = rtpSequenceNumber;
    3727        1041 :         dataUnit->silenceIndicator = 0; /* there are no partial copies for SID frames */
    3728        1041 :         dataUnit->timeScale = 1000;
    3729        1041 :         dataUnit->rcvTime = rcvTime_ms;
    3730        1041 :         dataUnit->timeStamp = rtpTimeStamp - partialCopyOffset * dataUnit->duration;
    3731        1041 :         dataUnit->partial_frame = 1;
    3732        1041 :         dataUnit->partialCopyOffset = partialCopyOffset;
    3733        1041 :         dataUnit->qBit = qBit;
    3734             : 
    3735             :         /* add the frame to the JBM */
    3736        1041 :         result = JB4_PushDataUnit( hIvasDec->hVoIP->hJBM, dataUnit, rcvTime_ms );
    3737        1041 :         if ( result != 0 )
    3738             :         {
    3739           0 :             return IVAS_ERR_UNKNOWN;
    3740             :         }
    3741             :     }
    3742             : 
    3743     1713086 :     return IVAS_ERR_OK;
    3744             : }
    3745             : 
    3746             : 
    3747             : /*---------------------------------------------------------------------*
    3748             :  * IVAS_DEC_VoIP_SetScale( )
    3749             :  *
    3750             :  * Set the TSM scale
    3751             :  *---------------------------------------------------------------------*/
    3752             : 
    3753     2554712 : ivas_error IVAS_DEC_VoIP_SetScale(
    3754             :     IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle                                                         */
    3755             :     const int16_t maxScaling, /* i  : max allowed absolute difference in samples from the default 20ms frame size */
    3756             :     const int16_t scale       /* i  : TSM scale to set in percent of the default frame size                       */
    3757             : )
    3758             : {
    3759     2554712 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    3760             :     {
    3761           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3762             :     }
    3763             : 
    3764     2554712 :     if ( hIvasDec->st_ivas->hDecoderConfig->Opt_tsm == 0 )
    3765             :     {
    3766           0 :         return IVAS_ERR_TSM_NOT_ENABLED;
    3767             :     }
    3768             :     else
    3769             :     {
    3770     2554712 :         hIvasDec->tsm_scale = scale;
    3771     2554712 :         hIvasDec->tsm_max_scaling = maxScaling;
    3772             :     }
    3773             : 
    3774     2554712 :     return IVAS_ERR_OK;
    3775             : }
    3776             : 
    3777             : #ifdef VARIABLE_SPEED_DECODING
    3778             : 
    3779             : /*---------------------------------------------------------------------*
    3780             :  * IVAS_DEC_EnableTsm( )
    3781             :  *
    3782             :  * Enable Time-Scale Modification (TSM)
    3783             :  *---------------------------------------------------------------------*/
    3784             : 
    3785             : ivas_error IVAS_DEC_EnableTsm(
    3786             :     IVAS_DEC_HANDLE hIvasDec /* i/o: IVAS decoder handle   */
    3787             : )
    3788             : {
    3789             :     AUDIO_CONFIG output_config;
    3790             :     ivas_error error;
    3791             : 
    3792             :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    3793             :     {
    3794             :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3795             :     }
    3796             : 
    3797             :     hIvasDec->st_ivas->hDecoderConfig->Opt_tsm = 1;
    3798             : 
    3799             :     /* init flush buffer if necessary (only needed for binaural) */
    3800             :     output_config = hIvasDec->st_ivas->hDecoderConfig->output_config;
    3801             :     if ( hIvasDec->flushbuffer == NULL && ( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) )
    3802             :     {
    3803             :         if ( ( error = create_flush_buffer( hIvasDec ) ) != IVAS_ERR_OK )
    3804             :         {
    3805             :             return error;
    3806             :         }
    3807             :     }
    3808             : 
    3809             :     return IVAS_ERR_OK;
    3810             : }
    3811             : 
    3812             : 
    3813             : /*---------------------------------------------------------------------*
    3814             :  * IVAS_DEC_TSM_SetQuality( )
    3815             :  *
    3816             :  * set the quality theshold for the time scale modiciation that is used
    3817             :  * to determine if the TSM yielded a signal that satisfies the minimum
    3818             :  * quality requirements.
    3819             :  * quality is lower limit for minimum quality
    3820             :  * Range is [-2;2] - where positive values allow
    3821             :  * only pasting with same phase information
    3822             :  * Negative values would yield cross phased pasting
    3823             :  * When not setting the minimum quality with this function the default
    3824             :  * value used is 1.0f
    3825             :  *
    3826             :  *---------------------------------------------------------------------*/
    3827             : 
    3828             : ivas_error IVAS_DEC_TSM_SetQuality(
    3829             :     IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle   */
    3830             :     const float quality       /* i  : target TSM quality    */
    3831             : )
    3832             : {
    3833             :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    3834             :     {
    3835             :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3836             :     }
    3837             : 
    3838             :     if ( !hIvasDec->st_ivas->hDecoderConfig->Opt_tsm )
    3839             :     {
    3840             :         return IVAS_ERR_TSM_NOT_ENABLED;
    3841             :     }
    3842             :     else
    3843             :     {
    3844             :         hIvasDec->tsm_quality = quality;
    3845             :     }
    3846             : 
    3847             :     return IVAS_ERR_OK;
    3848             : }
    3849             : 
    3850             : #endif
    3851             : 
    3852             : 
    3853             : /*---------------------------------------------------------------------*
    3854             :  * ivas_dec_voip_get_samples_common( )
    3855             :  *
    3856             :  * Main function to output one frame in VoIP. Holds common code for
    3857             :  * regular output configs and split rendering configs.
    3858             :  *---------------------------------------------------------------------*/
    3859             : 
    3860     6571632 : static ivas_error ivas_dec_voip_get_samples_common(
    3861             :     IVAS_DEC_HANDLE hIvasDec,                 /* i/o: IVAS decoder handle                                                     */
    3862             :     uint16_t nSamplesPerChannel,              /* i  : number of samples per channel requested to be written to output buffer  */
    3863             :     const IVAS_DEC_PCM_TYPE pcmType,          /* i  : type for the decoded PCM resolution                                     */
    3864             :     void *pcmBuf,                             /* o  : output synthesis signal                                                 */
    3865             :     ISAR_SPLIT_REND_BITS_DATA *splitRendBits, /* o  : output split rendering bits                                    */
    3866             :     float **p_head_pose_buf,                  /* i  : PCM buffer with head-pose data                                          */
    3867             : #ifdef SUPPORT_JBM_TRACEFILE
    3868             :     JbmTraceFileWriterFn jbmWriterFn,
    3869             :     void *jbmWriter,
    3870             : #endif
    3871             :     bool *bitstreamReadDone,             /* o  : flag indicating that bitstream was read                                 */
    3872             :     uint16_t *nSamplesRendered,          /* o  : number of samples rendered                                              */
    3873             :     bool *parametersAvailableForEditing, /* o  : indicates whether objects editing is available                          */
    3874             :     const uint32_t systemTimestamp_ms    /* i  : current system timestamp                                                */
    3875             : )
    3876             : {
    3877             :     Decoder_Struct *st_ivas;
    3878             :     DECODER_CONFIG_HANDLE hDecoderConfig;
    3879             :     IVAS_DEC_VOIP *hVoIP;
    3880             :     uint32_t extBufferedTime_ms, scale, maxScaling;
    3881             :     JB4_DATAUNIT_HANDLE dataUnit;
    3882             :     uint16_t extBufferedSamples;
    3883             :     int16_t result;
    3884             :     ivas_error error;
    3885             :     uint8_t nOutChannels;
    3886             : 
    3887     6571632 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->hVoIP == NULL )
    3888             :     {
    3889           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3890             :     }
    3891             : 
    3892     6571632 :     st_ivas = hIvasDec->st_ivas;
    3893     6571632 :     hDecoderConfig = st_ivas->hDecoderConfig;
    3894     6571632 :     hVoIP = hIvasDec->hVoIP;
    3895     6571632 :     nOutChannels = (uint8_t) st_ivas->hDecoderConfig->nchan_out;
    3896     6571632 :     *parametersAvailableForEditing = false;
    3897             : 
    3898     6571632 :     if ( nSamplesPerChannel == 0 )
    3899             :     {
    3900           0 :         return IVAS_ERR_WRONG_PARAMS;
    3901             :     }
    3902             : 
    3903     6571632 :     if ( ( hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ||
    3904     6571632 :            hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ) &&
    3905             :          splitRendBits == NULL )
    3906             :     {
    3907           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    3908             :     }
    3909             : 
    3910             :     /* make sure that the FIFO after decoder/scaler contains at least one sound card frame (i.e. 20ms) */
    3911     9405886 :     while ( *nSamplesRendered < nSamplesPerChannel )
    3912             :     {
    3913     6683939 :         if ( hIvasDec->nSamplesAvailableNext == 0 )
    3914             :         {
    3915             :             int16_t nSamplesBuffered;
    3916     2554712 :             nSamplesBuffered = 0;
    3917     2554712 :             if ( hIvasDec->hasBeenFedFirstGoodFrame )
    3918             :             {
    3919             :                 /* check if the TC buffer already exists, otherweise nothing is buffered anyway */
    3920     2099357 :                 if ( st_ivas->hTcBuffer != NULL )
    3921             :                 {
    3922     2099357 :                     nSamplesBuffered = st_ivas->hTcBuffer->n_samples_buffered - st_ivas->hTcBuffer->n_samples_rendered;
    3923     2099357 :                     nSamplesBuffered += hVoIP->nSamplesRendered20ms;
    3924             :                 }
    3925             :             }
    3926             : 
    3927     2554712 :             extBufferedSamples = nSamplesBuffered;
    3928     2554712 :             extBufferedTime_ms = extBufferedSamples * 1000 / hDecoderConfig->output_Fs;
    3929     2554712 :             dataUnit = NULL;
    3930             : 
    3931             :             /* pop one access unit from the jitter buffer */
    3932     2554712 :             result = JB4_PopDataUnit( hVoIP->hJBM, systemTimestamp_ms, extBufferedTime_ms, &dataUnit, &scale, &maxScaling );
    3933     2554712 :             if ( result != 0 )
    3934             :             {
    3935           0 :                 return IVAS_ERR_UNKNOWN;
    3936             :             }
    3937     2554712 :             maxScaling = maxScaling * hDecoderConfig->output_Fs / 1000;
    3938             : 
    3939             : #ifdef DEBUG_MODE_JBM
    3940             :             dbgwrite( &extBufferedSamples, sizeof( uint16_t ), 1, 1, "./res/JBM_extBufferedSamples.dat" );
    3941             :             dbgwrite( &systemTimestamp_ms, sizeof( uint32_t ), 1, 1, "./res/JBM_systemTimestamp.dat" );
    3942             :             dbgwrite( &scale, sizeof( uint32_t ), 1, 1, "./res/JBM_scale.dat" );
    3943             :             dbgwrite( &maxScaling, sizeof( uint32_t ), 1, 1, "./res/JBM_maxScale.dat" );
    3944             : #endif
    3945             : 
    3946             :             /* avoid time scaling multiple times within one 20ms frame*/
    3947     2554712 :             if ( scale != 100U )
    3948             :             {
    3949       20254 :                 if ( hIvasDec->timeScalingDone )
    3950             :                 {
    3951           0 :                     scale = 100;
    3952             :                 }
    3953             :             }
    3954             : 
    3955             :             /* limit scale to range supported by time scaler */
    3956     2554712 :             if ( scale < APA_MIN_SCALE )
    3957             :             {
    3958           0 :                 scale = APA_MIN_SCALE;
    3959             :             }
    3960     2554712 :             else if ( scale > APA_MAX_SCALE )
    3961             :             {
    3962           0 :                 scale = APA_MAX_SCALE;
    3963             :             }
    3964             : 
    3965     2554712 :             if ( ( error = IVAS_DEC_VoIP_SetScale( hIvasDec, (int16_t) maxScaling, (int16_t) scale ) ) != IVAS_ERR_OK )
    3966             :             {
    3967           0 :                 return error;
    3968             :             }
    3969             : 
    3970             :             /* copy bitstream into decoder state */
    3971     2554712 :             if ( dataUnit )
    3972             :             {
    3973     1712115 :                 hIvasDec->hVoIP->hCurrentDataUnit = dataUnit;
    3974             : 
    3975     1712115 :                 bsCompactToSerial( dataUnit->data, hIvasDec->hVoIP->bs_conversion_buf, dataUnit->dataSize );
    3976             : 
    3977     1712115 :                 if ( ( error = IVAS_DEC_FeedFrame_Serial( hIvasDec, hIvasDec->hVoIP->bs_conversion_buf, dataUnit->dataSize, 0 ) ) != IVAS_ERR_OK )
    3978             :                 {
    3979           0 :                     return error;
    3980             :                 }
    3981             : 
    3982     1712115 :                 *bitstreamReadDone = true;
    3983             :             }
    3984      842597 :             else if ( hIvasDec->hasDecodedFirstGoodFrame )
    3985             :             {
    3986             :                 /* Decoder has been initialized with first good frame - do PLC */
    3987      428207 :                 if ( ( error = IVAS_DEC_FeedFrame_Serial( hIvasDec, hIvasDec->hVoIP->bs_conversion_buf, 0, 1 ) ) != IVAS_ERR_OK )
    3988             :                 {
    3989           0 :                     return error;
    3990             :                 }
    3991             :             }
    3992             : 
    3993             : #ifdef SUPPORT_JBM_TRACEFILE
    3994             :             /* jbmWriterFn and jbmWriter may be NULL if tracefile writing was not requested on CLI */
    3995     2554712 :             if ( jbmWriterFn != NULL && jbmWriter != NULL )
    3996             :             {
    3997             :                 /* write JBM trace data entry */
    3998     2335072 :                 store_JbmData( hVoIP, dataUnit, systemTimestamp_ms, extBufferedSamples, hDecoderConfig->output_Fs );
    3999     2335072 :                 if ( ( jbmWriterFn( &hVoIP->JbmTraceData, jbmWriter ) ) != IVAS_ERR_OK )
    4000             :                 {
    4001           0 :                     fprintf( stderr, "\nError writing JBM Trace data to file\n" );
    4002           0 :                     return IVAS_ERR_UNKNOWN;
    4003             :                 }
    4004             :             }
    4005             : #endif
    4006     2554712 :             if ( dataUnit )
    4007             :             {
    4008     1712115 :                 if ( dataUnit->partial_frame != 0 )
    4009             :                 {
    4010         184 :                     hVoIP->lastDecodedWasActive = 1;
    4011             :                 }
    4012             :                 else
    4013             :                 {
    4014     1711931 :                     hVoIP->lastDecodedWasActive = !dataUnit->silenceIndicator;
    4015             :                 }
    4016             : 
    4017             :                 /* data unit memory is no longer used */
    4018     1712115 :                 JB4_FreeDataUnit( hVoIP->hJBM, dataUnit );
    4019             :             }
    4020             : 
    4021     2554712 :             if ( hIvasDec->hasBeenFedFirstGoodFrame && *bitstreamReadDone == true )
    4022             :             {
    4023             :                 /* new bitstream was feeded, return for reconfiguration */
    4024     1712115 :                 return IVAS_ERR_OK;
    4025             :             }
    4026             : 
    4027      842597 :             if ( !hIvasDec->hasBeenFedFirstGoodFrame )
    4028             :             {
    4029      417142 :                 hIvasDec->nSamplesAvailableNext = hIvasDec->nSamplesFrame;
    4030             :             }
    4031             :         }
    4032             : 
    4033             :         /* decode */
    4034     4971824 :         if ( !hIvasDec->hasBeenFedFirstGoodFrame )
    4035             :         {
    4036             :             /* codec mode to use not known yet - simply output silence */
    4037             :             /* directly set output zero */
    4038      418678 :             int16_t nSamplesToZero = min( nSamplesPerChannel, hIvasDec->nSamplesAvailableNext );
    4039      418678 :             set_pcm_buffer_to_zero( pcm_buffer_offset( pcmBuf, pcmType, ( *nSamplesRendered ) * nOutChannels ), pcmType, nSamplesToZero * nOutChannels );
    4040      418678 :             *nSamplesRendered += nSamplesToZero;
    4041      418678 :             hIvasDec->nSamplesAvailableNext -= nSamplesToZero;
    4042      418678 :             update_voip_rendered20ms( hIvasDec, nSamplesToZero );
    4043             :         }
    4044             :         else
    4045             :         {
    4046             :             int16_t nSamplesToRender, nSamplesRendered_loop;
    4047             :             bool tmp;
    4048             : 
    4049             :             /* decode TCs, do TSM and prepare the renderer */
    4050     4553146 :             if ( !hIvasDec->isInitialized || hIvasDec->hasBeenFedFrame )
    4051             :             {
    4052     2137570 :                 if ( hIvasDec->nSamplesAvailableNext == 0 || hIvasDec->nSamplesAvailableNext == hIvasDec->nSamplesFrame )
    4053             :                 {
    4054     2137570 :                     if ( ( error = IVAS_DEC_GetSamplesDecoder( hIvasDec, splitRendBits ) ) != IVAS_ERR_OK )
    4055             :                     {
    4056     2137570 :                         return error;
    4057             :                     }
    4058             : 
    4059     2137570 :                     *bitstreamReadDone = false;
    4060     2137570 :                     *parametersAvailableForEditing = true;
    4061     2137570 :                     return IVAS_ERR_OK;
    4062             :                 }
    4063             :             }
    4064             : 
    4065     2415576 :             nSamplesToRender = nSamplesPerChannel - *nSamplesRendered;
    4066             : 
    4067             :             /* check if we still need to prepare the renderer */
    4068     2415576 :             if ( hIvasDec->hasBeenPreparedRendering == false )
    4069             :             {
    4070     2137570 :                 if ( ( error = IVAS_DEC_PrepareRenderer( hIvasDec ) ) != IVAS_ERR_OK )
    4071             :                 {
    4072           0 :                     return error;
    4073             :                 }
    4074             :             }
    4075             : 
    4076     2415576 :             if ( splitRendBits != NULL )
    4077             :             {
    4078             :                 /* Render head poses from time-scaled transport channels */
    4079      200204 :                 if ( ( error = isar_render_poses( hIvasDec, nSamplesToRender, &nSamplesRendered_loop, &tmp ) ) != IVAS_ERR_OK )
    4080             :                 {
    4081           0 :                     return error;
    4082             :                 }
    4083             :             }
    4084             :             else
    4085             :             {
    4086             :                 /* render IVAS frames directly to the output buffer */
    4087     2215372 :                 if ( ( error = IVAS_DEC_GetSamplesRenderer( hIvasDec, nSamplesToRender, pcmType, pcm_buffer_offset( pcmBuf, pcmType, *nSamplesRendered * nOutChannels ), &nSamplesRendered_loop, &tmp ) ) != IVAS_ERR_OK )
    4088             :                 {
    4089           0 :                     return error;
    4090             :                 }
    4091             :             }
    4092             : 
    4093     2415576 :             *nSamplesRendered += nSamplesRendered_loop;
    4094     2415576 :             update_voip_rendered20ms( hIvasDec, nSamplesRendered_loop );
    4095             :         }
    4096             :     }
    4097             : 
    4098     2721947 :     if ( hIvasDec->hasDecodedFirstGoodFrame && splitRendBits != NULL )
    4099             :     {
    4100             :         /* Analyse head poses over entire frame, generate ISAR metadata and maybe encode if split coded */
    4101      154573 :         if ( ( error = isar_generate_metadata_and_bitstream( st_ivas, p_head_pose_buf, *nSamplesRendered, splitRendBits ) ) != IVAS_ERR_OK )
    4102             :         {
    4103           0 :             return error;
    4104             :         }
    4105             : 
    4106             :         /* Synthesise PCM output if split PCM */
    4107      154573 :         if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    4108             :         {
    4109        8487 :             if ( st_ivas->hDecoderConfig->render_framesize == IVAS_RENDER_FRAMESIZE_5MS )
    4110             :             {
    4111             : #ifndef DISABLE_LIMITER
    4112           0 :                 ivas_limiter_dec( st_ivas->hLimiter, p_head_pose_buf, st_ivas->hDecoderConfig->nchan_out, *nSamplesRendered, st_ivas->BER_detect );
    4113             : #endif
    4114             :             }
    4115             :             else
    4116             :             {
    4117        8487 :                 ivas_limiter_dec( st_ivas->hLimiter, p_head_pose_buf, st_ivas->hDecoderConfig->nchan_out, *nSamplesRendered, st_ivas->BER_detect );
    4118             :             }
    4119             : 
    4120             : #ifdef DEBUGGING
    4121             :             st_ivas->noClipping +=
    4122             : #endif
    4123        8487 :                 ivas_syn_output( p_head_pose_buf, *nSamplesRendered, st_ivas->hDecoderConfig->nchan_out, (int16_t *) pcmBuf );
    4124             :         }
    4125             :     }
    4126             : 
    4127     2721947 :     return IVAS_ERR_OK;
    4128             : }
    4129             : 
    4130             : 
    4131             : /*---------------------------------------------------------------------*
    4132             :  * IVAS_DEC_VoIP_GetSamples( )
    4133             :  *
    4134             :  * Main function to decode one frame in VoIP
    4135             :  *---------------------------------------------------------------------*/
    4136             : 
    4137     6123550 : ivas_error IVAS_DEC_VoIP_GetSamples(
    4138             :     IVAS_DEC_HANDLE hIvasDec,        /* i/o: IVAS decoder handle                                                     */
    4139             :     uint16_t nSamplesPerChannel,     /* i  : number of samples per channel requested to be written to output buffer  */
    4140             :     const IVAS_DEC_PCM_TYPE pcmType, /* i  : type for the decoded PCM resolution                                     */
    4141             :     void *pcmBuf,                    /* o  : output synthesis signal                                                 */
    4142             : #ifdef SUPPORT_JBM_TRACEFILE
    4143             :     JbmTraceFileWriterFn jbmWriterFn,
    4144             :     void *jbmWriter,
    4145             : #endif
    4146             :     bool *bitstreamReadDone,             /* o  : flag indicating that bitstream was read                                 */
    4147             :     uint16_t *nSamplesRendered,          /* o  : number of samples rendered                                              */
    4148             :     bool *parametersAvailableForEditing, /* o  : indicates whether objects editing is available                          */
    4149             :     const uint32_t systemTimestamp_ms    /* i  : current system timestamp                                                */
    4150             : )
    4151             : {
    4152     6123550 :     return ivas_dec_voip_get_samples_common(
    4153             :         hIvasDec,
    4154             :         nSamplesPerChannel,
    4155             :         pcmType,
    4156             :         pcmBuf,
    4157             :         NULL,
    4158             :         NULL,
    4159             : #ifdef SUPPORT_JBM_TRACEFILE
    4160             :         jbmWriterFn,
    4161             :         jbmWriter,
    4162             : #endif
    4163             :         bitstreamReadDone,
    4164             :         nSamplesRendered,
    4165             :         parametersAvailableForEditing,
    4166             :         systemTimestamp_ms );
    4167             : }
    4168             : 
    4169             : 
    4170             : /*---------------------------------------------------------------------*
    4171             :  * IVAS_DEC_VoIP_GetSplitBinauralBitstream( )
    4172             :  *
    4173             :  * Main function to decode one split-rendering frame in VoIP
    4174             :  *---------------------------------------------------------------------*/
    4175             : 
    4176             : /*! r: error code */
    4177      448082 : ivas_error IVAS_DEC_VoIP_GetSplitBinauralBitstream(
    4178             :     IVAS_DEC_HANDLE hIvasDec,                 /* i/o: IVAS decoder handle                                                     */
    4179             :     /* const IVAS_DEC_PCM_TYPE pcmType, */    /* i  : type for the decoded PCM resolution                                     */
    4180             :     void *pcmBuf,                             /* o  : output synthesis signal                                                 */
    4181             :     ISAR_SPLIT_REND_BITS_DATA *splitRendBits, /* o  : output split rendering bits                                             */
    4182             : #ifdef SUPPORT_JBM_TRACEFILE
    4183             :     JbmTraceFileWriterFn jbmWriterFn,
    4184             :     void *jbmWriter,
    4185             : #endif
    4186             :     bool *bitstreamReadDone,             /* o  : flag indicating that bitstream was read                                 */
    4187             :     uint16_t *nSamplesRendered,          /* o  : number of samples rendered                                              */
    4188             :     bool *parametersAvailableForEditing, /* o  : indicates whether objects editing is available                          */
    4189             :     const uint32_t systemTimestamp_ms    /* i  : current system timestamp                                                */
    4190             : )
    4191             : {
    4192             :     int16_t i;
    4193             :     float head_pose_buf[BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES][L_FRAME48k];
    4194             :     float *pp_head_pose_buf[BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES];
    4195      448082 :     ivas_error error = IVAS_ERR_UNKNOWN;
    4196      448082 :     int16_t nSamplesPerChannel = 0;
    4197             : 
    4198      448082 :     if ( ( error = IVAS_DEC_GetRenderFramesizeSamples( hIvasDec, &nSamplesPerChannel ) ) != IVAS_ERR_OK )
    4199             :     {
    4200           0 :         return error;
    4201             :     }
    4202             : 
    4203             :     /* Set pointers to beginning of head pose buffers */
    4204     7617394 :     for ( i = 0; i < BINAURAL_CHANNELS * MAX_HEAD_ROT_POSES; ++i )
    4205             :     {
    4206     7169312 :         pp_head_pose_buf[i] = head_pose_buf[i];
    4207             :     }
    4208             : 
    4209      448082 :     return ivas_dec_voip_get_samples_common(
    4210             :         hIvasDec,
    4211             :         nSamplesPerChannel,
    4212             :         IVAS_DEC_PCM_INT16,
    4213             :         pcmBuf,
    4214             :         splitRendBits,
    4215             :         pp_head_pose_buf,
    4216             : #ifdef SUPPORT_JBM_TRACEFILE
    4217             :         jbmWriterFn,
    4218             :         jbmWriter,
    4219             : #endif
    4220             :         bitstreamReadDone,
    4221             :         nSamplesRendered,
    4222             :         parametersAvailableForEditing,
    4223             :         systemTimestamp_ms );
    4224             : }
    4225             : 
    4226             : 
    4227             : /*---------------------------------------------------------------------*
    4228             :  * update_voip_rendered20ms( )
    4229             :  *
    4230             :  * Update the number of samples that have been rendered since the last 20ms render border
    4231             :  *---------------------------------------------------------------------*/
    4232             : 
    4233     2834254 : static void update_voip_rendered20ms(
    4234             :     IVAS_DEC_HANDLE hIvasDec,
    4235             :     const int16_t nSamplesRendered )
    4236             : {
    4237             :     int16_t nSamplesRenderedTotal;
    4238             : 
    4239     2834254 :     nSamplesRenderedTotal = hIvasDec->hVoIP->nSamplesRendered20ms + nSamplesRendered;
    4240             : 
    4241             :     /* we have crossed a 20ms border, reset the time scaling done flag */
    4242     2834254 :     if ( nSamplesRenderedTotal >= hIvasDec->nSamplesFrame )
    4243             :     {
    4244     2555524 :         hIvasDec->timeScalingDone = 0;
    4245             :     }
    4246             : 
    4247     2834254 :     hIvasDec->hVoIP->nSamplesRendered20ms = nSamplesRenderedTotal % hIvasDec->nSamplesFrame;
    4248             : 
    4249     2834254 :     return;
    4250             : }
    4251             : 
    4252             : 
    4253             : /*---------------------------------------------------------------------*
    4254             :  * IVAS_DEC_VoIP_Flush( )
    4255             :  *
    4256             :  * Function to flush remaining audio samples in VoIP
    4257             :  *---------------------------------------------------------------------*/
    4258             : 
    4259       38213 : ivas_error IVAS_DEC_Flush(
    4260             :     IVAS_DEC_HANDLE hIvasDec,         /* i/o: IVAS decoder handle                                                           */
    4261             :     const int16_t nSamplesPerChannel, /* i  : number of samples per channel requested to be written to output buffer        */
    4262             :     const IVAS_DEC_PCM_TYPE pcmType,  /* i  : type for the decoded PCM resolution                                           */
    4263             :     void *pcmBuf,                     /* o  : output synthesis signal                                                       */
    4264             :     int16_t *nSamplesFlushed          /* o  : number of samples flushed                                                     */
    4265             : )
    4266             : {
    4267             :     ivas_error error;
    4268             :     uint16_t nSamplesToRender;
    4269             :     uint16_t nSamplesFlushedLocal;
    4270             : 
    4271       38213 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    4272             :     {
    4273           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4274             :     }
    4275             : 
    4276             : #ifdef TMP_1342_WORKAROUND_DEC_FLUSH_BROKEN_IN_SR
    4277       38213 :     hIvasDec->st_ivas->flushing = 1;
    4278             : #endif
    4279             : 
    4280       38213 :     *nSamplesFlushed = min( nSamplesPerChannel, hIvasDec->nSamplesAvailableNext );
    4281             : 
    4282       38213 :     nSamplesToRender = (uint16_t) *nSamplesFlushed;
    4283             : 
    4284             :     /* render IVAS frames  */
    4285       38213 :     error = IVAS_ERR_OK;
    4286       38213 :     if ( nSamplesToRender > 0 && hIvasDec->st_ivas->ivas_format != MONO_FORMAT )
    4287             :     {
    4288         573 :         error = ivas_jbm_dec_render( hIvasDec->st_ivas, nSamplesToRender, &nSamplesFlushedLocal, &hIvasDec->nSamplesAvailableNext, pcm_type_API_to_internal( pcmType ), pcmBuf );
    4289             :     }
    4290             :     else
    4291             :     {
    4292       37640 :         *nSamplesFlushed = 0;
    4293             :     }
    4294             : 
    4295             : #ifdef TMP_1342_WORKAROUND_DEC_FLUSH_BROKEN_IN_SR
    4296       38213 :     hIvasDec->st_ivas->flushing = 0;
    4297             : #endif
    4298             : 
    4299       38213 :     return error;
    4300             : }
    4301             : 
    4302             : 
    4303             : /*---------------------------------------------------------------------*
    4304             :  * IVAS_DEC_isRestartNeeded( )
    4305             :  *
    4306             :  *
    4307             :  *---------------------------------------------------------------------*/
    4308             : 
    4309     1712115 : ivas_error IVAS_DEC_isRestartNeeded(
    4310             :     IVAS_DEC_HANDLE hIvasDec, /* i  : IVAS decoder handle            */
    4311             :     bool *restartNeeded       /* o  : flag to signal decoder restart */
    4312             : 
    4313             : )
    4314             : {
    4315     1712115 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    4316             :     {
    4317           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4318             :     }
    4319             : 
    4320     1712115 :     *restartNeeded = hIvasDec->st_ivas->restartNeeded > 0;
    4321             : 
    4322     1712115 :     return IVAS_ERR_OK;
    4323             : }
    4324             : 
    4325             : 
    4326             : /*---------------------------------------------------------------------*
    4327             :  * IVAS_DEC_VoIP_IsEmpty( )
    4328             :  *
    4329             :  * Returns 'true' if decoder has no data in VoIP jitter buffer
    4330             :  *---------------------------------------------------------------------*/
    4331             : 
    4332     2760160 : ivas_error IVAS_DEC_VoIP_IsEmpty(
    4333             :     IVAS_DEC_HANDLE hIvasDec,    /* i  : IVAS decoder handle             */
    4334             :     const int16_t nSamplesAsked, /* i  : number of output samples asked  */
    4335             :     bool *isEmpty                /* o  : isEmpty flag                    */
    4336             : )
    4337             : {
    4338     2760160 :     if ( hIvasDec == NULL || hIvasDec->hVoIP == NULL )
    4339             :     {
    4340           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4341             :     }
    4342             : 
    4343     2760160 :     *isEmpty = ( JB4_bufferedDataUnits( hIvasDec->hVoIP->hJBM ) == 0 ) && ( hIvasDec->nSamplesAvailableNext < nSamplesAsked );
    4344             : 
    4345     2760160 :     return IVAS_ERR_OK;
    4346             : }
    4347             : 
    4348             : 
    4349             : /*---------------------------------------------------------------------*
    4350             :  * IVAS_DEC_VoIP_Get_CA_offset( )
    4351             :  *
    4352             :  *
    4353             :  *---------------------------------------------------------------------*/
    4354             : 
    4355           0 : ivas_error IVAS_DEC_VoIP_Get_CA_offset(
    4356             :     IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle */
    4357             :     int16_t *optimum_offset,
    4358             :     int16_t *FEC_hi )
    4359             : {
    4360           0 :     if ( hIvasDec == NULL || hIvasDec->hVoIP == NULL || hIvasDec->hVoIP->hJBM == NULL )
    4361             :     {
    4362           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4363             :     }
    4364             : 
    4365           0 :     *optimum_offset = JB4_getFECoffset( hIvasDec->hVoIP->hJBM );
    4366           0 :     *FEC_hi = JB4_FECoffset( hIvasDec->hVoIP->hJBM );
    4367             : 
    4368           0 :     return IVAS_ERR_OK;
    4369             : }
    4370             : 
    4371             : 
    4372             : /*---------------------------------------------------------------------*
    4373             :  * ivas_destroy_handle_VoIP( )
    4374             :  *
    4375             :  * Deallocate VoIP handle
    4376             :  *---------------------------------------------------------------------*/
    4377             : 
    4378       38234 : static void ivas_destroy_handle_VoIP(
    4379             :     IVAS_DEC_VOIP *hVoIP /* i/o: VoIP decoder handle */
    4380             : )
    4381             : {
    4382       38234 :     JB4_Destroy( &hVoIP->hJBM );
    4383             : 
    4384       38234 :     if ( hVoIP->bs_conversion_buf != NULL )
    4385             :     {
    4386             : #define WMC_TOOL_SKIP
    4387             :         /* Bitstream conversion is not counted towards complexity and memory usage */
    4388       38234 :         free( hVoIP->bs_conversion_buf );
    4389             : #undef WMC_TOOL_SKIP
    4390             :     }
    4391             : 
    4392       38234 :     free( hVoIP );
    4393             : 
    4394       38234 :     return;
    4395             : }
    4396             : 
    4397             : 
    4398             : #ifdef SUPPORT_JBM_TRACEFILE
    4399             : /*---------------------------------------------------------------------*
    4400             :  * store_JbmData()
    4401             :  *
    4402             :  * Store JBM trace data entry
    4403             :  *---------------------------------------------------------------------*/
    4404             : 
    4405     2335072 : static void store_JbmData(
    4406             :     IVAS_DEC_VOIP *hVoIP,
    4407             :     JB4_DATAUNIT_HANDLE dataUnit,
    4408             :     const uint32_t systemTimestamp_ms,
    4409             :     const uint16_t extBufferedSamples,
    4410             :     const int32_t output_Fs )
    4411             : {
    4412             :     IVAS_JBM_TRACE_DATA *JbmTraceData;
    4413             : 
    4414     2335072 :     if ( hVoIP == NULL )
    4415             :     {
    4416           0 :         return;
    4417             :     }
    4418             : 
    4419     2335072 :     JbmTraceData = &hVoIP->JbmTraceData;
    4420             : 
    4421     2335072 :     JbmTraceData->systemTimestamp_ms = systemTimestamp_ms;
    4422     2335072 :     JbmTraceData->extBufferedSamples = extBufferedSamples;
    4423     2335072 :     JbmTraceData->lastDecodedWasActive = hVoIP->lastDecodedWasActive;
    4424     2335072 :     JbmTraceData->output_Fs = output_Fs;
    4425     2335072 :     JbmTraceData->dataUnit_flag = dataUnit != NULL;
    4426     2335072 :     if ( dataUnit != NULL )
    4427             :     {
    4428     1512146 :         JbmTraceData->sequenceNumber = dataUnit->sequenceNumber;
    4429     1512146 :         JbmTraceData->timeStamp = dataUnit->timeStamp;
    4430     1512146 :         JbmTraceData->rcvTime = dataUnit->rcvTime;
    4431     1512146 :         JbmTraceData->partial_frame = dataUnit->partial_frame;
    4432     1512146 :         JbmTraceData->partialCopyOffset = dataUnit->partialCopyOffset;
    4433             :     }
    4434             : 
    4435     2335072 :     return;
    4436             : }
    4437             : 
    4438             : 
    4439             : /*---------------------------------------------------------------------*
    4440             :  * IVAS_DEC_GetJbmData()
    4441             :  *
    4442             :  *
    4443             :  *---------------------------------------------------------------------*/
    4444             : 
    4445           0 : ivas_error IVAS_DEC_GetJbmData(
    4446             :     IVAS_DEC_HANDLE hIvasDec,         /* i/o: IVAS decoder handle */
    4447             :     IVAS_JBM_TRACE_DATA *JbmTraceData /* o  : JBM Trace data      */
    4448             : 
    4449             : )
    4450             : {
    4451           0 :     if ( hIvasDec == NULL || hIvasDec->hVoIP == NULL || JbmTraceData == NULL )
    4452             :     {
    4453           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4454             :     }
    4455             : 
    4456           0 :     *JbmTraceData = hIvasDec->hVoIP->JbmTraceData;
    4457             : 
    4458           0 :     return IVAS_ERR_OK;
    4459             : }
    4460             : 
    4461             : #endif
    4462             : 
    4463             : 
    4464             : /*---------------------------------------------------------------------*
    4465             :  * IVAS_DEC_GetErrorMessage( )
    4466             :  *
    4467             :  * Maps error codes to error description strings
    4468             :  *---------------------------------------------------------------------*/
    4469             : 
    4470           0 : const char *IVAS_DEC_GetErrorMessage(
    4471             :     ivas_error error /* i  : decoder error code enum */
    4472             : )
    4473             : {
    4474           0 :     return ivas_error_to_string( error );
    4475             : }
    4476             : 
    4477             : 
    4478             : /*---------------------------------------------------------------------*
    4479             :  * printConfigInfo_dec( )
    4480             :  *
    4481             :  *
    4482             :  *---------------------------------------------------------------------*/
    4483             : 
    4484       86054 : static ivas_error printConfigInfo_dec(
    4485             :     Decoder_Struct *st_ivas,
    4486             :     const int16_t bitstreamformat,
    4487             :     const bool Opt_VOIP,
    4488             :     const bool quietModeEnabled )
    4489             : {
    4490             :     ivas_error error;
    4491             :     char config_str[50];
    4492             :     AUDIO_CONFIG output_config;
    4493             : 
    4494             :     /*-----------------------------------------------------------------*
    4495             :      * Print info on screen
    4496             :      *-----------------------------------------------------------------*/
    4497             : 
    4498       86054 :     fprintf( stdout, "\n" );
    4499             : 
    4500             :     /*-----------------------------------------------------------------*
    4501             :      * Print output sampling frequency
    4502             :      *-----------------------------------------------------------------*/
    4503             : 
    4504       86054 :     fprintf( stdout, "Output sampling rate:   %d Hz\n", st_ivas->hDecoderConfig->output_Fs );
    4505             : 
    4506             :     /*-----------------------------------------------------------------*
    4507             :      * Print bitrate
    4508             :      *-----------------------------------------------------------------*/
    4509             : 
    4510       86054 :     if ( !quietModeEnabled )
    4511             :     {
    4512           0 :         if ( !Opt_VOIP )
    4513             :         {
    4514           0 :             fprintf( stdout, "Bitrate:                %.2f kbps\n", (float) st_ivas->hDecoderConfig->ivas_total_brate / 1000 );
    4515             : 
    4516           0 :             if ( st_ivas->hDecoderConfig->ivas_total_brate <= 0 )
    4517             :             {
    4518           0 :                 if ( bitstreamformat == G192 )
    4519             :                 {
    4520           0 :                     fprintf( stdout, "Active Bitrate not identified in bitstream file \n" );
    4521             :                 }
    4522             :                 else /* MIME */
    4523             :                 {
    4524           0 :                     fprintf( stdout, "Active Bitrate not identified from first MIME frame \n" );
    4525             :                 }
    4526             :             }
    4527             :         }
    4528             :     }
    4529             : 
    4530             :     /*-----------------------------------------------------------------*
    4531             :      * Print output configuration
    4532             :      *-----------------------------------------------------------------*/
    4533             : 
    4534       86054 :     if ( st_ivas->ivas_format == MONO_FORMAT )
    4535             :     {
    4536         495 :         if ( st_ivas->hDecoderConfig->Opt_non_diegetic_pan )
    4537             :         {
    4538           3 :             fprintf( stdout, "Output configuration:   mono EVS bit-exact decoding to stereo\n" );
    4539           3 :             fprintf( stdout, "Non-diegetic panning:   %.2f\n", st_ivas->hDecoderConfig->non_diegetic_pan_gain * 90.f );
    4540             :         }
    4541             :         else
    4542             :         {
    4543         492 :             output_config = st_ivas->hDecoderConfig->output_config;
    4544         492 :             if ( output_config != IVAS_AUDIO_CONFIG_MONO )
    4545             :             {
    4546           0 :                 get_channel_config( output_config, &config_str[0] );
    4547           0 :                 fprintf( stdout, "Output configuration:   mono EVS bit-exact decoding rendering to %s\n", config_str );
    4548             :             }
    4549             :             else
    4550             :             {
    4551         492 :                 fprintf( stdout, "Output configuration:   mono EVS bit-exact decoding\n" );
    4552             :             }
    4553             :         }
    4554             :     }
    4555             :     else
    4556             :     {
    4557       85559 :         if ( !quietModeEnabled )
    4558             :         {
    4559           0 :             if ( st_ivas->ivas_format == STEREO_FORMAT )
    4560             :             {
    4561           0 :                 fprintf( stdout, "Input configuration:    Stereo\n" );
    4562             :             }
    4563           0 :             else if ( st_ivas->ivas_format == ISM_FORMAT )
    4564             :             {
    4565           0 :                 if ( st_ivas->ism_mode == ISM_MODE_PARAM )
    4566             :                 {
    4567           0 :                     fprintf( stdout, "Input configuration:    ISM (ParamISM): 2 transport channels\n" );
    4568             :                 }
    4569             :                 else
    4570             :                 {
    4571           0 :                     fprintf( stdout, "Input configuration:    ISM: %d transport channel(s)\n", st_ivas->nchan_transport );
    4572             :                 }
    4573             :             }
    4574           0 :             else if ( st_ivas->ivas_format == SBA_FORMAT )
    4575             :             {
    4576           0 :                 fprintf( stdout, "Input configuration:    Scene Based Audio, Ambisonic order %i%s, %d transport channel(s)\n", st_ivas->sba_order, st_ivas->sba_planar ? " (Planar)" : "", st_ivas->nchan_transport );
    4577             :             }
    4578           0 :             else if ( st_ivas->ivas_format == SBA_ISM_FORMAT )
    4579             :             {
    4580           0 :                 fprintf( stdout, "Input configuration:    Combined Scene Based Audio, Ambisonic order %i, with %d Objects \n", st_ivas->sba_order, st_ivas->nchan_ism );
    4581             :             }
    4582           0 :             else if ( st_ivas->ivas_format == MASA_FORMAT )
    4583             :             {
    4584           0 :                 fprintf( stdout, "Input configuration:    MASA - %d channel(s)\n", st_ivas->nchan_transport );
    4585             :             }
    4586           0 :             else if ( st_ivas->ivas_format == MC_FORMAT )
    4587             :             {
    4588           0 :                 if ( ( error = get_channel_config( st_ivas->transport_config, &config_str[0] ) ) != IVAS_ERR_OK )
    4589             :                 {
    4590           0 :                     return error;
    4591             :                 }
    4592             : 
    4593           0 :                 fprintf( stdout, "Input configuration:    %s\n", config_str );
    4594             :             }
    4595           0 :             else if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    4596             :             {
    4597           0 :                 fprintf( stdout, "Input configuration:    combined ISM and MASA (%i ISM stream(s)) \n", st_ivas->nchan_ism );
    4598             :             }
    4599             :         }
    4600             : 
    4601       85559 :         output_config = st_ivas->hDecoderConfig->output_config;
    4602       85559 :         get_channel_config( output_config, &config_str[0] );
    4603       85559 :         fprintf( stdout, "Output configuration:   %s\n", config_str );
    4604             : 
    4605       85559 :         if ( output_config == IVAS_AUDIO_CONFIG_BINAURAL || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_IR || output_config == IVAS_AUDIO_CONFIG_BINAURAL_ROOM_REVERB || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
    4606             :         {
    4607       25973 :             fprintf( stdout, "Render framesize:       %dms\n", get_render_frame_size_ms( st_ivas->hDecoderConfig->render_framesize ) );
    4608             :         }
    4609       85559 :         if ( st_ivas->hDecoderConfig->Opt_HRTF_binary )
    4610             :         {
    4611        7800 :             fprintf( stdout, "HRIR/BRIR file:         ON\n" );
    4612             :         }
    4613             : 
    4614       85559 :         if ( st_ivas->hDecoderConfig->Opt_RendConfigCustom )
    4615             :         {
    4616        2176 :             fprintf( stdout, "Renderer config. file:  ON\n" );
    4617             :         }
    4618             : 
    4619       85559 :         if ( st_ivas->hDecoderConfig->Opt_Headrotation )
    4620             :         {
    4621        2345 :             fprintf( stdout, "Head rotation:          ON\n" );
    4622             :         }
    4623             : 
    4624       85559 :         if ( st_ivas->hDecoderConfig->Opt_ExternalOrientation )
    4625             :         {
    4626          93 :             fprintf( stdout, "External orientation:   ON\n" );
    4627             :         }
    4628             : 
    4629       85559 :         if ( st_ivas->hDecoderConfig->orientation_tracking != IVAS_HEAD_ORIENT_TRK_NONE )
    4630             :         {
    4631          48 :             switch ( st_ivas->hDecoderConfig->orientation_tracking )
    4632             :             {
    4633          36 :                 case IVAS_HEAD_ORIENT_TRK_AVG:
    4634          36 :                     fprintf( stdout, "Orientation tracking:   AVG\n" );
    4635          36 :                     break;
    4636           0 :                 case IVAS_HEAD_ORIENT_TRK_REF:
    4637           0 :                     fprintf( stdout, "Orientation tracking:   REF\n" );
    4638           0 :                     break;
    4639           6 :                 case IVAS_HEAD_ORIENT_TRK_REF_VEC:
    4640           6 :                     fprintf( stdout, "Orientation tracking:   REF_VEC\n" );
    4641           6 :                     break;
    4642           6 :                 case IVAS_HEAD_ORIENT_TRK_REF_VEC_LEV:
    4643           6 :                     fprintf( stdout, "Orientation tracking:   REF_VEC_LEV\n" );
    4644           6 :                     break;
    4645           0 :                 default:
    4646           0 :                     break;
    4647             :             }
    4648       85511 :         }
    4649             : 
    4650       85559 :         if ( st_ivas->hDecoderConfig->Opt_non_diegetic_pan )
    4651             :         {
    4652           3 :             fprintf( stdout, "Non-diegetic panning:   %.2f\n", st_ivas->hDecoderConfig->non_diegetic_pan_gain * 90.f );
    4653             :         }
    4654             : 
    4655       85559 :         if ( st_ivas->hDecoderConfig->Opt_dpid_on )
    4656             :         {
    4657           6 :             fprintf( stdout, "Directivity pattern:    ON\n" );
    4658             :         }
    4659             : 
    4660       85559 :         if ( st_ivas->hDecoderConfig->Opt_aeid_on )
    4661             :         {
    4662          12 :             fprintf( stdout, "Acoustic environment ID:ON\n" );
    4663             :         }
    4664             : 
    4665       85559 :         if ( st_ivas->hDecoderConfig->Opt_ObjEdit_on )
    4666             :         {
    4667          39 :             fprintf( stdout, "Objects editing :       ON\n" );
    4668             :         }
    4669             :     }
    4670             : 
    4671             :     /*-----------------------------------------------------------------*
    4672             :      * Print TSM mode info
    4673             :      *-----------------------------------------------------------------*/
    4674             : 
    4675       86054 :     if ( st_ivas->hDecoderConfig->Opt_tsm )
    4676             :     {
    4677       38234 :         fprintf( stdout, "TSM mode:               ON\n" );
    4678             :     }
    4679             : 
    4680       86054 :     return IVAS_ERR_OK;
    4681             : }
    4682             : 
    4683             : 
    4684             : /*---------------------------------------------------------------------*
    4685             :  * IVAS_DEC_PrintConfig( )
    4686             :  *
    4687             :  * Print decoder set-up info
    4688             :  *---------------------------------------------------------------------*/
    4689             : 
    4690       86054 : ivas_error IVAS_DEC_PrintConfig(
    4691             :     const IVAS_DEC_HANDLE hIvasDec,
    4692             :     const bool quietModeEnabled,
    4693             :     const bool voipMode )
    4694             : {
    4695       86054 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    4696             :     {
    4697           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4698             :     }
    4699             : 
    4700       86054 :     printConfigInfo_dec( hIvasDec->st_ivas, hIvasDec->bitstreamformat, voipMode, quietModeEnabled );
    4701             : 
    4702       86054 :     return IVAS_ERR_OK;
    4703             : }
    4704             : 
    4705             : 
    4706             : #ifdef DEBUGGING
    4707             : /*---------------------------------------------------------------------*
    4708             :  * IVAS_DEC_PrintConfigWithBitstream( )
    4709             :  *
    4710             :  *
    4711             :  *---------------------------------------------------------------------*/
    4712             : #define WMC_TOOL_SKIP
    4713             : void IVAS_DEC_PrintConfigWithBitstream(
    4714             :     IVAS_DEC_HANDLE hIvasDec,
    4715             :     const bool quietModeEnabled,
    4716             :     uint16_t bit_stream[],
    4717             :     const int16_t num_bits )
    4718             : {
    4719             :     Decoder_Struct *st_ivas;
    4720             : 
    4721             :     /* Create a copy of decoder struct that will be modified by preview_indices(),
    4722             :      * leaving the original decoder struct unchanged. The additional memory used here
    4723             :      * should not be counted towards memory footprint of the decoder. */
    4724             :     st_ivas = malloc( sizeof( Decoder_Struct ) );
    4725             :     memcpy( st_ivas, hIvasDec->st_ivas, sizeof( Decoder_Struct ) );
    4726             : 
    4727             :     preview_indices( st_ivas, bit_stream, num_bits );
    4728             : 
    4729             :     /* Print config from modified decoder struct */
    4730             :     printConfigInfo_dec( st_ivas, hIvasDec->bitstreamformat, hIvasDec->Opt_VOIP, quietModeEnabled );
    4731             : 
    4732             :     free( st_ivas );
    4733             : 
    4734             :     return;
    4735             : }
    4736             : 
    4737             : 
    4738             : /*---------------------------------------------------------------------*
    4739             :  * IVAS_DEC_PrintConfigWithVoipBitstream( )
    4740             :  *
    4741             :  *
    4742             :  *---------------------------------------------------------------------*/
    4743             : 
    4744             : void IVAS_DEC_PrintConfigWithVoipBitstream(
    4745             :     IVAS_DEC_HANDLE hIvasDec,
    4746             :     const bool quietModeEnabled,
    4747             :     uint8_t *au,
    4748             :     const uint16_t auSizeBits )
    4749             : {
    4750             :     Decoder_Struct *st_ivas;
    4751             :     uint16_t bit_stream[MAX_BITS_PER_FRAME + 4 * 8];
    4752             : 
    4753             :     /* Create a copy of decoder struct that will be modified by preview_indices(),
    4754             :      * leaving the original decoder struct unchanged. The additional memory used here
    4755             :      * should not be counted towards memory footprint of the decoder. */
    4756             :     st_ivas = malloc( sizeof( Decoder_Struct ) );
    4757             :     memcpy( st_ivas, hIvasDec->st_ivas, sizeof( Decoder_Struct ) );
    4758             : 
    4759             :     bsCompactToSerial( au, bit_stream, auSizeBits );
    4760             :     preview_indices( st_ivas, bit_stream, auSizeBits );
    4761             : 
    4762             :     /* Print config from modified decoder struct */
    4763             :     printConfigInfo_dec( st_ivas, hIvasDec->bitstreamformat, hIvasDec->Opt_VOIP, quietModeEnabled );
    4764             : 
    4765             :     free( st_ivas );
    4766             : 
    4767             :     return;
    4768             : }
    4769             : #undef WMC_TOOL_SKIP
    4770             : #endif
    4771             : 
    4772             : 
    4773             : /*---------------------------------------------------------------------*
    4774             :  * IVAS_DEC_PrintDisclaimer( )
    4775             :  *
    4776             :  * Print IVAS disclaimer to console
    4777             :  *---------------------------------------------------------------------*/
    4778             : 
    4779       86033 : void IVAS_DEC_PrintDisclaimer( void )
    4780             : {
    4781       86033 :     print_disclaimer( stderr );
    4782             : 
    4783       86033 :     return;
    4784             : }
    4785             : 
    4786             : 
    4787             : /*---------------------------------------------------------------------*
    4788             :  * evs_dec_main( )
    4789             :  *
    4790             :  * EVS codec main decoder fucntion
    4791             :  *---------------------------------------------------------------------*/
    4792             : 
    4793       39210 : static ivas_error evs_dec_main(
    4794             :     Decoder_Struct *st_ivas /* i  : IVAS decoder structure      */
    4795             : )
    4796             : {
    4797             :     DEC_CORE_HANDLE *hCoreCoder;
    4798             : #ifndef FIX_1435_MOVE_STEREO_PANNING
    4799             :     float mixer_left, mixer_rigth;
    4800             :     float *p_output[MAX_TRANSPORT_CHANNELS];
    4801             :     int16_t ch, nOutSamples;
    4802             : #else
    4803             :     float *p_output[1];
    4804             :     int16_t nOutSamples;
    4805             : #endif
    4806             :     ivas_error error;
    4807             : 
    4808       39210 :     hCoreCoder = st_ivas->hSCE[0]->hCoreCoder;
    4809       39210 :     hCoreCoder[0]->total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
    4810       39210 :     nOutSamples = (int16_t) ( st_ivas->hDecoderConfig->output_Fs / FRAMES_PER_SEC );
    4811             : 
    4812       39210 :     mdct_switching_dec( hCoreCoder[0] );
    4813             : 
    4814             : #ifdef FIX_1435_MOVE_STEREO_PANNING
    4815       39210 :     p_output[0] = st_ivas->p_output_f[0];
    4816             : #else
    4817             :     for ( ch = 0; ch < MAX_TRANSPORT_CHANNELS; ch++ )
    4818             :     {
    4819             :         p_output[ch] = st_ivas->p_output_f[ch];
    4820             :         if ( p_output[ch] != NULL )
    4821             :         {
    4822             :             set_zero( p_output[ch], L_FRAME48k );
    4823             :         }
    4824             :     }
    4825             : #endif
    4826             : 
    4827             :     /* run the main EVS decoding routine */
    4828       39210 :     if ( hCoreCoder[0]->codec_mode == MODE1 )
    4829             :     {
    4830       22086 :         if ( hCoreCoder[0]->Opt_AMR_WB )
    4831             :         {
    4832        4040 :             if ( ( error = amr_wb_dec( hCoreCoder[0], st_ivas->mem_hp20_out[0], p_output[0] ) ) != IVAS_ERR_OK )
    4833             :             {
    4834           0 :                 return error;
    4835             :             }
    4836             :         }
    4837             :         else
    4838             :         {
    4839       18046 :             if ( ( error = evs_dec( hCoreCoder[0], st_ivas->mem_hp20_out[0], p_output[0], FRAMEMODE_NORMAL ) ) != IVAS_ERR_OK )
    4840             :             {
    4841           0 :                 return error;
    4842             :             }
    4843             :         }
    4844             :     }
    4845             :     else
    4846             :     {
    4847       17124 :         if ( hCoreCoder[0]->bfi == 0 )
    4848             :         {
    4849       15762 :             if ( ( error = evs_dec( hCoreCoder[0], st_ivas->mem_hp20_out[0], p_output[0], FRAMEMODE_NORMAL ) ) != IVAS_ERR_OK )
    4850             :             {
    4851           0 :                 return error;
    4852             :             }
    4853             :         }
    4854        1362 :         else if ( hCoreCoder[0]->bfi == 2 )
    4855             :         {
    4856         184 :             if ( ( error = evs_dec( hCoreCoder[0], st_ivas->mem_hp20_out[0], p_output[0], FRAMEMODE_FUTURE ) ) != IVAS_ERR_OK )
    4857             :             {
    4858           0 :                 return error;
    4859             :             }
    4860             :         }
    4861             :         else
    4862             :         {
    4863        1178 :             if ( ( error = evs_dec( hCoreCoder[0], st_ivas->mem_hp20_out[0], p_output[0], FRAMEMODE_MISSING ) ) != IVAS_ERR_OK )
    4864             :             {
    4865           0 :                 return error;
    4866             :             }
    4867             :         }
    4868             :     }
    4869             : 
    4870       39210 :     st_ivas->BER_detect = hCoreCoder[0]->BER_detect;
    4871             : 
    4872             : #ifndef FIX_1435_MOVE_STEREO_PANNING
    4873             :     if ( st_ivas->renderer_type == RENDERER_NON_DIEGETIC_DOWNMIX )
    4874             :     {
    4875             :         mixer_left = ( st_ivas->hDecoderConfig->non_diegetic_pan_gain + 1.f ) * 0.5f;
    4876             :         mixer_rigth = 1.f - mixer_left;
    4877             :         v_multc( p_output[0], mixer_rigth, p_output[1], nOutSamples );
    4878             :         v_multc( p_output[0], mixer_left, p_output[0], nOutSamples );
    4879             :     }
    4880             : #endif
    4881       39210 :     if ( st_ivas->hDecoderConfig->Opt_tsm && st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_MONO )
    4882             :     {
    4883             :         /* BE workaround: in order to keep EVS bit-exact wrt. TS 26.443, convert 'float' output data to 'short' before the TSM */
    4884             :         int16_t pcm_buf_local[L_FRAME48k];
    4885             : #ifdef DEBUGGING
    4886             :         st_ivas->noClipping +=
    4887             : #endif
    4888       17910 :             ivas_syn_output( &p_output[0], nOutSamples, 1, pcm_buf_local );
    4889       17910 :         mvs2r( pcm_buf_local, p_output[0], nOutSamples );
    4890             :     }
    4891             : 
    4892       39210 :     return IVAS_ERR_OK;
    4893             : }
    4894             : 
    4895             : 
    4896             : #ifdef DEBUGGING
    4897             : /*---------------------------------------------------------------------*
    4898             :  * IVAS_DEC_GetBer_detect_flag()
    4899             :  *
    4900             :  * return BER_detect flag
    4901             :  *---------------------------------------------------------------------*/
    4902             : 
    4903             : bool IVAS_DEC_GetBerDetectFlag(
    4904             :     IVAS_DEC_HANDLE hIvasDec /* i  : IVAS decoder handle   */
    4905             : )
    4906             : {
    4907             :     if ( hIvasDec->st_ivas->BER_detect == 1 )
    4908             :     {
    4909             :         return 1;
    4910             :     }
    4911             :     else
    4912             :     {
    4913             :         return 0;
    4914             :     }
    4915             : }
    4916             : 
    4917             : 
    4918             : /*---------------------------------------------------------------------*
    4919             :  * IVAS_DEC_GetNoCLipping()
    4920             :  *
    4921             :  * return number of clipped samples
    4922             :  *---------------------------------------------------------------------*/
    4923             : 
    4924             : int32_t IVAS_DEC_GetNoCLipping(
    4925             :     IVAS_DEC_HANDLE hIvasDec /* i  : IVAS decoder handle   */
    4926             : )
    4927             : {
    4928             :     return hIvasDec->st_ivas->noClipping;
    4929             : }
    4930             : 
    4931             : 
    4932             : /*---------------------------------------------------------------------*
    4933             :  * IVAS_DEC_GetCntFramesLimited()
    4934             :  *
    4935             :  * return number of frames where limiter is applied
    4936             :  *---------------------------------------------------------------------*/
    4937             : 
    4938             : int32_t IVAS_DEC_GetCntFramesLimited(
    4939             :     IVAS_DEC_HANDLE hIvasDec /* i  : IVAS decoder handle   */
    4940             : )
    4941             : {
    4942             :     if ( hIvasDec->st_ivas->hLimiter == NULL )
    4943             :     {
    4944             :         return 0;
    4945             :     }
    4946             :     else
    4947             :     {
    4948             :         return hIvasDec->st_ivas->hLimiter->cnt_frames_limited;
    4949             :     }
    4950             : }
    4951             : 
    4952             : #ifdef DEBUG_SBA_AUDIO_DUMP
    4953             : /*---------------------------------------------------------------------*
    4954             :  * IVAS_DEC_GetSbaDebugParams( )
    4955             :  *
    4956             :  * Returns SBA debug parameters
    4957             :  *---------------------------------------------------------------------*/
    4958             : 
    4959             : ivas_error IVAS_DEC_GetSbaDebugParams(
    4960             :     const IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle             */
    4961             :     int16_t *numOutputChannels,
    4962             :     int16_t *numTransportChannels,
    4963             :     int16_t *pca_ingest_channels )
    4964             : {
    4965             :     if ( hIvasDec->st_ivas == NULL )
    4966             :     {
    4967             :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    4968             :     }
    4969             : 
    4970             :     if ( hIvasDec->st_ivas->ivas_format != SBA_FORMAT || hIvasDec->st_ivas->hSpar == NULL )
    4971             :     {
    4972             :         *numOutputChannels = 1;
    4973             :         *numTransportChannels = 1;
    4974             :         *pca_ingest_channels = 1;
    4975             :     }
    4976             :     else
    4977             :     {
    4978             :         *numOutputChannels = hIvasDec->st_ivas->hSpar->numOutChannels;
    4979             :         *numTransportChannels = hIvasDec->st_ivas->nchan_transport;
    4980             :         *pca_ingest_channels = hIvasDec->st_ivas->hSpar->pca_ingest_channels;
    4981             :     }
    4982             : 
    4983             :     return IVAS_ERR_OK;
    4984             : }
    4985             : #endif /* DEBUG_SBA_AUDIO_DUMP */
    4986             : 
    4987             : #endif /* DEBUGGING */
    4988             : 
    4989             : 
    4990             : /*---------------------------------------------------------------------*
    4991             :  * input_format_API_to_internal()
    4992             :  *
    4993             :  *
    4994             :  *---------------------------------------------------------------------*/
    4995             : 
    4996       38234 : static ivas_error input_format_API_to_internal(
    4997             :     IVAS_DEC_INPUT_FORMAT input_format,
    4998             :     int16_t *bitstream_format_internal,
    4999             :     int16_t *sdp_hf_only,
    5000             :     const bool is_voip_enabled )
    5001             : {
    5002       38234 :     switch ( input_format )
    5003             :     {
    5004       38186 :         case IVAS_DEC_INPUT_FORMAT_G192:
    5005       38186 :             *bitstream_format_internal = is_voip_enabled ? VOIP_G192_RTP : G192;
    5006       38186 :             *sdp_hf_only = 0;
    5007       38186 :             break;
    5008           0 :         case IVAS_DEC_INPUT_FORMAT_MIME:
    5009           0 :             *bitstream_format_internal = MIME;
    5010           0 :             *sdp_hf_only = 0;
    5011           0 :             break;
    5012           0 :         case IVAS_DEC_INPUT_FORMAT_RTPDUMP:
    5013           0 :             assert( is_voip_enabled && "RTP dump only supported in VoIP mode" );
    5014           0 :             *bitstream_format_internal = VOIP_RTPDUMP;
    5015           0 :             *sdp_hf_only = 0;
    5016           0 :             break;
    5017          48 :         case IVAS_DEC_INPUT_FORMAT_RTPDUMP_HF:
    5018          48 :             assert( is_voip_enabled && "RTP dump only supported in VoIP mode" );
    5019          48 :             *bitstream_format_internal = VOIP_RTPDUMP;
    5020          48 :             *sdp_hf_only = 1;
    5021          48 :             break;
    5022           0 :         default:
    5023           0 :             return IVAS_ERR_INVALID_BITSTREAM;
    5024             :             break;
    5025             :     }
    5026             : 
    5027       38234 :     return IVAS_ERR_OK;
    5028             : }
    5029             : 
    5030             : 
    5031             : /*---------------------------------------------------------------------*
    5032             :  * apa_setup()
    5033             :  *
    5034             :  * Setup APA decoder
    5035             :  *---------------------------------------------------------------------*/
    5036             : 
    5037       53413 : static ivas_error apa_setup(
    5038             :     IVAS_DEC_HANDLE hIvasDec,
    5039             :     const bool isInitialized_voip,
    5040             :     const uint16_t nTransportChannels )
    5041             : {
    5042             :     uint16_t l_ts;
    5043             : 
    5044       53413 :     l_ts = (uint16_t) hIvasDec->st_ivas->hTcBuffer->n_samples_granularity;
    5045             : 
    5046       53413 :     if ( !isInitialized_voip )
    5047             :     {
    5048             :         DECODER_CONFIG_HANDLE hDecoderConfig;
    5049             :         uint16_t wss, css;
    5050             :         float startQuality;
    5051             : 
    5052       38213 :         startQuality = hIvasDec->tsm_quality;
    5053       38213 :         hDecoderConfig = hIvasDec->st_ivas->hDecoderConfig;
    5054             : 
    5055       38213 :         if ( hDecoderConfig->output_Fs == 8000 )
    5056             :         {
    5057          16 :             wss = 1;
    5058          16 :             css = 1;
    5059             :         }
    5060       38197 :         else if ( hDecoderConfig->output_Fs == 16000 )
    5061             :         {
    5062       12783 :             wss = 2;
    5063       12783 :             css = 1;
    5064             :         }
    5065       25414 :         else if ( hDecoderConfig->output_Fs == 32000 )
    5066             :         {
    5067       12801 :             wss = 4;
    5068       12801 :             css = 2;
    5069             :         }
    5070       12613 :         else if ( hDecoderConfig->output_Fs == 48000 )
    5071             :         {
    5072       12613 :             wss = 6;
    5073       12613 :             css = 3;
    5074             :         }
    5075             :         else
    5076             :         {
    5077           0 :             return IVAS_ERR_INIT_ERROR;
    5078             :         }
    5079             : 
    5080       76426 :         if ( apa_init( &hIvasDec->hTimeScaler, nTransportChannels ) != IVAS_ERR_OK ||
    5081       76426 :              apa_set_rate( hIvasDec->hTimeScaler, hDecoderConfig->output_Fs ) != 0 ||
    5082       76426 :              apa_set_complexity_options( hIvasDec->hTimeScaler, wss, css ) != 0 ||
    5083       76426 :              apa_set_quality( hIvasDec->hTimeScaler, startQuality, 4, 4 ) != 0 ||
    5084       38213 :              apa_set_renderer_granularity( hIvasDec->hTimeScaler, l_ts ) != 0 )
    5085             :         {
    5086           0 :             return IVAS_ERR_INIT_ERROR;
    5087             :         }
    5088             : 
    5089       38213 :         if ( hIvasDec->mode == IVAS_DEC_MODE_EVS )
    5090             :         {
    5091         246 :             if ( apa_set_evs_compat_mode( hIvasDec->hTimeScaler, true ) != 0 )
    5092             :             {
    5093           0 :                 return IVAS_ERR_INIT_ERROR;
    5094             :             }
    5095             :         }
    5096             :     }
    5097             :     else
    5098             :     {
    5099       15200 :         if ( apa_reconfigure( hIvasDec->hTimeScaler, nTransportChannels, l_ts ) != 0 )
    5100             :         {
    5101           0 :             return IVAS_ERR_INIT_ERROR;
    5102             :         }
    5103             :     }
    5104             : 
    5105       53413 :     hIvasDec->nTransportChannelsOld = nTransportChannels;
    5106             : 
    5107       53413 :     return IVAS_ERR_OK;
    5108             : }
    5109             : 
    5110             : 
    5111             : /*---------------------------------------------------------------------*
    5112             :  * IVAS_DEC_GetSplitRendBitstreamHeader()
    5113             :  *
    5114             :  *
    5115             :  *---------------------------------------------------------------------*/
    5116             : 
    5117        2088 : ivas_error IVAS_DEC_GetSplitRendBitstreamHeader(
    5118             :     IVAS_DEC_HANDLE hIvasDec,                             /* i/o: IVAS decoder handle                   */
    5119             :     ISAR_SPLIT_REND_CODEC *pCodec,                        /* o  : pointer to codec setting              */
    5120             :     ISAR_SPLIT_REND_POSE_CORRECTION_MODE *poseCorrection, /* o  : pointer to pose correction mode       */
    5121             :     int16_t *pIsar_frame_size_ms,                         /* o  : pointer to ISAR frame size setting    */
    5122             :     int16_t *pCodec_frame_size_ms,                        /* o  : pointer to codec frame size setting   */
    5123             :     int16_t *pLc3plusHighRes                              /* o  : pointer to LC3plus High-Res setting   */
    5124             : )
    5125             : {
    5126        2088 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    5127             :     {
    5128           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    5129             :     }
    5130             : 
    5131        2088 :     *pCodec = hIvasDec->st_ivas->hRenderConfig->split_rend_config.codec;
    5132        2088 :     *pCodec_frame_size_ms = hIvasDec->st_ivas->hRenderConfig->split_rend_config.codec_frame_size_ms;
    5133        2088 :     *poseCorrection = hIvasDec->st_ivas->hRenderConfig->split_rend_config.poseCorrectionMode;
    5134        2088 :     *pIsar_frame_size_ms = hIvasDec->st_ivas->hRenderConfig->split_rend_config.isar_frame_size_ms;
    5135        2088 :     *pLc3plusHighRes = hIvasDec->st_ivas->hRenderConfig->split_rend_config.lc3plus_highres;
    5136             : 
    5137        2088 :     return IVAS_ERR_OK;
    5138             : }
    5139             : 
    5140             : 
    5141             : /*---------------------------------------------------------------------*
    5142             :  * IVAS_DEC_GetCldfbSamples()
    5143             :  *
    5144             :  * API function to output CLDFB samples
    5145             :  *---------------------------------------------------------------------*/
    5146             : 
    5147           0 : ivas_error IVAS_DEC_GetCldfbSamples(
    5148             :     IVAS_DEC_HANDLE hIvasDec,   /* i/o: IVAS decoder handle                                    */
    5149             :     float *out_real,            /* o  : buffer for decoded PCM real output in CLDFB domain     */
    5150             :     float *out_imag,            /* o  : buffer for decoded PCM imag output in CLDFB domain     */
    5151             :     AUDIO_CONFIG *audio_config, /* o  : audio configuration                                    */
    5152             :     int16_t *nOutSamples        /* o  : number of samples per channel written to output buffer */
    5153             : )
    5154             : {
    5155             :     ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE hSplitBinRend;
    5156             :     int16_t ch, b, slot_idx, num_chs, maxBand, num_samples;
    5157             : 
    5158           0 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL || hIvasDec->st_ivas->hSplitBinRend == NULL )
    5159             :     {
    5160           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    5161             :     }
    5162             : 
    5163           0 :     hSplitBinRend = hIvasDec->st_ivas->hSplitBinRend;
    5164           0 :     num_samples = 0;
    5165             : 
    5166           0 :     if ( hSplitBinRend->hCldfbDataOut != NULL )
    5167             :     {
    5168           0 :         *audio_config = hSplitBinRend->hCldfbDataOut->config;
    5169           0 :         if ( hSplitBinRend->hCldfbDataOut->config != IVAS_AUDIO_CONFIG_INVALID )
    5170             :         {
    5171           0 :             num_chs = audioCfg2channels( hSplitBinRend->hCldfbDataOut->config );
    5172           0 :             maxBand = (int16_t) ( ( CLDFB_NO_CHANNELS_MAX * hIvasDec->st_ivas->hDecoderConfig->output_Fs ) / 48000 );
    5173             : 
    5174           0 :             for ( slot_idx = 0; slot_idx < CLDFB_NO_COL_MAX; slot_idx++ )
    5175             :             {
    5176           0 :                 for ( b = 0; b < maxBand; b++ )
    5177             :                 {
    5178           0 :                     for ( ch = 0; ch < num_chs; ch++ )
    5179             :                     {
    5180           0 :                         *out_real++ = hSplitBinRend->hCldfbDataOut->Cldfb_RealBuffer[ch][slot_idx][b];
    5181           0 :                         *out_imag++ = hSplitBinRend->hCldfbDataOut->Cldfb_ImagBuffer[ch][slot_idx][b];
    5182             :                     }
    5183             :                 }
    5184             :             }
    5185           0 :             num_samples = CLDFB_NO_COL_MAX * maxBand;
    5186             :         }
    5187             :     }
    5188             :     else
    5189             :     {
    5190           0 :         *audio_config = IVAS_AUDIO_CONFIG_INVALID;
    5191             :     }
    5192             : 
    5193           0 :     *nOutSamples = num_samples;
    5194             : 
    5195           0 :     return IVAS_ERR_OK;
    5196             : }
    5197             : 
    5198             : 
    5199             : /*---------------------------------------------------------------------*
    5200             :  * pcm_buffer_offset()
    5201             :  *
    5202             :  *
    5203             :  *---------------------------------------------------------------------*/
    5204             : 
    5205    10572619 : static void *pcm_buffer_offset(
    5206             :     void *buffer,
    5207             :     const IVAS_DEC_PCM_TYPE pcmType,
    5208             :     const int32_t offset )
    5209             : {
    5210    10572619 :     switch ( pcmType )
    5211             :     {
    5212      347454 :         case IVAS_DEC_PCM_FLOAT:
    5213             :         {
    5214      347454 :             float *tmpBuf = (float *) buffer;
    5215      347454 :             return (void *) ( tmpBuf + offset );
    5216             :         }
    5217             :         break;
    5218    10225165 :         case IVAS_DEC_PCM_INT16:
    5219             :         {
    5220    10225165 :             int16_t *tmpBuf = (int16_t *) buffer;
    5221    10225165 :             return (void *) ( tmpBuf + offset );
    5222             :         }
    5223             :         break;
    5224           0 :         default:
    5225           0 :             break;
    5226             :     }
    5227             : 
    5228           0 :     return NULL;
    5229             : }
    5230             : 
    5231             : 
    5232             : /*---------------------------------------------------------------------*
    5233             :  * set_pcm_buffer_to_zero()
    5234             :  *
    5235             :  *
    5236             :  *---------------------------------------------------------------------*/
    5237             : 
    5238      418678 : static ivas_error set_pcm_buffer_to_zero(
    5239             :     void *buffer,
    5240             :     const IVAS_DEC_PCM_TYPE pcmType,
    5241             :     const int16_t nZeroSamples )
    5242             : {
    5243             :     ivas_error error;
    5244             : 
    5245      418678 :     error = IVAS_ERR_OK;
    5246      418678 :     switch ( pcmType )
    5247             :     {
    5248           0 :         case IVAS_DEC_PCM_FLOAT:
    5249           0 :             set_zero( (float *) buffer, nZeroSamples );
    5250           0 :             break;
    5251      418678 :         case IVAS_DEC_PCM_INT16:
    5252      418678 :             set_s( (int16_t *) buffer, 0, nZeroSamples );
    5253      418678 :             break;
    5254           0 :         default:
    5255           0 :             error = IVAS_ERR_INTERNAL;
    5256             :     }
    5257             : 
    5258      418678 :     return error;
    5259             : }
    5260             : 
    5261             : 
    5262             : /*---------------------------------------------------------------------*
    5263             :  * pcm_type_API_to_internal()
    5264             :  *
    5265             :  *
    5266             :  *---------------------------------------------------------------------*/
    5267             : 
    5268     7940978 : PCM_RESOLUTION pcm_type_API_to_internal(
    5269             :     const IVAS_DEC_PCM_TYPE pcmType )
    5270             : {
    5271             :     PCM_RESOLUTION pcm_resolution;
    5272     7940978 :     pcm_resolution = PCM_NOT_KNOW;
    5273             : 
    5274     7940978 :     switch ( pcmType )
    5275             :     {
    5276      347454 :         case IVAS_DEC_PCM_FLOAT:
    5277      347454 :             pcm_resolution = PCM_FLOAT32;
    5278      347454 :             break;
    5279     7593524 :         case IVAS_DEC_PCM_INT16:
    5280     7593524 :             pcm_resolution = PCM_INT16;
    5281     7593524 :             break;
    5282           0 :         default:
    5283           0 :             pcm_resolution = PCM_NOT_KNOW;
    5284             :     }
    5285             : 
    5286     7940978 :     return pcm_resolution;
    5287             : }
    5288             : 
    5289             : 
    5290             : /*-------------------------------------------------------------------*
    5291             :  * ivas_create_handle_isar()
    5292             :  *
    5293             :  * Initialize IVAS decoder split-rendering handle
    5294             :  *-------------------------------------------------------------------*/
    5295             : 
    5296        2088 : static ivas_error ivas_create_handle_isar(
    5297             :     ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE *hSplitBinRend_out /* o  : ISAR split binaural rendering handle */
    5298             : )
    5299             : {
    5300             :     ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE hSplitBinRend;
    5301             :     int16_t i;
    5302             : 
    5303        2088 :     if ( ( hSplitBinRend = (ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE) malloc( sizeof( ISAR_DEC_SPLIT_REND_WRAPPER ) ) ) == NULL )
    5304             :     {
    5305           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for IVAS decoder handle" );
    5306             :     }
    5307             : 
    5308        2088 :     isar_init_split_rend_handles( &hSplitBinRend->splitrend );
    5309             : 
    5310        2088 :     hSplitBinRend->hMultiBinTdData = NULL;
    5311       35496 :     for ( i = 0; i < MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS; ++i )
    5312             :     {
    5313       33408 :         hSplitBinRend->hMultiBinCldfbData[i] = NULL;
    5314             :     }
    5315        2088 :     hSplitBinRend->hCldfbDataOut = NULL;
    5316        2088 :     hSplitBinRend->numTdSamplesPerChannelCached = 0;
    5317             : 
    5318        2088 :     *hSplitBinRend_out = hSplitBinRend;
    5319             : 
    5320        2088 :     return IVAS_ERR_OK;
    5321             : }
    5322             : 
    5323             : 
    5324             : /*-------------------------------------------------------------------*
    5325             :  * ivas_destroy_handle_isar()
    5326             :  *
    5327             :  * destroy IVAS decoder split rend handle
    5328             :  *-------------------------------------------------------------------*/
    5329             : 
    5330       86054 : static void ivas_destroy_handle_isar(
    5331             :     ISAR_DEC_SPLIT_REND_WRAPPER_HANDLE *hSplitBinRend /* i/o: ISAR split binaural rendering handle */
    5332             : )
    5333             : {
    5334             :     int16_t i;
    5335             : 
    5336       86054 :     if ( *hSplitBinRend != NULL )
    5337             :     {
    5338        2088 :         if ( ( *hSplitBinRend )->hMultiBinTdData != NULL )
    5339             :         {
    5340         464 :             ivas_TD_RINGBUF_Close( &( *hSplitBinRend )->hMultiBinTdData );
    5341             :         }
    5342       35496 :         for ( i = 0; i < MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS; ++i )
    5343             :         {
    5344       33408 :             if ( ( *hSplitBinRend )->hMultiBinCldfbData[i] != NULL )
    5345             :             {
    5346       12720 :                 ivas_CLDFB_RINGBUF_Close( &( *hSplitBinRend )->hMultiBinCldfbData[i] );
    5347             :             }
    5348             :         }
    5349             : 
    5350        2088 :         ISAR_PRE_REND_close( &( *hSplitBinRend )->splitrend, NULL );
    5351             : 
    5352        2088 :         if ( ( *hSplitBinRend )->hCldfbDataOut != NULL )
    5353             :         {
    5354         508 :             free( ( *hSplitBinRend )->hCldfbDataOut );
    5355         508 :             ( *hSplitBinRend )->hCldfbDataOut = NULL;
    5356             :         }
    5357             : 
    5358        2088 :         free( ( *hSplitBinRend ) );
    5359        2088 :         ( *hSplitBinRend ) = NULL;
    5360             :     }
    5361             : 
    5362       86054 :     return;
    5363             : }
    5364             : 
    5365             : 
    5366             : /*---------------------------------------------------------------------*
    5367             :  * IVAS_DEC_is_split_rendering_enabled()
    5368             :  *
    5369             :  *
    5370             :  *---------------------------------------------------------------------*/
    5371             : 
    5372             : /*! r: decoder error code */
    5373      172066 : ivas_error IVAS_DEC_is_split_rendering_enabled(
    5374             :     IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle                            */
    5375             :     int16_t *isSplitRend      /* o  : flag to indicate if split rendering is enabled */
    5376             : )
    5377             : {
    5378             :     Decoder_Struct *st_ivas;
    5379             : 
    5380      172066 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    5381             :     {
    5382           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    5383             :     }
    5384             : 
    5385      172066 :     st_ivas = hIvasDec->st_ivas;
    5386             : 
    5387      172066 :     *isSplitRend = is_split_rendering_enabled( st_ivas->hDecoderConfig, st_ivas->hRenderConfig );
    5388             : 
    5389      172066 :     return IVAS_ERR_OK;
    5390             : }
    5391             : 
    5392             : 
    5393             : /*-------------------------------------------------------------------*
    5394             :  * ivas_dec_reconfig_split_rend()
    5395             :  *
    5396             :  * IVAS decoder split rend reconfig
    5397             :  *-------------------------------------------------------------------*/
    5398             : 
    5399      286028 : static ivas_error ivas_dec_reconfig_split_rend(
    5400             :     Decoder_Struct *st_ivas /* i  : IVAS decoder structure      */
    5401             : )
    5402             : {
    5403             :     ivas_error error;
    5404             :     int16_t cldfb_in_flag, num_ch, ch, isCldfbNeeded, i, pcm_out_flag;
    5405             :     SPLIT_REND_WRAPPER *hSplitRendWrapper;
    5406             : 
    5407      286028 :     hSplitRendWrapper = &st_ivas->hSplitBinRend->splitrend;
    5408      286028 :     pcm_out_flag = ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0;
    5409      286028 :     cldfb_in_flag = 0;
    5410             : 
    5411      286028 :     if ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV ||
    5412      187777 :          st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
    5413      187777 :          st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC ||
    5414       78804 :          st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
    5415             :     {
    5416      207224 :         cldfb_in_flag = 1;
    5417             :     }
    5418             : 
    5419      286028 :     ISAR_PRE_REND_GetMultiBinPoseData( &st_ivas->hRenderConfig->split_rend_config, &hSplitRendWrapper->multiBinPoseData, ( st_ivas->hHeadTrackData != NULL ) ? st_ivas->hHeadTrackData->sr_pose_pred_axis : DEFAULT_AXIS );
    5420             : 
    5421      286028 :     isCldfbNeeded = 0;
    5422             : 
    5423      286028 :     if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV && st_ivas->ivas_format == SBA_ISM_FORMAT ) ||
    5424      241664 :          ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
    5425             :     {
    5426       73868 :         cldfb_in_flag = 0;
    5427             :     }
    5428             : 
    5429      286028 :     if ( st_ivas->renderer_type != RENDERER_DISABLE )
    5430             :     {
    5431      286028 :         if ( cldfb_in_flag == 0 )
    5432             :         {
    5433      152672 :             isCldfbNeeded = 1;
    5434             :         }
    5435      133356 :         else if ( st_ivas->hRenderConfig->split_rend_config.codec == ISAR_SPLIT_REND_CODEC_LC3PLUS && cldfb_in_flag )
    5436             :         {
    5437       34103 :             isCldfbNeeded = 1;
    5438             :         }
    5439       99253 :         else if ( pcm_out_flag && cldfb_in_flag )
    5440             :         {
    5441        2350 :             isCldfbNeeded = 1;
    5442             :         }
    5443             :     }
    5444           0 :     else if ( st_ivas->hDecoderConfig->Opt_non_diegetic_pan )
    5445             :     {
    5446           0 :         isCldfbNeeded = 1;
    5447             :     }
    5448             : 
    5449      286028 :     if ( isCldfbNeeded == 1 && hSplitRendWrapper->hCldfbHandles == NULL )
    5450             :     {
    5451           0 :         if ( ( hSplitRendWrapper->hCldfbHandles = (CLDFB_HANDLES_WRAPPER_HANDLE) malloc( sizeof( CLDFB_HANDLES_WRAPPER ) ) ) == NULL )
    5452             :         {
    5453           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CLDFB handles\n" ) );
    5454             :         }
    5455             : 
    5456           0 :         num_ch = MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS;
    5457           0 :         for ( ch = 0; ch < num_ch; ch++ )
    5458             :         {
    5459           0 :             hSplitRendWrapper->hCldfbHandles->cldfbAna[ch] = NULL;
    5460             :         }
    5461             : 
    5462           0 :         num_ch = hSplitRendWrapper->multiBinPoseData.num_poses * BINAURAL_CHANNELS;
    5463             : 
    5464           0 :         for ( ch = 0; ch < num_ch; ch++ )
    5465             :         {
    5466           0 :             if ( ( error = openCldfb( &( hSplitRendWrapper->hCldfbHandles->cldfbAna[ch] ), CLDFB_ANALYSIS, st_ivas->hDecoderConfig->output_Fs, CLDFB_PROTOTYPE_5_00MS ) ) != IVAS_ERR_OK )
    5467             :             {
    5468           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not open CLDFB handles\n" ) );
    5469             :             }
    5470             :         }
    5471             : 
    5472           0 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    5473             :         {
    5474           0 :             if ( ( error = openCldfb( &( hSplitRendWrapper->hCldfbHandles->cldfbSyn[ch] ), CLDFB_SYNTHESIS, st_ivas->hDecoderConfig->output_Fs, CLDFB_PROTOTYPE_5_00MS ) ) != IVAS_ERR_OK )
    5475             :             {
    5476           0 :                 return error;
    5477             :             }
    5478             :         }
    5479             :     }
    5480      286028 :     else if ( isCldfbNeeded == 0 && hSplitRendWrapper->hCldfbHandles != NULL )
    5481             :     {
    5482           0 :         num_ch = MAX_HEAD_ROT_POSES * BINAURAL_CHANNELS;
    5483           0 :         for ( ch = 0; ch < num_ch; ch++ )
    5484             :         {
    5485           0 :             if ( hSplitRendWrapper->hCldfbHandles->cldfbAna[ch] != NULL )
    5486             :             {
    5487           0 :                 deleteCldfb( &hSplitRendWrapper->hCldfbHandles->cldfbAna[ch] );
    5488           0 :                 hSplitRendWrapper->hCldfbHandles->cldfbAna[ch] = NULL;
    5489             :             }
    5490             :         }
    5491             : 
    5492           0 :         for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
    5493             :         {
    5494           0 :             if ( hSplitRendWrapper->hCldfbHandles->cldfbSyn[ch] != NULL )
    5495             :             {
    5496           0 :                 deleteCldfb( &hSplitRendWrapper->hCldfbHandles->cldfbSyn[ch] );
    5497           0 :                 hSplitRendWrapper->hCldfbHandles->cldfbSyn[ch] = NULL;
    5498             :             }
    5499             :         }
    5500             : 
    5501           0 :         free( hSplitRendWrapper->hCldfbHandles );
    5502           0 :         hSplitRendWrapper->hCldfbHandles = NULL;
    5503             :     }
    5504             : 
    5505      286028 :     if ( ( st_ivas->renderer_type != RENDERER_BINAURAL_OBJECTS_TD ) &&
    5506      214838 :          ( st_ivas->renderer_type != RENDERER_BINAURAL_FASTCONV || st_ivas->ivas_format != SBA_ISM_FORMAT ) &&
    5507      170474 :          !( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
    5508             :     {
    5509     1127760 :         for ( i = 0; i < MAX_HEAD_ROT_POSES - 1; ++i )
    5510             :         {
    5511      986790 :             if ( st_ivas->hTdRendHandles[i] != NULL )
    5512             :             {
    5513           0 :                 st_ivas->hTdRendHandles[i]->HrFiltSet_p = NULL;
    5514           0 :                 ivas_td_binaural_close( &st_ivas->hTdRendHandles[i] );
    5515             :             }
    5516             :         }
    5517             :     }
    5518             : 
    5519      286028 :     return IVAS_ERR_OK;
    5520             : }
    5521             : 
    5522             : 
    5523             : /*-------------------------------------------------------------------*
    5524             :  * ivas_dec_split_rend_cldfb_in()
    5525             :  *
    5526             :  *
    5527             :  *-------------------------------------------------------------------*/
    5528             : 
    5529      651365 : static int16_t ivas_dec_split_rend_cldfb_in(
    5530             :     const RENDERER_TYPE renderer_type /* i  : renderer type     */
    5531             : )
    5532             : {
    5533      651365 :     if ( renderer_type == RENDERER_BINAURAL_FASTCONV ||
    5534      416853 :          renderer_type == RENDERER_BINAURAL_FASTCONV_ROOM ||
    5535      179601 :          renderer_type == RENDERER_BINAURAL_PARAMETRIC ||
    5536             :          renderer_type == RENDERER_BINAURAL_PARAMETRIC_ROOM )
    5537             :     {
    5538      471764 :         return 1;
    5539             :     }
    5540             :     else
    5541             :     {
    5542      179601 :         return 0;
    5543             :     }
    5544             : }
    5545             : 
    5546             : 
    5547             : /*-------------------------------------------------------------------*
    5548             :  * ivas_dec_init_split_rend()
    5549             :  *
    5550             :  * IVAS decoder split rendering initialization
    5551             :  *-------------------------------------------------------------------*/
    5552             : 
    5553        2088 : static ivas_error ivas_dec_init_split_rend(
    5554             :     Decoder_Struct *st_ivas /* i  : IVAS decoder structure      */
    5555             : )
    5556             : {
    5557             :     ivas_error error;
    5558             :     int16_t cldfb_in_flag, pcm_out_flag;
    5559             :     int16_t mixed_td_cldfb_flag;
    5560             :     int16_t i, num_poses;
    5561             : 
    5562        2088 :     pcm_out_flag = ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM ) ? 1 : 0;
    5563        2088 :     cldfb_in_flag = 0;
    5564             : 
    5565        2088 :     cldfb_in_flag = ivas_dec_split_rend_cldfb_in( st_ivas->renderer_type );
    5566             : 
    5567        2088 :     ISAR_PRE_REND_GetMultiBinPoseData( &st_ivas->hRenderConfig->split_rend_config, &st_ivas->hSplitBinRend->splitrend.multiBinPoseData, ( st_ivas->hHeadTrackData != NULL ) ? st_ivas->hHeadTrackData->sr_pose_pred_axis : DEFAULT_AXIS );
    5568             : 
    5569        2088 :     num_poses = st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses;
    5570        2088 :     assert( num_poses <= MAX_HEAD_ROT_POSES );
    5571             : 
    5572        2088 :     if ( cldfb_in_flag )
    5573             :     {
    5574       14344 :         for ( i = 0; i < num_poses * BINAURAL_CHANNELS; ++i )
    5575             :         {
    5576             :             /* note: this is intra-frame heap memory */
    5577       12720 :             if ( ( error = ivas_CLDFB_RINGBUF_Open( &st_ivas->hSplitBinRend->hMultiBinCldfbData[i], CLDFB_NO_COL_MAX ) ) != IVAS_ERR_OK )
    5578             :             {
    5579           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for split rendering structure" );
    5580             :             }
    5581             :         }
    5582             :     }
    5583             :     else
    5584             :     {
    5585         464 :         if ( ( error = ivas_TD_RINGBUF_Open( &st_ivas->hSplitBinRend->hMultiBinTdData, get_render_frame_size_samples( st_ivas->hDecoderConfig ), num_poses * BINAURAL_CHANNELS ) ) != IVAS_ERR_OK )
    5586             :         {
    5587           0 :             return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for split rendering structure" );
    5588             :         }
    5589             :     }
    5590             : 
    5591        2088 :     if ( cldfb_in_flag == 1 && ( st_ivas->hSplitBinRend->splitrend.multiBinPoseData.poseCorrectionMode == ISAR_SPLIT_REND_POSE_CORRECTION_MODE_NONE ) )
    5592             :     {
    5593         508 :         if ( ( st_ivas->hSplitBinRend->hCldfbDataOut = (ISAR_DEC_SPLIT_REND_CLDFB_OUT_DATA_HANDLE) malloc( sizeof( ISAR_DEC_SPLIT_REND_CLDFB_OUT_DATA ) ) ) == NULL )
    5594             :         {
    5595           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for cldfb data out buffer\n" ) );
    5596             :         }
    5597             :     }
    5598             : 
    5599        2088 :     mixed_td_cldfb_flag = 0;
    5600        2088 :     if ( ( st_ivas->renderer_type == RENDERER_BINAURAL_FASTCONV && st_ivas->ivas_format == SBA_ISM_FORMAT ) ||
    5601        1814 :          ( st_ivas->renderer_type == RENDERER_BINAURAL_PARAMETRIC && st_ivas->ivas_format == MASA_ISM_FORMAT && st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
    5602             :     {
    5603         552 :         mixed_td_cldfb_flag = 1;
    5604             :     }
    5605             : 
    5606        2088 :     error = ISAR_PRE_REND_open( &st_ivas->hSplitBinRend->splitrend, &st_ivas->hRenderConfig->split_rend_config, st_ivas->hDecoderConfig->output_Fs, cldfb_in_flag, pcm_out_flag, (int16_t) st_ivas->hDecoderConfig->render_framesize, mixed_td_cldfb_flag );
    5607             : 
    5608        2088 :     return error;
    5609             : }
    5610             : 
    5611             : 
    5612             : /*---------------------------------------------------------------------*
    5613             :  * IVAS_DEC_is_split_rendering_coded_out()
    5614             :  *
    5615             :  * Return flag to indicate if split rendering is enabled
    5616             :  *---------------------------------------------------------------------*/
    5617             : 
    5618             : /*! r: decoder error code */
    5619      172066 : ivas_error IVAS_DEC_is_split_rendering_coded_out(
    5620             :     IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle                            */
    5621             :     int16_t *isSplitCoded     /* o  : flag to indicate if split rendering is enabled */
    5622             : )
    5623             : {
    5624             :     Decoder_Struct *st_ivas;
    5625             : 
    5626      172066 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    5627             :     {
    5628           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    5629             :     }
    5630             : 
    5631      172066 :     st_ivas = hIvasDec->st_ivas;
    5632             : 
    5633      172066 :     *isSplitCoded = 0;
    5634             : 
    5635      172066 :     if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED ||
    5636      167962 :          ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_STEREO && st_ivas->hDecoderConfig->Opt_non_diegetic_pan && st_ivas->hRenderConfig->split_rend_config.dof == 0 ) )
    5637             :     {
    5638        4104 :         *isSplitCoded = 1;
    5639             :     }
    5640             : 
    5641      172066 :     return IVAS_ERR_OK;
    5642             : }
    5643             : 
    5644             : 
    5645             : /*---------------------------------------------------------------------*
    5646             :  * feedSinglePIorientation( )
    5647             :  *
    5648             :  * Feed a single orientation PI data to external orientation handle.
    5649             :  *---------------------------------------------------------------------*/
    5650             : 
    5651           0 : static ivas_error feedSinglePIorientation(
    5652             :     IVAS_DEC_HANDLE hIvasDec,    /* i/o: IVAS decoder handle                                     */
    5653             :     IVAS_QUATERNION *orientation /* i  : orientation for this PI type                            */
    5654             : )
    5655             : {
    5656             :     int16_t i;
    5657             :     ivas_error error;
    5658             :     Decoder_Struct *st_ivas;
    5659             :     IVAS_QUATERNION invOrientation;
    5660             : 
    5661           0 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    5662             :     {
    5663           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    5664             :     }
    5665             : 
    5666           0 :     st_ivas = hIvasDec->st_ivas;
    5667             : 
    5668           0 :     if ( !st_ivas->hExtOrientationData )
    5669             :     {
    5670           0 :         if ( ( error = ivas_external_orientation_open( &( st_ivas->hExtOrientationData ), st_ivas->hDecoderConfig->render_framesize ) ) != IVAS_ERR_OK )
    5671             :         {
    5672           0 :             return error;
    5673             :         }
    5674             :     }
    5675             : 
    5676           0 :     if ( !st_ivas->hCombinedOrientationData )
    5677             :     {
    5678           0 :         if ( ( error = ivas_combined_orientation_open( &( st_ivas->hCombinedOrientationData ), st_ivas->hDecoderConfig->output_Fs, st_ivas->hDecoderConfig->render_framesize ) ) != IVAS_ERR_OK )
    5679             :         {
    5680           0 :             return error;
    5681             :         }
    5682             :     }
    5683             : 
    5684           0 :     QuaternionInverse( *orientation, &invOrientation );
    5685             : 
    5686             :     /* use the new PI orientation or the previously saved orientation in processing */
    5687           0 :     for ( i = 0; i < st_ivas->hExtOrientationData->num_subframes; i++ )
    5688             :     {
    5689           0 :         QuaternionProduct( st_ivas->hExtOrientationData->Quaternions[i], invOrientation,
    5690           0 :                            &st_ivas->hExtOrientationData->Quaternions[i] );
    5691           0 :         st_ivas->hExtOrientationData->enableExternalOrientation[i] = true;
    5692             :     }
    5693             : 
    5694           0 :     hIvasDec->updateOrientation = true;
    5695             : 
    5696           0 :     return IVAS_ERR_OK;
    5697             : }
    5698             : 
    5699             : 
    5700             : /*---------------------------------------------------------------------*
    5701             :  * setDiegeticInput( )
    5702             :  *
    5703             :  * Set isDiegeticInput flag for combined orientation handle based on PI data.
    5704             :  *---------------------------------------------------------------------*/
    5705             : 
    5706           0 : static void setDiegeticInputPI(
    5707             :     Decoder_Struct *st_ivas,     /* i/o: IVAS decoder handle                    */
    5708             :     const bool *diegeticPIValues /* i  : diegetic values for the input stream   */
    5709             : )
    5710             : {
    5711             :     int16_t i;
    5712             : 
    5713           0 :     if ( st_ivas->hCombinedOrientationData != NULL )
    5714             :     {
    5715           0 :         for ( i = 0; i < ( 1 + IVAS_MAX_NUM_OBJECTS ); i++ )
    5716             :         {
    5717           0 :             st_ivas->hCombinedOrientationData->isDiegeticInputPI[i] = diegeticPIValues[i];
    5718             :         }
    5719             : 
    5720           0 :         st_ivas->hCombinedOrientationData->isDiegeticInputPISet = true;
    5721             :     }
    5722             : 
    5723           0 :     return;
    5724             : }
    5725             : 
    5726             : 
    5727             : /*---------------------------------------------------------------------*
    5728             :  * IVAS_DEC_FeedPiDataToDecoder( )
    5729             :  *
    5730             :  *
    5731             :  *---------------------------------------------------------------------*/
    5732             : 
    5733           0 : ivas_error IVAS_DEC_FeedPiDataToDecoder(
    5734             :     IVAS_DEC_HANDLE hIvasDec, /* i/o: IVAS decoder handle                       */
    5735             :     IVAS_PIDATA_TS *piData,   /* i  : PI data received in rtp packet            */
    5736             :     uint32_t numPiData        /* i  : number of PI data received in rtp packet  */
    5737             : )
    5738             : {
    5739             :     uint32_t i;
    5740             :     Decoder_Struct *st_ivas;
    5741           0 :     ivas_error error = IVAS_ERR_OK;
    5742             : 
    5743           0 :     if ( hIvasDec == NULL || hIvasDec->st_ivas == NULL )
    5744             :     {
    5745           0 :         return IVAS_ERR_UNEXPECTED_NULL_POINTER;
    5746             :     }
    5747             : 
    5748           0 :     st_ivas = hIvasDec->st_ivas;
    5749             : 
    5750           0 :     for ( i = 0; i < numPiData; i++ )
    5751             :     {
    5752           0 :         uint32_t piDataType = piData->data.noPiData.piDataType;
    5753             : 
    5754           0 :         switch ( piDataType )
    5755             :         {
    5756           0 :             case IVAS_PI_SCENE_ORIENTATION:
    5757             :             {
    5758           0 :                 IVAS_QUATERNION *quat = &piData->data.scene.orientation;
    5759             : #ifdef DEBUGGING
    5760             :                 fprintf( stdout, "PI_SCENE_ORIENTATION : %f, %f, %f, %f\n", quat->w, quat->x, quat->y, quat->z );
    5761             : #endif
    5762           0 :                 error = feedSinglePIorientation( hIvasDec, quat );
    5763             :             }
    5764           0 :             break;
    5765             : 
    5766           0 :             case IVAS_PI_DEVICE_ORIENTATION_COMPENSATED:
    5767             :             {
    5768           0 :                 IVAS_QUATERNION *quat = &piData->data.deviceCompensated.orientation;
    5769             : #ifdef DEBUGGING
    5770             :                 fprintf( stdout, "PI_DEVICE_ORIENTATION : %f, %f, %f, %f\n", quat->w, quat->x, quat->y, quat->z );
    5771             : #endif
    5772           0 :                 error = feedSinglePIorientation( hIvasDec, quat );
    5773             :             }
    5774           0 :             break;
    5775             : 
    5776           0 :             case IVAS_PI_ACOUSTIC_ENVIRONMENT:
    5777             :             {
    5778           0 :                 uint16_t aeid = piData->data.acousticEnv.aeid;
    5779             : #ifdef DEBUGGING
    5780             :                 fprintf( stdout, "PI_ACOUSTIC_ENVIRONMENT : AEID : %d\n", aeid );
    5781             : #endif
    5782             : 
    5783           0 :                 if ( piData->data.acousticEnv.availLateReverb && st_ivas->hRenderConfig != NULL && aeid != st_ivas->hRenderConfig->roomAcoustics.aeID )
    5784             :                 {
    5785           0 :                     error = feedAcousticEnvPI( hIvasDec, piData->data.acousticEnv );
    5786             :                 }
    5787             :             }
    5788           0 :             break;
    5789             : 
    5790           0 :             case IVAS_PI_DIEGETIC_TYPE:
    5791             :             {
    5792             : #ifdef DEBUGGING
    5793             :                 fprintf( stdout, "PI_DIEGETIC_TYPE : %d, %d, %d, %d, %d\n", piData->data.digeticIndicator.isDiegetic[0], piData->data.digeticIndicator.isDiegetic[1], piData->data.digeticIndicator.isDiegetic[2], piData->data.digeticIndicator.isDiegetic[3], piData->data.digeticIndicator.isDiegetic[4] );
    5794             : #endif
    5795           0 :                 setDiegeticInputPI( st_ivas, piData->data.digeticIndicator.isDiegetic );
    5796             :             }
    5797           0 :             break;
    5798             : 
    5799           0 :             default:
    5800             :             {
    5801             :                 /* NOT HANDLED PI DATA  - DO NOTHING */
    5802             :             }
    5803           0 :             break;
    5804             :         }
    5805             : 
    5806           0 :         if ( error != IVAS_ERR_OK )
    5807             :         {
    5808           0 :             return error;
    5809             :         }
    5810             : 
    5811           0 :         piData++;
    5812             :     }
    5813             : 
    5814           0 :     return IVAS_ERR_OK;
    5815             : }

Generated by: LCOV version 1.14