LCOV - code coverage report
Current view: top level - lib_dec - lib_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ efe53129c9ed87a5067dd0a8fb9dca41db9c4add Lines: 1427 1848 77.2 %
Date: 2026-02-12 08:06:51 Functions: 84 94 89.4 %

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

Generated by: LCOV version 1.14