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

Generated by: LCOV version 1.14