LCOV - code coverage report
Current view: top level - lib_dec - lib_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 1090 1610 67.7 %
Date: 2025-05-23 08:37:30 Functions: 67 81 82.7 %

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

Generated by: LCOV version 1.14